-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Due: solve issues with nonstandard pin operations #3524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9491c1f
8589c79
33255ef
d1c65ca
5a37e94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,20 @@ extern void pinMode( uint32_t ulPin, uint32_t ulMode ) | |
return ; | ||
} | ||
|
||
if ((g_pinStatus[ulPin] & 0xF) == PIN_STATUS_ANALOG) | ||
{ | ||
adc_disable_channel( ADC, g_APinDescription[ulPin].ulADCChannelNumber); | ||
} | ||
|
||
if ((g_pinStatus[ulPin] & 0xF) < PIN_STATUS_DIGITAL_OUTPUT && g_pinStatus[ulPin] != 0) | ||
{ | ||
// return if already configured in the right way | ||
if (((g_pinStatus[ulPin] & 0xF) == PIN_STATUS_DIGITAL_INPUT && ulMode == INPUT) || | ||
((g_pinStatus[ulPin] & 0xF) == PIN_STATUS_DIGITAL_INPUT_PULLUP && ulMode == INPUT_PULLUP) || | ||
((g_pinStatus[ulPin] & 0xF) == PIN_STATUS_DIGITAL_OUTPUT && ulMode == OUTPUT)) | ||
return; | ||
} | ||
|
||
switch ( ulMode ) | ||
{ | ||
case INPUT: | ||
|
@@ -39,6 +53,7 @@ extern void pinMode( uint32_t ulPin, uint32_t ulMode ) | |
PIO_INPUT, | ||
g_APinDescription[ulPin].ulPin, | ||
0 ) ; | ||
g_pinStatus[ulPin] = (g_pinStatus[ulPin] & 0xF0) | PIN_STATUS_DIGITAL_INPUT; | ||
break ; | ||
|
||
case INPUT_PULLUP: | ||
|
@@ -49,15 +64,18 @@ extern void pinMode( uint32_t ulPin, uint32_t ulMode ) | |
PIO_INPUT, | ||
g_APinDescription[ulPin].ulPin, | ||
PIO_PULLUP ) ; | ||
g_pinStatus[ulPin] = (g_pinStatus[ulPin] & 0xF0) | PIN_STATUS_DIGITAL_INPUT_PULLUP; | ||
break ; | ||
|
||
case OUTPUT: | ||
PIO_Configure( | ||
g_APinDescription[ulPin].pPort, | ||
PIO_OUTPUT_1, | ||
(g_pinStatus[ulPin] & 0xF0) >> 4 ? PIO_OUTPUT_1 : PIO_OUTPUT_0, | ||
g_APinDescription[ulPin].ulPin, | ||
g_APinDescription[ulPin].ulPinConfiguration ) ; | ||
|
||
g_pinStatus[ulPin] = (g_pinStatus[ulPin] & 0xF0) | PIN_STATUS_DIGITAL_OUTPUT; | ||
|
||
/* if all pins are output, disable PIO Controller clocking, reduce power consumption */ | ||
if ( g_APinDescription[ulPin].pPort->PIO_OSR == 0xffffffff ) | ||
{ | ||
|
@@ -78,6 +96,12 @@ extern void digitalWrite( uint32_t ulPin, uint32_t ulVal ) | |
return ; | ||
} | ||
|
||
if ((g_pinStatus[ulPin] & 0xF) == PIN_STATUS_PWM) { | ||
pinMode(ulPin, OUTPUT); | ||
} | ||
|
||
g_pinStatus[ulPin] = (g_pinStatus[ulPin] & 0x0F) | (ulVal << 4) ; | ||
|
||
if ( PIO_GetOutputDataStatus( g_APinDescription[ulPin].pPort, g_APinDescription[ulPin].ulPin ) == 0 ) | ||
{ | ||
PIO_PullUp( g_APinDescription[ulPin].pPort, g_APinDescription[ulPin].ulPin, ulVal ) ; | ||
|
@@ -90,6 +114,14 @@ extern void digitalWrite( uint32_t ulPin, uint32_t ulVal ) | |
|
||
extern int digitalRead( uint32_t ulPin ) | ||
{ | ||
if ((g_pinStatus[ulPin] & 0xF) == PIN_STATUS_DIGITAL_OUTPUT) { | ||
return (g_pinStatus[ulPin] & 0xF0) >> 4; | ||
} | ||
|
||
if ((g_pinStatus[ulPin] & 0xF) == PIN_STATUS_ANALOG) { | ||
pinMode(ulPin, INPUT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the meaning of calling pinMode here? Edit: I finally understood: the meaning is to commute the function of a pin from analog to digital. I feel a bit weird seeing the system pins configured during setup and their functions updated on the fly later during the main process but it may happen, indeed. The secondary goal being to have this commutation done a fast as possible, the clock activation of the given PIOx should be done at once during the system inits and never touched again later. It is the way we followed for the Zero. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, the main goal of this patchset was solving the incompatibilities between pins behaviour on AVR and on SAM core, losing as few performances as we can. |
||
} | ||
|
||
if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) | ||
{ | ||
return LOW ; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't these definitions be put in a typedef'ed enum?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Being a mashup between an enum and a mask (to save some bytes) I'd prefer to keep the hardcoded values, but it's a matter of choice 😄