From 66395d80b568a12b88838c72080c035e00dee71d Mon Sep 17 00:00:00 2001 From: Ziehe Enrico Date: Tue, 20 Aug 2019 00:51:11 +0200 Subject: [PATCH 1/3] fix GPIO Port 1 --- cores/nRF5/Arduino.h | 4 ++++ cores/nRF5/SDK/components/device/nrf52.h | 2 ++ cores/nRF5/wiring_digital.c | 23 ++++++++++++++++------- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index 39fbc0fe..b622522d 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -92,7 +92,11 @@ void loop( void ) ; #define bit(b) (1UL << (b)) +#ifdef NRF52 +#define digitalPinToPort(P) ((g_ADigitalPinMap[P] < 32 ? NRF_P0 : NRF_P1)) +#else #define digitalPinToPort(P) ( &(NRF_GPIO[P]) ) +#endif #define digitalPinToBitMask(P) ( 1 << g_ADigitalPinMap[P] ) //#define analogInPinToBit(P) ( ) #define portOutputRegister(port) ( &(port->OUTSET) ) diff --git a/cores/nRF5/SDK/components/device/nrf52.h b/cores/nRF5/SDK/components/device/nrf52.h index 412ecef0..438f9572 100755 --- a/cores/nRF5/SDK/components/device/nrf52.h +++ b/cores/nRF5/SDK/components/device/nrf52.h @@ -2038,6 +2038,7 @@ typedef struct { /*!< GPIO Structure #define NRF_I2S_BASE 0x40025000UL #define NRF_FPU_BASE 0x40026000UL #define NRF_P0_BASE 0x50000000UL +#define NRF_P1_BASE 0x50000300UL //NRF52840 /* ================================================================================ */ @@ -2110,6 +2111,7 @@ typedef struct { /*!< GPIO Structure #define NRF_I2S ((NRF_I2S_Type *) NRF_I2S_BASE) #define NRF_FPU ((NRF_FPU_Type *) NRF_FPU_BASE) #define NRF_P0 ((NRF_GPIO_Type *) NRF_P0_BASE) +#define NRF_P1 ((NRF_GPIO_Type *) NRF_P1_BASE) /** @} */ /* End of group Device_Peripheral_Registers */ diff --git a/cores/nRF5/wiring_digital.c b/cores/nRF5/wiring_digital.c index 1d9c27fd..20f11b02 100644 --- a/cores/nRF5/wiring_digital.c +++ b/cores/nRF5/wiring_digital.c @@ -33,12 +33,15 @@ void pinMode( uint32_t ulPin, uint32_t ulMode ) ulPin = g_ADigitalPinMap[ulPin]; + NRF_GPIO_Type * port = digitalPinToPort(&ulPin); + if(ulPin > 32) ulPin -=32; + // Set pin mode according to chapter '22.6.3 I/O Pin Configuration' switch ( ulMode ) { case INPUT: // Set pin to input mode - NRF_GPIO->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + 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) @@ -47,7 +50,7 @@ void pinMode( uint32_t ulPin, uint32_t ulMode ) case INPUT_PULLUP: // Set pin to input mode with pull-up resistor enabled - NRF_GPIO->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + 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) @@ -56,7 +59,7 @@ void pinMode( uint32_t ulPin, uint32_t ulMode ) case INPUT_PULLDOWN: // Set pin to input mode with pull-down resistor enabled - NRF_GPIO->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) + 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) @@ -65,7 +68,7 @@ void pinMode( uint32_t ulPin, uint32_t ulMode ) case OUTPUT: // Set pin to output mode - NRF_GPIO->PIN_CNF[ulPin] = ((uint32_t)GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos) + 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) @@ -86,14 +89,17 @@ void digitalWrite( uint32_t ulPin, uint32_t ulVal ) ulPin = g_ADigitalPinMap[ulPin]; + NRF_GPIO_Type * port = digitalPinToPort(&ulPin); + if(ulPin > 32) ulPin -=32; + switch ( ulVal ) { case LOW: - NRF_GPIO->OUTCLR = (1UL << ulPin); + port->OUTCLR = (1UL << ulPin); break ; default: - NRF_GPIO->OUTSET = (1UL << ulPin); + port->OUTSET = (1UL << ulPin); break ; } @@ -108,7 +114,10 @@ int digitalRead( uint32_t ulPin ) ulPin = g_ADigitalPinMap[ulPin]; - return ((NRF_GPIO->IN >> ulPin) & 1UL) ? HIGH : LOW ; + NRF_GPIO_Type * port = digitalPinToPort(&ulPin); + if(ulPin > 32) ulPin -=32; + + return ((port->IN >> ulPin) & 1UL) ? HIGH : LOW ; } #ifdef __cplusplus From 594ab927591a9fe56d47a0272e38a04f6f1acdea Mon Sep 17 00:00:00 2001 From: Ziehe Enrico Date: Tue, 20 Aug 2019 00:54:00 +0200 Subject: [PATCH 2/3] add testboard --- variants/miMotion52480/pins_arduino.h | 17 ++++ variants/miMotion52480/readme.txt | 9 ++ variants/miMotion52480/variant.cpp | 71 ++++++++++++++++ variants/miMotion52480/variant.h | 117 ++++++++++++++++++++++++++ 4 files changed, 214 insertions(+) create mode 100644 variants/miMotion52480/pins_arduino.h create mode 100644 variants/miMotion52480/readme.txt create mode 100644 variants/miMotion52480/variant.cpp create mode 100644 variants/miMotion52480/variant.h diff --git a/variants/miMotion52480/pins_arduino.h b/variants/miMotion52480/pins_arduino.h new file mode 100644 index 00000000..3ef4d4a9 --- /dev/null +++ b/variants/miMotion52480/pins_arduino.h @@ -0,0 +1,17 @@ +/* + Copyright (c) 2014-2015 Arduino LLC. All right reserved. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +// API compatibility +#include "variant.h" diff --git a/variants/miMotion52480/readme.txt b/variants/miMotion52480/readme.txt new file mode 100644 index 00000000..78fdf6ff --- /dev/null +++ b/variants/miMotion52480/readme.txt @@ -0,0 +1,9 @@ + +This contains the Variant definition for Milight Motion on NRF52840 Board. + + +This directory has to be copied to your platformio Packages variants directory. +"c:\user\xxx\.platformio\packages\framework-arduinonordicnrf5\variants" + +make sure the dir and filenames are not changed. + diff --git a/variants/miMotion52480/variant.cpp b/variants/miMotion52480/variant.cpp new file mode 100644 index 00000000..2092c32a --- /dev/null +++ b/variants/miMotion52480/variant.cpp @@ -0,0 +1,71 @@ +/* + Copyright (c) 2014-2015 Arduino LLC. All right reserved. + Copyright (c) 2016 Sandeep Mistry All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "variant.h" + +const uint32_t g_ADigitalPinMap[] = { + 0, //P0.00 + 1, //P0.01 + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, //P0.24 PIN_PIR + 25, + 26, + 27, + 28, + 29, + 30, + 31, + 32, //P1.00 + 33, + 34, + 35, + 36, //P1.04 PIN_LED1 + 37, + 38, //P1.06 PIN_LED2 / BTN_USR + 39, + 40, //P1.08 + 41, //P1.09 PIN_LED3 + 42, + 43, + 44, + 45, + 46, + 47 //P1.15 +}; diff --git a/variants/miMotion52480/variant.h b/variants/miMotion52480/variant.h new file mode 100644 index 00000000..b34838a2 --- /dev/null +++ b/variants/miMotion52480/variant.h @@ -0,0 +1,117 @@ +/* + Copyright (c) 2014-2015 Arduino LLC. All right reserved. + Copyright (c) 2016 Sandeep Mistry All right reserved. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef _VARIANT_MIMOTION_NRF52840_ +#define _VARIANT_MIMOTION_NRF52840_ + +/** Master clock frequency */ +#ifdef NRF52 +#define VARIANT_MCK (64000000ul) +#else +#define VARIANT_MCK (16000000ul) +#endif + +/*---------------------------------------------------------------------------- + * Headers + *----------------------------------------------------------------------------*/ + +#include "WVariant.h" + +#ifdef __cplusplus +extern "C" +{ +#endif // __cplusplus + +// Number of pins defined in PinDescription array +#define PINS_COUNT (48u) +#define NUM_DIGITAL_PINS (48u) +#define NUM_ANALOG_INPUTS (6u) +#define NUM_ANALOG_OUTPUTS (0u) + +// LEDs +#define PIN_LED (13) // P0.13 +#define LED_BUILTIN PIN_LED + +#define PIN_LED1 (36) +#define PIN_LED2 (38) +#define PIN_LED3 (41) +#define PIN_LED4 (46) + +// BUTTONs +#define PIN_BUTTON1 (42) +#define PIN_BUTTON2 (43) +#define PIN_BUTTON3 (44) +#define PIN_BUTTON4 (45) + +/* + * Analog pins + */ +#define PIN_A0 (1) // P0.01 +#define PIN_A1 (2) // P0.02 +#define PIN_A2 (3) // P0.03 +#define PIN_A3 (4) // P0.04 +#define PIN_A4 (5) // P0.05 +#define PIN_A5 (6) // P0.06 + +static const uint8_t A0 = PIN_A0 ; +static const uint8_t A1 = PIN_A1 ; +static const uint8_t A2 = PIN_A2 ; +static const uint8_t A3 = PIN_A3 ; +static const uint8_t A4 = PIN_A4 ; +static const uint8_t A5 = PIN_A5 ; +#ifdef NRF52 +#define ADC_RESOLUTION 14 +#else +#define ADC_RESOLUTION 10 +#endif + +/* + * Serial interfaces + */ +// Serial +#define PIN_SERIAL_RX (8) // P0.00 +#define PIN_SERIAL_TX (6) // P0.01 + +/* + * SPI Interfaces + */ +#define SPI_INTERFACES_COUNT 1 + +#define PIN_SPI_MISO (22) // P0.22 +#define PIN_SPI_MOSI (23) // P0.23 +#define PIN_SPI_SCK (24) // P0.24 + +static const uint8_t SS = 25 ; // P0.25 +static const uint8_t MOSI = PIN_SPI_MOSI ; +static const uint8_t MISO = PIN_SPI_MISO ; +static const uint8_t SCK = PIN_SPI_SCK ; + +/* + * Wire Interfaces + */ +#define WIRE_INTERFACES_COUNT 1 + +#define PIN_WIRE_SDA (20u) // P0.20 +#define PIN_WIRE_SCL (21u) // P0.21 + +static const uint8_t SDA = PIN_WIRE_SDA; +static const uint8_t SCL = PIN_WIRE_SCL; + +#ifdef __cplusplus +} +#endif + +#endif From 1579866b5038958eaf48312f599a27a5477068b5 Mon Sep 17 00:00:00 2001 From: Ziehe Enrico Date: Tue, 20 Aug 2019 08:26:06 +0200 Subject: [PATCH 3/3] fix compile Error --- cores/nRF5/Arduino.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/cores/nRF5/Arduino.h b/cores/nRF5/Arduino.h index b622522d..faa10caf 100644 --- a/cores/nRF5/Arduino.h +++ b/cores/nRF5/Arduino.h @@ -92,11 +92,7 @@ void loop( void ) ; #define bit(b) (1UL << (b)) -#ifdef NRF52 -#define digitalPinToPort(P) ((g_ADigitalPinMap[P] < 32 ? NRF_P0 : NRF_P1)) -#else -#define digitalPinToPort(P) ( &(NRF_GPIO[P]) ) -#endif +#define digitalPinToPort(P) ((g_ADigitalPinMap[(uint32_t)P] < 32 ? NRF_P0 : NRF_P1)) #define digitalPinToBitMask(P) ( 1 << g_ADigitalPinMap[P] ) //#define analogInPinToBit(P) ( ) #define portOutputRegister(port) ( &(port->OUTSET) )