forked from newaetech/chipshouter-picoemp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
228 lines (198 loc) · 7.08 KB
/
main.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
227
228
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "picoemp.h"
#include "serial.h"
#include "trigger_basic.pio.h"
static bool armed = false;
static bool pulse = false;
static bool timeout_active = true;
static bool hvp_internal = true;
static absolute_time_t timeout_time;
static uint offset = 0xFFFFFFFF;
// defaults taken from original code
#define PULSE_DELAY_CYCLES_DEFAULT 0
#define PULSE_TIME_CYCLES_DEFAULT 625 // 5us in 8ns cycles
#define PULSE_TIME_US_DEFAULT 5 // 5us
#define PULSE_POWER_DEFAULT 0.0122
static uint32_t pulse_time;
static uint32_t pulse_delay_cycles;
static uint32_t pulse_time_cycles;
static bool button_enabled = true;
static int32_t button_alarm_id = 0;
static union float_union {float f; uint32_t ui32;} pulse_power;
void arm() {
gpio_put(PIN_LED_CHARGE_ON, true);
armed = true;
}
void disarm() {
gpio_put(PIN_LED_CHARGE_ON, false);
armed = false;
picoemp_disable_pwm();
}
uint32_t get_status() {
uint32_t result = 0;
if(armed) {
result |= 0b1;
}
if(gpio_get(PIN_IN_CHARGED)) {
result |= 0b10;
}
if(timeout_active) {
result |= 0b100;
}
if(hvp_internal) {
result |= 0b1000;
}
return result;
}
void update_timeout() {
timeout_time = delayed_by_ms(get_absolute_time(), 60 * 1000);
}
void fast_trigger() {
// Choose which PIO instance to use (there are two instances)
PIO pio = pio0;
// Our assembled program needs to be loaded into this PIO's instruction
// memory. This SDK function will find a location (offset) in the
// instruction memory where there is enough space for our program. We need
// to remember this location!
if (offset == 0xFFFFFFFF) { // Only load the program once
offset = pio_add_program(pio, &trigger_basic_program);
}
// Find a free state machine on our chosen PIO (erroring if there are
// none). Configure it to run our program, and start it, using the
// helper function we included in our .pio file.
uint sm = 0;
trigger_basic_init(pio, sm, offset, PIN_IN_TRIGGER, PIN_OUT_HVPULSE);
pio_sm_put_blocking(pio, sm, pulse_delay_cycles);
pio_sm_put_blocking(pio, sm, pulse_time_cycles);
}
// Alarm callback to re-enable buttons
int64_t button_enable(alarm_id_t alarm_id, void *user_data) {
button_enabled = true;
return 0;
}
void button_press_callback(uint gpio, uint32_t events) {
if(button_enabled) {
button_enabled = false;
update_timeout();
if(gpio == PIN_BTN_PULSE) {
pulse = true;
} else if(gpio == PIN_BTN_ARM) {
if(!armed) {
arm();
} else {
disarm();
}
}
} else {
cancel_alarm(button_alarm_id);
}
// Add an alarm to re-enable the buttons in 80ms (aka debounce time)
button_alarm_id = add_alarm_in_ms(80, button_enable, NULL, false);
}
int main() {
// Initialize USB-UART as STDIO
stdio_init_all();
picoemp_init();
// Init for reset pin (move somewhere else)
gpio_init(1);
gpio_set_dir(1, GPIO_OUT);
gpio_put(1, 1);
// Run serial-console on second core
multicore_launch_core1(serial_console);
pulse_time = PULSE_TIME_US_DEFAULT;
pulse_power.f = PULSE_POWER_DEFAULT;
pulse_delay_cycles = PULSE_DELAY_CYCLES_DEFAULT;
pulse_time_cycles = PULSE_TIME_CYCLES_DEFAULT;
// Set button interrupt and callback function
gpio_set_irq_enabled_with_callback(PIN_BTN_ARM, GPIO_IRQ_LEVEL_HIGH, true, &button_press_callback);
gpio_set_irq_enabled(PIN_BTN_PULSE, GPIO_IRQ_LEVEL_LOW, true);
while(1) {
gpio_put(PIN_LED_HV, gpio_get(PIN_IN_CHARGED));
// Handle serial commands (if any)
while(multicore_fifo_rvalid()) {
uint32_t command = multicore_fifo_pop_blocking();
switch(command) {
case cmd_arm:
arm();
update_timeout();
multicore_fifo_push_blocking(return_ok);
break;
case cmd_disarm:
disarm();
multicore_fifo_push_blocking(return_ok);
break;
case cmd_pulse:
picoemp_pulse(pulse_time);
update_timeout();
multicore_fifo_push_blocking(return_ok);
break;
case cmd_status:
multicore_fifo_push_blocking(return_ok);
multicore_fifo_push_blocking(get_status());
break;
case cmd_enable_timeout:
timeout_active = true;
update_timeout();
multicore_fifo_push_blocking(return_ok);
break;
case cmd_disable_timeout:
timeout_active = false;
multicore_fifo_push_blocking(return_ok);
break;
case cmd_config_pulse_delay_cycles:
pulse_delay_cycles = multicore_fifo_pop_blocking();
multicore_fifo_push_blocking(return_ok);
break;
case cmd_config_pulse_time_cycles:
pulse_time_cycles = multicore_fifo_pop_blocking();
multicore_fifo_push_blocking(return_ok);
break;
case cmd_fast_trigger:
fast_trigger();
multicore_fifo_push_blocking(return_ok);
while(!pio_interrupt_get(pio0, 0));
multicore_fifo_push_blocking(return_ok);
pio_sm_set_enabled(pio0, 0, false);
picoemp_configure_pulse_output();
break;
case cmd_internal_hvp:
picoemp_configure_pulse_output();
hvp_internal = true;
multicore_fifo_push_blocking(return_ok);
break;
case cmd_external_hvp:
picoemp_configure_pulse_external();
hvp_internal = false;
multicore_fifo_push_blocking(return_ok);
break;
case cmd_config_pulse_time:
pulse_time = multicore_fifo_pop_blocking();
multicore_fifo_push_blocking(return_ok);
break;
case cmd_config_pulse_power:
pulse_power.ui32 = multicore_fifo_pop_blocking();
multicore_fifo_push_blocking(return_ok);
break;
case cmd_toggle_gp1:
gpio_xor_mask(1<<1);
multicore_fifo_push_blocking(return_ok);
break;
}
}
// Pulse
if(pulse) {
pulse = false;
picoemp_pulse(pulse_time);
}
if(!gpio_get(PIN_IN_CHARGED) && armed) {
picoemp_enable_pwm(pulse_power.f);
}
if(timeout_active && (get_absolute_time() > timeout_time) && armed) {
disarm();
}
}
return 0;
}