2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
|
|
|
* @file drv_usart.c
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief USART driver of RT-Thread RTOS for EFM32
|
2011-02-28 12:47:50 +08:00
|
|
|
* COPYRIGHT (C) 2011, RT-Thread Development Team
|
2011-02-17 11:33:15 +08:00
|
|
|
* @author onelife
|
2011-07-08 15:13:20 +08:00
|
|
|
* @version 0.4 beta
|
|
|
|
*******************************************************************************
|
2011-02-17 11:33:15 +08:00
|
|
|
* @section License
|
2011-12-16 09:05:50 +08:00
|
|
|
* The license and distribution terms for this file may be found in the file
|
2011-07-08 15:13:20 +08:00
|
|
|
* LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
|
|
|
|
*******************************************************************************
|
2011-12-16 09:05:50 +08:00
|
|
|
* @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.
|
|
|
|
*
|
2011-02-17 11:33:15 +08:00
|
|
|
* @section Change Logs
|
|
|
|
* Date Author Notes
|
|
|
|
* 2010-12-22 onelife Initial creation for EFM32
|
|
|
|
* 2011-01-17 onelife Merge with serial.c
|
2011-05-12 15:19:37 +08:00
|
|
|
* 2011-05-06 onelife Add sync mode (SPI) support
|
2011-06-20 09:56:28 +08:00
|
|
|
* 2011-06-14 onelife Fix a bug of TX by DMA
|
2011-12-16 09:05:50 +08:00
|
|
|
* 2011-06-16 onelife Modify init function for EFM32 library v2.0.0
|
2011-11-29 17:15:10 +08:00
|
|
|
* upgrading
|
2011-07-08 15:13:20 +08:00
|
|
|
* 2011-07-07 onelife Modify write function to avoid sleep in ISR
|
2011-12-16 09:05:50 +08:00
|
|
|
* 2011-07-26 onelife Add lock (semaphore) to prevent simultaneously
|
2011-07-29 10:47:37 +08:00
|
|
|
* access
|
2011-12-16 09:05:50 +08:00
|
|
|
* 2011-11-29 onelife Modify init function for EFM32 library v2.2.2
|
2011-11-29 17:15:10 +08:00
|
|
|
* upgrading
|
2011-12-16 09:05:50 +08:00
|
|
|
* 2011-12-09 onelife Add giant gecko support
|
|
|
|
* 2011-12-09 onelife Add UART module support
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @addtogroup efm32
|
|
|
|
* @{
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/* Includes ------------------------------------------------------------------*/
|
2011-02-17 11:33:15 +08:00
|
|
|
#include "board.h"
|
|
|
|
#include "hdl_interrupt.h"
|
|
|
|
#include "drv_usart.h"
|
|
|
|
|
2011-12-16 09:05:50 +08:00
|
|
|
#if (defined(RT_USING_USART0) || defined(RT_USING_USART1) || \
|
|
|
|
defined(RT_USING_USART2) || defined(RT_USING_UART0) || \
|
|
|
|
defined(RT_USING_UART1))
|
2011-07-08 15:13:20 +08:00
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
2011-05-12 15:19:37 +08:00
|
|
|
#ifdef RT_USART_DEBUG
|
|
|
|
#define usart_debug(format,args...) rt_kprintf(format, ##args)
|
|
|
|
#else
|
|
|
|
#define usart_debug(format,args...)
|
|
|
|
#endif
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/* Private variables ---------------------------------------------------------*/
|
2011-02-17 11:33:15 +08:00
|
|
|
#ifdef RT_USING_USART0
|
2011-12-16 09:05:50 +08:00
|
|
|
#if (RT_USING_USART0 >= EFM32_USART_LOCATION_COUNT)
|
|
|
|
#error "Wrong location number"
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
struct rt_device usart0_device;
|
2011-07-29 10:47:37 +08:00
|
|
|
static struct rt_semaphore usart0_lock;
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_USART1
|
2011-12-16 09:05:50 +08:00
|
|
|
#if (RT_USING_USART1 >= EFM32_USART_LOCATION_COUNT)
|
|
|
|
#error "Wrong location number"
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
struct rt_device usart1_device;
|
2011-07-29 10:47:37 +08:00
|
|
|
static struct rt_semaphore usart1_lock;
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_USART2
|
2011-12-16 09:05:50 +08:00
|
|
|
#if (RT_USING_USART2 >= EFM32_USART_LOCATION_COUNT)
|
|
|
|
#error "Wrong location number"
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
struct rt_device usart2_device;
|
2011-07-29 10:47:37 +08:00
|
|
|
static struct rt_semaphore usart2_lock;
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
|
2011-12-16 09:05:50 +08:00
|
|
|
#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
|
|
|
|
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
|
|
/* Private functions ---------------------------------------------------------*/
|
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
|
|
|
* Initialize USART device
|
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
* @param[in] dev
|
|
|
|
* Pointer to device descriptor
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Error code
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
static rt_err_t rt_usart_init (rt_device_t dev)
|
|
|
|
{
|
|
|
|
struct efm32_usart_device_t *usart;
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
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;
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
int_rx = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
int_rx->data_ptr = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable USART */
|
|
|
|
USART_Enable(usart->usart_device, usartEnable);
|
|
|
|
|
|
|
|
dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
|
|
|
* Open USART device
|
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
* @param[in] dev
|
|
|
|
* Pointer to device descriptor
|
|
|
|
*
|
|
|
|
* @param[in] oflag
|
|
|
|
* Device open flag
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Error code
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
static rt_err_t rt_usart_open(rt_device_t dev, rt_uint16_t oflag)
|
|
|
|
{
|
2011-02-28 12:47:50 +08:00
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
struct efm32_usart_device_t *usart;
|
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
usart = (struct efm32_usart_device_t *)(dev->user_data);
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
|
|
|
|
{
|
|
|
|
IRQn_Type rxIrq;
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
//if (usart->state & USART_STATE_CONSOLE)
|
|
|
|
{ /* Allocate new RX buffer */
|
|
|
|
struct efm32_usart_int_mode_t *int_mode;
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
int_mode = (struct efm32_usart_int_mode_t *)(usart->rx_mode);
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
if ((int_mode->data_ptr = rt_malloc(USART_RX_BUFFER_SIZE)) == RT_NULL)
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
|
2011-05-12 15:19:37 +08:00
|
|
|
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;
|
|
|
|
}
|
2011-12-16 09:05:50 +08:00
|
|
|
|
|
|
|
/* 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 */
|
2011-05-12 15:19:37 +08:00
|
|
|
switch (usart->unit)
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
2011-05-12 15:19:37 +08:00
|
|
|
case 0:
|
2011-12-16 09:05:50 +08:00
|
|
|
if (usart->state & USART_STATE_ASYNC_ONLY)
|
|
|
|
{
|
|
|
|
rxIrq = UART0_RX_IRQn;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rxIrq = USART0_RX_IRQn;
|
|
|
|
}
|
2011-05-12 15:19:37 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2011-12-16 09:05:50 +08:00
|
|
|
#if defined(EFM32_GIANT_FAMILY)
|
|
|
|
if (usart->state & USART_STATE_ASYNC_ONLY)
|
|
|
|
{
|
|
|
|
rxIrq = UART1_RX_IRQn;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
rxIrq = USART1_RX_IRQn;
|
|
|
|
}
|
2011-05-12 15:19:37 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
rxIrq = USART2_RX_IRQn;
|
|
|
|
break;
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
2011-05-12 15:19:37 +08:00
|
|
|
if (oflag != RT_DEVICE_OFLAG_WRONLY)
|
|
|
|
{
|
|
|
|
NVIC_ClearPendingIRQ(rxIrq);
|
|
|
|
NVIC_SetPriority(rxIrq, EFM32_IRQ_PRI_DEFAULT);
|
|
|
|
NVIC_EnableIRQ(rxIrq);
|
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
2011-12-16 09:05:50 +08:00
|
|
|
|
|
|
|
/* Clear Flag */
|
|
|
|
if (usart->state & USART_STATE_ASYNC_ONLY)
|
|
|
|
{
|
|
|
|
usart->usart_device->IFC = _UART_IFC_MASK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
usart->usart_device->IFC = _USART_IFC_MASK;
|
|
|
|
}
|
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (oflag != RT_DEVICE_OFLAG_RDONLY))
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
|
|
|
/* DMA IRQ is enabled by DMA_Init() */
|
|
|
|
NVIC_SetPriority(DMA_IRQn, EFM32_IRQ_PRI_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
usart->counter++;
|
2011-05-12 15:19:37 +08:00
|
|
|
usart_debug("USART%d: Open with flag %x\n", usart->unit, oflag);
|
2011-02-17 11:33:15 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
|
|
|
* Close USART device
|
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
* @param[in] dev
|
|
|
|
* Pointer to device descriptor
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Error code
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
static rt_err_t rt_usart_close(rt_device_t dev)
|
|
|
|
{
|
2011-07-29 10:47:37 +08:00
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
|
|
|
|
|
struct efm32_usart_device_t *usart;
|
|
|
|
|
|
|
|
usart = (struct efm32_usart_device_t *)(dev->user_data);
|
|
|
|
|
|
|
|
if (--usart->counter == 0)
|
2011-05-12 15:19:37 +08:00
|
|
|
{
|
2011-07-29 10:47:37 +08:00
|
|
|
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
|
|
|
|
{
|
|
|
|
struct efm32_usart_int_mode_t *int_rx;
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
int_rx = (struct efm32_usart_int_mode_t *)usart->rx_mode;
|
|
|
|
|
|
|
|
rt_free(int_rx->data_ptr);
|
|
|
|
int_rx->data_ptr = RT_NULL;
|
|
|
|
}
|
2011-05-12 15:19:37 +08:00
|
|
|
}
|
2011-07-29 10:47:37 +08:00
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @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
|
2011-06-20 09:56:28 +08:00
|
|
|
* Number of read bytes
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
static rt_size_t rt_usart_read (
|
2011-12-16 09:05:50 +08:00
|
|
|
rt_device_t dev,
|
|
|
|
rt_off_t pos,
|
|
|
|
void *buffer,
|
2011-02-17 11:33:15 +08:00
|
|
|
rt_size_t size)
|
|
|
|
{
|
2011-07-29 10:47:37 +08:00
|
|
|
struct efm32_usart_device_t *usart;
|
2011-05-12 15:19:37 +08:00
|
|
|
rt_uint8_t *ptr;
|
2011-02-17 11:33:15 +08:00
|
|
|
rt_err_t err_code;
|
2011-05-12 15:19:37 +08:00
|
|
|
rt_size_t read_len;
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
usart = (struct efm32_usart_device_t *)(dev->user_data);
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
|
|
|
|
if (dev->flag & RT_DEVICE_FLAG_INT_RX)
|
|
|
|
{
|
2011-05-12 15:19:37 +08:00
|
|
|
ptr = buffer;
|
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
/* 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 ++;
|
2011-05-12 15:19:37 +08:00
|
|
|
if (int_rx->read_index >= USART_RX_BUFFER_SIZE)
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2011-05-12 15:19:37 +08:00
|
|
|
|
|
|
|
read_len = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-12 15:19:37 +08:00
|
|
|
struct efm32_usart_device_t *usart;
|
2011-02-17 11:33:15 +08:00
|
|
|
USART_TypeDef *usart_device;
|
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
usart = (struct efm32_usart_device_t *)(dev->user_data);
|
2011-02-17 11:33:15 +08:00
|
|
|
usart_device = ((struct efm32_usart_device_t *)(dev->user_data))->usart_device;
|
2011-05-12 15:19:37 +08:00
|
|
|
|
|
|
|
if (usart->state & USART_STATE_SYNC)
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
2011-05-12 15:19:37 +08:00
|
|
|
/* 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));
|
2011-06-20 09:56:28 +08:00
|
|
|
rt_off_t i;
|
2011-05-12 15:19:37 +08:00
|
|
|
|
|
|
|
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;
|
2011-06-20 09:56:28 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
while ((rt_uint32_t)ptr - (rt_uint32_t)rx_buf < size)
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
2011-05-12 15:19:37 +08:00
|
|
|
/* dummy write */
|
|
|
|
while (!(usart_device->STATUS & USART_STATUS_TXBL));
|
2011-06-20 09:56:28 +08:00
|
|
|
usart_device->TXDATA = (rt_uint32_t)0xff;
|
2011-05-12 15:19:37 +08:00
|
|
|
/* read a byte of data */
|
|
|
|
while (!(usart_device->STATUS & USART_STATUS_RXDATAV));
|
2011-02-17 11:33:15 +08:00
|
|
|
*ptr = usart_device->RXDATA & 0xff;
|
|
|
|
ptr ++;
|
|
|
|
}
|
|
|
|
}
|
2011-05-12 15:19:37 +08:00
|
|
|
else
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
rt_uint32_t flag;
|
|
|
|
|
|
|
|
if (usart->state & USART_STATE_ASYNC_ONLY)
|
|
|
|
{
|
|
|
|
flag = UART_STATUS_RXDATAV;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flag = USART_STATUS_RXDATAV;
|
|
|
|
}
|
2011-05-12 15:19:37 +08:00
|
|
|
ptr = buffer;
|
|
|
|
|
|
|
|
/* polling mode */
|
|
|
|
while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
while (usart_device->STATUS & flag)
|
2011-05-12 15:19:37 +08:00
|
|
|
{
|
|
|
|
*ptr = usart_device->RXDATA & 0xff;
|
|
|
|
ptr ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
read_len = size;
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/* Unlock device */
|
|
|
|
rt_sem_release(usart->lock);
|
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
/* set error code */
|
|
|
|
rt_set_errno(err_code);
|
2011-05-12 15:19:37 +08:00
|
|
|
return read_len;
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @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
|
2011-05-12 15:19:37 +08:00
|
|
|
* Number of written bytes
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
static rt_size_t rt_usart_write (
|
2011-12-16 09:05:50 +08:00
|
|
|
rt_device_t dev,
|
|
|
|
rt_off_t pos,
|
|
|
|
const void* buffer,
|
2011-02-17 11:33:15 +08:00
|
|
|
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);
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
if ((dev->flag & RT_DEVICE_FLAG_DMA_TX) && (size > 2))
|
2011-02-17 11:33:15 +08:00
|
|
|
{ /* DMA mode Tx */
|
|
|
|
struct efm32_usart_dma_mode_t *dma_tx;
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
if (dev->flag & RT_DEVICE_FLAG_STREAM)
|
|
|
|
{
|
2011-06-20 09:56:28 +08:00
|
|
|
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;
|
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2011-07-29 15:24:09 +08:00
|
|
|
(void *)buffer,
|
2011-06-20 09:56:28 +08:00
|
|
|
(rt_uint32_t)(size - 1));
|
2011-02-17 11:33:15 +08:00
|
|
|
|
|
|
|
/* Wait, otherwise the TX buffer is overwrite */
|
2011-07-08 15:13:20 +08:00
|
|
|
// if (usart->state & USART_STATE_CONSOLE)
|
|
|
|
// {
|
2011-02-17 11:33:15 +08:00
|
|
|
while(usart->state & USART_STATE_TX_BUSY);
|
2011-07-08 15:13:20 +08:00
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// while(usart->state & USART_STATE_TX_BUSY)
|
|
|
|
// {
|
|
|
|
// rt_thread_sleep(USART_WAIT_TIME_TX);
|
|
|
|
// }
|
|
|
|
// }
|
2011-12-16 09:05:50 +08:00
|
|
|
// TODO: This function blocks the process
|
2011-02-17 11:33:15 +08:00
|
|
|
write_size = size;
|
|
|
|
}
|
|
|
|
else
|
2011-12-16 09:05:50 +08:00
|
|
|
{ /* polling mode */
|
2011-05-12 15:19:37 +08:00
|
|
|
rt_uint8_t *ptr = (rt_uint8_t *)buffer;
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
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
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
rt_uint32_t flag;
|
|
|
|
|
|
|
|
if (usart->state & USART_STATE_ASYNC_ONLY)
|
|
|
|
{
|
|
|
|
flag = UART_STATUS_TXBL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flag = USART_STATUS_TXBL;
|
|
|
|
}
|
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
/* write data directly */
|
|
|
|
while (size)
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
while (!(usart->usart_device->STATUS & flag));
|
2011-02-17 11:33:15 +08:00
|
|
|
usart->usart_device->TXDATA = (rt_uint32_t)*ptr;
|
|
|
|
++ptr; --size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write_size = (rt_size_t)ptr - (rt_size_t)buffer;
|
|
|
|
}
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/* Unlock device */
|
|
|
|
rt_sem_release(usart->lock);
|
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
/* set error code */
|
|
|
|
rt_set_errno(err_code);
|
|
|
|
return write_size;
|
|
|
|
}
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @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
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
static rt_err_t rt_usart_control (
|
2011-12-16 09:05:50 +08:00
|
|
|
rt_device_t dev,
|
|
|
|
rt_uint8_t cmd,
|
2011-02-17 11:33:15 +08:00
|
|
|
void *args)
|
|
|
|
{
|
2011-02-28 12:47:50 +08:00
|
|
|
RT_ASSERT(dev != RT_NULL);
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
rt_err_t err_code;
|
2011-02-17 11:33:15 +08:00
|
|
|
struct efm32_usart_device_t *usart;
|
|
|
|
|
|
|
|
usart = (struct efm32_usart_device_t *)(dev->user_data);
|
2011-07-29 10:47:37 +08:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
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;
|
|
|
|
|
2011-02-28 12:47:50 +08:00
|
|
|
case RT_DEVICE_CTRL_USART_RBUFFER:
|
2011-02-17 11:33:15 +08:00
|
|
|
/* 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)
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
|
2011-07-29 10:47:37 +08:00
|
|
|
err_code = -RT_ENOMEM;
|
|
|
|
break;
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
2011-03-29 17:41:22 +08:00
|
|
|
// TODO: Is the following line necessary?
|
2011-12-16 09:05:50 +08:00
|
|
|
//rt_memset(int_rx->data_ptr, 0, size);
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
2011-12-16 09:05:50 +08:00
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Allocate new RX buffer */
|
|
|
|
if ((int_rx->data_ptr = rt_malloc(size)) == RT_NULL)
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
usart_debug("USART%d err: no mem for RX BUF\n", usart->unit);
|
2011-07-29 10:47:37 +08:00
|
|
|
err_code = -RT_ENOMEM;
|
|
|
|
break;
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
int_rx->data_size = size;
|
|
|
|
int_rx->read_index = 0;
|
2011-12-16 09:05:50 +08:00
|
|
|
int_rx->save_index = 0;
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/* Unlock device */
|
|
|
|
rt_sem_release(usart->lock);
|
|
|
|
|
|
|
|
return err_code;
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
|
|
|
* USART RX data valid interrupt handler
|
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
* @param[in] dev
|
|
|
|
* Pointer to device descriptor
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
void rt_hw_usart_rx_isr(rt_device_t dev)
|
|
|
|
{
|
|
|
|
struct efm32_usart_device_t *usart;
|
|
|
|
struct efm32_usart_int_mode_t *int_rx;
|
2011-12-16 09:05:50 +08:00
|
|
|
rt_uint32_t flag;
|
2011-02-17 11:33:15 +08:00
|
|
|
|
|
|
|
/* interrupt mode receive */
|
|
|
|
RT_ASSERT(dev->flag & RT_DEVICE_FLAG_INT_RX);
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
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;
|
2011-12-16 09:05:50 +08:00
|
|
|
|
|
|
|
/* 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)
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
|
|
|
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 ++;
|
2011-05-12 15:19:37 +08:00
|
|
|
if (int_rx->save_index >= USART_RX_BUFFER_SIZE)
|
2011-02-17 11:33:15 +08:00
|
|
|
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 ++;
|
2011-05-12 15:19:37 +08:00
|
|
|
if (int_rx->read_index >= USART_RX_BUFFER_SIZE)
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
|
|
|
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 ?
|
2011-05-12 15:19:37 +08:00
|
|
|
USART_RX_BUFFER_SIZE - int_rx->read_index + int_rx->save_index : \
|
2011-02-17 11:33:15 +08:00
|
|
|
int_rx->save_index - int_rx->read_index;
|
|
|
|
|
|
|
|
dev->rx_indicate(dev, rx_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
|
|
|
* DMA for USART TX interrupt handler
|
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
* @param[in] dev
|
|
|
|
* Pointer to device descriptor
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @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
|
2011-12-16 09:05:50 +08:00
|
|
|
* Pointer to USART device descriptor
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Error code
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
rt_err_t rt_hw_usart_register(
|
2011-12-16 09:05:50 +08:00
|
|
|
rt_device_t device,
|
|
|
|
const char *name,
|
|
|
|
rt_uint32_t flag,
|
2011-02-17 11:33:15 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-12-16 09:05:50 +08:00
|
|
|
if (usart->state & USART_STATE_SYNC)
|
|
|
|
{
|
|
|
|
device->type = RT_Device_Class_SPIBUS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
device->type = RT_Device_Class_Char;
|
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
device->rx_indicate = RT_NULL;
|
|
|
|
device->tx_complete = RT_NULL;
|
2011-12-16 09:05:50 +08:00
|
|
|
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;
|
2011-02-17 11:33:15 +08:00
|
|
|
|
|
|
|
/* register a character device */
|
|
|
|
return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
|
|
|
|
}
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
2011-12-16 09:05:50 +08:00
|
|
|
* Initialize the specified USART unit
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
* @param[in] device
|
|
|
|
* Pointer to device descriptor
|
|
|
|
*
|
|
|
|
* @param[in] unitNumber
|
|
|
|
* Unit number
|
|
|
|
*
|
|
|
|
* @param[in] location
|
2011-12-16 09:05:50 +08:00
|
|
|
* Pin location number
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @param[in] flag
|
|
|
|
* Configuration flag
|
|
|
|
*
|
|
|
|
* @param[in] dmaChannel
|
|
|
|
* DMA channel number for TX
|
|
|
|
*
|
|
|
|
* @param[in] console
|
2011-12-16 09:05:50 +08:00
|
|
|
* Indicate if using as console
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @return
|
2011-12-16 09:05:50 +08:00
|
|
|
* Pointer to USART device
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
static struct efm32_usart_device_t *rt_hw_usart_unit_init(
|
|
|
|
rt_device_t device,
|
2011-12-16 09:05:50 +08:00
|
|
|
rt_uint8_t unitNumber,
|
2011-02-17 11:33:15 +08:00
|
|
|
rt_uint8_t location,
|
|
|
|
rt_uint32_t flag,
|
|
|
|
rt_uint32_t dmaChannel,
|
2011-05-12 15:19:37 +08:00
|
|
|
rt_uint8_t config)
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
|
|
|
struct efm32_usart_device_t *usart;
|
|
|
|
struct efm32_usart_dma_mode_t *dma_mode;
|
2011-06-20 09:56:28 +08:00
|
|
|
DMA_CB_TypeDef *callback;
|
2011-02-17 11:33:15 +08:00
|
|
|
CMU_Clock_TypeDef usartClock;
|
|
|
|
rt_uint32_t txDmaSelect;
|
2011-11-29 17:15:10 +08:00
|
|
|
GPIO_Port_TypeDef port_tx, port_rx, port_clk, port_cs;
|
|
|
|
rt_uint32_t pin_tx, pin_rx, pin_clk, pin_cs;
|
2011-02-17 11:33:15 +08:00
|
|
|
efm32_irq_hook_init_t hook;
|
|
|
|
|
2011-06-20 09:56:28 +08:00
|
|
|
do
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
2011-06-20 09:56:28 +08:00
|
|
|
/* Allocate device */
|
|
|
|
usart = rt_malloc(sizeof(struct efm32_usart_device_t));
|
|
|
|
if (usart == RT_NULL)
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
usart_debug("USART%d err: no mem\n", usart->unit);
|
2011-06-20 09:56:28 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-07-29 10:47:37 +08:00
|
|
|
usart->counter = 0;
|
2011-06-20 09:56:28 +08:00
|
|
|
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)
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
usart_debug("USART%d err: no mem for DMA TX\n", usart->unit);
|
2011-06-20 09:56:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
dma_mode->dma_channel = dmaChannel;
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
|
2011-06-20 09:56:28 +08:00
|
|
|
/* Allocate RX */
|
|
|
|
if (flag & RT_DEVICE_FLAG_INT_RX)
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
2011-06-20 09:56:28 +08:00
|
|
|
usart->rx_mode = rt_malloc(sizeof(struct efm32_usart_int_mode_t));
|
|
|
|
if (usart->rx_mode == RT_NULL)
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
usart_debug("USART%d err: no mem for INT RX\n", usart->unit);
|
2011-06-20 09:56:28 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
|
2011-06-20 09:56:28 +08:00
|
|
|
/* Initialization */
|
2011-12-16 09:05:50 +08:00
|
|
|
if ((!(config & USART_STATE_ASYNC_ONLY) && (unitNumber >= USART_COUNT)) || \
|
|
|
|
((config & USART_STATE_ASYNC_ONLY) && (unitNumber >= UART_COUNT)))
|
2011-06-20 09:56:28 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (unitNumber)
|
|
|
|
{
|
|
|
|
case 0:
|
2011-12-16 09:05:50 +08:00
|
|
|
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);
|
|
|
|
}
|
2011-06-20 09:56:28 +08:00
|
|
|
break;
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-06-20 09:56:28 +08:00
|
|
|
case 1:
|
2011-12-16 09:05:50 +08:00
|
|
|
#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);
|
|
|
|
}
|
2011-06-20 09:56:28 +08:00
|
|
|
break;
|
2011-12-16 09:05:50 +08:00
|
|
|
|
2011-06-20 09:56:28 +08:00
|
|
|
case 2:
|
|
|
|
usart->usart_device = USART2;
|
|
|
|
usartClock = (CMU_Clock_TypeDef)cmuClock_USART2;
|
|
|
|
txDmaSelect = DMAREQ_USART2_TXBL;
|
2011-11-29 17:15:10 +08:00
|
|
|
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);
|
2011-06-20 09:56:28 +08:00
|
|
|
break;
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-06-20 09:56:28 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable USART clock */
|
|
|
|
CMU_ClockEnable(usartClock, true);
|
|
|
|
|
|
|
|
/* Config GPIO */
|
2011-05-12 15:19:37 +08:00
|
|
|
GPIO_PinModeSet(
|
2011-11-29 17:15:10 +08:00
|
|
|
port_tx,
|
|
|
|
pin_tx,
|
2011-05-12 15:19:37 +08:00
|
|
|
gpioModePushPull,
|
|
|
|
0);
|
|
|
|
GPIO_PinModeSet(
|
2011-11-29 17:15:10 +08:00
|
|
|
port_rx,
|
|
|
|
pin_rx,
|
2011-06-20 09:56:28 +08:00
|
|
|
gpioModeInputPull,
|
2011-05-12 15:19:37 +08:00
|
|
|
1);
|
2011-06-20 09:56:28 +08:00
|
|
|
if (config & USART_STATE_SYNC)
|
|
|
|
{
|
|
|
|
GPIO_PinModeSet(
|
2011-11-29 17:15:10 +08:00
|
|
|
port_clk,
|
|
|
|
pin_clk,
|
2011-06-20 09:56:28 +08:00
|
|
|
gpioModePushPull,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
if (config & USART_STATE_AUTOCS)
|
|
|
|
{
|
|
|
|
GPIO_PinModeSet(
|
2011-11-29 17:15:10 +08:00
|
|
|
port_cs,
|
|
|
|
pin_cs,
|
2011-06-20 09:56:28 +08:00
|
|
|
gpioModePushPull,
|
|
|
|
1);
|
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-06-20 09:56:28 +08:00
|
|
|
/* Config interrupt and NVIC */
|
|
|
|
if (flag & RT_DEVICE_FLAG_INT_RX)
|
2011-12-16 09:05:50 +08:00
|
|
|
{
|
2011-06-20 09:56:28 +08:00
|
|
|
hook.type = efm32_irq_type_usart;
|
|
|
|
hook.unit = unitNumber * 2 + 1;
|
2011-12-16 09:05:50 +08:00
|
|
|
if (config & USART_STATE_ASYNC_ONLY)
|
|
|
|
{
|
|
|
|
hook.unit += USART_COUNT * 2;
|
|
|
|
}
|
2011-06-20 09:56:28 +08:00
|
|
|
hook.cbFunc = rt_hw_usart_rx_isr;
|
|
|
|
hook.userPtr = device;
|
|
|
|
efm32_irq_hook_register(&hook);
|
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-06-20 09:56:28 +08:00
|
|
|
/* Config DMA */
|
|
|
|
if (flag & RT_DEVICE_FLAG_DMA_TX)
|
2011-05-12 15:19:37 +08:00
|
|
|
{
|
2011-06-20 09:56:28 +08:00
|
|
|
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)
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
usart_debug("USART%d err: no mem for callback\n", usart->unit);
|
2011-06-20 09:56:28 +08:00
|
|
|
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 */
|
2011-12-16 09:05:50 +08:00
|
|
|
chnlCfg.select = txDmaSelect;
|
2011-06-20 09:56:28 +08:00
|
|
|
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;
|
2011-12-16 09:05:50 +08:00
|
|
|
DMA_CfgDescr(dmaChannel, true, &descrCfg);
|
2011-05-12 15:19:37 +08:00
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-06-20 09:56:28 +08:00
|
|
|
/* Init specified USART unit */
|
|
|
|
if (config & USART_STATE_SYNC)
|
2011-05-12 15:19:37 +08:00
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
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;
|
2011-06-20 09:56:28 +08:00
|
|
|
if (config & USART_STATE_MASTER)
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
init_sync.master = true;
|
2011-06-20 09:56:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
init_sync.master = false;
|
2011-06-20 09:56:28 +08:00
|
|
|
}
|
2011-12-16 09:05:50 +08:00
|
|
|
init_sync.msbf = true;
|
|
|
|
init_sync.clockMode = usartClockMode0; /* Clock idle low, sample on rising edge. */
|
|
|
|
USART_InitSync(usart->usart_device, &init_sync);
|
2011-05-12 15:19:37 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-16 09:05:50 +08:00
|
|
|
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);
|
2011-06-20 09:56:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2011-05-12 15:19:37 +08:00
|
|
|
}
|
2011-06-20 09:56:28 +08:00
|
|
|
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);
|
2011-05-12 15:19:37 +08:00
|
|
|
}
|
2011-06-20 09:56:28 +08:00
|
|
|
if (usart->tx_mode)
|
2011-05-12 15:19:37 +08:00
|
|
|
{
|
2011-06-20 09:56:28 +08:00
|
|
|
rt_free(usart->tx_mode);
|
2011-05-12 15:19:37 +08:00
|
|
|
}
|
2011-06-20 09:56:28 +08:00
|
|
|
if (usart)
|
|
|
|
{
|
|
|
|
rt_free(usart);
|
|
|
|
}
|
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
rt_free(usart);
|
|
|
|
}
|
2011-07-29 10:47:37 +08:00
|
|
|
|
2011-12-16 09:05:50 +08:00
|
|
|
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);
|
|
|
|
}
|
2011-06-20 09:56:28 +08:00
|
|
|
return RT_NULL;
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
2011-12-16 09:05:50 +08:00
|
|
|
* Initialize all USART module related hardware and register USART device to
|
2011-07-08 15:13:20 +08:00
|
|
|
* kernel
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
void rt_hw_usart_init(void)
|
|
|
|
{
|
|
|
|
struct efm32_usart_device_t *usart;
|
|
|
|
rt_uint32_t flag;
|
2011-05-12 15:19:37 +08:00
|
|
|
rt_uint8_t config;
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
do
|
|
|
|
{
|
2011-02-17 11:33:15 +08:00
|
|
|
#ifdef RT_USING_USART0
|
2011-07-29 10:47:37 +08:00
|
|
|
config = 0x00;
|
|
|
|
flag = RT_DEVICE_FLAG_RDWR;
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
#ifdef RT_USART0_SYNC_MODE
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_SYNC;
|
2011-05-12 15:19:37 +08:00
|
|
|
#if (RT_USART0_SYNC_MODE != 0x0UL)
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_MASTER;
|
2011-05-12 15:19:37 +08:00
|
|
|
#else
|
2011-07-29 10:47:37 +08:00
|
|
|
flag |= RT_DEVICE_FLAG_INT_RX;
|
2011-05-12 15:19:37 +08:00
|
|
|
#endif
|
|
|
|
#else
|
2011-07-29 10:47:37 +08:00
|
|
|
flag |= RT_DEVICE_FLAG_INT_RX;
|
2011-05-12 15:19:37 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (SPI_AUTOCS_ENABLE & (1 << 0))
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_AUTOCS;
|
2011-05-12 15:19:37 +08:00
|
|
|
#endif
|
|
|
|
|
2011-12-16 09:05:50 +08:00
|
|
|
#if (RT_CONSOLE_DEVICE == EFM_USART0)
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_CONSOLE;
|
|
|
|
flag |= RT_DEVICE_FLAG_STREAM;
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USART0_USING_DMA
|
2011-07-29 10:47:37 +08:00
|
|
|
RT_ASSERT(RT_USART0_USING_DMA < DMA_CHAN_COUNT);
|
|
|
|
flag |= RT_DEVICE_FLAG_DMA_TX;
|
2011-02-17 11:33:15 +08:00
|
|
|
#else
|
2011-12-16 09:05:50 +08:00
|
|
|
#define RT_USART0_USING_DMA EFM32_NO_DMA
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/* Initialize and Register usart0 */
|
|
|
|
if ((usart = rt_hw_usart_unit_init(
|
2011-12-16 09:05:50 +08:00
|
|
|
&usart0_device,
|
|
|
|
0,
|
|
|
|
RT_USING_USART0,
|
|
|
|
flag,
|
|
|
|
RT_USART0_USING_DMA,
|
2011-07-29 10:47:37 +08:00
|
|
|
config)) != RT_NULL)
|
|
|
|
{
|
|
|
|
rt_hw_usart_register(&usart0_device, RT_USART0_NAME, flag, usart);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Initialize lock for usart0 */
|
2011-07-29 15:24:09 +08:00
|
|
|
usart->lock = &usart0_lock;
|
2011-07-29 10:47:37 +08:00
|
|
|
if (rt_sem_init(usart->lock, RT_USART0_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_USART1
|
2011-07-29 10:47:37 +08:00
|
|
|
config = 0x00;
|
|
|
|
flag = RT_DEVICE_FLAG_RDWR;
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
#ifdef RT_USART1_SYNC_MODE
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_SYNC;
|
2011-05-12 15:19:37 +08:00
|
|
|
#if (RT_USART1_SYNC_MODE != 0x0UL)
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_MASTER;
|
2011-05-12 15:19:37 +08:00
|
|
|
#else
|
2011-07-29 10:47:37 +08:00
|
|
|
flag |= RT_DEVICE_FLAG_INT_RX;
|
2011-05-12 15:19:37 +08:00
|
|
|
#endif
|
|
|
|
#else
|
2011-07-29 10:47:37 +08:00
|
|
|
flag |= RT_DEVICE_FLAG_INT_RX;
|
2011-05-12 15:19:37 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (SPI_AUTOCS_ENABLE & (1 << 1))
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_AUTOCS;
|
2011-05-12 15:19:37 +08:00
|
|
|
#endif
|
|
|
|
|
2011-12-16 09:05:50 +08:00
|
|
|
#if (RT_CONSOLE_DEVICE == EFM_USART1)
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_CONSOLE;
|
|
|
|
flag |= RT_DEVICE_FLAG_STREAM;
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USART1_USING_DMA
|
2011-07-29 10:47:37 +08:00
|
|
|
RT_ASSERT(RT_USART1_USING_DMA < DMA_CHAN_COUNT);
|
|
|
|
flag |= RT_DEVICE_FLAG_DMA_TX;
|
2011-02-17 11:33:15 +08:00
|
|
|
#else
|
2011-12-16 09:05:50 +08:00
|
|
|
|
|
|
|
#define RT_USART1_USING_DMA EFM32_NO_DMA
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
2011-07-29 10:47:37 +08:00
|
|
|
|
|
|
|
/* Initialize and Register usart1 */
|
|
|
|
if ((usart = rt_hw_usart_unit_init(
|
2011-12-16 09:05:50 +08:00
|
|
|
&usart1_device,
|
|
|
|
1,
|
|
|
|
RT_USING_USART1,
|
|
|
|
flag,
|
|
|
|
RT_USART1_USING_DMA,
|
2011-07-29 10:47:37 +08:00
|
|
|
config)) != RT_NULL)
|
|
|
|
{
|
|
|
|
rt_hw_usart_register(&usart1_device, RT_USART1_NAME, flag, usart);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Initialize lock for usart1 */
|
2011-07-29 15:24:09 +08:00
|
|
|
usart->lock = &usart1_lock;
|
2011-07-29 10:47:37 +08:00
|
|
|
if (rt_sem_init(usart->lock, RT_USART1_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_USART2
|
2011-07-29 10:47:37 +08:00
|
|
|
config = 0x00;
|
|
|
|
flag = RT_DEVICE_FLAG_RDWR;
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
#ifdef RT_USART2_SYNC_MODE
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_SYNC;
|
2011-05-12 15:19:37 +08:00
|
|
|
#if (RT_USART2_SYNC_MODE != 0x0UL)
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_MASTER;
|
2011-05-12 15:19:37 +08:00
|
|
|
#else
|
2011-07-29 10:47:37 +08:00
|
|
|
flag |= RT_DEVICE_FLAG_INT_RX;
|
2011-05-12 15:19:37 +08:00
|
|
|
#endif
|
2011-02-17 11:33:15 +08:00
|
|
|
#else
|
2011-07-29 10:47:37 +08:00
|
|
|
flag |= RT_DEVICE_FLAG_INT_RX;
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
#if (SPI_AUTOCS_ENABLE & (1 << 2))
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_AUTOCS;
|
2011-05-12 15:19:37 +08:00
|
|
|
#endif
|
|
|
|
|
2011-12-16 09:05:50 +08:00
|
|
|
#if (RT_CONSOLE_DEVICE == EFM_USART2)
|
2011-07-29 10:47:37 +08:00
|
|
|
config |= USART_STATE_CONSOLE;
|
|
|
|
flag |= RT_DEVICE_FLAG_STREAM;
|
2011-05-12 15:19:37 +08:00
|
|
|
#endif
|
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
#ifdef RT_USART2_USING_DMA
|
2011-07-29 10:47:37 +08:00
|
|
|
RT_ASSERT(RT_USART2_USING_DMA < DMA_CHAN_COUNT);
|
|
|
|
flag |= RT_DEVICE_FLAG_DMA_TX;
|
2011-02-17 11:33:15 +08:00
|
|
|
|
|
|
|
#else
|
2011-12-16 09:05:50 +08:00
|
|
|
|
|
|
|
#define RT_USART2_USING_DMA EFM32_NO_DMA
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/* Initialize and Register usart2 */
|
|
|
|
if ((usart = rt_hw_usart_unit_init(
|
2011-12-16 09:05:50 +08:00
|
|
|
&usart2_device,
|
|
|
|
2,
|
|
|
|
RT_USING_USART2,
|
|
|
|
flag,
|
|
|
|
RT_USART2_USING_DMA,
|
2011-07-29 10:47:37 +08:00
|
|
|
config)) != RT_NULL)
|
|
|
|
{
|
|
|
|
rt_hw_usart_register(&usart2_device, RT_USART2_NAME, flag, usart);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Initialize lock for usart2 */
|
2011-07-29 15:24:09 +08:00
|
|
|
usart->lock = &usart2_lock;
|
2011-07-29 10:47:37 +08:00
|
|
|
if (rt_sem_init(usart->lock, RT_USART2_NAME, 1, RT_IPC_FLAG_FIFO) != RT_EOK)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-02-17 11:33:15 +08:00
|
|
|
#endif
|
2011-07-29 10:47:37 +08:00
|
|
|
|
2011-12-16 09:05:50 +08:00
|
|
|
#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
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
usart_debug("USART: H/W init OK!\n");
|
|
|
|
return;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
rt_kprintf("USART: H/W init failed!\n");
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
|
2011-12-16 09:05:50 +08:00
|
|
|
#endif /* (defined(RT_USING_USART0) || defined(RT_USING_USART1) || \
|
|
|
|
defined(RT_USING_USART2) || defined(RT_USING_UART0) || \
|
|
|
|
defined(RT_USING_UART1)) */
|
2011-07-08 15:13:20 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @}
|
2011-07-08 15:13:20 +08:00
|
|
|
******************************************************************************/
|