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"
3130typedef 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
3837static 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 */
112154struct rt_serial_device serial0 ;
113- struct serial_ringbuffer uart0_int_rx_buf ;
114155hw_uart_t uart0 =
115156{
116157 UART0_BASE ,
117158};
118159
119160void 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