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 @@
#include <string.h>
#include <math.h>
#include "nrf.h"
#include "nrf_peripherals.h"
typedef bool boolean;
typedef uint8_t byte;
typedef uint16_t word;
......@@ -92,9 +95,11 @@ void loop( void ) ;
#define bit(b) (1UL << (b))
#define digitalPinToPort(P) ( &(NRF_GPIO[P]) )
#define digitalPinToBitMask(P) ( 1 << g_ADigitalPinMap[P] )
//#define analogInPinToBit(P) ( )
#define gpioBaseForPin(P) ( NRF_GPIO )
#define digitalPinToPort(P) ( (NRF_GPIO_Type *) gpioBaseForPin(P) )
#define digitalPinToPin(P) ( g_ADigitalPinMap[P] )
#define digitalPinToBitMask(P) ( 1 << digitalPinToPin(P) )
#define portOutputRegister(port) ( &(port->OUTSET) )
#define portInputRegister(port) ( &(port->IN) )
#define portModeRegister(port) ( &(port->DIRSET) )
......
......@@ -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
// digitalRead() instead yields much coarser resolution.
// 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;
// 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)
// count low-level loops during the pulse (or until maxLoops)
// 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
// 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 )
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'
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)
| ((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);
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_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
NRF_GPIO->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);
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_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
NRF_GPIO->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);
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_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
NRF_GPIO->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);
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_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:
......@@ -84,16 +85,17 @@ void digitalWrite( uint32_t ulPin, uint32_t ulVal )
return;
}
ulPin = g_ADigitalPinMap[ulPin];
NRF_GPIO_Type* port = digitalPinToPort(ulPin);
uint32_t mask = digitalPinToBitMask(ulPin);
switch ( ulVal )
{
case LOW:
NRF_GPIO->OUTCLR = (1UL << ulPin);
port->OUTCLR = mask;
break ;
default:
NRF_GPIO->OUTSET = (1UL << ulPin);
port->OUTSET = mask;
break ;
}
......@@ -106,9 +108,10 @@ int digitalRead( uint32_t ulPin )
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
......
......@@ -35,8 +35,8 @@ extern "C" {
TwoWire::TwoWire(NRF_TWI_Type * p_twi, uint8_t pinSDA, uint8_t pinSCL)
{
this->_p_twi = p_twi;
this->_uc_pinSDA = g_ADigitalPinMap[pinSDA];
this->_uc_pinSCL = g_ADigitalPinMap[pinSCL];
this->_uc_pinSDA = pinSDA;
this->_uc_pinSCL = pinSCL;
this->transmissionBegun = false;
this->suspended = false;
}
......@@ -44,8 +44,8 @@ TwoWire::TwoWire(NRF_TWI_Type * p_twi, uint8_t pinSDA, uint8_t pinSCL)
#ifdef ARDUINO_GENERIC
void TwoWire::setPins(uint8_t pinSDA, uint8_t pinSCL)
{
this->_uc_pinSDA = g_ADigitalPinMap[pinSDA];
this->_uc_pinSCL = g_ADigitalPinMap[pinSCL];
this->_uc_pinSDA = pinSDA;
this->_uc_pinSCL = pinSCL;
}
#endif // ARDUINO_GENERIC
......@@ -53,22 +53,27 @@ void TwoWire::begin(void) {
//Master Mode
master = true;
NRF_GPIO->PIN_CNF[_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);
NRF_GPIO_Type* portSCL = digitalPinToPort(_uc_pinSCL);
NRF_GPIO_Type* portSDA = digitalPinToPort(_uc_pinSDA);
uint32_t pinSCL = digitalPinToPin(_uc_pinSCL);
uint32_t pinSDA = digitalPinToPin(_uc_pinSDA);
NRF_GPIO->PIN_CNF[_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);
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_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);
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->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos);
_p_twi->PSELSCL = _uc_pinSCL;
_p_twi->PSELSDA = _uc_pinSDA;
_p_twi->PSELSCL = g_ADigitalPinMap[_uc_pinSCL];
_p_twi->PSELSDA = g_ADigitalPinMap[_uc_pinSDA];
}
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,
this->_p_twim = p_twim;
this->_p_twis = p_twis;
this->_IRQn = IRQn;
this->_uc_pinSDA = g_ADigitalPinMap[pinSDA];
this->_uc_pinSCL = g_ADigitalPinMap[pinSCL];
this->_uc_pinSDA = pinSDA;
this->_uc_pinSCL = pinSCL;
transmissionBegun = false;
}
#ifdef ARDUINO_GENERIC
void TwoWire::setPins(uint8_t pinSDA, uint8_t pinSCL)
{
this->_uc_pinSDA = g_ADigitalPinMap[pinSDA];
this->_uc_pinSCL = g_ADigitalPinMap[pinSCL];
this->_uc_pinSDA = pinSDA;
this->_uc_pinSCL = pinSCL;
}
#endif // ARDUINO_GENERIC
......@@ -53,22 +53,27 @@ void TwoWire::begin(void) {
//Master Mode
master = true;
NRF_GPIO->PIN_CNF[_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);
NRF_GPIO_Type* portSCL = digitalPinToPort(_uc_pinSCL);
NRF_GPIO_Type* portSDA = digitalPinToPort(_uc_pinSDA);
uint32_t pinSCL = digitalPinToPin(_uc_pinSCL);
uint32_t pinSDA = digitalPinToPin(_uc_pinSDA);
NRF_GPIO->PIN_CNF[_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);
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_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);
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->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
_p_twim->PSEL.SCL = _uc_pinSCL;
_p_twim->PSEL.SDA = _uc_pinSDA;
_p_twim->PSEL.SCL = g_ADigitalPinMap[_uc_pinSCL];
_p_twim->PSEL.SDA = g_ADigitalPinMap[_uc_pinSDA];
NVIC_ClearPendingIRQ(_IRQn);
NVIC_SetPriority(_IRQn, 2);
......@@ -93,8 +98,8 @@ void TwoWire::begin(uint8_t address) {
_p_twis->ADDRESS[0] = address;
_p_twis->CONFIG = TWIS_CONFIG_ADDRESS0_Msk;
_p_twis->PSEL.SCL = _uc_pinSCL;
_p_twis->PSEL.SDA = _uc_pinSDA;
_p_twis->PSEL.SCL = g_ADigitalPinMap[_uc_pinSCL];
_p_twis->PSEL.SDA = g_ADigitalPinMap[_uc_pinSDA];
_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