From 033bdf6491850a305b3c8c206445f0c38efccfa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Sun, 27 May 2018 01:43:14 +0200 Subject: [PATCH] Merge initI2cMainBus() and initI2cEepromBus() as initI2cBus() --- lib/agent | 2 +- right/src/init_peripherals.c | 66 ++++++++++++++++-------------------- right/src/init_peripherals.h | 26 ++++++++------ 3 files changed, 46 insertions(+), 48 deletions(-) diff --git a/lib/agent b/lib/agent index 65ea786..8e20c85 160000 --- a/lib/agent +++ b/lib/agent @@ -1 +1 @@ -Subproject commit 65ea786358c9ab1561626cc5c04bfa9dc76a30b8 +Subproject commit 8e20c85e0736adb18841e085f5d5fd2056a5c6a3 diff --git a/right/src/init_peripherals.c b/right/src/init_peripherals.c index a88aed0..e45afc7 100644 --- a/right/src/init_peripherals.c +++ b/right/src/init_peripherals.c @@ -22,20 +22,32 @@ volatile uint32_t I2cMainBusRequestedBaudRateBps = I2C_MAIN_BUS_NORMAL_BAUD_RATE volatile uint32_t I2cMainBusActualBaudRateBps; static i2c_bus_t i2cMainBus = { + .baseAddr = I2C_MAIN_BUS_BASEADDR, + .clockSrc = I2C_MAIN_BUS_CLK_SRC, + .mux = I2C_MAIN_BUS_MUX, + + .sdaClock = I2C_MAIN_BUS_SDA_CLOCK, .sdaGpio = I2C_MAIN_BUS_SDA_GPIO, .sdaPort = I2C_MAIN_BUS_SDA_PORT, .sdaPin = I2C_MAIN_BUS_SDA_PIN, + .sclClock = I2C_MAIN_BUS_SCL_CLOCK, .sclGpio = I2C_MAIN_BUS_SCL_GPIO, .sclPort = I2C_MAIN_BUS_SCL_PORT, .sclPin = I2C_MAIN_BUS_SCL_PIN, }; static i2c_bus_t i2cEepromBus = { + .baseAddr = I2C_EEPROM_BUS_BASEADDR, + .clockSrc = I2C_EEPROM_BUS_CLK_SRC, + .mux = I2C_EEPROM_BUS_MUX, + + .sdaClock = I2C_EEPROM_BUS_SDA_CLOCK, .sdaGpio = I2C_EEPROM_BUS_SDA_GPIO, .sdaPort = I2C_EEPROM_BUS_SDA_PORT, .sdaPin = I2C_EEPROM_BUS_SDA_PIN, + .sclClock = I2C_EEPROM_BUS_SCL_CLOCK, .sclGpio = I2C_EEPROM_BUS_SCL_GPIO, .sclPort = I2C_EEPROM_BUS_SCL_PORT, .sclPin = I2C_EEPROM_BUS_SCL_PIN, @@ -94,64 +106,44 @@ static void recoverI2cBus(i2c_bus_t *i2cBus) delay(); } -static void initI2cMainBus(void) +static void initI2cBus(i2c_bus_t *i2cBus) { - CLOCK_EnableClock(I2C_MAIN_BUS_SDA_CLOCK); - CLOCK_EnableClock(I2C_MAIN_BUS_SCL_CLOCK); + CLOCK_EnableClock(i2cBus->sdaClock); + CLOCK_EnableClock(i2cBus->sclClock); - recoverI2cBus(&i2cMainBus); + recoverI2cBus(i2cBus); port_pin_config_t pinConfig = { .pullSelect = kPORT_PullUp, .openDrainEnable = kPORT_OpenDrainEnable, - .mux = I2C_MAIN_BUS_MUX, + .mux = i2cBus->mux, }; - PORT_SetPinConfig(I2C_MAIN_BUS_SDA_PORT, I2C_MAIN_BUS_SDA_PIN, &pinConfig); - PORT_SetPinConfig(I2C_MAIN_BUS_SCL_PORT, I2C_MAIN_BUS_SCL_PIN, &pinConfig); + PORT_SetPinConfig(i2cBus->sdaPort, i2cBus->sdaPin, &pinConfig); + PORT_SetPinConfig(i2cBus->sclPort, i2cBus->sclPin, &pinConfig); i2c_master_config_t masterConfig; I2C_MasterGetDefaultConfig(&masterConfig); - masterConfig.baudRate_Bps = I2cMainBusRequestedBaudRateBps; - uint32_t sourceClock = CLOCK_GetFreq(I2C_MAIN_BUS_CLK_SRC); - I2C_MasterInit(I2C_MAIN_BUS_BASEADDR, &masterConfig, sourceClock); - I2cMainBusActualBaudRateBps = I2C_ActualBaudRate; + masterConfig.baudRate_Bps = i2cBus == &i2cMainBus ? I2cMainBusRequestedBaudRateBps : I2C_EEPROM_BUS_BAUD_RATE; + uint32_t sourceClock = CLOCK_GetFreq(i2cBus->clockSrc); + I2C_MasterInit(i2cBus->baseAddr, &masterConfig, sourceClock); + + if (i2cBus == &i2cMainBus) { + I2cMainBusActualBaudRateBps = I2C_ActualBaudRate; + } } void ReinitI2cMainBus(void) { I2C_MasterDeinit(I2C_MAIN_BUS_BASEADDR); - initI2cMainBus(); + initI2cBus(&i2cMainBus); InitSlaveScheduler(); } -static void initI2cEepromBus(void) -{ - port_pin_config_t pinConfig = { - .pullSelect = kPORT_PullUp, - .openDrainEnable = kPORT_OpenDrainEnable, - .mux = I2C_EEPROM_BUS_MUX, - }; - - CLOCK_EnableClock(I2C_EEPROM_BUS_SDA_CLOCK); - CLOCK_EnableClock(I2C_EEPROM_BUS_SCL_CLOCK); - - recoverI2cBus(&i2cEepromBus); - - PORT_SetPinConfig(I2C_EEPROM_BUS_SDA_PORT, I2C_EEPROM_BUS_SDA_PIN, &pinConfig); - PORT_SetPinConfig(I2C_EEPROM_BUS_SCL_PORT, I2C_EEPROM_BUS_SCL_PIN, &pinConfig); - - i2c_master_config_t masterConfig; - I2C_MasterGetDefaultConfig(&masterConfig); - masterConfig.baudRate_Bps = I2C_EEPROM_BUS_BAUD_RATE; - uint32_t sourceClock = CLOCK_GetFreq(I2C_EEPROM_BUS_CLK_SRC); - I2C_MasterInit(I2C_EEPROM_BUS_BASEADDR, &masterConfig, sourceClock); -} - static void initI2c(void) { - initI2cMainBus(); - initI2cEepromBus(); + initI2cBus(&i2cMainBus); + initI2cBus(&i2cEepromBus); } void InitPeripherals(void) diff --git a/right/src/init_peripherals.h b/right/src/init_peripherals.h index 8e79f86..1969261 100644 --- a/right/src/init_peripherals.h +++ b/right/src/init_peripherals.h @@ -1,21 +1,27 @@ #ifndef __INIT_PERIPHERALS_H__ #define __INIT_PERIPHERALS_H__ -// Includes +// Includes: #include "fsl_common.h" -// Typedefs +// Typedefs: -typedef struct { - GPIO_Type *sdaGpio; - PORT_Type *sdaPort; - uint32_t sdaPin; + typedef struct { + clock_name_t clockSrc; + I2C_Type *baseAddr; + uint16_t mux; - GPIO_Type *sclGpio; - PORT_Type *sclPort; - uint32_t sclPin; -} i2c_bus_t; + clock_ip_name_t sdaClock; + GPIO_Type *sdaGpio; + PORT_Type *sdaPort; + uint32_t sdaPin; + + clock_ip_name_t sclClock; + GPIO_Type *sclGpio; + PORT_Type *sclPort; + uint32_t sclPin; + } i2c_bus_t; // Variables: