Subversion Repositories EngineBay2

Rev

Rev 8 | Rev 28 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 mjames 1
/*
2
 * misc.c
3
 *
4
 *  Created on: 21 Sep 2016
5
 *      Author: Mike
6
 */
8 mjames 7
#include "stm32l1xx_hal.h"
9 mjames 8
#include "misc.h"
9
#include "main.h"
3 mjames 10
 
9 mjames 11
unsigned volatile long RPM_Time[RPM_SAMPLES];  // sampled on falling edge
12
unsigned volatile long RPM_Count; // incremented every reading
13
 
14
 
15
 
3 mjames 16
void _init(void)
17
{
18
 
19
}
6 mjames 20
 
21
 
8 mjames 22
void TIM2_IRQHandler(void)
23
{
9 mjames 24
        if(__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1))
25
        {
26
                  __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1) ;
27
                        RPM_Time[RPM_Count++] = __HAL_TIM_GET_COMPARE(&htim2,TIM_CHANNEL_1);
28
                        if (RPM_Count == RPM_SAMPLES) {
29
                                RPM_Count = 0;
30
                        }
31
        }
8 mjames 32
}
33
 
34
 
35
char blink = 0;
36
// 100mS periodic sampler handler
37
void  TIM6_IRQHandler(void)
38
{
9 mjames 39
        if(__HAL_TIM_GET_FLAG(&htim6, TIM_FLAG_UPDATE))
40
        {
41
          __HAL_TIM_CLEAR_FLAG(&htim6, TIM_FLAG_UPDATE) ;
8 mjames 42
 
9 mjames 43
          blink = !blink;
44
          HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, blink ? GPIO_PIN_SET : GPIO_PIN_RESET);
8 mjames 45
 
9 mjames 46
          TimerFlag = 1;
47
          if (NoSerialInCTR < 5) {
48
            NoSerialInCTR++;
49
                if (NoSerialInCTR == 5) {
50
                  NoSerialIn = 1;
8 mjames 51
                }
9 mjames 52
          }
53
        }
8 mjames 54
 
55
}
56
 
57
 
58
 
6 mjames 59
#if OLD_ENGINEBAY
60
//-----------------------------------------------------------
61
// This is the 10Hz timer
62
 
63
void interrupt TimerISR1(void) {
64
        TimerFlag = 1;
65
        if (NoSerialInCTR < 5) {
66
                NoSerialInCTR++;
67
                if (NoSerialInCTR == 5) {
68
                        NoSerialIn = 1;
69
                }
70
        }
71
}
72
//-----------------------------------------------------------
73
 
74
 
75
volatile unsigned long RPM_Time[RPM_LIMIT];
76
volatile unsigned long RPM_Count; // incremented every reading
77
 
78
void TimerISR0(void) {
79
        // check the listing
80
        asm volatile ("STMDB    SP!, {r0-r4} ");
81
// sort out the captures
82
        int stat = REG(TIMER0_IR);
83
        if (stat & (1 << 4)) //CR0
84
                        {
85
                RPM_Time[RPM_Count++] = REG(TIMER0_CR0);
86
                if (RPM_Count == RPM_LIMIT) {
87
                        RPM_Count = 0;
88
                }
89
        }
90
        if (stat & (1 << 5)) //CR1
91
                        {
92
//      TDC_Time = REG(TIMER0_CR1);
93
//      TDC_Count++;
94
        }
95
 
96
        REG(TIMER0_IR) = stat; // clear the interrupts just handled.
97
        REG(VICVectAddr) = 0; // reset the VIC
98
        asm volatile ("LDMIA    SP!, {r0-r4} ");
99
        asm volatile ("SUBS PC,lr,#4");
100
}
101
 
102
//-----------------------------------------------------------
103
// UART management
104
// serial counter
105
#define THRE (1<<6)
106
#define RDR  (1<<0)
107
#define BUFFSIZ 256
108
 
109
volatile char txbuff[BUFFSIZ];
110
volatile int txptr = 0;
111
volatile int txcnt = 0;
112
 
113
volatile char rxbuff[BUFFSIZ];
114
volatile int rxptr = 0;
115
volatile int rxcnt = 0;
116
 
117
void Uart0ISR(void) __attribute__((naked));
118
void Uart0ISR(void) __attribute__((noinline));
119
void Uart0ISR(void) {
120
        asm volatile ("STMDB    SP!, {r0-r3} ");
121
        int stat;
122
        while (!((stat = REG(UART0_IIR)) & 1)) { // LSbit is inactive
123
                switch (stat & 0x0E) {
124
                case 4:
125
                case 12: {
126
                        char c = REG(UART0_RBR);
127
 
128
                        NoSerialIn = NoSerialInCTR = 0;
129
 
130
                        if (rxcnt < (BUFFSIZ - 1)) {
131
                                rxbuff[(rxptr + rxcnt) % BUFFSIZ] = c;
132
                                rxcnt++;
133
                        }
134
                }
135
                        break;
136
                case 2: {
137
                        if (txcnt > 0) {
138
                                REG(UART0_THR) = txbuff[txptr];
139
                                txcnt--;
140
                                txptr++;
141
                                txptr %= BUFFSIZ;
142
                        } else {
143
                                REG(UART0_IER) &= ~(1 << 1); // disable TX
144
                        }
145
                }
146
                        break;
147
                }
148
        }
149
        REG(VICVectAddr) = 0; // reset the VIC
150
        asm volatile ("LDMIA    SP!, {r0-r3} ");
151
        asm volatile ("SUBS PC,lr,#4");
152
}
153
 
154
int my_putchar(int c) {
155
        int rc = 0;
156
         {
157
                STI;
158
                if (!(REG(UART0_IER) & (1 << 1))) {
159
                        REG(UART0_IER) |= (1 << 1);
160
                    REG(UART0_THR) = c;
161
                        rc=1;
162
                } else {
163
 
164
                        if (txcnt < (BUFFSIZ - 1)) {
165
                                txbuff[(txptr + txcnt) % BUFFSIZ] = c;
166
                                txcnt++;
167
                                rc = 1;
168
                        }
169
                }
170
                CLI;
171
        }
172
        return rc;
173
}
174
 
175
int my_getchar(void) {
176
 
177
        int c = -1;
178
        STI;
179
        if (rxcnt) {
180
                c = rxbuff[rxptr];
181
                rxcnt--;
182
                rxptr++;
183
                rxptr %= BUFFSIZ;
184
        }
185
        CLI;
186
        return c;
187
}
188
 
189
 
190
 
191
#endif