portserial_m.c
6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* 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