Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Skyward Boardcore
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Avionics
Software Development
Skyward Boardcore
Commits
df879caf
Commit
df879caf
authored
3 years ago
by
Alberto Nidasio
Browse files
Options
Downloads
Patches
Plain Diff
[InrternalADC] Fixed regular channels bug and added readChannel function
parent
7ecdc7cf
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/shared/drivers/adc/InternalADC.cpp
+23
-22
23 additions, 22 deletions
src/shared/drivers/adc/InternalADC.cpp
src/shared/drivers/adc/InternalADC.h
+6
-1
6 additions, 1 deletion
src/shared/drivers/adc/InternalADC.h
with
29 additions
and
23 deletions
src/shared/drivers/adc/InternalADC.cpp
+
23
−
22
View file @
df879caf
...
@@ -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 tran
s
fer 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 (proba
b
ly 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
->
SQR
3
);
sqrPtr
=
&
(
adc
->
SQR
2
);
break
;
break
;
case
2
:
case
2
:
sqrPtr
=
&
(
adc
->
SQR
2
);
sqrPtr
=
&
(
adc
->
SQR
1
);
break
;
break
;
default:
default:
sqrPtr
=
&
(
adc
->
SQR
1
);
sqrPtr
=
&
(
adc
->
SQR
3
);
}
}
*
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
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/shared/drivers/adc/InternalADC.h
+
6
−
1
View file @
df879caf
...
@@ -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
=
409
6
;
///< 12 bits
static
constexpr
int
RESOLUTION
=
409
5
;
///< 12 bits
};
};
}
// namespace Boardcore
}
// namespace Boardcore
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment