How to change UART port on MCU Bootloader2.0.0

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to change UART port on MCU Bootloader2.0.0

How to change UART port on MCU Bootloader2.0.0

 

MCU Bootloader2.0.0 enables quick and easy programming of Kinetis MCUs through the entire product life cycle, including application development, final product manufacturing, and beyond. It supports many kinds of peripherals, include UART, I2C, SPI, USB, CAN and so on. Among these peripherals, UART is most common used. In reference manual, it only says that feature can be turned on or off by using #define statement in bootloader_config.h. In fact, you can use UART0 by default. But if you want to use other UART port, change TERM_PORT_NUM to other value is useless. If you traced this value, you’ll find it is not used at all, nor the TERMINAL_BAUD.

Here we use FRDM-KV31F512 as the example. We want to download image by UART2. First, we should modify peripherals_pinmux.h.

#define BL_ENABLE_PINMUX_UART2 (BL_CONFIG_SCUART)     //line 38

 

//! UART pinmux configurations

#define UART2_RX_PORT_BASE PORTE

#define UART2_RX_GPIO_BASE PTE

#define UART2_RX_GPIO_PIN_NUM 17               // PIN 16 in the PTB group

#define UART2_RX_FUNC_ALT_MODE kPORT_MuxAlt3   // ALT mode for UART0 RX

#define UART2_RX_GPIO_ALT_MODE kPORT_MuxAsGpio // ALT mode for GPIO functionality

#define UART2_RX_GPIO_IRQn PORTE_IRQn

#define UART2_RX_GPIO_IRQHandler PORTE_IRQHandler

#define UART2_TX_PORT_BASE PORTE

#define UART2_TX_GPIO_PIN_NUM 16             // PIN 17 in the PTB group

#define UART2_TX_FUNC_ALT_MODE kPORT_MuxAlt3 // ALT mode for UART0 TX

 

The original define is UART0, here we change it to UART2. It is strongly recommended to do so. Otherwise you’ll find that UART can’t work at all.

Another comment here is PTE16 and PTE17 is conflict with SPI. You must disable SPI or change SPI function to other pins.

 

Next we must modify peripherals_KV31F512.h.

const peripheral_descriptor_t g_peripherals[] = {

#if BL_CONFIG_SCUART

   // UART0

   {.typeMask = kPeripheralType_UART,

     .instance = 2, // change this value from 0 to 2

     .pinmuxConfig = uart_pinmux_config,

     .controlInterface = &g_scuartControlInterface,

     .byteInterface = &g_scuartByteInterface,

     .packetInterface = &g_framingPacketInterface },

 

Although there is a baud rate definition TERMINAL_BAUD, but it is never used too. MCU bootloader2.0.0 use auto baud rate detection. When power on, bootloader will go to autobaud detection mode. KinetisFlashTool sends ‘0x ’ every second. Bootloader check this byte and calculate baud rate.

 pub1.png

After getting this value, bootloader will change to normal communication mode. Baud rate will not change until reset. If blhost is used, subsequent blhost invocations must specify the same baud rate as was used for the initial invocation unless the bootloader is reset. If the baud rate is not specified using the -p COMx, <baudrate> option, the UART baud rate will be set to 57600.

Since Kinetis MCU UART module don’t have auto frequency detect function, the bootloader detects frequcny by software. It uses GPIO interrupt and timer to measure frequency. But in bootloader, there is only code for UART0, there isn’t code for other UART port. We must add the code. In hardware_init_KV31F512.c, modify the function get_uart_clock()

 

uint32_t get_uart_clock(uint32_t instance)

{

   switch (instance)

   {

       case 0:

       case 1:

           // UART0 and UART1 always use the system clock

           return SystemCoreClock;

       case 2:

           return get_bus_clock();

       default:

           return 0;

   }

}

 

KV31 has 4 UART, include three UART modules and one low-power UART. They have different clock source. UART0 and UART1 use System clock while UART2 and LPUART0 use Bus clock.

Thus, we finished the work. UART2 can work as the download port now.

Labels (1)
No ratings
Version history
Last update:
‎06-27-2018 10:10 PM
Updated by: