Exemplo n.º 1
0
//------------------------------------------------------------------------------
bool SdSpiCard::writeBlock(uint32_t blockNumber, const uint8_t* src) {
  SD_TRACE("WB", blockNumber);
  // use address if not SDHC card
  if (type() != SD_CARD_TYPE_SDHC) {
    blockNumber <<= 9;
  }
  if (cardCommand(CMD24, blockNumber)) {
    error(SD_CARD_ERROR_CMD24);
    goto fail;
  }
  if (!writeData(DATA_START_BLOCK, src)) {
    goto fail;
  }


#if CHECK_FLASH_PROGRAMMING
  // wait for flash programming to complete
  if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
    error(SD_CARD_ERROR_WRITE_TIMEOUT);
    goto fail;
  }
  // response is r2 so get and check two bytes for nonzero
  if (cardCommand(CMD13, 0) || spiReceive()) {
    error(SD_CARD_ERROR_CMD13);
    goto fail;
  }
#endif  // CHECK_PROGRAMMING

  spiStop();
  return true;

fail:
  spiStop();
  return false;
}
Exemplo n.º 2
0
//------------------------------------------------------------------------------
bool SdSpiCard::readStop() {
  if (cardCommand(CMD12, 0)) {
    error(SD_CARD_ERROR_CMD12);
    goto fail;
  }
  spiStop();
  return true;

fail:
  spiStop();
  return false;
}
Exemplo n.º 3
0
//------------------------------------------------------------------------------
bool SdSpiCard::writeStop() {
  if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
    goto fail;
  }
  spiSend(STOP_TRAN_TOKEN);
  spiStop();
  return true;

fail:
  error(SD_CARD_ERROR_STOP_TRAN);
  spiStop();
  return false;
}
Exemplo n.º 4
0
/**
 * @brief   Starts a sequential write.
 *
 * @param[in] mmcp      pointer to the @p MMCDriver object
 * @param[in] startblk  first block to write
 *
 * @return              The operation status.
 * @retval HAL_SUCCESS   the operation succeeded.
 * @retval HAL_FAILED    the operation failed.
 *
 * @api
 */
bool mmcStartSequentialWrite(MMCDriver *mmcp, uint32_t startblk) {

  osalDbgCheck(mmcp != NULL);
  osalDbgAssert(mmcp->state == BLK_READY, "invalid state");

  /* Write operation in progress.*/
  mmcp->state = BLK_WRITING;

  spiStart(mmcp->config->spip, mmcp->config->hscfg);
  spiSelect(mmcp->config->spip);
  if (mmcp->block_addresses) {
    send_hdr(mmcp, MMCSD_CMD_WRITE_MULTIPLE_BLOCK, startblk);
  }
  else {
    send_hdr(mmcp, MMCSD_CMD_WRITE_MULTIPLE_BLOCK,
             startblk * MMCSD_BLOCK_SIZE);
  }

  if (recvr1(mmcp) != 0x00U) {
    spiStop(mmcp->config->spip);
    mmcp->state = BLK_READY;
    return HAL_FAILED;
  }
  return HAL_SUCCESS;
}
Exemplo n.º 5
0
/**
 * @brief   Writes a block within a sequential write operation.
 *
 * @param[in] mmcp      pointer to the @p MMCDriver object
 * @param[out] buffer   pointer to the write buffer
 *
 * @return              The operation status.
 * @retval HAL_SUCCESS   the operation succeeded.
 * @retval HAL_FAILED    the operation failed.
 *
 * @api
 */
