2011年10月12日 星期三

將printf&scanf導向VirtualCOMPort

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

在C Code加入程式碼,將printf、scanf底層函式fputc、fgetc導向USB

加入所需變數

/* Private variables ---------------------------------------------------------*/
uint16_t CHAR_Rx_Cnt = 0;

/* Extern variables ----------------------------------------------------------*/
extern LINE_CODING linecoding;
extern uint8_t  USART_Rx_Buffer [USART_RX_DATA_SIZE]; 
extern uint32_t USART_Rx_ptr_in;
extern uint16_t USB_Rx_Cnt;
extern uint8_t USB_Rx_Buffer[VIRTUAL_COM_PORT_DATA_SIZE];

將資料放入USART_Rx_Buffer中,並將USART_Rx_ptr_in +1計數收到資料個數,USB中斷發生時會將資料傳回

/*******************************************************************************
* Function Name  : PUTCHAR_PROTOTYPE
* Description    : Retargets the C library printf function to the USART.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
PUTCHAR_PROTOTYPE
{
  if (linecoding.datatype == 7)
  {
    USART_Rx_Buffer[USART_Rx_ptr_in] = ch & 0x7F;
  }
  else if (linecoding.datatype == 8)
  {
    USART_Rx_Buffer[USART_Rx_ptr_in] = ch;
  }
  
  USART_Rx_ptr_in++;
  
  /* To avoid buffer overflow */
  if(USART_Rx_ptr_in == USART_RX_DATA_SIZE)
  {
    USART_Rx_ptr_in = 0;
  }

  return ch;
}

當USB收到資料將發生中斷,資料放入USB_Rx_Buffer並得到資料個數USB_Rx_Cnt,等待資料個數不為0後將依序送出,輸出一個BYTE將個數-1直到個數為0,取出USB_Rx_Buffer位置USB_Rx_Cnt將+1

/*******************************************************************************
* Function Name  : GETCHAR_PROTOTYPE
* Description    : Retargets the C library scanf function from the USART.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
GETCHAR_PROTOTYPE
{
while(USB_Rx_Cnt==0){}
USB_Rx_Cnt--;
return USB_Rx_Buffer[CHAR_Rx_Cnt++];
}

修改USB_Rx_Cnt為全域變數使GETCHAR_PROTOTYPE能夠看見,每收到一筆新資料將CHAR_Rx_Cnt歸0

檔案"usb_endp.c"

/* Private variables ---------------------------------------------------------*/
uint16_t USB_Rx_Cnt = 0;
extern uint16_t CHAR_Rx_Cnt;

/*******************************************************************************
* 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 */
}

更改Optimization使用Level 0,使用其他最佳化Level程式編譯會有問題


沒有留言:

張貼留言