Advertisement
Guest User

Untitled

a guest
Jan 25th, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 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. static  unsigned char  xchg(unsigned char  c)
  17. {
  18.     SPDR = c;
  19.     while ((SPSR & (1<<SPIF)) == 0)  ;
  20.     return  SPDR;
  21. }
  22.  
  23.  
  24. static int8_t  sd_wait_for_data(void)
  25. {
  26.     int16_t             i;
  27.     uint8_t             r;
  28.  
  29.     for (i=0; i<100; i++)
  30.     {
  31.         r = xchg(0xff);
  32.         if (r != 0xff)  break;
  33.     }
  34.     return  (int8_t) r;
  35. }
  36.  
  37.  
  38. static    uint8_t lock()
  39.     {
  40.         uint8_t response,i,r;
  41.         uint32_t arg = 0x01020102;
  42.         uint8_t command = 0x2a;
  43.         uint16_t crc = calc_crc(mess,((command&arg)|command),CRC16STARTBIT);
  44.         Serial.print("Starting lock procedure");
  45.         select_card(); // select SD card first
  46.         sd_raw_rec_byte();
  47.         sd_raw_send_command(CMD_CRC_ON_OFF, 0);
  48.         if(sd_raw_send_command(CMD_SET_BLOCKLEN, 4))
  49.         {
  50.                 Serial.print("\nIMPOSIBLE TO SET_BLOCKLEN to 1 byte\n");
  51.                 unselect_card();
  52.                 return 0;
  53.         }else{
  54.                 Serial.print("\nSET_BLOCKLEN to 1 byte\n");
  55.         }
  56.     xchg(0xfe);
  57.     xchg(0x2a);
  58.     sd_raw_send_byte((arg >> 24) & 0xff);
  59.     sd_raw_send_byte((arg >> 16) & 0xff);
  60.     sd_raw_send_byte((arg >> 8) & 0xff);
  61.     sd_raw_send_byte((arg >> 0) & 0xff);
  62.     /*xchg(arg)*/;                      
  63.     xchg((crc >> 8) & 0xff);  
  64.     xchg((crc >> 0) & 0xff);
  65.     xchg(0xff);
  66.     r=sd_wait_for_data();
  67.     Serial.println(r, BIN);
  68.    
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement