-
Notifications
You must be signed in to change notification settings - Fork 4
/
SPI.h
132 lines (118 loc) · 2.79 KB
/
SPI.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @file Software/SPI.h
* @version 1.2
*
* @section License
* Copyright (C) 2017, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifndef SOFTWARE_SPI_H
#define SOFTWARE_SPI_H
#include "GPIO.h"
#include "SPI.h"
#ifndef CHARBITS
#define CHARBITS 8
#endif
/**
* Software Serial Perpheral Interface (SPI) class using GPIO.
* @param[in] MOSI_PIN board pin for master output slave input.
* @param[in] MISO_PIN board pin for master input slave output.
* @param[in] SCK_PIN board pin for clock.
*/
namespace Software {
template<BOARD::pin_t MOSI_PIN,
BOARD::pin_t MISO_PIN,
BOARD::pin_t SCK_PIN>
class SPI : public ::SPI {
public:
/**
* Serial Perpheral Interface (SPI) constructor. Initiate pin
* input/output mode.
*/
SPI() :
::SPI()
{
m_sck.output();
m_mosi.output();
m_miso.input();
}
/**
* @override{SPI}
* Acquire bus access. Yield until bus is released.
* @param[in] mode of access.
* @param[in] bitorder of serial data.
* @param[in] scale clock frequency.
*/
virtual void acquire(uint8_t mode, uint8_t bitorder, uint8_t scale)
{
(void) scale;
lock();
m_sck = mode & 2;
m_cpha = mode & 1;
m_bitorder = bitorder;
}
/**
* @override{SPI}
* Release bus access.
*/
virtual void release()
{
m_mosi = LOW;
unlock();
}
/**
* @override{SPI}
* Send given byte to device. Returns byte received from device.
* @param[in] value to send to device.
* @return received value.
*/
virtual uint8_t transfer(uint8_t value)
{
const bool cpha = m_cpha;
uint8_t bits = CHARBITS;
if (m_bitorder == MSBFIRST) {
do {
if (cpha) m_sck.toggle();
m_mosi = value & 0x80;
m_sck.toggle();
value <<= 1;
value |= m_miso;
if (!cpha) m_sck.toggle();
} while (--bits);
}
else {
do {
if (cpha) m_sck.toggle();
m_mosi = value & 0x01;
m_sck.toggle();
value >>= 1;
value |= (m_miso ? 0x80 : 0x00);
if (!cpha) m_sck.toggle();
} while (--bits);
}
return (value);
}
using ::SPI::transfer;
protected:
/** Clock pin. */
GPIO<SCK_PIN> m_sck;
/** Master Output Slave Input pin. */
GPIO<MOSI_PIN> m_mosi;
/** Master Input Slave Output pin. */
GPIO<MISO_PIN> m_miso;
/** Clock phase flag. */
bool m_cpha;
/** Serial data bitorder flag. */
uint8_t m_bitorder;
};
};
#endif