⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hw_config.c

📁 STM32F103C8-PKT评估板例程
💻 C
📖 第 1 页 / 共 2 页
字号:
      USART_InitStructure.USART_Parity = USART_Parity_No;
      break;
    case 1:
      USART_InitStructure.USART_Parity = USART_Parity_Even;
      break;
    case 2:
      USART_InitStructure.USART_Parity = USART_Parity_Odd;
      break;
    default :
    {
      USART_Config_Default();
      return (FALSE);
    }
  }

  /*set the data type : only 8bits and 9bits is supported */
  switch (linecoding.datatype)
  {
    case 0x07:
      /* With this configuration a parity (Even or Odd) should be set */
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      break;
    case 0x08:
      if (USART_InitStructure.USART_Parity == USART_Parity_No)
      {
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      }
      else 
      {
        USART_InitStructure.USART_WordLength = USART_WordLength_9b;
      }
      
      break;
    default :
    {
      USART_Config_Default();
      return (FALSE);
    }
  }

  if (linecoding.bitrate < 115200) 
    TimeBase = TIMER_ARR_10MS;
  else if (linecoding.bitrate >= 256000)
    TimeBase = TIMER_ARR_1MS;
  else 
    TimeBase = TIMER_ARR_5MS;
  Timer_Init(TimeBase);
  
  USART_InitStructure.USART_BaudRate = linecoding.bitrate;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART1, &USART_InitStructure);
  USART_Cmd(USART1, ENABLE);
  return (TRUE);
}

/*******************************************************************************
* 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(USART1, *(data_buffer + i));
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); 
  }
}

/*******************************************************************************
* Function Name  : USART_2_USB_Send_Data.
* Description    : send the received data from UART 0 to USB.
* Input          : None.
* Return         : none.
*******************************************************************************/
void USART_2_USB_Send_Data(void)
{
  if (linecoding.datatype == 7)
  {
    *pbuffer_in_usart++ = USART_ReceiveData(USART1) & 0x7F;
  }
  else if (linecoding.datatype == 8)
  {
    *pbuffer_in_usart++ = USART_ReceiveData(USART1);
  }
  
  if (pbuffer_in_usart == &buffer_in[BUFFER_SIZE])
    pbuffer_in_usart = buffer_in;
}

/*******************************************************************************
* Function Name  : Get_SerialNum.
* Description    : Create the serial number string descriptor.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void Get_SerialNum(void)
{
  uint32_t Device_Serial0, Device_Serial1, Device_Serial2;

  Device_Serial0 = *(__IO uint32_t*)(0x1FFFF7E8);
  Device_Serial1 = *(__IO uint32_t*)(0x1FFFF7EC);
  Device_Serial2 = *(__IO uint32_t*)(0x1FFFF7F0);

  if (Device_Serial0 != 0)
  {
    Virtual_Com_Port_StringSerial[2] = (uint8_t)(Device_Serial0 & 0x000000FF);
    Virtual_Com_Port_StringSerial[4] = (uint8_t)((Device_Serial0 & 0x0000FF00) >> 8);
    Virtual_Com_Port_StringSerial[6] = (uint8_t)((Device_Serial0 & 0x00FF0000) >> 16);
    Virtual_Com_Port_StringSerial[8] = (uint8_t)((Device_Serial0 & 0xFF000000) >> 24);

    Virtual_Com_Port_StringSerial[10] = (uint8_t)(Device_Serial1 & 0x000000FF);
    Virtual_Com_Port_StringSerial[12] = (uint8_t)((Device_Serial1 & 0x0000FF00) >> 8);
    Virtual_Com_Port_StringSerial[14] = (uint8_t)((Device_Serial1 & 0x00FF0000) >> 16);
    Virtual_Com_Port_StringSerial[16] = (uint8_t)((Device_Serial1 & 0xFF000000) >> 24);

    Virtual_Com_Port_StringSerial[18] = (uint8_t)(Device_Serial2 & 0x000000FF);
    Virtual_Com_Port_StringSerial[20] = (uint8_t)((Device_Serial2 & 0x0000FF00) >> 8);
    Virtual_Com_Port_StringSerial[22] = (uint8_t)((Device_Serial2 & 0x00FF0000) >> 16);
    Virtual_Com_Port_StringSerial[24] = (uint8_t)((Device_Serial2 & 0xFF000000) >> 24);
  }
}


/*******************************************************************************
* Function Name  : Timer_Init
* Description    : Initiate the TIM2 to generate the xms timebase
* Input          : - ARR()
* Output         : None
* Return         : None
*******************************************************************************/
void Timer_Init(uint16_t Time_ARR)
{
  /* ---------------------------------------------------------------
    TIM2 Configuration: Timing Mode:
    TIM2CLK = 72 MHz, Prescaler = 23, TIM2 counter clock = 3MHz
    TIM2 ARR Register = 14999 => TIM2 Frequency = TIM2 counter clock/(ARR + 1)
    TIM2 Frequency = 200Hz.(5ms)
    TIM2 ARR Register = 29999 => TIM2 Frequency = TIM2 counter clock/(ARR + 1)
    TIM2 Frequency = 100Hz.(10ms)
    TIM2 ARR Register = 2999 => TIM2 Frequency = TIM2 counter clock/(ARR + 1)
    TIM2 Frequency = 1000Hz.(1ms)
  --------------------------------------------------------------- */
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;  
  
  TIM_DeInit(TIM2);         /* deinitiate */
  
  TIM_TimeBaseStructure.TIM_Period = Time_ARR;   /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 23;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;   /* Timing Mode :Channel1 */
  TIM_OC1Init(TIM2, &TIM_OCInitStructure);
  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM2, ENABLE);  

  TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);   /* TIM IT enable */

  TIM_Cmd(TIM2, ENABLE);  /* TIM3 enable counter */
}

/*******************************************************************************
* Function Name  : USART_2_USB_Send.
* Description    : send the received data from UART 0 to USB.
* Input          : None.
* Return         : none.
*******************************************************************************/
volatile USB_USART_STATE usart_2_usb_process;
void usart_2_usb_send(void)
{
  unsigned char *ptemp = NULL;
  
  if (pbuffer_in_usb == &buffer_in[BUFFER_SIZE])
    pbuffer_in_usb = buffer_in;
  
  if(pbuffer_in_usb == pbuffer_in_usart) {
    usart_2_usb_process = WAIT_USART2USB_END;        // no data needed to send from USB
    usart_2_usb_complete = TRUE;
    return;
  }
  else if(pbuffer_in_usb > pbuffer_in_usart) {  // if the pbuffer_in_usart is rollback
    usart_2_usb_count = &buffer_in[BUFFER_SIZE] - pbuffer_in_usb;
  }
  else {
    usart_2_usb_count = pbuffer_in_usart - pbuffer_in_usb;
  }
  
  if (usart_2_usb_count > VIRTUAL_COM_PORT_DATA_SIZE){
    ptemp = pbuffer_in_usb;
    UserToPMABufferCopy(ptemp, ENDP1_TXADDR, VIRTUAL_COM_PORT_DATA_SIZE);
    SetEPTxCount(ENDP1, VIRTUAL_COM_PORT_DATA_SIZE);
    usart_2_usb_count -= VIRTUAL_COM_PORT_DATA_SIZE;
    pbuffer_in_usb += VIRTUAL_COM_PORT_DATA_SIZE;
  }
  else {
    ptemp = pbuffer_in_usb;
    UserToPMABufferCopy(ptemp, ENDP1_TXADDR, usart_2_usb_count);
    SetEPTxCount(ENDP1, usart_2_usb_count);
    pbuffer_in_usb += usart_2_usb_count;
    usart_2_usb_count = 0;    
  }
  
  usart_2_usb_process = WAIT_USART2USB_END; 
  SetEPTxValid(ENDP1); 
}

/*******************************************************************************
* Function Name  : usart_2_usb_waitend.
* Description    : waitting the flag set by the usb interrupt to indicate the end
                   of the usb transmission.
* Input          : None.
* Return         : none.
*******************************************************************************/
#define CR1_CEN_Set                 ((uint16_t)0x0001)
void usart_2_usb_waitend(void)
{
  if (usart_2_usb_complete == TRUE){
    usart_2_usb_process = USART_RECEIVE;     
    TIM2->CR1 |= CR1_CEN_Set;       // enable timer
    usart_2_usb_complete = FALSE;
  }
  else {
  }
}


/*******************************************************************************
* Function Name  : usb_2_usart_send_data.
* Description    : start the usart sending operation which is called by the usart interrupt
* Input          : None.
* Return         : none.
*******************************************************************************/
extern __IO uint32_t count_out;
void usb_2_usart_send_data(void)
{
  if (count_out == 0) {
    USART_ITConfig(USART1, USART_IT_TXE, DISABLE); 
    SetEPRxValid(ENDP3);
  }
  else {
    USART1->DR = *pbuffer_out_usart & (uint16_t)0x01FF;
    pbuffer_out_usart++;
    count_out--;
  }
}
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -