Subversion Repositories chibiosIgnition

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/*
2
 * hardware.c
3
 *
4
 *  Created on: 19 Aug 2017
5
 *      Author: Mike
6
 */
7
#include "ch.h"
8
#include "hal.h"
9
#include "hardware.h"
10
 
3 mjames 11
uint16_t timerSamples[timerSampleSize];
12
 
13
uint8_t timerInIndex;
14
uint8_t timerOutIndex;
15
 
16
 
17
 
2 mjames 18
void TIM1_UP_IRQHandler(void) {
19
// we have an interrupt here .
3 mjames 20
        if (TIM1->SR & TIM_SR_CC1IF) {
21
                TIM1->SR &= ~TIM_SR_CC1IF;
22
                timerSamples[timerInIndex++] = TIM1->CCMR1;
23
                if (timerInIndex == timerSampleSize) {
24
                        timerInIndex = 0;
25
                }
2 mjames 26
 
3 mjames 27
        }
2 mjames 28
 
29
}
30
 
31
void initTimer(void) {
32
        rccEnableTIM1(FALSE);
33
        rccResetTIM1();
7 mjames 34
        nvicEnableVector(TIM1_UP_IRQn,STM32_GPT_TIM1_IRQ_PRIORITY);
2 mjames 35
        //gptp->clock = STM32_TIMCLK2;
36
 
37
        TIM1->CR1 = 0; /* Initially stopped.           */
38
        TIM1->CR2 = TIM_CR2_CCDS; /* DMA on UE (if any).          */
3 mjames 39
        TIM1->PSC = 72 * 3; /* Prescaler value : 3 uS tick timer .             */
2 mjames 40
        TIM1->DIER = 0;
41
 
42
}
43
 
44
void stopTimer(void) {
45
        nvicDisableVector(TIM1_UP_IRQn);
7 mjames 46
        rccDisableTIM1();
2 mjames 47
}
48
 
49
void startTimer(void) {
3 mjames 50
          TIM1->ARR  = 0xFFFF;                 /* Time constant.               */
2 mjames 51
          TIM1->EGR  = TIM_EGR_UG;             /* Update event.                */
52
          TIM1->CNT  = 0;                      /* Reset counter.               */
53
          /* NOTE: After generating the UG event it takes several clock cycles before
54
             SR bit 0 goes to 1. This is because the clearing of CNT has been inserted
55
             before the clearing of SR, to give it some time.*/
56
          TIM1->SR   = 0;                      /* Clear pending IRQs (if any). */
57
          TIM1->DIER = TIM_DIER_UIE;           /* Update Event IRQ enabled.    */
58
          TIM1->CR1  = TIM_CR1_URS | TIM_CR1_CEN;
3 mjames 59
 
60
          TIM1->CCMR1 = 3344;
2 mjames 61
}