diff options
Diffstat (limited to 'src/inc/spi')
-rwxr-xr-x | src/inc/spi/dev/audio_vs1011.c | 173 | ||||
-rwxr-xr-x | src/inc/spi/dev/audio_vs1011.h | 91 | ||||
-rwxr-xr-x | src/inc/spi/dev/display_dogm.c | 39 | ||||
-rwxr-xr-x | src/inc/spi/dev/display_dogm.h | 31 | ||||
m--------- | src/inc/spi/dev/petit | 0 | ||||
-rwxr-xr-x | src/inc/spi/spi.c | 56 | ||||
-rwxr-xr-x | src/inc/spi/spi.h | 37 |
7 files changed, 427 insertions, 0 deletions
diff --git a/src/inc/spi/dev/audio_vs1011.c b/src/inc/spi/dev/audio_vs1011.c new file mode 100755 index 0000000..ad44654 --- /dev/null +++ b/src/inc/spi/dev/audio_vs1011.c | |||
@@ -0,0 +1,173 @@ | |||
1 | #include "audio_vs1011.h" | ||
2 | /// inline void audio_vs1011_setVolume(unsigned char left, unsigned char right) | ||
3 | /// { audio_vs1011_cmd(SCI_VOL, left, right); } | ||
4 | |||
5 | void audio_vs1011_init() | ||
6 | { | ||
7 | // [initialize pins and ports] | ||
8 | DDRC |= ((1<<PC1) | (1<<PC2) | (1<<PC4)); // XCS/XDCS/RESET | ||
9 | DDRC &= ~(1<<PC3); // DREQ | ||
10 | //DDRA |= (1<<PA6); // activate O-Port A6 as [XCS] | ||
11 | //DDRC |= (1<<PC6); // activate O-Port C6 as [XDCS] | ||
12 | //DDRD |= (1<<PD6); // activate O-Port D6 as [RESET] | ||
13 | //DDRD &= ~(1<<PD5); // activate I-Port D5 as [DREQ] | ||
14 | |||
15 | |||
16 | // pins... | ||
17 | /// PORTA |= (1<<PA6); | ||
18 | /// PORTC |= (1<<PC6); | ||
19 | PORTC |= (1<<PC3); // enable pullup resistor for [DREQ] | ||
20 | audio_vs1011_exit_xcs(); // activate XCS: deactivate vs1011-cmd-line | ||
21 | audio_vs1011_exit_xdcs(); // activate XDCS: deactivate vs1011-data-line | ||
22 | |||
23 | // [reset vs1011] | ||
24 | // this ensures, that it finds XCS and XDCS in proper state (see below) | ||
25 | /// PORTD &= ~(1<<PD6); // <reset> | ||
26 | // Wait for at least 10 ms after startup (required!!) | ||
27 | // then make GPIO an output signal and set it to 1... | ||
28 | /// _delay_ms(20); | ||
29 | PORTC |= (1<<PC4); // </reset> | ||
30 | /// _delay_ms(20); | ||
31 | |||
32 | // wait until DREQ is set- so chip has rebooted from reset... | ||
33 | wait4dreq(); | ||
34 | |||
35 | // [set vs1011 to 'new-mode'] | ||
36 | // where xdcs is driven low, before you want to send mp3-data or test-bits | ||
37 | // where xcs is driven low to send SCI-commands. | ||
38 | // and both together may not been activated, why we started with resetting | ||
39 | // after having configured those output-ports. | ||
40 | audio_vs1011_cmd(SCI_MODE, (SM_SDINEW | SM_TESTS)); | ||
41 | |||
42 | // [set clock-frequency...] | ||
43 | // formular: | ||
44 | // 32768{if clock doubler} | ||
45 | // + (F_CRYSTAL{crystals frequency in Hz} / 2000{const}) | ||
46 | // = 0x9770 for a 12MHz-crystal (with clock-doubler), so send: | ||
47 | /// audio_vs1011_cmd(SCI_CLOCKF, 0x98, 0x00); | ||
48 | /// stdout_put_uint(uart_putc, ((F_CPU / 2000) | SC_CLK2X)); | ||
49 | /// stdout_put_string(uart_putc, "\t< clock freq for vs1011\n"); | ||
50 | |||
51 | audio_vs1011_cmd(SCI_CLOCKF, ((F_CPU / 2000) | SC_CLK2X)); | ||
52 | audio_vs1011_cmd(SCI_AUDATA, 44100); | ||
53 | |||
54 | /* | ||
55 | spi_setspeed(SPICLK_FAST); | ||
56 | int q; | ||
57 | for(q=0; q<=30000; q++) | ||
58 | spi_putc(0x00); | ||
59 | spi_setspeed(SPICLK_DEFAULT); | ||
60 | // wait for at least 11000 clock cycles... | ||
61 | */ | ||
62 | |||
63 | // wait until DREQ is set- so chip has rebooted from reset... | ||
64 | wait4dreq(); | ||
65 | |||
66 | |||
67 | /// spi_putc(0xFF); | ||
68 | |||
69 | audio_vs1011_setVolume(1, 1); // left and right both to 100%=0xFF | ||
70 | /// _delay_ms(250); | ||
71 | |||
72 | /// stdout_put_string(uart_putc, "clock frequency set!\n"); | ||
73 | } | ||
74 | |||
75 | |||
76 | void audio_vs1011_setVolume(const unsigned char left, const unsigned char right) | ||
77 | { | ||
78 | //uint16_t volume = right + (256 * left); | ||
79 | /// stdout_put_uint(uart_putc, volume); | ||
80 | /// stdout_put_string(uart_putc, "\t< new volume for vs1011\n"); | ||
81 | audio_vs1011_cmd(SCI_VOL, ((uint16_t) right) + ((uint16_t) left * 256)); | ||
82 | } | ||
83 | |||
84 | void audio_vs1011_test_volume() | ||
85 | { | ||
86 | audio_vs1011_setVolume(0, 0); // left and right to 0%=0x00 -> standby | ||
87 | _delay_ms(250); | ||
88 | audio_vs1011_setVolume(255, 255); // left and right both to 100%=0xFF | ||
89 | _delay_ms(250); | ||
90 | } | ||
91 | |||
92 | void audio_vs1011_test_sine() | ||
93 | { | ||
94 | static const unsigned char sinetest[] = {0x53, 0xEF, 0x6E, 0x30, 0x0, 0x0, 0x0, 0x0}; | ||
95 | static const unsigned char testexit[] = {'E', 'x', 'i', 't', 0x0, 0x0, 0x0, 0x0}; | ||
96 | |||
97 | /// audio_vs1011_setVolume(255, 255); // left and right both to 100%=0xFF | ||
98 | /// stdout_put_string(uart_putc, "activating sine-test...\n"); | ||
99 | audio_vs1011_dat(sinetest, sizeof(sinetest)); | ||
100 | _delay_ms(250); | ||
101 | |||
102 | /// stdout_put_string(uart_putc, "deactivating sine-test...\n"); | ||
103 | audio_vs1011_dat(testexit, sizeof(testexit)); | ||
104 | _delay_ms(250); | ||
105 | /// audio_vs1011_setVolume(255, 255); // left and right both to 100%=0xFF | ||
106 | } | ||
107 | |||
108 | /* | ||
109 | void audio_vs1011_test_eeprom() | ||
110 | { | ||
111 | unsigned char block[512]; | ||
112 | eeprom_read_block((void*) &block, (const void*) 0, 512); | ||
113 | |||
114 | int i,q; | ||
115 | for(i=0; i<512; i+=32) | ||
116 | { | ||
117 | audio_vs1011_enable(); | ||
118 | loop_until_bit_is_set(PIND, PD5); | ||
119 | for(q=0; q<32; q++) | ||
120 | spi_putc(block[i+q]); | ||
121 | audio_vs1011_disable(); | ||
122 | } | ||
123 | loop_until_bit_is_set(PIND, PD5); | ||
124 | _delay_ms(500); | ||
125 | } | ||
126 | */ | ||
127 | |||
128 | void audio_vs1011_dat(const unsigned char* dat, const unsigned char size) | ||
129 | { | ||
130 | audio_vs1011_enable(); | ||
131 | unsigned char i; | ||
132 | for(i=0; i<size; i++) | ||
133 | spi_putc(dat[i]); | ||
134 | audio_vs1011_disable(); | ||
135 | } | ||
136 | |||
137 | |||
138 | /* | ||
139 | unsigned char audio_vs1011_stream(unsigned char (*readfn)()) | ||
140 | { | ||
141 | audio_vs1011_enable(); | ||
142 | register unsigned char i; | ||
143 | for(i=0; i<32; i++) | ||
144 | spi_putc(readfn()); | ||
145 | audio_vs1011_disable(); | ||
146 | audio_vs1011_waitDREQ(); | ||
147 | return 32; | ||
148 | }*/ | ||
149 | |||
150 | |||
151 | |||
152 | |||
153 | void audio_vs1011_cmd(const unsigned char cmd, uint16_t dat) | ||
154 | { | ||
155 | loop_until_bit_is_set(PIND, PD5); | ||
156 | /// audio_vs1011_enable(); | ||
157 | //PORTA &= ~(1<<PA6); // <XCS> | ||
158 | audio_vs1011_init_xcs(); | ||
159 | spi_putc(VS_WRITE); | ||
160 | spi_putc(cmd); | ||
161 | spi_putc((dat >> 8) & 0xFF); | ||
162 | spi_putc((dat >> 0) & 0xFF); | ||
163 | //PORTA |= (1<<PA6); // </XCS> | ||
164 | audio_vs1011_exit_xcs(); | ||
165 | spi_putc(0); | ||
166 | spi_putc(0); | ||
167 | spi_putc(0); | ||
168 | spi_putc(0); | ||
169 | /// audio_vs1011_disable(); | ||
170 | /// _delay_ms(1); // << wait to make sure vs1011 got the message | ||
171 | _delay_ms(5); | ||
172 | loop_until_bit_is_set(PIND, PD5); // << wait for DREQ to become ready again | ||
173 | } | ||
diff --git a/src/inc/spi/dev/audio_vs1011.h b/src/inc/spi/dev/audio_vs1011.h new file mode 100755 index 0000000..ec97550 --- /dev/null +++ b/src/inc/spi/dev/audio_vs1011.h | |||
@@ -0,0 +1,91 @@ | |||
1 | #ifndef AUDIO_VS1011_H | ||
2 | #define AUDIO_VS1011_H | ||
3 | |||
4 | #include <avr/io.h> | ||
5 | #include <util/delay.h> | ||
6 | #include <inttypes.h> | ||
7 | #include <avr/eeprom.h> | ||
8 | |||
9 | #include "../spi.h" | ||
10 | /// #include "../../uart/uart.h" | ||
11 | /// #include "../../stdout.h" | ||
12 | |||
13 | |||
14 | #define audio_vs1011_enable() PORTC &= ~(1<<PC6); // <XDCS> | ||
15 | #define audio_vs1011_disable() PORTC |= (1<<PC6); // </XDCS> | ||
16 | #define audio_vs1011_isDREQ() (PIND & (1<<PD5)) | ||
17 | #define audio_vs1011_waitDREQ(); loop_until_bit_is_set(PIND, PD5); | ||
18 | |||
19 | |||
20 | // [OPERATIONS] | ||
21 | #define VS_WRITE 0x2 | ||
22 | #define VS_READ 0x3 | ||
23 | |||
24 | |||
25 | // [SCI REGISTERS] - page 29 | ||
26 | #define SCI_MODE 0x0 // Mode control | ||
27 | #define SCI_STATUS 0x1 // Status of VS1011e | ||
28 | #define SCI_BASS 0x2 // Built-in bass/treble enhancer | ||
29 | #define SCI_CLOCKF 0x3 // Clock freq + multiplier | ||
30 | #define SCI_DECODE_TIME 0x4 // Decode time in seconds | ||
31 | #define SCI_AUDATA 0x5 // Misc. audio data | ||
32 | #define SCI_WRAM 0x6 // RAM write/read | ||
33 | #define SCI_WRAMADDR 0x7 // Base address for RAM write/read | ||
34 | #define SCI_HDAT0 0x8 // Stream header data 0 | ||
35 | #define SCI_HDAT1 0x9 // Stream header data 1 | ||
36 | #define SCI_AIADDR 0xA // Start address of application | ||
37 | #define SCI_VOL 0xB // Volume control | ||
38 | #define SCI_AICTRL0 0xC // Application control register 0 | ||
39 | #define SCI_AICTRL1 0xD // Application control register 1 | ||
40 | #define SCI_AICTRL2 0xE // Application control register 2 | ||
41 | #define SCI_AICTRL3 0xF // Application control register 3 | ||
42 | |||
43 | // [VALUES::SCI_MODE] | ||
44 | #define SM_DIFF 0x1 // Differential | ||
45 | #define SM_LAYER12 0x2 // Allow MPEG layers I & II | ||
46 | #define SM_RESET 0x4 // Soft reset | ||
47 | #define SM_OUTOFWAV 0x8 // Jump out of WAV decoding | ||
48 | #define SM_SETTOZERO1 0x10 // set to zero | ||
49 | #define SM_TESTS 0x20 // Allow SDI tests | ||
50 | #define SM_STREAM 0x40 // Stream mode | ||
51 | #define SM_SETTOZERO2 0x80 // set to zero | ||
52 | |||
53 | #define SM_DACT 0x100 // DCLK active edge | ||
54 | #define SM_SDIORD 0x200 // SDI bit order | ||
55 | #define SM_SDISHARE 0x400 // Share SPI chip select | ||
56 | #define SM_SDINEW 0x800 // VS1002 native SPI modes | ||
57 | #define SM_SETTOZERO3 0x1000 // set to zero | ||
58 | |||
59 | // [VALUES::SCI_CLOCKF] | ||
60 | #define SC_CLK2X 0x8000 // clock doubler | ||
61 | |||
62 | #define audio_vs1011_init_xcs() PORTC &= ~(1<<PC1) // <XCS> | ||
63 | #define audio_vs1011_exit_xcs() PORTC |= (1<<PC1); // </XCS> | ||
64 | |||
65 | #define audio_vs1011_init_xdcs() PORTC &= ~(1<<PC2) // <XCS> | ||
66 | #define audio_vs1011_exit_xdcs() PORTC |= (1<<PC2); // </XCS> | ||
67 | |||
68 | #define wait4dreq() loop_until_bit_is_set(PINC, PC3); // <DREQ></DREQ> | ||
69 | |||
70 | |||
71 | /// void audio_vs1011_cmd(const unsigned char sci_cmd, const unsigned char hdata, | ||
72 | /// const unsigned char ldata); | ||
73 | void audio_vs1011_init(); | ||
74 | void audio_vs1011_cmd(const unsigned char cmd, uint16_t dat); | ||
75 | void audio_vs1011_setVolume(const unsigned char left, const unsigned char right); | ||
76 | |||
77 | //void audio_vs1011_dat(const unsigned char dat[32]); | ||
78 | void audio_vs1011_dat(const unsigned char* dat, const unsigned char size); | ||
79 | /// unsigned char audio_vs1011_stream(unsigned char (*readfn)()); | ||
80 | void audio_vs1011_test_sine(); | ||
81 | void audio_vs1011_test_volume(); | ||
82 | /// void audio_vs1011_test_eeprom(); | ||
83 | /// void audio_vs1011_enable(); | ||
84 | /// void audio_vs1011_disable(); | ||
85 | /// char audio_vs1011_isDREQ(); | ||
86 | |||
87 | |||
88 | /// void audio_vs1011_test_softreset(); | ||
89 | |||
90 | |||
91 | #endif | ||
diff --git a/src/inc/spi/dev/display_dogm.c b/src/inc/spi/dev/display_dogm.c new file mode 100755 index 0000000..a655769 --- /dev/null +++ b/src/inc/spi/dev/display_dogm.c | |||
@@ -0,0 +1,39 @@ | |||
1 | #include "display_dogm.h" | ||
2 | |||
3 | void display_dogm_init() | ||
4 | { | ||
5 | //DDRA |= (1<<PA7); // activate portA for display | ||
6 | //DDRC |= (1<<PC7); // activate portC for display | ||
7 | DDRD |= (1<<PD7) | (1<<PD6); | ||
8 | PORTD |= (1<<PD7) | (1<<PD6); | ||
9 | |||
10 | // Wait time >40mS after VDD stable | ||
11 | _delay_ms(50); // 10ms for safty | ||
12 | |||
13 | display_dogm_exec(CMD_FUNCTION_SET); | ||
14 | display_dogm_exec(CMD_FUNCTION_SET); | ||
15 | display_dogm_exec(CMD_CONTRAST_SET); | ||
16 | display_dogm_exec(CMD_POWER_CONTROL); | ||
17 | display_dogm_exec(CMD_FOLLOWER_CONTROL); | ||
18 | display_dogm_exec(CMD_DISPLAY_ONOFF); | ||
19 | display_dogm_exec(CMD_ENTRY_MODE_SET); | ||
20 | display_dogm_exec(CMD_DISPLAY_CLEAR); | ||
21 | } | ||
22 | |||
23 | void display_dogm_putc(char c) | ||
24 | { | ||
25 | PORTD &= ~(1<<PD7); // chip select: activate display | ||
26 | PORTD |= (1<<PD6); // L=Commandmode, H=Datamode | ||
27 | spi_putc(c); | ||
28 | _delay_us(40); // try and error value (you get chinese chars? increase!) | ||
29 | PORTD |= (1<<PD7); // chip select: deactivate display | ||
30 | } | ||
31 | |||
32 | void display_dogm_exec(char c) | ||
33 | { | ||
34 | PORTD &= ~(1<<PD7); // chip select: activate display | ||
35 | PORTD &= ~(1<<PD6); // L=Commandmode, H=Datamode | ||
36 | spi_putc(c); | ||
37 | _delay_ms(4); // time the longest command takes on display | ||
38 | PORTD |= (1<<PD7); // chip select: deactivate display | ||
39 | } | ||
diff --git a/src/inc/spi/dev/display_dogm.h b/src/inc/spi/dev/display_dogm.h new file mode 100755 index 0000000..d9cf560 --- /dev/null +++ b/src/inc/spi/dev/display_dogm.h | |||
@@ -0,0 +1,31 @@ | |||
1 | #ifndef DISPLAY_DOGM_H | ||
2 | #define DISPLAY_DOGM_H | ||
3 | |||
4 | #include <avr/io.h> | ||
5 | #include <util/delay.h> | ||
6 | #include "../spi.h" | ||
7 | |||
8 | #define CONTAST 3 | ||
9 | |||
10 | #define CMD_FUNCTION_SET 32 + 16 + 8 + 0 + 1 | ||
11 | #define CMD_POWER_CONTROL 64 + 0 + 16 + 0 + 4 + CONTAST | ||
12 | #define CMD_FOLLOWER_CONTROL 0x6E | ||
13 | |||
14 | #define CMD_ENTRY_MODE_SET 4 + 2 + 0 | ||
15 | #define CMD_DISPLAY_HOME 0 + 2 + 0 | ||
16 | #define CMD_DISPLAY_CLEAR 0 + 0 + 1 | ||
17 | |||
18 | #define CMD_CONTRAST_SET 0x7F | ||
19 | #define CMD_FUNCTION_SET2 0x38 | ||
20 | #define CMD_DISPLAY_ONOFF 0x0C | ||
21 | #define CMD_DISPLAY_POS 0x80 | ||
22 | |||
23 | //#define display_dogm_enable() PORTD &= ~(1<<PD7); PORTD |= (1<<PD6); // <XDCS> | ||
24 | //#define display_dogm_disable() PORTD |= (1<<PD7); PORTD &= ~(1<<PD6); // </XDCS> | ||
25 | |||
26 | |||
27 | void display_dogm_init(); | ||
28 | void display_dogm_putc(char); | ||
29 | void display_dogm_exec(char); | ||
30 | |||
31 | #endif | ||
diff --git a/src/inc/spi/dev/petit b/src/inc/spi/dev/petit new file mode 160000 | |||
Subproject b1109539d5d34b62849d67a65ce244fcb1d0f5e | |||
diff --git a/src/inc/spi/spi.c b/src/inc/spi/spi.c new file mode 100755 index 0000000..b04fe7e --- /dev/null +++ b/src/inc/spi/spi.c | |||
@@ -0,0 +1,56 @@ | |||
1 | #include "spi.h" | ||
2 | |||
3 | void spi_init() | ||
4 | { | ||
5 | // setup SPI-Port | ||
6 | DDR_SPI = (1<<DD_MOSI) | (1<<DD_SS) | (1<<DD_SCK); | ||
7 | PORT_SPI |= (1<<DD_SS); | ||
8 | |||
9 | // SPI Control Register (SPCR) | ||
10 | // SPI Enable=1 | Data Order=LSB | SPI_Master=1 (!make SS stay HIGH!) | ||
11 | // SPCR |= (1<<SPE) | (1<<DORD) | (1<<MSTR) | (1<<CPOL) | (1<<CPHA) | (1<<SPR1) | (1<<SPR0); | ||
12 | // (1<<DORD) - would be LSB-mode (, but we need MSB-mode!) | ||
13 | SPCR = (1<<SPE) | (1<<MSTR); // (1<<SPR1) | (1<<SPR0) | ||
14 | spi_setspeed(SPICLK_DEFAULT); | ||
15 | // setup SPI options | ||
16 | /// SPSR &= ~(1<<SPI2X); // do not use SPI speed-doubler | ||
17 | } | ||
18 | |||
19 | void spi_setspeed(SPICLK speed) | ||
20 | { | ||
21 | if(speed < 4) // nominal clock speeds | ||
22 | { | ||
23 | SPSR &= ~(1<<SPI2X); // do not use SPI speed-doubler | ||
24 | SPCR |= speed; | ||
25 | } | ||
26 | else // use SPI2X | ||
27 | { | ||
28 | SPSR |= (1<<SPI2X); // use SPI speed-doubler | ||
29 | SPCR |= (speed - 0x4); | ||
30 | } | ||
31 | _delay_ms(2); | ||
32 | } | ||
33 | |||
34 | void spi_putc(unsigned char c) | ||
35 | { | ||
36 | /// uart_putc('>'); | ||
37 | /// uart_putc(c); | ||
38 | /// uart_putc('\n'); | ||
39 | SPDR = c; | ||
40 | loop_until_bit_is_set(SPSR, SPIF); | ||
41 | /// stdout_put_string(uart_putc, "- [done]\n"); | ||
42 | } | ||
43 | |||
44 | unsigned char spi_getc() | ||
45 | { | ||
46 | SPDR = 0xFF; // init receive register with value | ||
47 | /* Wait for reception complete */ | ||
48 | while(!(SPSR & (1<<SPIF))); | ||
49 | /* Return data register */ | ||
50 | return SPDR; | ||
51 | /// unsigned char c = SPDR; | ||
52 | /// uart_putc('<'); | ||
53 | /// uart_putc(c); | ||
54 | /// uart_putc('\n'); | ||
55 | /// return c; | ||
56 | } | ||
diff --git a/src/inc/spi/spi.h b/src/inc/spi/spi.h new file mode 100755 index 0000000..bb92cac --- /dev/null +++ b/src/inc/spi/spi.h | |||
@@ -0,0 +1,37 @@ | |||
1 | #ifndef SPI_H | ||
2 | #define SPI_H | ||
3 | |||
4 | #include <avr/io.h> | ||
5 | #include <util/delay.h> | ||
6 | #include "../uart/uart.h" | ||
7 | #include "../stdout.h" | ||
8 | |||
9 | #define SPICLK_DEFAULT SPICLK_64 | ||
10 | #define SPICLK_FAST SPICLK_X2 | ||
11 | |||
12 | #define PORT_SPI PORTB | ||
13 | #define DDR_SPI DDRB | ||
14 | #define DD_SS DDB4 | ||
15 | #define DD_MOSI DDB5 | ||
16 | #define DD_MISO DDB6 | ||
17 | #define DD_SCK DDB7 | ||
18 | |||
19 | typedef enum | ||
20 | { | ||
21 | SPICLK_4, // vv nominal clk-speeds | ||
22 | SPICLK_16, | ||
23 | SPICLK_64, | ||
24 | SPICLK_128, | ||
25 | SPICLK_X2, // vv with SPI2x | ||
26 | SPICLK_X32, | ||
27 | SPICLK_X64 | ||
28 | } SPICLK; | ||
29 | |||
30 | |||
31 | void spi_init(); | ||
32 | void spi_putc(unsigned char c); | ||
33 | unsigned char spi_getc(); | ||
34 | void spi_setspeed(SPICLK speed); | ||
35 | |||
36 | |||
37 | #endif | ||