Ejemplo n.º 1
0
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);
    }
}
Ejemplo n.º 2
0
/**@snippet [Handling the data received over BLE] */
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{
    uint32_t i;
    for (i = 0; i < length; i++) {
      jshPushIOCharEvent(EV_BLUETOOTH, (char) p_data[i]);
    }
}
Ejemplo n.º 3
0
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);
  }
}
Ejemplo n.º 4
0
void jshIdle() {
  /*static bool foo = false;
  foo = !foo;
  jshPinSetValue(LED1_PININDEX, foo);*/

  while (serial_readable(&mbedSerial[0])>0)
        jshPushIOCharEvent(EV_SERIAL1, serial_getc(&mbedSerial[0]));
}
Ejemplo n.º 5
0
/**
 * Queue a character for transmission.
 */
void jshTransmit(
    IOEventFlags device, //!< The device to be used for transmission.
    unsigned char data   //!< The character to transmit.
  ) {
  if (device==EV_LOOPBACKA || device==EV_LOOPBACKB) {
    jshPushIOCharEvent(device==EV_LOOPBACKB ? EV_LOOPBACKA : EV_LOOPBACKB, (char)data);
    return;
  }
#ifndef LINUX
#ifdef USB
  if (device==EV_USBSERIAL && !jshIsUSBSERIALConnected()) {
    jshTransmitClearDevice(EV_USBSERIAL); // clear out stuff already waiting
    return;
  }
#endif
#else // if PC, just put to stdout
  if (device==DEFAULT_CONSOLE_DEVICE) {
    fputc(data, stdout);
    fflush(stdout);
    return;
  }
#endif
  // If the device is EV_NONE then there is nowhere to send the data.
  if (device==EV_NONE) return;

  // The txHead global points to the current item in the txBuffer.  Since we are adding a new
  // character, we increment the head pointer.   If it has caught up with the tail, then that means
  // we have filled the array backing the list.  What we do next is to wait for space to free up.
  unsigned char txHeadNext = (unsigned char)((txHead+1)&TXBUFFERMASK);
  if (txHeadNext==txTail) {
    jsiSetBusy(BUSY_TRANSMIT, true);
    bool wasConsoleLimbo = device==EV_LIMBO && jsiGetConsoleDevice()==EV_LIMBO;
    while (txHeadNext==txTail) {
      // wait for send to finish as buffer is about to overflow
#ifdef USB
      // just in case USB was unplugged while we were waiting!
      if (!jshIsUSBSERIALConnected()) jshTransmitClearDevice(EV_USBSERIAL);
#endif
    }
    if (wasConsoleLimbo && jsiGetConsoleDevice()!=EV_LIMBO) {
      /* It was 'Limbo', but now it's not - see jsiOneSecondAfterStartup.
      Basically we must have printed a bunch of stuff to LIMBO and blocked
      with our output buffer full. But then jsiOneSecondAfterStartup
      switches to the right console device and swaps everything we wrote
      over to that device too. Only we're now here, still writing to the
      old device when really we should be writing to the new one. */
      device = jsiGetConsoleDevice();
    }
    jsiSetBusy(BUSY_TRANSMIT, false);
  }
  // Save the device and data for the new character to be transmitted.
  txBuffer[txHead].flags = device;
  txBuffer[txHead].data = data;
  txHead = txHeadNext;

  jshUSARTKick(device); // set up interrupts if required
}
Ejemplo n.º 6
0
void jshIdle() {
  // hack in order to get console device set correctly
  static bool inited = false;
  if (!inited) {
    inited = true;
    jsiOneSecondAfterStartup();
  }

  while (Serial.available() > 0) {
    jshPushIOCharEvent(EV_SERIAL1, Serial.read());
  }
}
Ejemplo n.º 7
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);
  }
}
Ejemplo n.º 8
0
void jshIdle() {
  // hack in order to get console device set correctly
  static bool inited = false;
  if (!inited) {
    inited = true;
    jsiOneSecondAfterStartup();
  }

  /*static bool foo = false;
  foo = !foo;
  jshPinSetValue(LED1_PININDEX, foo);*/

  while (serial_readable(&mbedSerial[0])>0)
        jshPushIOCharEvent(EV_SERIAL1, serial_getc(&mbedSerial[0]));
}
Ejemplo n.º 9
0
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);
  }
}
Ejemplo n.º 10
0
// UART callback function. Registered in uart_init(). Allows to asychronously read characters from UART.
void uart_event_handle(app_uart_evt_t * p_event)
{
  if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
  {
    APP_ERROR_HANDLER(p_event->data.error_communication);
  }
  else if (p_event->evt_type == APP_UART_FIFO_ERROR)
  {
    APP_ERROR_HANDLER(p_event->data.error_code);
  }
  else if (p_event->evt_type == APP_UART_DATA_READY) // There is one byte in the RX FIFO.
  {
    uint8_t character;
    while(app_uart_get(&character) != NRF_SUCCESS); // Non blocking.
    jshPushIOCharEvent(EV_SERIAL1, (char) character);
  }
}
Ejemplo n.º 11
0
void jshIdle() {
  while (kbhit()) {
    jshPushIOCharEvent(EV_USBSERIAL, (char)getch());
  }

#ifdef SYSFS_GPIO_DIR
  Pin pin;
  for (pin=0;pin<JSH_PIN_COUNT;pin++)
    if (gpioShouldWatch[pin]) {
      bool state = jshPinGetValue(pin);
      if (state != gpioLastState[pin]) {
        jshPushIOEvent(pinToEVEXTI(pin) | (state?EV_EXTI_IS_HIGH:0), jshGetSystemTime());
        gpioLastState[pin] = state;
      }
    }
#endif
}
Ejemplo n.º 12
0
/*******************************************************************************
* Function Name  : EP3_OUT_Callback
* Description    :
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void EP3_OUT_Callback(void)
{
  uint8_t USB_Rx_Buffer[VIRTUAL_COM_PORT_DATA_SIZE];
  int USB_Rx_Cnt;
  
  /* Get the received data buffer and update the counter */
  USB_Rx_Cnt = USB_SIL_Read(EP3_OUT, USB_Rx_Buffer);
  
  /* USB data will be immediately processed, this allow next USB traffic being 
  NAKed till the end of the USART Xfer */
  
  int i=0;
  for (i=0;i<USB_Rx_Cnt;i++)
    jshPushIOCharEvent(EV_USBSERIAL, USB_Rx_Buffer[i]);
 
  /* Enable the receive of data on EP3 */
  SetEPRxStatus(ENDP3, jshHasEventSpaceForChars(VIRTUAL_COM_PORT_DATA_SIZE) ? EP_RX_VALID : EP_RX_NAK);
}
Ejemplo n.º 13
0
/**@snippet [Handling the data received over BLE] */
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{
	uint32_t i;

	if (ble_com)
	{
		for (i = 0; i < length; i++)
		{
			jshPushIOCharEvent(EV_SERIAL1, (char) p_data[i]);
		}
	}
	else
	{
		for (i = 0; i < length; i++)
		{
			jsiConsolePrintChar((char) p_data[i]);
		}
	}
}
Ejemplo n.º 14
0
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);
   }
}
Ejemplo n.º 15
0
// Queue a character for transmission
void jshTransmit(IOEventFlags device, unsigned char data) {
  if (device==EV_LOOPBACKA || device==EV_LOOPBACKB) {
    jshPushIOCharEvent(device==EV_LOOPBACKB ? EV_LOOPBACKA : EV_LOOPBACKB, (char)data);
    return;
  }
#ifndef LINUX
#ifdef USB
  if (device==EV_USBSERIAL && !jshIsUSBSERIALConnected()) {
    jshTransmitClearDevice(EV_USBSERIAL); // clear out stuff already waiting
    return;
  }
#endif
  if (device==EV_NONE) return;
  unsigned char txHeadNext = (txHead+1)&TXBUFFERMASK;
  if (txHeadNext==txTail) {
    jsiSetBusy(BUSY_TRANSMIT, true);
    while (txHeadNext==txTail) {
      // wait for send to finish as buffer is about to overflow
#ifdef USB
      // just in case USB was unplugged while we were waiting!
      if (!jshIsUSBSERIALConnected()) jshTransmitClearDevice(EV_USBSERIAL);
#endif
    }
    jsiSetBusy(BUSY_TRANSMIT, false);
  }
  txBuffer[txHead].flags = device;
  txBuffer[txHead].data = (char)data;
  txHead = txHeadNext;

  jshUSARTKick(device); // set up interrupts if required

#else // if PC, just put to stdout
  if (device==DEFAULT_CONSOLE_DEVICE) {
    fputc(data, stdout);
    fflush(stdout);
  }
#endif
}
Ejemplo n.º 16
0
void jshIdle() {
   while (Serial.available() > 0) {
      jshPushIOCharEvent(EV_SERIAL1, Serial.read());
   } 
}