diff --git a/examples/platforms/nrf52840/DIAG.md b/examples/platforms/nrf52840/DIAG.md index 06a25c1a0..ea180ae32 100644 --- a/examples/platforms/nrf52840/DIAG.md +++ b/examples/platforms/nrf52840/DIAG.md @@ -6,6 +6,7 @@ New commands allow for more accurate low level radio testing. ### New commands * [diag ccathreshold](#diag-ccathreshold) + * [diag gpio](#diag-gpio) * [diag id](#diag-id) * [diag listen](#diag-listen) * [diag temp](#diag-temp) @@ -52,6 +53,52 @@ Value range 0 to 255. Default: `45`. +### diag gpio +Set of commands for managing gpio pins. + +### diag gpio \ +Return the current value of the gpio. + +Note: \ is an integer that combines port and pin into a single, +contiguous number space as follows: +``` + pinnum = (port * 32) + pin +``` +See also the [`NRF_GPIO_PIN_MAP`](../../../third_party/NordicSemiconductor/hal/nrf_gpio.h) macro. + +```bash +> diag gpio 47 +gpio 47 = 0 +``` + +### diag gpio out \ +Sets the given gpio to output mode. +```bash +> diag gpio out 47 +gpio 47: out +``` + +### diag gpio in \ +Sets the given gpio to input, no pull mode. +```bash +> diag gpio in 47 +gpio 47: in no pull +``` + +### diag gpio set \ +Sets the given output gpio to high. +```bash +> diag gpio set 47 +gpio 47 = 1 +``` + +### diag gpio clr \ +Sets the given output gpio to low. +```bash +> diag gpio clr 47 +gpio 47 = 0 +``` + ### diag id Get board ID. diff --git a/examples/platforms/nrf52840/diag.c b/examples/platforms/nrf52840/diag.c index 81c5fa814..3d6cd9d97 100644 --- a/examples/platforms/nrf52840/diag.c +++ b/examples/platforms/nrf52840/diag.c @@ -37,6 +37,8 @@ #include "platform-nrf5.h" +#include + #include #include #include @@ -239,6 +241,79 @@ exit: appendErrorResult(error, aOutput, aOutputMaxLen); } +static void processGpio(otInstance *aInstance, int argc, char *argv[], + char *aOutput, size_t aOutputMaxLen) +{ + OT_UNUSED_VARIABLE(aInstance); + + long pinnum; + otError error = OT_ERROR_NONE; + + otEXPECT_ACTION(otPlatDiagModeGet(), error = OT_ERROR_INVALID_STATE); + + if (argc == 1) + { + uint32_t value; + + error = parseLong(argv[0], &pinnum); + otEXPECT(error == OT_ERROR_NONE); + + value = nrf_gpio_pin_read(pinnum); + + snprintf(aOutput, aOutputMaxLen, "gpio %d = %d\r\n", + (uint8_t)pinnum, (uint8_t)value); + } + else if (strcmp(argv[0], "set") == 0) + { + otEXPECT_ACTION(argc == 2, error = OT_ERROR_INVALID_ARGS); + error = parseLong(argv[1], &pinnum); + otEXPECT(error == OT_ERROR_NONE); + + nrf_gpio_pin_set(pinnum); + + snprintf(aOutput, aOutputMaxLen, "gpio %d = 1\r\n", (uint8_t)pinnum); + } + else if (strcmp(argv[0], "clr") == 0) + { + otEXPECT_ACTION(argc == 2, error = OT_ERROR_INVALID_ARGS); + error = parseLong(argv[1], &pinnum); + otEXPECT(error == OT_ERROR_NONE); + + nrf_gpio_pin_clear(pinnum); + + snprintf(aOutput, aOutputMaxLen, "gpio %d = 0\r\n", (uint8_t)pinnum); + } + else if (strcmp(argv[0], "out") == 0) + { + otEXPECT_ACTION(argc == 2, error = OT_ERROR_INVALID_ARGS); + error = parseLong(argv[1], &pinnum); + otEXPECT(error == OT_ERROR_NONE); + + nrf_gpio_cfg_output(pinnum); + + snprintf(aOutput, aOutputMaxLen, "gpio %d: out\r\n", (uint8_t)pinnum); + } + else if (strcmp(argv[0], "in") == 0) + { + otEXPECT_ACTION(argc == 2, error = OT_ERROR_INVALID_ARGS); + error = parseLong(argv[1], &pinnum); + otEXPECT(error == OT_ERROR_NONE); + + nrf_gpio_cfg_input(pinnum, NRF_GPIO_PIN_NOPULL); + + snprintf(aOutput, aOutputMaxLen, "gpio %d: in no pull\r\n", + (uint8_t)pinnum); + } + else + { + error = OT_ERROR_INVALID_ARGS; + } + +exit: + appendErrorResult(error, aOutput, aOutputMaxLen); +} + + static void processTemp(otInstance *aInstance, int argc, char *argv[], char *aOutput, size_t aOutputMaxLen) { OT_UNUSED_VARIABLE(aInstance); @@ -296,11 +371,12 @@ exit: const struct PlatformDiagCommand sCommands[] = { - { "listen", &processListen }, - { "transmit", &processTransmit }, + { "ccathreshold", &processCcaThreshold }, + { "gpio", &processGpio }, { "id", &processID }, + { "listen", &processListen }, { "temp", &processTemp }, - { "ccathreshold", &processCcaThreshold } + { "transmit", &processTransmit }, }; void otPlatDiagProcess(otInstance *aInstance, int argc, char *argv[], char *aOutput, size_t aOutputMaxLen)