i2c.c 18 KB

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