Statistics
| Branch: | Tag: | Revision:

root / usrp2 / firmware / lib / sd.h @ 8056ff42

History | View | Annotate | Download (2.7 kB)

1
/* -*- c -*- */
2
/*
3
 * Copyright 2008 Ettus Research LLC
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
#ifndef INCLUDED_SD_H
20
#define INCLUDED_SD_H
21
22
#include "memory_map.h"
23
24
#define SD_READY 1
25
#define SD_IDLE_WAIT_MAX 100
26
#define SD_CMD_TIMEOUT 100
27
#define SD_RD_TIMEOUT 1000
28
29
#define SD_CMD0 0
30
#define SD_CMD1 1
31
#define SD_CMD9 9
32
#define SD_CMD10 10
33
#define SD_CMD12 12
34
#define SD_CMD13 13
35
#define SD_CMD16 16
36
#define SD_CMD17 17 
37
#define SD_CMD18 18
38
#define SD_CMD24 24
39
#define SD_CMD25 25
40
#define SD_CMD27 27
41
#define SD_CMD28 28
42
#define SD_CMD29 29
43
#define SD_CMD30 30
44
#define SD_CMD32 32
45
#define SD_CMD33 33
46
#define SD_CMD38 38
47
#define SD_CMD55 55
48
#define SD_CMD58 58
49
#define SD_CMD59 59
50
#define SD_ACMD41 41
51
#define SD_IDLE 0xFF
52
#define SD_CRC 0x95
53
54
#define SD_R1 1
55
#define SD_R1B 2
56
#define SD_R2 3
57
#define SD_R3 4
58
59
#define SD_CMD0_R SD_R1
60
#define SD_CMD16_R SD_R1
61
#define SD_CMD17_R SD_R1
62
#define SD_CMD55_R SD_R1
63
#define SD_ACMD41_R SD_R1
64
#define SD_CMD58_R SD_R3
65
66
#define SD_BLOCKLEN 512
67
#define SD_BLOCKLEN_NBITS 9
68
69
#define SD_MSK_IDLE 0x01
70
#define SD_MSK_OCR_33 0xC0
71
#define SD_MSK_TOK_DATAERROR 0xE0
72
73
74
int sd_init(void);
75
76
static inline void
77
sd_assert_cs(void)
78
{
79
  // Wait for idle before doing anything
80
  while(sdspi_regs->status != SD_READY)
81
    ;
82
  sdspi_regs->status = 1;
83
}
84
85
static inline void
86
sd_deassert_cs(void)
87
{
88
  // Wait for idle before doing anything
89
  while(sdspi_regs->status != SD_READY)
90
    ;
91
  sdspi_regs->status = 0;
92
}
93
94
static inline char
95
sd_rcv_byte(void)
96
{
97
  // Wait for idle before doing anything
98
  while(sdspi_regs->status != SD_READY)
99
    ;
100
  sdspi_regs->send_dat = SD_IDLE;
101
  while(sdspi_regs->status != SD_READY)
102
    ;
103
  return sdspi_regs-> receive_dat;
104
}
105
106
static inline void
107
sd_send_byte(char dat)
108
{
109
  // Wait for idle before doing anything
110
  while(sdspi_regs->status != SD_READY)
111
    ;      // Wait for status = 1 (ready)
112
  sdspi_regs->send_dat = dat;
113
}
114
115
116
int sd_send_command(unsigned char cmd,unsigned char response_type,
117
                    unsigned char *response,unsigned char *argument);
118
119
int sd_read_block (unsigned int blockaddr, unsigned char *buf);
120
int sd_write_block(unsigned int blockaddr, const unsigned char *buf);
121
122
#endif /* INCLUDED_SD_H */