mongoose/examples/ti/ek-tm4c1294xl-make-baremetal-builtin/main.c

104 lines
3.6 KiB
C
Raw Normal View History

2023-03-03 03:47:09 +08:00
// Copyright (c) 2022-2023 Cesanta Software Limited
2022-11-11 05:44:53 +08:00
// All rights reserved
2023-03-03 03:47:09 +08:00
#include "hal.h"
2022-11-11 05:44:53 +08:00
#include "mongoose.h"
2023-05-27 01:43:36 +08:00
#include "net.h"
2022-11-11 05:44:53 +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-03 03:47:09 +08:00
#define LED LED1 // Use this LED for blinking
#define BLINK_PERIOD_MS 1000 // LED blinking period in millis
2022-11-11 05:44:53 +08:00
2023-03-03 03:47:09 +08:00
static volatile uint64_t s_ticks; // Milliseconds since boot
void SysTick_Handler(void) { // SyStick IRQ handler, triggered every 1ms
s_ticks++;
2022-11-11 05:44:53 +08:00
}
2023-03-03 03:47:09 +08:00
uint64_t mg_millis(void) { // Let Mongoose use our uptime function
2022-11-11 05:44:53 +08:00
return s_ticks; // Return number of milliseconds since boot
}
2023-03-03 03:47:09 +08:00
static void timer_fn(void *arg) {
gpio_toggle(LED); // Blink LED
struct mg_tcpip_if *ifp = arg; // And show
const char *names[] = {"down", "up", "req", "ready"}; // network stats
2023-03-03 03:47:09 +08:00
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-11 05:44:53 +08:00
}
2023-03-03 03:47:09 +08:00
static void ethernet_init(void) {
// Initialise Ethernet. Enable MAC GPIO pins, see
// https://www.ti.com/lit/pdf/spms433
2022-11-11 05:44:53 +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-03 03:47:09 +08:00
NVIC_EnableIRQ(EMAC0_IRQn); // Setup Ethernet IRQ handler
2022-11-11 05:44:53 +08:00
// Initialize Ethernet clocks, see datasheet section 5
// Turn Flash Prefetch off (silicon errata ETH#02)
2023-03-03 03:47:09 +08:00
uint32_t val = FLASH_CTRL->CONF;
2022-11-11 05:44:53 +08:00
val &= ~BIT(17);
val |= BIT(16);
2023-03-03 03:47:09 +08:00
FLASH_CTRL->CONF = val;
2022-11-11 05:44:53 +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
2023-03-03 03:47:09 +08:00
}
2022-11-11 05:44:53 +08:00
2023-03-03 03:47:09 +08:00
int main(void) {
gpio_output(LED); // Setup leftmost LED
uart_init(UART_DEBUG, 115200); // Initialise debug printf
ethernet_init(); // Initialise ethernet pins
MG_INFO(("Starting, CPU freq %g MHz", (double) SystemCoreClock / 1000000));
struct mg_mgr mgr; // Initialise
mg_mgr_init(&mgr); // Mongoose event manager
2022-11-11 05:44:53 +08:00
mg_log_set(MG_LL_DEBUG); // Set log level
2023-03-03 03:47:09 +08:00
// Initialise Mongoose network stack
struct mg_tcpip_driver_tm4c_data driver_data = {.mdc_cr =
1}; // See driver_tm4c.h
struct mg_tcpip_if mif = {
2023-03-07 04:32:43 +08:00
.mac = READ_PREFLASHED_MAC(),
2023-07-19 04:10:05 +08:00
// Uncomment below for static configuration:
// .ip = mg_htonl(MG_U32(192, 168, 0, 223)),
// .mask = mg_htonl(MG_U32(255, 255, 255, 0)),
// .gw = mg_htonl(MG_U32(192, 168, 0, 1)),
.driver = &mg_tcpip_driver_tm4c,
2022-11-11 05:44:53 +08:00
.driver_data = &driver_data,
};
mg_tcpip_init(&mgr, &mif);
2023-03-03 03:47:09 +08:00
uint32_t val = FLASH_CTRL->CONF; // Turn Flash Prefetch on again
2022-11-11 05:44:53 +08:00
val &= ~BIT(16);
val |= BIT(17);
2023-03-03 03:47:09 +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-03 03:47:09 +08:00
mg_mgr_poll(&mgr, 0);
}
2022-11-11 05:44:53 +08:00
2023-03-03 03:47:09 +08:00
MG_INFO(("Initialising application..."));
2023-05-27 01:43:36 +08:00
web_init(&mgr);
2023-03-03 03:47:09 +08:00
MG_INFO(("Starting event loop"));
for (;;) {
mg_mgr_poll(&mgr, 0);
}
2022-11-11 05:44:53 +08:00
return 0;
}