2013-10-19 13:13:48 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <stm32f4xx.h>
|
|
|
|
#include <stm32f4xx_rcc.h>
|
|
|
|
#include <stm32f4xx_gpio.h>
|
|
|
|
|
2013-10-23 17:17:26 -04:00
|
|
|
#include "misc.h"
|
2014-01-02 16:30:26 -05:00
|
|
|
#include "mpconfig.h"
|
2014-01-21 16:40:13 -05:00
|
|
|
#include "qstr.h"
|
2013-10-23 17:17:26 -04:00
|
|
|
#include "systick.h"
|
2014-01-02 16:30:26 -05:00
|
|
|
#include "obj.h"
|
|
|
|
#include "runtime.h"
|
2014-02-16 16:41:57 -05:00
|
|
|
#include "accel.h"
|
2013-10-19 13:13:48 -04:00
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
#define ACCEL_ADDR (0x4c)
|
2014-01-02 16:30:26 -05:00
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
void accel_init(void) {
|
2013-10-19 13:13:48 -04:00
|
|
|
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; // enable I2C1
|
2013-10-23 17:17:26 -04:00
|
|
|
|
2013-10-19 13:13:48 -04:00
|
|
|
//gpio_pin_init(GPIOB, 6 /* B6 is SCL */, 2 /* AF mode */, 1 /* open drain output */, 1 /* 25 MHz */, 0 /* no pull up or pull down */);
|
|
|
|
//gpio_pin_init(GPIOB, 7 /* B7 is SDA */, 2 /* AF mode */, 1 /* open drain output */, 1 /* 25 MHz */, 0 /* no pull up or pull down */);
|
|
|
|
//gpio_pin_af(GPIOB, 6, 4 /* AF 4 for I2C1 */);
|
|
|
|
//gpio_pin_af(GPIOB, 7, 4 /* AF 4 for I2C1 */);
|
|
|
|
// XXX untested GPIO init! (was above code)
|
2013-10-23 17:17:26 -04:00
|
|
|
|
2013-10-19 13:13:48 -04:00
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
2013-10-23 17:17:26 -04:00
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
// PB5 is connected to AVDD; pull high to enable MMA accel device
|
2013-10-23 17:17:26 -04:00
|
|
|
GPIOB->BSRRH = GPIO_Pin_5; // PB5 low to start with
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
// PB6=SCL, PB7=SDA
|
2013-10-19 13:13:48 -04:00
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
|
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
|
2013-10-23 17:17:26 -04:00
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
2013-10-19 13:13:48 -04:00
|
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
2013-10-23 17:17:26 -04:00
|
|
|
|
|
|
|
// alternate functions for SCL and SDA
|
2013-10-19 13:13:48 -04:00
|
|
|
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
|
|
|
|
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
|
|
|
|
|
|
|
|
// get clock speeds
|
|
|
|
RCC_ClocksTypeDef rcc_clocks;
|
|
|
|
RCC_GetClocksFreq(&rcc_clocks);
|
|
|
|
|
|
|
|
// disable the I2C peripheral before we configure it
|
|
|
|
I2C1->CR1 &= ~I2C_CR1_PE;
|
|
|
|
|
|
|
|
// program peripheral input clock
|
|
|
|
I2C1->CR2 = 4; // no interrupts; 4 MHz (hopefully!) (could go up to 42MHz)
|
|
|
|
|
|
|
|
// configure clock control reg
|
|
|
|
uint32_t freq = rcc_clocks.PCLK1_Frequency / (100000 << 1); // want 100kHz, this is the formula for freq
|
|
|
|
I2C1->CCR = freq; // standard mode (speed), freq calculated as above
|
|
|
|
|
|
|
|
// configure rise time reg
|
|
|
|
I2C1->TRISE = (rcc_clocks.PCLK1_Frequency / 1000000) + 1; // formula for trise, gives maximum rise time
|
|
|
|
|
|
|
|
// enable the I2C peripheral
|
|
|
|
I2C1->CR1 |= I2C_CR1_PE;
|
|
|
|
|
2013-10-23 17:17:26 -04:00
|
|
|
// wait 20ms, then turn on AVDD, then wait 20ms again; this seems to work, but maybe can decrease delays
|
2013-10-23 19:22:27 -04:00
|
|
|
// doesn't work for soft reboot; 50ms doesn't work either...
|
2013-10-23 17:17:26 -04:00
|
|
|
sys_tick_delay_ms(20);
|
|
|
|
GPIOB->BSRRL = GPIO_Pin_5;
|
|
|
|
sys_tick_delay_ms(20);
|
|
|
|
|
2013-10-19 13:13:48 -04:00
|
|
|
// set START bit in CR1 to generate a start cond!
|
2014-01-02 16:30:26 -05:00
|
|
|
|
|
|
|
// init the chip via I2C commands
|
2014-02-16 16:41:57 -05:00
|
|
|
accel_start(ACCEL_ADDR, 1);
|
|
|
|
accel_send_byte(0);
|
|
|
|
accel_stop();
|
2014-01-02 16:30:26 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
// read and print all 11 registers
|
2014-02-16 16:41:57 -05:00
|
|
|
accel_start(ACCEL_ADDR, 1);
|
|
|
|
accel_send_byte(0);
|
|
|
|
accel_restart(ACCEL_ADDR, 0);
|
2014-01-02 16:30:26 -05:00
|
|
|
for (int i = 0; i <= 0xa; i++) {
|
|
|
|
int data;
|
|
|
|
if (i == 0xa) {
|
2014-02-16 16:41:57 -05:00
|
|
|
data = accel_read_nack();
|
2014-01-02 16:30:26 -05:00
|
|
|
} else {
|
2014-02-16 16:41:57 -05:00
|
|
|
data = accel_read_ack();
|
2014-01-02 16:30:26 -05:00
|
|
|
}
|
|
|
|
printf(" %02x", data);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
*/
|
|
|
|
|
|
|
|
// put into active mode
|
2014-02-16 16:41:57 -05:00
|
|
|
accel_start(ACCEL_ADDR, 1);
|
|
|
|
accel_send_byte(7); // mode
|
|
|
|
accel_send_byte(1); // active mode
|
|
|
|
accel_stop();
|
2014-01-02 16:30:26 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
// infinite loop to read values
|
|
|
|
for (;;) {
|
|
|
|
sys_tick_delay_ms(500);
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
accel_start(ACCEL_ADDR, 1);
|
|
|
|
accel_send_byte(0);
|
|
|
|
accel_restart(ACCEL_ADDR, 0);
|
2014-01-02 16:30:26 -05:00
|
|
|
for (int i = 0; i <= 3; i++) {
|
|
|
|
int data;
|
|
|
|
if (i == 3) {
|
2014-02-16 16:41:57 -05:00
|
|
|
data = accel_read_nack();
|
2014-01-02 16:30:26 -05:00
|
|
|
printf(" %02x\n", data);
|
|
|
|
} else {
|
2014-02-16 16:41:57 -05:00
|
|
|
data = accel_read_ack() & 0x3f;
|
2014-01-02 16:30:26 -05:00
|
|
|
if (data & 0x20) {
|
|
|
|
data |= ~0x1f;
|
|
|
|
}
|
|
|
|
printf(" % 2d", data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2013-10-19 13:13:48 -04:00
|
|
|
}
|
|
|
|
|
2013-10-23 15:39:20 -04:00
|
|
|
static uint32_t i2c_get_sr(void) {
|
2013-10-19 13:13:48 -04:00
|
|
|
// must read SR1 first, then SR2, as the read can clear some flags
|
|
|
|
uint32_t sr1 = I2C1->SR1;
|
|
|
|
uint32_t sr2 = I2C1->SR2;
|
|
|
|
return (sr2 << 16) | sr1;
|
|
|
|
}
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
void accel_restart(uint8_t addr, int write) {
|
2013-10-19 13:13:48 -04:00
|
|
|
// send start condition
|
|
|
|
I2C1->CR1 |= I2C_CR1_START;
|
|
|
|
|
|
|
|
// wait for BUSY, MSL and SB --> Slave has acknowledged start condition
|
2013-10-23 17:17:26 -04:00
|
|
|
uint32_t timeout = 1000000;
|
2013-10-19 13:13:48 -04:00
|
|
|
while ((i2c_get_sr() & 0x00030001) != 0x00030001) {
|
2013-10-23 17:17:26 -04:00
|
|
|
if (--timeout == 0) {
|
2014-02-16 16:41:57 -05:00
|
|
|
printf("timeout in accel_restart\n");
|
2013-10-23 17:17:26 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-10-19 13:13:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (write) {
|
|
|
|
// send address and write bit
|
|
|
|
I2C1->DR = (addr << 1) | 0;
|
|
|
|
// wait for BUSY, MSL, ADDR, TXE and TRA
|
2013-10-23 17:17:26 -04:00
|
|
|
timeout = 1000000;
|
2013-10-19 13:13:48 -04:00
|
|
|
while ((i2c_get_sr() & 0x00070082) != 0x00070082) {
|
2013-10-23 17:17:26 -04:00
|
|
|
if (--timeout == 0) {
|
2014-02-16 16:41:57 -05:00
|
|
|
printf("timeout in accel_restart write\n");
|
2013-10-23 17:17:26 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-10-19 13:13:48 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// send address and read bit
|
|
|
|
I2C1->DR = (addr << 1) | 1;
|
|
|
|
// wait for BUSY, MSL and ADDR flags
|
2013-10-23 17:17:26 -04:00
|
|
|
timeout = 1000000;
|
2013-10-19 13:13:48 -04:00
|
|
|
while ((i2c_get_sr() & 0x00030002) != 0x00030002) {
|
2013-10-23 17:17:26 -04:00
|
|
|
if (--timeout == 0) {
|
2014-02-16 16:41:57 -05:00
|
|
|
printf("timeout in accel_restart read\n");
|
2013-10-23 17:17:26 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-10-19 13:13:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
void accel_start(uint8_t addr, int write) {
|
2013-10-19 13:13:48 -04:00
|
|
|
// wait until I2C is not busy
|
2013-10-23 17:17:26 -04:00
|
|
|
uint32_t timeout = 1000000;
|
2013-10-19 13:13:48 -04:00
|
|
|
while (I2C1->SR2 & I2C_SR2_BUSY) {
|
2013-10-23 17:17:26 -04:00
|
|
|
if (--timeout == 0) {
|
2014-02-16 16:41:57 -05:00
|
|
|
printf("timeout in accel_start\n");
|
2013-10-23 17:17:26 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-10-19 13:13:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// do rest of start
|
2014-02-16 16:41:57 -05:00
|
|
|
accel_restart(addr, write);
|
2013-10-19 13:13:48 -04:00
|
|
|
}
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
void accel_send_byte(uint8_t data) {
|
2013-10-19 13:13:48 -04:00
|
|
|
// send byte
|
|
|
|
I2C1->DR = data;
|
|
|
|
// wait for TRA, BUSY, MSL, TXE and BTF (byte transmitted)
|
2013-10-23 17:17:26 -04:00
|
|
|
uint32_t timeout = 1000000;
|
2013-10-19 13:13:48 -04:00
|
|
|
while ((i2c_get_sr() & 0x00070084) != 0x00070084) {
|
2013-10-23 17:17:26 -04:00
|
|
|
if (--timeout == 0) {
|
2014-02-16 16:41:57 -05:00
|
|
|
printf("timeout in accel_send_byte\n");
|
2013-10-23 17:17:26 -04:00
|
|
|
return;
|
2013-10-19 13:13:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
uint8_t accel_read_ack(void) {
|
2013-10-19 13:13:48 -04:00
|
|
|
// enable ACK of received byte
|
|
|
|
I2C1->CR1 |= I2C_CR1_ACK;
|
|
|
|
// wait for BUSY, MSL and RXNE (byte received)
|
2013-10-23 17:17:26 -04:00
|
|
|
uint32_t timeout = 1000000;
|
2013-10-19 13:13:48 -04:00
|
|
|
while ((i2c_get_sr() & 0x00030040) != 0x00030040) {
|
2013-10-23 17:17:26 -04:00
|
|
|
if (--timeout == 0) {
|
2014-02-16 16:41:57 -05:00
|
|
|
printf("timeout in accel_read_ack\n");
|
2013-10-23 17:17:26 -04:00
|
|
|
break;
|
|
|
|
}
|
2013-10-19 13:13:48 -04:00
|
|
|
}
|
|
|
|
// read and return data
|
|
|
|
uint8_t data = I2C1->DR;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
uint8_t accel_read_nack(void) {
|
2013-10-19 13:13:48 -04:00
|
|
|
// disable ACK of received byte (to indicate end of receiving)
|
|
|
|
I2C1->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ACK);
|
|
|
|
// last byte should apparently also generate a stop condition
|
|
|
|
I2C1->CR1 |= I2C_CR1_STOP;
|
|
|
|
// wait for BUSY, MSL and RXNE (byte received)
|
2013-10-23 17:17:26 -04:00
|
|
|
uint32_t timeout = 1000000;
|
2013-10-19 13:13:48 -04:00
|
|
|
while ((i2c_get_sr() & 0x00030040) != 0x00030040) {
|
2013-10-23 17:17:26 -04:00
|
|
|
if (--timeout == 0) {
|
2014-02-16 16:41:57 -05:00
|
|
|
printf("timeout in accel_read_nack\n");
|
2013-10-23 17:17:26 -04:00
|
|
|
break;
|
|
|
|
}
|
2013-10-19 13:13:48 -04:00
|
|
|
}
|
|
|
|
// read and return data
|
|
|
|
uint8_t data = I2C1->DR;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
void accel_stop(void) {
|
2013-10-19 13:13:48 -04:00
|
|
|
// send stop condition
|
|
|
|
I2C1->CR1 |= I2C_CR1_STOP;
|
|
|
|
}
|
2014-01-02 16:30:26 -05:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Micro Python bindings */
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
int accel_buf[12];
|
2014-01-02 16:30:26 -05:00
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
mp_obj_t pyb_accel_read(void) {
|
2014-01-02 16:30:26 -05:00
|
|
|
for (int i = 0; i <= 6; i += 3) {
|
2014-02-16 16:41:57 -05:00
|
|
|
accel_buf[0 + i] = accel_buf[0 + i + 3];
|
|
|
|
accel_buf[1 + i] = accel_buf[1 + i + 3];
|
|
|
|
accel_buf[2 + i] = accel_buf[2 + i + 3];
|
2014-01-02 16:30:26 -05:00
|
|
|
}
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
accel_start(ACCEL_ADDR, 1);
|
|
|
|
accel_send_byte(0);
|
|
|
|
accel_restart(ACCEL_ADDR, 0);
|
2014-01-02 16:30:26 -05:00
|
|
|
for (int i = 0; i <= 2; i++) {
|
2014-02-16 16:41:57 -05:00
|
|
|
int v = accel_read_ack() & 0x3f;
|
2014-01-02 16:30:26 -05:00
|
|
|
if (v & 0x20) {
|
|
|
|
v |= ~0x1f;
|
|
|
|
}
|
2014-02-16 16:41:57 -05:00
|
|
|
accel_buf[9 + i] = v;
|
2014-01-02 16:30:26 -05:00
|
|
|
}
|
2014-02-16 16:41:57 -05:00
|
|
|
int jolt_info = accel_read_nack();
|
2014-01-02 16:30:26 -05:00
|
|
|
|
|
|
|
mp_obj_t data[4];
|
2014-02-16 16:41:57 -05:00
|
|
|
data[0] = mp_obj_new_int(accel_buf[0] + accel_buf[3] + accel_buf[6] + accel_buf[9]);
|
|
|
|
data[1] = mp_obj_new_int(accel_buf[1] + accel_buf[4] + accel_buf[7] + accel_buf[10]);
|
|
|
|
data[2] = mp_obj_new_int(accel_buf[2] + accel_buf[5] + accel_buf[8] + accel_buf[11]);
|
2014-01-23 13:10:42 -05:00
|
|
|
data[3] = mp_obj_new_int(jolt_info);
|
2014-01-02 16:30:26 -05:00
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
return mp_build_tuple(4, data);
|
2014-01-02 16:30:26 -05:00
|
|
|
}
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(pyb_accel_read_obj, pyb_accel_read);
|
2014-01-02 16:30:26 -05:00
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
mp_obj_t pyb_accel_read_all(void) {
|
2014-01-02 16:30:26 -05:00
|
|
|
mp_obj_t data[11];
|
2014-02-16 16:41:57 -05:00
|
|
|
accel_start(ACCEL_ADDR, 1);
|
|
|
|
accel_send_byte(0);
|
|
|
|
accel_restart(ACCEL_ADDR, 0);
|
2014-01-02 16:30:26 -05:00
|
|
|
for (int i = 0; i <= 9; i++) {
|
2014-02-16 16:41:57 -05:00
|
|
|
data[i] = mp_obj_new_int(accel_read_ack());
|
2014-01-02 16:30:26 -05:00
|
|
|
}
|
2014-02-16 16:41:57 -05:00
|
|
|
data[10] = mp_obj_new_int(accel_read_nack());
|
2014-01-02 16:30:26 -05:00
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
return mp_build_tuple(11, data);
|
2014-01-02 16:30:26 -05:00
|
|
|
}
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(pyb_accel_read_all_obj, pyb_accel_read_all);
|
2014-01-02 16:30:26 -05:00
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
mp_obj_t pyb_accel_write_mode(mp_obj_t o_int, mp_obj_t o_mode) {
|
|
|
|
accel_start(ACCEL_ADDR, 1);
|
|
|
|
accel_send_byte(6); // start at int
|
|
|
|
accel_send_byte(mp_obj_get_int(o_int));
|
|
|
|
accel_send_byte(mp_obj_get_int(o_mode));
|
|
|
|
accel_stop();
|
2014-01-02 16:30:26 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-02-16 16:41:57 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(pyb_accel_write_mode_obj, pyb_accel_write_mode);
|