Skip to content

Commit

Permalink
working sampling on STM32
Browse files Browse the repository at this point in the history
tested on Nucelo STM32F302R8 but still need some work and testing
  • Loading branch information
erik1392 committed Dec 12, 2023
1 parent a561961 commit 51be011
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/AeroShield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include "AeroShield.h" // Include header file

// Initializes hardware pins
float AeroClass::begin(void){ // Board initialisation
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_RENESAS_UNO) // For AVR, SAMD, Renesas architecture boards
void AeroClass::begin(void){ // Board initialisation
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_RENESAS_UNO) || defined(ARDUINO_ARCH_STM32) // For AVR, SAMD, Renesas architecture boards
Wire.begin(); // Use Wire object
as5600.setWirePtr(&Wire);
#elif ARDUINO_ARCH_SAM // For SAM architecture boards
Expand Down
2 changes: 1 addition & 1 deletion src/AeroShield.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AeroClass{ // Class for the AeroShield de
public:
#include "getGainLQ.inl"
#include "getKalmanEstimate.inl"
float begin(void); // Board initialisation - initialisation of pin modes and variables
void begin(void); // Board initialisation - initialisation of pin modes and variables
void actuatorWrite(float PotPercent); // Write actuator - function takes input 0.0-100.0% and sets motor speed accordingly
void actuatorWriteVolt(float);
bool calibrate(void); // sensor calibration
Expand Down
2 changes: 1 addition & 1 deletion src/AutomationShield.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#ifdef ARDUINO_ARCH_AVR // Chip uses 10-bit ADC

#define ADCREF 1023.0 // 10-bit resolution for AD converter
#elif ARDUINO_ARCH_SAMD || ARDUINO_ARCH_SAM || ARDUINO_ARCH_RENESAS_UNO // Chip uses 12-bit ADC
#elif ARDUINO_ARCH_SAMD || ARDUINO_ARCH_SAM || ARDUINO_ARCH_RENESAS_UNO || ARDUINO_ARCH_STM32 // Chip uses 12-bit ADC
#define ADCREF 4095.0 // 12-bit resolution for AD converter
#endif

Expand Down
5 changes: 4 additions & 1 deletion src/MagnetoShield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ void MagnetoShieldClass::begin(){
#endif
analogReadResolution(12);
Wire.begin();
#elif ARDUINO_ARCH_STM32
analogReadResolution(12);
Wire.begin();
#endif
}

Expand All @@ -68,7 +71,7 @@ void MagnetoShieldClass::dacWrite(uint8_t DAClevel){
// Write DAC levels (12-bit) to the MCP4725 chip
#elif SHIELDRELEASE == 3 || SHIELDRELEASE == 4
void MagnetoShieldClass::dacWrite(uint16_t DAClevel){ // 16 bits in the form (0,0,0,0,D11,D10,D9,D8,D7,D6,D5,D4,D3,D2,D1,D0)
#if ARDUINO_ARCH_AVR || ARDUINO_ARCH_SAMD || ARDUINO_ARCH_RENESAS_UNO
#if ARDUINO_ARCH_AVR || ARDUINO_ARCH_SAMD || ARDUINO_ARCH_RENESAS_UNO || ARDUINO_ARCH_STM32
Wire.beginTransmission(MCP4725); //addressing
Wire.write(0x40); // write dac(DAC and EEPROM is 0x60)
uint8_t firstbyte=(DAClevel>>4); //(0,0,0,0,0,0,0,0,D11,D10,D9,D8,D7,D6,D5,D4) of which only the 8 LSB's survive
Expand Down
4 changes: 2 additions & 2 deletions src/MagnetoShield.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#ifdef ARDUINO_ARCH_AVR
#define VIN 11.6 // [V] Input voltage from source
#elif ARDUINO_ARCH_SAMD || ARDUINO_ARCH_RENESAS_UNO
#elif ARDUINO_ARCH_SAMD || ARDUINO_ARCH_RENESAS_UNO || ARDUINO_ARCH_STM32
#define VIN 12.5 // [V] Input voltage from source
#elif ARDUINO_ARCH_SAM
#define VIN 11.6 // [V] Input voltage from source
Expand Down Expand Up @@ -99,7 +99,7 @@
#ifdef ARDUINO_ARCH_AVR
#define HALL_LSAT 35 // [10-bit ADC] Lower saturation of the Hall sensor
#define HALL_HSAT 621 // [10-bit ADC] Higher (upper) saturation of the Hall sensor
#elif ARDUINO_ARCH_SAMD || ARDUINO_ARCH_SAM || ARDUINO_ARCH_RENESAS_UNO
#elif ARDUINO_ARCH_SAMD || ARDUINO_ARCH_SAM || ARDUINO_ARCH_RENESAS_UNO || ARDUINO_ARCH_STM32
#define HALL_LSAT 112 // [12-bit ADC] Lower saturation of the Hall sensor
#define HALL_HSAT 2526 // [12-bit ADC] Higher (upper) saturation of the Hall sensor
#endif
Expand Down
3 changes: 3 additions & 0 deletions src/Sampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ void TC5_Handler(void){
#elif ARDUINO_ARCH_RENESAS_UNO
#include "sampling/SamplingUNO_R4_ISR.h"

#elif ARDUINO_ARCH_STM32
// nothing to do...

#else
#error "Architecture not supported."
#endif
Expand Down
19 changes: 19 additions & 0 deletions src/sampling/SamplingCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ void SamplingNoServo::SamplingClass::period(unsigned long microseconds) {
Serial.println("Sampling error: timer did not start");
#endif
}
#elif ARDUINO_ARCH_STM32
sampling_timer->setOverflow((uint32_t)microseconds, MICROSEC_FORMAT);

#else
#error "Architecture not supported."
Expand Down Expand Up @@ -327,6 +329,8 @@ bool SamplingNoServo::SamplingClass::setSamplingPeriod(unsigned long microsecond
else {
return false;
}
#elif ARDUINO_ARCH_STM32
// just nothing to do....

#else
#error "Architecture not supported."
Expand All @@ -349,6 +353,10 @@ unsigned long int SamplingNoServo::SamplingClass::getSamplingMicroseconds() {

void SamplingNoServo::SamplingClass::interrupt(p_to_void_func interruptCallback) {
this->interruptCallback = interruptCallback;
#if defined(ARDUINO_ARCH_STM32)
sampling_timer->attachInterrupt(interruptCallback);
sampling_timer->resume();
#endif
}

p_to_void_func SamplingNoServo::SamplingClass::getInterruptCallback () {
Expand Down Expand Up @@ -476,6 +484,10 @@ void SamplingServo::SamplingClass::period(unsigned long microseconds) {
Serial.println("Sampling error: timer did not start");
#endif
}

#elif ARDUINO_ARCH_STM32
sampling_timer->setOverflow((uint32_t)microseconds, MICROSEC_FORMAT);

#else
#error "Architecture not supported."
#endif
Expand Down Expand Up @@ -630,6 +642,9 @@ bool SamplingServo::SamplingClass::setSamplingPeriod(unsigned long microseconds)
return false;
}

#elif ARDUINO_ARCH_STM32
// nothing to do...

#else
#error "Architecture not supported."
#endif
Expand All @@ -655,6 +670,10 @@ unsigned long int SamplingServo::SamplingClass::getSamplingMicroseconds() {

void SamplingServo::SamplingClass::interrupt(p_to_void_func interruptCallback) {
this->interruptCallback = interruptCallback;
#if defined(ARDUINO_ARCH_STM32)
sampling_timer->attachInterrupt(interruptCallback);
sampling_timer->resume();
#endif
}

p_to_void_func SamplingServo::SamplingClass::getInterruptCallback () {
Expand Down
11 changes: 10 additions & 1 deletion src/sampling/SamplingCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ namespace SamplingNoServo {
uint32_t period_counts = 0;
timer_source_div_t prescaler = TIMER_SOURCE_DIV_1;

#elif ARDUINO_ARCH_STM32
TIM_TypeDef *Instance = TIM2;
HardwareTimer *sampling_timer = new HardwareTimer(Instance);
const unsigned char cpuFrequency = 72;

#else
#error "Architecture not supported."
#endif
Expand Down Expand Up @@ -184,7 +189,11 @@ namespace SamplingServo {
#define COMPARE_10MS 30000 // Compare @ 48 MHz, prescaler 16, for 10 ms
uint32_t period_counts = 0;
timer_source_div_t prescaler = TIMER_SOURCE_DIV_1;


#elif ARDUINO_ARCH_STM32
TIM_TypeDef *Instance = TIM2;
HardwareTimer *sampling_timer = new HardwareTimer(Instance);
const unsigned char cpuFrequency = 72;

#else
#error "Architecture not supported."
Expand Down

0 comments on commit 51be011

Please sign in to comment.