drv_usart.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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
  13. * Date Author Notes
  14. * 2010-12-22 onelife Initial creation for EFM32
  15. * 2011-01-17 onelife Merge with serial.c
  16. * 2011-05-06 onelife Add sync mode (SPI) support
  17. * 2011-06-14 onelife Fix a bug of TX by DMA
  18. * 2011-06-16 onelife Modify init function for EFM32 library v2.0.0
  19. * upgrading
  20. * 2011-07-07 onelife Modify write function to avoid sleep in ISR
  21. * 2011-07-26 onelife Add lock (semaphore) to prevent simultaneously
  22. * access
  23. * 2011-11-29 onelife Modify init function for EFM32 library v2.2.2
  24. * upgrading
  25. *
  26. * @section Change Logs of serial.c
  27. * 2009-02-05 Bernard first version
  28. * 2009-10-25 Bernard fix rt_serial_read bug when there is no data in the
  29. * buffer.
  30. * 2010-03-29 Bernard cleanup code.
  31. ******************************************************************************/
  32. /***************************************************************************//**
  33. * @addtogroup efm32
  34. * @{
  35. ******************************************************************************/
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "board.h"
  38. #include "hdl_interrupt.h"
  39. #include "drv_usart.h"
  40. #if (defined(RT_USING_USART0) || defined(RT_USING_USART1) || defined(RT_USING_USART2))
  41. /* Private typedef -----------------------------------------------------------*/
  42. union efm32_usart_init_t
  43. {
  44. USART_InitAsync_TypeDef async;
  45. USART_InitSync_TypeDef sync;
  46. };
  47. /* Private define ------------------------------------------------------------*/
  48. /* Private macro -------------------------------------------------------------*/
  49. #ifdef RT_USART_DEBUG
  50. #define usart_debug(format,args...) rt_kprintf(format, ##args)
  51. #else
  52. #define usart_debug(format,args...)
  53. #endif
  54. /* Private variables ---------------------------------------------------------*/
  55. #ifdef RT_USING_USART0
  56. #if (RT_USING_USART0 > 3)
  57. #error "The location number range of usart is 0~3"
  58. #endif
  59. struct rt_device usart0_device;
  60. static struct rt_semaphore usart0_lock;
  61. #endif
  62. #ifdef RT_USING_USART1
  63. #if (RT_USING_USART1 > 3)
  64. #error "The location number range of usart is 0~3"
  65. #endif
  66. struct rt_device usart1_device;
  67. static struct rt_semaphore usart1_lock;
  68. #endif
  69. #ifdef RT_USING_USART2
  70. #if (RT_USING_USART2 > 3)
  71. #error "The location number range of usart is 0~3"
  72. #endif
  73. struct rt_device usart2_device;
  74. static struct rt_semaphore usart2_lock;
  75. #endif
  76. /* Private function prototypes -----------------------------------------------*/
  77. /* Private functions ---------------------------------------------------------*/
  78. /***************************************************************************//**
  79. * @brief
  80. * Initialize USART device
  81. *
  82. * @details
  83. *
  84. * @note
  85. *
  86. * @param[in] dev
  87. * Pointer to device descriptor
  88. *
  89. * @return
  90. * Error code
  91. ******************************************************************************/
  92. static rt_err_t rt_usart_init (rt_device_t dev)
  93. {
  94. struct efm32_usart_device_t *usart;
  95. usart = (struct efm32_usart_device_t *)(dev->user_data);
  96. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  97. {
  98. if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  99. {
  100. struct efm32_usart_dma_mode_t *dma_tx;
  101. dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
  102. usart->state |= USART_STATE_RX_BUSY;
  103. }
  104. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  105. {
  106. struct efm32_usart_int_mode_t *int_rx;
  107. int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  108. int_rx->data_ptr = RT_NULL;
  109. }
  110. /* Enable USART */
  111. USART_Enable(usart->usart_device, usartEnable);
  112. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  113. }
  114. return RT_EOK;
  115. }
  116. /***************************************************************************//**
  117. * @brief
  118. * Open USART device
  119. *
  120. * @details
  121. *
  122. * @note
  123. *
  124. * @param[in] dev
  125. * Pointer to device descriptor
  126. *
  127. * @param[in] oflag
  128. * Device open flag
  129. *
  130. * @return
  131. * Error code
  132. ******************************************************************************/
  133. static rt_err_t rt_usart_open(rt_device_t dev, rt_uint16_t oflag)
  134. {
  135. RT_ASSERT(dev != RT_NULL);
  136. struct efm32_usart_device_t *usart;
  137. usart = (struct efm32_usart_device_t *)(dev->user_data);
  138. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  139. {
  140. IRQn_Type rxIrq;
  141. //if (usart->state & USART_STATE_CONSOLE)
  142. { /* Allocate new RX buffer */
  143. struct efm32_usart_int_mode_t *int_mode;
  144. int_mode = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  145. if ((int_mode->data_ptr = rt_malloc(USART_RX_BUFFER_SIZE)) == RT_NULL)
  146. {
  147. usart_debug("USART: no memory for RX buffer\n");
  148. return -RT_ENOMEM;
  149. }
  150. rt_memset(int_mode->data_ptr, 0, USART_RX_BUFFER_SIZE);
  151. int_mode->data_size = USART_RX_BUFFER_SIZE;
  152. int_mode->read_index = 0;
  153. int_mode->save_index = 0;
  154. }
  155. /* Enable TX/RX interrupt */
  156. switch (usart->unit)
  157. {
  158. case 0:
  159. rxIrq = USART0_RX_IRQn;
  160. break;
  161. case 1:
  162. rxIrq = USART1_RX_IRQn;
  163. break;
  164. case 2:
  165. rxIrq = USART2_RX_IRQn;
  166. break;
  167. }
  168. /* Enable RX interrupts */
  169. usart->usart_device->IEN = USART_IEN_RXDATAV;
  170. /* Enable IRQ */
  171. if (oflag != RT_DEVICE_OFLAG_WRONLY)
  172. {
  173. NVIC_ClearPendingIRQ(rxIrq);
  174. NVIC_SetPriority(rxIrq, EFM32_IRQ_PRI_DEFAULT);
  175. NVIC_EnableIRQ(rxIrq);
  176. }
  177. }
  178. usart->usart_device->IFC = _USART_IFC_MASK;
  179. if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (oflag != RT_DEVICE_OFLAG_RDONLY))
  180. {
  181. /* DMA IRQ is enabled by DMA_Init() */
  182. NVIC_SetPriority(DMA_IRQn, EFM32_IRQ_PRI_DEFAULT);
  183. }
  184. usart->counter++;
  185. usart_debug("USART%d: Open with flag %x\n", usart->unit, oflag);
  186. return RT_EOK;
  187. }
  188. /***************************************************************************//**
  189. * @brief
  190. * Close USART device
  191. *
  192. * @details
  193. *
  194. * @note
  195. *
  196. * @param[in] dev
  197. * Pointer to device descriptor
  198. *
  199. * @return
  200. * Error code
  201. ******************************************************************************/
  202. static rt_err_t rt_usart_close(rt_device_t dev)
  203. {
  204. RT_ASSERT(dev != RT_NULL);
  205. struct efm32_usart_device_t *usart;
  206. usart = (struct efm32_usart_device_t *)(dev->user_data);
  207. if (--usart->counter == 0)
  208. {
  209. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  210. {
  211. struct efm32_usart_int_mode_t *int_rx;
  212. int_rx = (struct efm32_usart_int_mode_t *)usart->rx_mode;
  213. rt_free(int_rx->data_ptr);
  214. int_rx->data_ptr = RT_NULL;
  215. }
  216. }
  217. return RT_EOK;
  218. }
  219. /***************************************************************************//**
  220. * @brief
  221. * Read from USART device
  222. *
  223. * @details
  224. *
  225. * @note
  226. *
  227. * @param[in] dev
  228. * Pointer to device descriptor
  229. *
  230. * @param[in] pos
  231. * Offset
  232. *
  233. * @param[in] buffer
  234. * Poniter to the buffer
  235. *
  236. * @param[in] size
  237. * Buffer size in byte
  238. *
  239. * @return
  240. * Number of read bytes
  241. ******************************************************************************/
  242. static rt_size_t rt_usart_read (
  243. rt_device_t dev,
  244. rt_off_t pos,
  245. void *buffer,
  246. rt_size_t size)
  247. {
  248. struct efm32_usart_device_t *usart;
  249. rt_uint8_t *ptr;
  250. rt_err_t err_code;
  251. rt_size_t read_len;
  252. usart = (struct efm32_usart_device_t *)(dev->user_data);
  253. /* Lock device */
  254. if (rt_hw_interrupt_check())
  255. {
  256. err_code = rt_sem_take(usart->lock, RT_WAITING_NO);
  257. }
  258. else
  259. {
  260. err_code = rt_sem_take(usart->lock, RT_WAITING_FOREVER);
  261. }
  262. if (err_code != RT_EOK)
  263. {
  264. rt_set_errno(err_code);
  265. return 0;
  266. }
  267. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  268. {
  269. ptr = buffer;
  270. /* interrupt mode Rx */
  271. while (size)
  272. {
  273. rt_base_t level;
  274. struct efm32_usart_int_mode_t *int_rx;
  275. int_rx = (struct efm32_usart_int_mode_t *)\
  276. (((struct efm32_usart_device_t *)(dev->user_data))->rx_mode);
  277. /* disable interrupt */
  278. level = rt_hw_interrupt_disable();
  279. if (int_rx->read_index != int_rx->save_index)
  280. {
  281. /* read a character */
  282. *ptr++ = int_rx->data_ptr[int_rx->read_index];
  283. size--;
  284. /* move to next position */
  285. int_rx->read_index ++;
  286. if (int_rx->read_index >= USART_RX_BUFFER_SIZE)
  287. {
  288. int_rx->read_index = 0;
  289. }
  290. }
  291. else
  292. {
  293. /* set error code */
  294. err_code = -RT_EEMPTY;
  295. /* enable interrupt */
  296. rt_hw_interrupt_enable(level);
  297. break;
  298. }
  299. /* enable interrupt */
  300. rt_hw_interrupt_enable(level);
  301. }
  302. read_len = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  303. }
  304. else
  305. {
  306. struct efm32_usart_device_t *usart;
  307. USART_TypeDef *usart_device;
  308. usart = (struct efm32_usart_device_t *)(dev->user_data);
  309. usart_device = ((struct efm32_usart_device_t *)(dev->user_data))->usart_device;
  310. if (usart->state & USART_STATE_SYNC)
  311. {
  312. /* SPI read */
  313. rt_uint8_t inst_len = *((rt_uint8_t *)buffer);
  314. rt_uint8_t *inst_ptr = (rt_uint8_t *)(buffer + 1);
  315. rt_uint8_t *rx_buf = *((rt_uint8_t **)(buffer + inst_len + 1));
  316. rt_off_t i;
  317. ptr = rx_buf;
  318. /* write instruction */
  319. while (inst_len)
  320. {
  321. while (!(usart->usart_device->STATUS & USART_STATUS_TXBL));
  322. usart->usart_device->TXDATA = (rt_uint32_t)*inst_ptr;
  323. ++inst_ptr; --inst_len;
  324. }
  325. /* Flushing RX */
  326. usart_device->CMD = USART_CMD_CLEARRX;
  327. /* Skip some bytes if necessary */
  328. for (i = 0; i < pos; i++)
  329. {
  330. /* dummy write */
  331. while (!(usart_device->STATUS & USART_STATUS_TXBL));
  332. usart_device->TXDATA = (rt_uint32_t)0xff;
  333. /* dummy read */
  334. while (!(usart_device->STATUS & USART_STATUS_RXDATAV));
  335. *((rt_uint32_t *)0x00) = usart_device->RXDATA;
  336. }
  337. while ((rt_uint32_t)ptr - (rt_uint32_t)rx_buf < size)
  338. {
  339. /* dummy write */
  340. while (!(usart_device->STATUS & USART_STATUS_TXBL));
  341. usart_device->TXDATA = (rt_uint32_t)0xff;
  342. /* read a byte of data */
  343. while (!(usart_device->STATUS & USART_STATUS_RXDATAV));
  344. *ptr = usart_device->RXDATA & 0xff;
  345. ptr ++;
  346. }
  347. }
  348. else
  349. {
  350. ptr = buffer;
  351. /* polling mode */
  352. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
  353. {
  354. while (usart_device->STATUS & USART_STATUS_RXDATAV)
  355. {
  356. *ptr = usart_device->RXDATA & 0xff;
  357. ptr ++;
  358. }
  359. }
  360. }
  361. read_len = size;
  362. }
  363. /* Unlock device */
  364. rt_sem_release(usart->lock);
  365. /* set error code */
  366. rt_set_errno(err_code);
  367. return read_len;
  368. }
  369. /***************************************************************************//**
  370. * @brief
  371. * Write to USART device
  372. *
  373. * @details
  374. *
  375. * @note
  376. *
  377. * @param[in] dev
  378. * Pointer to device descriptor
  379. *
  380. * @param[in] pos
  381. * Offset
  382. *
  383. * @param[in] buffer
  384. * Poniter to the buffer
  385. *
  386. * @param[in] size
  387. * Buffer size in byte
  388. *
  389. * @return
  390. * Number of written bytes
  391. ******************************************************************************/
  392. static rt_size_t rt_usart_write (
  393. rt_device_t dev,
  394. rt_off_t pos,
  395. const void* buffer,
  396. rt_size_t size)
  397. {
  398. rt_err_t err_code;
  399. rt_size_t write_size;
  400. struct efm32_usart_device_t* usart;
  401. write_size = 0;
  402. usart = (struct efm32_usart_device_t*)(dev->user_data);
  403. /* Lock device */
  404. if (rt_hw_interrupt_check())
  405. {
  406. err_code = rt_sem_take(usart->lock, RT_WAITING_NO);
  407. }
  408. else
  409. {
  410. err_code = rt_sem_take(usart->lock, RT_WAITING_FOREVER);
  411. }
  412. if (err_code != RT_EOK)
  413. {
  414. rt_set_errno(err_code);
  415. return 0;
  416. }
  417. if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (size > 2))
  418. { /* DMA mode Tx */
  419. struct efm32_usart_dma_mode_t *dma_tx;
  420. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  421. {
  422. if (*((rt_uint8_t *)buffer + size - 1) == '\n')
  423. {
  424. *((rt_uint8_t *)buffer + size - 1) = '\r';
  425. *((rt_uint8_t *)buffer + size++) = '\n';
  426. *((rt_uint8_t *)buffer + size) = 0;
  427. }
  428. }
  429. dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
  430. dma_tx->data_ptr = (rt_uint32_t *)buffer;
  431. dma_tx->data_size = size;
  432. usart->state |= USART_STATE_TX_BUSY;
  433. DMA_ActivateBasic(
  434. dma_tx->dma_channel,
  435. true,
  436. false,
  437. (void *)&(usart->usart_device->TXDATA),
  438. (void *)buffer,
  439. (rt_uint32_t)(size - 1));
  440. /* Wait, otherwise the TX buffer is overwrite */
  441. // if (usart->state & USART_STATE_CONSOLE)
  442. // {
  443. while(usart->state & USART_STATE_TX_BUSY);
  444. // }
  445. // else
  446. // {
  447. // while(usart->state & USART_STATE_TX_BUSY)
  448. // {
  449. // rt_thread_sleep(USART_WAIT_TIME_TX);
  450. // }
  451. // }
  452. // TODO: This function blocks the process
  453. write_size = size;
  454. }
  455. else
  456. { /* polling mode */
  457. rt_uint8_t *ptr = (rt_uint8_t *)buffer;
  458. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  459. {
  460. /* stream mode */
  461. while (size)
  462. {
  463. if (*ptr == '\n')
  464. {
  465. while (!(usart->usart_device->STATUS & USART_STATUS_TXBL));
  466. usart->usart_device->TXDATA = '\r';
  467. }
  468. while (!(usart->usart_device->STATUS & USART_STATUS_TXBL));
  469. usart->usart_device->TXDATA = (rt_uint32_t)*ptr;
  470. ++ptr; --size;
  471. }
  472. }
  473. else
  474. {
  475. /* write data directly */
  476. while (size)
  477. {
  478. while (!(usart->usart_device->STATUS & USART_STATUS_TXBL));
  479. usart->usart_device->TXDATA = (rt_uint32_t)*ptr;
  480. ++ptr; --size;
  481. }
  482. }
  483. write_size = (rt_size_t)ptr - (rt_size_t)buffer;
  484. }
  485. /* Unlock device */
  486. rt_sem_release(usart->lock);
  487. /* set error code */
  488. rt_set_errno(err_code);
  489. return write_size;
  490. }
  491. /***************************************************************************//**
  492. * @brief
  493. * Configure USART device
  494. *
  495. * @details
  496. *
  497. * @note
  498. *
  499. * @param[in] dev
  500. * Pointer to device descriptor
  501. *
  502. * @param[in] cmd
  503. * IIC control command
  504. *
  505. * @param[in] args
  506. * Arguments
  507. *
  508. * @return
  509. * Error code
  510. ******************************************************************************/
  511. static rt_err_t rt_usart_control (
  512. rt_device_t dev,
  513. rt_uint8_t cmd,
  514. void *args)
  515. {
  516. RT_ASSERT(dev != RT_NULL);
  517. rt_err_t err_code;
  518. struct efm32_usart_device_t *usart;
  519. usart = (struct efm32_usart_device_t *)(dev->user_data);
  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. return err_code;
  532. }
  533. switch (cmd)
  534. {
  535. case RT_DEVICE_CTRL_SUSPEND:
  536. /* Suspend device */
  537. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  538. USART_Enable(usart->usart_device, usartDisable);
  539. break;
  540. case RT_DEVICE_CTRL_RESUME:
  541. /* Resume device */
  542. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  543. USART_Enable(usart->usart_device, usartEnable);
  544. break;
  545. case RT_DEVICE_CTRL_USART_RBUFFER:
  546. /* Set RX buffer */
  547. {
  548. struct efm32_usart_int_mode_t *int_rx;
  549. rt_uint8_t size;
  550. int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  551. size = (rt_uint8_t)((rt_uint32_t)args & 0xFFUL);
  552. /* Free previous RX buffer */
  553. if (int_rx->data_ptr != RT_NULL)
  554. {
  555. if (size == 0)
  556. { /* Free RX buffer */
  557. rt_free(int_rx->data_ptr);
  558. int_rx->data_ptr = RT_NULL;
  559. }
  560. else if (size != int_rx->data_size)
  561. {
  562. /* Re-allocate RX buffer */
  563. if ((int_rx->data_ptr = rt_realloc(int_rx->data_ptr, size)) \
  564. == RT_NULL)
  565. {
  566. usart_debug("USART: no memory for RX buffer\n");
  567. err_code = -RT_ENOMEM;
  568. break;
  569. }
  570. // TODO: Is the following line necessary?
  571. //rt_memset(int_rx->data_ptr, 0, size);
  572. }
  573. }
  574. else
  575. {
  576. /* Allocate new RX buffer */
  577. if ((int_rx->data_ptr = rt_malloc(size)) == RT_NULL)
  578. {
  579. usart_debug("USART: no memory for RX buffer\n");
  580. err_code = -RT_ENOMEM;
  581. break;
  582. }
  583. }
  584. int_rx->data_size = size;
  585. int_rx->read_index = 0;
  586. int_rx->save_index = 0;
  587. }
  588. break;
  589. }
  590. /* Unlock device */
  591. rt_sem_release(usart->lock);
  592. return err_code;
  593. }
  594. /***************************************************************************//**
  595. * @brief
  596. * USART RX data valid interrupt handler
  597. *
  598. * @details
  599. *
  600. * @note
  601. *
  602. * @param[in] dev
  603. * Pointer to device descriptor
  604. ******************************************************************************/
  605. void rt_hw_usart_rx_isr(rt_device_t dev)
  606. {
  607. struct efm32_usart_device_t *usart;
  608. struct efm32_usart_int_mode_t *int_rx;
  609. /* interrupt mode receive */
  610. RT_ASSERT(dev->flag & RT_DEVICE_FLAG_INT_RX);
  611. usart = (struct efm32_usart_device_t *)(dev->user_data);
  612. int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  613. RT_ASSERT(int_rx->data_ptr != RT_NULL);
  614. /* Set status */
  615. usart->state |= USART_STATE_RX_BUSY;
  616. /* save on rx buffer */
  617. while (usart->usart_device->STATUS & USART_STATUS_RXDATAV)
  618. {
  619. rt_base_t level;
  620. /* disable interrupt */
  621. level = rt_hw_interrupt_disable();
  622. /* save character */
  623. int_rx->data_ptr[int_rx->save_index] = \
  624. (rt_uint8_t)(usart->usart_device->RXDATA & 0xFFUL);
  625. int_rx->save_index ++;
  626. if (int_rx->save_index >= USART_RX_BUFFER_SIZE)
  627. int_rx->save_index = 0;
  628. /* if the next position is read index, discard this 'read char' */
  629. if (int_rx->save_index == int_rx->read_index)
  630. {
  631. int_rx->read_index ++;
  632. if (int_rx->read_index >= USART_RX_BUFFER_SIZE)
  633. {
  634. int_rx->read_index = 0;
  635. }
  636. }
  637. /* enable interrupt */
  638. rt_hw_interrupt_enable(level);
  639. }
  640. /* invoke callback */
  641. if (dev->rx_indicate != RT_NULL)
  642. {
  643. rt_size_t rx_length;
  644. /* get rx length */
  645. rx_length = int_rx->read_index > int_rx->save_index ?
  646. USART_RX_BUFFER_SIZE - int_rx->read_index + int_rx->save_index : \
  647. int_rx->save_index - int_rx->read_index;
  648. dev->rx_indicate(dev, rx_length);
  649. }
  650. }
  651. /***************************************************************************//**
  652. * @brief
  653. * DMA for USART TX interrupt handler
  654. *
  655. * @details
  656. *
  657. * @note
  658. *
  659. * @param[in] dev
  660. * Pointer to device descriptor
  661. ******************************************************************************/
  662. void rt_hw_usart_dma_tx_isr(rt_device_t dev)
  663. {
  664. /* DMA mode receive */
  665. struct efm32_usart_device_t *usart;
  666. struct efm32_usart_dma_mode_t *dma_tx;
  667. RT_ASSERT(dev->flag & RT_DEVICE_FLAG_DMA_TX);
  668. usart = (struct efm32_usart_device_t *)(dev->user_data);
  669. dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
  670. /* invoke call to notify tx complete */
  671. if (dev->tx_complete != RT_NULL)
  672. {
  673. dev->tx_complete(dev, dma_tx->data_ptr);
  674. }
  675. /* Set status */
  676. usart->state &= ~(rt_uint32_t)USART_STATE_TX_BUSY;
  677. }
  678. /***************************************************************************//**
  679. * @brief
  680. * Register USART device
  681. *
  682. * @details
  683. *
  684. * @note
  685. *
  686. * @param[in] device
  687. * Pointer to device descriptor
  688. *
  689. * @param[in] name
  690. * Device name
  691. *
  692. * @param[in] flag
  693. * Configuration flags
  694. *
  695. * @param[in] usart
  696. * Pointer to USART device descriptor
  697. *
  698. * @return
  699. * Error code
  700. ******************************************************************************/
  701. rt_err_t rt_hw_usart_register(
  702. rt_device_t device,
  703. const char *name,
  704. rt_uint32_t flag,
  705. struct efm32_usart_device_t *usart)
  706. {
  707. RT_ASSERT(device != RT_NULL);
  708. if ((flag & RT_DEVICE_FLAG_DMA_RX) ||
  709. (flag & RT_DEVICE_FLAG_INT_TX))
  710. {
  711. RT_ASSERT(0);
  712. }
  713. device->type = RT_Device_Class_Char;
  714. device->rx_indicate = RT_NULL;
  715. device->tx_complete = RT_NULL;
  716. device->init = rt_usart_init;
  717. device->open = rt_usart_open;
  718. device->close = rt_usart_close;
  719. device->read = rt_usart_read;
  720. device->write = rt_usart_write;
  721. device->control = rt_usart_control;
  722. device->user_data = usart;
  723. /* register a character device */
  724. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  725. }
  726. /***************************************************************************//**
  727. * @brief
  728. * Initialize the specified USART unit
  729. *
  730. * @details
  731. *
  732. * @note
  733. *
  734. * @param[in] device
  735. * Pointer to device descriptor
  736. *
  737. * @param[in] unitNumber
  738. * Unit number
  739. *
  740. * @param[in] location
  741. * Pin location number
  742. *
  743. * @param[in] flag
  744. * Configuration flag
  745. *
  746. * @param[in] dmaChannel
  747. * DMA channel number for TX
  748. *
  749. * @param[in] console
  750. * Indicate if using as console
  751. *
  752. * @return
  753. * Pointer to USART device
  754. ******************************************************************************/
  755. static struct efm32_usart_device_t *rt_hw_usart_unit_init(
  756. rt_device_t device,
  757. rt_uint8_t unitNumber,
  758. rt_uint8_t location,
  759. rt_uint32_t flag,
  760. rt_uint32_t dmaChannel,
  761. rt_uint8_t config)
  762. {
  763. struct efm32_usart_device_t *usart;
  764. struct efm32_usart_dma_mode_t *dma_mode;
  765. DMA_CB_TypeDef *callback;
  766. CMU_Clock_TypeDef usartClock;
  767. rt_uint32_t txDmaSelect;
  768. GPIO_Port_TypeDef port_tx, port_rx, port_clk, port_cs;
  769. rt_uint32_t pin_tx, pin_rx, pin_clk, pin_cs;
  770. union efm32_usart_init_t init;
  771. efm32_irq_hook_init_t hook;
  772. do
  773. {
  774. /* Allocate device */
  775. usart = rt_malloc(sizeof(struct efm32_usart_device_t));
  776. if (usart == RT_NULL)
  777. {
  778. usart_debug("USART: no memory for USART%d device\n", unitNumber);
  779. break;
  780. }
  781. usart->counter = 0;
  782. usart->unit = unitNumber;
  783. usart->state = config;
  784. usart->tx_mode = RT_NULL;
  785. usart->rx_mode = RT_NULL;
  786. /* Allocate TX */
  787. dma_mode = RT_NULL;
  788. if (flag & RT_DEVICE_FLAG_DMA_TX)
  789. {
  790. usart->tx_mode = dma_mode = rt_malloc(sizeof(struct efm32_usart_dma_mode_t));
  791. if (dma_mode == RT_NULL)
  792. {
  793. usart_debug("USART: no memory for DMA TX\n");
  794. break;
  795. }
  796. dma_mode->dma_channel = dmaChannel;
  797. }
  798. /* Allocate RX */
  799. if (flag & RT_DEVICE_FLAG_INT_RX)
  800. {
  801. usart->rx_mode = rt_malloc(sizeof(struct efm32_usart_int_mode_t));
  802. if (usart->rx_mode == RT_NULL)
  803. {
  804. usart_debug("USART: no memory for interrupt RX\n");
  805. break;
  806. }
  807. }
  808. /* Initialization */
  809. if (unitNumber >= USART_COUNT)
  810. {
  811. break;
  812. }
  813. switch (unitNumber)
  814. {
  815. case 0:
  816. usart->usart_device = USART0;
  817. usartClock = (CMU_Clock_TypeDef)cmuClock_USART0;
  818. txDmaSelect = DMAREQ_USART0_TXBL;
  819. port_tx = AF_USART0_TX_PORT(location);
  820. pin_tx = AF_USART0_TX_PIN(location);
  821. port_rx = AF_USART0_RX_PORT(location);
  822. pin_rx = AF_USART0_RX_PIN(location);
  823. port_clk = AF_USART0_CLK_PORT(location);
  824. pin_clk = AF_USART0_CLK_PIN(location);
  825. port_cs = AF_USART0_CS_PORT(location);
  826. pin_cs = AF_USART0_CS_PIN(location);
  827. break;
  828. case 1:
  829. usart->usart_device = USART1;
  830. usartClock = (CMU_Clock_TypeDef)cmuClock_USART1;
  831. txDmaSelect = DMAREQ_USART1_TXBL;
  832. port_tx = AF_USART1_TX_PORT(location);
  833. pin_tx = AF_USART1_TX_PIN(location);
  834. port_rx = AF_USART1_RX_PORT(location);
  835. pin_rx = AF_USART1_RX_PIN(location);
  836. port_clk = AF_USART1_CLK_PORT(location);
  837. pin_clk = AF_USART1_CLK_PIN(location);
  838. port_cs = AF_USART1_CS_PORT(location);
  839. pin_cs = AF_USART1_CS_PIN(location);
  840. break;
  841. case 2:
  842. usart->usart_device = USART2;
  843. usartClock = (CMU_Clock_TypeDef)cmuClock_USART2;
  844. txDmaSelect = DMAREQ_USART2_TXBL;
  845. port_tx = AF_USART2_TX_PORT(location);
  846. pin_tx = AF_USART2_TX_PIN(location);
  847. port_rx = AF_USART2_RX_PORT(location);
  848. pin_rx = AF_USART2_RX_PIN(location);
  849. port_clk = AF_USART2_CLK_PORT(location);
  850. pin_clk = AF_USART2_CLK_PIN(location);
  851. port_cs = AF_USART2_CS_PORT(location);
  852. pin_cs = AF_USART2_CS_PIN(location);
  853. break;
  854. default:
  855. break;
  856. }
  857. /* Enable USART clock */
  858. CMU_ClockEnable(usartClock, true);
  859. /* Config GPIO */
  860. GPIO_PinModeSet(
  861. port_tx,
  862. pin_tx,
  863. gpioModePushPull,
  864. 0);
  865. GPIO_PinModeSet(
  866. port_rx,
  867. pin_rx,
  868. gpioModeInputPull,
  869. 1);
  870. if (config & USART_STATE_SYNC)
  871. {
  872. GPIO_PinModeSet(
  873. port_clk,
  874. pin_clk,
  875. gpioModePushPull,
  876. 0);
  877. }
  878. if (config & USART_STATE_AUTOCS)
  879. {
  880. GPIO_PinModeSet(
  881. port_cs,
  882. pin_cs,
  883. gpioModePushPull,
  884. 1);
  885. }
  886. /* Config interrupt and NVIC */
  887. if (flag & RT_DEVICE_FLAG_INT_RX)
  888. {
  889. hook.type = efm32_irq_type_usart;
  890. hook.unit = unitNumber * 2 + 1;
  891. hook.cbFunc = rt_hw_usart_rx_isr;
  892. hook.userPtr = device;
  893. efm32_irq_hook_register(&hook);
  894. }
  895. /* Config DMA */
  896. if (flag & RT_DEVICE_FLAG_DMA_TX)
  897. {
  898. DMA_CfgChannel_TypeDef chnlCfg;
  899. DMA_CfgDescr_TypeDef descrCfg;
  900. hook.type = efm32_irq_type_dma;
  901. hook.unit = dmaChannel;
  902. hook.cbFunc = rt_hw_usart_dma_tx_isr;
  903. hook.userPtr = device;
  904. efm32_irq_hook_register(&hook);
  905. callback = (DMA_CB_TypeDef *)rt_malloc(sizeof(DMA_CB_TypeDef));
  906. if (callback == RT_NULL)
  907. {
  908. usart_debug("USART: no memory for callback\n");
  909. break;
  910. }
  911. callback->cbFunc = DMA_IRQHandler_All;
  912. callback->userPtr = RT_NULL;
  913. callback->primary = 0;
  914. /* Setting up DMA channel */
  915. chnlCfg.highPri = false; /* Can't use with peripherals */
  916. chnlCfg.enableInt = true; /* Interrupt for callback function */
  917. chnlCfg.select = txDmaSelect;
  918. chnlCfg.cb = callback;
  919. DMA_CfgChannel(dmaChannel, &chnlCfg);
  920. /* Setting up DMA channel descriptor */
  921. descrCfg.dstInc = dmaDataIncNone;
  922. descrCfg.srcInc = dmaDataInc1;
  923. descrCfg.size = dmaDataSize1;
  924. descrCfg.arbRate = dmaArbitrate1;
  925. descrCfg.hprot = 0;
  926. DMA_CfgDescr(dmaChannel, true, &descrCfg);
  927. }
  928. /* Init specified USART unit */
  929. if (config & USART_STATE_SYNC)
  930. {
  931. init.sync.enable = usartEnable;
  932. init.sync.refFreq = 0;
  933. init.sync.baudrate = SPI_BAUDRATE;
  934. init.sync.databits = usartDatabits8;
  935. if (config & USART_STATE_MASTER)
  936. {
  937. init.sync.master = true;
  938. }
  939. else
  940. {
  941. init.sync.master = false;
  942. }
  943. init.sync.msbf = true;
  944. init.sync.clockMode = usartClockMode0; /* Clock idle low, sample on rising edge. */
  945. USART_InitSync(usart->usart_device, &init.sync);
  946. }
  947. else
  948. {
  949. init.async.enable = usartEnable;
  950. init.async.refFreq = 0;
  951. init.async.baudrate = UART_BAUDRATE;
  952. init.async.oversampling = USART_CTRL_OVS_X4;
  953. init.async.databits = USART_FRAME_DATABITS_EIGHT;
  954. init.async.parity = USART_FRAME_PARITY_NONE;
  955. init.async.stopbits = USART_FRAME_STOPBITS_ONE;
  956. USART_InitAsync(usart->usart_device, &init.async);
  957. }
  958. /* Enable RX and TX pins and set location */
  959. usart->usart_device->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | \
  960. (location << _USART_ROUTE_LOCATION_SHIFT);
  961. if (config & USART_STATE_SYNC)
  962. {
  963. usart->usart_device->ROUTE |= USART_ROUTE_CLKPEN;
  964. }
  965. if (config & USART_STATE_AUTOCS)
  966. {
  967. usart->usart_device->ROUTE |= USART_ROUTE_CSPEN;
  968. if (config & USART_STATE_MASTER)
  969. {
  970. usart->usart_device->CTRL |= USART_CTRL_AUTOCS;
  971. }
  972. }
  973. /* Clear RX/TX buffers */
  974. usart->usart_device->CMD = USART_CMD_CLEARRX | USART_CMD_CLEARTX;
  975. return usart;
  976. } while(0);
  977. if (usart->rx_mode)
  978. {
  979. rt_free(usart->rx_mode);
  980. }
  981. if (usart->tx_mode)
  982. {
  983. rt_free(usart->tx_mode);
  984. }
  985. if (usart)
  986. {
  987. rt_free(usart);
  988. }
  989. if (callback)
  990. {
  991. rt_free(usart);
  992. }
  993. usart_debug("USART: Unit %d init failed!\n", unitNumber);
  994. return RT_NULL;
  995. }
  996. /***************************************************************************//**
  997. * @brief
  998. * Initialize all USART module related hardware and register USART device to
  999. * kernel
  1000. *
  1001. * @details
  1002. *
  1003. * @note
  1004. ******************************************************************************/
  1005. void rt_hw_usart_init(void)
  1006. {
  1007. struct efm32_usart_device_t *usart;
  1008. rt_uint32_t flag;
  1009. rt_uint8_t config;
  1010. do
  1011. {
  1012. #ifdef RT_USING_USART0
  1013. config = 0x00;
  1014. flag = RT_DEVICE_FLAG_RDWR;
  1015. #ifdef RT_USART0_SYNC_MODE
  1016. config |= USART_STATE_SYNC;
  1017. #if (RT_USART0_SYNC_MODE != 0x0UL)
  1018. config |= USART_STATE_MASTER;
  1019. #else
  1020. flag |= RT_DEVICE_FLAG_INT_RX;
  1021. #endif
  1022. #else
  1023. flag |= RT_DEVICE_FLAG_INT_RX;
  1024. #endif
  1025. #if (SPI_AUTOCS_ENABLE & (1 << 0))
  1026. config |= USART_STATE_AUTOCS;
  1027. #endif
  1028. #if (RT_CONSOLE_DEVICE == 0x0UL)
  1029. config |= USART_STATE_CONSOLE;
  1030. flag |= RT_DEVICE_FLAG_STREAM;
  1031. #endif
  1032. #ifdef RT_USART0_USING_DMA
  1033. RT_ASSERT(RT_USART0_USING_DMA < DMA_CHAN_COUNT);
  1034. flag |= RT_DEVICE_FLAG_DMA_TX;
  1035. #else
  1036. #define RT_USART0_USING_DMA EFM32_NO_DATA
  1037. #endif
  1038. /* Initialize and Register usart0 */
  1039. if ((usart = rt_hw_usart_unit_init(
  1040. &usart0_device,
  1041. 0,
  1042. RT_USING_USART0,
  1043. flag,
  1044. RT_USART0_USING_DMA,
  1045. config)) != RT_NULL)
  1046. {
  1047. rt_hw_usart_register(&usart0_device, RT_USART0_NAME, flag, usart);
  1048. }
  1049. else
  1050. {
  1051. break;
  1052. }
  1053. /* Initialize lock for usart0 */
  1054. usart->lock = &usart0_lock;
  1055. if (rt_sem_init(usart->lock, RT_USART0_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
  1056. {
  1057. break;
  1058. }
  1059. #endif
  1060. #ifdef RT_USING_USART1
  1061. config = 0x00;
  1062. flag = RT_DEVICE_FLAG_RDWR;
  1063. #ifdef RT_USART1_SYNC_MODE
  1064. config |= USART_STATE_SYNC;
  1065. #if (RT_USART1_SYNC_MODE != 0x0UL)
  1066. config |= USART_STATE_MASTER;
  1067. #else
  1068. flag |= RT_DEVICE_FLAG_INT_RX;
  1069. #endif
  1070. #else
  1071. flag |= RT_DEVICE_FLAG_INT_RX;
  1072. #endif
  1073. #if (SPI_AUTOCS_ENABLE & (1 << 1))
  1074. config |= USART_STATE_AUTOCS;
  1075. #endif
  1076. #if (RT_CONSOLE_DEVICE == 0x1UL)
  1077. config |= USART_STATE_CONSOLE;
  1078. flag |= RT_DEVICE_FLAG_STREAM;
  1079. #endif
  1080. #ifdef RT_USART1_USING_DMA
  1081. RT_ASSERT(RT_USART1_USING_DMA < DMA_CHAN_COUNT);
  1082. flag |= RT_DEVICE_FLAG_DMA_TX;
  1083. #else
  1084. #define RT_USART1_USING_DMA EFM32_NO_DATA
  1085. #endif
  1086. /* Initialize and Register usart1 */
  1087. if ((usart = rt_hw_usart_unit_init(
  1088. &usart1_device,
  1089. 1,
  1090. RT_USING_USART1,
  1091. flag,
  1092. RT_USART1_USING_DMA,
  1093. config)) != RT_NULL)
  1094. {
  1095. rt_hw_usart_register(&usart1_device, RT_USART1_NAME, flag, usart);
  1096. }
  1097. else
  1098. {
  1099. break;
  1100. }
  1101. /* Initialize lock for usart1 */
  1102. usart->lock = &usart1_lock;
  1103. if (rt_sem_init(usart->lock, RT_USART1_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
  1104. {
  1105. break;
  1106. }
  1107. #endif
  1108. #ifdef RT_USING_USART2
  1109. config = 0x00;
  1110. flag = RT_DEVICE_FLAG_RDWR;
  1111. #ifdef RT_USART2_SYNC_MODE
  1112. config |= USART_STATE_SYNC;
  1113. #if (RT_USART2_SYNC_MODE != 0x0UL)
  1114. config |= USART_STATE_MASTER;
  1115. #else
  1116. flag |= RT_DEVICE_FLAG_INT_RX;
  1117. #endif
  1118. #else
  1119. flag |= RT_DEVICE_FLAG_INT_RX;
  1120. #endif
  1121. #if (SPI_AUTOCS_ENABLE & (1 << 2))
  1122. config |= USART_STATE_AUTOCS;
  1123. #endif
  1124. #if (RT_CONSOLE_DEVICE == 0x2UL)
  1125. config |= USART_STATE_CONSOLE;
  1126. flag |= RT_DEVICE_FLAG_STREAM;
  1127. #endif
  1128. #ifdef RT_USART2_USING_DMA
  1129. RT_ASSERT(RT_USART2_USING_DMA < DMA_CHAN_COUNT);
  1130. flag |= RT_DEVICE_FLAG_DMA_TX;
  1131. #else
  1132. #define RT_USART2_USING_DMA EFM32_NO_DATA
  1133. #endif
  1134. /* Initialize and Register usart2 */
  1135. if ((usart = rt_hw_usart_unit_init(
  1136. &usart2_device,
  1137. 2,
  1138. RT_USING_USART2,
  1139. flag,
  1140. RT_USART2_USING_DMA,
  1141. config)) != RT_NULL)
  1142. {
  1143. rt_hw_usart_register(&usart2_device, RT_USART2_NAME, flag, usart);
  1144. }
  1145. else
  1146. {
  1147. break;
  1148. }
  1149. /* Initialize lock for usart2 */
  1150. usart->lock = &usart2_lock;
  1151. if (rt_sem_init(usart->lock, RT_USART2_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
  1152. {
  1153. break;
  1154. }
  1155. #endif
  1156. usart_debug("USART: H/W init OK!\n");
  1157. return;
  1158. } while (0);
  1159. rt_kprintf("USART: H/W init failed!\n");
  1160. }
  1161. #endif /* (defined(RT_USING_USART0) || defined(RT_USING_USART1) || defined(RT_USING_USART2)) */
  1162. /***************************************************************************//**
  1163. * @}
  1164. ******************************************************************************/