Exemplo n.º 1
0
void
uart_close_ext(const serial_port sp, const bool restore_termios)
{
  if (UART_DATA(sp)->fd >= 0) {
    if (restore_termios)
      tcsetattr(UART_DATA(sp)->fd, TCSANOW, &UART_DATA(sp)->termios_backup);
    close(UART_DATA(sp)->fd);
  }
  free(sp);
}
Exemplo n.º 2
0
void
uart_set_speed(serial_port sp, const uint32_t uiPortSpeed)
{
  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Serial port speed requested to be set to %d bauds.", uiPortSpeed);

  // Portability note: on some systems, B9600 != 9600 so we have to do
  // uint32_t <=> speed_t associations by hand.
  speed_t stPortSpeed = B9600;
  switch (uiPortSpeed) {
    case 9600:
      stPortSpeed = B9600;
      break;
    case 19200:
      stPortSpeed = B19200;
      break;
    case 38400:
      stPortSpeed = B38400;
      break;
#  ifdef B57600
    case 57600:
      stPortSpeed = B57600;
      break;
#  endif
#  ifdef B115200
    case 115200:
      stPortSpeed = B115200;
      break;
#  endif
#  ifdef B230400
    case 230400:
      stPortSpeed = B230400;
      break;
#  endif
#  ifdef B460800
    case 460800:
      stPortSpeed = B460800;
      break;
#  endif
    default:
      log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set serial port speed to %d bauds. Speed value must be one of those defined in termios(3).",
              uiPortSpeed);
      return;
  };

  // Set port speed (Input and Output)
  cfsetispeed(&(UART_DATA(sp)->termios_new), stPortSpeed);
  cfsetospeed(&(UART_DATA(sp)->termios_new), stPortSpeed);
  if (tcsetattr(UART_DATA(sp)->fd, TCSADRAIN, &(UART_DATA(sp)->termios_new)) == -1) {
    log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to apply new speed settings.");
  }
}
Exemplo n.º 3
0
static
VOID
UartpCom2Write
    (
        CHAR c
    )
{
    *UART_DATA(UART2_BASE) = c;
}
Exemplo n.º 4
0
/**
 * @brief Send \a pbtTx content to UART
 *
 * @return 0 on success, otherwise a driver error is returned
 */
