diff --git a/.gdbinit b/.gdbinit new file mode 100644 index 0000000..a4f8b46 --- /dev/null +++ b/.gdbinit @@ -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 diff --git a/Makefile b/Makefile index 239660b..793fe72 100644 --- a/Makefile +++ b/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 diff --git a/src/main.c b/src/main.c index 578fa5f..b9eaa4a 100644 --- a/src/main.c +++ b/src/main.c @@ -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> 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