Reorganize storage to per-pin

This commit is contained in:
Hierophect 2019-09-19 12:18:18 -04:00
parent 12cd009bb9
commit 60985968a8
5 changed files with 155 additions and 97 deletions

View File

@ -33,101 +33,103 @@
#include "shared-bindings/microcontroller/__init__.h"
#include "supervisor/shared/translate.h"
#include "common-hal/microcontroller/Pin.h"
#include "stm32f4/periph.h"
STATIC I2C_HandleTypeDef hi2c;
STATIC I2C_TypeDef * I2Cbanks[3] =
{
#ifdef I2C1
I2C1,
#else
NULL,
#endif
#ifdef I2C2
I2C2,
#else
NULL,
#endif
#ifdef I2C3
I2C3
#else
NULL
#endif
};
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency, uint32_t timeout) {
//match pins to I2C objects
I2C_TypeDef * I2Cx;
uint8_t i2cidx = 0;
uint8_t i2cidx_c = 0;
uint8_t len = sizeof(mcu_i2c_list)/sizeof(*mcu_i2c_list);
for(uint i=0; i<len;i++) {
if (mcu_i2c_list[i]->sda_pin == sda) {
i2cidx = mcu_i2c_list[i]->i2c_index;
}
if (mcu_i2c_list[i]->scl_pin == scl) {
i2cidx_c = mcu_i2c_list[i]->i2c_index;
uint8_t sda_len = sizeof(mcu_i2c_sda_list)/sizeof(*mcu_i2c_sda_list);
uint8_t scl_len = sizeof(mcu_i2c_scl_list)/sizeof(*mcu_i2c_scl_list);
for(uint i=0; i<sda_len;i++) {
if (mcu_i2c_sda_list[i]->pin == sda) {
for(uint j=0; j<scl_len;j++) {
if ((mcu_i2c_scl_list[j]->pin == scl)
&& (mcu_i2c_scl_list[j]->i2c_index == mcu_i2c_sda_list[i]->i2c_index)) {
self->scl = mcu_i2c_scl_list[j];
self->sda = mcu_i2c_sda_list[i];
break;
}
}
}
}
if(i2cidx == i2cidx_c) {
if(I2Cbanks[i2cidx] == NULL || i2cidx>=len) {
mp_raise_RuntimeError(translate("Bad I2C pin definition, check peripheral files"));
}
I2Cx = I2Cbanks[i2cidx];
//check if pins are on the same I2C peripheral
if(self->sda->i2c_index == self->scl->i2c_index) {
I2Cx = mcu_i2c_banks[self->sda->i2c_index];
} else {
mp_raise_RuntimeError(translate("Invalid I2C pin selection"));
}
//Start GPIO for each pin
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_mask(sda->number)|pin_mask(scl->number);
GPIO_InitStruct.Pin = pin_mask(sda->number);
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; //TODO; custom per pin? Or error if mixed is bad.
GPIO_InitStruct.Alternate = 4;//self->sda->altfn_index;
HAL_GPIO_Init(pin_port(sda->port), &GPIO_InitStruct);
__HAL_RCC_I2C1_CLK_ENABLE(); //TODO custom per periph
hi2c.Instance = I2Cx;
hi2c.Init.ClockSpeed = 100000;
hi2c.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c.Init.OwnAddress1 = 0;
hi2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c.Init.OwnAddress2 = 0;
hi2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if(HAL_I2C_Init(&hi2c) != HAL_OK) {
GPIO_InitStruct.Pin = pin_mask(scl->number);
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = 4;//self->scl->altfn_index;
HAL_GPIO_Init(pin_port(scl->port), &GPIO_InitStruct);
#ifdef I2C1
if(I2Cx==I2C1) __HAL_RCC_I2C1_CLK_ENABLE();
#endif
#ifdef I2C2
if(I2Cx==I2C2) __HAL_RCC_I2C2_CLK_ENABLE();
#endif
#ifdef I2C3
if(I2Cx==I2C3) __HAL_RCC_I2C3_CLK_ENABLE();
#endif
self->handle.Instance = I2C1; //I2Cx;
self->handle.Init.ClockSpeed = 100000;
self->handle.Init.DutyCycle = I2C_DUTYCYCLE_2;
self->handle.Init.OwnAddress1 = 0;
self->handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
self->handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
self->handle.Init.OwnAddress2 = 0;
self->handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
self->handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if(HAL_I2C_Init(&(self->handle)) != HAL_OK) {
mp_raise_RuntimeError(translate("I2C Init Error"));
} else {
mp_printf(&mp_plat_print, "I2C INIT OK");
mp_printf(&mp_plat_print, "I2C INIT OK\n");
}
self->sda_pin = sda;
self->scl_pin = scl;
claim_pin(sda);
claim_pin(scl);
}
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
return self->sda_pin == NULL;
return self->sda->pin == mp_const_none;
}
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
if (common_hal_busio_i2c_deinited(self)) {
return;
}
HAL_I2C_MspDeInit(&hi2c);
self->sda_pin = NULL;
self->scl_pin = NULL;
#ifdef I2C1
if(self->handle.Instance==I2C1) __HAL_RCC_I2C1_CLK_DISABLE();
#endif
#ifdef I2C2
if(self->handle.Instance==I2C2) __HAL_RCC_I2C2_CLK_DISABLE();
#endif
#ifdef I2C3
if(self->handle.Instance==I2C3) __HAL_RCC_I2C3_CLK_DISABLE();
#endif
HAL_GPIO_DeInit(pin_port(self->sda->pin->port), pin_mask(self->sda->pin->number));
HAL_GPIO_DeInit(pin_port(self->scl->pin->port), pin_mask(self->scl->pin->number));
self->sda = mp_const_none;
self->scl = mp_const_none;
}
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
return HAL_I2C_IsDeviceReady(&hi2c, (uint16_t)(addr<<1),2,2) == HAL_OK;
return HAL_I2C_IsDeviceReady(&(self->handle), (uint16_t)(addr<<1),2,2) == HAL_OK;
}
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
@ -159,10 +161,10 @@ void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
const uint8_t *data, size_t len, bool transmit_stop_bit) {
return HAL_I2C_Master_Transmit(&hi2c, (uint16_t)(addr<<1), (uint8_t *)data, (uint16_t)len, 2) == HAL_OK ? 0 : MP_EIO;
return HAL_I2C_Master_Transmit(&(self->handle), (uint16_t)(addr<<1), (uint8_t *)data, (uint16_t)len, 2) == HAL_OK ? 0 : MP_EIO;
}
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
uint8_t *data, size_t len) {
return HAL_I2C_Master_Receive(&hi2c, (uint16_t)(addr<<1), data, (uint16_t)len, 2) == HAL_OK ? 0 : MP_EIO;
return HAL_I2C_Master_Receive(&(self->handle), (uint16_t)(addr<<1), data, (uint16_t)len, 2) == HAL_OK ? 0 : MP_EIO;
}

