Commit a9ca22cd authored by Sandeep Mistry's avatar Sandeep Mistry

Correct + use digitalPinToPort, digitalPinToPin, digitalPinToPin macros

for pulseIn, pinMode, digitalWrite, digitalRead, Wire
parent 7da8ba75
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include "nrf.h"
#include "nrf_peripherals.h"
typedef bool boolean; typedef bool boolean;
typedef uint8_t byte; typedef uint8_t byte;
typedef uint16_t word; typedef uint16_t word;
...@@ -92,9 +95,11 @@ void loop( void ) ; ...@@ -92,9 +95,11 @@ void loop( void ) ;
#define bit(b) (1UL << (b)) #define bit(b) (1UL << (b))
#define digitalPinToPort(P) ( &(NRF_GPIO[P]) ) #define gpioBaseForPin(P) ( NRF_GPIO )
#define digitalPinToBitMask(P) ( 1 << g_ADigitalPinMap[P] ) #define digitalPinToPort(P) ( (NRF_GPIO_Type *) gpioBaseForPin(P) )
//#define analogInPinToBit(P) ( ) #define digitalPinToPin(P) ( g_ADigitalPinMap[P] )
#define digitalPinToBitMask(P) ( 1 << digitalPinToPin(P) )
#define portOutputRegister(port) ( &(port->OUTSET) ) #define portOutputRegister(port) ( &(port->OUTSET) )
#define portInputRegister(port) ( &(port->IN) ) #define portInputRegister(port) ( &(port->IN) )
#define portModeRegister(port) ( &(port->DIRSET) ) #define portModeRegister(port) ( &(port->DIRSET) )
......
...@@ -33,7 +33,8 @@ uint32_t pulseIn(uint32_t ulPin, uint32_t state, uint32_t timeout) ...@@ -33,7 +33,8 @@ uint32_t pulseIn(uint32_t ulPin, uint32_t state, uint32_t timeout)
// pulse width measuring loop and achieve finer resolution. calling // pulse width measuring loop and achieve finer resolution. calling
// digitalRead() instead yields much coarser resolution. // digitalRead() instead yields much coarser resolution.
// PinDescription p = g_APinDescription[pin]; // PinDescription p = g_APinDescription[pin];
uint32_t bit = 1 << digitalPinToPin(ulPin); NRF_GPIO_Type* port = digitalPinToPort(ulPin);
uint32_t bit = digitalPinToBitMask(ulPin);
uint32_t stateMask = state ? bit : 0; uint32_t stateMask = state ? bit : 0;
// convert the timeout from microseconds to a number of times through // convert the timeout from microseconds to a number of times through
...@@ -42,7 +43,7 @@ uint32_t pulseIn(uint32_t ulPin, uint32_t state, uint32_t timeout) ...@@ -42,7 +43,7 @@ uint32_t pulseIn(uint32_t ulPin, uint32_t state, uint32_t timeout)
// count low-level loops during the pulse (or until maxLoops) // count low-level loops during the pulse (or until maxLoops)
// a zero loopCount means that a complete pulse was not detected within the timeout // a zero loopCount means that a complete pulse was not detected within the timeout
uint32_t loopCount = countPulseASM(&(NRF_GPIO->IN), bit, stateMask, maxloops); uint32_t loopCount = countPulseASM(&(port->IN), bit, stateMask, maxloops);
// convert the reading to (approximate) microseconds. The loop time as measured with an // convert the reading to (approximate) microseconds. The loop time as measured with an
// oscilloscope is 10 cycles on a BBC micro:bit 1.3 (nRF51822). There is error because the // oscilloscope is 10 cycles on a BBC micro:bit 1.3 (nRF51822). There is error because the
......
...@@ -31,45 +31,46 @@ void pinMode( uint32_t ulPin, uint32_t ulMode ) ...@@ -31,45 +31,46 @@ void pinMode( uint32_t ulPin, uint32_t ulMode )
return; return;
} }
ulPin = g_ADigitalPinMap[ulPin]; NRF_GPIO_Type* port = digitalPinToPort(ulPin);
uint32_t pin = digitalPinToPin(ulPin);
// Set pin mode according to chapter '22.6.3 I/O Pin Configuration' // Set pin mode according to chapter '22.6.3 I/O Pin Configuration'
switch ( ulMode ) switch ( ulMode )
{ {
case INPUT: case INPUT:
// Set pin to input mode // 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[pin] = ((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_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_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_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
break ; break ;
case INPUT_PULLUP: case INPUT_PULLUP:
// Set pin to input mode with pull-up resistor enabled // 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[pin] = ((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_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_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_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
break ; break ;
case INPUT_PULLDOWN: case INPUT_PULLDOWN:
// Set pin to input mode with pull-down resistor enabled // 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[pin] = ((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_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)GPIO_PIN_CNF_PULL_Pulldown << GPIO_PIN_CNF_PULL_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_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
break ; break ;
case OUTPUT: case OUTPUT:
// Set pin to output mode // 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[pin] = ((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_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_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_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
break ; break ;
default: default:
...@@ -84,16 +85,17 @@ void digitalWrite( uint32_t ulPin, uint32_t ulVal ) ...@@ -84,16 +85,17 @@ void digitalWrite( uint32_t ulPin, uint32_t ulVal )
return; return;
} }
ulPin = g_ADigitalPinMap[ulPin]; NRF_GPIO_Type* port = digitalPinToPort(ulPin);
uint32_t mask = digitalPinToBitMask(ulPin);
switch ( ulVal ) switch ( ulVal )
{ {
case LOW: case LOW:
NRF_GPIO->OUTCLR = (1UL << ulPin); port->OUTCLR = mask;
break ; break ;
default: default:
NRF_GPIO->OUTSET = (1UL << ulPin); port->OUTSET = mask;
break ; break ;
} }
...@@ -106,9 +108,10 @@ int digitalRead( uint32_t ulPin ) ...@@ -106,9 +108,10 @@ int digitalRead( uint32_t ulPin )
return 0; return 0;
} }
ulPin = g_ADigitalPinMap[ulPin]; NRF_GPIO_Type* port = digitalPinToPort(ulPin);
uint32_t pin = digitalPinToPin(ulPin);
return ((NRF_GPIO->IN >> ulPin) & 1UL) ? HIGH : LOW ; return ((port->IN >> pin) & 1UL) ? HIGH : LOW ;
} }
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -35,8 +35,8 @@ extern "C" { ...@@ -35,8 +35,8 @@ extern "C" {
TwoWire::TwoWire(NRF_TWI_Type * p_twi, uint8_t pinSDA, uint8_t pinSCL) TwoWire::TwoWire(NRF_TWI_Type * p_twi, uint8_t pinSDA, uint8_t pinSCL)
{ {
this->_p_twi = p_twi; this->_p_twi = p_twi;
this->_uc_pinSDA = g_ADigitalPinMap[pinSDA]; this->_uc_pinSDA = pinSDA;
this->_uc_pinSCL = g_ADigitalPinMap[pinSCL]; this->_uc_pinSCL = pinSCL;
this->transmissionBegun = false; this->transmissionBegun = false;
this->suspended = false; this->suspended = false;
} }
...@@ -44,8 +44,8 @@ TwoWire::TwoWire(NRF_TWI_Type * p_twi, uint8_t pinSDA, uint8_t pinSCL) ...@@ -44,8 +44,8 @@ TwoWire::TwoWire(NRF_TWI_Type * p_twi, uint8_t pinSDA, uint8_t pinSCL)
#ifdef ARDUINO_GENERIC #ifdef ARDUINO_GENERIC
void TwoWire::setPins(uint8_t pinSDA, uint8_t pinSCL) void TwoWire::setPins(uint8_t pinSDA, uint8_t pinSCL)
{ {
this->_uc_pinSDA = g_ADigitalPinMap[pinSDA]; this->_uc_pinSDA = pinSDA;
this->_uc_pinSCL = g_ADigitalPinMap[pinSCL]; this->_uc_pinSCL = pinSCL;
} }
#endif // ARDUINO_GENERIC #endif // ARDUINO_GENERIC
...@@ -53,22 +53,27 @@ void TwoWire::begin(void) { ...@@ -53,22 +53,27 @@ void TwoWire::begin(void) {
//Master Mode //Master Mode
master = true; master = true;
NRF_GPIO->PIN_CNF[_uc_pinSCL] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) NRF_GPIO_Type* portSCL = digitalPinToPort(_uc_pinSCL);
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) NRF_GPIO_Type* portSDA = digitalPinToPort(_uc_pinSDA);
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) uint32_t pinSCL = digitalPinToPin(_uc_pinSCL);
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) uint32_t pinSDA = digitalPinToPin(_uc_pinSDA);
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
NRF_GPIO->PIN_CNF[_uc_pinSDA] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) portSCL->PIN_CNF[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_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_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_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
portSDA->PIN_CNF[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_twi->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos); _p_twi->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos);
_p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos); _p_twi->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
_p_twi->PSELSCL = _uc_pinSCL; _p_twi->PSELSCL = g_ADigitalPinMap[_uc_pinSCL];
_p_twi->PSELSDA = _uc_pinSDA; _p_twi->PSELSDA = g_ADigitalPinMap[_uc_pinSDA];
} }
void TwoWire::setClock(uint32_t baudrate) { void TwoWire::setClock(uint32_t baudrate) {
......
...@@ -36,16 +36,16 @@ TwoWire::TwoWire(NRF_TWIM_Type * p_twim, NRF_TWIS_Type * p_twis, IRQn_Type IRQn, ...@@ -36,16 +36,16 @@ TwoWire::TwoWire(NRF_TWIM_Type * p_twim, NRF_TWIS_Type * p_twis, IRQn_Type IRQn,
this->_p_twim = p_twim; this->_p_twim = p_twim;
this->_p_twis = p_twis; this->_p_twis = p_twis;
this->_IRQn = IRQn; this->_IRQn = IRQn;
this->_uc_pinSDA = g_ADigitalPinMap[pinSDA]; this->_uc_pinSDA = pinSDA;
this->_uc_pinSCL = g_ADigitalPinMap[pinSCL]; this->_uc_pinSCL = pinSCL;
transmissionBegun = false; transmissionBegun = false;
} }
#ifdef ARDUINO_GENERIC #ifdef ARDUINO_GENERIC
void TwoWire::setPins(uint8_t pinSDA, uint8_t pinSCL) void TwoWire::setPins(uint8_t pinSDA, uint8_t pinSCL)
{ {
this->_uc_pinSDA = g_ADigitalPinMap[pinSDA]; this->_uc_pinSDA = pinSDA;
this->_uc_pinSCL = g_ADigitalPinMap[pinSCL]; this->_uc_pinSCL = pinSCL;
} }
#endif // ARDUINO_GENERIC #endif // ARDUINO_GENERIC
...@@ -53,22 +53,27 @@ void TwoWire::begin(void) { ...@@ -53,22 +53,27 @@ void TwoWire::begin(void) {
//Master Mode //Master Mode
master = true; master = true;
NRF_GPIO->PIN_CNF[_uc_pinSCL] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) NRF_GPIO_Type* portSCL = digitalPinToPort(_uc_pinSCL);
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) NRF_GPIO_Type* portSDA = digitalPinToPort(_uc_pinSDA);
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) uint32_t pinSCL = digitalPinToPin(_uc_pinSCL);
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) uint32_t pinSDA = digitalPinToPin(_uc_pinSDA);
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
NRF_GPIO->PIN_CNF[_uc_pinSDA] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) portSCL->PIN_CNF[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_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_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_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos); | ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
portSDA->PIN_CNF[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->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K100;
_p_twim->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos); _p_twim->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
_p_twim->PSEL.SCL = _uc_pinSCL; _p_twim->PSEL.SCL = g_ADigitalPinMap[_uc_pinSCL];
_p_twim->PSEL.SDA = _uc_pinSDA; _p_twim->PSEL.SDA = g_ADigitalPinMap[_uc_pinSDA];
NVIC_ClearPendingIRQ(_IRQn); NVIC_ClearPendingIRQ(_IRQn);
NVIC_SetPriority(_IRQn, 2); NVIC_SetPriority(_IRQn, 2);
...@@ -93,8 +98,8 @@ void TwoWire::begin(uint8_t address) { ...@@ -93,8 +98,8 @@ void TwoWire::begin(uint8_t address) {
_p_twis->ADDRESS[0] = address; _p_twis->ADDRESS[0] = address;
_p_twis->CONFIG = TWIS_CONFIG_ADDRESS0_Msk; _p_twis->CONFIG = TWIS_CONFIG_ADDRESS0_Msk;
_p_twis->PSEL.SCL = _uc_pinSCL; _p_twis->PSEL.SCL = g_ADigitalPinMap[_uc_pinSCL];
_p_twis->PSEL.SDA = _uc_pinSDA; _p_twis->PSEL.SDA = g_ADigitalPinMap[_uc_pinSDA];
_p_twis->ORC = 0xff; _p_twis->ORC = 0xff;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment