My favorites
▼
|
Sign in
tinkercode
Miscellaneous Code Snippets for AVR programming
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
automation-framework
/
common
/
protocol.h
‹r104
r143
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
///////////////////////////////////////////////////////////////////////////////
// protocol.h - Home automation library
//
// Copyright (C) 2009 Jochen Toppe <jochen@jtoee.com>. All right reserved.
//
// 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
///////////////////////////////////////////////////////////////////////////////
#ifndef PROTOCOL_H
#define PROTOCOL_H
#include "common.h"
//
// Package data definitions.
//
//! initializes a packet
#define INIT_BYTE 0x00
//! starts a packet
#define START_BYTE 0xAA
//! max payload
#define MAX_PACKET_SIZE 0x4
//!
//! protocol peculiarities
//!
#define NUMBER_OF_INIT_BYTES 0x5
//!
//! receiver modes
//!
#define garbage_mode 0x1
#define pre_header 0x2
#define pkg_header 0x3
#define pkg_data 0x4
// Package structure
// +-------------------------+--------------------------------------------------+-------+
// | Pre Header | Header | Data |
// +------------+------------+--------+------------+---------------+------------+-------+
// | PHDR | SBT | ADR | CMD | INF | CHK | DT |
// +------------+------------+--------+------------+---------------+------------+-------+
// | pre-header | start byte | source | command & | DT Len, ACK, | checksum | dat a |
// | | | target | request id | checksum | | (0-4) |
// +------------+------------+--------+------------+---------------+------------+-------+
// bit structure of the ADR field
// +------------------+--------------+
// | [source device] | [target id ] |
// +------------------+--------------+
// | [7 6 5 4] | [3 2 1 0] |
// +------------------+--------------+
#define getADRSource(adr) (adr >> 4)
#define getADRTarget(adr) (adr & 0xF)
#define generateADR(src, tar) ((src << 4)|(tar & 0xF))
// bit structure of the INF field and convenience macros
// the reserved field is for future bi-directional acknowledgement
// of command receipt.
//
// The checksum are the upper 5 bits of the total checksum which is
// composed of the CHK field (lower 8 bits) and these 5 bits.
//
// The reserved bit is for the future auto-acknowledge feature which will let a
// remote device equipped with both transmitter and receiver acknowledge the
// receipt of a command
// +----------------+-------+---------------+
// | [data length ] | [res] | [checksum] |
// +----------------+-------+---------------+
// | [7 6] | [5] | [4 3 2 1 0] |
// +----------------+-------+---------------+
#define getINFDataLength(inf) (inf >> 6)
#define getINFReservedBit(inf) ((inf>> 5) & 0x1)
#define getINFChecksum(inf) (inf & 0x1F)
//#define generateINF(len,ack,chk) (len << 7)|((res&0x1)<<6)|(chk&0x1F00)
// bit structure of the CMD field and convenience macros
// +------------------+----------------+
// | [command] | [request id ] |
// +------------------+----------------+
// | [7 6 5 4] | [3 2 1 0] |
// +------------------+----------------+
#define getCMD(cmd) (cmd >> 4)
#define getRID(cmd) (cmd & 0xF)
#define generateCMD(cmd, rid) ((cmd << 4)|(rid & 0xF))
#define COMMAND_TYPE_REMOTE 'R'
#define COMMAND_TYPE_LOCAL 'L'
#define COMMAND_TYPE_NOOP 'N'
// command structure definition
struct Command {
// optional command type
uint8_t commandType;
// source device id (set to FF in case the parity could not be computed)
uint8_t sourceDevice;
// command
uint8_t command;
// request id
uint8_t requestId;
// target device (i.e. the receiving / executing device)
uint8_t targetDevice;
// checksum
uint16_t checksum;
// data size
uint8_t dataSize;
// data
uint8_t data[MAX_PACKET_SIZE];
// raw src field
uint8_t _adr;
// raw command and request id
uint8_t _cmd;
// taw inf - target address, data length, and ack required bit
uint8_t _inf;
// raw chksum
};
//
extern struct Command _incomingCommand;
extern struct Command _outgoingCommand;
//
// Process an incoming byte. Returns 0 if this yielded nothing, returns 1 if a ready
// and valid command object can be retrieved via the currentCommand variable.
//
uint8_t parse_incoming(uint8_t incoming);
//
// Assemble a command into the outgoingCommand structure. The current device id is also autmatically emdedded into the command object.
//
void assemble_command(uint8_t command, uint8_t requestId, uint8_t targetAddress, uint8_t dataSize, uint8_t data[]);
//
// send the outgoing command structure
//
void send_command(void);
//
// Returns one if the parity checks out ok.
//
uint8_t check_src_parity(struct Command *aCommand);
//
// function pointers to hook into functionality
//
// can be used to turn on/off the transmission circuits.
//
extern void (*_proto_pre_send_hook)(void);
extern void (*_proto_post_send_hook)(void);
// set the pre send hook
#define setPreSendHook(fx) _proto_pre_send_hook=fx
// set the post send hook
#define setPostSendHook(fx) _proto_post_send_hook=fx
// request id generator, a revolving unsigned int
extern uint8_t _request_id;
#define next_request_id() _request_id++
#endif
Show details
Hide details
Change log
r130
by jochen.toppe on Jan 8, 2010
Diff
[No log message]
Go to:
...tion-framework/common/protocol.c
...tion-framework/common/protocol.h
...ation-framework/eagle_schematics
...ematics/catrpillr_components.lbr
...e_schematics/master_version1.sch
Project members,
sign in
to write a code review
Older revisions
r104
by jochen.toppe on Jan 1, 2010
Diff
[No log message]
r103
by jochen.toppe on Dec 31, 2009
Diff
[No log message]
r101
by jochen.toppe on Dec 30, 2009
Diff
[No log message]
All revisions of this file
File info
Size: 5855 bytes, 173 lines
View raw file
Powered by
Google Project Hosting