1 // Copyright 2014 Emilie Gillet.
2 //
3 // Author: Emilie Gillet (emilie.o.gillet@gmail.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 // See http://creativecommons.org/licenses/MIT/ for more information.
24 
25 #include "clouds/cv_scaler.h"
26 #include "clouds/drivers/codec.h"
27 #include "clouds/drivers/debug_pin.h"
28 #include "clouds/drivers/debug_port.h"
29 #include "clouds/drivers/system.h"
30 #include "clouds/drivers/version.h"
31 #include "clouds/dsp/granular_processor.h"
32 #include "clouds/meter.h"
33 #include "clouds/resources.h"
34 #include "clouds/settings.h"
35 #include "clouds/ui.h"
36 
37 // #define PROFILE_INTERRUPT 1
38 
39 using namespace clouds;
40 using namespace stmlib;
41 
42 GranularProcessor processor;
43 Codec codec;
44 DebugPort debug_port;
45 CvScaler cv_scaler;
46 Meter meter;
47 Settings settings;
48 Ui ui;
49 
50 // Pre-allocate big blocks in main memory and CCM. No malloc here.
51 uint8_t block_mem[118784];
52 uint8_t block_ccm[65536 - 128] __attribute__ ((section (".ccmdata")));
53 
54 int __errno;
55 
56 // Default interrupt handlers.
57 extern "C" {
58 
NMI_Handler()59 void NMI_Handler() { }
HardFault_Handler()60 void HardFault_Handler() { while (1); }
MemManage_Handler()61 void MemManage_Handler() { while (1); }
BusFault_Handler()62 void BusFault_Handler() { while (1); }
UsageFault_Handler()63 void UsageFault_Handler() { while (1); }
SVC_Handler()64 void SVC_Handler() { }
DebugMon_Handler()65 void DebugMon_Handler() { }
PendSV_Handler()66 void PendSV_Handler() { }
67 
68 }
69 
70 extern "C" {
71 
SysTick_Handler()72 void SysTick_Handler() {
73   ui.Poll();
74   if (settings.freshly_baked()) {
75     if (debug_port.readable()) {
76       uint8_t command = debug_port.Read();
77       uint8_t response = ui.HandleFactoryTestingRequest(command);
78       debug_port.Write(response);
79     }
80   }
81 }
82 
83 }
84 
FillBuffer(Codec::Frame * input,Codec::Frame * output,size_t n)85 void FillBuffer(Codec::Frame* input, Codec::Frame* output, size_t n) {
86 #ifdef PROFILE_INTERRUPT
87   TIC
88 #endif  // PROFILE_INTERRUPT
89   cv_scaler.Read(processor.mutable_parameters());
90   processor.Process((ShortFrame*)input, (ShortFrame*)output, n);
91   meter.Process(processor.parameters().freeze ? output : input, n);
92 #ifdef PROFILE_INTERRUPT
93   TOC
94 #endif  // PROFILE_INTERRUPT
95 }
96 
Init()97 void Init() {
98   System sys;
99   Version version;
100 
101   sys.Init(true);
102   version.Init();
103 
104   // Init granular processor.
105   processor.Init(
106       block_mem, sizeof(block_mem),
107       block_ccm, sizeof(block_ccm));
108 
109   settings.Init();
110   cv_scaler.Init(settings.mutable_calibration_data());
111   meter.Init(32000);
112   ui.Init(&settings, &cv_scaler, &processor, &meter);
113 
114   bool master = !version.revised();
115   if (!codec.Init(master, 32000)) {
116     ui.Panic();
117   }
118   if (!codec.Start(32, &FillBuffer)) {
119     ui.Panic();
120   }
121   if (settings.freshly_baked()) {
122 #ifdef PROFILE_INTERRUPT
123     DebugPin::Init();
124 #else
125     debug_port.Init();
126 #endif  // PROFILE_INTERRUPT
127   }
128   sys.StartTimers();
129 }
130 
main(void)131 int main(void) {
132   Init();
133   while (1) {
134     ui.DoEvents();
135     processor.Prepare();
136   }
137 }
138