Esempio n. 1
0
//TODO: implement sanity checks.
hal_uart_port hal_uart_open(hal_uart_port port, hal_uart_baudrate baudrate,
                    hal_uart_parity parity,
                    hal_uart_stop_bits stop_bits, 
    hal_uart_on_data_received_callback data_received){
    on_data_received[port] = data_received;
    assert(port >= HAL_UART_PORT_1 && port <= HAL_UART_NUMBER_OF_PORTS );
    /* Configure uart */
    UART_MODULE uart = logic_uart2phy_uart(port);
    SetRxTxPins(uart);
    UARTConfigure(uart, UART_ENABLE_PINS_TX_RX_ONLY);
    UARTSetFifoMode(uart, UART_INTERRUPT_ON_TX_DONE | UART_INTERRUPT_ON_RX_NOT_EMPTY);
    UARTSetLineControl(uart, UART_DATA_SIZE_8_BITS | get_parity(parity) | get_stop_bits(stop_bits));
    UARTSetDataRate(uart, PIC32_PERIPHERALBUS_FREQ, get_baudrate(baudrate));
    UARTEnable(uart, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX));
    INTClearFlag(INT_SOURCE_UART_RX(uart));
    INTEnable(INT_SOURCE_UART_RX(uart),INT_ENABLED);
    return port;
}
Esempio n. 2
0
File: comm.c Progetto: horazont/hint
int _comm_open(struct comm_t *state)
{
    int fd = open(state->_devfile, O_RDWR|O_CLOEXEC|O_NOCTTY);
    if (fd < 0) {
        return -1;
    }

    if (fcntl(fd, F_SETFL, O_NONBLOCK) != 0) {
        fprintf(stderr, "fcntl(comm_device, F_SETFL, O_NONBLOCK) failed: %d: %s\n", errno, strerror(errno));
    }

    struct termios port_settings;
    memset(&port_settings, 0, sizeof(port_settings));
    speed_t speed = get_baudrate(state->_baudrate);
    cfsetispeed(&port_settings, speed);
    cfsetospeed(&port_settings, speed);
    cfmakeraw(&port_settings);
    tcsetattr(fd, TCSANOW, &port_settings);
    tcflush(fd, TCIOFLUSH);

    state->_fd = fd;
    return 0;
}