2023-03-07 02:57:51 +08:00
|
|
|
// Copyright (c) 2022-2023 Cesanta Software Limited
|
2022-11-25 03:32:05 +08:00
|
|
|
// All rights reserved
|
|
|
|
|
2023-03-07 02:57:51 +08:00
|
|
|
#include "hal.h"
|
2022-11-25 03:32:05 +08:00
|
|
|
#include "mongoose.h"
|
2023-05-27 01:43:36 +08:00
|
|
|
#include "net.h"
|
2022-11-25 03:32:05 +08:00
|
|
|
|
|
|
|
#define LED1 PIN('N', 1) // On-board LED pin
|
|
|
|
#define LED2 PIN('N', 0) // On-board LED pin
|
|
|
|
#define LED3 PIN('F', 4) // On-board LED pin
|
|
|
|
#define LED4 PIN('F', 0) // On-board LED pin
|
|
|
|
|
2023-03-07 02:57:51 +08:00
|
|
|
#define LED LED1 // Use this LED for blinking
|
|
|
|
#define BLINK_PERIOD_MS 1000 // LED blinking period in millis
|
|
|
|
|
|
|
|
static void timer_fn(void *arg) {
|
|
|
|
struct mg_tcpip_if *ifp = arg; // And show
|
|
|
|
const char *names[] = {"down", "up", "ready"}; // network stats
|
|
|
|
MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u",
|
|
|
|
names[ifp->state], mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent,
|
|
|
|
ifp->ndrop, ifp->nerr));
|
2022-11-25 03:32:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ethernet_init(void) {
|
2023-03-07 02:57:51 +08:00
|
|
|
// Initialise Ethernet. Enable MAC GPIO pins, see
|
|
|
|
// https://www.ti.com/lit/pdf/spms433
|
2022-11-25 03:32:05 +08:00
|
|
|
// Assign LED3 and LED4 to the EPHY, "activity" and "link", respectively.
|
|
|
|
// (20.4.2.4)
|
|
|
|
gpio_init(LED3, GPIO_MODE_AF, GPIO_OTYPE_PUSH_PULL, GPIO_SPEED_HIGH,
|
|
|
|
GPIO_PULL_NONE, 5); // EN0LED1
|
|
|
|
gpio_init(LED4, GPIO_MODE_AF, GPIO_OTYPE_PUSH_PULL, GPIO_SPEED_HIGH,
|
|
|
|
GPIO_PULL_NONE, 5); // EN0LED0
|
2023-03-07 02:57:51 +08:00
|
|
|
NVIC_EnableIRQ(EMAC0_IRQn); // Setup Ethernet IRQ handler
|
2022-11-25 03:32:05 +08:00
|
|
|
// Initialize Ethernet clocks, see datasheet section 5
|
|
|
|
// Turn Flash Prefetch off (silicon errata ETH#02)
|
2023-03-07 02:57:51 +08:00
|
|
|
uint32_t val = FLASH_CTRL->CONF;
|
2022-11-25 03:32:05 +08:00
|
|
|
val &= ~BIT(17);
|
|
|
|
val |= BIT(16);
|
2023-03-07 02:57:51 +08:00
|
|
|
FLASH_CTRL->CONF = val;
|
2022-11-25 03:32:05 +08:00
|
|
|
SYSCTL->RCGCEMAC |= BIT(0); // Enable EMAC clock
|
|
|
|
SYSCTL->SREMAC |= BIT(0); // Reset EMAC
|
|
|
|
SYSCTL->SREMAC &= ~BIT(0);
|
|
|
|
SYSCTL->RCGCEPHY |= BIT(0); // Enable EPHY clock
|
|
|
|
SYSCTL->SREPHY |= BIT(0); // Reset EPHY
|
|
|
|
SYSCTL->SREPHY &= ~BIT(0);
|
|
|
|
while (!(SYSCTL->PREMAC & BIT(0)) || !(SYSCTL->PREPHY & BIT(0)))
|
|
|
|
spin(1); // Wait for reset to complete
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server(void *args) {
|
|
|
|
struct mg_mgr mgr; // Initialise Mongoose event manager
|
2023-03-15 20:43:22 +08:00
|
|
|
mg_mgr_init(&mgr); // and attach it to the interface
|
2022-11-25 03:32:05 +08:00
|
|
|
mg_log_set(MG_LL_DEBUG); // Set log level
|
|
|
|
|
|
|
|
// Initialise Mongoose network stack
|
2022-12-19 19:43:25 +08:00
|
|
|
// Specify MAC address, and IP/mask/GW in network byte order for static
|
|
|
|
// IP configuration. If IP/mask/GW are unset, DHCP is going to be used
|
2022-11-25 03:32:05 +08:00
|
|
|
ethernet_init();
|
2023-03-07 02:57:51 +08:00
|
|
|
struct mg_tcpip_driver_tm4c_data driver_data = {.mdc_cr = 1};
|
2023-02-08 05:16:42 +08:00
|
|
|
struct mg_tcpip_if mif = {
|
2023-03-07 04:32:43 +08:00
|
|
|
.mac = READ_PREFLASHED_MAC(),
|
2023-02-08 05:16:42 +08:00
|
|
|
.driver = &mg_tcpip_driver_tm4c,
|
2022-11-25 03:32:05 +08:00
|
|
|
.driver_data = &driver_data,
|
|
|
|
};
|
2023-02-08 05:16:42 +08:00
|
|
|
mg_tcpip_init(&mgr, &mif);
|
2023-03-07 02:57:51 +08:00
|
|
|
uint32_t val = FLASH_CTRL->CONF; // Turn Flash Prefetch on again
|
2022-11-25 03:32:05 +08:00
|
|
|
val &= ~BIT(16);
|
|
|
|
val |= BIT(17);
|
2023-03-07 02:57:51 +08:00
|
|
|
FLASH_CTRL->CONF = val;
|
|
|
|
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &mif);
|
|
|
|
|
|
|
|
MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
|
2023-03-09 22:25:02 +08:00
|
|
|
while (mif.state != MG_TCPIP_STATE_READY) {
|
2023-03-07 02:57:51 +08:00
|
|
|
mg_mgr_poll(&mgr, 0);
|
|
|
|
}
|
2022-11-25 03:32:05 +08:00
|
|
|
|
2023-03-07 02:57:51 +08:00
|
|
|
MG_INFO(("Initialising application..."));
|
2023-05-27 01:43:36 +08:00
|
|
|
web_init(&mgr);
|
2023-03-07 02:57:51 +08:00
|
|
|
|
|
|
|
MG_INFO(("Starting event loop"));
|
|
|
|
for (;;) mg_mgr_poll(&mgr, 1); // Infinite event loop
|
|
|
|
(void) args;
|
2022-11-25 03:32:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void blinker(void *args) {
|
2023-03-07 02:57:51 +08:00
|
|
|
gpio_init(LED, GPIO_MODE_OUTPUT, GPIO_OTYPE_PUSH_PULL, GPIO_SPEED_HIGH,
|
2022-11-25 03:32:05 +08:00
|
|
|
GPIO_PULL_NONE, 0);
|
|
|
|
for (;;) {
|
2023-03-07 02:57:51 +08:00
|
|
|
gpio_toggle(LED);
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(BLINK_PERIOD_MS));
|
2022-11-25 03:32:05 +08:00
|
|
|
}
|
2023-03-07 02:57:51 +08:00
|
|
|
(void) args;
|
2022-11-25 03:32:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2023-02-07 10:10:30 +08:00
|
|
|
uart_init(UART_DEBUG, 115200); // Initialise UART
|
2023-03-07 02:57:51 +08:00
|
|
|
|
|
|
|
// Start tasks. NOTE: stack sizes are in 32-bit words
|
2022-11-25 03:32:05 +08:00
|
|
|
xTaskCreate(blinker, "blinker", 128, ":)", configMAX_PRIORITIES - 1, NULL);
|
|
|
|
xTaskCreate(server, "server", 2048, 0, configMAX_PRIORITIES - 1, NULL);
|
2023-03-07 02:57:51 +08:00
|
|
|
|
2022-11-25 03:32:05 +08:00
|
|
|
vTaskStartScheduler(); // This blocks
|
|
|
|
return 0;
|
|
|
|
}
|