Beispiel #1
0
void hipAdcCallback(adcsample_t value) {
	if (state == WAITING_FOR_ADC_TO_SKIP) {
		state = WAITING_FOR_RESULT_ADC;
	} else if (state == WAITING_FOR_RESULT_ADC) {
		engine->knockVolts = adcToVoltsDivided(value);
		hipValueMax = maxF(engine->knockVolts, hipValueMax);
		engine->knockLogic(engine->knockVolts);

		float angleWindowWidth =
		engineConfiguration->knockDetectionWindowEnd - engineConfiguration->knockDetectionWindowStart;

		if (angleWindowWidth != currentAngleWindowWidth) {
			currentAngleWindowWidth = angleWindowWidth;
		prepareHip9011RpmLookup(currentAngleWindowWidth);
		}

		int integratorIndex = getIntegrationIndexByRpm(engine->rpmCalculator.rpmValue);
		int gainIndex = getHip9011GainIndex(boardConfiguration->hip9011Gain);
		int bandIndex = getBandIndex();
		int prescalerIndex = engineConfiguration->hip9011PrescalerAndSDO;


		if (currentGainIndex != gainIndex) {
			currentGainIndex = gainIndex;
			tx_buff[0] = SET_GAIN_CMD + gainIndex;

			state = IS_SENDING_SPI_COMMAND;
			spiSelectI(driver);
			spiStartExchangeI(driver, 1, tx_buff, rx_buff);
		} else if (currentIntergratorIndex != integratorIndex) {
			currentIntergratorIndex = integratorIndex;
			tx_buff[0] = SET_INTEGRATOR_CMD + integratorIndex;

			state = IS_SENDING_SPI_COMMAND;
			spiSelectI(driver);
			spiStartExchangeI(driver, 1, tx_buff, rx_buff);
		} else if (currentBandIndex != bandIndex) {
			currentBandIndex = bandIndex;
			tx_buff[0] = SET_BAND_PASS_CMD + bandIndex;

			state = IS_SENDING_SPI_COMMAND;
			spiSelectI(driver);
			spiStartExchangeI(driver, 1, tx_buff, rx_buff);
		} else if (currentPrescaler != prescalerIndex) {
			currentPrescaler = prescalerIndex;
			tx_buff[0] = SET_PRESCALER_CMD + prescalerIndex;

			state = IS_SENDING_SPI_COMMAND;
			spiSelectI(driver);
			spiStartExchangeI(driver, 1, tx_buff, rx_buff);
		} else {
			state = READY_TO_INTEGRATE;
		}
	}
}
Beispiel #2
0
/**
 * @brief   Exchanges data on the SPI bus.
 * @details This asynchronous function starts a simultaneous transmit/receive
 *          operation.
 * @pre     A slave must have been selected using @p spiSelect() or
 *          @p spiSelectI().
 * @post    At the end of the operation the configured callback is invoked.
 * @note    The buffers are organized as uint8_t arrays for data sizes below
 *          or equal to 8 bits else it is organized as uint16_t arrays.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[in] txbuf     the pointer to the transmit buffer
 * @param[out] rxbuf    the pointer to the receive buffer
 *
 * @api
 */
void spiStartExchange(SPIDriver *spip, size_t n,
                      const void *txbuf, void *rxbuf) {

  osalDbgCheck((spip != NULL) && (n > 0U) &&
               (rxbuf != NULL) && (txbuf != NULL));

  osalSysLock();
  osalDbgAssert(spip->state == SPI_READY, "not ready");
  spiStartExchangeI(spip, n, txbuf, rxbuf);
  osalSysUnlock();
}
Beispiel #3
0
/**
 * @brief   Exchanges data on the SPI bus.
 * @details This asynchronous function starts a simultaneous transmit/receive
 *          operation.
 * @pre     A slave must have been selected using @p spiSelect() or
 *          @p spiSelectI().
 * @post    At the end of the operation the configured callback is invoked.
 * @note    The buffers are organized as uint8_t arrays for data sizes below
 *          or equal to 8 bits else it is organized as uint16_t arrays.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[in] txbuf     the pointer to the transmit buffer
 * @param[out] rxbuf    the pointer to the receive buffer
 *
 * @api
 */
void spiStartExchange(SPIDriver *spip, size_t n,
                      const void *txbuf, void *rxbuf) {

  chDbgCheck((spip != NULL) && (n > 0) && (rxbuf != NULL) && (txbuf != NULL),
             "spiStartExchange");

  chSysLock();
  chDbgAssert(spip->spd_state == SPI_READY,
              "spiStartExchange(), #1", "not ready");
  spiStartExchangeI(spip, n, txbuf, rxbuf);
  chSysUnlock();
}
Beispiel #4
0
/**
 * @brief   Exchanges data on the SPI bus.
 * @details This synchronous function performs a simultaneous transmit/receive
 *          operation.
 * @pre     In order to use this function the option @p SPI_USE_WAIT must be
 *          enabled.
 * @pre     In order to use this function the driver must have been configured
 *          without callbacks (@p end_cb = @p NULL).
 * @note    The buffers are organized as uint8_t arrays for data sizes below
 *          or equal to 8 bits else it is organized as uint16_t arrays.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[in] txbuf     the pointer to the transmit buffer
 * @param[out] rxbuf    the pointer to the receive buffer
 *
 * @api
 */
