From 02663d22688833f7d6f3feced1c02734aec9234f Mon Sep 17 00:00:00 2001 From: erik1392 Date: Wed, 13 Dec 2023 16:13:30 +0100 Subject: [PATCH] updating Arduino examples for STM32 and editing file headers --- .../MagnetoShield_Identification.ino | 4 +++- .../MagnetoShield_LQ/MagnetoShield_LQ.ino | 10 +++++++--- .../MagnetoShield_PID/MagnetoShield_PID.ino | 13 ++++++++----- .../MagnetoShield_PolePlacement.ino | 5 +++++ src/AeroShield.cpp | 2 +- src/FloatShield.cpp | 6 +++--- src/LinkShield.h | 2 ++ src/MagnetoShield.cpp | 12 ++++++++---- src/MagnetoShield.h | 2 +- src/Sampling.h | 11 ++++++++--- src/SamplingServo.h | 9 ++++++++- src/sampling/SamplingCore.cpp | 11 ++++++++--- src/sampling/SamplingCore.h | 11 ++++++++--- 13 files changed, 70 insertions(+), 28 deletions(-) diff --git a/examples/MagnetoShield/MagnetoShield_Identification/MagnetoShield_Identification.ino b/examples/MagnetoShield/MagnetoShield_Identification/MagnetoShield_Identification.ino index d8d91332..508f8c24 100644 --- a/examples/MagnetoShield/MagnetoShield_Identification/MagnetoShield_Identification.ino +++ b/examples/MagnetoShield/MagnetoShield_Identification/MagnetoShield_Identification.ino @@ -56,7 +56,7 @@ int wP=(int)wPP*100; // For (pseudo)-random generator unsigned long Ts = 5000; // Sampling in microseconds #elif ARDUINO_ARCH_SAM unsigned long Ts = 3250; // Sampling in microseconds -#elif ARDUINO_ARCH_RENESAS_UNO +#elif ARDUINO_ARCH_RENESAS_UNO || ARDUINO_ARCH_STM32 unsigned long Ts = 5000; // Sampling in microseconds #endif @@ -68,6 +68,8 @@ void setup() { Serial.begin(250000); // Initialize serial, maximum for Due (baud mismatch issues) #elif ARDUINO_ARCH_RENESAS_UNO Serial.begin(115200); // Initialize serial, maximum for UNO R4 (serial communication limitations) +#elif ARDUINO_ARCH_STM32 + Serial.begin(2000000); // Initialize serial, maximum for STM32 given by hardware #endif // Initialize and calibrate board diff --git a/examples/MagnetoShield/MagnetoShield_LQ/MagnetoShield_LQ.ino b/examples/MagnetoShield/MagnetoShield_LQ/MagnetoShield_LQ.ino index 2d3cc748..214898bb 100644 --- a/examples/MagnetoShield/MagnetoShield_LQ/MagnetoShield_LQ.ino +++ b/examples/MagnetoShield/MagnetoShield_LQ/MagnetoShield_LQ.ino @@ -22,8 +22,7 @@ // // Created by: Gergely Takacs and Gabor penzinger. // Created on: 23.9.2020 -// Last updated by: Gergely Takacs -// Last update on: 24.9.2020 +// Last update: 13.12.2023 by Erik Mikuláš #include @@ -64,6 +63,9 @@ int i = 0; // Section counter #elif ARDUINO_ARCH_RENESAS_UNO float Ts = 5; // Sampling in microseconds, lower limit 1.3 ms int T = 1000; // Experiment section length (steps) +#elif ARDUINO_ARCH_STM32 + float Ts = 5; // Sampling in microseconds, lower limit 1.3 ms + int T = 1000; // Experiment section length (steps) #endif #if USE_KALMAN_FILTER && ARDUINO_ARCH_SAM @@ -92,6 +94,8 @@ void setup() { Serial.begin(250000); // Initialize serial, maximum for Due (baud mismatch issues) #elif ARDUINO_ARCH_RENESAS_UNO Serial.begin(115200); // Initialize serial, maximum for UNO R4 (serial communication limitations) +#elif ARDUINO_ARCH_STM32 + Serial.begin(2000000); // Initialize serial, maximum for STM32 given by hardware #endif // Initialize and calibrate board @@ -99,7 +103,7 @@ void setup() { MagnetoShield.calibration(); // Calibrates shield // Initialize sampling function - Sampling.period(Ts*1000); // Sampling init. + Sampling.period((unsigned long)(Ts*1000)); // Sampling init. Sampling.interrupt(stepEnable); // Interrupt fcn. } diff --git a/examples/MagnetoShield/MagnetoShield_PID/MagnetoShield_PID.ino b/examples/MagnetoShield/MagnetoShield_PID/MagnetoShield_PID.ino index 38308081..3506fc72 100644 --- a/examples/MagnetoShield/MagnetoShield_PID/MagnetoShield_PID.ino +++ b/examples/MagnetoShield/MagnetoShield_PID/MagnetoShield_PID.ino @@ -19,7 +19,7 @@ Attribution-NonCommercial 4.0 International License. Created by Gergely Takács. - Last update: 13.10.2020 + Last update: 13.12.2023 by Erik Mikuláš */ #include // Include header for hardware API #include // Include sampling @@ -59,15 +59,18 @@ float u = 0.0; // [V] Input unsigned long Ts = 5000; // Sampling in microseconds, lower limit 1.3 ms int T = 1000; // Experiment section length (steps) #elif ARDUINO_ARCH_RENESAS_UNO - float Ts = 5; // Sampling in microseconds, lower limit 1.3 ms - int T = 1000; // Experiment section length (steps) + unsigned long Ts = 5000; // Sampling in microseconds, lower limit 1.3 ms + int T = 1000; // Experiment section length (steps) +#elif ARDUINO_ARCH_STM32 + unsigned long Ts = 5000; // Sampling in microseconds, lower limit 1.3 ms + int T = 1000; // Experiment section length (steps) #endif void setup() { -#if ARDUINO_ARCH_AVR || ARDUINO_ARCH_SAMD +#if ARDUINO_ARCH_AVR || ARDUINO_ARCH_SAMD || ARDUINO_ARCH_STM32 Serial.begin(2000000); // Initialize serial, maximum for AVR given by hardware -#elif ARDUINO_ARCH_SAM +#elif ARDUINO_ARCH_SAM Serial.begin(250000); // Initialize serial, maximum for Due (baud mismatch issues) #elif ARDUINO_ARCH_RENESAS_UNO Serial.begin(115200); // Initialize serial, maximum for UNO R4 (serial communication limitations) diff --git a/examples/MagnetoShield/MagnetoShield_PolePlacement/MagnetoShield_PolePlacement.ino b/examples/MagnetoShield/MagnetoShield_PolePlacement/MagnetoShield_PolePlacement.ino index 10d5d84c..02080ee3 100644 --- a/examples/MagnetoShield/MagnetoShield_PolePlacement/MagnetoShield_PolePlacement.ino +++ b/examples/MagnetoShield/MagnetoShield_PolePlacement/MagnetoShield_PolePlacement.ino @@ -63,6 +63,9 @@ float Ts = 5; // Sampling in microseconds #elif ARDUINO_ARCH_RENESAS_UNO float Ts = 5; // Sampling in microseconds, lower limit 1.3 ms int T = 1000; // Experiment section length (steps) +#elif ARDUINO_ARCH_STM32 + float Ts = 5; // Sampling in microseconds, lower limit 1.3 ms + int T = 1000; // Experiment section length (steps) #endif #if USE_KALMAN_FILTER && ARDUINO_ARCH_SAM @@ -91,6 +94,8 @@ void setup() { Serial.begin(250000); // Initialize serial, maximum for Due (baud mismatch issues) #elif ARDUINO_ARCH_RENESAS_UNO Serial.begin(115200); // Initialize serial, maximum for UNO R4 (serial comunication limitations) +#elif ARDUINO_ARCH_STM32 + Serial.begin(2000000); // Initialize serial, maximum for STM32 given by hardware #endif // Initialize and calibrate board diff --git a/src/AeroShield.cpp b/src/AeroShield.cpp index 42ba3a36..eb2c498e 100644 --- a/src/AeroShield.cpp +++ b/src/AeroShield.cpp @@ -12,7 +12,7 @@ Attribution-NonCommercial 4.0 International License. Created by Peter Tibenský, Erik Mikuláš and Ján Boldocký - Last update: 03.10.2023 + Last update: 13.12.2023 by Erik Mikuláš */ #include "AeroShield.h" // Include header file diff --git a/src/FloatShield.cpp b/src/FloatShield.cpp index 5cf88f83..8ea2b027 100644 --- a/src/FloatShield.cpp +++ b/src/FloatShield.cpp @@ -12,7 +12,7 @@ Attribution-NonCommercial 4.0 International License. Created by Gergely Takács, Peter Chmurčiak and Erik Mikuláš. - Last update: 6.21.2021. + Last update: 13.12.2023 by Erik Mikuláš */ @@ -39,7 +39,7 @@ void FloatClass::begin(void) { // Board ini #elif ARDUINO_ARCH_SAM // For SAM architecture boards analogReadResolution(12); Wire1.begin(); // Use Wire1 object -#elif ARDUINO_ARCH_SAMD || ARDUINO_ARCH_RENESAS_UNO // For SAMD and RA4M1 architecture boards +#elif ARDUINO_ARCH_SAMD || ARDUINO_ARCH_RENESAS_UNO || ARDUINO_ARCH_STM32 // For SAMD, RA4M1 and STM32 architecture boards analogReadResolution(12); Wire.begin(); // Use Wire object #endif @@ -94,7 +94,7 @@ void FloatClass::calibrate(void) { // Board calibration #if SHIELDRELEASE == 4 void FloatClass::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 (defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_RENESAS_UNO) || defined(ARDUINO_ARCH_SAMD)) + #if (defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_RENESAS_UNO) || defined(ARDUINO_ARCH_SAMD)) || defined(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/LinkShield.h b/src/LinkShield.h index 6c71539c..0f7903fd 100644 --- a/src/LinkShield.h +++ b/src/LinkShield.h @@ -71,6 +71,8 @@ void LinkClass::begin() { #elif ARDUINO_ARCH_RENESAS_UNO Wire.begin(); // Starts the "Wire" library for I2C analogReference(AR_EXTERNAL); // Set reference voltage + #elif ARDUINO_ARCH_STM32 + Wire.begin(); // Starts the "Wire" library for I2C #endif LinkShield.ADXL_POWER_CTL(); diff --git a/src/MagnetoShield.cpp b/src/MagnetoShield.cpp index 38678a24..baed9a90 100644 --- a/src/MagnetoShield.cpp +++ b/src/MagnetoShield.cpp @@ -11,7 +11,7 @@ details. This code is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Created by Gergely Takács and Jakub Mihalík. - Last update: 17.09.2020. + Last update: 13.12.2023 by Erik Mikuláš */ #include "MagnetoShield.h" @@ -48,15 +48,19 @@ void MagnetoShieldClass::begin(){ analogReadResolution(12); Wire.begin(); #elif ARDUINO_ARCH_STM32 - analogReadResolution(12); - Wire.begin(); + #if SHIELDRELEASE == 1 + #error "MagnetoShield R1 is not compatible with 3.3V CMOS logic" + #else + analogReadResolution(12); + Wire.begin(); + #endif #endif } // Write DAC levels (8-bit) to the PCF8591 chip #if SHIELDRELEASE == 1 || SHIELDRELEASE == 2 void MagnetoShieldClass::dacWrite(uint8_t DAClevel){ - #ifdef ARDUINO_ARCH_AVR || ARDUINO_ARCH_SAMD || ARDUINO_ARCH_RENESAS_UNO + #ifdef ARDUINO_ARCH_AVR || ARDUINO_ARCH_SAMD || ARDUINO_ARCH_RENESAS_UNO || ARDUINO_ARCH_STM32 Wire.beginTransmission(PCF8591); // I2C addressing, transmission begin Wire.write(0x40); // Use DAC (0100 0000) Wire.write(DAClevel); // 8-bit value of DAC output diff --git a/src/MagnetoShield.h b/src/MagnetoShield.h index 2fad3a81..a6057b95 100644 --- a/src/MagnetoShield.h +++ b/src/MagnetoShield.h @@ -11,7 +11,7 @@ details. This code is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Created by Gergely Takács and Jakub Mihalík. - Last update: 21.08.2020. + Last update: 13.12.2023 by Erik Mikuláš */ #ifndef MAGNETOSHIELD_H // Include guard diff --git a/src/Sampling.h b/src/Sampling.h index 411ecd12..bb88561a 100644 --- a/src/Sampling.h +++ b/src/Sampling.h @@ -6,8 +6,9 @@ an interrupt-driven system for deploying digital control systems on AVR, SAMD and SAM-based Arduino prototyping boards with the R3 pinout. The module should be compatible with the Arduino Uno, - Mega 2560, Zero, Due, Uno R4 and Adafruit Metro M4 Express. - There should be no timer conflicts when using the Servo library. + Mega 2560, Zero, Due, Uno R4, Adafruit Metro M4 Express and + STM32 Nucleo 64. There should be no timer conflicts + when using the Servo library. A "Sampling" object is created first from the namespace referring to the case that does not assume the use of the Servo library. @@ -25,7 +26,11 @@ SAM51 Timer: Gergely Takacs, 2019 (Metro M4) SAM3X Timer: Gergely Takacs, 2019 (Due) RA4M1 Timer: Erik Mikuláš, 2023 (UNO R4) - Last update: 3.10.2023 by Erik Mikuláš + STM32 Timer: Erik Mikuláš, 2023 (STM32 Nucleo 64*) + Last update: 13.12.2023 by Erik Mikuláš + + *Note: library was tested only with Nucleo STM32F302 board but it should + work with all STM32duino compatible boards. */ #ifndef SAMPLING_H //Include guard diff --git a/src/SamplingServo.h b/src/SamplingServo.h index 9796133c..6eab6f0d 100644 --- a/src/SamplingServo.h +++ b/src/SamplingServo.h @@ -25,7 +25,11 @@ SAM51 Timer: Gergely Takacs, 2019 (Metro M4) SAM3X Timer: Gergely Takacs, 2019 (Due) RA4M1 Timer: Erik Mikuláš, 2023 (UNO R4) - Last update: 3.10.2023 by Erik Mikuláš + STM32 Timer: Erik Mikuláš, 2023 (STM32 Nucleo 64*) + Last update: 13.12.2023 by Erik Mikuláš + + *Note: library was tested only with Nucleo STM32F302 board but it should + work with all STM32duino compatible boards. */ #ifndef SAMPLINGSERVO_H //Include guard @@ -53,6 +57,9 @@ void TC1_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 d822db8a..a839f826 100644 --- a/src/sampling/SamplingCore.cpp +++ b/src/sampling/SamplingCore.cpp @@ -5,8 +5,9 @@ an interrupt-driven system for deploying digital control systems on AVR, SAMD and SAM-based Arduino prototyping boards with the R3 pinout. The module should be compatible with the Arduino Uno, - Mega 2560, Zero, Due, Uno R4 and Adafruit Metro M4 Express. - There should be no timer conflicts when using the Servo library. + Mega 2560, Zero, Due, Uno R4, Adafruit Metro M4 Express and + STM32 Nucleo 64. There should be no timer conflicts + when using the Servo library. This file contains the forward declarations of two classes having an identical name (SamplingClass), that are separated @@ -27,7 +28,11 @@ SAM51 Timer: Gergely Takacs, 2019 (Metro M4) SAM3X Timer: Gergely Takacs, 2019 (Due) RA4M1 Timer: Erik Mikuláš, 2023 (UNO R4) - Last update: 4.10.2023 by Erik Mikuláš + STM32 Timer: Erik Mikuláš, 2023 (STM32 Nucleo 64*) + Last update: 13.12.2023 by Erik Mikuláš + + *Note: library was tested only with Nucleo STM32F302 board but it should + work with all STM32duino compatible boards. */ #include "SamplingCore.h" diff --git a/src/sampling/SamplingCore.h b/src/sampling/SamplingCore.h index 7bc286c4..a858884a 100644 --- a/src/sampling/SamplingCore.h +++ b/src/sampling/SamplingCore.h @@ -5,8 +5,9 @@ an interrupt-driven system for deploying digital control systems on AVR, SAMD and SAM-based Arduino prototyping boards with the R3 pinout. The module should be compatible with the Arduino Uno, - Mega 2560, Zero, Due, Uno R4 and Adafruit Metro M4 Express. - There should be no timer conflicts when using the Servo library. + Mega 2560, Zero, Due, Uno R4, Adafruit Metro M4 Express and + STM32 Nucleo 64. There should be no timer conflicts + when using the Servo library. This file contains the forward declarations of two classes having an identical name (SamplingClass), that are separated @@ -27,7 +28,11 @@ SAM51 Timer: Gergely Takacs, 2019 (Metro M4) SAM3X Timer: Gergely Takacs, 2019 (Due) RA4M1 Timer: Erik Mikuláš, 2023 (UNO R4) - Last update: 4.10.2021 by Erik Mikuláš + STM32 Timer: Erik Mikuláš, 2023 (STM32 Nucleo 64*) + Last update: 13.12.2023 by Erik Mikuláš + + *Note: library was tested only with Nucleo STM32F302R8 board but it should + work with all STM32duino compatible boards. */ #ifndef SAMPLINGCORE_H