4
4
#include "mcu.h"
5
5
#include "mongoose.h"
6
6
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
8
9
enum { SPI_CS = 17 , SPI_CLK = 18 , SPI_TX = 19 , SPI_RX = 16 }; // SPI pins
9
10
enum { STATUS_TIMER_MS = 1000 , BLINK_TIMER_MS = 500 }; // Timeouts
10
11
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 ); }
22
15
23
16
void fn (struct mg_connection * c , int ev , void * ev_data , void * fn_data ) {
24
17
if (ev == MG_EV_HTTP_MSG ) {
@@ -27,14 +20,12 @@ void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
27
20
(void ) ev_data , (void ) fn_data ;
28
21
}
29
22
30
- static volatile uint32_t s_ticks ;
23
+ static volatile uint64_t s_ticks ;
31
24
void SysTick_Handler (void ) { // SyStick IRQ handler, triggered every 1ms
32
25
s_ticks ++ ;
33
26
}
34
27
35
- uint64_t mg_millis (void ) {
36
- return s_ticks ;
37
- }
28
+ uint64_t mg_millis (void ) { return s_ticks ; }
38
29
39
30
int main (void ) {
40
31
clock_init (); // Init clocks
@@ -45,8 +36,9 @@ int main(void) {
45
36
46
37
// Init SPI
47
38
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 ));
50
42
51
43
// Init Mongoose
52
44
struct mip_spi spi = {& spi0 , my_spi_begin , my_spi_end , my_spi_txn };
@@ -56,23 +48,23 @@ int main(void) {
56
48
struct mg_mgr mgr ; // Declare event manager
57
49
mg_mgr_init (& mgr ); // Init event manager
58
50
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
61
53
62
54
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
64
56
65
57
// Infinite event manager loop
66
58
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
71
63
}
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" ));
74
66
}
75
- // mg_mgr_poll(&mgr, 1);
67
+ mg_mgr_poll (& mgr , 1 );
76
68
}
77
69
78
70
return 0 ;
0 commit comments