1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
/*
* SharedSpiDevice.cpp
*
* Created on: 28 Jul 2019
* Author: David
*/
#include "SharedSpiClient.h"
#include "SharedSpiDevice.h"
#include <Hardware/IoPorts.h>
// SharedSpiDevice class members
SharedSpiClient::SharedSpiClient(SharedSpiDevice& dev, uint32_t clockFreq, SpiMode m, Pin p, bool polarity) noexcept
: device(dev), clockFrequency(clockFreq), csPin(p), mode(m), csActivePolarity(polarity)
{
InitCsPin();
}
void SharedSpiClient::InitCsPin() const noexcept
{
if (csPin != NoPin)
{
IoPort::SetPinMode(csPin, (csActivePolarity) ? OUTPUT_LOW : OUTPUT_HIGH);
}
}
// Get ownership of this SPI, return true if successful
bool SharedSpiClient::Select(uint32_t timeout) const noexcept
{
const bool ok = device.Take(timeout);
if (ok)
{
device.SetClockFrequencyAndMode(clockFrequency, mode);
delayMicroseconds(1); // allow the clock time to settle
IoPort::WriteDigital(csPin, csActivePolarity);
}
return ok;
}
void SharedSpiClient::Deselect() const noexcept
{
IoPort::WriteDigital(csPin, !csActivePolarity);
delayMicroseconds(1); // in case the clock makes an abrupt transition when we disable SPI
device.Disable();
device.Release();
}
bool SharedSpiClient::TransceivePacket(const uint8_t* tx_data, uint8_t* rx_data, size_t len) const noexcept
{
return device.TransceivePacket(tx_data, rx_data, len);
}
// End
|