Skip to content

Commit

Permalink
updating Arduino examples for STM32
Browse files Browse the repository at this point in the history
and editing file headers
  • Loading branch information
erik1392 committed Dec 13, 2023
1 parent 71ab428 commit 02663d2
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions examples/MagnetoShield/MagnetoShield_LQ/MagnetoShield_LQ.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 <MagnetoShield.h>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -92,14 +94,16 @@ 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
MagnetoShield.begin(); // Define hardware pins
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.

}
Expand Down
13 changes: 8 additions & 5 deletions examples/MagnetoShield/MagnetoShield_PID/MagnetoShield_PID.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 <MagnetoShield.h> // Include header for hardware API
#include <Sampling.h> // Include sampling
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/AeroShield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/FloatShield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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áš
*/

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/LinkShield.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 8 additions & 4 deletions src/MagnetoShield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/MagnetoShield.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/Sampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/SamplingServo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/sampling/SamplingCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
11 changes: 8 additions & 3 deletions src/sampling/SamplingCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 02663d2

Please sign in to comment.