bool mmcSequentialWrite(MMCDriver *mmcp, const uint8_t *buffer) {
  static const uint8_t start[] = {0xFF, 0xFC};
  uint8_t b[1];

  osalDbgCheck((mmcp != NULL) && (buffer != NULL));

  if (mmcp->state != BLK_WRITING) {
    return HAL_FAILED;
  }

  spiSend(mmcp->config->spip, sizeof(start), start);    /* Data prologue.   */
  spiSend(mmcp->config->spip, MMCSD_BLOCK_SIZE, buffer);/* Data.            */
  spiIgnore(mmcp->config->spip, 2);                     /* CRC ignored.     */
  spiReceive(mmcp->config->spip, 1, b);
  if ((b[0] & 0x1FU) == 0x05U) {
    wait(mmcp);
    return HAL_SUCCESS;
  }

  /* Error.*/
  spiUnselect(mmcp->config->spip);
  spiStop(mmcp->config->spip);
  mmcp->state = BLK_READY;
  return HAL_FAILED;
}
Exemplo n.º 6
0
/**
 * @brief   Erases blocks.
 *
 * @param[in] mmcp      pointer to the @p MMCDriver object
 * @param[in] startblk  starting block number
 * @param[in] endblk    ending block number
 *
 * @return              The operation status.
 * @retval HAL_SUCCESS   the operation succeeded.
 * @retval HAL_FAILED    the operation failed.
 *
 * @api
 */
bool mmcErase(MMCDriver *mmcp, uint32_t startblk, uint32_t endblk) {

  osalDbgCheck((mmcp != NULL));

  /* Erase operation in progress.*/
  mmcp->state = BLK_WRITING;

  /* Handling command differences between HC and normal cards.*/
  if (!mmcp->block_addresses) {
    startblk *= MMCSD_BLOCK_SIZE;
    endblk *= MMCSD_BLOCK_SIZE;
  }

  if (send_command_R1(mmcp, MMCSD_CMD_ERASE_RW_BLK_START, startblk) != 0x00U) {
    goto failed;
  }

  if (send_command_R1(mmcp, MMCSD_CMD_ERASE_RW_BLK_END, endblk) != 0x00U) {
    goto failed;
  }

  if (send_command_R1(mmcp, MMCSD_CMD_ERASE, 0) != 0x00U) {
    goto failed;
  }

  mmcp->state = BLK_READY;
  return HAL_SUCCESS;

  /* Command failed, state reset to BLK_ACTIVE.*/
failed:
  spiStop(mmcp->config->spip);
  mmcp->state = BLK_READY;
  return HAL_FAILED;
}
Exemplo n.º 7
0
//-----------------------------------------------------------------------------
bool SdSpiCard::readStatus(uint8_t* status) {
  // retrun is R2 so read extra status byte.
  if (cardAcmd(ACMD13, 0) || spiReceive()) {
    error(SD_CARD_ERROR_ACMD13);
    goto fail;
  }
  if (!readData(status, 64)) {
    goto fail;
  }
  spiStop();
  return true;

fail:
  spiStop();
  return false;
}
Exemplo n.º 8
0
/** read CID or CSR register */
bool SdSpiCard::readRegister(uint8_t cmd, void* buf) {
  uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
  if (cardCommand(cmd, 0)) {
    error(SD_CARD_ERROR_READ_REG);
    goto fail;
  }
  if (!readData(dst, 16)) {
    goto fail;
  }
  spiStop();
  return true;

fail:
  spiStop();
  return false;
}
Exemplo n.º 9
0
//-----------------------------------------------------------------------------
int
kuroBoxVectorNavStop(VectorNavDriver * nvp)
{
	spiStop(nvp->spip);
	gptStop(nvp->gpdp);
	return KB_OK;
}
Exemplo n.º 10
0
/**
 * @brief   Starts a sequential read.
 *
 * @param[in] mmcp      pointer to the @p MMCDriver object
 * @param[in] startblk  first block to read
 *
 * @return              The operation status.
 * @retval CH_SUCCESS   the operation succeeded.
 * @retval CH_FAILED    the operation failed.
 *
 * @api
 */
