2013-08-03 11:39:51 +08:00
/*
2013-08-13 16:45:39 +08:00
* FreeModbus Libary : STM32 Port
* Copyright ( C ) 2013 Armink < armink . ztl @ gmail . com >
2013-08-03 11:39:51 +08:00
*
* 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
*
2013-08-17 17:13:52 +08:00
* File : $ Id : portserial_m . c , v 1.60 2013 / 08 / 13 15 : 07 : 05 Armink add Master Functions $
2013-08-03 11:39:51 +08:00
*/
# include "port.h"
/* ----------------------- Modbus includes ----------------------------------*/
# include "mb.h"
# include "mbport.h"
2013-08-19 16:51:08 +08:00
2013-08-20 13:07:51 +08:00
# if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED
2013-08-03 11:39:51 +08:00
/* ----------------------- static functions ---------------------------------*/
static void prvvUARTTxReadyISR ( void ) ;
static void prvvUARTRxISR ( void ) ;
/* ----------------------- Start implementation -----------------------------*/
2013-08-13 21:54:54 +08:00
void vMBMasterPortSerialEnable ( BOOL xRxEnable , BOOL xTxEnable )
2013-08-03 11:39:51 +08:00
{
if ( xRxEnable )
{
2013-08-13 16:45:39 +08:00
MASTER_RS485_RECEIVE_MODE ;
USART_ITConfig ( USART2 , USART_IT_RXNE , ENABLE ) ;
2013-08-03 11:39:51 +08:00
}
else
{
2013-08-13 16:45:39 +08:00
MASTER_RS485_SEND_MODE ;
USART_ITConfig ( USART2 , USART_IT_RXNE , DISABLE ) ;
2013-08-03 11:39:51 +08:00
}
if ( xTxEnable )
{
2013-08-13 16:45:39 +08:00
USART_ITConfig ( USART2 , USART_IT_TXE , ENABLE ) ;
2013-08-03 11:39:51 +08:00
}
else
{
2013-08-13 16:45:39 +08:00
USART_ITConfig ( USART2 , USART_IT_TXE , DISABLE ) ;
2013-08-03 11:39:51 +08:00
}
}
2013-08-13 21:54:54 +08:00
void vMBMasterPortClose ( void )
2013-08-03 11:39:51 +08:00
{
2013-08-13 16:45:39 +08:00
USART_ITConfig ( USART2 , USART_IT_TXE | USART_IT_RXNE , DISABLE ) ;
USART_Cmd ( USART2 , DISABLE ) ;
2013-08-03 11:39:51 +08:00
}
2013-08-29 16:45:56 +08:00
//Ĭ<> <C4AC> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> 2 <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ʿ<EFBFBD> <CABF> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> ż<EFBFBD> <C5BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2013-08-13 21:54:54 +08:00
BOOL xMBMasterPortSerialInit ( UCHAR ucPORT , ULONG ulBaudRate , UCHAR ucDataBits ,
2013-08-03 11:39:51 +08:00
eMBParity eParity )
{
GPIO_InitTypeDef GPIO_InitStructure ;
USART_InitTypeDef USART_InitStructure ;
NVIC_InitTypeDef NVIC_InitStructure ;
//======================ʱ<> ӳ<EFBFBD> ʼ <EFBFBD> <CABC> =======================================
2013-08-29 16:45:56 +08:00
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB , ENABLE ) ;
RCC_APB1PeriphClockCmd ( RCC_APB1Periph_USART2 , ENABLE ) ;
2013-08-03 11:39:51 +08:00
//======================IO<49> <4F> ʼ <EFBFBD> <CABC> =======================================
2013-08-13 16:45:39 +08:00
//USART2_TX
2013-08-03 11:39:51 +08:00
GPIO_InitStructure . GPIO_Speed = GPIO_Speed_50MHz ;
GPIO_InitStructure . GPIO_Mode = GPIO_Mode_AF_PP ;
2013-08-13 16:45:39 +08:00
GPIO_InitStructure . GPIO_Pin = GPIO_Pin_2 ;
2013-08-03 11:39:51 +08:00
GPIO_Init ( GPIOA , & GPIO_InitStructure ) ;
2013-08-13 16:45:39 +08:00
//USART2_RX
2013-08-03 11:39:51 +08:00
GPIO_InitStructure . GPIO_Mode = GPIO_Mode_IN_FLOATING ;
2013-08-13 16:45:39 +08:00
GPIO_InitStructure . GPIO_Pin = GPIO_Pin_3 ;
2013-08-03 11:39:51 +08:00
GPIO_Init ( GPIOA , & GPIO_InitStructure ) ;
//<2F> <> <EFBFBD> <EFBFBD> 485<38> <35> <EFBFBD> ͺ ͽ<CDBA> <CDBD> <EFBFBD> ģʽ
// TODO <20> <> ʱ<EFBFBD> <CAB1> дB13 <20> <> ֮<EFBFBD> <D6AE> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD>
GPIO_InitStructure . GPIO_Mode = GPIO_Mode_Out_PP ;
GPIO_InitStructure . GPIO_Pin = GPIO_Pin_13 ;
GPIO_Init ( GPIOB , & GPIO_InitStructure ) ;
//======================<3D> <> <EFBFBD> ڳ<EFBFBD> ʼ <EFBFBD> <CABC> =======================================
USART_InitStructure . USART_BaudRate = ulBaudRate ;
//<2F> <> <EFBFBD> <EFBFBD> У <EFBFBD> <D0A3> ģʽ
switch ( eParity )
{
case MB_PAR_NONE : //<2F> <> У <EFBFBD> <D0A3>
USART_InitStructure . USART_Parity = USART_Parity_No ;
USART_InitStructure . USART_WordLength = USART_WordLength_8b ;
break ;
case MB_PAR_ODD : //<2F> <> У <EFBFBD> <D0A3>
USART_InitStructure . USART_Parity = USART_Parity_Odd ;
USART_InitStructure . USART_WordLength = USART_WordLength_9b ;
break ;
case MB_PAR_EVEN : //żУ <C5BC> <D0A3>
USART_InitStructure . USART_Parity = USART_Parity_Even ;
USART_InitStructure . USART_WordLength = USART_WordLength_9b ;
break ;
default :
return FALSE ;
}
USART_InitStructure . USART_StopBits = USART_StopBits_1 ;
USART_InitStructure . USART_HardwareFlowControl =
USART_HardwareFlowControl_None ;
USART_InitStructure . USART_Mode = USART_Mode_Rx | USART_Mode_Tx ;
2013-08-29 16:45:56 +08:00
if ( ucPORT ! = 2 )
2013-08-03 11:39:51 +08:00
return FALSE ;
ENTER_CRITICAL_SECTION ( ) ; //<2F> <> ȫ<EFBFBD> <C8AB> <EFBFBD> ж<EFBFBD>
2013-08-13 16:45:39 +08:00
USART_Init ( USART2 , & USART_InitStructure ) ;
USART_ITConfig ( USART2 , USART_IT_RXNE , ENABLE ) ;
USART_Cmd ( USART2 , ENABLE ) ;
2013-08-03 11:39:51 +08:00
//=====================<3D> жϳ <D0B6> ʼ <EFBFBD> <CABC> ======================================
//<2F> <> <EFBFBD> <EFBFBD> NVIC<49> <43> <EFBFBD> ȼ<EFBFBD> <C8BC> <EFBFBD> <EFBFBD> <EFBFBD> ΪGroup2<70> <32> 0-3<> <33> ռ ʽ <D5BC> <CABD> <EFBFBD> ȼ<EFBFBD> <C8BC> <EFBFBD> 0-3<> <33> <EFBFBD> <EFBFBD> Ӧʽ <D3A6> <CABD> <EFBFBD> ȼ<EFBFBD>
NVIC_PriorityGroupConfig ( NVIC_PriorityGroup_2 ) ;
2013-08-13 16:45:39 +08:00
NVIC_InitStructure . NVIC_IRQChannel = USART2_IRQn ;
2013-08-03 11:39:51 +08:00
NVIC_InitStructure . NVIC_IRQChannelPreemptionPriority = 0 ;
2013-08-13 16:45:39 +08:00
NVIC_InitStructure . NVIC_IRQChannelSubPriority = 0 ;
2013-08-03 11:39:51 +08:00
NVIC_InitStructure . NVIC_IRQChannelCmd = ENABLE ;
NVIC_Init ( & NVIC_InitStructure ) ;
EXIT_CRITICAL_SECTION ( ) ; //<2F> <> ȫ<EFBFBD> <C8AB> <EFBFBD> ж<EFBFBD>
return TRUE ;
}
2013-08-13 21:54:54 +08:00
BOOL xMBMasterPortSerialPutByte ( CHAR ucByte )
2013-08-03 11:39:51 +08:00
{
2013-08-13 16:45:39 +08:00
USART_SendData ( USART2 , ucByte ) ;
2013-08-03 11:39:51 +08:00
return TRUE ;
}
2013-08-13 21:54:54 +08:00
BOOL xMBMasterPortSerialGetByte ( CHAR * pucByte )
2013-08-03 11:39:51 +08:00
{
2013-08-13 16:45:39 +08:00
* pucByte = USART_ReceiveData ( USART2 ) ;
2013-08-03 11:39:51 +08:00
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 )
{
2013-08-13 21:54:54 +08:00
pxMBMasterFrameCBTransmitterEmpty ( ) ;
2013-08-03 11:39:51 +08:00
}
/*
* 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 )
{
2013-08-13 21:54:54 +08:00
pxMBMasterFrameCBByteReceived ( ) ;
2013-08-03 11:39:51 +08:00
}
/*******************************************************************************
2013-08-13 16:45:39 +08:00
* Function Name : USART2_IRQHandler
* Description : This function handles USART2 global interrupt request .
2013-08-03 11:39:51 +08:00
* Input : None
* Output : None
* Return : None
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2013-08-13 16:45:39 +08:00
void USART2_IRQHandler ( void )
2013-08-03 11:39:51 +08:00
{
rt_interrupt_enter ( ) ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> ж<EFBFBD>
2013-08-13 16:45:39 +08:00
if ( USART_GetITStatus ( USART2 , USART_IT_RXNE ) = = SET )
2013-08-03 11:39:51 +08:00
{
2013-08-13 16:45:39 +08:00
USART_ClearITPendingBit ( USART2 , USART_IT_RXNE ) ;
2013-08-03 11:39:51 +08:00
prvvUARTRxISR ( ) ;
}
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> ж<EFBFBD>
2013-08-13 16:45:39 +08:00
if ( USART_GetITStatus ( USART2 , USART_IT_TXE ) = = SET )
2013-08-03 11:39:51 +08:00
{
prvvUARTTxReadyISR ( ) ;
}
rt_interrupt_leave ( ) ;
}
2013-08-19 16:51:08 +08:00
# endif