Zephyr Portenta C33 - Fast GPIO?

I have been playing around with building a simple library to allow faster IO and Pin Name access to several of the Zephyr boards. Started off with the STM32 based ones: Giga, Portenta H7, UnoQ, Nicla Vision.

I thought I would try the Portenta C33 (Renesas). Will extend later when Uno R4 is merged in.

Note: Current code is up in github library:
KurtE/Arduino_zephyr_toolbox: Toolbox of features currently missing from ArduinoCore-zephyr

Sorry in advance, if some of this probably only might make some sense to some of the Zephyr developers. Not sure if best to do here or github...

Currently in the library, I am extracting from the device tree, a list of GPIO objects (in this case IOPorts)


// Generate a list of the stm32 GPIO port zephyr device objects.
#define ADD_GPIO_NODE(node_id) DEVICE_DT_GET(node_id),

const struct device *zephyr_gpio_devices[] = {
#if defined(STM32H7) || defined(STM32U5)
    DT_FOREACH_STATUS_OKAY(st_stm32_gpio, ADD_GPIO_NODE)
#elif defined(ARDUINO_PORTENTA_C33)
    DT_FOREACH_STATUS_OKAY(renesas_ra_gpio_ioport, ADD_GPIO_NODE)
#endif
};

const uint8_t COUNT_ZEPHYR_GPIO_DEVICES = (sizeof(zephyr_gpio_devices) / sizeof(zephyr_gpio_devices[0]));

I also created a list pointers to the underlying Register structures:

// Generate a list of the stm32 GPIO port zephyr device objects.
#define ADD_GPIO_NODE_REG_ADDR(node_id) (R_PORT0_Type *)DT_REG_ADDR(node_id),

R_PORT0_Type *renesas_gpio_port_table[] = {
#if defined(ARDUINO_PORTENTA_C33)
    DT_FOREACH_STATUS_OKAY(renesas_ra_gpio_ioport, ADD_GPIO_NODE_REG_ADDR)
#endif
};

I also created a set of pseudo pin names similar to the ones that were typically done for
STM32 boards: STM32 names were like: PA_1.... PK_7
For this they have IO Ports 0-B, so I have: P0_1... PB_15.

Current functions in header file:

//=============================================================================
// Renesas optiomized functions: currently portenta C33
//=============================================================================
#elif defined(ARDUINO_PORTENTA_C33)
extern R_PORT0_Type *renesas_gpio_port_table[];

struct gpio_ra_config_head {
  struct gpio_driver_config common;
  uint8_t port_num;
  R_PORT0_Type *port;
};

inline void digitalWriteFast(uint8_t pin, PinStatus val) {
  const struct gpio_ra_config_head *config = (gpio_ra_config_head*)zephyr::arduino::arduino_pins[pin].port->config;
  R_PORT0_Type *port = config->port;
  uint16_t mask = 1 << zephyr::arduino::arduino_pins[pin].pin;

  if (val) port->POSR = mask;
  else port->PORR = mask;
}

inline void digitalWriteFast(PinName pin_name, PinStatus val) {
  uint16_t mask = 1 << (pin_name & 0xf);
  R_PORT0_Type *port = renesas_gpio_port_table[pin_name >> 4];
  if (val) port->POSR = mask;
  else port->PORR = mask;
}

inline void digitalToggleFast(uint8_t pin) {
  const struct gpio_ra_config_head *config = (gpio_ra_config_head*)zephyr::arduino::arduino_pins[pin].port->config;
  R_PORT0_Type *port = config->port;
  uint16_t mask = 1 << zephyr::arduino::arduino_pins[pin].pin;

  if (port->PODR & mask) port->PORR = mask;
  else port->POSR = mask;
}

inline void digitalToggleFast(PinName pin_name) {
  uint16_t mask = 1 << (pin_name & 0xf);
  R_PORT0_Type *port = renesas_gpio_port_table[pin_name >> 4];

  if (port->PODR & mask) port->PORR = mask;
  else port->POSR = mask;
}

I have a test sketch which loops 1000 times through different methods, for example:
For example the ones with pin numbers:

void do_digitalWrite() {

  digitalWrite(PIN_MARKER, HIGH);
  uint32_t start_time = micros();
  for (int i = 0; i < 1000; i++) {
    digitalWrite(PIN, HIGH);
    digitalWrite(PIN, LOW);
  }
  uint32_t delta_time = micros() - start_time;
  digitalWrite(PIN_MARKER, LOW);
  Serial.print("digitalWrite: ");
  Serial.println(delta_time, DEC);
}

