Skip to content

Commit

Permalink
Merge pull request #1190 from spark/feature/setup-button-mirror
Browse files Browse the repository at this point in the history
MODE/SETUP button mirroring
  • Loading branch information
technobly authored Dec 9, 2016
2 parents f475f5b + 4d1e3aa commit c887198
Show file tree
Hide file tree
Showing 33 changed files with 717 additions and 112 deletions.
2 changes: 1 addition & 1 deletion bootloader/import.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BOOTLOADER_MODULE_PATH ?= $(PROJECT_ROOT)/bootloader
BOOTLOADER_VERSION ?= 8
BOOTLOADER_VERSION ?= 9
BOOTLOADER_BUILD_PATH_EXT = $(BUILD_TARGET_PLATFORM)

# bring in the include folders from inc and src/<platform> is includes
Expand Down
16 changes: 16 additions & 0 deletions bootloader/src/core/button.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "button.h"

extern __IO uint16_t BUTTON_DEBOUNCED_TIME[];

void BUTTON_Init_Ext() {
if (BUTTON_Is_Pressed(BUTTON1))
TIM_ITConfig(TIM1, TIM_IT_CC4, ENABLE);
}

uint8_t BUTTON_Is_Pressed(Button_TypeDef button) {
return BUTTON_GetState(BUTTON1) == BUTTON1_PRESSED;
}

uint16_t BUTTON_Pressed_Time(Button_TypeDef button) {
return BUTTON_DEBOUNCED_TIME[BUTTON1];
}
18 changes: 18 additions & 0 deletions bootloader/src/core/button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef __BUTTON_H
#define __BUTTON_H

#ifdef __cplusplus
extern "C" {
#endif

#include "hw_config.h"

void BUTTON_Init_Ext();
uint8_t BUTTON_Is_Pressed(Button_TypeDef button);
uint16_t BUTTON_Pressed_Time(Button_TypeDef button);

#ifdef __cplusplus
}
#endif

#endif /* __BUTTON_H */
14 changes: 8 additions & 6 deletions bootloader/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "dfu_hal.h"
#include "hw_config.h"
#include "rgbled.h"
#include "button.h"

void platform_startup();

Expand Down Expand Up @@ -110,6 +111,7 @@ int main(void)
// Configure the MODE button
//--------------------------------------------------------------------------
Set_System();
BUTTON_Init_Ext();

//--------------------------------------------------------------------------

