Bug with code generation when using HAL and Ethernet (+ workaround)
I've created a new project using HAL for the NUCLEO-F207ZG with CubeMX that ships within CubeIDE (1.5.0). After implementing a minimal network stack from scratch that supported the bare minimum of ARP, IP and ICMP to ping my board, I was wondering why I was only ever receiving broadcast Ethernet frames, but never frames designated at my board.
Turns out the generated code in eth.c by CubeMX has an obvious bug when it comes to initializing pointers with array values:
heth.Init.MACAddr[0] = 0x00;
heth.Init.MACAddr[1] = 0x80;
heth.Init.MACAddr[2] = 0xE1;
heth.Init.MACAddr[3] = 0x00;
heth.Init.MACAddr[4] = 0x00;
heth.Init.MACAddr[5] = 0x00;MACAddr of ETH_InitTypeDef is not defined as uint8_t[6] but simply as a pointer to uint8_t.
typedef struct
{
// ...
uint8_t *MACAddr; /*!< MAC Address of used Hardware: must be pointer on an array of 6 bytes */
// ...
} ETH_InitTypeDef;Nowhere in the code this pointer gets allocated, so the values end up pretty much nowhere.
My quick workaround now is to just set an allocated array to MACAddr in eth.c:
// ...
/* USER CODE BEGIN 0 */
uint8_t mac_addr[6] = { 0x00, 0x80, 0xE1, 0x00, 0x00, 0x00 };
/* USER CODE END 0 */
ETH_HandleTypeDef heth;
/* ETH init function */
void MX_ETH_Init(void)
{
// ...
heth.Init.MACAddr = mac_addr;
// ...
}
// ...This works but is annoying as it gets overwritten with every code generation. Weirdly enough, sample code that comes for LwIP and the like do not suffer this problem and pretty much do what I have done as a workaround. So I suppose this is something that came along with a new release or something.
