Skip to content

Commit bba2a74

Browse files
committed
adding nrf52840 port1 (pin > 32) support
- also remove ledToggle()
1 parent 3ab35ab commit bba2a74

File tree

10 files changed

+73
-79
lines changed

10 files changed

+73
-79
lines changed

cores/nRF5/Arduino.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,17 @@ uint32_t setLoopStacksize(void);
119119
#define digitalPinToPort(P) ( NRF_P0 )
120120
#endif
121121

122-
#define digitalPinToBitMask(P) ( 1 << (P) )
122+
#define digitalPinToBitMask(P) ( 1UL << ( (P) < 32 ? (P) : ((P)-32) ) )
123123
//#define analogInPinToBit(P) ( )
124124
#define portOutputRegister(port) ( &(port->OUT) )
125125
#define portInputRegister(port) ( (volatile uint32_t*) &(port->IN) )
126126
#define portModeRegister(port) ( &(port->DIR) )
127+
127128
#define digitalPinHasPWM(P) ( (P) > 1 )
128129

130+
// PIN_CNF for nrf52
131+
#define digitalPinCnfRegister(P) ( &(digitalPinToPort(P)->PIN_CNF[(P) < 32 ? (P): ((P)-32)]) )
132+
129133
void rtos_idle_callback(void) ATTR_WEAK;
130134
/*
131135
* digitalPinToTimer(..) is AVR-specific and is not defined for nRF52

cores/nRF5/pulse.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ uint32_t pulseIn(uint32_t pin, uint32_t state, uint32_t timeout)
3333
// pulse width measuring loop and achieve finer resolution. calling
3434
// digitalRead() instead yields much coarser resolution.
3535
// PinDescription p = g_APinDescription[pin];
36-
uint32_t bit = 1 << pin; //p.ulPin;
36+
uint32_t bit = digitalPinToBitMask(pin); //p.ulPin;
3737
uint32_t stateMask = state ? bit : 0;
3838

3939
// convert the timeout from microseconds to a number of times through
4040
// the initial loop; it takes (roughly) 13 clock cycles per iteration.
4141
uint32_t maxloops = microsecondsToClockCycles(timeout) / 13;
4242

43-
uint32_t width = countPulseASM(&(NRF_GPIO->IN), bit, stateMask, maxloops);
43+
uint32_t width = countPulseASM(portInputRegister(digitalPinToPort(pin)), bit, stateMask, maxloops);
4444

4545
// convert the reading to microseconds. The loop has been determined
4646
// to be 13 clock cycles long and have about 16 clocks between the edge

cores/nRF5/wiring.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ void init( void )
6161
NRF_RTC1->TASKS_CLEAR = 1;
6262

6363
// Make sure all pin is set HIGH when pinmode() is called
64-
NRF_GPIO->OUTSET = UINT32_MAX;
64+
NRF_P0->OUTSET = UINT32_MAX;
65+
#ifdef NRF_P1
66+
NRF_P1->OUTSET = UINT32_MAX;
67+
#endif
6568
}
6669

6770
void enterUf2Dfu(void)
@@ -117,16 +120,9 @@ void systemOff(uint32_t pin, uint8_t wake_logic)
117120
// NRF_POWER->RAM[i].POWERCLR = 0x03UL;
118121
// }
119122

120-
#if 0
121-
// pin 0 & 1 is for XTAL
122-
for(int i=2; i<PINS_COUNT; i++)
123-
{
124-
pinMode(i, INPUT);
125-
}
126-
#endif
127-
128123
pinMode(pin, wake_logic ? INPUT_PULLDOWN : INPUT_PULLUP);
129-
NRF_GPIO->PIN_CNF[pin] |= ((uint32_t) (wake_logic ? GPIO_PIN_CNF_SENSE_High : GPIO_PIN_CNF_SENSE_Low) << GPIO_PIN_CNF_SENSE_Pos);
124+
125+
*digitalPinCnfRegister(pin) |= ((wake_logic ? GPIO_PIN_CNF_SENSE_High : GPIO_PIN_CNF_SENSE_Low) << GPIO_PIN_CNF_SENSE_Pos);
130126

131127
uint8_t sd_en;
132128
(void) sd_softdevice_is_enabled(&sd_en);

cores/nRF5/wiring_digital.c

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,45 @@ void pinMode( uint32_t ulPin, uint32_t ulMode )
3131
return;
3232
}
3333

34-
ulPin = g_ADigitalPinMap[ulPin];
34+
// ulPin = g_ADigitalPinMap[ulPin];
3535

3636
// Set pin mode according to chapter '22.6.3 I/O Pin Configuration'
3737
switch ( ulMode )
3838
{
3939
case INPUT:
4040
// Set pin to input mode
41-
NRF_GPIO->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
42-
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
43-
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
44-
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
45-
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
41+
*digitalPinCnfRegister(ulPin) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
42+
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
43+
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
44+
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
45+
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
4646
break ;
4747

4848
case INPUT_PULLUP:
4949
// Set pin to input mode with pull-up resistor enabled
50-
NRF_GPIO->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
51-
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
52-
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos)
53-
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
54-
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
50+
*digitalPinCnfRegister(ulPin) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
51+
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
52+
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos)
53+
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
54+
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
5555
break ;
5656

5757
case INPUT_PULLDOWN:
5858
// Set pin to input mode with pull-down resistor enabled
59-
NRF_GPIO->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
60-
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
61-
| ((uint32_t)GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos)
62-
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
63-
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
59+
*digitalPinCnfRegister(ulPin) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
60+
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
61+
| ((uint32_t)GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos)
62+
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
63+
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
6464
break ;
6565

6666
case OUTPUT:
6767
// Set pin to output mode
68-
NRF_GPIO->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos)
69-
| ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
70-
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
71-
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
72-
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
68+
*digitalPinCnfRegister(ulPin) = ((uint32_t)GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos)
69+
| ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
70+
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
71+
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
72+
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
7373
break ;
7474

7575
default:
@@ -84,20 +84,19 @@ void digitalWrite( uint32_t ulPin, uint32_t ulVal )
8484
return;
8585
}
8686

87-
ulPin = g_ADigitalPinMap[ulPin];
87+
// ulPin = g_ADigitalPinMap[ulPin];
88+
NRF_GPIO_Type* nrf_port = (NRF_GPIO_Type*) digitalPinToPort(ulPin);
8889

8990
switch ( ulVal )
9091
{
9192
case LOW:
92-
NRF_GPIO->OUTCLR = (1UL << ulPin);
93+
nrf_port->OUTCLR = digitalPinToBitMask(ulPin);
9394
break ;
9495

9596
default:
96-
NRF_GPIO->OUTSET = (1UL << ulPin);
97+
nrf_port->OUTSET = digitalPinToBitMask(ulPin);
9798
break ;
9899
}
99-
100-
return ;
101100
}
102101

103102
int digitalRead( uint32_t ulPin )
@@ -106,17 +105,20 @@ int digitalRead( uint32_t ulPin )
106105
return 0;
107106
}
108107

109-
ulPin = g_ADigitalPinMap[ulPin];
108+
// ulPin = g_ADigitalPinMap[ulPin];
109+
110+
// Return bit in OUT or IN depending on configured direction
111+
NRF_GPIO_Type* nrf_port = (NRF_GPIO_Type*) digitalPinToPort(ulPin);
110112

111-
// Return bit in NRF_GPIO->OUT or NRF_GPIO->IN depending on configured direction
112-
return bitRead(NRF_GPIO->PIN_CNF[ulPin], GPIO_PIN_CNF_DIR_Pos) ? bitRead(NRF_GPIO->OUT, ulPin) : bitRead(NRF_GPIO->IN, ulPin);
113+
if ( ulPin > 32 ) ulPin -= 32;
114+
115+
return bitRead(nrf_port->DIR, ulPin) ? bitRead(nrf_port->OUT, ulPin) : bitRead(nrf_port->IN, ulPin);
113116
}
114117

115-
int digitalToggle( uint32_t pin )
118+
void digitalToggle( uint32_t pin )
116119
{
117-
int state = 1 - (((NRF_GPIO->OUT >> pin) & 1UL) ? HIGH : LOW);
120+
int state = 1 - digitalRead(pin);
118121
digitalWrite(pin, state);
119-
return state;
120122
}
121123

122124
void ledOn(uint32_t pin)
@@ -129,13 +131,6 @@ void ledOff(uint32_t pin)
129131
digitalWrite(pin, 1-LED_STATE_ON);
130132
}
131133

132-
int ledToggle( uint32_t pin )
133-
{
134-
int state = digitalToggle(pin);
135-
136-
return (state == LED_STATE_ON) ? 1 : 0;
137-
}
138-
139134

140135
#ifdef __cplusplus
141136
}

cores/nRF5/wiring_digital.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ extern void digitalWrite( uint32_t dwPin, uint32_t dwVal ) ;
6464
*/
6565
extern int digitalRead( uint32_t ulPin ) ;
6666

