Skip to content

Cleanup/Refactor and prepera project for new targets #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 75 additions & 23 deletions app/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,10 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef __TARGET_INIT_H
#define __TARGET_INIT_H

#include "stm32h7xx_hal.h"
#include "stm32h7xx_hal_mdma.h"
#include "stm32h7xx_hal_qspi.h"
#if MCUBOOT_APPLICATION_DFU
#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_dfu.h"
#include "usbd_dfu_flash.h"
#endif
#ifndef __BOARD_H
#define __BOARD_H

#if MCUBOOT_APPLICATION_DFU
#define APP_DEFAULT_ADD USBD_DFU_APP_DEFAULT_ADD
#else
#define APP_DEFAULT_ADD 0x08040000
#endif
#define BOARD_APP_DEFAULT_ADD 0x08040000

#define BOOTLOADER_CONFIG_MAGIC 0xA0
#define BOOTLOADER_VERSION 4
Expand Down Expand Up @@ -77,10 +63,76 @@
#define BOARD_EXTCLOCK 25
#endif

int target_debug_init(void);
int target_loop(void);
int target_debug(void);
int target_led_off(void);
int target_empty_keys(void);
#define BOARD_GREEN_LED PK_6
#define BOARD_RED_LED PK_5
#define BOARD_BLUE_LED PK_7

#define BOARD_BOOT_SEL PI_8
#define BOARD_USB_RESET PJ_4

#ifdef BOARD_HAS_VIDEO
#define BOARD_VIDEO_ENABLE PJ_2
#define BOARD_VIDEO_RESET PJ_3
#endif

#endif /* __TARGET_INIT_H */
#define BOARD_I2C_SCL PB_6
#define BOARD_I2C_SDA PB_7

#define BOARD_USBD_VID 0x2341
#define BOARD_USBD_PID 0x035B

#define BOARD_USBD_STRING "Portenta H7 MCUboot"

#define BOARD_QSPI_SO0 PD_11
#define BOARD_QSPI_SO1 PD_12
#define BOARD_QSPI_SO2 PF_7
#define BOARD_QSPI_SO3 PD_13
#define BOARD_QSPI_SCK PF_10
#define BOARD_QSPI_CS PG_6

#define BOARD_USB_OTG_FS_DM_DP_PIN (GPIO_PIN_11 | GPIO_PIN_12)
#define BOARD_USB_OTG_FS_DM_DP_MODE (GPIO_MODE_AF_PP)
#define BOARD_USB_OTG_FS_DM_DP_PULL (GPIO_NOPULL)
#define BOARD_USB_OTG_FS_DM_DP_SPEED (GPIO_SPEED_FREQ_VERY_HIGH)
#define BOARD_USB_OTG_FS_DM_DP_ALTERNATE (GPIO_AF10_OTG1_FS)
#define BOARD_USB_OTG_FS_DM_DP_GPIO (GPIOA)

#define BOARD_USB_OTG_HS_CLK_PIN (GPIO_PIN_5)
#define BOARD_USB_OTG_HS_CLK_MODE (GPIO_MODE_AF_PP)
#define BOARD_USB_OTG_HS_CLK_PULL (GPIO_NOPULL)
#define BOARD_USB_OTG_HS_CLK_SPEED (GPIO_SPEED_FREQ_VERY_HIGH)
#define BOARD_USB_OTG_HS_CLK_ALTERNATE (GPIO_AF10_OTG2_HS)
#define BOARD_USB_OTG_HS_CLK_GPIO (GPIOA)

#define BOARD_USB_OTG_HS_D0_PIN (GPIO_PIN_3)
#define BOARD_USB_OTG_HS_D0_MODE (GPIO_MODE_AF_PP)
#define BOARD_USB_OTG_HS_D0_PULL (GPIO_NOPULL)
#define BOARD_USB_OTG_HS_D0_SPEED (GPIO_SPEED_FREQ_VERY_HIGH)
#define BOARD_USB_OTG_HS_D0_ALTERNATE (GPIO_AF10_OTG2_HS)
#define BOARD_USB_OTG_HS_D0_GPIO (GPIOA)

#define BOARD_USB_OTG_HS_D1_D7_PIN (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13)
#define BOARD_USB_OTG_HS_D1_D7_MODE (GPIO_MODE_AF_PP)
#define BOARD_USB_OTG_HS_D1_D7_PULL (GPIO_NOPULL)
#define BOARD_USB_OTG_HS_D1_D7_ALTERNATE (GPIO_AF10_OTG2_HS)
#define BOARD_USB_OTG_HS_D1_D7_GPIO (GPIOB)

#define BOARD_USB_OTG_HS_STP_PIN (GPIO_PIN_0)
#define BOARD_USB_OTG_HS_STP_MODE (GPIO_MODE_AF_PP)
#define BOARD_USB_OTG_HS_STP_PULL (GPIO_NOPULL)
#define BOARD_USB_OTG_HS_STP_ALTERNATE (GPIO_AF10_OTG2_HS)
#define BOARD_USB_OTG_HS_STP_GPIO (GPIOC)

#define BOARD_USB_OTG_HS_NXT_PIN (GPIO_PIN_4)
#define BOARD_USB_OTG_HS_NXT_MODE (GPIO_MODE_AF_PP)
#define BOARD_USB_OTG_HS_NXT_PULL (GPIO_NOPULL)
#define BOARD_USB_OTG_HS_NXT_ALTERNATE (GPIO_AF10_OTG2_HS)
#define BOARD_USB_OTG_HS_NXT_GPIO (GPIOH)

#define BOARD_USB_OTG_HS_DIR_PIN (GPIO_PIN_11)
#define BOARD_USB_OTG_HS_DIR_MODE (GPIO_MODE_AF_PP)
#define BOARD_USB_OTG_HS_DIR_PULL (GPIO_NOPULL)
#define BOARD_USB_OTG_HS_DIR_ALTERNATE (GPIO_AF10_OTG2_HS)
#define BOARD_USB_OTG_HS_DIR_GPIO (GPIOI)

#endif /* __BOARD_H */
18 changes: 18 additions & 0 deletions app/bootutil/bootutil_extra.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,21 @@ int boot_set_debug(int enable) {

return RTCSetBKPRegister(RTC_BKP_DR7, rtc_reg);
}

int boot_empty_keys() {
unsigned int i;
uint8_t* encript_key = (uint8_t*)(0x08000300);
uint8_t* signing_key = (uint8_t*)(0x08000400);

for(i = 0; i < 256; i++) {
if(encript_key[i] != 0xFF)
return 0;
}

for(i = 0; i < 256; i++) {
if(signing_key[i] != 0xFF)
return 0;
}

return 1;
}
10 changes: 10 additions & 0 deletions app/bootutil/bootutil_extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@

int boot_set_debug(int enable);

#ifdef __cplusplus
extern "C" {
#endif

int boot_empty_keys();

#ifdef __cplusplus
}
#endif

#endif //__BOOTUTIL_EXTRA_H
7 changes: 4 additions & 3 deletions app/default_bd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "ota.h"
#include "rtc.h"
#include "board.h"
#include "bootutil/bootutil_extra.h"
#include "bootutil/bootutil_log.h"

