rp2040/example/peripherals/dma/dma_sniff/main.c

86 lines
2.4 KiB
C
Raw Normal View History

2025-03-23 12:59:34 +08:00
#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;
}