|
1 | 1 | #include <SPI.h>
|
2 | 2 | #include "mongoose.h"
|
3 | 3 |
|
4 |
| -#define SS_PIN 17 // Slave select pin |
5 |
| -struct mg_mgr mgr; // Mongoose event manager |
| 4 | +#define LED_PIN 21 // Slave select pin |
| 5 | +#define SS_PIN 17 // Slave select pin |
| 6 | + |
6 | 7 | struct mg_tcpip_spi spi = {
|
7 |
| - NULL, // SPI data |
8 |
| - [](void *) { digitalWrite(SS_PIN, LOW); }, // begin transation |
9 |
| - [](void *) { digitalWrite(SS_PIN, HIGH); }, // end transaction |
10 |
| - [](void *, uint8_t c) { return SPI.transfer(c); }, // execute transaction |
| 8 | + NULL, // SPI metadata |
| 9 | + [](void *) { digitalWrite(SS_PIN, LOW); SPI.beginTransaction(SPISettings()); }, |
| 10 | + [](void *) { digitalWrite(SS_PIN, HIGH); SPI.endTransaction(); }, |
| 11 | + [](void *, uint8_t c) { return SPI.transfer(c); }, // Execute transaction |
11 | 12 | };
|
12 |
| -struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // network interface |
| 13 | +struct mg_mgr mgr; // Mongoose event manager |
| 14 | +struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // Network interface |
| 15 | + |
| 16 | +uint64_t mg_millis(void) { |
| 17 | + return millis(); |
| 18 | +} |
13 | 19 |
|
14 | 20 | void setup() {
|
15 |
| - Serial.begin(115200); |
16 |
| - mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL); |
17 |
| - mg_log_set(MG_LL_DEBUG); |
18 |
| - while (!Serial) (void) 0; |
19 |
| - |
20 |
| - pinMode(SS_PIN, OUTPUT); |
21 |
| - SPI.begin(); |
22 |
| - |
23 |
| - mg_mgr_init(&mgr); |
24 |
| - // delay(3000); |
25 |
| - MG_INFO(("Starting TCP/IP stack...")); |
26 |
| - SPI.transfer(0); |
27 |
| - MG_INFO(("--->...")); |
28 |
| - |
29 |
| - mif.driver = &mg_tcpip_driver_w5500; |
30 |
| - mif.driver_data = &spi; |
31 |
| - mg_tcpip_init(&mgr, &mif); |
32 |
| - |
33 |
| - // Start a 5 sec timer, print status message periodically |
34 |
| - mg_timer_add( |
35 |
| - &mgr, 5000, MG_TIMER_REPEAT, |
36 |
| - [](void *) { |
37 |
| - MG_INFO(("ethernet: %s", mg_tcpip_driver_w5500.up(&mif) ? "up" : "down")); |
38 |
| - }, |
39 |
| - NULL); |
| 21 | + Serial.begin(115200); // Initialise serial |
| 22 | + while (!Serial) delay(50); // for debug output |
| 23 | + |
| 24 | + SPI.begin(); // Iniitialise SPI |
| 25 | + pinMode(SS_PIN, OUTPUT); // to communicate with W5500 Ethernet module |
| 26 | + pinMode(LED_PIN, OUTPUT); // Initialise LED |
| 27 | + |
| 28 | + mg_mgr_init(&mgr); // Initialise Mongoose event manager |
| 29 | + mg_log_set(MG_LL_DEBUG); // Set debug log level |
| 30 | + mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL); // Log serial |
| 31 | + mif.driver = &mg_tcpip_driver_w5500; // Use W5500 built-in driver |
| 32 | + mif.driver_data = &spi; // Pass SPI interface to W5500 driver |
| 33 | + mg_tcpip_init(&mgr, &mif); // Initialise built-in TCP/IP stack |
40 | 34 |
|
41 | 35 | // Setup HTTP listener. Respond "ok" on any HTTP request
|
42 | 36 | mg_http_listen(
|
|
0 commit comments