From 1064e73ac9dfe634ce2c2ba687c815145dda488e Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 24 Nov 2018 00:27:01 +0700 Subject: [PATCH] correct pin mapping --- cores/nRF5/Arduino.h | 10 ++---- cores/nRF5/WVariant.h | 1 + cores/nRF5/wiring.c | 10 ++++-- cores/nRF5/wiring_digital.c | 66 ++++++++++++++++++----------------- libraries/Wire/Wire_nRF52.cpp | 46 +++++++++++++----------- 5 files changed, 72 insertions(+), 61 deletions(-) diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index c14a5856..fb068535 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -114,21 +114,17 @@ uint32_t setLoopStacksize(void); #define bit(b) (1UL << (b)) #ifdef NRF_P1 -#define digitalPinToPort(P) ( ((P) < 32) ? NRF_P0 : NRF_P1 ) +#define digitalPinToPort(P) ( (g_ADigitalPinMap[P] < 32) ? NRF_P0 : NRF_P1 ) #else #define digitalPinToPort(P) ( NRF_P0 ) #endif -#define digitalPinToBitMask(P) ( 1UL << ( (P) < 32 ? (P) : ((P)-32) ) ) +#define digitalPinToBitMask(P) ( 1UL << ( g_ADigitalPinMap[P] < 32 ? g_ADigitalPinMap[P] : (g_ADigitalPinMap[P]-32) ) ) //#define analogInPinToBit(P) ( ) #define portOutputRegister(port) ( &(port->OUT) ) #define portInputRegister(port) ( (volatile uint32_t*) &(port->IN) ) #define portModeRegister(port) ( &(port->DIR) ) - -#define digitalPinHasPWM(P) ( (P) > 1 ) - -// PIN_CNF for nrf52 -#define digitalPinCnfRegister(P) ( &(digitalPinToPort(P)->PIN_CNF[(P) < 32 ? (P): ((P)-32)]) ) +#define digitalPinHasPWM(P) ( g_ADigitalPinMap[P] > 1 ) void rtos_idle_callback(void) ATTR_WEAK; /* diff --git a/cores/nRF5/WVariant.h b/cores/nRF5/WVariant.h index 61ce293d..48784355 100644 --- a/cores/nRF5/WVariant.h +++ b/cores/nRF5/WVariant.h @@ -23,6 +23,7 @@ #include "nrf.h" #include "nrf_soc.h" #include "nrf_sdm.h" +#include "nrf_gpio.h" #ifdef __cplusplus extern "C" { diff --git a/cores/nRF5/wiring.c b/cores/nRF5/wiring.c index 351d1ab3..3a939e44 100644 --- a/cores/nRF5/wiring.c +++ b/cores/nRF5/wiring.c @@ -120,9 +120,15 @@ void systemOff(uint32_t pin, uint8_t wake_logic) // NRF_POWER->RAM[i].POWERCLR = 0x03UL; // } - pinMode(pin, wake_logic ? INPUT_PULLDOWN : INPUT_PULLUP); + pin = g_ADigitalPinMap[pin]; - *digitalPinCnfRegister(pin) |= ((wake_logic ? GPIO_PIN_CNF_SENSE_High : GPIO_PIN_CNF_SENSE_Low) << GPIO_PIN_CNF_SENSE_Pos); + if ( wake_logic ) + { + nrf_gpio_cfg_sense_input(pin, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH); + }else + { + nrf_gpio_cfg_sense_input(pin, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); + } uint8_t sd_en; (void) sd_softdevice_is_enabled(&sd_en); diff --git a/cores/nRF5/wiring_digital.c b/cores/nRF5/wiring_digital.c index 42e0e774..ba435c40 100644 --- a/cores/nRF5/wiring_digital.c +++ b/cores/nRF5/wiring_digital.c @@ -33,44 +33,46 @@ void pinMode( uint32_t ulPin, uint32_t ulMode ) ulPin = g_ADigitalPinMap[ulPin]; + NRF_GPIO_Type * port = nrf_gpio_pin_port_decode(&ulPin); + // Set pin mode according to chapter '22.6.3 I/O Pin Configuration' switch ( ulMode ) { case INPUT: // Set pin to input mode - *digitalPinCnfRegister(ulPin) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); - break ; + port->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) + | ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) + | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) + | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); + break; case INPUT_PULLUP: // Set pin to input mode with pull-up resistor enabled - *digitalPinCnfRegister(ulPin) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); - break ; + port->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) + | ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) + | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) + | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); + break; case INPUT_PULLDOWN: // Set pin to input mode with pull-down resistor enabled - *digitalPinCnfRegister(ulPin) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); - break ; + port->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) + | ((uint32_t)GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_Pos) + | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) + | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); + break; case OUTPUT: // Set pin to output mode - *digitalPinCnfRegister(ulPin) = ((uint32_t)GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); - break ; + port->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos) + | ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) + | ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) + | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) + | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); + break; default: // do nothing @@ -85,16 +87,17 @@ void digitalWrite( uint32_t ulPin, uint32_t ulVal ) } ulPin = g_ADigitalPinMap[ulPin]; - NRF_GPIO_Type* nrf_port = (NRF_GPIO_Type*) digitalPinToPort(ulPin); + + NRF_GPIO_Type * port = nrf_gpio_pin_port_decode(&ulPin); switch ( ulVal ) { case LOW: - nrf_port->OUTCLR = digitalPinToBitMask(ulPin); + port->OUTCLR = (1UL << ulPin); break ; default: - nrf_port->OUTSET = digitalPinToBitMask(ulPin); + port->OUTSET = (1UL << ulPin); break ; } } @@ -107,12 +110,11 @@ int digitalRead( uint32_t ulPin ) ulPin = g_ADigitalPinMap[ulPin]; + NRF_GPIO_Type * port = nrf_gpio_pin_port_decode(&ulPin); + uint32_t const bm = (1UL << ulPin); + // Return bit in OUT or IN depending on configured direction - NRF_GPIO_Type* nrf_port = (NRF_GPIO_Type*) digitalPinToPort(ulPin); - - if ( ulPin > 32 ) ulPin -= 32; - - return bitRead(nrf_port->DIR, ulPin) ? bitRead(nrf_port->OUT, ulPin) : bitRead(nrf_port->IN, ulPin); + return (bm & ((port->DIR & bm) ? port->OUT : port->IN)) ? 1 : 0; } void digitalToggle( uint32_t pin ) diff --git a/libraries/Wire/Wire_nRF52.cpp b/libraries/Wire/Wire_nRF52.cpp index f840d273..fa5666e4 100755 --- a/libraries/Wire/Wire_nRF52.cpp +++ b/libraries/Wire/Wire_nRF52.cpp @@ -29,6 +29,12 @@ extern "C" { #include "Wire.h" +static volatile uint32_t* pincfg_reg(uint32_t pin) +{ + NRF_GPIO_Type * port = nrf_gpio_pin_port_decode(&pin); + return &port->PIN_CNF[pin]; +} + TwoWire::TwoWire(NRF_TWIM_Type * p_twim, NRF_TWIS_Type * p_twis, IRQn_Type IRQn, uint8_t pinSDA, uint8_t pinSCL) { this->_p_twim = p_twim; @@ -43,17 +49,17 @@ void TwoWire::begin(void) { //Master Mode master = true; - *digitalPinCnfRegister(_uc_pinSCL) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); + *pincfg_reg(_uc_pinSCL) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) + | ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) + | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) + | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); - *digitalPinCnfRegister(_uc_pinSDA) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); + *pincfg_reg(_uc_pinSDA) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + | ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) + | ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) + | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) + | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); _p_twim->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K100; _p_twim->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos); @@ -69,17 +75,17 @@ void TwoWire::begin(uint8_t address) { //Slave mode master = false; - *digitalPinCnfRegister(_uc_pinSCL) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); + *pincfg_reg(_uc_pinSCL) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + | ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) + | ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) + | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) + | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); - *digitalPinCnfRegister(_uc_pinSDA) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) - | ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) - | ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) - | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) - | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); + *pincfg_reg(_uc_pinSDA) = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + | ((uint32_t)GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) + | ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) + | ((uint32_t)GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) + | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); _p_twis->ADDRESS[0] = address; _p_twis->CONFIG = TWIS_CONFIG_ADDRESS0_Msk;