feat: SPI DMA

Squashed commit of the following:

commit c3e1f696b4fbafb6dd30c6934e4c7c181562e055
Author: kuwoyuki <kuwoyuki@cock.li>
Date:   Sat Oct 12 20:43:12 2024 +0600

    chore: spi dma

commit d1e7df60be3e06ed85ce8639516e299085d3c72b
Author: kuwoyuki <kuwoyuki@cock.li>
Date:   Sat Oct 12 15:14:11 2024 +0600

    static dummies

commit 81682428b471f825e3350e37aa74c373b46d4fef
Author: kuwoyuki <kuwoyuki@cock.li>
Date:   Sat Oct 12 14:45:39 2024 +0600

    dma works?

commit 150c97a4b566712a502d8a2861a2dc0864324d61
Author: kuwoyuki <kuwoyuki@cock.li>
Date:   Sat Oct 12 02:07:39 2024 +0600

    dma?
This commit is contained in:
2024-10-12 20:43:38 +06:00
parent 259d63197e
commit ec9dcc76f7
11 changed files with 362 additions and 229 deletions

View File

@@ -1,46 +1,123 @@
#include "w5500.h"
#include <DHCP/dhcp.h>
#include <DNS/dns.h>
#include <W5500/w5500.h>
#include <socket.h>
#include <stdio.h>
#include "ch32v003fun.h"
// #include "spi.h"
#include "spi_dma.h"
static inline uint8_t SPI_is_RX_empty() { return SPI1->STATR & SPI_STATR_RXNE; }
// Definitions for socket indexes
#define DHCP_SOCKET 0
#define DNS_SOCKET 1
#define HTTP_SOCKET 2
static inline void SPI_wait_not_busy() {
while ((SPI1->STATR & SPI_STATR_BSY) != 0);
}
// Global variables
volatile uint32_t count = 0;
volatile int ip_assigned = 0;
static inline uint8_t SPI_transfer(uint8_t data) {
while (!(SPI1->STATR & SPI_STATR_TXE));
// SPI_wait_not_busy();
SPI1->DATAR = data;
// Global buffers for DHCP and DNS
uint8_t dhcp_buffer[1024];
uint8_t dns_buffer[1024];
while (!(SPI1->STATR & SPI_STATR_RXNE));
return SPI1->DATAR;
}
void W5500_Select(void) {
void w5500_select(void) {
GPIOA->BCR = (1 << 4); // Set PA4 (CS) low
// Delay_Ms(2);
}
void W5500_Unselect(void) {
void w5500_unselect(void) {
GPIOA->BSHR = (1 << 4); // Set PA4 (CS) high
// Delay_Ms(2);
}
uint8_t W5500_ReadByte() {
return SPI_transfer(0xFF); // dummy byte to init read
// Function to handle IP assignment details
void handle_ip_assigned(void) {
printf("IP Assigned!\n");
wiz_NetInfo net_info;
getIPfromDHCP(net_info.ip);
getGWfromDHCP(net_info.gw);
getSNfromDHCP(net_info.sn);
uint8_t dns[4];
getDNSfromDHCP(dns);
printf(
"IP: %d.%d.%d.%d\nGW: %d.%d.%d.%d\nNet: %d.%d.%d.%d\nDNS: "
"%d.%d.%d.%d\n",
net_info.ip[0], net_info.ip[1], net_info.ip[2], net_info.ip[3],
net_info.gw[0], net_info.gw[1], net_info.gw[2], net_info.gw[3],
net_info.sn[0], net_info.sn[1], net_info.sn[2], net_info.sn[3], dns[0],
dns[1], dns[2], dns[3]);
wizchip_setnetinfo(&net_info);
}
void W5500_WriteByte(uint8_t byte) { SPI_transfer(byte); }
// Callback functions
void callback_ip_assigned(void) {
printf("Callback: IP assigned! Leased time: %u sec\n", getDHCPLeasetime());
ip_assigned = 1;
}
void W5500_ReadBuff(uint8_t* buffer, uint16_t len) {
for (uint16_t i = 0; i < len; i++) {
buffer[i] = W5500_ReadByte();
void callback_ip_conflict(void) { printf("Callback: IP conflict!\n"); }
void configure_network(void) {
printf("Starting network configuration...\n");
// Setup chip select and SPI callbacks
reg_wizchip_cs_cbfunc(spi_select, spi_unselect);
reg_wizchip_spi_cbfunc(spi_read_byte, spi_write_byte);
reg_wizchip_spiburst_cbfunc(spidma_read_buffer, spidma_write_buffer);
uint8_t rx_tx_buff_sizes[] = {2, 2, 2, 2, 2, 2, 2, 2};
wizchip_init(rx_tx_buff_sizes, rx_tx_buff_sizes);
// Start DHCP process
wiz_NetInfo net_info = {.mac = {0xEA, 0x11, 0x22, 0x33, 0x44, 0xEA},
.dhcp = NETINFO_DHCP};
setSHAR(net_info.mac);
DHCP_init(DHCP_SOCKET, dhcp_buffer);
// Register DHCP callbacks
reg_dhcp_cbfunc(callback_ip_assigned, callback_ip_assigned,
callback_ip_conflict);
// Attempt to acquire an IP address using DHCP
const uint32_t max_attempts = 1e5;
for (uint32_t attempt = 0; !ip_assigned && attempt < max_attempts;
++attempt) {
DHCP_run();
}
if (!ip_assigned) {
printf("\r\nIP was not assigned :(\r\n");
return;
}
handle_ip_assigned();
}
void W5500_WriteBuff(uint8_t* buffer, uint16_t len) {
for (uint16_t i = 0; i < len; i++) {
W5500_WriteByte(buffer[i]);
void resolve_domain_name(const char* domain_name) {
printf("Resolving domain name \"%s\"...\n", domain_name);
DNS_init(DNS_SOCKET, dns_buffer);
// cloudflare dns
uint8_t dns[] = {1, 1, 1, 1};
uint8_t addr[4];
int8_t res;
uint8_t retries = 0;
while (retries < 3) {
Delay_Ms(250);
res = DNS_run(dns, (uint8_t*)domain_name, addr);
if (res == 1) {
printf("Result: %d.%d.%d.%d\n", addr[0], addr[1], addr[2], addr[3]);
break;
} else {
printf("DNS_run() failed, res = %d. Retries: %u\n", res, retries);
}
retries++;
}
}