Exemplo n.º 1
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    int is_stdio_uart = 0;
    
    // determine the UART to use
    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
    UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
    if ((int)uart == NC) {
        error("Serial pinout mapping failed");
    }
    
    obj->uart = (LPC_UART_TypeDef *)uart;
    // enable power
    switch (uart) {
        case UART_0: LPC_SC->PCONP |= 1 <<  3; break;
        case UART_1: LPC_SC->PCONP |= 1 <<  4; break;
        case UART_2: LPC_SC->PCONP |= 1 << 24; break;
        case UART_3: LPC_SC->PCONP |= 1 << 25; break;
    }
    
    // enable fifos and default rx trigger level
    obj->uart->FCR = 1 << 0  // FIFO Enable - 0 = Disables, 1 = Enabled
                   | 0 << 1  // Rx Fifo Reset
                   | 0 << 2  // Tx Fifo Reset
                   | 0 << 6; // Rx irq trigger level - 0 = 1 char, 1 = 4 chars, 2 = 8 chars, 3 = 14 chars

    // disable irqs
    obj->uart->IER = 0 << 0  // Rx Data available irq enable
                   | 0 << 1  // Tx Fifo empty irq enable
                   | 0 << 2; // Rx Line Status irq enable
    
    // set default baud rate and format
    serial_baud  (obj, 9600);
    serial_format(obj, 8, ParityNone, 1);
    
    // pinout the chosen uart
    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);
    
    // set rx/tx pins in PullUp mode
    pin_mode(tx, PullUp);
    pin_mode(rx, PullUp);
    
    switch (uart) {
        case UART_0: obj->index = 0; break;
        case UART_1: obj->index = 1; break;
        case UART_2: obj->index = 2; break;
        case UART_3: obj->index = 3; break;
    }
    uart_data[obj->index].sw_rts.pin = NC;
    uart_data[obj->index].sw_cts.pin = NC;
    serial_set_flow_control(obj, FlowControlNone, NC, NC);
    
    is_stdio_uart = (uart == STDIO_UART) ? (1) : (0);
    
    if (is_stdio_uart) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Exemplo n.º 2
0
void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) {
    FlowControl flow_type = (FlowControl)type;
    switch(type) {
    case RTS:
        serial_set_flow_control(&_serial, flow_type, flow1, NC);
        break;

    case CTS:
        serial_set_flow_control(&_serial, flow_type, NC, flow1);
        break;

    case RTSCTS:
    case Disabled:
        serial_set_flow_control(&_serial, flow_type, flow1, flow2);
        break;

    default:
        break;
    }
}
void UARTClass::begin(const uint32_t BaudRate, uint32_t rx_pin, uint32_t tx_pin)
{
	PinName	pin_rx, pin_tx;
	UARTName uart = UART_0;
	serial_t obj;
	
	pin_rx = Pin_nRF51822_to_Arduino(rx_pin);
	pin_tx = Pin_nRF51822_to_Arduino(tx_pin);
	
	obj.uart = (NRF_UART_Type *)uart;
	serial_init(&obj, pin_tx, pin_rx);
	serial_set_flow_control(&obj, FlowControlNone, (PinName)NC, (PinName)NC);
	serial_baud(&obj, BaudRate);
	/* TXIrq has a error, don't use it */
	serial_irq_set(&obj, RxIrq, 1);
	//serial_irq_set(&obj, TxIrq, 1);
}
Exemplo n.º 4
0
// This function sets the current object as the "listening"
// one and returns true if it replaces another 
bool SoftwareSerial::listen()
{
    bool ret = false;

    serial_init((serial_t *)pUART, (PinName)g_APinDescription[transmitPin].pinname, (PinName)g_APinDescription[receivePin].pinname);
    serial_baud((serial_t *)pUART, speed);
    serial_format((serial_t *)pUART, data_bits, (SerialParity)parity, stop_bits);

    serial_irq_handler((serial_t *)pUART, (uart_irq_handler)handle_interrupt, (uint32_t)this);
    serial_irq_set((serial_t *)pUART, RxIrq, 1);
    serial_irq_set((serial_t *)pUART, TxIrq, 1);

    if (flowctrl) {
        serial_set_flow_control((serial_t *)pUART, FlowControlRTSCTS, (PinName)0, (PinName)0);
    }

    return ret;
}