Skip to content

Commit f74d7f1

Browse files
committed
[bsp] modify the uart_driver to fit the new rt-thread serial device driver framework. Modify the template.uvproj for auto generate MDK project.
1 parent 3a1c1c4 commit f74d7f1

File tree

5 files changed

+152
-958
lines changed

5 files changed

+152
-958
lines changed

bsp/tm4c129x/applications/application.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
void rt_init_thread_entry(void *parameter)
2121
{
2222
/* Initialization RT-Thread Components */
23-
#ifdef RT_USING_COMPONENTS_INIT
2423
rt_components_init();
24+
#ifdef RT_USING_FINSH
25+
finsh_set_device(RT_CONSOLE_DEVICE_NAME);
2526
#endif
2627
}
2728

bsp/tm4c129x/applications/board.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515

1616
#include <rthw.h>
1717
#include <rtthread.h>
18-
#include <components.h>
1918

2019
#include "board.h"
2120
#include "drv_uart.h"
22-
#include "interrupt.h"
23-
#include "sysctl.h"
24-
#include "systick.h"
25-
#include "fpu.h"
21+
22+
#include "driverlib/interrupt.h"
23+
#include "driverlib/sysctl.h"
24+
#include "driverlib/systick.h"
25+
#include "driverlib/fpu.h"
2626
#include "driverlib/rom_map.h"
2727

2828
#define SYS_CLOCK_DEFAULT 120000000
@@ -56,16 +56,15 @@ void SysTick_Handler(void)
5656
extern void PendSV_Handler(void);
5757
extern void HardFault_Handler(void);
5858

59-
6059
/**
6160
* This function will initial LPC40xx board.
6261
*/
6362
void rt_hw_board_init()
6463
{
6564
IntRegister(FAULT_HARD, HardFault_Handler);
66-
IntRegister(FAULT_PENDSV, PendSV_Handler);
67-
IntRegister(FAULT_SYSTICK, SysTick_Handler);
68-
65+
IntRegister(FAULT_PENDSV, PendSV_Handler);
66+
IntRegister(FAULT_SYSTICK, SysTick_Handler);
67+
6968
//
7069
// Enable lazy stacking for interrupt handlers. This allows floating-point
7170
// instructions to be used within interrupt handlers, but at the expense of
@@ -78,23 +77,24 @@ void rt_hw_board_init()
7877
// TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
7978
// crystal on your board.
8079
//
81-
SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480),
82-
SYS_CLOCK_DEFAULT);
80+
SysClock = MAP_SysCtlClockFreqSet(
81+
(SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480),
82+
SYS_CLOCK_DEFAULT);
8383

84-
MAP_SysTickDisable();
84+
MAP_SysTickDisable();
8585
MAP_SysTickPeriodSet(SysClock/ RT_TICK_PER_SECOND - 1);
8686
MAP_SysTickIntEnable();
8787
MAP_SysTickEnable();
8888

89-
9089
/* set pend exception priority */
91-
//IntPrioritySet(FAULT_PENDSV, (1 << __NVIC_PRIO_BITS) - 1);
92-
/*init uart device*/
93-
90+
IntPrioritySet(FAULT_PENDSV, (1 << 5) - 1);
91+
92+
/*init uart device*/
9493
rt_hw_uart_init();
94+
//redirect RTT stdio to CONSOLE device
9595
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
9696
//
9797
// Enable interrupts to the processor.
9898
//
99-
MAP_IntMasterEnable();
99+
MAP_IntMasterEnable();
100100
}

bsp/tm4c129x/drivers/drv_uart.c

Lines changed: 83 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,83 @@
2020
#include "board.h"
2121
//#include <components.h>
2222

23-
#include "sysctl.h"
24-
#include "gpio.h"
25-
#include "uart.h"
26-
#include "hw_memmap.h"
27-
#include "pin_map.h"
28-
#include "interrupt.h"
29-
#include "rom.h"
30-
#include "rom_map.h"
23+
#include "inc/hw_memmap.h"
24+
#include "driverlib/sysctl.h"
25+
#include "driverlib/gpio.h"
26+
#include "driverlib/uart.h"
27+
#include "driverlib/pin_map.h"
28+
#include "driverlib/interrupt.h"
29+
#include "driverlib/rom_map.h"
3130
typedef struct hw_uart_device
3231
{
3332
uint32_t hw_base; // base address
3433
}hw_uart_t;
3534

