generated from LailaTheElf/rp2040_c
85 lines
2.5 KiB
C++
85 lines
2.5 KiB
C++
#include <hardware/timer.h>
|
|
#include <pico/time.h>
|
|
#include <stdio.h>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include "hardware/spi.h"
|
|
#include <pico/stdlib.h>
|
|
#include <wizchip_conf.h>
|
|
#include <dhcp.h>
|
|
|
|
#include "config.h"
|
|
#include "w5500_rp.h"
|
|
|
|
void w5500_rp_spi_write_byte(uint8_t byte) {
|
|
spi_write_blocking(W5500_SPI_DRIVER, &byte, 1);
|
|
}
|
|
uint8_t w5500_rp_spi_read_byte() {
|
|
uint8_t byte;
|
|
spi_read_blocking(W5500_SPI_DRIVER, 0, &byte, 1);
|
|
return byte;
|
|
}
|
|
|
|
void w5500_rp_spi_write_burst(uint8_t *buffer, uint16_t size) {
|
|
spi_write_blocking(W5500_SPI_DRIVER, buffer, size);
|
|
}
|
|
void w5500_rp_spi_read_burst(uint8_t *buffer, uint16_t size) {
|
|
uint8_t byte;
|
|
spi_read_blocking(W5500_SPI_DRIVER, 0, buffer, 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;
|
|
} |