Exemplo n.º 1
0
//-----------------------------------------------------------------------------
// polled read of a register
uint8_t
kbv_readRegister(VectorNavDriver * nvp, uint8_t reg, uint8_t size, uint8_t * buf)
{
	uint8_t r[] = { 0x01, reg, 0x00, 0x00 };

	chSysLock();

	spiSelectI(nvp->spip);
	// we ignore the first exchange's return
	for ( int i = 0 ; i < 4 ; ++i )
		spiPolledExchange(nvp->spip, r[i]);
	spiUnselectI(nvp->spip);

	gptPolledDelay(nvp->gpdp, 50);

	spiSelectI(nvp->spip);
	for ( int i = 0 ; i < 4 ; ++i )
		r[i] = spiPolledExchange(nvp->spip, 0);

	// was there an error?
	if ( r[0] == 0x00 && r[1] == 0x01 && r[2] == reg && r[3] == 0x00 )
	{
		// all good!
		for ( int i = 0 ; i < size ; ++i )
			buf[i] = spiPolledExchange(nvp->spip, 0);
	}
	spiUnselectI(nvp->spip);
	gptPolledDelay(nvp->gpdp, 50);
	chSysUnlock();
	return r[3];
}
Exemplo n.º 2
0
msg_t Mtd25::spi_write_enable(void) {
  msg_t ret = MSG_RESET;
  uint8_t tmp;

#if SPI_USE_MUTUAL_EXCLUSION
  spiAcquireBus(this->spip);
#endif

  /* try to enable write access */
  spiSelect(spip);
  spiPolledExchange(spip, S25_CMD_WREN);
  spiUnselect(spip);
  chSysPolledDelayX(10);

  /* check result */
  spiSelect(spip);
  spiPolledExchange(spip, S25_CMD_RDSR1);
  tmp = spiPolledExchange(spip, 0);
  spiUnselect(spip);
  if (tmp & S25_SR1_WEL)
    ret = MSG_OK;
  else
    ret = MSG_RESET;

#if SPI_USE_MUTUAL_EXCLUSION
  spiReleaseBus(this->spip);
#endif

  return ret;
}
Exemplo n.º 3
0
/**
 * @brief 25XX low level write then read rountine.
 *
 * @param[in]  eepcfg pointer to configuration structure of eeprom file.
 * @param[in]  txbuf  pointer to buffer to be transfered.
 * @param[in]  txlen  number of bytes to be transfered.
 * @param[out] rxbuf  pointer to buffer to be received.
 * @param[in]  rxlen  number of bytes to be received.
 */
