Skip to content

Commit f859289

Browse files
authored
Merge pull request #10489 from kjbracey-arm/gpio_api_nc
gpio_api.h: Clarify desired behaviour for NC
2 parents b636241 + 66f446b commit f859289

File tree

12 files changed

+110
-28
lines changed

12 files changed

+110
-28
lines changed

TESTS/mbed_hal/gpio/main.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "utest/utest.h"
19+
#include "unity/unity.h"
20+
#include "greentea-client/test_env.h"
21+
22+
using namespace utest::v1;
23+
24+
#include "PinNames.h"
25+
#include "gpio_api.h"
26+
27+
static void gpio_nc_test()
28+
{
29+
gpio_t nc_obj;
30+
gpio_init(&nc_obj, NC);
31+
TEST_ASSERT_FALSE(gpio_is_connected(&nc_obj));
32+
33+
gpio_t led_obj;
34+
gpio_init(&led_obj, LED1);
35+
if (LED1 == NC) {
36+
TEST_ASSERT_FALSE(gpio_is_connected(&led_obj));
37+
} else {
38+
TEST_ASSERT_TRUE(gpio_is_connected(&led_obj));
39+
}
40+
}
41+
42+
Case cases[] = {
43+
Case("gpio NC test", gpio_nc_test)
44+
};
45+
46+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
47+
{
48+
GREENTEA_SETUP(20, "default_auto");
49+
return greentea_test_setup_handler(number_of_cases);
50+
}
51+
52+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
53+
54+
int main()
55+
{
56+
Harness::run(specification);
57+
}

