2021-01-28 19:59:31 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2018, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2023-10-02 21:28:09 +08:00
|
|
|
* 2023-09-26 1ridic Integrate with RT-Thread driver framework.
|
2021-01-28 19:59:31 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include "board.h"
|
|
|
|
#include "drv_uart.h"
|
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
#ifdef RT_USING_SERIAL
|
|
|
|
|
2021-01-28 19:59:31 +08:00
|
|
|
#include "hardware/uart.h"
|
|
|
|
#include "hardware/irq.h"
|
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
#if !defined(BSP_USING_UART0) && !defined(BSP_USING_UART1)
|
|
|
|
#error "Please define at least one BSP_USING_UARTx"
|
|
|
|
/* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
|
|
|
|
#endif
|
2021-01-28 19:59:31 +08:00
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
#ifdef BSP_USING_UART0
|
|
|
|
void pico_uart0_isr(void);
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
void pico_uart1_isr(void);
|
|
|
|
#endif
|
2021-01-28 19:59:31 +08:00
|
|
|
|
|
|
|
struct pico_uart_dev
|
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
rt_serial_t parent;
|
|
|
|
uart_inst_t *instance;
|
2021-01-28 19:59:31 +08:00
|
|
|
rt_uint32_t irqno;
|
2023-10-02 21:28:09 +08:00
|
|
|
rt_uint32_t tx_pin;
|
|
|
|
rt_uint32_t rx_pin;
|
|
|
|
void (*uart_isr)(void);
|
2021-01-28 19:59:31 +08:00
|
|
|
};
|
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
static struct pico_uart_dev uart_dev[] =
|
2021-01-28 19:59:31 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
#ifdef BSP_USING_UART0
|
2021-01-28 19:59:31 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
.instance = uart0,
|
|
|
|
.irqno = UART0_IRQ,
|
|
|
|
.tx_pin = BSP_UART0_TX_PIN,
|
|
|
|
.rx_pin = BSP_UART0_RX_PIN,
|
|
|
|
.uart_isr = pico_uart0_isr,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
{
|
|
|
|
.instance = uart1,
|
|
|
|
.irqno = UART1_IRQ,
|
|
|
|
.tx_pin = BSP_UART1_TX_PIN,
|
|
|
|
.rx_pin = BSP_UART1_RX_PIN,
|
|
|
|
.uart_isr = pico_uart1_isr,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
};
|
2021-01-28 19:59:31 +08:00
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
enum
|
2021-01-28 19:59:31 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
#ifdef BSP_USING_UART0
|
|
|
|
UART0_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
UART1_INDEX,
|
|
|
|
#endif
|
|
|
|
};
|
2021-01-28 19:59:31 +08:00
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
#ifdef BSP_USING_UART0
|
|
|
|
void pico_uart0_isr(void)
|
2021-01-28 19:59:31 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
rt_interrupt_enter();
|
|
|
|
/* read interrupt status and clear it */
|
|
|
|
if (uart_is_readable(uart0)) /* rx ind */
|
2021-02-24 11:00:51 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
rt_hw_serial_isr(&uart_dev[UART0_INDEX].parent, RT_SERIAL_EVENT_RX_IND);
|
2021-02-24 11:00:51 +08:00
|
|
|
}
|
2021-01-28 19:59:31 +08:00
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
rt_interrupt_leave();
|
2021-01-28 19:59:31 +08:00
|
|
|
}
|
2023-10-02 21:28:09 +08:00
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
void pico_uart1_isr(void)
|
2021-01-28 19:59:31 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
rt_interrupt_enter();
|
|
|
|
/* read interrupt status and clear it */
|
|
|
|
if (uart_is_readable(uart1)) /* rx ind */
|
2021-01-28 19:59:31 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
rt_hw_serial_isr(&uart_dev[UART1_INDEX].parent, RT_SERIAL_EVENT_RX_IND);
|
2021-01-28 19:59:31 +08:00
|
|
|
}
|
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
rt_interrupt_leave();
|
2021-01-28 19:59:31 +08:00
|
|
|
}
|
2023-10-02 21:28:09 +08:00
|
|
|
#endif
|
2021-01-28 19:59:31 +08:00
|
|
|
|
|
|
|
/*
|
2023-10-02 21:28:09 +08:00
|
|
|
* UART interface
|
2021-01-28 19:59:31 +08:00
|
|
|
*/
|
2023-10-02 21:28:09 +08:00
|
|
|
static rt_err_t pico_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
|
2021-01-28 19:59:31 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
struct pico_uart_dev *uart = RT_NULL;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
RT_ASSERT(cfg != RT_NULL);
|
|
|
|
uart = rt_container_of(serial, struct pico_uart_dev, parent);
|
2021-01-28 19:59:31 +08:00
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
uart_init(uart->instance, cfg->baud_rate);
|
2021-01-28 19:59:31 +08:00
|
|
|
|
|
|
|
// Set the TX and RX pins by using the function select on the GPIO
|
|
|
|
// Set datasheet for more information on function select
|
2023-10-02 21:28:09 +08:00
|
|
|
gpio_set_function(uart->rx_pin, GPIO_FUNC_UART);
|
|
|
|
gpio_set_function(uart->tx_pin, GPIO_FUNC_UART);
|
2021-01-28 19:59:31 +08:00
|
|
|
|
2023-09-30 14:36:09 +08:00
|
|
|
// Set UART flow control CTS/RTS
|
|
|
|
if (cfg->flowcontrol == RT_SERIAL_FLOWCONTROL_CTSRTS)
|
|
|
|
uart_set_hw_flow(uart->instance, true, true);
|
|
|
|
else
|
|
|
|
uart_set_hw_flow(uart->instance, false, false);
|
2021-01-28 19:59:31 +08:00
|
|
|
|
|
|
|
// Set our data format
|
2023-09-30 14:36:09 +08:00
|
|
|
uart_parity_t uart_parity = UART_PARITY_NONE;
|
|
|
|
if (cfg->parity == PARITY_ODD)
|
|
|
|
uart_parity = UART_PARITY_ODD;
|
|
|
|
else if (cfg->parity == PARITY_EVEN)
|
|
|
|
uart_parity = UART_PARITY_EVEN;
|
|
|
|
uart_set_format(uart->instance, cfg->data_bits, cfg->stop_bits, uart_parity);
|
2021-01-28 19:59:31 +08:00
|
|
|
|
|
|
|
// Turn off FIFO's - we want to do this character by character
|
2023-10-02 21:28:09 +08:00
|
|
|
uart_set_fifo_enabled(uart->instance, false);
|
2021-03-24 19:56:48 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
static rt_err_t pico_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
|
2021-03-24 19:56:48 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
struct pico_uart_dev *uart = RT_NULL;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = rt_container_of(serial, struct pico_uart_dev, parent);
|
|
|
|
|
2021-03-24 19:56:48 +08:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* enable interrupt */
|
|
|
|
case RT_DEVICE_CTRL_SET_INT:
|
|
|
|
// Set up a RX interrupt
|
|
|
|
// We need to set up the handler first
|
|
|
|
// And set up and enable the interrupt handlers
|
2023-10-02 21:28:09 +08:00
|
|
|
irq_set_exclusive_handler(uart->irqno, uart->uart_isr);
|
|
|
|
irq_set_enabled(uart->irqno, true);
|
2021-03-24 19:56:48 +08:00
|
|
|
|
|
|
|
// Now enable the UART to send interrupts - RX only
|
2023-10-02 21:28:09 +08:00
|
|
|
uart_set_irq_enables(uart->instance, true, false);
|
2021-03-24 19:56:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
static int pico_uart_putc(struct rt_serial_device *serial, char c)
|
2021-03-24 19:56:48 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
struct pico_uart_dev *uart = RT_NULL;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = rt_container_of(serial, struct pico_uart_dev, parent);
|
|
|
|
uart_putc_raw(uart->instance, c);
|
2021-03-24 19:56:48 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
static int pico_uart_getc(struct rt_serial_device *serial)
|
2021-03-24 19:56:48 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
struct pico_uart_dev *uart = RT_NULL;
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = rt_container_of(serial, struct pico_uart_dev, parent);
|
|
|
|
|
2021-03-24 19:56:48 +08:00
|
|
|
int ch;
|
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
if (uart_is_readable(uart->instance))
|
2021-03-24 19:56:48 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
ch = uart_get_hw(uart->instance)->dr;
|
2021-03-24 19:56:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
ch = -1;
|
2021-03-24 19:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
const static struct rt_uart_ops _uart_ops =
|
2021-03-24 19:56:48 +08:00
|
|
|
{
|
2023-10-02 21:28:09 +08:00
|
|
|
pico_uart_configure,
|
|
|
|
pico_uart_control,
|
|
|
|
pico_uart_putc,
|
|
|
|
pico_uart_getc,
|
2021-03-24 19:56:48 +08:00
|
|
|
RT_NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UART Initiation
|
|
|
|
*/
|
2023-10-02 21:28:09 +08:00
|
|
|
int rt_hw_uart_init(void)
|
2021-03-24 19:56:48 +08:00
|
|
|
{
|
|
|
|
rt_err_t ret = RT_EOK;
|
|
|
|
|
|
|
|
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
|
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
#ifdef BSP_USING_UART0
|
|
|
|
uart_dev[UART0_INDEX].parent.ops = &_uart_ops;
|
|
|
|
uart_dev[UART0_INDEX].parent.config = config;
|
2021-03-24 19:56:48 +08:00
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
ret = rt_hw_serial_register(&uart_dev[UART0_INDEX].parent,
|
|
|
|
"uart0",
|
|
|
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
|
|
|
|
&uart_dev[UART0_INDEX]);
|
|
|
|
RT_ASSERT(ret == RT_EOK);
|
|
|
|
#endif
|
2021-03-24 19:56:48 +08:00
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
#ifdef BSP_USING_UART1
|
|
|
|
uart_dev[UART1_INDEX].parent.ops = &_uart_ops;
|
|
|
|
uart_dev[UART1_INDEX].parent.config = config;
|
2021-03-24 19:56:48 +08:00
|
|
|
|
2023-10-02 21:28:09 +08:00
|
|
|
ret = rt_hw_serial_register(&uart_dev[UART1_INDEX].parent,
|
2021-03-24 19:56:48 +08:00
|
|
|
"uart1",
|
|
|
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
|
2023-10-02 21:28:09 +08:00
|
|
|
&uart_dev[UART1_INDEX]);
|
|
|
|
RT_ASSERT(ret == RT_EOK);
|
|
|
|
#endif
|
2021-03-24 19:56:48 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2023-10-02 21:28:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
#endif /* RT_USING_SERIAL */
|