Example #1
0
void BootLoaderInit()
{
	unsigned int LoaderLength = sizeof(BootLoader);
	unsigned int CRC1,CRC2;
	
	ROMBootLoader = (const far rom char *)Boot_Loader_Start;
	CRC1 = CRC_CCITT(BootLoader,LoaderLength,(unsigned int)CRC_CCITT_SEED);
	CRC2 = CRC_CCITT(ROMBootLoader,LoaderLength,(unsigned int)CRC_CCITT_SEED);
	

	if(CRC1 != CRC2) {
		BootLoader_Update_ROM();

	}
}
Example #2
0
//------------------------------------------------------------------------------
bool Sd2Card::readData(uint8_t* dst, uint16_t count) {
  // wait for start block token
  uint16_t t0 = millis();
  while ((status_ = spiRec()) == 0XFF) {
    if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) {
      error(SD_CARD_ERROR_READ_TIMEOUT);
      goto fail;
    }
  }
  if (status_ != DATA_START_BLOCK) {
    error(SD_CARD_ERROR_READ);
    goto fail;
  }
  // transfer data
  spiRead(dst, count);

#ifdef SD_CHECK_AND_RETRY
  {
    uint16_t calcCrc = CRC_CCITT(dst, count);
    uint16_t recvCrc = spiRec() << 8;
    recvCrc |= spiRec();
    if (calcCrc != recvCrc)
    {
        error(SD_CARD_ERROR_CRC);
        goto fail;
    }
  }
#else
  // discard CRC
  spiRec();
  spiRec();
#endif
  chipSelectHigh();
  // Send an additional dummy byte, required by Toshiba Flash Air SD Card
  spiSend(0XFF);
  return true;

 fail:
  chipSelectHigh();
  // Send an additional dummy byte, required by Toshiba Flash Air SD Card
  spiSend(0XFF);
  return false;
}
//------------------------------------------------------------------------------
bool SdSpiCard::readData(uint8_t* dst, size_t count) {
#if USE_SD_CRC
  uint16_t crc;
#endif  // USE_SD_CRC
  // wait for start block token
  uint16_t t0 = curTimeMS();
  while ((m_status = spiReceive()) == 0XFF) {
    if (isTimedOut(t0, SD_READ_TIMEOUT)) {
      error(SD_CARD_ERROR_READ_TIMEOUT);
      goto fail;
    }
  }
  if (m_status != DATA_START_BLOCK) {
    error(SD_CARD_ERROR_READ);
    goto fail;
  }
  // transfer data
  if ((m_status = spiReceive(dst, count))) {
    error(SD_CARD_ERROR_DMA);
    goto fail;
  }

#if USE_SD_CRC
  // get crc
  crc = (spiReceive() << 8) | spiReceive();
  if (crc != CRC_CCITT(dst, count)) {
    error(SD_CARD_ERROR_READ_CRC);
    goto fail;
  }
#else
  // discard crc
  spiReceive();
  spiReceive();
#endif  // USE_SD_CRC
  return true;

fail:
  spiStop();
  return false;
}
//------------------------------------------------------------------------------
// send one block of data for write block or write multiple blocks
bool SdSpiCard::writeData(uint8_t token, const uint8_t* src) {
#if USE_SD_CRC
  uint16_t crc = CRC_CCITT(src, 512);
#else  // USE_SD_CRC
  uint16_t crc = 0XFFFF;
#endif  // USE_SD_CRC
  spiSend(token);
  spiSend(src, 512);
  spiSend(crc >> 8);
  spiSend(crc & 0XFF);

  m_status = spiReceive();
  if ((m_status & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
    error(SD_CARD_ERROR_WRITE);
    goto fail;
  }
  return true;

fail:
  spiStop();
  return false;
}