Skip to content

Commit 7e802f6

Browse files
committed
FEAT ADC-DMA fix deadlock
1 parent a5c7528 commit 7e802f6

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

src/current_sense/LowSideCurrentSense.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ PhaseCurrent_s LowSideCurrentSense::getPhaseCurrents(){
5555
// current.b = (_readADCVoltage(pinB) - offset_ib)*gain_b;// amps
5656
// current.c = (!_isset(pinC)) ? 0 : (_readADCVoltage(pinC) - offset_ic)*gain_c; // amps
5757

58-
adc.readResults(current.a, current.b, current.c);
58+
if(adc.readResults(current.a, current.b, current.c))
59+
{
60+
oldCurrent = current;
61+
}
5962
adc.startADCScan();
6063

61-
return current;
64+
return oldCurrent;
6265
}
6366
// Function synchronizing current sense with motor driver.
6467
// for in-line sensig no such thing is necessary

src/current_sense/LowSideCurrentSense.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ class LowSideCurrentSense: public CurrentSense{
5252
double offset_ia; //!< zero current A voltage value (center of the adc reading)
5353
double offset_ib; //!< zero current B voltage value (center of the adc reading)
5454
double offset_ic; //!< zero current C voltage value (center of the adc reading)
55-
55+
56+
PhaseCurrent_s oldCurrent;
5657
SAMDCurrentSenseADCDMA adc;
5758

5859
};

src/current_sense/hardware_specific/samd21_mcu.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,19 @@ void SAMDCurrentSenseADCDMA::startADCScan(){
4040
adcStartWithDMA();
4141
}
4242

43-
void SAMDCurrentSenseADCDMA::readResults(float & a, float & b, float & c){
44-
while(ADC->CTRLA.bit.ENABLE) ;
43+
bool SAMDCurrentSenseADCDMA::readResults(float & a, float & b, float & c){
44+
if(ADC->CTRLA.bit.ENABLE)
45+
return false;
4546
uint32_t ainA = g_APinDescription[pinA].ulADCChannelNumber;
4647
uint32_t ainB = g_APinDescription[pinB].ulADCChannelNumber;
47-
uint32_t ainC = g_APinDescription[pinC].ulADCChannelNumber;
4848
a = adcBuffer[ainA] * countsToVolts;
4949
b = adcBuffer[ainB] * countsToVolts;
5050
if(_isset(pinC))
5151
{
52+
uint32_t ainC = g_APinDescription[pinC].ulADCChannelNumber;
5253
c = adcBuffer[ainC] * countsToVolts;
5354
}
55+
return true;
5456
}
5557

5658
void SAMDCurrentSenseADCDMA::initPins(){
@@ -61,11 +63,11 @@ void SAMDCurrentSenseADCDMA::initPins(){
6163

6264
uint32_t ainA = g_APinDescription[pinA].ulADCChannelNumber;
6365
uint32_t ainB = g_APinDescription[pinB].ulADCChannelNumber;
64-
uint32_t ainC = g_APinDescription[pinC].ulADCChannelNumber;
6566
firstAIN = min(ainA, ainB);
6667
lastAIN = max(ainA, ainB);
6768
if( _isset(pinC) )
6869
{
70+
uint32_t ainC = g_APinDescription[pinC].ulADCChannelNumber;
6971
pinMode(pinC, INPUT);
7072
firstAIN = min(firstAIN, ainC);
7173
lastAIN = max(lastAIN, ainC);
@@ -158,7 +160,7 @@ void SAMDCurrentSenseADCDMA::initDMA() {
158160
}
159161

160162

161-
void SAMDCurrentSenseADCDMA::adcToDMATransfer(void *rxdata, size_t hwords) {
163+
void SAMDCurrentSenseADCDMA::adcToDMATransfer(void *rxdata, uint32_t hwords) {
162164

163165
DMAC->CHID.reg = DMAC_CHID_ID(channelDMA);
164166
DMAC->CHCTRLA.reg &= ~DMAC_CHCTRLA_ENABLE;
@@ -183,13 +185,19 @@ void SAMDCurrentSenseADCDMA::adcToDMATransfer(void *rxdata, size_t hwords) {
183185

184186

185187
void adcStopWithDMA(void){
188+
ADCsync();
186189
ADC->CTRLA.bit.ENABLE = 0x00;
190+
187191
}
188192

189193
void adcStartWithDMA(void){
194+
ADCsync();
190195
ADC->INPUTCTRL.bit.INPUTOFFSET = 0;
196+
ADCsync();
191197
ADC->SWTRIG.bit.FLUSH = 1;
198+
ADCsync();
192199
ADC->CTRLA.bit.ENABLE = 0x01;
200+
ADCsync();
193201
}
194202

195203
void DMAC_Handler() {

src/current_sense/hardware_specific/samd21_mcu.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
#include "../hardware_api.h"
3-
3+
#include <stdint.h>
44
typedef struct {
55
uint16_t btctrl;
66
uint16_t btcnt;
@@ -18,11 +18,11 @@ class SAMDCurrentSenseADCDMA
1818

1919
void init();
2020
void startADCScan();
21-
void readResults(float & a, float & b, float & c);
21+
bool readResults(float & a, float & b, float & c);
2222

2323
private:
2424

25-
void adcToDMATransfer(void *rxdata, size_t hwords);
25+
void adcToDMATransfer(void *rxdata, uint32_t hwords);
2626

2727
void initPins();
2828
void initADC();

0 commit comments

Comments
 (0)