rp2040/example/peripherals/dma/dma_sniff/main.c
2025-06-02 18:41:52 +08:00

74 lines
2.2 KiB
C

#include "resets.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,
};
int main(void)
{
uint32_t sniff_result;
reset_unreset_blocks_wait(RESETS_BLOCK_UART0 | RESETS_BLOCK_DMA);
gpio_init(0, GPIO_FUNC_UART | GPIO_PULL_UP | GPIO_DRIVE_4MA);
uart_init(uart0_hw, 2 * 1000 * 1000, UART_DATABITS_8 | UART_PARITY_NONE | UART_STOPBITS_1);
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;
}