Prusa MINI Firmware overview
|
#include "main.h"
#include "lwip/opt.h"
#include "lwip/timeouts.h"
#include "netif/ethernet.h"
#include "netif/etharp.h"
#include "lwip/ethip6.h"
#include "ethernetif.h"
#include <string.h>
#include "cmsis_os.h"
#include "lwip/netifapi.h"
#include "otp.h"
◆ TIME_WAITING_FOR_INPUT
File Name : ethernetif.c Description : This file provides code for the configuration of the ethernetif.c MiddleWare.
This notice applies to any and all portions of this file that are not between comment pairs USER CODE BEGIN and USER CODE END. Other portions of this file, whether inserted by the user or by software development tools are owned by their respective copyright owners.
Copyright (c) 2019 STMicroelectronics International N.V. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted, provided that the following conditions are met:
- Redistribution of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of STMicroelectronics nor the names of other contributors to this software may be used to endorse or promote products derived from this software without specific written permission.
- This software, including modifications and/or derivative works of this software, must execute solely and exclusively on microcontroller or microprocessor devices manufactured by or for STMicroelectronics.
- Redistribution and use of this software other than as permitted under this license is void and will automatically terminate your rights under this license.
THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
◆ INTERFACE_THREAD_STACK_SIZE
#define INTERFACE_THREAD_STACK_SIZE (350) |
◆ IFNAME0
◆ IFNAME1
◆ HAL_ETH_MspInit()
void HAL_ETH_MspInit |
( |
ETH_HandleTypeDef * |
ethHandle | ) |
|
ETH GPIO Configuration PC1 ---—> ETH_MDC PA1 ---—> ETH_REF_CLK PA2 ---—> ETH_MDIO PA7 ---—> ETH_CRS_DV PC4 ---—> ETH_RXD0 PC5 ---—> ETH_RXD1 PB11 ---—> ETH_TX_EN PB12 ---—> ETH_TXD0 PB13 ---—> ETH_TXD1
118 if (ethHandle->Instance == ETH) {
123 __HAL_RCC_ETH_CLK_ENABLE();
140 GPIO_InitStruct.
Alternate = GPIO_AF11_ETH;
147 GPIO_InitStruct.
Alternate = GPIO_AF11_ETH;
154 GPIO_InitStruct.
Alternate = GPIO_AF11_ETH;
◆ HAL_ETH_MspDeInit()
void HAL_ETH_MspDeInit |
( |
ETH_HandleTypeDef * |
ethHandle | ) |
|
ETH GPIO Configuration PC1 ---—> ETH_MDC PA1 ---—> ETH_REF_CLK PA2 ---—> ETH_MDIO PA7 ---—> ETH_CRS_DV PC4 ---—> ETH_RXD0 PC5 ---—> ETH_RXD1 PB11 ---—> ETH_TX_EN PB12 ---—> ETH_TXD0 PB13 ---—> ETH_TXD1
167 if (ethHandle->Instance == ETH) {
172 __HAL_RCC_ETH_CLK_DISABLE();
◆ HAL_ETH_RxCpltCallback()
void HAL_ETH_RxCpltCallback |
( |
ETH_HandleTypeDef * |
heth | ) |
|
Ethernet Rx Transfer completed callback.
- Parameters
-
- Return values
-
◆ ethernetif_link()
213 uint32_t phyreg = 0U;
221 netifapi_netif_set_link_up(
netif);
224 netifapi_netif_set_link_down(
netif);
◆ low_level_init()
static void low_level_init |
( |
struct netif * |
netif | ) |
|
|
static |
In this function, the hardware should be initialized. Called from ethernetif_init().
- Parameters
-
netif | the already initialized lwip network interface structure for this ethernetif |
245 uint32_t regvalue = 0;
251 heth.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE;
255 heth.Init.RxMode = ETH_RXINTERRUPT_MODE;
256 heth.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
257 heth.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;
263 hal_eth_init_status = HAL_ETH_Init(&
heth);
265 if (hal_eth_init_status ==
HAL_OK) {
270 HAL_ETH_DMATxDescListInit(&
heth, DMATxDscrTab, &Tx_Buff[0][0],
ETH_TXBUFNB);
273 HAL_ETH_DMARxDescListInit(&
heth, DMARxDscrTab, &Rx_Buff[0][0],
ETH_RXBUFNB);
275 #if LWIP_ARP || LWIP_ETHERNET
307 HAL_ETH_Start(&
heth);
◆ low_level_output()
static err_t low_level_output |
( |
struct netif * |
netif, |
|
|
struct pbuf * |
p |
|
) |
| |
|
static |
This function should do the actual transmission of the packet. The packet is contained in the pbuf that is passed to the function. This pbuf might be chained.
- Parameters
-
netif | the lwip network interface structure for this ethernetif |
p | the MAC packet to send (e.g. IP packet including MAC addresses and type) |
- Returns
- ERR_OK if the packet could be sent an err_t value if the packet couldn't be sent
- Note
- Returning ERR_MEM here if a DMA queue of your MAC is full can lead to strange results. You might consider waiting for space in the DMA queue to become availale since the stack doesn't retry to send a packet dropped because of memory failure (except for the TCP timers).
357 __IO ETH_DMADescTypeDef *DmaTxDesc;
358 uint32_t framelength = 0;
359 uint32_t bufferoffset = 0;
360 uint32_t byteslefttocopy = 0;
361 uint32_t payloadoffset = 0;
362 DmaTxDesc =
heth.TxDesc;
366 for (q = p; q !=
NULL; q = q->
next) {
368 if ((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)
RESET) {
374 byteslefttocopy = q->
len;
383 DmaTxDesc = (ETH_DMADescTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
386 if ((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)
RESET) {
391 buffer = (
uint8_t *)(DmaTxDesc->Buffer1Addr);
401 bufferoffset = bufferoffset + byteslefttocopy;
402 framelength = framelength + byteslefttocopy;
406 HAL_ETH_TransmitFrame(&
heth, framelength);
413 if ((
heth.Instance->DMASR & ETH_DMASR_TUS) != (uint32_t)
RESET) {
415 heth.Instance->DMASR = ETH_DMASR_TUS;
418 heth.Instance->DMATPDR = 0;
◆ low_level_input()
static struct pbuf* low_level_input |
( |
struct netif * |
netif | ) |
|
|
static |
Should allocate a pbuf and transfer the bytes of the incoming packet from the interface into the pbuf.
- Parameters
-
netif | the lwip network interface structure for this ethernetif |
- Returns
- a pbuf filled with the received packet (including MAC header) NULL on memory error
436 __IO ETH_DMADescTypeDef *dmarxdesc;
437 uint32_t bufferoffset = 0;
438 uint32_t payloadoffset = 0;
439 uint32_t byteslefttocopy = 0;
443 if (HAL_ETH_GetReceivedFrame_IT(&
heth) !=
HAL_OK)
447 len =
heth.RxFrameInfos.length;
456 dmarxdesc =
heth.RxFrameInfos.FSRxDesc;
458 for (q = p; q !=
NULL; q = q->
next) {
459 byteslefttocopy = q->
len;
468 dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
469 buffer = (
uint8_t *)(dmarxdesc->Buffer1Addr);
477 bufferoffset = bufferoffset + byteslefttocopy;
483 dmarxdesc =
heth.RxFrameInfos.FSRxDesc;
485 for (
i = 0;
i <
heth.RxFrameInfos.SegCount;
i++) {
486 dmarxdesc->Status |= ETH_DMARXDESC_OWN;
487 dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
491 heth.RxFrameInfos.SegCount = 0;
494 if ((
heth.Instance->DMASR & ETH_DMASR_RBUS) != (uint32_t)
RESET) {
496 heth.Instance->DMASR = ETH_DMASR_RBUS;
498 heth.Instance->DMARPDR = 0;
◆ ethernetif_input()
This function should be called when a packet is ready to be read from the interface. It uses the function low_level_input() that should handle the actual reception of bytes from the network interface. Then the type of the received packet is determined and the appropriate input function is called.
- Parameters
-
netif | the lwip network interface structure for this ethernetif |
◆ low_level_output_arp_off()
static err_t low_level_output_arp_off |
( |
struct netif * |
netif, |
|
|
struct pbuf * |
q, |
|
|
const ip4_addr_t * |
ipaddr |
|
) |
| |
|
static |
This function has to be completed by user in case of ARP OFF.
- Parameters
-
netif | the lwip network interface structure for this ethernetif |
- Returns
- ERR_OK if ...
◆ ethernetif_init()
Should be called at the beginning of the program to set up the network interface. It calls the function low_level_init() to do the actual setup of the hardware.
This function should be passed as a parameter to netif_add().
- Parameters
-
netif | the lwip network interface structure for this ethernetif |
- Returns
- ERR_OK if the loopif is initialized ERR_MEM if private data couldn't be allocated any other err_t on error
564 #if LWIP_NETIF_HOSTNAME
566 netif->hostname =
"lwip";
577 #if LWIP_ARP || LWIP_ETHERNET
579 netif->output = etharp_output;
588 netif->output_ip6 = ethip6_output;
◆ sys_jiffies()
Returns the current time in milliseconds when LWIP_TIMERS == 1 and NO_SYS == 1.
- Parameters
-
- Return values
-
◆ __ALIGN_END
◆ s_xSemaphore
◆ heth
#define OTP_MAC_ADDRESS_ADDR
Definition: otp.h:18
#define PHY_ISFR_INT4
Definition: stm32f4xx_hal_conf.h:230
#define GPIO_MODE_AF_PP
Definition: stm32f4xx_hal_gpio.h:138
u16_t len
Definition: pbuf.h:159
bool error
Definition: auto_build.py:637
#define osThreadDef(name, thread, priority, instances, stacksz)
Definition: cmsis_os.h:468
Definition: stm32f4xx_hal_def.h:57
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
#define RESET
Definition: dac_mcp4728.h:34
#define osSemaphore(name)
Definition: cmsis_os.h:709
int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec)
Wait until a Semaphore token becomes available.
Definition: cmsis_os.c:822
uint32_t Mode
Definition: stm32f4xx_hal_gpio.h:68
#define GPIO_PIN_13
Definition: stm32f4xx_hal_gpio.h:115
#define GPIO_PIN_7
Definition: stm32f4xx_hal_gpio.h:109
netif_input_fn input
Definition: netif.h:244
#define GPIO_PIN_2
Definition: stm32f4xx_hal_gpio.h:104
#define NETIF_FLAG_ETHARP
Definition: netif.h:91
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]
Definition: netif.h:303
uint32_t Alternate
Definition: stm32f4xx_hal_gpio.h:77
uint8_t i
Definition: screen_test_graph.c:72
struct pbuf * next
Definition: pbuf.h:144
uint32_t Pull
Definition: stm32f4xx_hal_gpio.h:71
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:715
u16_t mtu
Definition: netif.h:299
void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
u8_t flags
Definition: netif.h:305
ETH_HandleTypeDef heth
Definition: ethernetif.c:108
#define TIME_WAITING_FOR_INPUT
Definition: ethernetif.c:69
#define NULL
Definition: usbd_def.h:53
netif_linkoutput_fn linkoutput
Definition: netif.h:255
#define ETH_TX_BUF_SIZE
Definition: stm32f4xx_hal_conf.h:187
#define PHY_LINKED_STATUS
Definition: stm32f4xx_hal_conf.h:220
osStatus osDelay(uint32_t millisec)
Wait for Timeout (Time Delay)
Definition: cmsis_os.c:365
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
Definition: stm32f4xx_hal.c:339
HAL_StatusTypeDef
HAL Status structures definition.
Definition: stm32f4xx_hal_def.h:55
#define INTERFACE_THREAD_STACK_SIZE
Definition: ethernetif.c:71
osSemaphoreId osSemaphoreCreate(const osSemaphoreDef_t *semaphore_def, int32_t count)
Create and Initialize a Semaphore object used for managing resources.
Definition: cmsis_os.c:755
#define osSemaphoreDef(name)
Definition: cmsis_os.h:700
#define netif_is_link_up(netif)
Definition: netif.h:413
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:248
#define GPIO_PIN_12
Definition: stm32f4xx_hal_gpio.h:114
function completed; no error or event occurred.
Definition: cmsis_os.h:235
#define ETH_TXBUFNB
Definition: stm32f4xx_hal_conf.h:189
static err_t low_level_output_arp_off(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr)
Definition: ethernetif.c:536
#define ETH_RXBUFNB
Definition: stm32f4xx_hal_conf.h:188
u8_t hwaddr_len
Definition: netif.h:301
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
#define GPIO_PIN_1
Definition: stm32f4xx_hal_gpio.h:103
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
char name[2]
Definition: netif.h:307
#define GPIO_NOPULL
Definition: stm32f4xx_hal_gpio.h:170
#define IFNAME1
Definition: ethernetif.c:74
if(size<=((png_alloc_size_t) -1) - ob)
Definition: pngwrite.c:2176
#define GPIO_SPEED_FREQ_VERY_HIGH
Definition: stm32f4xx_hal_gpio.h:161
void ethernetif_input(void const *argument)
Definition: ethernetif.c:511
osSemaphoreId s_xSemaphore
Definition: ethernetif.c:106
#define LAN8742A_PHY_ADDRESS
Definition: stm32f4xx_hal_conf.h:194
static void low_level_init(struct netif *netif)
Definition: ethernetif.c:243
const uint8_t[]
Definition: 404_html.c:3
priority: realtime (highest)
Definition: cmsis_os.h:224
static struct pbuf * low_level_input(struct netif *netif)
Definition: ethernetif.c:430
void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
s8_t err_t
Definition: err.h:57
#define IFNAME0
Definition: ethernetif.c:73
#define GPIO_PIN_5
Definition: stm32f4xx_hal_gpio.h:107
priority: low
Definition: cmsis_os.h:219
#define PHY_ISFR
Definition: stm32f4xx_hal_conf.h:229
uint32_t Pin
Definition: stm32f4xx_hal_gpio.h:65
uint32_t Speed
Definition: stm32f4xx_hal_gpio.h:74
void HAL_NVIC_DisableIRQ(IRQn_Type IRQn)
#define NETIF_FLAG_LINK_UP
Definition: netif.h:87
#define PHY_BSR
Definition: stm32f4xx_hal_conf.h:206
#define ETH_HWADDR_LEN
Definition: ethernet.h:50
#define GPIO_PIN_11
Definition: stm32f4xx_hal_gpio.h:113
void ethernetif_link(const void *arg)
Definition: ethernetif.c:210
#define ETH_RX_BUF_SIZE
Definition: stm32f4xx_hal_conf.h:186
static err_t low_level_output(struct netif *netif, struct pbuf *p)
Definition: ethernetif.c:352
osThreadId osThreadCreate(const osThreadDef_t *thread_def, void *argument)
Create a thread and add it to Active Threads and set it to state READY.
Definition: cmsis_os.c:245
GPIO Init structure definition.
Definition: stm32f4xx_hal_gpio.h:63
void * payload
Definition: pbuf.h:147
osStatus osSemaphoreRelease(osSemaphoreId semaphore_id)
Release a Semaphore token.
Definition: cmsis_os.c:862
#define osThread(name)
Definition: cmsis_os.h:478
#define NETIF_FLAG_BROADCAST
Definition: netif.h:81
#define GPIO_PIN_4
Definition: stm32f4xx_hal_gpio.h:106