|
| 1 | +/* app_stm32f1.c |
| 2 | + * |
| 3 | + * Test bare-metal application for Blue Pill (STM32F103) board. |
| 4 | + * |
| 5 | + * Copyright (C) 2025 wolfSSL Inc. |
| 6 | + * |
| 7 | + * This file is part of wolfBoot. |
| 8 | + * |
| 9 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License as published by |
| 11 | + * the Free Software Foundation; either version 3 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * wolfBoot is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program; if not, write to the Free Software |
| 21 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 22 | + */ |
| 23 | + |
| 24 | +#include <stdint.h> |
| 25 | + |
| 26 | +#include "hal.h" |
| 27 | +#include "system.h" |
| 28 | +#include "target.h" |
| 29 | + |
| 30 | +#include "wolfboot/wolfboot.h" |
| 31 | + |
| 32 | +/*** LED ***/ |
| 33 | +#define RCC_BASE (0x40021000U) |
| 34 | +#define RCC_APB2ENR (*(volatile uint32_t *)(RCC_BASE + 0x18)) |
| 35 | +#define RCC_APB2ENR_IOPCEN (1 << 4) |
| 36 | + |
| 37 | +#define GPIOC_BASE (0x40011000) |
| 38 | +#define GPIOC_CRL (*(volatile uint32_t *)(GPIOC_BASE + 0x00U)) |
| 39 | +#define GPIOC_CRH (*(volatile uint32_t *)(GPIOC_BASE + 0x04U)) |
| 40 | +#define GPIOC_IDR (*(volatile uint32_t *)(GPIOC_BASE + 0x08U)) |
| 41 | +#define GPIOC_ODR (*(volatile uint32_t *)(GPIOC_BASE + 0x0CU)) |
| 42 | +#define GPIOC_BSRR (*(volatile uint32_t *)(GPIOC_BASE + 0x10U)) |
| 43 | +#define GPIOC_BRR (*(volatile uint32_t *)(GPIOC_BASE + 0x14U)) |
| 44 | + |
| 45 | +/* Register values */ |
| 46 | +#define GPIOx_CRL_MASK(pin) (0xF << ((pin) * 4)) |
| 47 | +#define GPIOx_CRH_MASK(pin) (0xF << (((pin) - 8) * 4)) |
| 48 | + |
| 49 | +#define GPIOx_CRL_CNF(pin, cnf) ((cnf) << (2 + ((pin) * 4))) |
| 50 | +#define GPIOx_CRH_CNF(pin, cnf) ((cnf) << (2 + (((pin) - 8) * 4))) |
| 51 | +#define GPIOx_CRy_CNF_ANALOG (0) |
| 52 | +#define GPIOx_CRy_CNF_FLOATING (1) |
| 53 | +#define GPIOx_CRy_CNF_PULL (2) |
| 54 | +#define GPIOx_CRy_CNF_OUTPUT_PP (0) |
| 55 | +#define GPIOx_CRy_CNF_OUTPUT_OD (1) |
| 56 | +#define GPIOx_CRy_CNF_AF_PP (2) |
| 57 | +#define GPIOx_CRy_CNF_AF_OD (3) |
| 58 | + |
| 59 | +#define GPIOx_CRL_MODE(pin, mode) ((mode) << ((pin) * 4)) |
| 60 | +#define GPIOx_CRH_MODE(pin, mode) ((mode) << (((pin) - 8) * 4)) |
| 61 | +#define GPIOx_CRy_MODE_INPUT (0) |
| 62 | +#define GPIOx_CRy_MODE_OUT_10_MHZ (1) |
| 63 | +#define GPIOx_CRy_MODE_OUT_2_MHZ (2) |
| 64 | +#define GPIOx_CRy_MODE_OUT_50_MHZ (3) |
| 65 | + |
| 66 | +static void led_init(void) |
| 67 | +{ |
| 68 | + uint32_t reg32; |
| 69 | + |
| 70 | + /* Use PC13 as led output as it's connected to the onboard LED of the |
| 71 | + * Blue Pill board. */ |
| 72 | + |
| 73 | + /* Enable GPIOC clock */ |
| 74 | + RCC_APB2ENR |= RCC_APB2ENR_IOPCEN; |
| 75 | + |
| 76 | + /* Configure pin PC13 as slow (2 MHz) output */ |
| 77 | + reg32 = GPIOC_CRH; |
| 78 | + reg32 &= ~(GPIOx_CRH_MASK(13)); |
| 79 | + reg32 |= GPIOx_CRH_CNF(13, GPIOx_CRy_CNF_OUTPUT_PP); |
| 80 | + reg32 |= GPIOx_CRH_MODE(13, GPIOx_CRy_MODE_OUT_2_MHZ); |
| 81 | + GPIOC_CRH = reg32; |
| 82 | + |
| 83 | + /* Set PC13 high to turn led OFF */ |
| 84 | + GPIOC_BSRR |= 1 << 13; |
| 85 | +} |
| 86 | + |
| 87 | +void led_toggle(void) |
| 88 | +{ |
| 89 | + if (GPIOC_IDR & (1 << 13)) |
| 90 | + GPIOC_BRR |= 1 << 13; |
| 91 | + else |
| 92 | + GPIOC_BSRR |= 1 << 13; |
| 93 | +} |
| 94 | + |
| 95 | +void main(void) { |
| 96 | + hal_init(); |
| 97 | + led_init(); |
| 98 | + |
| 99 | + switch (wolfBoot_current_firmware_version()) { |
| 100 | + case 1: |
| 101 | + wolfBoot_success(); |
| 102 | + wolfBoot_update_trigger(); |
| 103 | + led_toggle(); |
| 104 | + break; |
| 105 | + case 2: |
| 106 | + wolfBoot_success(); |
| 107 | + led_toggle(); |
| 108 | + break; |
| 109 | + default: |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + while(1) |
| 114 | + WFI(); |
| 115 | +} |
0 commit comments