Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * \ingroup sd_raw
- * Sends a raw byte to the memory card.
- *
- * \param[in] b The byte to sent.
- * \see sd_raw_rec_byte
- */
- void sd_raw_send_byte(uint8_t b)
- {
- SPDR = b;
- /* wait for byte to be shifted out */
- while(!(SPSR & (1 << SPIF)));
- SPSR &= ~(1 << SPIF);
- }
- static unsigned char xchg(unsigned char c)
- {
- SPDR = c;
- while ((SPSR & (1<<SPIF)) == 0) ;
- return SPDR;
- }
- static int8_t sd_wait_for_data(void)
- {
- int16_t i;
- uint8_t r;
- for (i=0; i<100; i++)
- {
- r = xchg(0xff);
- if (r != 0xff) break;
- }
- return (int8_t) r;
- }
- static uint8_t lock()
- {
- uint8_t response,i,r;
- uint32_t arg = 0x01020102;
- uint8_t command = 0x2a;
- uint16_t crc = calc_crc(mess,((command&arg)|command),CRC16STARTBIT);
- Serial.print("Starting lock procedure");
- select_card(); // select SD card first
- sd_raw_rec_byte();
- sd_raw_send_command(CMD_CRC_ON_OFF, 0);
- if(sd_raw_send_command(CMD_SET_BLOCKLEN, 4))
- {
- Serial.print("\nIMPOSIBLE TO SET_BLOCKLEN to 1 byte\n");
- unselect_card();
- return 0;
- }else{
- Serial.print("\nSET_BLOCKLEN to 1 byte\n");
- }
- xchg(0xfe);
- xchg(0x2a);
- sd_raw_send_byte((arg >> 24) & 0xff);
- sd_raw_send_byte((arg >> 16) & 0xff);
- sd_raw_send_byte((arg >> 8) & 0xff);
- sd_raw_send_byte((arg >> 0) & 0xff);
- /*xchg(arg)*/;
- xchg((crc >> 8) & 0xff);
- xchg((crc >> 0) & 0xff);
- xchg(0xff);
- r=sd_wait_for_data();
- Serial.println(r, BIN);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement