Skip to content

Commit

Permalink
more progress
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Oct 16, 2024
1 parent f7384dc commit a2906b8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 21 deletions.
12 changes: 6 additions & 6 deletions hal/src/main/native/athena/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,25 +379,25 @@ int32_t HAL_ReadSPI(HAL_SPIPort port, uint8_t* buffer, int32_t count) {
return ioctl(spi->nativeHandle, SPI_IOC_MESSAGE(1), &xfer);
}

void HAL_CloseSPI(HAL_SPIPort port) {
auto spi = spiHandles->Get(spiHandles->GetHandleForPort(port));
void HAL_CloseSPI(HAL_SPIHandle handle) {
auto spi = spiHandles->Get(handle);
if (!spi) {
return;
}

int32_t status = 0;
HAL_FreeSPIAuto(port, &status);
HAL_FreeSPIAuto(spi->port, &status);

{
std::scoped_lock lock(spi->apiMutex);
close(spi->nativeHandle);
}

if (port < 4) {
if (spi->port < 4) {
CommonSPIPortFree();
}

switch (port) {
switch (spi->port) {
// Case 0 does not need to do anything
case 1:
HAL_FreeDIOPort(digitalHandles[0]);
Expand All @@ -417,7 +417,7 @@ void HAL_CloseSPI(HAL_SPIPort port) {
default:
break;
}
spiHandles->Free(spiHandles->GetHandleForPort(port));
spiHandles->Free(handle);
}

void HAL_SetSPISpeed(HAL_SPIPort port, int32_t speed) {
Expand Down
6 changes: 3 additions & 3 deletions hal/src/main/native/include/hal/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ HAL_SPIHandle HAL_InitializeSPI(HAL_SPIPort port, int32_t* status);
* @param size Number of bytes to transfer. [0..7]
* @return Number of bytes transferred, -1 for error
*/
int32_t HAL_TransactionSPI(HAL_SPIPort port, const uint8_t* dataToSend,
int32_t HAL_TransactionSPI(HAL_SPIHandle port, const uint8_t* dataToSend,
uint8_t* dataReceived, int32_t size);

/**
Expand Down Expand Up @@ -82,9 +82,9 @@ int32_t HAL_ReadSPI(HAL_SPIPort port, uint8_t* buffer, int32_t count);
/**
* Closes the SPI port.
*
* @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP
* @param handle The port handle.
*/
void HAL_CloseSPI(HAL_SPIPort port);
void HAL_CloseSPI(HAL_SPIHandle handle);

/**
* Sets the clock speed for the SPI bus.
Expand Down
62 changes: 54 additions & 8 deletions hal/src/main/native/sim/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,80 @@

#include "HALInitializer.h"
#include "mockdata/SPIDataInternal.h"

#include "hal/handles/IndexedHandleResource.h"
#include "HALInternal.h"
#include <fmt/format.h>
using namespace hal;

namespace {
struct SPI {
HAL_SPIPort port;
};
} // namespace

static constexpr int32_t kSpiMaxHandles = 5;

typedef IndexedHandleResource<HAL_SPIHandle, SPI, kSpiMaxHandles,
HAL_HandleEnum::SPI>
SPIHandleResource;

static SPIHandleResource* spiHandles;

namespace hal::init {
void InitializeSPI() {}
void InitializeSPI() {
static SPIHandleResource sH;
spiHandles = &sH;
}
} // namespace hal::init

extern "C" {

HAL_SPIHandle HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
hal::init::CheckInit();
if (port < 0 || port >= kSpiMaxHandles) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(
status, fmt::format("SPI port must be between 0 and {}. Requested {}",
kSpiMaxHandles, static_cast<int>(port)));
return HAL_kInvalidHandle;
}

HAL_SPIHandle hal_handle;
auto spi =
spiHandles->Allocate(static_cast<int16_t>(port), &hal_handle, status);

if (*status != 0) {
return HAL_kInvalidHandle;
}

SimSPIData[port].initialized = true;
// TODO: deal with this
return 0;
spi->port = port;
return hal_handle;
}
int32_t HAL_TransactionSPI(HAL_SPIPort port, const uint8_t* dataToSend,

int32_t HAL_TransactionSPI(HAL_SPIHandle handle, const uint8_t* dataToSend,
uint8_t* dataReceived, int32_t size) {
return SimSPIData[port].Transaction(dataToSend, dataReceived, size);
auto spi = spiHandles->Get(handle);
if (!spi) {
return -1;
}
return SimSPIData[spi->port].Transaction(dataToSend, dataReceived, size);
}

int32_t HAL_WriteSPI(HAL_SPIPort port, const uint8_t* dataToSend,
int32_t sendSize) {
return SimSPIData[port].Write(dataToSend, sendSize);
}
int32_t HAL_ReadSPI(HAL_SPIPort port, uint8_t* buffer, int32_t count) {
return SimSPIData[port].Read(buffer, count);
}
void HAL_CloseSPI(HAL_SPIPort port) {
SimSPIData[port].initialized = false;
void HAL_CloseSPI(HAL_SPIHandle handle) {
auto spi = spiHandles->Get(handle);
if (!spi) {
return;
}
SimSPIData[spi->port].initialized = false;
spiHandles->Free(handle);
}
void HAL_SetSPISpeed(HAL_SPIPort port, int32_t speed) {}
void HAL_SetSPIMode(HAL_SPIPort port, HAL_SPIMode mode) {}
Expand Down
6 changes: 3 additions & 3 deletions wpilibc/src/main/native/cpp/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ void SPI::Accumulator::Update() {

SPI::SPI(Port port) : m_port(static_cast<HAL_SPIPort>(port)) {
int32_t status = 0;
HAL_InitializeSPI(m_port, &status);
HAL_SetSPIMode(m_port, m_mode);
m_handle = HAL_InitializeSPI(m_port, &status);
FRC_CheckErrorStatus(status, "Port {}", static_cast<int>(m_port));
HAL_SetSPIMode(m_port, m_mode);

HAL_Report(HALUsageReporting::kResourceType_SPI,
static_cast<uint8_t>(port) + 1);
Expand Down Expand Up @@ -203,7 +203,7 @@ int SPI::Read(bool initiate, uint8_t* dataReceived, int size) {
if (initiate) {
wpi::SmallVector<uint8_t, 32> dataToSend;
dataToSend.resize(size);
retVal = HAL_TransactionSPI(m_port, dataToSend.data(), dataReceived, size);
retVal = HAL_TransactionSPI(m_handle, dataToSend.data(), dataReceived, size);
} else {
retVal = HAL_ReadSPI(m_port, dataReceived, size);
}
Expand Down
4 changes: 3 additions & 1 deletion wpilibc/src/main/native/include/frc/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <span>

#include <hal/SPI.h>
#include <hal/SPITypes.h>
#include <units/time.h>

Expand Down Expand Up @@ -356,7 +357,8 @@ class SPI {
double GetAccumulatorIntegratedAverage() const;

protected:
hal::SPIPort m_port;
HAL_SPIPort m_port;
hal::Handle<HAL_SPIHandle, HAL_CloseSPI> m_handle;
HAL_SPIMode m_mode = HAL_SPIMode::HAL_SPI_kMode0;

private:
Expand Down

0 comments on commit a2906b8

Please sign in to comment.