#include "SlicingBlockDevice.h"
Expand Down Expand Up @@ -362,7 +363,7 @@ static void initBlockTable(void) {

mbed::BlockDevice* get_secondary_bd(void) {

if(!target_empty_keys()) {
if(!boot_empty_keys()) {
if(!BlockTableLoaded) {
initBlockTable();
BlockTableLoaded = true;
Expand All @@ -379,7 +380,7 @@ mbed::BlockDevice* get_secondary_bd(void) {

mbed::BlockDevice* get_scratch_bd(void) {

if(!target_empty_keys()) {
if(!boot_empty_keys()) {
if(!BlockTableLoaded) {
initBlockTable();
BlockTableLoaded = true;
Expand All @@ -396,7 +397,7 @@ mbed::BlockDevice* get_scratch_bd(void) {

mbed::BlockDevice* BlockDevice::get_default_instance()
{
static QSPIFBlockDevice default_bd(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000);
static QSPIFBlockDevice default_bd(BOARD_QSPI_SO0, BOARD_QSPI_SO1, BOARD_QSPI_SO2, BOARD_QSPI_SO3, BOARD_QSPI_SCK, BOARD_QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000);
return &default_bd;
}

Expand Down
84 changes: 45 additions & 39 deletions app/dfu/usbd_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

/* Includes ------------------------------------------------------------------ */
#include "board.h"
#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_conf.h"

/* Private typedef ----------------------------------------------------------- */
/* Private define ------------------------------------------------------------ */
Expand All @@ -34,6 +37,10 @@ PCD_HandleTypeDef hpcd;
/*******************************************************************************
PCD BSP Routines
*******************************************************************************/
PCD_HandleTypeDef * HAL_PCD_GetHandle(void)
{
return &hpcd;
}

/**
* @brief Initializes the PCD MSP.
Expand All @@ -49,12 +56,12 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef * hpcd)
__HAL_RCC_GPIOA_CLK_ENABLE();

/* Configure DM DP Pins */
GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG1_FS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BOARD_USB_OTG_FS_DM_DP_PIN;
GPIO_InitStruct.Mode = BOARD_USB_OTG_FS_DM_DP_MODE;
GPIO_InitStruct.Pull = BOARD_USB_OTG_FS_DM_DP_PULL;
GPIO_InitStruct.Speed = BOARD_USB_OTG_FS_DM_DP_SPEED;
GPIO_InitStruct.Alternate = BOARD_USB_OTG_FS_DM_DP_ALTERNATE;
HAL_GPIO_Init(BOARD_USB_OTG_FS_DM_DP_GPIO, &GPIO_InitStruct);

RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

Expand Down Expand Up @@ -103,49 +110,48 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef * hpcd)
__GPIOI_CLK_ENABLE();

/* CLK */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_CLK_PIN;
GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_CLK_MODE;
GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_CLK_PULL;
GPIO_InitStruct.Speed = BOARD_USB_OTG_HS_CLK_SPEED;
GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_CLK_ALTERNATE;
HAL_GPIO_Init(BOARD_USB_OTG_HS_CLK_GPIO, &GPIO_InitStruct);

/* D0 */
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_D0_PIN;
GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_D0_MODE;
GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_D0_PULL;
GPIO_InitStruct.Speed = BOARD_USB_OTG_HS_D0_SPEED;
GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_D0_ALTERNATE;
HAL_GPIO_Init(BOARD_USB_OTG_HS_D0_GPIO, &GPIO_InitStruct);

/* D1 D2 D3 D4 D5 D6 D7 */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5 |
GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_D1_D7_PIN;
GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_D1_D7_MODE;
GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_D1_D7_PULL;
GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_D1_D7_ALTERNATE;
HAL_GPIO_Init(BOARD_USB_OTG_HS_D1_D7_GPIO, &GPIO_InitStruct);

/* STP */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_STP_PIN;
GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_STP_MODE;
GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_STP_PULL;
GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_STP_ALTERNATE;
HAL_GPIO_Init(BOARD_USB_OTG_HS_STP_GPIO, &GPIO_InitStruct);

/* NXT */
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_NXT_PIN;
GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_NXT_MODE;
GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_NXT_PULL;
GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_NXT_ALTERNATE;
HAL_GPIO_Init(BOARD_USB_OTG_HS_NXT_GPIO, &GPIO_InitStruct);

/* DIR */
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG2_HS;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BOARD_USB_OTG_HS_DIR_PIN;
GPIO_InitStruct.Mode = BOARD_USB_OTG_HS_DIR_MODE;
GPIO_InitStruct.Pull = BOARD_USB_OTG_HS_DIR_PULL;
GPIO_InitStruct.Alternate = BOARD_USB_OTG_HS_DIR_ALTERNATE;
HAL_GPIO_Init(BOARD_USB_OTG_HS_DIR_GPIO, &GPIO_InitStruct);
__HAL_RCC_USB1_OTG_HS_ULPI_CLK_ENABLE();

/* Enable USB HS Clocks */
Expand Down
4 changes: 3 additions & 1 deletion app/dfu/usbd_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define __USBD_CONF_H

/* Includes ------------------------------------------------------------------*/
#include "board.h"
#include "stm32h7xx_hal.h"
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -40,7 +41,7 @@
/* DFU Class Config */
#define USBD_DFU_MAX_ITF_NUM 4
#define USBD_DFU_XFER_SIZE 4096 /* Max DFU Packet Size = 4096 bytes */
#define USBD_DFU_APP_DEFAULT_ADD 0x08040000 /* The first sector (32 KB) is reserved for DFU code */
#define USBD_DFU_APP_DEFAULT_ADD BOARD_APP_DEFAULT_ADD /* The first sector (32 KB) is reserved for DFU code */
#define USBD_DFU_MAX_NB_OF_SECTORS 16 /* Max number of sectors */

#define CDC_CLASS_DESC_SIZE (8 + 58)
Expand Down Expand Up @@ -80,6 +81,7 @@
#endif

/* Exported functions ------------------------------------------------------- */
extern PCD_HandleTypeDef * HAL_PCD_GetHandle(void);

#endif /* __USBD_CONF_H */

Expand Down
9 changes: 5 additions & 4 deletions app/dfu/usbd_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
#if MCUBOOT_APPLICATION_HOOKS && MCUBOOT_APPLICATION_DFU

/* Includes ------------------------------------------------------------------ */
#include "board.h"
#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_conf.h"

/* Private typedef ----------------------------------------------------------- */
/* Private define ------------------------------------------------------------ */
#define USBD_VID 0x2341
#define USBD_PID 0x035B
#define USBD_VID BOARD_USBD_VID
#define USBD_PID BOARD_USBD_PID
#define USBD_LANGID_STRING 0x409
#define USBD_MANUFACTURER_STRING "Arduino SA"
#define USBD_PRODUCT_HS_STRING "Portenta MCUBoot"
#define USBD_PRODUCT_FS_STRING "Portenta MCUBoot"
#define USBD_PRODUCT_HS_STRING BOARD_USBD_STRING
#define USBD_PRODUCT_FS_STRING BOARD_USBD_STRING
#define USBD_CONFIGURATION_HS_STRING "DFU Config"
#define USBD_INTERFACE_HS_STRING "DFU Interface"
#define USBD_CONFIGURATION_FS_STRING "DFU Config"
Expand Down
1 change: 1 addition & 0 deletions app/dfu/usbd_dfu_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extern USBD_DFU_MediaTypeDef USBD_DFU_Flash_fops;
#define FLASH_END_ADDR (uint32_t)(0x081FFFFF)

/* Exported functions ------------------------------------------------------- */
extern void init_Memories(void);

#endif /* __USBD_DFU_FLASH_H_ */

Expand Down
Loading