View File

@ -31,15 +31,16 @@
#include "common-hal/microcontroller/Pin.h"
#include "stm32f4xx_hal.h"
#include "stm32f4/periph.h"
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
I2C_HandleTypeDef i2c_handle;
I2C_HandleTypeDef handle;
bool has_lock;
const mcu_pin_obj_t *scl_pin;
const mcu_pin_obj_t *sda_pin;
const mcu_i2c_scl_obj_t *scl;
const mcu_i2c_sda_obj_t *sda;
} busio_i2c_obj_t;
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_I2C_H

View File

@ -33,19 +33,17 @@
#include "stm32f4xx_hal.h"
#include "stm32f4/pins.h"
#define PA 0
#define PB 1
#define PC 2
#define PD 3
#define PE 4
#define PF 5
#define PG 6
#define PH 7
#define PI 8
#define PJ 9
#define PK 10
#define NO_ALT 0x0F
// #define PA 0
// #define PB 1
// #define PC 2
// #define PD 3
// #define PE 4
// #define PF 5
// #define PG 6
// #define PH 7
// #define PI 8
// #define PJ 9
// #define PK 10
// I2C
@ -82,21 +80,32 @@ typedef struct {
// Address Version
typedef struct {
uint8_t i2c_index:4; // Index of the I2C unit
uint8_t alt_index:4; // Alt index is arbitrary, it just lists additional pin options
const mcu_pin_obj_t * sda_pin;
const mcu_pin_obj_t * scl_pin;
} mcu_i2c_periph_obj_t;
uint8_t i2c_index:4; // Index of the I2C unit (1 to 3)
uint8_t altfn_index:4; //Index of the altfn for this pin (0 to 15)
const mcu_pin_obj_t * pin;
} mcu_i2c_sda_obj_t;
// Address Version
typedef struct {
uint8_t i2c_index:4; // Index of the I2C unit (1 to 3)
uint8_t altfn_index:4; //Index of the altfn for this pin (0 to 15)
const mcu_pin_obj_t * pin;
} mcu_i2c_scl_obj_t;
#define I2C(index, alt, sda, scl) \
#define I2C_SDA(index, alt, sda_pin) \
{ \
.i2c_index = index, \
.alt_index = alt, \
.sda_pin = sda, \
.scl_pin = scl, \
.altfn_index = alt, \
.pin = sda_pin, \
}
#define I2C_SCL(index, alt, scl_pin) \
{ \
.i2c_index = index, \
.altfn_index = alt, \
.pin = scl_pin, \
}
// TODO: SPI, UART, etc