bool_t mmcStartSequentialRead(MMCDriver *mmcp, uint32_t startblk) {

  chDbgCheck(mmcp != NULL, "mmcStartSequentialRead");
  chDbgAssert(mmcp->state == BLK_READY,
              "mmcStartSequentialRead(), #1", "invalid state");

  /* Read operation in progress.*/
  mmcp->state = BLK_READING;

  /* (Re)starting the SPI in case it has been reprogrammed externally, it can
     happen if the SPI bus is shared among multiple peripherals.*/
  spiStart(mmcp->config->spip, mmcp->config->hscfg);
  spiSelect(mmcp->config->spip);

  if (mmcp->block_addresses)
    send_hdr(mmcp, MMCSD_CMD_READ_MULTIPLE_BLOCK, startblk);
  else
    send_hdr(mmcp, MMCSD_CMD_READ_MULTIPLE_BLOCK, startblk * MMCSD_BLOCK_SIZE);

  if (recvr1(mmcp) != 0x00) {
    spiStop(mmcp->config->spip);
    return CH_FAILED;
  }
  return CH_SUCCESS;
}
Exemplo n.º 11
0
//-----------------------------------------------------------------------------
int
kuroBoxStop(void)
{
	kbg_setLED3(1);

	extStop(&EXTD1);

	kuroBoxExternalDisplayStop();

	kuroBoxConfigStop();
	kuroBoxMenuStop();
	kuroBoxWriterStop();
	kuroBoxVectorNavStop(&VND1);
	kuroBoxTimeStop();
	kuroBoxGPSStop();
	kuroBoxButtonsStop();
	kuroBoxScreenStop();
	kuroBoxADCStop();
#ifdef HAVE_BLINK_THREAD
	chThdTerminate(blinkerThread);
	chThdWait(blinkerThread);
#endif // HAVE_BLINK_THREAD
	sdcStop(&SDCD1);
	spiStop(&SPID1);

	kuroBoxSerialStop();
	chSysDisable();

	kbg_setLED1(0);
	kbg_setLED2(0);
	kbg_setLED3(0);

	return KB_OK;
}
Exemplo n.º 12
0
/**
 * @brief   Brings the driver in a state safe for card removal.
 *
 * @param[in] mmcp      pointer to the @p MMCDriver object
 * @return              The operation status.
 * @retval FALSE        the operation was successful and the driver is now
 *                      in the @p MMC_INSERTED state.
 * @retval TRUE         the operation failed.
 */
bool_t mmcDisconnect(MMCDriver *mmcp) {
  bool_t status;

  chDbgCheck(mmcp != NULL, "mmcDisconnect");

  chDbgAssert((mmcp->mmc_state != MMC_UNINIT) &&
              (mmcp->mmc_state != MMC_STOP),
              "mmcDisconnect(), #1",
              "invalid state");
  switch (mmcp->mmc_state) {
  case MMC_READY:
    /* Wait for the pending write operations to complete.*/
    sync(mmcp);
    chSysLock();
    if (mmcp->mmc_state == MMC_READY)
      mmcp->mmc_state = MMC_INSERTED;
    chSysUnlock();
  case MMC_INSERTED:
    status = FALSE;
  default:
    status = TRUE;
  }
  spiStop(mmcp->mmc_spip);
  return status;
}
Exemplo n.º 13
0
/**
 * @brief   Erases blocks.
 *
 * @param[in] mmcp      pointer to the @p MMCDriver object
 * @param[in] startblk  starting block number
 * @param[in] endblk    ending block number
 *
 * @return              The operation status.
 * @retval CH_SUCCESS   the operation succeeded.
 * @retval CH_FAILED    the operation failed.
 *
 * @api
 */
bool_t mmcErase(MMCDriver *mmcp, uint32_t startblk, uint32_t endblk) {

  chDbgCheck((mmcp != NULL), "mmcErase");

  /* Handling command differences between HC and normal cards.*/
  if (!mmcp->block_addresses) {
    startblk *= MMCSD_BLOCK_SIZE;
    endblk *= MMCSD_BLOCK_SIZE;
  }

  if (send_command_R1(mmcp, MMCSD_CMD_ERASE_RW_BLK_START, startblk))
    goto failed;

  if (send_command_R1(mmcp, MMCSD_CMD_ERASE_RW_BLK_END, endblk))
    goto failed;

  if (send_command_R1(mmcp, MMCSD_CMD_ERASE, 0))
    goto failed;

  return CH_SUCCESS;

  /* Command failed, state reset to BLK_ACTIVE.*/
failed:
  spiStop(mmcp->config->spip);
  return CH_FAILED;
}
Exemplo n.º 14
0
/**
 * @brief   Deactivates the L3GD20 Complex Driver peripheral.
 *
 * @param[in] devp       pointer to the @p L3GD20Driver object
 *
 * @api
 */
