Skip to content

Commit fca0d0c

Browse files
committed
FEAT refactoring of the hardware specific code into the same folder
1 parent a875e40 commit fca0d0c

24 files changed

+123
-106
lines changed

src/drivers/hardware_specific/atmega2560_mcu.cpp renamed to src/hardware_specific/atmega/atmega2560_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../hardware_api.h"
1+
#include "./../../drivers/hardware_api.h"
22

33
#if defined(__AVR_ATmega2560__)
44

src/drivers/hardware_specific/atmega328_mcu.cpp renamed to src/hardware_specific/atmega/atmega328_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../hardware_api.h"
1+
#include "./../../drivers/hardware_api.h"
22

33
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega328PB__)
44

src/current_sense/hardware_specific/atmega_mcu.cpp renamed to src/hardware_specific/atmega/atmega_current_sense.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../hardware_api.h"
1+
#include "../../current_sense/hardware_api.h"
22

33
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega328PB__)
44

src/current_sense/hardware_specific/due_mcu.cpp renamed to src/hardware_specific/due/due_current_sense.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../hardware_api.h"
1+
#include "../../current_sense/hardware_api.h"
22

33
#if defined(__arm__) && defined(__SAM3X8E__)
44

src/drivers/hardware_specific/due_mcu.cpp renamed to src/hardware_specific/due/due_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../hardware_api.h"
1+
#include "./../../drivers/hardware_api.h"
22

33
#if defined(__arm__) && defined(__SAM3X8E__)
44

src/current_sense/hardware_specific/esp32_mcu.cpp renamed to src/hardware_specific/esp32/esp32_current_sense.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#include "../hardware_api.h"
2-
#include "../../drivers/hardware_api.h"
1+
#include "../../current_sense/hardware_api.h"
32

43
#ifdef ESP_H
54

src/drivers/hardware_specific/esp32_mcu.cpp renamed to src/hardware_specific/esp32/esp32_driver.cpp

Lines changed: 25 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,12 @@
1-
#include "../hardware_api.h"
1+
#include "./esp32_driver.h"
22

33
#if defined(ESP_H)
44

