drv_usart.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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 LICENSE in this
  10. * 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. *
  17. * @section Change Logs of serial.c
  18. * 2009-02-05 Bernard first version
  19. * 2009-10-25 Bernard fix rt_serial_read bug when there is no data in the buffer.
  20. * 2010-03-29 Bernard cleanup code.
  21. *********************************************************************/
  22. /******************************************************************//**
  23. * @addtogroup efm32
  24. * @{
  25. *********************************************************************/
  26. /* Includes -------------------------------------------------------------------*/
  27. #include "board.h"
  28. #include "hdl_interrupt.h"
  29. #include "drv_usart.h"
  30. /* Private typedef -------------------------------------------------------------*/
  31. /* Private define --------------------------------------------------------------*/
  32. /* Private macro --------------------------------------------------------------*/
  33. /* Private variables ------------------------------------------------------------*/
  34. #ifdef RT_USING_USART0
  35. #if (RT_USING_USART0 > 3)
  36. #error "The location number range of usart is 0~3"
  37. #endif
  38. struct rt_device usart0_device;
  39. #endif
  40. #ifdef RT_USING_USART1
  41. #if (RT_USING_USART1 > 3)
  42. #error "The location number range of usart is 0~3"
  43. #endif
  44. struct rt_device usart1_device;
  45. #endif
  46. #ifdef RT_USING_USART2
  47. #if (RT_USING_USART2 > 3)
  48. #error "The location number range of usart is 0~3"
  49. #endif
  50. struct rt_device usart2_device;
  51. #endif
  52. /* Private function prototypes ---------------------------------------------------*/
  53. /* Private functions ------------------------------------------------------------*/
  54. /******************************************************************//**
  55. * @brief
  56. * Initialize USART device
  57. *
  58. * @details
  59. *
  60. * @note
  61. *
  62. * @param[in] dev
  63. * Pointer to device descriptor
  64. *
  65. * @return
  66. * Error code
  67. *********************************************************************/
  68. static rt_err_t rt_usart_init (rt_device_t dev)
  69. {
  70. struct efm32_usart_device_t *usart;
  71. usart = (struct efm32_usart_device_t *)(dev->user_data);
  72. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  73. {
  74. if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  75. {
  76. struct efm32_usart_dma_mode_t *dma_tx;
  77. dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
  78. usart->state |= USART_STATE_RX_BUSY;
  79. }
  80. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  81. {
  82. struct efm32_usart_int_mode_t *int_rx;
  83. int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  84. int_rx->data_ptr = RT_NULL;
  85. }
  86. /* Enable USART */
  87. USART_Enable(usart->usart_device, usartEnable);
  88. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  89. }
  90. return RT_EOK;
  91. }
  92. /******************************************************************//**
  93. * @brief
  94. * Open USART device
  95. *
  96. * @details
  97. *
  98. * @note
  99. *
  100. * @param[in] dev
  101. * Pointer to device descriptor
  102. *
  103. * @param[in] oflag
  104. * Device open flag
  105. *
  106. * @return
  107. * Error code
  108. *********************************************************************/
  109. static rt_err_t rt_usart_open(rt_device_t dev, rt_uint16_t oflag)
  110. {
  111. RT_ASSERT(dev != RT_NULL);
  112. struct efm32_usart_device_t *usart;
  113. IRQn_Type rxIrq;
  114. usart = (struct efm32_usart_device_t *)(dev->user_data);
  115. //if (usart->state & USART_STATE_CONSOLE)
  116. { /* Allocate new RX buffer */
  117. struct efm32_usart_int_mode_t *int_mode;
  118. int_mode = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  119. if ((int_mode->data_ptr = rt_malloc(SERIAL_RX_BUFFER_SIZE)) == RT_NULL)
  120. {
  121. rt_kprintf("no memory for serial RX buffer\n");
  122. return -RT_ENOMEM;
  123. }
  124. rt_memset(int_mode->data_ptr, 0, SERIAL_RX_BUFFER_SIZE);
  125. int_mode->data_size = SERIAL_RX_BUFFER_SIZE;
  126. int_mode->read_index = 0;
  127. int_mode->save_index = 0;
  128. }
  129. /* Enable TX/RX interrupt */
  130. switch (usart->unit)
  131. {
  132. case 0:
  133. rxIrq = USART0_RX_IRQn;
  134. break;
  135. case 1:
  136. rxIrq = USART1_RX_IRQn;
  137. break;
  138. case 2:
  139. rxIrq = USART2_RX_IRQn;
  140. break;
  141. }
  142. /* Enable RX interrupts */
  143. usart->usart_device->IEN = USART_IEN_RXDATAV;
  144. usart->usart_device->IFC = _USART_IFC_MASK;
  145. /* Enable IRQ */
  146. if (oflag != RT_DEVICE_OFLAG_WRONLY)
  147. {
  148. NVIC_ClearPendingIRQ(rxIrq);
  149. NVIC_SetPriority(rxIrq, EFM32_IRQ_PRI_DEFAULT);
  150. NVIC_EnableIRQ(rxIrq);
  151. }
  152. if (oflag != RT_DEVICE_OFLAG_RDONLY)
  153. {
  154. /* DMA IRQ is enabled by DMA_Init() */
  155. NVIC_SetPriority(DMA_IRQn, EFM32_IRQ_PRI_DEFAULT);
  156. }
  157. #ifdef RT_USART_DEBUG
  158. rt_kprintf("USART%d: Open with flag %x\n", usart->unit, oflag);
  159. #endif
  160. return RT_EOK;
  161. }
  162. /******************************************************************//**
  163. * @brief
  164. * Close USART device
  165. *
  166. * @details
  167. *
  168. * @note
  169. *
  170. * @param[in] dev
  171. * Pointer to device descriptor
  172. *
  173. * @return
  174. * Error code
  175. *********************************************************************/
  176. static rt_err_t rt_usart_close(rt_device_t dev)
  177. {
  178. struct efm32_usart_int_mode_t *int_rx;
  179. int_rx = (struct efm32_usart_int_mode_t *)\
  180. (((struct efm32_usart_device_t *)(dev->user_data))->rx_mode);
  181. rt_free(int_rx->data_ptr);
  182. int_rx->data_ptr = RT_NULL;
  183. return RT_EOK;
  184. }
  185. /******************************************************************//**
  186. * @brief
  187. * Read from USART device
  188. *
  189. * @details
  190. *
  191. * @note
  192. *
  193. * @param[in] dev
  194. * Pointer to device descriptor
  195. *
  196. * @param[in] pos
  197. * Offset
  198. *
  199. * @param[in] buffer
  200. * Poniter to the buffer
  201. *
  202. * @param[in] size
  203. * Buffer size in byte
  204. *
  205. * @return
  206. * Error code
  207. *********************************************************************/
  208. static rt_size_t rt_usart_read (
  209. rt_device_t dev,
  210. rt_off_t pos,
  211. void* buffer,
  212. rt_size_t size)
  213. {
  214. rt_uint8_t* ptr;
  215. rt_err_t err_code;
  216. ptr = buffer;
  217. err_code = RT_EOK;
  218. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  219. {
  220. /* interrupt mode Rx */
  221. while (size)
  222. {
  223. rt_base_t level;
  224. struct efm32_usart_int_mode_t *int_rx;
  225. int_rx = (struct efm32_usart_int_mode_t *)\
  226. (((struct efm32_usart_device_t *)(dev->user_data))->rx_mode);
  227. /* disable interrupt */
  228. level = rt_hw_interrupt_disable();
  229. if (int_rx->read_index != int_rx->save_index)
  230. {
  231. /* read a character */
  232. *ptr++ = int_rx->data_ptr[int_rx->read_index];
  233. size--;
  234. /* move to next position */
  235. int_rx->read_index ++;
  236. if (int_rx->read_index >= SERIAL_RX_BUFFER_SIZE)
  237. {
  238. int_rx->read_index = 0;
  239. }
  240. }
  241. else
  242. {
  243. /* set error code */
  244. err_code = -RT_EEMPTY;
  245. /* enable interrupt */
  246. rt_hw_interrupt_enable(level);
  247. break;
  248. }
  249. /* enable interrupt */
  250. rt_hw_interrupt_enable(level);
  251. }
  252. }
  253. else
  254. {
  255. /* polling mode */
  256. USART_TypeDef *usart_device;
  257. RT_ASSERT(buffer != RT_NULL);
  258. usart_device = ((struct efm32_usart_device_t *)(dev->user_data))->usart_device;
  259. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
  260. {
  261. while (usart_device->STATUS & USART_STATUS_RXDATAV)
  262. {
  263. *ptr = usart_device->RXDATA & 0xff;
  264. ptr ++;
  265. }
  266. }
  267. }
  268. /* set error code */
  269. rt_set_errno(err_code);
  270. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  271. }
  272. /******************************************************************//**
  273. * @brief
  274. * Write to USART device
  275. *
  276. * @details
  277. *
  278. * @note
  279. *
  280. * @param[in] dev
  281. * Pointer to device descriptor
  282. *
  283. * @param[in] pos
  284. * Offset
  285. *
  286. * @param[in] buffer
  287. * Poniter to the buffer
  288. *
  289. * @param[in] size
  290. * Buffer size in byte
  291. *
  292. * @return
  293. * Error code
  294. *********************************************************************/
  295. static rt_size_t rt_usart_write (
  296. rt_device_t dev,
  297. rt_off_t pos,
  298. const void* buffer,
  299. rt_size_t size)
  300. {
  301. rt_err_t err_code;
  302. rt_size_t write_size;
  303. struct efm32_usart_device_t* usart;
  304. err_code = RT_EOK;
  305. write_size = 0;
  306. usart = (struct efm32_usart_device_t*)(dev->user_data);
  307. if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  308. { /* DMA mode Tx */
  309. struct efm32_usart_dma_mode_t *dma_tx;
  310. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  311. {
  312. *((rt_uint8_t *)buffer + size++) = '\r';
  313. *((rt_uint8_t *)buffer + size) = 0x0;
  314. }
  315. dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
  316. dma_tx->data_ptr = (rt_uint32_t *)buffer;
  317. dma_tx->data_size = size;
  318. usart->state |= USART_STATE_TX_BUSY;
  319. DMA_ActivateBasic(
  320. dma_tx->dma_channel,
  321. true,
  322. false,
  323. (void *)&(usart->usart_device->TXDATA),
  324. buffer,
  325. size - 1);
  326. /* Wait, otherwise the TX buffer is overwrite */
  327. if (usart->state & USART_STATE_CONSOLE)
  328. {
  329. while(usart->state & USART_STATE_TX_BUSY);
  330. }
  331. else
  332. {
  333. while(usart->state & USART_STATE_TX_BUSY)
  334. {
  335. rt_thread_sleep(USART_WAIT_TIME_TX);
  336. }
  337. }
  338. write_size = size;
  339. }
  340. else
  341. { /* polling mode */
  342. rt_uint8_t* ptr = (rt_uint8_t*)buffer;
  343. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  344. {
  345. /* stream mode */
  346. while (size)
  347. {
  348. if (*ptr == '\n')
  349. {
  350. while (!(usart->usart_device->STATUS & USART_STATUS_TXBL));
  351. usart->usart_device->TXDATA = '\r';
  352. }
  353. while (!(usart->usart_device->STATUS & USART_STATUS_TXBL));
  354. usart->usart_device->TXDATA = (rt_uint32_t)*ptr;
  355. ++ptr; --size;
  356. }
  357. }
  358. else
  359. {
  360. /* write data directly */
  361. while (size)
  362. {
  363. while (!(usart->usart_device->STATUS & USART_STATUS_TXBL));
  364. usart->usart_device->TXDATA = (rt_uint32_t)*ptr;
  365. ++ptr; --size;
  366. }
  367. }
  368. write_size = (rt_size_t)ptr - (rt_size_t)buffer;
  369. }
  370. /* set error code */
  371. rt_set_errno(err_code);
  372. return write_size;
  373. }
  374. /******************************************************************//**
  375. * @brief
  376. * Configure USART device
  377. *
  378. * @details
  379. *
  380. * @note
  381. *
  382. * @param[in] dev
  383. * Pointer to device descriptor
  384. *
  385. * @param[in] cmd
  386. * IIC control command
  387. *
  388. * @param[in] args
  389. * Arguments
  390. *
  391. * @return
  392. * Error code
  393. *********************************************************************/
  394. static rt_err_t rt_usart_control (
  395. rt_device_t dev,
  396. rt_uint8_t cmd,
  397. void *args)
  398. {
  399. RT_ASSERT(dev != RT_NULL);
  400. struct efm32_usart_device_t *usart;
  401. usart = (struct efm32_usart_device_t *)(dev->user_data);
  402. switch (cmd)
  403. {
  404. case RT_DEVICE_CTRL_SUSPEND:
  405. /* Suspend device */
  406. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  407. USART_Enable(usart->usart_device, usartDisable);
  408. break;
  409. case RT_DEVICE_CTRL_RESUME:
  410. /* Resume device */
  411. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  412. USART_Enable(usart->usart_device, usartEnable);
  413. break;
  414. case RT_DEVICE_CTRL_USART_RBUFFER:
  415. /* Set RX buffer */
  416. {
  417. struct efm32_usart_int_mode_t *int_rx;
  418. rt_uint8_t size;
  419. int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  420. size = (rt_uint8_t)((rt_uint32_t)args & 0xFFUL);
  421. /* Free previous RX buffer */
  422. if (int_rx->data_ptr != RT_NULL)
  423. {
  424. if (size == 0)
  425. { /* Free RX buffer */
  426. rt_free(int_rx->data_ptr);
  427. int_rx->data_ptr = RT_NULL;
  428. }
  429. else if (size != int_rx->data_size)
  430. {
  431. /* Re-allocate RX buffer */
  432. if ((int_rx->data_ptr = rt_realloc(int_rx->data_ptr, size)) \
  433. == RT_NULL)
  434. {
  435. rt_kprintf("no memory for usart rx buffer\n");
  436. return -RT_ENOMEM;
  437. }
  438. //rt_memset(int_rx->data_ptr, 0, size); //TODO
  439. }
  440. }
  441. else
  442. {
  443. /* Allocate new RX buffer */
  444. if ((int_rx->data_ptr = rt_malloc(size)) == RT_NULL)
  445. {
  446. rt_kprintf("no memory for usart rx buffer\n");
  447. return -RT_ENOMEM;
  448. }
  449. }
  450. int_rx->data_size = size;
  451. int_rx->read_index = 0;
  452. int_rx->save_index = 0;
  453. }
  454. break;
  455. }
  456. return RT_EOK;
  457. }
  458. /******************************************************************//**
  459. * @brief
  460. * USART RX data valid interrupt handler
  461. *
  462. * @details
  463. *
  464. * @note
  465. *
  466. * @param[in] dev
  467. * Pointer to device descriptor
  468. *********************************************************************/
  469. void rt_hw_usart_rx_isr(rt_device_t dev)
  470. {
  471. struct efm32_usart_device_t *usart;
  472. struct efm32_usart_int_mode_t *int_rx;
  473. /* interrupt mode receive */
  474. RT_ASSERT(dev->flag & RT_DEVICE_FLAG_INT_RX);
  475. usart = (struct efm32_usart_device_t *)(dev->user_data);
  476. int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
  477. RT_ASSERT(int_rx->data_ptr != RT_NULL);
  478. /* Set status */
  479. usart->state |= USART_STATE_RX_BUSY;
  480. /* save on rx buffer */
  481. while (usart->usart_device->STATUS & USART_STATUS_RXDATAV)
  482. {
  483. rt_base_t level;
  484. /* disable interrupt */
  485. level = rt_hw_interrupt_disable();
  486. /* save character */
  487. int_rx->data_ptr[int_rx->save_index] = \
  488. (rt_uint8_t)(usart->usart_device->RXDATA & 0xFFUL);
  489. int_rx->save_index ++;
  490. if (int_rx->save_index >= SERIAL_RX_BUFFER_SIZE)
  491. int_rx->save_index = 0;
  492. /* if the next position is read index, discard this 'read char' */
  493. if (int_rx->save_index == int_rx->read_index)
  494. {
  495. int_rx->read_index ++;
  496. if (int_rx->read_index >= SERIAL_RX_BUFFER_SIZE)
  497. {
  498. int_rx->read_index = 0;
  499. }
  500. }
  501. /* enable interrupt */
  502. rt_hw_interrupt_enable(level);
  503. }
  504. /* invoke callback */
  505. if (dev->rx_indicate != RT_NULL)
  506. {
  507. rt_size_t rx_length;
  508. /* get rx length */
  509. rx_length = int_rx->read_index > int_rx->save_index ?
  510. SERIAL_RX_BUFFER_SIZE - int_rx->read_index + int_rx->save_index : \
  511. int_rx->save_index - int_rx->read_index;
  512. dev->rx_indicate(dev, rx_length);
  513. }
  514. }
  515. /******************************************************************//**
  516. * @brief
  517. * DMA for USART TX interrupt handler
  518. *
  519. * @details
  520. *
  521. * @note
  522. *
  523. * @param[in] dev
  524. * Pointer to device descriptor
  525. *********************************************************************/
  526. void rt_hw_usart_dma_tx_isr(rt_device_t dev)
  527. {
  528. /* DMA mode receive */
  529. struct efm32_usart_device_t *usart;
  530. struct efm32_usart_dma_mode_t *dma_tx;
  531. RT_ASSERT(dev->flag & RT_DEVICE_FLAG_DMA_TX);
  532. usart = (struct efm32_usart_device_t *)(dev->user_data);
  533. dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
  534. /* invoke call to notify tx complete */
  535. if (dev->tx_complete != RT_NULL)
  536. {
  537. dev->tx_complete(dev, dma_tx->data_ptr);
  538. }
  539. /* Set status */
  540. usart->state &= ~(rt_uint32_t)USART_STATE_TX_BUSY;
  541. }
  542. /******************************************************************//**
  543. * @brief
  544. * Register USART device
  545. *
  546. * @details
  547. *
  548. * @note
  549. *
  550. * @param[in] device
  551. * Pointer to device descriptor
  552. *
  553. * @param[in] name
  554. * Device name
  555. *
  556. * @param[in] flag
  557. * Configuration flags
  558. *
  559. * @param[in] usart
  560. * Pointer to USART device descriptor
  561. *
  562. * @return
  563. * Error code
  564. *********************************************************************/
  565. rt_err_t rt_hw_usart_register(
  566. rt_device_t device,
  567. const char *name,
  568. rt_uint32_t flag,
  569. struct efm32_usart_device_t *usart)
  570. {
  571. RT_ASSERT(device != RT_NULL);
  572. if ((flag & RT_DEVICE_FLAG_DMA_RX) ||
  573. (flag & RT_DEVICE_FLAG_INT_TX))
  574. {
  575. RT_ASSERT(0);
  576. }
  577. device->type = RT_Device_Class_Char;
  578. device->rx_indicate = RT_NULL;
  579. device->tx_complete = RT_NULL;
  580. device->init = rt_usart_init;
  581. device->open = rt_usart_open;
  582. device->close = rt_usart_close;
  583. device->read = rt_usart_read;
  584. device->write = rt_usart_write;
  585. device->control = rt_usart_control;
  586. device->user_data = usart;
  587. /* register a character device */
  588. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  589. }
  590. /******************************************************************//**
  591. * @brief
  592. * Initialize the specified USART unit
  593. *
  594. * @details
  595. *
  596. * @note
  597. *
  598. * @param[in] device
  599. * Pointer to device descriptor
  600. *
  601. * @param[in] unitNumber
  602. * Unit number
  603. *
  604. * @param[in] location
  605. * Pin location number
  606. *
  607. * @param[in] flag
  608. * Configuration flag
  609. *
  610. * @param[in] dmaChannel
  611. * DMA channel number for TX
  612. *
  613. * @param[in] console
  614. * Indicate if using as console
  615. *
  616. * @return
  617. * Pointer to USART device
  618. *********************************************************************/
  619. static struct efm32_usart_device_t *rt_hw_usart_unit_init(
  620. rt_device_t device,
  621. rt_uint8_t unitNumber,
  622. rt_uint8_t location,
  623. rt_uint32_t flag,
  624. rt_uint32_t dmaChannel,
  625. rt_bool_t console)
  626. {
  627. struct efm32_usart_device_t *usart;
  628. struct efm32_usart_dma_mode_t *dma_mode;
  629. CMU_Clock_TypeDef usartClock;
  630. rt_uint32_t txDmaSelect;
  631. USART_InitAsync_TypeDef init;
  632. efm32_irq_hook_init_t hook;
  633. /* Allocate device */
  634. usart = rt_malloc(sizeof(struct efm32_usart_device_t));
  635. if (usart == RT_NULL)
  636. {
  637. rt_kprintf("no memory for USART driver\n");
  638. return usart;
  639. }
  640. usart->unit = unitNumber;
  641. if (console == true)
  642. {
  643. usart->state = USART_STATE_CONSOLE;
  644. }
  645. else
  646. {
  647. usart->state = 0;
  648. }
  649. usart->tx_mode = RT_NULL;
  650. usart->rx_mode = RT_NULL;
  651. /* Allocate TX */
  652. dma_mode = RT_NULL;
  653. if (flag & RT_DEVICE_FLAG_DMA_TX)
  654. {
  655. usart->tx_mode = dma_mode = rt_malloc(sizeof(struct efm32_usart_dma_mode_t));
  656. if (dma_mode == RT_NULL)
  657. {
  658. rt_kprintf("no memory for USART TX by DMA\n");
  659. rt_free(usart->rx_mode);
  660. rt_free(usart);
  661. usart = RT_NULL;
  662. return usart;
  663. }
  664. dma_mode->dma_channel = dmaChannel;
  665. }
  666. /* Allocate RX */
  667. if (flag & RT_DEVICE_FLAG_INT_RX)
  668. {
  669. usart->rx_mode = rt_malloc(sizeof(struct efm32_usart_int_mode_t));
  670. if (usart->rx_mode == RT_NULL)
  671. {
  672. rt_kprintf("no memory for USART RX by interrupt\n");
  673. rt_free(usart->tx_mode);
  674. rt_free(usart);
  675. usart = RT_NULL;
  676. return usart;
  677. }
  678. }
  679. /* Initialization */
  680. switch (unitNumber)
  681. {
  682. case 0:
  683. usart->usart_device = USART0;
  684. usartClock = (CMU_Clock_TypeDef)cmuClock_USART0;
  685. txDmaSelect = DMAREQ_USART0_TXBL;
  686. break;
  687. case 1:
  688. usart->usart_device = USART1;
  689. usartClock = (CMU_Clock_TypeDef)cmuClock_USART1;
  690. txDmaSelect = DMAREQ_USART1_TXBL;
  691. break;
  692. case 2:
  693. usart->usart_device = USART2;
  694. usartClock = (CMU_Clock_TypeDef)cmuClock_USART2;
  695. txDmaSelect = DMAREQ_USART2_TXBL;
  696. break;
  697. }
  698. /* Enable USART clock */
  699. CMU_ClockEnable(usartClock, true);
  700. /* Reset */
  701. USART_Reset(usart->usart_device);
  702. /* Config GPIO */
  703. GPIO_PinModeSet(
  704. (GPIO_Port_TypeDef)AF_PORT(AF_USART_TX(unitNumber), location),
  705. AF_PIN(AF_USART_TX(unitNumber), location),
  706. gpioModePushPull,
  707. 0);
  708. GPIO_PinModeSet(
  709. (GPIO_Port_TypeDef)AF_PORT(AF_USART_RX(unitNumber), location),
  710. AF_PIN(AF_USART_RX(unitNumber), location),
  711. gpioModeInputPull,
  712. 1);
  713. /* Config interrupt and NVIC */
  714. if (flag & RT_DEVICE_FLAG_INT_RX)
  715. {
  716. hook.type = efm32_irq_type_usart;
  717. hook.unit = unitNumber * 2 + 1;
  718. hook.cbFunc = rt_hw_usart_rx_isr;
  719. hook.userPtr = device;
  720. efm32_irq_hook_register(&hook);
  721. }
  722. /* Config DMA */
  723. if (flag & RT_DEVICE_FLAG_DMA_TX)
  724. {
  725. DMA_CB_TypeDef *callback;
  726. DMA_CfgChannel_TypeDef chnlCfg;
  727. DMA_CfgDescr_TypeDef descrCfg;
  728. hook.type = efm32_irq_type_dma;
  729. hook.unit = dmaChannel;
  730. hook.cbFunc = rt_hw_usart_dma_tx_isr;
  731. hook.userPtr = device;
  732. efm32_irq_hook_register(&hook);
  733. callback = (DMA_CB_TypeDef *)rt_malloc(sizeof(DMA_CB_TypeDef));
  734. callback->cbFunc = DMA_IRQHandler_All;
  735. callback->userPtr = RT_NULL;
  736. callback->primary = 0;
  737. /* Setting up DMA channel */
  738. chnlCfg.highPri = false; /* Can't use with peripherals */
  739. chnlCfg.enableInt = true; /* Interrupt for callback function */
  740. chnlCfg.select = txDmaSelect;
  741. chnlCfg.cb = callback;
  742. DMA_CfgChannel(dmaChannel, &chnlCfg);
  743. /* Setting up DMA channel descriptor */
  744. descrCfg.dstInc = dmaDataIncNone;
  745. descrCfg.srcInc = dmaDataInc1;
  746. descrCfg.size = dmaDataSize1;
  747. descrCfg.arbRate = dmaArbitrate1;
  748. descrCfg.hprot = 0;
  749. DMA_CfgDescr(dmaChannel, true, &descrCfg);
  750. }
  751. /* Enable RX and TX pins and set location */
  752. usart->usart_device->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | \
  753. (location << _USART_ROUTE_LOCATION_SHIFT);
  754. /* Init specified USART unit */
  755. init.enable = usartEnable;
  756. init.refFreq = 0;
  757. init.baudrate = UART_BAUDRATE;
  758. init.oversampling = USART_CTRL_OVS_X4;
  759. init.databits = USART_FRAME_DATABITS_EIGHT;
  760. init.parity = USART_FRAME_PARITY_NONE;
  761. init.stopbits = USART_FRAME_STOPBITS_ONE;
  762. USART_InitAsync(usart->usart_device, &init);
  763. /* Clear RX/TX buffers */
  764. usart->usart_device->CMD = USART_CMD_CLEARRX | USART_CMD_CLEARTX;
  765. return usart;
  766. }
  767. /******************************************************************//**
  768. * @brief
  769. * Initialize all USART module related hardware and register USART device to kernel
  770. *
  771. * @details
  772. *
  773. * @note
  774. *********************************************************************/
  775. void rt_hw_usart_init(void)
  776. {
  777. struct efm32_usart_device_t *usart;
  778. rt_uint32_t flag;
  779. rt_bool_t console;
  780. /* Register usart0 */
  781. #ifdef RT_USING_USART0
  782. flag = RT_DEVICE_FLAG_RDWR;
  783. #if (RT_CONSOLE_DEVICE == 0x0UL)
  784. console = true;
  785. flag |= RT_DEVICE_FLAG_STREAM;
  786. #else
  787. console = false;
  788. #endif
  789. #ifdef RT_USART0_USING_DMA
  790. RT_ASSERT(RT_USART0_USING_DMA < DMA_CHAN_COUNT);
  791. flag |= RT_DEVICE_FLAG_DMA_TX;
  792. #else
  793. #define RT_USART0_USING_DMA EFM32_NO_DATA
  794. #endif
  795. flag |= RT_DEVICE_FLAG_INT_RX;
  796. usart = rt_hw_usart_unit_init(
  797. &usart0_device,
  798. 0,
  799. RT_USING_USART0,
  800. flag,
  801. RT_USART0_USING_DMA,
  802. console);
  803. rt_hw_usart_register(&usart0_device, RT_USART0_NAME, flag, usart);
  804. #endif
  805. /* Register usart1 */
  806. #ifdef RT_USING_USART1
  807. flag = RT_DEVICE_FLAG_RDWR;
  808. #if (RT_CONSOLE_DEVICE == 0x1UL)
  809. console = true;
  810. flag |= RT_DEVICE_FLAG_STREAM;
  811. #else
  812. console = false;
  813. #endif
  814. #ifdef RT_USART1_USING_DMA
  815. RT_ASSERT(RT_USART1_USING_DMA < DMA_CHAN_COUNT);
  816. flag |= RT_DEVICE_FLAG_DMA_TX;
  817. #else
  818. #define RT_USART1_USING_DMA EFM32_NO_DATA
  819. #endif
  820. flag |= RT_DEVICE_FLAG_INT_RX;
  821. usart = rt_hw_usart_unit_init(
  822. &usart1_device,
  823. 1,
  824. RT_USING_USART1,
  825. flag,
  826. RT_USART1_USING_DMA,
  827. console);
  828. rt_hw_usart_register(&usart1_device, RT_USART1_NAME, flag, usart);
  829. #endif
  830. /* Register usart2 */
  831. #ifdef RT_USING_USART2
  832. flag = RT_DEVICE_FLAG_RDWR;
  833. #if (RT_CONSOLE_DEVICE == 0x2UL)
  834. console = true;
  835. flag |= RT_DEVICE_FLAG_STREAM;
  836. #else
  837. console = false;
  838. #endif
  839. #ifdef RT_USART2_USING_DMA
  840. RT_ASSERT(RT_USART2_USING_DMA < DMA_CHAN_COUNT);
  841. flag |= RT_DEVICE_FLAG_DMA_TX;
  842. #else
  843. #define RT_USART2_USING_DMA EFM32_NO_DATA
  844. #endif
  845. flag |= RT_DEVICE_FLAG_INT_RX;
  846. usart = rt_hw_usart_unit_init(
  847. &usart2_device,
  848. 2,
  849. RT_USING_USART2,
  850. flag,
  851. RT_USART2_USING_DMA,
  852. console);
  853. rt_hw_usart_register(&usart2_device, RT_USART2_NAME, flag, usart);
  854. #endif
  855. }
  856. /******************************************************************//**
  857. * @}
  858. *********************************************************************/