static void ll_25xx_transmit_receive(const SPIEepromFileConfig *eepcfg,
                                     const uint8_t *txbuf, size_t txlen,
                                     uint8_t *rxbuf, size_t rxlen) {

#if SPI_USE_MUTUAL_EXCLUSION
  spiAcquireBus(eepcfg->spip);
#endif

  spiStart(eepcfg->spip, eepcfg->spicfg);
  spiSelect(eepcfg->spip);
#ifdef POLLED_SPI
  size_t count = 0;
  for( count = 0; count < txlen; ++count )
      spiPolledExchange( eepcfg->spip, txbuf[count] );
  for( count = 0; count < rxlen; ++count )
      rxbuf[count] = spiPolledExchange( eepcfg->spip, 0 );
#else
  spiSend(eepcfg->spip, txlen, txbuf);
  if (rxlen) /* Check if receive is needed. */
    spiReceive(eepcfg->spip, rxlen, rxbuf);
#endif
  spiUnselect(eepcfg->spip);

#if SPI_USE_MUTUAL_EXCLUSION
  spiReleaseBus(eepcfg->spip);
#endif
}
Exemplo n.º 4
0
//----------------------------------------------------------------------------
void 
spiEepromWriteSR(spiEepromDriver * sedp, uint8_t sr)
{
	spiStart(sedp->spip, &sedp->cfgp->spicfg);
	spiSelect(sedp->spip);
	spiPolledExchange(sedp->spip, OP_WRSR);
	spiPolledExchange(sedp->spip, sr);
	spiUnselect(sedp->spip);
}
Exemplo n.º 5
0
//----------------------------------------------------------------------------
uint8_t 
spiEepromReadSR(spiEepromDriver * sedp)
{
	spiStart(sedp->spip, &sedp->cfgp->spicfg);
	spiSelect(sedp->spip);
	spiPolledExchange(sedp->spip, OP_RDSR);
	uint8_t sr = spiPolledExchange(sedp->spip, 0x00);
	spiUnselect(sedp->spip);
	return sr;
}
Exemplo n.º 6
0
void write_digit(int8_t num, uint8_t dig){
     
    uint8_t out_bytes[1]; /*= {
        (((num<10)&&(num>=0)) ? number_seg_bytes[num] : number_seg_bytes[10]),
    };*/
    out_bytes[0] =(((num<10)&&(num>=0)) ? number_seg_bytes[num] : number_seg_bytes[10]); 
    chSysLockFromIsr();
    palClearPad(GPIOA,3);
    /* SPI slave selection and transmission start.*/
    spiSelectI(&SPID1);
    //spiStartSendI(&SPID1, 1, out_bytes);
    spiPolledExchange(&SPID1, out_bytes[0]);
    uint8_t nd = NUM_DIGS;
    while(nd--){
        if(nd == dig){
            palSetPad(GPIOC,nd); 
        } else {
            palClearPad(GPIOC,nd);
        }
    }
    spiUnselectI(&SPID1);
    palSetPad(GPIOA, 3);
    chSysUnlockFromIsr();
    //while(SPI_done == FALSE);
    chThdSleepMicroseconds(DIG_SWITCH_DELAY_US);
    //chThdSleepMilliseconds(DIG_SWITCH_DELAY_MS); 
}
Exemplo n.º 7
0
//----------------------------------------------------------------------------
void
spiEepromReadPage(spiEepromDriver * sedp, uint16_t page, uint8_t * buf)
{
	uint32_t addr = page << SPIEEPROM_PAGE_SIZE_SHIFT;

	spiStart(sedp->spip, &sedp->cfgp->spicfg);
	spiSelect(sedp->spip);
	spiPolledExchange(sedp->spip, OP_READ);
#ifdef SPIEEPROM_24BIT_ADDRESS
	spiPolledExchange(sedp->spip, (addr>>16)&0xff);
#endif
	spiPolledExchange(sedp->spip, (addr>>8)&0xff);	
	spiPolledExchange(sedp->spip, addr&0xff);		
	for ( uint16_t idx = 0 ; idx < SPIEEPROM_PAGE_SIZE ; ++idx )
		*buf++ = spiPolledExchange(sedp->spip, 0);
	spiUnselect(sedp->spip);
}
Exemplo n.º 8
0
//----------------------------------------------------------------------------
void
spiEepromDisableWrite(spiEepromDriver * sedp)
{
	spiStart(sedp->spip, &sedp->cfgp->spicfg);
	spiSelect(sedp->spip);
	spiPolledExchange(sedp->spip, OP_WRDI);
	spiUnselect(sedp->spip);
}
/*
 * Application entry point.
 */
void main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * OS initialization.
   */
  chSysInit();

  /*
   * Activates the SPI driver 1 using the driver default configuration.
   */
  spiStart(&SPID1, &spicfg);

  /*
   * Normal main() thread activity.
   */
  while (TRUE) {
    volatile uint8_t b;

    chThdSleepMilliseconds(1000);
    /* Exchanging data, if the pins MISO and MOSI are connected then the
       transmitted data is received back into the buffer. On the
       STM8S-Discovery board the pins are CN2-9 and CN2-10.*/
    spiSelect(&SPID1);
    spiExchange(&SPID1, sizeof(digits), digits, buffer);
    /* Polled transfers test.*/
    b = spiPolledExchange(&SPID1, 0x55);    
    b = spiPolledExchange(&SPID1, 0xAA);    
    spiUnselect(&SPID1);
  }
}
Exemplo n.º 10
0
msg_t Mtd25::wait_op_complete(systime_t timeout) {

  systime_t start = chVTGetSystemTimeX();
  systime_t end = start + timeout;

  osalThreadSleepMilliseconds(2);

  while (chVTIsSystemTimeWithinX(start, end)) {
    uint8_t tmp;

#if SPI_USE_MUTUAL_EXCLUSION
    spiAcquireBus(this->spip);
#endif

    spiSelect(spip);
    spiPolledExchange(spip, S25_CMD_RDSR1);
    tmp = spiPolledExchange(spip, 0);
    spiUnselect(spip);

#if SPI_USE_MUTUAL_EXCLUSION
    spiReleaseBus(this->spip);
#endif

    if (tmp & S25_SR1_WIP) {
      continue;
    }
    else {
      if (tmp & (S25_SR1_PERR | S25_SR1_EERR))
        return MSG_RESET;
      else
        return MSG_OK;
    }

    osalThreadSleepMilliseconds(10);
  }

  /* time is out */
  return MSG_RESET;
}
Exemplo n.º 11
0
/*
 * Application entry point.
 */
