Code import

This commit is contained in:
2017-12-15 23:52:14 +01:00
commit 56a935cdd0
103 changed files with 14510 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
//
// Created by MightyPork on 2017/12/15.
//
#include "platform.h"
#include "framework/resources.h"
#include "debug_uart.h"
#include "plat_compat.h"
#if USE_DEBUG_UART
/** Init the submodule. */
void DebugUart_Init(void)
{
// Debug UART
bool ok = true;
ok &= rsc_claim(&UNIT_SYSTEM, R_USART2);
ok &= rsc_claim(&UNIT_SYSTEM, R_PA2);
assert_param(ok);
}
/** Init the hardware peripheral - this is called early in the boot process */
void DebugUart_PreInit(void)
{
__HAL_RCC_USART2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_2, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetPinOutputType(GPIOA, LL_GPIO_PIN_2, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinSpeed(GPIOA, LL_GPIO_PIN_2, LL_GPIO_SPEED_FREQ_HIGH);
// commented out default values
// LL_USART_ConfigAsyncMode(USART2);
// LL_USART_SetDataWidth(USART2, LL_USART_DATAWIDTH_8B);
// LL_USART_SetParity(USART2, LL_USART_PARITY_NONE);
// LL_USART_SetStopBitsLength(USART2, LL_USART_STOPBITS_1);
// LL_USART_SetHWFlowCtrl(USART2, LL_USART_HWCONTROL_NONE);
LL_USART_EnableDirectionTx(USART2);
LL_USART_SetBaudRate(USART2, SystemCoreClock/2, 115200); // This is not great, let's hope it's like this on all platforms...
LL_USART_Enable(USART2);
}
/** Debug print, used by debug / newlib */
ssize_t _write_r(struct _reent *rptr, int fd, const void *buf, size_t len)
{
(void)rptr;
uint8_t *buff = buf;
for (uint32_t i = 0; i < len; i++) {
while (!LL_USART_IsActiveFlag_TC(USART2));
LL_USART_TransmitData8(USART2, *buff++);
}
return len;
}
#else
// No-uart variant
void DebugUart_Init(void) {}
ssize_t _write_r(struct _reent *rptr, int fd, const void *buf, size_t len) {}
#endif //USE_DEBUG_UART
+11
View File
@@ -0,0 +1,11 @@
//
// Created by MightyPork on 2017/12/15.
//
#ifndef GEX_DEBUG_UART_H
#define GEX_DEBUG_UART_H
void DebugUart_PreInit(void);
void DebugUart_Init(void);
#endif //GEX_DEBUG_UART_H
+86
View File
@@ -0,0 +1,86 @@
//
// Created by MightyPork on 2017/12/15.
//
#include <sched_queue.h>
#include <task_sched.h>
#include "usbd_core.h"
#include "USB/usb_device.h"
#include "framework/settings.h"
#include "framework/resources.h"
#include "framework/system_settings.h"
#include "pin_utils.h"
#include "lock_jumper.h"
static GPIO_TypeDef *lock_periph;
static uint32_t lock_llpin;
// We use macros LOCK_JUMPER_PORT, LOCK_JUMPER_PIN from plat_compat.h
/** Init the jumper subsystem */
void LockJumper_Init(void)
{
bool suc = true;
// Resolve and claim resource
Resource rsc = plat_pin2resource(LOCK_JUMPER_PORT, LOCK_JUMPER_PIN, &suc);
assert_param(suc);
suc &= rsc_claim(&UNIT_SYSTEM, rsc);
assert_param(suc);
// Resolve pin
lock_periph = plat_port2periph(LOCK_JUMPER_PORT, &suc);
lock_llpin = plat_pin2ll(LOCK_JUMPER_PIN, &suc);
assert_param(suc);
// Configure for input
LL_GPIO_SetPinMode(lock_periph, lock_llpin, LL_GPIO_MODE_INPUT);
LL_GPIO_SetPinPull(lock_periph, lock_llpin, LL_GPIO_PULL_UP);
SystemSettings.editable = (bool) LL_GPIO_IsInputPinSet(lock_periph, lock_llpin);
dbg("Settings editable? %d", SystemSettings.editable);
}
/** Handle jumper state change */
static void jumper_changed(void)
{
if (SystemSettings.editable) {
// Unlock
dbg("LOCK jumper removed, enabling MSC!");
} else {
// Lock
dbg("LOCK jumper replaced, saving to Flash & disabling MSC!");
if (SystemSettings.modified) {
settings_save();
}
}
plat_usb_reconnect();
}
/** Periodic jumper check */
void LockJumper_Check(void)
{
// Debounce cooldown
static uint32_t cooldown = 0;
if (cooldown > 0) {
cooldown--;
return;
}
// Read the pin state
bool old = SystemSettings.editable;
SystemSettings.editable = (bool) LL_GPIO_IsInputPinSet(lock_periph, lock_llpin);
if (old != SystemSettings.editable) {
// --- State changed ---
cooldown = 5; // 0.5s if called every 100 ms
jumper_changed();
}
}
+20
View File
@@ -0,0 +1,20 @@
//
// Created by MightyPork on 2017/12/15.
//
#ifndef GEX_LOCK_JUMPER_H
#define GEX_LOCK_JUMPER_H
#include "plat_compat.h"
/**
* Init the lock jumper subsystem
*/
void LockJumper_Init(void);
/**
* Check state of the lock jumper
*/
void LockJumper_Check(void);
#endif //GEX_LOCK_JUMPER_H
+90
View File
@@ -0,0 +1,90 @@
//
// Created by MightyPork on 2017/11/26.
//
#include "pin_utils.h"
#define PINS_COUNT 16
/** Pin number to LL bitfield mapping */
static const uint32_t ll_pins[PINS_COUNT] = {
LL_GPIO_PIN_0,
LL_GPIO_PIN_1,
LL_GPIO_PIN_2,
LL_GPIO_PIN_3,
LL_GPIO_PIN_4,
LL_GPIO_PIN_5,
LL_GPIO_PIN_6,
LL_GPIO_PIN_7,
LL_GPIO_PIN_8,
LL_GPIO_PIN_9,
LL_GPIO_PIN_10,
LL_GPIO_PIN_11,
LL_GPIO_PIN_12,
LL_GPIO_PIN_13,
LL_GPIO_PIN_14,
LL_GPIO_PIN_15,
};
/** Port number (A=0) to config struct pointer mapping */
static GPIO_TypeDef * const port_periphs[PORTS_COUNT] = {
GPIOA,
GPIOB,
GPIOC,
GPIOD,
GPIOE,
};
/** Convert pin number to LL bitfield */
uint32_t plat_pin2ll(uint8_t pin_number, bool *suc)
{
assert_param(suc != NULL);
if(pin_number >= PINS_COUNT) {
dbg("Bad pin: %d", pin_number);
// TODO proper report
*suc = false;
return 0;
}
return ll_pins[pin_number];
}
/** Convert port name (A,B,C...) to peripheral struct pointer */
GPIO_TypeDef *plat_port2periph(char port_name, bool *suc)
{
assert_param(suc != NULL);
if(port_name < 'A' || port_name >= ('A'+PORTS_COUNT)) {
dbg("Bad port: %c", port_name);
// TODO proper report
*suc = false;
return NULL;
}
uint8_t num = (uint8_t) (port_name - 'A');
return port_periphs[num];
}
/** Convert a pin to resource handle */
Resource plat_pin2resource(char port_name, uint8_t pin_number, bool *suc)
{
assert_param(suc != NULL);
if(port_name < 'A' || port_name >= ('A'+PORTS_COUNT)) {
dbg("Bad port: %c", port_name);
// TODO proper report
*suc = false;
return R_NONE;
}
if(pin_number >= PINS_COUNT) {
// TODO proper report
dbg("Bad pin: %d", pin_number);
*suc = false;
return R_NONE;
}
uint8_t num = (uint8_t) (port_name - 'A');
return R_PA0 + num*16 + pin_number;
}
+41
View File
@@ -0,0 +1,41 @@
//
// Utilities for parsing pins from settings to LL and resources
//
// Created by MightyPork on 2017/12/08.
//
#ifndef GEX_PIN_UTILS_H
#define GEX_PIN_UTILS_H
#include "platform.h"
#include "resources.h"
/**
* Convert pin number to LL driver bitfield for working with the pin.
*
* @param pin_number - number 0..15
* @param suc - set to false on failure, left unchanged on success.
* @return LL_GPIO_PIN_x
*/
uint32_t plat_pin2ll(uint8_t pin_number, bool *suc);
/**
* Convert pin name and number to a resource enum
*
* @param port_name - char 'A'..'Z'
* @param pin_number - number 0..15
* @param suc - set to false on failure, left unchanged on success
* @return the resource, or R_NONE
*/
Resource plat_pin2resource(char port_name, uint8_t pin_number, bool *suc);
/**
* Convert port name to peripheral instance
*
* @param port_name - char 'A'..'Z'
* @param suc - set to false on failure, left unchanged on success.
* @return instance
*/
GPIO_TypeDef *plat_port2periph(char port_name, bool *suc);
#endif //GEX_PIN_UTILS_H
+52
View File
@@ -0,0 +1,52 @@
//
// Created by MightyPork on 2017/12/08.
//
#ifndef GEX_PLAT_COMPAT_H
#define GEX_PLAT_COMPAT_H
// platform name for the version string
#define GEX_PLATFORM "STM32F103"
#include <stm32f1xx.h>
#include <stm32f1xx_hal.h>
#include <stm32f1xx_ll_adc.h>
#include <stm32f1xx_ll_bus.h>
#include <stm32f1xx_ll_cortex.h>
#include <stm32f1xx_ll_crc.h>
#include <stm32f1xx_ll_dac.h>
#include <stm32f1xx_ll_dma.h>
#include <stm32f1xx_ll_exti.h>
#include <stm32f1xx_ll_fsmc.h>
#include <stm32f1xx_ll_gpio.h>
#include <stm32f1xx_ll_i2c.h>
#include <stm32f1xx_ll_iwdg.h>
#include <stm32f1xx_ll_pwr.h>
#include <stm32f1xx_ll_rcc.h>
#include <stm32f1xx_ll_rtc.h>
#include <stm32f1xx_ll_sdmmc.h>
#include <stm32f1xx_ll_spi.h>
#include <stm32f1xx_ll_system.h>
#include <stm32f1xx_ll_tim.h>
#include <stm32f1xx_ll_usart.h>
#include <stm32f1xx_ll_usb.h>
#include <stm32f1xx_ll_utils.h>
#include <stm32f1xx_ll_wwdg.h>
// size, determines position of the flash storage
#define FLASH_SIZE (64*1024)
#define SETTINGS_BLOCK_SIZE (1024*2) // this must be a multiple of FLASH pages
#define SETTINGS_FLASH_ADDR (0x08000000 + FLASH_SIZE - SETTINGS_BLOCK_SIZE)
// Number of GPIO ports A,B,C...
#define PORTS_COUNT 5
// Lock jumper config
#define LOCK_JUMPER_PORT 'C'
#define LOCK_JUMPER_PIN 14
// Status LED config
#define STATUS_LED_PORT 'C'
#define STATUS_LED_PIN 13
#endif //GEX_PLAT_COMPAT_H
+37
View File
@@ -0,0 +1,37 @@
//
// Created by MightyPork on 2017/11/26.
//
#include "platform.h"
#include "comm/messages.h"
#include "framework/resources.h"
#include "framework/settings.h"
#include "framework/system_settings.h"
#include "lock_jumper.h"
#include "status_led.h"
#include "debug_uart.h"
void plat_init(void)
{
// Load system defaults
systemsettings_init();
dbg("Setting up resources ...");
rsc_init_registry();
plat_init_resources();
LockJumper_Init();
StatusLed_Init();
DebugUart_Init(); // <- only the resource claim
dbg("Registering platform units ...");
// All user-configurable units are now added to the repository
plat_register_units();
dbg("Loading settings ...");
// Load settings from Flash and apply (includes System settings and all Unit settings)
settings_load();
comm_init();
}
+87
View File
@@ -0,0 +1,87 @@
//
// Created by MightyPork on 2017/11/26.
//
#include "platform.h"
#include "usbd_core.h"
#include "USB/usb_device.h"
#include "framework/resources.h"
// ----- SUPPORTED UNITS -----
#include "framework/unit_registry.h"
#include "units/pin/unit_pin.h"
#include "units/neopixel/unit_neopixel.h"
void plat_register_units(void)
{
ureg_add_type(&UNIT_PIN);
ureg_add_type(&UNIT_NEOPIXEL);
}
// ----- RELEASE AVAILABLE RESOURCES -----
void plat_init_resources(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
// Platform F103C8T6 - free all present resources
{
rsc_free(NULL, R_ADC1);
rsc_free(NULL, R_ADC2);
rsc_free(NULL, R_I2C1);
rsc_free(NULL, R_I2C2);
rsc_free(NULL, R_SPI1);
rsc_free(NULL, R_SPI2);
rsc_free(NULL, R_TIM1);
rsc_free(NULL, R_TIM2);
rsc_free(NULL, R_TIM3);
rsc_free(NULL, R_TIM4);
rsc_free(NULL, R_USART1);
rsc_free(NULL, R_USART2);
rsc_free(NULL, R_USART3);
rsc_free_range(NULL, R_PA0, R_PA15);
rsc_free_range(NULL, R_PB0, R_PB15);
rsc_free_range(NULL, R_PC13, R_PC15);
rsc_free_range(NULL, R_PD0, R_PD1);
}
// Claim resources not available due to board layout or internal usage
{
bool ok = true;
// HAL timebase
ok &= rsc_claim(&UNIT_SYSTEM, R_TIM1);
// HSE crystal
ok &= rsc_claim(&UNIT_SYSTEM, R_PD0);
ok &= rsc_claim(&UNIT_SYSTEM, R_PD1);
// SWD
ok &= rsc_claim(&UNIT_SYSTEM, R_PA13);
ok &= rsc_claim(&UNIT_SYSTEM, R_PA14);
// USB
ok &= rsc_claim(&UNIT_SYSTEM, R_PA11);
ok &= rsc_claim(&UNIT_SYSTEM, R_PA12);
// BOOT pin(s)
ok &= rsc_claim(&UNIT_SYSTEM, R_PB2); // BOOT1
assert_param(ok);
}
}
// ---- USB reconnect ----
/** USB re-connect */
void plat_usb_reconnect(void)
{
// F103 doesn't have pull-up control, this is probably the best we can do
USBD_LL_Reset(&hUsbDeviceFS);
}
+59
View File
@@ -0,0 +1,59 @@
//
// Created by MightyPork on 2017/12/08.
//
#ifndef GEX_PLATFORM_H
#define GEX_PLATFORM_H
#include <inttypes.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stddef.h>
#include <string.h>
#include <malloc.h>
// FreeRTOS includes
#include <cmsis_os.h>
// platform-specific stuff (includes stm32 driver headers)
#include "plat_compat.h"
// assert_param, trap...
#include "stm32_assert.h"
// inIRQ etc
#include "cortex_utils.h"
// MIN, MAX, static assert etc
#include "macro.h"
// smaller replacement for regular snprintf - SNPRINTF
#include "snprintf.h"
// debug logging
#include "debug.h"
// error codes and strings
#include "utils/error.h"
// GEX version string
#include "version.h"
// ---
/**
* Init the platform
*/
void plat_init(void);
/**
* Init resources available for this platform
*/
void plat_init_resources(void);
/**
* Register units available for this platform / build
*/
void plat_register_units(void);
/**
* Re-connect the USB, triggering descriptors reload.
* Use the DPPU bit on USB_BCDR, if available.
*/
void plat_usb_reconnect(void);
#endif //GEX_PLATFORM_H
+90
View File
@@ -0,0 +1,90 @@
//
// Created by MightyPork on 2017/12/15.
//
#include "platform.h"
#include "framework/resources.h"
#include "status_led.h"
#include "pin_utils.h"
static uint32_t indicators[_INDICATOR_COUNT];
static GPIO_TypeDef *led_periph;
static uint32_t led_llpin;
/** Set up the LED */
void StatusLed_Init(void)
{
bool suc = true;
// Resolve and claim resource
Resource rsc = plat_pin2resource(STATUS_LED_PORT, STATUS_LED_PIN, &suc);
assert_param(suc);
suc &= rsc_claim(&UNIT_SYSTEM, rsc);
assert_param(suc);
// Resolve pin
led_periph = plat_port2periph(STATUS_LED_PORT, &suc);
led_llpin = plat_pin2ll(STATUS_LED_PIN, &suc);
assert_param(suc);
// Configure for output
LL_GPIO_SetPinMode(led_periph, led_llpin, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinOutputType(led_periph, led_llpin, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinSpeed(led_periph, led_llpin, LL_GPIO_SPEED_FREQ_LOW);
}
/** Set indicator ON */
void StatusLed_On(enum GEX_StatusIndicator indicator)
{
indicators[indicator] = osWaitForever;
if (indicator == STATUS_FAULT) {
// Persistent light
GPIOC->ODR |= 1<<13;
}
}
/** Set indicator OFF */
void StatusLed_Off(enum GEX_StatusIndicator indicator)
{
indicators[indicator] = 0;
// TODO some effect
}
/** Set or reset a indicator */
void StatusLed_Set(enum GEX_StatusIndicator indicator, bool set)
{
if (set) {
StatusLed_On(indicator);
} else {
StatusLed_Off(indicator);
}
}
/** Turn indicator ON for a given interval */
void StatusLed_Flash(enum GEX_StatusIndicator indicator, uint32_t ms)
{
indicators[indicator] = ms;
// TODO
}
/** Millisecond tick */
void StatusLed_Tick(void)
{
for (uint32_t i = 0; i < _INDICATOR_COUNT; i++) {
if (indicators[i] != osWaitForever && indicators[i] != 0) {
if (--indicators[i]) {
StatusLed_Off((enum GEX_StatusIndicator) i);
}
}
}
}
/** Heartbeat callback from the main thread */
void StatusLed_Heartbeat(void)
{
// TODO fixme
GPIOC->ODR ^= 1<<13;
}
+67
View File
@@ -0,0 +1,67 @@
//
// Created by MightyPork on 2017/12/15.
//
#ifndef GEX_INDICATORS_H
#define GEX_INDICATORS_H
#include "platform.h"
/**
* Indicator (LED or blinking pattern)
*/
enum GEX_StatusIndicator {
STATUS_FAULT = 0,
STATUS_USB_CONN,
STATUS_USB_ACTIVITY,
STATUS_DISK_BUSY,
STATUS_DISK_ATTACHED,
_INDICATOR_COUNT
};
/**
* Initialize the statis LED(s)
*/
void StatusLed_Init(void);
/**
* Set indicator ON
*
* @param indicator
*/
void StatusLed_On(enum GEX_StatusIndicator indicator);
/**
* Set indicator OFF
*
* @param indicator
*/
void StatusLed_Off(enum GEX_StatusIndicator indicator);
/**
* Indicator set or reset
*
* @param indicator
* @param set
*/
void StatusLed_Set(enum GEX_StatusIndicator indicator, bool set);
/**
* Turn indicator ON for a given interval
*
* @param indicator
* @param ms - time ON in ms
*/
void StatusLed_Flash(enum GEX_StatusIndicator indicator, uint32_t ms);
/**
* Ms tick for indicators
*/
void StatusLed_Tick(void);
/**
* Heartbeat callback from the main thread to indicate activity
*/
void StatusLed_Heartbeat(void);
#endif //GEX_INDICATORS_H