void l3gd20Stop(L3GD20Driver *devp) {
  uint8_t cr1;
  osalDbgCheck(devp != NULL);

  osalDbgAssert((devp->state == L3GD20_STOP) || (devp->state == L3GD20_READY),
                "l3gd20Stop(), invalid state");

  if (devp->state == L3GD20_READY) {
    /* Disabling all axes and enabling power down mode.*/
    cr1 = 0;
    
#if L3GD20_USE_SPI
#if	L3GD20_SHARED_SPI
    spiAcquireBus(devp->config->spip);
    spiStart(devp->config->spip,
             devp->config->spicfg);
#endif /* L3GD20_SHARED_SPI */

    l3gd20SPIWriteRegister(devp->config->spip, L3GD20_AD_CTRL_REG1, 
                           1, &cr1);
    spiStop(devp->config->spip);
    
#if	L3GD20_SHARED_SPI
    spiReleaseBus(devp->config->spip);
#endif /* L3GD20_SHARED_SPI */ 
#endif /* L3GD20_USE_SPI */
  }			 
  devp->state = L3GD20_STOP;
}
Exemplo n.º 15
0
//------------------------------------------------------------------------------
bool SdSpiCard::readOCR(uint32_t* ocr) {
  uint8_t *p = reinterpret_cast<uint8_t*>(ocr);
  if (cardCommand(CMD58, 0)) {
    error(SD_CARD_ERROR_CMD58);
    goto fail;
  }
  for (uint8_t i = 0; i < 4; i++) {
    p[3 - i] = spiReceive();
  }

  spiStop();
  return true;

fail:
  spiStop();
  return false;
}
Exemplo n.º 16
0
void BoardDriverShutdown(void)
{
    sduStop(&SDU1);
    ws2811Stop(&ws2811);

    MFRC522Stop(&RFID1);
    spiStop(&SPID1);
}
Exemplo n.º 17
0
void sc_spi_stop(uint8_t spin)
{
    chDbgAssert(spin < SC_SPI_MAX_CLIENTS, "SPI n outside range", "#2");
    chDbgAssert(spi_conf[spin].spip != NULL, "SPI n not initialized", "#1");

    spiStop(spi_conf[spin].spip);
    spi_conf[spin].spip = NULL;
}
Exemplo n.º 18
0
static int SPISendData(SPIDriver *SPIPtr, uint8_t *TxBuf, size_t Size)
  {
    spiStart(SPIPtr, &HSSpiConfig); /* Setup transfer parameters.       */
    spiSelect(SPIPtr); /* Slave Select assertion.          */
    spiSend(SPIPtr, Size, TxBuf); /* Send command                     */
    spiUnselect(SPIPtr); /* Slave Select de-assertion.       */
    spiStop(SPIPtr);
    return 0;
  }
Exemplo n.º 19
0
/**
 * @brief   Disables the MMC peripheral.
 *
 * @param[in] mmcp      pointer to the @p MMCDriver object
 *
 * @api
 */
void mmcStop(MMCDriver *mmcp) {

  chDbgCheck(mmcp != NULL, "mmcStop");
  chDbgAssert((mmcp->state == BLK_STOP) || (mmcp->state == BLK_ACTIVE),
              "mmcStop(), #1", "invalid state");

  spiStop(mmcp->config->spip);
  mmcp->state = BLK_STOP;
}
Exemplo n.º 20
0
/**
 * @brief   Disables the MMC peripheral.
 *
 * @param[in] mmcp      pointer to the @p MMCDriver object
 *
 * @api
 */
