Tutorial: DIY Kinetis SDK Project with Eclipse – Board Configuration
Join the DZone community and get the full member experience.
Join For Freein “ tutorial: diy kinetis sdk project with eclipse – startup ” i showed how to create a kinetis sdk project from scratch. in this post it is about adding the board initialization files. with the board initialization the peripheral clocks and pin muxing is configured.
clocks and pin muxing
as outlined in “
tutorial: diy kinetis sdk project with eclipse – startup
“, up to
main()
i have already a basic configuration:
- stack and heap are defined
- ‘critical’ hardware like watchdog/cop and basic cpu clocks are configured
- c/c++ variables are initialized
- any ansi library runtime settings are done
but for many modern microcontroller, there is yet another hardware configuration needed, which usually is done from
main()
as one of the first steps: to configure the clock gating and pin muxing.
‘ clock gating ‘ means that clocks are configured as such that the peripherals used are clocked. by default on arm cores, peripheral clocks need to be enabled, and accessing a peripheral like i/o pins which are not clocked will very likely result in an exception or hard fault .
the other thing is ‘pin muxing': on modern microcontroller an external pin on the processor package can be used by different peripherals. for example the picture below shows the ‘processor’ view in processor expert. the pin no 7 can be used for i/o, uart, i2s, ftm or even for usb. and it is now configured and routed to pte6 as i/o pin:
pin muxing or routing can be in many cases changed at runtime, but it is important to configure it properly at the beginning before doing the driver initialization, as the muxin is ‘shared’ between the peripherals and drivers.
sdk board files
i’m adding my board to the project with the files in ${ksdk_path}\boards:
and i make sure the compiler knows the include path to the boards folder. but as the board files are using a bunch of other include files of the sdk, i need to add some extra compiler include paths:
"${ksdk_path}/boards/frdmk64f120m" "${ksdk_path}/platform/drivers/gpio" "${ksdk_path}/platform/hal/port" "${ksdk_path}/platform/hal/gpio" "${ksdk_path}/platform/drivers/clock" "${ksdk_path}/platform/utilities" "${ksdk_path}/platform/hal/sim"
with this, i’m able to compile and link the board files :-).
initializing the board
to initialize the board, i include the header file:
#include "board.h"
and call the initialization function:
hardware_init(); /* initialize the hardware */
my application does now ‘nothing': it only calls main() and there i initialize the hardware:
but it will complain about missing
clock_manager_set_gate()
:
this means i need to add the needed files to configure clocks and the sim (system integration module). so i add more files: clock and sim :
and again, i need to add an include path for the compiler:
"${ksdk_path}/platform/drivers/interrupt"
unfortunately, there is a bug in the current sdk v1.0.0-beta: fsl_sim_features.h reports that it does not know my correctly specified “cpu_mk64fn1m0vll12″ :-(:
description resource path location type #error "no valid cpu defined" fsl_sim_features.h /frdm-k64f_bare_sdk/sdk/platform/hal/sim line 279 c/c++ problem
so i need to add the following to my compiler preprocessor settings to make the compiler happy:
"cpu_mk64fn1m0vmd12"
and with this i can build:
text data bss dec hex filename 17912 2476 260 20648 50a8 frdm-k64f_bare_sdk.elf
newlib-nano
well, that’s a lot of code size! time to switch to the smaller newlib-nano library. so i add
-specs=nano.specs
to my linker settings:
with this, the code size gets cut by half :-):
text data bss dec hex filename 8376 152 44 8572 217c frdm-k64f_bare_sdk.elf
now, that 8 kbyte (yikes!) code is still a *lot*, given what our application does: it does *nothing* (yet). the reason is how the sdk is architected: it uses a lot of tables and internal data structures, and does a lot of things dynamically (e.g. hardware initialization). therefore the code (and speed) overhead compared to a ‘traditional’ project is not small. even ‘pure’ processor expert projects are much smaller for ‘doing nothing’ because processor expert can just generate the code needed, and does not need to carry on all the extra stuff. the same thing with a processor expert project would be less than 4 kbyte code!
hardware_init()
but what is
hardware_init()
doing? not that much, but a very important thing: to setup the clocks and pin muxing:
void hardware_init(void) { int32_t i; /* enable clock for ports */ for (i = 0; i < hw_port_instance_count; i++) { clock_manager_set_gate(kclockmoduleport, i, true); } /* init the general pinmux */ configure_enet_pin_mux(0); for (i = 0; i < hw_port_instance_count; i++) { configure_gpio_pin_mux(i); } configure_i2c_pin_mux(0); configure_i2c_pin_mux(1); configure_sdhc_pin_mux(0); configure_spi_pin_mux(0); configure_uart_pin_mux(0); }
the first for loop enables the clock gates (passes the clocks to the
peripherals) for the port (gpio) peripherals. i recommend that you use
the debugger and step through the code (e.g. go into
clock_manager_set_gate()
. then you can see what contributes to the code size).
next it configures the ethernet pin muxing with
configure_enet_pin_mux()
,
followed by a for loop which does the same for the general purpose i/o
pins. as we have two i2c on microcontroller, there are two calls to
configure_i2c_pin_mux()
.
summary
before i can run my application code, the microcontroller needs to be
initialized properly. basically this means configuring the pin muxing
and clock gates. as this is usually depending on the board or what is
attached to the pins, this is named ‘board configuration’ too. the
kinetis sdk has preconfigured board configuration, e.g. for the
frdm-k64f. the configuration is done with the function
hardware_init()
which needs to be called right after
main()
.
with the kinetis sdk i can initialize my hardware and board in
programmatic way and do my custom board configuration. however, with the
way how the sdk is architected, be ready for overhead. for
microcontroller with a lot of flash like the k64f this does not matter
much, but for smaller microcontroller saving every byte is important.
the project is available on github . so what’s next? blinking a led!
happy boarding :-)
Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments