Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/encoders/stm32hwencoder/STM32HWEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ STM32HWEncoder::STM32HWEncoder(unsigned int _ppr, int pinA, int pinB, int pinI)
// function returns encoder handle
TIM_HandleTypeDef STM32HWEncoder::getEncoderTimerHandle() { return encoder_handle; }

#if STM32_CORE_VERSION_MAJOR >= 3
void STM32HWEncoder::IndexInterrupt()
{
encoder_handle.Instance->CNT = 0; // reset counter
index_found = true;
}
#endif

/*
Shaft angle calculation
*/
Expand Down Expand Up @@ -95,7 +103,11 @@ void STM32HWEncoder::init() {
encoder_config.IC2Filter = 0;

encoder_handle.Instance = InstanceA; // e.g. TIM4;
#if STM32_CORE_VERSION_MAJOR >= 3
enableTimerClock(encoder_handle.Instance);
#else
enableTimerClock(&encoder_handle);
#endif

if (HAL_TIM_Encoder_Init(&encoder_handle, &encoder_config) != HAL_OK) {
initialized = false;
Expand Down Expand Up @@ -125,10 +137,14 @@ void STM32HWEncoder::init() {
// on index pulse
if(hasIndex())
{
#if STM32_CORE_VERSION_MAJOR >= 3
attachInterruptParam(digitalPinToInterrupt(pinNametoDigitalPin(_pinI)), (void (*)(void*))&STM32HWEncoder::IndexInterrupt, index_polarity, this);
#else
attachInterrupt(digitalPinToInterrupt(pinNametoDigitalPin(_pinI)), [this]() {
encoder_handle.Instance->CNT = 0; // reset counter
index_found = true;
}, index_polarity);
#endif
}

if (HAL_TIM_Encoder_Start(&encoder_handle, TIM_CHANNEL_1) != HAL_OK) {
Expand Down
10 changes: 9 additions & 1 deletion src/encoders/stm32hwencoder/STM32HWEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ class STM32HWEncoder : public Sensor {
uint32_t cpr; //!< encoder cpr number
PinName _pinA, _pinB, _pinI;
bool index_found;
uint32_t index_polarity = RISING;
#if STM32_CORE_VERSION_MAJOR >= 3
PinStatus index_polarity = RISING;
#else
int index_polarity = RISING;
#endif
// Use TIM_ENCODERMODE_CLOCKPLUSDIRECTION_X1 for step/dir counting (pinA ch1 dir, pinB ch2 step)
uint32_t encoder_mode = TIM_ENCODERMODE_TI12; // Must be set before calling init

protected:
float getSensorAngle() override;

#if STM32_CORE_VERSION_MAJOR >= 3
void IndexInterrupt();
#endif

TIM_HandleTypeDef encoder_handle;

};
Expand Down
27 changes: 13 additions & 14 deletions src/utilities/stm32math/STM32G4CORDICTrigFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "stm32g4xx_ll_rcc.h"
#include "stm32g4xx_ll_bus.h"
#include "common/foc_utils.h"
#include "arm_math.h"

CORDIC_HandleTypeDef thisCordic;

Expand Down Expand Up @@ -46,37 +45,37 @@ bool SimpleFOC_CORDIC_Config(void){
return true;
};

#define float_to_q1_31(in) ((uint32_t)(int32_t)(in * 0x80000000))
#define q31_to_float(in) ((float)((int32_t)in) / 0x80000000)


float _sin(float a) {
a = fmod(a, _2PI);
if (a>_PI) a -= _2PI;
if (a<-_PI) a += _2PI;
CORDIC->WDATA = (q31_t)((a / _PI) * 0x80000000);
q31_t out_cos = (int32_t)CORDIC->RDATA; // read cosine result
q31_t out_sin = (int32_t)CORDIC->RDATA; // read sine result
return (float)out_sin / (float)0x80000000;
CORDIC->WDATA = float_to_q1_31(a / _PI);
uint32_t out_cos = CORDIC->RDATA; // read cosine result
float out_sin = q31_to_float(CORDIC->RDATA); // read sine result
return out_sin;
}

float _cos(float a) {
a = fmod(a, _2PI);
if (a>_PI) a -= _2PI;
if (a<-_PI) a += _2PI;
CORDIC->WDATA = (q31_t)((a / _PI) * 0x80000000);
q31_t out_cos = (int32_t)CORDIC->RDATA; // read cosine result
q31_t out_sin = (int32_t)CORDIC->RDATA; // read sine result
return (float)out_cos / (float)0x80000000;
CORDIC->WDATA = float_to_q1_31(a / _PI);
float out_cos = q31_to_float(CORDIC->RDATA); // read cosine result
uint32_t out_sin = CORDIC->RDATA; // read sine result
return out_cos;
}

void _sincos(float a, float* s, float* c) {
a = fmod(a, _2PI);
if (a>_PI) a -= _2PI;
if (a<-_PI) a += _2PI;
CORDIC->WDATA = (q31_t)((a / _PI) * 0x80000000);
q31_t out_cos = (int32_t)CORDIC->RDATA; // read cosine result
q31_t out_sin = (int32_t)CORDIC->RDATA; // read sine result
*c = (float)out_cos / (float)0x80000000;
*s = (float)out_sin / (float)0x80000000;
CORDIC->WDATA = float_to_q1_31(a / _PI);
*c = q31_to_float(CORDIC->RDATA); // read cosine result
*s = q31_to_float(CORDIC->RDATA); // read sine result
}


Expand Down
Loading