Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AT32F435 I2C support #12423

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
438 changes: 254 additions & 184 deletions lib/main/AT32F43x/middlewares/i2c_application_library/i2c_application.c

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/config/REVO_AT/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@
#define SPI3_MOSI_PIN PC12

#define USE_FLASH_M25P16

#define I2C1_SCL_PIN PB8
#define I2C1_SDA_PIN PB9

#define I2C2_SCL_PIN PB10
#define I2C2_SDA_PIN PB11

#define BARO_I2C_INSTANCE I2C2
#define USE_BARO
#define USE_BARO_MS5611
16 changes: 9 additions & 7 deletions src/main/drivers/at32/bus_i2c_atbsp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "drivers/bus_i2c_impl.h"
#include "drivers/bus_i2c_timing.h"

#include "pg/pinio.h"

// Number of bits in I2C protocol phase
#define LEN_ADDR 7
#define LEN_RW 1
Expand Down Expand Up @@ -140,29 +142,29 @@ void i2cInit(I2CDevice device)


// Init I2C peripheral

i2c_handle_type *pHandle = &pDev->handle;

memset(pHandle, 0, sizeof(*pHandle));

pHandle->i2cx = pDev->hardware->reg;
i2c_type *i2cx = (i2c_type *)pDev->hardware->reg;
pHandle->i2cx = i2cx;

crm_clocks_freq_type crm_clk_freq;
crm_clocks_freq_get(&crm_clk_freq);

uint32_t i2cPclk = crm_clk_freq.apb1_freq;

uint32_t I2Cx_CLKCTRL = i2cClockTIMINGR(i2cPclk, pDev->clockSpeed, 0);

i2c_reset( pHandle->i2cx);
i2c_config(pHandle);

i2c_init( pHandle->i2cx, 0x0f, I2Cx_CLKCTRL);
i2c_init(i2cx, 0x0f, I2Cx_CLKCTRL);

i2c_own_address1_set( pHandle->i2cx, I2C_ADDRESS_MODE_7BIT, 0x0);
i2c_own_address1_set(i2cx, I2C_ADDRESS_MODE_7BIT, 0x0);

nvic_irq_enable(hardware->er_irq, NVIC_PRIORITY_BASE(NVIC_PRIO_I2C_ER), NVIC_PRIORITY_SUB(NVIC_PRIO_I2C_ER));
nvic_irq_enable(hardware->ev_irq, NVIC_PRIORITY_BASE(NVIC_PRIO_I2C_EV), NVIC_PRIORITY_SUB(NVIC_PRIO_I2C_EV));

i2c_enable(pHandle->i2cx, TRUE);
i2c_enable(i2cx, TRUE);
}

static void i2cUnstick(IO_t scl, IO_t sda)
Expand Down
4 changes: 4 additions & 0 deletions src/main/drivers/at32/platform_mcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
#if defined(AT32F435ZMT7)

#include "at32f435_437.h"
#include "at32f435_437_i2c.h"
#include "i2c_application.h"

typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;

