i2c.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. ******************************************************************************
  3. * @file : i2c.c
  4. * @brief : I2C base functions
  5. ******************************************************************************
  6. */
  7. #include "main.h"
  8. /* Private macros */
  9. #define ENABLE_TIM TIM17->CNT=1; TIM17->CR1 |= TIM_CR1_CEN
  10. #define TIM_CNT TIM17->CNT
  11. /* Private variables */
  12. static i2c_status_t st_Read = I2C_Ret_OK;
  13. /* Private function prototypes */
  14. static i2c_status_t i2c_check_err(void);
  15. static inline void i2c_start(void) {
  16. // Send 'Start' condition, and wait for acknowledge.
  17. I2C1->CR2 |= (I2C_CR2_START);
  18. while ((I2C1->CR2 & I2C_CR2_START)) {}
  19. }
  20. static inline void i2c_stop(void) {
  21. // Send 'Stop' condition, and wait for acknowledge.
  22. I2C1->CR2 |= (I2C_CR2_STOP);
  23. while ((I2C1->CR2 & I2C_CR2_STOP)) {}
  24. // Reset the ICR ('Interrupt Clear Register') event flag.
  25. I2C1->ICR |= (I2C_ICR_STOPCF);
  26. while ((I2C1->ICR & I2C_ICR_STOPCF)) {}
  27. }
  28. static void i2c_write_byte(uint8_t dat);
  29. static uint8_t i2c_read_byte(void);
  30. static uint8_t i2c_read_register(uint8_t reg_addr);
  31. /**
  32. * @brief Check I2C bus for errors.
  33. * @retval I2C return code
  34. */
  35. static i2c_status_t i2c_check_err(void) {
  36. i2c_status_t r = I2C_Ret_OK;
  37. if ((I2C1->ISR & I2C_ISR_NACKF) != 0) {
  38. /* device not present */
  39. r = I2C_Ret_NACK;
  40. } else if ((I2C1->ISR & I2C_ISR_TIMEOUT) != 0) {
  41. /* timeout error */
  42. r = I2C_Ret_Tout;
  43. } else if ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR)) != 0) {
  44. /* other error */
  45. r = I2C_Ret_Err;
  46. }
  47. if (r != I2C_Ret_OK) {
  48. /* restart I2C and clear flags */
  49. I2C1->CR1 &= ~I2C_CR1_PE;
  50. while ((I2C1->CR1 & I2C_CR1_PE) != 0);
  51. I2C1->CR1 |= I2C_CR1_PE;
  52. }
  53. return r;
  54. }
  55. /**
  56. * @brief Read len bytes from I2C bus to data by reg_addr.
  57. * @retval I2C return code
  58. */
  59. i2c_status_t user_i2c_read(const uint8_t id, const uint8_t reg_addr, uint8_t *data, uint16_t len) {
  60. /* wait for i2c */
  61. while ( I2C1->ISR & I2C_ISR_BUSY ) { __NOP(); };
  62. st_Read = I2C_Ret_OK;
  63. Flag.I2C_RX_End = 0;
  64. Flag.I2C_RX_Err = 0;
  65. Flag.I2C_TX_Err = 0;
  66. /* prepare i2c for sending reg addr */
  67. I2C1->CR2 &= ~(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
  68. I2C1->CR2 |= (id | (1 << I2C_CR2_NBYTES_Pos));
  69. i2c_start();
  70. /* wait for byte request or any error */
  71. while ((I2C1->ISR & (I2C_ISR_TIMEOUT | I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) {
  72. __NOP();
  73. }
  74. if ((I2C1->ISR & I2C_ISR_TXE) != 0) {
  75. /* device ok, send reg addr */
  76. I2C1->TXDR = reg_addr;
  77. } else {
  78. st_Read = i2c_check_err();
  79. Flag.I2C_TX_Err = 1;
  80. Flag.I2C_RX_End = 1;
  81. return st_Read;
  82. }
  83. /* wait for i2c or any error */
  84. while (((I2C1->ISR & I2C_ISR_BUSY) != 0) && ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF)) == 0)) { __NOP(); };
  85. st_Read = i2c_check_err();
  86. if (st_Read != I2C_Ret_OK) {
  87. Flag.I2C_TX_Err = 1;
  88. Flag.I2C_RX_End = 1;
  89. return st_Read;
  90. }
  91. /* prepare i2c for receiving data */
  92. I2C1->CR2 &= ~(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
  93. I2C1->CR2 |= (id | (len << I2C_CR2_NBYTES_Pos) | I2C_CR2_RD_WRN);
  94. /* launch receiving */
  95. i2c_start();
  96. /* receiving data */
  97. while (len) {
  98. while ((I2C1->ISR & (I2C_ISR_NACKF | I2C_ISR_RXNE))==0) { __NOP(); }
  99. if (I2C1->ISR & I2C_ISR_RXNE) { // Reading data
  100. *data = I2C1->RXDR;
  101. data ++;
  102. len --;
  103. } else {
  104. st_Read = i2c_check_err();
  105. Flag.I2C_RX_Err = 1;
  106. Flag.I2C_RX_End = 1;
  107. return st_Read;
  108. }
  109. }
  110. Flag.I2C_RX_End = 1;
  111. return st_Read;
  112. }
  113. /**
  114. * @brief Write len bytes to I2C bus from data by reg_addr.
  115. * @retval I2C return code
  116. */
  117. i2c_status_t user_i2c_write(const uint8_t id, const uint8_t reg_addr, uint8_t *data, uint16_t len) {
  118. i2c_status_t r = I2C_Ret_OK;
  119. while ( I2C1->ISR & I2C_ISR_BUSY );
  120. Flag.I2C_TX_End = 0;
  121. Flag.I2C_TX_Err = 0;
  122. I2C1->CR2 &= ~(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
  123. I2C1->CR2 |= (id | ((len + 1) << I2C_CR2_NBYTES_Pos ));
  124. i2c_start();
  125. while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) {
  126. __NOP();
  127. }
  128. if ((I2C1->ISR & I2C_ISR_TXE) != 0) {
  129. I2C1->TXDR = reg_addr;
  130. } else {
  131. r = i2c_check_err();
  132. Flag.I2C_TX_Err = 1;
  133. Flag.I2C_TX_End = 1;
  134. return r;
  135. }
  136. while (len) {
  137. while ((I2C1->ISR & (I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); }
  138. if ((I2C1->ISR & I2C_ISR_TXE) != 0) {
  139. I2C1->TXDR = *data;
  140. data ++;
  141. len --;
  142. } else {
  143. r = i2c_check_err();
  144. Flag.I2C_TX_Err = 1;
  145. Flag.I2C_TX_End = 1;
  146. return r;
  147. }
  148. }
  149. Flag.I2C_TX_End = 1;
  150. return r;
  151. }
  152. static void i2c_write_byte(uint8_t dat) {
  153. I2C1->TXDR = (I2C1->TXDR & 0xFFFFFF00) | dat;
  154. // Wait for one of these ISR bits:
  155. // 'TXIS' ("ready for next byte")
  156. // 'TC' ("transfer complete")
  157. while (!(I2C1->ISR & (I2C_ISR_TXIS | I2C_ISR_TC))) {}
  158. // (Also of interest: 'TXE' ("TXDR register is empty") and
  159. // 'TCR' ("transfer complete, and 'RELOAD' is set."))
  160. }
  161. static uint8_t i2c_read_byte(void) {
  162. // Wait for a byte of data to be available, then read it.
  163. while (!(I2C1->ISR & I2C_ISR_RXNE)) {}
  164. return (I2C1->RXDR & 0xFF);
  165. }
  166. static uint8_t i2c_read_register(uint8_t reg_addr) {
  167. // Set '1 byte to send.'
  168. I2C1->CR2 &= ~(I2C_CR2_NBYTES);
  169. I2C1->CR2 |= (0x01 << I2C_CR2_NBYTES_Pos);
  170. // Start the I2C write transmission.
  171. i2c_start();
  172. // Send the register address.
  173. i2c_write_byte(reg_addr);
  174. // Stop the I2C write transmission.
  175. i2c_stop();
  176. // Set '1 byte to receive.'
  177. I2C1->CR2 &= ~(I2C_CR2_NBYTES);
  178. I2C1->CR2 |= (0x01 << I2C_CR2_NBYTES_Pos);
  179. // Set 'read' I2C direction.
  180. I2C1->CR2 |= (I2C_CR2_RD_WRN);
  181. // Start the I2C read transmission.
  182. i2c_start();
  183. // Read the transmitted data.
  184. uint8_t read_result = i2c_read_byte();
  185. // Stop the I2C read transmission.
  186. i2c_stop();
  187. // Set 'write' I2C direction again.
  188. I2C1->CR2 &= ~(I2C_CR2_RD_WRN);
  189. // Return the read value.
  190. return read_result;
  191. }