From b760f5fc9db0f4e1c9ffeceef9233a8041dd5169 Mon Sep 17 00:00:00 2001 From: Robert Fisk Date: Sun, 21 Aug 2016 09:37:11 +1200 Subject: [PATCH] Fix for occasional timeout on writes... ...Downstream was not always changing state correctly after closely spaced interrupts. Also improve flash-write-lockout function to avoid dependency on optimisation level. Conflicts: Upstream/Src/interrupts.c --- Downstream/Src/interrupts.c | 12 +++++++++--- Downstream/Src/main.c | 27 +++++++++++++++++---------- Upstream/Src/interrupts.c | 23 ++++++++++++++--------- Upstream/Src/main.c | 17 +++++++---------- 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/Downstream/Src/interrupts.c b/Downstream/Src/interrupts.c index 3d88c18..147ba29 100644 --- a/Downstream/Src/interrupts.c +++ b/Downstream/Src/interrupts.c @@ -46,6 +46,8 @@ extern HCD_HandleTypeDef hhcd_USB_OTG_FS; extern DMA_HandleTypeDef hdma_spi1_rx; extern DMA_HandleTypeDef hdma_spi1_tx; +extern volatile uint8_t UsbInterruptHasHappened; + uint8_t BusFaultAllowed = 0; @@ -83,9 +85,8 @@ void DMA2_Stream3_IRQHandler(void) void OTG_FS_IRQHandler(void) { - //INT_ACTIVE_ON; HAL_HCD_IRQHandler(&hhcd_USB_OTG_FS); - //INT_ACTIVE_OFF; + UsbInterruptHasHappened = 1; } @@ -93,6 +94,11 @@ void OTG_FS_IRQHandler(void) //The deliberate flash lockout will cause a bus fault that we need to process. void EnableOneBusFault(void) { + //It should not be enabled already! + if (BusFaultAllowed) + { + while (1); + } SCB->SHCSR = SCB_SHCSR_BUSFAULTENA_Msk; BusFaultAllowed = 1; } @@ -104,7 +110,7 @@ void BusFault_Handler(void) BusFaultAllowed = 0; return; } - while(1); + while (1); } diff --git a/Downstream/Src/main.c b/Downstream/Src/main.c index 3dbd865..2d6e9fa 100644 --- a/Downstream/Src/main.c +++ b/Downstream/Src/main.c @@ -44,16 +44,17 @@ /* Private function prototypes -----------------------------------------------*/ -void SystemClock_Config(void); +static void SystemClock_Config(void); static void GPIO_Init(void); -void DisableFlashWrites(void); -void CheckFirmwareMatchesHardware(void); +static void DisableFlashWrites(void); +static void CheckFirmwareMatchesHardware(void); + +volatile uint8_t UsbInterruptHasHappened = 0; +uint8_t IterationCount = 0; int main(void) { - uint32_t iterationCount = 0; - //First things first! DisableFlashWrites(); CheckFirmwareMatchesHardware(); @@ -77,12 +78,17 @@ int main(void) Downstream_SPIProcess(); Downstream_PacketProcessor_CheckNotifyDisconnectReply(); + //Count number of main loops since last USB interrupt + if (UsbInterruptHasHappened) + { + UsbInterruptHasHappened = 0; + IterationCount = 0; + } + //Some USB host state transitions take 3 iterations to fully apply. //We'll be generous and give it 5 before sleeping. - iterationCount++; - if (iterationCount > 4) + if (IterationCount++ > 4) { - iterationCount = 0; __WFI(); //sleep time! } } @@ -97,10 +103,11 @@ void DisableFlashWrites(void) FLASH->KEYR = 999; //Confirm that flash cannot be unlocked - //This unlock attempt will also cause a bus fault. - EnableOneBusFault(); + //This unlock attempt will also cause two bus faults. if ((FLASH->CR & FLASH_CR_LOCK) == 0) while(1); + EnableOneBusFault(); FLASH->KEYR = FLASH_KEY1; + EnableOneBusFault(); FLASH->KEYR = FLASH_KEY2; if ((FLASH->CR & FLASH_CR_LOCK) == 0) while(1); } diff --git a/Upstream/Src/interrupts.c b/Upstream/Src/interrupts.c index 3da4f86..ff902f0 100755 --- a/Upstream/Src/interrupts.c +++ b/Upstream/Src/interrupts.c @@ -75,17 +75,17 @@ void OTG_FS_IRQHandler(void) } void DMA2_Stream2_IRQHandler(void) -{ - INT_ACTIVE_ON; - HAL_DMA_IRQHandler(&spiRxDmaHandle); - INT_ACTIVE_OFF; +{ + INT_ACTIVE_ON; + HAL_DMA_IRQHandler(&spiRxDmaHandle); + INT_ACTIVE_OFF; } void DMA2_Stream3_IRQHandler(void) -{ - INT_ACTIVE_ON; - HAL_DMA_IRQHandler(&spiTxDmaHandle); - INT_ACTIVE_OFF; +{ + INT_ACTIVE_ON; + HAL_DMA_IRQHandler(&spiTxDmaHandle); + INT_ACTIVE_OFF; } void EXTI3_IRQHandler(void) @@ -101,6 +101,11 @@ void EXTI3_IRQHandler(void) //The deliberate flash lockout will cause a bus fault that we need to process. void EnableOneBusFault(void) { + //It should not be enabled already! + if (BusFaultAllowed) + { + while (1); + } SCB->SHCSR = SCB_SHCSR_BUSFAULTENA_Msk; BusFaultAllowed = 1; } @@ -112,7 +117,7 @@ void BusFault_Handler(void) BusFaultAllowed = 0; return; } - while(1); + while (1); } diff --git a/Upstream/Src/main.c b/Upstream/Src/main.c index 83444ee..ee48bab 100755 --- a/Upstream/Src/main.c +++ b/Upstream/Src/main.c @@ -44,15 +44,11 @@ #include "interrupts.h" -/* Private variables ---------------------------------------------------------*/ - - - /* Private function prototypes -----------------------------------------------*/ -void SystemClock_Config(void); -void GPIO_Init(void); -void DisableFlashWrites(void); -void CheckFirmwareMatchesHardware(void); +static void SystemClock_Config(void); +static void GPIO_Init(void); +static void DisableFlashWrites(void); +static void CheckFirmwareMatchesHardware(void); @@ -92,10 +88,11 @@ void DisableFlashWrites(void) FLASH->KEYR = 999; //Confirm that flash cannot be unlocked - //This unlock attempt will also cause a bus fault. - EnableOneBusFault(); + //This unlock attempt will also cause two bus faults. if ((FLASH->CR & FLASH_CR_LOCK) == 0) while(1); + EnableOneBusFault(); FLASH->KEYR = FLASH_KEY1; + EnableOneBusFault(); FLASH->KEYR = FLASH_KEY2; if ((FLASH->CR & FLASH_CR_LOCK) == 0) while(1); }