2013-11-06 23:04:33 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2014-01-21 21:40:13 +00:00
|
|
|
#include <string.h>
|
2013-11-06 23:04:33 +00:00
|
|
|
|
|
|
|
#include "stm_misc.h"
|
|
|
|
#include "stm32f4xx_rcc.h"
|
|
|
|
#include "stm32f4xx_tim.h"
|
|
|
|
|
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "mpconfig.h"
|
2014-01-21 21:40:13 +00:00
|
|
|
#include "qstr.h"
|
2013-11-06 23:04:33 +00:00
|
|
|
#include "parse.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "obj.h"
|
2013-11-06 23:04:33 +00:00
|
|
|
#include "runtime.h"
|
|
|
|
|
|
|
|
#include "timer.h"
|
|
|
|
|
|
|
|
// TIM6 is used as an internal interrup to schedule something at a specific rate
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_t timer_py_callback;
|
2013-11-06 23:04:33 +00:00
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_t timer_py_set_callback(mp_obj_t f) {
|
2013-11-06 23:04:33 +00:00
|
|
|
timer_py_callback = f;
|
2013-12-21 18:17:45 +00:00
|
|
|
return mp_const_none;
|
2013-11-06 23:04:33 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_t timer_py_set_period(mp_obj_t period) {
|
|
|
|
TIM6->ARR = mp_obj_get_int(period) & 0xffff;
|
|
|
|
return mp_const_none;
|
2013-11-09 11:29:49 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_t timer_py_set_prescaler(mp_obj_t prescaler) {
|
|
|
|
TIM6->PSC = mp_obj_get_int(prescaler) & 0xffff;
|
|
|
|
return mp_const_none;
|
2013-11-06 23:04:33 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_t timer_py_get_value(void) {
|
|
|
|
return mp_obj_new_int(TIM6->CNT & 0xfffff);
|
2013-11-06 23:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void timer_init(void) {
|
2013-12-21 18:17:45 +00:00
|
|
|
timer_py_callback = mp_const_none;
|
2013-11-06 23:04:33 +00:00
|
|
|
|
|
|
|
// TIM6 clock enable
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
|
|
|
|
|
2013-11-09 11:29:49 +00:00
|
|
|
// Compute the prescaler value so TIM6 runs at 20kHz
|
|
|
|
uint16_t PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 20000) - 1;
|
2013-11-06 23:04:33 +00:00
|
|
|
|
|
|
|
// Time base configuration
|
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
2013-11-09 11:29:49 +00:00
|
|
|
TIM_TimeBaseStructure.TIM_Period = 20000; // timer cycles at 1Hz
|
2013-11-06 23:04:33 +00:00
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
|
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // unused for TIM6
|
|
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // unused for TIM6
|
|
|
|
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
|
|
|
|
|
|
|
|
// enable perhipheral preload register
|
|
|
|
TIM_ARRPreloadConfig(TIM6, ENABLE);
|
|
|
|
|
|
|
|
// enable interrupt when counter overflows
|
|
|
|
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
|
|
|
|
|
|
|
|
// set up interrupt
|
|
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = TIM6_DAC_IRQn;
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0f; // lowest priority
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f; // lowest priority
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
|
|
|
// TIM6 enable counter
|
|
|
|
TIM_Cmd(TIM6, ENABLE);
|
|
|
|
|
|
|
|
// Python interface
|
2014-01-21 21:40:13 +00:00
|
|
|
mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("timer"));
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_store_attr(m, QSTR_FROM_STR_STATIC("callback"), mp_make_function_n(1, timer_py_set_callback));
|
|
|
|
mp_store_attr(m, QSTR_FROM_STR_STATIC("period"), mp_make_function_n(1, timer_py_set_period));
|
|
|
|
mp_store_attr(m, QSTR_FROM_STR_STATIC("prescaler"), mp_make_function_n(1, timer_py_set_prescaler));
|
|
|
|
mp_store_attr(m, QSTR_FROM_STR_STATIC("value"), mp_make_function_n(0, timer_py_get_value));
|
|
|
|
mp_store_name(QSTR_FROM_STR_STATIC("timer"), m);
|
2013-11-06 23:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void timer_interrupt(void) {
|
2013-12-21 18:17:45 +00:00
|
|
|
if (timer_py_callback != mp_const_none) {
|
2013-11-06 23:04:33 +00:00
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
2013-11-09 11:29:49 +00:00
|
|
|
// XXX what to do if the GC is in the middle of running??
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_call_function_0(timer_py_callback);
|
2013-11-06 23:04:33 +00:00
|
|
|
nlr_pop();
|
|
|
|
} else {
|
|
|
|
// uncaught exception
|
|
|
|
printf("exception in timer interrupt\n");
|
2014-01-13 17:19:16 +00:00
|
|
|
mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR);
|
2013-11-06 23:04:33 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|