2 * Copyright (C) 2007 Anton Blad
3 * Copyright (C) 2007 Fredrik Kuivinen
4 * Copyright (C) 2007 Jakob Rosén
6 * This file is licensed under GPL v2.
13 #include <avr/pgmspace.h>
14 #include "trutproto.h"
15 #include "msgbuffer.h"
17 // Flags for communication
18 // Sending messages is allowed
19 #define TRUTCOMM_MSGENABLED 0x01
20 // Set if the functions should block on the _previous_ message sent.
21 // If not set, and the previous message has not been sent, the message
22 // is simply discarded.
23 #define TRUTCOMM_MSGBLOCKS 0x02
25 extern uint8_t trut_flags
;
27 static inline void trut_setmsgenabled(uint8_t e
)
30 trut_flags
|= TRUTCOMM_MSGENABLED
;
32 trut_flags
&= (uint8_t) ~TRUTCOMM_MSGENABLED
;
35 static inline uint8_t trut_msgenabled() { return trut_flags
& TRUTCOMM_MSGENABLED
; }
37 static inline void trut_setmsgblocks(uint8_t b
)
40 trut_flags
|= TRUTCOMM_MSGBLOCKS
;
42 trut_flags
&= (uint8_t) ~TRUTCOMM_MSGBLOCKS
;
45 static inline uint8_t trut_msgblocks() { return trut_flags
& TRUTCOMM_MSGBLOCKS
; }
47 // Send a string message to host. This function blocks.
49 // data: block to send
50 // len: length of data
51 void trut_msg_str(const char* str
);
53 // Send a HEX message to host. This function blocks.
55 // data: block to send
56 // len: length of data
57 void trut_msg_hex(const uint8_t* data
, uint8_t len
);
59 // Used for simple (one byte) status messages.
60 static inline void trut_msg_simple(uint8_t msg
)
62 if(msgbuffer_numfree() >= 2)
65 msgbuffer_addbyte(msg
);
69 // Used for messages with one-byte arguments.
70 static inline void trut_msg_arg(uint8_t msg
, uint8_t arg
)
72 if(msgbuffer_numfree() >= 3)
75 msgbuffer_addbyte(msg
);
76 msgbuffer_addbyte(arg
);
80 // Used for messages with word arguments.
81 static inline void trut_msg_argw(uint8_t msg
, uint16_t arg
)
83 if(msgbuffer_numfree() >= 4)
86 msgbuffer_addbyte(msg
);
87 msgbuffer_addbyte(arg
>> 8);
88 msgbuffer_addbyte(arg
& 0xff);
92 static inline void trut_msg_pong()
94 trut_msg_simple(TRUT_MSG_PONG
);
97 static inline void trut_msg_invalidcmd()
99 trut_msg_simple(TRUT_MSG_INVALIDCMD
);
102 static inline void trut_msg_protoerror()
104 trut_msg_simple(TRUT_MSG_PROTOERROR
);
107 static inline void trut_msg_loadstarted()
109 trut_msg_simple(TRUT_MSG_LOADSTARTED
);
112 static inline void trut_msg_loadfinished()
114 trut_msg_simple(TRUT_MSG_LOADFINISHED
);
117 static inline void trut_msg_dumpstarted()
119 trut_msg_simple(TRUT_MSG_DUMPSTARTED
);
122 static inline void trut_msg_dumpfinished()
124 trut_msg_simple(TRUT_MSG_DUMPFINISHED
);
127 static inline void trut_msg_profile_wc(uint8_t idx
, uint16_t m
)
129 if(msgbuffer_numfree() >= 5)
131 msgbuffer_addbyte(4);
132 msgbuffer_addbyte(TRUT_MSG_PROFILE_WC
);
133 msgbuffer_addbyte(idx
);
134 msgbuffer_addbyte(m
>> 8);
135 msgbuffer_addbyte(m
& 0xff);
139 static inline void trut_msg_noprofile()
141 trut_msg_simple(TRUT_MSG_NOPROFILE
);
144 static inline void trut_msg_motor(uint8_t s
)
146 trut_msg_arg(TRUT_MSG_MOTOR
, s
);
149 static inline void trut_msg_commerror(uint8_t s
)
151 trut_msg_arg(TRUT_MSG_COMMERROR
, s
);
154 static inline void trut_msg_acceptingdata()
156 trut_msg_simple(TRUT_MSG_ACCEPTINGDATA
);
159 static inline void trut_msg_bufferusage(uint8_t s
)
161 trut_msg_arg(TRUT_MSG_BUFFERUSAGE
, s
);
164 static inline void trut_msg_stackusage(uint8_t s
)
166 trut_msg_arg(TRUT_MSG_STACKUSAGE
, s
);
169 static inline void trut_msg_crc(uint32_t len
, uint16_t crc
)
171 if(msgbuffer_numfree() >= 8)
173 msgbuffer_addbyte(7);
174 msgbuffer_addbyte(TRUT_MSG_CRC
);
175 msgbuffer_addbyte((len
& 0xff000000) >> 24);
176 msgbuffer_addbyte((len
& 0xff0000) >> 16);
177 msgbuffer_addbyte((len
& 0xff00) >> 8);
178 msgbuffer_addbyte(len
& 0xff);
179 msgbuffer_addbyte((crc
& 0xff00) >> 8);
180 msgbuffer_addbyte(crc
& 0xff);
184 static inline void trut_msg_timing_error()
186 if(msgbuffer_numfree() >= 2)
188 msgbuffer_addbyte(1);
189 msgbuffer_addbyte(TRUT_MSG_TIMERERROR
);
193 // Should be called periodically. The function will send the status messages
194 // stored in the msgbuffer.
195 void trutcomm_task();
197 // Returns 1 if there is a command received, 0 otherwise.
198 uint8_t trut_has_cmd();
200 // Receive a command from the host. The function does not block.
202 // cmd: will be filled with the command type
203 // data: buffer that will be filled with the command arguments.
204 // At most TRUT_EP_CMD_SIZE-2 bytes will be written.
205 // len: will be filled with the number of argument bytes written.
207 // 0 on success, 1 on error.
208 uint8_t trut_cmd_read(uint8_t* cmd
, uint8_t* data
, uint8_t* len
);
210 // Returns 1 if there is data in the usb receive buffer.
211 uint8_t trut_has_data();
213 // Writes a packet of data to the circular buffer. The packet is supposed
214 // to contain an even number of bytes. If not, 1 is returned, although
215 // this will not happen if the whole data block is sent as a transaction.
217 // 0 on success, 1 on error
218 uint8_t trut_read_data_to_buffer();
220 // Returns 1 if a data packet can be sent.
221 uint8_t trut_can_send_data();
223 // Sends a packet of data to the host from the circular buffer.
225 // 0 on success, 1 on error
226 uint8_t trut_send_buffer_data();