Пример #1
0
void serial_init(serial_t *obj, PinName tx, PinName rx)
{
    // Determine the UART to use (UART_1, UART_2, ...)
    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);

    // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
    obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
    MBED_ASSERT(obj->uart != (UARTName)NC);

    // Enable USART clock
    switch (obj->uart) {
        case UART_1:
            __USART1_CLK_ENABLE();
            obj->index = 0;
            break;
        case UART_2:
            __USART2_CLK_ENABLE();
            obj->index = 1;
            break;
#if defined(USART3_BASE)
        case UART_3:
            __USART3_CLK_ENABLE();
            obj->index = 2;
            break;
#endif
#if defined(UART4_BASE)
        case UART_4:
            __UART4_CLK_ENABLE();
            obj->index = 3;
            break;
#endif
#if defined(UART5_BASE)
        case UART_5:
            __UART5_CLK_ENABLE();
            obj->index = 4;
            break;
#endif
        case UART_6:
            __USART6_CLK_ENABLE();
            obj->index = 5;
            break;
#if defined(UART7_BASE)
        case UART_7:
            __UART7_CLK_ENABLE();
            obj->index = 6;
            break;
#endif
#if defined(UART8_BASE)
        case UART_8:
            __UART8_CLK_ENABLE();
            obj->index = 7;
            break;
#endif
    }

    // Configure the UART pins
    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);
    if (tx != NC) {
        pin_mode(tx, PullUp);
    }
    if (rx != NC) {
        pin_mode(rx, PullUp);
    }

    // Configure UART
    obj->baudrate = 9600;
    obj->databits = UART_WORDLENGTH_8B;
    obj->stopbits = UART_STOPBITS_1;
    obj->parity   = UART_PARITY_NONE;

    obj->pin_tx = tx;
    obj->pin_rx = rx;

    init_uart(obj);

    // For stdio management
    if (obj->uart == STDIO_UART) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
