-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBt_Spi.cpp
69 lines (48 loc) · 2 KB
/
Bt_Spi.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//*************************************************************************************************
//
// BITTAILOR.CH - BtMqttSn, an Arduino library for MQTT-SN over nRF24L01+
//
//-------------------------------------------------------------------------------------------------
//
// Bt::Spi
//
//*************************************************************************************************
#include "Bt_Spi.hpp"
#include <Arduino.h>
#include <SPI.h>
namespace Bt {
//-------------------------------------------------------------------------------------------------
Spi::Spi(BitOrder pBitOrder, Mode pSpiMode, Speed pSpeed, Pin& pChipSelectPin)
: mChipSelectPin(&pChipSelectPin) {
SPI.begin();
switch (pBitOrder) {
case Spi::BIT_ORDER_LSBFIRST : SPI.setBitOrder(LSBFIRST); break;
case Spi::BIT_ORDER_MSBFIRST : SPI.setBitOrder(MSBFIRST); break;
}
switch (pSpiMode) {
case Spi::MODE_0 : SPI.setDataMode(SPI_MODE0); break;
case Spi::MODE_1 : SPI.setDataMode(SPI_MODE1); break;
case Spi::MODE_2 : SPI.setDataMode(SPI_MODE2); break;
case Spi::MODE_3 : SPI.setDataMode(SPI_MODE3); break;
}
//TODO (BT) check/calculate from F_CPU
switch (pSpeed) {
case Spi::SPEED_4_MHZ : SPI.setClockDivider(SPI_CLOCK_DIV4); break;
case Spi::SPEED_8_MHZ : SPI.setClockDivider(SPI_CLOCK_DIV2); break;
}
mChipSelectPin->mode(Pin::MODE_OUTPUT);
mChipSelectPin->write(true);
}
//-------------------------------------------------------------------------------------------------
Spi::~Spi() {
}
//-------------------------------------------------------------------------------------------------
void Spi::chipSelect(bool iSelect) {
mChipSelectPin->write(!iSelect);
}
//-------------------------------------------------------------------------------------------------
uint8_t Spi::transfer(uint8_t iData) {
return SPI.transfer(iData);;
}
//-------------------------------------------------------------------------------------------------
} // namespace Bt