67-
extern int digitalToggle( uint32_t pin );
67+
extern void digitalToggle( uint32_t pin );
6868

6969
void ledOn(uint32_t pin);
7070
void ledOff(uint32_t pin);
71-
int ledToggle( uint32_t pin );
7271

7372
#ifdef __cplusplus
7473
}

keywords.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ setLoopStacksize KEYWORD2
2525

2626
ledOn KEYWORD2
2727
ledOff KEYWORD2
28-
ledToggle KEYWORD2
2928

3029
printf KEYWORD2
3130
printBuffer KEYWORD2

libraries/Bluefruit52Lib/examples/Hardware/blinky/blinky.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// the setup function runs once when you press reset or power the board
2121
void setup() {
2222
// initialize digital pin LED_BUILTIN as an output.
23+
pinMode(LED_BLUE, OUTPUT);
2324
}
2425

2526
// the loop function runs over and over again forever

libraries/Bluefruit52Lib/examples/Hardware/hw_systick/hw_systick.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern "C"
3333
*/
3434
void SysTick_Handler(void)
3535
{
36-
ledToggle(LED_RED);
36+
digitalToggle(LED_RED);
3737
}
3838

3939
} // extern C

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void adafruit_soc_task(void* arg);
6767
static void bluefruit_blinky_cb( TimerHandle_t xTimer )
6868
{
6969
(void) xTimer;
70-
ledToggle(LED_BLUE);
70+
digitalToggle(LED_BLUE);
7171
}
7272

