stm32f1_i2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * File : stm32f1_i2c.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2017, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-07-04 aubrcool@qq.com 1st version
  13. */
  14. #include "stm32f10x.h"
  15. #include "stm32f1_i2c.h"
  16. #include <rtdevice.h>
  17. #ifdef RT_USING_I2C
  18. /* I2C SPE mask */
  19. #define CR1_PE_Set ((uint16_t)0x0001)
  20. #define CR1_PE_Reset ((uint16_t)0xFFFE)
  21. /* I2C START mask */
  22. #define CR1_START_Set ((uint16_t)0x0100)
  23. #define CR1_START_Reset ((uint16_t)0xFEFF)
  24. /* I2C STOP mask */
  25. #define CR1_STOP_Set ((uint16_t)0x0200)
  26. #define CR1_STOP_Reset ((uint16_t)0xFDFF)
  27. /* I2C ACK mask */
  28. #define CR1_ACK_Set ((uint16_t)0x0400)
  29. #define CR1_ACK_Reset ((uint16_t)0xFBFF)
  30. /* I2C ENGC mask */
  31. #define CR1_ENGC_Set ((uint16_t)0x0040)
  32. #define CR1_ENGC_Reset ((uint16_t)0xFFBF)
  33. /* I2C SWRST mask */
  34. #define CR1_SWRST_Set ((uint16_t)0x8000)
  35. #define CR1_SWRST_Reset ((uint16_t)0x7FFF)
  36. /* I2C PEC mask */
  37. #define CR1_PEC_Set ((uint16_t)0x1000)
  38. #define CR1_PEC_Reset ((uint16_t)0xEFFF)
  39. /* I2C ENPEC mask */
  40. #define CR1_ENPEC_Set ((uint16_t)0x0020)
  41. #define CR1_ENPEC_Reset ((uint16_t)0xFFDF)
  42. /* I2C ENARP mask */
  43. #define CR1_ENARP_Set ((uint16_t)0x0010)
  44. #define CR1_ENARP_Reset ((uint16_t)0xFFEF)
  45. /* I2C NOSTRETCH mask */
  46. #define CR1_NOSTRETCH_Set ((uint16_t)0x0080)
  47. #define CR1_NOSTRETCH_Reset ((uint16_t)0xFF7F)
  48. /* I2C registers Masks */
  49. #define CR1_CLEAR_Mask ((uint16_t)0xFBF5)
  50. /* I2C DMAEN mask */
  51. #define CR2_DMAEN_Set ((uint16_t)0x0800)
  52. #define CR2_DMAEN_Reset ((uint16_t)0xF7FF)
  53. /* I2C LAST mask */
  54. #define CR2_LAST_Set ((uint16_t)0x1000)
  55. #define CR2_LAST_Reset ((uint16_t)0xEFFF)
  56. /* I2C FREQ mask */
  57. #define CR2_FREQ_Reset ((uint16_t)0xFFC0)
  58. /* I2C ADD0 mask */
  59. #define OAR1_ADD0_Set ((uint16_t)0x0001)
  60. #define OAR1_ADD0_Reset ((uint16_t)0xFFFE)
  61. /* I2C ENDUAL mask */
  62. #define OAR2_ENDUAL_Set ((uint16_t)0x0001)
  63. #define OAR2_ENDUAL_Reset ((uint16_t)0xFFFE)
  64. /* I2C ADD2 mask */
  65. #define OAR2_ADD2_Reset ((uint16_t)0xFF01)
  66. /* I2C F/S mask */
  67. #define CCR_FS_Set ((uint16_t)0x8000)
  68. /* I2C CCR mask */
  69. #define CCR_CCR_Set ((uint16_t)0x0FFF)
  70. /* I2C FLAG mask */
  71. #define FLAG_Mask ((uint32_t)0x00FFFFFF)
  72. /* I2C Interrupt Enable mask */
  73. #define ITEN_Mask ((uint32_t)0x07000000)
  74. #define I2CADDR 0x0A
  75. enum
  76. {
  77. EV_END = 0,
  78. };
  79. #ifdef RT_USING_I2C1
  80. static struct stm32_i2c_bus stm32_i2c1 =
  81. {
  82. .I2C = I2C1,
  83. };
  84. #endif /*RT_USING_I2C1*/
  85. #ifdef RT_USING_I2C2
  86. static struct stm32_i2c_bus stm32_i2c2 =
  87. {
  88. .I2C = I2C2,
  89. };
  90. #endif /*RT_USING_I2C2*/
  91. rt_inline rt_err_t stm32_i2c_wait_ev(struct stm32_i2c_bus *bus,
  92. rt_uint32_t ev, rt_uint32_t timeout)
  93. {
  94. rt_uint32_t res = 0;
  95. rt_event_recv(&bus->ev, 0x01 << ev,
  96. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  97. timeout, &res);
  98. if(res != ev)
  99. {
  100. return RT_ERROR;
  101. }
  102. else
  103. {
  104. return RT_EOK;
  105. }
  106. }
  107. rt_inline void stm32_i2c_send_ev(struct stm32_i2c_bus *bus, rt_uint32_t ev)
  108. {
  109. rt_event_send(&bus->ev, 0x01 << ev);
  110. }
  111. static rt_size_t stm_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  112. struct rt_i2c_msg msgs[],
  113. rt_uint32_t num);
  114. static rt_size_t stm_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
  115. struct rt_i2c_msg msgs[],
  116. rt_uint32_t num);
  117. static rt_err_t stm_i2c_bus_control(struct rt_i2c_bus_device *bus,
  118. rt_uint32_t,
  119. rt_uint32_t);
  120. static const struct rt_i2c_bus_device_ops stm32_i2c_ops =
  121. {
  122. stm_i2c_mst_xfer,
  123. stm_i2c_slv_xfer,
  124. stm_i2c_bus_control,
  125. };
  126. rt_inline void stm32_i2c_disable_nvic(I2C_TypeDef *I2C, rt_uint32_t value)
  127. {
  128. NVIC_InitTypeDef NVIC_InitStructure;
  129. rt_uint32_t evno, erno;
  130. if(I2C == I2C1)
  131. {
  132. evno = I2C1_EV_IRQn;
  133. erno = I2C1_ER_IRQn;
  134. }
  135. else
  136. {
  137. evno = I2C2_EV_IRQn;
  138. erno = I2C2_ER_IRQn;
  139. }
  140. NVIC_InitStructure.NVIC_IRQChannel = evno;
  141. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  142. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
  143. NVIC_InitStructure.NVIC_IRQChannelCmd = value;
  144. NVIC_Init(&NVIC_InitStructure);
  145. NVIC_InitStructure.NVIC_IRQChannel = erno;
  146. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  147. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
  148. NVIC_InitStructure.NVIC_IRQChannelCmd = value;
  149. NVIC_Init(&NVIC_InitStructure);
  150. }
  151. static void stm32_i2c_nvic_Config(I2C_TypeDef *I2C)
  152. {
  153. stm32_i2c_disable_nvic(I2C, ENABLE);
  154. }
  155. static rt_err_t stm_i2c_init(struct rt_i2c_bus_device *bus, rt_uint32_t bitrate)
  156. {
  157. struct stm32_i2c_bus *stm32_i2c;
  158. I2C_InitTypeDef I2C_InitStructure;
  159. RT_ASSERT(bus != RT_NULL);
  160. if(bitrate != 100000 && bitrate != 400000)
  161. {
  162. return RT_EIO;
  163. }
  164. stm32_i2c = (struct stm32_i2c_bus *) bus;
  165. I2C_Cmd(stm32_i2c->I2C, DISABLE);
  166. I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  167. I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  168. I2C_InitStructure.I2C_OwnAddress1 = I2CADDR;
  169. I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  170. I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  171. I2C_InitStructure.I2C_ClockSpeed = bitrate;
  172. I2C_Init(stm32_i2c->I2C, &I2C_InitStructure);
  173. I2C_Cmd(stm32_i2c->I2C, ENABLE);
  174. I2C_ITConfig(stm32_i2c->I2C, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR, ENABLE);
  175. stm32_i2c_nvic_Config(stm32_i2c->I2C);
  176. return RT_EOK;
  177. }
  178. static rt_size_t stm_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  179. struct rt_i2c_msg msgs[],
  180. rt_uint32_t num)
  181. {
  182. struct stm32_i2c_bus *stm32_i2c;
  183. rt_uint32_t numbak = num;
  184. RT_ASSERT(bus != RT_NULL);
  185. stm32_i2c = (struct stm32_i2c_bus *) bus;
  186. stm32_i2c->msg = msgs;
  187. stm32_i2c->msg_ptr = 0;
  188. stm32_i2c->msg_cnt = num;
  189. stm32_i2c->dptr = 0;
  190. stm32_i2c->wait_stop = 0;
  191. I2C_GetLastEvent(stm32_i2c->I2C);
  192. while(stm32_i2c->msg_ptr < stm32_i2c->msg_cnt)
  193. {
  194. stm32_i2c->wait_stop = 0;
  195. if(!(stm32_i2c->msg[stm32_i2c->msg_ptr].flags & RT_I2C_NO_START))
  196. {
  197. stm32_i2c->I2C->CR1 |= CR1_START_Set;
  198. }
  199. stm32_i2c_wait_ev(stm32_i2c, EV_END, 1000);
  200. }
  201. stm32_i2c->msg = RT_NULL;
  202. stm32_i2c->msg_ptr = 0;
  203. stm32_i2c->msg_cnt = 0;
  204. stm32_i2c->dptr = 0;
  205. stm32_i2c->wait_stop = 0;
  206. return numbak;
  207. }
  208. static rt_size_t stm_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
  209. struct rt_i2c_msg msgs[],
  210. rt_uint32_t num)
  211. {
  212. return 0;
  213. }
  214. static rt_err_t stm_i2c_bus_control(struct rt_i2c_bus_device *bus,
  215. rt_uint32_t cmd,
  216. rt_uint32_t arg)
  217. {
  218. return RT_ERROR;
  219. }
  220. rt_inline void stm32_i2c_ev_handler(struct stm32_i2c_bus *stm32_i2c)
  221. {
  222. unsigned int I2C_Event;
  223. rt_uint8_t data = 0;
  224. struct rt_i2c_msg *pmsg;
  225. I2C_Event = I2C_GetLastEvent(stm32_i2c->I2C);
  226. if(!stm32_i2c->msg)
  227. {
  228. return;
  229. }
  230. // EV5 0x00030001
  231. if ((I2C_Event & I2C_EVENT_MASTER_MODE_SELECT) == I2C_EVENT_MASTER_MODE_SELECT)
  232. {
  233. // EV5 0x00030001
  234. pmsg = &stm32_i2c->msg[stm32_i2c->msg_ptr];
  235. if(pmsg->flags & RT_I2C_ADDR_10BIT)
  236. {
  237. data = ((pmsg->addr >> 8) << 1) | 0xF0;
  238. }
  239. else
  240. {
  241. data = pmsg->addr << 1;
  242. }
  243. if(pmsg->flags & RT_I2C_RD)
  244. {
  245. data |= 0x01;
  246. }
  247. stm32_i2c->I2C->DR = data;
  248. if(!(pmsg->flags & RT_I2C_RD))
  249. {
  250. return;
  251. }
  252. if(pmsg->len > 1)
  253. {
  254. stm32_i2c->I2C->CR1 |= CR1_ACK_Set;
  255. return;
  256. }
  257. }
  258. else if((I2C_Event & I2C_EVENT_MASTER_MODE_ADDRESS10) ==
  259. I2C_EVENT_MASTER_MODE_ADDRESS10)
  260. {
  261. // EV9
  262. data = pmsg->addr & 0xFF;
  263. stm32_i2c->I2C->DR = data;
  264. }
  265. else if((I2C_Event & I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) ==
  266. I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)
  267. {
  268. //EVT 6 SEND 0x00070082
  269. }
  270. else if ((I2C_Event & I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) ==
  271. I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)
  272. {
  273. //EVT 6 RECE 0x00030002
  274. pmsg = &stm32_i2c->msg[stm32_i2c->msg_ptr];
  275. if(!(pmsg->flags & RT_I2C_RD))
  276. {
  277. return;
  278. }
  279. if(pmsg->len > 1)
  280. {
  281. return;
  282. }
  283. if(stm32_i2c->msg_ptr < stm32_i2c->msg_cnt - 1)
  284. {
  285. return;
  286. }
  287. else if((pmsg[1].flags & RT_I2C_NO_START))
  288. {
  289. return;
  290. }
  291. stm32_i2c->I2C->CR1 |= CR1_STOP_Set;
  292. stm32_i2c->I2C->CR1 &= CR1_ACK_Reset;
  293. }
  294. else if ((I2C_Event & I2C_EVENT_MASTER_BYTE_RECEIVED) ==
  295. I2C_EVENT_MASTER_BYTE_RECEIVED)
  296. {
  297. // EVT 7 0x00030040
  298. pmsg = &stm32_i2c->msg[stm32_i2c->msg_ptr];
  299. if(pmsg->len && (pmsg->flags & RT_I2C_RD))
  300. {
  301. pmsg->buf[stm32_i2c->dptr] = stm32_i2c->I2C->DR;
  302. stm32_i2c->dptr++;
  303. pmsg->len--;
  304. }
  305. if(pmsg->len == 1 && (pmsg->flags & RT_I2C_RD))
  306. {
  307. if(stm32_i2c->msg_ptr >= stm32_i2c->msg_cnt - 1)
  308. {
  309. stm32_i2c->I2C->CR1 &= CR1_ACK_Reset;
  310. stm32_i2c->I2C->CR1 |= CR1_STOP_Set;
  311. }
  312. else if(!(pmsg[1].flags & RT_I2C_NO_START))
  313. {
  314. stm32_i2c->I2C->CR1 &= CR1_ACK_Reset;
  315. stm32_i2c->I2C->CR1 |= CR1_STOP_Set;
  316. }
  317. }
  318. if(pmsg->len)
  319. {
  320. return;
  321. }
  322. stm32_i2c->dptr = 0;
  323. stm32_i2c->msg_ptr++;
  324. if(stm32_i2c->msg_ptr < stm32_i2c->msg_cnt)
  325. {
  326. return;
  327. }
  328. stm32_i2c->I2C->CR1 |= CR1_ACK_Set;
  329. stm32_i2c_send_ev(stm32_i2c, EV_END);
  330. }
  331. else if((I2C_Event & I2C_EVENT_MASTER_BYTE_TRANSMITTING) ==
  332. I2C_EVENT_MASTER_BYTE_TRANSMITTING)
  333. {
  334. //EVT8 0x00070080
  335. if(stm32_i2c->wait_stop == 0xAAAA5555)
  336. {
  337. stm32_i2c->wait_stop = 0;
  338. stm32_i2c->I2C->CR1 |= CR1_STOP_Set;
  339. stm32_i2c_send_ev(stm32_i2c, EV_END);
  340. return;
  341. }
  342. if(stm32_i2c->wait_stop == 0x5555AAAA)
  343. { //restart cond
  344. stm32_i2c->wait_stop = 0;
  345. stm32_i2c_send_ev(stm32_i2c, EV_END);
  346. return;
  347. }
  348. pmsg = &stm32_i2c->msg[stm32_i2c->msg_ptr];
  349. if(!(pmsg->flags & RT_I2C_RD) && pmsg->len)
  350. {
  351. stm32_i2c->I2C->DR = pmsg->buf[stm32_i2c->dptr];
  352. stm32_i2c->dptr++;
  353. pmsg->len--;
  354. }
  355. if(!(pmsg->flags & RT_I2C_RD) && pmsg->len)
  356. {
  357. return;
  358. }
  359. if(stm32_i2c->msg_ptr < stm32_i2c->msg_cnt - 1 && pmsg->len == 0)
  360. {
  361. stm32_i2c->msg_ptr++;
  362. stm32_i2c->dptr = 0;
  363. pmsg = &stm32_i2c->msg[stm32_i2c->msg_ptr];
  364. if(pmsg->flags & RT_I2C_NO_START)
  365. {
  366. return;
  367. }
  368. else
  369. {
  370. stm32_i2c->wait_stop == 0x5555AAAA;
  371. return;
  372. }
  373. }
  374. if(stm32_i2c->msg_ptr < stm32_i2c->msg_cnt && pmsg->len == 0)
  375. {
  376. stm32_i2c->msg_ptr++;
  377. stm32_i2c->dptr = 0;
  378. }
  379. stm32_i2c->wait_stop = 0xAAAA5555;
  380. }
  381. }
  382. #ifdef RT_USING_I2C1
  383. void I2C1_EV_IRQHandler(void)
  384. {
  385. /* enter interrupt */
  386. rt_interrupt_enter();
  387. stm32_i2c_ev_handler(&stm32_i2c1);
  388. /* leave interrupt */
  389. rt_interrupt_leave();
  390. }
  391. #endif /*RT_USING_I2C1*/
  392. #ifdef RT_USING_I2C2
  393. void I2C2_EV_IRQHandler(void)
  394. {
  395. /* enter interrupt */
  396. rt_interrupt_enter();
  397. stm32_i2c_ev_handler(&stm32_i2c2);
  398. /* leave interrupt */
  399. rt_interrupt_leave();
  400. }
  401. #endif /*RT_USING_I2C2*/
  402. rt_inline void stm32_i2c_er_handler(struct stm32_i2c_bus *stm32_i2c)
  403. {
  404. if (I2C2->SR1 & 1 << 10)
  405. {
  406. I2C2->SR1 &= ~(1 << 10);
  407. }
  408. if (I2C2->SR1 & 1 << 14)
  409. {
  410. I2C2->SR1 &= ~(1 << 14);
  411. }
  412. if (I2C2->SR1 & 1 << 11)
  413. {
  414. I2C2->SR1 &= ~(1 << 11);
  415. }
  416. if (I2C2->SR1 & 1 << 9)
  417. {
  418. I2C2->SR1 &= ~(1 << 9);
  419. }
  420. if (I2C2->SR1 & 1 << 8)
  421. {
  422. I2C2->SR1 &= ~(1 << 8);
  423. }
  424. }
  425. #ifdef RT_USING_I2C1
  426. void I2C1_ER_IRQHandler(void) //I2C2 Error Interrupt
  427. {
  428. /* enter interrupt */
  429. rt_interrupt_enter();
  430. stm32_i2c_er_handler(&stm32_i2c1);
  431. /* leave interrupt */
  432. rt_interrupt_leave();
  433. }
  434. #endif /*RT_USING_I2C1*/
  435. #ifdef RT_USING_I2C2
  436. void I2C2_ER_IRQHandler(void) //I2C2 Error Interrupt
  437. {
  438. /* enter interrupt */
  439. rt_interrupt_enter();
  440. stm32_i2c_er_handler(&stm32_i2c2);
  441. /* leave interrupt */
  442. rt_interrupt_leave();
  443. }
  444. #endif /*RT_USING_I2C2*/
  445. rt_err_t stm32_i2c_register(I2C_TypeDef *I2C, rt_uint32_t bitrate,
  446. const char * i2c_bus_name)
  447. {
  448. struct stm32_i2c_bus *pi2c;
  449. rt_err_t res;
  450. #ifdef RT_USING_I2C1
  451. if(I2C == I2C1)
  452. {
  453. pi2c = &stm32_i2c1;
  454. RCC_APB2PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
  455. }
  456. else
  457. #endif /*RT_USING_I2C1*/
  458. #ifdef RT_USING_I2C2
  459. if(I2C == I2C2)
  460. {
  461. pi2c = &stm32_i2c2;
  462. RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
  463. }
  464. else
  465. #endif /*RT_USING_I2C2*/
  466. {
  467. return RT_ENOSYS;
  468. }
  469. if(rt_event_init(&pi2c->ev, i2c_bus_name, RT_IPC_FLAG_FIFO) != RT_EOK)
  470. {
  471. return RT_ERROR;
  472. }
  473. pi2c->parent.ops = &stm32_i2c_ops;
  474. if((res = stm_i2c_init(&pi2c->parent, bitrate)) != RT_EOK)
  475. {
  476. return res;
  477. }
  478. return rt_i2c_bus_device_register(&pi2c->parent, i2c_bus_name);
  479. }
  480. #endif /*RT_USING_I2C*/