X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=boards%2Fstm32-p103%2Fhw%2Fhw_i2c_bitbang.h;h=a5aea90acb96d2509bf2252d26f31b6941923268;hb=15810459b8f5fb09b12cef6f8e4d4e64d167087b;hp=8f3fbf5e88a5771876dd8a9130a20331e52b3329;hpb=b46f64914c62fbb0297728280478681659469654;p=bertos.git diff --git a/boards/stm32-p103/hw/hw_i2c_bitbang.h b/boards/stm32-p103/hw/hw_i2c_bitbang.h index 8f3fbf5e..a5aea90a 100644 --- a/boards/stm32-p103/hw/hw_i2c_bitbang.h +++ b/boards/stm32-p103/hw/hw_i2c_bitbang.h @@ -41,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 */