works
This commit is contained in:
parent
78d5b2712d
commit
fb56c8913c
3 changed files with 168 additions and 106 deletions
23
.gdbinit
Normal file
23
.gdbinit
Normal 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
|
||||
2
Makefile
2
Makefile
|
|
@ -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
|
||||
|
|
|
|||
121
src/main.c
121
src/main.c
|
|
@ -1,9 +1,41 @@
|
|||
#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};
|
||||
|
|
@ -12,6 +44,7 @@ static void MX_GPIO_Init(void)
|
|||
__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);
|
||||
|
|
@ -23,33 +56,29 @@ static void MX_GPIO_Init(void)
|
|||
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;
|
||||
|
||||
if (boot_counter[i]) {
|
||||
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_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)&boot_counter[i], 0);
|
||||
HAL_FLASH_Lock();
|
||||
|
||||
return count;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
count = array_len * 32;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
|
||||
|
|
@ -60,12 +89,10 @@ 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)
|
||||
|
|
@ -77,36 +104,48 @@ uint32_t xorshift32(uint32_t x)
|
|||
return x;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
HAL_Init();
|
||||
SystemClock_Config();
|
||||
void increment_light(void) {
|
||||
|
||||
/* The boot counter is in FLASH, increment it before other initialization */
|
||||
int boot_count = get_and_increment_boot_count();
|
||||
|
||||
MX_GPIO_Init();
|
||||
|
||||
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);
|
||||
}
|
||||
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();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue