drv_usart.c 37 KB

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