prepare hardware timestamps: add a 100kHz timer

This commit is contained in:
Hubert Denkmair 2016-05-02 21:55:26 +02:00
parent edd477101c
commit e0e9e77225
3 changed files with 85 additions and 0 deletions

32
include/timer.h Normal file
View File

@ -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 <stdint.h>
void timer_init();
uint32_t timer_get();

View File

@ -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);

50
src/timer.c Normal file
View File

@ -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;
}