1 | // AT90USB1287 (16MHz)
|
2 | //SD.c
|
3 |
|
4 | #include "mmc.h"
|
5 | #include "usart.h"
|
6 |
|
7 | uint8_t mmc_init (void){
|
8 | uint16_t Timeout = 0;
|
9 |
|
10 | DDRB &= ~(1<<PINB3); // PortB Pin 3 (SPI MISO) -> Eingang
|
11 | DDRB |= (1<<PINB1); // PortB Pin 1 (SPI SCK) -> Ausgang
|
12 | DDRB |= (1<<PINB2); // PortB Pin 2 (SPI MOSI) -> Ausgang
|
13 | DDRB |= (1<<PINB0); // PortB Pin 0 (SPI CS) -> Ausgang
|
14 |
|
15 | USART_Sende_String("Aktiviere SPI\n\r");
|
16 |
|
17 | // Aktiviere SPI als Master mit OSC/128
|
18 | SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0);
|
19 |
|
20 | // Aktiviere doppelte Geschwindigkeit
|
21 | SPSR = (1<<SPI2X);
|
22 |
|
23 | int ii;
|
24 | for (ii = 0;ii<0xff;ii++){};
|
25 | // Setze MOSI und CS auf 1
|
26 | PORTB |= (1<<PINB2);
|
27 | PORTB |= (1<<PINB0);
|
28 |
|
29 | //Initialisiert MMC/SD-Karte in den SPI-Mode
|
30 | USART_Sende_String("Initialisiere SD Karte in SPI-Mode\n\r");
|
31 | uint8_t b;
|
32 | for (b = 0;b<0x0f;b++) //Sendet min 74+ Clocks an die MMC/SD-Karte
|
33 | {
|
34 | mmc_write_byte(0xff);
|
35 | }
|
36 | // CS auf 0
|
37 | PORTB &=~ (1<<PINB0);
|
38 |
|
39 | //Sendet Commando CMD0 an MMC/SD-Karte (Go_Idle_State)
|
40 | USART_Sende_String("SENDE Command CMD0\n\r");
|
41 |
|
42 | uint8_t CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
|
43 |
|
44 | while(mmc_write_command (CMD) !=1)
|
45 | {
|
46 | if (Timeout++ > 200)
|
47 | {
|
48 | MMC_Disable();
|
49 | USART_Sende_String("Timeout CMD0\n\r");
|
50 | return(1); //Abbruch bei Commando1 (Return Code1)
|
51 | }
|
52 | }
|
53 | USART_Sende_String("Sende Command CMD1\n\r");
|
54 |
|
55 | //Sendet Commando CMD1 an MMC/SD-Karte
|
56 | Timeout = 0;
|
57 | CMD[0] = 0x41;//Commando 1
|
58 | CMD[5] = 0xFF;
|
59 | while( mmc_write_command (CMD) !=0)
|
60 | {
|
61 | if (Timeout++ > 400)
|
62 | {
|
63 | MMC_Disable();
|
64 | return(2); //Abbruch bei Commando2 (Return Code2)
|
65 | }
|
66 | }
|
67 | USART_Sende_String("SPI-MODe\n\r");
|
68 | #if SPI_Mode
|
69 | //SPI Bus auf max Geschwindigkeit
|
70 | SPCR &= ~((1<<SPR0) | (1<<SPR1));
|
71 | SPSR = SPSR|(1<<SPI2X);
|
72 | #endif
|
73 |
|
74 | USART_Sende_String("Disable MCC\n\r");
|
75 | //set MMC_Chip_Select to high (MMC/SD-Karte Inaktiv)
|
76 | MMC_Disable();
|
77 | return(0);
|
78 | }
|
79 | //############################################################################
|
80 | //Sendet ein Commando an die MMC/SD-Karte
|
81 | unsigned char mmc_write_command (unsigned char *cmd)
|
82 | //############################################################################
|
83 | {
|
84 | unsigned char tmp = 0xff;
|
85 | unsigned int Timeout = 0;
|
86 |
|
87 | //set MMC_Chip_Select to high (MMC/SD-Karte Inaktiv)
|
88 | MMC_Disable();
|
89 |
|
90 | //sendet 8 Clock Impulse
|
91 | mmc_write_byte(0xFF);
|
92 |
|
93 | //set MMC_Chip_Select to low (MMC/SD-Karte Aktiv)
|
94 | MMC_Enable();
|
95 |
|
96 | //sendet 6 Byte Commando
|
97 | unsigned char a;
|
98 | for (a = 0;a<0x06;a++) //sendet 6 Byte Commando zur MMC/SD-Karte
|
99 | {
|
100 | mmc_write_byte(*cmd++);
|
101 | }
|
102 |
|
103 | //Wartet auf ein gültige Antwort von der MMC/SD-Karte
|
104 | while (tmp == 0xff)
|
105 | {
|
106 | tmp = mmc_read_byte();
|
107 |
|
108 | if (Timeout++ > 500)
|
109 | {
|
110 | USART_Sende_String("Timeout Read Byte in (write command)\n\r");
|
111 | break; //Abbruch da die MMC/SD-Karte nicht Antwortet
|
112 | }
|
113 | }
|
114 | usart_write("While schleife verlassen: tmp = %i\n\r",tmp);
|
115 | return(tmp);
|
116 | }
|
117 | //############################################################################
|
118 | //Routine zum Empfangen eines Bytes von der MMC-Karte
|
119 | inline unsigned char mmc_read_byte (void)
|
120 | //############################################################################
|
121 | {
|
122 | unsigned char Byte = 0;
|
123 |
|
124 | SPDR = 0xff;
|
125 | while(!(SPSR & (1<<SPIF))){};
|
126 | Byte = SPDR;
|
127 | usart_write("Byte: %i\n\r",Byte);
|
128 | return (Byte);
|
129 | }
|
130 | //############################################################################
|
131 | //Routine zum Senden eines Bytes zur MMC-Karte
|
132 | inline void mmc_write_byte (unsigned char Byte)
|
133 | //############################################################################
|
134 | {
|
135 | SPDR = Byte; //Sendet ein Byte
|
136 | while(!(SPSR & (1<<SPIF))) //Wartet bis Byte gesendet wurde
|
137 | {
|
138 | }
|
139 | }
|