From 7d33688e7550bdd2b73f998b17f258560459202e Mon Sep 17 00:00:00 2001 From: kuwoyuki Date: Mon, 14 Oct 2024 02:13:05 +0600 Subject: [PATCH] i broke it --- .vscode/settings.json | 3 +- include/systick.h | 8 ++++-- include/timer.h | 9 ++++++ include/w5500.h | 2 ++ lib/ioLibrary_Driver/DHCP/dhcp.c | 17 ++++++++++-- lib/ioLibrary_Driver/DHCP/dhcp.h | 2 +- src/funconfig.h | 2 ++ src/main.c | 47 +++++++++----------------------- src/spi_dma.c | 3 ++ src/systick.c | 32 +++++++--------------- src/timer.c | 42 ++++++++++++++++++++++++++++ src/w5500.c | 15 +++++----- 12 files changed, 112 insertions(+), 70 deletions(-) create mode 100644 include/timer.h create mode 100644 src/timer.c diff --git a/.vscode/settings.json b/.vscode/settings.json index fbd54d8..df05f40 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,6 +15,7 @@ "spi_dma.h": "c", "dhcp.h": "c", "debug.h": "c", - "systick.h": "c" + "systick.h": "c", + "timer.h": "c" } } \ No newline at end of file diff --git a/include/systick.h b/include/systick.h index 9b8df87..0f4e55c 100644 --- a/include/systick.h +++ b/include/systick.h @@ -3,12 +3,14 @@ #include -#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) -void systick_init(void); +void init_systick(void); // ms counter incremented by SysTick -extern volatile uint32_t systick_millis; +// extern volatile uint32_t systick_millis; #endif // SYSTICK_H diff --git a/include/timer.h b/include/timer.h new file mode 100644 index 0000000..1a774bc --- /dev/null +++ b/include/timer.h @@ -0,0 +1,9 @@ +#ifndef TIMER_H +#define TIMER_H + +#include "ch32v003fun.h" + +// Function prototypes +void tim2_init(void); + +#endif // TIMER_H diff --git a/include/w5500.h b/include/w5500.h index 4e3fa84..02a01c3 100644 --- a/include/w5500.h +++ b/include/w5500.h @@ -24,6 +24,8 @@ void handle_ip_assigned(void); // Initializes the W5500 chip void configure_network(void); +void configure_dhcp(void); + // resolves a domain name void resolve_domain_name(const char* domain_name); diff --git a/lib/ioLibrary_Driver/DHCP/dhcp.c b/lib/ioLibrary_Driver/DHCP/dhcp.c index 87f4db9..741cc06 100644 --- a/lib/ioLibrary_Driver/DHCP/dhcp.c +++ b/lib/ioLibrary_Driver/DHCP/dhcp.c @@ -251,7 +251,7 @@ uint8_t check_DHCP_timeout(void); void reset_DHCP_timeout(void); /* 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 */ void default_ip_assign(void) @@ -749,6 +749,9 @@ uint8_t DHCP_run(void) if (check_DHCP_leasedIP()) { // Network info assignment from DHCP dhcp_ip_assign(); +#ifdef _DHCP_DEBUG_ + printf("> DHCP Success\r\n"); +#endif reset_DHCP_timeout(); dhcp_state = STATE_DHCP_LEASED; @@ -901,10 +904,17 @@ int8_t check_DHCP_leasedIP(void) tmp = getRCR(); 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 // 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); +#ifdef _DHCP_DEBUG_ + printf("> sendto result : %ld\r\n", ret); +#endif + // RCR value restore setRCR(tmp); @@ -964,7 +974,10 @@ void DHCP_init(uint8_t s, uint8_t * buf) /* Reset the DHCP timeout count and retry count. */ 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_retry_count = 0; } diff --git a/lib/ioLibrary_Driver/DHCP/dhcp.h b/lib/ioLibrary_Driver/DHCP/dhcp.h index f5c36f6..658703c 100644 --- a/lib/ioLibrary_Driver/DHCP/dhcp.h +++ b/lib/ioLibrary_Driver/DHCP/dhcp.h @@ -57,7 +57,7 @@ extern "C" { * @details If you want to display debug & processing message, Define _DHCP_DEBUG_ * @note If defined, it depends on */ -// #define _DHCP_DEBUG_ +#define _DHCP_DEBUG_ /* Retry to processing DHCP */ diff --git a/src/funconfig.h b/src/funconfig.h index c4a704d..4def413 100644 --- a/src/funconfig.h +++ b/src/funconfig.h @@ -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 diff --git a/src/main.c b/src/main.c index bcddce9..01aa937 100644 --- a/src/main.c +++ b/src/main.c @@ -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); diff --git a/src/spi_dma.c b/src/spi_dma.c index 59d4215..a5435d9 100644 --- a/src/spi_dma.c +++ b/src/spi_dma.c @@ -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); } diff --git a/src/systick.c b/src/systick.c index 604ed2f..29dc5c9 100644 --- a/src/systick.c +++ b/src/systick.c @@ -1,32 +1,22 @@ #include "systick.h" -#include - #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++; } \ No newline at end of file diff --git a/src/timer.c b/src/timer.c new file mode 100644 index 0000000..2f53dd0 --- /dev/null +++ b/src/timer.c @@ -0,0 +1,42 @@ +#include "timer.h" + +#include +#include + +#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(); + } +} diff --git a/src/w5500.c b/src/w5500.c index 58b7c41..60dabbc 100644 --- a/src/w5500.c +++ b/src/w5500.c @@ -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; }