summaryrefslogtreecommitdiff
path: root/main.cpp
blob: c7dc04e4cf18f21c6c009d14b0419510465d5941 (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <rcc/rcc.h>
#include <gpio/pin.h>
#include <os/time.h>
#include <usb/usb.h>
#include <usb/descriptor.h>
#include <spi/spi.h>
#include <dma/dma.h>
#include <i2c/i2c.h>

Pin& dac_nreset = PD4;

Pin& i2c_scl = PB6;
Pin& i2c_sda = PB9;

Pin& i2s_mck = PC7;
Pin& i2s_sck = PC10;
Pin& i2s_sd  = PC12;
Pin& i2s_ws  = PA4;

Pin& usb_vbus = PA9;
Pin& usb_dm   = PA11;
Pin& usb_dp   = PA12;

const uint32_t audio_buf_size = 1024;
uint16_t audio_buf[audio_buf_size];
uint32_t audio_buf_pos = 0;

struct AS_General_desc {
	uint8_t bLength;
	uint8_t bDescriptorType;
	uint8_t bDescriptorSubType;
	uint8_t bTerminalLink;
	uint8_t bDelay;
	uint16_t wFormatTag;
} __attribute__((packed));

constexpr AS_General_desc as_general_desc(
		uint8_t bTerminalLink,
		uint8_t bDelay,
		uint16_t wFormatTag
	) {
	
	return {
		sizeof(AS_General_desc),
		0x24,
		0x01,
		bTerminalLink,
		bDelay,
		wFormatTag
	};
}

template <uint32_t S>
struct foo_t {
	uint8_t a[S];
} __attribute__((packed));

const foo_t<43> acd = {{
	9, 36, 1, 0,1, 43,0, 1, 1,
	12, 36, 2, 1, 1,1, 0, 2, 3,0, 0, 0,
	13, 36, 6, 13, 1, 2, 1, 2, 2, 0, 2, 0, 0,
	9, 36, 3, 3, 1,3, 0, 13, 0
}};

const foo_t<11> asfd = {{11, 36, 2, 1, 2, 2, 16, 1, 48000 & 0xff, (48000 >> 8) & 0xff, 48000 >> 16}};

const foo_t<7> aced = {{7, 37, 1, 1, 0, 0, 0}};

auto dev_desc = device_desc(0x200, 0, 0, 0, 64, 0x1234, 0x5678, 0, 0, 0, 0, 1);
auto conf_desc = configuration_desc(2, 1, 0, 0xc0, 0,
	// AudioControl
	interface_desc(0, 0, 0, 0x01, 0x01, 0x00, 0,
		acd
	),
	// AudioStreaming
	interface_desc(1, 0, 0, 0x01, 0x02, 0x00, 0),
	interface_desc(1, 1, 1, 0x01, 0x02, 0x00, 0,
		as_general_desc(1, 1, 1),
		asfd,
		endpoint_desc(0x01, 0x09, 256, 1),
		aced
	)
);

class DAC {
	Pin& nreset;
	I2C& i2c;

	public:
		DAC(Pin& _nreset, I2C& _i2c) : nreset(_nreset), i2c(_i2c) {}
		
		void init() {
			// Configure CS43L22.
			nreset.set_mode(Pin::Output);
			nreset.on();
			
			i2c.write_reg(0x4a, 0x02, 0x9e);
			i2c.write_reg(0x4a, 0x06, (0 << 7) | (1 << 6) | (0 << 4) | (1 << 2) | (3 << 0));
		}
		uint8_t get_volume(uint8_t c) {
			uint8_t buf;
			i2c.read_reg(0x4a, 0x1a + c, 1, &buf);
			return ((buf & 0x7f) - 0x19) & 0x7f;
		}
		void set_volume(uint8_t c, uint8_t v) {
			i2c.write_reg(0x4a, 0x1a + c, (v + 0x19) & 0x7f);
		}
};

DAC dac(dac_nreset, I2C1);

desc_t dev_desc_p = {sizeof(dev_desc), (void*)&dev_desc};
desc_t conf_desc_p = {sizeof(conf_desc), (void*)&conf_desc};

USB_otg usb(OTG_FS, dev_desc_p, conf_desc_p);

class USB_Audio : public USB_class_driver {
	private:
		USB_generic& usb;
		
		enum ControlState {None, VolLeft, VolRight};
		ControlState control_state;
	
	public:
		USB_Audio(USB_generic& usbd) : usb(usbd) {
			usb.register_driver(this);
		}
	
	protected:
		virtual SetupStatus handle_setup(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength) {
			control_state = ControlState::None;
			
			// GET_CUR
			if(bmRequestType == 0xa1 && bRequest == 0x81) {
				uint32_t res = 0;
				usb.write(0, &res, wLength);
				return SetupStatus::Ok;
			}
			
			// GET_MIN
			if(bmRequestType == 0xa1 && bRequest == 0x82) {
				uint32_t res = 0;
				usb.write(0, &res, wLength);
				return SetupStatus::Ok;
			}
			
			// GET_MAX
			if(bmRequestType == 0xa1 && bRequest == 0x83) {
				uint32_t res = 127;
				usb.write(0, &res, wLength);
				return SetupStatus::Ok;
			}
			
			// GET_RES
			if(bmRequestType == 0xa1 && bRequest == 0x84) {
				uint32_t res = 1;
				usb.write(0, &res, wLength);
				return SetupStatus::Ok;
			}
			
			// SET_CUR
			if(bmRequestType == 0x21 && bRequest == 0x01) {
				switch(wValue) {
					case 0x0201:
						control_state = ControlState::VolLeft;
						break;
					
					case 0x0202:
						control_state = ControlState::VolRight;
						break;
				}
				
				return SetupStatus::Ok;
			}
			
			// SET_CUR
			if(bmRequestType == 0x22 && bRequest == 0x01) {
				return SetupStatus::Ok;
			}
			
			return SetupStatus::Unhandled;
		}
		
		virtual void handle_set_configuration(uint8_t configuration) {
			if(configuration) {
				usb.register_out_handler(this, 1);
				usb.hw_conf_ep(0x01, EPType::Isochronous, 256);
			}
		}
		
		virtual void handle_out(uint8_t ep, uint32_t len) {
			if(ep == 0 && len > 0) {
				uint32_t buf;
				usb.read(0, &buf, len > 4 ? 4 : len);
				
				switch(control_state) {
					case ControlState::VolLeft:
						dac.set_volume(0, buf & 0xff);
						break;
					
					case ControlState::VolRight:
						dac.set_volume(1, buf & 0xff);
						break;
					
					default:
						break;
				}
				
				usb.write(0, nullptr, 0);
				
			} else if(ep == 1) {
				usb_rblog.log("Received audio frame with %d bytes.", len);
				
				if(audio_buf_pos + (len >> 1) >= audio_buf_size) {
					usb.read(1, (uint32_t*)&audio_buf[audio_buf_pos], (audio_buf_size - audio_buf_pos) << 1);
					len -= (audio_buf_size - audio_buf_pos) << 1;
					audio_buf_pos = 0;
				}
				
				if(len) {
					usb.read(1, (uint32_t*)&audio_buf[audio_buf_pos], len);
					audio_buf_pos += len >> 1;
				}
			}
		}
};

USB_Audio usb_audio(usb);

int main() {
	// Initialize system timer.
	STK.LOAD = 168000000 / 8 / 1000; // 1000 Hz.
	STK.CTRL = 0x03;
	
	RCC.enable(RCC.GPIOA);
	RCC.enable(RCC.GPIOB);
	RCC.enable(RCC.GPIOC);
	RCC.enable(RCC.GPIOD);
	
	// Initialize I2S.
	i2s_mck.set_mode(Pin::AF);
	i2s_mck.set_af(6);
	i2s_sck.set_mode(Pin::AF);
	i2s_sck.set_af(6);
	i2s_sd.set_mode(Pin::AF);
	i2s_sd.set_af(6);
	i2s_ws.set_mode(Pin::AF);
	i2s_ws.set_af(6);
	
	RCC.PLLI2SCFGR = (3 << 28) | (258 << 6);
	RCC.CR |= 1 << 26;
	while(!(RCC.CR & (1 << 27)));
	
	RCC.enable(RCC.SPI3);
	
	SPI3.reg.I2SPR = 0x303;
	SPI3.reg.I2SCFGR = 0xe00;
	SPI3.reg.CR2 = 2;
	
	// Initialize DMA.
	RCC.enable(RCC.DMA1);
	
	DMA1.reg.S[7].CR = (0 << 25) | (1 << 13) | (1 << 11) | (1 << 10) | (1 << 8) | (1 << 6);
	DMA1.reg.S[7].PAR = uint32_t(&SPI3.reg.DR);
	DMA1.reg.S[7].M0AR = uint32_t(&audio_buf);
	DMA1.reg.S[7].NDTR = audio_buf_size;
	DMA1.reg.S[7].CR |= 1;
	
	// Initialize I2C.
	I2C1.enable(i2c_scl, i2c_sda);
	
	dac.init();
	
	// Initialize USB.
	usb_vbus.set_mode(Pin::Input);
	usb_dm.set_mode(Pin::AF);
	usb_dm.set_af(10);
	usb_dp.set_mode(Pin::AF);
	usb_dp.set_af(10);
	
	RCC.enable(RCC.OTGFS);
	
	usb.set_rxfifo_size(512);
	usb.init();
	
	while(1) {
		usb.process();
	}
}