int
uart_send(serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout)
{
  (void) timeout;
  LOG_HEX(LOG_GROUP, "TX", pbtTx, szTx);
  if ((int) szTx == write(UART_DATA(sp)->fd, pbtTx, szTx))
    return NFC_SUCCESS;
  else
    return NFC_EIO;
}
Exemplo n.º 5
0
void
uart_flush_input(serial_port sp)
{
  // This line seems to produce absolutely no effect on my system (GNU/Linux 2.6.35)
  tcflush(UART_DATA(sp)->fd, TCIFLUSH);
  // So, I wrote this byte-eater
  // Retrieve the count of the incoming bytes
  int available_bytes_count = 0;
  int res;
  res = ioctl(UART_DATA(sp)->fd, FIONREAD, &available_bytes_count);
  if (res != 0) {
    return;
  }
  if (available_bytes_count == 0) {
    return;
  }
  char *rx = malloc(available_bytes_count);
  // There is something available, read the data
  res = read(UART_DATA(sp)->fd, rx, available_bytes_count);
  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d bytes have eatten.", available_bytes_count);
  free(rx);
}
Exemplo n.º 6
0
void
uart_flush_input(serial_port sp, bool wait)
{
  // flush commands may seem to be without effect
  // if asked too quickly after previous event, cf comments below
  // therefore a "wait" argument allows now to wait before flushing
  // I believe that now the byte-eater part is not required anymore --Phil
  if (wait) {
    msleep(50); // 50 ms
  }

  // This line seems to produce absolutely no effect on my system (GNU/Linux 2.6.35)
  tcflush(UART_DATA(sp)->fd, TCIFLUSH);
  // So, I wrote this byte-eater
  // Retrieve the count of the incoming bytes
  int available_bytes_count = 0;
  int res;
  res = ioctl(UART_DATA(sp)->fd, FIONREAD, &available_bytes_count);
  if (res != 0) {
    return;
  }
  if (available_bytes_count == 0) {
    return;
  }
  char *rx = malloc(available_bytes_count);
  if (!rx) {
    perror("malloc");
    return;
  }
  // There is something available, read the data
  if (read(UART_DATA(sp)->fd, rx, available_bytes_count) < 0) {
    perror("uart read");
    free(rx);
    return;
  }
  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d bytes have eaten.", available_bytes_count);
  free(rx);
}
Exemplo n.º 7
0
uint32_t
uart_get_speed(serial_port sp)
{
  uint32_t uiPortSpeed = 0;
  switch (cfgetispeed(&UART_DATA(sp)->termios_new)) {
    case B9600:
      uiPortSpeed = 9600;
      break;
    case B19200:
      uiPortSpeed = 19200;
      break;
    case B38400:
      uiPortSpeed = 38400;
      break;
#  ifdef B57600
    case B57600:
      uiPortSpeed = 57600;
      break;
#  endif
#  ifdef B115200
    case B115200:
      uiPortSpeed = 115200;
      break;
#  endif
#  ifdef B230400
    case B230400:
      uiPortSpeed = 230400;
      break;
#  endif
#  ifdef B460800
    case B460800:
      uiPortSpeed = 460800;
      break;
#  endif
  }

  return uiPortSpeed;
}
Exemplo n.º 8
0
static
CHAR
UartpCom2Read()
{
    return *UART_DATA(UART2_BASE);
}
Exemplo n.º 9
0
static
CHAR
UartpCom1Read()
{
    return *UART_DATA(UART1_BASE);
}
Exemplo n.º 10
0
/**
 * @brief Receive data from UART and copy data to \a pbtRx
 *
 * @return 0 on success, otherwise driver error code
 */
int
uart_receive(serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout)
{
  int iAbortFd = abort_p ? *((int *)abort_p) : 0;
  int received_bytes_count = 0;
  int available_bytes_count = 0;
  const int expected_bytes_count = (int)szRx;
  int res;
  fd_set rfds;
  do {
select:
    // Reset file descriptor
    FD_ZERO(&rfds);
    FD_SET(UART_DATA(sp)->fd, &rfds);

    if (iAbortFd) {
      FD_SET(iAbortFd, &rfds);
    }

    struct timeval timeout_tv;
    if (timeout > 0) {
      timeout_tv.tv_sec = (timeout / 1000);
      timeout_tv.tv_usec = ((timeout % 1000) * 1000);
    }

    res = select(MAX(UART_DATA(sp)->fd, iAbortFd) + 1, &rfds, NULL, NULL, timeout ? &timeout_tv : NULL);

    if ((res < 0) && (EINTR == errno)) {
      // The system call was interupted by a signal and a signal handler was
      // run.  Restart the interupted system call.
      goto select;
    }

    // Read error
    if (res < 0) {
      log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Error: %s", strerror(errno));
      return NFC_EIO;
    }
    // Read time-out
    if (res == 0) {
      log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "Timeout!");
      return NFC_ETIMEOUT;
    }

    if (FD_ISSET(iAbortFd, &rfds)) {
      // Abort requested
      log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "Abort!");
      close(iAbortFd);
      return NFC_EOPABORTED;
    }

    // Retrieve the count of the incoming bytes
    res = ioctl(UART_DATA(sp)->fd, FIONREAD, &available_bytes_count);
    if (res != 0) {
      return NFC_EIO;
    }
    // There is something available, read the data
    res = read(UART_DATA(sp)->fd, pbtRx + received_bytes_count, MIN(available_bytes_count, (expected_bytes_count - received_bytes_count)));
    // Stop if the OS has some troubles reading the data
    if (res <= 0) {
      return NFC_EIO;
    }
    received_bytes_count += res;

  } while (expected_bytes_count > received_bytes_count);
  LOG_HEX(LOG_GROUP, "RX", pbtRx, szRx);
  return NFC_SUCCESS;
}