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

@@ -15,6 +15,7 @@
"spi_dma.h": "c", "spi_dma.h": "c",
"dhcp.h": "c", "dhcp.h": "c",
"debug.h": "c", "debug.h": "c",
"systick.h": "c" "systick.h": "c",
"timer.h": "c"
} }
} }

View File

@@ -3,12 +3,14 @@
#include <stdint.h> #include <stdint.h>
#define SYSTICK_ONE_MILLISECOND ((uint32_t)FUNCONF_SYSTEM_CORE_CLOCK / 1000) #define TIMER_DELAY 1000
#define SYSTICK_ONE_MILLISECOND \
(uint32_t)(FUNCONF_SYSTEM_CORE_CLOCK / TIMER_DELAY)
#define millis() (systick_millis) #define millis() (systick_millis)
void systick_init(void); void init_systick(void);
// ms counter incremented by SysTick // ms counter incremented by SysTick
extern volatile uint32_t systick_millis; // extern volatile uint32_t systick_millis;
#endif // SYSTICK_H #endif // SYSTICK_H

9
include/timer.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef TIMER_H
#define TIMER_H
#include "ch32v003fun.h"
// Function prototypes
void tim2_init(void);
#endif // TIMER_H

View File

@@ -24,6 +24,8 @@ void handle_ip_assigned(void);
// Initializes the W5500 chip // Initializes the W5500 chip
void configure_network(void); void configure_network(void);
void configure_dhcp(void);
// resolves a domain name // resolves a domain name
void resolve_domain_name(const char* domain_name); void resolve_domain_name(const char* domain_name);

View File

