i2c_master_poll.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. ******************************************************************************
  3. * @file i2c_master_poll.c
  4. * @author MCD Application Team
  5. * @version V0.0.3
  6. * @date Oct 2010
  7. * @brief This file contains optimized drivers for I2C master
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  19. */
  20. #include "i2c_master_poll.h"
  21. /* Таймаут ожидания события I2C */
  22. extern __IO uint8_t I2C_timeout;
  23. /* flag clearing sequence - uncoment next for peripheral clock under 2MHz */
  24. #define dead_time() { /* _asm("nop"); _asm("nop"); */ }
  25. #define tout() (I2C_timeout)
  26. #define set_tout_ms(a) { I2C_timeout = a; }
  27. /******************************************************************************
  28. * Function name : I2C_Init
  29. * Description : Initialize I2C peripheral
  30. * Input param : None
  31. * Return : None
  32. * See also : None
  33. *******************************************************************************/
  34. void I2C_Init(void) {
  35. //define SDA, SCL outputs, HiZ, Open drain, Fast
  36. GPIOB->ODR |= (GPIO_PIN_4 | GPIO_PIN_5);
  37. GPIOB->DDR |= (GPIO_PIN_4 | GPIO_PIN_5);
  38. GPIOB->CR2 |= (GPIO_PIN_4 | GPIO_PIN_5);
  39. #ifdef FAST_I2C_MODE
  40. I2C->FREQR = 16; // input clock to I2C - 16MHz
  41. I2C->CCRL = 15; // 900/62.5= 15, (SCLhi must be at least 600+300=900ns!)
  42. I2C->CCRH = 0x80; // fast mode, duty 2/1 (bus speed 62.5*3*15~356kHz)
  43. I2C->TRISER = 5; // 300/62.5 + 1= 5 (maximum 300ns)
  44. #else
  45. I2C->FREQR = 8; // input clock to I2C - 8MHz
  46. I2C->CCRL = 40; // CCR= 40 - (SCLhi must be at least 4000+1000=5000ns!)
  47. I2C->CCRH = 0; // standard mode, duty 1/1 bus speed 100kHz
  48. I2C->TRISER = 9; // 1000ns/(125ns) + 1 (maximum 1000ns)
  49. #endif
  50. I2C->OARL = 0xA0; // own address A0;
  51. I2C->OARH |= 0x40;
  52. //I2C->ITR = 1; // enable error interrupts
  53. I2C->CR2 |= 0x04; // ACK=1, Ack enable
  54. I2C->CR1 |= 0x01; // PE=1
  55. }
  56. /******************************************************************************
  57. * Function name : I2C_ReadRegister
  58. * Description : Read defined number bytes from slave memory starting with defined offset
  59. * Input param : offset in slave memory, number of bytes to read, starting address to store received data
  60. * Return : None
  61. * See also : None
  62. *******************************************************************************/
  63. void I2C_ReadRegister(u8 u8_regAddr, u8 u8_NumByteToRead, u8 *ReadBuffer)
  64. {
  65. /*--------------- BUSY? -> STOP request ---------------------*/
  66. while(I2C->SR3 & I2C_SR3_BUSY && tout()) // Wait while the bus is busy
  67. {
  68. I2C->CR2 |= I2C_CR2_STOP; // Generate stop here (STOP=1)
  69. while(I2C->CR2 & I2C_CR2_STOP && tout()); // Wait until stop is performed
  70. }
  71. I2C->CR2 |= I2C_CR2_ACK; // ACK=1, Ack enable
  72. /*--------------- Start communication -----------------------*/
  73. I2C->CR2 |= I2C_CR2_START; // START=1, generate start
  74. while((I2C->SR1 & I2C_SR1_SB)==0 && tout()); // Wait for start bit detection (SB)
  75. /*------------------ Address send ---------------------------*/
  76. if(tout())
  77. {
  78. I2C->DR = (u8)(SLAVE_ADDRESS << 1); // Send 7-bit device address & Write (R/W = 0)
  79. }
  80. while(!(I2C->SR1 & I2C_SR1_ADDR) && tout()); // test EV6 - wait for address ack (ADDR)
  81. dead_time(); // ADDR clearing sequence
  82. I2C->SR3;
  83. /*--------------- Register/Command send ----------------------*/
  84. while(!(I2C->SR1 & I2C_SR1_TXE) && tout()); // Wait for TxE
  85. if(tout())
  86. {
  87. I2C->DR = u8_regAddr; // Send register address
  88. } // Wait for TxE & BTF
  89. while((I2C->SR1 & (I2C_SR1_TXE | I2C_SR1_BTF)) != (I2C_SR1_TXE | I2C_SR1_BTF) && tout());
  90. dead_time(); // clearing sequence
  91. /*-------------- Stop/Restart communication -------------------*/
  92. #ifdef NO_RESTART // if 7bit address and NO_RESTART setted
  93. I2C->CR2 |= I2C_CR2_STOP; // STOP=1, generate stop
  94. while(I2C->CR2 & I2C_CR2_STOP && tout()); // wait until stop is performed
  95. #endif // NO_RESTART
  96. /*--------------- Restart communication ---------------------*/
  97. I2C->CR2 |= I2C_CR2_START; // START=1, generate re-start
  98. while((I2C->SR1 & I2C_SR1_SB)==0 && tout()); // Wait for start bit detection (SB)
  99. /*------------------ Address send ---------------------------*/
  100. if(tout())
  101. {
  102. I2C->DR = (u8)(SLAVE_ADDRESS << 1) | 1; // Send 7-bit device address & Write (R/W = 1)
  103. }
  104. while(!(I2C->SR1 & I2C_SR1_ADDR) && tout()); // Wait for address ack (ADDR)
  105. /*------------------- Data Receive --------------------------*/
  106. if (u8_NumByteToRead > 2) // *** more than 2 bytes are received? ***
  107. {
  108. I2C->SR3; // ADDR clearing sequence
  109. while(u8_NumByteToRead > 3 && tout()) // not last three bytes?
  110. {
  111. while(!(I2C->SR1 & I2C_SR1_BTF) && tout()); // Wait for BTF
  112. *ReadBuffer++ = I2C->DR; // Reading next data byte
  113. --u8_NumByteToRead; // Decrease Numbyte to reade by 1
  114. }
  115. //last three bytes should be read
  116. while(!(I2C->SR1 & I2C_SR1_BTF) && tout()); // Wait for BTF
  117. I2C->CR2 &=~I2C_CR2_ACK; // Clear ACK
  118. disableInterrupts(); // Errata workaround (Disable interrupt)
  119. *ReadBuffer++ = I2C->DR; // Read 1st byte
  120. I2C->CR2 |= I2C_CR2_STOP; // Generate stop here (STOP=1)
  121. *ReadBuffer++ = I2C->DR; // Read 2nd byte
  122. enableInterrupts(); // Errata workaround (Enable interrupt)
  123. while(!(I2C->SR1 & I2C_SR1_RXNE) && tout()); // Wait for RXNE
  124. *ReadBuffer++ = I2C->DR; // Read 3rd Data byte
  125. }
  126. else
  127. {
  128. if(u8_NumByteToRead == 2) // *** just two bytes are received? ***
  129. {
  130. I2C->CR2 |= I2C_CR2_POS; // Set POS bit (NACK at next received byte)
  131. disableInterrupts(); // Errata workaround (Disable interrupt)
  132. I2C->SR3; // Clear ADDR Flag
  133. I2C->CR2 &=~I2C_CR2_ACK; // Clear ACK
  134. enableInterrupts(); // Errata workaround (Enable interrupt)
  135. while(!(I2C->SR1 & I2C_SR1_BTF) && tout()); // Wait for BTF
  136. disableInterrupts(); // Errata workaround (Disable interrupt)
  137. I2C->CR2 |= I2C_CR2_STOP; // Generate stop here (STOP=1)
  138. *ReadBuffer++ = I2C->DR; // Read 1st Data byte
  139. enableInterrupts(); // Errata workaround (Enable interrupt)
  140. *ReadBuffer = I2C->DR; // Read 2nd Data byte
  141. }
  142. else // *** only one byte is received ***
  143. {
  144. I2C->CR2 &=~I2C_CR2_ACK;; // Clear ACK
  145. disableInterrupts(); // Errata workaround (Disable interrupt)
  146. I2C->SR3; // Clear ADDR Flag
  147. I2C->CR2 |= I2C_CR2_STOP; // generate stop here (STOP=1)
  148. enableInterrupts(); // Errata workaround (Enable interrupt)
  149. while(!(I2C->SR1 & I2C_SR1_RXNE) && tout()); // test EV7, wait for RxNE
  150. *ReadBuffer = I2C->DR; // Read Data byte
  151. }
  152. }
  153. /*--------------- All Data Received -----------------------*/
  154. while((I2C->CR2 & I2C_CR2_STOP) && tout()); // Wait until stop is performed (STOPF = 1)
  155. I2C->CR2 &=~I2C_CR2_POS; // return POS to default state (POS=0)
  156. }
  157. /******************************************************************************
  158. * Function name : I2C_WriteRegister
  159. * Description : write defined number bytes to slave memory starting with defined offset
  160. * Input param : offset in slave memory, number of bytes to write, starting address to send
  161. * Return : None.
  162. * See also : None.
  163. *******************************************************************************/
  164. void I2C_WriteRegister(u8 u8_regAddr, u8 u8_NumByteToWrite, u8 *ReadBuffer)
  165. {
  166. while((I2C->SR3 & 2) && tout()) // Wait while the bus is busy
  167. {
  168. I2C->CR2 |= 2; // STOP=1, generate stop
  169. while((I2C->CR2 & 2) && tout()); // wait until stop is performed
  170. }
  171. I2C->CR2 |= 1; // START=1, generate start
  172. while(((I2C->SR1 & 1)==0) && tout()); // Wait for start bit detection (SB)
  173. dead_time(); // SB clearing sequence
  174. if(tout())
  175. {
  176. I2C->DR = (u8)(SLAVE_ADDRESS << 1); // Send 7-bit device address & Write (R/W = 0)
  177. }
  178. while(!(I2C->SR1 & 2) && tout()); // Wait for address ack (ADDR)
  179. dead_time(); // ADDR clearing sequence
  180. I2C->SR3;
  181. while(!(I2C->SR1 & 0x80) && tout()); // Wait for TxE
  182. if(tout())
  183. {
  184. I2C->DR = u8_regAddr; // send Offset command
  185. }
  186. if(u8_NumByteToWrite)
  187. {
  188. while(u8_NumByteToWrite--)
  189. { // write data loop start
  190. while(!(I2C->SR1 & 0x80) && tout()); // test EV8 - wait for TxE
  191. I2C->DR = *ReadBuffer++; // send next data byte
  192. } // write data loop end
  193. }
  194. while(((I2C->SR1 & 0x84) != 0x84) && tout()); // Wait for TxE & BTF
  195. dead_time(); // clearing sequence
  196. I2C->CR2 |= 2; // generate stop here (STOP=1)
  197. while((I2C->CR2 & 2) && tout()); // wait until stop is performed
  198. }
  199. /**
  200. * @brief
  201. *
  202. * @param NumByteToRead
  203. * @param ReadBuffer
  204. */
  205. void I2C_ReadBytes(const u8 Addr, const u8 NumByteToRead, u8 *ReadBuffer) {
  206. u8 adr = Addr << 1;
  207. u8 n = NumByteToRead;
  208. set_tout_ms(10);
  209. /*--------------- BUSY? -> STOP request ---------------------*/
  210. while(I2C->SR3 & I2C_SR3_BUSY && tout()) // Wait while the bus is busy
  211. {
  212. I2C->CR2 |= I2C_CR2_STOP; // Generate stop here (STOP=1)
  213. while(I2C->CR2 & I2C_CR2_STOP && tout()); // Wait until stop is performed
  214. }
  215. I2C->CR2 |= I2C_CR2_ACK; // ACK=1, Ack enable
  216. /*--------------- Start communication -----------------------*/
  217. I2C->CR2 |= I2C_CR2_START; // START=1, generate start
  218. while((I2C->SR1 & I2C_SR1_SB)==0 && tout()); // Wait for start bit detection (SB)
  219. /*------------------ Address send ---------------------------*/
  220. if(tout())
  221. {
  222. I2C->DR = (adr | 1); // Send 7-bit device address & Write (R/W = 1)
  223. }
  224. while(!(I2C->SR1 & I2C_SR1_ADDR) && tout()); // Wait for address ack (ADDR)
  225. /*------------------- Data Receive --------------------------*/
  226. if (n > 2) // *** more than 2 bytes are received? ***
  227. {
  228. I2C->SR3; // ADDR clearing sequence
  229. while(n > 3 && tout()) // not last three bytes?
  230. {
  231. while(!(I2C->SR1 & I2C_SR1_BTF) && tout()); // Wait for BTF
  232. *ReadBuffer++ = I2C->DR; // Reading next data byte
  233. --n; // Decrease Numbyte to reade by 1
  234. }
  235. //last three bytes should be read
  236. while(!(I2C->SR1 & I2C_SR1_BTF) && tout()); // Wait for BTF
  237. I2C->CR2 &=~I2C_CR2_ACK; // Clear ACK
  238. disableInterrupts(); // Errata workaround (Disable interrupt)
  239. *ReadBuffer++ = I2C->DR; // Read 1st byte
  240. I2C->CR2 |= I2C_CR2_STOP; // Generate stop here (STOP=1)
  241. *ReadBuffer++ = I2C->DR; // Read 2nd byte
  242. enableInterrupts(); // Errata workaround (Enable interrupt)
  243. while(!(I2C->SR1 & I2C_SR1_RXNE) && tout()); // Wait for RXNE
  244. *ReadBuffer++ = I2C->DR; // Read 3rd Data byte
  245. }
  246. else
  247. {
  248. if(n == 2) // *** just two bytes are received? ***
  249. {
  250. I2C->CR2 |= I2C_CR2_POS; // Set POS bit (NACK at next received byte)
  251. disableInterrupts(); // Errata workaround (Disable interrupt)
  252. I2C->SR3; // Clear ADDR Flag
  253. I2C->CR2 &=~I2C_CR2_ACK; // Clear ACK
  254. enableInterrupts(); // Errata workaround (Enable interrupt)
  255. while(!(I2C->SR1 & I2C_SR1_BTF) && tout()); // Wait for BTF
  256. disableInterrupts(); // Errata workaround (Disable interrupt)
  257. I2C->CR2 |= I2C_CR2_STOP; // Generate stop here (STOP=1)
  258. *ReadBuffer++ = I2C->DR; // Read 1st Data byte
  259. enableInterrupts(); // Errata workaround (Enable interrupt)
  260. *ReadBuffer = I2C->DR; // Read 2nd Data byte
  261. }
  262. else // *** only one byte is received ***
  263. {
  264. I2C->CR2 &=~I2C_CR2_ACK;; // Clear ACK
  265. disableInterrupts(); // Errata workaround (Disable interrupt)
  266. I2C->SR3; // Clear ADDR Flag
  267. I2C->CR2 |= I2C_CR2_STOP; // generate stop here (STOP=1)
  268. enableInterrupts(); // Errata workaround (Enable interrupt)
  269. while(!(I2C->SR1 & I2C_SR1_RXNE) && tout()); // test EV7, wait for RxNE
  270. *ReadBuffer = I2C->DR; // Read Data byte
  271. }
  272. }
  273. /*--------------- All Data Received -----------------------*/
  274. while((I2C->CR2 & I2C_CR2_STOP) && tout()); // Wait until stop is performed (STOPF = 1)
  275. I2C->CR2 &= ~I2C_CR2_POS; // return POS to default state (POS=0)
  276. }
  277. /**
  278. * @brief write defined number bytes to slave device
  279. *
  280. * @param Addr
  281. * @param NumByteToWrite
  282. * @param DataBuffer
  283. */
  284. void I2C_WriteBytes(const u8 Addr, const u8 NumByteToWrite, u8 *DataBuffer) {
  285. u8 adr = Addr << 1;
  286. u8 n = NumByteToWrite;
  287. set_tout_ms(10);
  288. while((I2C->SR3 & 2) && tout()) // Wait while the bus is busy
  289. {
  290. I2C->CR2 |= 2; // STOP=1, generate stop
  291. while((I2C->CR2 & 2) && tout()); // wait until stop is performed
  292. }
  293. I2C->CR2 |= 1; // START=1, generate start
  294. while(((I2C->SR1 & 1)==0) && tout()); // Wait for start bit detection (SB)
  295. dead_time(); // SB clearing sequence
  296. if(tout())
  297. {
  298. I2C->DR = adr; // Send 7-bit device address & Write (R/W = 0)
  299. }
  300. while(!(I2C->SR1 & 2) && tout()); // Wait for address ack (ADDR)
  301. dead_time(); // ADDR clearing sequence
  302. I2C->SR3;
  303. if(n)
  304. {
  305. while(n--)
  306. { // write data loop start
  307. while(!(I2C->SR1 & 0x80) && tout()); // test EV8 - wait for TxE
  308. I2C->DR = *DataBuffer++; // send next data byte
  309. } // write data loop end
  310. }
  311. while(((I2C->SR1 & 0x84) != 0x84) && tout()); // Wait for TxE & BTF
  312. dead_time(); // clearing sequence
  313. I2C->CR2 |= 2; // generate stop here (STOP=1)
  314. while((I2C->CR2 & 2) && tout()); // wait until stop is performed
  315. }