Skip to content
Snippets Groups Projects
Commit df879caf authored by Alberto Nidasio's avatar Alberto Nidasio
Browse files

[InrternalADC] Fixed regular channels bug and added readChannel function

parent 7ecdc7cf
Branches
Tags
No related merge requests found
...@@ -72,10 +72,8 @@ InternalADC::InternalADC(ADC_TypeDef* adc, const float supplyVoltage, ...@@ -72,10 +72,8 @@ InternalADC::InternalADC(ADC_TypeDef* adc, const float supplyVoltage,
// Init indexMap // Init indexMap
for (auto i = 0; i < CH_NUM; i++) for (auto i = 0; i < CH_NUM; i++)
{
indexMap[i] = -1; indexMap[i] = -1;
} }
}
InternalADC::~InternalADC() InternalADC::~InternalADC()
{ {
...@@ -130,28 +128,37 @@ bool InternalADC::init() ...@@ -130,28 +128,37 @@ bool InternalADC::init()
return true; return true;
} }
ADCData InternalADC::readChannel(Channel channel)
{
if (activeChannels == 0)
addRegularChannel(channel);
setChannelSampleTime(channel, SampleTime::CYCLES_480);
startRegularConversion();
while (!(adc->SR & ADC_SR_EOC))
;
return {TimestampTimer::getInstance().getTimestamp(), channel,
static_cast<uint16_t>(adc->DR) * supplyVoltage / RESOLUTION};
}
bool InternalADC::enableChannel(Channel channel, SampleTime sampleTime) bool InternalADC::enableChannel(Channel channel, SampleTime sampleTime)
{ {
// Check channel number // Check channel number
if (channel < CH0 || channel >= CH_NUM) if (channel < CH0 || channel >= CH_NUM)
{
return false; return false;
}
// Add channel to the sequence // Add channel to the sequence
if (!isUsingDMA) if (!isUsingDMA)
{ {
if (!addInjectedChannel(channel)) if (!addInjectedChannel(channel))
{
return false; return false;
} }
}
else else
{ {
if (!addRegularChannel(channel)) if (!addRegularChannel(channel))
{
return false; return false;
}
// Update the DMA number of data // Update the DMA number of data
dmaStream->NDTR = activeChannels; dmaStream->NDTR = activeChannels;
...@@ -214,14 +221,14 @@ ADCData InternalADC::sampleImpl() ...@@ -214,14 +221,14 @@ ADCData InternalADC::sampleImpl()
// This should trigger the DMA stream for each channel's conversion // This should trigger the DMA stream for each channel's conversion
// Wait for tranfer end // Wait for transfer end
while (!(*statusReg & (transferCompleteMask | transferErrorMask))) while (!(*statusReg & (transferCompleteMask | transferErrorMask)))
; ;
// Clear the transfer complete flag // Clear the transfer complete flag
*clearFlagReg |= transferCompleteMask; *clearFlagReg |= transferCompleteMask;
// If and error has occurred (probaly due to a higher priority stream) // If and error has occurred (probably due to a higher priority stream)
// don't update the timestamp, the values should not have been updated // don't update the timestamp, the values should not have been updated
if (*statusReg & transferErrorMask) if (*statusReg & transferErrorMask)
{ {
...@@ -272,9 +279,7 @@ inline bool InternalADC::addInjectedChannel(Channel channel) ...@@ -272,9 +279,7 @@ inline bool InternalADC::addInjectedChannel(Channel channel)
{ {
// Check active channels number // Check active channels number
if (activeChannels >= 4) if (activeChannels >= 4)
{
return false; return false;
}
// Add the channel to the sequence, starting from position 4 // Add the channel to the sequence, starting from position 4
adc->JSQR |= channel << (15 - activeChannels * 5); adc->JSQR |= channel << (15 - activeChannels * 5);
...@@ -287,10 +292,8 @@ inline bool InternalADC::addInjectedChannel(Channel channel) ...@@ -287,10 +292,8 @@ inline bool InternalADC::addInjectedChannel(Channel channel)
for (auto i = 0; i < CH_NUM; i++) for (auto i = 0; i < CH_NUM; i++)
{ {
if (indexMap[i] >= 0) if (indexMap[i] >= 0)
{
indexMap[i]++; indexMap[i]++;
} }
}
// Set this channel index to 0 // Set this channel index to 0
indexMap[channel] = 0; indexMap[channel] = 0;
...@@ -311,20 +314,21 @@ inline bool InternalADC::addRegularChannel(Channel channel) ...@@ -311,20 +314,21 @@ inline bool InternalADC::addRegularChannel(Channel channel)
// Add the channel to the sequence // Add the channel to the sequence
volatile uint32_t* sqrPtr; volatile uint32_t* sqrPtr;
switch (activeChannels % 6) switch (activeChannels / 6)
{ {
case 1: case 1:
sqrPtr = &(adc->SQR3); sqrPtr = &(adc->SQR2);
break; break;
case 2: case 2:
sqrPtr = &(adc->SQR2); sqrPtr = &(adc->SQR1);
break; break;
default: default:
sqrPtr = &(adc->SQR1); sqrPtr = &(adc->SQR3);
} }
*sqrPtr = channel << ((activeChannels % 6) * 5); *sqrPtr = channel << ((activeChannels % 6) * 5);
// Update the channels number in the register // Update the channels number in the register
adc->SQR1 &= ~ADC_SQR1_L;
adc->SQR1 |= activeChannels << 20; adc->SQR1 |= activeChannels << 20;
// Save the index of the channel in the ADC's regular sequence // Save the index of the channel in the ADC's regular sequence
...@@ -341,13 +345,10 @@ inline void InternalADC::setChannelSampleTime(Channel channel, ...@@ -341,13 +345,10 @@ inline void InternalADC::setChannelSampleTime(Channel channel,
{ {
volatile uint32_t* smprPtr; volatile uint32_t* smprPtr;
if (channel <= 9) if (channel <= 9)
{
smprPtr = &(adc->SMPR2); smprPtr = &(adc->SMPR2);
}
else else
{
smprPtr = &(adc->SMPR1); smprPtr = &(adc->SMPR1);
}
*smprPtr = sampleTime << (channel * 3); *smprPtr = sampleTime << (channel * 3);
} }
......
...@@ -130,6 +130,11 @@ public: ...@@ -130,6 +130,11 @@ public:
*/ */
bool init() override; bool init() override;
/**
* @brief Make a regular conversion for the specified channel.
*/
ADCData readChannel(Channel channel);
bool enableChannel(Channel channel, SampleTime sampleTime = CYCLES_3); bool enableChannel(Channel channel, SampleTime sampleTime = CYCLES_3);
ADCData getVoltage(Channel channel); ADCData getVoltage(Channel channel);
...@@ -183,7 +188,7 @@ private: ...@@ -183,7 +188,7 @@ private:
volatile uint32_t* clearFlagReg; volatile uint32_t* clearFlagReg;
static constexpr int INJECTED_CHANNEL_N = 4; static constexpr int INJECTED_CHANNEL_N = 4;
static constexpr int RESOLUTION = 4096; ///< 12 bits static constexpr int RESOLUTION = 4095; ///< 12 bits
}; };
} // namespace Boardcore } // namespace Boardcore
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment