i broke it

This commit is contained in:
2024-10-14 02:13:05 +06:00
parent 480a4a1ca5
commit 7d33688e75
12 changed files with 112 additions and 70 deletions

View File

@@ -12,5 +12,7 @@
#define FUNCONF_UART_PRINTF_BAUD \
115200 // Only used if FUNCONF_USE_UARTPRINTF is set.
#define FUNCONF_USE_CLK_SEC 0
#define FUNCONF_SYSTICK_USE_HCLK 1
#define FUNCONF_DEBUG 1
#endif

View File

@@ -6,6 +6,7 @@
#include "socket.h"
#include "spi_dma.h"
#include "systick.h"
#include "timer.h"
#include "uart.h"
#include "w5500.h"
@@ -15,7 +16,6 @@ void init_system(void) {
init_gpio();
init_uart(UART_BRR_APB1);
init_spidma();
// systick_init();
}
// cb fn for when a message is received
@@ -48,34 +48,18 @@ int main(void) {
init_system();
configure_network();
// todo: fucking systick
// uint32_t dhcp_last_invocation = 0;
// const uint32_t DHCP_INTERVAL = 100; // DHCP processing interval in ms
// TODO: enabling any kind of SysTick IRQ literally causes the socket to hang forever
// with 1ms interval the hang is on DHCP_ACK, on check_DHCP_leasedIP -> sendto() ARP req
// with 1s it happens somewhere on DNS req
// wtf?
init_systick();
tim2_init();
// // DHCP process blocking loop
// while (!ip_assigned) {
// if ((millis() - dhcp_last_invocation) >= DHCP_INTERVAL) {
// dhcp_last_invocation = millis();
// DEBUG_PRINT("Running DHCP...\n");
// DHCP_run(); // Attempt to obtain an IP address
// }
// }
// systick irq enable here would complete the DHCP configuration and hang on DNS..
// init_systick();
// 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) {
// DEBUG_PRINT("\r\nIP was not assigned :(\r\n");
// return -1;
// }
// handle_ip_assigned();
// ip_assigned = 0;
resolve_domain_name("hye.su");
configure_dhcp();
resolve_domain_name("example.com");
Network network;
ch32_mqtt_options_t opts = {CLIENT_ID, "", "", QOS0};
@@ -90,14 +74,9 @@ int main(void) {
while (1) {
// if ((millis() - dhcp_last_invocation) >= DHCP_INTERVAL) {
// dhcp_last_invocation = millis();
// DHCP_run(); // Run the DHCP processing function
// }
MQTTYield(&client, 1000); // Keep the connection alive
// if (ip_assigned) {
// handle_ip_assigned();
// ip_assigned = 0;
// DHCP_run();
// }
MQTTYield(&client, 1000); // keepalive
}
MQTTDisconnect(&client);

View File

@@ -1,6 +1,7 @@
#include "spi_dma.h"
#include "ch32v003fun.h"
#include "debug.h"
volatile transfer_state_t tx_state = IDLE;
volatile transfer_state_t rx_state = IDLE;
@@ -158,6 +159,8 @@ void init_spidma(void) {
DMA_MemoryInc_Enable | DMA_PeripheralInc_Disable |
DMA_Mode_Normal | DMA_DIR_PeripheralSRC | DMA_IT_TC;
NVIC_SetPriority(DMA1_Channel2_IRQn, 0);
NVIC_SetPriority(DMA1_Channel3_IRQn, 0);
NVIC_EnableIRQ(DMA1_Channel3_IRQn);
NVIC_EnableIRQ(DMA1_Channel2_IRQn);
}

View File

@@ -1,32 +1,22 @@
#include "systick.h"
#include <stdint.h>
#include "ch32v003fun.h"
#include "debug.h"
// ms counter
volatile uint32_t systick_millis = 0;
void systick_init(void) {
// Reset any pre-existing configuration
SysTick->CTLR = 0x0000;
// Set the compare register to trigger once per millisecond
void init_systick(void) {
SysTick->CTLR = 0;
SysTick->CMP = SYSTICK_ONE_MILLISECOND - 1;
// Reset the Count Register and variables
SysTick->CNT = 0x00000000;
systick_millis = 0x00000000;
// Set the SysTick Configuration
// NOTE: By not setting SYSTICK_CTLR_STRE, we maintain compatibility with
// busywait delay funtions used by ch32v003_fun.
SysTick->CTLR |= SYSTICK_CTLR_STE | // Enable Counter
SYSTICK_CTLR_STIE | // Enable Interrupts
SYSTICK_CTLR_STCLK; // Set Clock Source to HCLK/1
SysTick->CNT = 0;
systick_millis = 0;
// Enable the SysTick IRQ
NVIC_SetPriority(SysTicK_IRQn, 0xf0);
NVIC_EnableIRQ(SysTicK_IRQn);
/* Enable SysTick counter, IRQ, HCLK/1 */
SysTick->CTLR = SYSTICK_CTLR_STE | SYSTICK_CTLR_STIE | SYSTICK_CTLR_STCLK;
}
/*
@@ -42,9 +32,7 @@ void SysTick_Handler(void) {
// (Make sure the IQR is lightweight and CMP value is reasonable)
SysTick->CMP += SYSTICK_ONE_MILLISECOND;
// Clear the trigger state for the next IRQ
SysTick->SR = 0x00000000;
// Increment the milliseconds count
// clear irq
SysTick->SR = 0;
systick_millis++;
}

42
src/timer.c Normal file
View File

@@ -0,0 +1,42 @@
#include "timer.h"
#include <DHCP/dhcp.h>
#include <DNS/dns.h>
#include "ch32v003fun.h"
#include "debug.h"
void tim2_init(void) {
// Enable TIM2 clock
RCC->APB1PCENR |= RCC_APB1Periph_TIM2;
// Reset TIM2 to init all regs
RCC->APB1PRSTR |= RCC_APB1Periph_TIM2;
RCC->APB1PRSTR &= ~RCC_APB1Periph_TIM2;
// Set prescaler to 14400-1, 10 kHz timer clock
TIM2->PSC = 14400 - 1;
// Set auto-reload value to 10,000-1 for 1 second overflow
TIM2->ATRLR = 10000 - 1;
// Enable update interrupt
TIM2->DMAINTENR |= TIM_UIE;
// Enable TIM2
TIM2->CTLR1 |= TIM_CEN;
// NVIC_SetPriority(TIM2_IRQn, 0xf0);
NVIC_EnableIRQ(TIM2_IRQn);
}
void TIM2_IRQHandler(void) __attribute__((interrupt));
void TIM2_IRQHandler(void) {
if (TIM2->INTFR & TIM_UIF) {
TIM2->INTFR &= ~TIM_UIF;
// DEBUG_PRINT("TIM2 IRQ\n");
DHCP_time_handler();
DNS_time_handler();
}
}

View File

@@ -52,7 +52,9 @@ void callback_ip_assigned(void) {
void callback_ip_conflict(void) { DEBUG_PRINT("Callback: IP conflict!\n"); }
void configure_network(void) {
DEBUG_PRINT("===\n");
DEBUG_PRINT("Starting network configuration...\n");
DEBUG_PRINT("===\n");
// Setup chip select and SPI callbacks
reg_wizchip_cs_cbfunc(spi_select, spi_unselect);
@@ -61,8 +63,9 @@ void configure_network(void) {
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
void configure_dhcp(void) {
wiz_NetInfo net_info = {.mac = {0xEA, 0x11, 0x22, 0x33, 0x44, 0xEA},
.dhcp = NETINFO_DHCP};
setSHAR(net_info.mac);
@@ -73,16 +76,14 @@ void configure_network(void) {
callback_ip_conflict);
// Attempt to acquire an IP address using DHCP
const uint32_t max_attempts = 1e5;
// todo: run in a loop by a timer or sth
for (uint32_t attempt = 0; !ip_assigned && attempt < max_attempts;
++attempt) {
while (!ip_assigned) {
DHCP_run();
Delay_Ms(100);
DEBUG_PRINT("DHCP_run()...\n");
}
if (!ip_assigned) {
DEBUG_PRINT("\r\nIP was not assigned :(\r\n");
DEBUG_PRINT("\r\nIP was not assigned\r\n");
return;
}