/** * Transmit all the characters in the transmit buffer. * */ void esp8266_uartTransmitAll(IOEventFlags device) { // Get the next character to transmit. We will have reached the end when // the value of the character to transmit is -1. int c = jshGetCharToTransmit(device); while (c >= 0) { uart_tx_one_char(0, c); c = jshGetCharToTransmit(device); } // No more characters to transmit } // End of esp8266_transmitAll
/*JSON{ "type" : "idle", "generate" : "jswrap_nrf_idle" }*/ bool jswrap_nrf_idle() { static uint8_t buf[BLE_NUS_MAX_DATA_LEN]; int idx = 0; int ch = jshGetCharToTransmit(EV_BLUETOOTH); while (ch>=0 && idx<BLE_NUS_MAX_DATA_LEN) { buf[idx++] = ch; ch = jshGetCharToTransmit(EV_BLUETOOTH); } if (idx>0) ble_nus_string_send(&m_nus, buf, idx); return idx>0; }
/** * @brief Handle_USBAsynchXfer * Send data to USB * @param pdev: instance * @retval None */ static void Handle_USBAsynchXfer (void *pdev) { if(USB_Tx_State != 1) { unsigned char USB_TX_Buffer[CDC_DATA_IN_PACKET_SIZE]; int USB_Tx_length = 0; // try and fill the buffer int c; while (USB_Tx_length<CDC_DATA_IN_PACKET_SIZE && ((c = jshGetCharToTransmit(EV_USBSERIAL)) >=0) ) { // get byte to transmit USB_TX_Buffer[USB_Tx_length++] = c; } // if nothing, set state to 0 if (USB_Tx_length==0) { USB_Tx_State = 0; return; } USB_Tx_State = 1; DCD_EP_Tx (pdev, CDC_IN_EP, (uint8_t*)USB_TX_Buffer, USB_Tx_length); } }
/** * @brief usbd_audio_DataIn * Data sent on non-control IN endpoint * @param pdev: device instance * @param epnum: endpoint number * @retval status */ static uint8_t usbd_cdc_DataIn (void *pdev, uint8_t epnum) { if (USB_Tx_State == 1) { unsigned char USB_TX_Buffer[CDC_DATA_IN_PACKET_SIZE]; int USB_Tx_length = 0; // try and fill the buffer int c; while (USB_Tx_length<CDC_DATA_IN_PACKET_SIZE && ((c = jshGetCharToTransmit(EV_USBSERIAL)) >= 0) ) { // get byte to transmit USB_TX_Buffer[USB_Tx_length++] = c; } // if nothing, set state to 0 if (USB_Tx_length==0) { USB_Tx_State = 0; return USBD_OK; } /* Prepare the available data buffer to be sent on IN endpoint */ DCD_EP_Tx (pdev, CDC_IN_EP, (uint8_t*)USB_TX_Buffer, USB_Tx_length); } return USBD_OK; }
/******************************************************************************* * Function Name : EP1_IN_Callback * Description : * Input : None. * Output : None. * Return : None. *******************************************************************************/ void EP1_IN_Callback (void) { if (USB_Tx_State == 1) { unsigned char USB_TX_Buffer[VIRTUAL_COM_PORT_DATA_SIZE]; int USB_Tx_length = 0; // try and fill the buffer int c; while (USB_Tx_length<VIRTUAL_COM_PORT_DATA_SIZE && ((c = jshGetCharToTransmit(EV_USBSERIAL)) >= 0) ) { // get byte to transmit USB_TX_Buffer[USB_Tx_length++] = c; } // if nothing, set state to 0 if (USB_Tx_length==0) { USB_Tx_State = 0; return; } // else send data and keep going UserToPMABufferCopy(&USB_TX_Buffer[0], ENDP1_TXADDR, USB_Tx_length); SetEPTxCount(ENDP1, USB_Tx_length); SetEPTxValid(ENDP1); } }
/******************************************************************************* * Function Name : Handle_USBAsynchXfer. * Description : send data to USB. * Input : None. * Return : none. *******************************************************************************/ void Handle_USBAsynchXfer (void) { if(USB_Tx_State != 1) { unsigned char USB_TX_Buffer[VIRTUAL_COM_PORT_DATA_SIZE]; int USB_Tx_length = 0; // try and fill the buffer int c; while (USB_Tx_length<VIRTUAL_COM_PORT_DATA_SIZE && ((c = jshGetCharToTransmit(EV_USBSERIAL)) >=0) ) { // get byte to transmit USB_TX_Buffer[USB_Tx_length++] = c; } // if nothing, set state to 0 if (USB_Tx_length==0) { USB_Tx_State = 0; return; } USB_Tx_State = 1; #ifdef USE_STM3210C_EVAL USB_SIL_Write(EP1_IN, &USB_TX_Buffer[0], USB_Tx_length); #else UserToPMABufferCopy(&USB_TX_Buffer[0], ENDP1_TXADDR, USB_Tx_length); SetEPTxCount(ENDP1, USB_Tx_length); SetEPTxValid(ENDP1); #endif /* USE_STM3210C_EVAL */ } }
static void USART_IRQHandler(USART_TypeDef *USART, IOEventFlags device) { if (USART_GetFlagStatus(USART, USART_FLAG_FE) != RESET) { // If we have a framing error, push status info onto the event queue jshPushIOEvent( IOEVENTFLAGS_SERIAL_TO_SERIAL_STATUS(device) | EV_SERIAL_STATUS_FRAMING_ERR, 0); } if (USART_GetFlagStatus(USART, USART_FLAG_PE) != RESET) { // If we have a parity error, push status info onto the event queue jshPushIOEvent( IOEVENTFLAGS_SERIAL_TO_SERIAL_STATUS(device) | EV_SERIAL_STATUS_PARITY_ERR, 0); } if(USART_GetITStatus(USART, USART_IT_RXNE) != RESET) { /* Clear the USART Receive interrupt */ USART_ClearITPendingBit(USART, USART_IT_RXNE); /* Read one byte from the receive data register */ jshPushIOCharEvent(device, (char)USART_ReceiveData(USART)); } /* If overrun condition occurs, clear the ORE flag and recover communication */ if (USART_GetFlagStatus(USART, USART_FLAG_ORE) != RESET) { (void)USART_ReceiveData(USART); } if(USART_GetITStatus(USART, USART_IT_TXE) != RESET) { /* If we have other data to send, send it */ int c = jshGetCharToTransmit(device); if (c >= 0) { USART_SendData(USART, (uint16_t)c); } else USART_ITConfig(USART, USART_IT_TXE, DISABLE); } }
/** Kick a device into action (if required). For instance we may need * to set up interrupts */ void jshUSARTKick(IOEventFlags device) { int c; while ((c = jshGetCharToTransmit(EV_SERIAL1)) >= 0) { Serial.write((char)c); Serial.print(":"); } }
void jshInputThread() { while (isInitialised) { bool shortSleep = false; /* Handle the delayed Ctrl-C -> interrupt behaviour (see description by EXEC_CTRL_C's definition) */ if (execInfo.execute & EXEC_CTRL_C_WAIT) execInfo.execute = (execInfo.execute & ~EXEC_CTRL_C_WAIT) | EXEC_INTERRUPTED; if (execInfo.execute & EXEC_CTRL_C) execInfo.execute = (execInfo.execute & ~EXEC_CTRL_C) | EXEC_CTRL_C_WAIT; // Read from the console while (kbhit()) { int ch = getch(); if (ch<0) break; jshPushIOCharEvent(EV_USBSERIAL, (char)ch); } // Read from any open devices - if we have space if (jshGetEventsUsed() < IOBUFFERMASK/2) { int i; for (i=0;i<=EV_DEVICE_MAX;i++) { if (ioDevices[i]) { char buf[32]; // read can return -1 (EAGAIN) because O_NONBLOCK is set int bytes = (int)read(ioDevices[i], buf, sizeof(buf)); if (bytes>0) { //int j; for (j=0;j<bytes;j++) printf("]] '%c'\r\n", buf[j]); jshPushIOCharEvents(i, buf, (unsigned int)bytes); shortSleep = true; } } } } // Write any data we have IOEventFlags device = jshGetDeviceToTransmit(); while (device != EV_NONE) { char ch = (char)jshGetCharToTransmit(device); //printf("[[ '%c'\r\n", ch); if (ioDevices[device]) { write(ioDevices[device], &ch, 1); shortSleep = true; } device = jshGetDeviceToTransmit(); } #ifdef SYSFS_GPIO_DIR Pin pin; for (pin=0;pin<JSH_PIN_COUNT;pin++) if (gpioShouldWatch[pin]) { shortSleep = true; bool state = jshPinGetValue(pin); if (state != gpioLastState[pin]) { jshPushIOEvent(pinToEVEXTI(pin) | (state?EV_EXTI_IS_HIGH:0), jshGetSystemTime()); gpioLastState[pin] = state; } } #endif usleep(shortSleep ? 1000 : 50000); } }
/** Kick a device into action (if required). For instance we may need * to set up interrupts */ void jshUSARTKick(IOEventFlags device) { int id = 0; // TODO: device int c = jshGetCharToTransmit(device); if (c >= 0) { serial_irq_set(&mbedSerial[id], TxIrq, 1); serial_putc(&mbedSerial[id], c); } }
bool jswrap_nrf_transmit_string() { if (m_conn_handle == BLE_CONN_HANDLE_INVALID) { // If no connection, drain the output buffer while (jshGetCharToTransmit(EV_BLUETOOTH)>=0); } if (bleStatus & BLE_IS_SENDING) return false; static uint8_t buf[BLE_NUS_MAX_DATA_LEN]; int idx = 0; int ch = jshGetCharToTransmit(EV_BLUETOOTH); while (ch>=0) { buf[idx++] = ch; if (idx>=BLE_NUS_MAX_DATA_LEN) break; ch = jshGetCharToTransmit(EV_BLUETOOTH); } if (idx>0) { if (ble_nus_string_send(&m_nus, buf, idx) == NRF_SUCCESS) bleStatus |= BLE_IS_SENDING; } return idx>0; }
// --------------------------------------------------- void mbedSerialIRQ(uint32_t id, SerialIrq event) { IOEventFlags device = EV_SERIAL1; // TODO: device if (event == RxIrq) { if (serial_readable(&mbedSerial[id])) jshPushIOCharEvent(device, (char)serial_getc(&mbedSerial[id])); } if (event == TxIrq) { int c = jshGetCharToTransmit(device); if (c >= 0) { serial_putc(&mbedSerial[id], c); } else serial_irq_set(&mbedSerial[id], TxIrq, 0); } }
/** Kick a device into action (if required). For instance we may need to set up interrupts */ void jshUSARTKick(IOEventFlags device) { if (device != EV_SERIAL1) { return; } int check_valid_char = jshGetCharToTransmit(EV_SERIAL1); if (check_valid_char >= 0) { uint8_t character = (uint8_t) check_valid_char; nrf_utils_app_uart_put(character); } }
void uart0_event_handle(app_uart_evt_t * p_event) { if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR) { jshPushIOEvent(IOEVENTFLAGS_SERIAL_TO_SERIAL_STATUS(EV_SERIAL1) | EV_SERIAL_STATUS_FRAMING_ERR, 0); } else if (p_event->evt_type == APP_UART_TX_EMPTY) { int ch = jshGetCharToTransmit(EV_SERIAL1); if (ch >= 0) { uartIsSending = true; while (app_uart_put((uint8_t)ch) != NRF_SUCCESS); } else uartIsSending = false; } else if (p_event->evt_type == APP_UART_DATA) { uint8_t character; while (app_uart_get(&character) != NRF_SUCCESS); jshPushIOCharEvent(EV_SERIAL1, (char) character); } }
static void USART_IRQHandler(USART_TypeDef *USART, IOEventFlags device) { if(USART_GetITStatus(USART, USART_IT_RXNE) != RESET) { /* Clear the USART Receive interrupt */ USART_ClearITPendingBit(USART, USART_IT_RXNE); /* Read one byte from the receive data register */ jshPushIOCharEvent(device, (char)USART_ReceiveData(USART)); } /* If overrun condition occurs, clear the ORE flag and recover communication */ if (USART_GetFlagStatus(USART, USART_FLAG_ORE) != RESET) { (void)USART_ReceiveData(USART); } if(USART_GetITStatus(USART, USART_IT_TXE) != RESET) { /* If we have other data to send, send it */ int c = jshGetCharToTransmit(device); if (c >= 0) { USART_SendData(USART, (uint16_t)c); } else USART_ITConfig(USART, USART_IT_TXE, DISABLE); } }
/** Kick a device into action (if required). For instance we may need to set up interrupts */ void jshUSARTKick(IOEventFlags device) { if (device == EV_BLUETOOTH) { /* For bluetooth, start transmit after one character. The BLE_EVT_TX_COMPLETE event will get triggered and will auto-reload whatever needs sending. */ bool jswrap_nrf_transmit_string(); jswrap_nrf_transmit_string(); } if (device == EV_SERIAL1 && !uartIsSending) { jshInterruptOff(); int ch = jshGetCharToTransmit(EV_SERIAL1); if (ch >= 0) { // put data - this will kick off the USART while (app_uart_put((uint8_t)ch) != NRF_SUCCESS); uartIsSending = true; } jshInterruptOn(); } }