drv_hard_i2c.c 52 KB

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