36-
#define GetHwUartPtr(serial) ((hw_uart_t*)(serial->parent.user_data))
35+
#define mUartGetHwPtr(serial) ((hw_uart_t*)(serial->parent.user_data))
3736

3837
static rt_err_t hw_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
3938
{
39+
uint32_t config;
4040
hw_uart_t* uart;
4141
RT_ASSERT(serial != RT_NULL);
42-
uart = GetHwUartPtr(serial);
42+
uart = mUartGetHwPtr(serial);
43+
4344
MAP_UARTDisable(uart->hw_base);
44-
/* Initialize UART Configuration parameter structure to default state:
45-
* Baudrate = 115200 bps
46-
* 8 data bit
47-
* 1 Stop bit
48-
* None parity
49-
*/
50-
// Initialize UART0 peripheral with given to corresponding parameter
51-
MAP_UARTConfigSetExpClk(uart->hw_base, SysClock, cfg->baud_rate,
52-
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
53-
MAP_UARTFIFOEnable(uart->hw_base);
54-
55-
//
45+
// build UART Configuration parameter structure
46+
switch(cfg->data_bits)
47+
{
48+
case DATA_BITS_9:
49+
// enable 9bit address mode and set DATA_BIT_8
50+
MAP_UART9BitEnable(uart->hw_base);
51+
case DATA_BITS_8:
52+
config |= UART_CONFIG_WLEN_8;
53+
break;
54+
case DATA_BITS_7:
55+
config |= UART_CONFIG_WLEN_7;
56+
break;
57+
case DATA_BITS_6:
58+
config |= UART_CONFIG_WLEN_6;
59+
break;
60+
case DATA_BITS_5:
61+
config |= UART_CONFIG_WLEN_5;
62+
break;
63+
default:
64+
RT_ASSERT(0);
65+
break;
66+
}
67+
switch(cfg->parity)
68+
{
69+
case PARITY_ODD:
70+
config |= UART_CONFIG_PAR_ODD;
71+
break;
72+
case PARITY_EVEN:
73+
config |= UART_CONFIG_PAR_EVEN;
74+
break;
75+
case PARITY_NONE:
76+
config |= UART_CONFIG_PAR_NONE;
77+
break;
78+
default:
79+
RT_ASSERT(0);
80+
break;
81+
}
82+
switch(cfg->stop_bits)
83+
{
84+
case STOP_BITS_1:
85+
config |= UART_CONFIG_STOP_ONE;
86+
break;
87+
case STOP_BITS_2:
88+
config |= UART_CONFIG_STOP_TWO;
89+
break;
90+
default:
91+
RT_ASSERT(0);
92+
break;
93+
}
94+
95+
// Initialize UART0 peripheral with given to corresponding parameter
96+
MAP_UARTConfigSetExpClk(uart->hw_base, SysClock, cfg->baud_rate, config);
97+
MAP_UARTFIFOEnable(uart->hw_base);
98+
5699
// Enable the UART.
57-
//
58100
MAP_UARTEnable(uart->hw_base);
59101
return RT_EOK;
60102
}
@@ -63,7 +105,7 @@ static rt_err_t hw_control(struct rt_serial_device *serial, int cmd, void *arg)
63105
{
64106
hw_uart_t* uart;
65107
RT_ASSERT(serial != RT_NULL);
66-
uart = GetHwUartPtr(serial);
108+
uart = mUartGetHwPtr(serial);
67109

68110
switch (cmd)
69111
{
@@ -84,7 +126,7 @@ static int hw_putc(struct rt_serial_device *serial, char c)
84126
{
85127
hw_uart_t* uart;
86128
RT_ASSERT(serial != RT_NULL);
87-
uart = GetHwUartPtr(serial);
129+
uart = mUartGetHwPtr(serial);
88130

89131
MAP_UARTCharPut(uart->hw_base, *((uint8_t *)&c));
90132
return 1;
@@ -94,7 +136,7 @@ static int hw_getc(struct rt_serial_device *serial)
94136
{
95137
hw_uart_t* uart;
96138
RT_ASSERT(serial != RT_NULL);
97-
uart = GetHwUartPtr(serial);
139+
uart = mUartGetHwPtr(serial);
98140

99141
return MAP_UARTCharGetNonBlocking(uart->hw_base);
100142
}
@@ -110,29 +152,27 @@ static const struct rt_uart_ops hw_uart_ops =
110152
#if defined(RT_USING_UART0)
111153
/* UART0 device driver structure */
112154
struct rt_serial_device serial0;
113-
struct serial_ringbuffer uart0_int_rx_buf;
114155
hw_uart_t uart0 =
115156
{
116157
UART0_BASE,
117158
};
118159

119160
void UART0_IRQHandler(void)
120161
{
121-
uint32_t intsrc;
162+
uint32_t intsrc;
122163
hw_uart_t *uart = &uart0;
123164

124165
/* enter interrupt */
125166
rt_interrupt_enter();
126167

127168
/* Determine the interrupt source */
128-
intsrc = UARTIntStatus(uart->hw_base, true);
169+
intsrc = MAP_UARTIntStatus(uart->hw_base, true);
129170

130171
// Receive Data Available or Character time-out
131172
if (intsrc & (UART_INT_RX | UART_INT_RT))
132173
{
133-
UARTIntClear(UART0_BASE, intsrc);
134-
rt_hw_serial_isr(&serial0);
135-
174+
MAP_UARTIntClear(uart->hw_base, intsrc);
175+
rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND);
136176
}
137177

138178
/* leave interrupt */
@@ -144,57 +184,38 @@ int rt_hw_uart_init(void)
144184
{
145185
hw_uart_t* uart;
146186
struct serial_configure config;
147-
148-
#ifdef RT_USING_UART0
149-
uart = &uart0;
187+
150188
config.baud_rate = BAUD_RATE_115200;
151189
config.bit_order = BIT_ORDER_LSB;
152190
config.data_bits = DATA_BITS_8;
153191
config.parity = PARITY_NONE;
154192
config.stop_bits = STOP_BITS_1;
155193
config.invert = NRZ_NORMAL;
156-
194+
config.bufsz = RT_SERIAL_RB_BUFSZ;
195+
196+
#ifdef RT_USING_UART0
197+
uart = &uart0;
157198
serial0.ops = &hw_uart_ops;
158-
serial0.int_rx = &uart0_int_rx_buf;
159199
serial0.config = config;
160200

161-
//
162-
// Enable the peripherals used by this example.
163-
// The UART itself needs to be enabled, as well as the GPIO port
164-
// containing the pins that will be used.
165-
//
166-
167201
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
168-
169-
//
170-
// Configure the GPIO pin muxing for the UART function.
171-
// This is only necessary if your part supports GPIO pin function muxing.
172-
// Study the data sheet to see which functions are allocated per pin.
173-
// TODO: change this to select the port/pin you are using
174-
//
175202
MAP_GPIOPinConfigure(GPIO_PA0_U0RX);
176203
MAP_GPIOPinConfigure(GPIO_PA1_U0TX);
177-
178-
//
179-
// Since GPIO A0 and A1 are used for the UART function, they must be
180-
// configured for use as a peripheral function (instead of GPIO).
181-
// TODO: change this to match the port/pin you are using
182-
//
204+
183205
MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
184-
185206
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
186207

187208
/* preemption = 1, sub-priority = 1 */
188-
//IntPrioritySet(INT_UART0, ((0x01 << 3) | 0x01));
209+
IntPrioritySet(INT_UART0, ((0x01 << 5) | 0x01));
189210

190211
/* Enable Interrupt for UART channel */
191-
UARTIntRegister(uart->hw_base, UART0_IRQHandler);
192-
MAP_IntEnable(INT_UART0);
193-
MAP_UARTEnable(uart->hw_base);
212+
UARTIntRegister(uart->hw_base, UART0_IRQHandler);
213+
MAP_IntEnable(INT_UART0);
214+
MAP_UARTEnable(uart->hw_base);
194215

195216
/* register UART0 device */
196217
rt_hw_serial_register(&serial0, "uart0",
197-
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
218+
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
198219
uart);
199220
#endif
200221
return 0;

0 commit comments

Comments
 (0)