-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.c
238 lines (209 loc) · 5.93 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
229
230
231
232
233
234
235
236
237
238
/**
* Copyright (C) 2019 Piers Titus van der Torren
*
* This file is part of Striso Control.
*
* Striso Control is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Striso Control is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Striso Control. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#define CONFIG_HERE
#include "config.h"
#undef CONFIG_HERE
#include "striso.h"
#include "usbcfg.h"
// #include "exceptions.h"
#include "pconnection.h"
#include "synth.h"
#include "button_read.h"
#include "messaging.h"
#include "motionsensor.h"
#include "ws2812.h"
#include "version.h"
#include "midi_serial.h"
#include "led.h"
#include "config_editor/config_editor.h"
#include "aux_jack.h"
/**
* Firmware version description on fixed flash address for bootloader
*/
#define FWVERSION_PREFIX "Firmware version: striso_control_"
__attribute__ ((section(".fwinfo"))) __attribute__((used))
const struct {
char fwversion[512];
uint32_t confightm[16];
} fwinfo = {
.fwversion = FWVERSION_PREFIX FWVERSION "\r\n",
.confightm = {(uint32_t)confightm_p1, sizeof(confightm_p1),
(uint32_t)devspec_id->id, sizeof(devspec_id->id),
(uint32_t)fwinfo.fwversion, sizeof(FWVERSION_PREFIX FWVERSION) - 1,
(uint32_t)confightm_p2, sizeof(confightm_p2),
(uint32_t)default_config, sizeof(default_config),
(uint32_t)flash_config, sizeof(default_config),
(uint32_t)confightm_p3, sizeof(confightm_p3)}
};
// force sqrtf to use FPU, the standard one apparently doesn't
float vsqrtf(float op1) {
float result;
__ASM volatile ("vsqrt.f32 %0, %1" : "=w" (result) : "w" (op1) );
return (result);
}
#ifdef USE_UART
static SerialConfig ser_cfg = {
500000,
0,
0,
0,
};
#endif
/*
* LED flash thread.
*/
static THD_WORKING_AREA(waThread1, 128);
static void Thread1(void *arg) {
(void)arg;
chRegSetThreadName("blinker");
// int msg[8];
// msg[0] = ID_SYS;
// msg[1] = ID_SYS_MSGQUE_OVERFLOW_BB;
while (TRUE) {
chThdSleepMilliseconds(300);
palSetLine(LINE_LED1);
chThdSleepMilliseconds(300);
// msg[2] = underruns;
//if (!msgSend(3,msg))
palClearLine(LINE_LED1);
}
}
static void pack(int *in, uint8_t *out, int n) {
int c;
for (c=0; c<n; c++) {
out[c*2] = 0x7f & (uint8_t)(in[c]>>7);
out[c*2+1] = 0x7f & (uint8_t)(in[c]);
}
}
static void unpack(uint8_t *in, int *out, int n) {
int c;
for (c=0; c<n; c++) {
out[c] = ((int)in[c*2])<<7 | ((int)in[c*2+1]);
}
}
/*
* Message send thread
*/
static THD_WORKING_AREA(waThreadSend, 512);
static void ThreadSend(void *arg) {
(void)arg;
chRegSetThreadName("send messages");
int msg[9];
uint8_t cmsg[16];
int size;
cmsg[0] = 0;
while (TRUE) {
size = msgGet(9, msg);
if (size >= 2 && size <= 9) {
synth_message(size, msg);
cmsg[0] = 0x80 | ((uint8_t)msg[0])<<3 | ((uint8_t)(size-2));
cmsg[1] = 0x7f & (uint8_t)msg[1];
pack(&msg[2], &cmsg[2], size - 2);
#ifdef USE_UART
chSequentialStreamWrite((BaseSequentialStream *)&SD1, cmsg, 2+(size-2)*2);
#endif
#ifdef USE_USB
if (config.send_usb_bulk) {
//chSequentialStreamWrite((BaseSequentialStream *)&BDU1,cmsg, 2+(size-2)*2);
obqWriteTimeout(&BDU1.obqueue, cmsg, 2+(size-2)*2, TIME_IMMEDIATE);
}
#endif
}
// sleep to limit the output stream to 2 messages per millisecond (with CH_FREQUENCY = 2000)
// for compatibility with MIDI usb on Axoloti
chThdSleep(1);
}
}
/*
* Application entry point.
*/
int main(void) {
/*
* System initializations.
* - HAL initialization, this also initializes the configured device drivers
* and performs the board-specific initializations.
* - Kernel initialization, the main() function becomes a thread and the
* RTOS is active.
*/
halInit();
chSysInit();
// Enable the headphone amp to stop noise at boot with some amps
palSetLine(LINE_HP_EN);
/*
* Activates the serial driver using the driver default configuration.
*/
#ifdef USE_UART
sdStart(&SD1, &ser_cfg);
#endif
#ifdef USE_WS2812
ws2812_init();
ws2812_write_led(0, 15, 31, 0);
ws2812_write_led(1, 15, 31, 0);
ws2812_write_led(2, 15, 31, 0);
ws2812_write_led(3, 0, 15, 15);
#endif
led_init();
led_rgb(0x000800);
#ifdef USE_USB
InitPConnection();
#endif
MessagingInit();
/*
* Creates the message send thread.
*/
chThdCreateStatic(waThreadSend, sizeof(waThreadSend), NORMALPRIO, ThreadSend, NULL);
/*
* Creates the LED flash thread.
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
#ifdef USE_INTERNAL_SYNTH
codec_init(SAMPLERATE);
start_synth_thread();
#endif
for (int i=8; i<168; i++) {
led_rgb3(0,i,0);
chThdSleepMilliseconds(1);
}
aux_jack_init();
synth_control_init();
#ifdef USE_MIDI_OUT
// Send initial configuration MIDI
midi_config();
#endif
ButtonReadStart();
#if defined(USE_MPU6050) || defined(USE_LSM6DSL)
MotionSensorStart();
#endif
#ifdef USE_WS2812
ws2812_write_led(0, 0, 1, 0);
ws2812_write_led(1, 0, 0, 0);
ws2812_write_led(2, 0, 0, 0);
ws2812_write_led(3, 0, 15, 15);
#endif
while (1) {
chThdSleepMilliseconds(2);
synth_tick();
led_tick();
#ifdef USE_USB
PExReceive();
#endif
}
}