void mmcStop(MMCDriver *mmcp) {

  osalDbgCheck(mmcp != NULL);
  osalDbgAssert((mmcp->state == BLK_STOP) || (mmcp->state == BLK_ACTIVE),
                "invalid state");

  spiStop(mmcp->config->spip);
  mmcp->state = BLK_STOP;
}
Exemplo n.º 21
0
static void sendToPot(Mcp42010Driver *driver, int channel, int value) {
	lockSpi(SPI_NONE);
	spiStart(driver->spi, &driver->spiConfig);
	spiSelect(driver->spi);
	int word = (17 + channel) * 256 + value;
	spiSend(driver->spi, 1, &word);
	spiUnselect(driver->spi);
	spiStop(driver->spi);
	unlockSpi();
}
Exemplo n.º 22
0
//------------------------------------------------------------------------------
bool SdSpiCard::readBlock(uint32_t blockNumber, uint8_t* dst) {
  SD_TRACE("RB", blockNumber);
  // use address if not SDHC card
  if (type() != SD_CARD_TYPE_SDHC) {
    blockNumber <<= 9;
  }
  if (cardCommand(CMD17, blockNumber)) {
    error(SD_CARD_ERROR_CMD17);
    goto fail;
  }
  if (!readData(dst, 512)) {
    goto fail;
  }
  spiStop();
  return true;

fail:
  spiStop();
  return false;
}
Exemplo n.º 23
0
static void M25P16ReadPage(uint32_t Address, uint8_t * Buffer)
  {
    uint8_t Command[] = { M25P16_READ_BYTES, (Address >> 16) & 0xFF, (Address >> 8) & 0xFF, Address & 0xFF};

    spiStart(&FLASH_SPI, &HSSpiConfig); /* Setup transfer parameters.       */
    spiSelect(&FLASH_SPI); /* Slave Select assertion.          */
    spiSend(&FLASH_SPI, 4, Command); /* Send command                     */
    spiReceive(&FLASH_SPI, PAGE_SIZE, Buffer);
    spiUnselect(&FLASH_SPI); /* Slave Select de-assertion.       */
    spiStop(&FLASH_SPI);
  }
Exemplo n.º 24
0
int max6675_get_temp(const struct max6675 *max)
{
	uint16_t read;

	spiAcquireBus(max->driver);
	spiStart(max->driver, &max->config);
	spiSelect(max->driver);
	spiReceive(max->driver, 1, &read);
	spiUnselect(max->driver);
	spiStop(max->driver);
	spiReleaseBus(max->driver);

	return (read >> 3) * 250;
}
Exemplo n.º 25
0
static void M25P16WritePage(uint32_t Address, uint8_t * Buffer)
  {
    uint8_t Command[] = { M25P16_PAGE_PROGRAM, (Address >> 16) & 0xFF, (Address >> 8) & 0xFF, Address & 0xFF};

    M25P16SetWriteEnable();
    spiStart(&FLASH_SPI, &HSSpiConfig); /* Setup transfer parameters.       */
    spiSelect(&FLASH_SPI); /* Slave Select assertion.          */
    spiSend(&FLASH_SPI, 4, Command); /* Send command                     */
    spiSend(&FLASH_SPI, 256, Buffer);
    spiUnselect(&FLASH_SPI); /* Slave Select de-assertion.       */
    spiStop(&FLASH_SPI);
    while(M25P16ReadStatus() & 0x1)
      chThdSleepMicroseconds(100);
  }
