#include "stm32f4xx_hal.h" #include #include "led_blink.h" #include "interrupts.h" #include "receiver.h" #include "sw_fifo_1.h" #include "sw_fifo_2.h" #include "queue.h" // Externals extern TIM_HandleTypeDef htim3; // Internals void doTimingStuff(); int logData[LOG_SIZE]; int logCounter = 0; #define SAMPLES_TO_SWITCH 5 void ASKreceiver() { static unsigned char currValue = 0; static int samples = 0; static int totSamples = 0; if( receiverEnabled == 0 ) return; // Exit if the receiver's not been started // Sample the pin value unsigned char value = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0); // PIN PA0 on Nucleo board totSamples+=10; if( value != currValue ) { samples++; } else { samples--; } if( samples >= SAMPLES_TO_SWITCH ) { samples = 0; currValue = !currValue; logData[logCounter++] = totSamples; if( logCounter == LOG_SIZE ) { logCounter=0; } rcvEnQueue(totSamples); totSamples = 0; } if( samples < 0 ) samples = 0; } void ASKreceiver_old() { static unsigned char currValue = 0; static unsigned short samples = 0; static unsigned short newSamples = 0; if( receiverEnabled == 0 ) return; // Exit if the receiver's not been started // Sample the pin value unsigned char value = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0); // PIN PA0 on Nucleo board if( value == currValue ) { samples++; samples+=newSamples; newSamples=0; } else { newSamples++; } if( newSamples == 5 ) { logData[logCounter++] = samples * 10; if( logCounter == LOG_SIZE ) { logCounter=0; } rcvEnQueue(samples * 10); samples = newSamples; newSamples = 0; currValue = value; } } //=================================== TIM2 =================================== // This function is called every 10uS (clk/100/10) // This interrupt handles the sampling of the 433MHz receiver module. // Sends the filtered data to a receive queue. // This interrupt has the highest prio in the system //============================================================================ void handle_TIM2_interrupt() { doTimingStuff(); ASKreceiver(); } //=========================== doTimingStuff ================================== // This function handles updating of all time-keeping timers in the system. // Remember to turn of the sys clock and tim3 //============================================================================ unsigned long mSTick = 0; unsigned long long uSTickX10 = 0; #pragma optimize=no_cse void doTimingStuff() { static int mSCounter = 0; uSTickX10++; if( (++mSCounter) == 100 ) { mSCounter = 0; mSTick++; } } // Function returns number of uS since startup // Returns an unsigned long long unsigned long long micros() { return (uSTickX10*10); } #pragma optimize=no_cse void delayMicroseconds(unsigned long long delay) { unsigned long long endtime = micros() + delay; while( micros() < endtime); } extern unsigned long millis() { return mSTick; } #pragma optimize=no_cse extern void delay(unsigned long delay) { unsigned long endtime = mSTick + delay; while( mSTick < endtime); } //=================================== TIM3 =================================== // This function is called every 1mS (clk/100/1000) // This timer/interrupt is responsible for the time keeping in the // micron() function. //============================================================================ unsigned long tim3mSTick = 0; void handle_TIM3_interrupt() { tim3mSTick++; } /* // Function returns number of uS since startup // Returns an unsigned long long unsigned long long micros() { volatile static unsigned long long mS=0,uS=0; __HAL_TIM_DISABLE_IT(&htim3, TIM_IT_UPDATE); uS = TIM3->CNT; // 1uS clock tick mS = tim3mSTick; __HAL_TIM_ENABLE_IT(&htim3, TIM_IT_UPDATE); return ((mS * 1000ULL) + uS); } void delayMicroseconds(unsigned int delay) { volatile unsigned long long endtime = micros() + ((unsigned long long)delay); while( micros() < endtime); } */ //================================== SysTick ================================= // This function is called every 1mS //============================================================================ void handle_SysTick_interrupt() { const int LED_BLINK_START_VALUE = 200; static int i=200; if( (--i) == 0 ) { i= LED_BLINK_START_VALUE; led_interrupt(); } receiver(); } //================================== RTC ===================================== // This function is called every 1 minutes //============================================================================ int newMinuteEvent = 0; void handle_RTC_interrupt() { newMinuteEvent++; } //================================== USART1 ================================== //============================================================================ void handle_USART1_interrupt() { uart1Interrupt(); } //================================== USART2 ================================== //============================================================================ void handle_USART2_interrupt() { uart2Interrupt(); }