forked from crteensy/DmaSpi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChipSelect.h
More file actions
84 lines (70 loc) · 2.22 KB
/
ChipSelect.h
File metadata and controls
84 lines (70 loc) · 2.22 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef CHIPSELECT_H
#define CHIPSELECT_H
#include <core_pins.h>
/** \brief An abstract base class that provides an interface for chip select classes.
**/
class AbstractChipSelect
{
public:
/** \brief Called to select a chip. The implementing class can do other things as well.
**/
virtual void select() = 0;
/** \brief Called to deselect a chip. The implementing class can do other things as well.
**/
virtual void deselect() = 0;
/** \brief the virtual destructor needed to inherit from this class **/
virtual ~AbstractChipSelect() {}
};
/** \brief "do nothing" chip select class **/
class DummyChipSelect : public AbstractChipSelect
{
void select() override {}
void deselect() override {}
};
/** \brief "do nothing" chip select class that
* outputs a message through Serial when something happens
**/
class DebugChipSelect : public AbstractChipSelect
{
void select() override {Serial.println("Debug CS: select()");}
void deselect() override {Serial.println("Debug CS: deselect()");}
};
/** \brief An active low chip select class. This also configures the given pin.
**/
class ActiveLowChipSelect : public AbstractChipSelect
{
public:
/** Configues a chip select pin for OUTPUT mode,
* manages the chip selection and a corresponding SPI transaction
*
* The chip select pin is asserted \e after the SPI settings are applied
* and deasserted before the SPI transaction ends.
* \param pin the CS pin to use
* \param settings which SPI settings to apply when the chip is selected
**/
ActiveLowChipSelect(const unsigned int& pin, const SPISettings& settings)
: pin_(pin),
settings_(settings)
{
pinMode(pin, OUTPUT);
digitalWriteFast(pin, 1);
}
/** \brief begins an SPI transaction selects the chip (sets the pin to low) and
**/
void select() override
{
SPI.beginTransaction(settings_);
digitalWriteFast(pin_, 0);
}
/** \brief deselects the chip (sets the pin to high) and ends the SPI transaction
**/
void deselect() override
{
digitalWriteFast(pin_, 1);
SPI.endTransaction();
}
private:
const unsigned int pin_;
const SPISettings settings_;
};
#endif // CHIPSELECT_H