@@ -251,7 +251,7 @@ uint8_t check_DHCP_timeout(void);
void reset_DHCP_timeout(void); void reset_DHCP_timeout(void);
/* Parse message as OFFER and ACK and NACK from DHCP server.*/ /* Parse message as OFFER and ACK and NACK from DHCP server.*/
int8_t parseDHCPCMSG(void); int8_t parseDHCPMSG(void);
/* The default handler of ip assign first */ /* The default handler of ip assign first */
void default_ip_assign(void) void default_ip_assign(void)
@@ -749,6 +749,9 @@ uint8_t DHCP_run(void)
if (check_DHCP_leasedIP()) { if (check_DHCP_leasedIP()) {
// Network info assignment from DHCP // Network info assignment from DHCP
dhcp_ip_assign(); dhcp_ip_assign();
#ifdef _DHCP_DEBUG_
printf("> DHCP Success\r\n");
#endif
reset_DHCP_timeout(); reset_DHCP_timeout();
dhcp_state = STATE_DHCP_LEASED; dhcp_state = STATE_DHCP_LEASED;
@@ -901,10 +904,17 @@ int8_t check_DHCP_leasedIP(void)
tmp = getRCR(); tmp = getRCR();
setRCR(0x03); setRCR(0x03);
#ifdef _DHCP_DEBUG_
printf("\r\n> Check leased IP - %d.%d.%d.%d\r\n", DHCP_allocated_ip[0], DHCP_allocated_ip[1], DHCP_allocated_ip[2], DHCP_allocated_ip[3]);
#endif
// IP conflict detection : ARP request - ARP reply // IP conflict detection : ARP request - ARP reply
// Broadcasting ARP Request for check the IP conflict using UDP sendto() function // Broadcasting ARP Request for check the IP conflict using UDP sendto() function
ret = sendto(DHCP_SOCKET, (uint8_t *)"CHECK_IP_CONFLICT", 17, DHCP_allocated_ip, 5000); ret = sendto(DHCP_SOCKET, (uint8_t *)"CHECK_IP_CONFLICT", 17, DHCP_allocated_ip, 5000);
#ifdef _DHCP_DEBUG_
printf("> sendto result : %ld\r\n", ret);
#endif
// RCR value restore // RCR value restore
setRCR(tmp); setRCR(tmp);
@@ -964,7 +974,10 @@ void DHCP_init(uint8_t s, uint8_t * buf)
/* Reset the DHCP timeout count and retry count. */ /* Reset the DHCP timeout count and retry count. */
void reset_DHCP_timeout(void) void reset_DHCP_timeout(void)
{ {
dhcp_tick_1s = 0; #ifdef _DHCP_DEBUG_
printf("> reset_DHCP_timeout !!!\r\n");
#endif
// dhcp_tick_1s = 0;
dhcp_tick_next = DHCP_WAIT_TIME; dhcp_tick_next = DHCP_WAIT_TIME;
dhcp_retry_count = 0; dhcp_retry_count = 0;
} }

View File

@@ -57,7 +57,7 @@ extern "C" {
* @details If you want to display debug & processing message, Define _DHCP_DEBUG_ * @details If you want to display debug & processing message, Define _DHCP_DEBUG_
* @note If defined, it depends on <stdio.h> * @note If defined, it depends on <stdio.h>
*/ */
// #define _DHCP_DEBUG_ #define _DHCP_DEBUG_
/* Retry to processing DHCP */ /* Retry to processing DHCP */

View File

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

View File

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

View File

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

View File

@@ -1,32 +1,22 @@
#include "systick.h" #include "systick.h"
#include <stdint.h>
#include "ch32v003fun.h" #include "ch32v003fun.h"
#include "debug.h"
// ms counter // ms counter
volatile uint32_t systick_millis = 0; volatile uint32_t systick_millis = 0;
void systick_init(void) { void init_systick(void) {
// Reset any pre-existing configuration SysTick->CTLR = 0;
SysTick->CTLR = 0x0000;
// Set the compare register to trigger once per millisecond
SysTick->CMP = SYSTICK_ONE_MILLISECOND - 1; SysTick->CMP = SYSTICK_ONE_MILLISECOND - 1;
SysTick->CNT = 0;
// Reset the Count Register and variables systick_millis = 0;
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
// Enable the SysTick IRQ // Enable the SysTick IRQ
NVIC_SetPriority(SysTicK_IRQn, 0xf0);
NVIC_EnableIRQ(SysTicK_IRQn); 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) // (Make sure the IQR is lightweight and CMP value is reasonable)
SysTick->CMP += SYSTICK_ONE_MILLISECOND; SysTick->CMP += SYSTICK_ONE_MILLISECOND;
// Clear the trigger state for the next IRQ // clear irq
SysTick->SR = 0x00000000; SysTick->SR = 0;
// Increment the milliseconds count
systick_millis++; 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 callback_ip_conflict(void) { DEBUG_PRINT("Callback: IP conflict!\n"); }
void configure_network(void) { void configure_network(void) {
DEBUG_PRINT("===\n");
DEBUG_PRINT("Starting network configuration...\n"); DEBUG_PRINT("Starting network configuration...\n");
DEBUG_PRINT("===\n");
// Setup chip select and SPI callbacks // Setup chip select and SPI callbacks
reg_wizchip_cs_cbfunc(spi_select, spi_unselect); 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}; uint8_t rx_tx_buff_sizes[] = {2, 2, 2, 2, 2, 2, 2, 2};
wizchip_init(rx_tx_buff_sizes, rx_tx_buff_sizes); 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}, wiz_NetInfo net_info = {.mac = {0xEA, 0x11, 0x22, 0x33, 0x44, 0xEA},
.dhcp = NETINFO_DHCP}; .dhcp = NETINFO_DHCP};
setSHAR(net_info.mac); setSHAR(net_info.mac);
@@ -73,16 +76,14 @@ void configure_network(void) {
callback_ip_conflict); callback_ip_conflict);
// Attempt to acquire an IP address using DHCP // Attempt to acquire an IP address using DHCP
const uint32_t max_attempts = 1e5; while (!ip_assigned) {
// todo: run in a loop by a timer or sth
for (uint32_t attempt = 0; !ip_assigned && attempt < max_attempts;
++attempt) {
DHCP_run(); DHCP_run();
Delay_Ms(100);
DEBUG_PRINT("DHCP_run()...\n");
} }
if (!ip_assigned) { if (!ip_assigned) {
DEBUG_PRINT("\r\nIP was not assigned :(\r\n"); DEBUG_PRINT("\r\nIP was not assigned\r\n");
return; return;
} }