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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
* i2stest.c
*
* Mon Sep 1 20:25:48 CEST 2014
* Copyright 2014 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of Pedal2Metal.
*
* Pedal2Metal 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 2 of the License, or
* (at your option) any later version.
*
* Pedal2Metal 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 Pedal2Metal; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include <wm8523.h>
#include <i2s.h>
#include <led.h>
//#include <LPC17xx.h>
#include <cli.h>
#include "../../src/sample.h"
unsigned int size = sizeof(samples) / sizeof(short);
#include "../test.h"
#if 0
void I2S_IRQHandler (void)
{
static int16_t cnt = 0;
for(int i = 0; i < 6; i++) {
//if(1 || (cnt % 24000) == 0) led_toggle();
i2s_write_pcm_16_stereo(cnt, cnt);
cnt++;
}
/*
uint32_t RxCount = 0;
if ( LPC_I2S->I2SSTATE & 0x01 )
{
RxCount = (LPC_I2S->I2SSTATE >> 8) & 0xFF;
if ( (RxCount != RXFIFO_EMPTY) && !I2SRXDone )
{
while ( RxCount > 0 )
{
if ( I2SReadLength == BUFSIZE )
{
LPC_I2S->I2SDAI |= ((0x01 << 3) | (0x01 << 4));
LPC_I2S->I2SIRQ &= ~(0x01 << 0); // Disable RX
I2SRXDone = 1;
break;
}
else
{
I2SRXBuffer[I2SReadLength++] = LPC_I2S->I2SRXFIFO;
}
RxCount--;
}
}
}
return;
*/
}
#endif
int main (void)
{
led_init();
cli_init();
/////////// WM8523
uint8_t portnum = 0;
wm8523_init(portnum, WM8523_FS_48K);
if(wm8523_get_chip_id(portnum) != WM8523_CHIP_ID) error();
wm8523_reset_registers(portnum);
wm8523_power_mode_t pwr = WM8523_PWR_POWER_UP_TO_UNMUTE;
wm8523_set_power_mode(portnum, pwr);
wm8523_aif_ctrl1_t ctl1;
ctl1.fmt = WM8523_FMT_I2S;
ctl1.wlen = WM8523_WLEN_16;
ctl1.invctl = WM8523_INVCTL_SLAVE_RISING;
ctl1.lrclkinvctl = WM8523_LRCLKINVCTL_SLAVE_NORMAL;
ctl1.modesel = WM8523_MODESEL_SLAVE;
ctl1.deemp = WM8523_DEEMPH_ENABLED;
wm8523_set_aif_ctrl1(portnum, ctl1);
wm8523_aif_ctrl2_t ctl2;
ctl2.clkratio = WM8523_CLKRATIO_AUTO;
ctl2.clkdiv = WM8523_CLKDIV_MCLK_4;
ctl2.mix = WM8523_MIX_STEREO;
wm8523_set_aif_ctrl2(portnum, ctl2);
wm8523_dac_ctrl3_t ctl3;
ctl3.downramp = WM8523_VOL_DOWN_INSTANT;
ctl3.upramp = WM8523_VOL_UP_INSTANT;
ctl3.lmute = WM8523_DACL_UNMUTE;
ctl3.rmute = WM8523_DACR_UNMUTE;
ctl3.dac_zc = WM8523_ZERO_CROSSING_DISABLED;
wm8523_set_dac_ctrl3(portnum, ctl3);
int res = i2s_clkcalc_init(48000, 16, 2);
/*
// From: tools/clkcalc 48000 16 2
int pclkdiv = 8;
int bitrate = 3;
int x = 57;
int y = 59;
// err = 16.949153 bits/second
int bitwidth = 16;
int channels = 2;
int res = i2s_init(pclkdiv, bitrate, x, y, bitwidth, channels);
*/
// if(res) error();
(void)res;
i2s_set_tx_mode_control(CLK_TX_SRC, 0, 1);
/*
//////// I2S
#if 1
// From: tools/clkcalc 48000 16 2
int pclkdiv = 8;
int bitrate = 1;
int x = 29;
int y = 59;
// err = 16.949153 bits/second
int bitwidth = 16;
int channels = 2;
int res = i2s_init(pclkdiv, bitrate, x, y, bitwidth, channels);
if(res) error();
// I2SInit();
i2s_set_tx_mode_control(CLK_TX_SRC, 0, 1);
// Set up IRQ
uint32_t *i2sirq = (uint32_t*)0x400A801C;
*i2sirq &= ~(0b1111 << 16); // Reset tx irq depth
int depth = 2;
*i2sirq |= ((depth & 0b1111) << 16); // Set tx irq depth
*i2sirq |= 0b1 << 1; // Enable TX IRQ, pg. 479 table 412
i2s_tx_start();
#else
// From: tools/clkcalc 48000 16 2
int pclkdiv = 8;
int bitrate = 1;
int x = 29;
int y = 59;
// err = 16.949153 bits/second
int bitwidth = 16;
int channels = 2;
int res = i2s_init(pclkdiv, bitrate, x, y, bitwidth, channels);
if(res) error();
// Set up IRQ
uint32_t *i2sirq = (uint32_t*)0x400A801C;
*i2sirq |= 0b1 << 0; // Enable IRQ, pg. 479 table 412
*i2sirq &= ~(0b1111 << 16); // Reset tx irq depth
int depth = 2;
*i2sirq |= ((depth & 0b1111) << 16); // Set tx irq depth
NVIC_EnableIRQ(I2S_IRQn); // I2S_IRQn = 27;
i2s_tx_reset_fifo();
i2s_tx_start();
#endif
*/
i2s_tx_start();
// int32_t *fifo = (int32_t*)0x400A8008;
unsigned int cnt = 0;
while(1) {
while(i2s_get_state_tx_level() > 7) {}
// i2s_write_pcm_16_stereo(cnt, cnt);
// *fifo = cnt;
// *fifo = (cnt / 40) | (cnt / 40) << 16;
if(cnt < size) {
i2s_write_pcm_16_stereo((samples[cnt] - 485) * 10,
(samples[cnt] - 485) * 10);
} else {
// repeat last sample
i2s_write_pcm_16_stereo(0, 0);
}
if(cnt > size * 30) cnt = 0;
// if(cnt > 64000) cnt = 0;
cnt++;
}
}
|