Board bring-up

I started playing with the FRDM-K64F board recently. I want to use it as a base for a bunch of hobby projects. The start-up code is not that different from the one for Tiva, which I describe here - it's the same Cortex-M4 architecture after all. Two additional things need to be taken care of, though: flash security and the COP watchdog.

The K64F MCU restricts external access to a bunch of resources by default. It's a great feature if you want to ship a product, but it makes debugging impossible. The Flash Configuration Field (see section 29.3.1 of the datasheet) defines the default security and boot settings.

 1static const struct {
 2  uint8_t backdor_key[8];   // backdor key
 3  uint8_t fprot[4];         // program flash protection (FPROT{0-3})
 4  uint8_t fsec;             // flash security (FSEC)
 5  uint8_t fopt;             // flash nonvolatile option (FOPT)
 6  uint8_t feprot;           // EEPROM protection (FEPROT)
 7  uint8_t fdprot;           // data flash protection (FDPROT)
 8} fcf  __attribute__ ((section (".fcf"))) = {
 9  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
10  {0xff, 0xff, 0xff, 0xff}, // disable flash program protection
11  0x02,                     // disable flash security
12  0x01,                     // disable low-power boot (section 6.3.3)
13  0x00,
14  0x00

If flash protection (the fprot field) is not disabled, you won't be able to flash new code by copying it to the MBED partition and will have to run mass erase from OpenOCD every time:

interface cmsis-dap
set CHIPNAME k60
source [find target/kx.cfg]
kinetis mdm mass_erase

If the MCU is in the secured state (the fsec field), the debugger will have no access to memory.

The structure listed above needs to end up in flash just after the interrupt vector. I use the linker script to make sure it happens. I define the appropriate memory block:

FLASH-FCF  (rx)  : ORIGIN = 0x00000400, LENGTH = 0x00000010

And then put the .fcf section in it:

.fcf :

See here.

I also disable the COP (computer operates properly) watchdog which resets the MCU if it is not serviced often enough.

1WDOG_UNLOCK = 0xc520;        // unlock magic #1
2WDOG_UNLOCK = 0xd928;        // unlock magic #2
3for(int i = 0; i < 2; ++i);  // delay a couple of cycles
4WDOG_STCTRLH &= ~0x0001;     // disable the watchdog

You can get the template code at GitHub.

If you like this kind of content, you can subscribe to my newsletter, follow me on Twitter, or subscribe to my RSS channel.