This commit is contained in:
jaseg 2025-12-19 11:14:57 +01:00
parent 78d5b2712d
commit fb56c8913c
3 changed files with 168 additions and 106 deletions

23
.gdbinit Normal file
View file

@ -0,0 +1,23 @@
target remote localhost:3333
set print pretty on
set print elements 512
set pagination off
# Update GDB's Python paths with the `sys.path` values of the local Python installation,
# whether that is brew'ed Python, a virtualenv, or another system python.
# Convert GDB to interpret in Python
python
import os,subprocess,sys
# Execute a Python using the user's shell and pull out the sys.path (for site-packages)
paths = subprocess.check_output('python -c "import os,sys;print(os.linesep.join(sys.path).strip())"',shell=True).decode("utf-8").split()
# Extend GDB's Python's search path
sys.path.extend(paths)
print(sys.path)
end
#source upstream/PyCortexMDebug/cmdebug/svd_gdb.py
#svd_load upstream/stm32square/svd/STM32G474.svd
load

View file

@ -67,7 +67,7 @@ PYTHON3 ?= python3
DOT ?= dot
DEVICE_FAMILY := $(shell echo $(DEVICE) | grep -Eio 'STM32[a-z]{1,2}[0-9]'|cut -c 6-)
DEVICE_DEFINES := -D$(DEVICE) -DSTM32$(DEVICE_FAMILY) $(addprefix -D,$(shell cat stm32_buildinfo.defines))
DEVICE_DEFINES := -D$(DEVICE) -DHSE_VALUE=8000000U -DSTM32$(DEVICE_FAMILY) $(addprefix -D,$(shell cat stm32_buildinfo.defines))
ARCH_FLAGS ?= -mthumb -mcpu=cortex-m3 -mfloat-abi=softfp
SYSTEM_FLAGS ?= -nostdlib -ffreestanding -nostartfiles

View file

@ -1,56 +1,85 @@
#include "main.h"
__attribute__((section(".boot_counter"), aligned(1024)))
uint32_t boot_counter[1024] = {[0 ... 1023] = 0xffffffff};
uint32_t boot_counter[4096] = {[0 ... 4095] = 0xffffffff};
void SystemClock_Config(void);
/* Bluepill GPIO pins in physical board order (left side, then right side) */
typedef struct {
GPIO_TypeDef *port;
uint16_t pin;
} GPIO_Pin_t;
static const GPIO_Pin_t light_pins[] = {
/* Left side (top to bottom) */
{GPIOB, GPIO_PIN_12}, /* yes */
{GPIOB, GPIO_PIN_13}, /* yes */
{GPIOB, GPIO_PIN_14}, /* yes */
{GPIOB, GPIO_PIN_15}, /* yes */
{GPIOA, GPIO_PIN_8}, /* yes */
{GPIOA, GPIO_PIN_9}, /* yes */
{GPIOA, GPIO_PIN_10}, /* yes */
{GPIOA, GPIO_PIN_11}, /* yes */
{GPIOB, GPIO_PIN_5}, /* yes */
{GPIOB, GPIO_PIN_6}, /* yes */
{GPIOB, GPIO_PIN_7}, /* yes */
{GPIOB, GPIO_PIN_8}, /* yes */
{GPIOB, GPIO_PIN_9}, /* yes */
{GPIOB, GPIO_PIN_10}, /* yes */
{GPIOB, GPIO_PIN_11}, /* yes */
{GPIOB, GPIO_PIN_1}, /* yes */
{GPIOA, GPIO_PIN_5}, /* yes */
{GPIOA, GPIO_PIN_4}, /* yes */
};
#define NUM_LIGHT_PINS (sizeof(light_pins) / sizeof(light_pins[0]))
volatile int current_index = 0;
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : LED_Pin */
GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : LED_Pin */
GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
/* LED output pins */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
for (int i = 0; i < NUM_LIGHT_PINS; i++) {
GPIO_InitStruct.Pin = light_pins[i].pin;
HAL_GPIO_Init(light_pins[i].port, &GPIO_InitStruct);
HAL_GPIO_WritePin(light_pins[i].port, light_pins[i].pin, GPIO_PIN_RESET);
}
}
int get_and_increment_boot_count(void) {
const size_t array_len = sizeof(boot_counter)/sizeof(boot_counter[0]);
int count = 0;
for (size_t i=0; i<array_len; i++) {
uint32_t val = boot_counter[i];
if (val) {
for (int j=0; j<32; j++) {
uint32_t mask = 1<<j;
if (val & mask) {
count = i*32 + j;
HAL_FLASH_Unlock();
uint32_t addr = (uint32_t)&boot_counter[i];
uint32_t new_val = val & ~mask;
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr, new_val);
HAL_FLASH_Lock();
return count;
}
}
if (boot_counter[i]) {
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)&boot_counter[i], 0);
HAL_FLASH_Lock();
return i;
}
}
count = array_len * 32;
HAL_FLASH_Unlock();
FLASH_EraseInitTypeDef erase_init;
@ -60,102 +89,112 @@ int get_and_increment_boot_count(void) {
uint32_t page_error;
HAL_FLASHEx_Erase(&erase_init, &page_error);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)&boot_counter[0], 0xfffffffe);
HAL_FLASH_Lock();
return count;
return array_len;
}
uint32_t xorshift32(uint32_t x)
{
/* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
/* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
}
void increment_light(void) {
/* The boot counter is in FLASH, increment it before other initialization */
int boot_count = get_and_increment_boot_count();
uint32_t seed = 0xc8f1de1f;
for (int i=0; i<boot_count; i++) {
seed = xorshift32(seed);
}
HAL_GPIO_WritePin(light_pins[current_index].port, light_pins[current_index].pin, GPIO_PIN_RESET);
current_index = seed % NUM_LIGHT_PINS;
/* Turn on current pin */
HAL_GPIO_WritePin(light_pins[current_index].port, light_pins[current_index].pin, GPIO_PIN_SET);
}
int main(void)
{
HAL_Init();
SystemClock_Config();
HAL_Init();
SystemClock_Config();
/* The boot counter is in FLASH, increment it before other initialization */
int boot_count = get_and_increment_boot_count();
MX_GPIO_Init();
MX_GPIO_Init();
for (int i=0; i<10; i++) {
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
HAL_Delay(50);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
HAL_Delay(50);
}
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
HAL_Delay(500);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
HAL_Delay(500);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
HAL_Delay(500);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
uint32_t seed = 0xc8f1de1f;
for (int i=0; i<boot_count; i++) {
seed = xorshift32(seed);
}
while (1) {
}
while (1) {
increment_light();
for (int day=0; day<14; day++) {
for (int hour=0; hour<24; hour++) {
for (int minute=0; minute<60; minute++) {
for (int second=0; second<60; second++) {
HAL_Delay(1000);
}
}
}
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT