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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
#include <stdlib.h>
#include <stdint.h>
#include "encoding.h"
#include "platform.h"
#include "prci_driver.h"
#include "eos.h"
#include "event.h"
#include "interrupt.h"
#include "board.h"
#include "dev/eve.h"
#include "uart.h"
#include "i2s.h"
#include "i2s_priv.h"
#define I2S_REG_CK(o) _REG32(I2S_CTRL_ADDR_CK, o)
#define I2S_REG_WS(o) _REG32(I2S_CTRL_ADDR_WS, o)
#define I2S_REG_SR_SEL(o) _REG32(I2S_CTRL_ADDR_SR_SEL, o)
#define I2S_PIN_PWM ((1 << I2S_PIN_CK) | (1 << I2S_PIN_SR_SEL) | (1 << I2S_PIN_WS_MIC) | (1 << I2S_PIN_WS_SPK))
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
#define EOS_ABUF_IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1))
static eos_i2s_handler_t i2s_mic_handler = NULL;
static eos_i2s_handler_t i2s_spk_handler = NULL;
static uint32_t i2s_clk_period;
static uint8_t i2s_mic_volume = 0; /* 0 - 8 */
static uint8_t i2s_spk_volume = 16; /* 0 - 16 */
EOSABuf _eos_i2s_mic_buf;
EOSABuf _eos_i2s_spk_buf;
uint32_t _eos_i2s_drvr[] = {
EOS_I2S_FMT_PCM16, /* I2S_FMT */
EOS_I2S_MODE_STEREO, /* I2S_MODE */
0, /* I2S_MIC_WM */
0, /* I2S_SPK_WM */
0, /* I2S_MIC_EVT */
0, /* I2S_SPK_EVT */
};
#define I2S_FMT 0
#define I2S_MODE 1
#define I2S_MIC_WM 2
#define I2S_SPK_WM 3
#define I2S_MIC_EVT 4
#define I2S_SPK_EVT 5
static void _abuf_init(EOSABuf *buf, uint8_t *array, uint16_t size) {
buf->idx_r = 0;
buf->idx_w = 0;
buf->size = size;
buf->array = array;
}
static int _abuf_push8(EOSABuf *buf, uint8_t sample) {
if ((uint16_t)(buf->idx_w - buf->idx_r) == buf->size) return EOS_ERR_FULL;
buf->array[EOS_ABUF_IDX_MASK(buf->idx_w, buf->size)] = sample;
buf->idx_w++;
return EOS_OK;
}
static int _abuf_push16(EOSABuf *buf, uint16_t sample) {
if ((uint16_t)(buf->idx_w - buf->idx_r) == buf->size) return EOS_ERR_FULL;
buf->array[EOS_ABUF_IDX_MASK(buf->idx_w, buf->size)] = sample >> 8;
buf->array[EOS_ABUF_IDX_MASK(buf->idx_w + 1, buf->size)] = sample & 0xFF;
buf->idx_w += 2;
return EOS_OK;
}
static int _abuf_pop8(EOSABuf *buf, uint8_t *sample) {
if (buf->idx_r == buf->idx_w) {
return EOS_ERR_EMPTY;
} else {
*sample = buf->array[EOS_ABUF_IDX_MASK(buf->idx_r, buf->size)];
buf->idx_r++;
return EOS_OK;
}
}
static int _abuf_pop16(EOSABuf *buf, uint16_t *sample) {
if (buf->idx_r == buf->idx_w) {
return EOS_ERR_EMPTY;
} else {
*sample = buf->array[EOS_ABUF_IDX_MASK(buf->idx_r, buf->size)] << 8;
*sample |= buf->array[EOS_ABUF_IDX_MASK(buf->idx_r + 1, buf->size)];
buf->idx_r += 2;
return EOS_OK;
}
}
static void _abuf_flush(EOSABuf *buf) {
buf->idx_r = 0;
buf->idx_w = 0;
}
static uint16_t _abuf_len(EOSABuf *buf) {
return buf->idx_w - buf->idx_r;
}
static void i2s_handle_evt(unsigned char type, unsigned char *buffer, uint16_t len) {
switch(type & ~EOS_EVT_MASK) {
case EOS_I2S_ETYPE_MIC:
if (i2s_mic_handler) {
i2s_mic_handler(type);
clear_csr(mstatus, MSTATUS_MIE);
_eos_i2s_drvr[I2S_MIC_EVT] = 1;
set_csr(mstatus, MSTATUS_MIE);
}
break;
case EOS_I2S_ETYPE_SPK:
if (i2s_spk_handler) {
i2s_spk_handler(type);
clear_csr(mstatus, MSTATUS_MIE);
_eos_i2s_drvr[I2S_SPK_EVT] = 1;
set_csr(mstatus, MSTATUS_MIE);
}
break;
default:
eos_evtq_bad_handler(type, buffer, len);
break;
}
}
static void i2s_set_cmp(void) {
int spk_ws_offset = i2s_spk_volume - 16 + i2s_mic_volume;
/* adjust spk ws relative to mic ws */
if (spk_ws_offset <= 0) {
spk_ws_offset += 32;
GPIO_REG(GPIO_OUTPUT_XOR) &= ~(1 << I2S_PIN_WS_SPK);
} else {
GPIO_REG(GPIO_OUTPUT_XOR) |= (1 << I2S_PIN_WS_SPK);
}
I2S_REG_WS(PWM_CMP2) = spk_ws_offset * i2s_clk_period - i2s_clk_period / 2;
I2S_REG_WS(PWM_CMP3) = (32 + spk_ws_offset) * i2s_clk_period - i2s_clk_period / 2;
I2S_REG_SR_SEL(PWM_CMP1) = (1 + i2s_mic_volume) * i2s_clk_period;
I2S_REG_SR_SEL(PWM_CMP2) = (17 + i2s_mic_volume) * i2s_clk_period - (i2s_clk_period / 4);
}
extern void _eos_i2s_start_pwm(void);
int eos_i2s_init(uint8_t wakeup_cause) {
eos_evtq_set_handler(EOS_EVT_I2S, i2s_handle_evt);
I2S_REG_CK(PWM_CFG) = 0;
I2S_REG_WS(PWM_CFG) = 0;
I2S_REG_SR_SEL(PWM_CFG) = 0;
GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << I2S_PIN_SR_SEL);
GPIO_REG(GPIO_OUTPUT_VAL) &= ~(1 << I2S_PIN_SR_CK);
GPIO_REG(GPIO_OUTPUT_EN) |= (1 << I2S_PIN_SR_SEL);
GPIO_REG(GPIO_OUTPUT_EN) |= (1 << I2S_PIN_SR_CK);
GPIO_REG(GPIO_INPUT_EN) |= (1 << I2S_PIN_SD_IN);
return EOS_OK;
}
void eos_i2s_start(uint32_t sample_rate) {
uint32_t iof_mask;
i2s_clk_period = PRCI_get_cpu_freq() / (sample_rate * 64);
i2s_clk_period = (i2s_clk_period & ~I2S_PWM_SCALE_CK_MASK) + 1;
I2S_REG_CK(PWM_CMP0) = i2s_clk_period >> I2S_PWM_SCALE_CK;
I2S_REG_CK(PWM_CMP1) = I2S_REG_CK(PWM_CMP0) / 2;
I2S_REG_CK(PWM_CMP2) = 0;
I2S_REG_CK(PWM_CMP3) = 0;
I2S_REG_WS(PWM_CMP0) = i2s_clk_period * 64 - 1;
I2S_REG_WS(PWM_CMP1) = i2s_clk_period * 32;
I2S_REG_SR_SEL(PWM_CMP0) = i2s_clk_period * 32 - 1;
I2S_REG_SR_SEL(PWM_CMP3) = 0;
i2s_set_cmp();
I2S_REG_CK(PWM_COUNT) = 0;
I2S_REG_WS(PWM_COUNT) = 0;
I2S_REG_SR_SEL(PWM_COUNT) = 0;
eos_eve_intr_disable();
eos_uart_disable();
GPIO_REG(GPIO_INPUT_EN) |= (1 << I2S_PIN_INT);
GPIO_REG(GPIO_FALL_IE) |= (1 << I2S_PIN_INT);
GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << I2S_PIN_SR_CK);
iof_mask = I2S_PIN_PWM;
if (_eos_i2s_mic_buf.size == 0) {
iof_mask &= ~(1 << I2S_PIN_WS_MIC);
}
if (_eos_i2s_spk_buf.size == 0) {
iof_mask &= ~(1 << I2S_PIN_WS_SPK);
}
GPIO_REG(GPIO_IOF_SEL) |= I2S_PIN_PWM;
GPIO_REG(GPIO_IOF_EN) |= iof_mask;
eos_intr_set_priority(I2S_IRQ_SD_ID, IRQ_PRIORITY_I2S_SD);
eos_intr_enable(I2S_IRQ_SD_ID);
_eos_i2s_start_pwm();
/*
I2S_REG_CK(PWM_CFG) = PWM_CFG_ENALWAYS | PWM_CFG_ZEROCMP | I2S_PWM_SCALE_CK;
I2S_REG_WS(PWM_CFG) = PWM_CFG_ENALWAYS | PWM_CFG_ZEROCMP | PWM_CFG_CMP2GANG;
I2S_REG_SR_SEL(PWM_CFG) = PWM_CFG_ENALWAYS | PWM_CFG_ZEROCMP | PWM_CFG_CMP1GANG;
*/
}
void eos_i2s_stop(void) {
I2S_REG_CK(PWM_CFG) = 0;
I2S_REG_WS(PWM_CFG) = 0;
I2S_REG_SR_SEL(PWM_CFG) = 0;
I2S_REG_CK(PWM_COUNT) = 0;
I2S_REG_WS(PWM_COUNT) = 0;
I2S_REG_SR_SEL(PWM_COUNT) = 0;
eos_intr_disable(I2S_IRQ_SD_ID);
GPIO_REG(GPIO_FALL_IE) &= ~(1 << I2S_PIN_INT);
GPIO_REG(GPIO_INPUT_EN) &= ~(1 << I2S_PIN_INT);
GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << I2S_PIN_SR_SEL);
GPIO_REG(GPIO_OUTPUT_VAL) &= ~(1 << I2S_PIN_SR_CK);
GPIO_REG(GPIO_OUTPUT_XOR) &= ~(1 << I2S_PIN_WS_SPK);
GPIO_REG(GPIO_IOF_EN) &= ~I2S_PIN_PWM;
eos_uart_enable();
eos_eve_intr_enable();
_eos_i2s_drvr[I2S_MIC_EVT] = 0;
_eos_i2s_drvr[I2S_SPK_EVT] = 0;
_eos_i2s_drvr[I2S_MIC_WM] = 0;
_eos_i2s_drvr[I2S_SPK_WM] = 0;
i2s_mic_handler = NULL;
i2s_spk_handler = NULL;
_abuf_init(&_eos_i2s_mic_buf, NULL, 0);
_abuf_init(&_eos_i2s_spk_buf, NULL, 0);
}
int eos_i2s_running(void) {
return !!(GPIO_REG(GPIO_IOF_EN) & (1 << I2S_PIN_CK));
}
void eos_i2s_set_fmt(unsigned char fmt) {
_eos_i2s_drvr[I2S_FMT] = fmt;
}
void eos_i2s_set_mode(unsigned char mode) {
_eos_i2s_drvr[I2S_MODE] = mode;
}
void eos_i2s_mic_set_handler(eos_i2s_handler_t handler, uint16_t wm) {
clear_csr(mstatus, MSTATUS_MIE);
if ((i2s_mic_handler == NULL) && handler) _eos_i2s_drvr[I2S_MIC_EVT] = 1;
if (i2s_mic_handler && (handler == NULL)) _eos_i2s_drvr[I2S_MIC_EVT] = 0;
_eos_i2s_drvr[I2S_MIC_WM] = wm;
i2s_mic_handler = handler;
set_csr(mstatus, MSTATUS_MIE);
}
void eos_i2s_mic_set_buf(uint8_t *mic_arr, uint16_t mic_arr_size) {
int run = eos_i2s_running();
clear_csr(mstatus, MSTATUS_MIE);
_abuf_init(&_eos_i2s_mic_buf, mic_arr, mic_arr_size);
if (run) {
if (mic_arr_size == 0) {
GPIO_REG(GPIO_IOF_EN) &= ~(1 << I2S_PIN_WS_MIC);
} else {
GPIO_REG(GPIO_IOF_EN) |= (1 << I2S_PIN_WS_MIC);
}
}
set_csr(mstatus, MSTATUS_MIE);
}
uint8_t *eos_i2s_mic_get_buf(void) {
return _eos_i2s_mic_buf.array;
}
uint16_t eos_i2s_mic_len(void) {
uint16_t ret;
clear_csr(mstatus, MSTATUS_MIE);
ret = _abuf_len(&_eos_i2s_mic_buf);
set_csr(mstatus, MSTATUS_MIE);
return ret;
}
uint16_t eos_i2s_mic_read(uint8_t *sample, uint16_t ssize) {
uint16_t i;
uint16_t _ssize = 0;
clear_csr(mstatus, MSTATUS_MIE);
_ssize = MIN(ssize, _abuf_len(&_eos_i2s_mic_buf));
set_csr(mstatus, MSTATUS_MIE);
for (i=0; i<_ssize; i++) {
sample[i] = _eos_i2s_mic_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_mic_buf.idx_r + i, _eos_i2s_mic_buf.size)];
}
clear_csr(mstatus, MSTATUS_MIE);
_eos_i2s_mic_buf.idx_r += _ssize;
set_csr(mstatus, MSTATUS_MIE);
return _ssize;
}
int eos_i2s_mic_pop8(uint8_t *sample) {
int ret;
clear_csr(mstatus, MSTATUS_MIE);
ret = _abuf_pop8(&_eos_i2s_mic_buf, sample);
set_csr(mstatus, MSTATUS_MIE);
return ret;
}
int eos_i2s_mic_pop16(uint16_t *sample) {
int ret;
clear_csr(mstatus, MSTATUS_MIE);
ret = _abuf_pop16(&_eos_i2s_mic_buf, sample);
set_csr(mstatus, MSTATUS_MIE);
return ret;
}
int eos_i2s_mic_get_vol(void) {
return i2s_mic_volume;
}
void eos_i2s_mic_set_vol(int vol) {
if ((vol < 0) || (vol > 8)) return;
i2s_mic_volume = vol;
clear_csr(mstatus, MSTATUS_MIE);
i2s_set_cmp();
set_csr(mstatus, MSTATUS_MIE);
}
void eos_i2s_spk_set_handler(eos_i2s_handler_t handler, uint16_t wm) {
clear_csr(mstatus, MSTATUS_MIE);
if ((i2s_spk_handler == NULL) && handler) _eos_i2s_drvr[I2S_SPK_EVT] = 1;
if (i2s_spk_handler && (handler == NULL)) _eos_i2s_drvr[I2S_SPK_EVT] = 0;
_eos_i2s_drvr[I2S_SPK_WM] = wm;
i2s_spk_handler = handler;
set_csr(mstatus, MSTATUS_MIE);
}
void eos_i2s_spk_set_buf(uint8_t *spk_arr, uint16_t spk_arr_size) {
int run = eos_i2s_running();
clear_csr(mstatus, MSTATUS_MIE);
_abuf_init(&_eos_i2s_spk_buf, spk_arr, spk_arr_size);
if (run) {
if (spk_arr_size == 0) {
GPIO_REG(GPIO_IOF_EN) &= ~(1 << I2S_PIN_WS_SPK);
} else {
GPIO_REG(GPIO_IOF_EN) |= (1 << I2S_PIN_WS_SPK);
}
}
set_csr(mstatus, MSTATUS_MIE);
}
uint8_t *eos_i2s_spk_get_buf(void) {
return _eos_i2s_spk_buf.array;
}
uint16_t eos_i2s_spk_len(void) {
uint16_t ret;
clear_csr(mstatus, MSTATUS_MIE);
ret = _abuf_len(&_eos_i2s_spk_buf);
set_csr(mstatus, MSTATUS_MIE);
return ret;
}
uint16_t eos_i2s_spk_write(uint8_t *sample, uint16_t ssize, uint8_t transform) {
uint16_t i;
uint16_t abuf_free;
uint8_t transform_l, transform_r;
clear_csr(mstatus, MSTATUS_MIE);
abuf_free = _eos_i2s_spk_buf.size - _abuf_len(&_eos_i2s_spk_buf);
if (transform) abuf_free = abuf_free / 2;
ssize = MIN(ssize, abuf_free);
set_csr(mstatus, MSTATUS_MIE);
transform_l = (transform == EOS_I2S_TRANS_MONO2D) || (transform == EOS_I2S_TRANS_MONO2L);
transform_r = (transform == EOS_I2S_TRANS_MONO2D) || (transform == EOS_I2S_TRANS_MONO2R);
if (transform) {
if (_eos_i2s_drvr[I2S_FMT] == EOS_I2S_FMT_PCM16) {
for (i=0; i<ssize / 2; i++) {
_eos_i2s_spk_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_spk_buf.idx_w + i * 4, _eos_i2s_spk_buf.size)] = transform_l ? sample[i * 2] : 0;
_eos_i2s_spk_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_spk_buf.idx_w + i * 4 + 2, _eos_i2s_spk_buf.size)] = transform_r ? sample[i * 2] : 0;
_eos_i2s_spk_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_spk_buf.idx_w + i * 4 + 1, _eos_i2s_spk_buf.size)] = transform_l ? sample[i * 2 + 1] : 0;
_eos_i2s_spk_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_spk_buf.idx_w + i * 4 + 3, _eos_i2s_spk_buf.size)] = transform_r ? sample[i * 2 + 1] : 0;
}
} else {
for (i=0; i<ssize; i++) {
_eos_i2s_spk_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_spk_buf.idx_w + i * 2, _eos_i2s_spk_buf.size)] = transform_l ? sample[i] : 0;
_eos_i2s_spk_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_spk_buf.idx_w + i * 2 + 1, _eos_i2s_spk_buf.size)] = transform_r ? sample[i] : 0;
}
}
} else {
for (i=0; i<ssize; i++) {
_eos_i2s_spk_buf.array[EOS_ABUF_IDX_MASK(_eos_i2s_spk_buf.idx_w + i, _eos_i2s_spk_buf.size)] = sample[i];
}
}
if (transform) ssize *= 2;
clear_csr(mstatus, MSTATUS_MIE);
_eos_i2s_spk_buf.idx_w += ssize;
set_csr(mstatus, MSTATUS_MIE);
return ssize;
}
int eos_i2s_spk_push8(uint8_t sample) {
int ret;
clear_csr(mstatus, MSTATUS_MIE);
ret = _abuf_push8(&_eos_i2s_spk_buf, sample);
set_csr(mstatus, MSTATUS_MIE);
return ret;
}
int eos_i2s_spk_push16(uint16_t sample) {
int ret;
clear_csr(mstatus, MSTATUS_MIE);
ret = _abuf_push16(&_eos_i2s_spk_buf, sample);
set_csr(mstatus, MSTATUS_MIE);
return ret;
}
int eos_i2s_spk_get_vol(void) {
return i2s_spk_volume;
}
void eos_i2s_spk_set_vol(int vol) {
if ((vol < 0) || (vol > 16)) return;
i2s_spk_volume = vol;
clear_csr(mstatus, MSTATUS_MIE);
i2s_set_cmp();
set_csr(mstatus, MSTATUS_MIE);
}
|