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