Expand Down Expand Up @@ -261,7 +263,7 @@ int main(void)
//--------------------------------------------------------------------------
// Check if BUTTON1 is pressed and determine the status
//--------------------------------------------------------------------------
if (BUTTON_GetState(BUTTON1) == BUTTON1_PRESSED && (features & BL_BUTTON_FEATURES))
if (BUTTON_Is_Pressed(BUTTON1) && (features & BL_BUTTON_FEATURES))
{
#define TIMING_SAFE_MODE 1000
#define TIMING_DFU_MODE 3000
Expand All @@ -273,23 +275,23 @@ int main(void)

TimingBUTTON = TIMING_ALL;
uint8_t factory_reset = 0;
while (BUTTON_GetState(BUTTON1) == BUTTON1_PRESSED && TimingBUTTON)
while (BUTTON_Is_Pressed(BUTTON1) && TimingBUTTON)
{
if(TimingBUTTON < (TIMING_ALL-TIMING_RESET_MODE))
if(BUTTON_Pressed_Time(BUTTON1) > TIMING_RESET_MODE)
{
// if pressed for 10 sec, enter Factory Reset Mode
// This tells the WLAN setup to clear the WiFi user profiles on bootup
LED_SetRGBColor(RGB_COLOR_WHITE);
SYSTEM_FLAG(NVMEM_SPARK_Reset_SysFlag) = 0x0001;
}
else if(!factory_reset && TimingBUTTON <= (TIMING_ALL-TIMING_RESTORE_MODE))
else if(!factory_reset && BUTTON_Pressed_Time(BUTTON1) > TIMING_RESTORE_MODE)
{
// if pressed for > 6.5 sec, enter firmware reset
LED_SetRGBColor(RGB_COLOR_GREEN);
SYSTEM_FLAG(NVMEM_SPARK_Reset_SysFlag) = 0x0000;
factory_reset = 1;
}
else if(!USB_DFU_MODE && TimingBUTTON <= (TIMING_ALL-TIMING_DFU_MODE))
else if(!USB_DFU_MODE && BUTTON_Pressed_Time(BUTTON1) >= TIMING_DFU_MODE)
{
// if pressed for > 3 sec, enter USB DFU Mode
if (features&BL_FEATURE_DFU_MODE) {
Expand All @@ -299,7 +301,7 @@ int main(void)
if (!factory_reset_available)
break;
}
else if(!SAFE_MODE && TimingBUTTON <= TIMING_ALL-TIMING_SAFE_MODE)
else if(!SAFE_MODE && BUTTON_Pressed_Time(BUTTON1) >= TIMING_SAFE_MODE)
{
OTA_FLASH_AVAILABLE = 0;
REFLASH_FROM_BACKUP = 0;
Expand Down
102 changes: 102 additions & 0 deletions bootloader/src/stm32f2xx/button.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "button.h"
#include "dct.h"
#include "hal_irq_flag.h"
#include <string.h>

/**
* @brief This function handles BUTTON EXTI Handler.
* @param None
* @retval None
*/
void BUTTON_Irq_Handler(uint16_t exti)
{
if (EXTI_GetITStatus(exti) != RESET)
{
/* Clear the EXTI line pending bit */
EXTI_ClearITPendingBit(exti);

BUTTON_Check_Irq(BUTTON1, exti);
BUTTON_Check_Irq(BUTTON1_MIRROR, exti);
}
}

void BUTTON_Check_Irq(uint16_t button, uint16_t exti) {
if (HAL_Buttons[button].exti_line == exti)
{
HAL_Buttons[button].debounce_time = 0x00;
HAL_Buttons[button].active = 1;

/* Disable button Interrupt */
BUTTON_EXTI_Config(button, DISABLE);

/* Enable TIM2 CC1 Interrupt */
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
}
}

void BUTTON_Check_State(uint16_t button, uint8_t pressed) {
if (HAL_Buttons[button].exti_line && BUTTON_GetState(button) == pressed)
{
if (!HAL_Buttons[button].active)
HAL_Buttons[button].active = 1;
HAL_Buttons[button].debounce_time += BUTTON_DEBOUNCE_INTERVAL;
}
else if (HAL_Buttons[button].active)
{
HAL_Buttons[button].active = 0;
/* Enable button Interrupt */
BUTTON_EXTI_Config(button, ENABLE);
}
}

int BUTTON_Debounce() {
BUTTON_Check_State(BUTTON1, BUTTON1_PRESSED);
BUTTON_Check_State(BUTTON1_MIRROR, HAL_Buttons[BUTTON1_MIRROR].exti_trigger == EXTI_Trigger_Rising ? 1 : 0);

int pressed = HAL_Buttons[BUTTON1].active + HAL_Buttons[BUTTON1_MIRROR].active;
if (pressed == 0) {
/* Disable TIM2 CC1 Interrupt */
TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE);
}

return pressed;
}

void BUTTON_Init_Ext() {
const button_config_t* conf = (const button_config_t*)dct_read_app_data(DCT_MODE_BUTTON_MIRROR_OFFSET);

if (conf->active == 0xAA && conf->debounce_time == 0xBBCC) {
int32_t state = HAL_disable_irq();
memcpy((void*)&HAL_Buttons[BUTTON1_MIRROR], (void*)conf, sizeof(button_config_t));
HAL_Buttons[BUTTON1_MIRROR].active = 0;
HAL_Buttons[BUTTON1_MIRROR].debounce_time = 0;
BUTTON_Init(BUTTON1_MIRROR, BUTTON_MODE_EXTI);
HAL_enable_irq(state);
}

if (BUTTON_Debounce())
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
}

uint8_t BUTTON_Is_Pressed(Button_TypeDef button) {
uint8_t pressed = 0;
pressed = HAL_Buttons[button].active;

if (button == BUTTON1 && HAL_Buttons[BUTTON1_MIRROR].exti_line) {
pressed |= BUTTON_Is_Pressed(BUTTON1_MIRROR);
}

return pressed;
}

uint16_t BUTTON_Pressed_Time(Button_TypeDef button) {
uint16_t pressed = 0;

pressed = HAL_Buttons[button].debounce_time;
if (button == BUTTON1 && HAL_Buttons[BUTTON1_MIRROR].exti_line) {
if (BUTTON_Pressed_Time(BUTTON1_MIRROR) > pressed)
pressed = BUTTON_Pressed_Time(BUTTON1_MIRROR);
}

return pressed;
}
24 changes: 24 additions & 0 deletions bootloader/src/stm32f2xx/button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef __BUTTON_H
#define __BUTTON_H

#ifdef __cplusplus
extern "C" {
#endif

#include "hw_config.h"

void BUTTON_Init_Ext();
uint8_t BUTTON_Is_Pressed(Button_TypeDef button);
uint16_t BUTTON_Pressed_Time(Button_TypeDef button);


void BUTTON_Irq_Handler(uint16_t exti);
void BUTTON_Check_Irq(uint16_t button, uint16_t exti);
void BUTTON_Check_State(uint16_t button, uint8_t pressed);
int BUTTON_Debounce();

#ifdef __cplusplus
}
#endif

#endif /* __BUTTON_H */
1 change: 1 addition & 0 deletions bootloader/src/stm32f2xx/hal_irq_flag.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../src/stm32f2xx/hal_irq_flag.c"
130 changes: 95 additions & 35 deletions bootloader/src/stm32f2xx/stm32_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "usb_conf.h"
#include "usbd_dfu_core.h"
#include "hw_config.h"
#include "button.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
Expand All @@ -41,7 +42,6 @@
/* Private function prototypes -----------------------------------------------*/

/* Extern variables and function prototypes ----------------------------------*/
extern __IO uint16_t BUTTON_DEBOUNCED_TIME[];
extern void Timing_Decrement(void);

extern USB_OTG_CORE_HANDLE USB_OTG_dev;
Expand Down Expand Up @@ -152,28 +152,6 @@ void SysTick_Handler(void)
Timing_Decrement();
}

/**
* @brief This function handles BUTTON EXTI Handler.
* @param None
* @retval None
*/
void BUTTON1_EXTI_IRQ_HANDLER(void)
{
if (EXTI_GetITStatus(BUTTON1_EXTI_LINE) != RESET)
{
/* Clear the EXTI line pending bit */
EXTI_ClearITPendingBit(BUTTON1_EXTI_LINE);

BUTTON_DEBOUNCED_TIME[BUTTON1] = 0x00;

/* Disable BUTTON1 Interrupt */
BUTTON_EXTI_Config(BUTTON1, DISABLE);

/* Enable TIM2 CC1 Interrupt */
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
}
}

/**
* @brief This function handles TIM2 Handler.
* @param None
Expand All @@ -185,18 +163,7 @@ void TIM2_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

if (BUTTON_GetState(BUTTON1) == BUTTON1_PRESSED)
{
BUTTON_DEBOUNCED_TIME[BUTTON1] += BUTTON_DEBOUNCE_INTERVAL;
}
else
{
/* Disable TIM2 CC1 Interrupt */
TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE);

/* Enable BUTTON1 Interrupt */
BUTTON_EXTI_Config(BUTTON1, ENABLE);
}
BUTTON_Debounce();
}
}

