rp2040/main.c
2025-03-19 22:15:07 +08:00

205 lines
6.6 KiB
C

#include "system.h"
#include "reset.h"
#include "clock.h"
#include "sio.h"
#include "gpio.h"
#include "dma.h"
#include "uart.h"
#include "pio_rmii_tx.h"
#include "RP2040.h"
#include "main_core0.h"
#include "main_core1.h"
#include "udp.h"
#include "stdio.h"
#define STR_MAC_SRC "11:22:33:44:55:66"
#define STR_MAC_DST "1C:1B:0D:2E:A9:99"
#define STR_IP_SRC "192.168.2.219"
#define STR_IP_DST "192.168.2.24"
#define STR_PORT_SRC "40103"
#define STR_PORT_DST "40103"
struct gpio_cfg_s gpio_led_cfg = {
.pin = 25,
.sio_dir = GPIO_SIO_DIR_OUT,
.funcsel = GPIO_FUNC_SIO,
.over_out = GPIO_OVER_OUT_PERIPHERAL,
.over_oe = GPIO_OVER_OE_PERIPHERAL,
.over_in = GPIO_OVER_IN_PERIPHERAL,
.over_irq = GPIO_OVER_IRQ_NORMAL,
.slew_rate = GPIO_PADS_SLEW_RATE_SLOW, /* slew rate control, slow or fast */
.schmitt = ENABLE, /* enable or disable schmitt */
.pull_down = DISABLE, /* enable or disable pull down */
.pull_up = DISABLE, /* enable or disable pull up */
.drive = GPIO_PADS_DRIVE_STRENGTH_4MA, /* drive strength */
.ie = DISABLE, /* enable or disable input */
.od = DISABLE, /* output disable, has priority over output enable from peripherals */
};
struct gpio_cfg_s gpio_clkout_cfg = {
.pin = 21,
.sio_dir = GPIO_SIO_DIR_IN,
.funcsel = GPIO_FUNC_CLOCK,
.over_out = GPIO_OVER_OUT_PERIPHERAL,
.over_oe = GPIO_OVER_OE_PERIPHERAL,
.over_in = GPIO_OVER_IN_PERIPHERAL,
.over_irq = GPIO_OVER_IRQ_NORMAL,
.slew_rate = GPIO_PADS_SLEW_RATE_SLOW, /* slew rate control, slow or fast */
.schmitt = ENABLE, /* enable or disable schmitt */
.pull_down = DISABLE, /* enable or disable pull down */
.pull_up = DISABLE, /* enable or disable pull up */
.drive = GPIO_PADS_DRIVE_STRENGTH_4MA, /* drive strength */
.ie = DISABLE, /* enable or disable input */
.od = DISABLE, /* output disable, has priority over output enable from peripherals */
};
void main_core1(void)
{
volatile uint32_t *addr = (volatile uint32_t *)0xE000E018;
__disable_irq();
SysTick_Config(400 * 10);
gpio_init(&gpio_clkout_cfg);
clock_gpout_set(0, ENABLE, CLOCK_GPOUT0_SRC_SYS, 40 << 8, DISABLE, 0, 0);
// while (*addr > 10 * 100) {}
gpio_led_cfg.pin = gpio_clkout_cfg.pin;
gpio_init(&gpio_led_cfg);
gpio_led_cfg.pin = 25;
main_core0();
main_core1();
while (1) {
if (*addr < 100 * 10) {
gpio_set(21);
} else {
gpio_clear(21);
}
}
}
extern uint32_t _vector;
extern uint32_t _stack_top_core1;
struct pio_rmii_tx_s rmii = {
.clkdiv = 2,
.sm = 0,
.addr = 0,
.flag = 0,
.pin = 12, /* increase order of pin CLK-EN-D0-D1 */
};
struct udp_cfg_s udp_cfg = {
.mac_src = STR_MAC_SRC,
.mac_dst = STR_MAC_DST,
.ip_src = STR_IP_SRC,
.ip_dst = STR_IP_DST,
.port_src = STR_PORT_SRC,
.port_dst = STR_PORT_DST,
};
struct udp_header_s udp_default;
uint32_t buffer[512];
uint32_t *data;
uint16_t length;
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,
};
void isr_uart0(void)
{
uint32_t status = uart_int_get_mask_status(UART_ID_0);
printf("sts = 0x%08lX\t\n", status);
}
int main(void)
{
reset_enable(RESET_IO_BANK0 | RESET_PADS_BANK0 | RESET_PIO0 | RESET_DMA | RESET_UART0);
reset_disable(RESET_IO_BANK0 | RESET_PADS_BANK0 | RESET_PIO0 | RESET_DMA | RESET_UART0);
gpio_init(&gpio_led_cfg);
gpio_init_simple(rmii.pin, GPIO_FUNC_PIO0, DISABLE, DISABLE);
gpio_init_simple(rmii.pin + 1, GPIO_FUNC_PIO0, DISABLE, DISABLE);
gpio_init_simple(rmii.pin + 2, GPIO_FUNC_PIO0, DISABLE, DISABLE);
gpio_init_simple(rmii.pin + 3, GPIO_FUNC_PIO0, DISABLE, DISABLE);
gpio_init_simple(0, GPIO_FUNC_UART, DISABLE, ENABLE);
if (udp_create_default(&udp_cfg, &udp_default)) {
while (1);
}
data = udp_copy_header(buffer, &udp_default);
for (uint32_t i = 0; i < 16; i++) {
data[i] = (i << 24) | (i << 16) | (i << 8) | i;
}
length = udp_pack_data((struct udp_header_s *)buffer, 16);
// system_reset(SYSTEM_BLOCK_PROC1);
// sio_launch_core1(main_core1, &_stack_top_core1, _vector);
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)));
__enable_irq();
pio_rmii_tx_init(&rmii);
// reset_enable(RESET_UART0);
// reset_disable(RESET_UART0);
while (!reset_get_state(RESET_UART0));
uart_init(UART_ID_0, &uart_cfg);
// uart_set_baudrate(UART_ID_0, 200 * 1000 * 1000, 2 * 1000 * 1000);
uart_int_enable(UART_ID_0, UART_INT_ALL);
// NVIC_EnableIRQ(UART0_IRQ_IRQn);
while (1) {
static uint32_t tick_led = 0;
static uint32_t tick_rmii = 0;
if (tick_1ms > (tick_rmii + 1)) {
tick_rmii = tick_1ms;
pio_rmii_write(&rmii, (uint32_t *)buffer, length);
}
if (tick_1ms > (tick_led + 500)) {
tick_led = tick_1ms;
gpio_toggle(gpio_led_cfg.pin);
printf("tick_led = %ld\r\n", tick_led);
}
}
return 0;
}