Files
kunlun/dtest/dtest3/romlib_test/romlib_test.c

141 lines
3.6 KiB
C
Raw Normal View History

2025-06-26 11:09:45 +08:00
#include "os_types.h"
#include "dbg_io.h"
#include "iot_io.h"
#include "cpu.h"
#include "watchdog.h"
// #include "platform.h"
#include "iot_gptmr_api.h"
#include "iot_clock.h"
#include "clk.h"
#include "strformat.h"
#include "uart.h"
#include "string.h"
typedef int (*iot_sprintf_t)(char* str, const char* format, ...);
typedef int (*iot_snprintf_t)(char* str, size_t size, const char* format, ...);
typedef int (*iot_vsnprintf_t)(char* str, size_t size, const char* format, va_list ap);
typedef int32_t (*iot_printf_t)(const char* fmt, ...);
typedef int (*format_str_t)(const str_format_context* ctxt, const char* format, ...);
typedef int (*format_str_v_t)(const str_format_context* ctxt, const char* format, va_list ap);
typedef void* (*memmove_t)(void* dest, const void* src, size_t n);
iot_sprintf_t rom_iot_sprintf;
iot_snprintf_t rom_iot_snprintf;
iot_vsnprintf_t rom_iot_vsnprintf;
iot_printf_t rom_iot_printf;
iot_printf_t dtest_iot_printf;
format_str_t rom_format_str;
format_str_v_t rom_format_str_v;
str_format_context* rom_log_ctxt;
extern struct uart_ctrl uart_e_ctrl;
memmove_t rom_memmove;
#define PROVIDE(s) rom_##s
#define PUT_PORT 0
static void uart_flush(void)
{
while (uart_e_ctrl.tx_fifo_cnt(PUT_PORT)) __asm volatile("nop\n");
return;
}
static StrFormatResult uart_puts(void *user_data, const char *data, unsigned int len)
{
(void)user_data;
uint8_t c;
int i = 0;
while (i < len) {
c = *(data+i);
if (c == '\n'){
uart_e_ctrl.try_putc(PUT_PORT,'\r');
}
uart_e_ctrl.try_putc(PUT_PORT,c);
i++;
}
uart_flush();
return 0;
}
int rom_iot_vsnprintf_test(char *str, size_t size, const char *format, ...)
{
int res;
va_list ap;
va_start(ap, format);
res = rom_iot_vsnprintf(str, size, format, ap);
va_end(ap);
return res;
}
#pragma GCC diagnostic ignored "-Wint-conversion"
void fun_pointer_init() {
PROVIDE(iot_sprintf = 0x0004455e);
PROVIDE(iot_printf = 0x00044616);
PROVIDE(iot_snprintf = 0x000445f6);
PROVIDE(iot_vsnprintf = 0x000445ca);
PROVIDE(log_ctxt = 0x100005a4);
PROVIDE(format_str = 0x0004451e);
PROVIDE(format_str_v = 0x0004451e);
PROVIDE(memmove = 0x0004654e);
rom_log_ctxt->write_str = uart_puts;
dtest_iot_printf = (int)&iot_printf;
}
int rom_format_str_v_test(const char *format, ...)
{
int res;
va_list ap;
va_start(ap, format);
res = rom_format_str_v(rom_log_ctxt, format, ap);
va_end(ap);
return res;
}
static char g_buff[1024];
static char g_buff2[1024];
int main(void) {
iot_interrupt_init();
dbg_uart_init();
clk_core_init();
clk_core_freq_set(CLK_FRQ_150M);
fun_pointer_init();
iot_printf("print by iot_printf()\n");
dtest_iot_printf("print by dtest_iot_printf()\n");
iot_snprintf(g_buff, 1024, "print by iot_snprintf()\n");
iot_printf("%s", g_buff);
rom_iot_snprintf(g_buff, 1024, "print by rom_iot_snprintf()\n");
iot_printf("%s", g_buff);
rom_iot_vsnprintf_test(g_buff, 1024, "print by rom_iot_vsnprintf()\n");
iot_printf("%s", g_buff);
iot_printf("rom_iot_printf() test\n");
rom_iot_printf("print by rom_iot_printf()\n");
iot_printf("rom_format_str() test\n");
rom_format_str(rom_log_ctxt, "print by rom_format_str()\n");
iot_printf("rom_format_str_v() test\n");
rom_format_str_v_test("print by rom_format_str_v()\n");
iot_printf("print test end\n");
memmove(g_buff, g_buff2, 1024);
iot_printf("%s", g_buff);
rom_memmove(g_buff, "print by rom_memmove()\n", 24);
iot_printf("%s", g_buff);
while (1);
}