HAL: Add software-based accumulator for SPI devices.

Change-Id: I154c4c8f438163edf3ebc2c38f67a976d8cfbfd7
This commit is contained in:
Peter Johnson
2015-11-22 11:50:49 -08:00
committed by Brad Miller (WPI)
parent 530ce310a6
commit de219055f0
7 changed files with 763 additions and 0 deletions

View File

@@ -172,3 +172,132 @@ int32_t SPI::Transaction(uint8_t* dataToSend, uint8_t* dataReceived,
retVal = spiTransaction(m_port, dataToSend, dataReceived, size);
return retVal;
}
/**
* Initialize the accumulator.
*
* @param period Time between reads
* @param cmd SPI command to send to request data
* @param xfer_size SPI transfer size, in bytes
* @param valid_mask Mask to apply to received data for validity checking
* @param valid_data After valid_mask is applied, required matching value for
* validity checking
* @param data_shift Bit shift to apply to received data to get actual data
* value
* @param data_size Size (in bits) of data field
* @param is_signed Is data field signed?
* @param big_endian Is device big endian?
*/
void SPI::InitAccumulator(double period, uint32_t cmd, uint8_t xfer_size,
uint32_t valid_mask, uint32_t valid_value,
uint8_t data_shift, uint8_t data_size, bool is_signed,
bool big_endian) {
int32_t status = 0;
spiInitAccumulator(m_port, (uint32_t)(period * 1e6), cmd, xfer_size,
valid_mask, valid_value, data_shift, data_size, is_signed,
big_endian, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
/**
* Frees the accumulator.
*/
void SPI::FreeAccumulator() {
int32_t status = 0;
spiFreeAccumulator(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
/**
* Resets the accumulator to zero.
*/
void SPI::ResetAccumulator() {
int32_t status = 0;
spiResetAccumulator(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
/**
* Set the center value of the accumulator.
*
* The center value is subtracted from each value before it is added to the accumulator. This
* is used for the center value of devices like gyros and accelerometers to make integration work
* and to take the device offset into account when integrating.
*/
void SPI::SetAccumulatorCenter(int32_t center) {
int32_t status = 0;
spiSetAccumulatorCenter(m_port, center, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
/**
* Set the accumulator's deadband.
*/
void SPI::SetAccumulatorDeadband(int32_t deadband) {
int32_t status = 0;
spiSetAccumulatorDeadband(m_port, deadband, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}
/**
* Read the last value read by the accumulator engine.
*/
int32_t SPI::GetAccumulatorLastValue() const {
int32_t status = 0;
int32_t retVal = spiGetAccumulatorLastValue(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return retVal;
}
/**
* Read the accumulated value.
*
* @return The 64-bit value accumulated since the last Reset().
*/
int64_t SPI::GetAccumulatorValue() const {
int32_t status = 0;
int64_t retVal = spiGetAccumulatorValue(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return retVal;
}
/**
* Read the number of accumulated values.
*
* Read the count of the accumulated values since the accumulator was last Reset().
*
* @return The number of times samples from the channel were accumulated.
*/
uint32_t SPI::GetAccumulatorCount() const {
int32_t status = 0;
uint32_t retVal = spiGetAccumulatorCount(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return retVal;
}
/**
* Read the average of the accumulated value.
*
* @return The accumulated average value (value / count).
*/
double SPI::GetAccumulatorAverage() const {
int32_t status = 0;
double retVal = spiGetAccumulatorAverage(m_port, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return retVal;
}
/**
* Read the accumulated value and the number of accumulated values atomically.
*
* This function reads the value and count atomically.
* This can be used for averaging.
*
* @param value Pointer to the 64-bit accumulated output.
* @param count Pointer to the number of accumulation cycles.
*/
void SPI::GetAccumulatorOutput(int64_t &value, uint32_t &count) const {
int32_t status = 0;
spiGetAccumulatorOutput(m_port, &value, &count, &status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
}