Advertisement
Guest User

Untitled

a guest
Jan 24th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. /**
  2.  * \ingroup sd_raw
  3.  * Sends a raw byte to the memory card.
  4.  *
  5.  * \param[in] b The byte to sent.
  6.  * \see sd_raw_rec_byte
  7.  */
  8. void sd_raw_send_byte(uint8_t b)
  9. {
  10.     SPDR = b;
  11.     /* wait for byte to be shifted out */
  12.     while(!(SPSR & (1 << SPIF)));
  13.     SPSR &= ~(1 << SPIF);
  14. }
  15.  
  16. /* FORCE ERASE */
  17. uint8_t send_cmd42_erase(){
  18.         uint8_t response,i;
  19.         uint8_t arg = 0x08;
  20.         uint8_t command = 0x2a;
  21.         uint16_t crc = calc_crc(mess,((command&arg)|command),CRC16STARTBIT);
  22.         /* wait some clock cycles */
  23.         sd_raw_rec_byte();
  24.         Serial.print("Starting erase procedure");
  25.         select_card(); // select SD card first
  26.        
  27.         sd_raw_send_byte(0x40 | 0x10);
  28.         sd_raw_send_byte(0x01 & 0xff);
  29.         sd_raw_send_byte(0x40 | 0x2a);
  30.         sd_raw_send_byte((arg >> 0) & 0xff);
  31.         /*CRC16 CCITT*/
  32.         //CRC16 is calculated over data given/sent (block length*n_frames_sent)
  33.         //SPI does not force CRC checking, but i'll enable it anyway
  34.         //CRC16=size(command+argument) (38 bits)
  35.         sd_raw_send_byte((crc >> 8) & 0xff);
  36.         sd_raw_send_byte((crc >> 0) & 0xff);
  37.        
  38.         /* receive response */
  39.         for(i = 0; i < 10; ++i)
  40.         {
  41.             response = sd_raw_rec_byte();
  42.             if(response != 0xff)
  43.                 break;
  44.         }
  45.        
  46.         return response;
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement