Skip to content

GIGA: analogWrite(5, 0); faults the processor - Analog APIS are not complete #19

Closed
@KurtE

Description

@KurtE

Describe the bug
analogWrite(5, 0);

Faults on the GIGA...

Output of Serial Monitor

  1. If you have an USB-to-Serial adapter, paste the complete output of the console crash, starting from
uart:~$ sketch
[00:00:11.044,000] <inf> usb_cdc_acm: Device suspended
[00:00:11.269,000] <inf> usb_cdc_acm: Device configured
[00:00:22.948,000] <err> os: ***** BUS FAULT *****
[00:00:22.953,000] <err> os:   Precise data bus error
[00:00:22.959,000] <err> os:   BFAR Address: 0x5f657378
[00:00:22.965,000] <err> os: r0/a1:  0x5f65736c  r1/a2:  0x00000000  r2/a3:  0x2400eee0
[00:00:22.973,000] <err> os: r3/a4:  0x08058a1f r12/ip:  0x00000000 r14/lr:  0x2400e7d7
[00:00:22.982,000] <err> os:  xpsr:  0xa1000000
[00:00:22.987,000] <err> os: s[ 0]:  0x2400f094  s[ 1]:  0x080510ad  s[ 2]:  0x00000000  s[ 3]:  0x0804ef99
[00:00:22.998,000] <err> os: s[ 4]:  0x00000000  s[ 5]:  0x24000568  s[ 6]:  0x000002d8  s[ 7]:  0x0804f057
[00:00:23.008,000] <err> os: s[ 8]:  0x00000000  s[ 9]:  0x00000000  s[10]:  0x2400ec95  s[11]:  0x00000001
[00:00:23.018,000] <err> os: s[12]:  0x2400e75d  s[13]:  0x2400e771  s[14]:  0xfffffffe  s[15]:  0x2400d510
[00:00:23.029,000] <err> os: fpscr:  0x0805b614
[00:00:23.034,000] <err> os: Faulting instruction address (r15/pc): 0x08058a20
[00:00:23.042,000] <err> os: >>> ZEPHYR FATAL ERROR 25: Unknown error on CPU 0
[00:00:23.050,000] <err> os: Current thread: 0x240008b0 (shell_uart)

Optional: attach the sketch

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial) {}

  Serial.println("Start of PWM test");
  delay(10000);

}


void loop() {
  // put your main code here, to run repeatedly:
  for (int i = 0; i < 256; i++) {
    analogWrite(5, i);
    delay(20);
  }

  for (int i = 255; i <= 0; i--) {
    analogWrite(5, i);
    delay(20);
  }
  delay(50);
}

Additional context
Add any other context about the problem here.
Instrumented the code:

void analogWrite(pin_size_t pinNumber, int value)
{
  Serial.print("AnalogWrite called: "); Serial.print(pinNumber);
  Serial.print(" "); Serial.print(value);
  delay(5);
  size_t idx = pwm_pin_index(pinNumber);
  Serial.print(" idx: "); Serial.println(idx); 
  delay(5);
  if (!pwm_is_ready_dt(&arduino_pwm[idx])) {
    return;
  }
  Serial.print("After is ready "); Serial.flush();

  if (idx >= ARRAY_SIZE(arduino_pwm) ) {
    return;
  }

  Serial.print(" ");Serial.print(arduino_pwm[idx].period); Serial.flush();

  if (((uint32_t)value) > arduino_pwm[idx].period) {
    value = arduino_pwm[idx].period;
  } else if (value < 0) {
    value = 0;
  }

  /*
   * A duty ratio determines by the period value defined in dts
   * and the value arguments. So usually the period value sets as 255.
   */
  Serial.println("before Call pwm_set_pulse_dt"); Serial.flush();
  (void)pwm_set_pulse_dt(&arduino_pwm[idx], value);
  Serial.println("After Call pwm_set_pulse_dt"); Serial.flush();
}

Debug output:

Start of PWM test
AnalogWrite called: 5 0 idx: 4294967295

So the line: size_t idx = pwm_pin_index(pinNumber);
returns: 0xffffffff which is obviously > idx >= ARRAY_SIZE(arduino_pwm)

which is checked, unfortunately after the code:
if (!pwm_is_ready_dt(&arduino_pwm[idx])) {
which faulted.
Will swap some code next. But wonder why 5 is not returning a valid index...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions