Porting CAN to K64 bootloader

Document created by Jing Pan Employee on Nov 5, 2019
Version 1Show Document
  • View in full screen mode

1. Introduction

MCUboot is a common used bootloader for most of Kinetis and i.mx RT devices. It can support download application via UART/USB/CAN/I2C/SPI. It enables quick and easy programming of Kinetis MCUs and i.mx RT MPU through the entire product life cycle, including application development, final product manufacturing, and beyond.

K64 is a very popular device in Kinetis family. It has a M4 core, 512k and above flash, 120M main frequency and plenty of interface, such as I2C/SPI/UART/CAN/USB/ENET. But it is a bit awkward that the MCUboot demo of K64 is not include CAN. Does K64’s CAN can’t support bootloader application? No, of course not. Here we are going to port CAN function to K64 bootloader.

There are two kind of CAN peripheral in Kinetis family, FlexCAN and MSCAN. FlexCAN is more complex than MSCAN. K64 has a FlexCAN. To speed up our work, we can port FlexCAN driver and related code from TWR-KV46 bootloader.


two TWR-SER board

two sets of TWR-ELEV





MCUXpresso 11.0

MCUBoot 2.0.0 package



2. Software porting

Step 1, copy below files to twrk64f120m_tower_bootloader project.





Step 2, modify the project to enable the FlexCAN.

  •       In bootloader_config.h, change BL_CONFIG_CAN definition to 1. 
  •       In peripherals_MK64F12.c, add


    // CAN0

    {.typeMask = kPeripheralType_CAN,

     .instance = 0,

     .pinmuxConfig = can_pinmux_config,

     .controlInterface = &g_flexcanControlInterface,

     .byteInterface = &g_flexcanByteInterface,

     .packetInterface = &g_framingPacketInterface },

#endif    // BL_CONFIG_CAN

  •       Pin mux setting. In peripherals_pinmux.h, add


//! CAN pinmux configurations


#define CAN0_RX_GPIO_PIN_NUM 18             // PIN 13 in the PTA group

#define CAN0_RX_FUNC_ALT_MODE kPORT_MuxAlt2 // ALT mode for CAN0 RX functionality for pin 13


#define CAN0_TX_GPIO_PIN_NUM 19             // PIN 12 in the PTA group

#define CAN0_TX_FUNC_ALT_MODE kPORT_MuxAlt2 // ALT mode for CAN0 TX functionality for pin 12

  •       Set clock. FlexCAN clock source can be OSCERCLK or bus clock. Here we use bus clock run at 48Mhz. In flexcan_peripheral.c, add these code.

const flexcan_timing_config_t bit_rate_table48m[] = {

    { 23, 3, 4, 4, 4 }, /* 125 kHz */

    { 11, 3, 4, 4, 4 }, /* 250 kHz */

    { 5, 3, 4, 4, 4 },  /* 500 kHz */

    { 3, 3, 4, 4, 4 },  /* 750 kHz */

    { 2, 3, 4, 4, 4 }   /* 1   MHz */


change line 621

FLEXCAN_SetTimingConfig((CAN_Type *)baseAddr, &bit_rate_table48m[s_flexcanInfo.baudrate]);

Step 3, compile the project.


3. Function test

Software preparation

To connect bootloader via CAN bus, NXP has TWR-K65 as bridge. But its source code is not in K64 SDK. It is in MCUBoot2.0.0 package. User can download the package from https://www.nxp.com/design/software/development-software/mcuxpresso-software-and-tools/mcuboot-mcu-bootloader-for-nxp-microcontrollers:MCUBOOT

The bridge project is called buspal which can be found in NXP_Kinetis_Bootloader_2_0_0\apps\bus_pal\MK65F18. BusPal is an embedded software tool that is available as a companion to blhost. The tool acts as a bus translator with an

established connection with blhost over UART and with the target device over I2C, SPI, or CAN, and assists blhost in carrying out commands and responses from the USB target device. The BusPal is available for selected platforms. The source code for BusPal is provided with the Kinetis bootloader release, it support FRDM-KL25, TWR-KV46F150M and TWR-K65F180M and can be customized to run on other platforms.

More detail of buspal is in Kinetis blhost User's Guide appendix C.


Hardware connection

TWR-SER has TJA1050 as transceiver. We can connect J7 on both boards. When construct the Tower system, user should take care the power. The power tree is very flexible. Improper setting may cause TJA1050 can’t work.


The Buspal project on TWR-K65F180M use UART1 to connect with computer. The port is on TWR-SER. To make the connection simple, we can share the openSDA UART port. The openSDA UART use UART2, we can jump UART1 signal to J33 and J34 on K65 tower board.




Open a command window, type

>blhost -p com4,57600 –buspal can,0,321,123 – get-property 10

This command can check if the whole system work properly.

Then, you can download the code to K64 now. Please type

>blhost -p com4,57600 –buspal can,0,321,123 – flash-image xxxxxx.s19 erase