diff --git a/workspace/ts100/inc/Interrupt.h b/workspace/ts100/inc/Interrupt.h index 70dee58d..409799df 100644 --- a/workspace/ts100/inc/Interrupt.h +++ b/workspace/ts100/inc/Interrupt.h @@ -25,6 +25,10 @@ inline uint16_t getRawButtons() { return rawKeys; } +extern volatile uint8_t RotationChangedFlag; + + + /*IRQ prototypes*/ void NMI_Handler(void); void HardFault_Handler(void); diff --git a/workspace/ts100/src/Bios.c b/workspace/ts100/src/Bios.c index 0f6e576b..c82ce417 100644 --- a/workspace/ts100/src/Bios.c +++ b/workspace/ts100/src/Bios.c @@ -5,7 +5,6 @@ #include "Bios.h" #include "I2C.h" - #define ADC1_DR_Address ((u32)0x4001244C) volatile uint32_t gHeat_cnt = 0; @@ -92,8 +91,13 @@ void GPIO_Config(void) { GPIO_Init(GPIOA, &GPIO_InitStructure); //--------INT 1 == PB5 -------------------------------------------------// - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//pullup just in case something resets the accel + GPIO_InitStructure.GPIO_Pin = INT1_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //pullup just in case something resets the accel + GPIO_Init(GPIOB, &GPIO_InitStructure); + + //--------INT 2 == PB3 -------------------------------------------------// + GPIO_InitStructure.GPIO_Pin = INT2_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //pullup just in case something resets the accel GPIO_Init(GPIOB, &GPIO_InitStructure); } @@ -211,7 +215,8 @@ void Init_EXTI(void) { GPIO_PinSource5); //PB5 == accelerometer /* Configure EXTI5/6/9 line */ - EXTI_InitStructure.EXTI_Line = EXTI_Line5 | EXTI_Line6 | EXTI_Line9; + EXTI_InitStructure.EXTI_Line = EXTI_Line5 | EXTI_Line6 | EXTI_Line9 + | EXTI_Line3; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //trigger on up and down EXTI_InitStructure.EXTI_LineCmd = ENABLE; @@ -224,6 +229,13 @@ void Init_EXTI(void) { NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); + /* Enable and set EXTI9_5 Interrupt to the lowest priority */ + NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); + } //Start the system watchdog with a timeout specified //Note you cannot turn this off once you turn it on diff --git a/workspace/ts100/src/Interrupt.c b/workspace/ts100/src/Interrupt.c index e8b1dfd6..772ffb98 100644 --- a/workspace/ts100/src/Interrupt.c +++ b/workspace/ts100/src/Interrupt.c @@ -8,6 +8,7 @@ volatile uint32_t AkeyChange; volatile uint8_t rawKeys; volatile uint8_t LongKeys; volatile uint32_t lastMovement; //millis() at last movement event +volatile uint8_t RotationChangedFlag; //Delay in milliseconds using systemTick void delayMs(uint32_t ticks) { @@ -161,6 +162,12 @@ void EXTI9_5_IRQHandler(void) { } } +void EXTI3_IRQHandler(void) { + if (EXTI_GetITStatus(EXTI_Line3) != RESET) { //Orientation Change + RotationChangedFlag = 1; + EXTI_ClearITPendingBit(EXTI_Line3); + } +} /*********************** UNUSED IRQ *****************************************/ void WWDG_IRQHandler(void) { @@ -181,8 +188,7 @@ void EXTI1_IRQHandler(void) { } void EXTI2_IRQHandler(void) { } -void EXTI3_IRQHandler(void) { -} + void EXTI4_IRQHandler(void) { } void DMA1_Channel1_IRQHandler(void) { diff --git a/workspace/ts100/src/MMA8652FC.c b/workspace/ts100/src/MMA8652FC.c index 26d300cb..267143ee 100644 --- a/workspace/ts100/src/MMA8652FC.c +++ b/workspace/ts100/src/MMA8652FC.c @@ -44,7 +44,7 @@ void StartUp_Accelerometer(uint8_t sensitivity) { I2C_RegisterWrite( CTRL_REG2, 0x40); // Reset all registers to POR values delayMs(2); // ~1ms delay I2C_RegisterWrite(FF_MT_CFG_REG, 0x78); // Enable motion detection for X and Y axis, latch enabled - uint8_t sens = 9 * 6 + 5; + uint8_t sens = 9 * 6 + 3; sens -= 6 * sensitivity; I2C_RegisterWrite(FF_MT_THS_REG, 0x80 | sens); // Set threshold @@ -52,9 +52,9 @@ void StartUp_Accelerometer(uint8_t sensitivity) { I2C_RegisterWrite(PL_CFG_REG, 0x40); //Enable the orientation detection I2C_RegisterWrite(PL_COUNT_REG, 200); //200 count debounce I2C_RegisterWrite(PL_BF_ZCOMP_REG, 0b01000111); //Set the threshold to 42 degrees - I2C_RegisterWrite(P_L_THS_REG, 0b10011100);//Up the trip angles - I2C_RegisterWrite( CTRL_REG4, 0x04); // Enable motion interrupt - I2C_RegisterWrite( CTRL_REG5, 0x04);// Route motion interrupts to INT1 ->PB5 ->EXTI5 + I2C_RegisterWrite(P_L_THS_REG, 0b10011100); //Up the trip angles + I2C_RegisterWrite( CTRL_REG4, 0x04 | (1<<4)); // Enable motion interrupt & orientation interrupt + I2C_RegisterWrite( CTRL_REG5, 0x04);// Route motion interrupts to INT1 ->PB5 ->EXTI5, leaving orientation routed to INT2 I2C_RegisterWrite( CTRL_REG1, 0x11); // ODR=800 Hz, Active mode } diff --git a/workspace/ts100/src/Main.c b/workspace/ts100/src/Main.c index a1867e81..c81883a4 100644 --- a/workspace/ts100/src/Main.c +++ b/workspace/ts100/src/Main.c @@ -14,17 +14,33 @@ void setup(); int main(void) { setup();/*Setup the system*/ - if(systemSettings.autoStart) + if (systemSettings.autoStart) operatingMode = SOLDERING; while (1) { Clear_Watchdog(); //reset the Watch dog timer ProcessUI(); DrawUI(); - delayMs(30); //Slow the system down a little bit + delayMs(15); //Slow the system down waiting for the iron. + + if (systemSettings.OrientationMode == 2) { + //Automatic mode + if (RotationChangedFlag) { + OLED_SetOrientation(!getOrientation()); + RotationChangedFlag = 0; + } + + if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_3) == Bit_RESET) { + OLED_SetOrientation(!getOrientation()); + RotationChangedFlag = 0; + //^ This is a workaround for the IRQ being set off before we have the handler setup and enabled. + } + } if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == Bit_RESET) { lastMovement = millis(); //This is a workaround for the line staying low as the user is still moving. (ie sensitivity is too high for their amount of movement) } + delayMs(15); //Slow the system down waiting for the iron. + } } void setup() { @@ -41,7 +57,7 @@ void setup() { setupPID(); //Init the PID values readIronTemp(systemSettings.tempCalibration, 0, 0); //load the default calibration value if (systemSettings.OrientationMode == 2) - Init_Oled(0); //Init the OLED display in RH mode, since accel wont have started up yet + Init_Oled(0); //Init the OLED display in RH mode, since accel wont have started up yet else Init_Oled(systemSettings.OrientationMode); //Init the OLED display diff --git a/workspace/ts100/src/Modes.c b/workspace/ts100/src/Modes.c index 6e945f87..841444d4 100644 --- a/workspace/ts100/src/Modes.c +++ b/workspace/ts100/src/Modes.c @@ -414,10 +414,7 @@ void DrawUI() { static uint16_t lastSolderingDrawnTemp2 = 0; static uint8_t settingsLongTestScrollPos = 0; uint16_t temp = readIronTemp(0, 0, 0xFFFF); - if (systemSettings.OrientationMode == 2) { - //Automatic mode - OLED_SetOrientation(!getOrientation()); - } + switch (operatingMode) { case STARTUP: //We are chilling in the idle mode