diff --git a/Downstream/Inc/board_config.h b/Downstream/Inc/board_config.h index f34e573..6b45bd5 100644 --- a/Downstream/Inc/board_config.h +++ b/Downstream/Inc/board_config.h @@ -35,20 +35,18 @@ #define USB_HS_VBUSON_PIN GPIO_PIN_8 #define USB_HS_VBUSON_PORT GPIOA -#define STAT_LED_PIN GPIO_PIN_13 -#define STAT_LED_PORT GPIOC -#define STAT_LED_ON (STAT_LED_PORT->BSRR = (STAT_LED_PIN << BSRR_SHIFT_LOW)) //Stat LED is active-low -#define STAT_LED_OFF (STAT_LED_PORT->BSRR = (STAT_LED_PIN << BSRR_SHIFT_HIGH)) +#define FAULT_LED_PIN GPIO_PIN_13 +#define FAULT_LED_PORT GPIOC +#define FAULT_LED_ON (FAULT_LED_PORT->BSRR = (FAULT_LED_PIN << BSRR_SHIFT_LOW)) //Fault LED is active-low +#define FAULT_LED_OFF (FAULT_LED_PORT->BSRR = (FAULT_LED_PIN << BSRR_SHIFT_HIGH)) -//#define RUN_LED_ON...... -#define FAULT_LED_ON STAT_LED_ON -#define FAULT_LED_OFF STAT_LED_OFF +#define H405_FAULT_LED_PIN GPIO_PIN_12 //Fault LED on Olimex H405 board +#define H405_FAULT_LED_ON (FAULT_LED_PORT->BSRR = (H405_FAULT_LED_PIN << BSRR_SHIFT_LOW)) - -#define SPI_INT_ACTIVE_PIN GPIO_PIN_2 /////////Temporary indicator of SPI activity -#define SPI_INT_ACTIVE_PORT GPIOA -#define SPI_INT_ACTIVE_ON SPI_INT_ACTIVE_PORT->BSRR = (SPI_INT_ACTIVE_PIN << BSRR_SHIFT_HIGH) -#define SPI_INT_ACTIVE_OFF SPI_INT_ACTIVE_PORT->BSRR = (SPI_INT_ACTIVE_PIN << BSRR_SHIFT_LOW) +#define INT_ACTIVE_PIN GPIO_PIN_2 //Temporary indicator of SPI (or whatever) activity +#define INT_ACTIVE_PORT GPIOA +#define INT_ACTIVE_ON INT_ACTIVE_PORT->BSRR = (INT_ACTIVE_PIN << BSRR_SHIFT_HIGH) +#define INT_ACTIVE_OFF INT_ACTIVE_PORT->BSRR = (INT_ACTIVE_PIN << BSRR_SHIFT_LOW) //#define SPI1_NSS_PIN GPIO_PIN_4 //#define SPI1_NSS_PORT GPIOA @@ -58,5 +56,7 @@ #define UPSTREAM_TX_REQUEST_ASSERT (UPSTREAM_TX_REQUEST_PORT->BSRR = (UPSTREAM_TX_REQUEST_PIN << BSRR_SHIFT_LOW)) #define UPSTREAM_TX_REQUEST_DEASSERT (UPSTREAM_TX_REQUEST_PORT->BSRR = (UPSTREAM_TX_REQUEST_PIN << BSRR_SHIFT_HIGH)) +#define DBGMCU_IDCODE_DEV_ID_405_407_415_417 0x413 + #endif /* INC_BOARD_CONFIG_H_ */ diff --git a/Downstream/Src/interrupts.c b/Downstream/Src/interrupts.c index 15e0f01..bc0cebe 100644 --- a/Downstream/Src/interrupts.c +++ b/Downstream/Src/interrupts.c @@ -84,9 +84,9 @@ void DMA2_Stream3_IRQHandler(void) void OTG_FS_IRQHandler(void) { - SPI_INT_ACTIVE_ON; + INT_ACTIVE_ON; HAL_HCD_IRQHandler(&hhcd_USB_OTG_FS); - SPI_INT_ACTIVE_OFF; + INT_ACTIVE_OFF; } diff --git a/Downstream/Src/main.c b/Downstream/Src/main.c index cedded5..b480514 100644 --- a/Downstream/Src/main.c +++ b/Downstream/Src/main.c @@ -45,11 +45,16 @@ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void GPIO_Init(void); +void CheckFirmwareMatchesHardware(void); int main(void) { + //First things first! + CheckFirmwareMatchesHardware(); + + /* Configure the system clock */ SystemClock_Config(); @@ -71,6 +76,45 @@ int main(void) } +void CheckFirmwareMatchesHardware(void) +{ + //Check we are running on the expected hardware: + //STM32F407 on an Olimex dev board + + GPIO_InitTypeDef GPIO_InitStruct; + + if ((*(uint32_t*)DBGMCU_BASE & DBGMCU_IDCODE_DEV_ID) == DBGMCU_IDCODE_DEV_ID_405_407_415_417) + { + //The H407 board has a STAT LED on PC13. If there is no pullup on this pin, + //then we are probably running on another board. + __HAL_RCC_GPIOC_CLK_ENABLE(); + GPIO_InitStruct.Pin = FAULT_LED_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_LOW; + GPIO_InitStruct.Alternate = 0; + HAL_GPIO_Init(FAULT_LED_PORT, &GPIO_InitStruct); + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(FAULT_LED_PORT, &GPIO_InitStruct); + + if (FAULT_LED_PORT->IDR & FAULT_LED_PIN) + { + //Pin pulls up, so this is an H407 board :) + return; + } + } + + //This is not the hardware we expected, so turn on our fault LED(s) and die in a heap. + GPIO_InitStruct.Pin = FAULT_LED_PIN | H405_FAULT_LED_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(FAULT_LED_PORT, &GPIO_InitStruct); + FAULT_LED_ON; + H405_FAULT_LED_ON; + while (1); +} + + /** System Clock Configuration */ void SystemClock_Config(void) @@ -158,14 +202,14 @@ void GPIO_Init(void) HAL_GPIO_Init(USB_HS_VBUSON_PORT, &GPIO_InitStruct); //STAT_LED is output - STAT_LED_OFF; - GPIO_InitStruct.Pin = STAT_LED_PIN; - HAL_GPIO_Init(STAT_LED_PORT, &GPIO_InitStruct); + FAULT_LED_OFF; + GPIO_InitStruct.Pin = FAULT_LED_PIN; + HAL_GPIO_Init(FAULT_LED_PORT, &GPIO_InitStruct); //SPI_INT_ACTIVE indicator - GPIO_InitStruct.Pin = SPI_INT_ACTIVE_PIN; - HAL_GPIO_Init(SPI_INT_ACTIVE_PORT, &GPIO_InitStruct); - SPI_INT_ACTIVE_OFF; + GPIO_InitStruct.Pin = INT_ACTIVE_PIN; + HAL_GPIO_Init(INT_ACTIVE_PORT, &GPIO_InitStruct); + INT_ACTIVE_OFF; } diff --git a/Upstream/Inc/board_config.h b/Upstream/Inc/board_config.h index e576c57..e019ad7 100644 --- a/Upstream/Inc/board_config.h +++ b/Upstream/Inc/board_config.h @@ -25,16 +25,15 @@ #define USB_P_PIN GPIO_PIN_4 #define USB_P_PORT GPIOC -#define STAT_LED_PIN GPIO_PIN_12 -#define STAT_LED_PORT GPIOC -#define STAT_LED_ON (STAT_LED_PORT->BSRR = (STAT_LED_PIN << BSRR_SHIFT_LOW)) //Stat LED is active-low -#define STAT_LED_OFF (STAT_LED_PORT->BSRR = (STAT_LED_PIN << BSRR_SHIFT_HIGH)) +#define FAULT_LED_PIN GPIO_PIN_12 +#define FAULT_LED_PORT GPIOC +#define FAULT_LED_ON (FAULT_LED_PORT->BSRR = (FAULT_LED_PIN << BSRR_SHIFT_LOW)) //Fault LED is active-low +#define FAULT_LED_OFF (FAULT_LED_PORT->BSRR = (FAULT_LED_PIN << BSRR_SHIFT_HIGH)) -//#define RUN_LED_ON...... -#define FAULT_LED_ON STAT_LED_ON -#define FAULT_LED_OFF STAT_LED_OFF +#define OTHER_BOARDS_FAULT_LED_PIN GPIO_PIN_13 //Fault LED on Olimex H407 board, and USG v1.0 +#define OTHER_BOARDS_FAULT_LED_ON (FAULT_LED_PORT->BSRR = (OTHER_BOARDS_FAULT_LED_PIN << BSRR_SHIFT_LOW)) -#define INT_ACTIVE_PIN GPIO_PIN_5 /////////Temporary indicator of SPI activity +#define INT_ACTIVE_PIN GPIO_PIN_5 //Temporary indicator of SPI (or whatever) activity #define INT_ACTIVE_PORT GPIOB #define INT_ACTIVE_ON INT_ACTIVE_PORT->BSRR = (INT_ACTIVE_PIN << BSRR_SHIFT_HIGH) #define INT_ACTIVE_OFF INT_ACTIVE_PORT->BSRR = (INT_ACTIVE_PIN << BSRR_SHIFT_LOW) @@ -48,5 +47,7 @@ #define DOWNSTREAM_TX_OK_PORT GPIOA #define DOWNSTREAM_TX_OK_ACTIVE (!(DOWNSTREAM_TX_OK_PORT->IDR & DOWNSTREAM_TX_OK_PIN)) +#define DBGMCU_IDCODE_DEV_ID_405_407_415_417 0x413 + #endif /* INC_BOARD_CONFIG_H_ */ diff --git a/Upstream/Src/main.c b/Upstream/Src/main.c index 5342664..4c9ef98 100755 --- a/Upstream/Src/main.c +++ b/Upstream/Src/main.c @@ -50,32 +50,77 @@ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void GPIO_Init(void); +void CheckFirmwareMatchesHardware(void); int main(void) { - /* Configure the system clock */ - SystemClock_Config(); + //First things first! + CheckFirmwareMatchesHardware(); - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ - HAL_Init(); - /* Initialize all configured peripherals */ - GPIO_Init(); - LED_Init(); - USB_Device_Init(); + /* Configure the system clock */ + SystemClock_Config(); - Upstream_InitStateMachine(); + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + HAL_Init(); + /* Initialize all configured peripherals */ + GPIO_Init(); + LED_Init(); + USB_Device_Init(); - while (1) - { - - } + Upstream_InitStateMachine(); + + + while (1) + { + + } } +void CheckFirmwareMatchesHardware(void) +{ + //Check we are running on the expected hardware: + //STM32F405 on an Olimex dev board + + GPIO_InitTypeDef GPIO_InitStruct; + + if ((*(uint32_t*)DBGMCU_BASE & DBGMCU_IDCODE_DEV_ID) == DBGMCU_IDCODE_DEV_ID_405_407_415_417) + { + //The H405 board has a STAT LED on PC12. If there is no pullup on this pin, + //then we are probably running on another board. + __HAL_RCC_GPIOC_CLK_ENABLE(); + GPIO_InitStruct.Pin = FAULT_LED_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLDOWN; + GPIO_InitStruct.Speed = GPIO_SPEED_LOW; + GPIO_InitStruct.Alternate = 0; + HAL_GPIO_Init(FAULT_LED_PORT, &GPIO_InitStruct); + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(FAULT_LED_PORT, &GPIO_InitStruct); + + if (FAULT_LED_PORT->IDR & FAULT_LED_PIN) + { + //Pin pulls up, so this is an H405 board :) + return; + } + } + + //This is not the hardware we expected, so turn on our fault LED(s) and die in a heap. + GPIO_InitStruct.Pin = FAULT_LED_PIN | OTHER_BOARDS_FAULT_LED_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(FAULT_LED_PORT, &GPIO_InitStruct); + FAULT_LED_ON; + OTHER_BOARDS_FAULT_LED_ON; + while (1); +} + + + /** System Clock Configuration */ void SystemClock_Config(void) @@ -149,11 +194,11 @@ void GPIO_Init(void) HAL_GPIO_Init(USB_P_PORT, &GPIO_InitStruct); //STAT_LED is output - GPIO_InitStruct.Pin = STAT_LED_PIN; + GPIO_InitStruct.Pin = FAULT_LED_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(STAT_LED_PORT, &GPIO_InitStruct); - STAT_LED_OFF; + HAL_GPIO_Init(FAULT_LED_PORT, &GPIO_InitStruct); + FAULT_LED_OFF; //SPI_INT_ACTIVE indicator GPIO_InitStruct.Pin = INT_ACTIVE_PIN;