void do_digitalWriteFast() {
  digitalWrite(PIN_MARKER, HIGH);
  uint32_t start_time = micros();
  for (int i = 0; i < 1000; i++) {
    digitalWriteFast(PIN, HIGH);
    digitalWriteFast(PIN, LOW);
  }
  uint32_t delta_time = micros() - start_time;
  digitalWrite(PIN_MARKER, LOW);
  Serial.print("digitalWriteFast: ");
  Serial.println(delta_time, DEC);
}

void do_digitalToggleFast() {
  digitalWrite(PIN_MARKER, HIGH);
  uint32_t start_time = micros();
  for (int i = 0; i < 1000; i++) {
    digitalToggleFast(PIN);
    digitalToggleFast(PIN);
  }
  uint32_t delta_time = micros() - start_time;
  digitalWrite(PIN_MARKER, LOW);
  Serial.print("digitalToggleFast: ");
  Serial.println(delta_time, DEC);
}

I also have ones that do by name. The complete sketch is up in the library mentioned:
KurtE/Arduino_zephyr_toolbox: Toolbox of features currently missing from ArduinoCore-zephyr

My question is, I finding timings are not fully as I expected.

digitalWrite: 1290
digitalWriteFast: 170
digitalWriteFast(name defined): 123
digitalWriteFast(name-var): 162
digitalToggleFast: 823
digitalToggleFast(name): 990
digitalWriteFast(Port): 950
digitalToggleFast(GPIO_pin_toggle): 611

Yes the digitalWriteFast worked as explected:
Using digitalWrite took 1290ms whereas the digitalWriteFast took 170
But I was surprised that the toggle fast was so slow: 823.
The really only difference between the two functions is:
digitalWriteFast does:

  if (val) port->POSR = mask;
  else port->PORR = mask;

Whereas the toggle does:

  if (port->PODR & mask) port->PORR = mask;
  else port->POSR = mask;

So reading the PODR register slowing it down? Generate some exception?

Now were it also gets interesting, is some of my other tests, try calling
the zephyr functions directly:
For Writes it does:

void  do_zephyr_gpio_writes() {
  digitalWrite(PIN_MARKER, HIGH);
  uint8_t pin_on_port;
  const struct device *dev = mapPinNameToZephyrGPIODevice(pin_name, &pin_on_port);
  uint32_t start_time = micros();
  for (int i = 0; i < 1000; i++) {
    gpio_pin_set(dev, pin_on_port, 1);
    gpio_pin_set(dev, pin_on_port, 0);
  }
  uint32_t delta_time = micros() - start_time;
  digitalWrite(PIN_MARKER, LOW);
  Serial.print("digitalWriteFast(Port): ");
  Serial.println(delta_time, DEC);
}

For toggle it does:

void do_digital_gpio_toggle() {
  digitalWrite(PIN_MARKER, HIGH);
  uint8_t pin_on_port;
  const struct device *dev = mapPinNameToZephyrGPIODevice(pin_name, &pin_on_port);
  uint32_t start_time = micros();
  for (int i = 0; i < 1000; i++) {
    gpio_pin_toggle(dev, pin_on_port);
    gpio_pin_toggle(dev, pin_on_port);
  }
  uint32_t delta_time = micros() - start_time;
  digitalWrite(PIN_MARKER, LOW);
  Serial.print("digitalToggleFast(GPIO_pin_toggle): ");
  Serial.println(delta_time, DEC);
}

And again the timeing:

digitalWriteFast(Port): 950
digitalToggleFast(GPIO_pin_toggle): 611

So again digitalWriteFast version here is faster than Arduino digitalWrite, but slower than going direct to registers.

However the Toggle Fast: is considerably faster?
I believe the Zephyr code for toggle is:

static int gpio_ra_port_toggle_bits(const struct device *dev, gpio_port_pins_t pins)
{
	const struct gpio_ra_config *config = dev->config;
	R_PORT0_Type *port = config->port;

	port->PODR = (port->PODR ^ pins);

	return 0;
}

Note: it is unclear to me if their function is safe as it is touching updating the state of all of the pins on the Port (Port Output Data Register). What happens if there is a context change in between the time this code reads the PODR and writes the updated value back up, if the other context also changed the state of some pins?

Which is why my version uses the Set and Reset bits registers (POSR, PORR)
Note: Logic analyzer confirms the timing and the outputs of the registers

Note: the timing are not overly consistent as in many cases I see gaps in time.
Probably where zephyr is doing something else...

Thoughts?
Thanks!
Kurt

EDIT: for reference the timing for Portenta H7

digitalWrite: 177
digitalWriteFast: 61
digitalWriteFast(name defined): 61
digitalWriteFast(name-var): 61
digitalToggleFast: 181
digitalToggleFast(name): 151
digitalWriteFast(Port): 168
digitalToggleFast(GPIO_pin_toggle): 357
1 Like