Exemplo n.º 26
0
static void hipStartupCode(void) {
//	D[4:1] = 0000 : 4 MHz
//	D[4:1] = 0001 : 5 MHz
//	D[4:1] = 0010 : 6 MHz
//	D[4:1] = 0011 ; 8 MHz
//	D[4:1] = 0100 ; 10 MHz
//	D[4:1] = 0101 ; 12 MHz
//	D[4:1] = 0110 : 16 MHz
//	D[4:1] = 0111 : 20 MHz
//	D[4:1] = 1000 : 24 MHz


// 0 for 4MHz
// 6 for 8 MHz
	currentPrescaler = engineConfiguration->hip9011PrescalerAndSDO;
	SPI_SYNCHRONOUS(SET_PRESCALER_CMD + currentPrescaler);

	chThdSleepMilliseconds(10);

	// '0' for channel #1
	SPI_SYNCHRONOUS(SET_CHANNEL_CMD + 0);

	chThdSleepMilliseconds(10);

	// band index depends on cylinder bore
	SPI_SYNCHRONOUS(SET_BAND_PASS_CMD + currentBandIndex);

	chThdSleepMilliseconds(10);

	if (correctResponse == 0) {
		warning(CUSTOM_OBD_41, "TPIC/HIP does not respond");
	}

	if (boardConfiguration->useTpicAdvancedMode) {
		// enable advanced mode for digital integrator output
		SPI_SYNCHRONOUS(SET_ADVANCED_MODE);

    	chThdSleepMilliseconds(10);
	}

	/**
	 * Let's restart SPI to switch it from synchronous mode into
	 * asynchronous mode
	 */
	spiStop(driver);
	hipSpiCfg.end_cb = endOfSpiExchange;
	spiStart(driver, &hipSpiCfg);
	state = READY_TO_INTEGRATE;
}
Exemplo n.º 27
0
//------------------------------------------------------------------------------
bool SdSpiCard::writeStart(uint32_t blockNumber) {
  // use address if not SDHC card
  if (type() != SD_CARD_TYPE_SDHC) {
    blockNumber <<= 9;
  }
  if (cardCommand(CMD25, blockNumber)) {
    error(SD_CARD_ERROR_CMD25);
    goto fail;
  }
  return true;

fail:
  spiStop();
  return false;
}
Exemplo n.º 28
0
//------------------------------------------------------------------------------
bool SdSpiCard::erase(uint32_t firstBlock, uint32_t lastBlock) {
  csd_t csd;
  if (!readCSD(&csd)) {
    goto fail;
  }
  // check for single block erase
  if (!csd.v1.erase_blk_en) {
    // erase size mask
    uint8_t m = (csd.v1.sector_size_high << 1) | csd.v1.sector_size_low;
    if ((firstBlock & m) != 0 || ((lastBlock + 1) & m) != 0) {
      // error card can't erase specified area
      error(SD_CARD_ERROR_ERASE_SINGLE_BLOCK);
      goto fail;
    }
  }
  if (m_type != SD_CARD_TYPE_SDHC) {
    firstBlock <<= 9;
    lastBlock <<= 9;
  }
  if (cardCommand(CMD32, firstBlock)
      || cardCommand(CMD33, lastBlock)
      || cardCommand(CMD38, 0)) {
    error(SD_CARD_ERROR_ERASE);
    goto fail;
  }
  if (!waitNotBusy(SD_ERASE_TIMEOUT)) {
    error(SD_CARD_ERROR_ERASE_TIMEOUT);
    goto fail;
  }
  spiStop();
  return true;

fail:
  spiStop();
  return false;
}
Exemplo n.º 29
0
//------------------------------------------------------------------------------
bool SdSpiCard::writeBlocks(uint32_t block, const uint8_t* src, size_t count) {
  if (!writeStart(block)) {
    goto fail;
  }
  for (size_t b = 0; b < count; b++, src += 512) {
    if (!writeData(src)) {
      goto fail;
    }
  }
  return writeStop();

 fail:
  spiStop();
  return false;
}
Exemplo n.º 30
0
//------------------------------------------------------------------------------
bool SdSpiCard::writeData(const uint8_t* src) {
  // wait for previous write to finish
  if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
    error(SD_CARD_ERROR_WRITE_TIMEOUT);
    goto fail;
  }
  if (!writeData(WRITE_MULTIPLE_TOKEN, src)) {
    goto fail;
  }
  return true;

fail:
  spiStop();
  return false;
}