Some general cleanups in the HAL (#188)

AnalogTrigger initialization checks are now in the correct order. Plus
frees no longer grab the structure if it is not needed.
This commit is contained in:
Thad House
2016-07-25 23:26:34 -07:00
committed by Peter Johnson
parent 8ac7e44f19
commit 866046edd1
6 changed files with 10 additions and 34 deletions

View File

@@ -9,6 +9,7 @@
#include <cassert>
#include <cstdio>
#include <memory>
#include <stdint.h>
@@ -25,7 +26,7 @@ static const uint8_t kControlTxRx = 1;
static const uint8_t kControlStart = 2;
static const uint8_t kControlStop = 4;
static tAccel* accel = 0;
static std::unique_ptr<tAccel> accel;
static HAL_AccelerometerRange accelerometerRange;
// Register addresses
@@ -84,7 +85,7 @@ static void initializeAccelerometer() {
int32_t status;
if (!accel) {
accel = tAccel::create(&status);
accel.reset(tAccel::create(&status));
// Enable I2C
accel->writeCNFG(1, &status);
@@ -124,8 +125,6 @@ static void writeRegister(Register reg, uint8_t data) {
while (accel->readSTAT(&status) & 1) {
if (HAL_GetFPGATime(&status) > initialTime + 1000) break;
}
std::fflush(stdout);
}
static uint8_t readRegister(Register reg) {
@@ -155,8 +154,6 @@ static uint8_t readRegister(Register reg) {
if (HAL_GetFPGATime(&status) > initialTime + 1000) break;
}
std::fflush(stdout);
return accel->readDATI(&status);
}

View File

@@ -63,8 +63,6 @@ HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle port_handle,
}
void HAL_FreeAnalogInputPort(HAL_AnalogInputHandle analog_port_handle) {
auto port = analogInputHandles.Get(analog_port_handle);
if (!port) return;
// no status, so no need to check for a proper free.
analogInputHandles.Free(analog_port_handle);
}

View File

@@ -32,26 +32,25 @@ extern "C" {
HAL_AnalogTriggerHandle HAL_InitializeAnalogTrigger(
HAL_AnalogInputHandle port_handle, int32_t* index, int32_t* status) {
if (port_handle == HAL_kInvalidHandle) {
*status = PARAMETER_OUT_OF_RANGE;
// ensure we are given a valid and active AnalogInput handle
auto analog_port = analogInputHandles.Get(port_handle);
if (analog_port == nullptr) {
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
HAL_AnalogInputHandle handle = analogTriggerHandles.Allocate();
HAL_AnalogTriggerHandle handle = analogTriggerHandles.Allocate();
if (handle == HAL_kInvalidHandle) {
*status = NO_AVAILABLE_RESOURCES;
return HAL_kInvalidHandle;
}
auto trigger = analogTriggerHandles.Get(handle);
trigger->analogHandle = port_handle;
auto analog_port = analogInputHandles.Get(trigger->analogHandle);
if (analog_port == nullptr) { // would only error on thread issue
if (trigger == nullptr) { // would only occur on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
trigger->analogHandle = port_handle;
trigger->index = static_cast<uint8_t>(getHandleIndex(handle));
*index = trigger->index;
// TODO: if (index == ~0ul) { CloneError(triggers); return; }
trigger->trigger.reset(tAnalogTrigger::create(trigger->index, status));
trigger->trigger->writeSourceSelect_Channel(analog_port->pin, status);
@@ -60,10 +59,6 @@ HAL_AnalogTriggerHandle HAL_InitializeAnalogTrigger(
void HAL_CleanAnalogTrigger(HAL_AnalogTriggerHandle analog_trigger_handle,
int32_t* status) {
auto trigger = analogTriggerHandles.Get(analog_trigger_handle);
if (trigger == nullptr) { // ignore status error
return;
}
analogTriggerHandles.Free(analog_trigger_handle);
// caller owns the analog input handle.
}

View File

@@ -49,10 +49,6 @@ HAL_CounterHandle HAL_InitializeCounter(HAL_Counter_Mode mode, int32_t* index,
}
void HAL_FreeCounter(HAL_CounterHandle counter_handle, int32_t* status) {
auto counter = counterHandles.Get(counter_handle);
if (counter == nullptr) { // don't throw status as unneccesary
return;
}
counterHandles.Free(counter_handle);
}

View File

@@ -84,12 +84,7 @@ HAL_FPGAEncoderHandle HAL_InitializeFPGAEncoder(
void HAL_FreeFPGAEncoder(HAL_FPGAEncoderHandle fpga_encoder_handle,
int32_t* status) {
auto encoder = fpgaEncoderHandles.Get(fpga_encoder_handle);
fpgaEncoderHandles.Free(fpga_encoder_handle);
if (encoder == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
}
/**

View File

@@ -52,11 +52,6 @@ HAL_InterruptHandle HAL_InitializeInterrupts(HAL_Bool watcher,
void HAL_CleanInterrupts(HAL_InterruptHandle interrupt_handle,
int32_t* status) {
auto anInterrupt = interruptHandles.Get(interrupt_handle);
if (anInterrupt == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
interruptHandles.Free(interrupt_handle);
}