Skip to content

Commit 823126b

Browse files
authored
Merge pull request #515 from simplefoc/feat_allow_spi_clock_speed_change
Feat allow spi clock speed change
2 parents 0f4300e + 9995643 commit 823126b

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/sensors/MagneticSensorSPI.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,32 @@ MagneticSensorSPIConfig_s MA730_SPI = {
5050
// cs - SPI chip select pin
5151
// _bit_resolution sensor resolution bit number
5252
// _angle_register - (optional) angle read register - default 0x3FFF
53-
MagneticSensorSPI::MagneticSensorSPI(int cs, int _bit_resolution, int _angle_register){
53+
MagneticSensorSPI::MagneticSensorSPI(int cs, int _bit_resolution, int _angle_register, long _clock_speed){
5454

5555
chip_select_pin = cs;
5656
// angle read register of the magnetic sensor
5757
angle_register = _angle_register ? _angle_register : DEF_ANGLE_REGISTER;
5858
// register maximum value (counts per revolution)
5959
cpr = _powtwo(_bit_resolution);
6060
spi_mode = SPI_MODE1;
61-
clock_speed = 1000000;
61+
clock_speed = _isset(_clock_speed) ? _clock_speed : 1000000;
6262
bit_resolution = _bit_resolution;
6363

6464
command_parity_bit = 15; // for backwards compatibilty
6565
command_rw_bit = 14; // for backwards compatibilty
6666
data_start_bit = 13; // for backwards compatibilty
6767
}
6868

69-
MagneticSensorSPI::MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs){
69+
MagneticSensorSPI::MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs, long _clock_speed){
7070
chip_select_pin = cs;
7171
// angle read register of the magnetic sensor
7272
angle_register = config.angle_register ? config.angle_register : DEF_ANGLE_REGISTER;
7373
// register maximum value (counts per revolution)
7474
cpr = _powtwo(config.bit_resolution);
7575
spi_mode = config.spi_mode;
76-
clock_speed = config.clock_speed;
76+
// allow to override clock speed from config with the one provided as argument
77+
// if the argument clock speed is not provided (i.e. is NOT_SET), use the clock speed from the config
78+
clock_speed = _isset(_clock_speed) ? _clock_speed : config.clock_speed;
7779
bit_resolution = config.bit_resolution;
7880

7981
command_parity_bit = config.command_parity_bit; // for backwards compatibilty

src/sensors/MagneticSensorSPI.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,19 @@ class MagneticSensorSPI: public Sensor{
2929
* @param cs SPI chip select pin
3030
* @param bit_resolution sensor resolution bit number
3131
* @param angle_register (optional) angle read register - default 0x3FFF
32+
* @param clock_speed (optional) SPI clock speed
3233
*/
33-
MagneticSensorSPI(int cs, int bit_resolution, int angle_register = 0);
34+
MagneticSensorSPI(int cs, int bit_resolution, int angle_register = 0, long clock_speed = NOT_SET);
35+
36+
/**
37+
* MagneticSensorSPI class constructor
3438
/**
3539
* MagneticSensorSPI class constructor
3640
* @param config SPI config
3741
* @param cs SPI chip select pin
42+
* @param clock_speed (optional) SPI clock speed
3843
*/
39-
MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs);
44+
MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs, long clock_speed = NOT_SET);
4045

4146
/** sensor initialise pins */
4247
void init(SPIClass* _spi = &SPI);

0 commit comments

Comments
 (0)