7373

libraries/Wire/Wire_nRF52.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ void TwoWire::begin(void) {
4343
//Master Mode
4444
master = true;
4545

46-
NRF_GPIO->PIN_CNF[_uc_pinSCL] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
47-
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
48-
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos)
49-
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
50-
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
51-
52-
NRF_GPIO->PIN_CNF[_uc_pinSDA] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
53-
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
54-
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos)
55-
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
56-
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
46+
*digitalPinCnfRegister(_uc_pinSCL) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
47+
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
48+
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos)
49+
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
50+
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
51+
52+
*digitalPinCnfRegister(_uc_pinSDA) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
53+
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
54+
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos)
55+
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
56+
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
5757

5858
_p_twim->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K100;
5959
_p_twim->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
@@ -69,17 +69,17 @@ void TwoWire::begin(uint8_t address) {
6969
//Slave mode
7070
master = false;
7171

72-
NRF_GPIO->PIN_CNF[_uc_pinSCL] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
73-
| ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
74-
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
75-
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
76-
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
77-
78-
NRF_GPIO->PIN_CNF[_uc_pinSDA] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
79-
| ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
80-
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
81-
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
82-
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
72+
*digitalPinCnfRegister(_uc_pinSCL) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
73+
| ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
74+
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
75+
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
76+
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
77+
78+
*digitalPinCnfRegister(_uc_pinSDA) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
79+
| ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
80+
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
81+
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
82+
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
8383

8484
_p_twis->ADDRESS[0] = address;
8585
_p_twis->CONFIG = TWIS_CONFIG_ADDRESS0_Msk;

0 commit comments

Comments
 (0)