void spiExchange(SPIDriver *spip, size_t n,
                 const void *txbuf, void *rxbuf) {

  osalDbgCheck((spip != NULL) && (n > 0U) &&
               (rxbuf != NULL) && (txbuf != NULL));

  osalSysLock();
  osalDbgAssert(spip->state == SPI_READY, "not ready");
  osalDbgAssert(spip->config->end_cb == NULL, "has callback");
  spiStartExchangeI(spip, n, txbuf, rxbuf);
  (void) osalThreadSuspendS(&spip->thread);
  osalSysUnlock();
}
/**
 * @brief   Exchanges data on the SPI bus.
 * @details This synchronous function performs a simultaneous transmit/receive
 *          operation.
 * @pre     In order to use this function the option @p SPI_USE_WAIT must be
 *          enabled.
 * @pre     In order to use this function the driver must have been configured
 *          without callbacks (@p end_cb = @p NULL).
 * @note    The buffers are organized as uint8_t arrays for data sizes below
 *          or equal to 8 bits else it is organized as uint16_t arrays.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[in] txbuf     the pointer to the transmit buffer
 * @param[out] rxbuf    the pointer to the receive buffer
 *
 * @api
 */
void spiExchange(SPIDriver *spip, size_t n,
                 const void *txbuf, void *rxbuf) {

  chDbgCheck((spip != NULL) && (n > 0) && (rxbuf != NULL) && (txbuf != NULL),
             "spiExchange");

  chSysLock();
  chDbgAssert(spip->state == SPI_READY, "spiExchange(), #1", "not ready");
  chDbgAssert(spip->config->end_cb == NULL,
              "spiExchange(), #2", "has callback");
  spiStartExchangeI(spip, n, txbuf, rxbuf);
  _spi_wait_s(spip);
  chSysUnlock();
}
Beispiel #6
0
/**
 * @brief   Exchanges data on the SPI bus.
 * @details This synchronous function performs a simultaneous transmit/receive
 *          operation.
 * @pre     In order to use this function the option @p SPI_USE_WAIT must be
 *          enabled.
 * @pre     In order to use this function the driver must have been configured
 *          without callbacks (@p end_cb = @p NULL).
 * @note    The buffers are organized as uint8_t arrays for data sizes below
 *          or equal to 8 bits else it is organized as uint16_t arrays.
 *
 * @param[in] spip      pointer to the @p SPIDriver object
 * @param[in] n         number of words to be exchanged
 * @param[in] txbuf     the pointer to the transmit buffer
 * @param[out] rxbuf    the pointer to the receive buffer
 *
 * @api
 */
void spiExchange(SPIDriver *spip, size_t n,
                 const void *txbuf, void *rxbuf) {

  osalDbgCheck((spip != NULL) && (n > 0U) &&
               (rxbuf != NULL) && (txbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
  osalDbgCheck((spip->config->circular == false) || ((n & 1U) == 0U));
#endif

  osalSysLock();
  osalDbgAssert(spip->state == SPI_READY, "not ready");
  spiStartExchangeI(spip, n, txbuf, rxbuf);
  (void) osalThreadSuspendS(&spip->thread);
  osalSysUnlock();
}
Beispiel #7
0
/*
 * SPI bus exchange routine
 */
int SPIExchangeDataI(SPIDriver *spip, uint8_t *tx, uint8_t *rx, size_t size) {
	//chMtxLock(&SPIMtx);

	/*
	 * Do exchange between device and MCU.
	 */
	//spiAcquireBus(spip);              /* Acquire ownership of the bus.    */
	//spiSelectI(spip);                  /* Slave Select assertion.          */
	spiStartExchangeI(spip, size, tx, rx);  /* Atomic transfer operations.      */
	//spiUnselectI(spip);                /* Slave Select de-assertion.       */
	//spiReleaseBus(spip);              /* Ownership release.               */
	//chMtxUnlock();

	return 0;
}
Beispiel #8
0
static void spi_end_cb(SPIDriver *spip){
  its++;

  if (stop){
    chSysLockFromISR();
    chBSemSignalI(&sem);
    chSysUnlockFromISR();
    return;
  }
  else{
    chSysLockFromISR();
    spiStartExchangeI(spip, SPI_BUF_SIZE, testbuf_flash, testbuf_ram);
    chSysUnlockFromISR();
  }
}
Beispiel #9
0
void Hip9011Hardware::sendCommand(unsigned char command) {
	tx_buff[0] = command;

	spiSelectI(driver);
	spiStartExchangeI(driver, 1, tx_buff, rx_buff);
}