55// _bit_resolution sensor resolution bit number
66// _angle_register - (optional) angle read register - default 0x3FFF
77MagneticSensorSPI::MagneticSensorSPI (int cs, float _bit_resolution, int _angle_register){
8- // chip select pin
8+
99 chip_select_pin = cs;
1010 // angle read register of the magnetic sensor
1111 angle_register = _angle_register ? _angle_register : DEF_ANGLE_REGISTAR;
1212 // register maximum value (counts per revolution)
1313 cpr = pow (2 ,_bit_resolution);
14-
14+ spi_mode = SPI_MODE1;
15+ clock_speed = 1000000 ;
16+ bit_resolution = _bit_resolution;
17+
18+ command_parity_bit = 15 ; // for backwards compatibilty
19+ command_rw_bit = 14 ; // for backwards compatibilty
20+ data_start_bit = 13 ; // for backwards compatibilty
21+
1522}
1623
24+ MagneticSensorSPI::MagneticSensorSPI (MagneticSensorSPIConfig_s config, int cs){
25+ chip_select_pin = cs;
26+ // angle read register of the magnetic sensor
27+ angle_register = config.angle_register ? config.angle_register : DEF_ANGLE_REGISTAR;
28+ // register maximum value (counts per revolution)
29+ cpr = pow (2 , config.bit_resolution );
30+ spi_mode = config.spi_mode ;
31+ clock_speed = config.clock_speed ;
32+ bit_resolution = config.bit_resolution ;
33+
34+ command_parity_bit = config.command_parity_bit ; // for backwards compatibilty
35+ command_rw_bit = config.command_rw_bit ; // for backwards compatibilty
36+ data_start_bit = config.data_start_bit ; // for backwards compatibilty
37+ }
1738
1839void MagneticSensorSPI::init (){
1940 // 1MHz clock (AMS should be able to accept up to 10MHz)
20- settings = SPISettings (1000000 , MSBFIRST, SPI_MODE1 );
41+ settings = SPISettings (clock_speed , MSBFIRST, spi_mode );
2142
2243 // setup pins
2344 pinMode (chip_select_pin, OUTPUT);
24-
2545
2646 // SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
2747 SPI.begin ();
2848#ifndef ESP_H // if not ESP32 board
2949 SPI.setBitOrder (MSBFIRST); // Set the SPI_1 bit order
30- SPI.setDataMode (SPI_MODE1 ) ;
50+ SPI.setDataMode (spi_mode ) ;
3151 SPI.setClockDivider (SPI_CLOCK_DIV8);
3252#endif
3353
@@ -77,7 +97,7 @@ float MagneticSensorSPI::getVelocity(){
7797 float angle_c = getAngle ();
7898 // velocity calculation
7999 float vel = (angle_c - angle_prev)/Ts;
80-
100+
81101 // save variables for future pass
82102 angle_prev = angle_c;
83103 velocity_calc_timestamp = now_us;
@@ -147,35 +167,28 @@ byte MagneticSensorSPI::spiCalcEvenParity(word value){
147167 * Returns the value of the register
148168 */
149169word MagneticSensorSPI::read (word angle_register){
150- word command = 0b0100000000000000 ; // PAR=0 R/W=R
151- command = command | angle_register;
152-
153- // Add a parity bit on the the MSB
154- command |= ((word)spiCalcEvenParity (command)<<15 );
155170
156- // Split the command into two bytes
157- byte right_byte = command & 0xFF ;
158- byte left_byte = ( command >> 8 ) & 0xFF ;
171+ word command = angle_register;
159172
173+ if (command_rw_bit > 0 ) {
174+ command = angle_register | (1 << command_rw_bit);
175+ }
176+ if (command_parity_bit > 0 ) {
177+ // Add a parity bit on the the MSB
178+ command |= ((word)spiCalcEvenParity (command) << command_parity_bit);
179+ }
160180
161181#if !defined(_STM32_DEF_) // if not stm chips
162182 // SPI - begin transaction
163183 SPI.beginTransaction (settings);
164184#endif
165- // SPI - begin transaction
166- // SPI.beginTransaction(settings);
167185
168186 // Send the command
169187 digitalWrite (chip_select_pin, LOW);
170- #ifndef ESP_H // if not ESP32 board
171188 digitalWrite (chip_select_pin, LOW);
172- #endif
173- SPI.transfer (left_byte);
174- SPI.transfer (right_byte);
189+ SPI.transfer16 (command);
175190 digitalWrite (chip_select_pin,HIGH);
176- #ifndef ESP_H // if not ESP32 board
177191 digitalWrite (chip_select_pin,HIGH);
178- #endif
179192
180193#if defined( ESP_H ) // if ESP32 board
181194 delayMicroseconds (50 );
@@ -186,18 +199,20 @@ word MagneticSensorSPI::read(word angle_register){
186199 // Now read the response
187200 digitalWrite (chip_select_pin, LOW);
188201 digitalWrite (chip_select_pin, LOW);
189- left_byte = SPI.transfer (0x00 );
190- right_byte = SPI.transfer (0x00 );
202+ word register_value = SPI.transfer16 (0x00 );
191203 digitalWrite (chip_select_pin, HIGH);
192204 digitalWrite (chip_select_pin,HIGH);
193205
194206#if !defined(_STM32_DEF_) // if not stm chips
195207 // SPI - end transaction
196208 SPI.endTransaction ();
197209#endif
210+
211+ register_value = register_value >> (1 + data_start_bit - bit_resolution); // this should shift data to the rightmost bits of the word
212+
213+ const static word data_mask = ~(0 >> (16 - bit_resolution));
198214
199- // Return the data, stripping the parity and error bits
200- return (( ( left_byte & 0xFF ) << 8 ) | ( right_byte & 0xFF )) & ~0xC000 ;
215+ return register_value & data_mask; // Return the data, stripping the non data (e.g parity) bits
201216}
202217
203218/* *
0 commit comments