| | |
| | | #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]; //æ°æ®åé帧éå |
| | |
| | | volatile uint8_t m_bEUARTTxEn = 0; //使è½åé |
| | | |
| | | void (*Usart1ParseDataCallback)(uint8_t); |
| | | |
| | | void Usart1InitVariables(void) |
| | | { |
| | | m_EUART_TxFrm_FreeFrmLen = EUART_TX_FRM_SIZE-1; |
| | |
| | | |
| | | //åå§å管è |
| | | 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 |
| | |
| | | |
| | | 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åå§åä¹åï¼ |
| | |
| | | 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 ) |
| | | { |
| | |
| | | { |
| | | if(m_bEUARTTxEn) |
| | | { |
| | | OUT485_DISABLE; |
| | | m_bEUARTTxEn = 0; |
| | | // temp32 = GPIOC->CRH; //C10æ¬ç©ºè¾å
¥ |
| | | // temp32 &= ~(0x00000000F<<8); |
| | |
| | | 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++; |
| | |
| | | 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); |
| | | } |
| | | |