From 1026053851d1b01363d0aa7e57d2a8eb85c8291c Mon Sep 17 00:00:00 2001 From: zhji Date: Sun, 23 Mar 2025 12:59:34 +0800 Subject: [PATCH] [feat] add dma_sniff demo --- component/printf/vsnprintf.c | 2 +- .../peripherals/dma/dma_sniff/CMakeLists.txt | 9 ++ example/peripherals/dma/dma_sniff/Makefile | 18 ++++ example/peripherals/dma/dma_sniff/main.c | 85 +++++++++++++++++++ example/peripherals/dma/dma_sniff/proj.conf | 1 + example/peripherals/gpio/gpio_led/main.c | 3 +- project.build | 2 + 7 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 example/peripherals/dma/dma_sniff/CMakeLists.txt create mode 100644 example/peripherals/dma/dma_sniff/Makefile create mode 100644 example/peripherals/dma/dma_sniff/main.c create mode 100644 example/peripherals/dma/dma_sniff/proj.conf diff --git a/component/printf/vsnprintf.c b/component/printf/vsnprintf.c index 84bf306..d463323 100644 --- a/component/printf/vsnprintf.c +++ b/component/printf/vsnprintf.c @@ -631,7 +631,7 @@ static size_t print_exponential_number(out_fct_type out, char* buffer, size_t id int exp10; bool abs_exp10_covered_by_powers_table; - struct scaling_factor normalization; + struct scaling_factor normalization = {0.0, 0}; // Determine the decimal exponent diff --git a/example/peripherals/dma/dma_sniff/CMakeLists.txt b/example/peripherals/dma/dma_sniff/CMakeLists.txt new file mode 100644 index 0000000..e07dfed --- /dev/null +++ b/example/peripherals/dma/dma_sniff/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.10) + +include(proj.conf) + +project(${EXAMPLE_NAME} VERSION 0.1) +add_executable(${EXAMPLE_NAME}.elf main.c) + +add_subdirectory(${SDK_BASE_DIR} sdk) +target_link_libraries(${EXAMPLE_NAME}.elf sdk) diff --git a/example/peripherals/dma/dma_sniff/Makefile b/example/peripherals/dma/dma_sniff/Makefile new file mode 100644 index 0000000..df5330b --- /dev/null +++ b/example/peripherals/dma/dma_sniff/Makefile @@ -0,0 +1,18 @@ +EXAMPLE_BASE_DIR ?= $(shell realpath .) +EXAMPLE_NAME := $(notdir $(patsubst %/,%,$(CURDIR))) +SDK_BASE_DIR ?= $(shell realpath ./../../../..) + +export SDK_BASE_DIR +export EXAMPLE_NAME +export EXAMPLE_BASE_DIR + +GCC_PATH := $(shell which arm-none-eabi-gcc) +CROSS_COMPILE := $(patsubst %gcc,%,$(GCC_PATH)) +ifeq ($(GCC_PATH),) +$(error arm-none-eabi-gcc not found in PATH. Please install the ARM toolchain.) +endif + +# add custom cmake definition +#cmake_definition+=-Dxxx=sss + +include $(SDK_BASE_DIR)/project.build diff --git a/example/peripherals/dma/dma_sniff/main.c b/example/peripherals/dma/dma_sniff/main.c new file mode 100644 index 0000000..428a1e9 --- /dev/null +++ b/example/peripherals/dma/dma_sniff/main.c @@ -0,0 +1,85 @@ +#include "reset.h" +#include "dma.h" +#include "gpio.h" +#include "uart.h" +#include "RP2040.h" +#include "stdio.h" + +uint8_t dma_buffer_raw[] = { 0x55, 0xD5, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE }; +uint8_t dma_buffer_calc[20]; +struct dma_cfg_s dma_cfg = { + .read_addr = (uint32_t)dma_buffer_raw, + .write_addr = (uint32_t)dma_buffer_calc, + .trans_count = sizeof(dma_buffer_raw) / 4, + .channel = DMA_CHANNEL_0, + .request = DMA_REQ_FOREVER, + .data_size = DMA_DATA_SIZE_32BIT, + .incr_read = ENABLE, + .incr_write = ENABLE, + .irq_quiet = DISABLE, + .byte_swap = DISABLE, + .chain_to = DMA_CHANNEL_0, + .high_priority = DISABLE, + .ring_sel_write = DISABLE, + .ring_size = DMA_RING_SIZE_NONE, + .sniff_en = ENABLE, +}; +struct dma_sniff_cfg_s sniff_cfg = { + .channel = DMA_CHANNEL_0, + .calc = DMA_SNIFF_CALC_CRC32_BITREV, + .byte_swap = DISABLE, + .out_rev = ENABLE, + .out_inv = ENABLE, +}; + +struct uart_cfg_s uart_cfg = { + .baudrate = 6 * 1000 * 1000, + .mode = UART_MODE_TX_RX, + .data_bits = UART_DATABITS_8, + .parity = UART_PARITY_NONE, + .stop_bits = UART_STOPBITS_1, + .fifo_enable = ENABLE, + .tx_fifo_level = UART_FIFO_LEVEL_1_2, + .rx_fifo_level = UART_FIFO_LEVEL_1_2, +}; + +int main(void) +{ + uint32_t sniff_result; + + reset_enable(RESET_DMA); + reset_disable(RESET_DMA); + gpio_init_simple(0, GPIO_FUNC_UART, DISABLE, ENABLE); + uart_init(UART_ID_0, &uart_cfg); + + for (int i = 0; i < sizeof(dma_buffer_calc); i++) { + dma_buffer_calc[i] = 0; + } + + dma_int_clear_raw_status(1 << DMA_CHANNEL_0); + dma_init(&dma_cfg); + dma_sniff_init(&sniff_cfg); + dma_sniff_write(0xA3123859); + dma_sniff_enable(); + dma_enable_and_trig(dma_cfg.channel); + while (!(dma_int_get_raw_status() & (1 << dma_cfg.channel))); + for (int i = 0; i < sizeof(dma_buffer_raw); i++) { + if (dma_buffer_raw[i] != dma_buffer_calc[i]) { + printf("error, [%d] 0x%02X -> 0x%02X\r\n", i, dma_buffer_raw[i], dma_buffer_calc[i]); + } + } + sniff_result = dma_sniff_read(); + if (sniff_result != 0xB0364FEA) { + printf("CRC32 check error, result = 0x%08lX, expert = 0x%08X\r\n", sniff_result, 0xB0364FEA); + } else { + printf("CRC32 check succeed, result = 0x%08lX\r\n", sniff_result); + } + + __enable_irq(); + + while (1) { + + } + + return 0; +} diff --git a/example/peripherals/dma/dma_sniff/proj.conf b/example/peripherals/dma/dma_sniff/proj.conf new file mode 100644 index 0000000..7d2ef31 --- /dev/null +++ b/example/peripherals/dma/dma_sniff/proj.conf @@ -0,0 +1 @@ +# set(CONFIG_COMPONENT1 1) diff --git a/example/peripherals/gpio/gpio_led/main.c b/example/peripherals/gpio/gpio_led/main.c index ecc492c..ca67528 100644 --- a/example/peripherals/gpio/gpio_led/main.c +++ b/example/peripherals/gpio/gpio_led/main.c @@ -1,11 +1,10 @@ -#include "system.h" #include "reset.h" #include "clock.h" #include "sio.h" #include "gpio.h" #include "uart.h" #include "stdio.h" -#include "cmsis_gcc.h" +#include "RP2040.h" struct gpio_cfg_s gpio_led_cfg = { .pin = 25, diff --git a/project.build b/project.build index 17175b0..e1d02ba 100644 --- a/project.build +++ b/project.build @@ -9,6 +9,7 @@ CFLAGS += -O2 CFLAGS += -Wall CFLAGS += -ffunction-sections CFLAGS += -fdata-sections +CFLAGS += -Wpointer-arith LDFLAGS += -mcpu=cortex-m0plus LDFLAGS += -mthumb @@ -52,6 +53,7 @@ build:Makefile $(MAKE) -C build -j $(CROSS_COMPILE)objcopy -O binary $(EXAMPLE_BASE_DIR)/build/$(EXAMPLE_NAME).elf $(EXAMPLE_BASE_DIR)/build/$(EXAMPLE_NAME).bin $(CROSS_COMPILE)objdump -d -S $(EXAMPLE_BASE_DIR)/build/$(EXAMPLE_NAME).elf > $(EXAMPLE_BASE_DIR)/build/$(EXAMPLE_NAME).asm + size $(EXAMPLE_BASE_DIR)/build/$(EXAMPLE_NAME).elf clean:: $(RM) build