This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to use TRACE pins as GPIO

We are developing a board based on nRF52840. One of the pins we would like to use as GPIO is P1.09. It is configured as GPIO (the same configuration as P0.12), but the output does not get set to 1 (the P0.12 does get set to 1 using identical code). 

Some searching revealed, that P1.09 is used by for TRACE. From what I have found, defining ENABLE_TRACE would enable TRACE, otherwise these pins would just be normal GPIOs. Looking into sstem_nrf52840.c, where ENABLE_TRACE is used if defined, the code is greyed out, indicating that TRACE is not enabled. At least not by ENABLE_TRACE.

Is there some other mode of enabling trace?

How can we use these pins (Especially P1.09) as GPIO?

Thank you!

  • Check the TRACECONFIG register (in CLOCK), e.g. with a debugger. Its contents shows whether the pins are GPIO or trace mode.

  • Turbo J thank you.

    It is set correct:
    NRF_CLOCK->TRACECONFIG: 0x0

    I added this: 
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "NRF_CLOCK->TRACECONFIG: 0x%x\n", NRF_CLOCK->TRACECONFIG);

    Which resutls in:
    main.c,  583, NRF_CLOCK->TRACECONFIG: 0x0

    This is pin definition:
    #define PIN_MOTOR_NEG    NRF_GPIO_PIN_MAP(1,9)

    This is pin configuration:
    #define PIN_MOTOR_CONFIG ((GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | \
    (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | \
    (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) | \
    (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) | \
    (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos))

    This is pin initialization:
    gpio_pin_init(PIN_MOTOR_NEG, PIN_MOTOR_CONFIG);

    The function:
    void gpio_pin_init(uint32_t pin, uint32_t configuration)
    {
    NRF_GPIO->PIN_CNF[pin] = configuration;
    }

    Setting pin:
    gpio_pin_set(PIN_MOTOR_NEG, 1);

    Function:
    void gpio_pin_set(uint32_t pin, uint8_t value)
    {
    if (value)
    {
    NRF_GPIO->OUTSET = 1UL << pin;
    }
    else
    {
    NRF_GPIO->OUTCLR = 1UL << pin;
    }
    }

    And reading the pin immediatelly after setting returns 0.

    The positive pin, P0.12 works fine with the same code.

  • Confusion between P0 and P1, easily done. NRF_GPIO refers to P0; better to either use all Nordic definitions or don't use any, but here they are mixed. Replace NRF_GPIO with NRF_P1 in this code, or just use the Nordic functions and not the NrF_GPIO and NRF_P1 registers directly. The function is (to set to '1') nrf_gpio_pin_set(pin) and so on.

  • Thank you both TurboJ and hmolesworth for help and solving this issue.

    I initially developed the application on nRF52 DK with nRF52832 where there was only P0. I was not aware of neat Nordic function, therefore I wrote my own. Switching to nRF52840 on custom PCB, use of P1 was more convenient, but obviously writing out of NRF_GPIO does no good.

    I would only like to add a few things which I "figured out" (learnt actually) solving this issue. These may be useful for other newbies.

    • Nordic GPIO functions are defined in nrf_gpio.h.
    • There is also a function:
      __STATIC_INLINE void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value);
      which can be used to "write" 0 and 1 (anything else actually).
    • NRF_CLOCK register is defined (among others) in nrf52840.h (or other corresponding file).
Related