/****************************************************************************** * Copyright (C) 2014-2015 HangZhou SiZhu Co.,LTD. * *----------------------------------------------------------------------------- * File: RS485.c * Description: RS485 gpio config ,receive and send data code * Author: Lishoujian (867693272@qq.com) * Date: Jan 8, 2015 *****************************************************************************/ /* ----------------------- Platform includes --------------------------------*/ #include "sys.h" #include "rs485.h" #include "delay.h" #include "stm32f10x_dma.h" RX_MODBUS_T rs485_receive_g ; u8 share_sendbuf[SEND_MAX_NUM]={0}; /****************************************** * func: RS485_ioconfig * desc: config gpio;initialize hardware interface of RS485 * input: USARTx - select which USART port to be initialized * output: none * return: none *****************************************/ void RS485_Usart_Init(uint32 system_BaudRate) { USART_InitTypeDef USART_InitStructure; DMA_InitTypeDef DMA_InitStructure; RCC_APB2PeriphClockCmd(APB_GPIO_GROUP, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB_Periph_UART , ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); USART_InitStructure.USART_BaudRate = system_BaudRate; USART_InitStructure.USART_WordLength = USART_WordLength_8b;//word length 8bit USART_InitStructure.USART_StopBits = USART_StopBits_1;//one stopbit 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_Init(UARTNUM, &USART_InitStructure); //initialize USART USART_Cmd(UARTNUM, ENABLE); RS485_EN=0; /////////////////////// /*DMA ½ÓÊÕ³õʼ»¯*/ /*¿ªÆôDMAʱÖÓ*/ DMA_DeInit(DMA_RS485_Channel); /*ÉèÖÃDMAÔ´£º´®¿ÚÊý¾Ý¼Ä´æÆ÷µØÖ·*/ DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)(&(UARTNUM->DR)) ; /*ÄÚ´æµØÖ·(Òª´«ÊäµÄ±äÁ¿µÄÖ¸Õë)*/ DMA_InitStructure.DMA_MemoryBaseAddr = (u32)(rs485_receive_g.RS485_BUF); /*·½Ïò£º´ÓË«Ïòµ½ÄÚ´æ*/ DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; /*´«Êä´óСDMA_BufferSize=SENDBUFF_SIZE*/ DMA_InitStructure.DMA_BufferSize = REC_LENGTH; /*ÍâÉèµØÖ·²»Ôö*/ DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; /*ÄÚ´æµØÖ·×ÔÔö*/ DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; /*ÍâÉèÊý¾Ýµ¥Î»*/ DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; /*ÄÚ´æÊý¾Ýµ¥Î» 8bit*/ DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; /*DMAģʽ£º²»¶ÏÑ­»·*/ //DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; /*ÓÅÏȼ¶£ºÖÐ*/ DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; /*½ûÖ¹ÄÚ´æµ½ÄÚ´æµÄ´«Êä */ DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA_RS485_Channel, &DMA_InitStructure); USART_DMACmd(UARTNUM, USART_DMAReq_Rx, ENABLE); DMA_Cmd(DMA_RS485_Channel, ENABLE); } void RS485_TXDMA_Init(u8 *data,u32 lenth) { DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; DMA_DeInit(DMA_RS485_TX_Channel); /*ÉèÖÃDMAÔ´£º´®¿ÚÊý¾Ý¼Ä´æÆ÷µØÖ·*/ DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)(&(UARTNUM->DR)) ; /*ÄÚ´æµØÖ·(Òª´«ÊäµÄ±äÁ¿µÄÖ¸Õë)*/ DMA_InitStructure.DMA_MemoryBaseAddr = (u32)(data); /*·½Ïò£º´ÓË«Ïòµ½ÄÚ´æ*/ DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /*´«Êä´óСDMA_BufferSize=SENDBUFF_SIZE*/ DMA_InitStructure.DMA_BufferSize = lenth; /*ÍâÉèµØÖ·²»Ôö*/ DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; /*ÄÚ´æµØÖ·×ÔÔö*/ DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; /*ÍâÉèÊý¾Ýµ¥Î»*/ DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; /*ÄÚ´æÊý¾Ýµ¥Î» 8bit*/ DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; /*DMAģʽ£º²»¶ÏÑ­»·*/ DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ; // DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; /*ÓÅÏȼ¶£ºÖÐ*/ DMA_InitStructure.DMA_Priority = DMA_Priority_High; /*½ûÖ¹ÄÚ´æµ½ÄÚ´æµÄ´«Êä */ DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA_RS485_TX_Channel, &DMA_InitStructure); USART_DMACmd(UARTNUM, USART_DMAReq_Tx, ENABLE); // DMA_ITConfig(DMA2_Channel5,DMA_IT_TC, ENABLE); // NVIC_InitStructure.NVIC_IRQChannel= DMA2_Channel4_5_IRQn; // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 2; // NVIC_InitStructure.NVIC_IRQChannelSubPriority= 1; // NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE; // NVIC_Init(&NVIC_InitStructure); } /****************************************** * func: RS485_Send_Data * desc: send data by RS485 * input: USARTx - select USART port , buf - send buffer , len - data length * output: none * return: none *****************************************/ void RS485_Send_Data(uint8 *buf,uint16 len) { uint16 t; RS485_EN = 1; //send mode for(t=0;t