5-
#include "driver/mcpwm.h"
6-
#include "soc/mcpwm_reg.h"
7-
#include "soc/mcpwm_struct.h"
8-
9-
// empty motor slot
10-
#define _EMPTY_SLOT -20
11-
#define _TAKEN_SLOT -21
12-
13-
// ABI bus frequency - would be better to take it from somewhere
14-
// but I did nto find a good exposed variable
15-
#define _MCPWM_FREQ 160e6f
16-
17-
// preferred pwm resolution default
18-
#define _PWM_RES_DEF 2048
19-
// min resolution
20-
#define _PWM_RES_MIN 1500
21-
// max resolution
22-
#define _PWM_RES_MAX 3000
23-
// pwm frequency
24-
#define _PWM_FREQUENCY 25000 // default
25-
#define _PWM_FREQUENCY_MAX 50000 // mqx
26-
27-
// structure containing motor slot configuration
28-
// this library supports up to 4 motors
29-
typedef struct {
30-
int pinA;
31-
mcpwm_dev_t* mcpwm_num;
32-
mcpwm_unit_t mcpwm_unit;
33-
mcpwm_operator_t mcpwm_operator;
34-
mcpwm_io_signals_t mcpwm_a;
35-
mcpwm_io_signals_t mcpwm_b;
36-
mcpwm_io_signals_t mcpwm_c;
37-
} bldc_3pwm_motor_slots_t;
38-
typedef struct {
39-
int pin1A;
40-
mcpwm_dev_t* mcpwm_num;
41-
mcpwm_unit_t mcpwm_unit;
42-
mcpwm_operator_t mcpwm_operator1;
43-
mcpwm_operator_t mcpwm_operator2;
44-
mcpwm_io_signals_t mcpwm_1a;
45-
mcpwm_io_signals_t mcpwm_1b;
46-
mcpwm_io_signals_t mcpwm_2a;
47-
mcpwm_io_signals_t mcpwm_2b;
48-
} stepper_4pwm_motor_slots_t;
49-
typedef struct {
50-
int pin1pwm;
51-
mcpwm_dev_t* mcpwm_num;
52-
mcpwm_unit_t mcpwm_unit;
53-
mcpwm_operator_t mcpwm_operator;
54-
mcpwm_io_signals_t mcpwm_a;
55-
mcpwm_io_signals_t mcpwm_b;
56-
} stepper_2pwm_motor_slots_t;
57-
58-
typedef struct {
59-
int pinAH;
60-
mcpwm_dev_t* mcpwm_num;
61-
mcpwm_unit_t mcpwm_unit;
62-
mcpwm_operator_t mcpwm_operator1;
63-
mcpwm_operator_t mcpwm_operator2;
64-
mcpwm_io_signals_t mcpwm_ah;
65-
mcpwm_io_signals_t mcpwm_bh;
66-
mcpwm_io_signals_t mcpwm_ch;
67-
mcpwm_io_signals_t mcpwm_al;
68-
mcpwm_io_signals_t mcpwm_bl;
69-
mcpwm_io_signals_t mcpwm_cl;
70-
} bldc_6pwm_motor_slots_t;
71-
725
// define bldc motor slots array
736
bldc_3pwm_motor_slots_t esp32_bldc_3pwm_motor_slots[4] = {
747
{_EMPTY_SLOT, &MCPWM0, MCPWM_UNIT_0, MCPWM_OPR_A, MCPWM0A, MCPWM1A, MCPWM2A}, // 1st motor will be MCPWM0 channel A
75-
{_EMPTY_SLOT, &MCPWM0, MCPWM_UNIT_0, MCPWM_OPR_B, MCPWM0B, MCPWM1B, MCPWM2B}, // 2nd motor will be MCPWM0 channel B
76-
{_EMPTY_SLOT, &MCPWM1, MCPWM_UNIT_1, MCPWM_OPR_A, MCPWM0A, MCPWM1A, MCPWM2A}, // 3rd motor will be MCPWM1 channel A
8+
{_EMPTY_SLOT, &MCPWM1, MCPWM_UNIT_1, MCPWM_OPR_A, MCPWM0A, MCPWM1A, MCPWM2A}, // 1rd motor will be MCPWM1 channel A
9+
{_EMPTY_SLOT, &MCPWM0, MCPWM_UNIT_0, MCPWM_OPR_B, MCPWM0B, MCPWM1B, MCPWM2B}, // 3nd motor will be MCPWM0 channel B
7710
{_EMPTY_SLOT, &MCPWM1, MCPWM_UNIT_1, MCPWM_OPR_B, MCPWM0B, MCPWM1B, MCPWM2B} // 4th motor will be MCPWM1 channel B
7811
};
7912

