-
Notifications
You must be signed in to change notification settings - Fork 82
/
main.cpp
executable file
·185 lines (154 loc) · 3.77 KB
/
main.cpp
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
#include <library.h>
#include <stm32f10x.h>
// TODO: cleanup code
typedef void (*THandler)(void);
extern "C"
{
void _Initialize(void* pDeviceInfo, void* pDevice, void* pDeviceProperty, void* pUserStandardRequests,
THandler arrHandlerIn[], THandler arrHandlerOut[], THandler callbacks[], THandler pLeaveLowPowerMode)
{
BIOS::USB::Initialize(pDeviceInfo, pDevice, pDeviceProperty, pUserStandardRequests, arrHandlerIn, arrHandlerOut, callbacks, pLeaveLowPowerMode);
}
void InitializeFinish(int imr_msk)
{
BIOS::USB::InitializeFinish(imr_msk);
}
}
RingBuffer<uint8_t, 128> bufferReceive;
RingBuffer<uint8_t, 128> bufferTransmit;
extern "C" {
char dbgPushBuf[512];
void dbgPrint(const char* msg)
{
strcat(dbgPushBuf, msg);
}
void dbgHex(int n)
{
char buf[16];
sprintf(buf, "%02x ", n);
strcat(dbgPushBuf, buf);
}
extern int32_t MIOS32_IRQ_Disable(void);
extern int32_t MIOS32_IRQ_Enable(void);
extern void MIOS32_USB_MIDI_TxBufferHandler();
int onTransmitAvailable()
{
return bufferTransmit.size();
}
uint_fast8_t onTransmitGet()
{
return bufferTransmit.pull();
}
uint_fast8_t onTransmitPeek()
{
return bufferTransmit.peek();
}
void onReceive(uint8_t value)
{
bufferReceive.push(value);
}
bool onReceiveAvailable(int bytes)
{
return true;
}
}
extern const u8 mios32_midi_pcktype_num_bytes[16];
void processMidiData(uint_fast8_t data, void(*send)(uint_fast8_t write))
{
static int lastCommand = 0;
static int command[4];
static int pos = 0;
if (data & 0x80)
{
// we are skipping sysex values anyway...
if (data == 0xf8 || data == 0xfe)
return;
lastCommand = data;
pos = 0;
command[pos++] = data;
return;
}
if (pos == 0 && lastCommand != 0)
{
pos = 0;
command[pos++] = lastCommand;
}
if (pos > 0)
{
int required = mios32_midi_pcktype_num_bytes[lastCommand >> 4];
command[pos++] = data;
if (pos >= required)
{
//BIOS::DBG::Print("{");
for (int i=0; i<required; i++)
{
//BIOS::DBG::Print("%2x ", command[i]);
send(command[i]);
}
//BIOS::DBG::Print("}");
pos = 0;
}
}
}
extern "C" void InitUsb();
using namespace BIOS;
#ifdef _ARM
__attribute__((__section__(".entry")))
#endif
int _main(void)
{
BIOS::GPIO::PinMode(BIOS::GPIO::P1, BIOS::GPIO::Uart);
BIOS::GPIO::PinMode(BIOS::GPIO::P2, BIOS::GPIO::Uart);
BIOS::GPIO::UART::Setup(31250, BIOS::GPIO::UART::length8);
USB::Enable();
InitUsb();
KEY::EKey key;
long lastSend = 0;
while ((key = KEY::GetKey()) != KEY::Escape)
{
long current = SYS::GetTick();
if (current-lastSend > 10)
{
lastSend = current;
if (bufferTransmit.size() > 0)
MIOS32_USB_MIDI_TxBufferHandler();
}
if (BIOS::GPIO::UART::Available())
{
// disabling disables also the serial receive isr!!! we are losing data!
// MIOS32_IRQ_Disable();
while (BIOS::GPIO::UART::Available())
{
uint_fast8_t value = BIOS::GPIO::UART::Read();
processMidiData(value, [](uint_fast8_t value) {
bufferTransmit.push(value);
});
}
// MIOS32_IRQ_Enable();
}
if (bufferReceive.size())
{
MIOS32_IRQ_Disable();
while (bufferReceive.size())
{
uint_fast8_t value = bufferReceive.pull();
BIOS::GPIO::UART::Write(value);
}
MIOS32_IRQ_Enable();
}
int l = strlen(dbgPushBuf);
if (l > 0)
{
for (int i=0; i<l; i+=32)
{
char c = dbgPushBuf[i+32];
dbgPushBuf[i+32] = 0;
BIOS::DBG::Print(dbgPushBuf+i); // cannot print more than 64 chas
dbgPushBuf[i+32] = c;
}
strcpy(dbgPushBuf, "");
}
}
BIOS::USB::InitializeMass();
return 0;
}