i2c.c 18 KB

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