Initial bootloader implementation
Took a publicly available XMODEM bootloader and made it work on HK32F. Known issues: - Erase is super slow on this MCU. The upload routine erases all flash before loading, which means the first packet seems to hang. This also erases possibly persistent data stored in unused pages on flash.
This commit is contained in:
41
bootloader_hk32f030m_uart_xmodem/code/inc/flash.h
Normal file
41
bootloader_hk32f030m_uart_xmodem/code/inc/flash.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* flash.h
|
||||
*
|
||||
* Created on: Jun 23, 2023
|
||||
* Author: true
|
||||
*/
|
||||
|
||||
#ifndef CODE_INC_FLASH_H_
|
||||
#define CODE_INC_FLASH_H_
|
||||
|
||||
|
||||
#include "hk32f030m.h"
|
||||
|
||||
|
||||
|
||||
#define FLASH_SIZE 16384 // size of overall flash available on MCU
|
||||
#define FLASH_ERASE_PAGE_SIZE ((uint16_t)0x80) // erase page size
|
||||
|
||||
#define USER_APP_START_ADDR ((uint32_t)FLASH_BASE + 0xa00) // 2560 bytes (20 pages) rsvd for bootloader
|
||||
#define USER_APP_END_ADDR ((uint32_t)FLASH_BASE + FLASH_SIZE)
|
||||
|
||||
|
||||
|
||||
/* Status report for the functions. */
|
||||
typedef enum {
|
||||
FLASH_OK = 0x00u, /**< The action was successful. */
|
||||
FLASH_ERROR_SIZE = 0x01u, /**< The binary is too big. */
|
||||
FLASH_ERROR_WRITE = 0x02u, /**< Writing failed. */
|
||||
FLASH_ERROR_VERIFY = 0x04u, /**< Writing was successful, but the content of the memory is wrong. */
|
||||
FLASH_ERROR = 0xFFu /**< Generic error. */
|
||||
} BL_flash_status;
|
||||
|
||||
|
||||
|
||||
BL_flash_status flash_erase_user_app();
|
||||
BL_flash_status flash_write(uint32_t addr, uint8_t *data, uint16_t length);
|
||||
void jump_to_user_app();
|
||||
|
||||
|
||||
|
||||
#endif /* CODE_INC_FLASH_H_ */
|
||||
107
bootloader_hk32f030m_uart_xmodem/code/inc/hk32f030m_conf.h
Normal file
107
bootloader_hk32f030m_uart_xmodem/code/inc/hk32f030m_conf.h
Normal file
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file hk32f030m_conf.h
|
||||
* @brief configuration file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __HK32F030M_CONF_H
|
||||
#define __HK32F030M_CONF_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
|
||||
/* ########################## HSE/HSI Values adaptation ##################### */
|
||||
/**
|
||||
* @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
|
||||
* This value is used by the RCC module to compute the system frequency
|
||||
* (when HSE is used as system clock source, directly or through the PLL).
|
||||
*/
|
||||
#define EXTCLK_VALUE ((uint32_t)32000000) /*!< Value of the External oscillator in Hz */
|
||||
|
||||
/**
|
||||
* @brief Internal High Speed oscillator (HSI) value.
|
||||
* This value is used by the RCC module to compute the system frequency
|
||||
* (when HSI is used as system clock source, directly or through the PLL).
|
||||
*/
|
||||
#define HSI_VALUE ((uint32_t)32000000) /*!< Value of the Internal oscillator in Hz */
|
||||
|
||||
/**
|
||||
* @brief In the following line adjust the Internal High Speed oscillator (HSI) Startup
|
||||
* Timeout value
|
||||
*/
|
||||
#define HSI_STARTUP_TIMEOUT ((uint32_t)0xFFFF) /*!< Time out for start up */
|
||||
|
||||
|
||||
/**
|
||||
* @brief Internal Low Speed oscillator (LSI) value.
|
||||
*/
|
||||
#define LSI_VALUE ((uint32_t)128000)
|
||||
/*!< Value of the Internal Low Speed oscillator in Hz
|
||||
The real value may vary depending on the variations */
|
||||
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief Include module's header file
|
||||
*/
|
||||
|
||||
#include "hk32f030m_rcc.h"
|
||||
//#include "hk32f030m_crc.h"
|
||||
#include "hk32f030m_exti.h"
|
||||
#include "hk32f030m_flash.h"
|
||||
#include "hk32f030m_gpio.h"
|
||||
#include "hk32f030m_misc.h"
|
||||
//#include "hk32f030m_adc.h"
|
||||
//#include "hk32f030m_syscfg.h"
|
||||
//#include "hk32f030m_def.h"
|
||||
//#include "hk32f030m_i2c.h"
|
||||
//#include "hk32f030m_iwdg.h"
|
||||
#include "hk32f030m_pwr.h"
|
||||
//#include "hk32f030m_spi.h"
|
||||
//#include "hk32f030m_tim.h"
|
||||
#include "hk32f030m_usart.h"
|
||||
//#include "hk32f030m_iwdg.h"
|
||||
//#include "hk32f030m_wwdg.h"
|
||||
//#include "hk32f030m_awu.h"
|
||||
//#include "hk32f030m_beep.h"
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* ########################## Assert Selection ############################## */
|
||||
/**
|
||||
* @brief Uncomment the line below to expanse the "assert_param" macro in the
|
||||
* drivers code
|
||||
*/
|
||||
|
||||
|
||||
//#define USE_FULL_ASSERT (1U)
|
||||
|
||||
#ifdef USE_FULL_ASSERT
|
||||
/**
|
||||
* @brief The assert_param macro is used for function's parameters check.
|
||||
* @param expr: If expr is false, it calls assert_failed function
|
||||
* which reports the name of the source file and the source
|
||||
* line number of the call that failed.
|
||||
* If expr is true, it returns no value.
|
||||
* @retval None
|
||||
*/
|
||||
#define assert_param(expr) ((expr) ? (void)0U : assert_failed((char *)__FILE__, __LINE__))
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void assert_failed(char* file, uint32_t line);
|
||||
#else
|
||||
#define assert_param(expr) ((void)0U)
|
||||
#endif /* USE_FULL_ASSERT */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __HK32F030M_CONF_H */
|
||||
|
||||
/************************ (C) COPYRIGHT MKMcircoChuip *****END OF FILE****/
|
||||
27
bootloader_hk32f030m_uart_xmodem/code/inc/hk32f030m_it.h
Normal file
27
bootloader_hk32f030m_uart_xmodem/code/inc/hk32f030m_it.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file hk32f030m_it.h
|
||||
* @brief This file contains the headers of the interrupt handlers.
|
||||
******************************************************************************
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
#ifndef __HK32F030M_IT_H
|
||||
#define __HK32F030M_IT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Exported functions prototypes ---------------------------------------------*/
|
||||
void NMI_Handler(void);
|
||||
void HardFault_Handler(void);
|
||||
void SVC_Handler(void);
|
||||
void PendSV_Handler(void);
|
||||
void SysTick_Handler(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __HK32F030M_IT_H */
|
||||
32
bootloader_hk32f030m_uart_xmodem/code/inc/usart.h
Normal file
32
bootloader_hk32f030m_uart_xmodem/code/inc/usart.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* usart.h
|
||||
*
|
||||
* Created on: Jun 23, 2023
|
||||
* Author: true
|
||||
*/
|
||||
|
||||
#ifndef CODE_INC_USART_H_
|
||||
#define CODE_INC_USART_H_
|
||||
|
||||
|
||||
#define COMM_TIMEOUT 512000;
|
||||
|
||||
|
||||
|
||||
/* Status report for the functions. */
|
||||
typedef enum {
|
||||
COMM_OK = 0x00, // success
|
||||
COMM_RX_TIMEOUT = 0xe1, // data did not arrive in time
|
||||
COMM_ERROR = 0xff // all other errors
|
||||
} BL_comm_status;
|
||||
|
||||
|
||||
|
||||
void comm_init();
|
||||
BL_comm_status comm_rx(uint8_t *data, uint16_t length);
|
||||
BL_comm_status comm_tx_str(uint8_t *data);
|
||||
BL_comm_status comm_tx_byte(uint8_t data);
|
||||
|
||||
|
||||
|
||||
#endif /* CODE_INC_USART_H_ */
|
||||
36
bootloader_hk32f030m_uart_xmodem/code/inc/user_io.h
Normal file
36
bootloader_hk32f030m_uart_xmodem/code/inc/user_io.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* user_io.h
|
||||
*
|
||||
* Created on: Jun 23, 2023
|
||||
* Author: true
|
||||
*/
|
||||
|
||||
#ifndef CODE_INC_USER_IO_H_
|
||||
#define CODE_INC_USER_IO_H_
|
||||
|
||||
|
||||
#include "hk32f030m.h"
|
||||
|
||||
|
||||
|
||||
// bootloader activation button (MODE button)
|
||||
#define BTN_CLK RCC_AHBPeriph_GPIOA
|
||||
#define BTN_PORT GPIOA
|
||||
#define BTN_PIN 1
|
||||
#define BTN_ACT_DIR 0 // when pushed, is signal high (1) or low (0)?
|
||||
|
||||
// bootloader indicator LED
|
||||
#define LED_CLK RCC_AHBPeriph_GPIOD
|
||||
#define LED_PORT GPIOD
|
||||
#define LED_PIN 7
|
||||
#define LED_ACT_DIR 0 // to light, does signal need to be high (1) or low (0)?
|
||||
|
||||
|
||||
|
||||
void user_io_init();
|
||||
void user_led_init();
|
||||
void user_led_set(uint8_t lit);
|
||||
|
||||
|
||||
|
||||
#endif /* CODE_INC_USER_IO_H_ */
|
||||
79
bootloader_hk32f030m_uart_xmodem/code/inc/xmodem.h
Normal file
79
bootloader_hk32f030m_uart_xmodem/code/inc/xmodem.h
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* xmodem.h
|
||||
*
|
||||
* Created on: Jun 23, 2023
|
||||
* Author: true
|
||||
*
|
||||
* originally Copyright (c) 2018 Ferenc Nemeth - https://github.com/ferenc-nemeth - 21 Dec 2018
|
||||
* modified for use with HK32F and my coding style.
|
||||
*
|
||||
* under MIT license.
|
||||
*/
|
||||
|
||||
#ifndef CODE_INC_XMODEM_H_
|
||||
#define CODE_INC_XMODEM_H_
|
||||
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "flash.h"
|
||||
#include "usart.h"
|
||||
|
||||
|
||||
/* Xmodem (128 bytes) packet format
|
||||
* Byte 0: Header
|
||||
* Byte 1: Packet number
|
||||
* Byte 2: Packet number complement
|
||||
* Bytes 3-130: Data
|
||||
* Bytes 131-132: CRC
|
||||
*/
|
||||
|
||||
/* Xmodem (1024 bytes) packet format
|
||||
* Byte 0: Header
|
||||
* Byte 1: Packet number
|
||||
* Byte 2: Packet number complement
|
||||
* Bytes 3-1026: Data
|
||||
* Bytes 1027-1028: CRC
|
||||
*/
|
||||
|
||||
/* Maximum allowed errors (user defined). */
|
||||
#define X_MAX_ERRORS ((uint8_t)3u)
|
||||
|
||||
/* Sizes of the packets. */
|
||||
#define X_PACKET_NUMBER_SIZE ((uint16_t)2u)
|
||||
#define X_PACKET_128_SIZE ((uint16_t)128u)
|
||||
#define X_PACKET_1024_SIZE ((uint16_t)1024u)
|
||||
#define X_PACKET_CRC_SIZE ((uint16_t)2u)
|
||||
|
||||
/* Indexes inside packets. */
|
||||
#define X_PACKET_NUMBER_INDEX ((uint16_t)0u)
|
||||
#define X_PACKET_NUMBER_COMPLEMENT_INDEX ((uint16_t)1u)
|
||||
#define X_PACKET_CRC_HIGH_INDEX ((uint16_t)0u)
|
||||
#define X_PACKET_CRC_LOW_INDEX ((uint16_t)1u)
|
||||
|
||||
|
||||
/* Bytes defined by the protocol. */
|
||||
#define X_SOH ((uint8_t)0x01u) /**< Start Of Header (128 bytes). */
|
||||
#define X_STX ((uint8_t)0x02u) /**< Start Of Header (1024 bytes). */
|
||||
#define X_EOT ((uint8_t)0x04u) /**< End Of Transmission. */
|
||||
#define X_ACK ((uint8_t)0x06u) /**< Acknowledge. */
|
||||
#define X_NAK ((uint8_t)0x15u) /**< Not Acknowledge. */
|
||||
#define X_CAN ((uint8_t)0x18u) /**< Cancel. */
|
||||
#define X_C ((uint8_t)0x43u) /**< ASCII "C" to notify the host we want to use CRC16. */
|
||||
|
||||
/* Status report for the functions. */
|
||||
typedef enum {
|
||||
X_OK = 0x00u, /**< The action was successful. */
|
||||
X_ERROR_CRC = 0x01u, /**< CRC calculation error. */
|
||||
X_ERROR_NUMBER = 0x02u, /**< Packet number mismatch error. */
|
||||
X_ERROR_UART = 0x04u, /**< UART communication error. */
|
||||
X_ERROR_FLASH = 0x08u, /**< Flash related error. */
|
||||
X_ERROR = 0xFFu /**< Generic error. */
|
||||
} BL_xmodem_status;
|
||||
|
||||
void xmodem_receive(void);
|
||||
|
||||
|
||||
|
||||
#endif /* XMODEM_H_ */
|
||||
Reference in New Issue
Block a user