Skip to content

Commit 00c2f2d

Browse files
committed
Fix serial/UART driver
1 parent 2616b19 commit 00c2f2d

File tree

10 files changed

+160
-156
lines changed

10 files changed

+160
-156
lines changed

bsp/nxp/imx/imx91/.config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ CONFIG_RT_BACKTRACE_LEVEL_MAX_NR=32
199199
#
200200
# AArch64 Architecture Configuration
201201
#
202-
CONFIG_ARCH_TEXT_OFFSET=0x200000
203-
CONFIG_ARCH_RAM_OFFSET=0
204-
CONFIG_ARCH_SECONDARY_CPU_STACK_SIZE=4096
202+
CONFIG_ARCH_TEXT_OFFSET=0x100000
203+
CONFIG_ARCH_RAM_OFFSET=0x80000000
204+
CONFIG_ARCH_SECONDARY_CPU_STACK_SIZE=16384
205205
CONFIG_ARCH_HAVE_EFFICIENT_UNALIGNED_ACCESS=y
206206
CONFIG_ARCH_USING_GENERIC_CPUID=y
207207
CONFIG_ARCH_HEAP_SIZE=0x4000000

bsp/nxp/imx/imx91/SConstruct

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ env = Environment(tools = ['mingw'],
2121
LINK = rtconfig.LINK, LINKFLAGS = rtconfig.LFLAGS)
2222
env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
2323
env['ASCOM'] = env['ASPPCOM']
24+
env['LINKCOM'] = '$LINK -o $TARGET $LINKFLAGS $__RPATH $SOURCES $_LIBDIRFLAGS -Wl,--start-group $_LIBFLAGS -Wl,--end-group'
2425

2526
Export('RTT_ROOT')
2627
Export('rtconfig')

bsp/nxp/imx/imx91/drivers/board.c

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,33 @@
77
* Date Author Notes
88
* 2025-07-12 BruceOu the first version
99
*/
10-
11-
#include <rthw.h>
12-
#include <rtthread.h>
13-
1410
#include "board.h"
1511

16-
// #include <registers/regsarmglobaltimer.h>
17-
// #include <registers/regsepit.h>
12+
#include "serial.h"
1813

19-
// #include <imx_uart.h>
20-
// #include <epit.h>
21-
// #include <cortex_a.h>
14+
#include <gtimer.h>
15+
#include <mm_aspace.h>
16+
#include <mm_page.h>
17+
#include <rtdevice.h>
2218

2319
#include <mmu.h>
2420

21+
#define BOARD_DRAM_SIZE (256 * 1024 * 1024)
22+
2523
struct mem_desc platform_mem_desc[] = {
26-
{0x00000000, 0x80000000, 0x00000000, DEVICE_MEM},
27-
{0x80000000, 0xFFF00000, 0x80000000, NORMAL_MEM}
24+
{0x00200000, 0x00200000 + BOARD_DRAM_SIZE - 1, 0x80000000, NORMAL_MEM}, // 256MB DRAM
25+
{0xD0000000, 0xFFFFFFFF, 0x38000000, DEVICE_MEM},
2826
};
2927

3028
const rt_uint32_t platform_mem_desc_size = sizeof(platform_mem_desc)/sizeof(platform_mem_desc[0]);
3129

30+
rt_region_t init_page_region = {
31+
(rt_size_t)PAGE_START,
32+
(rt_size_t)PAGE_END,
33+
};
34+
35+
extern unsigned long MMUTable[];
36+
3237
static void rt_hw_timer_isr(int vector, void *param)
3338
{
3439
rt_tick_increase();
@@ -77,11 +82,37 @@ void rt_hw_board_init(void)
7782
// enable_neon_fpu();
7883
// disable_strict_align_check();
7984

80-
extern void uart1_puts(const char *str);
81-
uart1_puts("RT-Thread on i.MX91\n");
85+
imx_uart1_puts("rt_hw_board_init!\n");
86+
// rt_hw_mmu_map_init(&rt_kernel_space, (void*)0x080000000000, 0x10000000, MMUTable, 0);
87+
88+
// imx_uart1_puts("rt_page_init!\n");
89+
// imx_uart1_print_hex("PAGE_START: ", (rt_base_t) PAGE_START);
90+
// imx_uart1_print_hex("PAGE_END: ", (rt_base_t) PAGE_END);
91+
// rt_page_init(init_page_region);
92+
93+
// imx_uart1_puts("rt_hw_mmu_setup!\n");
94+
// rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, platform_mem_desc_size);
95+
96+
#ifdef RT_USING_HEAP
97+
/* initialize system heap */
98+
rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
99+
#endif
100+
101+
/* initialize hardware interrupt */
102+
rt_hw_interrupt_init();
103+
104+
rt_hw_uart_init();
105+
rt_hw_console_output("rt_hw_uart_init done!\n");
106+
107+
/* initialize timer for os tick */
108+
rt_hw_gtimer_init();
82109

83110
rt_components_board_init();
84111
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
112+
113+
rt_hw_console_output("rt_hw_board_init done!\n");
114+
rt_kprintf("rt_kprintf works!\n");
115+
rt_hw_console_output("rt_hw_board_init done!!\n");
85116
}
86117

87118
/*@}*/

bsp/nxp/imx/imx91/drivers/board.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@ extern int __bss_end;
2525
#define HEAP_BEGIN ((void*)&__bss_end)
2626
#endif
2727

28-
#define HEAP_END (void*)(0x80000000 + 32 * 1024 * 1024)
28+
// #define KERNEL_VADDR_START 0x0
29+
#ifndef KERNEL_VADDR_START
30+
#define KERNEL_VADDR_START (ARCH_RAM_OFFSET + ARCH_TEXT_OFFSET)
31+
#endif
32+
33+
#define HEAP_END (KERNEL_VADDR_START + 32 * 1024 * 1024)
34+
35+
// #define HEAP_END (void*)(HEAP_BEGIN + 32 * 1024 * 1024)
36+
#define PAGE_START HEAP_END
37+
#define PAGE_END ((size_t)PAGE_START + 128 * 1024 * 1024)
2938

3039
void rt_hw_board_init(void);
3140

bsp/nxp/imx/imx91/drivers/sdk/drivers/lpuart/fsl_lpuart.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#include "fsl_lpuart.h"
1010

11+
#include <rtthread.h>
12+
13+
#define memset rt_memset
14+
1115
/*
1216
* $Coverage Justification Reference$
1317
*
@@ -656,6 +660,7 @@ void LPUART_GetDefaultConfig(lpuart_config_t *config)
656660
{
657661
assert(NULL != config);
658662

663+
rt_hw_console_output("LPUART_GetDefaultConfig start!\n");
659664
/* Initializes the configure structure to zero. */
660665
(void)memset(config, 0, sizeof(*config));
661666

@@ -683,6 +688,7 @@ void LPUART_GetDefaultConfig(lpuart_config_t *config)
683688
#if defined(FSL_FEATURE_LPUART_HAS_CTRL_SWAP) && FSL_FEATURE_LPUART_HAS_CTRL_SWAP
684689
config->swapTxdRxd = false;
685690
#endif
691+
rt_hw_console_output("LPUART_GetDefaultConfig done!\n");
686692
}
687693

688694
/*!

bsp/nxp/imx/imx91/drivers/serial.c

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
*/
1111

1212
#include <rthw.h>
13-
#include <rtconfig.h>
1413
#include <rtdevice.h>
15-
// #include <drivers/drv_serial.h>
14+
#include <ioremap.h>
15+
#include <drivers/misc.h>
1616

1717
#include "serial.h"
1818

@@ -44,9 +44,15 @@ static struct hw_uart_device _uart1_device =
4444
.clock_mux = kCLOCK_LPUART1_ClockRoot_MuxOsc24M,
4545
.clock_ip_name = kCLOCK_Lpuart1,
4646
};
47-
// static struct rt_serial_device _serial1;
4847
#endif
4948

49+
static struct hw_uart_device* hw_uart_devices[] =
50+
{
51+
#ifdef BSP_USING_UART1
52+
&_uart1_device,
53+
#endif
54+
};
55+
5056
static void rt_hw_uart_isr(int irqn, void *param)
5157
{
5258
struct rt_serial_device *serial = (struct rt_serial_device *)param;
@@ -89,12 +95,16 @@ static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_co
8995
// uart_set_FIFO_mode(uart->uart_base, RX_FIFO, 1, IRQ_MODE);
9096

9197
struct hw_uart_device *uart = RT_NULL;
92-
lpuart_config_t config;
98+
static lpuart_config_t config;
9399

100+
rt_hw_console_output("uart_configure start ...\n");
94101
RT_ASSERT(serial != RT_NULL);
95102
uart = (struct hw_uart_device *)serial->parent.user_data;
96103

104+
rt_hw_console_output("uart_configure LPUART_GetDefaultConfig ...\n");
97105
LPUART_GetDefaultConfig(&config);
106+
rt_hw_console_output("uart_configure LPUART_GetDefaultConfig done!\n");
107+
98108
config.baudRate_Bps = cfg->baud_rate;
99109

100110
// data bits
@@ -171,17 +181,22 @@ static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_co
171181
config.enableRx = serial->parent.flag & RT_DEVICE_FLAG_RDONLY;
172182

173183
// Set UART clock source and clock divider
174-
CLOCK_SetRootClockMux(uart->clock_root, uart->clock_mux);
175-
CLOCK_SetRootClockDiv(uart->clock_root, 1U);
184+
// CLOCK_SetRootClockMux(uart->clock_root, uart->clock_mux);
185+
// CLOCK_SetRootClockDiv(uart->clock_root, 1U);
176186

177187
// Initialize the LPUART module with the configuration structure and clock source
188+
rt_hw_console_output("uart_configure LPUART_Init ...\n");
178189
LPUART_Init(uart->uart_base, &config, CLOCK_GetIpFreq(uart->clock_root));
179190

180-
// Enable RX interrupt
191+
// Install interrupt handler
192+
rt_hw_console_output("uart_configure rt_hw_interrupt_install ...\n");
181193
rt_hw_interrupt_install(uart->irqn, rt_hw_uart_isr, serial, "uart");
182194
rt_hw_interrupt_mask(uart->irqn);
183195

184-
return RT_EOK;
196+
// Enable RX interrupt
197+
rt_hw_console_output("uart_configure LPUART_EnableInterrupts ...\n");
198+
LPUART_EnableInterrupts(uart->uart_base, kLPUART_RxDataRegFullInterruptEnable);
199+
return RT_EOK;
185200
}
186201