int main(void) {
  uint8_t i;

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Activates the SD1 and SPI1 drivers.
   */
  sdStart(&SD1, NULL);                  /* Default: 38400,8,N,1.            */
  spiStart(&SPID1, &spicfg);

  /*
   * Creates the blinker threads.
   */
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL);

  /*
   * Normal main() thread activity, in this demo it updates the 7-segments
   * display on the LPCXpresso main board using the SPI driver.
   */
  i = 0;
  while (TRUE) {
    if (!palReadPad(GPIO0, GPIO0_SW3))
      TestThread(&SD1);
    spiSelect(&SPID1);
    spiSend(&SPID1, 1, &digits[i]);                 /* Non polled method.   */
    spiUnselect(&SPID1);
    chThdSleepMilliseconds(500);
    spiSelect(&SPID1);
    spiPolledExchange(&SPID1, digits[i | 0x10]);    /* Polled method.       */
    spiUnselect(&SPID1);
    chThdSleepMilliseconds(500);
    i = (i + 1) & 15;
  }
}
Exemplo n.º 12
0
static flash_error_t program(void *instance, flash_address_t addr,
                             const uint8_t *pp, size_t n) {
  N25Q128Driver *devp = (N25Q128Driver *)instance;
  SPIDriver *spip = devp->config->spip;
  flash_error_t err;

  osalDbgAssert(devp->state == FLASH_READY, "invalid state");

#if N25Q128_SHARED_SPI == TRUE
  spiAcquireBus(spip);
  spiStart(spip, devp->config->spicfg);
#endif
  devp->state = FLASH_ACTIVE;

  while (n > 0U) {
    uint8_t sts;

    /* Data size that can be written in a single program page operation.*/
    size_t chunk = (size_t)(((addr | PAGE_MASK) + 1U) - addr);
    if (chunk > n) {
      chunk = n;
    }

    /* Enabling write operation.*/
    spiSelect(spip);
    spi_send_cmd(devp, N25Q128_CMD_WRITE_ENABLE);
    spiUnselect(spip);
    (void) spiPolledExchange(spip, 0xFF);   /* One frame delay.*/

    /* Page program command.*/
    spiSelect(spip);
    spi_send_cmd_addr(devp, N25Q128_CMD_PAGE_PROGRAM, addr);
    spiSend(spip, chunk, pp);
    spiUnselect(spip);
    (void) spiPolledExchange(spip, 0xFF);   /* One frame delay.*/

    /* Operation end waiting.*/
    do {
#if N25Q128_NICE_WAITING == TRUE
      osalThreadSleepMilliseconds(1);
#endif
      /* Read status command.*/
      spiSelect(spip);
      spi_send_cmd(devp, N25Q128_CMD_READ_STATUS_REGISTER);
      spiReceive(spip, 1, &sts);
      spiUnselect(spip);
    } while ((sts & N25Q128_STS_BUSY) != 0U);

    /* Checking for errors.*/
    if ((sts & N25Q128_STS_ALL_ERRORS) != 0U) {
      /* Clearing status register.*/
      (void) spiPolledExchange(spip, 0xFF);   /* One frame delay.*/
      spiSelect(spip);
      spi_send_cmd(devp, N25Q128_CMD_CLEAR_FLAG_STATUS_REGISTER);
      spiUnselect(spip);

      /* Program operation failed.*/
      err = FLASH_PROGRAM_FAILURE;
      goto exit_error;
    }

    /* Next page.*/
    addr += chunk;
    pp   += chunk;
    n    -= chunk;
  }

  /* Program operation succeeded.*/
  err = FLASH_NO_ERROR;

  /* Common exit path for this function.*/
exit_error:
  devp->state = FLASH_READY;
#if N25Q128_SHARED_SPI == TRUE
  spiReleaseBus(spip);
#endif
  return err;
}