portserial_m.c 6.06 KB
/*
 * FreeModbus Libary: RT-Thread Port
 * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
 *
 * 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_m.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions $
 */

#include "port.h"
#include "user.h"
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
//#include "rtdevice.h"
#include "bsp.h"
#include "mbconfig.h"
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
/* ----------------------- Static variables ---------------------------------*/
//ALIGN(RT_ALIGN_SIZE)
///* software simulation serial transmit IRQ handler thread stack */
//static rt_uint8_t serial_soft_trans_irq_stack[512];
///* software simulation serial transmit IRQ handler thread */
//static struct rt_thread thread_serial_soft_trans_irq;
///* serial event */
//static struct rt_event event_serial;
///* modbus master serial device */
//static rt_serial_t *serial;

///* ----------------------- Defines ------------------------------------------*/
///* serial transmit event */
//#define EVENT_SERIAL_TRANS_START    (1<<0)

///* ----------------------- static functions ---------------------------------*/
//static void prvvUARTTxReadyISR(void);
//static void prvvUARTRxISR(void);
//static rt_err_t serial_rx_ind(rt_device_t dev, rt_size_t size);
//static void serial_soft_trans_irq(void* parameter);
void prvvUARTRxISR_Master(void);
void prvvUARTTxReadyISR_Master(void);
/* ----------------------- Start implementation -----------------------------*/
BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
        eMBParity eParity)
{
    return TRUE;
}

void vMBMasterPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
{
     /* If xRXEnable enable serial receive interrupts. If xTxENable enable
     * transmitter empty interrupts.
     */
    if(TRUE==xRxEnable)
	{
//		if(pSetup[SETUP_WIRELESS_TYPE] != 4)
//		{
//			USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
//			GPIO_ResetBits(GPIOF,GPIO_Pin_3);	  //½ÓÊÕ
//		}
//		else
		{
			USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
			GPIO_ResetBits(GPIOF, GPIO_Pin_4);
		}
	}
	else
	{
//		if(pSetup[SETUP_WIRELESS_TYPE] != 4)
//		{
//			USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);	
//			GPIO_SetBits(GPIOF,GPIO_Pin_3);	  //·¢ËÍ
//		}
//		else
		{
			USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
			GPIO_SetBits(GPIOF, GPIO_Pin_4);
		}
		
	}

	if(TRUE==xTxEnable)
	{
//		if(pSetup[SETUP_WIRELESS_TYPE] != 4)
//		{
//			USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
//		}
//		else
		{
			USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
		}
	}
	else
	{
//		if(pSetup[SETUP_WIRELESS_TYPE] != 4)
//		{
//			USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
//		}
//		else
		{
			USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
		}
	}
}

void vMBMasterPortClose(void)
{
   // serial->parent.close(&(serial->parent));
}

BOOL xMBMasterPortSerialPutByte(CHAR ucByte)
{
   /* Put a byte in the UARTs transmit buffer. This function is called
     * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
     * called. */
//	if(pSetup[SETUP_WIRELESS_TYPE] != 4)
//	{
//		GPIO_SetBits(GPIOF, GPIO_Pin_3);   //·¢ËÍ

//		USART_SendData(USART3, ucByte);
//		while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
//		{
//		}  
//		GPIO_ResetBits(GPIOF, GPIO_Pin_3);  //½ÓÊÕ
//	}
//	else
	{
		GPIO_SetBits(GPIOF, GPIO_Pin_4);
		USART_SendData(USART2, ucByte);

		while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
		{
		}
		GPIO_ResetBits(GPIOF, GPIO_Pin_4);
	}
    return TRUE;
}

BOOL xMBMasterPortSerialGetByte(CHAR * pucByte)
{
/* Return the byte in the UARTs receive buffer. This function is called
     * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
     */
//	if(pSetup[SETUP_WIRELESS_TYPE] != 4)
//		*pucByte =(unsigned char) USART_ReceiveData(USART3);
//	else
		*pucByte =(unsigned char) USART_ReceiveData(USART2);
    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_Master(void)
{
    pxMBMasterFrameCBTransmitterEmpty();
}

/* 
 * 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_Master(void)
{		
    pxMBMasterFrameCBByteReceived();
}

///**
// * Software simulation serial transmit IRQ handler.
// *
// * @param parameter parameter
// */
//static void serial_soft_trans_irq(void* parameter) {
//    rt_uint32_t recved_event;
//    while (1)
//    {
//        /* waiting for serial transmit start */
//        rt_event_recv(&event_serial, EVENT_SERIAL_TRANS_START, RT_EVENT_FLAG_OR,
//                RT_WAITING_FOREVER, &recved_event);
//        /* execute modbus callback */
//        prvvUARTTxReadyISR();
//    }
//}

///**
// * This function is serial receive callback function
// *
// * @param dev the device of serial
// * @param size the data size that receive
// *
// * @return return RT_EOK
// */
//static rt_err_t serial_rx_ind(rt_device_t dev, rt_size_t size) {
//    prvvUARTRxISR();
//    return RT_EOK;
//}

#endif