hal/gpio_api.h

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ extern "C" {
2929

3030
/**
3131
* \defgroup hal_gpio GPIO HAL functions
32+
*
33+
* # Defined behavior
34+
* * ::gpio_init and other init functions can be called with NC or a valid PinName for the target - Verified by ::gpio_nc_test
35+
* * ::gpio_is_connected can be used to test whether a gpio_t object was initialized with NC - Verified by ::gpio_nc_test
36+
*
37+
* # Undefined behavior
38+
* * Calling any ::gpio_mode, ::gpio_dir, ::gpio_write or ::gpio_read on a gpio_t object that was initialized
39+
* with NC.
40+
* * Calling ::gpio_set with NC.
41+
*
3242
* @{
3343
*/
3444

@@ -38,43 +48,45 @@ extern "C" {
3848
* @return The GPIO port mask for this pin
3949
**/
4050
uint32_t gpio_set(PinName pin);
41-
/* Checks if gpio object is connected (pin was not initialized with NC)
42-
* @param pin The pin to be set as GPIO
43-
* @return 0 if port is initialized with NC
51+
52+
/** Checks if gpio object is connected (pin was not initialized with NC)
53+
* @param obj The GPIO object
54+
* @return 0 if object was initialized with NC
55+
* @return non-zero if object was initialized with a valid PinName
4456
**/
4557
int gpio_is_connected(const gpio_t *obj);
4658

4759
/** Initialize the GPIO pin
4860
*
4961
* @param obj The GPIO object to initialize
50-
* @param pin The GPIO pin to initialize
62+
* @param pin The GPIO pin to initialize (may be NC)
5163
*/
5264
void gpio_init(gpio_t *obj, PinName pin);
5365

5466
/** Set the input pin mode
5567
*
56-
* @param obj The GPIO object
68+
* @param obj The GPIO object (must be connected)
5769
* @param mode The pin mode to be set
5870
*/
5971
void gpio_mode(gpio_t *obj, PinMode mode);
6072

6173
/** Set the pin direction
6274
*
63-
* @param obj The GPIO object
75+
* @param obj The GPIO object (must be connected)
6476
* @param direction The pin direction to be set
6577
*/
6678
void gpio_dir(gpio_t *obj, PinDirection direction);
6779

6880
/** Set the output value
6981
*
70-
* @param obj The GPIO object
82+
* @param obj The GPIO object (must be connected)
7183
* @param value The value to be set
7284
*/
7385
void gpio_write(gpio_t *obj, int value);
7486

7587
/** Read the input value
7688
*
77-
* @param obj The GPIO object
89+
* @param obj The GPIO object (must be connected)
7890
* @return An integer value 1 or 0
7991
*/
8092
int gpio_read(gpio_t *obj);
@@ -85,38 +97,38 @@ int gpio_read(gpio_t *obj);
8597
/** Init the input pin and set mode to PullDefault
8698
*
8799
* @param gpio The GPIO object
88-
* @param pin The pin name
100+
* @param pin The pin name (may be NC)
89101
*/
90102
void gpio_init_in(gpio_t *gpio, PinName pin);
91103

92104
/** Init the input pin and set the mode
93105
*
94106
* @param gpio The GPIO object
95-
* @param pin The pin name
107+
* @param pin The pin name (may be NC)
96108
* @param mode The pin mode to be set
97109
*/
98110
void gpio_init_in_ex(gpio_t *gpio, PinName pin, PinMode mode);
99111

100112
/** Init the output pin as an output, with predefined output value 0
101113
*
102114
* @param gpio The GPIO object
103-
* @param pin The pin name
115+
* @param pin The pin name (may be NC)
104116
* @return An integer value 1 or 0
105117
*/
106118
void gpio_init_out(gpio_t *gpio, PinName pin);
107119

108120
/** Init the pin as an output and set the output value
109121
*
110122
* @param gpio The GPIO object
111-
* @param pin The pin name
123+
* @param pin The pin name (may be NC)
112124
* @param value The value to be set
113125
*/
114126
void gpio_init_out_ex(gpio_t *gpio, PinName pin, int value);
115127

116128
/** Init the pin to be in/out
117129
*
118130
* @param gpio The GPIO object
119-
* @param pin The pin name
131+
* @param pin The pin name (may be NC)
120132
* @param direction The pin direction to be set
121133
* @param mode The pin mode to be set
122134
* @param value The value to be set for an output pin

targets/TARGET_ARM_FM/TARGET_FVP_MPS2/gpio_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ uint32_t gpio_set(PinName pin)
2727
// with the object created for the pin
2828
void gpio_init(gpio_t *obj, PinName pin)
2929
{
30+
obj->pin = pin;
3031
if (pin == NC) {
3132
return;
3233
} else {
3334
int pin_value = 0;
34-
obj->pin = pin;
3535
if (pin <= 15) {
3636
pin_value = pin;
3737
} else if (pin >= 16 && pin <= 31) {

targets/TARGET_ARM_SSG/TARGET_CM3DS_MPS2/gpio_api.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ void gpio_init(gpio_t *obj, PinName pin)
101101
{
102102
struct arm_gpio_dev_t *gpio_dev;
103103

104+
if (pin == NC) {
105+
obj->pin_number = NC;
106+
obj->gpio_dev = NULL;
107+
obj->mps2_io_dev = NULL;
108+
return;
109+
}
110+
104111
if (pin >= EXP0 && pin <= EXP51) {
105112
/* GPIO pins */
106113
switch (GPIO_DEV_NUMBER(pin)) {

targets/TARGET_ARM_SSG/TARGET_IOTSS/gpio_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ uint32_t gpio_set(PinName pin) {
2525
// this links the board control bits for each pin
2626
// with the object created for the pin
2727
void gpio_init(gpio_t *obj, PinName pin) {
28+
obj->pin = pin;
2829
if(pin == NC){ return;}
2930
else {
3031
int pin_value = 0;
31-
obj->pin = pin;
3232
if(pin <=15){
3333
pin_value = pin;
3434
}else if (pin >= 16 && pin <= 31){

targets/TARGET_Atmel/TARGET_SAM_CortexM0P/gpio_api.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ uint32_t gpio_set(PinName pin)
2828

2929
void gpio_init(gpio_t *obj, PinName pin)
3030
{
31-
MBED_ASSERT(pin != (PinName)NC);
3231
struct port_config pin_conf;
3332
PortGroup *const port_base = (PortGroup*)port_get_group_from_gpio_pin(pin);
3433

targets/TARGET_Atmel/TARGET_SAM_CortexM4/gpio_api.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ uint32_t gpio_set(PinName pin)
3030

3131
void gpio_init(gpio_t *obj, PinName pin)
3232
{
33-
MBED_ASSERT(pin != (PinName)NC);
3433
if (g_sys_init == 0) {
3534
sysclk_init();
3635
system_board_init();
3736
g_sys_init = 1;
3837
}
3938
obj->pin = pin;
39+
if (pin == NC) {
40+
return;
41+
}
4042

4143
ioport_set_pin_dir(pin, IOPORT_DIR_INPUT);
4244
ioport_set_pin_mode(pin, IOPORT_MODE_PULLUP);

targets/TARGET_Realtek/TARGET_AMEBA/TARGET_MCU_RTL8195A/gpio_api.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ void gpio_init(gpio_t *obj, PinName pin)
8383
{
8484
uint32_t pin_name;
8585

86+
obj->pin = pin;
87+
8688
if (pin == (PinName)NC)
8789
return;
8890

89-
obj->pin = pin;
9091
obj->mode = PullNone;
9192
obj->direction = PIN_INPUT;
9293
pin_name = gpio_set(pin); // get the IP pin name

targets/TARGET_TT/TARGET_TT_M3HQ/gpio_api.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ void gpio_init(gpio_t *obj, PinName pin)
3636
{
3737
// Store above pin mask, pin name into GPIO object
3838
obj->pin = pin;
39-
obj->mask = gpio_set(pin);
40-
obj->port = (PortName) (pin >> 3);
41-
TSB_CG->FSYSENA |= (1<<(obj->port));
39+
if (pin != NC) {
40+
obj->mask = gpio_set(pin);
41+
obj->port = (PortName) (pin >> 3);
42+
TSB_CG->FSYSENA |= (1<<(obj->port));
43+
}
4244
}
4345

4446
void gpio_mode(gpio_t *obj, PinMode mode)

targets/TARGET_TT/TARGET_TT_M4G9/gpio_api.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ void gpio_init(gpio_t *obj, PinName pin)
6161
{
6262
// Store above pin mask, pin name into GPIO object
6363
obj->pin = pin;
64-
obj->pin_num = PIN_POS(pin);
65-
obj->mask = gpio_set(pin);
66-
obj->port = (PortName) PIN_PORT(pin);
67-
//Enable clock for particular port
68-
TSB_CG->FSYSMENB |= (1 << ((obj->port) + 2));
64+
if (pin != NC) {
65+
obj->pin_num = PIN_POS(pin);
66+
obj->mask = gpio_set(pin);
67+
obj->port = (PortName) PIN_PORT(pin);
68+
//Enable clock for particular port
69+
TSB_CG->FSYSMENB |= (1 << ((obj->port) + 2));
70+
}
6971
}
7072

7173
void gpio_mode(gpio_t *obj, PinMode mode)

0 commit comments

Comments
 (0)