Trying to get lwIP to work Topic is solved

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Pethead
Posts: 37
Joined: Mon Jul 11, 2016 1:47 pm
Has thanked: 1 time

Re: Trying to get lwIP to work

Post by Pethead »

Code: Select all

##############################################################################
# Build global options
# NOTE: Can be overridden externally.
#

# Compiler options here.
ifeq ($(USE_OPT),)
  USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16 -std=gnu99
  USE_OPT += -DBOARD_OTG_NOVBUSSENS $(build_args)
  USE_OPT += -fsingle-precision-constant -Wdouble-promotion
endif

# C specific options here (added to USE_OPT).
ifeq ($(USE_COPT),)
  USE_COPT =
endif

# C++ specific options here (added to USE_OPT).
ifeq ($(USE_CPPOPT),)
  USE_CPPOPT = -fno-rtti
endif

# Enable this if you want the linker to remove unused code and data
ifeq ($(USE_LINK_GC),)
  USE_LINK_GC = yes
endif

# Linker extra options here.
ifeq ($(USE_LDOPT),)
  USE_LDOPT =
endif

# Enable this if you want link time optimizations (LTO)
ifeq ($(USE_LTO),)
  USE_LTO = no
endif

# If enabled, this option allows to compile the application in THUMB mode.
ifeq ($(USE_THUMB),)
  USE_THUMB = yes
endif

# Enable this if you want to see the full log while compiling.
ifeq ($(USE_VERBOSE_COMPILE),)
  USE_VERBOSE_COMPILE = yes
endif

# If enabled, this option makes the build process faster by not compiling
# modules not used in the current configuration.
ifeq ($(USE_SMART_BUILD),)
  USE_SMART_BUILD = yes
endif

#
# Build global options
##############################################################################

##############################################################################
# Architecture or project specific options
#

# Stack size to be allocated to the Cortex-M process stack. This stack is
# the stack used by the main() thread.
ifeq ($(USE_PROCESS_STACKSIZE),)
  USE_PROCESS_STACKSIZE = 0x400
endif

# Stack size to the allocated to the Cortex-M main/exceptions stack. This
# stack is used for processing interrupts and exceptions.
ifeq ($(USE_EXCEPTIONS_STACKSIZE),)
  USE_EXCEPTIONS_STACKSIZE = 0x400
endif

# Enables the use of FPU on Cortex-M4 (no, softfp, hard).
ifeq ($(USE_FPU),)
  USE_FPU = hard
endif

# Enable this if you really want to use the STM FWLib.
ifeq ($(USE_FWLIB),)
  USE_FWLIB = yes
endif

#
# Architecture or project specific options
##############################################################################

##############################################################################
# Project, sources and paths
#

# Define project name here
PROJECT = ethernet

# Imported source files and paths
CHIBIOS = ChibiOS_3.0.2
# Startup files.
include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/startup_stm32f4xx.mk
# HAL-OSAL files (optional).
include $(CHIBIOS)/os/hal/hal.mk
include $(CHIBIOS)/os/hal/ports/STM32/STM32F4xx/platform.mk
include $(CHIBIOS)/os/hal/boards/EASYMX_PRO_V7/board.mk
include $(CHIBIOS)/os/hal/osal/rt/osal.mk
# RTOS files (optional).
include $(CHIBIOS)/os/rt/rt.mk
include $(CHIBIOS)/os/rt/ports/ARMCMx/compilers/GCC/mk/port_v7m.mk
# LWIP stack
include $(CHIBIOS)/os/various/lwip_bindings/lwip.mk

# Define linker script file here
LDSCRIPT= ld_eeprom_emu.ld

# C sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
CSRC = $(STARTUPSRC) \
       $(KERNSRC) \
       $(PORTSRC) \
       $(OSALSRC) \
       $(HALSRC) \
       $(PLATFORMSRC) \
       $(BOARDSRC) \
       $(CHIBIOS)/os/hal/lib/streams/chprintf.c \
       $(CHIBIOS)/os/various/syscalls.c \
       $(LWSRC) \
      main.c \
       led.c \
       adconv.c \
       eeprom.c \
       comm_usb_serial.c \
       web.c


# C++ sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
CPPSRC =

# C sources to be compiled in ARM mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
ACSRC =

# C++ sources to be compiled in ARM mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
ACPPSRC =

# C sources to be compiled in THUMB mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
TCSRC =

# C sources to be compiled in THUMB mode regardless of the global setting.
# NOTE: Mixing ARM and THUMB mode enables the -mthumb-interwork compiler
#       option that results in lower performance and larger code size.
TCPPSRC =

# List ASM source files here
ASMSRC = $(STARTUPASM) $(PORTASM) $(OSALASM)

