portserial.c 4.59 KB
/*
  * FreeModbus Libary: LPC214X Port
  * Copyright (C) 2007 Tiago Prado Lone <tiago@maxwellbohr.com.br>
 *
 * 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.1 2007/04/24 23:15:18 wolti Exp $
 */

#include "port.h"

/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
/* ----------------------- Start implementation -----------------------------*/
void vMBPortSerialEnable( _BOOL xRxEnable, _BOOL xTxEnable )
{
	if(xRxEnable)
	{
		USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
	}
	else
	{
		USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);	
	}

	if(xTxEnable)
	{
		USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
	}
	else
	{
	   USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
	}
}

void
vMBPortClose( void )
{
	USART_ITConfig(USART3, USART_IT_TXE|USART_IT_RXNE, DISABLE);
	USART_Cmd(USART3, DISABLE);
}

_BOOL
xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
{	
	
	GPIO_InitTypeDef  GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef  NVIC_InitStructure;
	
	(void)ucPORT;  //不修改串口号
	(void)ucDataBits;  //不修改数据位长度
	(void)eParity;  //不修改检验格式
	
//	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD , ENABLE);
//	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3  , ENABLE);		//
//	
//	USART_DeInit(USART3);

//	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//	//USART3
//	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
//	GPIO_Init(GPIOB, &GPIO_InitStructure);
//	
//	GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
//	GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
//	//
//	//配置串口参数
//	//
//	USART_InitStructure.USART_BaudRate            = ulBaudRate;  //只修改波特率
//	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_Parity              = USART_Mode_Rx | USART_Mode_Tx;
//	USART_Init(USART3, &USART_InitStructure);
//	//
//	//使能串口
//	//
//	USART_Cmd(USART3, ENABLE);
	
	//
	//配置中断优先级
//	//
//	NVIC_InitStructure.NVIC_IRQChannel                   = USART3_IRQn;
//	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//	NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 2;
//	NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
//	NVIC_Init(&NVIC_InitStructure);
	
	return TRUE;
}



_BOOL
xMBPortSerialPutByte( CHAR ucByte )
{
	USART_SendData(USART3, ucByte);
    return TRUE;
}

_BOOL
xMBPortSerialGetByte( CHAR * pucByte )
{
	*pucByte = 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.
 */
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.
 */
void prvvUARTRxISR(void)
{
    pxMBFrameCBByteReceived();
}
//串口3
void USART3_IRQHandler(void)
{
	if (USART_GetITStatus(USART3, USART_IT_RXNE) == SET)
	{
		USART_ClearITPendingBit(USART3, USART_IT_RXNE);
		prvvUARTRxISR();	   //Modbus Uart1 ISR
	}
	if (USART_GetITStatus(USART3, USART_IT_TXE) == SET)
	{
		USART_ClearITPendingBit(USART3, USART_IT_TXE);
		prvvUARTTxReadyISR();  //Modbus Uart1 ISR
	}
}