test timer implemented

This commit is contained in:
King Kévin 2016-01-24 16:51:32 +01:00
parent 20538ae13b
commit c7d6b855d9
2 changed files with 31 additions and 1 deletions

29
main.c
View File

@ -27,7 +27,8 @@
#include <libopencm3/cm3/scb.h> // vector table definition
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
#include <libopencm3/stm32/spi.h> // SPI library
#include <libopencm3/stm32/timer.h>
#include <libopencm3/cm3/nvic.h>
/* own libraries */
#include "main.h" // board definitions
@ -64,6 +65,31 @@ static void gpio_setup(void)
gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull'
}
static void timer_setup(void)
{
#define VFD_TIMER TIM2
#define VFD_TIMER_RCC RCC_TIM2
#define VFD_TIMER_IRQ NVIC_TIM2_IRQ
rcc_periph_clock_enable(VFD_TIMER_RCC); // enable clock for timer block
timer_reset(VFD_TIMER); // reset timer state
timer_set_mode(VFD_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock,edge alignment (simple count), and count up
timer_set_prescaler(VFD_TIMER, (SYSTEM_CLOCK_FREQ/(1<<16))-1); // set the prescaler so this 16 bits timer overflows at 1Hz
//timer_set_period(0xffff/(VFD_DIGITS+VFD_MATRIX)/150); // set the refresh frequency
timer_set_period(VFD_TIMER, 0xffff);
timer_enable_irq(VFD_TIMER, TIM_DIER_CC1IE); // enable interrupt for timer
nvic_enable_irq(VFD_TIMER_IRQ); // allow interrupt for timer
timer_enable_counter(VFD_TIMER); // enable timer
}
void tim2_isr(void)
{
if (timer_get_flag(VFD_TIMER, TIM_SR_UIF)) {
timer_clear_flag(VFD_TIMER, TIM_SR_UIF);
gpio_toggle(LED_PORT, LED_PIN);
}
}
int main(void)
{
SCB_VTOR = (uint32_t) 0x08002000; // relocate vector table because of the bootloader
@ -73,6 +99,7 @@ int main(void)
usart_setup(); // setup USART (for printing)
cdcacm_setup(); // setup USB CDC ACM (for printing)
vfd_setup(); // setup VFD
timer_setup();
setbuf(stdout, NULL); // set standard out buffer to NULL to immediately print
setbuf(stderr, NULL); // set standard error buffer to NULL to immediately print

3
main.h
View File

@ -14,6 +14,9 @@
*/
/* Copyright (c) 2016 King Kévin <kingkevin@cuvoodoo.info> */
/* system clock frequency in Hz */
#define SYSTEM_CLOCK_FREQ 72000000
/* LED is on pin 11/PA1 */
#define LED_RCC RCC_GPIOA
#define LED_PORT GPIOA