INCDIR = $(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \
         $(HALINC) $(PLATFORMINC) $(BOARDINC) $(TESTINC) \
         $(CHIBIOS)/os/various \
         $(CHIBIOS)/os/hal/lib/streams \
       $(LWINC) \
         $(CC2520INC) $(CC1120INC)

#
# Project, sources and paths
##############################################################################

##############################################################################
# Compiler settings
#

MCU  = cortex-m4

#TRGT = arm-elf-
TRGT = arm-none-eabi-
CC   = $(TRGT)gcc
CPPC = $(TRGT)g++
# Enable loading with g++ only if you need C++ runtime support.
# NOTE: You can use C++ even without C++ support if you are careful. C++
#       runtime support makes code size explode.
LD   = $(TRGT)gcc
#LD   = $(TRGT)g++
CP   = $(TRGT)objcopy
AS   = $(TRGT)gcc -x assembler-with-cpp
AR   = $(TRGT)ar
OD   = $(TRGT)objdump
SZ   = $(TRGT)size
HEX  = $(CP) -O ihex
BIN  = $(CP) -O binary

# ARM-specific options here
AOPT =

# THUMB-specific options here
TOPT = -mthumb -DTHUMB

# Define C warning options here
CWARN = -Wall -Wextra -Wundef -Wstrict-prototypes

# Define C++ warning options here
CPPWARN = -Wall -Wextra -Wundef

#
# Compiler settings
##############################################################################

##############################################################################
# Start of user section
#

# List all user C define here, like -D_DEBUG=1
UDEFS =

# Define ASM defines here
UADEFS =

# List all user directories here
UINCDIR =

# List the user directory to look for the libraries here
ULIBDIR =

# List all user libraries here
ULIBS = -lm

#
# End of user defines
##############################################################################

ifeq ($(USE_FWLIB),yes)
  include $(CHIBIOS)/ext/stdperiph_stm32f4/stm32lib.mk
  CSRC += $(STM32SRC)
  INCDIR += $(STM32INC)
  USE_OPT += -DUSE_STDPERIPH_DRIVER
endif

build/$(PROJECT).bin: build/$(PROJECT).elf
   $(BIN) build/$(PROJECT).elf build/$(PROJECT).bin

# Program
upload: build/$(PROJECT).bin
   #qstlink2 --cli --erase --write build/$(PROJECT).bin
   openocd -f interface/stlink-v2.cfg -c "set WORKAREASIZE 0x2000" -f target/stm32f4x_stlink.cfg -c "program build/$(PROJECT).elf verify reset"
   #openocd -f board/stm32f4discovery.cfg -c "reset_config trst_only combined" -c "program build/$(PROJECT).elf verify reset exit" # For openocd 0.9

#program with olimex arm-usb-tiny-h and jtag-swd adapter board. needs openocd>=0.9
upload-olimex: build/$(PROJECT).bin
   openocd -f interface/ftdi/olimex-arm-usb-tiny-h.cfg -f interface/ftdi/olimex-arm-jtag-swd.cfg -c "set WORKAREASIZE 0x2000" -f target/stm32f4x.cfg -c "program build/$(PROJECT).elf verify reset"

debug-start:
   openocd -f stm32-bv_openocd.cfg

RULESPATH = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC
include $(RULESPATH)/rules.mk
Last edited by Pethead on Wed Feb 01, 2017 4:25 pm, edited 2 times in total.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Trying to get lwIP to work

Post by Giovanni »

Code: Select all

CSRC = $(STARTUPSRC) \
       $(KERNSRC) \
       $(PORTSRC) \
       $(OSALSRC) \
       $(HALSRC) \
       $(PLATFORMSRC) \
       $(BOARDSRC) \
       $(CHIBIOS)/os/hal/lib/streams/chprintf.c \
       $(CHIBIOS)/os/various/syscalls.c \
       $(LWSRC) \
      main.c \
       led.c \
       adconv.c \
       eeprom.c \
       comm_usb_serial.c \
       web.c


See? the event timer source is not in this list.

Giovanni
Pethead
Posts: 37
Joined: Mon Jul 11, 2016 1:47 pm
Has thanked: 1 time

Re: Trying to get lwIP to work

Post by Pethead »

Added $(CHIBIOS)/os/various/evtimer.c and now it works.

Thanks for the help, please see my question regarding the board.h file as well. I edited my earlier post in the thread.

If the case is such that I only need to configure the ports for the ethernet interface since the reason why the board hangs is because.

Code: Select all

ch.dbg.panic_msg = reason; // which is mac failure

The reason why I'm asking is because it would be faster to just configure the board file for those specific ports just to see that it works.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Trying to get lwIP to work

Post by Giovanni »

Yes, probably it is a pins setup problem, second possible reason is PHY clock.

Giovanni
Pethead
Posts: 37
Joined: Mon Jul 11, 2016 1:47 pm
Has thanked: 1 time

Re: Trying to get lwIP to work

Post by Pethead »

I've created a board.h file and configured the ports for using the RMII interface that is connected to the LAN8720A chip as you can see on this MCU board. Below you can see the configuration that I set up in my board file:

ETH_RMII_REF_CLK - PA1  (alternate 11), Resistor: Floating, Level: High
ETH_MDIO - PA2 (alternate 11), Resistor: Floating, Level: High
ETH_RMII_CRS_DV - PA7 (alternate 11), Resistor: Floating, Level: High
ETH_TX_EN - PB11 (alternate 11), Resistor: Floating, Level: High
ETH_RMII_TXD0 - PB12 (alternate 11), Resistor: Floating, Level: High
ETH_RMII_TXD1 - PB13 (alternate 11) Resistor: Floating, Level: High
nINT - PB14 (alternate 0), Resistor: Floating, Level: Low
ETH_MDC - PC1 (alternate 11), Resistor: Floating, Level: High
ETH_RMII_RXD0 - PC4 (alternate 11), Resistor: Floating, Level: High
ETH_RMII_RXD1 - PC5 (alternate 11), Resistor: Floating, Level: High

I am wondering one thing about the schematic though, PA1 (ETH_RMII_REF_CLK) is connected to the signal MCO on the schematic and so is PA8 (MCO1 in alternate 0), I assume I should only configure one of them as a clock for the LAN8720A chip (that one being PA1)?

I am also wondering if I should set the nINT pin to low or high.
This configuration strap
selects the mode of the
nINT/REFCLKO pin.

When nINTSEL
is floated or pulled to VDD2A,
nINT is selected for operation on the
nINT/REFCLKO pin (default).

When nINTSEL
is pulled low to VSS,
REFCLKO is selected for operation on the
nINT/REFCLKO pin.

When I step through the code during debugging the code starts executing in mii_lld.c in the mii_read() function and then it eventually runs mii_find_phy(&ETHD1); and continues to jump back and forth between those two functions and runs the macro rccEnableETH(false); in between and eventually we end up at the following:

Code: Select all

 ch.dbg.panic_msg = reason; // MAC failure 

As you said, another possible reason was the PHY clock, the original MCU card had a 25 Mhz crystal which I've replaced with a 8 Mhz crystal so the system clock is 168Mhz (as on the STM32F4-Discovery board). I am wondering if this might be causing the problem since I am still getting the MAC failure when I am starting it. And the datasheet for LAN8720A states that it needs a 25Mhz clock... :/

I've attached my board files and the XML configuration file which I've only configured for the RMII interface as it is now and a few blinking leds, the rest is mapped as inputs.
Attachments
EASYMX_PRO_V7.zip
(10.14 KiB) Downloaded 392 times
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Trying to get lwIP to work

Post by Giovanni »

Yes it is a problem,

MII and RMII PHYs require 25/50 MHz very accurate clocks. You may need to add an external oscillator.

Also make sure that the code searches for the correct PHY ID, each device has a different code.

Giovanni
Pethead
Posts: 37
Joined: Mon Jul 11, 2016 1:47 pm
Has thanked: 1 time

Re: Trying to get lwIP to work

Post by Pethead »

Okay thanks for the response, I'll go and try to fix an external clock source and keep trying to get it to work.

in mii.h

Code: Select all

#define MII_LAN8720_ID    0x0007C0F0

And in the LAN8720A datasheet we can see that for PHY register 1 the default value is 0007h and for PHY register 2 the default value is 1100 0000 1111 XXXXb (where XXXX  = Four-bit manufacturer’s revision number).

If we set the revision number to 0's we get C0F0 which is the same as the MII_LAN8720_ID, so I assume it's the same for LAN8720 / LAN8720A.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Trying to get lwIP to work

Post by Giovanni »

You may verify the actual returned code by stepping into that loop and inspecting the result.

Giovanni
Pethead
Posts: 37
Joined: Mon Jul 11, 2016 1:47 pm
Has thanked: 1 time

Re: Trying to get lwIP to work

Post by Pethead »

I managed to generate a 50MHz clock from my 8 MHz crystal with PLL and routed it to the MCO1 pin which is connected to the same pin as the ETH_RMII_REF_CLK on the MCU card and I've confirmed the frequency with an oscilloscope.

Now I am no longer getting the panic message due to MAC failure, however it's still not working and whenever I run the debugger it starts at and sits there endlessly

Code: Select all

  /* Transmit FIFO flush.*/
  ETH->DMAOMR   = ETH_DMAOMR_FTF;
  while (ETH->DMAOMR & ETH_DMAOMR_FTF)

In mac_lld_start function in mac_lld.c

I think it has something to do with the lwipInit(NULL) function since whenever I uncomment that line my board is starting to blink as intended but when use that function call it get stuck at the code section mentiond above.

Any ideas?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Trying to get lwIP to work

Post by Giovanni »

Hi,

There are 3 possible causes:
1) Clocks, you covered this apparently.
2) Pins setup.
3) PHY initializations, some devices could require special initializations to be performed outside the driver.

Anyway it is matter of MAC driver, lwIP has no impact.

Giovanni
Post Reply