2011年10月12日 星期三

VirtualCOMPort範例中的USB與USART


範例版本 
* Version            : V3.2.1
* Date               : 07/05/2010

USART傳回USB使用USART1的interrupt當USART1 RX中斷發生呼叫function
USART_To_USB_Send_Data 

檔案"stm32f10x_it.c"

#if defined (USE_STM3210B_EVAL) || defined (USE_STM3210E_EVAL)
/*******************************************************************************
* Function Name  : USART1_IRQHandler
* Description    : This function handles USART1 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
  if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
    /* Send the received data to the PC Host*/
    USART_To_USB_Send_Data();
  }

  /* If overrun condition occurs, clear the ORE flag and recover communication */
  if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
  {
    (void)USART_ReceiveData(USART1);
  }
}
#endif /* USE_STM3210B_EVAL or USE_STM3210E_EVAL */

USART_To_USB_Send_Data() function利用USART_ReceiveData收到USART資料放入USART_Rx_Buffer中,並將USART_Rx_ptr_in +1計數收到資料個數,USB中斷發生時會將資料傳回

檔案"hw_config.c"

/*******************************************************************************
* Function Name  : UART_To_USB_Send_Data.
* Description    : send the received data from UART 0 to USB.
* Input          : None.
* Return         : none.
*******************************************************************************/
void USART_To_USB_Send_Data(void)
{
  
  if (linecoding.datatype == 7)
  {
    USART_Rx_Buffer[USART_Rx_ptr_in] = USART_ReceiveData(EVAL_COM1) & 0x7F;
  }
  else if (linecoding.datatype == 8)
  {
    USART_Rx_Buffer[USART_Rx_ptr_in] = USART_ReceiveData(EVAL_COM1);
  }
  
  USART_Rx_ptr_in++;
  
  /* To avoid buffer overflow */
  if(USART_Rx_ptr_in == USART_RX_DATA_SIZE)
  {
    USART_Rx_ptr_in = 0;
  }
}

USB傳入USART使用USB的interrupt,當USB收到資料將發生中斷呼叫function EP3_OUT_Callback,使用USB_SIL_Read收到資料放入USB_Rx_Buffer並得到資料個數USB_Rx_Cnt,呼叫USB_To_USART_Send_Datar將資料由USART傳出

檔案"usb_endp.c"

/*******************************************************************************
* Function Name  : EP3_OUT_Callback
* Description    :
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void EP3_OUT_Callback(void)
{
  uint16_t USB_Rx_Cnt;
  
  /* Get the received data buffer and update the counter */
  USB_Rx_Cnt = USB_SIL_Read(EP3_OUT, USB_Rx_Buffer);

  CHAR_Rx_Cnt=0;
  
  /* USB data will be immediately processed, this allow next USB traffic beeing 
  NAKed till the end of the USART Xfet */
  
  USB_To_USART_Send_Data(USB_Rx_Buffer, USB_Rx_Cnt);
  
#ifndef STM32F10X_CL
  /* Enable the receive of data on EP3 */
  SetEPRxValid(ENDP3);
#endif /* STM32F10X_CL */
}

檔案"hw_config.c"

/*******************************************************************************
* Function Name  : USB_To_USART_Send_Data.
* Description    : send the received data from USB to the UART 0.
* Input          : data_buffer: data address.
                   Nb_bytes: number of bytes to send.
* Return         : none.
*******************************************************************************/
void USB_To_USART_Send_Data(uint8_t* data_buffer, uint8_t Nb_bytes)
{
  
  uint32_t i;
  
  for (i = 0; i < Nb_bytes; i++)
  {
    USART_SendData(EVAL_COM1, *(data_buffer + i));
    while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET); 
  }  
}

沒有留言:

張貼留言