FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ChildbusBootloader/BaseProtocol.cpp at master · 3devo/ChildbusBootloader · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
3devo
/
ChildbusBootloader
Public
Notifications
You must be signed in to change notification settings
Fork
9
Star
12
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ChildbusBootloader
/
BaseProtocol.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
158 lines (137 loc) · 4.5 KB
Breadcrumbs
ChildbusBootloader
/
BaseProtocol.cpp
Copy path
File metadata and controls
158 lines (137 loc) · 4.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Copyright (C) 2017-2025 3devo (http://www.3devo.eu)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#
include
<
stdint.h
>
#
include
"
Bus.h
"
#
include
"
Crc.h
"
#
include
"
BaseProtocol.h
"
static
int
configuredAddress =
0
;
static
int
handleGeneralCall
(
uint8_t
*data,
uint8_t
len,
uint8_t
/*
maxLen
*/
) {
if
(len ==
1
&& data[
0
] == GeneralCallCommands::
RESET
) {
resetSystem
();
}
else
if
(len ==
1
&& data[
0
] == GeneralCallCommands::
RESET_ADDRESS
) {
BusResetDeviceAddress
();
configuredAddress =
0
;
}
return
0
;
}
cmd_result
handleCommand
(
uint8_t
cmd,
uint8_t
*datain,
uint8_t
len,
uint8_t
*dataout,
uint8_t
maxLen) {
if
(maxLen <
5
)
return
cmd_result
(Status::
NO_REPLY
);
switch
(cmd) {
case
ProtocolCommands::
GET_PROTOCOL_VERSION
:
dataout[
0
] =
PROTOCOL_VERSION
>>
8
;
dataout[
1
] =
PROTOCOL_VERSION
&
0xFF
;
return
cmd_ok
(
2
);
case
ProtocolCommands::
SET_ADDRESS
:
if
(len !=
2
)
return
cmd_result
(Status::
INVALID_ARGUMENTS
);
//
Only respond if the hw type in the request is
//
the wildcard or matches ours.
if
(datain[
1
] !=
0
&& datain[
1
] !=
INFO_HW_TYPE
)
return
cmd_result
(Status::
NO_REPLY
);
BusSetDeviceAddress
(datain[
0
]);
configuredAddress = datain[
0
];
return
cmd_ok
();
case
ProtocolCommands::
GET_MAX_PACKET_LENGTH
:
dataout[
0
] =
MAX_PACKET_LENGTH
>>
8
;
dataout[
1
] =
MAX_PACKET_LENGTH
&
0xFF
;
return
cmd_ok
(
2
);
default
:
return
processCommand
(cmd, datain, len, dataout, maxLen);
}
}
//
The bus implementation will already have checked whether the request
//
is addressed to us, this just checks whether child select maybe
//
prevents a response.
bool
shouldRespondToAddress
(
uint8_t
address) {
#
if
defined(USE_CHILD_SELECT)
//
Child select does not apply to general call or the configured
//
address, and the pin is active low.
return
address ==
0
|| address == configuredAddress || !
CHILD_SELECT_PIN
.
read
();
#
else
(
void
)address;
//
unused
return
true
;
#
endif
}
#
if
defined(USE_I2C)
int
BusCallback
(
uint8_t
address,
uint8_t
*data,
uint8_t
len,
uint8_t
maxLen) {
if
(!
shouldRespondToAddress
(address))
return
0
;
if
(address ==
0
)
return
handleGeneralCall
(data, len, maxLen);
//
Check that there is at least room for a status, length and a CRC
if
(maxLen <
3
)
return
0
;
cmd_result
res
(
0
);
//
Check we received at least command and crc
if
(len <
2
) {
res =
cmd_result
(Status::
INVALID_TRANSFER
);
}
else
{
uint8_t
crc =
Crc8Ccitt
().
update
(data, len).
get
();
if
(crc !=
0
) {
res =
cmd_result
(Status::
INVALID_CRC
);
}
else
{
//
CRC checks out, process a command
res =
handleCommand
(data[
0
], data +
1
, len -
2
, data +
2
, maxLen -
3
);
if
(res.
status
== Status::
NO_REPLY
)
return
0
;
}
}
data[
0
] = res.
status
;
data[
1
] = res.
len
;
len = res.
len
+
2
;
uint8_t
crc =
Crc8Ccitt
().
update
(data, len).
get
();
data[len++] = crc;
return
len;
}
#
elif
defined(USE_RS485)
int
BusCallback
(
uint8_t
address,
uint8_t
*data,
uint8_t
len,
uint8_t
maxLen) {
if
(!
shouldRespondToAddress
(address))
return
0
;
//
Check that there is at least room for an address, status, length and CRC
if
(maxLen <
5
)
return
0
;
cmd_result
res
(
0
);
//
Check we received at least command and crc
if
(len <
3
) {
res =
cmd_result
(Status::
INVALID_TRANSFER
);
}
else
{
uint16_t
crc =
Crc16Ibm
().
update
(address).
update
(data, len -
2
).
get
();
if
(crc != (data[len -
2
] | data[len -
1
] <<
8
)) {
//
Invalid CRC, so no reply (we cannot
//
be sure that the message was really
//
for us, some someone else might also
//
reply).
return
0
;
}
else
if
(address ==
0
) {
return
handleGeneralCall
(data, len -
2
, maxLen);
}
else
{
//
CRC checks out, process a command
res =
handleCommand
(data[
0
], data +
1
, len -
3
, data +
3
, maxLen -
5
);
if
(res.
status
== Status::
NO_REPLY
)
return
0
;
}
}
data[
0
] = address;
data[
1
] = res.
status
;
data[
2
] = res.
len
;
len = res.
len
+
3
;
uint16_t
crc =
Crc16Ibm
().
update
(data, len).
get
();
data[len++] = crc;
data[len++] = crc >>
8
;
return
len;
}
#
endif
Back
|
FazBrowse Home
|
New Git URL