View File

@ -34,9 +34,41 @@
//const mcu_i2c_periph_obj_t periph_I2C2 = I2C(1, NO_ALT, SDA(PB,11), SCL(PB,10));
//const mcu_i2c_periph_obj_t periph_I2C3 = I2C(1, NO_ALT, SDA(PB,9), SCL(PB,8));
const mcu_i2c_periph_obj_t periph_I2C1_0 = I2C(1, 0, &pin_PB07, &pin_PB06);
const mcu_i2c_periph_obj_t periph_I2C1_1 = I2C(1, 1, &pin_PB09, &pin_PB08);
//const mcu_i2c_periph_obj_t periph_I2C2 = I2C(1, NO_ALT, &pin_PB11, &pin_PB10); //AFN only
const mcu_i2c_periph_obj_t periph_I2C3 = I2C(1, NO_ALT, &pin_PB09, &pin_PB08);
// I2C
const mcu_i2c_periph_obj_t* mcu_i2c_list[3] = {&periph_I2C1_0,&periph_I2C1_1,&periph_I2C3};
I2C_TypeDef * mcu_i2c_banks[3] = {I2C1, I2C2, I2C3};
//SDA Pins
const mcu_i2c_sda_obj_t per_SDA_1B07 = I2C_SDA(1, 4, &pin_PB07);
const mcu_i2c_sda_obj_t per_SDA_1B09 = I2C_SDA(1, 4, &pin_PB09);
// const mcu_i2c_sda_obj_t per_SDA_2B11 = I2C_SDA(2, 4, &pin_PB11); //not on LQFP100
const mcu_i2c_sda_obj_t per_SDA_2B09 = I2C_SDA(2, 9, &pin_PB09);
const mcu_i2c_sda_obj_t per_SDA_2B03 = I2C_SDA(2, 9, &pin_PB03);
const mcu_i2c_sda_obj_t per_SDA_3C09 = I2C_SDA(3, 4, &pin_PC09);
const mcu_i2c_sda_obj_t per_SDA_3B04 = I2C_SDA(3, 9, &pin_PB04);
const mcu_i2c_sda_obj_t per_SDA_3B08 = I2C_SDA(3, 9, &pin_PB08);
//SCL Pins
const mcu_i2c_scl_obj_t per_SCL_1B06 = I2C_SCL(1, 4, &pin_PB06);
const mcu_i2c_scl_obj_t per_SCL_1B08 = I2C_SCL(1, 4, &pin_PB08);
const mcu_i2c_scl_obj_t per_SCL_2B10 = I2C_SCL(2, 4, &pin_PB10);
const mcu_i2c_scl_obj_t per_SCL_3B08 = I2C_SCL(3, 4, &pin_PA08);
const mcu_i2c_sda_obj_t* mcu_i2c_sda_list[7] = {
&per_SDA_1B07,
&per_SDA_1B09,
&per_SDA_2B09,
&per_SDA_2B03,
&per_SDA_3C09,
&per_SDA_3B04,
&per_SDA_3B08
};
const mcu_i2c_scl_obj_t* mcu_i2c_scl_list[4] = {
&per_SCL_1B06,
&per_SCL_1B08,
&per_SCL_2B10,
&per_SCL_3B08
};
//SPI, UART, Etc

View File

@ -27,10 +27,24 @@
#ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
#define MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
extern const mcu_i2c_periph_obj_t periph_I2C1_0;
extern const mcu_i2c_periph_obj_t periph_I2C1_1;
extern const mcu_i2c_periph_obj_t periph_I2C3;
//I2C
extern I2C_TypeDef * mcu_i2c_banks[3];
extern const mcu_i2c_periph_obj_t* mcu_i2c_list[3];
extern const mcu_i2c_sda_obj_t per_SDA_1B07;
extern const mcu_i2c_sda_obj_t per_SDA_1B09;
//extern const mcu_i2c_sda_obj_t per_SDA_2B11;
extern const mcu_i2c_sda_obj_t per_SDA_2B09;
extern const mcu_i2c_sda_obj_t per_SDA_2B03;
extern const mcu_i2c_sda_obj_t per_SDA_3C09;
extern const mcu_i2c_sda_obj_t per_SDA_3B04;
extern const mcu_i2c_sda_obj_t per_SDA_3B08;
extern const mcu_i2c_scl_obj_t per_SCL_1B06;
extern const mcu_i2c_scl_obj_t per_SCL_1B08;
extern const mcu_i2c_scl_obj_t per_SCL_2B10;
extern const mcu_i2c_scl_obj_t per_SCL_3B08;
extern const mcu_i2c_sda_obj_t* mcu_i2c_sda_list[7];
extern const mcu_i2c_scl_obj_t* mcu_i2c_scl_list[4];
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H