From a3e043a8e1719f09c820ee651651846febb79756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 17 Dec 2017 01:54:35 +0100 Subject: [PATCH 1/3] work on f407 support --- FreeRTOSConfig.h | 3 ++- gex_hooks.c | 2 +- platform/plat_compat.h | 45 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/FreeRTOSConfig.h b/FreeRTOSConfig.h index 727eb79..2c2a0b7 100644 --- a/FreeRTOSConfig.h +++ b/FreeRTOSConfig.h @@ -126,7 +126,8 @@ to exclude the API function. */ ///* Cortex-M specific definitions. */ -#if defined(GEX_PLAT_F103_BLUEPILL) || defined(GEX_PLAT_F303_DISCOVERY) +#if defined(GEX_PLAT_F103_BLUEPILL) || defined(GEX_PLAT_F303_DISCOVERY) \ + || defined(GEX_PLAT_F407_DISCOVERY) // This is for F103+ /* The lowest interrupt priority that can be used in a call to a "set priority" diff --git a/gex_hooks.c b/gex_hooks.c index 5be3785..3db152a 100644 --- a/gex_hooks.c +++ b/gex_hooks.c @@ -18,7 +18,7 @@ void GEX_MsTick(void) TF_Tick(comm); StatusLed_Tick(); } -extern void HardFault_Handler(void); + /** * Early init, even before RTOS starts */ diff --git a/platform/plat_compat.h b/platform/plat_compat.h index ad0d456..2b78d8f 100644 --- a/platform/plat_compat.h +++ b/platform/plat_compat.h @@ -145,6 +145,51 @@ #define STATUS_LED_PORT 'E' #define STATUS_LED_PIN 13 +#elif defined(GEX_PLAT_F407_DISCOVERY) + +// platform name for the version string + #define GEX_PLATFORM "STM32F407-Discovery" + + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + + // size, determines position of the flash storage + #define FLASH_SIZE (1024*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 6 + + // Lock jumper config + #define LOCK_JUMPER_PORT 'C' + #define LOCK_JUMPER_PIN 14 // completely random choice here... TBD + + // Status LED config + #define STATUS_LED_PORT 'D' // orange + #define STATUS_LED_PIN 13 + #else #error "BAD PLATFORM! Please select GEX platform using a -DGEX_PLAT_* compile flag" #endif From 03075bdd5e4a1f3169b282c8587b406539feab67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 17 Dec 2017 11:31:04 +0100 Subject: [PATCH 2/3] builds with f407 --- USB/usb_device.c | 9 +++- USB/usbd_conf.c | 118 ++++++++++++++++++++++++++++++++++++++++- USB/usbd_desc.c | 2 +- framework/resources.h | 6 ++- framework/settings.c | 9 ++++ platform/debug_uart.c | 10 +++- platform/plat_compat.h | 11 ++-- platform/platform.c | 57 ++++++++++++++++++-- 8 files changed, 208 insertions(+), 14 deletions(-) diff --git a/USB/usb_device.c b/USB/usb_device.c index b04f13b..c89e048 100644 --- a/USB/usb_device.c +++ b/USB/usb_device.c @@ -95,8 +95,7 @@ static void __attribute__((used)) BASE_USB_IRQHandler(void) HAL_PCD_IRQHandler(&hpcd_USB_FS); } - -// Function from F103 +// Function for F103 /** * @brief This function handles USB low priority or CAN RX0 interrupts. */ @@ -114,6 +113,12 @@ void USB_LP_CAN_RX0_IRQHandler(void) __attribute__((alias("BASE_USB_IRQHandler") */ void USB_IRQHandler(void) __attribute__((alias("BASE_USB_IRQHandler"))); +// Function for F407 +/** +* @brief This function handles USB On The Go FS global interrupt. +*/ +void OTG_FS_IRQHandler(void) __attribute__((alias("BASE_USB_IRQHandler"))); + /** * @} */ diff --git a/USB/usbd_conf.c b/USB/usbd_conf.c index e0d1703..3f9f3ab 100644 --- a/USB/usbd_conf.c +++ b/USB/usbd_conf.c @@ -73,6 +73,7 @@ void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state); /* MSP Init */ void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle) { +#if defined(USB) if(pcdHandle->Instance==USB) { /* USER CODE BEGIN USB_MspInit 0 */ @@ -109,10 +110,35 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle) /* USER CODE END USB_MspInit 1 */ } +#elif defined(USB_OTG_FS) + // This is for F407 with USB OTG peripheral + if (pcdHandle->Instance==USB_OTG_FS) { + /**USB_OTG_FS GPIO Configuration + PA11 ------> USB_OTG_FS_DM + PA12 ------> USB_OTG_FS_DP + */ + __HAL_RCC_GPIOA_CLK_ENABLE(); + + LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_11, LL_GPIO_MODE_ALTERNATE); + LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_12, LL_GPIO_MODE_ALTERNATE); + LL_GPIO_SetAFPin_8_15(GPIOA, LL_GPIO_PIN_11, LL_GPIO_AF_10); + LL_GPIO_SetAFPin_8_15(GPIOA, LL_GPIO_PIN_12, LL_GPIO_AF_10); + + /* Peripheral clock enable */ + __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); + + /* Peripheral interrupt init */ + HAL_NVIC_SetPriority(OTG_FS_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(OTG_FS_IRQn); + } +#else + #error "BAD USB!" +#endif } void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle) { +#if defined(USB) if(pcdHandle->Instance==USB) { /* USER CODE BEGIN USB_MspDeInit 0 */ @@ -136,6 +162,20 @@ void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle) /* USER CODE END USB_MspDeInit 1 */ } +#elif defined(USB_OTG_FS) + // This is for F407 with USB OTG peripheral + if (pcdHandle->Instance==USB_OTG_FS) { + __HAL_RCC_USB_OTG_FS_CLK_DISABLE(); + + LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_11, LL_GPIO_MODE_ANALOG); + LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_12, LL_GPIO_MODE_ANALOG); + + /* Peripheral interrupt init */ + HAL_NVIC_DisableIRQ(OTG_FS_IRQn); + } +#else +#error "BAD USB!" +#endif } /** @@ -216,6 +256,11 @@ void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) { /* Inform USB library that core enters in suspend Mode */ USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData); + +#if PLAT_USB_PHYCLOCK + __HAL_PCD_GATE_PHYCLOCK(hpcd); +#endif + /*Enter in STOP mode */ /* USER CODE BEGIN 2 */ if (hpcd->Init.low_power_enable) @@ -290,7 +335,39 @@ void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) * @retval USBD Status */ USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev) -{ +{ +#if PLAT_USB_OTGFS + /* Init USB_IP */ + if (pdev->id == DEVICE_FS) { + /* Link The driver to the stack */ + hpcd_USB_FS.pData = pdev; + pdev->pData = &hpcd_USB_FS; + + hpcd_USB_FS.Instance = USB_OTG_FS; + hpcd_USB_FS.Init.dev_endpoints = 4; + hpcd_USB_FS.Init.speed = PCD_SPEED_FULL; + hpcd_USB_FS.Init.dma_enable = DISABLE; + hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_8; + hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED; + hpcd_USB_FS.Init.Sof_enable = DISABLE; + hpcd_USB_FS.Init.low_power_enable = DISABLE; + hpcd_USB_FS.Init.lpm_enable = DISABLE; + hpcd_USB_FS.Init.vbus_sensing_enable = DISABLE; + hpcd_USB_FS.Init.use_dedicated_ep1 = DISABLE; + if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK) + { + _Error_Handler(__FILE__, __LINE__); + } + + // FIXME this is likely wrong + HAL_PCDEx_SetRxFiFo(&hpcd_USB_FS, 0x80); + + HAL_PCDEx_SetTxFiFo(&hpcd_USB_FS, 0, 0x40); // EP0 + HAL_PCDEx_SetTxFiFo(&hpcd_USB_FS, MSC_EPOUT_ADDR, 0x40); + HAL_PCDEx_SetTxFiFo(&hpcd_USB_FS, CDC_OUT_EP, 0x40); + // EP 3 is Rx only + } +#else /* Init USB_IP */ /* Link The driver to the stack */ hpcd_USB_FS.pData = pdev; @@ -322,8 +399,8 @@ USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev) HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_IN_EP , PCD_SNG_BUF, ptr += 64); // 64 HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_OUT_EP , PCD_SNG_BUF, ptr += 64); // 64 HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , CDC_CMD_EP , PCD_SNG_BUF, ptr += 16); // 16 - (void)ptr; +#endif return USBD_OK; } @@ -509,6 +586,43 @@ uint32_t USBD_LL_GetRxDataSize (USBD_HandleTypeDef *pdev, uint8_t ep_addr) return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*) pdev->pData, ep_addr); } +#if (USBD_LPM_ENABLED == 1) +/** + * @brief HAL_PCDEx_LPM_Callback : Send LPM message to user layer + * @param hpcd: PCD handle + * @param msg: LPM message + * @retval HAL status + */ +void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg) +{ + switch ( msg) + { + case PCD_LPM_L0_ACTIVE: + if (hpcd->Init.low_power_enable) + { + SystemClock_Config(); + + /* Reset SLEEPDEEP bit of Cortex System Control Register */ + SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk)); + } + __HAL_PCD_UNGATE_PHYCLOCK(hpcd); + USBD_LL_Resume(hpcd->pData); + break; + + case PCD_LPM_L1_ACTIVE: + __HAL_PCD_GATE_PHYCLOCK(hpcd); + USBD_LL_Suspend(hpcd->pData); + + /*Enter in STOP mode */ + if (hpcd->Init.low_power_enable) + { + /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register */ + SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk)); + } + break; + } +} +#endif /** * @brief Delays routine for the USB Device Library. * @param Delay: Delay in ms diff --git a/USB/usbd_desc.c b/USB/usbd_desc.c index d987f0e..e27ba3c 100644 --- a/USB/usbd_desc.c +++ b/USB/usbd_desc.c @@ -247,7 +247,7 @@ uint8_t * USBD_FS_ManufacturerStrDescriptor( USBD_SpeedTypeDef speed , uint16_t uint8_t * USBD_FS_SerialStrDescriptor( USBD_SpeedTypeDef speed , uint16_t *length) { char buff[25]; - fixup_sprintf(buff, "%08"PRIX32"%08"PRIX32"%08"PRIX32, + fixup_sprintf(buff, "%08"PRIX32"-%08"PRIX32"-%08"PRIX32, LL_GetUID_Word0(), LL_GetUID_Word1(), LL_GetUID_Word2() diff --git a/framework/resources.h b/framework/resources.h index 2d59f6f..54285c7 100644 --- a/framework/resources.h +++ b/framework/resources.h @@ -31,8 +31,12 @@ X(ADC1) X(ADC2) X(ADC3) X(ADC4) \ X(OPAMP1) X(OPAMP2) X(OPAMP3) X(OPAMP4) \ X(DAC1) X(DAC2) \ - X(CAN) \ + X(CAN1) X(CAN2) \ X(TSC) \ + X(DCMI) \ + X(ETH) \ + X(FSMC) \ + X(SDIO) \ X(COMP1) X(COMP2) X(COMP3) X(COMP4) X(COMP5) X(COMP6) X(COMP7) \ X(HDMI_CEC) \ X(USART1) X(USART2) X(USART3) X(USART4) X(USART5) X(USART6) \ diff --git a/framework/settings.c b/framework/settings.c index f4c2cc9..42ddb7b 100644 --- a/framework/settings.c +++ b/framework/settings.c @@ -145,9 +145,18 @@ void settings_save(void) erase.Banks = FLASH_BANK_1; // TODO ????? #endif +#if defined(GEX_PLAT_F407_DISCOVERY) + // specialty for F4 with too much flash + erase.NbSectors = 1; + erase.Sector = SETTINGS_FLASH_SECTOR; + erase.TypeErase = FLASH_TYPEERASE_SECTORS; + erase.VoltageRange = FLASH_VOLTAGE_RANGE_3; + erase.Banks = FLASH_BANK_1; // unused for sector erase +#else erase.NbPages = SETTINGS_BLOCK_SIZE/FLASH_PAGE_SIZE; erase.PageAddress = SETTINGS_FLASH_ADDR; erase.TypeErase = FLASH_TYPEERASE_PAGES; +#endif uint32_t pgerror = 0; hst = HAL_FLASHEx_Erase(&erase, &pgerror); assert_param(pgerror == 0xFFFFFFFFU); diff --git a/platform/debug_uart.c b/platform/debug_uart.c index 0a3d5b6..a9f91d6 100644 --- a/platform/debug_uart.c +++ b/platform/debug_uart.c @@ -48,6 +48,12 @@ void DebugUart_PreInit(void) LL_USART_OVERSAMPLING_16, 115200); LL_GPIO_SetAFPin_0_7(GPIOA, LL_GPIO_PIN_2, LL_GPIO_AF_7); // uart2 is AF7 here +#elif GEX_PLAT_F407_DISCOVERY + LL_USART_SetBaudRate(USART2, + SystemCoreClock/4, // if core is at 168 MHz, this is 48 MHz + LL_USART_OVERSAMPLING_16, + 115200); + LL_GPIO_SetAFPin_0_7(GPIOA, LL_GPIO_PIN_2, LL_GPIO_AF_7); // uart2 is AF7 here (same like 303) #else #error "BAD PLATFORM!" #endif @@ -74,6 +80,8 @@ ssize_t _write_r(struct _reent *rptr, int fd, const void *buf, size_t len) // No-uart variant void DebugUart_Init(void) {} -ssize_t _write_r(struct _reent *rptr, int fd, const void *buf, size_t len) {} +ssize_t _write_r(struct _reent *rptr, int fd, const void *buf, size_t len) { + return len; +} #endif //USE_DEBUG_UART diff --git a/platform/plat_compat.h b/platform/plat_compat.h index 2b78d8f..c47ce95 100644 --- a/platform/plat_compat.h +++ b/platform/plat_compat.h @@ -150,6 +150,9 @@ // platform name for the version string #define GEX_PLATFORM "STM32F407-Discovery" + #define PLAT_USB_PHYCLOCK 1 + #define PLAT_USB_OTGFS 1 + #include #include #include @@ -175,9 +178,11 @@ #include // size, determines position of the flash storage - #define FLASH_SIZE (1024*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) + + // we use the first 128kB sector. Unfortunately the whole sector must be erased before writing. + #define SETTINGS_FLASH_SECTOR 6 + #define SETTINGS_BLOCK_SIZE (1024*2) + #define SETTINGS_FLASH_ADDR (0x08000000 + (16*4+64)*1024) // Number of GPIO ports A,B,C... #define PORTS_COUNT 6 diff --git a/platform/platform.c b/platform/platform.c index bdc5cb4..d798e21 100644 --- a/platform/platform.c +++ b/platform/platform.c @@ -75,7 +75,7 @@ void plat_init_resources(void) // Platform F073RBT - free all present resources { rsc_free(NULL, R_ADC1); - rsc_free(NULL, R_CAN); + rsc_free(NULL, R_CAN1); rsc_free_range(NULL, R_COMP1, R_COMP2); rsc_free(NULL, R_DAC1); rsc_free(NULL, R_HDMI_CEC); @@ -121,7 +121,7 @@ void plat_init_resources(void) // Platform F303VCT - free all present resources { rsc_free_range(NULL, R_ADC1, R_ADC4); - rsc_free(NULL, R_CAN); + rsc_free(NULL, R_CAN1); rsc_free_range(NULL, R_COMP1, R_COMP7); rsc_free(NULL, R_HDMI_CEC); rsc_free(NULL, R_DAC1); @@ -164,6 +164,55 @@ void plat_init_resources(void) // BOOT pin(s) ok &= rsc_claim(&UNIT_SYSTEM, R_PB2); // BOOT1 + assert_param(ok); + } +#elif defined(GEX_PLAT_F407_DISCOVERY) + __HAL_RCC_GPIOF_CLK_ENABLE(); + + // Platform F407VGT - free all present resources + { + rsc_free_range(NULL, R_ADC1, R_ADC3); + rsc_free_range(NULL, R_CAN1, R_CAN2); + rsc_free_range(NULL, R_COMP1, R_COMP7); + rsc_free(NULL, R_DAC1); + rsc_free(NULL, R_DCMI); + rsc_free(NULL, R_ETH); + rsc_free(NULL, R_FSMC); + rsc_free_range(NULL, R_I2C1, R_I2C3); + rsc_free_range(NULL, R_I2S2, R_I2S3); + rsc_free(NULL, R_SDIO); + rsc_free_range(NULL, R_SPI1, R_SPI3); + rsc_free_range(NULL, R_TIM1, R_TIM14); + rsc_free_range(NULL, R_USART1, R_USART3); + rsc_free(NULL, R_USART6); + + rsc_free_range(NULL, R_PA0, R_PA15); + rsc_free_range(NULL, R_PB0, R_PB15); + rsc_free_range(NULL, R_PC0, R_PC15); + rsc_free_range(NULL, R_PD0, R_PD15); + rsc_free_range(NULL, R_PE0, R_PE15); + // also has 2 PH pins + + // F407 appears to have fewer GPIOs than F303? + } + + // 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 + // H0 and H1 + // 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); } #else @@ -179,10 +228,10 @@ void plat_init_resources(void) */ void plat_usb_reconnect(void) { -#ifdef GEX_PLAT_F103_BLUEPILL + // TODO add better reset methods available on different chips + // F103 doesn't have pull-up control, this is probably the best we can do // This does not seem to trigger descriptors reload. USBD_LL_Reset(&hUsbDeviceFS); -#endif } From 803cd579608e26af02e304396376d15adf48e686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 17 Dec 2017 16:09:07 +0100 Subject: [PATCH 3/3] simplified config file comments, fixed settings not peristed, fixed F407 flash code --- framework/settings.c | 19 ++++++++++++++---- framework/system_settings.c | 14 ++++++++++--- framework/system_settings.h | 7 ++++++- framework/unit_registry.c | 39 ++++++++++++++++++------------------- gex.mk | 1 + platform/lock_jumper.c | 5 ++++- platform/plat_compat.h | 2 +- tasks/task_main.c | 2 +- 8 files changed, 58 insertions(+), 31 deletions(-) diff --git a/framework/settings.c b/framework/settings.c index 42ddb7b..ae14bc2 100644 --- a/framework/settings.c +++ b/framework/settings.c @@ -2,6 +2,7 @@ // Created by MightyPork on 2017/11/26. // +#include "utils/hexdump.h" #include "platform.h" #include "settings.h" #include "unit_registry.h" @@ -16,6 +17,11 @@ void settings_load(void) { uint8_t *buffer = (uint8_t *) SETTINGS_FLASH_ADDR; +#if DEBUG_FLASH_WRITE + dbg("reading @ %p", (void*)SETTINGS_FLASH_ADDR); + hexDump("Flash", buffer, 64); +#endif + PayloadParser pp = pp_start(buffer, SETTINGS_BLOCK_SIZE, NULL); // Check the integrity marker @@ -46,7 +52,7 @@ static uint8_t save_buffer[SAVE_BUF_SIZE]; static uint32_t save_addr; #if DEBUG_FLASH_WRITE -#define fls_printf(fmt, ...) dbg(fmt, ##__VA_ARGS__) +#define fls_printf(fmt, ...) PRINTF(fmt, ##__VA_ARGS__) #else #define fls_printf(fmt, ...) do {} while (0) #endif @@ -180,6 +186,11 @@ void settings_save(void) hst = HAL_FLASH_Lock(); assert_param(hst == HAL_OK); fls_printf("--- Flash done ---\r\n"); + +#if DEBUG_FLASH_WRITE + dbg("written @ %p", (void*)SETTINGS_FLASH_ADDR); + hexDump("Flash", (void*)SETTINGS_FLASH_ADDR, 64); +#endif } /** @@ -189,8 +200,8 @@ void settings_write_ini(IniWriter *iw) { // File header iw_comment(iw, "CONFIG.INI"); - iw_comment(iw, "Changes are applied on file save and can be immediately tested and verified."); - iw_comment(iw, "To persist to flash, replace the LOCK jumper before disconnecting from USB."); // TODO the jumper... + iw_comment(iw, "Overwrite this file to change settings."); + iw_comment(iw, "Close the LOCK jumper to save them to Flash."); systemsettings_write_ini(iw); iw_newline(iw); @@ -204,7 +215,7 @@ void settings_read_ini_begin(void) SystemSettings.modified = true; // load defaults - systemsettings_init(); + systemsettings_loadDefaults(); ureg_remove_all_units(); } diff --git a/framework/system_settings.c b/framework/system_settings.c index 9dc7771..9d4633f 100644 --- a/framework/system_settings.c +++ b/framework/system_settings.c @@ -9,11 +9,19 @@ struct system_settings SystemSettings; -void systemsettings_init(void) +/** Load defaults only */ +void systemsettings_loadDefaults(void) { SystemSettings.visible_vcom = true; - SystemSettings.modified = false; +} +/** Load defaults and init flags */ +void systemsettings_init(void) +{ + systemsettings_loadDefaults(); + + // Flags + SystemSettings.modified = false; LockJumper_ReadHardware(); } @@ -40,7 +48,7 @@ bool systemsettings_load(PayloadParser *pp) void systemsettings_write_ini(IniWriter *iw) { iw_section(iw, "SYSTEM"); - iw_comment(iw, "Expose the comm. channel as a virtual comport (Y, N)"); + iw_comment(iw, "Data link accessible as virtual comport (Y, N)"); iw_entry(iw, "expose_vcom", str_yn(SystemSettings.visible_vcom)); } diff --git a/framework/system_settings.h b/framework/system_settings.h index 144fe06..b2f9b51 100644 --- a/framework/system_settings.h +++ b/framework/system_settings.h @@ -21,10 +21,15 @@ struct system_settings { extern struct system_settings SystemSettings; /** - * Load defaults + * Init the store */ void systemsettings_init(void); +/** + * Load defaults + */ +void systemsettings_loadDefaults(void); + /** * Write system settings to the pack context */ diff --git a/framework/unit_registry.c b/framework/unit_registry.c index a77d6dc..5940c01 100644 --- a/framework/unit_registry.c +++ b/framework/unit_registry.c @@ -131,8 +131,6 @@ Unit *ureg_instantiate(const char *driver_name) { bool suc = true; - dbg("Creating unit of type %s", driver_name); - // Find type in the repository UregEntry *re = ureg_head; while (re != NULL) { @@ -157,7 +155,7 @@ Unit *ureg_instantiate(const char *driver_name) // in which case the data structure is not populated and keeping the // broken unit doesn't serve any purpose. Just ditch it... - dbg("!! Unit failed to pre-init!"); + dbg("!! Unit type %s failed to pre-init!", driver_name); clean_failed_unit(pUnit); free(le); return NULL; @@ -176,7 +174,7 @@ Unit *ureg_instantiate(const char *driver_name) // remove before init() void ureg_clean_failed(Unit *unit) { - dbg("Cleaning failed unit from registry"); + dbg("Removing failed unit from registry..."); UlistEntry *le; UlistEntry *parent; @@ -193,7 +191,7 @@ void ureg_clean_failed(Unit *unit) // remove after successful init() void ureg_remove_unit(Unit *unit) { - dbg("Cleaning & removing unit from registry"); + dbg("Cleaning & removing unit from registry..."); UlistEntry *le; UlistEntry *parent; @@ -239,6 +237,7 @@ bool ureg_load_units(PayloadParser *pp) if (pp_char(pp) != 'U') return false; uint16_t unit_count = pp_u16(pp); + dbg("Units to load: %d", (int)unit_count); for (uint32_t j = 0; j < unit_count; j++) { // We're now unpacking a single unit @@ -256,7 +255,7 @@ bool ureg_load_units(PayloadParser *pp) pUnit->name = strdup(typebuf); assert_param(pUnit->name); - // CALLSIGN + // callsign pUnit->callsign = pp_u8(pp); assert_param(pUnit->callsign != 0); @@ -264,16 +263,12 @@ bool ureg_load_units(PayloadParser *pp) pUnit->driver->cfgLoadBinary(pUnit, pp); assert_param(pp->ok); + dbg("Adding unit \"%s\" of type %s", pUnit->name, pUnit->driver->name); + suc = pUnit->driver->init(pUnit); // finalize the load and init the unit if (pUnit->status == E_LOADING) { pUnit->status = suc ? E_SUCCESS : E_BAD_CONFIG; } - - // XXX we want to keep the failed unit to preserve settings and for error reporting -// if (!suc) { -// // Discard, remove from registry -// ureg_clean_failed(unit); -// } } return pp->ok; @@ -284,6 +279,9 @@ void ureg_remove_all_units(void) { UlistEntry *le = ulist_head; UlistEntry *next; + + dbg("Removing all units"); + while (le != NULL) { next = le->next; @@ -349,7 +347,7 @@ bool ureg_read_unit_ini(const char *restrict name, if (streq(li->unit.name, name)) { Unit *const pUnit = &li->unit; - if (streq(key, "CALLSIGN")) { + if (streq(key, "callsign")) { // handled separately from unit data pUnit->callsign = (uint8_t) avr_atoi(value); return true; @@ -403,10 +401,13 @@ static void export_unit_do(UlistEntry *li, IniWriter *iw) Unit *const pUnit = &li->unit; iw_section(iw, "%s:%s", pUnit->driver->name, pUnit->name); - iw_comment(iw, ">> Status: %s", error_get_string(pUnit->status)); + if (pUnit->status != E_SUCCESS) { + iw_comment(iw, "!!! %s", error_get_string(pUnit->status)); + } iw_newline(iw); - iw_comment(iw, "Address for control messages (1-255)"); - iw_entry(iw, "CALLSIGN", "%d", pUnit->callsign); + + iw_comment(iw, "Unit address 1-255"); + iw_entry(iw, "callsign", "%d", pUnit->callsign); pUnit->driver->cfgWriteIni(pUnit, iw); iw_newline(iw); @@ -436,10 +437,8 @@ void ureg_export_combined(IniWriter *iw) // Unit list iw_section(iw, "UNITS"); - iw_comment(iw, "Here is a list of all unit types supported by the current firmware."); - iw_comment(iw, "To manage units, simply add/remove their comma-separated names next to"); - iw_comment(iw, "the desired unit type. Reload the file and the corresponding unit"); - iw_comment(iw, "sections should appear below, ready to configure."); + iw_comment(iw, "Create units by adding their names next to a type (e.g. PIN=A,B),"); + iw_comment(iw, "remove the same way. Reload to update the unit sections below."); // This could certainly be done in some more efficient way ... re = ureg_head; diff --git a/gex.mk b/gex.mk index 5201a12..a5df107 100644 --- a/gex.mk +++ b/gex.mk @@ -72,6 +72,7 @@ GEX_CDEFS = \ -DUSE_FULL_ASSERT=1 \ -DVERBOSE_ASSERT=1 \ -DDEBUG_VFS=0 \ + -DDEBUG_FLASH_WRITE=0 \ -DVERBOSE_HARDFAULT=1 \ -DUSE_STACK_MONITOR=1 \ -DUSE_DEBUG_UART=1 diff --git a/platform/lock_jumper.c b/platform/lock_jumper.c index 36b6927..42c12a2 100644 --- a/platform/lock_jumper.c +++ b/platform/lock_jumper.c @@ -52,10 +52,13 @@ static void jumper_changed(void) dbg("LOCK jumper removed, enabling MSC!"); } else { // Lock - dbg("LOCK jumper replaced, saving to Flash & disabling MSC!"); + dbg("LOCK jumper replaced, disabling MSC!"); if (SystemSettings.modified) { + dbg("Saving settings to Flash..."); settings_save(); + } else { + dbg("No changes to save."); } } diff --git a/platform/plat_compat.h b/platform/plat_compat.h index c47ce95..7462094 100644 --- a/platform/plat_compat.h +++ b/platform/plat_compat.h @@ -180,7 +180,7 @@ // size, determines position of the flash storage // we use the first 128kB sector. Unfortunately the whole sector must be erased before writing. - #define SETTINGS_FLASH_SECTOR 6 + #define SETTINGS_FLASH_SECTOR FLASH_SECTOR_5 #define SETTINGS_BLOCK_SIZE (1024*2) #define SETTINGS_FLASH_ADDR (0x08000000 + (16*4+64)*1024) diff --git a/tasks/task_main.c b/tasks/task_main.c index e9993bb..60b9e1a 100644 --- a/tasks/task_main.c +++ b/tasks/task_main.c @@ -48,7 +48,7 @@ void TaskMain(void const * argument) // Periodically check stacks for overrun stackmon_check_canaries(); // Periodically dump all stacks - for checking levels before critical (to reduce size if not needed) - if ((cnt%50)==0) stackmon_dump(); +// if ((cnt%50)==0) stackmon_dump(); continue; }