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.
14 #include "debugleds.h"
15 #include "usbdescriptors.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
30 if(!usb_transin_canstart())
33 // Signal the start of a transaction.
39 static inline void trut_msg_end()
41 // Signal the end of a transaction.
45 void trut_msg_str(const char* str
)
48 uint8_t len
= strlen(str
);
50 if(!trut_msgenabled())
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.
63 written
= usb_transin_block(str
, len
, 0);
71 void trut_msg_hex(const uint8_t* data
, uint8_t len
)
75 if(!trut_msgenabled())
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.
88 written
= usb_transin_block(data
, len
, 0);
100 // Return if messages are not enabled.
101 if(!trut_msgenabled())
104 // Return if there are no messages in the buffer.
105 if(msgbuffer_isempty())
108 // Return if the previous message has not been sent.
112 // Get the message length.
113 len
= msgbuffer_getbyte();
115 // Write the message.
117 usb_writebyte(msgbuffer_getbyte());
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
)
134 // Select the endpoint.
135 usb_select_ep(TRUT_EP_CMD
);
137 // Return if there is no packet received.
138 if(!usb_pktout_received())
141 // Acknowledge packet reception.
144 // Read the type of the received command.
145 if(!usb_pktout_byte(cmd
))
151 // Read the length of the received command data.
152 if(!usb_pktout_byte(&l
))
160 // Read the command arguments. Return if the length does not match the
162 if(!(usb_pktout_block(data
, l
) == l
))
168 // Clear the receive buffer.
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()
185 // Select the endpoint.
186 usb_select_ep(TRUT_EP_DATA
);
188 // Return if there is no packet received.
189 if(!usb_pktout_received())
192 // Acknowledge packet reception.
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
)))
207 // Clear the receive buffer.
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()
224 usb_select_ep(TRUT_EP_DATA
);
231 while(!buffer_isempty() && usb_rwal())
233 w
= buffer_getword();
235 usb_writebyte(w
>> 8);
236 usb_writebyte(w
& 0x00ff);