Stm32f103ze stm3210e-eval

From Miosix Wiki
Jump to navigation Jump to search

This board has an STM32F103ZE, a Cortex-M3 running at 72MHz with 512KB of FLASH and 64KB of RAM, plus 1MB of external SRAM. In addtion, the board has some nice peripherals including an USB port and a 320x240 LCD display, which are supported by the mxusb and mxgui libraries. Also, there is a microSD socket allowing filesystem support.

To develop for this board you'll need an USB to serial adapter to be able to deploy your code to the board through its serial bootloader as well as to be able to see debug messages as printf is redirected to the serial port on this board. Optionally, you'll need a JTAG in circuit debugger compatible with OpenOCD and Linux, an ARM-USB-OCD was tested succesfully.

Stm3210e-eval small.jpeg

Configuring the kernel

The minimum configuration required is to edit the miosix/config/Makefile.inc file to uncomment the OPT_BOARD := stm32f103ze_stm3210e-eval line. If you had compiled the kernel before, it is recommended to do a make clean (or in the Netbeans IDE, to click on the "Clean project" button) to avoid leaving object files around. After, do a make to compile.

In addition, this board has a couple of board-specific options in the Makefile.inc file. For convenience, the relevant part of that configuration file is reported here so as to be easily located within the file

##---------------------------------------------------------------------------
## stm32f103ze_stm3210e-eval
##
ifeq ($(OPT_BOARD),stm32f103ze_stm3210e-eval)

  ## Linker script type, there are three options
  ## 1) Code in FLASH, stack + heap in internal RAM (file *_rom.ld)
  ##    the most common choice, available for all microcontrollers
  ## 2) Code in FLASH stack in internal RAM heap in external RAM (file
  ##    *_xram.ld) useful for hardware like STM3210E-EVAL when big heap is
  ##    needed. The microcontroller must have an external memory interface.
  ## 3) Code + stack + heap in external RAM, (file *_all_in_xram.ld)
  ##    useful for debugging code in hardware like STM3210E-EVAL. Code runs
  ##    *very* slow compared to FLASH. Works only with a booloader that
  ##    forwards interrrupts @ 0x68000000 (see miosix/bootloaders for one).
  ##    The microcontroller must have an external memory interface.
  ## Warning! enable external ram if you use a linker script that requires it
  ## (see the XRAM flag below)
  LINKER_SCRIPT_PATH := arch/cortexM3_stm32/stm32f103ze_stm3210e-eval/
  #LINKER_SCRIPT := $(LINKER_SCRIPT_PATH)stm32_512k+64k_rom.ld
  #LINKER_SCRIPT := $(LINKER_SCRIPT_PATH)stm32_512k+64k_xram.ld
  LINKER_SCRIPT := $(LINKER_SCRIPT_PATH)stm32_512k+64k_all_in_xram.ld

  ## Enable/disable initialization of external RAM at boot. Three options:
  ## __ENABLE_XRAM : If you want the heap in xram (with an appropriate linker
  ## script selected above)
  ## __ENABLE_XRAM and __CODE_IN_XRAM : Debug mode with code + stack + heap
  ## in xram (with an appropriate linker script selected above)
  ## none selected : don't use xram (with an appropriate linker script
  ## selected above)
  #XRAM := -D__ENABLE_XRAM
  XRAM := -D__ENABLE_XRAM -D__CODE_IN_XRAM

  ## Select clock frequency
  ## Not defining any of these results in the internal 8MHz clock to be used
  #CLOCK_FREQ := -DSYSCLK_FREQ_24MHz=24000000
  #CLOCK_FREQ := -DSYSCLK_FREQ_36MHz=36000000
  #CLOCK_FREQ := -DSYSCLK_FREQ_48MHz=48000000
  #CLOCK_FREQ := -DSYSCLK_FREQ_56MHz=56000000
  CLOCK_FREQ := -DSYSCLK_FREQ_72MHz=72000000

As can be seen, there are three options for this board: LINKER_SCRIPT, XRAM and CLOCK_FREQ. The comments explain in detail their meaning, so it isn't necessary to further explain them.

After modifying configuration files it is recommended to do a make clean; make (or in the Netbeans IDE, to click on the "Clean and build project" button) to be sure that changes are applied.

Loading code

As explained before, there is more than one linker script for this board, and the selected linker script affects the way code should be loaded on the board.

