X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=boards%2Fstm32-p103%2Fhw%2Fhw_i2c_bitbang.h;h=a5aea90acb96d2509bf2252d26f31b6941923268;hb=b9e1e52093d33494bf4a8ca27c14a56a25b6e0bc;hp=4822af7c81462edea4647179aa79f75e0513df2e;hpb=a256e501c16b63a423267f8621d6bdcda20d3f3a;p=bertos.git diff --git a/boards/stm32-p103/hw/hw_i2c_bitbang.h b/boards/stm32-p103/hw/hw_i2c_bitbang.h index 4822af7c..a5aea90a 100644 --- a/boards/stm32-p103/hw/hw_i2c_bitbang.h +++ b/boards/stm32-p103/hw/hw_i2c_bitbang.h @@ -34,7 +34,6 @@ * \brief Macro for I2C bitbang operation. * * - * \version $Id$ * * \author Francesco Sacchi */ @@ -42,25 +41,116 @@ #ifndef HW_I2C_BITBANG_H #define HW_I2C_BITBANG_H -#warning TODO:This is an example implementation, you must implement it! +#include -#define SDA_HI do { /* Implement me:Set SDA High by setting SDA pin as input */ } while (0) -#define SDA_LO do { /* Implement me:Set SDA Low by setting SDA pin as open collector output */ } while (0) -#define SCL_HI do { /* Implement me:Set SCL High by setting SCL pin as input */ } while (0) -#define SCL_LO do { /* Implement me:Set SCL Low by setting SCL pin as open collector output */ } while (0) +#include +#include +#include +#include -#define SCL_IN (true) /* Implement me: read SDA pin state */ -#define SDA_IN (true) /* Implement me: read SCL pin state */ +#define SDA_PIN BV(0) +#define SCL_PIN BV(1) + +#define SDA_HI \ + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, SDA_PIN, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50MHZ) + +#define SDA_LO \ + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, SDA_PIN, GPIO_MODE_OUT_PP, GPIO_SPEED_50MHZ) + +#define SCL_HI \ + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, SCL_PIN, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50MHZ) + +#define SCL_LO \ + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, SCL_PIN, GPIO_MODE_OUT_PP, GPIO_SPEED_50MHZ) + + +#define SCL_IN \ + ({ \ + (stm32_gpioPinRead((struct stm32_gpio *)GPIOB_BASE, SCL_PIN)); \ + }) + +#define SDA_IN \ + ({ \ + (stm32_gpioPinRead((struct stm32_gpio *)GPIOB_BASE, SDA_PIN)); \ + }) /** * This macro should set SDA and SCL lines as input. */ -#define I2C_BITBANG_HW_INIT do { /* Implement me! */ } while (0) +#define I2C_BITBANG_HW_INIT \ + do { \ + RCC->APB2ENR |= RCC_APB2_GPIOB; \ + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, SDA_PIN | SCL_PIN, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50MHZ); \ + } while (0) + +/** + * Half bit delay routine used to generate the correct timings. + */ +#define I2C_HALFBIT_DELAY() \ + do { \ + timer_delay(1); \ + } while (0) + + +/* + * New api + */ +#include + +INLINE void i2c_sdaHi(int dev) +{ + (void)(dev); + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, SDA_PIN, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50MHZ); +} + +INLINE void i2c_sdaLo(int dev) +{ + (void)(dev); + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, SDA_PIN, GPIO_MODE_OUT_PP, GPIO_SPEED_50MHZ); +} + +INLINE void i2c_sclHi(int dev) +{ + (void)(dev); + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, SCL_PIN, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50MHZ); +} + +INLINE void i2c_sclLo(int dev) +{ + (void)(dev); + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, SCL_PIN, GPIO_MODE_OUT_PP, GPIO_SPEED_50MHZ); +} + +INLINE bool i2c_sdaIn(int dev) +{ + (void)(dev); + return stm32_gpioPinRead((struct stm32_gpio *)GPIOB_BASE, SDA_PIN); +} + +INLINE bool i2c_sclIn(int dev) +{ + (void)(dev); + return stm32_gpioPinRead((struct stm32_gpio *)GPIOB_BASE, SCL_PIN); +} /** * Half bit delay routine used to generate the correct timings. */ -#define I2C_HALFBIT_DELAY() do { /* Implement me! */ } while (0) +INLINE void i2c_halfbitDelay(int dev) +{ + (void)(dev); + timer_udelay(1); +} + +/** + * This macro should set SDA and SCL lines as input. + */ +INLINE void i2c_bitbangInit(int dev) +{ + (void)(dev); + RCC->APB2ENR |= RCC_APB2_GPIOB; + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, SDA_PIN | SCL_PIN, GPIO_MODE_IN_FLOATING, GPIO_SPEED_50MHZ); +} #endif /* HW_I2C_BITBANG_H */