-
Notifications
You must be signed in to change notification settings - Fork 1
/
mb_bsp.c
369 lines (328 loc) · 11 KB
/
mb_bsp.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-11-21 Meco Man the first verion
*/
/*
*********************************************************************************************************
* uC/Modbus
* The Embedded Modbus Stack
*
* Copyright 2003-2020 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* uC/Modbus
*
* MODBUS BOARD SUPPORT PACKAGE
* RT-Thread UART Device
*
*
* Filename : mb_bsp.c
* Version : V2.14.00
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#include "mb.h"
#include <os.h>
#include <rtdevice.h>
#include <stdlib.h>
/*
*********************************************************************************************************
* GLOBALS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* MB_CommExit()
*
* Description : This function is called to terminate Modbus communications. All Modbus channels are close.
*
* Argument(s) : none
*
* Return(s) : none.
*
* Caller(s) : MB_Exit()
*
* Note(s) : none.
*********************************************************************************************************
*/
CPU_VOID MB_CommExit (CPU_VOID)
{
CPU_INT08U ch;
MODBUS_CH *pch;
char uart_dev_name[RT_NAME_MAX]={0};
rt_device_t uart_dev;
pch = &MB_ChTbl[0];
for (ch = 0; ch < MODBUS_CFG_MAX_CH; ch++) {
if(pch == RT_NULL){
pch++;
continue;
}
rt_snprintf(uart_dev_name, RT_NAME_MAX, "uart%d", pch->PortNbr);
uart_dev = rt_device_find(uart_dev_name);
if(uart_dev == (rt_device_t)0){
pch++;
continue;
}
rt_device_set_rx_indicate(uart_dev, RT_NULL);
rt_device_close(uart_dev);
pch++;
}
}
static rt_err_t mb_rx_handler(rt_device_t dev, rt_size_t size)
{
CPU_INT08U byte;
CPU_INT08U ch;
MODBUS_CH *pch;
char dev_id_name[RT_NAME_MAX] = {0};
CPU_INT08U id;
rt_strncpy(dev_id_name, &dev->parent.name[4], 2); /* "uart1" -> "1"*/
id = atoi(dev_id_name); /* "1" -> 1 */
pch = &MB_ChTbl[0];
for (ch = 0; ch < MODBUS_CFG_MAX_CH; ch++) {
if(pch->PortNbr == id){
break;
}
pch++;
}
for(ch=size; ch>0; ch--)
{
if(rt_device_read(dev, -1, &byte, 1) == 1) /* read one byte from uart */
{
MB_RxByte(pch, byte); /* invoke MB_RxByte() */
}
}
return RT_EOK;
}
/*
*********************************************************************************************************
* MB_CommPortCfg()
*
* Description : This function initializes the serial port to the desired baud rate and the UART will be
* configured for N, 8, 1 (No parity, 8 bits, 1 stop).
*
* Argument(s) : pch is a pointer to the Modbus channel
* port_nbr is the desired serial port number. This argument allows you to assign a
* specific serial port to a sepcific Modbus channel.
* baud is the desired baud rate for the serial port.
* parity is the desired parity and can be either:
*
* MODBUS_PARITY_NONE
* MODBUS_PARITY_ODD
* MODBUS_PARITY_EVEN
*
* bits specifies the number of bit and can be either 7 or 8.
* stops specifies the number of stop bits and can either be 1 or 2
*
* Return(s) : none.
*
* Caller(s) : MB_CfgCh()
*
* Note(s) : none.
*********************************************************************************************************
*/
CPU_VOID MB_CommPortCfg (MODBUS_CH *pch,
CPU_INT08U port_nbr,
CPU_INT32U baud,
CPU_INT08U bits,
CPU_INT08U parity,
CPU_INT08U stops)
{
char uart_dev_name[RT_NAME_MAX]={0};
rt_device_t uart_dev; /* uart device handler */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
if (pch == (MODBUS_CH *)0){
return;
}
pch->PortNbr = port_nbr; /* Store configuration in channel */
pch->BaudRate = baud;
pch->Parity = parity;
pch->Bits = bits;
pch->Stops = stops;
rt_snprintf(uart_dev_name, RT_NAME_MAX, "uart%d", port_nbr);
uart_dev = rt_device_find(uart_dev_name);
if(uart_dev == (rt_device_t)0){
return;
}
switch(baud)
{
case 2400:
config.baud_rate = BAUD_RATE_2400; break;
case 4800:
config.baud_rate = BAUD_RATE_4800; break;
case 9600:
config.baud_rate = BAUD_RATE_9600; break;
case 19200:
config.baud_rate = BAUD_RATE_19200; break;
case 38400:
config.baud_rate = BAUD_RATE_38400; break;
case 57600:
config.baud_rate = BAUD_RATE_57600; break;
case 115200:
config.baud_rate = BAUD_RATE_115200; break;
case 230400:
config.baud_rate = BAUD_RATE_230400; break;
case 460800:
config.baud_rate = BAUD_RATE_460800; break;
case 921600:
config.baud_rate = BAUD_RATE_921600; break;
case 2000000:
config.baud_rate = BAUD_RATE_2000000; break;
case 3000000:
config.baud_rate = BAUD_RATE_3000000; break;
default:
config.baud_rate = BAUD_RATE_115200; break;
}
switch(bits)
{
case 7:
config.data_bits = DATA_BITS_7; break;
case 8:
default:
config.data_bits = DATA_BITS_8; break;
}
switch(stops)
{
case 2:
config.stop_bits = STOP_BITS_2; break;
case 1:
default:
config.stop_bits = STOP_BITS_1; break;
}
switch(parity)
{
case MODBUS_PARITY_ODD:
config.parity = PARITY_ODD; break;
case MODBUS_PARITY_EVEN:
config.parity = PARITY_EVEN; break;
case MODBUS_PARITY_NONE:
default:
config.parity = PARITY_NONE; break;
}
config.bufsz = 128;
rt_device_control(uart_dev, RT_DEVICE_CTRL_CONFIG, &config);
if(rt_device_open(uart_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_DMA_RX) == RT_EOK)
{
rt_device_set_rx_indicate(uart_dev, mb_rx_handler);
}
}
/*
*********************************************************************************************************
* MB_Tx()
*
* Description : This function is called to start transmitting a packet to a modbus channel.
*
* Argument(s) : pch Is a pointer to the Modbus channel's data structure.
*
* Return(s) : none.
*
* Caller(s) : MB_ASCII_Tx(),
* MB_RTU_Tx().
*
* Note(s) : none.
*********************************************************************************************************
*/
/* combine MB_Tx() and MB_TxByte() */
void MB_Tx (MODBUS_CH *pch)
{
char uart_dev_name[RT_NAME_MAX]={0};
rt_device_t uart_dev;
pch->TxBufPtr = &pch->TxBuf[0];
if (pch->TxBufByteCtr > 0) {
#if (MODBUS_CFG_MASTER_EN == DEF_ENABLED)
if (pch->MasterSlave == MODBUS_MASTER) {
#if (MODBUS_CFG_RTU_EN == DEF_ENABLED)
pch->RTU_TimeoutEn = MODBUS_FALSE; /* Disable RTU timeout timer until we start receiving */
#endif
pch->RxBufByteCtr = 0; /* Flush Rx buffer */
}
#endif
rt_snprintf(uart_dev_name, RT_NAME_MAX, "uart%d", pch->PortNbr);
uart_dev = rt_device_find(uart_dev_name);
if(uart_dev == (rt_device_t)0){
return;
}
rt_device_write(uart_dev,0,pch->TxBufPtr,pch->TxBufByteCtr); /* send a message */
/* end of transmission */
pch->TxCtr = pch->TxBufByteCtr;
pch->TxBufByteCtr = 0;
}
}
/*
*********************************************************************************************************
* MB_RTU_TmrInit()
*
* Description : This function is called to initialize the RTU timeout timer.
*
* Argument(s) : freq Is the frequency of the modbus RTU timer interrupt.
*
* Return(s) : none.
*
* Caller(s) : MB_Init().
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_RTU_EN == DEF_ENABLED)
CPU_VOID MB_RTU_TmrInit (void)
{
}
#endif
/*
*********************************************************************************************************
* MB_RTU_TmrExit()
*
* Description : This function is called to disable the RTU timeout timer.
*
* Argument(s) : none.
*
* Return(s) : none.
*
* Caller(s) : MB_Exit()
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_RTU_EN == DEF_ENABLED)
CPU_VOID MB_RTU_TmrExit (CPU_VOID)
{
}
#endif
/*
*********************************************************************************************************
* MB_RTU_TmrISR_Handler()
*
* Description : This function handles the case when the RTU timeout timer expires.
*
* Arguments : none.
*
* Returns : none.
*
* Caller(s) : This is a ISR.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (MODBUS_CFG_RTU_EN == DEF_ENABLED)
CPU_VOID MB_RTU_Tmr_Timeout (CPU_VOID)
{
}
#endif