rp2040-uext-ch390-dhcp-client-9.ino
Example showing lwIP stack initialization and DHCP IP acquisition using the UEXT-CH390 module on Raspberry Pi Pico.
Requires Pico SDK and CH390 SPI library (e.g., ch390.c/h). Pins must be adapted to the specific UEXT shield used. Ensure the lwIP stack is enabled in CMake options.
rp2040-uext-ch390-dhcp-client-9.ino
#include "pico/stdlib.h"
#include "lwipopts.h"
#include "lwip/netif.h"
#include "lwip/tcpip.h"
#include "pico/cyw43_arch.h" // Nécessaire pour lwIP init même sans WiFi
// Pin mapping pour UEXT-CH390 sur RP2040 Pico
#define CH390_CS 17
#define CH390_SCK 2
#define CH390_MOSI 3
#define CH390_MISO 4
#define CH390_INT 5 // Optionnel, si utilisé pour l'interruption
#define CH390_RST 6
// Déclaration externe du driver CH390 (doit être inclus dans le projet)
extern err_t ch390_netif_init(struct netif *netif);
extern err_t ch390_netif_output(struct netif *netif, struct pbuf *p, const ip_addr_t *ipaddr);
// Structure pour le DHCP callback
struct dhcp_client {
struct netif *netif;
ip_addr_t ipaddr;
bool acquired;
};
void dhcp_callback(struct netif *netif) {
dhcp_client *client = (dhcp_client*)netif->user_data;
if (netif->ip_addr.addr != 0) {
client->ipaddr = netif->ip_addr;
client->acquired = true;
printf("DHCP Success: IP = " IPSTR "\n", IP2STR(&client->ipaddr));
} else {
printf("DHCP Failed\n");
}
}
int main() {
stdio_init_all();
printf("Starting CH390 DHCP Client...\n");
// Initialiser le hardware SPI pour CH390
// Assurez-vous que la bibliothèque CH390 est compilée et liée
// Cette partie dépend de l'implémentation spécifique du driver CH390
// Par exemple : ch390_init(CH390_CS, CH390_SCK, CH390_MOSI, CH390_MISO);
tcpip_init(NULL, NULL); // Démarrer la pile TCP/IP
// Créer une interface réseau
struct netif nif;
ip_addr_t ipaddr, netmask, gw;
IP4_ADDR(&ipaddr, 0, 0, 0, 0); // 0.0.0.0 pour DHCP
IP4_ADDR(&netmask, 0, 0, 0, 0);
IP4_ADDR(&gw, 0, 0, 0, 0);
// Ajouter l'interface avec le driver CH390
netif_add(&nif, &ipaddr, &netmask, &gw, NULL, ch390_netif_init, ethernet_input);
netif_set_default(&nif);
netif_set_up(&nif);
// Configurer le DHCP
dhcp_client client;
client.netif = &nif;
client.acquired = false;
nif.user_data = &client;
dhcp_start(&nif, dhcp_callback);
// Boucle principale
while (true) {
if (client.acquired) {
printf("IP Address: " IPSTR "\n", IP2STR(&client.ipaddr));
// Ici, vous pouvez initier des services réseau
}
sleep_ms(1000);
}
}