i2c.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-09-21 JoyChen First version, support I2C1
  9. */
  10. #include <rtthread.h>
  11. #include "i2c.h"
  12. #include "stm32f2xx_rcc.h"
  13. #include "stm32f2xx_i2c.h"
  14. #include "stm32f2xx_dma.h"
  15. #define EV_SB 1
  16. #define EV_ADDR (1<<1)
  17. #define EV_STOPF (1<<2)
  18. #define EV_BTF (1<<3)
  19. #define ERR_ARLO (1<<4)
  20. #define ERR_AF (1<<5)
  21. #define ERR_OVR (1<<6)
  22. #define ERR_PECERR (1<<7)
  23. #define ERR_BERR (1<<8)
  24. #define I2C_COMPLETE (1<<9)
  25. #define I2C_BUSY 1
  26. #define I2C_FREE 2
  27. #define I2C_WRITE 0
  28. #define I2C_READ_DMA 1
  29. #define I2C_READ_POLLING 2
  30. #define I2C_READ_INTERRUPT 3
  31. #define I2C_TRACE(...)
  32. enum i2c_state {S1=0, S2, S2_1, S2_2, S3, S4, S5, S6, S_STOP};
  33. extern void rt_hw_led_on(rt_uint32_t n);
  34. extern void rt_hw_led_off(rt_uint32_t n);
  35. DMA_InitTypeDef I2CDMA_InitStructure;
  36. uint32_t I2CDirection = I2C_DIRECTION_TX;
  37. uint32_t i2cErrorNo = 0;
  38. struct rt_event i2c_event;
  39. static rt_mutex_t i2c_mux;
  40. __IO uint8_t DevAddr;
  41. static uint8_t* i2c_buf, *MemAddr, i2cStatus, i2cFlag, i2cPhase, memtype, i2c1_init_flag = 0;
  42. static uint32_t BufSize;
  43. I2C_ProgrammingModel I2CMode = DMA;
  44. Status I2C_Free_Bus(I2C_TypeDef* I2Cx, u32 timeout );
  45. void I2C_DMAConfig(I2C_TypeDef* I2Cx, uint8_t* pBuffer, uint32_t BufferSize, uint32_t Direction);
  46. void dump_i2c_register(I2C_TypeDef* I2Cx)
  47. {
  48. if(I2Cx == I2C1 )
  49. I2C_TRACE("======I2C1======\n");
  50. else
  51. I2C_TRACE("======I2C2======\n");
  52. I2C_TRACE("CR1: 0x%x\tCR2: 0x%x\n", I2Cx->CR1, I2Cx->CR2);
  53. I2C_TRACE("SR1: 0x%x\tSR2: 0x%x\n", I2Cx->SR1, I2Cx->SR2);
  54. }
  55. /*TODO: If your device need more time to initialize I2C bus or waiting memory write, you can use I2C_AcknowledgePolling avoid I2C bus lose.*/
  56. Status I2C_AcknowledgePolling(I2C_TypeDef* I2Cx ,uint8_t Addr)
  57. {
  58. uint32_t timeout = 0xFFFF, ret;
  59. uint16_t tmp;
  60. ret = rt_mutex_take(i2c_mux, RT_WAITING_FOREVER );
  61. if( ret == RT_EOK )
  62. {
  63. do{
  64. if( timeout-- <= 0 )
  65. {
  66. I2C_ClearFlag(I2Cx,I2C_FLAG_AF);
  67. I2Cx->CR1 |= CR1_STOP_Set;
  68. rt_mutex_release(i2c_mux);
  69. return Error;
  70. }
  71. I2Cx->CR1 |= CR1_START_Set;
  72. tmp = I2Cx->SR1;//MSB
  73. I2Cx->DR = Addr;
  74. }while((I2Cx->SR1&0x0002) != 0x0002);
  75. I2C_ClearFlag(I2Cx,I2C_FLAG_AF);
  76. I2Cx->CR1 |= CR1_STOP_Set;
  77. while ((I2Cx->CR1&0x200) == 0x200);
  78. rt_kprintf( "AcknowledgePolling OK\n");
  79. rt_mutex_release(i2c_mux);
  80. return Success;
  81. }
  82. else
  83. return Error;
  84. }
  85. /*
  86. Only 1 byte READ using Interrupt or Polling otherwise using DMA
  87. */
  88. void I2C1_EV_IRQHandler()
  89. {
  90. __IO uint16_t regSR1, regSR2;
  91. __IO uint32_t regSR;
  92. int i=10;
  93. rt_interrupt_enter();
  94. //rt_hw_led_on(10);
  95. regSR1 = I2C1->SR1;
  96. regSR2 = I2C1->SR2;
  97. regSR = (regSR2 << 16) | regSR1;
  98. //rt_kprintf("EV=> SR1: 0x%x\tSR2: 0x%x\tSR: 0x%x status: %d\n", regSR1, regSR2, regSR, i2cStatus);
  99. if( (regSR & I2C_EVENT_MASTER_MODE_SELECT) == I2C_EVENT_MASTER_MODE_SELECT) //EV5
  100. {
  101. if( i2cStatus == S1 ) //Send TX Command
  102. {
  103. I2C1->DR = DevAddr & 0xFE;
  104. i2cStatus = S2;
  105. }
  106. else if( i2cStatus == S4 ) //Send RX Command
  107. {
  108. I2C1->DR = DevAddr | 0x01;
  109. i2cStatus = S5;
  110. }
  111. regSR1 = 0;
  112. regSR2 = 0;
  113. }
  114. if( (regSR & I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)== I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ) //EV6
  115. {
  116. switch( i2cStatus )
  117. {
  118. case S2: //Send 1st memory address phase
  119. {
  120. //I2C_DMACmd(I2C1, ENABLE);
  121. I2C1->DR = MemAddr[0];
  122. if( memtype == I2C_MEM_1Byte )
  123. i2cStatus = S2_2;
  124. else if( memtype == I2C_MEM_2Bytes )
  125. i2cStatus = S2_1;
  126. }
  127. break;
  128. case S5: //Set RX buffer phase
  129. {
  130. if( i2cFlag == I2C_READ_DMA )
  131. {
  132. I2C_DMAConfig(I2C1, i2c_buf, BufSize, I2C_DIRECTION_RX);
  133. I2C1->CR2 |= CR2_LAST_Set | CR2_DMAEN_Set;
  134. DMA_ITConfig( I2C1_DMA_CHANNEL_RX, DMA_IT_TC, ENABLE);
  135. }
  136. else if( i2cFlag == I2C_READ_INTERRUPT )
  137. {
  138. I2C1->CR2 |= I2C_IT_BUF;
  139. I2C1->CR1 &= CR1_ACK_Reset;
  140. /* Program the STOP */
  141. I2C1->CR1 |= CR1_STOP_Set;
  142. }
  143. i2cStatus = S6;
  144. }
  145. break;
  146. }
  147. regSR1 = 0;
  148. regSR2 = 0;
  149. //dump_i2c_register(I2C1);
  150. }
  151. if((regSR & I2C_EVENT_MASTER_BYTE_RECEIVED) == I2C_EVENT_MASTER_BYTE_RECEIVED) //EV7
  152. {
  153. //Interrupt RX complete phase
  154. if( i2cStatus == S6 && i2cFlag == I2C_READ_INTERRUPT )
  155. {
  156. *i2c_buf = I2C1->DR;
  157. i2cStatus = S_STOP;
  158. rt_event_send(&i2c_event, I2C_COMPLETE);
  159. }
  160. }
  161. if( (regSR & I2C_EVENT_MASTER_BYTE_TRANSMITTED) == I2C_EVENT_MASTER_BYTE_TRANSMITTED ) //EV8_2
  162. {
  163. //Start TX/RX phase
  164. if(i2cStatus == S3)
  165. {
  166. DMA_ClearFlag(I2C1_DMA_CHANNEL_TX, DMA_FLAG_TCIF6 );
  167. DMA_Cmd(I2C1_DMA_CHANNEL_TX, DISABLE);
  168. switch (i2cFlag)
  169. {
  170. case I2C_WRITE:
  171. i2cStatus = S_STOP;
  172. I2C1->CR1 |= CR1_STOP_Set;
  173. rt_event_send(&i2c_event, I2C_COMPLETE);
  174. break;
  175. case I2C_READ_DMA:
  176. i2cStatus = S4;
  177. I2C1->CR1 |= CR1_START_Set;
  178. break;
  179. case I2C_READ_POLLING:
  180. i2cStatus = S_STOP;
  181. rt_event_send(&i2c_event, I2C_COMPLETE);
  182. I2C1->CR2 &= ~(CR2_LAST_Set | I2C_IT_EVT | CR2_DMAEN_Set);
  183. I2C1->CR1 |= CR1_START_Set;
  184. break;
  185. case I2C_READ_INTERRUPT:
  186. i2cStatus = S4;
  187. I2C1->CR1 |= CR1_START_Set;
  188. break;
  189. }
  190. }
  191. if( i2cStatus == S2_1 ) //Send 2nd memory address
  192. {
  193. if( memtype == I2C_MEM_2Bytes ) //memory address has 2 bytes
  194. {
  195. I2C1->DR = MemAddr[1];
  196. i2cStatus = S2_2;
  197. }
  198. if( i2cFlag == I2C_READ_POLLING || i2cFlag == I2C_READ_DMA || i2cFlag == I2C_READ_INTERRUPT)
  199. {
  200. i2cStatus = S3;
  201. }
  202. }
  203. if( i2cStatus == S2_2 ) //Set TX DAM phase
  204. {
  205. I2C_DMAConfig(I2C1, i2c_buf, BufSize, I2C_DIRECTION_TX);
  206. I2C1->CR2 |= CR2_DMAEN_Set;
  207. i2cStatus = S3;
  208. }
  209. }
  210. rt_interrupt_leave();
  211. }
  212. void DMA1_Stream6_IRQHandler(void) //I2C1 TX
  213. {
  214. rt_interrupt_enter();
  215. if (DMA_GetITStatus(I2C1_DMA_CHANNEL_TX, DMA_IT_TCIF6))
  216. {
  217. I2C_TRACE("TXTC\n");
  218. DMA_ClearFlag(I2C1_DMA_CHANNEL_TX, DMA_FLAG_TCIF6 );
  219. }
  220. rt_interrupt_leave();
  221. }
  222. void DMA1_Stream0_IRQHandler(void) //I2C1 RX
  223. {
  224. rt_interrupt_enter();
  225. if (DMA_GetITStatus(I2C1_DMA_CHANNEL_RX, DMA_IT_TCIF0))
  226. {
  227. I2C_TRACE("RXTC\n");
  228. /* clear DMA flag */
  229. DMA_ClearFlag(I2C1_DMA_CHANNEL_RX, DMA_FLAG_TCIF0 );
  230. DMA_ITConfig( I2C1_DMA_CHANNEL_RX, DMA_IT_TC, DISABLE);
  231. DMA_Cmd(I2C1_DMA_CHANNEL_RX, DISABLE);
  232. if( i2cStatus == S6 )
  233. {
  234. i2cStatus = S_STOP;
  235. I2C1->CR1 |= CR1_STOP_Set;
  236. rt_event_send(&i2c_event, I2C_COMPLETE);
  237. }
  238. }
  239. if (DMA_GetITStatus(I2C1_DMA_CHANNEL_RX, DMA_IT_HTIF0))
  240. {
  241. I2C_TRACE("RXHT\n");
  242. DMA_ClearFlag(I2C1_DMA_CHANNEL_RX, DMA_FLAG_HTIF0 );
  243. }
  244. if (DMA_GetITStatus(I2C1_DMA_CHANNEL_RX, DMA_IT_TEIF0))
  245. {
  246. I2C_TRACE("RXTE\n");
  247. DMA_ClearFlag(I2C1_DMA_CHANNEL_RX, DMA_FLAG_TEIF0 );
  248. }
  249. if (DMA_GetITStatus(I2C1_DMA_CHANNEL_RX, DMA_IT_FEIF0))
  250. {
  251. I2C_TRACE("RXFE\n");
  252. DMA_ClearFlag(I2C1_DMA_CHANNEL_RX, DMA_FLAG_FEIF0 );
  253. }
  254. if (DMA_GetITStatus(I2C1_DMA_CHANNEL_RX, DMA_IT_DMEIF0))
  255. {
  256. I2C_TRACE("RXDME\n");
  257. DMA_ClearFlag(I2C1_DMA_CHANNEL_RX, DMA_FLAG_DMEIF0 );
  258. }
  259. rt_interrupt_leave();
  260. }
  261. void I2C1_ER_IRQHandler()
  262. {
  263. __IO uint16_t regSR1, regSR2;
  264. i2cErrorNo = 0;
  265. regSR1 = I2C1->SR1;
  266. I2C_TRACE("I2C Error SR1= 0x%X CR1 = 0x%X\n" , regSR1, I2C1->CR1);
  267. if( (regSR1 & SR1_AF_Set) == SR1_AF_Set)
  268. {
  269. I2C1->SR1 &= ~SR1_AF_Set;
  270. i2cErrorNo |= ERR_AF;
  271. I2C_TRACE("ACK failure\n");
  272. }
  273. if( (regSR1 & SR1_BERR_Set) == SR1_BERR_Set)
  274. {
  275. I2C1->SR1 &= ~SR1_BERR_Set;
  276. i2cErrorNo |= ERR_BERR;
  277. I2C_TRACE("Bus Error\n");
  278. }
  279. if( (regSR1 & SR1_ARLO_Set) == SR1_ARLO_Set)
  280. {
  281. I2C1->SR1 &= ~SR1_ARLO_Set;
  282. i2cErrorNo |= ERR_ARLO;
  283. I2C_TRACE("Arblitation lost\n");
  284. }
  285. //dump_i2c_register(I2C1);
  286. }
  287. Status I2C_Free_Bus(I2C_TypeDef* I2Cx, u32 timeout )
  288. {
  289. /*u32 i = 0;
  290. u16 tmp = 0;
  291. GPIO_InitTypeDef GPIO_InitStructure;
  292. tmp = I2Cx->SR2;
  293. while( tmp & SR2_BUSY )
  294. {
  295. if( i++ < timeout )
  296. {
  297. if( I2Cx == I2C1 )
  298. {
  299. //rt_kprintf("Free Bus!\n");
  300. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  301. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  302. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  303. GPIO_Init(GPIOB, &GPIO_InitStructure);
  304. GPIO_SetBits(GPIOB, GPIO_Pin_6);
  305. GPIO_SetBits(GPIOB, GPIO_Pin_7);
  306. }
  307. else if( I2Cx == I2C2 )
  308. {
  309. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
  310. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  311. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  312. GPIO_Init(GPIOB, &GPIO_InitStructure);
  313. GPIO_ResetBits(GPIOB, GPIO_Pin_10);
  314. }
  315. rt_thread_delay(10);
  316. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  317. GPIO_Init(GPIOB, &GPIO_InitStructure);
  318. I2C_Cmd(I2Cx, DISABLE);
  319. I2C_Cmd(I2Cx, ENABLE);
  320. }
  321. else
  322. return Error;
  323. tmp = I2Cx->SR2;
  324. } */
  325. return Success;
  326. }
  327. /*
  328. I2Cx: I2C1 or I2C2 (Now it only support I2C1)
  329. pBuffer: Buffer point
  330. NumByteToRW: Number of bytes read/write
  331. memAddr: 1-2 bytes memory address
  332. SlaveAddress: device address
  333. MemType: 1 = memory address size 1 bytes, 2 = memory address size 2 bytes
  334. */
  335. Status I2C_IORW(I2C_TypeDef* I2Cx, uint8_t* pBuffer, uint32_t NumByteToRW, uint16_t memAddr, uint8_t SlaveAddress, uint8_t MemType )
  336. {
  337. uint32_t ev, Timeout=0xFFFF;
  338. uint16_t temp, temp2;
  339. static uint32_t call_cnt = 0, i;
  340. Status ret;
  341. ret = rt_mutex_take(i2c_mux, RT_WAITING_FOREVER );
  342. if( ret == RT_EOK )
  343. {
  344. ret = Success;
  345. DevAddr = SlaveAddress;
  346. BufSize = NumByteToRW;
  347. i2c_buf = pBuffer;
  348. memtype = MemType;
  349. MemAddr = (uint8_t*)&memAddr;
  350. I2CDirection = I2C_DIRECTION_TX;
  351. I2CMode = DMA;
  352. i2cStatus = S1;
  353. if( SlaveAddress & 0x01 )
  354. {
  355. if( BufSize == 1 )
  356. i2cFlag = I2C_READ_INTERRUPT; //I2C_READ_POLLING;
  357. else
  358. i2cFlag = I2C_READ_DMA;
  359. }
  360. else
  361. i2cFlag = I2C_WRITE;
  362. I2Cx->CR2 |= I2C_IT_ERR | I2C_IT_EVT;// | CR2_DMAEN_Set;
  363. I2Cx->CR1 |= CR1_START_Set;
  364. Timeout = 0xFFFF;
  365. if( rt_event_recv( &i2c_event, I2C_COMPLETE, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &ev ) != RT_EOK ) {ret = Error; goto i2cError;}
  366. if( i2cFlag == I2C_READ_POLLING )
  367. {
  368. while ((I2Cx->SR1&0x0001) != 0x0001)
  369. if (Timeout-- == 0) {ret = Error; goto i2cError;}
  370. Timeout = 0xFFFF;
  371. I2Cx->DR = DevAddr;
  372. /* Wait until ADDR is set: EV6 */
  373. while ((I2Cx->SR1&0x0002) != 0x0002)
  374. {
  375. if (Timeout-- == 0){ret = Error; goto i2cError;}
  376. }
  377. /* Clear ACK bit */
  378. I2Cx->CR1 &= CR1_ACK_Reset;
  379. /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
  380. software sequence must complete before the current byte end of transfer */
  381. __disable_irq();
  382. /* Clear ADDR flag */
  383. temp = I2Cx->SR2;
  384. /* Program the STOP */
  385. I2Cx->CR1 |= CR1_STOP_Set;
  386. /* Re-enable IRQs */
  387. __enable_irq();
  388. /* Wait until a data is received in DR register (RXNE = 1) EV7 */
  389. while ((I2Cx->SR1 & 0x00040) != 0x000040)if (Timeout-- == 0){ret = Error; goto i2cError;}
  390. /* Read the data */
  391. *i2c_buf = I2Cx->DR;
  392. /* Make sure that the STOP bit is cleared by Hardware before CR1 write access */
  393. while ((I2Cx->CR1&0x200) == 0x200)if (Timeout-- == 0){ret = Error; goto i2cError;}
  394. /* Enable Acknowledgement to be ready for another reception */
  395. I2Cx->CR1 |= CR1_ACK_Set;
  396. }
  397. else
  398. {
  399. while ((I2Cx->CR1&0x200) == 0x200)
  400. {
  401. if (Timeout-- == 0) {ret = Error; break;}
  402. }
  403. if( i2cFlag == I2C_READ_INTERRUPT )
  404. I2Cx->CR1 |= CR1_ACK_Set;
  405. }
  406. i2cError:
  407. if( ret == Error )
  408. {
  409. /* TODO: i2c error handler */
  410. /* Need check i2cErrorNo and Reset I2C bus */
  411. }
  412. I2Cx->CR2 &= ~CR2_FREQ_Reset;
  413. //dump_i2c_register(I2C1);
  414. rt_mutex_release(i2c_mux);
  415. return ret;
  416. }
  417. else
  418. return Error;
  419. }
  420. void I2C1_INIT()
  421. {
  422. GPIO_InitTypeDef GPIO_InitStructure;
  423. I2C_InitTypeDef I2C_InitStructure;
  424. NVIC_InitTypeDef NVIC_InitStructure;
  425. if( i2c1_init_flag == 0 )
  426. {
  427. /* Enable the I2C clock */
  428. RCC_APB1PeriphClockCmd(I2C1_CLK, ENABLE);
  429. /* GPIOB clock enable */
  430. RCC_AHB1PeriphClockCmd(I2C1_GPIO_CLK, ENABLE);
  431. /* Enable the DMA1 clock */
  432. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
  433. //Reset GPIO
  434. GPIO_InitStructure.GPIO_Pin = I2C1_SDA_PIN | I2C1_SCL_PIN;
  435. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  436. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  437. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  438. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  439. GPIO_Init(I2C1_GPIO_PORT, &GPIO_InitStructure);
  440. /* Connect PXx to I2C_SCL*/
  441. GPIO_PinAFConfig(I2C1_GPIO_PORT, I2C1_SDA_SOURCE, GPIO_AF_I2C1);
  442. /* Connect PXx to I2C_SDA*/
  443. GPIO_PinAFConfig(I2C1_GPIO_PORT, I2C1_SCL_SOURCE, GPIO_AF_I2C1);
  444. /* Enable I2C1 reset state */
  445. RCC_APB1PeriphResetCmd(I2C1_CLK, ENABLE);
  446. /* Release I2C1 from reset state */
  447. RCC_APB1PeriphResetCmd(I2C1_CLK, DISABLE);
  448. I2C_DeInit(I2C1);
  449. I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  450. I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  451. I2C_InitStructure.I2C_OwnAddress1 = OwnAddress1;
  452. I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  453. I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  454. I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
  455. I2C_Init(I2C1, &I2C_InitStructure);
  456. I2C_Cmd(I2C1, ENABLE);
  457. /* Configure and enable I2C1 event interrupt -------------------------------*/
  458. NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
  459. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  460. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  461. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  462. NVIC_Init(&NVIC_InitStructure);
  463. /* Configure and enable I2C1 DMA interrupt -------------------------------*/
  464. NVIC_InitStructure.NVIC_IRQChannel = I2C1_DMA_TX_IRQn;
  465. NVIC_Init(&NVIC_InitStructure);
  466. NVIC_InitStructure.NVIC_IRQChannel = I2C1_DMA_RX_IRQn;
  467. NVIC_Init(&NVIC_InitStructure);
  468. /* Configure and enable I2C1 error interrupt -------------------------------*/
  469. NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;
  470. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  471. NVIC_Init(&NVIC_InitStructure);
  472. /* I2C1 TX DMA Channel configuration */
  473. DMA_Cmd(I2C1_DMA_CHANNEL_TX, DISABLE);
  474. DMA_DeInit(I2C1_DMA_CHANNEL_TX);
  475. I2CDMA_InitStructure.DMA_Channel = DMA_Channel_1;
  476. I2CDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C1_DR_Address;
  477. I2CDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0; /* This parameter will be configured durig communication */
  478. I2CDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; /* This parameter will be configured durig communication */
  479. I2CDMA_InitStructure.DMA_BufferSize = 0xFFFF; /* This parameter will be configured durig communication */
  480. I2CDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  481. I2CDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  482. I2CDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  483. I2CDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  484. I2CDMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  485. I2CDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  486. //I2CDMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  487. I2CDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  488. I2CDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  489. I2CDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  490. I2CDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  491. DMA_Init(I2C1_DMA_CHANNEL_TX, &I2CDMA_InitStructure);
  492. /* I2C1 RX DMA Channel configuration */
  493. DMA_Cmd(I2C1_DMA_CHANNEL_RX, DISABLE);
  494. DMA_DeInit(I2C1_DMA_CHANNEL_RX);
  495. DMA_Init(I2C1_DMA_CHANNEL_RX, &I2CDMA_InitStructure);
  496. //I2C_AcknowledgePolling(I2C1, 0x70);
  497. rt_event_init(&i2c_event, "i2c_event", RT_IPC_FLAG_FIFO );
  498. i2c_mux = rt_mutex_create("i2c_mux", RT_IPC_FLAG_FIFO );
  499. i2c1_init_flag = 1;
  500. }
  501. }
  502. void I2C_DMAConfig(I2C_TypeDef* I2Cx, uint8_t* pBuffer, uint32_t BufferSize, uint32_t Direction)
  503. {
  504. I2CDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)pBuffer;
  505. I2CDMA_InitStructure.DMA_BufferSize = (uint32_t)BufferSize;
  506. /* Initialize the DMA with the new parameters */
  507. if (Direction == I2C_DIRECTION_TX)
  508. {
  509. /* Configure the DMA Tx Channel with the buffer address and the buffer size */
  510. I2CDMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  511. if (I2Cx == I2C1)
  512. {
  513. I2CDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C1_DR_Address;
  514. //DMA_Cmd(I2C1_DMA_CHANNEL_TX, DISABLE);
  515. DMA_Init(I2C1_DMA_CHANNEL_TX, &I2CDMA_InitStructure);
  516. DMA_Cmd(I2C1_DMA_CHANNEL_TX, ENABLE);
  517. }
  518. else
  519. {
  520. I2CDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C2_DR_Address;
  521. //DMA_Cmd(I2C2_DMA_CHANNEL_TX, DISABLE);
  522. DMA_Init(I2C2_DMA_CHANNEL_TX, &I2CDMA_InitStructure);
  523. DMA_Cmd(I2C2_DMA_CHANNEL_TX, ENABLE);
  524. }
  525. }
  526. else /* Reception */
  527. {
  528. /* Configure the DMA Rx Channel with the buffer address and the buffer size */
  529. I2CDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  530. if (I2Cx == I2C1)
  531. {
  532. I2CDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C1_DR_Address;
  533. //DMA_Cmd(I2C1_DMA_CHANNEL_RX, DISABLE);
  534. DMA_Init(I2C1_DMA_CHANNEL_RX, &I2CDMA_InitStructure);
  535. DMA_Cmd(I2C1_DMA_CHANNEL_RX, ENABLE);
  536. }
  537. else
  538. {
  539. I2CDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C2_DR_Address;
  540. // DMA_Cmd(I2C2_DMA_CHANNEL_RX, DISABLE);
  541. DMA_Init(I2C2_DMA_CHANNEL_RX, &I2CDMA_InitStructure);
  542. DMA_Cmd(I2C2_DMA_CHANNEL_RX, ENABLE);
  543. }
  544. }
  545. }