From 4cf8adc0dd1286f8585d0be7a2bf367bb8119cdc Mon Sep 17 00:00:00 2001 From: zhyinch <zhyinch@gmail.com> Date: 星期二, 06 四月 2021 16:16:38 +0800 Subject: [PATCH] V2.16 测试完成WIFI串口功能。 --- 源码/核心板/Src/OnChipDevices/Usart.c | 153 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 147 insertions(+), 6 deletions(-) diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.c" index 41efcbd..6e9b0e5 100644 --- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.c" +++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.c" @@ -9,7 +9,7 @@ volatile int8_t m_EUART_TxFrm_FreeFrmLen = 0; //数据发送帧队列剩余帧数 //DMA数据接收缓存 uint8_t m_EUART_DMA_RXBuf[EUART_RX_BUF_SIZE]; //DMA数据接收缓存 -volatile int32_t m_EUART_DMA_RXPtr = 0; //当前数据地址 +volatile int32_t m_EUART_DMA_RXPtr = 0,m_EUART2_DMA_RXPtr = 0; //当前数据地址 //标志变量 volatile uint8_t m_bEUARTPushingFrms = 0; //正在往发送队列存数据 volatile uint8_t m_bEUARTCheckingSend = 0; //正在确认数据发送 @@ -17,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; @@ -83,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 @@ -139,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 ) { @@ -175,6 +179,7 @@ { if(m_bEUARTTxEn) { + OUT485_DISABLE; m_bEUARTTxEn = 0; // temp32 = GPIOC->CRH; //C10悬空输入 // temp32 &= ~(0x00000000F<<8); @@ -185,17 +190,21 @@ 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++; + EXT_UART->DR = m_EUART_TxFrames[m_EUART_TxFrm_Tail].buf[s_count]; + s_count++; if(s_count >= m_EUART_TxFrames[m_EUART_TxFrm_Tail].len) //一帧数据发送完毕 { s_count = 0; @@ -313,4 +322,136 @@ while (!(USART1->SR & USART_FLAG_TXE)); return (ch); } +//////////////////////////////////////////////////////////////USART2 + +void UART2_DMAConfiguration(void) +{ + DMA_InitTypeDef DMA_InitStructure; + + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); + + //UART的DMA数据接收初始化 + DMA_DeInit(UART2_RX_DMA_CH); + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)( &(EXT_UART2->DR)); //外设数据寄存器 + DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)m_EUART_DMA_RXBuf; //数据Buf + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //外设作源头 + DMA_InitStructure.DMA_BufferSize = EUART_RX_BUF_SIZE; //Buf大小 + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址不增加 + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址增加 + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //字节 + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //字节(注意与上一个变量名称不同!!) + DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循环模式 + DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //优先级 + DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //非内存到内存 + DMA_Init(UART2_RX_DMA_CH, &DMA_InitStructure); + USART_DMACmd(EXT_UART2, USART_DMAReq_Rx, ENABLE); + DMA_Cmd(UART2_RX_DMA_CH, ENABLE); + +#ifdef EXUART_USE_TXDMA + //UART的DMA数据发送初始化 + DMA_DeInit(EUART_TX_DMA_CH); + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(EXT_UART->DR)); //外设数据寄存器 + DMA_InitStructure.DMA_MemoryBaseAddr = 0; //数据Buf + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //外设作目标 + DMA_InitStructure.DMA_BufferSize = 0; //Buf大小 + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器不递增 + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址递增 + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设字节为单位 + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //内存字节为单位(注意与上一个变量名称不同!!) + DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //普通模式 + DMA_InitStructure.DMA_Priority = DMA_Priority_High; //优先级 + DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //非内存到内存 + DMA_Init(EUART_TX_DMA_CH, &DMA_InitStructure); + +// DMA_ITConfig(EUART_TX_DMA_CH, DMA_IT_TC, ENABLE); //DMA传输完成中断 + USART_DMACmd(EXT_UART,USART_DMAReq_Tx,ENABLE); //外设使能DMA // */ +#endif +} + +void Uart2_Init(void) +{ + GPIO_InitTypeDef GPIO_InitStructure; + USART_InitTypeDef USART_InitStructure; + USART_ClockInitTypeDef USART_ClockInitStructure; + + /* Enable GPIO clock */ + // RCC_APB2PeriphClockCmd(EU_RCC_GPIO | RCC_APB2Periph_AFIO, ENABLE); + /* Enable USART clock */ + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); + + Usart1InitVariables(); + + //初始化管脚 + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; + 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 = GPIO_Pin_2; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Tx推挽复用输出 GPIO_Mode_AF_PP + GPIO_Init(EU_TX_GPIO, &GPIO_InitStructure); + + +#ifdef UART_GPIO_REMAP_ENABLE + GPIO_PinRemapConfig(UART_GPIO_REMAP, ENABLE); +#endif + + USART_DeInit(EXT_UART2); + USART_InitStructure.USART_BaudRate = EXUART_BAUD_RADE; + 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_ClockInitStructure.USART_Clock = USART_Clock_Disable; + USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; + USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; + USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; + + USART_ClockInit(EXT_UART2, &USART_ClockInitStructure); + USART_Init(EXT_UART2, &USART_InitStructure); +// USART_ITConfig(EXT_UART, USART_IT_RXNE, ENABLE);//??????,????? ?????????? + // Enable the USARTx + USART_Cmd(EXT_UART2, ENABLE); + //DMA配置(须放到UART初始化之后) + UART2_DMAConfiguration(); + +} + +void UART2_CheckReceive(void) +{ + int32_t DMACnt = 0; + int32_t MaxDataLen = EUART_RX_BUF_SIZE; + + //如果正在往发送队列中添加数据,退出 + if(m_bEUARTPushingFrms) + return; + //判断是否正在Check + if(m_bEUARTCheckingRec) + return; + m_bEUARTCheckingRec = 1; + if(UART2_RX_DMA_CH->CNDTR == 0) + { + m_bEUARTCheckingRec = 0; + return; + } + DMACnt = EUART_RX_BUF_SIZE - (UART2_RX_DMA_CH->CNDTR); + while( m_EUART2_DMA_RXPtr != DMACnt && MaxDataLen > 0) + { + Usart1ParseDataCallback(m_EUART_DMA_RXBuf[m_EUART2_DMA_RXPtr]); + delay_us(10); + m_EUART2_DMA_RXPtr++; + if( m_EUART2_DMA_RXPtr >= EUART_RX_BUF_SIZE ) + { + m_EUART2_DMA_RXPtr = 0; + } + DMACnt = EUART_RX_BUF_SIZE - (UART2_RX_DMA_CH->CNDTR); + MaxDataLen--; + } + m_bEUARTCheckingRec = 0; +} + + + -- Gitblit v1.9.3