-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimekeeper.c
227 lines (172 loc) · 4.97 KB
/
timekeeper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* timekeeper.c - keeps master clock and updates internally used timers
*
* Author: Dan Green (danngreen1@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* See http://creativecommons.org/licenses/MIT/ for more information.
*
* -----------------------------------------------------------------------------
*/
#include "globals.h"
#include "timekeeper.h"
#include "params.h"
#include "calibration.h"
#include "system_settings.h"
#include "leds.h"
#include "dig_pins.h"
volatile uint32_t ping_tmr;
volatile uint32_t ping_ledbut_tmr;
volatile uint32_t clkout_trigger_tmr;
volatile uint32_t loopled_tmr[2];
extern volatile uint32_t ping_time;
extern uint8_t mode[NUM_CHAN][NUM_CHAN_MODES];
extern uint8_t loop_led_state[NUM_CHAN];
extern uint8_t global_mode[NUM_GLOBAL_MODES];
void inc_tmrs(void)
{
ping_tmr++;
ping_ledbut_tmr++;
clkout_trigger_tmr++;
loopled_tmr[0]++;
loopled_tmr[1]++;
if (clkout_trigger_tmr>=ping_time)
{
CLKOUT_ON;
reset_clkout_trigger_tmr();
}
else if (clkout_trigger_tmr >= (ping_time>>1))
{
CLKOUT_OFF;
}
else if (mode[0][MAIN_CLOCK_GATETRIG]==TRIG_MODE && clkout_trigger_tmr >= TRIG_TIME){
CLKOUT_OFF;
}
}
void reset_ping_ledbut_tmr(void)
{
ping_ledbut_tmr=0;
}
void reset_ping_tmr(void)
{
ping_tmr=0;
}
void reset_clkout_trigger_tmr(void)
{
clkout_trigger_tmr=0;
if (global_mode[QUANTIZE_MODE_CHANGES]!=0)
{
process_mode_flags(0);
process_mode_flags(1);
}
}
void reset_loopled_tmr(uint8_t channel)
{
loopled_tmr[channel]=0;
// if (!mode[channel][CONTINUOUS_REVERSE])
// {
if (!global_mode[CALIBRATE] && !global_mode[SYSTEM_SETTINGS])
loop_led_state[channel]=1;
if (channel==0) {
CLKOUT1_ON;
} else {
CLKOUT2_ON;
}
// }
}
void init_timekeeper(void){
NVIC_InitTypeDef nvic;
EXTI_InitTypeDef EXTI_InitStructure;
ping_tmr=0;
ping_ledbut_tmr=0;
clkout_trigger_tmr=0;
loopled_tmr[0]=0;
loopled_tmr[1]=0;
//Set Priority Grouping mode to 2-bits for priority and 2-bits for sub-priority
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_EXTILineConfig(EXTI_CLOCK_GPIO, EXTI_CLOCK_pin);
EXTI_InitStructure.EXTI_Line = EXTI_CLOCK_line;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
nvic.NVIC_IRQChannel = EXTI_CLOCK_IRQ;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 0;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
}
// Sample Clock EXTI line (I2S2 LRCLK)
void EXTI_Handler(void)
{
if(EXTI_GetITStatus(EXTI_CLOCK_line) != RESET)
{
inc_tmrs();
if (!global_mode[SYSTEM_SETTINGS] && !global_mode[CALIBRATE])
update_channel_leds();
EXTI_ClearITPendingBit(EXTI_CLOCK_line);
}
}
void init_adc_param_update_timer(void)
{
TIM_TimeBaseInitTypeDef tim;
NVIC_InitTypeDef nvic;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);
nvic.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 3;
nvic.NVIC_IRQChannelSubPriority = 2;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
//168MHz / prescale=3 ---> 42MHz / 30000 ---> 1.4kHz
//20000 and 0x1 ==> works well
TIM_TimeBaseStructInit(&tim);
tim.TIM_Period = 30000;
tim.TIM_Prescaler = 0x3;
tim.TIM_ClockDivision = 0;
tim.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM9, &tim);
TIM_ITConfig(TIM9, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM9, ENABLE);
}
void adc_param_update_IRQHandler(void)
{
//Takes 7-8us
if (TIM_GetITStatus(TIM9, TIM_IT_Update) != RESET) {
process_adc();
if (global_mode[CALIBRATE])
{
update_calibration();
update_calibrate_leds();
}
else
update_params();
if (global_mode[SYSTEM_SETTINGS])
{
update_system_settings();
update_system_settings_leds();
}
check_entering_system_mode();
update_ping_ledbut();
update_INF_REV_ledbut(0);
update_INF_REV_ledbut(1);
TIM_ClearITPendingBit(TIM9, TIM_IT_Update);
}
}