summaryrefslogtreecommitdiffstats
path: root/bootloader/rk27xx.c
blob: d94b08034593c8d1d713214c764c86246a3d420e (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
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "inttypes.h"
#include "string.h"
#include "cpu.h"
#include "system.h"
#include "lcd.h"
#include "kernel.h"
#include "thread.h"
#include "backlight.h"
#include "backlight-target.h"
#include "font.h"
#include "common.h"
#include "version.h"

// 441 Hz samples table, 44100 Hz and 441 Hz -> 100 samples 
const int16_t samples[] = {
         0,   2057,   4106,   6139,   8148,  10125,  12062,  13951,  15785,  17557,
     19259,  20886,  22430,  23886,  25247,  26509,  27666,  28713,  29648,  30465,
     31163,  31737,  32186,  32508,  32702,  32767,  32702,  32508,  32186,  31737,
     31163,  30465,  29648,  28713,  27666,  26509,  25247,  23886,  22430,  20886,
     19259,  17557,  15785,  13951,  12062,  10125,   8148,   6139,   4106,   2057,
         0,  -2057,  -4106,  -6139,  -8148, -10125, -12062, -13951, -15785, -17557,
    -19259, -20886, -22430, -23886, -25247, -26509, -27666, -28713, -29648, -30465,
    -31163, -31737, -32186, -32508, -32702, -32767, -32702, -32508, -32186, -31737,
    -31163, -30465, -29648, -28713, -27666, -26509, -25247, -23886, -22430, -20886,
    -19259, -17557, -15785, -13951, -12062, -10125,  -8148,  -6139,  -4106,  -2057 };

extern int show_logo( void );

void INT_HDMA(void)
{
#if 0
//    static uint32_t i;
//    printf("hdma int: %d", i++);

    HDMA_ISRC0 = (uint32_t)&samples;
    HDMA_IDST0 = (uint32_t)&I2S_TXR;
    HDMA_ICNT0 = (sizeof(samples)/4) - 1;
    HDMA_CON0 = (1<<22)|  // slice mode
                (1<<21)|  // channel enable
                (1<<18)|  // interrupt mode
                (5<<13)|  // transfer mode inc8
                (6<<9) |  // hdreq from i2s tx
                (0<<7) |  // source address increment
                (1<<5) |  // destination address fixed
                (2<<3) |  // data size word
                (1<<0);   // enable hardware triggered dma

    HDMA_ISR = (1<<13) | // mask ch1 page overflow
               (1<<11) | // mask ch1 page count down
               (1<<9);   // mask ch1 interrupts
#endif
return;
}

static int codec_write(uint8_t reg, uint8_t data)
{
    uint8_t tmp = data;
    return i2c_write(0x27<<1, reg<<1, 1, &tmp);
}

void main(void)
{
    int i;

    _backlight_init();

    system_init();
    kernel_init();
    enable_irq();

    lcd_init_device();
    _backlight_on();
    font_init();
    lcd_setfont(FONT_SYSFIXED);

    show_logo();
    sleep(HZ*2);

printf("show logo passed");
    // I2S init
    SCU_CLKCFG &= ~((1<<17) | (1<<16)); // enable i2s, i2c pclk
//SCU_CLKCFG |= ((1<<17) | (1<<16));
    I2S_OPR = (1<<17) |    // reset Tx
              (1<<16) |    // reset Rx
              (1<<6)  |    // disable HDMA Req1
              (1<<5);      // disable HDMA Req2

    I2S_TXCTL = (1<<16) |  // LRCK/SCLK = 64
                (4<<8)  |  // MCLK/SCK = 4
                (1<<4)  |  // 16bit samples
                (0<<3)  |  // stereo mode
                (0<<1);    // I2S
 
    I2S_RXCTL = (1<<16) |  // LRCK/SCLK = 64
                (4<<8)  |  // MCLK/SCK = 4
                (1<<4)  |  // 16bit samples
                (0<<3)  |  // stereo mode
                (0<<1);    // I2S

    I2S_FIFOSTS = (1<<18) |  // Tx int trigger half full
                  (1<<16);   // Rx int trigger half full

    // I2S start
    I2S_OPR = (1<<17) | (1<<16);
    sleep(HZ/100);

    I2S_OPR = (0<<6) | // req channel 1 enable                               
              (1<<5) | // req channel 2 disable
              (0<<4) | // HDMA req channel 1 Tx
              (0<<2) | // normal I2S operation (no loopback)
              (1<<1);  // Tx start

printf("I2S config passed");

    HDMA_ISRC0 = (uint32_t)&samples;
    HDMA_IDST0 = (uint32_t)&I2S_TXR;
    HDMA_ICNT0 = (sizeof(samples)/4) - 1;
    HDMA_ISCNT0 = 7;
    HDMA_IPNCNTD0 = 1;
    HDMA_CON0 = (1<<22)|  // slice mode
                (1<<21)|  // channel enable
                (1<<18)|  // interrupt mode
                (5<<13)|  // transfer mode inc8
                (6<<9) |  // hdreq from i2s tx
                (0<<7) |  // source address increment
                (1<<5) |  // destination address fixed
                (2<<3) |  // data size word
                (1<<0);   // enable hardware triggered dma

    HDMA_ISR = (1<<13) | // mask ch1 page overflow
               (1<<11) | // mask ch1 page count down
               (1<<9);   // mask ch1 interrupts

    INTC_IMR |= (1<<12);
    INTC_IECR |= (1<<12);

printf("HDMA config passed");

    i2c_init();

printf("I2C config passed");

    // codec init
    codec_write(0x00, (1<<3)|(1<<2)|(1<<1)|(1<<0)); // AICR
    codec_write(0x01, (1<<7)|(1<<5)|(1<<3));        // CR1
    codec_write(0x02, (1<<2));                      // CR2
    codec_write(0x03, 0);                           // CCR1
    codec_write(0x04, (2<<4)|(2<<0));               // CCR2
    codec_write(0x07, (3<<5)|(3<<0));               // CCR


    codec_write(0x0f, 0x1f|(2<<6));                 // CGR6
    codec_write(0x14, (1<<1));                      // TR1
    codec_write(0x05, (1<<6)|(1<<5)|(1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0)); // PMR1
    sleep(HZ/100);

    codec_write(0x06, (1<<3)|(1<<2)|(1<<0));               // PMR2


    codec_write(0x05, (1<<6)|(1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0));        // PMR1
    codec_write(0x05, (1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0));               // PMR1


    // DACout mode
    codec_write(0x01, (1<<7)|(1<<3)|(1<<5)|(1<<4));  // CR1
    codec_write(0x05, (1<<4)|(1<<3)|(1<<2)|(1<<1)|(1<<0)); //PMR1
//    codec_write(0x06, (1<<3)|(1<<2));                // PMR2

printf("codec init passed");

    codec_write(0x01, (1<<7)|(1<<3));         // CR1

    codec_write(0x0a, 0); // 0dB digital gain
    codec_write(0x11, 15|(2<<6)); // 

    while(1)
    {
        printf("HDMA_CCNT0: 0x%0x FIFOSTS: 0x%0x", HDMA_CCNT0, I2S_FIFOSTS);
    }
}