diff --git a/targets/CMSIS-OS/ChibiOS/nanoCLR/Windows.Devices.Gpio/win_dev_gpio_native_Windows_Devices_Gpio_GpioPin.cpp b/targets/CMSIS-OS/ChibiOS/nanoCLR/Windows.Devices.Gpio/win_dev_gpio_native_Windows_Devices_Gpio_GpioPin.cpp index c54aec619e..48a210bc14 100644 --- a/targets/CMSIS-OS/ChibiOS/nanoCLR/Windows.Devices.Gpio/win_dev_gpio_native_Windows_Devices_Gpio_GpioPin.cpp +++ b/targets/CMSIS-OS/ChibiOS/nanoCLR/Windows.Devices.Gpio/win_dev_gpio_native_Windows_Devices_Gpio_GpioPin.cpp @@ -89,8 +89,15 @@ static void debounceTimer_Callback( void* arg ) if(lastPadValue == currentValue) { // value hasn't change for debounce interval so this is a valid change - // post a managed event with the current pin value - PostManagedEvent( EVENT_GPIO, 0, pinNumber, currentValue ); + + // flag to determine if there are any callbacks registered in managed code + bool callbacksRegistered = (pThis[ Library_win_dev_gpio_native_Windows_Devices_Gpio_GpioPin::FIELD___callbacks ].Dereference() != NULL); + + // post a managed event with the current pin value, only if there is anyone listening otherwise don't bother + if(callbacksRegistered) + { + PostManagedEvent( EVENT_GPIO, 0, pinNumber, currentValue ); + } } } @@ -156,8 +163,14 @@ static void GpioEventCallback(void *arg) // read pad pThis[ Library_win_dev_gpio_native_Windows_Devices_Gpio_GpioPin::FIELD___lastInputValue ].NumericByRef().s4 = palReadLine(ioLine); - // post a managed event with the current pin reading - PostManagedEvent( EVENT_GPIO, 0, pinNumber, palReadLine(ioLine) ); + // flag to determine if there are any callbacks registered in managed code + bool callbacksRegistered = (pThis[ Library_win_dev_gpio_native_Windows_Devices_Gpio_GpioPin::FIELD___callbacks ].Dereference() != NULL); + + // post a managed event with the current pin reading, only if there is anyone listening otherwise don't bother + if(callbacksRegistered) + { + PostManagedEvent( EVENT_GPIO, 0, pinNumber, palReadLine(ioLine) ); + } } chSysUnlockFromISR(); diff --git a/targets/CMSIS-OS/ChibiOS/nanoCLR/Windows.Devices.SerialCommunication/win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice.cpp b/targets/CMSIS-OS/ChibiOS/nanoCLR/Windows.Devices.SerialCommunication/win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice.cpp index 432a751f0a..1e6579a591 100644 --- a/targets/CMSIS-OS/ChibiOS/nanoCLR/Windows.Devices.SerialCommunication/win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice.cpp +++ b/targets/CMSIS-OS/ChibiOS/nanoCLR/Windows.Devices.SerialCommunication/win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice.cpp @@ -256,7 +256,11 @@ static void RxChar(UARTDriver *uartp, uint16_t c) else { // no read operation ongoing, so fire an event + // post a managed event with the port index and event code (check if this is the watch char or just another another) + // TODO: check if callbacks are registered so this is called only if there is anyone listening otherwise don't bother + // for that to happen ChibiOS callback has to accept arg which we would passing the GpioPin CLR_RT_HeapBlock (see Gpio handler) + // check http://www.chibios.com/forum/viewtopic.php?f=36&t=4798 PostManagedEvent( EVENT_SERIAL, 0, portIndex, (c == palUart->WatchChar) ? SerialData_WatchChar : SerialData_Chars ); }