Пример #2
0
// assumes Init parameters have been set up correctly
STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) {
    USART_TypeDef *UARTx;
    IRQn_Type irqn;
    int uart_unit;

    const pin_obj_t *pins[4] = {0};

    switch (uart_obj->uart_id) {
        #if defined(MICROPY_HW_UART1_TX) && defined(MICROPY_HW_UART1_RX)
        case PYB_UART_1:
            uart_unit = 1;
            UARTx = USART1;
            irqn = USART1_IRQn;
            pins[0] = &MICROPY_HW_UART1_TX;
            pins[1] = &MICROPY_HW_UART1_RX;
            __USART1_CLK_ENABLE();
            break;
        #endif

        #if defined(MICROPY_HW_UART2_TX) && defined(MICROPY_HW_UART2_RX)
        case PYB_UART_2:
            uart_unit = 2;
            UARTx = USART2;
            irqn = USART2_IRQn;
            pins[0] = &MICROPY_HW_UART2_TX;
            pins[1] = &MICROPY_HW_UART2_RX;
            #if defined(MICROPY_HW_UART2_RTS)
            if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) {
                pins[2] = &MICROPY_HW_UART2_RTS;
            }
            #endif
            #if defined(MICROPY_HW_UART2_CTS)
            if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) {
                pins[3] = &MICROPY_HW_UART2_CTS;
            }
            #endif
            __USART2_CLK_ENABLE();
            break;
        #endif

        #if defined(MICROPY_HW_UART3_TX) && defined(MICROPY_HW_UART3_RX)
        case PYB_UART_3:
            uart_unit = 3;
            UARTx = USART3;
            irqn = USART3_IRQn;
            pins[0] = &MICROPY_HW_UART3_TX;
            pins[1] = &MICROPY_HW_UART3_RX;
            #if defined(MICROPY_HW_UART3_RTS)
            if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) {
                pins[2] = &MICROPY_HW_UART3_RTS;
            }
            #endif
            #if defined(MICROPY_HW_UART3_CTS)
            if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) {
                pins[3] = &MICROPY_HW_UART3_CTS;
            }
            #endif
            __USART3_CLK_ENABLE();
            break;
        #endif

        #if defined(MICROPY_HW_UART4_TX) && defined(MICROPY_HW_UART4_RX)
        case PYB_UART_4:
            uart_unit = 4;
            UARTx = UART4;
            irqn = UART4_IRQn;
            pins[0] = &MICROPY_HW_UART4_TX;
            pins[1] = &MICROPY_HW_UART4_RX;
            __UART4_CLK_ENABLE();
            break;
        #endif

        #if defined(MICROPY_HW_UART5_TX) && defined(MICROPY_HW_UART5_RX)
        case PYB_UART_5:
            uart_unit = 5;
            UARTx = UART5;
            irqn = UART5_IRQn;
            pins[0] = &MICROPY_HW_UART5_TX;
            pins[1] = &MICROPY_HW_UART5_RX;
            __UART5_CLK_ENABLE();
            break;
        #endif

        #if defined(MICROPY_HW_UART6_TX) && defined(MICROPY_HW_UART6_RX)
        case PYB_UART_6:
            uart_unit = 6;
            UARTx = USART6;
            irqn = USART6_IRQn;
            pins[0] = &MICROPY_HW_UART6_TX;
            pins[1] = &MICROPY_HW_UART6_RX;
            #if defined(MICROPY_HW_UART6_RTS)
            if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_RTS) {
                pins[2] = &MICROPY_HW_UART6_RTS;
            }
            #endif
            #if defined(MICROPY_HW_UART6_CTS)
            if (uart_obj->uart.Init.HwFlowCtl & UART_HWCONTROL_CTS) {
                pins[3] = &MICROPY_HW_UART6_CTS;
            }
            #endif
            __USART6_CLK_ENABLE();
            break;
        #endif

        #if defined(MICROPY_HW_UART7_TX) && defined(MICROPY_HW_UART7_RX)
        case PYB_UART_7:
            uart_unit = 7;
            UARTx = UART7;
            irqn = UART7_IRQn;
            pins[0] = &MICROPY_HW_UART7_TX;
            pins[1] = &MICROPY_HW_UART7_RX;
            __UART7_CLK_ENABLE();
            break;
        #endif

        #if defined(MICROPY_HW_UART8_TX) && defined(MICROPY_HW_UART8_RX)
        case PYB_UART_8:
            uart_unit = 8;
            UARTx = UART8;
            irqn = UART8_IRQn;
            pins[0] = &MICROPY_HW_UART8_TX;
            pins[1] = &MICROPY_HW_UART8_RX;
            __UART8_CLK_ENABLE();
            break;
        #endif

        default:
            // UART does not exist or is not configured for this board
            return false;
    }

    uint32_t mode = MP_HAL_PIN_MODE_ALT;
    uint32_t pull = MP_HAL_PIN_PULL_UP;

    for (uint i = 0; i < 4; i++) {
        if (pins[i] != NULL) {
            bool ret = mp_hal_pin_config_alt(pins[i], mode, pull, AF_FN_UART, uart_unit);
            if (!ret) {
                return false;
            }
        }
    }

    uart_obj->irqn = irqn;
    uart_obj->uart.Instance = UARTx;

    // init UARTx
    HAL_UART_Init(&uart_obj->uart);

    uart_obj->is_enabled = true;

    return true;
}
Пример #3
0
void serial_init(serial_t *obj, PinName tx, PinName rx)
{
    // Determine the UART to use (UART_1, UART_2, ...)
    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);

    // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object
    UARTName instance = (UARTName)pinmap_merge(uart_tx, uart_rx);
    MBED_ASSERT(instance != (UARTName)NC);

    // Enable USART clock
    switch (instance) {
        case UART_1:
            __USART1_CLK_ENABLE();
            obj->serial.module = 0;
            break;
        case UART_2:
            __USART2_CLK_ENABLE();
            obj->serial.module = 1;
            break;
#if defined(USART3_BASE)
        case UART_3:
            __USART3_CLK_ENABLE();
            obj->serial.module = 2;
            break;
#endif
#if defined(UART4_BASE)
        case UART_4:
            __UART4_CLK_ENABLE();
            obj->serial.module = 3;
            break;
#endif
#if defined(UART5_BASE)
        case UART_5:
            __UART5_CLK_ENABLE();
            obj->serial.module = 4;
            break;
#endif
        case UART_6:
            __USART6_CLK_ENABLE();
            obj->serial.module = 5;
            break;
#if defined(UART7_BASE)
        case UART_7:
            __UART7_CLK_ENABLE();
            obj->serial.module = 6;
            break;
#endif
#if defined(UART8_BASE)
        case UART_8:
            __UART8_CLK_ENABLE();
            obj->serial.module = 7;
            break;
#endif
    }

    // Configure the UART pins
    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);
    if (tx != NC) pin_mode(tx, PullUp);
    if (rx != NC) pin_mode(rx, PullUp);
    obj->serial.pin_tx = tx;
    obj->serial.pin_rx = rx;

    // initialize the handle for this master!
    UART_HandleTypeDef *handle = &UartHandle[obj->serial.module];

    handle->Instance            = (USART_TypeDef *)instance;
    handle->Init.BaudRate       = 9600;
    handle->Init.WordLength     = UART_WORDLENGTH_8B;
    handle->Init.StopBits       = UART_STOPBITS_1;
    handle->Init.Parity         = UART_PARITY_NONE;
    if (rx == NC) {
        handle->Init.Mode = UART_MODE_TX;
    } else if (tx == NC) {
        handle->Init.Mode = UART_MODE_RX;
    } else {
        handle->Init.Mode = UART_MODE_TX_RX;
    }
    handle->Init.HwFlowCtl      = UART_HWCONTROL_NONE;
    handle->Init.OverSampling   = UART_OVERSAMPLING_16;
    handle->TxXferCount         = 0;
    handle->RxXferCount         = 0;

    if (tx == STDIO_UART_TX && rx == STDIO_UART_RX) {
        handle->Init.BaudRate   = YOTTA_CFG_MBED_OS_STDIO_DEFAULT_BAUD;
    }

    HAL_UART_Init(handle);

    // DEBUG_PRINTF("UART%u: Init\n", obj->serial.module+1);
}