i2c.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. \file i2c.h
  3. \author G. Icking-Konert
  4. \date 2013-11-22
  5. \version 0.1
  6. \brief implementation of I2C functions/macros
  7. implementation of functions for I2C bus communication
  8. For I2C bus, see http://en.wikipedia.org/wiki/I2C
  9. */
  10. /*-----------------------------------------------------------------------------
  11. INCLUDE FILES
  12. -----------------------------------------------------------------------------*/
  13. #include "stm8s.h"
  14. /*-----------------------------------------------------------------------------
  15. DEFINITION OF GLOBAL MACROS/#DEFINES
  16. -----------------------------------------------------------------------------*/
  17. #define I2C_FAST 1
  18. #define F_MASTER_MHZ 16UL
  19. #define F_MASTER_HZ 16000000UL
  20. #ifdef I2C_FAST
  21. //400 kHz
  22. #define F_I2C_HZ 400000UL
  23. #else
  24. //100 kHz
  25. #define F_I2C_HZ 100000UL
  26. #endif // I2C_FAST
  27. //CCR = Fmaster/2*Fiic = 16MHz/2*100kHz = 80
  28. #define CCR (uint16_t)(F_MASTER_HZ / (2 * F_I2C_HZ))
  29. /* Таймаут ожидания события I2C */
  30. extern __IO uint8_t I2C_timeout;
  31. /*----------------------------------------------------------
  32. FUNCTIONS
  33. ----------------------------------------------------------*/
  34. static uint8_t i2c_read(void);
  35. /**
  36. \fn void i2c_init(void)
  37. \brief configure I2C bus
  38. configure I2C bus as master in standard mode, BR=100kHz, 7bit address, 250ns rise time
  39. */
  40. void i2c_init() {
  41. //Частота тактирования периферии MHz
  42. I2C->FREQR = (uint8_t)(F_MASTER_MHZ);
  43. //Отключаем I2C
  44. I2C->CR1 &= (uint8_t)(~I2C_CR1_PE);
  45. //Выбираем режим
  46. #ifdef I2C_FAST
  47. I2C->CCRH = I2C_CCRH_FS; // | I2C_CCRH_DUTY;
  48. #else
  49. I2C->CCRH = 0;
  50. #endif
  51. //Set Maximum Rise Time: 1000ns max in Standard Mode
  52. //= [1000ns/(1/InputClockFrequencyMHz.10e6)]+1
  53. //= InputClockFrequencyMHz+1
  54. I2C->TRISER = (uint8_t)(F_MASTER_MHZ + 1);
  55. I2C->CCRL = (uint8_t)CCR;
  56. I2C->CCRH |= (uint8_t)((uint8_t)(CCR >> 8) & I2C_CCRH_CCR);
  57. //Включаем I2C
  58. I2C->CR1 |= I2C_CR1_PE;
  59. //Разрешаем подтверждение в конце посылки
  60. I2C->CR2 |= I2C_CR2_ACK;
  61. } // i2c_init
  62. /**
  63. \fn uint8_t i2c_waitFree(void)
  64. \brief wait until bus is free
  65. \return error code (0=ok; 1=timeout)
  66. wait until bus is free with timeout
  67. */
  68. uint8_t i2c_waitFree() {
  69. // wait until bus free with timeout
  70. I2C_timeout = 10;
  71. while (((I2C->SR3 & I2C_SR3_BUSY)) && I2C_timeout);
  72. // on I2C timeout return error
  73. if ((I2C->SR3 & I2C_SR3_BUSY)) {
  74. i2c_init();
  75. return(1);
  76. }
  77. // return success
  78. return(0);
  79. } // i2c_waitFree
  80. /**
  81. \fn uint8_t i2c_start(void)
  82. \brief generate I2C start condition
  83. \return error code (0=ok; 1=timeout)
  84. generate I2C start condition with timeout
  85. */
  86. uint8_t i2c_start() {
  87. // wait until bus free with timeout
  88. I2C_timeout = 10;
  89. while (((I2C->SR3 & I2C_SR3_BUSY)) && I2C_timeout);
  90. // on I2C timeout return error
  91. if ((I2C->SR3 & I2C_SR3_BUSY)) {
  92. i2c_init();
  93. return(2);
  94. }
  95. // generate start condition with timeout
  96. I2C->CR2 |= I2C_CR2_START;
  97. I2C_timeout = 10;
  98. while (!(I2C->SR1 & I2C_SR1_SB) && I2C_timeout);
  99. // on I2C timeout return error
  100. if (!(I2C->SR1 & I2C_SR1_SB)) {
  101. i2c_init();
  102. return(1);
  103. }
  104. // return success
  105. return(0);
  106. } // i2c_start
  107. /**
  108. \fn uint8_t i2c_stop(void)
  109. \brief generate I2C stop condition
  110. \return error code (0=ok; 1=timeout)
  111. generate I2C stop condition with timeout
  112. */
  113. uint8_t i2c_stop() {
  114. // generate stop condition with timeout
  115. I2C->CR2 |= I2C_CR2_STOP;
  116. I2C_timeout = 10;
  117. while ((I2C->SR3 & I2C_SR3_MSL) && I2C_timeout);
  118. // on I2C timeout set error flag
  119. if (I2C->SR3 & I2C_SR3_MSL) {
  120. i2c_init();
  121. return(1);
  122. }
  123. // return success
  124. return(0);
  125. } // i2c_stop
  126. /**
  127. \fn uint8_t i2c_send(uint8_t addr, uint8_t numTx, uint8_t *bufTx)
  128. \brief write data via I2C
  129. \param[in] addr 7b address [6:0] of I2C slave
  130. \param[in] numTx number of bytes to send
  131. \param[in] bufTx send buffer
  132. \return error code (0=ok; 1=timeout)
  133. write data to I2C slave with frame timeout. Note that no start or
  134. stop condition is generated.
  135. */
  136. uint8_t i2c_send(uint8_t addr, uint8_t numTx, uint8_t *bufTx) {
  137. uint8_t i;
  138. // send 7b slave adress [7:1] + write flag (LSB=0)
  139. I2C->DR = (uint8_t) ((addr << 1) & ~0x01); // shift left and set LSB (write=0, read=1)
  140. I2C_timeout = 10;
  141. while ((!(I2C->SR1 & I2C_SR1_ADDR)) && I2C_timeout); // wait until address sent or timeout
  142. I2C_timeout = 10;
  143. while ((!(I2C->SR3 & I2C_SR3_BUSY)) && I2C_timeout); // seems to be required...???
  144. // no I2C timeout -> send data
  145. if (I2C_timeout) {
  146. for (i=0; i<numTx; i++) {
  147. I2C->DR = bufTx[i];
  148. I2C_timeout = 2;
  149. while ((!(I2C->SR1 & I2C_SR1_TXE)) && I2C_timeout); // wait until DR buffer empty or timeout
  150. }
  151. }
  152. // on I2C timeout return error
  153. else {
  154. i2c_init();
  155. return(1);
  156. }
  157. // return success
  158. return(0);
  159. } // i2c_send
  160. static uint8_t i2c_read(void) {
  161. I2C->CR2 &= ~I2C_CR2_ACK;
  162. i2c_stop();
  163. while (!(I2C->SR1 & I2C_SR1_RXNE));
  164. return I2C->DR;
  165. }
  166. /**
  167. \fn uint8_t i2c_request(uint8_t slaveAddr, uint8_t numRx, uint8_t *bufRx)
  168. \brief request data via I2C as master with i2c_stop()
  169. \param[in] slaveAddr 7b address [6:0] of I2C slave
  170. \param[in] numRx number of bytes to receive
  171. \param[out] bufRx receive buffer
  172. \return error code (0=ok; 1=timeout)
  173. request data from I2C slave with frame timeout. Note that no start or
  174. stop condition is generated.
  175. */
  176. uint8_t i2c_request(const uint8_t slaveAddr, const uint8_t numRx, uint8_t *bufRx) {
  177. uint8_t i = numRx;
  178. // send 7b slave adress [7:1] + read flag (LSB=1)
  179. I2C->DR = (uint8_t) ((slaveAddr << 1) | 0x01); // shift left and set LSB (write=0, read=1)
  180. I2C_timeout = 10;
  181. while ((!(I2C->SR1 & I2C_SR1_ADDR)) && I2C_timeout); // wait until adress sent or timeout
  182. I2C_timeout = 2;
  183. while ((!(I2C->SR3 & I2C_SR3_BUSY)) && I2C_timeout); // seems to be required...???
  184. while (i-- > 1) {
  185. I2C_timeout = 2;
  186. while ((!(I2C->SR1 & I2C_SR1_RXNE)) && I2C_timeout); // wait until DR buffer not empty or timeout
  187. *(bufRx++) = I2C->DR;
  188. }
  189. *bufRx = i2c_read();
  190. // on I2C timeout return error
  191. if (I2C_timeout == 0) {
  192. i2c_init();
  193. return(1);
  194. }
  195. // return success
  196. return(0);
  197. } // i2c_request
  198. /*-----------------------------------------------------------------------------
  199. END OF MODULE
  200. -----------------------------------------------------------------------------*/