@@ -59,19 +59,22 @@ MagneticSensorSPI::MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs){
59
59
data_start_bit = config.data_start_bit ; // for backwards compatibilty
60
60
}
61
61
62
- void MagneticSensorSPI::init (){
62
+ void MagneticSensorSPI::init (SPIClass* _spi){
63
+
64
+ spi = _spi;
65
+
63
66
// 1MHz clock (AMS should be able to accept up to 10MHz)
64
67
settings = SPISettings (clock_speed, MSBFIRST, spi_mode);
65
68
66
69
// setup pins
67
70
pinMode (chip_select_pin, OUTPUT);
68
71
69
72
// SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
70
- SPI. begin ();
73
+ spi-> begin ();
71
74
#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);
75
78
#endif
76
79
77
80
digitalWrite (chip_select_pin, HIGH);
@@ -203,13 +206,13 @@ word MagneticSensorSPI::read(word angle_register){
203
206
204
207
#if !defined(_STM32_DEF_) // if not stm chips
205
208
// SPI - begin transaction
206
- SPI. beginTransaction (settings);
209
+ spi-> beginTransaction (settings);
207
210
#endif
208
211
209
212
// Send the command
210
213
digitalWrite (chip_select_pin, LOW);
211
214
digitalWrite (chip_select_pin, LOW);
212
- SPI. transfer16 (command);
215
+ spi-> transfer16 (command);
213
216
digitalWrite (chip_select_pin,HIGH);
214
217
digitalWrite (chip_select_pin,HIGH);
215
218
@@ -222,13 +225,13 @@ word MagneticSensorSPI::read(word angle_register){
222
225
// Now read the response
223
226
digitalWrite (chip_select_pin, LOW);
224
227
digitalWrite (chip_select_pin, LOW);
225
- word register_value = SPI. transfer16 (0x00 );
228
+ word register_value = spi-> transfer16 (0x00 );
226
229
digitalWrite (chip_select_pin, HIGH);
227
230
digitalWrite (chip_select_pin,HIGH);
228
231
229
232
#if !defined(_STM32_DEF_) // if not stm chips
230
233
// SPI - end transaction
231
- SPI. endTransaction ();
234
+ spi-> endTransaction ();
232
235
#endif
233
236
234
237
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){
243
246
* SPI has an internal SPI-device counter, for each init()-call the close() function must be called exactly 1 time
244
247
*/
245
248
void MagneticSensorSPI::close (){
246
- SPI. end ();
249
+ spi-> end ();
247
250
}
0 commit comments