Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Arduino Core for SAMD21 CPU
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
josc941e
Arduino Core for SAMD21 CPU
Commits
5cf84a4b
Commit
5cf84a4b
authored
10 years ago
by
Cristian Maglie
Committed by
Cristian Maglie
10 years ago
Browse files
Options
Downloads
Patches
Plain Diff
wiring_analog.c: factored out SYNCBUSY loops
parent
a594bc32
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cores/arduino/wiring_analog.c
+27
-16
27 additions, 16 deletions
cores/arduino/wiring_analog.c
with
27 additions
and
16 deletions
cores/arduino/wiring_analog.c
+
27
−
16
View file @
5cf84a4b
...
@@ -27,20 +27,32 @@ extern "C" {
...
@@ -27,20 +27,32 @@ extern "C" {
static
int
_readResolution
=
10
;
static
int
_readResolution
=
10
;
static
int
_writeResolution
=
10
;
static
int
_writeResolution
=
10
;
// Wait for synchronization of registers between the clock domains
static
__inline__
void
syncADC
()
__attribute__
((
always_inline
,
unused
));
static
void
syncADC
()
{
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
)
;
}
// Wait for synchronization of registers between the clock domains
static
__inline__
void
syncDAC
()
__attribute__
((
always_inline
,
unused
));
static
void
syncDAC
()
{
while
(
DAC
->
STATUS
.
bit
.
SYNCBUSY
==
1
)
;
}
void
analogReadResolution
(
int
res
)
void
analogReadResolution
(
int
res
)
{
{
syncADC
();
switch
(
res
)
switch
(
res
)
{
{
case
12
:
case
12
:
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
ADC
->
CTRLB
.
bit
.
RESSEL
=
ADC_CTRLB_RESSEL_12BIT_Val
;
ADC
->
CTRLB
.
bit
.
RESSEL
=
ADC_CTRLB_RESSEL_12BIT_Val
;
break
;
break
;
case
8
:
case
8
:
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
ADC
->
CTRLB
.
bit
.
RESSEL
=
ADC_CTRLB_RESSEL_8BIT_Val
;
ADC
->
CTRLB
.
bit
.
RESSEL
=
ADC_CTRLB_RESSEL_8BIT_Val
;
break
;
break
;
default:
default:
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
ADC
->
CTRLB
.
bit
.
RESSEL
=
ADC_CTRLB_RESSEL_10BIT_Val
;
ADC
->
CTRLB
.
bit
.
RESSEL
=
ADC_CTRLB_RESSEL_10BIT_Val
;
break
;
break
;
}
}
...
@@ -77,7 +89,7 @@ static inline uint32_t mapResolution( uint32_t value, uint32_t from, uint32_t to
...
@@ -77,7 +89,7 @@ static inline uint32_t mapResolution( uint32_t value, uint32_t from, uint32_t to
*/
*/
void
analogReference
(
eAnalogReference
ulMode
)
void
analogReference
(
eAnalogReference
ulMode
)
{
{
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncADC
(
);
switch
(
ulMode
)
switch
(
ulMode
)
{
{
case
AR_INTERNAL
:
case
AR_INTERNAL
:
...
@@ -122,15 +134,15 @@ uint32_t analogRead( uint32_t ulPin )
...
@@ -122,15 +134,15 @@ uint32_t analogRead( uint32_t ulPin )
if
(
ulPin
==
A0
)
// Disable DAC, if analogWrite(A0,dval) used previously the DAC is enabled
if
(
ulPin
==
A0
)
// Disable DAC, if analogWrite(A0,dval) used previously the DAC is enabled
{
{
while
(
DAC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncDAC
(
);
DAC
->
CTRLA
.
bit
.
ENABLE
=
0x00
;
// Disable DAC
DAC
->
CTRLA
.
bit
.
ENABLE
=
0x00
;
// Disable DAC
//DAC->CTRLB.bit.EOEN = 0x00; // The DAC output is turned off.
//DAC->CTRLB.bit.EOEN = 0x00; // The DAC output is turned off.
while
(
DAC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncDAC
(
);
}
}
pinPeripheral
(
ulPin
,
g_APinDescription
[
ulPin
].
ulPinType
);
pinPeripheral
(
ulPin
,
g_APinDescription
[
ulPin
].
ulPinType
);
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncADC
(
);
ADC
->
INPUTCTRL
.
bit
.
MUXPOS
=
g_APinDescription
[
ulPin
].
ulADCChannelNumber
;
// Selection for the positive ADC input
ADC
->
INPUTCTRL
.
bit
.
MUXPOS
=
g_APinDescription
[
ulPin
].
ulADCChannelNumber
;
// Selection for the positive ADC input
// Control A
// Control A
...
@@ -145,28 +157,27 @@ uint32_t analogRead( uint32_t ulPin )
...
@@ -145,28 +157,27 @@ uint32_t analogRead( uint32_t ulPin )
* Before enabling the ADC, the asynchronous clock source must be selected and enabled, and the ADC reference must be
* Before enabling the ADC, the asynchronous clock source must be selected and enabled, and the ADC reference must be
* configured. The first conversion after the reference is changed must not be used.
* configured. The first conversion after the reference is changed must not be used.
*/
*/
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncADC
(
);
ADC
->
CTRLA
.
bit
.
ENABLE
=
0x01
;
// Enable ADC
ADC
->
CTRLA
.
bit
.
ENABLE
=
0x01
;
// Enable ADC
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
// Start conversion
// Start conversion
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncADC
(
);
ADC
->
SWTRIG
.
bit
.
START
=
1
;
ADC
->
SWTRIG
.
bit
.
START
=
1
;
// Clear the Data Ready flag
// Clear the Data Ready flag
ADC
->
INTFLAG
.
bit
.
RESRDY
=
1
;
ADC
->
INTFLAG
.
bit
.
RESRDY
=
1
;
// Start conversion again, since The first conversion after the reference is changed must not be used.
// Start conversion again, since The first conversion after the reference is changed must not be used.
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncADC
(
);
ADC
->
SWTRIG
.
bit
.
START
=
1
;
ADC
->
SWTRIG
.
bit
.
START
=
1
;
// Store the value
// Store the value
while
(
ADC
->
INTFLAG
.
bit
.
RESRDY
==
0
);
// Waiting for conversion to complete
while
(
ADC
->
INTFLAG
.
bit
.
RESRDY
==
0
);
// Waiting for conversion to complete
valueRead
=
ADC
->
RESULT
.
reg
;
valueRead
=
ADC
->
RESULT
.
reg
;
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncADC
(
);
ADC
->
CTRLA
.
bit
.
ENABLE
=
0x00
;
// Disable ADC
ADC
->
CTRLA
.
bit
.
ENABLE
=
0x00
;
// Disable ADC
while
(
ADC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncADC
(
);
return
valueRead
;
return
valueRead
;
}
}
...
@@ -192,11 +203,11 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
...
@@ -192,11 +203,11 @@ void analogWrite( uint32_t ulPin, uint32_t ulValue )
return
;
return
;
}
}
while
(
DAC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncDAC
(
);
DAC
->
DATA
.
reg
=
ulValue
&
0x3FF
;
// DAC on 10 bits.
DAC
->
DATA
.
reg
=
ulValue
&
0x3FF
;
// DAC on 10 bits.
while
(
DAC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncDAC
(
);
DAC
->
CTRLA
.
bit
.
ENABLE
=
0x01
;
//Enable ADC
DAC
->
CTRLA
.
bit
.
ENABLE
=
0x01
;
//Enable ADC
while
(
DAC
->
STATUS
.
bit
.
SYNCBUSY
==
1
);
syncDAC
(
);
return
;
return
;
}
}
...
...
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