Skip to content

Rp2040 #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/common/base_classes/Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ enum Direction{
* Pullup configuration structure
*/
enum Pullup{
INTERN, //!< Use internal pullups
EXTERN //!< Use external pullups
INTERNAL, //!< Use internal pullups
EXTERNAL //!< Use external pullups
};

/**
Expand All @@ -43,4 +43,4 @@ class Sensor{
long velocity_calc_timestamp=0; //!< last velocity calculation timestamp
};

#endif
#endif
4 changes: 4 additions & 0 deletions src/communication/Commander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ void Commander::run(){
received_chars[0] = 0;
rec_cnt=0;
}
if (rec_cnt>=MAX_COMMAND_LENGTH) { // prevent buffer overrun if message is too long
received_chars[0] = 0;
rec_cnt=0;
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/communication/Commander.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include "../common/lowpass_filter.h"
#include "commands.h"


#define MAX_COMMAND_LENGTH 20


// Commander verbose display to the user type
enum VerboseMode{
nothing = 0, // display nothing - good for monitoring
Expand Down Expand Up @@ -174,7 +178,7 @@ class Commander
int call_count = 0;//!< number callbacks that are subscribed

// helping variable for serial communication reading
char received_chars[20] = {0}; //!< so far received user message - waiting for newline
char received_chars[MAX_COMMAND_LENGTH] = {0}; //!< so far received user message - waiting for newline
int rec_cnt = 0; //!< number of characters receives

// serial printing functions
Expand Down
2 changes: 2 additions & 0 deletions src/drivers/hardware_specific/generic_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#elif defined(__SAME51J19A__) || defined(__ATSAME51J19A__) // samd51

#elif defined(TARGET_RP2040)

#else

// function setting the high pwm frequency to the supplied pins
Expand Down
163 changes: 163 additions & 0 deletions src/drivers/hardware_specific/rp2040_mcu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@

/**
* Support for the RP2040 MCU, as found on the Raspberry Pi Pico.
*/
#if defined(TARGET_RP2040)

#define SIMPLEFOC_DEBUG_RP2040


#ifdef SIMPLEFOC_DEBUG_RP2040

#ifndef SIMPLEFOC_RP2040_DEBUG_SERIAL
#define SIMPLEFOC_RP2040_DEBUG_SERIAL Serial
#endif

#endif

#include "Arduino.h"




// until I can figure out if this can be quickly read from some register, keep it here.
// it also serves as a marker for what slices are already used.
uint16_t wrapvalues[NUM_PWM_SLICES];


// TODO add checks which channels are already used...

void setupPWM(int pin, long pwm_frequency, bool invert = false) {
gpio_set_function(pin, GPIO_FUNC_PWM);
uint slice = pwm_gpio_to_slice_num(pin);
uint chan = pwm_gpio_to_channel(pin);
pwm_set_clkdiv_int_frac(slice, 1, 0); // fastest pwm we can get
pwm_set_phase_correct(slice, true);
uint16_t wrapvalue = ((125L * 1000L * 1000L) / pwm_frequency) / 2L - 1L;
if (wrapvalue < 999) wrapvalue = 999; // 66kHz, resolution 1000
if (wrapvalue > 3299) wrapvalue = 3299; // 20kHz, resolution 3300
#ifdef SIMPLEFOC_DEBUG_RP2040
SIMPLEFOC_RP2040_DEBUG_SERIAL.print("Configuring pin ");
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(pin);
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(" slice ");
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(slice);
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(" channel ");
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(chan);
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(" frequency ");
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(pwm_frequency);
SIMPLEFOC_RP2040_DEBUG_SERIAL.print(" top value ");
SIMPLEFOC_RP2040_DEBUG_SERIAL.println(wrapvalue);
#endif
pwm_set_wrap(slice, wrapvalue);
wrapvalues[slice] = wrapvalue;
if (invert) {
if (chan==0)
hw_write_masked(&pwm_hw->slice[slice].csr, 0x1 << PWM_CH0_CSR_A_INV_LSB, PWM_CH0_CSR_A_INV_BITS);
else
hw_write_masked(&pwm_hw->slice[slice].csr, 0x1 << PWM_CH0_CSR_B_INV_LSB, PWM_CH0_CSR_B_INV_BITS);
}
pwm_set_chan_level(slice, chan, 0); // switch off initially
}


void syncSlices() {
for (int i=0;i<NUM_PWM_SLICES;i++) {
pwm_set_enabled(i, false);
pwm_set_counter(i, 0);
}
// enable all slices
pwm_set_mask_enabled(0x7F);
}


void _configure2PWM(long pwm_frequency, const int pinA, const int pinB) {
setupPWM(pinA, pwm_frequency);
setupPWM(pinB, pwm_frequency);
syncSlices();
}



void _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const int pinC) {
setupPWM(pinA, pwm_frequency);
setupPWM(pinB, pwm_frequency);
setupPWM(pinC, pwm_frequency);
syncSlices();
}




void _configure4PWM(long pwm_frequency, const int pin1A, const int pin1B, const int pin2A, const int pin2B) {
setupPWM(pin1A, pwm_frequency);
setupPWM(pin1B, pwm_frequency);
setupPWM(pin2A, pwm_frequency);
setupPWM(pin2B, pwm_frequency);
syncSlices();
}


int _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const int pinA_l, const int pinB_h, const int pinB_l, const int pinC_h, const int pinC_l) {
// non-PIO solution...
setupPWM(pinA_h, pwm_frequency);
setupPWM(pinB_h, pwm_frequency);
setupPWM(pinC_h, pwm_frequency);
setupPWM(pinA_l, pwm_frequency, true);
setupPWM(pinB_l, pwm_frequency, true);
setupPWM(pinC_l, pwm_frequency, true);
syncSlices();
return 0;
}





void writeDutyCycle(float val, int pin) {
uint slice = pwm_gpio_to_slice_num(pin);
uint chan = pwm_gpio_to_channel(pin);
pwm_set_chan_level(slice, chan, (wrapvalues[slice]+1) * val);
}





void _writeDutyCycle2PWM(float dc_a, float dc_b, int pinA, int pinB) {
writeDutyCycle(dc_a, pinA);
writeDutyCycle(dc_b, pinB);
}



void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC) {
writeDutyCycle(dc_a, pinA);
writeDutyCycle(dc_b, pinB);
writeDutyCycle(dc_c, pinC);
}



void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, int pin1A, int pin1B, int pin2A, int pin2B) {
writeDutyCycle(dc_1a, pin1A);
writeDutyCycle(dc_1b, pin1B);
writeDutyCycle(dc_2a, pin2A);
writeDutyCycle(dc_2b, pin2B);
}

inline float swDti(float val, float dt) {
float ret = dt+val;
if (ret>1.0) ret = 1.0;
return ret;
}

void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, float dead_zone, int pinA_h, int pinA_l, int pinB_h, int pinB_l, int pinC_h, int pinC_l) {
writeDutyCycle(dc_a, pinA_h);
writeDutyCycle(swDti(dc_a, dead_zone), pinA_l);
writeDutyCycle(dc_b, pinB_h);
writeDutyCycle(swDti(dc_b,dead_zone), pinB_l);
writeDutyCycle(dc_c, pinC_h);
writeDutyCycle(swDti(dc_c,dead_zone), pinC_l);
}

#endif
4 changes: 2 additions & 2 deletions src/sensors/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Encoder::Encoder(int _encA, int _encB , float _ppr, int _index){
prev_timestamp_us = _micros();

// extern pullup as default
pullup = Pullup::EXTERN;
pullup = Pullup::EXTERNAL;
// enable quadrature encoder by default
quadrature = Quadrature::ON;
}
Expand Down Expand Up @@ -160,7 +160,7 @@ int Encoder::hasIndex(){
void Encoder::init(){

// Encoder - check if pullup needed for your encoder
if(pullup == Pullup::INTERN){
if(pullup == Pullup::INTERNAL){
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
if(hasIndex()) pinMode(index_pin,INPUT_PULLUP);
Expand Down
4 changes: 2 additions & 2 deletions src/sensors/HallSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ HallSensor::HallSensor(int _hallA, int _hallB, int _hallC, int _pp){
cpr = _pp * 6;

// extern pullup as default
pullup = Pullup::EXTERN;
pullup = Pullup::EXTERNAL;
}

// HallSensor interrupt callback functions
Expand Down Expand Up @@ -119,7 +119,7 @@ void HallSensor::init(){
electric_rotations = 0;

// HallSensor - check if pullup needed for your HallSensor
if(pullup == Pullup::INTERN){
if(pullup == Pullup::INTERNAL){
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
pinMode(pinC, INPUT_PULLUP);
Expand Down
2 changes: 1 addition & 1 deletion src/sensors/MagneticSensorAnalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ MagneticSensorAnalog::MagneticSensorAnalog(uint8_t _pinAnalog, int _min_raw_coun
min_raw_count = _min_raw_count;
max_raw_count = _max_raw_count;

if(pullup == Pullup::INTERN){
if(pullup == Pullup::INTERNAL){
pinMode(pinAnalog, INPUT_PULLUP);
}else{
pinMode(pinAnalog, INPUT);
Expand Down
6 changes: 6 additions & 0 deletions src/sensors/MagneticSensorI2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ struct MagneticSensorI2CConfig_s {
// some predefined structures
extern MagneticSensorI2CConfig_s AS5600_I2C,AS5048_I2C;

#if defined(TARGET_RP2040)
#define SDA I2C_SDA
#define SCL I2C_SCL
#endif


class MagneticSensorI2C: public Sensor{
public:
/**
Expand Down
5 changes: 5 additions & 0 deletions src/sensors/MagneticSensorSPI.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifndef TARGET_RP2040

#include "MagneticSensorSPI.h"

/** Typical configuration for the 14bit AMS AS5147 magnetic sensor over SPI interface */
Expand Down Expand Up @@ -212,3 +214,6 @@ word MagneticSensorSPI::read(word angle_register){
void MagneticSensorSPI::close(){
spi->end();
}


#endif
3 changes: 3 additions & 0 deletions src/sensors/MagneticSensorSPI.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef MAGNETICSENSORSPI_LIB_H
#define MAGNETICSENSORSPI_LIB_H

#ifndef TARGET_RP2040

#include "Arduino.h"
#include <SPI.h>
#include "../common/base_classes/Sensor.h"
Expand Down Expand Up @@ -91,3 +93,4 @@ class MagneticSensorSPI: public Sensor{


#endif
#endif