mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-02 13:20:51 +00:00
Add MIP - an experimental TCP/IP stack
This commit is contained in:
1 parent
47e15a3d47
commit
8e0f529a00
10 files changed
+1896
-3
No files matched your search
@@ -0,0 +1,2 @@
|
||||
# This is a collection of low-level drivers for the experimental
|
||||
# built-in TCP/IP stack
|
||||
@@ -0,0 +1,140 @@
|
||||
// Copyright (c) 2022 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#include "mip_driver_stm32.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
struct eth {
|
||||
uint32_t MACCR, MACFFR, MACHTHR, MACHTLR, MACMIIAR, MACMIIDR, MACFCR,
|
||||
MACVLANTR, RESERVED0[2], MACRWUFFR, MACPMTCSR, RESERVED1, MACDBGR, MACSR,
|
||||
MACIMR, MACA0HR, MACA0LR, MACA1HR, MACA1LR, MACA2HR, MACA2LR, MACA3HR,
|
||||
MACA3LR, RESERVED2[40], MMCCR, MMCRIR, MMCTIR, MMCRIMR, MMCTIMR,
|
||||
RESERVED3[14], MMCTGFSCCR, MMCTGFMSCCR, RESERVED4[5], MMCTGFCR,
|
||||
RESERVED5[10], MMCRFCECR, MMCRFAECR, RESERVED6[10], MMCRGUFCR,
|
||||
RESERVED7[334], PTPTSCR, PTPSSIR, PTPTSHR, PTPTSLR, PTPTSHUR, PTPTSLUR,
|
||||
PTPTSAR, PTPTTHR, PTPTTLR, RESERVED8, PTPTSSR, PTPPPSCR, RESERVED9[564],
|
||||
DMABMR, DMATPDR, DMARPDR, DMARDLAR, DMATDLAR, DMASR, DMAOMR, DMAIER,
|
||||
DMAMFBOCR, DMARSWTR, RESERVED10[8], DMACHTDR, DMACHRDR, DMACHTBAR,
|
||||
DMACHRBAR;
|
||||
};
|
||||
#define ETH ((struct eth *) 0x40028000)
|
||||
|
||||
#define BIT(x) (1UL << (x))
|
||||
#define ETH_PKT_SIZE 1540 // Max frame size
|
||||
#define ETH_DESC_CNT 4 // Descriptors count
|
||||
#define ETH_DS 4 // Descriptor size (words)
|
||||
|
||||
static uint32_t s_rxdesc[ETH_DESC_CNT][ETH_DS]; // RX descriptors
|
||||
static uint32_t s_txdesc[ETH_DESC_CNT][ETH_DS]; // TX descriptors
|
||||
static uint8_t s_rxbuf[ETH_DESC_CNT][ETH_PKT_SIZE]; // RX ethernet buffers
|
||||
static uint8_t s_txbuf[ETH_DESC_CNT][ETH_PKT_SIZE]; // TX ethernet buffers
|
||||
static void (*s_rx)(void *, size_t, void *); // Recv callback
|
||||
static void *s_rxdata; // Recv callback data
|
||||
static void *s_userdata; // Driver data
|
||||
enum { PHY_ADDR = 0, PHY_BCR = 0, PHY_BSR = 1 }; // PHY constants
|
||||
|
||||
static inline void spin(volatile uint32_t count) {
|
||||
while (count--) asm("nop");
|
||||
}
|
||||
|
||||
uint32_t eth_read_phy(uint8_t addr, uint8_t reg) {
|
||||
ETH->MACMIIAR &= (7 << 2);
|
||||
ETH->MACMIIAR |= ((uint32_t) addr << 11) | ((uint32_t) reg << 6);
|
||||
ETH->MACMIIAR |= BIT(0);
|
||||
while (ETH->MACMIIAR & BIT(0)) spin(1);
|
||||
return ETH->MACMIIDR;
|
||||
}
|
||||
|
||||
void eth_write_phy(uint8_t addr, uint8_t reg, uint32_t val) {
|
||||
ETH->MACMIIDR = val;
|
||||
ETH->MACMIIAR &= (7 << 2);
|
||||
ETH->MACMIIAR |= ((uint32_t) addr << 11) | ((uint32_t) reg << 6) | BIT(1);
|
||||
ETH->MACMIIAR |= BIT(0);
|
||||
while (ETH->MACMIIAR & BIT(0)) spin(1);
|
||||
}
|
||||
|
||||
void mip_driver_stm32_init(void *userdata) {
|
||||
s_userdata = userdata;
|
||||
|
||||
// Init RX descriptors
|
||||
for (int i = 0; i < ETH_DESC_CNT; i++) {
|
||||
s_rxdesc[i][0] = BIT(31); // Own
|
||||
s_rxdesc[i][1] = sizeof(s_rxbuf[i]) | BIT(14); // 2nd address chained
|
||||
s_rxdesc[i][2] = (uint32_t) s_rxbuf[i]; // Point to data buffer
|
||||
s_rxdesc[i][3] = (uint32_t) s_rxdesc[(i + 1) % ETH_DESC_CNT]; // Chain
|
||||
}
|
||||
|
||||
// Init TX descriptors
|
||||
for (int i = 0; i < ETH_DESC_CNT; i++) {
|
||||
s_txdesc[i][2] = (uint32_t) s_txbuf[i]; // Buf pointer
|
||||
s_txdesc[i][3] = (uint32_t) s_txdesc[(i + 1) % ETH_DESC_CNT]; // Chain
|
||||
}
|
||||
|
||||
ETH->DMABMR |= BIT(0); // Software reset
|
||||
while ((ETH->DMABMR & BIT(0)) != 0) spin(1); // Wait until done
|
||||
// NOTE(cpq): we do not use extended descriptor bit 7, and do not use
|
||||
// hardware checksum. Therefore, descriptor size is 4, not 8
|
||||
// ETH->DMABMR = BIT(13) | BIT(16) | BIT(22) | BIT(23) | BIT(25);
|
||||
ETH->MACIMR = BIT(3) | BIT(9); // Mask timestamp & PMT IT
|
||||
ETH->MACMIIAR = 4 << 2; // MDC clock 150-216 MHz, 38.8.1
|
||||
ETH->MACFCR = BIT(7); // Disable zero quarta pause
|
||||
ETH->MACFFR = BIT(31); // Receive all
|
||||
eth_write_phy(PHY_ADDR, PHY_BCR, BIT(15)); // Reset PHY
|
||||
eth_write_phy(PHY_ADDR, PHY_BCR, BIT(12)); // Set autonegotiation
|
||||
ETH->DMARDLAR = (uint32_t) s_rxdesc; // RX descriptors
|
||||
ETH->DMATDLAR = (uint32_t) s_txdesc; // RX descriptors
|
||||
ETH->DMAIER = BIT(6) | BIT(16); // RIE, NISE
|
||||
ETH->MACCR = BIT(2) | BIT(3) | BIT(11) | BIT(14); // RE, TE, Duplex, Fast
|
||||
ETH->DMAOMR = BIT(1) | BIT(13) | BIT(21) | BIT(25); // SR, ST, TSF, RSF
|
||||
}
|
||||
|
||||
void mip_driver_stm32_setrx(void (*rx)(void *, size_t, void *), void *rxdata) {
|
||||
s_rx = rx;
|
||||
s_rxdata = rxdata;
|
||||
}
|
||||
|
||||
static uint32_t s_txno;
|
||||
size_t mip_driver_stm32_tx(const void *buf, size_t len, void *userdata) {
|
||||
if (len > sizeof(s_txbuf[s_txno])) {
|
||||
printf("%s: frame too big, %ld\n", __func__, (long) len);
|
||||
len = 0; // Frame is too big
|
||||
} else if ((s_txdesc[s_txno][0] & BIT(31))) {
|
||||
printf("%s: no free descr\n", __func__);
|
||||
len = 0; // All descriptors are busy, fail
|
||||
} else {
|
||||
memcpy(s_txbuf[s_txno], buf, len); // Copy data
|
||||
s_txdesc[s_txno][1] = (uint32_t) len; // Set data len
|
||||
s_txdesc[s_txno][0] = BIT(20) | BIT(28) | BIT(29) | BIT(30); // Chain,FS,LS
|
||||
s_txdesc[s_txno][0] |= BIT(31); // Set OWN bit - let DMA take over
|
||||
if (++s_txno >= ETH_DESC_CNT) s_txno = 0;
|
||||
}
|
||||
uint32_t sr = ETH->DMASR;
|
||||
if (sr & BIT(2)) ETH->DMASR = BIT(2), ETH->DMATPDR = 0; // Resume
|
||||
if (sr & BIT(5)) ETH->DMASR = BIT(5), ETH->DMATPDR = 0; // if busy
|
||||
if (len == 0) printf("E: D0 %lx, DMASR %lx\n", s_txdesc[0][0], sr);
|
||||
return len;
|
||||
(void) userdata;
|
||||
}
|
||||
|
||||
bool mip_driver_stm32_status(void *userdata) {
|
||||
uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR);
|
||||
return bsr & BIT(2) ? 1 : 0;
|
||||
(void) userdata;
|
||||
}
|
||||
|
||||
void ETH_IRQHandler(void) {
|
||||
volatile uint32_t sr = ETH->DMASR;
|
||||
if (sr & BIT(6)) { // Frame received, loop
|
||||
for (uint32_t i = 0; i < ETH_DESC_CNT; i++) {
|
||||
if (s_rxdesc[i][0] & BIT(31)) continue;
|
||||
uint32_t len = ((s_rxdesc[i][0] >> 16) & (BIT(14) - 1));
|
||||
// printf("%lx %lu %lx %lx\n", i, len, s_rxdesc[i][0], sr);
|
||||
if (s_rx != NULL) s_rx(s_rxbuf[i], len > 4 ? len - 4 : len, s_rxdata);
|
||||
s_rxdesc[i][0] = BIT(31);
|
||||
}
|
||||
}
|
||||
if (sr & BIT(7)) ETH->DMARPDR = 0; // Resume RX
|
||||
ETH->DMASR = sr & ~(BIT(2) | BIT(7)); // Clear status
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright (c) 2022 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void mip_driver_stm32_init(void *userdata);
|
||||
bool mip_driver_stm32_status(void *);
|
||||
void mip_driver_stm32_setrx(void (*rx)(void *, size_t, void *), void *);
|
||||
size_t mip_driver_stm32_tx(const void *buf, size_t len, void *);
|
||||
@@ -0,0 +1,99 @@
|
||||
// Copyright (c) 2022 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// This software is dual-licensed: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License version 3 as
|
||||
// published by the Free Software Foundation. For the terms of this
|
||||
// license, see http://www.fsf.org/licensing/licenses/agpl-3.0.html
|
||||
//
|
||||
// You are free to use this software under the terms of the GNU General
|
||||
// Public License, but WITHOUT ANY WARRANTY; without even the implied
|
||||
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
// See the GNU General Public License for more details.
|
||||
//
|
||||
// Alternatively, you can license this software under a commercial
|
||||
// license, please contact us at https://cesanta.com/contact.html
|
||||
|
||||
#pragma once
|
||||
|
||||
struct w5500 {
|
||||
uint8_t mac[6]; // MAC address
|
||||
void *spi; // Opaque SPI bus descriptor
|
||||
uint8_t (*txn)(void *, uint8_t); // SPI transaction
|
||||
void (*begin)(void *); // SPI begin
|
||||
void (*end)(void *); // SPI end
|
||||
};
|
||||
|
||||
enum { W5500_CR = 0, W5500_S0 = 1, W5500_TX0 = 2, W5500_RX0 = 3 };
|
||||
|
||||
static inline void w5500_txn(struct w5500 *w, uint8_t block, uint16_t addr,
|
||||
bool wr, void *buf, size_t len) {
|
||||
uint8_t *p = buf, cmd[] = {(uint8_t) (addr >> 8), (uint8_t) (addr & 255),
|
||||
(uint8_t) ((block << 3) | (wr ? 4 : 0))};
|
||||
w->begin(w->spi);
|
||||
for (size_t i = 0; i < sizeof(cmd); i++) w->txn(w->spi, cmd[i]);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
uint8_t r = w->txn(w->spi, p[i]);
|
||||
if (!wr) p[i] = r;
|
||||
}
|
||||
w->end(w->spi);
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
static inline void w5500_wn(struct w5500 *w, uint8_t block, uint16_t addr, void *buf, size_t len) { w5500_txn(w, block, addr, true, buf, len); }
|
||||
static inline void w5500_w1(struct w5500 *w, uint8_t block, uint16_t addr, uint8_t val) { w5500_wn(w, block, addr, &val, 1); }
|
||||
static inline void w5500_w2(struct w5500 *w, uint8_t block, uint16_t addr, uint16_t val) { uint8_t buf[2] = {(uint8_t) (val >> 8), (uint8_t) (val & 255)}; w5500_wn(w, block, addr, buf, sizeof(buf)); }
|
||||
static inline void w5500_rn(struct w5500 *w, uint8_t block, uint16_t addr, void *buf, size_t len) { w5500_txn(w, block, addr, false, buf, len); }
|
||||
static inline uint8_t w5500_r1(struct w5500 *w, uint8_t block, uint16_t addr) { uint8_t r = 0; w5500_rn(w, block, addr, &r, 1); return r; }
|
||||
static inline uint16_t w5500_r2(struct w5500 *w, uint8_t block, uint16_t addr) { uint8_t buf[2] = {0, 0}; w5500_rn(w, block, addr, buf, sizeof(buf)); return (uint16_t) ((buf[0] << 8) | buf[1]); }
|
||||
// clang-format on
|
||||
|
||||
static inline uint8_t w5500_status(struct w5500 *w) {
|
||||
return w5500_r1(w, W5500_CR, 0x2e);
|
||||
}
|
||||
|
||||
static inline uint16_t w5500_rx(struct w5500 *w, uint8_t *buf, uint16_t len) {
|
||||
uint16_t r = 0, n = 0, n2; // Read recv len
|
||||
while ((n2 = w5500_r2(w, W5500_S0, 0x26)) > n) n = n2; // Until it is stable
|
||||
// printf("RSR: %d\n", (int) n);
|
||||
if (n > 0) {
|
||||
uint16_t ptr = w5500_r2(w, W5500_S0, 0x28); // Get read pointer
|
||||
n = w5500_r2(w, W5500_RX0, ptr); // Read frame length
|
||||
if (n <= len + 2) r = n - 2, w5500_rn(w, W5500_RX0, ptr + 2, buf, r);
|
||||
w5500_w2(w, W5500_S0, 0x28, ptr + n); // Advance read pointer
|
||||
w5500_w1(w, W5500_S0, 1, 0x40); // Sock0 CR -> RECV
|
||||
// printf(" RX_RD: tot=%u n=%u r=%u\n", n2, n, r);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline uint16_t w5500_tx(struct w5500 *w, void *buf, uint16_t len) {
|
||||
uint16_t n = 0;
|
||||
while (n < len) n = w5500_r2(w, W5500_S0, 0x20); // Wait for space
|
||||
uint16_t ptr = w5500_r2(w, W5500_S0, 0x24); // Get write pointer
|
||||
w5500_wn(w, W5500_TX0, ptr, buf, len); // Write data
|
||||
w5500_w2(w, W5500_S0, 0x24, ptr + len); // Advance write pointer
|
||||
w5500_w1(w, W5500_S0, 1, 0x20); // Sock0 CR -> SEND
|
||||
for (int i = 0; i < 40; i++) {
|
||||
uint8_t ir = w5500_r1(w, W5500_S0, 2); // Read S0 IR
|
||||
if (ir == 0) continue;
|
||||
// printf("IR %d, len=%d, free=%d, ptr %d\n", ir, (int) len, (int) n, ptr);
|
||||
w5500_w1(w, W5500_S0, 2, ir); // Write S0 IR: clear it!
|
||||
if (ir & 8) len = 0; // Timeout. Report error
|
||||
if (ir & (16 | 8)) break; // Stop on SEND_OK or timeout
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
static inline bool w5500_init(struct w5500 *w) {
|
||||
w->end(w->spi);
|
||||
w5500_w1(w, W5500_CR, 0, 0x80); // Reset chip: CR -> 0x80
|
||||
w5500_w1(w, W5500_CR, 0x2e, 0); // CR PHYCFGR -> reset
|
||||
w5500_w1(w, W5500_CR, 0x2e, 0xf8); // CR PHYCFGR -> set
|
||||
// w5500_wn(w, W5500_CR, 9, w->mac, 6); // Set source MAC
|
||||
w5500_w1(w, W5500_S0, 0x1e, 16); // Sock0 RX buf size
|
||||
w5500_w1(w, W5500_S0, 0x1f, 16); // Sock0 TX buf size
|
||||
w5500_w1(w, W5500_S0, 0, 4); // Sock0 MR -> MACRAW
|
||||
w5500_w1(w, W5500_S0, 1, 1); // Sock0 CR -> OPEN
|
||||
return w5500_r1(w, W5500_S0, 3) == 0x42; // Sock0 SR == MACRAW
|
||||
}
|
||||
Reference in new issue
Block a user