Exemple #1
0
void uart_setCallbacks(uart_tx_cbt txCb, uart_rx_cbt rxCb) 
{
  uart_vars.txCb = txCb;
  uart_vars.rxCb = rxCb;
  
  //enable nvic uart.
  NVIC_uart();
}
Exemple #2
0
void uart_init() {
    
    GPIO_InitTypeDef  GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    
    // reset local variables
    memset(&uart_vars,0,sizeof(uart_vars_t));
    
    // enable GPIO and USART clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    
    // configure USART TX pin as alternate function push-pull
    GPIO_InitStructure.GPIO_Mode                      = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin                       = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed                     = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // configure USART RX as input floating
    GPIO_InitStructure.GPIO_Mode                      = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin                       = GPIO_Pin_10;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    USART_InitStructure.USART_BaudRate                = 115200;
    USART_InitStructure.USART_WordLength              = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits                = USART_StopBits_1;
    USART_InitStructure.USART_Parity                  = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl     = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode                    = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART1, &USART_InitStructure);
    
    // make sure no interrupts fire as we enable the UART
    uart_clearTxInterrupts();
    uart_clearRxInterrupts();
    
    // enable USART1
    USART_Cmd(USART1, ENABLE);
    
    // enable NVIC uart
    NVIC_uart();
}