Skip to content

Commit a13a76e

Browse files
Merge pull request #87 from runger1101001/stm32_sensor_fix
STM32 MagneticSensorSPI bugfix - fix for issue #82
2 parents 9105744 + efda8c5 commit a13a76e

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

src/sensors/MagneticSensorSPI.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,8 @@ void MagneticSensorSPI::init(SPIClass* _spi){
7575

7676
//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
7777
spi->begin();
78-
#ifndef ESP_H // if not ESP32 board
79-
spi->setBitOrder(MSBFIRST); // Set the SPI_1 bit order
80-
spi->setDataMode(spi_mode) ;
81-
spi->setClockDivider(SPI_CLOCK_DIV8);
82-
#endif
78+
// do any architectures need to set the clock divider for SPI? Why was this in the code?
79+
//spi->setClockDivider(SPI_CLOCK_DIV8);
8380

8481
digitalWrite(chip_select_pin, HIGH);
8582
// velocity calculation init
@@ -170,35 +167,27 @@ word MagneticSensorSPI::read(word angle_register){
170167
command |= ((word)spiCalcEvenParity(command) << command_parity_bit);
171168
}
172169

173-
#if !defined(_STM32_DEF_) // if not stm chips
174170
//SPI - begin transaction
175171
spi->beginTransaction(settings);
176-
#endif
177172

178173
//Send the command
179174
digitalWrite(chip_select_pin, LOW);
180-
digitalWrite(chip_select_pin, LOW);
181175
spi->transfer16(command);
182176
digitalWrite(chip_select_pin,HIGH);
183-
digitalWrite(chip_select_pin,HIGH);
184177

185178
#if defined( ESP_H ) // if ESP32 board
186-
delayMicroseconds(50);
179+
delayMicroseconds(50); // why do we need to delay 50us on ESP32? In my experience no extra delays are needed, on any of the architectures I've tested...
187180
#else
188-
delayMicroseconds(10);
181+
delayMicroseconds(1); // delay 1us, the minimum time possible in plain arduino. 350ns is the required time for AMS sensors, 80ns for MA730, MA702
189182
#endif
190183

191184
//Now read the response
192185
digitalWrite(chip_select_pin, LOW);
193-
digitalWrite(chip_select_pin, LOW);
194186
word register_value = spi->transfer16(0x00);
195187
digitalWrite(chip_select_pin, HIGH);
196-
digitalWrite(chip_select_pin,HIGH);
197188

198-
#if !defined(_STM32_DEF_) // if not stm chips
199189
//SPI - end transaction
200190
spi->endTransaction();
201-
#endif
202191

203192
register_value = register_value >> (1 + data_start_bit - bit_resolution); //this should shift data to the rightmost bits of the word
204193

0 commit comments

Comments
 (0)