diff --git a/include/timer.h b/include/timer.h new file mode 100644 index 0000000..000abeb --- /dev/null +++ b/include/timer.h @@ -0,0 +1,32 @@ +/* + +The MIT License (MIT) + +Copyright (c) 2016 Hubert Denkmair + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +*/ + +#pragma once + +#include + +void timer_init(); +uint32_t timer_get(); diff --git a/src/main.c b/src/main.c index 63e067a..045e226 100644 --- a/src/main.c +++ b/src/main.c @@ -38,6 +38,7 @@ THE SOFTWARE. #include "can.h" #include "led.h" #include "dfu.h" +#include "timer.h" void HAL_MspInit(void); void SystemClock_Config(void); @@ -63,6 +64,8 @@ int main(void) led_init(&hLED, LED1_GPIO_Port, LED1_Pin, false, LED2_GPIO_Port, LED2_Pin, false); led_set_mode(&hLED, led_mode_off); + timer_init(); + can_init(&hCAN, CAN); diff --git a/src/timer.c b/src/timer.c new file mode 100644 index 0000000..e315cc1 --- /dev/null +++ b/src/timer.c @@ -0,0 +1,50 @@ +/* + +The MIT License (MIT) + +Copyright (c) 2016 Hubert Denkmair + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +*/ + +#include "timer.h" +#include "stm32f0xx_hal.h" + +void timer_init() +{ + __HAL_RCC_TIM2_CLK_ENABLE(); + + TIM2->CR1 = 0; + TIM2->CR2 = 0; + TIM2->SMCR = 0; + TIM2->DIER = 0; + TIM2->CCMR1 = 0; + TIM2->CCMR2 = 0; + TIM2->CCER = 0; + TIM2->PSC = 480-1; // run @48MHz/480 = 100kHz + TIM2->ARR = 0xFFFFFFFF; + TIM2->CR1 |= TIM_CR1_CEN; + TIM2->EGR = TIM_EGR_UG; +} + +uint32_t timer_get() +{ + return TIM2->CNT; +}