Expand Down Expand Up @@ -271,3 +238,96 @@ void OTG_HS_IRQHandler(void)
/*void PPP_IRQHandler(void)
{
}*/

/*******************************************************************************
* Function Name : EXTI0_IRQHandler
* Description : This function handles EXTI0 interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI0_IRQHandler(void)
{
BUTTON_Irq_Handler(EXTI_Line0);
}

/*******************************************************************************
* Function Name : EXTI1_IRQHandler
* Description : This function handles EXTI1 interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI1_IRQHandler(void)
{
BUTTON_Irq_Handler(EXTI_Line1);
}

/*******************************************************************************
* Function Name : EXTI2_IRQHandler
* Description : This function handles EXTI2 interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI2_IRQHandler(void)
{
BUTTON_Irq_Handler(EXTI_Line2);
}

/*******************************************************************************
* Function Name : EXTI3_IRQHandler
* Description : This function handles EXTI3 interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI3_IRQHandler(void)
{
BUTTON_Irq_Handler(EXTI_Line3);
}

/*******************************************************************************
* Function Name : EXTI4_IRQHandler
* Description : This function handles EXTI4 interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI4_IRQHandler(void)
{
BUTTON_Irq_Handler(EXTI_Line4);
}

/*******************************************************************************
* Function Name : EXTI9_5_IRQHandler
* Description : This function handles EXTI9_5 interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI9_5_IRQHandler(void)
{
BUTTON_Irq_Handler(EXTI_Line5);
BUTTON_Irq_Handler(EXTI_Line6);
BUTTON_Irq_Handler(EXTI_Line7);
BUTTON_Irq_Handler(EXTI_Line8);
BUTTON_Irq_Handler(EXTI_Line9);
}

/*******************************************************************************
* Function Name : EXTI15_10_IRQHandler
* Description : This function handles EXTI15_10 interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI15_10_IRQHandler(void)
{
BUTTON_Irq_Handler(EXTI_Line10);
BUTTON_Irq_Handler(EXTI_Line11);
BUTTON_Irq_Handler(EXTI_Line12);
BUTTON_Irq_Handler(EXTI_Line13);
BUTTON_Irq_Handler(EXTI_Line14);
BUTTON_Irq_Handler(EXTI_Line15);
}
Loading

0 comments on commit c887198

Please sign in to comment.