stm32_512k+64k_all_in_xram.ld

This is the default linker script, it is very useful for debugging code on this board, as it locates everything (code, stack, heap) in the external RAM. It has the disadvantage that the loaded code is lost upon reboot (being in a volatile memory) and that code will run ~10 times slower due to external RAM access timings, but for debugging, these are not issues.

To be able to use this linker script you need to program a bootloader into the microcontroller's FLASH memory that allows code loading to the external RAM as well as forwarding interrupts there. This bootloader is in the miosix/_tools/bootloaders/stm32/stm3210e-eval directory, and information on how to install it can be found in the miosix/_doc/textdoc/stm32-bootloader.txt file. For programming the bootloader the stm32flash tool can be used.

stm32_512k+64k_rom.ld and stm32_512k+64k_xram.ld

This linker scripts put the code in FLASH, so you need to program the microcontroller's FLASH every time you change your code, but code will run faster and won't be lost at each reboot.

A simple way to load code in FLASH is to use the http://sourceforge.net/projects/stm32flash stm32flash] tool. After having installed it on your system, all you need to do is to connect a serial cable between the USART1 connector on the board and your PC. USB to serial adapters work well if you don't have a serial port. Then, flip up the boot0 switch on your board, and hit the Reset key. On the PC side, you'll need to run the

stm32flash -w main.hex -v /dev/ttyUSB0

Of course, replace /dev/ttyUSB0 with your serial port device name. After that, flip down the boot0 switch and hit again Reset. The code you have loaded should start.

Reading printf output

The board support package of this board simply redirects printf to the serial port USART1. The board has an RS232 connector, so you'll simply need to connect it to your computer via a serial cable, or use an USB to serial adapter. The serial port baudrate is set to 19200baud, 8N1.

On Linux you can use screen. Open a terminal and type screen /dev/ttyUSB0 115200 (Note that to quit from screen you need to type "Ctrl-C, \"). You'll need to change /dev/ttyUSB0 with your serial port device name.

On windows you can use HyperTerminal, you need to configure it to use the required baud rate, no flow control, and select the correct COM port device name.

In circuit debugging

In-circuit debugging requires an USB to JTAG adapter. Ther recomended one is the ARM-USB-OCD. If you use a different one, you may need to modify the miosix/arch/arm7_lpc2000/lpc2138_miosix_board/lpc2138_armusbocd.cfg to adapt it to your JTAG tool.

Before you begin you should note that the Miosix kernel will put the CPU to a low power state when no thread is running. This low power state will usually confuse debuggers and make them lose sync with the CPU. To avoid this you need to edit the miosix/config/miosix_settings.h file and uncomment #define JTAG_DISABLE_SLEEP line. This will prevent the kernel from putting the CPU in its low power state making debugging possible. Also, to be able to precisely single-step your code you need to disable compiler optimizations as gdb is incapable of reliably debug an optimized code. The option is in the miosix/config/Makefile.inc file and is the OPT_OPTIMIZATION := -O0 that should be uncommented while the other lines related to the same option should be commented out.

After modifying configuration files it is recommended to do a make clean; make (or in the Netbeans IDE, to click on the "Clean and build project" button) to be sure that changes are applied.

On the software side, you need to open two shells. In one do a

openocd -f miosix/arch/cortexM3_stm32/stm32f103ze_stm3210e-eval/stm32f10x_eval.cfg

This will start the OpenOCD program that will connect to the board and listen for GDB connections. If the connection fails on Linux, you may not have installed the UDEV rules for your USB to JTAG device. If this is the case, running OpenOCD as root will fix the issue.

In the second shell type the following commands

arm-miosix-eabi-gdb main.elf
target remote :3333
monitor soft_reset_halt
load
break main
continue

The first command will start the gdb debugger. The following commands are typed into the gdb console, and tell it to connect to OpenOCD, reset the board and stop at the beginning of main() Please not that the load command implies that you are using the stm32_512k+64k_all_in_xram.ld linker script, and will instruct gdb to load your code in RAM. If you are using another linker script that loads code in FLASH you'll need to replace this command with a

monitor flash write_image erase main.bin 0x08000000

that will program the FLASH memory.

From there on, have a look at Quick_start for debugger commands.

(Optional) Configuring the Netbeans IDE

To have a fully working code completion that can also resolve the board-specific symbols, right click on the project, go to Set Configuration and select the right board.

Netbeans-config.png