-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmodbus.c
259 lines (236 loc) · 6.36 KB
/
modbus.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
/**
Modbus slave implementation for STM32 HAL under FreeRTOS.
(c) 2017 Viacheslav Kaloshin, [email protected]
Licensed under LGPL.
**/
#include "FreeRTOS.h"
#include "cmsis_os.h"
#include "modbus.h"
// If you want directly send to usb-cdc
// #include "usbd_cdc_if.h"
osMessageQId ModBusInHandle;
osMessageQId ModBusOutHandle;
osThreadId ModBusTaskHandle;
uint16_t mb_reg[ModBusRegisters];
// Here is actual modbus data stores
uint8_t mb_buf_in[256];
uint8_t mb_buf_in_count;
uint8_t mb_addr;
uint8_t mb_buf_out[256];
uint8_t mb_buf_out_count;
void ModBusParse(void);
void ModBusTask(void const * argument)
{
for(;;)
{
osEvent evt = osMessageGet(ModBusInHandle,ModBus35);
// Frame end?
if (evt.status == osEventTimeout)
{
if(mb_buf_in_count > 0) // ok, something in buffer exist, lets parse it
{
ModBusParse();
}
mb_buf_in_count=0;
}
// Wow, something come!
if (evt.status == osEventMessage)
{
uint8_t byte = (uint8_t) evt.value.v;
// buffer has space for incoming?
if(mb_buf_in_count<254)
{
mb_buf_in[mb_buf_in_count]=byte;
mb_buf_in_count=mb_buf_in_count+1; // prevent opt/war on come compilers
}
else // oops, bad frame, by standard we should drop it and no answer
{
mb_buf_in_count=0;
}
}
}
}
void ModBus_Init(void)
{
osMessageQDef(ModBusIn, 256, uint8_t);
ModBusInHandle = osMessageCreate(osMessageQ(ModBusIn), NULL);
osMessageQDef(ModBusOut, 256, uint8_t);
ModBusOutHandle = osMessageCreate(osMessageQ(ModBusOut), NULL);
osThreadDef(ModBusTask, ModBusTask, osPriorityNormal, 0, 128);
ModBusTaskHandle = osThreadCreate(osThread(ModBusTask), NULL);
mb_buf_in_count=0;
mb_addr=247; // by default maximum possible adrress
mb_buf_out_count=0;
for(int i=0;i<ModBusRegisters;i++)
{
mb_reg[i]=0;
}
}
void ModBus_SetAddress(uint8_t addr)
{
mb_addr = addr;
}
void CRC16_OUT(void);
uint8_t CRC16_IN(void);
// parse something in incoming buffer
void ModBusParse(void)
{
if(mb_buf_in_count==0) // call as by mistake on empty buffer?
{
return;
}
if(mb_buf_in[0] != mb_addr) // its not our address!
{
return;
}
// check CRC
if(CRC16_IN()==0)
{
mb_buf_out_count = 0;
uint16_t st,nu;
uint8_t func = mb_buf_in[1];
uint8_t i;
switch(func)
{
case 3:
// read holding registers. by bytes addr func starth startl totalh totall
st=mb_buf_in[2]*256+mb_buf_in[3];
nu=mb_buf_in[4]*256+mb_buf_in[5];
if( (st+nu) > ModBusRegisters) // dont ask more, that we has!
{
mb_buf_out[mb_buf_out_count++]=mb_addr;
mb_buf_out[mb_buf_out_count++]=func+0x80;
mb_buf_out[mb_buf_out_count++]=2;
}
else
{
mb_buf_out[mb_buf_out_count++]=mb_addr;
mb_buf_out[mb_buf_out_count++]=func;
mb_buf_out[mb_buf_out_count++]=nu*2; // how many bytes we will send?
for(i=st;i<(st+nu);i++)
{
mb_buf_out[mb_buf_out_count++]=( mb_reg[i] >> 8 ) & 0xFF; // hi part
mb_buf_out[mb_buf_out_count++]=mb_reg[i] & 0xFF; // lo part
}
}
break;
case 16:
// write holding registers. by bytes addr func starth startl totalh totall num_bytes regh regl ...
st=mb_buf_in[2]*256+mb_buf_in[3];
nu=mb_buf_in[4]*256+mb_buf_in[5];
if( (st+nu) > ModBusRegisters) // dont ask more, that we has!
{
mb_buf_out[mb_buf_out_count++]=mb_addr;
mb_buf_out[mb_buf_out_count++]=func+0x80;
mb_buf_out[mb_buf_out_count++]=2;
}
else
{ // ATTN : skip num_bytes
for(i=0;i<nu;i++)
{
mb_reg[st+i]=mb_buf_in[7+i*2]*256+mb_buf_in[8+i*2];
}
mb_buf_out[mb_buf_out_count++]=mb_addr;
mb_buf_out[mb_buf_out_count++]=func;
mb_buf_out[mb_buf_out_count++]=mb_buf_in[2]; // how many registers ask, so many wrote
mb_buf_out[mb_buf_out_count++]=mb_buf_in[3];
mb_buf_out[mb_buf_out_count++]=mb_buf_in[4];
mb_buf_out[mb_buf_out_count++]=mb_buf_in[5];
}
break;
default:
// Exception as we does not provide this function
mb_buf_out[mb_buf_out_count++]=mb_addr;
mb_buf_out[mb_buf_out_count++]=func+0x80;
mb_buf_out[mb_buf_out_count++]=1;
break;
}
CRC16_OUT();
// If you want directly to USB-CDC
//CDC_Transmit_FS(&mb_buf_out[0], mb_buf_out_count);
for(int i=0;i<mb_buf_out_count;i++)
{
osMessagePut(ModBusOutHandle,mb_buf_out[i],0);
}
}
// Ok, we parsed buffer, clean up
mb_buf_in_count=0;
mb_buf_out_count=0;
}
// set value of register
void ModBus_SetRegister(uint8_t reg,uint16_t value)
{
if(reg<ModBusRegisters)
{
mb_reg[reg]=value;
}
}
// grab value of register
uint16_t ModBus_GetRegister(uint8_t reg)
{
if(reg<ModBusRegisters)
{
return mb_reg[reg];
}
return 0;
}
// Calculate CRC for outcoming buffer
// and place it to end.
void CRC16_OUT(void)
{
uint16_t crc = 0xFFFF;
uint16_t pos = 0;
uint8_t i =0;
uint8_t lo =0;
uint8_t hi =0;
for (pos = 0; pos < mb_buf_out_count; pos++)
{
crc ^= mb_buf_out[pos];
for (i = 8; i != 0; i--)
{
if ((crc & 0x0001) != 0)
{
crc >>= 1;
crc ^= 0xA001;
}
else
crc >>= 1;
}
}
lo = crc & 0xFF;
hi = ( crc >> 8 ) & 0xFF;
mb_buf_out[mb_buf_out_count++] = lo;
mb_buf_out[mb_buf_out_count++] = hi;
}
// Calculate CRC fro incoming buffer
// Return 0 - if CRC is correct, overwise return 0
uint8_t CRC16_IN(void)
{
uint16_t crc = 0xFFFF;
uint16_t pos = 0;
uint8_t i =0;
uint8_t lo =0;
uint8_t hi =0;
for (pos = 0; pos < mb_buf_in_count-2; pos++)
{
crc ^= mb_buf_in[pos];
for (i = 8; i != 0; i--)
{
if ((crc & 0x0001) != 0)
{
crc >>= 1;
crc ^= 0xA001;
}
else
crc >>= 1;
}
}
lo = crc & 0xFF;
hi = ( crc >> 8 ) & 0xFF;
if( (mb_buf_in[mb_buf_in_count-2] == lo) &&
(mb_buf_in[mb_buf_in_count-1] == hi) )
{
return 0;
}
return 1;
}