Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions Drv/LinuxSpiDriver/LinuxSpiDriverComponentImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ namespace Drv {

bool LinuxSpiDriverComponentImpl::open(NATIVE_INT_TYPE device,
NATIVE_INT_TYPE select,
SpiFrequency clock) {
SpiFrequency clock,
SpiMode spiMode) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should assert that spiMode is in the correct range:

FW_ASSERT(spiMode >= SPI_MODE_0 || spiMode <= SPI_MODE_3);

this->m_device = device;
this->m_select = select;
Expand All @@ -97,9 +98,29 @@ namespace Drv {

// Configure:
/*
* SPI Mode 0
* SPI Mode 0, 1, 2, 3
*/
U8 mode = SPI_MODE_0; // Mode 0 (CPOL = 0, CPHA = 0)

U8 mode; // Mode Select (CPOL = 0/1, CPHA = 0/1)
switch(spiMode) {

Check warning

Code scanning / CodeQL

Unchecked function argument

This use of parameter spiMode has not been checked.
case SpiMode::SPI_MODE_CPOL_LOW_CPHA_LOW:
mode = SPI_MODE_0;
break;
case SpiMode::SPI_MODE_CPOL_LOW_CPHA_HIGH:
mode = SPI_MODE_1;
break;
case SpiMode::SPI_MODE_CPOL_HIGH_CPHA_LOW:
mode = SPI_MODE_2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed! Missing break statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just added the missing statements

break;
case SpiMode::SPI_MODE_CPOL_HIGH_CPHA_HIGH:
mode = SPI_MODE_3;
break;
default:
//Assert if the device SPI Mode is not in the correct range
FW_ASSERT(0, spiMode);
break;
}

ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1) {
DEBUG_PRINT("ioctl SPI_IOC_WR_MODE fd %d failed. %d\n",fd,errno);
Expand Down
22 changes: 21 additions & 1 deletion Drv/LinuxSpiDriver/LinuxSpiDriverComponentImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ namespace Drv {
SPI_FREQUENCY_20MHZ = 20000000UL,
};

/**
* SPI Mode Select
*
* Defines the SPI Clock Polarity and Phase for each SPI Transaction.
*
* SPI Clock Polarity(CPOL): Defines clock polarity as idle low (CPOL = 0) or idle high(CPOL = 1)
* SPI Clock Phase(CPHA): Defines if data is shifted out on the rising clock edge and sampled
* on the falling clock edge(CPHA = 0) or if data is shifted out on the
* falling clock edge and sampled on the rising clock edge(CPHA=1)
*
*/
enum SpiMode
{
SPI_MODE_CPOL_LOW_CPHA_LOW, ///< (CPOL = 0, CPHA = 0)
SPI_MODE_CPOL_LOW_CPHA_HIGH,///< (CPOL = 0, CPHA = 1)
SPI_MODE_CPOL_HIGH_CPHA_LOW,///< (CPOL = 1, CPHA = 0)
SPI_MODE_CPOL_HIGH_CPHA_HIGH,///< (CPOL = 1, CPHA = 1)
};

class LinuxSpiDriverComponentImpl: public LinuxSpiDriverComponentBase {

public:
Expand All @@ -59,7 +78,8 @@ namespace Drv {
//! Open device
bool open(NATIVE_INT_TYPE device,
NATIVE_INT_TYPE select,
SpiFrequency clock);
SpiFrequency clock,
SpiMode spiMode = SpiMode::SPI_MODE_CPOL_LOW_CPHA_LOW);

PRIVATE:

Expand Down
3 changes: 2 additions & 1 deletion Drv/LinuxSpiDriver/LinuxSpiDriverComponentImplStub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace Drv {

bool LinuxSpiDriverComponentImpl::open(NATIVE_INT_TYPE device,
NATIVE_INT_TYPE select,
SpiFrequency clock) {
SpiFrequency clock,
SpiMode spiMode) {
//TODO: fill this function out
return false;
}
Expand Down