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
#include "embiosapp.h"


void main();
EMBIOS_APP_HEADER("Audiotest", 0x4000, main, 127)


#define INTMOD *((volatile uint32_t*)(0x39C00004))
#define INTMSK *((volatile uint32_t*)(0x39C00008))
#define SRCPND *((volatile uint32_t*)(0x39C00000))
#define INTPND *((volatile uint32_t*)(0x39C00010))
#define PCON5 *((volatile uint32_t*)(0x3CF00050))
#define PCON6 *((volatile uint32_t*)(0x3CF00060))
#define CLKCON *((volatile uint32_t*)(0x3C500000))
#define PLL1PMS *((volatile uint32_t*)(0x3C500008))
#define PLL2PMS *((volatile uint32_t*)(0x3C50000C))
#define PLL1LCNT *((volatile uint32_t*)(0x3C500018))
#define PLL2LCNT *((volatile uint32_t*)(0x3C50001C))
#define PLLLOCK *((volatile uint32_t*)(0x3C500020))
#define PLLCON *((volatile uint32_t*)(0x3C500024))
#define PWRCON *((volatile uint32_t*)(0x3C500028))
#define DMABASE0 *((volatile uint32_t*)(0x38400000))
#define DMACON0 *((volatile uint32_t*)(0x38400004))
#define DMATCNT0 *((volatile uint32_t*)(0x38400008))
#define DMACADDR0 *((volatile uint32_t*)(0x3840000C))
#define DMACTCNT0 *((volatile uint32_t*)(0x38400010))
#define DMACOM0 *((volatile uint32_t*)(0x38400014))
#define DMABASE1 *((volatile uint32_t*)(0x38400020))
#define DMACON1 *((volatile uint32_t*)(0x38400024))
#define DMATCNT1 *((volatile uint32_t*)(0x38400028))
#define DMACADDR1 *((volatile uint32_t*)(0x3840002C))
#define DMACTCNT1 *((volatile uint32_t*)(0x38400030))
#define DMACOM1 *((volatile uint32_t*)(0x38400034))
#define DMABASE2 *((volatile uint32_t*)(0x38400040))
#define DMACON2 *((volatile uint32_t*)(0x38400044))
#define DMATCNT2 *((volatile uint32_t*)(0x38400048))
#define DMACADDR2 *((volatile uint32_t*)(0x3840004C))
#define DMACTCNT2 *((volatile uint32_t*)(0x38400050))
#define DMACOM2 *((volatile uint32_t*)(0x38400054))
#define DMAALLST *((volatile uint32_t*)(0x38400100))
#define I2SCLKCON *((volatile uint32_t*)(0x3CA00000))
#define I2STXCON *((volatile uint32_t*)(0x3CA00004))
#define I2STXCOM *((volatile uint32_t*)(0x3CA00008))
#define I2SRXCON *((volatile uint32_t*)(0x3CA00030))
#define I2SRXCOM *((volatile uint32_t*)(0x3CA00034))
#define I2SSTATUS *((volatile uint32_t*)(0x3CA0003C))


uint32_t dataptr;
#define CHUNKSIZE 4096


void wmcodec_write(int reg, int data)
{
    unsigned char d = data & 0xff;
    i2c_sendbyte(0, 0x34, (reg << 1) | ((data & 0x100) >> 8), d & 0xff);
}

void dmahandler()
{
    DMACOM0 = 7;
    DMABASE0 = dataptr;
    dataptr += CHUNKSIZE * 4;
    DMATCNT0 = CHUNKSIZE * 2 - 1;
    DMACOM0 = 4;
}

void main()
{
    cputs(3, "Preparing...\n");
    dataptr = 0x08100000;
    PLLCON = (PLLCON & 0x2f) | 4;
    PLL1PMS = 0x22903;
    PLL1LCNT = 0x4d2;
    while ((PLLLOCK & 2) == 0) sleep(0);
    CLKCON = (CLKCON & ~0xff) | 0x53;
    clockgate_enable(6, true);
    PCON5 = (PCON5 & ~0xFFFF0000) | 0x22220000;
    PCON6 = (PCON6 & ~0x0F000000) | 0x02000000;
    I2SCLKCON = 0;
    wmcodec_write(0xf, 0x1ff);
    wmcodec_write(0xf, 0);
    wmcodec_write(0x19, 0xfc);
    wmcodec_write(0x1a, 0x1f8);
    sleep(20000);
    wmcodec_write(0x5, 0x4);
    wmcodec_write(0x7, 0x42);
    wmcodec_write(0x17, 0);
    wmcodec_write(0x18, 0);
    wmcodec_write(0x1b, 00);
    wmcodec_write(0xa, 0x1ff);
    wmcodec_write(0xb, 0x1ff);
    wmcodec_write(0x43, 0x8);
    wmcodec_write(0x17, 0xc0);
    wmcodec_write(0x18, 0);
    wmcodec_write(0x1b, 0);
    wmcodec_write(0x8, 0);
    wmcodec_write(0xa, 0xff);
    wmcodec_write(0xa, 0xff);
    wmcodec_write(0x22, 0x150);
    wmcodec_write(0x23, 0x50);
    wmcodec_write(0x24, 0x50);
    wmcodec_write(0x25, 0x150);
    wmcodec_write(0x26, 0);
    wmcodec_write(0x27, 0);
    wmcodec_write(0x2, 0x177);
    wmcodec_write(0x3, 0x177);
    wmcodec_write(0x5, 0);
    wmcodec_write(0x43, 0x8);
    wmcodec_write(0x2, 0x167);
    wmcodec_write(0x3, 0x167);
    wmcodec_write(0x29, 0x179);
    wmcodec_write(0x28, 0x179);
    PCON5 = (PCON5 & ~0xFFFF0000) | 0x77720000;
    PCON6 = (PCON6 & ~0x0F000000) | 0x02000000;
    I2STXCON = 0x100511;
    I2SCLKCON = 1;
    I2STXCOM = 0xe;
    int_dma_set_handler(0, dmahandler);
    DMACON0 = 0x20420000;
    DMABASE0 = dataptr;
    dataptr += CHUNKSIZE * 4;
    DMATCNT0 = CHUNKSIZE * 2 - 1;
    DMACOM0 = 4;
    cputs(3, "Playing...\n");
}