金牌会员
- 积分
- 1108
- 金钱
- 1108
- 注册时间
- 2018-11-6
- 在线时间
- 240 小时
|
发表于 2019-12-27 13:42:00
|
显示全部楼层
portserial.c
/*
* FreeModbus Libary: MSP430 Port
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* File: $Id: portserial.c,v 1.3 2006/11/19 03:57:49 wolti Exp $
*/
/* ----------------------- Platform includes --------------------------------*/
#include "port.h"
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
/* ----------------------- static functions ---------------------------------*/
static void prvvUARTTxReadyISR( void );
static void prvvUARTRxISR( void );
/* ----------------------- Static variables ---------------------------------*/
UCHAR ucGIEWasEnabled = FALSE;
UCHAR ucCriticalNesting = 0x00;
/* ----------------------- Start implementation -----------------------------*/
void
vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
{
ENTER_CRITICAL_SECTION( );
if( xRxEnable )
{
//MODBUS_RS485_RECIEVE();
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //禁止USART3接收不为空中断
}
else
{
//MODBUS_RS485_SEND();
USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
}
if( xTxEnable )
{
//数据空中断模式传输
//USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
//完成中断模式传输
//MODBUS_RS485_SEND();
USART_ITConfig(USART3, USART_IT_TC, ENABLE);
}
else
{
//数据空中断模式传输
//USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
//完成中断模式传输
//MODBUS_RS485_RECIEVE();
USART_ITConfig(USART3, USART_IT_TC, DISABLE);
}
EXIT_CRITICAL_SECTION( );
}
BOOL
xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
{
BOOL bInitialized = TRUE;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_DeInit(USART3);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 , ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = ulBaudRate;
switch ( eParity )
{
case MB_PAR_NONE:
USART_InitStructure.USART_Parity = USART_Parity_No;
break;
case MB_PAR_ODD:
USART_InitStructure.USART_Parity = USART_Parity_Odd;
break;
case MB_PAR_EVEN:
USART_InitStructure.USART_Parity = USART_Parity_Even;
break;
}
switch ( ucDataBits )
{
case 8:
if (eParity == MB_PAR_NONE)
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
else
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
break;
case 7:
break;
default:
bInitialized = FALSE;
}
if( bInitialized )
{
ENTER_CRITICAL_SECTION( );
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
EXIT_CRITICAL_SECTION( );
}
return bInitialized;
}
BOOL
xMBPortSerialPutByte( CHAR ucByte )
{
//数据空中断模式传输
//USART_SendData(USART1, ucByte);
//while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == 0) __NOP(); //等待发送完成
//完成中断模式传输
USART_SendData(USART3, ucByte);
return TRUE;
}
BOOL
xMBPortSerialGetByte( CHAR * pucByte )
{
//USART_ClearFlag(USART3, USART_IT_RXNE) ; //中断接收
//while(USART_GetFlagStatus(USART1, USART_IT_RXNE) == 0); //等待接收标志
*pucByte = (u8)USART_ReceiveData(USART3);
return TRUE;
}
/* Create an interrupt handler for the transmit buffer empty interrupt
* (or an equivalent) for your target processor. This function should then
* call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
* a new character can be sent. The protocol stack will then call
* xMBPortSerialPutByte( ) to send the character.
*/
static void prvvUARTTxReadyISR( void )
{
pxMBFrameCBTransmitterEmpty( );
}
/* Create an interrupt handler for the receive interrupt for your target
* processor. This function should then call pxMBFrameCBByteReceived( ). The
* protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
* character.
*/
static void prvvUARTRxISR( void )
{
pxMBFrameCBByteReceived( );
}
void USART3_IRQHandler(void)
{
if (USART_GetITStatus(USART3,USART_IT_TC))//完成中断模式传输
{
//pxMBFrameCBTransmitterEmpty( );
prvvUARTTxReadyISR();
USART_ClearITPendingBit(USART3, USART_IT_TC);
}
else if(USART_GetITStatus(USART3,USART_IT_RXNE))
{
//pxMBFrameCBByteReceived( );
prvvUARTRxISR();
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
}
}
|
|