123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524 |
- /***************************************************************************//**
- * @file drv_usart.c
- * @brief USART driver of RT-Thread RTOS for EFM32
- * COPYRIGHT (C) 2011, RT-Thread Development Team
- * @author onelife
- * @version 0.4 beta
- *******************************************************************************
- * @section License
- * The license and distribution terms for this file may be found in the file
- * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
- *******************************************************************************
- * @section Change Logs of serial.c
- * Date Author Notes
- * 2009-02-05 Bernard first version
- * 2009-10-25 Bernard fix rt_serial_read bug when there is no data in the
- * buffer.
- * 2010-03-29 Bernard cleanup code.
- *
- * @section Change Logs
- * Date Author Notes
- * 2010-12-22 onelife Initial creation for EFM32
- * 2011-01-17 onelife Merge with serial.c
- * 2011-05-06 onelife Add sync mode (SPI) support
- * 2011-06-14 onelife Fix a bug of TX by DMA
- * 2011-06-16 onelife Modify init function for EFM32 library v2.0.0
- * upgrading
- * 2011-07-07 onelife Modify write function to avoid sleep in ISR
- * 2011-07-26 onelife Add lock (semaphore) to prevent simultaneously
- * access
- * 2011-11-29 onelife Modify init function for EFM32 library v2.2.2
- * upgrading
- * 2011-12-09 onelife Add giant gecko support
- * 2011-12-09 onelife Add UART module support
- ******************************************************************************/
- /***************************************************************************//**
- * @addtogroup efm32
- * @{
- ******************************************************************************/
- /* Includes ------------------------------------------------------------------*/
- #include "board.h"
- #include "hdl_interrupt.h"
- #include "drv_usart.h"
- #if (defined(RT_USING_USART0) || defined(RT_USING_USART1) || \
- defined(RT_USING_USART2) || defined(RT_USING_UART0) || \
- defined(RT_USING_UART1))
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- #ifdef RT_USART_DEBUG
- #define usart_debug(format,args...) rt_kprintf(format, ##args)
- #else
- #define usart_debug(format,args...)
- #endif
- /* Private variables ---------------------------------------------------------*/
- #ifdef RT_USING_USART0
- #if (RT_USING_USART0 >= EFM32_USART_LOCATION_COUNT)
- #error "Wrong location number"
- #endif
- struct rt_device usart0_device;
- static struct rt_semaphore usart0_lock;
- #endif
- #ifdef RT_USING_USART1
- #if (RT_USING_USART1 >= EFM32_USART_LOCATION_COUNT)
- #error "Wrong location number"
- #endif
- struct rt_device usart1_device;
- static struct rt_semaphore usart1_lock;
- #endif
- #ifdef RT_USING_USART2
- #if (RT_USING_USART2 >= EFM32_USART_LOCATION_COUNT)
- #error "Wrong location number"
- #endif
- struct rt_device usart2_device;
- static struct rt_semaphore usart2_lock;
- #endif
- #ifdef RT_USING_UART0
- #if (RT_USING_UART0 >= EFM32_UART_LOCATION_COUNT)
- #error "Wrong location number"
- #endif
- struct rt_device uart0_device;
- static struct rt_semaphore uart0_lock;
- #endif
- #ifdef RT_USING_UART1
- #if (RT_USING_UART1 >= EFM32_UART_LOCATION_COUNT)
- #error "Wrong location number"
- #endif
- struct rt_device uart1_device;
- static struct rt_semaphore uart1_lock;
- #endif
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /***************************************************************************//**
- * @brief
- * Initialize USART device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @return
- * Error code
- ******************************************************************************/
- static rt_err_t rt_usart_init (rt_device_t dev)
- {
- struct efm32_usart_device_t *usart;
- usart = (struct efm32_usart_device_t *)(dev->user_data);
- if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
- {
- if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
- {
- struct efm32_usart_dma_mode_t *dma_tx;
- dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
- usart->state |= USART_STATE_RX_BUSY;
- }
- if (dev->flag & RT_DEVICE_FLAG_INT_RX)
- {
- struct efm32_usart_int_mode_t *int_rx;
- int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
- int_rx->data_ptr = RT_NULL;
- }
- /* Enable USART */
- USART_Enable(usart->usart_device, usartEnable);
- dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
- }
- return RT_EOK;
- }
- /***************************************************************************//**
- * @brief
- * Open USART device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @param[in] oflag
- * Device open flag
- *
- * @return
- * Error code
- ******************************************************************************/
- static rt_err_t rt_usart_open(rt_device_t dev, rt_uint16_t oflag)
- {
- RT_ASSERT(dev != RT_NULL);
- struct efm32_usart_device_t *usart;
- usart = (struct efm32_usart_device_t *)(dev->user_data);
- if (dev->flag & RT_DEVICE_FLAG_INT_RX)
- {
- IRQn_Type rxIrq;
- //if (usart->state & USART_STATE_CONSOLE)
- { /* Allocate new RX buffer */
- struct efm32_usart_int_mode_t *int_mode;
- int_mode = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
- if ((int_mode->data_ptr = rt_malloc(USART_RX_BUFFER_SIZE)) == RT_NULL)
- {
- usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
- return -RT_ENOMEM;
- }
- rt_memset(int_mode->data_ptr, 0, USART_RX_BUFFER_SIZE);
- int_mode->data_size = USART_RX_BUFFER_SIZE;
- int_mode->read_index = 0;
- int_mode->save_index = 0;
- }
- /* Enable RX interrupt */
- if (usart->state & USART_STATE_ASYNC_ONLY)
- {
- usart->usart_device->IEN = UART_IEN_RXDATAV;
- }
- else
- {
- usart->usart_device->IEN = USART_IEN_RXDATAV;
- }
- /* Enable IRQ */
- switch (usart->unit)
- {
- case 0:
- if (usart->state & USART_STATE_ASYNC_ONLY)
- {
- rxIrq = UART0_RX_IRQn;
- }
- else
- {
- rxIrq = USART0_RX_IRQn;
- }
- break;
- case 1:
- #if defined(EFM32_GIANT_FAMILY)
- if (usart->state & USART_STATE_ASYNC_ONLY)
- {
- rxIrq = UART1_RX_IRQn;
- }
- else
- #endif
- {
- rxIrq = USART1_RX_IRQn;
- }
- break;
- case 2:
- rxIrq = USART2_RX_IRQn;
- break;
- }
- if (oflag != RT_DEVICE_OFLAG_WRONLY)
- {
- NVIC_ClearPendingIRQ(rxIrq);
- NVIC_SetPriority(rxIrq, EFM32_IRQ_PRI_DEFAULT);
- NVIC_EnableIRQ(rxIrq);
- }
- }
- /* Clear Flag */
- if (usart->state & USART_STATE_ASYNC_ONLY)
- {
- usart->usart_device->IFC = _UART_IFC_MASK;
- }
- else
- {
- usart->usart_device->IFC = _USART_IFC_MASK;
- }
- if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (oflag != RT_DEVICE_OFLAG_RDONLY))
- {
- /* DMA IRQ is enabled by DMA_Init() */
- NVIC_SetPriority(DMA_IRQn, EFM32_IRQ_PRI_DEFAULT);
- }
- usart->counter++;
- usart_debug("USART%d: Open with flag %x\n", usart->unit, oflag);
- return RT_EOK;
- }
- /***************************************************************************//**
- * @brief
- * Close USART device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @return
- * Error code
- ******************************************************************************/
- static rt_err_t rt_usart_close(rt_device_t dev)
- {
- RT_ASSERT(dev != RT_NULL);
- struct efm32_usart_device_t *usart;
- usart = (struct efm32_usart_device_t *)(dev->user_data);
- if (--usart->counter == 0)
- {
- if (dev->flag & RT_DEVICE_FLAG_INT_RX)
- {
- struct efm32_usart_int_mode_t *int_rx;
- int_rx = (struct efm32_usart_int_mode_t *)usart->rx_mode;
- rt_free(int_rx->data_ptr);
- int_rx->data_ptr = RT_NULL;
- }
- }
- return RT_EOK;
- }
- /***************************************************************************//**
- * @brief
- * Read from USART device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @param[in] pos
- * Offset
- *
- * @param[in] buffer
- * Poniter to the buffer
- *
- * @param[in] size
- * Buffer size in byte
- *
- * @return
- * Number of read bytes
- ******************************************************************************/
- static rt_size_t rt_usart_read (
- rt_device_t dev,
- rt_off_t pos,
- void *buffer,
- rt_size_t size)
- {
- struct efm32_usart_device_t *usart;
- rt_uint8_t *ptr;
- rt_err_t err_code;
- rt_size_t read_len;
- usart = (struct efm32_usart_device_t *)(dev->user_data);
- /* Lock device */
- if (rt_hw_interrupt_check())
- {
- err_code = rt_sem_take(usart->lock, RT_WAITING_NO);
- }
- else
- {
- err_code = rt_sem_take(usart->lock, RT_WAITING_FOREVER);
- }
- if (err_code != RT_EOK)
- {
- rt_set_errno(err_code);
- return 0;
- }
- if (dev->flag & RT_DEVICE_FLAG_INT_RX)
- {
- ptr = buffer;
- /* interrupt mode Rx */
- while (size)
- {
- rt_base_t level;
- struct efm32_usart_int_mode_t *int_rx;
- int_rx = (struct efm32_usart_int_mode_t *)\
- (((struct efm32_usart_device_t *)(dev->user_data))->rx_mode);
- /* disable interrupt */
- level = rt_hw_interrupt_disable();
- if (int_rx->read_index != int_rx->save_index)
- {
- /* read a character */
- *ptr++ = int_rx->data_ptr[int_rx->read_index];
- size--;
- /* move to next position */
- int_rx->read_index ++;
- if (int_rx->read_index >= USART_RX_BUFFER_SIZE)
- {
- int_rx->read_index = 0;
- }
- }
- else
- {
- /* set error code */
- err_code = -RT_EEMPTY;
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
- break;
- }
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
- }
- read_len = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
- }
- else
- {
- struct efm32_usart_device_t *usart;
- USART_TypeDef *usart_device;
- usart = (struct efm32_usart_device_t *)(dev->user_data);
- usart_device = ((struct efm32_usart_device_t *)(dev->user_data))->usart_device;
- if (usart->state & USART_STATE_SYNC)
- {
- /* SPI read */
- rt_uint8_t inst_len = *((rt_uint8_t *)buffer);
- rt_uint8_t *inst_ptr = (rt_uint8_t *)(buffer + 1);
- rt_uint8_t *rx_buf = *((rt_uint8_t **)(buffer + inst_len + 1));
- rt_off_t i;
- ptr = rx_buf;
- /* write instruction */
- while (inst_len)
- {
- while (!(usart->usart_device->STATUS & USART_STATUS_TXBL));
- usart->usart_device->TXDATA = (rt_uint32_t)*inst_ptr;
- ++inst_ptr; --inst_len;
- }
- /* Flushing RX */
- usart_device->CMD = USART_CMD_CLEARRX;
- /* Skip some bytes if necessary */
- for (i = 0; i < pos; i++)
- {
- /* dummy write */
- while (!(usart_device->STATUS & USART_STATUS_TXBL));
- usart_device->TXDATA = (rt_uint32_t)0xff;
- /* dummy read */
- while (!(usart_device->STATUS & USART_STATUS_RXDATAV));
- *((rt_uint32_t *)0x00) = usart_device->RXDATA;
- }
- while ((rt_uint32_t)ptr - (rt_uint32_t)rx_buf < size)
- {
- /* dummy write */
- while (!(usart_device->STATUS & USART_STATUS_TXBL));
- usart_device->TXDATA = (rt_uint32_t)0xff;
- /* read a byte of data */
- while (!(usart_device->STATUS & USART_STATUS_RXDATAV));
- *ptr = usart_device->RXDATA & 0xff;
- ptr ++;
- }
- }
- else
- {
- rt_uint32_t flag;
- if (usart->state & USART_STATE_ASYNC_ONLY)
- {
- flag = UART_STATUS_RXDATAV;
- }
- else
- {
- flag = USART_STATUS_RXDATAV;
- }
- ptr = buffer;
- /* polling mode */
- while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
- {
- while (usart_device->STATUS & flag)
- {
- *ptr = usart_device->RXDATA & 0xff;
- ptr ++;
- }
- }
- }
- read_len = size;
- }
- /* Unlock device */
- rt_sem_release(usart->lock);
- /* set error code */
- rt_set_errno(err_code);
- return read_len;
- }
- /***************************************************************************//**
- * @brief
- * Write to USART device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @param[in] pos
- * Offset
- *
- * @param[in] buffer
- * Poniter to the buffer
- *
- * @param[in] size
- * Buffer size in byte
- *
- * @return
- * Number of written bytes
- ******************************************************************************/
- static rt_size_t rt_usart_write (
- rt_device_t dev,
- rt_off_t pos,
- const void* buffer,
- rt_size_t size)
- {
- rt_err_t err_code;
- rt_size_t write_size;
- struct efm32_usart_device_t* usart;
- write_size = 0;
- usart = (struct efm32_usart_device_t*)(dev->user_data);
- /* Lock device */
- if (rt_hw_interrupt_check())
- {
- err_code = rt_sem_take(usart->lock, RT_WAITING_NO);
- }
- else
- {
- err_code = rt_sem_take(usart->lock, RT_WAITING_FOREVER);
- }
- if (err_code != RT_EOK)
- {
- rt_set_errno(err_code);
- return 0;
- }
- if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (size > 2))
- { /* DMA mode Tx */
- struct efm32_usart_dma_mode_t *dma_tx;
- if (dev->flag & RT_DEVICE_FLAG_STREAM)
- {
- if (*((rt_uint8_t *)buffer + size - 1) == '\n')
- {
- *((rt_uint8_t *)buffer + size - 1) = '\r';
- *((rt_uint8_t *)buffer + size++) = '\n';
- *((rt_uint8_t *)buffer + size) = 0;
- }
- }
- dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
- dma_tx->data_ptr = (rt_uint32_t *)buffer;
- dma_tx->data_size = size;
- usart->state |= USART_STATE_TX_BUSY;
- DMA_ActivateBasic(
- dma_tx->dma_channel,
- true,
- false,
- (void *)&(usart->usart_device->TXDATA),
- (void *)buffer,
- (rt_uint32_t)(size - 1));
- /* Wait, otherwise the TX buffer is overwrite */
- // if (usart->state & USART_STATE_CONSOLE)
- // {
- while(usart->state & USART_STATE_TX_BUSY);
- // }
- // else
- // {
- // while(usart->state & USART_STATE_TX_BUSY)
- // {
- // rt_thread_sleep(USART_WAIT_TIME_TX);
- // }
- // }
- // TODO: This function blocks the process
- write_size = size;
- }
- else
- { /* polling mode */
- rt_uint8_t *ptr = (rt_uint8_t *)buffer;
- if (dev->flag & RT_DEVICE_FLAG_STREAM)
- {
- /* stream mode */
- while (size)
- {
- if (*ptr == '\n')
- {
- while (!(usart->usart_device->STATUS & USART_STATUS_TXBL));
- usart->usart_device->TXDATA = '\r';
- }
- while (!(usart->usart_device->STATUS & USART_STATUS_TXBL));
- usart->usart_device->TXDATA = (rt_uint32_t)*ptr;
- ++ptr; --size;
- }
- }
- else
- {
- rt_uint32_t flag;
- if (usart->state & USART_STATE_ASYNC_ONLY)
- {
- flag = UART_STATUS_TXBL;
- }
- else
- {
- flag = USART_STATUS_TXBL;
- }
- /* write data directly */
- while (size)
- {
- while (!(usart->usart_device->STATUS & flag));
- usart->usart_device->TXDATA = (rt_uint32_t)*ptr;
- ++ptr; --size;
- }
- }
- write_size = (rt_size_t)ptr - (rt_size_t)buffer;
- }
- /* Unlock device */
- rt_sem_release(usart->lock);
- /* set error code */
- rt_set_errno(err_code);
- return write_size;
- }
- /***************************************************************************//**
- * @brief
- * Configure USART device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @param[in] cmd
- * IIC control command
- *
- * @param[in] args
- * Arguments
- *
- * @return
- * Error code
- ******************************************************************************/
- static rt_err_t rt_usart_control (
- rt_device_t dev,
- rt_uint8_t cmd,
- void *args)
- {
- RT_ASSERT(dev != RT_NULL);
- rt_err_t err_code;
- struct efm32_usart_device_t *usart;
- usart = (struct efm32_usart_device_t *)(dev->user_data);
- /* Lock device */
- if (rt_hw_interrupt_check())
- {
- err_code = rt_sem_take(usart->lock, RT_WAITING_NO);
- }
- else
- {
- err_code = rt_sem_take(usart->lock, RT_WAITING_FOREVER);
- }
- if (err_code != RT_EOK)
- {
- return err_code;
- }
- switch (cmd)
- {
- case RT_DEVICE_CTRL_SUSPEND:
- /* Suspend device */
- dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
- USART_Enable(usart->usart_device, usartDisable);
- break;
- case RT_DEVICE_CTRL_RESUME:
- /* Resume device */
- dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
- USART_Enable(usart->usart_device, usartEnable);
- break;
- case RT_DEVICE_CTRL_USART_RBUFFER:
- /* Set RX buffer */
- {
- struct efm32_usart_int_mode_t *int_rx;
- rt_uint8_t size;
- int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
- size = (rt_uint8_t)((rt_uint32_t)args & 0xFFUL);
- /* Free previous RX buffer */
- if (int_rx->data_ptr != RT_NULL)
- {
- if (size == 0)
- { /* Free RX buffer */
- rt_free(int_rx->data_ptr);
- int_rx->data_ptr = RT_NULL;
- }
- else if (size != int_rx->data_size)
- {
- /* Re-allocate RX buffer */
- if ((int_rx->data_ptr = rt_realloc(int_rx->data_ptr, size)) \
- == RT_NULL)
- {
- usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
- err_code = -RT_ENOMEM;
- break;
- }
- // TODO: Is the following line necessary?
- //rt_memset(int_rx->data_ptr, 0, size);
- }
- }
- else
- {
- /* Allocate new RX buffer */
- if ((int_rx->data_ptr = rt_malloc(size)) == RT_NULL)
- {
- usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
- err_code = -RT_ENOMEM;
- break;
- }
- }
- int_rx->data_size = size;
- int_rx->read_index = 0;
- int_rx->save_index = 0;
- }
- break;
- }
- /* Unlock device */
- rt_sem_release(usart->lock);
- return err_code;
- }
- /***************************************************************************//**
- * @brief
- * USART RX data valid interrupt handler
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- ******************************************************************************/
- void rt_hw_usart_rx_isr(rt_device_t dev)
- {
- struct efm32_usart_device_t *usart;
- struct efm32_usart_int_mode_t *int_rx;
- rt_uint32_t flag;
- /* interrupt mode receive */
- RT_ASSERT(dev->flag & RT_DEVICE_FLAG_INT_RX);
- usart = (struct efm32_usart_device_t *)(dev->user_data);
- int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
- RT_ASSERT(int_rx->data_ptr != RT_NULL);
- /* Set status */
- usart->state |= USART_STATE_RX_BUSY;
- /* save into rx buffer */
- if (usart->state & USART_STATE_ASYNC_ONLY)
- {
- flag = UART_STATUS_RXDATAV;
- }
- else
- {
- flag = USART_STATUS_RXDATAV;
- }
- while (usart->usart_device->STATUS & flag)
- {
- rt_base_t level;
- /* disable interrupt */
- level = rt_hw_interrupt_disable();
- /* save character */
- int_rx->data_ptr[int_rx->save_index] = \
- (rt_uint8_t)(usart->usart_device->RXDATA & 0xFFUL);
- int_rx->save_index ++;
- if (int_rx->save_index >= USART_RX_BUFFER_SIZE)
- int_rx->save_index = 0;
- /* if the next position is read index, discard this 'read char' */
- if (int_rx->save_index == int_rx->read_index)
- {
- int_rx->read_index ++;
- if (int_rx->read_index >= USART_RX_BUFFER_SIZE)
- {
- int_rx->read_index = 0;
- }
- }
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
- }
- /* invoke callback */
- if (dev->rx_indicate != RT_NULL)
- {
- rt_size_t rx_length;
- /* get rx length */
- rx_length = int_rx->read_index > int_rx->save_index ?
- USART_RX_BUFFER_SIZE - int_rx->read_index + int_rx->save_index : \
- int_rx->save_index - int_rx->read_index;
- dev->rx_indicate(dev, rx_length);
- }
- }
- /***************************************************************************//**
- * @brief
- * DMA for USART TX interrupt handler
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- ******************************************************************************/
- void rt_hw_usart_dma_tx_isr(rt_device_t dev)
- {
- /* DMA mode receive */
- struct efm32_usart_device_t *usart;
- struct efm32_usart_dma_mode_t *dma_tx;
- RT_ASSERT(dev->flag & RT_DEVICE_FLAG_DMA_TX);
- usart = (struct efm32_usart_device_t *)(dev->user_data);
- dma_tx = (struct efm32_usart_dma_mode_t *)(usart->tx_mode);
- /* invoke call to notify tx complete */
- if (dev->tx_complete != RT_NULL)
- {
- dev->tx_complete(dev, dma_tx->data_ptr);
- }
- /* Set status */
- usart->state &= ~(rt_uint32_t)USART_STATE_TX_BUSY;
- }
- /***************************************************************************//**
- * @brief
- * Register USART device
- *
- * @details
- *
- * @note
- *
- * @param[in] device
- * Pointer to device descriptor
- *
- * @param[in] name
- * Device name
- *
- * @param[in] flag
- * Configuration flags
- *
- * @param[in] usart
- * Pointer to USART device descriptor
- *
- * @return
- * Error code
- ******************************************************************************/
- rt_err_t rt_hw_usart_register(
- rt_device_t device,
- const char *name,
- rt_uint32_t flag,
- struct efm32_usart_device_t *usart)
- {
- RT_ASSERT(device != RT_NULL);
- if ((flag & RT_DEVICE_FLAG_DMA_RX) ||
- (flag & RT_DEVICE_FLAG_INT_TX))
- {
- RT_ASSERT(0);
- }
- if (usart->state & USART_STATE_SYNC)
- {
- device->type = RT_Device_Class_SPIBUS;
- }
- else
- {
- device->type = RT_Device_Class_Char;
- }
- device->rx_indicate = RT_NULL;
- device->tx_complete = RT_NULL;
- device->init = rt_usart_init;
- device->open = rt_usart_open;
- device->close = rt_usart_close;
- device->read = rt_usart_read;
- device->write = rt_usart_write;
- device->control = rt_usart_control;
- device->user_data = usart;
- /* register a character device */
- return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
- }
- /***************************************************************************//**
- * @brief
- * Initialize the specified USART unit
- *
- * @details
- *
- * @note
- *
- * @param[in] device
- * Pointer to device descriptor
- *
- * @param[in] unitNumber
- * Unit number
- *
- * @param[in] location
- * Pin location number
- *
- * @param[in] flag
- * Configuration flag
- *
- * @param[in] dmaChannel
- * DMA channel number for TX
- *
- * @param[in] console
- * Indicate if using as console
- *
- * @return
- * Pointer to USART device
- ******************************************************************************/
- static struct efm32_usart_device_t *rt_hw_usart_unit_init(
- rt_device_t device,
- rt_uint8_t unitNumber,
- rt_uint8_t location,
- rt_uint32_t flag,
- rt_uint32_t dmaChannel,
- rt_uint8_t config)
- {
- struct efm32_usart_device_t *usart;
- struct efm32_usart_dma_mode_t *dma_mode;
- DMA_CB_TypeDef *callback;
- CMU_Clock_TypeDef usartClock;
- rt_uint32_t txDmaSelect;
- GPIO_Port_TypeDef port_tx, port_rx, port_clk, port_cs;
- rt_uint32_t pin_tx, pin_rx, pin_clk, pin_cs;
- efm32_irq_hook_init_t hook;
- do
- {
- /* Allocate device */
- usart = rt_malloc(sizeof(struct efm32_usart_device_t));
- if (usart == RT_NULL)
- {
- usart_debug("USART%d err: no mem\n", usart->unit);
- break;
- }
- usart->counter = 0;
- usart->unit = unitNumber;
- usart->state = config;
- usart->tx_mode = RT_NULL;
- usart->rx_mode = RT_NULL;
- /* Allocate TX */
- dma_mode = RT_NULL;
- if (flag & RT_DEVICE_FLAG_DMA_TX)
- {
- usart->tx_mode = dma_mode = rt_malloc(sizeof(struct efm32_usart_dma_mode_t));
- if (dma_mode == RT_NULL)
- {
- usart_debug("USART%d err: no mem for DMA TX\n", usart->unit);
- break;
- }
- dma_mode->dma_channel = dmaChannel;
- }
- /* Allocate RX */
- if (flag & RT_DEVICE_FLAG_INT_RX)
- {
- usart->rx_mode = rt_malloc(sizeof(struct efm32_usart_int_mode_t));
- if (usart->rx_mode == RT_NULL)
- {
- usart_debug("USART%d err: no mem for INT RX\n", usart->unit);
- break;
- }
- }
- /* Initialization */
- if ((!(config & USART_STATE_ASYNC_ONLY) && (unitNumber >= USART_COUNT)) || \
- ((config & USART_STATE_ASYNC_ONLY) && (unitNumber >= UART_COUNT)))
- {
- break;
- }
- switch (unitNumber)
- {
- case 0:
- if (config & USART_STATE_ASYNC_ONLY)
- {
- usart->usart_device = UART0;
- usartClock = (CMU_Clock_TypeDef)cmuClock_UART0;
- txDmaSelect = DMAREQ_UART0_TXBL;
- port_tx = AF_UART0_TX_PORT(location);
- pin_tx = AF_UART0_TX_PIN(location);
- port_rx = AF_UART0_RX_PORT(location);
- pin_rx = AF_UART0_RX_PIN(location);
- }
- else
- {
- usart->usart_device = USART0;
- usartClock = (CMU_Clock_TypeDef)cmuClock_USART0;
- txDmaSelect = DMAREQ_USART0_TXBL;
- port_tx = AF_USART0_TX_PORT(location);
- pin_tx = AF_USART0_TX_PIN(location);
- port_rx = AF_USART0_RX_PORT(location);
- pin_rx = AF_USART0_RX_PIN(location);
- port_clk = AF_USART0_CLK_PORT(location);
- pin_clk = AF_USART0_CLK_PIN(location);
- port_cs = AF_USART0_CS_PORT(location);
- pin_cs = AF_USART0_CS_PIN(location);
- }
- break;
- case 1:
- #if defined(EFM32_GIANT_FAMILY)
- if (config & USART_STATE_ASYNC_ONLY)
- {
- usart->usart_device = UART1;
- usartClock = (CMU_Clock_TypeDef)cmuClock_UART1;
- txDmaSelect = DMAREQ_UART1_TXBL;
- port_tx = AF_UART1_TX_PORT(location);
- pin_tx = AF_UART1_TX_PIN(location);
- port_rx = AF_UART1_RX_PORT(location);
- pin_rx = AF_UART1_RX_PIN(location);
- }
- else
- #endif
- {
- usart->usart_device = USART1;
- usartClock = (CMU_Clock_TypeDef)cmuClock_USART1;
- txDmaSelect = DMAREQ_USART1_TXBL;
- port_tx = AF_USART1_TX_PORT(location);
- pin_tx = AF_USART1_TX_PIN(location);
- port_rx = AF_USART1_RX_PORT(location);
- pin_rx = AF_USART1_RX_PIN(location);
- port_clk = AF_USART1_CLK_PORT(location);
- pin_clk = AF_USART1_CLK_PIN(location);
- port_cs = AF_USART1_CS_PORT(location);
- pin_cs = AF_USART1_CS_PIN(location);
- }
- break;
- case 2:
- usart->usart_device = USART2;
- usartClock = (CMU_Clock_TypeDef)cmuClock_USART2;
- txDmaSelect = DMAREQ_USART2_TXBL;
- port_tx = AF_USART2_TX_PORT(location);
- pin_tx = AF_USART2_TX_PIN(location);
- port_rx = AF_USART2_RX_PORT(location);
- pin_rx = AF_USART2_RX_PIN(location);
- port_clk = AF_USART2_CLK_PORT(location);
- pin_clk = AF_USART2_CLK_PIN(location);
- port_cs = AF_USART2_CS_PORT(location);
- pin_cs = AF_USART2_CS_PIN(location);
- break;
- default:
- break;
- }
- /* Enable USART clock */
- CMU_ClockEnable(usartClock, true);
- /* Config GPIO */
- GPIO_PinModeSet(
- port_tx,
- pin_tx,
- gpioModePushPull,
- 0);
- GPIO_PinModeSet(
- port_rx,
- pin_rx,
- gpioModeInputPull,
- 1);
- if (config & USART_STATE_SYNC)
- {
- GPIO_PinModeSet(
- port_clk,
- pin_clk,
- gpioModePushPull,
- 0);
- }
- if (config & USART_STATE_AUTOCS)
- {
- GPIO_PinModeSet(
- port_cs,
- pin_cs,
- gpioModePushPull,
- 1);
- }
- /* Config interrupt and NVIC */
- if (flag & RT_DEVICE_FLAG_INT_RX)
- {
- hook.type = efm32_irq_type_usart;
- hook.unit = unitNumber * 2 + 1;
- if (config & USART_STATE_ASYNC_ONLY)
- {
- hook.unit += USART_COUNT * 2;
- }
- hook.cbFunc = rt_hw_usart_rx_isr;
- hook.userPtr = device;
- efm32_irq_hook_register(&hook);
- }
- /* Config DMA */
- if (flag & RT_DEVICE_FLAG_DMA_TX)
- {
- DMA_CfgChannel_TypeDef chnlCfg;
- DMA_CfgDescr_TypeDef descrCfg;
- hook.type = efm32_irq_type_dma;
- hook.unit = dmaChannel;
- hook.cbFunc = rt_hw_usart_dma_tx_isr;
- hook.userPtr = device;
- efm32_irq_hook_register(&hook);
- callback = (DMA_CB_TypeDef *)rt_malloc(sizeof(DMA_CB_TypeDef));
- if (callback == RT_NULL)
- {
- usart_debug("USART%d err: no mem for callback\n", usart->unit);
- break;
- }
- callback->cbFunc = DMA_IRQHandler_All;
- callback->userPtr = RT_NULL;
- callback->primary = 0;
- /* Setting up DMA channel */
- chnlCfg.highPri = false; /* Can't use with peripherals */
- chnlCfg.enableInt = true; /* Interrupt for callback function */
- chnlCfg.select = txDmaSelect;
- chnlCfg.cb = callback;
- DMA_CfgChannel(dmaChannel, &chnlCfg);
- /* Setting up DMA channel descriptor */
- descrCfg.dstInc = dmaDataIncNone;
- descrCfg.srcInc = dmaDataInc1;
- descrCfg.size = dmaDataSize1;
- descrCfg.arbRate = dmaArbitrate1;
- descrCfg.hprot = 0;
- DMA_CfgDescr(dmaChannel, true, &descrCfg);
- }
- /* Init specified USART unit */
- if (config & USART_STATE_SYNC)
- {
- USART_InitSync_TypeDef init_sync = USART_INITSYNC_DEFAULT;
- init_sync.enable = usartEnable;
- init_sync.refFreq = 0;
- init_sync.baudrate = SPI_BAUDRATE;
- init_sync.databits = usartDatabits8;
- if (config & USART_STATE_MASTER)
- {
- init_sync.master = true;
- }
- else
- {
- init_sync.master = false;
- }
- init_sync.msbf = true;
- init_sync.clockMode = usartClockMode0; /* Clock idle low, sample on rising edge. */
- USART_InitSync(usart->usart_device, &init_sync);
- }
- else
- {
- USART_InitAsync_TypeDef init_async = USART_INITASYNC_DEFAULT;
- init_async.enable = usartEnable;
- init_async.refFreq = 0;
- init_async.baudrate = UART_BAUDRATE;
- init_async.oversampling = USART_CTRL_OVS_X4;
- init_async.databits = USART_FRAME_DATABITS_EIGHT;
- init_async.parity = USART_FRAME_PARITY_NONE;
- init_async.stopbits = USART_FRAME_STOPBITS_ONE;
- USART_InitAsync(usart->usart_device, &init_async);
- }
- /* Enable RX and TX pins and set location */
- usart->usart_device->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | \
- (location << _USART_ROUTE_LOCATION_SHIFT);
- if (config & USART_STATE_SYNC)
- {
- usart->usart_device->ROUTE |= USART_ROUTE_CLKPEN;
- }
- if (config & USART_STATE_AUTOCS)
- {
- usart->usart_device->ROUTE |= USART_ROUTE_CSPEN;
- if (config & USART_STATE_MASTER)
- {
- usart->usart_device->CTRL |= USART_CTRL_AUTOCS;
- }
- }
- /* Clear RX/TX buffers */
- usart->usart_device->CMD = USART_CMD_CLEARRX | USART_CMD_CLEARTX;
- return usart;
- } while(0);
- if (usart->rx_mode)
- {
- rt_free(usart->rx_mode);
- }
- if (usart->tx_mode)
- {
- rt_free(usart->tx_mode);
- }
- if (usart)
- {
- rt_free(usart);
- }
- if (callback)
- {
- rt_free(usart);
- }
- if (config & USART_STATE_ASYNC_ONLY)
- {
- usart_debug("UART%d err: init failed!\n", unitNumber);
- }
- else
- {
- usart_debug("USART%d err: init failed!\n", unitNumber);
- }
- return RT_NULL;
- }
- /***************************************************************************//**
- * @brief
- * Initialize all USART module related hardware and register USART device to
- * kernel
- *
- * @details
- *
- * @note
- ******************************************************************************/
- void rt_hw_usart_init(void)
- {
- struct efm32_usart_device_t *usart;
- rt_uint32_t flag;
- rt_uint8_t config;
- do
- {
- #ifdef RT_USING_USART0
- config = 0x00;
- flag = RT_DEVICE_FLAG_RDWR;
- #ifdef RT_USART0_SYNC_MODE
- config |= USART_STATE_SYNC;
- #if (RT_USART0_SYNC_MODE != 0x0UL)
- config |= USART_STATE_MASTER;
- #else
- flag |= RT_DEVICE_FLAG_INT_RX;
- #endif
- #else
- flag |= RT_DEVICE_FLAG_INT_RX;
- #endif
- #if (SPI_AUTOCS_ENABLE & (1 << 0))
- config |= USART_STATE_AUTOCS;
- #endif
- #if (RT_CONSOLE_DEVICE == EFM_USART0)
- config |= USART_STATE_CONSOLE;
- flag |= RT_DEVICE_FLAG_STREAM;
- #endif
- #ifdef RT_USART0_USING_DMA
- RT_ASSERT(RT_USART0_USING_DMA < DMA_CHAN_COUNT);
- flag |= RT_DEVICE_FLAG_DMA_TX;
- #else
- #define RT_USART0_USING_DMA EFM32_NO_DMA
- #endif
- /* Initialize and Register usart0 */
- if ((usart = rt_hw_usart_unit_init(
- &usart0_device,
- 0,
- RT_USING_USART0,
- flag,
- RT_USART0_USING_DMA,
- config)) != RT_NULL)
- {
- rt_hw_usart_register(&usart0_device, RT_USART0_NAME, flag, usart);
- }
- else
- {
- break;
- }
- /* Initialize lock for usart0 */
- usart->lock = &usart0_lock;
- if (rt_sem_init(usart->lock, RT_USART0_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
- {
- break;
- }
- #endif
- #ifdef RT_USING_USART1
- config = 0x00;
- flag = RT_DEVICE_FLAG_RDWR;
- #ifdef RT_USART1_SYNC_MODE
- config |= USART_STATE_SYNC;
- #if (RT_USART1_SYNC_MODE != 0x0UL)
- config |= USART_STATE_MASTER;
- #else
- flag |= RT_DEVICE_FLAG_INT_RX;
- #endif
- #else
- flag |= RT_DEVICE_FLAG_INT_RX;
- #endif
- #if (SPI_AUTOCS_ENABLE & (1 << 1))
- config |= USART_STATE_AUTOCS;
- #endif
- #if (RT_CONSOLE_DEVICE == EFM_USART1)
- config |= USART_STATE_CONSOLE;
- flag |= RT_DEVICE_FLAG_STREAM;
- #endif
- #ifdef RT_USART1_USING_DMA
- RT_ASSERT(RT_USART1_USING_DMA < DMA_CHAN_COUNT);
- flag |= RT_DEVICE_FLAG_DMA_TX;
- #else
- #define RT_USART1_USING_DMA EFM32_NO_DMA
- #endif
- /* Initialize and Register usart1 */
- if ((usart = rt_hw_usart_unit_init(
- &usart1_device,
- 1,
- RT_USING_USART1,
- flag,
- RT_USART1_USING_DMA,
- config)) != RT_NULL)
- {
- rt_hw_usart_register(&usart1_device, RT_USART1_NAME, flag, usart);
- }
- else
- {
- break;
- }
- /* Initialize lock for usart1 */
- usart->lock = &usart1_lock;
- if (rt_sem_init(usart->lock, RT_USART1_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
- {
- break;
- }
- #endif
- #ifdef RT_USING_USART2
- config = 0x00;
- flag = RT_DEVICE_FLAG_RDWR;
- #ifdef RT_USART2_SYNC_MODE
- config |= USART_STATE_SYNC;
- #if (RT_USART2_SYNC_MODE != 0x0UL)
- config |= USART_STATE_MASTER;
- #else
- flag |= RT_DEVICE_FLAG_INT_RX;
- #endif
- #else
- flag |= RT_DEVICE_FLAG_INT_RX;
- #endif
- #if (SPI_AUTOCS_ENABLE & (1 << 2))
- config |= USART_STATE_AUTOCS;
- #endif
- #if (RT_CONSOLE_DEVICE == EFM_USART2)
- config |= USART_STATE_CONSOLE;
- flag |= RT_DEVICE_FLAG_STREAM;
- #endif
- #ifdef RT_USART2_USING_DMA
- RT_ASSERT(RT_USART2_USING_DMA < DMA_CHAN_COUNT);
- flag |= RT_DEVICE_FLAG_DMA_TX;
- #else
- #define RT_USART2_USING_DMA EFM32_NO_DMA
- #endif
- /* Initialize and Register usart2 */
- if ((usart = rt_hw_usart_unit_init(
- &usart2_device,
- 2,
- RT_USING_USART2,
- flag,
- RT_USART2_USING_DMA,
- config)) != RT_NULL)
- {
- rt_hw_usart_register(&usart2_device, RT_USART2_NAME, flag, usart);
- }
- else
- {
- break;
- }
- /* Initialize lock for usart2 */
- usart->lock = &usart2_lock;
- if (rt_sem_init(usart->lock, RT_USART2_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
- {
- break;
- }
- #endif
- #ifdef RT_USING_UART0
- config = USART_STATE_ASYNC_ONLY;
- flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
- #if (RT_CONSOLE_DEVICE == EFM_UART0)
- config |= USART_STATE_CONSOLE;
- flag |= RT_DEVICE_FLAG_STREAM;
- #endif
- #ifdef RT_UART0_USING_DMA
- RT_ASSERT(RT_UART0_USING_DMA < DMA_CHAN_COUNT);
- flag |= RT_DEVICE_FLAG_DMA_TX;
- #else
- #define RT_UART0_USING_DMA EFM32_NO_DMA
- #endif
- /* Initialize and Register uart0 */
- if ((usart = rt_hw_usart_unit_init(
- &uart0_device,
- 0,
- RT_USING_UART0,
- flag,
- RT_UART0_USING_DMA,
- config)) != RT_NULL)
- {
- rt_hw_usart_register(&uart0_device, RT_UART0_NAME, flag, usart);
- }
- else
- {
- break;
- }
- /* Initialize lock for uart0 */
- usart->lock = &uart0_lock;
- if (rt_sem_init(usart->lock, RT_UART0_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
- {
- break;
- }
- #endif
- #ifdef RT_USING_UART1
- config = USART_STATE_ASYNC_ONLY;
- flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
- #if (RT_CONSOLE_DEVICE == EFM_UART1)
- config |= USART_STATE_CONSOLE;
- flag |= RT_DEVICE_FLAG_STREAM;
- #endif
- #ifdef RT_UART1_USING_DMA
- RT_ASSERT(RT_UART1_USING_DMA < DMA_CHAN_COUNT);
- flag |= RT_DEVICE_FLAG_DMA_TX;
- #else
- #define RT_UART1_USING_DMA EFM32_NO_DMA
- #endif
- /* Initialize and Register uart1 */
- if ((usart = rt_hw_usart_unit_init(
- &uart1_device,
- 1,
- RT_USING_UART1,
- flag,
- RT_UART1_USING_DMA,
- config)) != RT_NULL)
- {
- rt_hw_usart_register(&uart1_device, RT_UART1_NAME, flag, usart);
- }
- else
- {
- break;
- }
- /* Initialize lock for uart1 */
- usart->lock = &uart1_lock;
- if (rt_sem_init(usart->lock, RT_UART1_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
- {
- break;
- }
- #endif
- usart_debug("USART: H/W init OK!\n");
- return;
- } while (0);
- rt_kprintf("USART: H/W init failed!\n");
- }
- #endif /* (defined(RT_USING_USART0) || defined(RT_USING_USART1) || \
- defined(RT_USING_USART2) || defined(RT_USING_UART0) || \
- defined(RT_USING_UART1)) */
- /***************************************************************************//**
- * @}
- ******************************************************************************/
|