1 | /** |
---|
2 | * \file reset_sdc.c |
---|
3 | * \author Cesar Fuguet |
---|
4 | * \date July 23, 2014 |
---|
5 | * |
---|
6 | * \brief Wrapper for the SD card and SPI drivers |
---|
7 | */ |
---|
8 | #include <reset_sdc.h> |
---|
9 | #include <reset_tty.h> |
---|
10 | #include <reset_utils.h> |
---|
11 | #include <defs.h> |
---|
12 | #include <sdcard.h> |
---|
13 | #include <spi.h> |
---|
14 | |
---|
15 | #ifndef SEG_IOC_BASE |
---|
16 | # error "SEG_IOC_BASE constant must be defined in the hard_config.h file" |
---|
17 | #endif |
---|
18 | |
---|
19 | static struct sdcard_dev _sdcard_device; |
---|
20 | static struct spi_dev *const _spi_device = (struct spi_dev*)SEG_IOC_BASE; |
---|
21 | |
---|
22 | static const int sdcard_reset_retries = 4; |
---|
23 | static const int spi_init_clkfreq = 200000 ; /* Hz */ |
---|
24 | static const int spi_func_clkfreq = 10000000; /* Hz */ |
---|
25 | |
---|
26 | int reset_sdc_init() |
---|
27 | { |
---|
28 | unsigned char sdcard_rsp; |
---|
29 | |
---|
30 | reset_puts("Initializing block device\n\r"); |
---|
31 | |
---|
32 | /** |
---|
33 | * Initializing the SPI controller |
---|
34 | */ |
---|
35 | spi_dev_config ( |
---|
36 | _spi_device , |
---|
37 | spi_init_clkfreq , |
---|
38 | RESET_SYSTEM_CLK * 1000, |
---|
39 | 8 , |
---|
40 | SPI_TX_NEGEDGE , |
---|
41 | SPI_RX_POSEDGE |
---|
42 | ); |
---|
43 | |
---|
44 | /** |
---|
45 | * Initializing the SD Card |
---|
46 | */ |
---|
47 | unsigned int iter = 0; |
---|
48 | while(1) |
---|
49 | { |
---|
50 | reset_puts("Trying to initialize SD card... "); |
---|
51 | |
---|
52 | sdcard_rsp = sdcard_dev_open(&_sdcard_device, _spi_device, 0); |
---|
53 | if (sdcard_rsp == 0) |
---|
54 | { |
---|
55 | reset_puts("OK\n"); |
---|
56 | break; |
---|
57 | } |
---|
58 | |
---|
59 | reset_puts("KO\n"); |
---|
60 | reset_sleep(1000); |
---|
61 | if (++iter >= sdcard_reset_retries) |
---|
62 | { |
---|
63 | reset_puts("\nERROR: During SD card reset to IDLE state\n" |
---|
64 | "/ card response = "); |
---|
65 | reset_putx(sdcard_rsp); |
---|
66 | reset_puts("\n"); |
---|
67 | reset_exit(); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Set the block length of the SD Card |
---|
73 | */ |
---|
74 | sdcard_rsp = sdcard_dev_set_blocklen(&_sdcard_device, 512); |
---|
75 | if (sdcard_rsp) |
---|
76 | { |
---|
77 | reset_puts("ERROR: During SD card blocklen initialization\n"); |
---|
78 | reset_exit(); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Incrementing SDCARD clock frequency for normal function |
---|
83 | */ |
---|
84 | spi_dev_config ( |
---|
85 | _spi_device , |
---|
86 | spi_func_clkfreq , |
---|
87 | RESET_SYSTEM_CLK * 1000, |
---|
88 | -1 , |
---|
89 | -1 , |
---|
90 | -1 |
---|
91 | ); |
---|
92 | |
---|
93 | reset_puts("Finish block device initialization\n\r"); |
---|
94 | |
---|
95 | return 0; |
---|
96 | } /* end reset_spi_init() */ |
---|
97 | |
---|
98 | int reset_sdc_read( unsigned int lba, void* buffer, unsigned int count ) |
---|
99 | { |
---|
100 | unsigned int sdcard_rsp; |
---|
101 | unsigned int i; |
---|
102 | |
---|
103 | sdcard_dev_lseek(&_sdcard_device, lba); |
---|
104 | for(i = 0; i < count; i++) |
---|
105 | { |
---|
106 | unsigned char* buf = (unsigned char *) buffer + (512 * i); |
---|
107 | if (( sdcard_rsp = sdcard_dev_read ( &_sdcard_device, buf, 512 ) )) |
---|
108 | { |
---|
109 | reset_puts("ERROR during read on the SDCARD device. Code: "); |
---|
110 | reset_putx(sdcard_rsp); |
---|
111 | reset_puts("\n"); |
---|
112 | return 1; |
---|
113 | } |
---|
114 | } |
---|
115 | return 0; |
---|
116 | } |
---|
117 | |
---|
118 | /* |
---|
119 | * vim: tabstop=4 : softtabstop=4 : shiftwidth=4 : expandtab |
---|
120 | */ |
---|