示例#1
0
/**
 * @brief Initialize USART2
 */
void UART2_Init(void) {

  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;

  // Enable clocks
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

  // USART2 TX on PA2
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  // USART2 RX on PA3
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  // Connect USART2 TX pin to AF2
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);

  // USART initialization
  USART_InitStructure.USART_BaudRate = UART2_BAUDRATE;
  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(USART2, &USART_InitStructure);

  // Enable USART2
  USART_Cmd(USART2, ENABLE);

    // Enable RXNE interrupt
  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  // Disable TXE interrupt
  USART_ITConfig(USART2, USART_IT_TXE, DISABLE);

  // Enable USART2 global interrupt
  NVIC_EnableIRQ(USART2_IRQn);

  // Initialize RX FIFO
  rxFifo.buf = rxBuffer;
  rxFifo.len = UART2_BUF_LEN;
  FIFO_Add(&rxFifo);

  // Initialize TX FIFO
  txFifo.buf = txBuffer;
  txFifo.len = UART2_BUF_LEN;
  FIFO_Add(&txFifo);

}
示例#2
0
/**
 * @brief Initialize communication terminal interface.
 * @param baud Required baud rate
 */
void COMM_Init(int baud) {

  // pass baud rate
  // callback for received data and callback for transmitted data
  COMM_HAL_Init(baud, COMM_RxCallback, COMM_TxCallback);

  // Initialize RX FIFO for receiving data from PC
  rxFifo.buf = rxBuffer;
  rxFifo.len = COMM_BUF_LEN_RX;
  FIFO_Add(&rxFifo);

  // Initialize TX FIFO for transferring data to PC
  txFifo.buf = txBuffer;
  txFifo.len = COMM_BUF_LEN_TX;
  FIFO_Add(&txFifo);

}
示例#3
0
/**
 * @brief Initialize communication terminal interface.
 *
 * @param baud Required baud rate
 */
void COMM_Init(uint32_t baud) {

  // pass baud rate
  // callback for received data and callback for
  // transmitted data
  COMM_HAL_Init(baud, COMM_RxCallback, COMM_TxCallback);

  // Initialize RX FIFO
  rxFifo.buf = rxBuffer;
  rxFifo.len = COMM_BUF_LEN;
  FIFO_Add(&rxFifo);

  // Initialize TX FIFO
  txFifo.buf = txBuffer;
  txFifo.len = COMM_BUF_LEN;
  FIFO_Add(&txFifo);

}
示例#4
0
/****************************************************************************
* DESCRIPTION: Transmits a frame using the UART
* RETURN:      none
* ALGORITHM:   none
* NOTES:       none
*****************************************************************************/
void RS485_Send_Frame(
    volatile struct mstp_port_struct_t *mstp_port,      /* port specific data */
    uint8_t * buffer,   /* frame to send (up to 501 bytes of data) */
    uint16_t nbytes)
{       /* number of bytes of data (up to 501) */
    uint16_t i = 0;     /* loop counter */
    uint8_t turnaround_time;

    if (!buffer)
        return;

    while (!FIFO_Empty(&FIFO_Tx)) {
        /* buffer is not empty.  Wait for ISR to transmit. */
    };

    /* wait 40 bit times since reception */
    if (RS485_Baud_Rate == 9600)
        turnaround_time = 4;
    else if (RS485_Baud_Rate == 19200)
        turnaround_time = 2;
    else
        turnaround_time = 1;

    while (mstp_port->SilenceTimer < turnaround_time) {
        /* The line has not been silent long enough, so wait. */
    };

    if (FIFO_Add(&FIFO_Tx, buffer, nbytes)) {
        /* disable the receiver */
        PIE3bits.RC2IE = 0;
        RCSTA2bits.CREN = 0;
        /* enable the transceiver */
        RS485_TX_ENABLE = 1;
        RS485_RX_DISABLE = 1;
        /* enable the transmitter */
        TXSTA2bits.TXEN = 1;
        PIE3bits.TX2IE = 1;
        /* reset the silence timer per MSTP spec, sort of */
        mstp_port->SilenceTimer = 0;
    }

    return;
}