From 51be011edc555b91a6c955179c6a51ae04d2846a Mon Sep 17 00:00:00 2001 From: erik1392 Date: Tue, 12 Dec 2023 23:36:50 +0100 Subject: [PATCH] working sampling on STM32 tested on Nucelo STM32F302R8 but still need some work and testing --- src/AeroShield.cpp | 4 ++-- src/AeroShield.h | 2 +- src/AutomationShield.h | 2 +- src/MagnetoShield.cpp | 5 ++++- src/MagnetoShield.h | 4 ++-- src/Sampling.h | 3 +++ src/sampling/SamplingCore.cpp | 19 +++++++++++++++++++ src/sampling/SamplingCore.h | 11 ++++++++++- 8 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/AeroShield.cpp b/src/AeroShield.cpp index 2c88e054..42ba3a36 100644 --- a/src/AeroShield.cpp +++ b/src/AeroShield.cpp @@ -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 diff --git a/src/AeroShield.h b/src/AeroShield.h index 5f6970e2..ec1f470c 100644 --- a/src/AeroShield.h +++ b/src/AeroShield.h @@ -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 diff --git a/src/AutomationShield.h b/src/AutomationShield.h index e8ee9383..214bf796 100644 --- a/src/AutomationShield.h +++ b/src/AutomationShield.h @@ -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 diff --git a/src/MagnetoShield.cpp b/src/MagnetoShield.cpp index 2828a673..38678a24 100644 --- a/src/MagnetoShield.cpp +++ b/src/MagnetoShield.cpp @@ -47,6 +47,9 @@ void MagnetoShieldClass::begin(){ #endif analogReadResolution(12); Wire.begin(); + #elif ARDUINO_ARCH_STM32 + analogReadResolution(12); + Wire.begin(); #endif } @@ -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 diff --git a/src/MagnetoShield.h b/src/MagnetoShield.h index d9ac3264..2fad3a81 100644 --- a/src/MagnetoShield.h +++ b/src/MagnetoShield.h @@ -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 @@ -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 diff --git a/src/Sampling.h b/src/Sampling.h index ea1100f0..411ecd12 100644 --- a/src/Sampling.h +++ b/src/Sampling.h @@ -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 diff --git a/src/sampling/SamplingCore.cpp b/src/sampling/SamplingCore.cpp index ff3d75ea..0f943689 100644 --- a/src/sampling/SamplingCore.cpp +++ b/src/sampling/SamplingCore.cpp @@ -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." @@ -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." @@ -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 () { @@ -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 @@ -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 @@ -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 () { diff --git a/src/sampling/SamplingCore.h b/src/sampling/SamplingCore.h index af2c092d..7bc286c4 100644 --- a/src/sampling/SamplingCore.h +++ b/src/sampling/SamplingCore.h @@ -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 @@ -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."