drv_usart.c 49 KB

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