drv_usart.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-12-22 onelife Initial creation for EFM32
  9. * 2011-01-17 onelife Merge with serial.c
  10. * 2011-05-06 onelife Add sync mode (SPI) support
  11. * 2011-06-14 onelife Fix a bug of TX by DMA
  12. * 2011-06-16 onelife Modify init function for EFM32 library v2.0.0
  13. * upgrading
  14. * 2011-07-07 onelife Modify write function to avoid sleep in ISR
  15. * 2011-07-26 onelife Add lock (semaphore) to prevent simultaneously
  16. * access
  17. * 2011-11-29 onelife Modify init function for EFM32 library v2.2.2
  18. * upgrading
  19. * 2011-12-09 onelife Add giant gecko support
  20. * 2011-12-09 onelife Add UART module support
  21. * 2011-12-20 onelife Add 9-bit SPI mode support
  22. * 2011-12-20 onelife Change SPI write format (same as SPI read)
  23. * 2011-12-20 onelife Change USART status format
  24. * 2011-12-27 onelife Utilize "USART_PRESENT", "USART_COUNT",
  25. * "UART_PRESENT" and "UART_COUNT"
  26. * 2012-05-16 onelife Fix a bug in rt_hw_usart_init()
  27. */
  28. /***************************************************************************//**
  29. * @addtogroup efm32
  30. * @{
  31. ******************************************************************************/
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "board.h"
  34. #include "hdl_interrupt.h"
  35. #include "drv_usart.h"
  36. #if (defined(RT_USING_USART0) || defined(RT_USING_USART1) || \
  37. defined(RT_USING_USART2) || defined(RT_USING_UART0) || \
  38. defined(RT_USING_UART1))
  39. #if ((defined(RT_USING_USART0) || defined(RT_USING_USART1) || \
  40. defined(RT_USING_USART2)) && !defined(USART_PRESENT))
  41. #error "USART module is not available"
  42. #endif
  43. #if ((defined(RT_USING_UART0) || defined(RT_USING_UART1)) && \
  44. !defined(UART_PRESENT))
  45. #error "UART module is not available"
  46. #endif
  47. /* Private typedef -----------------------------------------------------------*/
  48. /* Private define ------------------------------------------------------------*/
  49. /* Private macro -------------------------------------------------------------*/
  50. #ifdef RT_USART_DEBUG
  51. #define usart_debug(format,args...) rt_kprintf(format, ##args)
  52. #else
  53. #define usart_debug(format,args...)
  54. #endif
  55. /* Private variables ---------------------------------------------------------*/
  56. #ifdef RT_USING_USART0
  57. #if (RT_USING_USART0 >= EFM32_USART_LOCATION_COUNT)
  58. #error "Wrong location number"
  59. #endif
  60. struct rt_device usart0_device;
  61. static struct rt_semaphore usart0_lock;
  62. #endif
  63. #ifdef RT_USING_USART1
  64. #if (USART_COUNT <= 1)
  65. #error "Wrong unit number"
  66. #endif
  67. #if (RT_USING_USART1 >= EFM32_USART_LOCATION_COUNT)
  68. #error "Wrong location number"
  69. #endif
  70. struct rt_device usart1_device;
  71. static struct rt_semaphore usart1_lock;
  72. #endif
  73. #ifdef RT_USING_USART2
  74. #if (USART_COUNT <= 2)
  75. #error "Wrong unit number"
  76. #endif
  77. #if (RT_USING_USART2 >= EFM32_USART_LOCATION_COUNT)
  78. #error "Wrong location number"
  79. #endif
  80. struct rt_device usart2_device;
  81. static struct rt_semaphore usart2_lock;
  82. #endif
  83. #ifdef RT_USING_UART0
  84. #if (RT_USING_UART0 >= EFM32_UART_LOCATION_COUNT)
  85. #error "Wrong location number"
  86. #endif
  87. struct rt_device uart0_device;
  88. static struct rt_semaphore uart0_lock;
  89. #endif
  90. #ifdef RT_USING_UART1
  91. #if (UART_COUNT <= 1)
  92. #error "Wrong unit number"
  93. #endif
  94. #if (RT_USING_UART1 >= EFM32_UART_LOCATION_COUNT)
  95. #error "Wrong location number"
  96. #endif
  97. struct rt_device uart1_device;
  98. static struct rt_semaphore uart1_lock;
  99. #endif
  100. /* Private function prototypes -----------------------------------------------*/
  101. /* Private functions ---------------------------------------------------------*/
  102. /***************************************************************************//**
  103. * @brief
  104. * Initialize USART device
  105. *
  106. * @details
  107. *
  108. * @note
  109. *
  110. * @param[in] dev
  111. * Pointer to device descriptor
  112. *
  113. * @return
  114. * Error code
  115. ******************************************************************************/
  116. static rt_err_t rt_usart_init (rt_device_t dev)
  117. {
  118. struct efm32_usart_device_t *usart;
  119. usart = (struct efm32_usart_device_t *)(dev->user_data);
  120. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  121. {
  122. if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  123. {
  124. struct efm32_usart_dma_mode_t *dma_tx;
  125. dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
  126. usart->state |= USART_STATE_RX_BUSY;
  127. }
  128. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  129. {
  130. struct efm32_usart_int_mode_t *int_rx;
  131. int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  132. int_rx->data_ptr = RT_NULL;
  133. }
  134. /* Enable USART */
  135. USART_Enable(usart->usart_device, usartEnable);
  136. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  137. }
  138. return RT_EOK;
  139. }
  140. /***************************************************************************//**
  141. * @brief
  142. * Open USART device
  143. *
  144. * @details
  145. *
  146. * @note
  147. *
  148. * @param[in] dev
  149. * Pointer to device descriptor
  150. *
  151. * @param[in] oflag
  152. * Device open flag
  153. *
  154. * @return
  155. * Error code
  156. ******************************************************************************/
  157. static rt_err_t rt_usart_open(rt_device_t dev, rt_uint16_t oflag)
  158. {
  159. RT_ASSERT(dev != RT_NULL);
  160. struct efm32_usart_device_t *usart;
  161. usart = (struct efm32_usart_device_t *)(dev->user_data);
  162. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  163. {
  164. IRQn_Type rxIrq;
  165. //if (usart->state & USART_STATE_CONSOLE)
  166. { /* Allocate new RX buffer */
  167. struct efm32_usart_int_mode_t *int_mode;
  168. int_mode = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  169. if ((int_mode->data_ptr = rt_malloc(USART_RX_BUFFER_SIZE)) == RT_NULL)
  170. {
  171. usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
  172. return -RT_ENOMEM;
  173. }
  174. rt_memset(int_mode->data_ptr, 0, USART_RX_BUFFER_SIZE);
  175. int_mode->data_size = USART_RX_BUFFER_SIZE;
  176. int_mode->read_index = 0;
  177. int_mode->save_index = 0;
  178. }
  179. /* Enable RX interrupt */
  180. #if defined(UART_PRESENT)
  181. if (usart->state & USART_STATE_ASYNC_ONLY)
  182. {
  183. usart->usart_device->IEN = UART_IEN_RXDATAV;
  184. }
  185. else
  186. #endif
  187. {
  188. usart->usart_device->IEN = USART_IEN_RXDATAV;
  189. }
  190. /* Enable IRQ */
  191. switch (usart->unit)
  192. {
  193. case 0:
  194. #if defined(UART_PRESENT)
  195. if (usart->state & USART_STATE_ASYNC_ONLY)
  196. {
  197. rxIrq = UART0_RX_IRQn;
  198. }
  199. else
  200. #endif
  201. {
  202. rxIrq = USART0_RX_IRQn;
  203. }
  204. break;
  205. #if ((defined(USART_PRESENT) && (USART_COUNT > 1)) || \
  206. (defined(UART_PRESENT) && (UART_COUNT > 1)))
  207. case 1:
  208. #if (defined(UART_PRESENT) && (UART_COUNT > 1))
  209. if (usart->state & USART_STATE_ASYNC_ONLY)
  210. {
  211. rxIrq = UART1_RX_IRQn;
  212. }
  213. else
  214. #endif
  215. {
  216. rxIrq = USART1_RX_IRQn;
  217. }
  218. break;
  219. #endif
  220. #if ((defined(USART_PRESENT) && (USART_COUNT > 2)) || \
  221. (defined(UART_PRESENT) && (UART_COUNT > 2)))
  222. case 2:
  223. #if (defined(UART_PRESENT) && (UART_COUNT > 2))
  224. if (usart->state & USART_STATE_ASYNC_ONLY)
  225. {
  226. rxIrq = UART2_RX_IRQn;
  227. }
  228. else
  229. #endif
  230. {
  231. rxIrq = USART2_RX_IRQn;
  232. }
  233. break;
  234. #endif
  235. }
  236. if (oflag != RT_DEVICE_OFLAG_WRONLY)
  237. {
  238. NVIC_ClearPendingIRQ(rxIrq);
  239. NVIC_SetPriority(rxIrq, EFM32_IRQ_PRI_DEFAULT);
  240. NVIC_EnableIRQ(rxIrq);
  241. }
  242. }
  243. /* Clear Flag */
  244. #if defined(UART_PRESENT)
  245. if (usart->state & USART_STATE_ASYNC_ONLY)
  246. {
  247. usart->usart_device->IFC = _UART_IFC_MASK;
  248. }
  249. else
  250. #endif
  251. {
  252. usart->usart_device->IFC = _USART_IFC_MASK;
  253. }
  254. if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (oflag != RT_DEVICE_OFLAG_RDONLY))
  255. {
  256. /* DMA IRQ is enabled by DMA_Init() */
  257. NVIC_SetPriority(DMA_IRQn, EFM32_IRQ_PRI_DEFAULT);
  258. }
  259. usart->counter++;
  260. usart_debug("USART%d: Open with flag %x\n", usart->unit, oflag);
  261. return RT_EOK;
  262. }
  263. /***************************************************************************//**
  264. * @brief
  265. * Close USART device
  266. *
  267. * @details
  268. *
  269. * @note
  270. *
  271. * @param[in] dev
  272. * Pointer to device descriptor
  273. *
  274. * @return
  275. * Error code
  276. ******************************************************************************/
  277. static rt_err_t rt_usart_close(rt_device_t dev)
  278. {
  279. RT_ASSERT(dev != RT_NULL);
  280. struct efm32_usart_device_t *usart;
  281. usart = (struct efm32_usart_device_t *)(dev->user_data);
  282. if (--usart->counter == 0)
  283. {
  284. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  285. {
  286. struct efm32_usart_int_mode_t *int_rx;
  287. int_rx = (struct efm32_usart_int_mode_t *)usart->rx_mode;
  288. rt_free(int_rx->data_ptr);
  289. int_rx->data_ptr = RT_NULL;
  290. }
  291. }
  292. return RT_EOK;
  293. }
  294. /***************************************************************************//**
  295. * @brief
  296. * Read from USART device
  297. *
  298. * @details
  299. *
  300. * @note
  301. * 9-bit SPI mode and SPI slave mode is untested
  302. *
  303. * @param[in] dev
  304. * Pointer to device descriptor
  305. *
  306. * @param[in] pos
  307. * Offset
  308. *
  309. * @param[in] buffer
  310. * Poniter to the buffer
  311. *
  312. * @param[in] size
  313. * Buffer size in byte
  314. *
  315. * @return
  316. * Number of read bytes
  317. ******************************************************************************/
  318. static rt_ssize_t rt_usart_read (
  319. rt_device_t dev,
  320. rt_off_t pos,
  321. void *buffer,
  322. rt_size_t size)
  323. {
  324. rt_err_t err_code;
  325. struct efm32_usart_device_t *usart;
  326. rt_size_t read_len, len;
  327. rt_uint8_t *ptr;
  328. rt_uint32_t rx_flag, tx_flag, b8_flag;
  329. usart = (struct efm32_usart_device_t *)(dev->user_data);
  330. #if defined(UART_PRESENT)
  331. if (usart->state & USART_STATE_ASYNC_ONLY)
  332. {
  333. rx_flag = UART_STATUS_RXDATAV;
  334. tx_flag = UART_STATUS_TXBL;
  335. b8_flag = UART_CTRL_BIT8DV;
  336. }
  337. else
  338. #endif
  339. {
  340. rx_flag = USART_STATUS_RXDATAV;
  341. tx_flag = USART_STATUS_TXBL;
  342. b8_flag = USART_CTRL_BIT8DV;
  343. }
  344. /* Lock device */
  345. if (rt_hw_interrupt_check())
  346. {
  347. err_code = rt_sem_take(usart->lock, RT_WAITING_NO);
  348. }
  349. else
  350. {
  351. err_code = rt_sem_take(usart->lock, RT_WAITING_FOREVER);
  352. }
  353. if (err_code != RT_EOK)
  354. {
  355. rt_set_errno(err_code);
  356. return 0;
  357. }
  358. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  359. {
  360. len = size;
  361. ptr = buffer;
  362. /* interrupt mode Rx */
  363. while (len)
  364. {
  365. rt_base_t level;
  366. struct efm32_usart_int_mode_t *int_rx;
  367. int_rx = (struct efm32_usart_int_mode_t *)\
  368. (((struct efm32_usart_device_t *)(dev->user_data))->rx_mode);
  369. /* disable interrupt */
  370. level = rt_hw_interrupt_disable();
  371. if (int_rx->read_index != int_rx->save_index)
  372. {
  373. /* read a character */
  374. *ptr++ = int_rx->data_ptr[int_rx->read_index];
  375. len--;
  376. /* move to next position */
  377. int_rx->read_index ++;
  378. if (int_rx->read_index >= USART_RX_BUFFER_SIZE)
  379. {
  380. int_rx->read_index = 0;
  381. }
  382. }
  383. else
  384. {
  385. /* set error code */
  386. err_code = -RT_EEMPTY;
  387. /* enable interrupt */
  388. rt_hw_interrupt_enable(level);
  389. break;
  390. }
  391. /* enable interrupt */
  392. rt_hw_interrupt_enable(level);
  393. }
  394. read_len = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  395. }
  396. else
  397. {
  398. if (usart->state & USART_STATE_SYNC)
  399. {
  400. /* SPI read */
  401. rt_uint8_t inst_len = *((rt_uint8_t *)buffer);
  402. rt_uint8_t *inst_ptr = (rt_uint8_t *)(buffer + 1);
  403. rt_uint8_t *rx_buf = *((rt_uint8_t **)(buffer + inst_len + 1));
  404. rt_off_t i;
  405. ptr = inst_ptr;
  406. len = inst_len;
  407. /* Write instructions */
  408. if (len)
  409. {
  410. if (usart->state & USART_STATE_9BIT)
  411. {
  412. usart->usart_device->CTRL &= ~b8_flag;
  413. }
  414. while (len)
  415. {
  416. while (!(usart->usart_device->STATUS & tx_flag));
  417. usart->usart_device->TXDATA = (rt_uint32_t)*(ptr++);
  418. len--;
  419. }
  420. if (usart->state & USART_STATE_9BIT)
  421. {
  422. usart->usart_device->CTRL |= b8_flag;
  423. }
  424. }
  425. /* Flushing RX */
  426. usart->usart_device->CMD = USART_CMD_CLEARRX;
  427. /* Skip some bytes if necessary */
  428. for (i = 0; i < pos; i++)
  429. {
  430. /* dummy write */
  431. while (!(usart->usart_device->STATUS & tx_flag));
  432. usart->usart_device->TXDATA = (rt_uint32_t)0xff;
  433. /* dummy read */
  434. while (!(usart->usart_device->STATUS & rx_flag));
  435. *((rt_uint32_t *)0x00) = usart->usart_device->RXDATA;
  436. }
  437. ptr = rx_buf;
  438. len = size;
  439. /* Read data */
  440. while (len)
  441. {
  442. /* dummy write */
  443. while (!(usart->usart_device->STATUS & tx_flag));
  444. usart->usart_device->TXDATA = (rt_uint32_t)0xff;
  445. /* read a byte of data */
  446. while (!(usart->usart_device->STATUS & rx_flag));
  447. *(ptr++) = usart->usart_device->RXDATA & 0xff;
  448. len--;
  449. }
  450. }
  451. else
  452. {
  453. ptr = buffer;
  454. len = size;
  455. /* polling mode */
  456. while (len)
  457. {
  458. while (usart->usart_device->STATUS & rx_flag)
  459. {
  460. *(ptr++) = usart->usart_device->RXDATA & 0xff;
  461. }
  462. len--;
  463. }
  464. }
  465. read_len = size - len;
  466. }
  467. /* Unlock device */
  468. rt_sem_release(usart->lock);
  469. /* set error code */
  470. rt_set_errno(err_code);
  471. return read_len;
  472. }
  473. /***************************************************************************//**
  474. * @brief
  475. * Write to USART device
  476. *
  477. * @details
  478. *
  479. * @note
  480. *
  481. * @param[in] dev
  482. * Pointer to device descriptor
  483. *
  484. * @param[in] pos
  485. * Offset
  486. *
  487. * @param[in] buffer
  488. * Poniter to the buffer
  489. *
  490. * @param[in] size
  491. * Buffer size in byte
  492. *
  493. * @return
  494. * Number of written bytes
  495. ******************************************************************************/
  496. static rt_ssize_t rt_usart_write (
  497. rt_device_t dev,
  498. rt_off_t pos,
  499. const void* buffer,
  500. rt_size_t size)
  501. {
  502. rt_err_t err_code;
  503. struct efm32_usart_device_t* usart = (struct efm32_usart_device_t*)(dev->user_data);
  504. rt_size_t read_len, len;
  505. rt_uint8_t *ptr;
  506. rt_size_t write_size = 0;
  507. rt_uint32_t tx_flag, b8_flag;
  508. #if defined(UART_PRESENT)
  509. if (usart->state & USART_STATE_ASYNC_ONLY)
  510. {
  511. tx_flag = UART_STATUS_TXBL;
  512. b8_flag = UART_CTRL_BIT8DV;
  513. }
  514. else
  515. #endif
  516. {
  517. tx_flag = USART_STATUS_TXBL;
  518. b8_flag = USART_CTRL_BIT8DV;
  519. }
  520. /* Lock device */
  521. if (rt_hw_interrupt_check())
  522. {
  523. err_code = rt_sem_take(usart->lock, RT_WAITING_NO);
  524. }
  525. else
  526. {
  527. err_code = rt_sem_take(usart->lock, RT_WAITING_FOREVER);
  528. }
  529. if (err_code != RT_EOK)
  530. {
  531. rt_set_errno(err_code);
  532. return 0;
  533. }
  534. if (usart->state & USART_STATE_SYNC)
  535. { /* SPI write */
  536. rt_uint8_t inst_len = *((rt_uint8_t *)buffer);
  537. rt_uint8_t *inst_ptr = (rt_uint8_t *)(buffer + 1);
  538. rt_uint8_t *tx_buf = *((rt_uint8_t **)(buffer + inst_len + 1));
  539. ptr = inst_ptr;
  540. len = inst_len;
  541. /* Write instructions */
  542. if (len)
  543. {
  544. if (usart->state & USART_STATE_9BIT)
  545. {
  546. usart->usart_device->CTRL &= ~b8_flag;
  547. }
  548. if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (len > 2))
  549. { /* DMA mode Tx */
  550. struct efm32_usart_dma_mode_t *dma_tx;
  551. usart_debug("USART: DMA TX INS (%d)\n", len);
  552. dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
  553. dma_tx->data_ptr = (rt_uint32_t *)ptr;
  554. dma_tx->data_size = len;
  555. usart->state |= USART_STATE_TX_BUSY;
  556. DMA_ActivateBasic(
  557. dma_tx->dma_channel,
  558. true,
  559. false,
  560. (void *)&(usart->usart_device->TXDATA),
  561. (void *)ptr,
  562. (rt_uint32_t)(len - 1));
  563. /* Wait, otherwise the TX buffer is overwrite */
  564. // TODO: This function blocks the process => goto low power mode?
  565. // if (usart->state & USART_STATE_CONSOLE)
  566. // {
  567. while(usart->state & USART_STATE_TX_BUSY);
  568. // }
  569. // else
  570. // {
  571. // while(usart->state & USART_STATE_TX_BUSY)
  572. // {
  573. // rt_thread_delay(USART_WAIT_TIME_TX);
  574. // }
  575. // }
  576. }
  577. else
  578. { /* polling mode */
  579. usart_debug("USART: Polling TX INS (%d)\n", len);
  580. while (len)
  581. {
  582. while (!(usart->usart_device->STATUS & tx_flag));
  583. usart->usart_device->TXDATA = (rt_uint32_t)*(ptr++);
  584. len--;
  585. }
  586. }
  587. if (usart->state & USART_STATE_9BIT)
  588. {
  589. usart->usart_device->CTRL |= b8_flag;
  590. }
  591. }
  592. ptr = tx_buf;
  593. }
  594. else
  595. {
  596. ptr = (rt_uint8_t *)buffer;
  597. }
  598. len = size;
  599. /* Write data */
  600. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  601. {
  602. if (*(ptr + len - 1) == '\n')
  603. {
  604. *(ptr + len - 1) = '\r';
  605. *(ptr + len++) = '\n';
  606. *(ptr + len) = 0;
  607. }
  608. }
  609. if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (len > 2))
  610. { /* DMA mode Tx */
  611. struct efm32_usart_dma_mode_t *dma_tx;
  612. usart_debug("USART: DMA TX data (%d)\n", len);
  613. dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
  614. dma_tx->data_ptr = (rt_uint32_t *)ptr;
  615. dma_tx->data_size = len;
  616. usart->state |= USART_STATE_TX_BUSY;
  617. DMA_ActivateBasic(
  618. dma_tx->dma_channel,
  619. true,
  620. false,
  621. (void *)&(usart->usart_device->TXDATA),
  622. (void *)ptr,
  623. (rt_uint32_t)(len - 1));
  624. /* Wait, otherwise the TX buffer is overwrite */
  625. // TODO: This function blocks the process => goto low power mode?
  626. // if (usart->state & USART_STATE_CONSOLE)
  627. // {
  628. while(usart->state & USART_STATE_TX_BUSY);
  629. // }
  630. // else
  631. // {
  632. // while(usart->state & USART_STATE_TX_BUSY)
  633. // {
  634. // rt_thread_delay(USART_WAIT_TIME_TX);
  635. // }
  636. // }
  637. write_size = size;
  638. }
  639. else
  640. { /* polling mode */
  641. usart_debug("USART: Polling TX data (%d)\n", len);
  642. while (len)
  643. {
  644. while (!(usart->usart_device->STATUS & tx_flag));
  645. usart->usart_device->TXDATA = (rt_uint32_t)*(ptr++);
  646. len--;
  647. }
  648. write_size = size - len;
  649. }
  650. /* Unlock device */
  651. rt_sem_release(usart->lock);
  652. /* set error code */
  653. rt_set_errno(err_code);
  654. return write_size;
  655. }
  656. /***************************************************************************//**
  657. * @brief
  658. * Configure USART device
  659. *
  660. * @details
  661. *
  662. * @note
  663. *
  664. * @param[in] dev
  665. * Pointer to device descriptor
  666. *
  667. * @param[in] cmd
  668. * IIC control command
  669. *
  670. * @param[in] args
  671. * Arguments
  672. *
  673. * @return
  674. * Error code
  675. ******************************************************************************/
  676. static rt_err_t rt_usart_control (
  677. rt_device_t dev,
  678. rt_uint8_t cmd,
  679. void *args)
  680. {
  681. RT_ASSERT(dev != RT_NULL);
  682. rt_err_t err_code;
  683. struct efm32_usart_device_t *usart;
  684. usart = (struct efm32_usart_device_t *)(dev->user_data);
  685. /* Lock device */
  686. if (rt_hw_interrupt_check())
  687. {
  688. err_code = rt_sem_take(usart->lock, RT_WAITING_NO);
  689. }
  690. else
  691. {
  692. err_code = rt_sem_take(usart->lock, RT_WAITING_FOREVER);
  693. }
  694. if (err_code != RT_EOK)
  695. {
  696. return err_code;
  697. }
  698. switch (cmd)
  699. {
  700. case RT_DEVICE_CTRL_SUSPEND:
  701. /* Suspend device */
  702. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  703. USART_Enable(usart->usart_device, usartDisable);
  704. break;
  705. case RT_DEVICE_CTRL_RESUME:
  706. /* Resume device */
  707. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  708. USART_Enable(usart->usart_device, usartEnable);
  709. break;
  710. case RT_DEVICE_CTRL_USART_RBUFFER:
  711. /* Set RX buffer */
  712. {
  713. struct efm32_usart_int_mode_t *int_rx;
  714. rt_uint8_t size;
  715. int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  716. size = (rt_uint8_t)((rt_uint32_t)args & 0xFFUL);
  717. /* Free previous RX buffer */
  718. if (int_rx->data_ptr != RT_NULL)
  719. {
  720. if (size == 0)
  721. { /* Free RX buffer */
  722. rt_free(int_rx->data_ptr);
  723. int_rx->data_ptr = RT_NULL;
  724. }
  725. else if (size != int_rx->data_size)
  726. {
  727. /* Re-allocate RX buffer */
  728. if ((int_rx->data_ptr = rt_realloc(int_rx->data_ptr, size)) \
  729. == RT_NULL)
  730. {
  731. usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
  732. err_code = -RT_ENOMEM;
  733. break;
  734. }
  735. // TODO: Is the following line necessary?
  736. //rt_memset(int_rx->data_ptr, 0, size);
  737. }
  738. }
  739. else
  740. {
  741. /* Allocate new RX buffer */
  742. if ((int_rx->data_ptr = rt_malloc(size)) == RT_NULL)
  743. {
  744. usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
  745. err_code = -RT_ENOMEM;
  746. break;
  747. }
  748. }
  749. int_rx->data_size = size;
  750. int_rx->read_index = 0;
  751. int_rx->save_index = 0;
  752. }
  753. break;
  754. }
  755. /* Unlock device */
  756. rt_sem_release(usart->lock);
  757. return err_code;
  758. }
  759. /***************************************************************************//**
  760. * @brief
  761. * USART RX data valid interrupt handler
  762. *
  763. * @details
  764. *
  765. * @note
  766. * 9-bit SPI mode has not implemented yet and SPI slave mode is untested
  767. *
  768. * @param[in] dev
  769. * Pointer to device descriptor
  770. ******************************************************************************/
  771. void rt_hw_usart_rx_isr(rt_device_t dev)
  772. {
  773. struct efm32_usart_device_t *usart;
  774. struct efm32_usart_int_mode_t *int_rx;
  775. rt_uint32_t flag;
  776. /* interrupt mode receive */
  777. RT_ASSERT(dev->flag & RT_DEVICE_FLAG_INT_RX);
  778. usart = (struct efm32_usart_device_t *)(dev->user_data);
  779. int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  780. RT_ASSERT(int_rx->data_ptr != RT_NULL);
  781. #if defined(UART_PRESENT)
  782. if (usart->state & USART_STATE_ASYNC_ONLY)
  783. {
  784. flag = UART_STATUS_RXDATAV;
  785. }
  786. else
  787. #endif
  788. {
  789. flag = USART_STATUS_RXDATAV;
  790. }
  791. /* Set status */
  792. usart->state |= USART_STATE_RX_BUSY;
  793. /* save into rx buffer */
  794. while (usart->usart_device->STATUS & flag)
  795. {
  796. rt_base_t level;
  797. /* disable interrupt */
  798. level = rt_hw_interrupt_disable();
  799. /* save character */
  800. int_rx->data_ptr[int_rx->save_index] = \
  801. (rt_uint8_t)(usart->usart_device->RXDATA & 0xFFUL);
  802. int_rx->save_index ++;
  803. if (int_rx->save_index >= USART_RX_BUFFER_SIZE)
  804. int_rx->save_index = 0;
  805. /* if the next position is read index, discard this 'read char' */
  806. if (int_rx->save_index == int_rx->read_index)
  807. {
  808. int_rx->read_index ++;
  809. if (int_rx->read_index >= USART_RX_BUFFER_SIZE)
  810. {
  811. int_rx->read_index = 0;
  812. }
  813. }
  814. /* enable interrupt */
  815. rt_hw_interrupt_enable(level);
  816. }
  817. /* invoke callback */
  818. if (dev->rx_indicate != RT_NULL)
  819. {
  820. rt_size_t rx_length;
  821. /* get rx length */
  822. rx_length = int_rx->read_index > int_rx->save_index ?
  823. USART_RX_BUFFER_SIZE - int_rx->read_index + int_rx->save_index : \
  824. int_rx->save_index - int_rx->read_index;
  825. dev->rx_indicate(dev, rx_length);
  826. }
  827. }
  828. /***************************************************************************//**
  829. * @brief
  830. * DMA for USART TX interrupt handler
  831. *
  832. * @details
  833. *
  834. * @note
  835. *
  836. * @param[in] dev
  837. * Pointer to device descriptor
  838. ******************************************************************************/
  839. void rt_hw_usart_dma_tx_isr(rt_device_t dev)
  840. {
  841. /* DMA mode receive */
  842. struct efm32_usart_device_t *usart;
  843. struct efm32_usart_dma_mode_t *dma_tx;
  844. RT_ASSERT(dev->flag & RT_DEVICE_FLAG_DMA_TX);
  845. usart = (struct efm32_usart_device_t *)(dev->user_data);
  846. dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
  847. /* invoke call to notify tx complete */
  848. if (dev->tx_complete != RT_NULL)
  849. {
  850. dev->tx_complete(dev, dma_tx->data_ptr);
  851. }
  852. /* Set status */
  853. usart->state &= ~(rt_uint32_t)USART_STATE_TX_BUSY;
  854. }
  855. /***************************************************************************//**
  856. * @brief
  857. * Register USART device
  858. *
  859. * @details
  860. *
  861. * @note
  862. *
  863. * @param[in] device
  864. * Pointer to device descriptor
  865. *
  866. * @param[in] name
  867. * Device name
  868. *
  869. * @param[in] flag
  870. * Configuration flags
  871. *
  872. * @param[in] usart
  873. * Pointer to USART device descriptor
  874. *
  875. * @return
  876. * Error code
  877. ******************************************************************************/
  878. rt_err_t rt_hw_usart_register(
  879. rt_device_t device,
  880. const char *name,
  881. rt_uint32_t flag,
  882. struct efm32_usart_device_t *usart)
  883. {
  884. RT_ASSERT(device != RT_NULL);
  885. if ((flag & RT_DEVICE_FLAG_DMA_RX) ||
  886. (flag & RT_DEVICE_FLAG_INT_TX))
  887. {
  888. RT_ASSERT(0);
  889. }
  890. if (usart->state & USART_STATE_SYNC)
  891. {
  892. device->type = RT_Device_Class_SPIBUS;
  893. }
  894. else
  895. {
  896. device->type = RT_Device_Class_Char;
  897. }
  898. device->rx_indicate = RT_NULL;
  899. device->tx_complete = RT_NULL;
  900. device->init = rt_usart_init;
  901. device->open = rt_usart_open;
  902. device->close = rt_usart_close;
  903. device->read = rt_usart_read;
  904. device->write = rt_usart_write;
  905. device->control = rt_usart_control;
  906. device->user_data = usart;
  907. /* register a character device */
  908. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  909. }
  910. /***************************************************************************//**
  911. * @brief
  912. * Initialize the specified USART unit
  913. *
  914. * @details
  915. *
  916. * @note
  917. *
  918. * @param[in] device
  919. * Pointer to device descriptor
  920. *
  921. * @param[in] unitNumber
  922. * Unit number
  923. *
  924. * @param[in] location
  925. * Pin location number
  926. *
  927. * @param[in] flag
  928. * Configuration flag
  929. *
  930. * @param[in] dmaChannel
  931. * DMA channel number for TX
  932. *
  933. * @param[in] console
  934. * Indicate if using as console
  935. *
  936. * @return
  937. * Pointer to USART device
  938. ******************************************************************************/
  939. static struct efm32_usart_device_t *rt_hw_usart_unit_init(
  940. rt_device_t device,
  941. rt_uint8_t unitNumber,
  942. rt_uint8_t location,
  943. rt_uint32_t flag,
  944. rt_uint32_t dmaChannel,
  945. rt_uint8_t config)
  946. {
  947. struct efm32_usart_device_t *usart;
  948. struct efm32_usart_dma_mode_t *dma_mode;
  949. DMA_CB_TypeDef *callback;
  950. CMU_Clock_TypeDef usartClock;
  951. rt_uint32_t txDmaSelect;
  952. GPIO_Port_TypeDef port_tx, port_rx, port_clk, port_cs;
  953. rt_uint32_t pin_tx, pin_rx, pin_clk, pin_cs;
  954. efm32_irq_hook_init_t hook;
  955. do
  956. {
  957. /* Allocate device */
  958. usart = rt_malloc(sizeof(struct efm32_usart_device_t));
  959. if (usart == RT_NULL)
  960. {
  961. usart_debug("USART%d err: no mem\n", usart->unit);
  962. break;
  963. }
  964. usart->counter = 0;
  965. usart->unit = unitNumber;
  966. usart->state = config;
  967. usart->tx_mode = RT_NULL;
  968. usart->rx_mode = RT_NULL;
  969. /* Allocate TX */
  970. dma_mode = RT_NULL;
  971. if (flag & RT_DEVICE_FLAG_DMA_TX)
  972. {
  973. usart->tx_mode = dma_mode = rt_malloc(sizeof(struct efm32_usart_dma_mode_t));
  974. if (dma_mode == RT_NULL)
  975. {
  976. usart_debug("USART%d err: no mem for DMA TX\n", usart->unit);
  977. break;
  978. }
  979. dma_mode->dma_channel = dmaChannel;
  980. }
  981. /* Allocate RX */
  982. if (flag & RT_DEVICE_FLAG_INT_RX)
  983. {
  984. usart->rx_mode = rt_malloc(sizeof(struct efm32_usart_int_mode_t));
  985. if (usart->rx_mode == RT_NULL)
  986. {
  987. usart_debug("USART%d err: no mem for INT RX\n", usart->unit);
  988. break;
  989. }
  990. }
  991. /* Initialization */
  992. #if defined(UART_PRESENT)
  993. if ((!(config & USART_STATE_ASYNC_ONLY) && (unitNumber >= USART_COUNT)) || \
  994. ((config & USART_STATE_ASYNC_ONLY) && (unitNumber >= UART_COUNT)))
  995. #else
  996. if (unitNumber >= USART_COUNT)
  997. #endif
  998. {
  999. break;
  1000. }
  1001. switch (unitNumber)
  1002. {
  1003. case 0:
  1004. #if defined(UART_PRESENT)
  1005. if (config & USART_STATE_ASYNC_ONLY)
  1006. {
  1007. usart->usart_device = UART0;
  1008. usartClock = (CMU_Clock_TypeDef)cmuClock_UART0;
  1009. txDmaSelect = DMAREQ_UART0_TXBL;
  1010. port_tx = AF_UART0_TX_PORT(location);
  1011. pin_tx = AF_UART0_TX_PIN(location);
  1012. port_rx = AF_UART0_RX_PORT(location);
  1013. pin_rx = AF_UART0_RX_PIN(location);
  1014. }
  1015. else
  1016. #endif
  1017. {
  1018. usart->usart_device = USART0;
  1019. usartClock = (CMU_Clock_TypeDef)cmuClock_USART0;
  1020. txDmaSelect = DMAREQ_USART0_TXBL;
  1021. port_tx = AF_USART0_TX_PORT(location);
  1022. pin_tx = AF_USART0_TX_PIN(location);
  1023. port_rx = AF_USART0_RX_PORT(location);
  1024. pin_rx = AF_USART0_RX_PIN(location);
  1025. port_clk = AF_USART0_CLK_PORT(location);
  1026. pin_clk = AF_USART0_CLK_PIN(location);
  1027. port_cs = AF_USART0_CS_PORT(location);
  1028. pin_cs = AF_USART0_CS_PIN(location);
  1029. }
  1030. break;
  1031. #if ((defined(USART_PRESENT) && (USART_COUNT > 1)) || \
  1032. (defined(UART_PRESENT) && (UART_COUNT > 1)))
  1033. case 1:
  1034. #if (defined(UART_PRESENT) && (UART_COUNT > 1))
  1035. if (config & USART_STATE_ASYNC_ONLY)
  1036. {
  1037. usart->usart_device = UART1;
  1038. usartClock = (CMU_Clock_TypeDef)cmuClock_UART1;
  1039. txDmaSelect = DMAREQ_UART1_TXBL;
  1040. port_tx = AF_UART1_TX_PORT(location);
  1041. pin_tx = AF_UART1_TX_PIN(location);
  1042. port_rx = AF_UART1_RX_PORT(location);
  1043. pin_rx = AF_UART1_RX_PIN(location);
  1044. }
  1045. else
  1046. #endif
  1047. {
  1048. usart->usart_device = USART1;
  1049. usartClock = (CMU_Clock_TypeDef)cmuClock_USART1;
  1050. txDmaSelect = DMAREQ_USART1_TXBL;
  1051. port_tx = AF_USART1_TX_PORT(location);
  1052. pin_tx = AF_USART1_TX_PIN(location);
  1053. port_rx = AF_USART1_RX_PORT(location);
  1054. pin_rx = AF_USART1_RX_PIN(location);
  1055. port_clk = AF_USART1_CLK_PORT(location);
  1056. pin_clk = AF_USART1_CLK_PIN(location);
  1057. port_cs = AF_USART1_CS_PORT(location);
  1058. pin_cs = AF_USART1_CS_PIN(location);
  1059. }
  1060. break;
  1061. #endif
  1062. #if ((defined(USART_PRESENT) && (USART_COUNT > 2)) || \
  1063. (defined(UART_PRESENT) && (UART_COUNT > 2)))
  1064. case 2:
  1065. #if (defined(UART_PRESENT) && (UART_COUNT > 2))
  1066. if (config & USART_STATE_ASYNC_ONLY)
  1067. {
  1068. usart->usart_device = UART2;
  1069. usartClock = (CMU_Clock_TypeDef)cmuClock_UART2;
  1070. txDmaSelect = DMAREQ_UART2_TXBL;
  1071. port_tx = AF_UART2_TX_PORT(location);
  1072. pin_tx = AF_UART2_TX_PIN(location);
  1073. port_rx = AF_UART2_RX_PORT(location);
  1074. pin_rx = AF_UART2_RX_PIN(location);
  1075. }
  1076. else
  1077. #endif
  1078. {
  1079. usart->usart_device = USART2;
  1080. usartClock = (CMU_Clock_TypeDef)cmuClock_USART2;
  1081. txDmaSelect = DMAREQ_USART2_TXBL;
  1082. port_tx = AF_USART2_TX_PORT(location);
  1083. pin_tx = AF_USART2_TX_PIN(location);
  1084. port_rx = AF_USART2_RX_PORT(location);
  1085. pin_rx = AF_USART2_RX_PIN(location);
  1086. port_clk = AF_USART2_CLK_PORT(location);
  1087. pin_clk = AF_USART2_CLK_PIN(location);
  1088. port_cs = AF_USART2_CS_PORT(location);
  1089. pin_cs = AF_USART2_CS_PIN(location);
  1090. }
  1091. break;
  1092. #endif
  1093. default:
  1094. break;
  1095. }
  1096. /* Enable USART clock */
  1097. CMU_ClockEnable(usartClock, true);
  1098. /* Config GPIO */
  1099. GPIO_PinModeSet(
  1100. port_tx,
  1101. pin_tx,
  1102. gpioModePushPull,
  1103. 0);
  1104. GPIO_PinModeSet(
  1105. port_rx,
  1106. pin_rx,
  1107. gpioModeInputPull,
  1108. 1);
  1109. if (config & USART_STATE_SYNC)
  1110. {
  1111. GPIO_PinModeSet(
  1112. port_clk,
  1113. pin_clk,
  1114. gpioModePushPull,
  1115. 0);
  1116. }
  1117. if (config & USART_STATE_AUTOCS)
  1118. {
  1119. GPIO_PinModeSet(
  1120. port_cs,
  1121. pin_cs,
  1122. gpioModePushPull,
  1123. 1);
  1124. }
  1125. /* Config interrupt and NVIC */
  1126. if (flag & RT_DEVICE_FLAG_INT_RX)
  1127. {
  1128. hook.type = efm32_irq_type_usart;
  1129. hook.unit = unitNumber * 2 + 1;
  1130. #if defined(UART_PRESENT)
  1131. if (config & USART_STATE_ASYNC_ONLY)
  1132. {
  1133. hook.unit += USART_COUNT * 2;
  1134. }
  1135. #endif
  1136. hook.cbFunc = rt_hw_usart_rx_isr;
  1137. hook.userPtr = device;
  1138. efm32_irq_hook_register(&hook);
  1139. }
  1140. /* Config DMA */
  1141. if (flag & RT_DEVICE_FLAG_DMA_TX)
  1142. {
  1143. DMA_CfgChannel_TypeDef chnlCfg;
  1144. DMA_CfgDescr_TypeDef descrCfg;
  1145. hook.type = efm32_irq_type_dma;
  1146. hook.unit = dmaChannel;
  1147. hook.cbFunc = rt_hw_usart_dma_tx_isr;
  1148. hook.userPtr = device;
  1149. efm32_irq_hook_register(&hook);
  1150. callback = (DMA_CB_TypeDef *)rt_malloc(sizeof(DMA_CB_TypeDef));
  1151. if (callback == RT_NULL)
  1152. {
  1153. usart_debug("USART%d err: no mem for callback\n", usart->unit);
  1154. break;
  1155. }
  1156. callback->cbFunc = DMA_IRQHandler_All;
  1157. callback->userPtr = RT_NULL;
  1158. callback->primary = 0;
  1159. /* Setting up DMA channel */
  1160. chnlCfg.highPri = false; /* Can't use with peripherals */
  1161. chnlCfg.enableInt = true; /* Interrupt for callback function */
  1162. chnlCfg.select = txDmaSelect;
  1163. chnlCfg.cb = callback;
  1164. DMA_CfgChannel(dmaChannel, &chnlCfg);
  1165. /* Setting up DMA channel descriptor */
  1166. descrCfg.dstInc = dmaDataIncNone;
  1167. descrCfg.srcInc = dmaDataInc1;
  1168. descrCfg.size = dmaDataSize1;
  1169. descrCfg.arbRate = dmaArbitrate1;
  1170. descrCfg.hprot = 0;
  1171. DMA_CfgDescr(dmaChannel, true, &descrCfg);
  1172. }
  1173. /* Init specified USART unit */
  1174. if (config & USART_STATE_SYNC)
  1175. {
  1176. USART_InitSync_TypeDef init_sync = USART_INITSYNC_DEFAULT;
  1177. init_sync.enable = usartEnable;
  1178. init_sync.refFreq = 0;
  1179. init_sync.baudrate = SPI_BAUDRATE;
  1180. if (config & USART_STATE_9BIT)
  1181. {
  1182. init_sync.databits = usartDatabits9;
  1183. }
  1184. else
  1185. {
  1186. init_sync.databits = usartDatabits8;
  1187. }
  1188. if (config & USART_STATE_MASTER)
  1189. {
  1190. init_sync.master = true;
  1191. }
  1192. else
  1193. {
  1194. init_sync.master = false;
  1195. }
  1196. init_sync.msbf = true;
  1197. switch (USART_CLK_MODE_GET(config))
  1198. {
  1199. case 0:
  1200. init_sync.clockMode = usartClockMode0;
  1201. break;
  1202. case 1:
  1203. init_sync.clockMode = usartClockMode1;
  1204. break;
  1205. case 2:
  1206. init_sync.clockMode = usartClockMode2;
  1207. break;
  1208. case 3:
  1209. init_sync.clockMode = usartClockMode3;
  1210. break;
  1211. }
  1212. USART_InitSync(usart->usart_device, &init_sync);
  1213. }
  1214. else
  1215. {
  1216. USART_InitAsync_TypeDef init_async = USART_INITASYNC_DEFAULT;
  1217. init_async.enable = usartEnable;
  1218. init_async.refFreq = 0;
  1219. init_async.baudrate = UART_BAUDRATE;
  1220. init_async.oversampling = USART_CTRL_OVS_X4;
  1221. init_async.databits = USART_FRAME_DATABITS_EIGHT;
  1222. init_async.parity = USART_FRAME_PARITY_NONE;
  1223. init_async.stopbits = USART_FRAME_STOPBITS_ONE;
  1224. USART_InitAsync(usart->usart_device, &init_async);
  1225. }
  1226. /* Enable RX and TX pins and set location */
  1227. usart->usart_device->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | \
  1228. (location << _USART_ROUTE_LOCATION_SHIFT);
  1229. if (config & USART_STATE_SYNC)
  1230. {
  1231. usart->usart_device->ROUTE |= USART_ROUTE_CLKPEN;
  1232. }
  1233. if (config & USART_STATE_AUTOCS)
  1234. {
  1235. usart->usart_device->ROUTE |= USART_ROUTE_CSPEN;
  1236. if (config & USART_STATE_MASTER)
  1237. {
  1238. usart->usart_device->CTRL |= USART_CTRL_AUTOCS;
  1239. }
  1240. }
  1241. /* Clear RX/TX buffers */
  1242. usart->usart_device->CMD = USART_CMD_CLEARRX | USART_CMD_CLEARTX;
  1243. return usart;
  1244. } while(0);
  1245. if (usart->rx_mode)
  1246. {
  1247. rt_free(usart->rx_mode);
  1248. }
  1249. if (usart->tx_mode)
  1250. {
  1251. rt_free(usart->tx_mode);
  1252. }
  1253. if (usart)
  1254. {
  1255. rt_free(usart);
  1256. }
  1257. if (callback)
  1258. {
  1259. rt_free(callback);
  1260. }
  1261. #if defined(UART_PRESENT)
  1262. if (config & USART_STATE_ASYNC_ONLY)
  1263. {
  1264. usart_debug("UART%d err: init failed!\n", unitNumber);
  1265. }
  1266. else
  1267. #endif
  1268. {
  1269. usart_debug("USART%d err: init failed!\n", unitNumber);
  1270. }
  1271. return RT_NULL;
  1272. }
  1273. /***************************************************************************//**
  1274. * @brief
  1275. * Initialize all USART module related hardware and register USART device to
  1276. * kernel
  1277. *
  1278. * @details
  1279. *
  1280. * @note
  1281. ******************************************************************************/
  1282. void rt_hw_usart_init(void)
  1283. {
  1284. struct efm32_usart_device_t *usart;
  1285. rt_uint32_t flag;
  1286. rt_uint8_t config;
  1287. do
  1288. {
  1289. #if (defined(USART_PRESENT) && defined(RT_USING_USART0))
  1290. config = 0x00;
  1291. flag = RT_DEVICE_FLAG_RDWR;
  1292. #ifdef RT_USART0_SYNC_MODE
  1293. config |= USART_STATE_SYNC;
  1294. config |= (RT_USART0_SYNC_MODE & SYNC_SETTING_MASK) << SYNC_SETTING_SHIFT;
  1295. #if (!((RT_USART0_SYNC_MODE << SYNC_SETTING_SHIFT) & USART_STATE_MASTER))
  1296. flag |= RT_DEVICE_FLAG_INT_RX;
  1297. #endif
  1298. #else
  1299. flag |= RT_DEVICE_FLAG_INT_RX;
  1300. #endif
  1301. #if (RT_CONSOLE_DEVICE == EFM_USART0)
  1302. config |= USART_STATE_CONSOLE;
  1303. flag |= RT_DEVICE_FLAG_STREAM;
  1304. #endif
  1305. #ifdef RT_USART0_USING_DMA
  1306. RT_ASSERT(RT_USART0_USING_DMA < DMA_CHAN_COUNT);
  1307. flag |= RT_DEVICE_FLAG_DMA_TX;
  1308. #else
  1309. #define RT_USART0_USING_DMA EFM32_NO_DMA
  1310. #endif
  1311. /* Initialize and Register usart0 */
  1312. if ((usart = rt_hw_usart_unit_init(
  1313. &usart0_device,
  1314. 0,
  1315. RT_USING_USART0,
  1316. flag,
  1317. RT_USART0_USING_DMA,
  1318. config)) != RT_NULL)
  1319. {
  1320. rt_hw_usart_register(&usart0_device, RT_USART0_NAME, flag, usart);
  1321. }
  1322. else
  1323. {
  1324. break;
  1325. }
  1326. /* Initialize lock for usart0 */
  1327. usart->lock = &usart0_lock;
  1328. if (rt_sem_init(usart->lock, RT_USART0_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
  1329. {
  1330. break;
  1331. }
  1332. #endif
  1333. #if (defined(USART_PRESENT) && (USART_COUNT > 1) && defined(RT_USING_USART1))
  1334. config = 0x00;
  1335. flag = RT_DEVICE_FLAG_RDWR;
  1336. #ifdef RT_USART1_SYNC_MODE
  1337. config |= USART_STATE_SYNC;
  1338. config |= (RT_USART1_SYNC_MODE & SYNC_SETTING_MASK) << SYNC_SETTING_SHIFT;
  1339. #if (!((RT_USART1_SYNC_MODE << SYNC_SETTING_SHIFT) & USART_STATE_MASTER))
  1340. flag |= RT_DEVICE_FLAG_INT_RX;
  1341. #endif
  1342. #else
  1343. flag |= RT_DEVICE_FLAG_INT_RX;
  1344. #endif
  1345. #if (RT_CONSOLE_DEVICE == EFM_USART1)
  1346. config |= USART_STATE_CONSOLE;
  1347. flag |= RT_DEVICE_FLAG_STREAM;
  1348. #endif
  1349. #ifdef RT_USART1_USING_DMA
  1350. RT_ASSERT(RT_USART1_USING_DMA < DMA_CHAN_COUNT);
  1351. flag |= RT_DEVICE_FLAG_DMA_TX;
  1352. #else
  1353. #define RT_USART1_USING_DMA EFM32_NO_DMA
  1354. #endif
  1355. /* Initialize and Register usart1 */
  1356. if ((usart = rt_hw_usart_unit_init(
  1357. &usart1_device,
  1358. 1,
  1359. RT_USING_USART1,
  1360. flag,
  1361. RT_USART1_USING_DMA,
  1362. config)) != RT_NULL)
  1363. {
  1364. rt_hw_usart_register(&usart1_device, RT_USART1_NAME, flag, usart);
  1365. }
  1366. else
  1367. {
  1368. break;
  1369. }
  1370. /* Initialize lock for usart1 */
  1371. usart->lock = &usart1_lock;
  1372. if (rt_sem_init(usart->lock, RT_USART1_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
  1373. {
  1374. break;
  1375. }
  1376. #endif
  1377. #if (defined(USART_PRESENT) && (USART_COUNT > 2) && defined(RT_USING_USART2))
  1378. config = 0x00;
  1379. flag = RT_DEVICE_FLAG_RDWR;
  1380. #ifdef RT_USART2_SYNC_MODE
  1381. config |= USART_STATE_SYNC;
  1382. config |= (RT_USART2_SYNC_MODE & SYNC_SETTING_MASK) << SYNC_SETTING_SHIFT;
  1383. #if (!((RT_USART2_SYNC_MODE << SYNC_SETTING_SHIFT) & USART_STATE_MASTER))
  1384. flag |= RT_DEVICE_FLAG_INT_RX;
  1385. #endif
  1386. #else
  1387. flag |= RT_DEVICE_FLAG_INT_RX;
  1388. #endif
  1389. #if (RT_CONSOLE_DEVICE == EFM_USART2)
  1390. config |= USART_STATE_CONSOLE;
  1391. flag |= RT_DEVICE_FLAG_STREAM;
  1392. #endif
  1393. #ifdef RT_USART2_USING_DMA
  1394. RT_ASSERT(RT_USART2_USING_DMA < DMA_CHAN_COUNT);
  1395. flag |= RT_DEVICE_FLAG_DMA_TX;
  1396. #else
  1397. #define RT_USART2_USING_DMA EFM32_NO_DMA
  1398. #endif
  1399. /* Initialize and Register usart2 */
  1400. if ((usart = rt_hw_usart_unit_init(
  1401. &usart2_device,
  1402. 2,
  1403. RT_USING_USART2,
  1404. flag,
  1405. RT_USART2_USING_DMA,
  1406. config)) != RT_NULL)
  1407. {
  1408. rt_hw_usart_register(&usart2_device, RT_USART2_NAME, flag, usart);
  1409. }
  1410. else
  1411. {
  1412. break;
  1413. }
  1414. /* Initialize lock for usart2 */
  1415. usart->lock = &usart2_lock;
  1416. if (rt_sem_init(usart->lock, RT_USART2_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
  1417. {
  1418. break;
  1419. }
  1420. #endif
  1421. #if (defined(UART_PRESENT) && defined(RT_USING_UART0))
  1422. config = USART_STATE_ASYNC_ONLY;
  1423. flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
  1424. #if (RT_CONSOLE_DEVICE == EFM_UART0)
  1425. config |= USART_STATE_CONSOLE;
  1426. flag |= RT_DEVICE_FLAG_STREAM;
  1427. #endif
  1428. #ifdef RT_UART0_USING_DMA
  1429. RT_ASSERT(RT_UART0_USING_DMA < DMA_CHAN_COUNT);
  1430. flag |= RT_DEVICE_FLAG_DMA_TX;
  1431. #else
  1432. #define RT_UART0_USING_DMA EFM32_NO_DMA
  1433. #endif
  1434. /* Initialize and Register uart0 */
  1435. if ((usart = rt_hw_usart_unit_init(
  1436. &uart0_device,
  1437. 0,
  1438. RT_USING_UART0,
  1439. flag,
  1440. RT_UART0_USING_DMA,
  1441. config)) != RT_NULL)
  1442. {
  1443. rt_hw_usart_register(&uart0_device, RT_UART0_NAME, flag, usart);
  1444. }
  1445. else
  1446. {
  1447. break;
  1448. }
  1449. /* Initialize lock for uart0 */
  1450. usart->lock = &uart0_lock;
  1451. if (rt_sem_init(usart->lock, RT_UART0_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
  1452. {
  1453. break;
  1454. }
  1455. #endif
  1456. #if (defined(UART_PRESENT) && (UART_COUNT > 1) && defined(RT_USING_UART1))
  1457. config = USART_STATE_ASYNC_ONLY;
  1458. flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
  1459. #if (RT_CONSOLE_DEVICE == EFM_UART1)
  1460. config |= USART_STATE_CONSOLE;
  1461. flag |= RT_DEVICE_FLAG_STREAM;
  1462. #endif
  1463. #ifdef RT_UART1_USING_DMA
  1464. RT_ASSERT(RT_UART1_USING_DMA < DMA_CHAN_COUNT);
  1465. flag |= RT_DEVICE_FLAG_DMA_TX;
  1466. #else
  1467. #define RT_UART1_USING_DMA EFM32_NO_DMA
  1468. #endif
  1469. /* Initialize and Register uart1 */
  1470. if ((usart = rt_hw_usart_unit_init(
  1471. &uart1_device,
  1472. 1,
  1473. RT_USING_UART1,
  1474. flag,
  1475. RT_UART1_USING_DMA,
  1476. config)) != RT_NULL)
  1477. {
  1478. rt_hw_usart_register(&uart1_device, RT_UART1_NAME, flag, usart);
  1479. }
  1480. else
  1481. {
  1482. break;
  1483. }
  1484. /* Initialize lock for uart1 */
  1485. usart->lock = &uart1_lock;
  1486. if (rt_sem_init(usart->lock, RT_UART1_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
  1487. {
  1488. break;
  1489. }
  1490. #endif
  1491. usart_debug("USART: H/W init OK!\n");
  1492. return;
  1493. } while (0);
  1494. rt_kprintf("USART: H/W init failed!\n");
  1495. }
  1496. #endif /* (defined(RT_USING_USART0) || defined(RT_USING_USART1) || \
  1497. defined(RT_USING_USART2) || defined(RT_USING_UART0) || \
  1498. defined(RT_USING_UART1)) */
  1499. /***************************************************************************//**
  1500. * @}
  1501. ******************************************************************************/