Skip to content

Commit a8fee49

Browse files
committed
Initial support for secondary SPI bus
1 parent f97f890 commit a8fee49

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/MagneticSensorSPI.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,22 @@ MagneticSensorSPI::MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs){
5959
data_start_bit = config.data_start_bit; // for backwards compatibilty
6060
}
6161

62-
void MagneticSensorSPI::init(){
62+
void MagneticSensorSPI::init(SPIClass* _spi){
63+
64+
spi = _spi;
65+
6366
// 1MHz clock (AMS should be able to accept up to 10MHz)
6467
settings = SPISettings(clock_speed, MSBFIRST, spi_mode);
6568

6669
//setup pins
6770
pinMode(chip_select_pin, OUTPUT);
6871

6972
//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
70-
SPI.begin();
73+
spi->begin();
7174
#ifndef ESP_H // if not ESP32 board
72-
SPI.setBitOrder(MSBFIRST); // Set the SPI_1 bit order
73-
SPI.setDataMode(spi_mode) ;
74-
SPI.setClockDivider(SPI_CLOCK_DIV8);
75+
spi->setBitOrder(MSBFIRST); // Set the SPI_1 bit order
76+
spi->setDataMode(spi_mode) ;
77+
spi->setClockDivider(SPI_CLOCK_DIV8);
7578
#endif
7679

7780
digitalWrite(chip_select_pin, HIGH);
@@ -203,13 +206,13 @@ word MagneticSensorSPI::read(word angle_register){
203206

204207
#if !defined(_STM32_DEF_) // if not stm chips
205208
//SPI - begin transaction
206-
SPI.beginTransaction(settings);
209+
spi->beginTransaction(settings);
207210
#endif
208211

209212
//Send the command
210213
digitalWrite(chip_select_pin, LOW);
211214
digitalWrite(chip_select_pin, LOW);
212-
SPI.transfer16(command);
215+
spi->transfer16(command);
213216
digitalWrite(chip_select_pin,HIGH);
214217
digitalWrite(chip_select_pin,HIGH);
215218

@@ -222,13 +225,13 @@ word MagneticSensorSPI::read(word angle_register){
222225
//Now read the response
223226
digitalWrite(chip_select_pin, LOW);
224227
digitalWrite(chip_select_pin, LOW);
225-
word register_value = SPI.transfer16(0x00);
228+
word register_value = spi->transfer16(0x00);
226229
digitalWrite(chip_select_pin, HIGH);
227230
digitalWrite(chip_select_pin,HIGH);
228231

229232
#if !defined(_STM32_DEF_) // if not stm chips
230233
//SPI - end transaction
231-
SPI.endTransaction();
234+
spi->endTransaction();
232235
#endif
233236

234237
register_value = register_value >> (1 + data_start_bit - bit_resolution); //this should shift data to the rightmost bits of the word
@@ -243,5 +246,5 @@ word MagneticSensorSPI::read(word angle_register){
243246
* SPI has an internal SPI-device counter, for each init()-call the close() function must be called exactly 1 time
244247
*/
245248
void MagneticSensorSPI::close(){
246-
SPI.end();
249+
spi->end();
247250
}

src/MagneticSensorSPI.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MagneticSensorSPI: public Sensor{
3838
MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs);
3939

4040
/** sensor initialise pins */
41-
void init();
41+
void init(SPIClass* _spi = &SPI);
4242

4343
// implementation of abstract functions of the Sensor class
4444
/** get current angle (rad) */
@@ -103,6 +103,7 @@ class MagneticSensorSPI: public Sensor{
103103
int command_rw_bit; //!< the bit where read/write flag is stored in command
104104
int data_start_bit; //!< the the position of first bit
105105

106+
SPIClass* spi;
106107
};
107108

108109

0 commit comments

Comments
 (0)