aboutsummaryrefslogtreecommitdiffstats
path: root/arduino/cores/nRF5/wiring_digital.c
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2019-02-28 17:04:22 -0500
committerClyne Sullivan <tullivan99@gmail.com>2019-02-28 17:04:22 -0500
commitd6869d1ec4bd24cd2c3eafa534f0849b25ec5607 (patch)
tree79e54ed27b39c31864895535d11399708d5a45c0 /arduino/cores/nRF5/wiring_digital.c
parent614ee97bf3a2270c413527a7f35c54cbecd9e601 (diff)
added basic code
Diffstat (limited to 'arduino/cores/nRF5/wiring_digital.c')
-rwxr-xr-xarduino/cores/nRF5/wiring_digital.c139
1 files changed, 139 insertions, 0 deletions
diff --git a/arduino/cores/nRF5/wiring_digital.c b/arduino/cores/nRF5/wiring_digital.c
new file mode 100755
index 0000000..ba435c4
--- /dev/null
+++ b/arduino/cores/nRF5/wiring_digital.c
@@ -0,0 +1,139 @@
+/*
+ Copyright (c) 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 "nrf.h"
+
+#include "Arduino.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void pinMode( uint32_t ulPin, uint32_t ulMode )
+{
+ if (ulPin >= PINS_COUNT) {
+ return;
+ }
+
+ 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
+ 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
+ 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
+ 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
+ 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
+ break ;
+ }
+}
+
+void digitalWrite( uint32_t ulPin, uint32_t ulVal )
+{
+ if (ulPin >= PINS_COUNT) {
+ return;
+ }
+
+ ulPin = g_ADigitalPinMap[ulPin];
+
+ NRF_GPIO_Type * port = nrf_gpio_pin_port_decode(&ulPin);
+
+ switch ( ulVal )
+ {
+ case LOW:
+ port->OUTCLR = (1UL << ulPin);
+ break ;
+
+ default:
+ port->OUTSET = (1UL << ulPin);
+ break ;
+ }
+}
+
+int digitalRead( uint32_t ulPin )
+{
+ if (ulPin >= PINS_COUNT) {
+ return 0;
+ }
+
+ 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
+ return (bm & ((port->DIR & bm) ? port->OUT : port->IN)) ? 1 : 0;
+}
+
+void digitalToggle( uint32_t pin )
+{
+ int state = 1 - digitalRead(pin);
+ digitalWrite(pin, state);
+}
+
+void ledOn(uint32_t pin)
+{
+ digitalWrite(pin, LED_STATE_ON);
+}
+
+void ledOff(uint32_t pin)
+{
+ digitalWrite(pin, 1-LED_STATE_ON);
+}
+
+
+#ifdef __cplusplus
+}
+#endif