drv_hard_i2c.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-07-31 shelton first version
  9. */
  10. #include "drv_common.h"
  11. #include "drv_hard_i2c.h"
  12. #include "drv_config.h"
  13. #include <string.h>
  14. #if defined(BSP_USING_HARD_I2C1) || defined(BSP_USING_HARD_I2C2) || \
  15. defined(BSP_USING_HARD_I2C3)
  16. //#define DRV_DEBUG
  17. #define LOG_TAG "drv.hwi2c"
  18. #include <drv_log.h>
  19. enum
  20. {
  21. #ifdef BSP_USING_HARD_I2C1
  22. I2C1_INDEX,
  23. #endif
  24. #ifdef BSP_USING_HARD_I2C2
  25. I2C2_INDEX,
  26. #endif
  27. #ifdef BSP_USING_HARD_I2C3
  28. I2C3_INDEX,
  29. #endif
  30. };
  31. static struct at32_i2c_handle i2c_handle[] = {
  32. #ifdef BSP_USING_HARD_I2C1
  33. I2C1_CONFIG,
  34. #endif
  35. #ifdef BSP_USING_HARD_I2C2
  36. I2C2_CONFIG,
  37. #endif
  38. #ifdef BSP_USING_HARD_I2C3
  39. I2C3_CONFIG,
  40. #endif
  41. };
  42. static struct at32_i2c i2cs[sizeof(i2c_handle) / sizeof(i2c_handle[0])] = {0};
  43. /* private rt-thread i2c ops function */
  44. static rt_ssize_t master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num);
  45. static struct rt_i2c_bus_device_ops at32_i2c_ops =
  46. {
  47. master_xfer,
  48. RT_NULL,
  49. RT_NULL
  50. };
  51. static rt_err_t at32_i2c_configure(struct rt_i2c_bus_device *bus)
  52. {
  53. RT_ASSERT(RT_NULL != bus);
  54. struct at32_i2c *instance = rt_container_of(bus, struct at32_i2c, i2c_bus);
  55. at32_msp_i2c_init(instance->handle->i2c_x);
  56. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  57. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32F415) || \
  58. defined (SOC_SERIES_AT32F421) || defined (SOC_SERIES_AT32A403A)
  59. i2c_init(instance->handle->i2c_x, I2C_FSMODE_DUTY_2_1, instance->handle->timing);
  60. #endif
  61. #if defined (SOC_SERIES_AT32F402) || defined (SOC_SERIES_AT32F405) || \
  62. defined (SOC_SERIES_AT32F423) || defined (SOC_SERIES_AT32F425) || \
  63. defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  64. defined (SOC_SERIES_AT32A423) || defined (SOC_SERIES_AT32M412) || \
  65. defined (SOC_SERIES_AT32M416)
  66. i2c_init(instance->handle->i2c_x, 0x0F, instance->handle->timing);
  67. #endif
  68. i2c_own_address1_set(instance->handle->i2c_x, I2C_ADDRESS_MODE_7BIT, HWI2C_OWN_ADDRESS);
  69. nvic_irq_enable(instance->handle->ev_irqn, 0, 0);
  70. nvic_irq_enable(instance->handle->er_irqn, 0, 0);
  71. i2c_enable(instance->handle->i2c_x, TRUE);
  72. return RT_EOK;
  73. }
  74. static void i2c_dma_config(struct at32_i2c_handle *handle, rt_uint8_t *buffer, rt_uint32_t size)
  75. {
  76. struct dma_config *dma = RT_NULL;
  77. if(handle->comm.mode == I2C_DMA_MA_TX)
  78. {
  79. dma = handle->dma_tx;
  80. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  81. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32F415) || \
  82. defined (SOC_SERIES_AT32F421) || defined (SOC_SERIES_AT32A403A)
  83. dma->dma_channel->paddr = (rt_uint32_t)&(handle->i2c_x->dt);
  84. #endif
  85. #if defined (SOC_SERIES_AT32F402) || defined (SOC_SERIES_AT32F405) || \
  86. defined (SOC_SERIES_AT32F423) || defined (SOC_SERIES_AT32F425) || \
  87. defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  88. defined (SOC_SERIES_AT32A423) || defined (SOC_SERIES_AT32M412) || \
  89. defined (SOC_SERIES_AT32M416)
  90. dma->dma_channel->paddr = (rt_uint32_t)&(handle->i2c_x->txdt);
  91. #endif
  92. }
  93. else if(handle->comm.mode == I2C_DMA_MA_RX)
  94. {
  95. dma = handle->dma_rx;
  96. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  97. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32F415) || \
  98. defined (SOC_SERIES_AT32F421) || defined (SOC_SERIES_AT32A403A)
  99. dma->dma_channel->paddr = (rt_uint32_t)&(handle->i2c_x->dt);
  100. #endif
  101. #if defined (SOC_SERIES_AT32F402) || defined (SOC_SERIES_AT32F405) || \
  102. defined (SOC_SERIES_AT32F423) || defined (SOC_SERIES_AT32F425) || \
  103. defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  104. defined (SOC_SERIES_AT32A423) || defined (SOC_SERIES_AT32M412) || \
  105. defined (SOC_SERIES_AT32M416)
  106. dma->dma_channel->paddr = (rt_uint32_t)&(handle->i2c_x->rxdt);
  107. #endif
  108. }
  109. dma->dma_channel->dtcnt = size;
  110. dma->dma_channel->maddr = (rt_uint32_t)buffer;
  111. /* enable transmit complete interrupt */
  112. dma_interrupt_enable(dma->dma_channel, DMA_FDT_INT, TRUE);
  113. /* mark dma flag */
  114. dma->dma_done = RT_FALSE;
  115. /* enable dma channel */
  116. dma_channel_enable(dma->dma_channel, TRUE);
  117. }
  118. #if defined (SOC_SERIES_AT32F402) || defined (SOC_SERIES_AT32F405) || \
  119. defined (SOC_SERIES_AT32F423) || defined (SOC_SERIES_AT32F425) || \
  120. defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  121. defined (SOC_SERIES_AT32A423) || defined (SOC_SERIES_AT32M412) || \
  122. defined (SOC_SERIES_AT32M416)
  123. void i2c_refresh_txdt_register(i2c_type *i2c_x)
  124. {
  125. /* clear tdis flag */
  126. if (i2c_flag_get(i2c_x, I2C_TDIS_FLAG) != RESET)
  127. {
  128. i2c_x->txdt = 0x00;
  129. }
  130. /* refresh txdt register*/
  131. if (i2c_flag_get(i2c_x, I2C_TDBE_FLAG) == RESET)
  132. {
  133. i2c_x->sts_bit.tdbe = 1;
  134. }
  135. }
  136. void i2c_reset_ctrl2_register(i2c_type *i2c_x)
  137. {
  138. i2c_x->ctrl2_bit.saddr = 0;
  139. i2c_x->ctrl2_bit.readh10 = 0;
  140. i2c_x->ctrl2_bit.cnt = 0;
  141. i2c_x->ctrl2_bit.rlden = 0;
  142. i2c_x->ctrl2_bit.dir = 0;
  143. }
  144. #endif
  145. i2c_status_type i2c_wait_end(struct at32_i2c_handle *handle, uint32_t timeout)
  146. {
  147. while(handle->comm.status != I2C_END)
  148. {
  149. /* check timeout */
  150. if((timeout--) == 0)
  151. {
  152. return I2C_ERR_TIMEOUT;
  153. }
  154. }
  155. if(handle->comm.error_code != I2C_OK)
  156. {
  157. return handle->comm.error_code;
  158. }
  159. else
  160. {
  161. return I2C_OK;
  162. }
  163. }
  164. i2c_status_type i2c_wait_flag(struct at32_i2c_handle *handle, uint32_t flag, uint32_t event_check, uint32_t timeout)
  165. {
  166. if(flag == I2C_BUSYF_FLAG)
  167. {
  168. while(i2c_flag_get(handle->i2c_x, flag) != RESET)
  169. {
  170. /* check timeout */
  171. if((timeout--) == 0)
  172. {
  173. handle->comm.error_code = I2C_ERR_TIMEOUT;
  174. return I2C_ERR_TIMEOUT;
  175. }
  176. }
  177. }
  178. else
  179. {
  180. while(i2c_flag_get(handle->i2c_x, flag) == RESET)
  181. {
  182. /* check the ack fail flag */
  183. if(event_check & I2C_EVENT_CHECK_ACKFAIL)
  184. {
  185. if(i2c_flag_get(handle->i2c_x, I2C_ACKFAIL_FLAG) != RESET)
  186. {
  187. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  188. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32F415) || \
  189. defined (SOC_SERIES_AT32F421) || defined (SOC_SERIES_AT32A403A)
  190. /* generate stop condtion */
  191. i2c_stop_generate(handle->i2c_x);
  192. #endif
  193. /* clear ack fail flag */
  194. i2c_flag_clear(handle->i2c_x, I2C_ACKFAIL_FLAG);
  195. handle->comm.error_code = I2C_ERR_ACKFAIL;
  196. return I2C_ERR_ACKFAIL;
  197. }
  198. }
  199. /* check the stop flag */
  200. if(event_check & I2C_EVENT_CHECK_STOP)
  201. {
  202. if(i2c_flag_get(handle->i2c_x, I2C_STOPF_FLAG) != RESET)
  203. {
  204. /* clear stop flag */
  205. i2c_flag_clear(handle->i2c_x, I2C_STOPF_FLAG);
  206. #if defined (SOC_SERIES_AT32F402) || defined (SOC_SERIES_AT32F405) || \
  207. defined (SOC_SERIES_AT32F423) || defined (SOC_SERIES_AT32F425) || \
  208. defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  209. defined (SOC_SERIES_AT32A423) || defined (SOC_SERIES_AT32M412) || \
  210. defined (SOC_SERIES_AT32M416)
  211. i2c_reset_ctrl2_register(handle->i2c_x);
  212. #endif
  213. handle->comm.error_code = I2C_ERR_STOP;
  214. return I2C_ERR_STOP;
  215. }
  216. }
  217. /* check timeout */
  218. if((timeout--) == 0)
  219. {
  220. handle->comm.error_code = I2C_ERR_TIMEOUT;
  221. return I2C_ERR_TIMEOUT;
  222. }
  223. }
  224. }
  225. return I2C_OK;
  226. }
  227. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  228. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32F415) || \
  229. defined (SOC_SERIES_AT32F421) || defined (SOC_SERIES_AT32A403A)
  230. i2c_status_type i2c_master_write_addr(struct at32_i2c_handle *handle, uint16_t address, uint32_t timeout)
  231. {
  232. /* generate start condtion */
  233. i2c_start_generate(handle->i2c_x);
  234. /* wait for the start flag to be set */
  235. if(i2c_wait_flag(handle, I2C_STARTF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  236. {
  237. handle->comm.error_code = I2C_ERR_START;
  238. return I2C_ERR_START;
  239. }
  240. if(handle->i2c_x->oaddr1_bit.addr1mode == I2C_ADDRESS_MODE_7BIT)
  241. {
  242. /* send slave address */
  243. i2c_7bit_address_send(handle->i2c_x, address, I2C_DIRECTION_TRANSMIT);
  244. }
  245. else
  246. {
  247. /* send slave 10-bit address header */
  248. i2c_data_send(handle->i2c_x, (uint8_t)((address & 0x0300) >> 7) | 0xF0);
  249. /* wait for the addrh flag to be set */
  250. if(i2c_wait_flag(handle, I2C_ADDRHF_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  251. {
  252. handle->comm.error_code = I2C_ERR_ADDR10;
  253. return I2C_ERR_ADDR10;
  254. }
  255. /* send slave address */
  256. i2c_data_send(handle->i2c_x, (uint8_t)(address & 0x00FF));
  257. }
  258. /* wait for the addr7 flag to be set */
  259. if(i2c_wait_flag(handle, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  260. {
  261. handle->comm.error_code = I2C_ERR_ADDR;
  262. return I2C_ERR_ADDR;
  263. }
  264. return I2C_OK;
  265. }
  266. i2c_status_type i2c_master_read_addr(struct at32_i2c_handle *handle, uint16_t address, uint32_t timeout)
  267. {
  268. /* enable ack */
  269. i2c_ack_enable(handle->i2c_x, TRUE);
  270. /* generate start condtion */
  271. i2c_start_generate(handle->i2c_x);
  272. /* wait for the start flag to be set */
  273. if(i2c_wait_flag(handle, I2C_STARTF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  274. {
  275. handle->comm.error_code = I2C_ERR_START;
  276. return I2C_ERR_START;
  277. }
  278. if(handle->i2c_x->oaddr1_bit.addr1mode == I2C_ADDRESS_MODE_7BIT)
  279. {
  280. /* send slave address */
  281. i2c_7bit_address_send(handle->i2c_x, address, I2C_DIRECTION_RECEIVE);
  282. }
  283. else
  284. {
  285. /* send slave 10-bit address header */
  286. i2c_data_send(handle->i2c_x, (uint8_t)((address & 0x0300) >> 7) | 0xF0);
  287. /* wait for the addrh flag to be set */
  288. if(i2c_wait_flag(handle, I2C_ADDRHF_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  289. {
  290. handle->comm.error_code = I2C_ERR_ADDR10;
  291. return I2C_ERR_ADDR10;
  292. }
  293. /* send slave address */
  294. i2c_data_send(handle->i2c_x, (uint8_t)(address & 0x00FF));
  295. /* wait for the addr7 flag to be set */
  296. if(i2c_wait_flag(handle, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  297. {
  298. handle->comm.error_code = I2C_ERR_ADDR;
  299. return I2C_ERR_ADDR;
  300. }
  301. /* clear addr flag */
  302. i2c_flag_clear(handle->i2c_x, I2C_ADDR7F_FLAG);
  303. /* generate restart condtion */
  304. i2c_start_generate(handle->i2c_x);
  305. /* wait for the start flag to be set */
  306. if(i2c_wait_flag(handle, I2C_STARTF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  307. {
  308. handle->comm.error_code = I2C_ERR_START;
  309. return I2C_ERR_START;
  310. }
  311. /* send slave 10-bit address header */
  312. i2c_data_send(handle->i2c_x, (uint8_t)((address & 0x0300) >> 7) | 0xF1);
  313. }
  314. /* wait for the addr7 flag to be set */
  315. if(i2c_wait_flag(handle, I2C_ADDR7F_FLAG, I2C_EVENT_CHECK_ACKFAIL, timeout) != I2C_OK)
  316. {
  317. handle->comm.error_code = I2C_ERR_ADDR;
  318. return I2C_ERR_ADDR;
  319. }
  320. return I2C_OK;
  321. }
  322. i2c_status_type i2c_master_transmit_int(struct at32_i2c_handle *handle, uint16_t address, uint8_t *pdata, uint16_t size, uint32_t timeout)
  323. {
  324. /* initialization parameters */
  325. handle->comm.mode = I2C_INT_MA_TX;
  326. handle->comm.status = I2C_START;
  327. handle->comm.pbuff = pdata;
  328. handle->comm.pcount = size;
  329. handle->comm.timeout = timeout;
  330. handle->comm.error_code = I2C_OK;
  331. /* wait for the busy flag to be reset */
  332. if(i2c_wait_flag(handle, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  333. {
  334. return I2C_ERR_STEP_1;
  335. }
  336. /* ack acts on the current byte */
  337. i2c_master_receive_ack_set(handle->i2c_x, I2C_MASTER_ACK_CURRENT);
  338. /* send slave address */
  339. if(i2c_master_write_addr(handle, address, timeout) != I2C_OK)
  340. {
  341. /* generate stop condtion */
  342. i2c_stop_generate(handle->i2c_x);
  343. return I2C_ERR_STEP_2;
  344. }
  345. /* clear addr flag */
  346. i2c_flag_clear(handle->i2c_x, I2C_ADDR7F_FLAG);
  347. /* enable interrupt */
  348. i2c_interrupt_enable(handle->i2c_x, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, TRUE);
  349. return I2C_OK;
  350. }
  351. i2c_status_type i2c_master_receive_int(struct at32_i2c_handle *handle, uint16_t address, uint8_t *pdata, uint16_t size, uint32_t timeout)
  352. {
  353. /* initialization parameters */
  354. handle->comm.mode = I2C_INT_MA_RX;
  355. handle->comm.status = I2C_START;
  356. handle->comm.pbuff = pdata;
  357. handle->comm.pcount = size;
  358. handle->comm.timeout = timeout;
  359. handle->comm.error_code = I2C_OK;
  360. /* wait for the busy flag to be reset */
  361. if(i2c_wait_flag(handle, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  362. {
  363. return I2C_ERR_STEP_1;
  364. }
  365. /* ack acts on the current byte */
  366. i2c_master_receive_ack_set(handle->i2c_x, I2C_MASTER_ACK_CURRENT);
  367. /* enable ack */
  368. i2c_ack_enable(handle->i2c_x, TRUE);
  369. /* send slave address */
  370. if(i2c_master_read_addr(handle, address, timeout) != I2C_OK)
  371. {
  372. /* generate stop condtion */
  373. i2c_stop_generate(handle->i2c_x);
  374. return I2C_ERR_STEP_2;
  375. }
  376. if(handle->comm.pcount == 1)
  377. {
  378. /* disable ack */
  379. i2c_ack_enable(handle->i2c_x, FALSE);
  380. /* clear addr flag */
  381. i2c_flag_clear(handle->i2c_x, I2C_ADDR7F_FLAG);
  382. /* generate stop condtion */
  383. i2c_stop_generate(handle->i2c_x);
  384. }
  385. else if(handle->comm.pcount == 2)
  386. {
  387. /* ack acts on the next byte */
  388. i2c_master_receive_ack_set(handle->i2c_x, I2C_MASTER_ACK_NEXT);
  389. /* clear addr flag */
  390. i2c_flag_clear(handle->i2c_x, I2C_ADDR7F_FLAG);
  391. /* disable ack */
  392. i2c_ack_enable(handle->i2c_x, FALSE);
  393. }
  394. else
  395. {
  396. /* enable ack */
  397. i2c_ack_enable(handle->i2c_x, TRUE);
  398. /* clear addr flag */
  399. i2c_flag_clear(handle->i2c_x, I2C_ADDR7F_FLAG);
  400. }
  401. /* enable interrupt */
  402. i2c_interrupt_enable(handle->i2c_x, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, TRUE);
  403. return I2C_OK;
  404. }
  405. i2c_status_type i2c_master_transmit_dma(struct at32_i2c_handle *handle, uint16_t address, uint8_t *pdata, uint16_t size, uint32_t timeout)
  406. {
  407. /* initialization parameters */
  408. handle->comm.mode = I2C_DMA_MA_TX;
  409. handle->comm.status = I2C_START;
  410. handle->comm.pbuff = pdata;
  411. handle->comm.pcount = size;
  412. handle->comm.timeout = timeout;
  413. handle->comm.error_code = I2C_OK;
  414. /* wait for the busy flag to be reset */
  415. if(i2c_wait_flag(handle, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  416. {
  417. return I2C_ERR_STEP_1;
  418. }
  419. /* ack acts on the current byte */
  420. i2c_master_receive_ack_set(handle->i2c_x, I2C_MASTER_ACK_CURRENT);
  421. /* disable dma request */
  422. i2c_dma_enable(handle->i2c_x, FALSE);
  423. /* configure the dma channel */
  424. i2c_dma_config(handle, pdata, size);
  425. /* send slave address */
  426. if(i2c_master_write_addr(handle, address, timeout) != I2C_OK)
  427. {
  428. /* generate stop condtion */
  429. i2c_stop_generate(handle->i2c_x);
  430. return I2C_ERR_STEP_2;
  431. }
  432. /* clear addr flag */
  433. i2c_flag_clear(handle->i2c_x, I2C_ADDR7F_FLAG);
  434. /* enable dma request */
  435. i2c_dma_enable(handle->i2c_x, TRUE);
  436. return I2C_OK;
  437. }
  438. i2c_status_type i2c_master_receive_dma(struct at32_i2c_handle *handle, uint16_t address, uint8_t *pdata, uint16_t size, uint32_t timeout)
  439. {
  440. /* initialization parameters */
  441. handle->comm.mode = I2C_DMA_MA_RX;
  442. handle->comm.status = I2C_START;
  443. handle->comm.pbuff = pdata;
  444. handle->comm.pcount = size;
  445. handle->comm.timeout = timeout;
  446. handle->comm.error_code = I2C_OK;
  447. /* wait for the busy flag to be reset */
  448. if(i2c_wait_flag(handle, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  449. {
  450. return I2C_ERR_STEP_1;
  451. }
  452. /* ack acts on the current byte */
  453. i2c_master_receive_ack_set(handle->i2c_x, I2C_MASTER_ACK_CURRENT);
  454. /* enable ack */
  455. i2c_ack_enable(handle->i2c_x, TRUE);
  456. /* disable dma request */
  457. i2c_dma_enable(handle->i2c_x, FALSE);
  458. /* configure the dma channel */
  459. i2c_dma_config(handle, pdata, size);
  460. /* send slave address */
  461. if(i2c_master_read_addr(handle, address, timeout) != I2C_OK)
  462. {
  463. /* generate stop condtion */
  464. i2c_stop_generate(handle->i2c_x);
  465. return I2C_ERR_STEP_2;
  466. }
  467. if(size == 1)
  468. {
  469. /* clear addr flag */
  470. i2c_flag_clear(handle->i2c_x, I2C_ADDR7F_FLAG);
  471. /* disable ack */
  472. i2c_ack_enable(handle->i2c_x, FALSE);
  473. /* generate stop condtion */
  474. i2c_stop_generate(handle->i2c_x);
  475. /* enable dma request */
  476. i2c_dma_enable(handle->i2c_x, TRUE);
  477. }
  478. else
  479. {
  480. /* enable dma end transfer */
  481. i2c_dma_end_transfer_set(handle->i2c_x, TRUE);
  482. /* enable dma request */
  483. i2c_dma_enable(handle->i2c_x, TRUE);
  484. /* clear addr flag */
  485. i2c_flag_clear(handle->i2c_x, I2C_ADDR7F_FLAG);
  486. }
  487. return I2C_OK;
  488. }
  489. void i2c_master_tx_isr_int(struct at32_i2c_handle *handle)
  490. {
  491. /* step 1: transfer data */
  492. if(i2c_flag_get(handle->i2c_x, I2C_TDBE_FLAG) != RESET)
  493. {
  494. if(handle->comm.pcount == 0)
  495. {
  496. rt_completion_done(&handle->completion);
  497. /* transfer complete */
  498. handle->comm.status = I2C_END;
  499. /* disable interrupt */
  500. i2c_interrupt_enable(handle->i2c_x, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, FALSE);
  501. /* generate stop condtion */
  502. i2c_stop_generate(handle->i2c_x);
  503. }
  504. else
  505. {
  506. /* write data */
  507. i2c_data_send(handle->i2c_x, *handle->comm.pbuff++);
  508. handle->comm.pcount--;
  509. }
  510. }
  511. }
  512. void i2c_master_rx_isr_int(struct at32_i2c_handle *handle)
  513. {
  514. if(i2c_flag_get(handle->i2c_x, I2C_TDC_FLAG) != RESET)
  515. {
  516. if(handle->comm.pcount == 3)
  517. {
  518. /* disable ack */
  519. i2c_ack_enable(handle->i2c_x, FALSE);
  520. /* read data */
  521. (*handle->comm.pbuff++) = i2c_data_receive(handle->i2c_x);
  522. handle->comm.pcount--;
  523. }
  524. else if(handle->comm.pcount == 2)
  525. {
  526. /* generate stop condtion */
  527. i2c_stop_generate(handle->i2c_x);
  528. /* read data */
  529. (*handle->comm.pbuff++) = i2c_data_receive(handle->i2c_x);
  530. handle->comm.pcount--;
  531. /* read data */
  532. (*handle->comm.pbuff++) = i2c_data_receive(handle->i2c_x);
  533. handle->comm.pcount--;
  534. /* transfer complete */
  535. rt_completion_done(&handle->completion);
  536. handle->comm.status = I2C_END;
  537. /* disable interrupt */
  538. i2c_interrupt_enable(handle->i2c_x, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, FALSE);
  539. }
  540. else
  541. {
  542. /* read data */
  543. (*handle->comm.pbuff++) = i2c_data_receive(handle->i2c_x);
  544. handle->comm.pcount--;
  545. }
  546. }
  547. else if(i2c_flag_get(handle->i2c_x, I2C_RDBF_FLAG) != RESET)
  548. {
  549. if(handle->comm.pcount > 3)
  550. {
  551. /* read data */
  552. (*handle->comm.pbuff++) = i2c_data_receive(handle->i2c_x);
  553. handle->comm.pcount--;
  554. }
  555. else if((handle->comm.pcount == 3) || (handle->comm.pcount == 2))
  556. {
  557. /* disable rdbf interrupt */
  558. i2c_interrupt_enable(handle->i2c_x, I2C_DATA_INT, FALSE);
  559. }
  560. else
  561. {
  562. /* read data */
  563. (*handle->comm.pbuff++) = i2c_data_receive(handle->i2c_x);
  564. handle->comm.pcount--;
  565. /* transfer complete */
  566. rt_completion_done(&handle->completion);
  567. handle->comm.status = I2C_END;
  568. /* disable interrupt */
  569. i2c_interrupt_enable(handle->i2c_x, I2C_EVT_INT | I2C_DATA_INT | I2C_ERR_INT, FALSE);
  570. }
  571. }
  572. }
  573. void i2c_master_tx_isr_dma(struct at32_i2c_handle *handle)
  574. {
  575. /* tdc interrupt */
  576. if(i2c_flag_get(handle->i2c_x, I2C_TDC_FLAG) != RESET)
  577. {
  578. rt_completion_done(&handle->completion);
  579. /* generate stop condtion */
  580. i2c_stop_generate(handle->i2c_x);
  581. /* disable evt interrupt */
  582. i2c_interrupt_enable(handle->i2c_x, I2C_EVT_INT, FALSE);
  583. /* transfer complete */
  584. handle->comm.status = I2C_END;
  585. }
  586. }
  587. void i2c_evt_isr(struct at32_i2c_handle *handle)
  588. {
  589. switch(handle->comm.mode)
  590. {
  591. case I2C_INT_MA_TX:
  592. i2c_master_tx_isr_int(handle);
  593. break;
  594. case I2C_INT_MA_RX:
  595. i2c_master_rx_isr_int(handle);
  596. break;
  597. case I2C_DMA_MA_TX:
  598. i2c_master_tx_isr_dma(handle);
  599. break;
  600. default:
  601. break;
  602. }
  603. }
  604. #endif
  605. #if defined (SOC_SERIES_AT32F402) || defined (SOC_SERIES_AT32F405) || \
  606. defined (SOC_SERIES_AT32F423) || defined (SOC_SERIES_AT32F425) || \
  607. defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  608. defined (SOC_SERIES_AT32A423) || defined (SOC_SERIES_AT32M412) || \
  609. defined (SOC_SERIES_AT32M416)
  610. void i2c_start_transfer(struct at32_i2c_handle *handle, uint16_t address, i2c_start_mode_type start)
  611. {
  612. if (handle->comm.pcount > MAX_TRANSFER_CNT)
  613. {
  614. handle->comm.psize = MAX_TRANSFER_CNT;
  615. i2c_transmit_set(handle->i2c_x, address, handle->comm.psize, I2C_RELOAD_MODE, start);
  616. }
  617. else
  618. {
  619. handle->comm.psize = handle->comm.pcount;
  620. i2c_transmit_set(handle->i2c_x, address, handle->comm.psize, I2C_AUTO_STOP_MODE, start);
  621. }
  622. }
  623. void i2c_start_transfer_dma(struct at32_i2c_handle *handle, uint16_t address, i2c_start_mode_type start)
  624. {
  625. if (handle->comm.pcount > MAX_TRANSFER_CNT)
  626. {
  627. handle->comm.psize = MAX_TRANSFER_CNT;
  628. /* config dma */
  629. i2c_dma_config(handle, handle->comm.pbuff, handle->comm.psize);
  630. i2c_transmit_set(handle->i2c_x, address, handle->comm.psize, I2C_RELOAD_MODE, start);
  631. }
  632. else
  633. {
  634. handle->comm.psize = handle->comm.pcount;
  635. /* config dma */
  636. i2c_dma_config(handle, handle->comm.pbuff, handle->comm.psize);
  637. i2c_transmit_set(handle->i2c_x, address, handle->comm.psize, I2C_AUTO_STOP_MODE, start);
  638. }
  639. }
  640. i2c_status_type i2c_master_transmit_int(struct at32_i2c_handle *handle, uint16_t address, uint8_t *pdata, uint16_t size, uint32_t timeout)
  641. {
  642. /* initialization parameters */
  643. handle->comm.mode = I2C_INT_MA_TX;
  644. handle->comm.status = I2C_START;
  645. handle->comm.pbuff = pdata;
  646. handle->comm.pcount = size;
  647. handle->comm.error_code = I2C_OK;
  648. /* wait for the busy flag to be reset */
  649. if (i2c_wait_flag(handle, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  650. {
  651. return I2C_ERR_STEP_1;
  652. }
  653. /* start transfer */
  654. i2c_start_transfer(handle, address, I2C_GEN_START_WRITE);
  655. /* enable interrupt */
  656. i2c_interrupt_enable(handle->i2c_x, I2C_ERR_INT | I2C_TDC_INT | I2C_STOP_INT | I2C_ACKFIAL_INT | I2C_TD_INT, TRUE);
  657. return I2C_OK;
  658. }
  659. i2c_status_type i2c_master_receive_int(struct at32_i2c_handle *handle, uint16_t address, uint8_t *pdata, uint16_t size, uint32_t timeout)
  660. {
  661. /* initialization parameters */
  662. handle->comm.mode = I2C_INT_MA_RX;
  663. handle->comm.status = I2C_START;
  664. handle->comm.pbuff = pdata;
  665. handle->comm.pcount = size;
  666. handle->comm.error_code = I2C_OK;
  667. /* wait for the busy flag to be reset */
  668. if (i2c_wait_flag(handle, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  669. {
  670. return I2C_ERR_STEP_1;
  671. }
  672. /* start transfer */
  673. i2c_start_transfer(handle, address, I2C_GEN_START_READ);
  674. /* enable interrupt */
  675. i2c_interrupt_enable(handle->i2c_x, I2C_ERR_INT | I2C_TDC_INT | I2C_STOP_INT | I2C_ACKFIAL_INT | I2C_RD_INT, TRUE);
  676. return I2C_OK;
  677. }
  678. i2c_status_type i2c_master_transmit_dma(struct at32_i2c_handle *handle, uint16_t address, uint8_t *pdata, uint16_t size, uint32_t timeout)
  679. {
  680. /* initialization parameters */
  681. handle->comm.mode = I2C_DMA_MA_TX;
  682. handle->comm.status = I2C_START;
  683. handle->comm.pbuff = pdata;
  684. handle->comm.pcount = size;
  685. handle->comm.error_code = I2C_OK;
  686. /* wait for the busy flag to be reset */
  687. if(i2c_wait_flag(handle, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  688. {
  689. return I2C_ERR_STEP_1;
  690. }
  691. /* disable dma request */
  692. i2c_dma_enable(handle->i2c_x, I2C_DMA_REQUEST_TX, FALSE);
  693. /* start transfer */
  694. i2c_start_transfer_dma(handle, address, I2C_GEN_START_WRITE);
  695. /* enable i2c interrupt */
  696. i2c_interrupt_enable(handle->i2c_x, I2C_ERR_INT | I2C_ACKFIAL_INT, TRUE);
  697. /* enable dma request */
  698. i2c_dma_enable(handle->i2c_x, I2C_DMA_REQUEST_TX, TRUE);
  699. return I2C_OK;
  700. }
  701. i2c_status_type i2c_master_receive_dma(struct at32_i2c_handle *handle, uint16_t address, uint8_t *pdata, uint16_t size, uint32_t timeout)
  702. {
  703. /* initialization parameters */
  704. handle->comm.mode = I2C_DMA_MA_RX;
  705. handle->comm.status = I2C_START;
  706. handle->comm.pbuff = pdata;
  707. handle->comm.pcount = size;
  708. handle->comm.error_code = I2C_OK;
  709. /* wait for the busy flag to be reset */
  710. if(i2c_wait_flag(handle, I2C_BUSYF_FLAG, I2C_EVENT_CHECK_NONE, timeout) != I2C_OK)
  711. {
  712. return I2C_ERR_STEP_1;
  713. }
  714. /* disable dma request */
  715. i2c_dma_enable(handle->i2c_x, I2C_DMA_REQUEST_RX, FALSE);
  716. /* start transfer */
  717. i2c_start_transfer_dma(handle, address, I2C_GEN_START_READ);
  718. /* enable i2c interrupt */
  719. i2c_interrupt_enable(handle->i2c_x, I2C_ERR_INT | I2C_ACKFIAL_INT, TRUE);
  720. /* enable dma request */
  721. i2c_dma_enable(handle->i2c_x, I2C_DMA_REQUEST_RX, TRUE);
  722. return I2C_OK;
  723. }
  724. void i2c_master_isr_int(struct at32_i2c_handle *handle)
  725. {
  726. if (i2c_flag_get(handle->i2c_x, I2C_ACKFAIL_FLAG) != RESET)
  727. {
  728. /* clear ackfail flag */
  729. i2c_flag_clear(handle->i2c_x, I2C_ACKFAIL_FLAG);
  730. /* refresh tx register */
  731. i2c_refresh_txdt_register(handle->i2c_x);
  732. if(handle->comm.pcount != 0)
  733. {
  734. handle->comm.error_code = I2C_ERR_ACKFAIL;
  735. }
  736. }
  737. else if (i2c_flag_get(handle->i2c_x, I2C_TDIS_FLAG) != RESET)
  738. {
  739. /* send data */
  740. i2c_data_send(handle->i2c_x, *handle->comm.pbuff++);
  741. handle->comm.pcount--;
  742. handle->comm.psize--;
  743. }
  744. else if (i2c_flag_get(handle->i2c_x, I2C_TCRLD_FLAG) != RESET)
  745. {
  746. if ((handle->comm.psize == 0) && (handle->comm.pcount != 0))
  747. {
  748. /* continue transfer */
  749. i2c_start_transfer(handle, i2c_transfer_addr_get(handle->i2c_x), I2C_WITHOUT_START);
  750. }
  751. }
  752. else if (i2c_flag_get(handle->i2c_x, I2C_RDBF_FLAG) != RESET)
  753. {
  754. /* read data */
  755. (*handle->comm.pbuff++) = i2c_data_receive(handle->i2c_x);
  756. handle->comm.pcount--;
  757. handle->comm.psize--;
  758. }
  759. else if (i2c_flag_get(handle->i2c_x, I2C_TDC_FLAG) != RESET)
  760. {
  761. if (handle->comm.pcount == 0)
  762. {
  763. if (handle->i2c_x->ctrl2_bit.astopen == 0)
  764. {
  765. /* generate stop condtion */
  766. i2c_stop_generate(handle->i2c_x);
  767. }
  768. }
  769. }
  770. else if (i2c_flag_get(handle->i2c_x, I2C_STOPF_FLAG) != RESET)
  771. {
  772. /* clear stop flag */
  773. i2c_flag_clear(handle->i2c_x, I2C_STOPF_FLAG);
  774. /* reset ctrl2 register */
  775. i2c_reset_ctrl2_register(handle->i2c_x);
  776. if (i2c_flag_get(handle->i2c_x, I2C_ACKFAIL_FLAG) != RESET)
  777. {
  778. /* clear ackfail flag */
  779. i2c_flag_clear(handle->i2c_x, I2C_ACKFAIL_FLAG);
  780. }
  781. /* refresh tx dt register */
  782. i2c_refresh_txdt_register(handle->i2c_x);
  783. /* disable interrupts */
  784. i2c_interrupt_enable(handle->i2c_x, I2C_ERR_INT | I2C_TDC_INT | I2C_STOP_INT | I2C_ACKFIAL_INT | I2C_TD_INT | I2C_RD_INT, FALSE);
  785. /* transfer complete */
  786. handle->comm.status = I2C_END;
  787. rt_completion_done(&handle->completion);
  788. }
  789. }
  790. void i2c_master_isr_dma(struct at32_i2c_handle *handle)
  791. {
  792. if (i2c_flag_get(handle->i2c_x, I2C_ACKFAIL_FLAG) != RESET)
  793. {
  794. /* clear ackfail flag */
  795. i2c_flag_clear(handle->i2c_x, I2C_ACKFAIL_FLAG);
  796. /* enable stop interrupt to wait for stop generate stop */
  797. i2c_interrupt_enable(handle->i2c_x, I2C_STOP_INT, TRUE);
  798. /* refresh tx dt register */
  799. i2c_refresh_txdt_register(handle->i2c_x);
  800. if(handle->comm.pcount != 0)
  801. {
  802. handle->comm.error_code = I2C_ERR_ACKFAIL;
  803. }
  804. }
  805. else if (i2c_flag_get(handle->i2c_x, I2C_TCRLD_FLAG) != RESET)
  806. {
  807. /* disable tdc interrupt */
  808. i2c_interrupt_enable(handle->i2c_x, I2C_TDC_INT, FALSE);
  809. if (handle->comm.pcount != 0)
  810. {
  811. /* continue transfer */
  812. i2c_start_transfer(handle, i2c_transfer_addr_get(handle->i2c_x), I2C_WITHOUT_START);
  813. /* enable dma request */
  814. if(handle->comm.mode == I2C_DMA_MA_TX)
  815. i2c_dma_enable(handle->i2c_x, I2C_DMA_REQUEST_TX, TRUE);
  816. else if(handle->comm.mode == I2C_DMA_MA_RX)
  817. i2c_dma_enable(handle->i2c_x, I2C_DMA_REQUEST_RX, TRUE);
  818. }
  819. }
  820. else if (i2c_flag_get(handle->i2c_x, I2C_STOPF_FLAG) != RESET)
  821. {
  822. /* clear stop flag */
  823. i2c_flag_clear(handle->i2c_x, I2C_STOPF_FLAG);
  824. /* reset ctrl2 register */
  825. i2c_reset_ctrl2_register(handle->i2c_x);
  826. if (i2c_flag_get(handle->i2c_x, I2C_ACKFAIL_FLAG) != RESET)
  827. {
  828. /* clear ackfail flag */
  829. i2c_flag_clear(handle->i2c_x, I2C_ACKFAIL_FLAG);
  830. }
  831. /* refresh tx dt register */
  832. i2c_refresh_txdt_register(handle->i2c_x);
  833. /* disable interrupts */
  834. i2c_interrupt_enable(handle->i2c_x, I2C_ERR_INT | I2C_TDC_INT | I2C_STOP_INT | I2C_ACKFIAL_INT | I2C_TD_INT | I2C_RD_INT, FALSE);
  835. /* transfer complete */
  836. handle->comm.status = I2C_END;
  837. rt_completion_done(&handle->completion);
  838. }
  839. }
  840. void i2c_evt_isr(struct at32_i2c_handle *handle)
  841. {
  842. switch(handle->comm.mode)
  843. {
  844. case I2C_INT_MA_TX:
  845. case I2C_INT_MA_RX:
  846. i2c_master_isr_int(handle);
  847. break;
  848. case I2C_DMA_MA_TX:
  849. case I2C_DMA_MA_RX:
  850. i2c_master_isr_dma(handle);
  851. break;
  852. default:
  853. break;
  854. }
  855. }
  856. #endif
  857. static rt_ssize_t master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
  858. {
  859. /* for dma may more stability */
  860. #define DMA_TRANS_MIN_LEN 2 /* only buffer length >= DMA_TRANS_MIN_LEN will use DMA mode */
  861. #define TRANS_TIMEOUT_PERSEC 8 /* per ms will trans nums bytes */
  862. rt_int32_t i, ret;
  863. struct rt_i2c_msg *msg = msgs;
  864. struct rt_completion *completion;
  865. rt_uint32_t timeout;
  866. if (num == 0)
  867. {
  868. return 0;
  869. }
  870. RT_ASSERT((msgs != RT_NULL) && (bus != RT_NULL));
  871. struct at32_i2c *instance = rt_container_of(bus, struct at32_i2c, i2c_bus);
  872. completion = &instance->handle->completion;
  873. LOG_D("xfer start %d mags", num);
  874. for (i = 0; i < (num - 1); i++)
  875. {
  876. msg = &msgs[i];
  877. LOG_D("xfer msgs[%d] addr=0x%2x buf=0x%x len= 0x%x flags= 0x%x", i, msg->addr, msg->buf, msg->len, msg->flags);
  878. timeout = msg->len / TRANS_TIMEOUT_PERSEC + 2;
  879. if (msg->flags & RT_I2C_RD)
  880. {
  881. if ((instance->handle->i2c_dma_flag & RT_DEVICE_FLAG_DMA_RX) && (msg->len >= DMA_TRANS_MIN_LEN))
  882. {
  883. ret = i2c_master_receive_dma(instance->handle, (msg->addr << 1) , msg->buf, msg->len, 0xFFFFFFFF);
  884. }
  885. else
  886. {
  887. ret = i2c_master_receive_int(instance->handle, (msg->addr << 1) , msg->buf, msg->len, 0xFFFFFFFF);
  888. }
  889. if (ret != RT_EOK)
  890. {
  891. LOG_E("[%s:%d]i2c read error(%d)!\n", __func__, __LINE__, ret);
  892. goto out;
  893. }
  894. if (rt_completion_wait(completion, timeout) != RT_EOK)
  895. {
  896. LOG_D("receive time out");
  897. goto out;
  898. }
  899. }
  900. else
  901. {
  902. if ((instance->handle->i2c_dma_flag & RT_DEVICE_FLAG_DMA_TX) && (msg->len >= DMA_TRANS_MIN_LEN))
  903. {
  904. ret = i2c_master_transmit_dma(instance->handle, (msg->addr << 1) , msg->buf, msg->len, 0xFFFFFFFF);
  905. }
  906. else
  907. {
  908. ret = i2c_master_transmit_int(instance->handle, (msg->addr << 1) , msg->buf, msg->len, 0xFFFFFFFF);
  909. }
  910. if (ret != RT_EOK)
  911. {
  912. LOG_D("[%s:%d]i2c write error(%d)!\n", __func__, __LINE__, ret);
  913. goto out;
  914. }
  915. if (rt_completion_wait(completion, timeout) != RT_EOK)
  916. {
  917. LOG_D("transmit time out");
  918. goto out;
  919. }
  920. }
  921. }
  922. /* last msg */
  923. msg = &msgs[i];
  924. timeout = msg->len / TRANS_TIMEOUT_PERSEC + 2;
  925. LOG_D("xfer last msgs[%d] addr=0x%2x buf= 0x%x len= 0x%x flags = 0x%x", i, msg->addr, msg->buf, msg->len, msg->flags);
  926. if (msg->flags & RT_I2C_RD)
  927. {
  928. if ((instance->handle->i2c_dma_flag & RT_DEVICE_FLAG_DMA_RX) && (msg->len >= DMA_TRANS_MIN_LEN))
  929. {
  930. ret = i2c_master_receive_dma(instance->handle, (msg->addr << 1), msg->buf, msg->len, 0xFFFFFFFF);
  931. }
  932. else
  933. {
  934. ret = i2c_master_receive_int(instance->handle, (msg->addr << 1), msg->buf, msg->len, 0xFFFFFFFF);
  935. }
  936. if (ret != RT_EOK)
  937. {
  938. LOG_D("[%s:%d]i2c read error(%d)!\n", __func__, __LINE__, ret);
  939. goto out;
  940. }
  941. if (rt_completion_wait(completion, timeout) != RT_EOK)
  942. {
  943. LOG_D("receive time out");
  944. goto out;
  945. }
  946. }
  947. else
  948. {
  949. if ((instance->handle->i2c_dma_flag & RT_DEVICE_FLAG_DMA_TX) && (msg->len >= DMA_TRANS_MIN_LEN))
  950. {
  951. ret = i2c_master_transmit_dma(instance->handle, (msg->addr << 1), msg->buf, msg->len, 0xFFFFFFFF);
  952. }
  953. else
  954. {
  955. ret = i2c_master_transmit_int(instance->handle, (msg->addr << 1), msg->buf, msg->len, 0xFFFFFFFF);
  956. }
  957. if (ret != RT_EOK)
  958. {
  959. LOG_D("[%s:%d]i2c write error(%d)!\n", __func__, __LINE__, ret);
  960. goto out;
  961. }
  962. if (rt_completion_wait(completion, timeout) != RT_EOK)
  963. {
  964. LOG_D("transmit time out");
  965. goto out;
  966. }
  967. }
  968. ret = num;
  969. LOG_D("xfer end %d mags\r\n", num);
  970. return ret;
  971. out:
  972. if(instance->handle->comm.error_code == I2C_ERR_ACKFAIL)
  973. {
  974. LOG_D("i2c nack error now stoped");
  975. }
  976. if(instance->handle->comm.error_code == I2C_ERR_INTERRUPT)
  977. {
  978. LOG_D("i2c bus error now stoped");
  979. ret = i - 1;
  980. }
  981. /* generate stop */
  982. i2c_stop_generate(instance->handle->i2c_x);
  983. return ret;
  984. }
  985. static void _dma_base_channel_check(struct at32_i2c *instance)
  986. {
  987. dma_channel_type *rx_channel = instance->handle->dma_rx->dma_channel;
  988. dma_channel_type *tx_channel = instance->handle->dma_tx->dma_channel;
  989. if(instance->handle->i2c_dma_flag & RT_DEVICE_FLAG_DMA_RX)
  990. {
  991. instance->handle->dma_rx->dma_done = RT_TRUE;
  992. instance->handle->dma_rx->dma_x = (dma_type *)((rt_uint32_t)rx_channel & ~0xFF);
  993. instance->handle->dma_rx->channel_index = ((((rt_uint32_t)rx_channel & 0xFF) - 8) / 0x14) + 1;
  994. }
  995. if(instance->handle->i2c_dma_flag & RT_DEVICE_FLAG_DMA_TX)
  996. {
  997. instance->handle->dma_tx->dma_done = RT_TRUE;
  998. instance->handle->dma_tx->dma_x = (dma_type *)((rt_uint32_t)tx_channel & ~0xFF);
  999. instance->handle->dma_tx->channel_index = ((((rt_uint32_t)tx_channel & 0xFF) - 8) / 0x14) + 1;
  1000. }
  1001. }
  1002. static void at32_i2c_dma_init(struct at32_i2c *instance)
  1003. {
  1004. dma_init_type dma_init_struct;
  1005. /* search dma base and channel index */
  1006. _dma_base_channel_check(instance);
  1007. /* config dma channel */
  1008. dma_default_para_init(&dma_init_struct);
  1009. dma_init_struct.peripheral_inc_enable = FALSE;
  1010. dma_init_struct.memory_inc_enable = TRUE;
  1011. dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE;
  1012. dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE;
  1013. dma_init_struct.priority = DMA_PRIORITY_MEDIUM;
  1014. dma_init_struct.loop_mode_enable = FALSE;
  1015. if (instance->handle->i2c_dma_flag & RT_DEVICE_FLAG_DMA_RX)
  1016. {
  1017. crm_periph_clock_enable(instance->handle->dma_rx->dma_clock, TRUE);
  1018. dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY;
  1019. dma_reset(instance->handle->dma_rx->dma_channel);
  1020. dma_init(instance->handle->dma_rx->dma_channel, &dma_init_struct);
  1021. #if defined (SOC_SERIES_AT32F425)
  1022. dma_flexible_config(instance->handle->dma_rx->dma_x, instance->handle->dma_rx->flex_channel, \
  1023. (dma_flexible_request_type)instance->handle->dma_rx->request_id);
  1024. #endif
  1025. #if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  1026. defined (SOC_SERIES_AT32F423) || defined (SOC_SERIES_AT32F402) || \
  1027. defined (SOC_SERIES_AT32F405) || defined (SOC_SERIES_AT32A423) || \
  1028. defined (SOC_SERIES_AT32M412) || defined (SOC_SERIES_AT32M416)
  1029. dmamux_enable(instance->handle->dma_rx->dma_x, TRUE);
  1030. dmamux_init(instance->handle->dma_rx->dmamux_channel, (dmamux_requst_id_sel_type)instance->handle->dma_rx->request_id);
  1031. #endif
  1032. /* dma irq should set in dma rx mode */
  1033. nvic_irq_enable(instance->handle->dma_rx->dma_irqn, 0, 1);
  1034. }
  1035. if (instance->handle->i2c_dma_flag & RT_DEVICE_FLAG_DMA_TX)
  1036. {
  1037. crm_periph_clock_enable(instance->handle->dma_tx->dma_clock, TRUE);
  1038. dma_init_struct.direction = DMA_DIR_MEMORY_TO_PERIPHERAL;
  1039. dma_reset(instance->handle->dma_tx->dma_channel);
  1040. dma_init(instance->handle->dma_tx->dma_channel, &dma_init_struct);
  1041. #if defined (SOC_SERIES_AT32F425)
  1042. dma_flexible_config(instance->handle->dma_tx->dma_x, instance->handle->dma_tx->flex_channel, \
  1043. (dma_flexible_request_type)instance->handle->dma_tx->request_id);
  1044. #endif
  1045. #if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  1046. defined (SOC_SERIES_AT32F423) || defined (SOC_SERIES_AT32F402) || \
  1047. defined (SOC_SERIES_AT32F405) || defined (SOC_SERIES_AT32A423) || \
  1048. defined (SOC_SERIES_AT32M412) || defined (SOC_SERIES_AT32M416)
  1049. dmamux_enable(instance->handle->dma_tx->dma_x, TRUE);
  1050. dmamux_init(instance->handle->dma_tx->dmamux_channel, (dmamux_requst_id_sel_type)instance->handle->dma_tx->request_id);
  1051. #endif
  1052. /* dma irq should set in dma tx mode */
  1053. nvic_irq_enable(instance->handle->dma_tx->dma_irqn, 0, 1);
  1054. }
  1055. }
  1056. void i2c_err_isr(struct at32_i2c_handle *handle)
  1057. {
  1058. /* buserr */
  1059. if(i2c_flag_get(handle->i2c_x, I2C_BUSERR_FLAG) != RESET)
  1060. {
  1061. i2c_flag_clear(handle->i2c_x, I2C_BUSERR_FLAG);
  1062. handle->comm.error_code = I2C_ERR_INTERRUPT;
  1063. }
  1064. /* arlost */
  1065. if(i2c_flag_get(handle->i2c_x, I2C_ARLOST_FLAG) != RESET)
  1066. {
  1067. i2c_flag_clear(handle->i2c_x, I2C_ARLOST_FLAG);
  1068. handle->comm.error_code = I2C_ERR_INTERRUPT;
  1069. }
  1070. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  1071. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32F415) || \
  1072. defined (SOC_SERIES_AT32F421) || defined (SOC_SERIES_AT32A403A)
  1073. /* ackfail */
  1074. if(i2c_flag_get(handle->i2c_x, I2C_ACKFAIL_FLAG) != RESET)
  1075. {
  1076. i2c_flag_clear(handle->i2c_x, I2C_ACKFAIL_FLAG);
  1077. switch(handle->comm.mode)
  1078. {
  1079. case I2C_DMA_SLA_TX:
  1080. /* disable ack */
  1081. i2c_ack_enable(handle->i2c_x, FALSE);
  1082. /* disable evt interrupt */
  1083. i2c_interrupt_enable(handle->i2c_x, I2C_EVT_INT, FALSE);
  1084. /* transfer complete */
  1085. handle->comm.status = I2C_END;
  1086. break;
  1087. default:
  1088. handle->comm.error_code = I2C_ERR_INTERRUPT;
  1089. break;
  1090. }
  1091. }
  1092. #endif
  1093. /* ouf */
  1094. if(i2c_flag_get(handle->i2c_x, I2C_OUF_FLAG) != RESET)
  1095. {
  1096. i2c_flag_clear(handle->i2c_x, I2C_OUF_FLAG);
  1097. handle->comm.error_code = I2C_ERR_INTERRUPT;
  1098. }
  1099. /* pecerr */
  1100. if(i2c_flag_get(handle->i2c_x, I2C_PECERR_FLAG) != RESET)
  1101. {
  1102. i2c_flag_clear(handle->i2c_x, I2C_PECERR_FLAG);
  1103. handle->comm.error_code = I2C_ERR_INTERRUPT;
  1104. }
  1105. /* tmout */
  1106. if(i2c_flag_get(handle->i2c_x, I2C_TMOUT_FLAG) != RESET)
  1107. {
  1108. i2c_flag_clear(handle->i2c_x, I2C_TMOUT_FLAG);
  1109. handle->comm.error_code = I2C_ERR_INTERRUPT;
  1110. }
  1111. /* alertf */
  1112. if(i2c_flag_get(handle->i2c_x, I2C_ALERTF_FLAG) != RESET)
  1113. {
  1114. i2c_flag_clear(handle->i2c_x, I2C_ALERTF_FLAG);
  1115. handle->comm.error_code = I2C_ERR_INTERRUPT;
  1116. }
  1117. /* disable all interrupts */
  1118. i2c_interrupt_enable(handle->i2c_x, I2C_ERR_INT, FALSE);
  1119. }
  1120. void i2c_dma_isr(struct at32_i2c_handle *handle)
  1121. {
  1122. volatile rt_uint32_t reg_sts = 0, index = 0;
  1123. struct dma_config *dma = RT_NULL;
  1124. if(handle->comm.mode == I2C_DMA_MA_TX)
  1125. {
  1126. dma = handle->dma_tx;
  1127. }
  1128. else if(handle->comm.mode == I2C_DMA_MA_RX)
  1129. {
  1130. dma = handle->dma_rx;
  1131. }
  1132. reg_sts = dma->dma_x->sts;
  1133. index = dma->channel_index;
  1134. /* transfer complete */
  1135. if((reg_sts & (DMA_FDT_FLAG << (4 * (index - 1)))) != RESET)
  1136. {
  1137. /* clear dma flag */
  1138. dma->dma_x->clr |= (rt_uint32_t)((DMA_FDT_FLAG << (4 * (index - 1))) | \
  1139. (DMA_HDT_FLAG << (4 * (index - 1))));
  1140. /* disable the transfer complete interrupt */
  1141. dma_interrupt_enable(dma->dma_channel, DMA_FDT_INT, FALSE);
  1142. /* mark done */
  1143. dma->dma_done = RT_TRUE;
  1144. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  1145. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32F415) || \
  1146. defined (SOC_SERIES_AT32F421) || defined (SOC_SERIES_AT32A403A)
  1147. /* disable dma request */
  1148. i2c_dma_enable(handle->i2c_x, FALSE);
  1149. #endif
  1150. #if defined (SOC_SERIES_AT32F402) || defined (SOC_SERIES_AT32F405) || \
  1151. defined (SOC_SERIES_AT32F423) || defined (SOC_SERIES_AT32F425) || \
  1152. defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  1153. defined (SOC_SERIES_AT32A423) || defined (SOC_SERIES_AT32M412) || \
  1154. defined (SOC_SERIES_AT32M416)
  1155. /* disable dma request */
  1156. if(handle->comm.mode == I2C_DMA_MA_TX)
  1157. i2c_dma_enable(handle->i2c_x, I2C_DMA_REQUEST_TX, FALSE);
  1158. else if(handle->comm.mode == I2C_DMA_MA_RX)
  1159. i2c_dma_enable(handle->i2c_x, I2C_DMA_REQUEST_RX, FALSE);
  1160. #endif
  1161. /* disable dma channel */
  1162. dma_channel_enable(dma->dma_channel, FALSE);
  1163. switch(handle->comm.mode)
  1164. {
  1165. #if defined (SOC_SERIES_AT32F403A) || defined (SOC_SERIES_AT32F407) || \
  1166. defined (SOC_SERIES_AT32F413) || defined (SOC_SERIES_AT32F415) || \
  1167. defined (SOC_SERIES_AT32F421) || defined (SOC_SERIES_AT32A403A)
  1168. case I2C_DMA_MA_TX:
  1169. /* enable tdc interrupt, generate stop condition in tdc interrupt */
  1170. handle->comm.pcount = 0;
  1171. i2c_interrupt_enable(handle->i2c_x, I2C_EVT_INT, TRUE);
  1172. break;
  1173. case I2C_DMA_MA_RX:
  1174. /* clear ackfail flag */
  1175. i2c_flag_clear(handle->i2c_x, I2C_ACKFAIL_FLAG);
  1176. handle->comm.pcount = 0;
  1177. /* generate stop condtion */
  1178. i2c_stop_generate(handle->i2c_x);
  1179. /* transfer complete */
  1180. rt_completion_done(&handle->completion);
  1181. handle->comm.status = I2C_END;
  1182. break;
  1183. default:
  1184. break;
  1185. #endif
  1186. #if defined (SOC_SERIES_AT32F402) || defined (SOC_SERIES_AT32F405) || \
  1187. defined (SOC_SERIES_AT32F423) || defined (SOC_SERIES_AT32F425) || \
  1188. defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
  1189. defined (SOC_SERIES_AT32A423) || defined (SOC_SERIES_AT32M412) || \
  1190. defined (SOC_SERIES_AT32M416)
  1191. case I2C_DMA_MA_TX:
  1192. case I2C_DMA_MA_RX:
  1193. {
  1194. /* update the number of transfers */
  1195. handle->comm.pcount -= handle->comm.psize;
  1196. /* transfer complete */
  1197. if (handle->comm.pcount == 0)
  1198. {
  1199. /* enable stop interrupt */
  1200. i2c_interrupt_enable(handle->i2c_x, I2C_STOP_INT, TRUE);
  1201. }
  1202. /* the transfer has not been completed */
  1203. else
  1204. {
  1205. /* update the buffer pointer of transfers */
  1206. handle->comm.pbuff += handle->comm.psize;
  1207. /* set the number to be transferred */
  1208. if (handle->comm.pcount > MAX_TRANSFER_CNT)
  1209. {
  1210. handle->comm.psize = MAX_TRANSFER_CNT;
  1211. }
  1212. else
  1213. {
  1214. handle->comm.psize = handle->comm.pcount;
  1215. }
  1216. /* config dma channel, continue to transfer data */
  1217. i2c_dma_config(handle, handle->comm.pbuff, handle->comm.psize);
  1218. /* enable tdc interrupt */
  1219. i2c_interrupt_enable(handle->i2c_x, I2C_TDC_INT, TRUE);
  1220. }
  1221. }
  1222. break;
  1223. default:
  1224. break;
  1225. #endif
  1226. }
  1227. }
  1228. }
  1229. #ifdef BSP_USING_HARD_I2C1
  1230. void I2C1_EVT_IRQHandler(void)
  1231. {
  1232. /* enter interrupt */
  1233. rt_interrupt_enter();
  1234. i2c_evt_isr(i2cs[I2C1_INDEX].handle);
  1235. /* leave interrupt */
  1236. rt_interrupt_leave();
  1237. }
  1238. void I2C1_ERR_IRQHandler(void)
  1239. {
  1240. /* enter interrupt */
  1241. rt_interrupt_enter();
  1242. i2c_err_isr(i2cs[I2C1_INDEX].handle);
  1243. /* leave interrupt */
  1244. rt_interrupt_leave();
  1245. }
  1246. #if defined(BSP_I2C1_RX_USING_DMA)
  1247. void I2C1_RX_DMA_IRQHandler(void)
  1248. {
  1249. /* enter interrupt */
  1250. rt_interrupt_enter();
  1251. i2c_dma_isr(i2cs[I2C1_INDEX].handle);
  1252. /* leave interrupt */
  1253. rt_interrupt_leave();
  1254. }
  1255. #endif /* defined(BSP_I2C1_RX_USING_DMA) */
  1256. #if defined(BSP_I2C1_TX_USING_DMA)
  1257. void I2C1_TX_DMA_IRQHandler(void)
  1258. {
  1259. /* enter interrupt */
  1260. rt_interrupt_enter();
  1261. i2c_dma_isr(i2cs[I2C1_INDEX].handle);
  1262. /* leave interrupt */
  1263. rt_interrupt_leave();
  1264. }
  1265. #endif /* defined(BSP_I2C1_TX_USING_DMA) */
  1266. #endif
  1267. #ifdef BSP_USING_HARD_I2C2
  1268. void I2C2_EVT_IRQHandler(void)
  1269. {
  1270. /* enter interrupt */
  1271. rt_interrupt_enter();
  1272. i2c_evt_isr(i2cs[I2C2_INDEX].handle);
  1273. /* leave interrupt */
  1274. rt_interrupt_leave();
  1275. }
  1276. void I2C2_ERR_IRQHandler(void)
  1277. {
  1278. /* enter interrupt */
  1279. rt_interrupt_enter();
  1280. i2c_err_isr(i2cs[I2C2_INDEX].handle);
  1281. /* leave interrupt */
  1282. rt_interrupt_leave();
  1283. }
  1284. #if defined(BSP_I2C2_RX_USING_DMA)
  1285. void I2C2_RX_DMA_IRQHandler(void)
  1286. {
  1287. /* enter interrupt */
  1288. rt_interrupt_enter();
  1289. i2c_dma_isr(i2cs[I2C2_INDEX].handle);
  1290. /* leave interrupt */
  1291. rt_interrupt_leave();
  1292. }
  1293. #endif /* defined(BSP_I2C2_RX_USING_DMA) */
  1294. #if defined(BSP_I2C2_TX_USING_DMA)
  1295. void I2C2_TX_DMA_IRQHandler(void)
  1296. {
  1297. /* enter interrupt */
  1298. rt_interrupt_enter();
  1299. i2c_dma_isr(i2cs[I2C2_INDEX].handle);
  1300. /* leave interrupt */
  1301. rt_interrupt_leave();
  1302. }
  1303. #endif /* defined(BSP_I2C2_TX_USING_DMA) */
  1304. #endif
  1305. #ifdef BSP_USING_HARD_I2C3
  1306. void I2C3_EVT_IRQHandler(void)
  1307. {
  1308. /* enter interrupt */
  1309. rt_interrupt_enter();
  1310. i2c_evt_isr(i2cs[I2C3_INDEX].handle);
  1311. /* leave interrupt */
  1312. rt_interrupt_leave();
  1313. }
  1314. void I2C3_ERR_IRQHandler(void)
  1315. {
  1316. /* enter interrupt */
  1317. rt_interrupt_enter();
  1318. i2c_err_isr(i2cs[I2C3_INDEX].handle);
  1319. /* leave interrupt */
  1320. rt_interrupt_leave();
  1321. }
  1322. #if defined(BSP_I2C3_RX_USING_DMA)
  1323. void I2C3_RX_DMA_IRQHandler(void)
  1324. {
  1325. /* enter interrupt */
  1326. rt_interrupt_enter();
  1327. i2c_dma_isr(i2cs[I2C3_INDEX].handle);
  1328. /* leave interrupt */
  1329. rt_interrupt_leave();
  1330. }
  1331. #endif /* defined(BSP_I2C3_RX_USING_DMA) */
  1332. #if defined(BSP_I2C3_TX_USING_DMA)
  1333. void I2C3_TX_DMA_IRQHandler(void)
  1334. {
  1335. /* enter interrupt */
  1336. rt_interrupt_enter();
  1337. i2c_dma_isr(i2cs[I2C3_INDEX].handle);
  1338. /* leave interrupt */
  1339. rt_interrupt_leave();
  1340. }
  1341. #endif /* defined(BSP_I2C3_TX_USING_DMA) */
  1342. #endif
  1343. #if defined (SOC_SERIES_AT32F421) || defined (SOC_SERIES_AT32F425)
  1344. void I2C1_TX_RX_DMA_IRQHandler(void)
  1345. {
  1346. #if defined(BSP_USING_HARD_I2C1) && defined(BSP_I2C1_TX_USING_DMA)
  1347. I2C1_TX_DMA_IRQHandler();
  1348. #endif
  1349. #if defined(BSP_USING_HARD_I2C1) && defined(BSP_I2C1_RX_USING_DMA)
  1350. I2C1_RX_DMA_IRQHandler();
  1351. #endif
  1352. }
  1353. void I2C2_TX_RX_DMA_IRQHandler(void)
  1354. {
  1355. #if defined(BSP_USING_HARD_I2C2) && defined(BSP_I2C2_TX_USING_DMA)
  1356. I2C2_TX_DMA_IRQHandler();
  1357. #endif
  1358. #if defined(BSP_USING_HARD_I2C2) && defined(BSP_I2C2_RX_USING_DMA)
  1359. I2C2_RX_DMA_IRQHandler();
  1360. #endif
  1361. }
  1362. #endif
  1363. static void at32_i2c_get_dma_config(void)
  1364. {
  1365. #ifdef BSP_USING_HARD_I2C1
  1366. i2c_handle[I2C1_INDEX].i2c_dma_flag = 0;
  1367. #ifdef BSP_I2C1_RX_USING_DMA
  1368. i2c_handle[I2C1_INDEX].i2c_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1369. static struct dma_config i2c1_dma_rx = I2C1_RX_DMA_CONFIG;
  1370. i2c_handle[I2C1_INDEX].dma_rx = &i2c1_dma_rx;
  1371. #endif
  1372. #ifdef BSP_I2C1_TX_USING_DMA
  1373. i2c_handle[I2C1_INDEX].i2c_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1374. static struct dma_config i2c1_dma_tx = I2C1_TX_DMA_CONFIG;
  1375. i2c_handle[I2C1_INDEX].dma_tx = &i2c1_dma_tx;
  1376. #endif
  1377. #endif
  1378. #ifdef BSP_USING_HARD_I2C2
  1379. i2c_handle[I2C2_INDEX].i2c_dma_flag = 0;
  1380. #ifdef BSP_I2C2_RX_USING_DMA
  1381. i2c_handle[I2C2_INDEX].i2c_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1382. static struct dma_config i2c2_dma_rx = I2C2_RX_DMA_CONFIG;
  1383. i2c_handle[I2C2_INDEX].dma_rx = &i2c2_dma_rx;
  1384. #endif
  1385. #ifdef BSP_I2C2_TX_USING_DMA
  1386. i2c_handle[I2C2_INDEX].i2c_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1387. static struct dma_config i2c2_dma_tx = I2C2_TX_DMA_CONFIG;
  1388. i2c_handle[I2C2_INDEX].dma_tx = &i2c2_dma_tx;
  1389. #endif
  1390. #endif
  1391. #ifdef BSP_USING_HARD_I2C3
  1392. i2c_handle[I2C3_INDEX].i2c_dma_flag = 0;
  1393. #ifdef BSP_I2C3_RX_USING_DMA
  1394. i2c_handle[I2C3_INDEX].i2c_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  1395. static struct dma_config i2c3_dma_rx = I2C3_RX_DMA_CONFIG;
  1396. i2c_handle[I2C3_INDEX].dma_rx = &i2c3_dma_rx;
  1397. #endif
  1398. #ifdef BSP_I2C3_TX_USING_DMA
  1399. i2c_handle[I2C3_INDEX].i2c_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  1400. static struct dma_config i2c3_dma_tx = I2C3_TX_DMA_CONFIG;
  1401. i2c_handle[I2C3_INDEX].dma_tx = &i2c3_dma_tx;
  1402. #endif
  1403. #endif
  1404. }
  1405. int rt_hw_hwi2c_init(void)
  1406. {
  1407. int i;
  1408. rt_err_t result;
  1409. rt_size_t obj_num = sizeof(i2c_handle) / sizeof(i2c_handle[0]);
  1410. at32_i2c_get_dma_config();
  1411. for (i = 0; i < obj_num; i++)
  1412. {
  1413. i2cs[i].handle = &i2c_handle[i];
  1414. i2cs[i].i2c_bus.ops = &at32_i2c_ops;
  1415. if(i2cs[i].handle->i2c_dma_flag & (RT_DEVICE_FLAG_DMA_RX | RT_DEVICE_FLAG_DMA_TX))
  1416. {
  1417. at32_i2c_dma_init(&i2cs[i]);
  1418. }
  1419. rt_completion_init(&i2cs[i].handle->completion);
  1420. at32_i2c_configure(&(i2cs[i].i2c_bus));
  1421. result = rt_i2c_bus_device_register(&(i2cs[i].i2c_bus), i2cs[i].handle->i2c_name);
  1422. }
  1423. return result;
  1424. }
  1425. INIT_BOARD_EXPORT(rt_hw_hwi2c_init);
  1426. #endif