fix: works?

This commit is contained in:
2025-11-07 23:15:53 +06:00
parent 1403c53d32
commit 90e8a20a8b
5 changed files with 358 additions and 361 deletions

125
main.c
View File

@@ -10,113 +10,132 @@
#include "netif/ethernet.h"
#include "systick.h"
#define LED1_PIN 0
#define LED2_PIN 2
#define LED1_PIN 0 // PA0
#define LED2_PIN 2 // PA2
#define HSE_STARTUP_TIMEOUT 10000
#define PLL_LOCK_TIMEOUT 10000
#define LED_TOGGLE_INTERVAL_MS 500
#define LINK_POLL_INTERVAL_MS 500
#define RCC_PREDIV1_OFFSET 0
#define HSE_CLOCK_MHZ 32
#define PREDIV1_DIVISOR 4
#define PLL_MULTIPLIER 15
struct netif g_netif;
void init_leds() {
RCC->APB2PCENR |= RCC_APB2Periph_GPIOA;
GPIOA->CFGLR &= ~((0xf << (4 * 0)) | (0xf << (4 * 2)));
GPIOA->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP) << (4 * 0);
GPIOA->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP) << (4 * 2);
}
int clock_init(void);
void led_init(void);
void lwip_stack_init(void);
int main() {
SystemInit();
// 1. HSE (32MHz)
// 2. PREDIV1 / 4.
// 3. PLL source = HSE, PLL x15.
// 4. (32MHz / 4) * 15 = 120MHz SYSCLK
RCC->INTR = 0x009F0000;
int clock_init(void) {
RCC->INTR = 0x009f0000;
RCC->CTLR &= ~(RCC_HSE_ON | RCC_PLLON);
RCC->CFGR0 = 0x00000000;
RCC->CTLR |= RCC_HSE_ON;
int timeout;
for (timeout = 10000; timeout > 0; timeout--) {
if (RCC->CTLR & RCC_HSERDY) break; // wait for HSE
}
if (timeout == 0) {
printf("Error: HSE failed to start\n");
return -1;
RCC->CTLR |= RCC_HSE_ON;
for (int timeout = HSE_STARTUP_TIMEOUT; timeout > 0; timeout--) {
if (RCC->CTLR & RCC_HSERDY) break;
if (timeout == 1) {
printf("Error: HSE failed to start\n");
return -1;
}
}
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
RCC->CFGR2 = ((3) << 0); // PREDIV1 divisor = 3+1 = 4
RCC->CFGR2 = (PREDIV1_DIVISOR - 1);
RCC->CFGR0 |= RCC_PLLSource_HSE_Div1 | RCC_PLLMul_15;
RCC->CTLR |= RCC_PLLON;
printf("Main PLL enabled. Waiting for lock...\n");
for (timeout = 10000; timeout > 0; timeout--) {
printf("Main PLL en. Waiting for lock...\n");
for (int timeout = PLL_LOCK_TIMEOUT; timeout > 0; timeout--) {
if (RCC->CTLR & RCC_PLLRDY) break;
}
if (timeout == 0) {
printf("error: main pll lock failed\n");
return -1;
if (timeout == 1) {
printf("Error: Main PLL lock failed\n");
return -1;
}
}
printf("Main PLL Locked\n");
RCC->CFGR0 = (RCC->CFGR0 & ~RCC_SW) | RCC_SW_PLL;
while ((RCC->CFGR0 & RCC_SWS) != RCC_SWS_PLL);
printf("SysClock set to 120MHz\n");
printf("System clock set to %dMHz.\n",
(HSE_CLOCK_MHZ / PREDIV1_DIVISOR) * PLL_MULTIPLIER);
return 0;
}
systick_init();
init_leds();
void led_init(void) {
RCC->APB2PCENR |= RCC_APB2Periph_GPIOA;
GPIOA->CFGLR &= ~((0xf << (4 * LED1_PIN)) | (0xf << (4 * LED2_PIN)));
GPIOA->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP) << (4 * LED1_PIN);
GPIOA->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP) << (4 * LED2_PIN);
}
void lwip_stack_init(void) {
ip_addr_t ipaddr, netmask, gw;
lwip_init();
ip_addr_t ipaddr, netmask, gw;
IP4_ADDR(&ipaddr, 0, 0, 0, 0);
IP4_ADDR(&netmask, 0, 0, 0, 0);
IP4_ADDR(&gw, 0, 0, 0, 0);
netif_add(&g_netif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init,
&ethernet_input);
// netif_set_link_callback(&g_netif, link_status_callback);
netif_set_default(&g_netif);
netif_set_up(&g_netif);
printf("Starting DHCP client...\n");
dhcp_start(&g_netif);
}
uint32_t last_led_toggle = 0;
uint32_t last_send_time = 0;
int main() {
SystemInit();
if (clock_init() != 0) {
// eating dirt?
while (1);
}
systick_init();
led_init();
lwip_stack_init();
uint32_t last_led_toggle_time = 0;
uint32_t last_link_poll_time = 0;
int led_state = 0;
int ip_address_printed = 0;
while (1) {
ethernetif_link_poll(&g_netif);
ethernetif_input(&g_netif);
if (netif_is_link_up(&g_netif)) {
ethernetif_input(&g_netif);
if (millis() - last_link_poll_time > LINK_POLL_INTERVAL_MS) {
ethernetif_link_poll(&g_netif);
last_link_poll_time = millis();
}
sys_check_timeouts();
// run_tx_test();
uint32_t now = millis();
if (now - last_led_toggle > 500) {
if (now - last_led_toggle_time > LED_TOGGLE_INTERVAL_MS) {
if (led_state) {
GPIOA->BSHR = (1 << 0);
GPIOA->BSHR = (1 << LED1_PIN);
} else {
GPIOA->BSHR = (1 << (0 + 16));
GPIOA->BSHR = (1 << (LED1_PIN + 16));
}
led_state = !led_state;
last_led_toggle = now;
last_led_toggle_time = now;
}
static int ip_printed = 0;
if (g_netif.ip_addr.addr != 0 && !ip_printed) {
if (!ip_address_printed && g_netif.ip_addr.addr != 0) {
printf("IP address assigned: %s\n",
ip4addr_ntoa(netif_ip4_addr(&g_netif)));
ip_printed = 1;
ip_address_printed = 1;
GPIOA->BSHR = (1 << LED2_PIN);
}
}
}