Skip to content

Commit 66a077c

Browse files
committed
Fix SPI. Check for Ethernet up
1 parent c541373 commit 66a077c

File tree

3 files changed

+31
-38
lines changed

3 files changed

+31
-38
lines changed

step-7-webserver/pico-w5500/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ firmware.uf2: firmware.bin $(BIN2UF2)
3131

3232
# Requires env variable VCON_API_KEY set
3333
DEVICE_URL ?= https://dash.vcon.io/api/v3/devices/1
34-
test: build
35-
curl --fail -su :$(VCON_API_KEY) $(DEVICE_URL)/ota?uf2=1 --data-binary @firmware.uf2
34+
test: update
3635
curl --fail -su :$(VCON_API_KEY) $(DEVICE_URL)/tx?t=5 | tee /tmp/output.txt
37-
grep 'Ethernet: ' /tmp/output.txt
36+
grep 'Ethernet: up' /tmp/output.txt
37+
38+
update: build
39+
curl --fail -su :$(VCON_API_KEY) $(DEVICE_URL)/ota?uf2=1 --data-binary @firmware.uf2
3840

3941
clean:
4042
$(RM) firmware.* bin2uf2

step-7-webserver/pico-w5500/main.c

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@
44
#include "mcu.h"
55
#include "mongoose.h"
66

7-
enum { LED = 25, UART_TX = 0, UART_RX = 1 }; // Pins
7+
enum { LED = 25 }; // LED pins
8+
enum { UART_TX = 0, UART_RX = 1 }; // UART pins
89
enum { SPI_CS = 17, SPI_CLK = 18, SPI_TX = 19, SPI_RX = 16 }; // SPI pins
910
enum { STATUS_TIMER_MS = 1000, BLINK_TIMER_MS = 500 }; // Timeouts
1011

11-
void my_spi_begin(void *spi) {
12-
spi_begin(spi);
13-
}
14-
void my_spi_end(void *spi) {
15-
spi_end(spi);
16-
}
17-
uint8_t my_spi_txn(void *spi, uint8_t byte) {
18-
uint8_t result = spi_txn(spi, byte);
19-
// MG_INFO(("%x -> %x", byte, result));
20-
return result;
21-
}
12+
void my_spi_begin(void *spi) { spi_begin(spi); }
13+
void my_spi_end(void *spi) { spi_end(spi); }
14+
uint8_t my_spi_txn(void *spi, uint8_t byte) { return spi_txn(spi, byte); }
2215

2316
void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
2417
if (ev == MG_EV_HTTP_MSG) {
@@ -27,14 +20,12 @@ void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
2720
(void) ev_data, (void) fn_data;
2821
}
2922

30-
static volatile uint32_t s_ticks;
23+
static volatile uint64_t s_ticks;
3124
void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms
3225
s_ticks++;
3326
}
3427

35-
uint64_t mg_millis(void) {
36-
return s_ticks;
37-
}
28+
uint64_t mg_millis(void) { return s_ticks; }
3829

