Skip to content

Commit

Permalink
Remove jshHadEvent duplication by moving it into jshPushIOEvent and j…
Browse files Browse the repository at this point in the history
…shPushIOWatchEvent
  • Loading branch information
gfwilliams committed Nov 27, 2024
1 parent 529b396 commit 76e707f
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 14 deletions.
2 changes: 0 additions & 2 deletions libs/bluetooth/bluetooth_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,12 @@ void jsble_queue_pending_buf(BLEPending blep, uint16_t data, char *ptr, size_t l
// Push the actual event
JsSysTime d = (JsSysTime)((data<<8)|blep);
jshPushIOEvent(EV_BLUETOOTH_PENDING, d);
jshHadEvent();
}

/// Add a new bluetooth event to the queue with 16 bits of data
void jsble_queue_pending(BLEPending blep, uint16_t data) {
JsSysTime d = (JsSysTime)((data<<8)|blep);
jshPushIOEvent(EV_BLUETOOTH_PENDING, d);
jshHadEvent();
}

/* Handler for common event types (between nRF52/ESP32). Called first
Expand Down
5 changes: 3 additions & 2 deletions src/jsdevices.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ void jshPushIOCharEvents(IOEventFlags channel, char *data, unsigned int count) {
for (i=0;i<count;i++) jshPushIOCharEvent(channel, data[i]);
}

/* Signal an IO watch event as having happened.
/* Signal an IO watch event as having happened. Calls jshHadEvent();
On the esp8266 we need this to be loaded into static RAM because it can run at interrupt time */
void CALLED_FROM_INTERRUPT jshPushIOWatchEvent(
IOEventFlags channel //!< The channel on which the IO watch event has happened.
Expand Down Expand Up @@ -514,7 +514,7 @@ void CALLED_FROM_INTERRUPT jshPushIOWatchEvent(
jshPushIOEvent(channel, time);
}

/// Add this IO event to the IO event queue.
/// Add this IO event to the IO event queue. Calls jshHadEvent();
void CALLED_FROM_INTERRUPT jshPushIOEvent(
IOEventFlags channel, //!< The event to add to the queue.
JsSysTime time //!< The time that the event is thought to have happened.
Expand All @@ -523,6 +523,7 @@ void CALLED_FROM_INTERRUPT jshPushIOEvent(
evt.flags = channel;
evt.data.time = (unsigned int)time;
jshPushEvent(&evt);
jshHadEvent();
}

// returns true on success
Expand Down
5 changes: 3 additions & 2 deletions src/jsdevices.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,10 @@ typedef struct IOEvent {

/// Push an IO event into the ioBuffer (designed to be called from IRQ)
void jshPushEvent(IOEvent *evt);
// Push an 'IO' even
/// Add this IO event to the IO event queue. Calls jshHadEvent();
void jshPushIOEvent(IOEventFlags channel, JsSysTime time);
void jshPushIOWatchEvent(IOEventFlags channel); // push an even when a pin changes state
/// Signal an IO watch event as having happened. Calls jshHadEvent();
void jshPushIOWatchEvent(IOEventFlags channel);
/// Push a single character event (for example USART RX)
void jshPushIOCharEvent(IOEventFlags channel, char charData);
/// Push many character events at once (for example USB RX)
Expand Down
1 change: 1 addition & 0 deletions targetlibs/stm32legacyusb/usb_endp.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ void EP3_OUT_Callback(void)
NAKed till the end of the USART Xfer */

jshPushIOCharEvents(EV_USBSERIAL, USB_Rx_Buffer, USB_Rx_Cnt);
jshHadEvent();

/* Enable the receive of data on EP3 */
SetEPRxStatus(ENDP3, jshHasEventSpaceForChars(VIRTUAL_COM_PORT_DATA_SIZE) ? EP_RX_VALID : EP_RX_NAK);
Expand Down
1 change: 1 addition & 0 deletions targetlibs/stm32usb/usbd_cdc_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ static uint8_t USBD_CDC_HID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum)
if (handle) {
// Process data
jshPushIOCharEvents(EV_USBSERIAL, (char*)handle->cdcRX, rxLength);
jshHadEvent();

// Set CDC_READ_WAIT_EMPTY flag - we'll re-enable USB RX using
// USBD_LL_PrepareReceive ONLY when we have enough space
Expand Down
7 changes: 1 addition & 6 deletions targets/nrf5x/jshardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
/*Get amount of data transfered*/
size_t size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
jshPushIOCharEvents(EV_USBSERIAL, m_rx_buffer, size);

jshHadEvent();

/*Setup next transfer*/
ret = app_usbd_cdc_acm_read(&m_app_cdc_acm,
Expand Down Expand Up @@ -1671,7 +1671,6 @@ static void jsvPinWatchHandler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t a
lastHandledPinState = !lastHandledPinState;
IOEventFlags evt = jshGetEventFlagsForWatchedPin(pin);
jshPushIOWatchEvent(evt);
jshHadEvent();
}


Expand Down Expand Up @@ -2205,7 +2204,6 @@ static void twis_event_handler(nrf_drv_twis_evt_t const * const p_event)
break;
case TWIS_EVT_READ_DONE:
jshPushIOEvent(EV_I2C1, twisAddr|0x80|(p_event->data.tx_amount<<8)); // send event to indicate a read
jshHadEvent();
twisAddr += p_event->data.tx_amount;
break;
case TWIS_EVT_WRITE_REQ:
Expand All @@ -2217,7 +2215,6 @@ static void twis_event_handler(nrf_drv_twis_evt_t const * const p_event)
twisAddr = twisRxBuf[0];
if (p_event->data.rx_amount>1) {
jshPushIOEvent(EV_I2C1, twisAddr|((p_event->data.rx_amount-1)<<8)); // send event to indicate a write
jshHadEvent();
JsVar *i2c = jsvObjectGetChildIfExists(execInfo.root,"I2C1");
if (i2c) {
JsVar *buf = jsvObjectGetChildIfExists(i2c,"buffer");
Expand Down Expand Up @@ -2921,12 +2918,10 @@ void COMP_LPCOMP_IRQHandler() {
if (nrf_lpcomp_event_check(NRF_LPCOMP_EVENT_UP) && nrf_lpcomp_int_enable_check(LPCOMP_INTENSET_UP_Msk)) {
nrf_lpcomp_event_clear(NRF_LPCOMP_EVENT_UP);
jshPushIOEvent(EV_CUSTOM, EVC_LPCOMP | EVC_DATA_LPCOMP_UP);
jshHadEvent();
}
if (nrf_lpcomp_event_check(NRF_LPCOMP_EVENT_DOWN) && nrf_lpcomp_int_enable_check(LPCOMP_INTENSET_DOWN_Msk)) {
nrf_lpcomp_event_clear(NRF_LPCOMP_EVENT_DOWN);
jshPushIOEvent(EV_CUSTOM, EVC_LPCOMP);
jshHadEvent();
}
}

Expand Down
7 changes: 5 additions & 2 deletions targets/stm32/jshardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -2740,10 +2740,12 @@ bool jshSleep(JsSysTime timeUntilWake) {

do { // we loop here so we can half-wake to kick the WDT without incurring wait for USB
JsSysTime timeToSleep = timeUntilWake;
// Don't sleep so long the WDT goes off!
if (isAutoWDT && timeToSleep>watchdogSleepMax)
timeToSleep = watchdogSleepMax;
if (timeUntilWake==JSSYSTIME_MAX) timeUntilWake = 0; // if we're just waiting for as long as possible
else timeUntilWake -= timeToSleep;
// if JSSYSTIME_MAX we just sleep as long as possible unless woken by something else
if (timeUntilWake!=JSSYSTIME_MAX)
timeUntilWake -= timeToSleep;
if (isAutoWDT) jshKickWatchDog();
/* Add EXTI for Serial port */
//jshPinWatch(JSH_PORTA_OFFSET+10, true);
Expand Down Expand Up @@ -2877,6 +2879,7 @@ bool jshSleep(JsSysTime timeUntilWake) {
#endif
__WFI(); // Wait for Interrupt
jsiSetSleep(JSI_SLEEP_AWAKE);
jshHadEventDuringSleep = false;

/* We may have woken up before the wakeup event. If so
then make sure we clear the event */
Expand Down
1 change: 1 addition & 0 deletions targets/stm32/stm32_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ NO_INLINE static void USART_IRQHandler(USART_TypeDef *USART, IOEventFlags device
if (jshIsSerial7Bit(device)) ch &= 0x7F;
/* Put it in our queue */
jshPushIOCharEvent(device, ch);
jshHadEvent();
}
/* If overrun condition occurs, clear the ORE flag and recover communication */
if (USART_GetFlagStatus(USART, USART_FLAG_ORE) != RESET) {
Expand Down
1 change: 1 addition & 0 deletions targets/stm32_ll/stm32_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ static void USART_IRQHandler(USART_TypeDef *USART, IOEventFlags device) {
if (jshIsSerial7Bit(device)) ch &= 0x7F;
/* Put it in our queue */
jshPushIOCharEvent(device, ch);
jshHadEvent();
}
/* If overrun condition occurs, clear the ORE flag and recover communication */
if (LL_USART_IsActiveFlag_ORE(USART) != RESET)
Expand Down

0 comments on commit 76e707f

Please sign in to comment.