zhyinch
2020-09-12 7fb3818d7bdae672c6df22c94698ce9c7c629e56
Ô´Âë/ºËÐİå/Src/OnChipDevices/Usart.c
@@ -1,62 +1,6 @@
#include "Usart.h"
//void Usart_Init(void)
//{
//   USART_InitTypeDef USART_InitStructure;
//    GPIO_InitTypeDef GPIO_InitStructure;
//
//   /* Enable GPIO clock */
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
//   /* Enable USART clock */
//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//
//   /* Configure USART Tx as alternate function push-pull */
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//    GPIO_InitStructure.GPIO_Pin = USART_TX_pin;
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_Init(USART_GPIO, &GPIO_InitStructure);
//    /* Configure USART Rx as input floating */
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//    GPIO_InitStructure.GPIO_Pin = USART_RX_pin;
//    GPIO_Init(USART_GPIO, &GPIO_InitStructure);
//    /* USARTx configured as follow:
//         - BaudRate = 115200 baud
//         - Word Length = 8 Bits
//         - One Stop Bit
//         - No parity
//         - Hardware flow control disabled (RTS and CTS signals)
//         - Receive and transmit enabled
//    */
//    USART_InitStructure.USART_BaudRate = BAUD_RATE ;
//    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 configuration */
//    USART_Init(EXT_USART, &USART_InitStructure);
//    /* Enable USART */
//    USART_Cmd(EXT_USART, ENABLE);
//}
//uint16_t Checksum_u16(uint8_t *pdata, uint32_t len)
//{
//    uint16_t sum = 0;
//    uint32_t i;
//    for(i = 0; i < len; i++)
//        sum += pdata[i];
//    sum = ~sum;
//    return sum;
//}
///////////////////////////////////
#include <string.h>
#include <stdio.h>
//数据发送队列变量
EUART_Frame m_EUART_TxFrames[EUART_TX_FRM_SIZE];   //数据发送帧队列   
@@ -73,7 +17,6 @@
volatile uint8_t m_bEUARTTxEn = 0;                  //使能发送
void (*Usart1ParseDataCallback)(uint8_t);
void Usart1InitVariables(void)
{   
   m_EUART_TxFrm_FreeFrmLen = EUART_TX_FRM_SIZE-1;
@@ -139,14 +82,18 @@
   
   //初始化管脚
   GPIO_InitStructure.GPIO_Pin = EU_RX_PIN;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;                     //Rx上拉输入
   GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;                     //Rx上拉输入
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(EU_RX_GPIO, &GPIO_InitStructure);
   
   GPIO_InitStructure.GPIO_Pin = EU_TX_PIN;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                     //Tx推挽复用输出
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                     //Tx推挽复用输出 GPIO_Mode_AF_PP
   GPIO_Init(EU_TX_GPIO, &GPIO_InitStructure);
   
   GPIO_InitStructure.GPIO_Pin = EU_485_PIN;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                     //Tx推挽复用输出 GPIO_Mode_AF_PP
   GPIO_Init(EU_485_GPIO, &GPIO_InitStructure);
   OUT485_DISABLE;
#ifdef UART_GPIO_REMAP_ENABLE
   GPIO_PinRemapConfig(UART_GPIO_REMAP, ENABLE);
#endif
@@ -166,7 +113,7 @@
   
   USART_ClockInit(EXT_UART, &USART_ClockInitStructure);
   USART_Init(EXT_UART, &USART_InitStructure);
//   USART_ITConfig(EXT_UART, USART_IT_RXNE, ENABLE);//??????,????? ??????????
   // Enable the USARTx 
   USART_Cmd(EXT_UART, ENABLE); 
   //DMA配置(须放到UART初始化之后)
@@ -195,6 +142,7 @@
   while( m_EUART_DMA_RXPtr != DMACnt && MaxDataLen > 0)
   {
      Usart1ParseDataCallback(m_EUART_DMA_RXBuf[m_EUART_DMA_RXPtr]);
      delay_us(10);
      m_EUART_DMA_RXPtr++;
      if( m_EUART_DMA_RXPtr >= EUART_RX_BUF_SIZE )
      {
@@ -231,6 +179,7 @@
      {
         if(m_bEUARTTxEn)
         {
            OUT485_DISABLE;
            m_bEUARTTxEn = 0;
//            temp32 = GPIOC->CRH;         //C10悬空输入
//            temp32 &= ~(0x00000000F<<8);      
@@ -241,14 +190,18 @@
      m_bEUARTCheckingSend = 0;
      return;
   }
   if(!m_bEUARTTxEn)
   {
      m_bEUARTTxEn = 1;
      OUT485_ENABLE;
      delay_us(10);
//      temp32 = GPIOC->CRH;         //C10复用推挽输出
//      temp32 &= ~(0x00000000F<<8);
//      temp32 |= (0x000000009<<8);
//      GPIOC->CRH = temp32;// */
   }
   //发送一个字节数据
   EXT_UART->DR = m_EUART_TxFrames[m_EUART_TxFrm_Tail].buf[s_count];      
   s_count++;
@@ -344,4 +297,29 @@
      m_EUART_TxFrm_Head = EUART_TX_FRM_SIZE-1;
   m_EUART_TxFrm_FreeFrmLen++;
}
void USART_putc(char c)
{
   //while(!(USART2->SR & 0x00000040));
   //USART_SendData(USART2,c);
   /* e.g. write a character to the USART */
   USART_SendData(USART1, c);
   /* Loop until the end of transmission */
   while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)   ;
}
void USART_puts(uint8_t *s,uint8_t len)
{
   int i;
   for(i=0; i<len; i++)
   {
      USART_putc(s[i]);
   }
}
int fputc(int ch, FILE *f)
{
   USART_SendData(USART1, (unsigned char) ch);// USART1 ???? USART2 ?
   while (!(USART1->SR & USART_FLAG_TXE));
   return (ch);
}