2022-04-14 10:54:53 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Email Notes
|
|
|
|
* 2019-07-16 Kevin.Liu kevin.liu.mchp@gmail.com First Release
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
|
|
|
#include <atmel_start.h>
|
|
|
|
|
|
|
|
/* SAM MCU serial device */
|
|
|
|
static struct rt_serial_device sam_serial;
|
|
|
|
|
2022-04-19 14:32:02 +08:00
|
|
|
static void serial_rxcallback(const struct usart_async_descriptor *const io_descr)
|
|
|
|
{
|
|
|
|
(void)io_descr;
|
|
|
|
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
/* Notify Serial driver to process RX data */
|
|
|
|
rt_hw_serial_isr(&sam_serial, RT_SERIAL_EVENT_RX_IND);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_txcallback(const struct usart_async_descriptor *const io_descr)
|
|
|
|
{
|
|
|
|
(void)io_descr;
|
|
|
|
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
|
|
|
|
/* Notify Serial driver to process TX done event */
|
|
|
|
rt_hw_serial_isr(&sam_serial, RT_SERIAL_EVENT_TX_DONE);
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:54:53 +08:00
|
|
|
/**
|
|
|
|
* @brief Configure serial port
|
|
|
|
*
|
|
|
|
* This function will configure UART baudrate, parity and so on.
|
|
|
|
*
|
|
|
|
* @return RT_EOK.
|
|
|
|
*/
|
|
|
|
static rt_err_t serial_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
|
|
|
|
{
|
2022-04-19 14:32:02 +08:00
|
|
|
struct usart_async_descriptor* desc;
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
|
2022-04-19 14:32:02 +08:00
|
|
|
desc = (struct usart_async_descriptor *)serial->parent.user_data;
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
RT_ASSERT(desc != RT_NULL);
|
|
|
|
RT_ASSERT(cfg != RT_NULL);
|
|
|
|
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_disable(desc);
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
/* Set baudrate */
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_baud_rate(desc, (const uint32_t)cfg->baud_rate);
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
/* Set stop bit */
|
|
|
|
if (cfg->stop_bits == STOP_BITS_1)
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_stopbits(desc, USART_STOP_BITS_ONE);
|
2022-04-14 10:54:53 +08:00
|
|
|
else if (cfg->stop_bits == STOP_BITS_2)
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_stopbits(desc, USART_STOP_BITS_TWO);
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
if (cfg->bit_order == BIT_ORDER_LSB)
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_data_order(desc, USART_DATA_ORDER_LSB);
|
2022-04-14 10:54:53 +08:00
|
|
|
else if (cfg->bit_order == BIT_ORDER_MSB)
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_data_order(desc, USART_DATA_ORDER_MSB);
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
/* Set character size */
|
|
|
|
switch (cfg->data_bits)
|
|
|
|
{
|
|
|
|
case DATA_BITS_5:
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_character_size(desc, USART_CHARACTER_SIZE_5BITS);
|
2022-04-14 10:54:53 +08:00
|
|
|
break;
|
|
|
|
case DATA_BITS_6:
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_character_size(desc, USART_CHARACTER_SIZE_6BITS);
|
2022-04-14 10:54:53 +08:00
|
|
|
break;
|
|
|
|
case DATA_BITS_7:
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_character_size(desc, USART_CHARACTER_SIZE_7BITS);
|
2022-04-14 10:54:53 +08:00
|
|
|
break;
|
|
|
|
case DATA_BITS_8:
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_character_size(desc, USART_CHARACTER_SIZE_8BITS);
|
2022-04-14 10:54:53 +08:00
|
|
|
break;
|
|
|
|
case DATA_BITS_9:
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_character_size(desc, USART_CHARACTER_SIZE_9BITS);
|
2022-04-14 10:54:53 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg->parity == PARITY_NONE)
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_parity(desc, USART_PARITY_NONE);
|
2022-04-14 10:54:53 +08:00
|
|
|
else if (cfg->parity == PARITY_ODD)
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_parity(desc, USART_PARITY_ODD);
|
2022-04-14 10:54:53 +08:00
|
|
|
else if (cfg->parity == PARITY_EVEN)
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_set_parity(desc, USART_PARITY_EVEN);
|
2022-04-14 10:54:53 +08:00
|
|
|
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_enable(desc);
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Control serial port
|
|
|
|
*
|
|
|
|
* This function provide UART enable/disable control.
|
|
|
|
*
|
|
|
|
* @return RT_EOK.
|
|
|
|
*/
|
|
|
|
static rt_err_t serial_control(struct rt_serial_device *serial, int cmd, void *arg)
|
|
|
|
{
|
2022-04-19 14:32:02 +08:00
|
|
|
struct usart_async_descriptor* desc;
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
2022-04-19 14:32:02 +08:00
|
|
|
desc = (struct usart_async_descriptor *)serial->parent.user_data;
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
RT_ASSERT(desc != RT_NULL);
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* disable interrupt */
|
|
|
|
case RT_DEVICE_CTRL_CLR_INT:
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_disable(desc);
|
2022-04-14 10:54:53 +08:00
|
|
|
break;
|
|
|
|
/* enable interrupt */
|
|
|
|
case RT_DEVICE_CTRL_SET_INT:
|
2022-04-19 14:32:02 +08:00
|
|
|
usart_async_enable(desc);
|
2022-04-14 10:54:53 +08:00
|
|
|
break;
|
|
|
|
/* UART config */
|
|
|
|
case RT_DEVICE_CTRL_CONFIG :
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Serial sends a char
|
|
|
|
*
|
|
|
|
* This function will send a char to the UART
|
|
|
|
*
|
|
|
|
* @return 1.
|
|
|
|
*/
|
|
|
|
static int serial_putc(struct rt_serial_device *serial, char c)
|
|
|
|
{
|
2022-04-19 14:32:02 +08:00
|
|
|
struct usart_async_descriptor* desc;
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
2022-04-19 14:32:02 +08:00
|
|
|
desc = (struct usart_async_descriptor *)serial->parent.user_data;
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
RT_ASSERT(desc != RT_NULL);
|
|
|
|
|
2022-04-19 14:32:02 +08:00
|
|
|
while (usart_async_is_tx_empty(desc) == 0);
|
|
|
|
_usart_async_write_byte(&TARGET_IO.device, (uint8_t)c);
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Serial gets a char
|
|
|
|
*
|
|
|
|
* This function will get a char from the UART
|
|
|
|
*
|
|
|
|
* @return received char character or -1 if no char received.
|
|
|
|
*/
|
|
|
|
static int serial_getc(struct rt_serial_device *serial)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
int ch;
|
2022-04-19 14:32:02 +08:00
|
|
|
struct usart_async_descriptor* desc;
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
2022-04-19 14:32:02 +08:00
|
|
|
desc = (struct usart_async_descriptor *)serial->parent.user_data;
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
RT_ASSERT(desc != RT_NULL);
|
|
|
|
|
|
|
|
ch = -1;
|
2022-04-19 14:32:02 +08:00
|
|
|
if (usart_async_is_rx_not_empty(desc))
|
2022-04-14 10:54:53 +08:00
|
|
|
{
|
2022-04-19 14:32:02 +08:00
|
|
|
io_read(&desc->io, (uint8_t *)&c, 1);
|
2022-04-14 10:54:53 +08:00
|
|
|
ch = c & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_uart_ops sam_serial_ops =
|
|
|
|
{
|
|
|
|
serial_configure,
|
|
|
|
serial_control,
|
|
|
|
serial_putc,
|
|
|
|
serial_getc,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize the UART
|
|
|
|
*
|
|
|
|
* This function initialize the UART
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*/
|
|
|
|
int rt_hw_uart_init(void)
|
|
|
|
{
|
|
|
|
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
|
|
|
|
|
|
|
|
sam_serial.ops = &sam_serial_ops;
|
|
|
|
sam_serial.config = config;
|
|
|
|
sam_serial.serial_rx = RT_NULL;
|
|
|
|
sam_serial.serial_rx = RT_NULL;
|
2022-04-19 14:32:02 +08:00
|
|
|
rt_hw_serial_register(&sam_serial, RT_CONSOLE_DEVICE_NAME,
|
|
|
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX |
|
|
|
|
RT_DEVICE_FLAG_INT_TX, (void *)&TARGET_IO);
|
|
|
|
|
|
|
|
usart_async_register_callback(&TARGET_IO, USART_ASYNC_TXC_CB, serial_txcallback);
|
|
|
|
usart_async_register_callback(&TARGET_IO, USART_ASYNC_RXC_CB, serial_rxcallback);
|
2022-04-14 10:54:53 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*@}*/
|