#define I2C_TypeDef i2c_type
#define I2C_HandleTypeDef i2c_handle_type
#define GPIO_TypeDef gpio_type
#define GPIO_InitTypeDef gpio_init_type
#define TIM_TypeDef tmr_type
Expand Down
4 changes: 2 additions & 2 deletions src/main/drivers/bus_i2c_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ void i2cHardwareConfigure(const i2cConfig_t *i2cConfig)
for (int pindex = 0 ; pindex < I2C_PIN_SEL_MAX ; pindex++) {
if (i2cConfig[device].ioTagScl == hardware->sclPins[pindex].ioTag) {
pDev->scl = IOGetByTag(i2cConfig[device].ioTagScl);
#if defined(STM32F4) || defined(STM32H7) || defined(STM32G4)
#if defined(STM32F4) || defined(STM32H7) || defined(STM32G4) || defined(AT32F4)
pDev->sclAF = hardware->sclPins[pindex].af;
#endif
}
if (i2cConfig[device].ioTagSda == hardware->sdaPins[pindex].ioTag) {
pDev->sda = IOGetByTag(i2cConfig[device].ioTagSda);
#if defined(STM32F4) || defined(STM32H7) || defined(STM32G4)
#if defined(STM32F4) || defined(STM32H7) || defined(STM32G4) || defined(AT32F4)
pDev->sdaAF = hardware->sdaPins[pindex].af;
#endif
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/drivers/bus_i2c_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

typedef struct i2cPinDef_s {
ioTag_t ioTag;
#if defined(STM32F4) || defined(STM32H7) || defined(STM32G4)
#if defined(STM32F4) || defined(STM32H7) || defined(STM32G4) || defined(AT32F4)
uint8_t af;
#endif
} i2cPinDef_t;

#if defined(STM32F4) || defined(STM32H7) || defined(STM32G4)
#if defined(STM32F4) || defined(STM32H7) || defined(STM32G4) || defined(AT32F4)
#define I2CPINDEF(pin, af) { DEFIO_TAG_E(pin), af }
#else
#define I2CPINDEF(pin) { DEFIO_TAG_E(pin) }
Expand Down Expand Up @@ -74,7 +74,7 @@ typedef struct i2cDevice_s {
I2C_TypeDef *reg;
IO_t scl;
IO_t sda;
#if defined(STM32F4) || defined(STM32H7) || defined(STM32G4)
#if defined(STM32F4) || defined(STM32H7) || defined(STM32G4) || defined(AT32F4)
uint8_t sclAF;
uint8_t sdaAF;
#endif
Expand All @@ -85,7 +85,7 @@ typedef struct i2cDevice_s {
#if defined(STM32F4)
i2cState_t state;
#endif
#ifdef USE_HAL_DRIVER
#if defined(USE_HAL_DRIVER) || defined(AT32F4)
I2C_HandleTypeDef handle;
#endif
} i2cDevice_t;
Expand Down
11 changes: 6 additions & 5 deletions src/main/target/AT32F435/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@
#define USE_SPI_DEVICE_3
#define USE_SPI_DMA_ENABLE_LATE

#define USE_GYRO_SPI_MPU6000
#define USE_ACC_SPI_MPU6000

#define USE_EXTI
#define USE_GYRO_EXTI

#define USE_I2C
#define USE_I2C_DEVICE_1
#define USE_I2C_DEVICE_2
#define USE_I2C_DEVICE_3

#define USE_USB_DETECT
#define USE_VCP
Expand All @@ -76,8 +77,8 @@
#undef USE_OSD
#undef USE_BLACKBOX
#undef USE_SDCARD
#undef USE_BARO
#undef USE_MAG
//#undef USE_BARO
//#undef USE_MAG
#undef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
#undef USE_SERIAL_4WAY_SK_BOOTLOADER

Expand Down
10 changes: 7 additions & 3 deletions src/main/target/AT32F435/target.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ TARGET_MCU_FAMILY := AT32F4
STDPERIPH_DIR = $(ROOT)/lib/main/AT32F43x/drivers
STDPERIPH_SRC = \
$(notdir $(wildcard $(STDPERIPH_DIR)/src/*.c)) \
$(notdir $(wildcard $(STDPERIPH_DIR)/src/*.c)) \

EXCLUDES = at32f435_437_dvp.c \
at32f435_437_can.c \
Expand All @@ -23,18 +22,23 @@ INCLUDE_DIRS := $(INCLUDE_DIRS) \
$(SRC_DIR)/startup/at32 \
$(STDPERIPH_DIR)/inc \
$(ROOT)/lib/main/AT32F43x/cmsis/cm4/core_support \
$(ROOT)/lib/main/AT32F43x/cmsis/cm4
$(ROOT)/lib/main/AT32F43x/cmsis/cm4 \
$(ROOT)/lib/main/AT32F43x/middlewares/i2c_application_library

DEVICE_STDPERIPH_SRC = $(STDPERIPH_SRC)

TARGET_SRC := $(ROOT)/lib/main/AT32F43x/middlewares/i2c_application_library/i2c_application.c

LD_SCRIPT = $(LINKER_DIR)/at32_flash_f43xM.ld

ARCH_FLAGS = -std=c99 -mthumb -mcpu=cortex-m4 -march=armv7e-m -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -Wdouble-promotion
DEVICE_FLAGS += -DUSE_ATBSP_DRIVER -DAT32F43x -DHSE_VALUE=$(HSE_VALUE) -DAT32 -DUSE_OTG_HOST_MODE

MCU_COMMON_SRC = \
$(addprefix startup/at32/,$(notdir $(wildcard $(SRC_DIR)/startup/at32/*.c))) \
$(addprefix drivers/at32/,$(notdir $(wildcard $(SRC_DIR)/drivers/at32/*.c)))
$(addprefix drivers/at32/,$(notdir $(wildcard $(SRC_DIR)/drivers/at32/*.c))) \
drivers/bus_i2c_timing.c


MCU_EXCLUDES =

Expand Down