-
Notifications
You must be signed in to change notification settings - Fork 1
/
bmp_spi.cpp
155 lines (132 loc) · 3.46 KB
/
bmp_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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "bmp_spi.h"
int8_t _BMP3_SoftwareSPI_MOSI; ///< Global SPI MOSI pin
int8_t _BMP3_SoftwareSPI_MISO; ///< Global SPI MISO pin
int8_t _BMP3_SoftwareSPI_SCK; ///< Global SPI Clock pin
static int8_t spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len);
static int8_t spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len);
static uint8_t spi_transfer(uint8_t x);
BMP3_SPI::BMP3_SPI(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin)
{
_cs = cspin;
_BMP3_SoftwareSPI_MOSI = mosipin;
_BMP3_SoftwareSPI_MISO = misopin;
_BMP3_SoftwareSPI_SCK = sckpin;
}
bool BMP3_SPI::init()
{
digitalWrite(_cs, HIGH);
pinMode(_cs, OUTPUT);
if (_BMP3_SoftwareSPI_SCK == -1)
{
// hardware SPI
SPI.begin();
}
else
{
// software SPI
pinMode(_BMP3_SoftwareSPI_SCK, OUTPUT);
pinMode(_BMP3_SoftwareSPI_MOSI, OUTPUT);
pinMode(_BMP3_SoftwareSPI_MISO, INPUT);
}
the_sensor.dev_id = _cs;
the_sensor.intf = BMP3_SPI_INTF;
the_sensor.read = &spi_read;
the_sensor.write = &spi_write;
return BMP3_I2C::initSensor();
}
/**************************************************************************/
/*!
@brief Reads 8 bit values over SPI
*/
/**************************************************************************/
static int8_t spi_read(uint8_t cspin, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
#ifdef BMP3XX_DEBUG
Serial.print("\tSPI $");
Serial.print(reg_addr, HEX);
Serial.print(" => ");
#endif
// If hardware SPI we should use transactions!
if (_BMP3_SoftwareSPI_SCK == -1)
{
SPI.beginTransaction(SPISettings(BMP3XX_DEFAULT_SPIFREQ, MSBFIRST, SPI_MODE0));
}
digitalWrite(cspin, LOW);
spi_transfer(reg_addr | 0x80);
while (len--)
{
*reg_data = spi_transfer(0x00);
#ifdef BMP3XX_DEBUG
Serial.print("0x");
Serial.print(*reg_data, HEX);
Serial.print(", ");
#endif
reg_data++;
}
digitalWrite(cspin, HIGH);
if (_BMP3_SoftwareSPI_SCK == -1)
{
SPI.endTransaction();
}
#ifdef BMP3XX_DEBUG
Serial.println("");
#endif
return 0;
}
/**************************************************************************/
/*!
@brief Writes 8 bit values over SPI
*/
/**************************************************************************/
static int8_t spi_write(uint8_t cspin, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
#ifdef BMP3XX_DEBUG
Serial.print("\tSPI $");
Serial.print(reg_addr, HEX);
Serial.print(" <= ");
#endif
// If hardware SPI we should use transactions!
if (_BMP3_SoftwareSPI_SCK == -1)
{
SPI.beginTransaction(SPISettings(BMP3XX_DEFAULT_SPIFREQ, MSBFIRST, SPI_MODE0));
}
digitalWrite(cspin, LOW);
spi_transfer(reg_addr);
while (len--)
{
spi_transfer(*reg_data);
#ifdef BMP3XX_DEBUG
Serial.print("0x");
Serial.print(*reg_data, HEX);
Serial.print(", ");
#endif
reg_data++;
}
digitalWrite(cspin, HIGH);
if (_BMP3_SoftwareSPI_SCK == -1)
{
SPI.endTransaction();
}
#ifdef BMP3XX_DEBUG
Serial.println("");
#endif
return 0;
}
static uint8_t spi_transfer(uint8_t x)
{
if (_BMP3_SoftwareSPI_SCK == -1)
return SPI.transfer(x);
// software spi
//Serial.println("Software SPI");
uint8_t reply = 0;
for (int i = 7; i >= 0; i--)
{
reply <<= 1;
digitalWrite(_BMP3_SoftwareSPI_SCK, LOW);
digitalWrite(_BMP3_SoftwareSPI_MOSI, x & (1 << i));
digitalWrite(_BMP3_SoftwareSPI_SCK, HIGH);
if (digitalRead(_BMP3_SoftwareSPI_MISO))
reply |= 1;
}
return reply;
}