From 34049c9e979a499396ad9973f29b1027979efc90 Mon Sep 17 00:00:00 2001 From: LailaTheElf Date: Wed, 26 Nov 2025 18:30:41 +0100 Subject: [PATCH] try to add dhcp --- src/config.h | 6 +++-- src/main.cpp | 69 +++++++++++++++++++++++++++++++++++++++--------- src/max31856.cpp | 4 +-- src/w5500_rp.cpp | 49 ++++++++++++++++++++++++++++++++-- src/w5500_rp.h | 4 ++- 5 files changed, 113 insertions(+), 19 deletions(-) diff --git a/src/config.h b/src/config.h index 0df5bca..4c1088d 100644 --- a/src/config.h +++ b/src/config.h @@ -6,15 +6,17 @@ #define PIN_RGB_G 16 #define PIN_RGB_B 25 +#define SPI_DRIVER spi0 +#define SPI_BOUD 1E6 #define PIN_SPI_CLK 2 #define PIN_SPI_MISO 4 #define PIN_SPI_MOSI 3 #define PIN_MAX31856_CS 1 #define PIN_DISPLAY_CS 0 #define PIN_DISPLAY_DC 7 -#define PIN_DISPLAY_RST 6 +#define PIN_RST 6 #define PIN_W5500_CS 29 -#define W5500_SPI_DRIVER spi0 +#define W5500_SPI_DRIVER SPI_DRIVER #endif diff --git a/src/main.cpp b/src/main.cpp index e709bba..99e8e59 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,10 +9,11 @@ #include "config.h" #include "max31856.h" #include "w5500_rp.h" +#include "wizchip_conf.h" max31856_t temp[2]; Adafruit_SPIDevice spidev(PIN_DISPLAY_CS, PIN_SPI_CLK, PIN_SPI_MISO, PIN_SPI_MOSI); -Adafruit_SSD1306 display(128, 64, &spidev, PIN_DISPLAY_DC, PIN_DISPLAY_RST, PIN_DISPLAY_CS); +Adafruit_SSD1306 display(128, 64, &spidev, PIN_DISPLAY_DC, PIN_RST, PIN_DISPLAY_CS); void init_rbg() { @@ -42,16 +43,32 @@ void rgb_rainbow() int main() { stdio_init_all(); + gpio_init(PIN_RST); + gpio_set_dir(PIN_RST, GPIO_OUT); + gpio_put(PIN_RST, 1); init_rbg(); + rgb_rainbow(); + gpio_put(PIN_RST, 0); + + gpio_set_function(PIN_SPI_CLK, GPIO_FUNC_SPI); + gpio_set_function(PIN_SPI_MOSI, GPIO_FUNC_SPI); + gpio_set_function(PIN_SPI_MISO, GPIO_FUNC_SPI); + spi_init(SPI_DRIVER, SPI_BOUD); + spi_set_format(SPI_DRIVER, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST); + display.begin(); + display.fillScreen(SSD1306_BLACK); + display.setTextColor(SSD1306_WHITE); + display.setTextSize(2); //12x16 + display.setCursor(64-18, 32-8); + display.print("hoi"); + display.display(); // init temp sensors + spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER); temp[0].pin_cs = PIN_MAX31856_CS; max31856_init(&(temp[0])); //temp[1].pin_cs = 5; //max31856_init(&(temp[1])); - gpio_set_function(PIN_SPI_CLK, GPIO_FUNC_SPI); - gpio_set_function(PIN_SPI_MOSI, GPIO_FUNC_SPI); - gpio_set_function(PIN_SPI_MISO, GPIO_FUNC_SPI); // config temp sensor 0 max31856_write8(&(temp[0]), MAX31856_REG_CR0, MAX31856_REG_CR0_CMODE | /*MAX31856_REG_CR0_OCFAULT |*/ MAX31856_REG_CR0_50HZ); max31856_write8(&(temp[0]), MAX31856_REG_CR1, MAX31856_REG_CR1_AVGSEL_4 | MAX31856_REG_CR1_TC_TYPE_K); @@ -61,12 +78,24 @@ int main() { // init ethernet w5500_rp_init(); - - display.begin(); - display.fillScreen(SSD1306_BLACK); - display.setTextColor(SSD1306_WHITE); - display.setTextSize(2); - display.display(); + wiz_NetInfo netinfo = { + .mac={'E', 'L', 'F', 177, 31, 252}, + .ip={192, 254, 18, 91}, + .sn={255, 255, 0, 0}, // subnet mask + .gw={0, 0, 0, 0}, + .dns={0, 0, 0, 0}, + .dhcp=NETINFO_DHCP + }; + wizchip_setnetinfo(&netinfo); + rgb_rainbow(); + if (!w5500_rp_dhcp()) { + display.fillScreen(SSD1306_BLACK); + display.setCursor(64-24, 32-8); + display.print("fail"); + display.display(); + sleep_ms(1000); + return 1; + } rgb_rainbow(); @@ -74,6 +103,7 @@ int main() { { char cj_str[8]; char tc_str[8]; + char ip[16]; spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER); sleep_ms(5); @@ -82,13 +112,28 @@ int main() { max31856_temp_t tc = max31856_get_tc_temp_c(&temp[0]); snprintf(&tc_str[0], 8, "%i.%02uC", tc.i, tc.f); - printf("CJ temp: %s\n", &cj_str[0]); - printf("TC temp: %s\n", &tc_str[0]); + netinfo = { + .mac={255, 255, 255, 255, 255, 255}, + .ip={255, 255, 255, 255}, + .sn={255, 255, 255, 255}, // subnet mask + .gw={255, 255, 255, 255}, + .dns={255, 255, 255, 255}, + .dhcp=NETINFO_STATIC + }; + wizchip_getnetinfo(&netinfo); + w5500_rp_print_netinfo(netinfo); + snprintf(&ip[0], 16, "%u.%u.%u.%u", netinfo.ip[0], netinfo.ip[1], netinfo.ip[2], netinfo.ip[3]); + + // printf("CJ temp: %s\n", &cj_str[0]); + // printf("TC temp: %s\n", &tc_str[0]); spi_set_format(spi0, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST); sleep_ms(5); display.fillScreen(SSD1306_BLACK); display.setCursor(0, 0); + display.setTextSize(1); + display.println(&ip[0]); + display.setTextSize(2); display.print("CJ "); display.println(&cj_str[0]); display.print("TC "); diff --git a/src/max31856.cpp b/src/max31856.cpp index 8c316a2..d5fc52f 100644 --- a/src/max31856.cpp +++ b/src/max31856.cpp @@ -13,8 +13,8 @@ void max31856_init(max31856_t *max) gpio_put(max->pin_cs, 1); // init SPI - spi_init(SPI_MAX31856, SPI_MAX31856_BOUD); - spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER); + // spi_init(SPI_MAX31856, SPI_MAX31856_BOUD); + // spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER); max->initilzed = true; } diff --git a/src/w5500_rp.cpp b/src/w5500_rp.cpp index 67226ac..7dc1ba2 100644 --- a/src/w5500_rp.cpp +++ b/src/w5500_rp.cpp @@ -1,9 +1,12 @@ - +#include +#include +#include #include #include -#include #include "hardware/spi.h" #include +#include +#include #include "config.h" #include "w5500_rp.h" @@ -26,15 +29,57 @@ void w5500_rp_spi_read_burst(uint8_t *buffer, uint16_t size) { } void w5500_rp_spi_cs_select() { + spi_set_format(W5500_SPI_DRIVER, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST); gpio_put(PIN_W5500_CS, 0); + printf("pling\n"); } void w5500_rp_spi_cs_deselect() { gpio_put(PIN_W5500_CS, 1); } void w5500_rp_init() { + gpio_init(PIN_W5500_CS); + gpio_set_dir(PIN_W5500_CS, GPIO_OUT); + reg_wizchip_spi_cbfunc(&w5500_rp_spi_read_byte, &w5500_rp_spi_write_byte); reg_wizchip_spiburst_cbfunc(&w5500_rp_spi_read_burst, &w5500_rp_spi_write_burst); reg_wizchip_cs_cbfunc(w5500_rp_spi_cs_select, w5500_rp_spi_cs_deselect); reg_wizchip_cris_cbfunc(NULL, NULL); } + +void w5500_rp_print_netinfo(wiz_NetInfo netinfo) { + printf("IP: %3u.%3u.%3u.%3u\n", netinfo.ip[0], netinfo.ip[1], netinfo.ip[2], netinfo.ip[3]); + printf("mask: %3u.%3u.%3u.%3u\n", netinfo.sn[0], netinfo.sn[1], netinfo.sn[2], netinfo.sn[3]); + printf("gate: %3u.%3u.%3u.%3u\n", netinfo.gw[0], netinfo.gw[1], netinfo.gw[2], netinfo.gw[3]); + printf("dns: %3u.%3u.%3u.%3u\n", netinfo.dns[0], netinfo.dns[1], netinfo.dns[2], netinfo.dns[3]); + printf("mac: %02x:%02x:%02x:%02x:%02x:%02x\n", netinfo.mac[0], netinfo.mac[1], netinfo.mac[2], netinfo.mac[3], netinfo.mac[4], netinfo.mac[5]); +} + +bool dhcp_assigned = false; +uint8_t dhcp_buffer[1024]; + +void w5500_rp_dhcp_assigned() { + dhcp_assigned = true; +} + +bool w5500_rp_dhcp() { + DHCP_init(1, &dhcp_buffer[0]); + reg_dhcp_cbfunc(NULL, NULL, NULL); + + uint64_t second_time = time_us_64() + 1E6; + uint8_t seconds = 0; + while (DHCP_run() != DHCP_IP_LEASED) { + if (second_time > time_us_64()) { + second_time += 1E6; + DHCP_time_handler(); + seconds++; + if (seconds > 60) { + DHCP_stop(); + return false; + } + } + sleep_ms(100); + } + DHCP_stop(); + return true; +} \ No newline at end of file diff --git a/src/w5500_rp.h b/src/w5500_rp.h index dc2f23f..0c3a542 100644 --- a/src/w5500_rp.h +++ b/src/w5500_rp.h @@ -1,8 +1,10 @@ #ifndef W5500_RP_H #define W5500_RP_H -#include "hardware/spi.h" +#include void w5500_rp_init(); +void w5500_rp_print_netinfo(wiz_NetInfo netinfo); +bool w5500_rp_dhcp(); #endif