CuVoodoo STM32F1 firmware template
flash_sdcard.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
25 /* standard libraries */
26 #include <stdint.h> // standard integer types
27 #include <stdlib.h> // general utilities
28 
29 /* STM32 (including CM3) libraries */
30 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
31 #include <libopencm3/stm32/rcc.h> // real-time control clock library
32 #include <libopencm3/stm32/gpio.h> // general purpose input output library
33 #include <libopencm3/stm32/spi.h> // SPI library
34 
35 #include "global.h" // global utilities
36 #include "flash_sdcard.h" // SD card header and definitions
37 
41 #define FLASH_SDCARD_SPI 1
45 static bool initialized = false;
46 
49 static uint32_t n_ac = 8;
54 static bool sdsc = false;
56 static uint64_t sdcard_size = 0;
58 static uint32_t erase_size = 0;
59 
64 static const uint8_t crc7_table[] = {
65  0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77,
66  0x19, 0x10, 0x0b, 0x02, 0x3d, 0x34, 0x2f, 0x26, 0x51, 0x58, 0x43, 0x4a, 0x75, 0x7c, 0x67, 0x6e,
67  0x32, 0x3b, 0x20, 0x29, 0x16, 0x1f, 0x04, 0x0d, 0x7a, 0x73, 0x68, 0x61, 0x5e, 0x57, 0x4c, 0x45,
68  0x2b, 0x22, 0x39, 0x30, 0x0f, 0x06, 0x1d, 0x14, 0x63, 0x6a, 0x71, 0x78, 0x47, 0x4e, 0x55, 0x5c,
69  0x64, 0x6d, 0x76, 0x7f, 0x40, 0x49, 0x52, 0x5b, 0x2c, 0x25, 0x3e, 0x37, 0x08, 0x01, 0x1a, 0x13,
70  0x7d, 0x74, 0x6f, 0x66, 0x59, 0x50, 0x4b, 0x42, 0x35, 0x3c, 0x27, 0x2e, 0x11, 0x18, 0x03, 0x0a,
71  0x56, 0x5f, 0x44, 0x4d, 0x72, 0x7b, 0x60, 0x69, 0x1e, 0x17, 0x0c, 0x05, 0x3a, 0x33, 0x28, 0x21,
72  0x4f, 0x46, 0x5d, 0x54, 0x6b, 0x62, 0x79, 0x70, 0x07, 0x0e, 0x15, 0x1c, 0x23, 0x2a, 0x31, 0x38,
73  0x41, 0x48, 0x53, 0x5a, 0x65, 0x6c, 0x77, 0x7e, 0x09, 0x00, 0x1b, 0x12, 0x2d, 0x24, 0x3f, 0x36,
74  0x58, 0x51, 0x4a, 0x43, 0x7c, 0x75, 0x6e, 0x67, 0x10, 0x19, 0x02, 0x0b, 0x34, 0x3d, 0x26, 0x2f,
75  0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c, 0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04,
76  0x6a, 0x63, 0x78, 0x71, 0x4e, 0x47, 0x5c, 0x55, 0x22, 0x2b, 0x30, 0x39, 0x06, 0x0f, 0x14, 0x1d,
77  0x25, 0x2c, 0x37, 0x3e, 0x01, 0x08, 0x13, 0x1a, 0x6d, 0x64, 0x7f, 0x76, 0x49, 0x40, 0x5b, 0x52,
78  0x3c, 0x35, 0x2e, 0x27, 0x18, 0x11, 0x0a, 0x03, 0x74, 0x7d, 0x66, 0x6f, 0x50, 0x59, 0x42, 0x4b,
79  0x17, 0x1e, 0x05, 0x0c, 0x33, 0x3a, 0x21, 0x28, 0x5f, 0x56, 0x4d, 0x44, 0x7b, 0x72, 0x69, 0x60,
80  0x0e, 0x07, 0x1c, 0x15, 0x2a, 0x23, 0x38, 0x31, 0x46, 0x4f, 0x54, 0x5d, 0x62, 0x6b, 0x70, 0x79
81 };
82 
85 static void flash_sdcard_spi_wait(void)
86 {
87  spi_send(SPI(FLASH_SDCARD_SPI), 0xffff); // send not command token (i.e. starting with 1)
88 }
89 
93 static uint16_t flash_sdcard_spi_read(void)
94 {
95  spi_send(SPI(FLASH_SDCARD_SPI), 0xffff); // send not command token (i.e. starting with 1)
96  (void)SPI_DR(SPI(FLASH_SDCARD_SPI)); // clear RXNE flag (by reading previously received data (not done by spi_read or spi_xref)
97  while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_TXE)); // wait until Tx buffer is empty before clearing the (previous) RXNE flag
98  while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_RXNE)); // wait for next data to be available
99  return SPI_DR(SPI(FLASH_SDCARD_SPI)); // return received adat
100 }
101 
106 static bool flash_sdcard_card_detect(void)
107 {
108  rcc_periph_clock_enable(RCC_SPI_NSS_PORT(FLASH_SDCARD_SPI)); // enable clock for NSS pin port peripheral for SD card CD signal
109  gpio_set_mode(SPI_NSS_PORT(FLASH_SDCARD_SPI), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set NSS pin as input to read CD signal
110  gpio_clear(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI)); // pull pin low to avoid false positive when card in not inserted
111  return (0!=gpio_get(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI))); // read CD signal: is card is present the internal 50 kOhm pull-up resistor will override our 1 MOhm pull-down resistor and set the signal high (see section 6.2)
112 }
113 
118 static void flash_sdcard_send_command(uint8_t index, uint32_t argument)
119 {
120  uint8_t command[5] = { 0x40+(index&0x3f), argument>>24, argument>>16, argument>>8, argument>>0 }; // commands are 5 bytes long, plus 1 bytes of CRC (see section 7.3.1.1)
121  uint8_t crc7 = 0x00; // CRC-7 checksum for command message
122  // send command
123  for (uint8_t i=0; i<LENGTH(command); i++) {
124  spi_send(SPI(FLASH_SDCARD_SPI), command[i]); // send data
125  crc7 = (crc7_table[((crc7<<1)^command[i])])&0x7f; // update checksum
126  }
127  spi_send(SPI(FLASH_SDCARD_SPI), (crc7<<1)+0x01); // send CRC value (see section 7.3.1.1)
128 }
129 
137 static uint8_t flash_sdcard_command_response(uint8_t index, uint32_t argument, uint8_t* response, size_t size)
138 {
139  // send command token
140  gpio_clear(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set CS low to select slave and start SPI mode (see section 7.2)
141  flash_sdcard_spi_wait(); // wait for N_CS (min. 0, but it works better with 8 clock cycles) before writing command (see section 7.5.1.1)
142  flash_sdcard_send_command(index, argument); // send command token
143 
144  // get response token R1
145  uint8_t r1 = 0xff; // response token R1 (see section 7.3.2.1)
146  for (uint8_t i=0; i<8 && r1&0x80; i++) { // wait for N_CR (1 to 8 8 clock cycles) before reading response (see section 7.5.1.1)
147  r1 = flash_sdcard_spi_read(); // get response (see section 7.3.2.1)
148  }
149  if (0x00==(r1&0xfe) && 0!=size && NULL!=response) { // we have to read a response
150  for (size_t i=0; i<size; i++) {
151  response[i] = flash_sdcard_spi_read(); // get byte
152  }
153  }
154 
155  // end communication
156  while (SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_BSY); // wait until not busy (= transmission completed)
157  // wait for N_EC (min. 0) before closing communication (see section 7.5.1.1)
158  gpio_set(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set CS high to unselect card
159  // wait for N_DS (min. 0) before allowing any further communication (see section 7.5.1.1)
160  return r1;
161 }
162 
168 static uint8_t flash_sdcard_read_block(uint8_t* data, size_t size)
169 {
170  if (size%2 || 0==size || NULL==data) { // can't (and shouldn't) read odd number of bytes
171  return 0xff;
172  }
173 
174  uint8_t token = 0xff; // to save the control block token (see section 7.3.3)
175  for (uint32_t i=0; i<n_ac && token==0xff; i++) { // wait for N_AC before reading data block (see section 7.5.2.1)
176  token = flash_sdcard_spi_read(); // get control token (see section 7.3.3)
177  }
178  if (0==(token&0xf0)) { // data error token received (see section 7.3.3.3)
179  if (0==(token&0x0f)) { // unknown error
180  token = 0xff;
181  }
182  } else if (0xfe==token) { // start block token received (see section 7.3.3.2)
183  // switch to 16-bits SPI data frame so we can use use built-in CRC-16
184  while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_TXE)); // wait until the end of any transmission
185  while (SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_BSY); // wait until not busy before disabling
186  spi_disable(SPI(FLASH_SDCARD_SPI)); // disable SPI to change format
187  spi_set_dff_16bit(SPI(FLASH_SDCARD_SPI)); // set SPI frame to 16 bits
188  SPI_CRC_PR(FLASH_SDCARD_SPI) = 0x1021; // set CRC-16-CCITT polynomial for data blocks (x^16+x^12+x^5+1) (see section 7.2.3)
189  spi_enable_crc(SPI(FLASH_SDCARD_SPI)); // enable and clear CRC
190  spi_enable(SPI(FLASH_SDCARD_SPI)); // enable SPI back
191  // get block data (ideally use DMA, but switching makes it more complex and this part doesn't take too much time)
192  for (size_t i=0; i<size/2; i++) {
193  uint16_t word = flash_sdcard_spi_read(); // get word
194  data[i*2+0] = (word>>8); // save byte
195  data[i*2+1] = (word>>0); // save byte
196  }
197  flash_sdcard_spi_read(); // read CRC (the CRC after the data block should clear the computed CRC)
198  if (SPI_CRC_RXR(FLASH_SDCARD_SPI)) { // CRC is wrong
199  token = 0xff;
200  } else { // no error occurred
201  token = 0;
202  }
203  // switch back to 8-bit SPI frames
204  while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_TXE)); // wait until the end of any transmission
205  while (SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_BSY); // wait until not busy before disabling
206  spi_disable(SPI(FLASH_SDCARD_SPI)); // disable SPI to change format
207  spi_disable_crc(SPI(FLASH_SDCARD_SPI)); // disable CRC since we don't use it anymore (and this allows us to clear the CRC next time we use it)
208  spi_set_dff_8bit(SPI(FLASH_SDCARD_SPI)); // set SPI frame to 8 bits
209  spi_enable(SPI(FLASH_SDCARD_SPI)); // enable SPI back
210  } else { // start block token not received
211  token = 0xff;
212  }
213 
214  return token;
215 }
216 
222 static uint8_t flash_sdcard_write_block(uint8_t* data, size_t size)
223 {
224  if (size%2 || 0==size || NULL==data) { // can't (and shouldn't) read odd number of bytes
225  return 0xff;
226  }
227 
228  spi_send(SPI(FLASH_SDCARD_SPI), 0xfe); // send start block token (see section 7.3.3.2)
229 
230  // switch to 16-bits SPI data frame so we can use use built-in CRC-16
231  while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_TXE)); // wait until the end of any transmission
232  while (SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_BSY); // wait until not busy before disabling
233  spi_disable(SPI(FLASH_SDCARD_SPI)); // disable SPI to change format
234  spi_set_dff_16bit(SPI(FLASH_SDCARD_SPI)); // set SPI frame to 16 bits
235  SPI_CRC_PR(FLASH_SDCARD_SPI) = 0x1021; // set CRC-16-CCITT polynomial for data blocks (x^16+x^12+x^5+1) (see section 7.2.3)
236  spi_enable_crc(SPI(FLASH_SDCARD_SPI)); // enable and clear CRC
237  spi_enable(SPI(FLASH_SDCARD_SPI)); // enable SPI back
238  // send block data (ideally use DMA, but switching makes it more complex and this part doesn't take too much time)
239  for (size_t i=0; i<size/2; i++) {
240  uint16_t word = (data[i*2+0]<<8)+data[i*2+1]; // prepare SPI frame
241  spi_send(SPI(FLASH_SDCARD_SPI), word); // senf data frame
242  }
243  spi_set_next_tx_from_crc(SPI(FLASH_SDCARD_SPI)); // send CRC
244  // switch back to 8-bit SPI frames
245  while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_TXE)); // wait until the end of any transmission
246  while (SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_BSY); // wait until not busy before disabling
247  spi_disable(SPI(FLASH_SDCARD_SPI)); // disable SPI to change format
248  spi_set_next_tx_from_buffer(SPI(FLASH_SDCARD_SPI)); // don't send CRC
249  spi_disable_crc(SPI(FLASH_SDCARD_SPI)); // disable CRC since we don't use it anymore (and this allows us to clear the CRC next time we use it)
250  spi_set_dff_8bit(SPI(FLASH_SDCARD_SPI)); // set SPI frame to 8 bits
251  spi_enable(SPI(FLASH_SDCARD_SPI)); // enable SPI back
252 
253  uint8_t token = 0xff;
254  while (0x01!=(token&0x11)) {
255  token = flash_sdcard_spi_read(); // get data response token (see section 7.3.3.1)
256  }
257  while (0==flash_sdcard_spi_read()); // wait N_EC while the card is busy programming the data
258 
259  return token;
260 }
265 static uint16_t flash_sdcard_status(uint8_t* status)
266 {
267  // send CMD55 (APP_CMD) to issue following application command (see table 7-4)
268  uint8_t r1 = flash_sdcard_command_response(55, 0, NULL, 0); // (see table 7-3)
269  if ((r1&0xfe)) { // error occurred, not in idle state
270  return false;
271  }
272 
273  // send ACMD13 command
274  gpio_clear(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set CS low to select slave and start SPI mode (see section 7.2)
275  flash_sdcard_spi_wait(); // wait for N_CS (min. 0, but it works better with 8 clock cycles) before writing command (see section 7.5.2.1)
276  flash_sdcard_send_command(13, 0); // send ACMD13 (SD_STATUS) (see table 7-4)
277 
278  // get response token R2
279  uint16_t r2 = 0xffff; // response token R2 (see section 7.3.2.3)
280  for (uint8_t i=0; i<8 && r2&0x8000; i++) { // wait for N_CR (1 to 8 8 clock cycles) before reading response (see section 7.5.1.1)
281  r2 = (flash_sdcard_spi_read()<<8); // get first byte of response (see section 7.3.2.1)
282  }
283  if (0==(r2&0x8000)) { // got the first byte
284  r2 += flash_sdcard_spi_read(); // read second byte (see 7.3.2.3)
285  }
286 
287  // get data block
288  if (0==r2) { // no error
289  if (flash_sdcard_read_block(status, 64)) { // read 512 bits data block containing SD status
290  r2 |= (1<<11); // set communication error
291  }
292  }
293 
294  // end communication
295  while (SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_BSY); // wait until not busy (= transmission completed)
296  // wait for N_EC (min. 0) before closing communication (see section 7.5.1.1)
297  gpio_set(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set CS high to unselect card
298  // wait for N_DS (min. 0) before allowing any further communication (see section 7.5.1.1)
299  return r2;
300 }
301 
309 static uint8_t flash_sdcard_data_read(uint8_t index, uint32_t argument, uint8_t* data, size_t size)
310 {
311  if (size%2 || 0==size || NULL==data) { // can't (and shouldn't) read odd number of bytes
312  return 0xff;
313  }
314 
315  // send command token
316  gpio_clear(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set CS low to select slave and start SPI mode (see section 7.2)
317  flash_sdcard_spi_wait(); // wait for N_CS (min. 0, but it works better with 8 clock cycles) before writing command (see section 7.5.2.1)
318  flash_sdcard_send_command(index, argument); // send command token
319 
320  // get response token R1
321  uint8_t r1 = 0xff; // response token R1 (see section 7.3.2.1)
322  for (uint8_t i=0; i<8 && r1&0x80; i++) { // wait for N_CR (1 to 8 8 clock cycles) before reading response (see section 7.5.1.1)
323  r1 = flash_sdcard_spi_read(); // get response (see section 7.3.2.1)
324  }
325 
326  // get data block
327  if (0x00==r1) { // we can read a data block
328  if (flash_sdcard_read_block(data, size)) { // read data block
329  r1 |= (1<<3); // set communication error
330  }
331  }
332 
333  // end communication
334  while (SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_BSY); // wait until not busy (= transmission completed)
335  // wait for N_EC (min. 0) before closing communication (see section 7.5.1.1)
336  gpio_set(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set CS high to unselect card
337  // wait for N_DS (min. 0) before allowing any further communication (see section 7.5.1.1)
338  return r1;
339 }
340 
349 static uint8_t flash_sdcard_data_write(uint8_t index, uint32_t argument, uint8_t* data, size_t size)
350 {
351  if (size%2 || 0==size || NULL==data) { // can't (and shouldn't) write odd number of bytes
352  return 0xff;
353  }
354 
355  // send command token
356  gpio_clear(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set CS low to select slave and start SPI mode (see section 7.2)
357  flash_sdcard_spi_wait(); // wait for N_CS (min. 0, but it works better with 8 clock cycles) before writing command (see section 7.5.2.1)
358  flash_sdcard_send_command(index, argument); // send command token
359 
360  // get response token R1
361  uint8_t r1 = 0xff; // response token R1 (see section 7.3.2.1)
362  for (uint8_t i=0; i<8 && r1&0x80; i++) { // wait for N_CR (1 to 8 8 clock cycles) before reading response (see section 7.5.1.1)
363  r1 = flash_sdcard_spi_read(); // get response (see section 7.3.2.1)
364  }
365 
366  // write data block
367  uint8_t drt = 0xff; // data response token (see section 7.3.3.1)
368  if (0x00==r1) { // we have to write the data block
369  drt = flash_sdcard_write_block(data, size); // write data block
370  }
371 
372  // end communication
373  while (SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_BSY); // wait until not busy (= transmission completed)
374  // wait for N_EC (min. 0) before closing communication (see section 7.5.1.1)
375  gpio_set(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set CS high to unselect card
376  // wait for N_DS (min. 0) before allowing any further communication (see section 7.5.1.1)
377 
378  return drt;
379 }
380 
382 {
383  // reset values
384  initialized = false;
385  n_ac = 8;
386  sdcard_size = 0;
387  erase_size = 0;
388 
389  // check if card is present
390  if (!flash_sdcard_card_detect()) {
391  return false;
392  }
393 
394  // configure SPI peripheral
395  rcc_periph_clock_enable(RCC_SPI_SCK_PORT(FLASH_SDCARD_SPI)); // enable clock for GPIO peripheral for clock signal
396  gpio_set_mode(SPI_SCK_PORT(FLASH_SDCARD_SPI), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, SPI_SCK_PIN(FLASH_SDCARD_SPI)); // set SCK as output (clock speed will be negotiated later)
397  rcc_periph_clock_enable(RCC_SPI_MOSI_PORT(FLASH_SDCARD_SPI)); // enable clock for GPIO peripheral for MOSI signal
398  gpio_set_mode(SPI_MOSI_PORT(FLASH_SDCARD_SPI), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, SPI_MOSI_PIN(FLASH_SDCARD_SPI)); // set MOSI as output
399  rcc_periph_clock_enable(RCC_SPI_MISO_PORT(FLASH_SDCARD_SPI)); // enable clock for GPIO peripheral for MISO signal
400  gpio_set_mode(SPI_MISO_PORT(FLASH_SDCARD_SPI), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, SPI_MISO_PIN(FLASH_SDCARD_SPI)); // set MISO as input
401  gpio_set(SPI_MISO_PORT(FLASH_SDCARD_SPI), SPI_MISO_PIN(FLASH_SDCARD_SPI)); // pull pin high to detect when the card is not answering (or not present) since responses always start with MSb 0
402  rcc_periph_clock_enable(RCC_SPI_NSS_PORT(FLASH_SDCARD_SPI)); // enable clock for GPIO peripheral for NSS (CS) signal
403  gpio_set_mode(SPI_NSS_PORT(FLASH_SDCARD_SPI), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set NSS (CS) as output
404  rcc_periph_clock_enable(RCC_AFIO); // enable clock for SPI alternate function
405  rcc_periph_clock_enable(RCC_SPI(FLASH_SDCARD_SPI)); // enable clock for SPI peripheral
406  spi_reset(SPI(FLASH_SDCARD_SPI)); // clear SPI values to default
407  spi_init_master(SPI(FLASH_SDCARD_SPI), SPI_CR1_BAUDRATE_FPCLK_DIV_256, SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE, SPI_CR1_CPHA_CLK_TRANSITION_1, SPI_CR1_DFF_8BIT, SPI_CR1_MSBFIRST); // initialise SPI as master, divide clock by 256 (72E6/256=281 kHz) since maximum SD card clock frequency (fOD, see section 7.8/6.6.6) during initial card-identification mode is 400 kHz (maximum SPI PCLK clock is 72 Mhz, depending on which SPI is used), set clock polarity to idle low (not that important), set clock phase to do bit change on falling edge (from SD card spec, polarity depends on clock phase), use 8 bits frames (as per spec), use MSb first
408  spi_set_full_duplex_mode(SPI(FLASH_SDCARD_SPI)); // ensure we are in full duplex mode
409  spi_enable_software_slave_management(SPI(FLASH_SDCARD_SPI)); // control NSS (CS) manually
410  spi_set_nss_high(SPI(FLASH_SDCARD_SPI)); // set NSS high (internally) so we can output
411  spi_disable_ss_output(SPI(FLASH_SDCARD_SPI)); // disable NSS output since we control CS manually
412  gpio_set(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set CS high to unselect card
413  // sadly we can't use the interrupts as events to sleep (WFE) since sleep disables also communication (e.g. going to sleep until Rx buffer is not empty prevents transmission)
414  spi_enable(SPI(FLASH_SDCARD_SPI)); // enable SPI
415 
416  // start card-identification (see section 7.2.1/4.2)
417  uint8_t r1 = 0;
418  // send CMD0 (GO_IDLE_START) to start the card identification (see section 7.2.1)
419  r1 = flash_sdcard_command_response(0, 0, NULL, 0); // (see table 7-3)
420  if (0x01!=r1) { // error occurred, not in idle state
421  return false;
422  }
423  // send CMD8 (SEND_IF_COND) to inform about voltage (1: 2.7-3.6V, aa: recommended check pattern) (see section 7.2.1)
424  uint8_t r7[4] = {0}; // to store response toke R7 (see section 7.3.2.6)
425  r1 = flash_sdcard_command_response(8, 0x000001aa, r7, sizeof(r7)); // (see table 7-3)
426  if (0x01==r1) { // command supported, in idle state
427  if (!(r7[2]&0x1)) { // 2.7-3.6V not supported (see table 5-1)
428  return false;
429  } else if (0xaa!=r7[3]) { // recommended pattern not returned (see section 4.3.13)
430  return false;
431  }
432  } else if (0x05!=r1) { // illegal command (cards < physical spec v2.0 don't support CMD8) (see section 7.2.1)
433  return false;
434  }
435  // send CMD58 (READ_OCR) to read Operation Conditions Register (see section 7.2.1)
436  uint8_t r3[4] = {0}; // to store response token R3 (see section 7.3.2.4)
437  r1 = flash_sdcard_command_response(58, 0, r3, sizeof(r3)); // (see table 7-3)
438  if (0x01!=r1) { // error occurred, not in idle state
439  return false;
440  } else if (!(r3[1]&0x30)) { // 3.3V not supported (see table 5-1)
441  return false;
442  }
443  do {
444  // send CMD55 (APP_CMD) to issue following application command (see table 7-4)
445  r1 = flash_sdcard_command_response(55, 0, NULL, 0); // (see table 7-3)
446  if (0x01!=r1) { // error occurred, not in idle state
447  return false;
448  }
449  // send ACMD41 (SD_SEND_OP_COND) with Host Capacity Support (0b: SDSC Only Host, 1b: SDHC or SDXC Supported) (see section 7.2.1)
450  r1 = flash_sdcard_command_response(41, 0x40000000, NULL, 0); // (see table 7-4)
451  if (r1&0xfe) { // error occurred
452  return false;
453  }
454  } while (0x00!=r1); // wait until card is ready (see section 7.2.1)
455  // send CMD58 (READ_OCR) to read Card Capacity Status (CCS) (see section 7.2.1)
456  r1 = flash_sdcard_command_response(58, 0, r3, sizeof(r3)); // (see table 7-3)
457  if (r1) { // error occurred
458  return false;
459  }
460  // card power up status bit (bit 31) is set when power up is complete (see table 5-1)
461  if (0x00==(r3[0]&0x80)) {
462  return false;
463  }
464  sdsc = (0==(r3[0]&0x40)); // CCS is bit 30 in OCR (see table 5-1)
465  // now the card identification is complete and we should be in data-transfer mode (see figure 7-1)
466 
467  // we can switch clock frequency to fPP (max. 25 MHz) (see section 4.3/6.6.6)
468  while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_TXE)); // wait until the end of any transmission
469  while (SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_BSY); // wait until not busy before disabling
470  spi_disable(SPI(FLASH_SDCARD_SPI)); // disable SPI to change clock speed
471  spi_set_baudrate_prescaler(SPI(FLASH_SDCARD_SPI), SPI_CR1_BR_FPCLK_DIV_4); // set clock speed to 18 MHz (72/4=18, < 25 MHz)
472  spi_enable(SPI(FLASH_SDCARD_SPI)); // enable SPI back
473 
474  // send CMD9 (SEND_CSD) to get Card Specific Data (CSD) and calculate N_AC (see section 7.2.6)
475  uint8_t csd[16] = {0}; // CSD response (see chapter 7.2.6)
476  r1 = flash_sdcard_data_read(9, 0, csd, sizeof(csd)); // (see table 7-3)
477  if (r1) { // error occurred
478  return false;
479  }
480  // check if CSD structure version matches capacity (see section 5.3.1)
481  if ((sdsc && (csd[0]>>6)) || (!sdsc && 0==(csd[0]>>6))) {
482  return false;
483  }
484  // calculate N_AC value (we use our set minimum frequency 16 MHz to calculate time)
485  if (sdsc) { // calculate N_AC using TAAC and NSAC
486  static const float TAAC_UNITS[] = {1E-9, 10E-9, 100E-9, 1E-6, 10E-6, 100E-6, 1E-3, 10E-3}; // (see table 5-5)
487  static const float TAAC_VALUES[] = {10.0, 1.0, 1.2, 1.3, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 7.0, 8.0}; // (see table 5-5)
488  double taac = TAAC_VALUES[(csd[1]>>2)&0xf]*TAAC_UNITS[csd[1]&0x7]; // time in ns
489  n_ac=100*((taac*16E6)+(csd[2]*100))/8; // (see section 7.5.4)
490  } else { // value is fixed to 100 ms
491  n_ac=100E-3*16E6/8;
492  }
493  // calculate card size
494  if (sdsc) { // see section 5.3.2
495  uint16_t c_size = (((uint16_t)csd[6]&0x03)<<10)+((uint16_t)csd[7]<<2)+(csd[8]>>6);
496  uint8_t c_size_mutl = ((csd[9]&0x03)<<1)+((csd[10]&0x80)>>7);
497  uint8_t read_bl_len = (csd[5]&0x0f);
498  sdcard_size = ((c_size+1)*(1UL<<(c_size_mutl+2)))*(1UL<<read_bl_len);
499  } else { // see section 5.3.3
500  uint32_t c_size = ((uint32_t)(csd[7]&0x3f)<<16)+((uint16_t)csd[8]<<8)+csd[9];
501  sdcard_size = (c_size+1)*(512<<10);
502  }
503  // calculate erase size
504  if (sdsc) { // see section 5.3.2
505  erase_size = (((csd[10]&0x3f)<<1)+((csd[11]&0x80)>>7)+1)<<(((csd[12]&0x03)<<2)+(csd[13]>>6));
506  } else {
507  uint8_t status[64] = {0}; // SD status (see section 4.10.2)
508  uint16_t r2 = flash_sdcard_status(status); // get status (see table 7-4)
509  if (r2) { // error occurred
510  return false;
511  }
512  erase_size = (8192UL<<(status[10]>>4)); // calculate erase size (see table 4-44, section 4.10.2.4)
513  }
514 
515  // ensure block length is 512 bytes for SDSC (should be per default) to we match SDHC/SDXC block size
516  if (sdsc) {
517  r1 = flash_sdcard_command_response(16, 512, NULL, 0); // set block size using CMD16 (SET_BLOCKLEN) (see table 7-3)
518  if (r1) { // error occurred
519  return false;
520  }
521  }
522 
523  // try to switch to high speed mode (see section 7.2.14/4.3.10)
524  if (csd[4]&0x40) { // ensure CMD6 is supported by checking if command class 10 is set
525  uint32_t n_ac_back = n_ac; // backup N_AC
526  n_ac = 100E-3*16E6/8; // temporarily set timeout to 100 ms (see section 4.3.10.1)
527  // query access mode (group function 1) to check if high speed is supported (fPP=50MHz at 3.3V, we can be faster)
528  uint8_t fnc[64] = {0}; // function status response (see table 4-12)
529  r1 = flash_sdcard_data_read(6, 0x00fffff1, fnc, sizeof(fnc)); // check high speed function using CMD6 (SWITCH_FUNC) to check (mode 0) access mode (function group 1) (see table 7-3/4-30)
530  if (r1) { // error occurred
531  return false;
532  }
533  if (0x1==(fnc[16]&0x0f)) { // we can to access mode function 1 (see table 4-12)
534  r1 = flash_sdcard_data_read(6, 0x80fffff1, fnc, sizeof(fnc)); // switch to high speed function using CMD6 (SWITCH_FUNC) to switch (mode 1) access mode (function group 1) (see table 7-3/4-30)
535  if (r1) { // error occurred
536  return false;
537  }
538  if (0x1!=(fnc[16]&0x0f)) { // could not switch to high speed
539  return false;
540  }
541  // we can switch clock frequency to fPP (max. 50 MHz) (see section 6.6.7)
542  while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_TXE)); // wait until the end of any transmission
543  while (SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_BSY); // wait until not busy before disabling
544  spi_disable(SPI(FLASH_SDCARD_SPI)); // disable SPI to change clock speed
545  spi_set_baudrate_prescaler(SPI(FLASH_SDCARD_SPI), SPI_CR1_BR_FPCLK_DIV_2); // set clock speed to 36 MHz (72/2=36 < 50 MHz)
546  spi_enable(SPI(FLASH_SDCARD_SPI)); // enable SPI back
547  n_ac_back /= 2; // since we go twice faster the N_AC timeout has to be halved
548  }
549  n_ac = n_ac_back; // restore N_AC
550  }
551 
552  initialized = true;
553 
554  return initialized;
555 }
556 
557 uint64_t flash_sdcard_size(void)
558 {
559  return sdcard_size;
560 }
561 
563 {
564  return erase_size;
565 }
566 
567 bool flash_sdcard_read_data(uint32_t block, uint8_t* data)
568 {
569  if (NULL==data) {
570  return false;
571  }
572  if (sdsc) { // the address for standard capacity cards must be provided in bytes
573  if (block>UINT32_MAX/512) { // check for integer overflow
574  return false;
575  } else {
576  block *= 512; // calculate byte address from block address
577  }
578  }
579  return (0==flash_sdcard_data_read(17, block, data, 512)); // read single data block using CMD17 (READ_SINGLE_BLOCK) (see table 7-3)
580 }
581 
582 bool flash_sdcard_write_data(uint32_t block, uint8_t* data)
583 {
584  if (NULL==data) {
585  return false;
586  }
587 
588  if (sdsc) { // the address for standard capacity cards must be provided in bytes
589  if (block>UINT32_MAX/512) { // check for integer overflow
590  return false;
591  } else {
592  block *= 512; // calculate byte address from block address
593  }
594  }
595 
596  uint8_t drt = flash_sdcard_data_write(24, block, data, 512); // write single data block using CMD24 (WRITE_SINGLE_BLOCK) (see table 7-3)
597  if (0x05!=(drt&0x1f)) { // write block failed
598  return false;
599  }
600 
601  // get status to check if programming succeeded
602  uint8_t r2[1] = {0}; // to store response token R2 (see section 7.3.2.3)
603  uint8_t r1 = flash_sdcard_command_response(13, 0, r2, sizeof(r2)); // get SD status using CMD13 (SEND_STATUS) (see table 7-3)
604  if (0x00!=r1) { // error occurred
605  return false;
606  } else if (r2[0]) { // programming error
607  return false;
608  }
609 
610  return true; // programming succeeded
611 }
#define SPI_MOSI_PORT(x)
get SPI port for MOSI signal based on SPI identifier
Definition: global.h:301
static uint8_t flash_sdcard_write_block(uint8_t *data, size_t size)
write a data block
Definition: flash_sdcard.c:222
library to communicate with an SD card flash memory using the SPI mode (API)
static bool flash_sdcard_card_detect(void)
test if card is present
Definition: flash_sdcard.c:106
static uint8_t flash_sdcard_data_write(uint8_t index, uint32_t argument, uint8_t *data, size_t size)
transmit command token, receive response token and write data block
Definition: flash_sdcard.c:349
#define SPI_NSS_PORT(x)
get SPI port for NSS signal based on SPI identifier
Definition: global.h:295
#define SPI_CRC_PR(x)
get SPI CRC polynomial register based on SPI identifier
Definition: global.h:311
#define SPI_MOSI_PIN(x)
get SPI pin for MOSI signal based on SPI identifier
Definition: global.h:309
static bool initialized
if the card has been initialized successfully
Definition: flash_sdcard.c:45
static bool sdsc
is it a Standard Capacity SD card (true), or High Capacity SD cards (false)
Definition: flash_sdcard.c:54
global definitions and methods (API)
enum menu_argument_t argument
what kind of argument it accepts
Definition: menu.h:35
#define SPI_CRC_RXR(x)
get SPI CRC receive register based on SPI identifier
Definition: global.h:315
static uint16_t flash_sdcard_status(uint8_t *status)
get card status
Definition: flash_sdcard.c:265
#define RCC_SPI_MISO_PORT(x)
get RCC for GPIO port for SPI MISO signals
Definition: global.h:285
static uint32_t n_ac
maximum N_AC value (in 8-clock cycles) (time between the response token R1 and data block when readin...
Definition: flash_sdcard.c:49
#define RCC_SPI_MOSI_PORT(x)
get RCC for GPIO port for SPI MOSI signals
Definition: global.h:290
#define RCC_SPI(x)
get RCC for SPI based on SPI identifier
Definition: global.h:273
uint64_t flash_sdcard_size(void)
get size of SD card flash memory
Definition: flash_sdcard.c:557
#define SPI_NSS_PIN(x)
get SPI pin for NSS signal based on SPI identifier
Definition: global.h:303
#define SPI_SCK_PORT(x)
get SPI port for SCK signal based on SPI identifier
Definition: global.h:297
static uint16_t flash_sdcard_spi_read(void)
read one SPI word
Definition: flash_sdcard.c:93
static uint8_t flash_sdcard_read_block(uint8_t *data, size_t size)
read a data block
Definition: flash_sdcard.c:168
static void flash_sdcard_spi_wait(void)
wait one SPI round (one SPI word)
Definition: flash_sdcard.c:85
#define SPI_MISO_PORT(x)
get SPI port for MISO signal based on SPI identifier
Definition: global.h:299
#define SPI(x)
get SPI based on SPI identifier
Definition: global.h:271
static uint8_t flash_sdcard_data_read(uint8_t index, uint32_t argument, uint8_t *data, size_t size)
transmit command token, receive response token and data block
Definition: flash_sdcard.c:309
#define RCC_SPI_SCK_PORT(x)
get RCC for GPIO port for SPI SCK signals
Definition: global.h:280
bool flash_sdcard_read_data(uint32_t block, uint8_t *data)
read data on flash of SD card
Definition: flash_sdcard.c:567
#define LENGTH(x)
get the length of an array
Definition: global.h:26
static uint8_t flash_sdcard_command_response(uint8_t index, uint32_t argument, uint8_t *response, size_t size)
transmit command token and receive response token
Definition: flash_sdcard.c:137
bool flash_sdcard_setup(void)
setup communication with SD card
Definition: flash_sdcard.c:381
static uint64_t sdcard_size
size of card in bytes
Definition: flash_sdcard.c:56
#define SPI_MISO_PIN(x)
get SPI pin for MISO signal based on SPI identifier
Definition: global.h:307
bool flash_sdcard_write_data(uint32_t block, uint8_t *data)
write data on flash of SD card
Definition: flash_sdcard.c:582
static uint32_t erase_size
size of an erase block bytes
Definition: flash_sdcard.c:58
#define RCC_SPI_NSS_PORT(x)
get RCC for GPIO port for SPI NSS signals
Definition: global.h:275
static void flash_sdcard_send_command(uint8_t index, uint32_t argument)
transmit command token
Definition: flash_sdcard.c:118
#define FLASH_SDCARD_SPI
SPI peripheral.
Definition: flash_sdcard.c:41
#define SPI_SCK_PIN(x)
get SPI pin for SCK signal based on SPI identifier
Definition: global.h:305
uint32_t flash_sdcard_erase_size(void)
get size of a erase block
Definition: flash_sdcard.c:562
static const uint8_t crc7_table[]
table for CRC-7 calculation for the command messages (see section 4.5)
Definition: flash_sdcard.c:64