187202
static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
@@ -223,24 +238,62 @@ static int uart_putc(struct rt_serial_device *serial, char c)
223238
return 1;
224239
}
225240

226-
void uart1_putc(char c)
241+
void imx_uart1_putc(char c)
242+
{
243+
LPUART_WriteByte(LPUART1, c);
244+
while (!(LPUART_GetStatusFlags(LPUART1) & kLPUART_TxDataRegEmptyFlag));
245+
}
246+
247+
void imx_uart1_puts(const char *str)
248+
{
249+
int has_cr = 0;
250+
while (*str) {
251+
if (*str == '\r') {
252+
has_cr = 1;
253+
} else if (*str == '\n') {
254+
if (!has_cr) {
255+
imx_uart1_putc('\r');
256+
}
257+
}
258+
imx_uart1_putc(*str++);
259+
}
260+
}
261+
262+
void imx_uart1_print_hex(const char *str, rt_base_t hex)
227263
{
264+
imx_uart1_puts(str);
265+
imx_uart1_putc('0');
266+
imx_uart1_putc('x');
267+
for (int i = 60; i >= 0; i -= 4) {
268+
rt_base_t h = (hex >> i) & 0xF;
269+
imx_uart1_putc(h < 10 ? '0' + h : 'A' + h - 10);
270+
}
271+
imx_uart1_putc('\r');
272+
imx_uart1_putc('\n');
273+
}
274+
275+
void rt_hw_console_putc(char c)
276+
{
277+
#if defined(BSP_USING_UART1)
228278
uart_putc(&_uart1_device.serial, c);
279+
#endif
229280
}
230281

231-
void uart1_puts(const char *str)
282+
void rt_hw_console_output(const char *str)
232283
{
284+
#if defined(BSP_USING_UART1)
233285
int has_cr = 0;
234286
while (*str) {
235287
if (*str == '\r') {
236288
has_cr = 1;
237289
} else if (*str == '\n') {
238290
if (!has_cr) {
239-
uart1_putc('\r');
291+
rt_hw_console_putc('\r');
240292
}
241293
}
242-
uart1_putc(*str++);
294+
rt_hw_console_putc(*str++);
243295
}
296+
#endif
244297
}
245298

246299
static int uart_getc(struct rt_serial_device *serial)
@@ -271,9 +324,8 @@ static const struct rt_uart_ops _uart_ops =
271324

272325
int rt_hw_uart_init(void)
273326
{
274-
struct hw_uart_device *uart;
275327
struct serial_configure config;
276-
328+
/* set serial configure */
277329
config.baud_rate = BAUD_RATE_115200;
278330
config.bit_order = BIT_ORDER_LSB;
279331
config.data_bits = DATA_BITS_8;
@@ -282,16 +334,19 @@ int rt_hw_uart_init(void)
282334
config.invert = NRZ_NORMAL;
283335
config.bufsz = RT_SERIAL_RB_BUFSZ;
284336

285-
#ifdef BSP_USING_UART1
286-
uart = &_uart1_device;
287-
_uart1_device.serial.ops = &_uart_ops;
288-
_uart1_device.serial.config = config;
289-
290-
/* register LPUART1 device */
291-
rt_hw_serial_register(&_uart1_device.serial, _uart1_device.device_name,
292-
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
293-
#endif
337+
for (int i = 0; i < RT_ARRAY_SIZE(hw_uart_devices); i++)
338+
{
339+
if (hw_uart_devices[i] != RT_NULL)
340+
{
341+
hw_uart_devices[i]->serial.ops = &_uart_ops;
342+
hw_uart_devices[i]->serial.config = config;
343+
// hw_uart_devices[i]->uart_base = rt_ioremap((void *)hw_uart_devices[i]->uart_base, sizeof(LPUART_Type));
344+
345+
rt_hw_serial_register(&hw_uart_devices[i]->serial, hw_uart_devices[i]->device_name,
346+
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, hw_uart_devices[i]);
347+
}
348+
}
294349

295350
return 0;
296351
}
297-
INIT_BOARD_EXPORT(rt_hw_uart_init);
352+
// INIT_BOARD_EXPORT(rt_hw_uart_init);

bsp/nxp/imx/imx91/drivers/serial.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717

1818
int rt_hw_uart_init(void);
1919

20+
void imx_uart1_putc(char c);
21+
void imx_uart1_puts(const char *str);
22+
void imx_uart1_print_hex(const char *str, rt_base_t hex);
23+
24+
void rt_hw_console_putc(char c);
25+
void rt_hw_console_output(const char *str);
26+
2027
#endif
2128

2229

0 commit comments

Comments
 (0)