#include "main.h" #include "delay.h" #include "deviceSetup.h" #include "usart.h" #include "ascii.h" #include "timer.h" #include "i2c.h" #include "pin.h" #include "stm32f042x6.h" #include "hardwareDescription.h" volatile uint8_t led_on; void setup(); #if 0 void EXTI0_1_IRQHandler(void) { if(EXTI->PR & EXTI_PR_PIF1) { // clear flag EXTI->PR |= EXTI_PR_PIF1; led_on = !led_on; } } void risingEdgeInterruptPinA1() { // https://controllerstech.com/external-interrupt-using-registers/ pinConfig(pinA1, input, def_stage, pullDown, def_speed); // Connect RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; SYSCFG->EXTICR[1] &= ~SYSCFG_EXTICR1_EXTI1_Msk; SYSCFG->EXTICR[1] |= SYSCFG_EXTICR1_EXTI1_PA; // Interrupt mask register EXTI->IMR |= EXTI_IMR_IM1; // rising trigger selection EXTI->RTSR |= EXTI_RTSR_RT1; NVIC_SetPriority (EXTI0_1_IRQn, 0); NVIC_EnableIRQ(EXTI0_1_IRQn); while(1) { if(led_on){ pinWrite(pinB3,1); } else{ pinWrite(pinB3,0); } } } #endif void TIM2_IRQHandler(){ /* Check what type of event occurred */ if (TIM2->SR & TIM_SR_UIF) { /* Clear the interrupt even flag. CPU will only respond to new flags thereafter */ TIM2->SR &= ~TIM_SR_UIF; pinToggle(pinB3); } } void LEDtoggleOnTimer2underflow() { /* //timerInitCounter(timer_2, 0xFFFF, 0, upCounting); // reset timer 2 periperal RCC->APB1RSTR |= RCC_APB1RSTR_TIM2RST; //while(RCC->APB1RSTR & RCC_APB1RSTR_TIM2RST); // enable timer 2 peripheral RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; // only allow under-/overflow interrupts TIM2->CR1 |= TIM_CR1_URS; // set to count down mode TIM2->CR1 |= TIM_CR1_DIR; // set clock prescaler TIM2->PSC = 0xFFFF; // set outo reload value TIM2->ARR = 0xFFFFFFFF; //TIM2->EGR |= TIM_EGR_UG; // Enable Update Interrupt TIM2->DIER |= TIM_DIER_UIE; // reset status register TIM2->SR = 0; //NVIC_SetVector(TIM2_IRQn, (uint32_t)&TIM2_IRQHandler); NVIC_SetPriority(TIM2_IRQn,3); NVIC_EnableIRQ(TIM2_IRQn); // timer enable TIM2->CR1 |= TIM_CR1_CEN; //timerSart(timer_2); while(1) { if(TIM2->CNT > 0xFFFF) { pinWrite(pinB3,1); } else { pinWrite(pinB3,0); } if(led_on){ pinWrite(pinB3,1); } else{ pinWrite(pinB3,0); } } */ #if 0 // example based on // https://alsaibie.github.io/me319/prelabsextra/lab4extra/ /* Enable Timer 2 peripheral */ RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; /* Default APB1 speed is 16MHz. Scale Timer 2 to 20kHz */ //TIM2->PSC = (uint32_t)(16E6 / 20E3) - 1; TIM2->PSC = (8E6 / 20E3) - 1 ; /* Disable Timer*/ TIM2->CR1 = 0; /* Reinitialize counter and update registers */ TIM2->EGR = TIM_EGR_UG; /* 1000 Counts at 20kHz => 20Hz IRQ */ //TIM2->ARR = 1000 - 1; TIM2->ARR = 10000-1; /* Enable hardware interrupts */ TIM2->DIER |= TIM_DIER_UIE; /* Start Timer */ TIM2->CR1 |= TIM_CR1_CEN; #endif timerInitCounter(timer_2,(8E6/20E3)-1,10000-1,upCounting); //TIM2->EGR = TIM_EGR_UG; TIM2->DIER = TIM_DIER_UIE; timerSart(timer_2); NVIC_SetPriority(TIM2_IRQn, 2); NVIC_EnableIRQ(TIM2_IRQn); while(1) { } } int main(int argc, char *argv[]) { setup(); LEDtoggleOnTimer2underflow(); while(1); return 1; } void setup() { uint8_t i = 0; delayInitMs(8000000, 1000); // Clock Freq and Divider for ARM library pinConfig(pinB3, output, pushPull, def_res, def_speed); pinConfig(pinA0, input, def_stage, pullDown, def_speed); setupInit(); // This is the sescond call of System init the assebly start code is calling it before the main. usartInit( usart2, pinA2, pinA15, 115200, eight, NO_PARITY_CTRL, noFlowControl); //clears screen and send the wellcome messgae print_Usart(usart2, ASCII_clear); print_Usart(usart2, "Hello to our KED project\n\r"); //blinks 10 times to indicate the sicsessfull init if the device for(i = 0 ; i < 10 ; i++) { delayMs(100); pinToggle(pinB3); delayMs(100); } pinWrite(pinB3,0); }