Skip to content

Commit

Permalink
Merge pull request #136 from particle-iot/feature/mesh/hal-drivers/gpio
Browse files Browse the repository at this point in the history
Feature/mesh/hal drivers/gpio
  • Loading branch information
technobly authored Jun 25, 2018
2 parents 37eddb6 + 02834e5 commit 769da09
Show file tree
Hide file tree
Showing 2 changed files with 243 additions and 40 deletions.
212 changes: 172 additions & 40 deletions hal/src/nRF52840/gpio_hal.cpp
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
/**
******************************************************************************
* @file gpio_hal.c
* @authors Matthew McGowan
* @version V1.0.0
* @date 27-Sept-2014
* @brief
******************************************************************************
Copyright (c) 2013-2015 Particle Industries, Inc. All rights 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 3 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, see <http://www.gnu.org/licenses/>.
******************************************************************************
/*
* Copyright (c) 2018 Particle Industries, Inc. All rights 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 3 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, see <http://www.gnu.org/licenses/>.
*/

/* Includes -----------------------------------------------------------------*/
#include "nrf_gpio.h"
#include "gpio_hal.h"
#include "pinmap_impl.h"
#include "hw_ticks.h"
#include <stddef.h>

/* Private typedef ----------------------------------------------------------*/

/* Private define -----------------------------------------------------------*/

/* Private macro ------------------------------------------------------------*/

/* Private variables --------------------------------------------------------*/

/* Extern variables ---------------------------------------------------------*/

/* Private function prototypes ----------------------------------------------*/
inline bool is_valid_pin(pin_t pin) __attribute__((always_inline));
inline bool is_valid_pin(pin_t pin)
{
return pin < TOTAL_PINS;
}

PinMode HAL_Get_Pin_Mode(pin_t pin)
{
return PIN_MODE_NONE;
return (!is_valid_pin(pin)) ? PIN_MODE_NONE : HAL_Pin_Map()[pin].pin_mode;
}

PinFunction HAL_Validate_Pin_Function(pin_t pin, PinFunction pinFunction)
{
NRF5x_Pin_Info* PIN_MAP = HAL_Pin_Map();

if (!is_valid_pin(pin))
return PF_NONE;
if (pinFunction==PF_ADC && PIN_MAP[pin].adc_channel != ADC_CHANNEL_NONE)
return PF_ADC;
// Compatible with STM32 for wiring layer
if (pinFunction==PF_TIMER && PIN_MAP[pin].pwm_instance != PWM_INSTANCE_NONE)
return PF_TIMER;
return PF_DIO;
}

Expand All @@ -54,34 +52,168 @@ PinFunction HAL_Validate_Pin_Function(pin_t pin, PinFunction pinFunction)
*/
void HAL_Pin_Mode(pin_t pin, PinMode setMode)
{
if (!is_valid_pin(pin))
{
return;
}

NRF5x_Pin_Info* PIN_MAP = HAL_Pin_Map();
uint32_t gpio_pin_map = NRF_GPIO_PIN_MAP(PIN_MAP[pin].gpio_port, PIN_MAP[pin].gpio_pin);

switch (setMode)
{
case OUTPUT:
nrf_gpio_cfg_output(gpio_pin_map);
PIN_MAP[pin].pin_mode = OUTPUT;
PIN_MAP[pin].pin_func = PF_DIO;
break;

case INPUT:
nrf_gpio_cfg_input(gpio_pin_map, NRF_GPIO_PIN_NOPULL);
PIN_MAP[pin].pin_mode = INPUT;
PIN_MAP[pin].pin_func = PF_DIO;
break;

case INPUT_PULLUP:
nrf_gpio_cfg_input(gpio_pin_map, NRF_GPIO_PIN_PULLUP);
PIN_MAP[pin].pin_mode = INPUT_PULLUP;
PIN_MAP[pin].pin_func = PF_DIO;
break;

case INPUT_PULLDOWN:
nrf_gpio_cfg_input(gpio_pin_map, NRF_GPIO_PIN_PULLDOWN);
PIN_MAP[pin].pin_mode = INPUT_PULLDOWN;
PIN_MAP[pin].pin_func = PF_DIO;
break;

case PIN_MODE_NONE:
nrf_gpio_cfg_default(gpio_pin_map);
PIN_MAP[pin].pin_func = PF_NONE;
default:
break;
}


}

/*
* @brief Saves a pin mode to be recalled later.
*/
void HAL_GPIO_Save_Pin_Mode(PinMode mode)
void HAL_GPIO_Save_Pin_Mode(uint16_t pin)
{
// deprecated
}

