Ejemplo n.º 1
0
/** Open a serial connection and applies settings to the connection.

    @param deviceFile The serial device to open (often /dev/ttyUSB0 or /dev/ttyACM0)
    @param speed The baud rate to be applied to the connection.
    @param parity 0=no parity; 1=odd parity; 2=even parity
    @param vmin 0 = nonblocking; if >1, block until we have received at least vmin bytes
    @param vtime If blocking, tenths of a second we should block until we give up.

    @return The file descriptor for the serial connection.
*/
int serial_open(const char *deviceFile, int speed, int parity, int vmin, int vtime)
{
	msg(DEBUG, "Opening serial connection to %s at %d baud\n", deviceFile, speed);
	int fd = 0;

	for(int i=0; i<10; i++)
	{
		if(i > 0 && fd == -1)
		{
			msg(ERROR, "Could not open serial connection to '%s', retrying...\n", deviceFile);
			sleep(1); // Give user time to plug in cable.
		}

#ifndef __MINGW32__
		fd = open(deviceFile, O_RDWR | O_NOCTTY);
#else
		fd = open(deviceFile, O_RDWR);
#endif
	}
	if(fd == -1)
	{
		msg(ERROR, "Failed to connect to '%s', giving up.\n", deviceFile);
		exit(EXIT_FAILURE);
	}
	
	if(!isatty(fd))
	{
		msg(FATAL, "'%s' is not a tty.\n");
		exit(EXIT_FAILURE);
	}

#ifndef __MINGW32__
	serial_settings(fd, speed, parity, vmin, vtime);
#endif

	msg(DEBUG, "Serial connection to '%s' is open on fd=%d.\n", deviceFile, fd);
	return fd;
}
Ejemplo n.º 2
0
static void transmit_task(rtems_task_argument arg)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  uart_bridge_master_control *control = (uart_bridge_master_control *) arg;
  rtems_chain_control *fifo = &control->transmit_fifo;

  int fd = open(control->device_path, O_WRONLY);
  assert(fd >= 0);

  serial_settings(fd);

  while (true) {
    intercom_packet *packet = NULL;
    sc = rtems_chain_get_with_wait(
      fifo,
      TRANSMIT_EVENT,
      RTEMS_NO_TIMEOUT,
      (rtems_chain_node **) &packet
    );
    assert(sc == RTEMS_SUCCESSFUL);
    write(fd, packet->data, packet->size);
    qoriq_intercom_free_packet(packet);
  }
}
Ejemplo n.º 3
0
static void receive_task(rtems_task_argument arg)
{
  uart_bridge_master_control *control = (uart_bridge_master_control *) arg;
  intercom_type type = control->type;

  int fd = open(control->device_path, O_RDONLY);
  assert(fd >= 0);

  serial_settings(fd);

  while (true) {
    intercom_packet *packet = qoriq_intercom_allocate_packet(
      type,
      INTERCOM_SIZE_64
    );
    ssize_t in = read(fd, packet->data, packet->size - 1);
    if (in > 0) {
      packet->size = (size_t) in;
      qoriq_intercom_send_packet(QORIQ_UART_BRIDGE_SLAVE_CORE, packet);
    } else {
      qoriq_intercom_free_packet(packet);
    }
  }
}