Reorganize storage to per-pin
This commit is contained in:
parent
12cd009bb9
commit
60985968a8
@ -33,101 +33,103 @@
|
|||||||
#include "shared-bindings/microcontroller/__init__.h"
|
#include "shared-bindings/microcontroller/__init__.h"
|
||||||
#include "supervisor/shared/translate.h"
|
#include "supervisor/shared/translate.h"
|
||||||
#include "common-hal/microcontroller/Pin.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,
|
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) {
|
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;
|
I2C_TypeDef * I2Cx;
|
||||||
uint8_t i2cidx = 0;
|
uint8_t sda_len = sizeof(mcu_i2c_sda_list)/sizeof(*mcu_i2c_sda_list);
|
||||||
uint8_t i2cidx_c = 0;
|
uint8_t scl_len = sizeof(mcu_i2c_scl_list)/sizeof(*mcu_i2c_scl_list);
|
||||||
uint8_t len = sizeof(mcu_i2c_list)/sizeof(*mcu_i2c_list);
|
for(uint i=0; i<sda_len;i++) {
|
||||||
for(uint i=0; i<len;i++) {
|
if (mcu_i2c_sda_list[i]->pin == sda) {
|
||||||
if (mcu_i2c_list[i]->sda_pin == sda) {
|
for(uint j=0; j<scl_len;j++) {
|
||||||
i2cidx = mcu_i2c_list[i]->i2c_index;
|
if ((mcu_i2c_scl_list[j]->pin == scl)
|
||||||
}
|
&& (mcu_i2c_scl_list[j]->i2c_index == mcu_i2c_sda_list[i]->i2c_index)) {
|
||||||
if (mcu_i2c_list[i]->scl_pin == scl) {
|
self->scl = mcu_i2c_scl_list[j];
|
||||||
i2cidx_c = mcu_i2c_list[i]->i2c_index;
|
self->sda = mcu_i2c_sda_list[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(i2cidx == i2cidx_c) {
|
|
||||||
if(I2Cbanks[i2cidx] == NULL || i2cidx>=len) {
|
//check if pins are on the same I2C peripheral
|
||||||
mp_raise_RuntimeError(translate("Bad I2C pin definition, check peripheral files"));
|
if(self->sda->i2c_index == self->scl->i2c_index) {
|
||||||
}
|
I2Cx = mcu_i2c_banks[self->sda->i2c_index];
|
||||||
I2Cx = I2Cbanks[i2cidx];
|
|
||||||
} else {
|
} else {
|
||||||
mp_raise_RuntimeError(translate("Invalid I2C pin selection"));
|
mp_raise_RuntimeError(translate("Invalid I2C pin selection"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Start GPIO for each pin
|
||||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
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.Mode = GPIO_MODE_AF_OD;
|
||||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
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_GPIO_Init(pin_port(sda->port), &GPIO_InitStruct);
|
||||||
__HAL_RCC_I2C1_CLK_ENABLE(); //TODO custom per periph
|
|
||||||
|
|
||||||
hi2c.Instance = I2Cx;
|
GPIO_InitStruct.Pin = pin_mask(scl->number);
|
||||||
hi2c.Init.ClockSpeed = 100000;
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
|
||||||
hi2c.Init.DutyCycle = I2C_DUTYCYCLE_2;
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||||
hi2c.Init.OwnAddress1 = 0;
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||||
hi2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
GPIO_InitStruct.Alternate = 4;//self->scl->altfn_index;
|
||||||
hi2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
HAL_GPIO_Init(pin_port(scl->port), &GPIO_InitStruct);
|
||||||
hi2c.Init.OwnAddress2 = 0;
|
|
||||||
hi2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
#ifdef I2C1
|
||||||
hi2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
if(I2Cx==I2C1) __HAL_RCC_I2C1_CLK_ENABLE();
|
||||||
if(HAL_I2C_Init(&hi2c) != HAL_OK) {
|
#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"));
|
mp_raise_RuntimeError(translate("I2C Init Error"));
|
||||||
} else {
|
} 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(sda);
|
||||||
claim_pin(scl);
|
claim_pin(scl);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
|
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) {
|
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
|
||||||
if (common_hal_busio_i2c_deinited(self)) {
|
if (common_hal_busio_i2c_deinited(self)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
#ifdef I2C1
|
||||||
HAL_I2C_MspDeInit(&hi2c);
|
if(self->handle.Instance==I2C1) __HAL_RCC_I2C1_CLK_DISABLE();
|
||||||
|
#endif
|
||||||
self->sda_pin = NULL;
|
#ifdef I2C2
|
||||||
self->scl_pin = NULL;
|
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) {
|
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) {
|
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,
|
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) {
|
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 common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
|
||||||
uint8_t *data, size_t len) {
|
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;
|
||||||
}
|
}
|
||||||
|
@ -31,15 +31,16 @@
|
|||||||
#include "common-hal/microcontroller/Pin.h"
|
#include "common-hal/microcontroller/Pin.h"
|
||||||
|
|
||||||
#include "stm32f4xx_hal.h"
|
#include "stm32f4xx_hal.h"
|
||||||
|
#include "stm32f4/periph.h"
|
||||||
|
|
||||||
#include "py/obj.h"
|
#include "py/obj.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
I2C_HandleTypeDef i2c_handle;
|
I2C_HandleTypeDef handle;
|
||||||
bool has_lock;
|
bool has_lock;
|
||||||
const mcu_pin_obj_t *scl_pin;
|
const mcu_i2c_scl_obj_t *scl;
|
||||||
const mcu_pin_obj_t *sda_pin;
|
const mcu_i2c_sda_obj_t *sda;
|
||||||
} busio_i2c_obj_t;
|
} busio_i2c_obj_t;
|
||||||
|
|
||||||
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_I2C_H
|
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_BUSIO_I2C_H
|
||||||
|
@ -33,19 +33,17 @@
|
|||||||
#include "stm32f4xx_hal.h"
|
#include "stm32f4xx_hal.h"
|
||||||
#include "stm32f4/pins.h"
|
#include "stm32f4/pins.h"
|
||||||
|
|
||||||
#define PA 0
|
// #define PA 0
|
||||||
#define PB 1
|
// #define PB 1
|
||||||
#define PC 2
|
// #define PC 2
|
||||||
#define PD 3
|
// #define PD 3
|
||||||
#define PE 4
|
// #define PE 4
|
||||||
#define PF 5
|
// #define PF 5
|
||||||
#define PG 6
|
// #define PG 6
|
||||||
#define PH 7
|
// #define PH 7
|
||||||
#define PI 8
|
// #define PI 8
|
||||||
#define PJ 9
|
// #define PJ 9
|
||||||
#define PK 10
|
// #define PK 10
|
||||||
|
|
||||||
#define NO_ALT 0x0F
|
|
||||||
|
|
||||||
// I2C
|
// I2C
|
||||||
|
|
||||||
@ -82,21 +80,32 @@ typedef struct {
|
|||||||
|
|
||||||
// Address Version
|
// Address Version
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t i2c_index:4; // Index of the I2C unit
|
uint8_t i2c_index:4; // Index of the I2C unit (1 to 3)
|
||||||
uint8_t alt_index:4; // Alt index is arbitrary, it just lists additional pin options
|
uint8_t altfn_index:4; //Index of the altfn for this pin (0 to 15)
|
||||||
const mcu_pin_obj_t * sda_pin;
|
const mcu_pin_obj_t * pin;
|
||||||
const mcu_pin_obj_t * scl_pin;
|
} mcu_i2c_sda_obj_t;
|
||||||
} mcu_i2c_periph_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, \
|
.i2c_index = index, \
|
||||||
.alt_index = alt, \
|
.altfn_index = alt, \
|
||||||
.sda_pin = sda, \
|
.pin = sda_pin, \
|
||||||
.scl_pin = scl, \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define I2C_SCL(index, alt, scl_pin) \
|
||||||
|
{ \
|
||||||
|
.i2c_index = index, \
|
||||||
|
.altfn_index = alt, \
|
||||||
|
.pin = scl_pin, \
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: SPI, UART, etc
|
// TODO: SPI, UART, etc
|
||||||
|
|
||||||
|
@ -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_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_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);
|
// I2C
|
||||||
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);
|
|
||||||
|
|
||||||
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
|
||||||
|
@ -27,10 +27,24 @@
|
|||||||
#ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
|
#ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
|
||||||
#define 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;
|
//I2C
|
||||||
extern const mcu_i2c_periph_obj_t periph_I2C1_1;
|
extern I2C_TypeDef * mcu_i2c_banks[3];
|
||||||
extern const mcu_i2c_periph_obj_t periph_I2C3;
|
|
||||||
|
|
||||||
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
|
#endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H
|
Loading…
Reference in New Issue
Block a user