/*
* @brief Recalls a saved pin mode.
*/
PinMode HAL_GPIO_Recall_Pin_Mode()
PinMode HAL_GPIO_Recall_Pin_Mode(uint16_t pin)
{
return PIN_MODE_NONE;
// deprecated
return PIN_MODE_NONE;
}

/*
* @brief Sets a GPIO pin to HIGH or LOW.
*/
void HAL_GPIO_Write(uint16_t pin, uint8_t value)
{
if (!is_valid_pin(pin))
{
return;
}

NRF5x_Pin_Info* PIN_MAP = HAL_Pin_Map();
uint32_t gpio_pin_map = NRF_GPIO_PIN_MAP(PIN_MAP[pin].gpio_port, PIN_MAP[pin].gpio_pin);

if(value == 0)
{
nrf_gpio_pin_clear(gpio_pin_map);
}
else
{
nrf_gpio_pin_set(gpio_pin_map);
}
}

/*
* @brief Reads the value of a GPIO pin. Should return either 1 (HIGH) or 0 (LOW).
*/
int32_t HAL_GPIO_Read(uint16_t pin)
{
return 0;
if (!is_valid_pin(pin))
{
return 0;
}

NRF5x_Pin_Info* PIN_MAP = HAL_Pin_Map();
uint32_t gpio_pin_map = NRF_GPIO_PIN_MAP(PIN_MAP[pin].gpio_port, PIN_MAP[pin].gpio_pin);

if ((PIN_MAP[pin].pin_mode == INPUT) ||
(PIN_MAP[pin].pin_mode == INPUT_PULLUP) ||
(PIN_MAP[pin].pin_mode == INPUT_PULLDOWN))
{
return nrf_gpio_pin_read(gpio_pin_map);
}
else
{
return 0;
}
}

/*
* @brief blocking call to measure a high or low pulse
* @returns uint32_t pulse width in microseconds up to 3 seconds,
* returns 0 on 3 second timeout error, or invalid pin.
*/
uint32_t HAL_Pulse_In(pin_t pin, uint16_t value)
{
if (!is_valid_pin(pin))
{
return 0;
}

volatile uint32_t timeout_start = GetSystem1MsTick();

HAL_Pin_Mode(pin, INPUT);

/* If already on the value we want to measure, wait for the next one.
* Time out after 3 seconds so we don't block the background tasks
*/
while (HAL_GPIO_Read(pin) == value)
{
if (GetSystem1MsTick() - timeout_start > 3000)
{
return 0;
}
}

/* Wait until the start of the pulse.
* Time out after 3 seconds so we don't block the background tasks
*/
while (HAL_GPIO_Read(pin) != value)
{
if (GetSystem1MsTick() - timeout_start > 3000)
{
return 0;
}
}

/* Wait until this value changes, this will be our elapsed pulse width.
* Time out after 3 seconds so we don't block the background tasks
*/
volatile uint32_t pulse_start = GetSystem1MsTick();
while (HAL_GPIO_Read(pin) == value)
{
if (GetSystem1MsTick() - timeout_start > 3000)
{
return 0;
}
}

return GetSystem1MsTick() - pulse_start;
}




71 changes: 71 additions & 0 deletions user/tests/hal/gpio/test_gpio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2018 Particle Industries, Inc. All rights 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 3 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, see <http://www.gnu.org/licenses/>.
*/


#include "application.h"
#include "gpio_hal.h"

SYSTEM_MODE(SEMI_AUTOMATIC);
Serial1LogHandler logHandler(115200);

void test_gpio_blink(void)
{
HAL_Pin_Mode(D7, OUTPUT);
delay(500);
HAL_GPIO_Write(D7, 1);
delay(500);
HAL_GPIO_Write(D7, 0);
}

void test_pulse_width(void)
{
uint32_t pulse_width = HAL_Pulse_In(D2, 0);
if (pulse_width)
{
Log.info("Pulse Width: %dms", pulse_width);
}
}

void test_gpio_input(void)
{
HAL_Pin_Mode(D2, INPUT_PULLUP);
HAL_Pin_Mode(D7, OUTPUT);

while (true)
{
if (HAL_GPIO_Read(D2))
{
HAL_GPIO_Write(D7, 1);
}
else
{
HAL_GPIO_Write(D7, 0);
}
}
}
/* executes once at startup */
void setup() {
Log.info("GPIO Test Start...");

}

/* executes continuously after setup() runs */
void loop() {
// test_gpio_blink();
// test_pulse_width();
test_gpio_input();
}

0 comments on commit 769da09

Please sign in to comment.