Skip to content

Commit c9ff03e

Browse files
Merge pull request #72 from runger1101001/dev
SAME51 support (tested on Feather M4 CAN) and minor tweaks
2 parents 4aac992 + ebd4906 commit c9ff03e

File tree

8 files changed

+48
-32
lines changed

8 files changed

+48
-32
lines changed

src/BLDCMotor.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,15 @@ int BLDCMotor::absoluteZeroSearch() {
247247
// Iterative function looping FOC algorithm, setting Uq on the Motor
248248
// The faster it can be run the better
249249
void BLDCMotor::loopFOC() {
250+
251+
// shaft angle
252+
shaft_angle = shaftAngle(); // read value even if motor is disabled to keep the monitoring updated
253+
250254
// if disabled do nothing
251255
if(!enabled) return;
252256
// if open-loop do nothing
253257
if( controller==MotionControlType::angle_openloop || controller==MotionControlType::velocity_openloop ) return;
254258

255-
// shaft angle
256-
shaft_angle = shaftAngle();
257259
// electrical angle - need shaftAngle to be called first
258260
electrical_angle = electricalAngle();
259261

@@ -298,15 +300,17 @@ void BLDCMotor::loopFOC() {
298300
// - needs to be called iteratively it is asynchronous function
299301
// - if target is not set it uses motor.target value
300302
void BLDCMotor::move(float new_target) {
303+
304+
// get angular velocity
305+
shaft_velocity = shaftVelocity(); // read value even if motor is disabled to keep the monitoring updated
306+
301307
// if disabled do nothing
302308
if(!enabled) return;
303309
// downsampling (optional)
304310
if(motion_cnt++ < motion_downsample) return;
305311
motion_cnt = 0;
306312
// set internal target variable
307313
if(_isset(new_target)) target = new_target;
308-
// get angular velocity
309-
shaft_velocity = shaftVelocity();
310314

311315
switch (controller) {
312316
case MotionControlType::torque:

src/drivers/BLDCDriver6PWM.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ BLDCDriver6PWM::BLDCDriver6PWM(int phA_h,int phA_l,int phB_h,int phB_l,int phC_h
2424
// enable motor driver
2525
void BLDCDriver6PWM::enable(){
2626
// enable_pin the driver - if enable_pin pin available
27-
if ( _isset(enable_pin) ) digitalWrite(enable_pin, HIGH);
27+
if ( _isset(enable_pin) ) digitalWrite(enable_pin, enable_active_high);
2828
// set zero to PWM
2929
setPwm(0, 0, 0);
3030
}
@@ -35,7 +35,7 @@ void BLDCDriver6PWM::disable()
3535
// set zero to PWM
3636
setPwm(0, 0, 0);
3737
// disable the driver - if enable_pin pin available
38-
if ( _isset(enable_pin) ) digitalWrite(enable_pin, LOW);
38+
if ( _isset(enable_pin) ) digitalWrite(enable_pin, !enable_active_high);
3939

4040
}
4141

@@ -82,4 +82,4 @@ void BLDCDriver6PWM::setPwm(float Ua, float Ub, float Uc) {
8282
// Set voltage to the pwm pin
8383
void BLDCDriver6PWM::setPhaseState(int sa, int sb, int sc) {
8484
// TODO implement disabling
85-
}
85+
}

src/drivers/BLDCDriver6PWM.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class BLDCDriver6PWM: public BLDCDriver
3737
int pwmB_h,pwmB_l; //!< phase B pwm pin number
3838
int pwmC_h,pwmC_l; //!< phase C pwm pin number
3939
int enable_pin; //!< enable pin number
40+
bool enable_active_high = true;
4041

4142
float dead_zone; //!< a percentage of dead-time(zone) (both high and low side in low) for each pwm cycle [0,1]
4243

src/drivers/hardware_specific/generic_mcu.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
#elif defined(_STM32_DEF_) // or stm32
1414

15-
#elif defined(_SAMD21_) // samd21 for the moment, samd51 in progress...
15+
#elif defined(_SAMD21_) // samd21
1616

17-
#elif defined(_SAMD51_) // samd21 for the moment, samd51 in progress...
17+
#elif defined(_SAMD51_) // samd51
18+
19+
#elif defined(__SAME51J19A__) || defined(__ATSAME51J19A__) // samd51
1820

1921
#else
2022

src/drivers/hardware_specific/samd21_mcu.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,17 +310,18 @@ void writeSAMDDutyCycle(int chaninfo, float dc) {
310310
uint8_t chan = GetTCChannelNumber(chaninfo);
311311
if (tccn<TCC_INST_NUM) {
312312
Tcc* tcc = (Tcc*)GetTC(chaninfo);
313-
// set via CC
314-
//tcc->CC[chan].reg = (uint32_t)((SIMPLEFOC_SAMD_PWM_RESOLUTION-1) * dc);
315-
//uint32_t chanbit = 0x1<<(TCC_SYNCBUSY_CC0_Pos+chan);
316-
//while ( (tcc->SYNCBUSY.reg & chanbit) > 0 );
313+
// set via CC
314+
// tcc->CC[chan].reg = (uint32_t)((SIMPLEFOC_SAMD_PWM_RESOLUTION-1) * dc);
315+
// uint32_t chanbit = 0x1<<(TCC_SYNCBUSY_CC0_Pos+chan);
316+
// while ( (tcc->SYNCBUSY.reg & chanbit) > 0 );
317317
// set via CCB
318+
while ( (tcc->SYNCBUSY.vec.CC & (0x1<<chan)) > 0 );
318319
tcc->CCB[chan].reg = (uint32_t)((SIMPLEFOC_SAMD_PWM_RESOLUTION-1) * dc);
319-
while ( (tcc->SYNCBUSY.vec.CCB & (0x1<<chan)) > 0 );
320-
tcc->STATUS.vec.CCBV |= (0x1<<chan);
321-
while ( tcc->SYNCBUSY.bit.STATUS > 0 );
322-
tcc->CTRLBSET.reg |= TCC_CTRLBSET_CMD(TCC_CTRLBSET_CMD_UPDATE_Val);
323-
while ( tcc->SYNCBUSY.bit.CTRLB > 0 );
320+
// while ( (tcc->SYNCBUSY.vec.CCB & (0x1<<chan)) > 0 );
321+
// tcc->STATUS.vec.CCBV |= (0x1<<chan);
322+
// while ( tcc->SYNCBUSY.bit.STATUS > 0 );
323+
// tcc->CTRLBSET.reg |= TCC_CTRLBSET_CMD(TCC_CTRLBSET_CMD_UPDATE_Val);
324+
// while ( tcc->SYNCBUSY.bit.CTRLB > 0 );
324325
}
325326
else {
326327
Tc* tc = (Tc*)GetTC(chaninfo);

src/drivers/hardware_specific/samd51_mcu.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "./samd_mcu.h"
44

55

6-
#ifdef _SAMD51_
6+
#if defined(_SAMD51_)||defined(_SAME51_)
77

88

99

@@ -129,12 +129,13 @@ void writeSAMDDutyCycle(int chaninfo, float dc) {
129129
uint8_t chan = GetTCChannelNumber(chaninfo);
130130
if (tccn<TCC_INST_NUM) {
131131
Tcc* tcc = (Tcc*)GetTC(chaninfo);
132-
// set via CCBUF]
132+
// set via CCBUF
133+
while ( (tcc->SYNCBUSY.vec.CC & (0x1<<chan)) > 0 );
133134
tcc->CCBUF[chan].reg = (uint32_t)((SIMPLEFOC_SAMD_PWM_RESOLUTION-1) * dc); // TODO pwm frequency!
134-
tcc->STATUS.vec.CCBUFV |= (0x1<<chan);
135-
while ( tcc->SYNCBUSY.bit.STATUS > 0 );
136-
tcc->CTRLBSET.reg |= TCC_CTRLBSET_CMD(TCC_CTRLBSET_CMD_UPDATE_Val);
137-
while ( tcc->SYNCBUSY.bit.CTRLB > 0 );
135+
// tcc->STATUS.vec.CCBUFV |= (0x1<<chan);
136+
// while ( tcc->SYNCBUSY.bit.STATUS > 0 );
137+
// tcc->CTRLBSET.reg |= TCC_CTRLBSET_CMD(TCC_CTRLBSET_CMD_UPDATE_Val);
138+
// while ( tcc->SYNCBUSY.bit.CTRLB > 0 );
138139
}
139140
else {
140141
// Tc* tc = (Tc*)GetTC(chaninfo);

src/drivers/hardware_specific/samd_mcu.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "./samd_mcu.h"
55

6-
#if defined(_SAMD21_)||defined(_SAMD51_)
6+
#if defined(_SAMD21_)||defined(_SAMD51_)||defined(_SAME51_)
77

88

99

@@ -90,7 +90,7 @@ tccConfiguration getTCCChannelNr(int pin, EPioType peripheral) {
9090
result.tcc.chaninfo = association.tccF;
9191
result.wo = association.woF;
9292
}
93-
#ifdef _SAMD51_
93+
#if defined(_SAMD51_)||defined(_SAME51_)
9494
else if (peripheral==PIO_TCC_PDEC) {
9595
result.tcc.chaninfo = association.tccG;
9696
result.wo = association.woG;
@@ -758,7 +758,7 @@ void printAllPinInfos() {
758758
case PORTA: Serial.print(" PA"); break;
759759
case PORTB: Serial.print(" PB"); break;
760760
case PORTC: Serial.print(" PC"); break;
761-
#ifdef _SAMD51_
761+
#if defined(_SAMD51_)||defined(_SAME51_)
762762
case PORTD: Serial.print(" PD"); break;
763763
#endif
764764
}
@@ -803,7 +803,7 @@ void printAllPinInfos() {
803803
else
804804
Serial.print(" None ");
805805

806-
#ifdef _SAMD51_
806+
#if defined(_SAMD51_)||defined(_SAME51_)
807807
Serial.print(" G=");
808808
if (association.tccG>=0) {
809809
int tcn = GetTCNumber(association.tccG);
@@ -842,7 +842,7 @@ void printTCCConfiguration(tccConfiguration& info) {
842842
Serial.print(" E "); break;
843843
case PIO_TIMER_ALT:
844844
Serial.print(" F "); break;
845-
#ifdef _SAMD51_
845+
#if defined(_SAMD51_)||defined(_SAME51_)
846846
case PIO_TCC_PDEC:
847847
Serial.print(" G "); break;
848848
#endif

src/drivers/hardware_specific/samd_mcu.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99
#include "../hardware_api.h"
1010

1111

12-
#if defined(_SAMD21_)||defined(_SAMD51_)
12+
#if defined(__SAME51J19A__) || defined(__ATSAME51J19A__)
13+
#ifndef _SAME51_
14+
#define _SAME51_
15+
#endif
16+
#endif
17+
18+
19+
#if defined(_SAMD21_)||defined(_SAMD51_)||defined(_SAME51_)
1320

1421

1522
#include "Arduino.h"
@@ -53,7 +60,7 @@ struct wo_association {
5360
uint8_t woE;
5461
ETCChannel tccF;
5562
uint8_t woF;
56-
#if defined(_SAMD51_)
63+
#if defined(_SAMD51_)||defined(_SAME51_)
5764
ETCChannel tccG;
5865
uint8_t woG;
5966
#endif
@@ -63,7 +70,7 @@ struct wo_association {
6370

6471
#if defined(_SAMD21_)
6572
#define NUM_PIO_TIMER_PERIPHERALS 2
66-
#elif defined(_SAMD51_)
73+
#elif defined(_SAMD51_)||defined(_SAME51_)
6774
#define NUM_PIO_TIMER_PERIPHERALS 3
6875
#endif
6976

0 commit comments

Comments
 (0)