Update TODO list
[trut64.git] / avr / trutcomm.c
blob3acf8e3bd52756552b3751f806b9dc1fe4f8d930
1 /*
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.
7 */
9 #include <string.h>
11 #include "trutcomm.h"
13 #include "avrusb.h"
14 #include "debugleds.h"
15 #include "usbdescriptors.h"
16 #include "dbuf.h"
17 #include "buffer.h"
19 uint8_t trut_flags = 0;
21 // Common logic for messages. Checks if message transmission is enabled,
22 // and if the previous message has been sent in case blocking is disabled.
23 static inline uint8_t trut_msg_start()
25 // Select the endpoint.
26 usb_select_ep(TRUT_EP_MSG);
28 // If not blocking and the previous message has not been sent, do not
29 // send this message.
30 if(!usb_transin_canstart())
31 return 1;
33 // Signal the start of a transaction.
34 usb_transin_start();
36 return 0;
39 static inline void trut_msg_end()
41 // Signal the end of a transaction.
42 usb_transin_end();
45 void trut_msg_str(const char* str)
47 uint8_t written;
48 uint8_t len = strlen(str);
50 if(!trut_msgenabled())
51 return;
53 while(trut_msg_start());
55 // Write the message type: STR message
56 usb_writebyte(TRUT_MSG_STR);
58 // Write the length of the message.
59 usb_writebyte(len);
61 while(len)
63 written = usb_transin_block(str, len, 0);
64 str += written;
65 len -= written;
68 trut_msg_end();
71 void trut_msg_hex(const uint8_t* data, uint8_t len)
73 uint8_t written;
75 if(!trut_msgenabled())
76 return;
78 while(trut_msg_start());
80 // Write the message type: HEX message
81 usb_writebyte(TRUT_MSG_HEX);
83 // Write the length of the message.
84 usb_writebyte(len);
86 while(len)
88 written = usb_transin_block(data, len, 0);
89 data += written;
90 len -= written;
93 trut_msg_end();
96 void trutcomm_task()
98 uint8_t len;
100 // Return if messages are not enabled.
101 if(!trut_msgenabled())
102 return;
104 // Return if there are no messages in the buffer.
105 if(msgbuffer_isempty())
106 return;
108 // Return if the previous message has not been sent.
109 if(trut_msg_start())
110 return;
112 // Get the message length.
113 len = msgbuffer_getbyte();
115 // Write the message.
116 while(len--)
117 usb_writebyte(msgbuffer_getbyte());
119 // Send the packet.
120 trut_msg_end();
123 uint8_t trut_has_cmd()
125 usb_select_ep(TRUT_EP_CMD);
127 return usb_pktout_received();
130 uint8_t trut_cmd_read(uint8_t* cmd, uint8_t* data, uint8_t* len)
132 uint8_t l = 0;
134 // Select the endpoint.
135 usb_select_ep(TRUT_EP_CMD);
137 // Return if there is no packet received.
138 if(!usb_pktout_received())
139 return 1;
141 // Acknowledge packet reception.
142 usb_pktout_start();
144 // Read the type of the received command.
145 if(!usb_pktout_byte(cmd))
147 usb_pktout_end();
148 return 1;
151 // Read the length of the received command data.
152 if(!usb_pktout_byte(&l))
154 usb_pktout_end();
155 return 1;
158 *len = l;
160 // Read the command arguments. Return if the length does not match the
161 // specified.
162 if(!(usb_pktout_block(data, l) == l))
164 usb_pktout_end();
165 return 1;
168 // Clear the receive buffer.
169 usb_pktout_end();
171 return 0;
174 uint8_t trut_has_data()
176 usb_select_ep(TRUT_EP_DATA);
178 return usb_pktout_received();
181 uint8_t trut_read_data_to_buffer()
183 uint16_t w;
185 // Select the endpoint.
186 usb_select_ep(TRUT_EP_DATA);
188 // Return if there is no packet received.
189 if(!usb_pktout_received())
190 return 1;
192 // Acknowledge packet reception.
193 usb_pktout_start();
195 // While there are words left in the usb fifo, write them to the buffer.
196 while(usb_pktout_byte(((uint8_t*) &w)+1))
198 if(!usb_pktout_byte(((uint8_t*) &w)))
200 usb_pktout_end();
201 return 1;
204 buffer_addword(w);
207 // Clear the receive buffer.
208 usb_pktout_end();
210 return 0;
213 uint8_t trut_can_send_data()
215 usb_select_ep(TRUT_EP_DATA);
217 return usb_fifofree();
220 uint8_t trut_send_buffer_data()
222 uint16_t w;
224 usb_select_ep(TRUT_EP_DATA);
226 if(!usb_fifofree())
227 return 1;
229 usb_cleartxini();
231 while(!buffer_isempty() && usb_rwal())
233 w = buffer_getword();
235 usb_writebyte(w >> 8);
236 usb_writebyte(w & 0x00ff);
239 usb_sendpacket();
241 return 0;