Squashed commit of the following: commit 5f16309f629b9928d2134b85ae64af69bc3ebbcd Author: kuwoyuki <kuwoyuki@cock.li> Date: Sun Nov 24 22:55:15 2024 +0600 fix: Makefile, improve onewire retries commit 55496a3bda941b52ff349dc75c9c06eb5a37c07d Author: kuwoyuki <kuwoyuki@cock.li> Date: Mon Nov 18 00:41:18 2024 +0600 fix: make onewire validity less strict commit 3428a9bc9792508972ce3e7e4e35a64f047bca10 Author: kuwoyuki <kuwoyuki@cock.li> Date: Sun Nov 17 23:57:55 2024 +0600 chore: rm bins commit 1594e5ed430522b15466c8afa62ff7fb1b28947c Author: kuwoyuki <kuwoyuki@cock.li> Date: Sun Nov 17 23:32:01 2024 +0600 chore: unplatformiofy
131 lines
3.8 KiB
C
131 lines
3.8 KiB
C
#include "network.h"
|
|
|
|
#include <DHCP/dhcp.h>
|
|
#include <W5500/w5500.h>
|
|
#include <string.h>
|
|
|
|
#include "ch32v003fun.h"
|
|
#include "config.h"
|
|
#include "debug.h"
|
|
#include "spi_dma.h"
|
|
#include "systick.h"
|
|
#include "utils.h"
|
|
|
|
#define DHCP_INTERVAL_CONNECTING 50 // init/renew
|
|
#define DHCP_INTERVAL_CONNECTED 500 // leased
|
|
|
|
void configure_network(void) {
|
|
DEBUG_PRINT("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);
|
|
}
|
|
|
|
static volatile dhcp_state_t dhcp_state = DHCP_STATE_INIT;
|
|
static volatile uint32_t dhcp_lease_time = 0;
|
|
static volatile uint32_t last_lease_time = 0;
|
|
static uint32_t last_processing_time = 0;
|
|
|
|
// Buffers
|
|
static uint8_t dhcp_buffer[512];
|
|
static wiz_NetInfo current_net_info = {
|
|
.mac = {0x02, 0x32, 0x00, 0x01, 0x00, 0x00}, .dhcp = NETINFO_DHCP};
|
|
|
|
static void update_network_config(void) {
|
|
wizchip_setnetinfo(¤t_net_info);
|
|
DEBUG_PRINT("IP config: %d.%d.%d.%d\n", current_net_info.ip[0],
|
|
current_net_info.ip[1], current_net_info.ip[2],
|
|
current_net_info.ip[3]);
|
|
}
|
|
|
|
void callback_dhcp_ip_update(void) {
|
|
dhcp_lease_time = getDHCPLeasetime();
|
|
DEBUG_PRINT("IP %s! Lease time: %lu sec\n",
|
|
dhcp_state == DHCP_STATE_INIT ? "assigned" : "updated",
|
|
dhcp_lease_time);
|
|
|
|
// Update all network configuration at once
|
|
getIPfromDHCP(current_net_info.ip);
|
|
getGWfromDHCP(current_net_info.gw);
|
|
getSNfromDHCP(current_net_info.sn);
|
|
getDNSfromDHCP(current_net_info.dns);
|
|
|
|
dhcp_state = DHCP_STATE_LEASED;
|
|
}
|
|
|
|
void callback_ip_conflict(void) {
|
|
DEBUG_PRINT("IP conflict!\n");
|
|
dhcp_state = DHCP_STATE_INIT;
|
|
}
|
|
|
|
// Initialize network with MAC based on node ID
|
|
void network_init(void) {
|
|
uint8_t node_num = parse_node_number(NODE_CONFIG.id);
|
|
|
|
// MAC address
|
|
current_net_info.mac[0] = 0x02; // Locally administered
|
|
current_net_info.mac[1] = 0x32; // Organization ID (CH32)
|
|
current_net_info.mac[2] = 0x00; // Sub-organization
|
|
current_net_info.mac[3] = 0x01; // Device type
|
|
current_net_info.mac[4] = node_num >> 8; // Node number high byte
|
|
current_net_info.mac[5] = node_num & 0xFF; // Node number low byte
|
|
|
|
// Copy MAC to network info
|
|
memcpy(NODE_CONFIG.mac, current_net_info.mac, 6);
|
|
|
|
// Initialize network
|
|
setSHAR(current_net_info.mac);
|
|
DHCP_init(DHCP_SOCKET, dhcp_buffer);
|
|
reg_dhcp_cbfunc(callback_dhcp_ip_update, callback_dhcp_ip_update,
|
|
callback_ip_conflict);
|
|
dhcp_state = DHCP_STATE_INIT;
|
|
}
|
|
|
|
void dhcp_process(void) {
|
|
uint32_t current_time = millis();
|
|
uint16_t processing_interval =
|
|
dhcp_state == DHCP_STATE_INIT || dhcp_state == DHCP_STATE_RENEW
|
|
? DHCP_INTERVAL_CONNECTING
|
|
: DHCP_INTERVAL_CONNECTED;
|
|
|
|
if ((current_time - last_processing_time) >= processing_interval) {
|
|
last_processing_time = current_time;
|
|
|
|
switch (dhcp_state) {
|
|
case DHCP_STATE_INIT:
|
|
DHCP_run();
|
|
if (dhcp_state == DHCP_STATE_LEASED) {
|
|
update_network_config();
|
|
}
|
|
break;
|
|
|
|
case DHCP_STATE_LEASED:
|
|
// renew @ 50% of lease time
|
|
if (current_time - last_lease_time >= (dhcp_lease_time * 500)) {
|
|
dhcp_state = DHCP_STATE_RENEW;
|
|
DHCP_run();
|
|
}
|
|
break;
|
|
|
|
case DHCP_STATE_RENEW:
|
|
DHCP_run();
|
|
if (dhcp_state == DHCP_STATE_LEASED) {
|
|
update_network_config();
|
|
last_lease_time = current_time;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
DHCP_run();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
uint8_t dhcp_get_state(void) { return dhcp_state; }
|