3930
int main(void) {
4031
clock_init(); // Init clocks
@@ -45,8 +36,9 @@ int main(void) {
4536

4637
// Init SPI
4738
struct spi spi0 = {
48-
.miso = SPI_TX, .mosi = SPI_RX, .clk = SPI_CLK, .cs = SPI_CS, .spin = 50};
49-
spi_init(&spi0);
39+
.miso = SPI_RX, .mosi = SPI_TX, .clk = SPI_CLK, .cs = SPI_CS, .spin = 50};
40+
bool ok = spi_init(&spi0);
41+
MG_INFO(("SPI init: %d", ok));
5042

5143
// Init Mongoose
5244
struct mip_spi spi = {&spi0, my_spi_begin, my_spi_end, my_spi_txn};
@@ -56,23 +48,23 @@ int main(void) {
5648
struct mg_mgr mgr; // Declare event manager
5749
mg_mgr_init(&mgr); // Init event manager
5850
mg_log_set(MG_LL_DEBUG); // Set DEBUG log level
59-
//mip_init(&mgr, &mif); // Init TCP/IP stack
60-
//mg_http_listen(&mgr, "http://0.0.0.0", fn, NULL); // HTTP listener
51+
mip_init(&mgr, &mif); // Init TCP/IP stack
52+
mg_http_listen(&mgr, "http://0.0.0.0", fn, NULL); // HTTP listener
6153

6254
bool led_on = false; // Initial LED state
63-
uint32_t status_timer = 0, blink_timer = 0; // Initial timer expirations
55+
uint64_t status_timer = 0, blink_timer = 0; // Initial timer expirations
6456

6557
// Infinite event manager loop
6658
for (;;) {
67-
if (timer_expired(&blink_timer, BLINK_TIMER_MS, s_ticks)) {
68-
led_on = !led_on; // Flip LED state
69-
// if (mip_driver_w5500.up(&mif)) led_on = true; // Always on if Eth up
70-
gpio_write(LED, led_on); // Set LED
59+
if (mg_timer_expired(&blink_timer, BLINK_TIMER_MS, mg_millis())) {
60+
led_on = !led_on; // Flip LED state
61+
if (mip_driver_w5500.up(&mif)) led_on = true; // Always on if Eth up
62+
gpio_write(LED, led_on); // Set LED
7163
}
72-
if (timer_expired(&status_timer, STATUS_TIMER_MS, s_ticks)) {
73-
MG_INFO(("Ethernet: %s", mip_driver_w5500.up(&mif) ? "up" : "down"));
64+
if (mg_timer_expired(&status_timer, STATUS_TIMER_MS, mg_millis())) {
65+
MG_INFO(("Ethernet: %s", mif.driver->up(&mif) ? "up" : "down"));
7466
}
75-
// mg_mgr_poll(&mgr, 1);
67+
mg_mgr_poll(&mgr, 1);
7668
}
7769

7870
return 0;

step-7-webserver/pico-w5500/mcu.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ static inline void nvic_set_prio(uint8_t irq, uint8_t prio) {
4040
NVIC->IP[irq >> 2] = ((uint32_t) (NVIC->IP[irq >> 2] & ~(255U << shift)) |
4141
(((prio << 6U) & (uint32_t) 255UL) << shift));
4242
}
43-
static inline void nvic_enable_irq(uint8_t irq) {
44-
NVIC->ISER[0] = BIT(irq);
45-
}
43+
static inline void nvic_enable_irq(uint8_t irq) { NVIC->ISER[0] = BIT(irq); }
4644

4745
struct scb {
4846
volatile uint32_t CPUID, ICSR, VTOR, AIRCR, SCR, CCR; // Incomplete
@@ -139,9 +137,7 @@ static inline void gpio_write(uint16_t pin, bool val) {
139137
}
140138
}
141139

142-
static inline bool gpio_read(uint16_t pin) {
143-
return SIO->GPIO_IN & BIT(pin);
144-
}
140+
static inline bool gpio_read(uint16_t pin) { return SIO->GPIO_IN & BIT(pin); }
145141

146142
// t: expiration time, prd: period, now: current time. Return true if expired
147143
static inline bool timer_expired(uint32_t *t, uint32_t prd, uint32_t now) {
@@ -217,19 +213,22 @@ static inline void uart_write_byte(struct uart *uart, uint8_t byte) {
217213
while (uart->FR & BIT(5)) (void) 0; // Wait while TX FIFO is full
218214
uart->DR = byte;
219215
}
216+
220217
static inline void uart_write_buf(struct uart *uart, char *buf, size_t len) {
221218
while (len-- > 0) uart_write_byte(uart, *(uint8_t *) buf++);
222219
}
220+
223221
static inline int uart_read_ready(struct uart *uart) {
224222
return uart->FR & BIT(4);
225223
}
224+
226225
static inline uint8_t uart_read_byte(struct uart *uart) {
227226
return (uint8_t) (uart->DR & 255);
228227
}
229228

230229
// Bit-bang SPI implementation
231230
struct spi {
232-
int miso, mosi, clk, cs; // Pins
231+
int miso, mosi, clk, cs; // SPI Pins
233232
int spin; // Number of NOP spins for bitbanging
234233
};
235234

0 commit comments

Comments
 (0)