-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
479 lines (399 loc) · 10.9 KB
/
main.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include<avr/io.h>
#include<util/delay.h>
// pin definitions
#define DDR_SPI DDRB
#define PORT_SPI PORTB
#define CS PINB2
#define MOSI PINB3
#define MISO PINB4
#define SCK PINB5
// macros
#define CS_ENABLE() PORT_SPI &= ~(1 << CS)
#define CS_DISABLE() PORT_SPI |= (1 << CS)
// command definitions
#define CMD0 0
#define CMD0_ARG 0x00000000
#define CMD0_CRC 0x94
#define CMD8 8
#define CMD8_ARG 0x0000001AA
#define CMD8_CRC 0x86
#define CMD55 55
#define CMD55_ARG 0x00000000
#define CMD55_CRC 0x00
#define CMD58 58
#define CMD58_ARG 0x00000000
#define CMD58_CRC 0x00
#define ACMD41 41
#define ACMD41_ARG 0x40000000
#define ACMD41_CRC 0x00
// R1 responses
#define PARAM_ERROR(X) X & 0b01000000
#define ADDR_ERROR(X) X & 0b00100000
#define ERASE_SEQ_ERROR(X) X & 0b00010000
#define CRC_ERROR(X) X & 0b00001000
#define ILLEGAL_CMD(X) X & 0b00000100
#define ERASE_RESET(X) X & 0b00000010
#define IN_IDLE(X) X & 0b00000001
#define POWER_UP_STATUS(X) X & 0x40
#define CCS_VAL(X) X & 0x40
#define VDD_2728(X) X & 0b10000000
#define VDD_2829(X) X & 0b00000001
#define VDD_2930(X) X & 0b00000010
#define VDD_3031(X) X & 0b00000100
#define VDD_3132(X) X & 0b00001000
#define VDD_3233(X) X & 0b00010000
#define VDD_3334(X) X & 0b00100000
#define VDD_3435(X) X & 0b01000000
#define VDD_3536(X) X & 0b10000000
// R7 responses
#define CMD_VER(X) ((X >> 4) & 0xF0)
#define VOL_ACC(X) (X & 0x1F)
#define VOLTAGE_ACC_27_33 0b00000001
#define VOLTAGE_ACC_LOW 0b00000010
#define VOLTAGE_ACC_RES1 0b00000100
#define VOLTAGE_ACC_RES2 0b00001000
// UART functions
void UART_init(uint16_t baudRate);
void UART_putc(unsigned char data);
void UART_puts(char* charString);
void UART_puthex8(uint8_t val);
unsigned char UART_getc(void);
// SPI functions
void SPI_init(void);
uint8_t SPI_transfer(uint8_t data);
// SD functions
void SD_powerUpSeq(void);
void SD_command(uint8_t cmd, uint32_t arg, uint8_t crc);
uint8_t SD_readRes1(void);
void SD_readRes3_7(uint8_t *res);
uint8_t SD_goIdleState(void);
void SD_sendIfCond(uint8_t *res);
void SD_readOCR(uint8_t *res);
uint8_t SD_sendApp(void);
uint8_t SD_sendOpCond(void);
void SD_printR1(uint8_t res);
void SD_printR3(uint8_t *res);
void SD_printR7(uint8_t *res);
int main(void)
{
// array to hold responses
uint8_t res[5];
// initialize UART
UART_init(57600);
// initialize SPI
SPI_init();
// start power up sequence
SD_powerUpSeq();
// received char from UART
char c;
while(1)
{
// print menu
UART_puts("MENU\r\n");
UART_puts("------------------\r\n");
UART_puts("0 - Send CMD0\r\n1 - Send CMD8\r\n2 - Send CMD58\r\n");
UART_puts("3 - Send CMD55\r\n4 - Send ACMD41\r\n");
UART_puts("------------------\r\n");
// get character from user
c = UART_getc();
if(c == '0')
{
// command card to idle
UART_puts("Sending CMD0...\r\n");
res[0] = SD_goIdleState();
UART_puts("Response:\r\n");
SD_printR1(res[0]);
}
else if(c == '1')
{
// send if conditions
UART_puts("Sending CMD8...\r\n");
SD_sendIfCond(res);
UART_puts("Response:\r\n");
SD_printR7(res);
}
else if(c == '2')
{
// send if conditions
UART_puts("Sending CMD58...\r\n");
SD_readOCR(res);
UART_puts("Response:\r\n");
SD_printR3(res);
}
else if(c == '3')
{
// command card to idle
UART_puts("Sending CMD55...\r\n");
res[0] = SD_sendApp();
UART_puts("Response:\r\n");
SD_printR1(res[0]);
}
else if(c == '4')
{
// command card to idle
UART_puts("Sending ACMD41...\r\n");
res[0] = SD_sendOpCond();
UART_puts("Response:\r\n");
SD_printR1(res[0]);
}
else
{
UART_puts("Unrecognized command\r\n");
}
}
}
void SPI_init()
{
// set CS, MOSI and SCK to output
DDR_SPI |= (1 << CS) | (1 << MOSI) | (1 << SCK);
// enable pull up resistor in MISO
DDR_SPI |= (1 << MISO);
// enable SPI, set as master, and clock to fosc/128
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
}
uint8_t SPI_transfer(uint8_t data)
{
// load data into register
SPDR = data;
// Wait for transmission complete
while(!(SPSR & (1 << SPIF)));
// return SPDR
return SPDR;
}
void SD_powerUpSeq()
{
// make sure card is deselected
CS_DISABLE();
// give SD card time to power up
_delay_ms(1);
// send 80 clock cycles to synchronize
for(uint8_t i = 0; i < 10; i++)
SPI_transfer(0xFF);
}
void SD_command(uint8_t cmd, uint32_t arg, uint8_t crc)
{
// transmit command to sd card
SPI_transfer(cmd|0x40);
// transmit argument
SPI_transfer((uint8_t)(arg >> 24));
SPI_transfer((uint8_t)(arg >> 16));
SPI_transfer((uint8_t)(arg >> 8));
SPI_transfer((uint8_t)(arg));
// transmit crc
SPI_transfer(crc|0x01);
}
uint8_t SD_readRes1()
{
uint8_t i = 0, res1;
// keep polling until actual data received
while((res1 = SPI_transfer(0xFF)) == 0xFF)
{
i++;
// if no data received for 8 bytes, break
if(i > 8) break;
}
return res1;
}
void SD_readRes3_7(uint8_t *res)
{
// read response 1 in R7
res[0] = SD_readRes1();
// if error reading R1, return
if(res[0] > 1) return;
// read remaining bytes
res[1] = SPI_transfer(0xFF);
res[2] = SPI_transfer(0xFF);
res[3] = SPI_transfer(0xFF);
res[4] = SPI_transfer(0xFF);
}
uint8_t SD_goIdleState()
{
// assert chip select
SPI_transfer(0xFF);
CS_ENABLE();
SPI_transfer(0xFF);
// send CMD0
SD_command(CMD0, CMD0_ARG, CMD0_CRC);
// read response
uint8_t res1 = SD_readRes1();
// deassert chip select
SPI_transfer(0xFF);
CS_DISABLE();
SPI_transfer(0xFF);
return res1;
}
void SD_sendIfCond(uint8_t *res)
{
// assert chip select
SPI_transfer(0xFF);
CS_ENABLE();
SPI_transfer(0xFF);
// send CMD8
SD_command(CMD8, CMD8_ARG, CMD8_CRC);
// read response
SD_readRes3_7(res);
// deassert chip select
SPI_transfer(0xFF);
CS_DISABLE();
SPI_transfer(0xFF);
}
void SD_readOCR(uint8_t *res)
{
// assert chip select
SPI_transfer(0xFF);
CS_ENABLE();
SPI_transfer(0xFF);
// send CMD58
SD_command(CMD58, CMD58_ARG, CMD58_CRC);
// read response
SD_readRes3_7(res);
// deassert chip select
SPI_transfer(0xFF);
CS_DISABLE();
SPI_transfer(0xFF);
}
uint8_t SD_sendApp()
{
// assert chip select
SPI_transfer(0xFF);
CS_ENABLE();
SPI_transfer(0xFF);
// send CMD0
SD_command(CMD55, CMD55_ARG, CMD55_CRC);
// read response
uint8_t res1 = SD_readRes1();
// deassert chip select
SPI_transfer(0xFF);
CS_DISABLE();
SPI_transfer(0xFF);
return res1;
}
uint8_t SD_sendOpCond()
{
// assert chip select
SPI_transfer(0xFF);
CS_ENABLE();
SPI_transfer(0xFF);
// send CMD0
SD_command(ACMD41, ACMD41_ARG, ACMD41_CRC);
// read response
uint8_t res1 = SD_readRes1();
// deassert chip select
SPI_transfer(0xFF);
CS_DISABLE();
SPI_transfer(0xFF);
return res1;
}
void SD_printR1(uint8_t res)
{
if(res & 0b10000000)
UART_puts("\tError: MSB = 1\r\n");
if(res == 0)
UART_puts("\tCard Ready\r\n");
if(PARAM_ERROR(res))
UART_puts("\tParameter Error\r\n");
if(ADDR_ERROR(res))
UART_puts("\tAddress Error\r\n");
if(ERASE_SEQ_ERROR(res))
UART_puts("\tErase Sequence Error\r\n");
if(CRC_ERROR(res))
UART_puts("\tCRC Error\r\n");
if(ILLEGAL_CMD(res))
UART_puts("\tIllegal Command\r\n");
if(ERASE_RESET(res))
UART_puts("\tErase Reset Error\r\n");
if(IN_IDLE(res))
UART_puts("\tIn Idle State\r\n");
}
void SD_printR3(uint8_t *res)
{
SD_printR1(res[0]);
if(res[0] > 1) return;
UART_puts("\tCard Power Up Status: ");
if(POWER_UP_STATUS(res[1]))
{
UART_puts("READY\r\n");
UART_puts("\tCCS Status: ");
if(CCS_VAL(res[1])){ UART_puts("1\r\n"); }
else UART_puts("0\r\n");
}
else
{
UART_puts("BUSY\r\n");
}
UART_puts("\tVDD Window: ");
if(VDD_2728(res[3])) UART_puts("2.7-2.8, ");
if(VDD_2829(res[2])) UART_puts("2.8-2.9, ");
if(VDD_2930(res[2])) UART_puts("2.9-3.0, ");
if(VDD_3031(res[2])) UART_puts("3.0-3.1, ");
if(VDD_3132(res[2])) UART_puts("3.1-3.2, ");
if(VDD_3233(res[2])) UART_puts("3.2-3.3, ");
if(VDD_3334(res[2])) UART_puts("3.3-3.4, ");
if(VDD_3435(res[2])) UART_puts("3.4-3.5, ");
if(VDD_3536(res[2])) UART_puts("3.5-3.6");
UART_puts("\r\n");
}
void SD_printR7(uint8_t *res)
{
SD_printR1(res[0]);
if(res[0] > 1) return;
UART_puts("\tCommand Version: ");
UART_puthex8(CMD_VER(res[1]));
UART_puts("\r\n");
UART_puts("\tVoltage Accepted: ");
if(VOL_ACC(res[3]) == VOLTAGE_ACC_27_33)
UART_puts("2.7-3.6V\r\n");
else if(VOL_ACC(res[3]) == VOLTAGE_ACC_LOW)
UART_puts("LOW VOLTAGE\r\n");
else if(VOL_ACC(res[3]) == VOLTAGE_ACC_RES1)
UART_puts("RESERVED\r\n");
else if(VOL_ACC(res[3]) == VOLTAGE_ACC_RES2)
UART_puts("RESERVED\r\n");
else UART_puts("NOT DEFINED\r\n");
UART_puts("\tEcho: ");
UART_puthex8(res[4]);
UART_puts("\r\n");
}
void UART_init(uint16_t baudRate)
{
// calculate baud rate
baudRate = (((F_CPU/(baudRate*16UL))) - 1);
// set rate
UBRR0H = (unsigned char)(baudRate >> 8);
UBRR0L = (unsigned char) baudRate;
// Enable reciever and transmitter
UCSR0B |= (1 << RXEN0)|(1 << TXEN0);
}
void UART_putc(unsigned char data)
{
// wait for empty transmit buffer
while(!(UCSR0A & (1<<UDRE0)));
// send data to output register
UDR0 = data;
}
void UART_puts(char* charString)
{
// iterate through string
while(*charString > 0)
// print character
UART_putc(*charString++);
}
void UART_puthex8(uint8_t val)
{
// extract upper and lower nibbles from input value
uint8_t upperNibble = (val & 0xF0) >> 4;
uint8_t lowerNibble = val & 0x0F;
// convert nibble to its ASCII hex equivalent
upperNibble += upperNibble > 9 ? 'A' - 10 : '0';
lowerNibble += lowerNibble > 9 ? 'A' - 10 : '0';
// print the characters
UART_putc(upperNibble);
UART_putc(lowerNibble);
}
unsigned char UART_getc(void)
{
// wait for data to be received
while(!(UCSR0A & (1 << RXC0)));
// get data to output register
return UDR0;
}