Skip to content

Commit 46d3418

Browse files
committed
For PR to add pinPeripheral() --- jcw
1 parent ac6a5f1 commit 46d3418

File tree

3 files changed

+190
-2
lines changed

3 files changed

+190
-2
lines changed

cores/arduino/wiring_constants.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//** just starting suggestion at this point --- jcw - 9/10/20
2+
/*
3+
Copyright (c) 2014 Arduino. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13+
See the GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _WIRING_CONSTANTS_
21+
#define _WIRING_CONSTANTS_
22+
23+
#ifdef __cplusplus
24+
extern "C"{
25+
#endif // __cplusplus
26+
27+
#define LOW (0x0)
28+
#define HIGH (0x1)
29+
30+
#define INPUT (0x0)
31+
#define OUTPUT (0x1)
32+
#define INPUT_PULLUP (0x2)
33+
#define INPUT_PULLDOWN (0x3)
34+
35+
#define PI 3.1415926535897932384626433832795
36+
#define HALF_PI 1.5707963267948966192313216916398
37+
#define TWO_PI 6.283185307179586476925286766559
38+
#define DEG_TO_RAD 0.017453292519943295769236907684886
39+
#define RAD_TO_DEG 57.295779513082320876798154814105
40+
#define EULER 2.718281828459045235360287471352
41+
42+
#define SERIAL 0x0
43+
#define DISPLAY 0x1
44+
45+
enum BitOrder {
46+
LSBFIRST = 0,
47+
MSBFIRST = 1
48+
};
49+
50+
// moved to WInterrupts.h
51+
//// LOW 0
52+
//// HIGH 1
53+
//#define CHANGE 2
54+
//#define FALLING 3
55+
//#define RISING 4
56+
//
57+
//#define DEFAULT 1
58+
//#define EXTERNAL 0
59+
60+
#ifdef __cplusplus
61+
} // extern "C"
62+
#endif // __cplusplus
63+
64+
#endif /* _WIRING_CONSTANTS_ */

cores/arduino/wiring_private.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//** just starting suggestion at this point --- jcw - 9/10/20
2+
/*
3+
Copyright (c) 2015 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13+
See the GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "Arduino.h"
21+
#include "wiring_private.h"
22+
23+
int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral )
24+
{
25+
// Handle the case the pin isn't usable as PIO
26+
if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN )
27+
{
28+
return -1 ;
29+
}
30+
31+
switch ( ulPeripheral )
32+
{
33+
case PIO_DIGITAL:
34+
case PIO_INPUT:
35+
case PIO_INPUT_PULLUP:
36+
case PIO_OUTPUT:
37+
// Disable peripheral muxing, done in pinMode
38+
// PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].bit.PMUXEN = 0 ;
39+
40+
// Configure pin mode, if requested
41+
if ( ulPeripheral == PIO_INPUT )
42+
{
43+
pinMode( ulPin, INPUT ) ;
44+
}
45+
else
46+
{
47+
if ( ulPeripheral == PIO_INPUT_PULLUP )
48+
{
49+
pinMode( ulPin, INPUT_PULLUP ) ;
50+
}
51+
else
52+
{
53+
if ( ulPeripheral == PIO_OUTPUT )
54+
{
55+
pinMode( ulPin, OUTPUT ) ;
56+
}
57+
else
58+
{
59+
// PIO_DIGITAL, do we have to do something as all cases are covered?
60+
}
61+
}
62+
}
63+
break ;
64+
65+
case PIO_ANALOG:
66+
case PIO_SERCOM:
67+
case PIO_SERCOM_ALT:
68+
case PIO_TIMER:
69+
case PIO_TIMER_ALT:
70+
case PIO_EXTINT:
71+
case PIO_COM:
72+
case PIO_AC_CLK:
73+
#if 0
74+
// Is the pio pin in the lower 16 ones?
75+
// The WRCONFIG register allows update of only 16 pin max out of 32
76+
if ( g_APinDescription[ulPin].ulPin < 16 )
77+
{
78+
PORT->Group[g_APinDescription[ulPin].ulPort].WRCONFIG.reg = PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_PMUXEN | PORT_WRCONFIG_PMUX( ulPeripheral ) |
79+
PORT_WRCONFIG_WRPINCFG |
80+
PORT_WRCONFIG_PINMASK( g_APinDescription[ulPin].ulPin ) ;
81+
}
82+
else
83+
{
84+
PORT->Group[g_APinDescription[ulPin].ulPort].WRCONFIG.reg = PORT_WRCONFIG_HWSEL |
85+
PORT_WRCONFIG_WRPMUX | PORT_WRCONFIG_PMUXEN | PORT_WRCONFIG_PMUX( ulPeripheral ) |
86+
PORT_WRCONFIG_WRPINCFG |
87+
PORT_WRCONFIG_PINMASK( g_APinDescription[ulPin].ulPin - 16 ) ;
88+
}
89+
#else
90+
if ( g_APinDescription[ulPin].ulPin & 1 ) // is pin odd?
91+
{
92+
uint32_t temp ;
93+
94+
// Get whole current setup for both odd and even pins and remove odd one
95+
temp = (PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg) & PORT_PMUX_PMUXE( 0xF ) ;
96+
// Set new muxing
97+
PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg = temp|PORT_PMUX_PMUXO( ulPeripheral ) ;
98+
// Enable port mux
99+
PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg |= PORT_PINCFG_PMUXEN ;
100+
}
101+
else // even pin
102+
{
103+
uint32_t temp ;
104+
105+
temp = (PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg) & PORT_PMUX_PMUXO( 0xF ) ;
106+
PORT->Group[g_APinDescription[ulPin].ulPort].PMUX[g_APinDescription[ulPin].ulPin >> 1].reg = temp|PORT_PMUX_PMUXE( ulPeripheral ) ;
107+
PORT->Group[g_APinDescription[ulPin].ulPort].PINCFG[g_APinDescription[ulPin].ulPin].reg |= PORT_PINCFG_PMUXEN ; // Enable port mux
108+
}
109+
#endif
110+
break ;
111+
112+
case PIO_NOT_A_PIN:
113+
return -1l ;
114+
break ;
115+
}
116+
117+
return 0l ;
118+
}
119+

cores/arduino/wiring_private.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//** just starting suggestion at this point --- jcw - 9/10/20
12
/*
23
wiring_private.h
34
Part of Arduino - http://www.arduino.cc/
@@ -23,6 +24,7 @@
2324
#ifndef WiringPrivate_h
2425
#define WiringPrivate_h
2526

27+
#include <stdint.h>
2628
#include <stdio.h>
2729
#include <stdarg.h>
2830

@@ -31,11 +33,14 @@
3133
#ifdef __cplusplus
3234
extern "C"{
3335
#endif
34-
36+
#include "wiring_constants.h"
3537
typedef void (*voidFuncPtr)(void);
3638

39+
40+
int pinPeripheral( uint32_t ulPin, EPioType ulPeripheral );
41+
3742
#ifdef __cplusplus
3843
} // extern "C"
3944
#endif
40-
45+
// #include "HardwareSerial.h"
4146
#endif

0 commit comments

Comments
 (0)