This commit is contained in:
2025-12-12 23:05:27 +06:00
parent 537e7bfe10
commit 64bb06f292
4 changed files with 23 additions and 26 deletions

Submodule ch32fun updated: d38b104838...b0764004f5

View File

@@ -150,10 +150,11 @@ static inline void i2c_init(void) {
RCC->APB2PCENR |= RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO; RCC->APB2PCENR |= RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO;
RCC->APB1PCENR |= RCC_APB1Periph_I2C1; RCC->APB1PCENR |= RCC_APB1Periph_I2C1;
// PB6 (SCL) and PB7 (SDA) AFIO->PCFR1 |= AFIO_PCFR1_I2C1_REMAP; // I2C1_RM: 1
GPIOB->CFGLR &= ~(0xFF << (4 * 6));
GPIOB->CFGLR |= ((GPIO_Speed_10MHz | GPIO_CNF_OUT_OD_AF) << (4 * 6)) | // PB8 (SCL) and PB9 (SDA)
((GPIO_Speed_10MHz | GPIO_CNF_OUT_OD_AF) << (4 * 7)); funPinMode(PB8, GPIO_CFGLR_OUT_10Mhz_AF_OD);
funPinMode(PB9, GPIO_CFGLR_OUT_10Mhz_AF_OD);
// Reset I2C1 to init all regs // Reset I2C1 to init all regs
RCC->APB1PRSTR |= RCC_APB1Periph_I2C1; RCC->APB1PRSTR |= RCC_APB1Periph_I2C1;

View File

@@ -70,6 +70,15 @@ err_t ethernetif_init(struct netif* netif) {
static err_t low_level_output(struct netif* netif, struct pbuf* p) { static err_t low_level_output(struct netif* netif, struct pbuf* p) {
(void)netif; (void)netif;
// single-segment pbuf
if (p->next == NULL && p->len <= ETH_TX_BUF_SIZE) {
if (eth_send_packet(p->payload, p->len) == 0) {
LINK_STATS_INC(link.xmit);
return ERR_OK;
}
}
// chain of pbufs
static uint8_t tx_buffer[ETH_TX_BUF_SIZE]; static uint8_t tx_buffer[ETH_TX_BUF_SIZE];
uint32_t total_len = 0; uint32_t total_len = 0;
@@ -82,21 +91,13 @@ static err_t low_level_output(struct netif* netif, struct pbuf* p) {
total_len += q->len; total_len += q->len;
} }
// send packet via driver if (eth_send_packet(tx_buffer, total_len) == 0) {
int result = eth_send_packet(tx_buffer, total_len);
if (result == -1) {
// tx queue full
LINK_STATS_INC(link.drop);
return ERR_BUF;
} else if (result == -2) {
// invalid length
LINK_STATS_INC(link.err);
return ERR_ARG;
}
LINK_STATS_INC(link.xmit); LINK_STATS_INC(link.xmit);
return ERR_OK; return ERR_OK;
}
LINK_STATS_INC(link.drop);
return ERR_BUF;
} }
void ethernetif_input(struct netif* netif) { void ethernetif_input(struct netif* netif) {
@@ -108,13 +109,8 @@ void ethernetif_input(struct netif* netif) {
struct pbuf* p = pbuf_alloc(PBUF_RAW, length, PBUF_POOL); struct pbuf* p = pbuf_alloc(PBUF_RAW, length, PBUF_POOL);
if (p != NULL) { if (p != NULL) {
// copy packet into pbuf chain // usually contiguous in PBUF_POOL
uint32_t offset = 0; pbuf_take(p, packet, length);
for (struct pbuf* q = p; q != NULL; q = q->next) {
memcpy(q->payload, packet + offset, q->len);
offset += q->len;
}
LINK_STATS_INC(link.recv); LINK_STATS_INC(link.recv);
// pass to lwIP // pass to lwIP

View File

@@ -11,7 +11,7 @@
// #define ETHARP_DEBUG LWIP_DBG_ON // #define ETHARP_DEBUG LWIP_DBG_ON
#define NO_SYS 1 #define NO_SYS 1
#define SYS_LIGHTWEIGHT_PROT 1 #define SYS_LIGHTWEIGHT_PROT 0
#define LWIP_NETCONN 0 #define LWIP_NETCONN 0
#define LWIP_SOCKET 0 #define LWIP_SOCKET 0