@@ -92,8 +25,8 @@ stepper_4pwm_motor_slots_t esp32_stepper_4pwm_motor_slots[2] = {
9225
// define 2pwm stepper motor slots array
9326
stepper_2pwm_motor_slots_t esp32_stepper_2pwm_motor_slots[4] = {
9427
{_EMPTY_SLOT, &MCPWM0, MCPWM_UNIT_0, MCPWM_OPR_A, MCPWM0A, MCPWM1A}, // 1st motor will be MCPWM0 channel A
95-
{_EMPTY_SLOT, &MCPWM0, MCPWM_UNIT_0, MCPWM_OPR_B, MCPWM0B, MCPWM1B}, // 2nd motor will be MCPWM0 channel B
96-
{_EMPTY_SLOT, &MCPWM1, MCPWM_UNIT_1, MCPWM_OPR_A, MCPWM0A, MCPWM1A}, // 3rd motor will be MCPWM1 channel A
28+
{_EMPTY_SLOT, &MCPWM1, MCPWM_UNIT_1, MCPWM_OPR_A, MCPWM0A, MCPWM1A}, // 2rd motor will be MCPWM1 channel A
29+
{_EMPTY_SLOT, &MCPWM0, MCPWM_UNIT_0, MCPWM_OPR_B, MCPWM0B, MCPWM1B}, // 3nd motor will be MCPWM0 channel B
9730
{_EMPTY_SLOT, &MCPWM1, MCPWM_UNIT_1, MCPWM_OPR_B, MCPWM0B, MCPWM1B} // 4th motor will be MCPWM1 channel B
9831
};
9932

@@ -195,7 +128,7 @@ void _configure2PWM(long pwm_frequency,const int pinA, const int pinB) {
195128
// disable all the slots with the same MCPWM
196129
// disable 3pwm bldc motor which would go in the same slot
197130
esp32_bldc_3pwm_motor_slots[slot_num].pinA = _TAKEN_SLOT;
198-
if( slot_num < 2 ){
131+
if( slot_num == 0 || slot_num == 2 ){
199132
// slot 0 of the 4pwm stepper
200133
esp32_stepper_4pwm_motor_slots[0].pin1A = _TAKEN_SLOT;
201134
// slot 0 of the 6pwm bldc
@@ -239,7 +172,7 @@ void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int
239172
// disable all the slots with the same MCPWM
240173
// disable 2pwm steppr motor which would go in the same slot
241174
esp32_stepper_2pwm_motor_slots[slot_num].pin1pwm = _TAKEN_SLOT;
242-
if( slot_num < 2 ){
175+
if( slot_num == 0 || slot_num == 2 ){
243176
// slot 0 of the 4pwm stepper
244177
esp32_stepper_4pwm_motor_slots[0].pin1A = _TAKEN_SLOT;
245178
// slot 0 of the 6pwm bldc
@@ -280,20 +213,20 @@ void _configure4PWM(long pwm_frequency,const int pinA, const int pinB, const int
280213
}
281214
// disable all the slots with the same MCPWM
282215
if( slot_num == 0 ){
283-
// slots 0 and 1 of the 3pwm bldc
216+
// slots 0 and 2 of the 3pwm bldc
284217
esp32_bldc_3pwm_motor_slots[0].pinA = _TAKEN_SLOT;
285-
esp32_bldc_3pwm_motor_slots[1].pinA = _TAKEN_SLOT;
286-
// slots 0 and 1 of the 2pwm stepper taken
218+
esp32_bldc_3pwm_motor_slots[2].pinA = _TAKEN_SLOT;
219+
// slots 0 and 2 of the 2pwm stepper taken
287220
esp32_stepper_2pwm_motor_slots[0].pin1pwm = _TAKEN_SLOT;
288-
esp32_stepper_2pwm_motor_slots[1].pin1pwm = _TAKEN_SLOT;
221+
esp32_stepper_2pwm_motor_slots[2].pin1pwm = _TAKEN_SLOT;
289222
// slot 0 of the 6pwm bldc
290223
esp32_bldc_6pwm_motor_slots[0].pinAH = _TAKEN_SLOT;
291224
}else{
292-
// slots 2 and 3 of the 3pwm bldc
293-
esp32_bldc_3pwm_motor_slots[2].pinA = _TAKEN_SLOT;
225+
// slots 1 and 3 of the 3pwm bldc
226+
esp32_bldc_3pwm_motor_slots[1].pinA = _TAKEN_SLOT;
294227
esp32_bldc_3pwm_motor_slots[3].pinA = _TAKEN_SLOT;
295-
// slots 2 and 3 of the 2pwm stepper taken
296-
esp32_stepper_2pwm_motor_slots[2].pin1pwm = _TAKEN_SLOT;
228+
// slots 1 and 3 of the 2pwm stepper taken
229+
esp32_stepper_2pwm_motor_slots[1].pin1pwm = _TAKEN_SLOT;
297230
esp32_stepper_2pwm_motor_slots[3].pin1pwm = _TAKEN_SLOT;
298231
// slot 1 of the 6pwm bldc
299232
esp32_bldc_6pwm_motor_slots[1].pinAH = _TAKEN_SLOT;
@@ -386,15 +319,21 @@ int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const
386319

387320
// disable all the slots with the same MCPWM
388321
if( slot_num == 0 ){
389-
// slots 0 and 1 of the 3pwm bldc
322+
// slots 0 and 2 of the 3pwm bldc
390323
esp32_bldc_3pwm_motor_slots[0].pinA = _TAKEN_SLOT;
391-
esp32_bldc_3pwm_motor_slots[1].pinA = _TAKEN_SLOT;
324+
esp32_bldc_3pwm_motor_slots[2].pinA = _TAKEN_SLOT;
325+
// slots 0 and 2 of the 2pwm stepper taken
326+
esp32_stepper_2pwm_motor_slots[0].pin1pwm = _TAKEN_SLOT;
327+
esp32_stepper_2pwm_motor_slots[2].pin1pwm = _TAKEN_SLOT;
392328
// slot 0 of the 6pwm bldc
393329
esp32_stepper_4pwm_motor_slots[0].pin1A = _TAKEN_SLOT;
394330
}else{
395-
// slots 2 and 3 of the 3pwm bldc
396-
esp32_bldc_3pwm_motor_slots[2].pinA = _TAKEN_SLOT;
331+
// slots 1 and 3 of the 3pwm bldc
332+
esp32_bldc_3pwm_motor_slots[1].pinA = _TAKEN_SLOT;
397333
esp32_bldc_3pwm_motor_slots[3].pinA = _TAKEN_SLOT;
334+
// slots 1 and 3 of the 2pwm stepper taken
335+
esp32_stepper_2pwm_motor_slots[1].pin1pwm = _TAKEN_SLOT;
336+
esp32_stepper_2pwm_motor_slots[3].pin1pwm = _TAKEN_SLOT;
398337
// slot 1 of the 6pwm bldc
399338
esp32_stepper_4pwm_motor_slots[1].pin1A = _TAKEN_SLOT;
400339
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
#ifndef ESP32_DRIVER_H
3+
#define ESP32_DRIVER_H
4+
5+
#include "./../../drivers/hardware_api.h"
6+
7+
#if defined(ESP_H)
8+
9+
#include "driver/mcpwm.h"
10+
#include "soc/mcpwm_reg.h"
11+
#include "soc/mcpwm_struct.h"
12+
13+
// empty motor slot
14+
#define _EMPTY_SLOT -20
15+
#define _TAKEN_SLOT -21
16+
17+
// ABI bus frequency - would be better to take it from somewhere
18+
// but I did nto find a good exposed variable
19+
#define _MCPWM_FREQ 160e6f
20+
21+
// preferred pwm resolution default
22+
#define _PWM_RES_DEF 2048
23+
// min resolution
24+
#define _PWM_RES_MIN 1500
25+
// max resolution
26+
#define _PWM_RES_MAX 3000
27+
// pwm frequency
28+
#define _PWM_FREQUENCY 25000 // default
29+
#define _PWM_FREQUENCY_MAX 50000 // mqx
30+
31+
// structure containing motor slot configuration
32+
// this library supports up to 4 motors
33+
typedef struct {
34+
int pinA;
35+
mcpwm_dev_t* mcpwm_num;
36+
mcpwm_unit_t mcpwm_unit;
37+
mcpwm_operator_t mcpwm_operator;
38+
mcpwm_io_signals_t mcpwm_a;
39+
mcpwm_io_signals_t mcpwm_b;
40+
mcpwm_io_signals_t mcpwm_c;
41+
} bldc_3pwm_motor_slots_t;
42+
typedef struct {
43+
int pin1A;
44+
mcpwm_dev_t* mcpwm_num;
45+
mcpwm_unit_t mcpwm_unit;
46+
mcpwm_operator_t mcpwm_operator1;
47+
mcpwm_operator_t mcpwm_operator2;
48+
mcpwm_io_signals_t mcpwm_1a;
49+
mcpwm_io_signals_t mcpwm_1b;
50+
mcpwm_io_signals_t mcpwm_2a;
51+
mcpwm_io_signals_t mcpwm_2b;
52+
} stepper_4pwm_motor_slots_t;
53+
typedef struct {
54+
int pin1pwm;
55+
mcpwm_dev_t* mcpwm_num;
56+
mcpwm_unit_t mcpwm_unit;
57+
mcpwm_operator_t mcpwm_operator;
58+
mcpwm_io_signals_t mcpwm_a;
59+
mcpwm_io_signals_t mcpwm_b;
60+
} stepper_2pwm_motor_slots_t;
61+
62+
typedef struct {
63+
int pinAH;
64+
mcpwm_dev_t* mcpwm_num;
65+
mcpwm_unit_t mcpwm_unit;
66+
mcpwm_operator_t mcpwm_operator1;
67+
mcpwm_operator_t mcpwm_operator2;
68+
mcpwm_io_signals_t mcpwm_ah;
69+
mcpwm_io_signals_t mcpwm_bh;
70+
mcpwm_io_signals_t mcpwm_ch;
71+
mcpwm_io_signals_t mcpwm_al;
72+
mcpwm_io_signals_t mcpwm_bl;
73+
mcpwm_io_signals_t mcpwm_cl;
74+
} bldc_6pwm_motor_slots_t;
75+
76+
77+
#endif
78+
#endif

src/current_sense/hardware_specific/generic_mcu.cpp renamed to src/hardware_specific/generic_current_sense.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../hardware_api.h"
1+
#include "../current_sense/hardware_api.h"
22

33
// function reading an ADC value and returning the read voltage
44
__attribute__((weak)) float _readADCVoltageInline(const int pinA){

src/drivers/hardware_specific/generic_mcu.cpp renamed to src/hardware_specific/generic_driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "../hardware_api.h"
1+
#include "../drivers/hardware_api.h"
22

3-
// if the mcu doen't have defiend analogWrite
3+
// if the mcu doesn't have defiend analogWrite
44
#if defined(ESP_H)
55
__attribute__((weak)) void analogWrite(uint8_t pin, int value){ };
66
#endif

src/drivers/hardware_specific/rp2040_mcu.cpp renamed to src/hardware_specific/rp2040/rp2040_driver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/**
33
* Support for the RP2040 MCU, as found on the Raspberry Pi Pico.
44
*/
5+
#include "./../../drivers/hardware_api.h"
6+
57
#if defined(TARGET_RP2040)
68

79
#define SIMPLEFOC_DEBUG_RP2040

src/drivers/hardware_specific/samd21_mcu.cpp renamed to src/hardware_specific/samd/samd21_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33

4-
#include "./samd_mcu.h"
4+
#include "./samd_driver.h"
55

66

77
#ifdef _SAMD21_

src/drivers/hardware_specific/samd51_mcu.cpp renamed to src/hardware_specific/samd/samd51_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
#include "./samd_mcu.h"
3+
#include "./samd_driver.h"
44

55

66
#if defined(_SAMD51_)||defined(_SAME51_)

src/current_sense/hardware_specific/samd_mcu.cpp renamed to src/hardware_specific/samd/samd_current_sense.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../hardware_api.h"
1+
#include "../../current_sense/hardware_api.h"
22

33
#if defined(_SAMD21_)||defined(_SAMD51_)||defined(_SAME51_)
44

src/drivers/hardware_specific/samd_mcu.cpp renamed to src/hardware_specific/samd/samd_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33

4-
#include "./samd_mcu.h"
4+
#include "./samd_driver.h"
55

66
#if defined(_SAMD21_)||defined(_SAMD51_)||defined(_SAME51_)
77

src/drivers/hardware_specific/samd_mcu.h renamed to src/hardware_specific/samd/samd_driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#define SIMPLEFOC_SAMD_DEBUG_SERIAL Serial
1010
#endif
1111

12-
#include "../hardware_api.h"
12+
#include "./../../drivers/hardware_api.h"
1313

1414

1515
#if defined(__SAME51J19A__) || defined(__ATSAME51J19A__)

src/current_sense/hardware_specific/stm32_mcu.cpp renamed to src/hardware_specific/stm32/stm32_current_sense.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
#include "../hardware_api.h"
2+
#include "../../current_sense/hardware_api.h"
33

44
#if defined(_STM32_DEF_)
55

src/drivers/hardware_specific/stm32_mcu.cpp renamed to src/hardware_specific/stm32/stm32_driver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
#include "../hardware_api.h"
1+
#include "./../../drivers/hardware_api.h"
32

43
#if defined(_STM32_DEF_)
54
// default pwm parameters

src/current_sense/hardware_specific/teensy_mcu.cpp renamed to src/hardware_specific/teensy/teensy_current_sense.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../hardware_api.h"
1+
#include "../../current_sense/hardware_api.h"
22

33
#if defined(__arm__) && defined(CORE_TEENSY)
44

src/drivers/hardware_specific/teensy_mcu.cpp renamed to src/hardware_specific/teensy/teensy_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../hardware_api.h"
1+
#include "./../../drivers/hardware_api.h"
22

33
#if defined(__arm__) && defined(CORE_TEENSY)
44

0 commit comments

Comments
 (0)