i broke it
This commit is contained in:
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -15,6 +15,7 @@
|
||||
"spi_dma.h": "c",
|
||||
"dhcp.h": "c",
|
||||
"debug.h": "c",
|
||||
"systick.h": "c"
|
||||
"systick.h": "c",
|
||||
"timer.h": "c"
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,14 @@
|
||||
|
||||
#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)
|
||||
|
||||
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
|
||||
|
||||
9
include/timer.h
Normal file
9
include/timer.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
|
||||
#include "ch32v003fun.h"
|
||||
|
||||
// Function prototypes
|
||||
void tim2_init(void);
|
||||
|
||||
#endif // TIMER_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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ extern "C" {
|
||||
* @details If you want to display debug & processing message, Define _DHCP_DEBUG_
|
||||
* @note If defined, it depends on <stdio.h>
|
||||
*/
|
||||
// #define _DHCP_DEBUG_
|
||||
#define _DHCP_DEBUG_
|
||||
|
||||
|
||||
/* Retry to processing DHCP */
|
||||
|
||||
@@ -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
|
||||
|
||||
47
src/main.c
47
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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
42
src/timer.c
Normal 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();
|
||||
}
|
||||
}
|
||||
15
src/w5500.c
15
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user