openwch / arduino_core_ch32

Core library for CH32duino
248 stars 41 forks source link

Parameter Mismatch Issue in attachInterrupt() of WInterrupts.h/cpp #15

Closed FZDSLR closed 3 months ago

FZDSLR commented 1 year ago

Expected Behavior

According to the documentation and Arduino core api , the function attachInterrupt() should accept params like (pin_size_t interruptNum, voidTemplateFuncPtrParam<T> userFunc, PinStatus mode, T& param), and attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE) should be OK.

Current Behavior

Err occurs when compiling:

C:\Users\xxx\AppData\Local\Temp\.arduinoIDE-unsaved202375-6936-ycii6s.ifkd\sketch_aug5a\sketch_aug5a.ino: In function 'void setup()':
C:\Users\xxx\AppData\Local\Temp\.arduinoIDE-unsaved202375-6936-ycii6s.ifkd\sketch_aug5a\sketch_aug5a.ino:15:56: error: cannot convert 'void (*)()' to 'GPIOMode_TypeDef'
   attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
                                                        ^~~~~
In file included from C:\Users\xxx\AppData\Local\Arduino15\packages\WCH\hardware\ch32v\1.0.3\cores\arduino/wiring.h:46,
                 from C:\Users\xxx\AppData\Local\Arduino15\packages\WCH\hardware\ch32v\1.0.3\cores\arduino/Arduino.h:36,
                 from C:\Users\xxx\AppData\Local\Temp\arduino\sketches\0F31CA54BC21916D8E00040838882DBE\sketch\sketch_aug5a.ino.cpp:1:
C:\Users\xxx\AppData\Local\Arduino15\packages\WCH\hardware\ch32v\1.0.3\cores\arduino/WInterrupts.h:27:52: note:   initializing argument 2 of 'void attachInterrupt(uint32_t, GPIOMode_TypeDef, void (*)(), EXTIMode_TypeDef, EXTITrigger_TypeDef)'
 void attachInterrupt(uint32_t pin,GPIOMode_TypeDef io_mode, void (*callback)(void), EXTIMode_TypeDef it_mode, EXTITrigger_TypeDef trigger_mode);
                                   ~~~~~~~~~~~~~~~~~^~~~~~~

exit status 1

Compilation error: cannot convert 'void (*)()' to 'GPIOMode_TypeDef'

Possible Solution

Steps to Reproduce

compiling the program:

#include <arduino.h>

const byte ledPin = 3;

const byte interruptPin = 2;  

volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP); 
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}

Context (Environment)

maxgerhardt commented 1 year ago

FYI, ArduinoCore-API docs don't apply here at all, the core uses it's own format

https://github.com/openwch/arduino_core_ch32/blob/29c76add9bafaceaa5acda53a53c582947ec3697/cores/arduino/WInterrupts.h#L27-L28

So it'll expect

   /* Input + Pull-Up, Interrupt, triggering on Rise or Fall */
  attachInterrupt(interruptPin, GPIO_Mode_IPU, &blink, EXTI_Mode_Interrupt, EXTI_Trigger_Rising_Falling);
TianpeiLee commented 3 months ago

I have reserved interfaces for events or interrupts and triggering modes(rising edge, falling edge, or both), This is more flexible, and in fact, it does lose compatibility.