freemyipod r896 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r895‎ | r896 | r897 >
Date:20:30, 5 August 2013
Author:theseven
Status:new
Tags:
Comment:
emCORE: Fix USB console and optimize its performance
Modified paths:
  • /emcore/trunk/tools/emcore.py (modified) (history)
  • /emcore/trunk/tools/libemcore.py (modified) (history)
  • /emcore/trunk/usb/usbdebug.c (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/emcore.py
@@ -407,10 +407,13 @@
408408 """
409409 Reads data from the USB console continuously
410410 """
 411+ size = 48
411412 while True:
412 - resp = self.emcore.usbcread()
 413+ resp = self.emcore.usbcread(size)
413414 self.logger.write(resp.data, target = "stdout")
414 -# time.sleep(0.1 / resp.maxsize * (resp.maxsize - len(resp.data)))
 415+ size = max(48, min(len(resp.data), size) + resp.queuesize)
 416+ if size < 0x800:
 417+ time.sleep(0.1 / 0x800 * (0x800 - size))
415418
416419 @command
417420 def writeusbconsole(self, *args):
Index: emcore/trunk/tools/libemcore.py
@@ -239,12 +239,12 @@
240240 return self.lib.monitorcommand(struct.pack("<IBBBBII%ds" % size, 9, index, slaveaddr, startaddr, size, 0, 0, data), "III", (None, None, None))
241241
242242 @command()
243 - def usbcread(self):
 243+ def usbcread(self, maxsize = 48):
244244 """ Reads one packet with the maximal cin size from the console """
245 - cin_maxsize = 48
246 - resp = self.lib.monitorcommand(struct.pack("<IIII", 10, cin_maxsize, 0, 0), "III%ds" % cin_maxsize, ("validsize", "buffersize", "queuesize", "data"))
 245+ maxsize = min(0xff0, maxsize)
 246+ resp = self.lib.monitorcommand(struct.pack("<IIII", 10, maxsize, 0, 0), "III%ds" % maxsize, ("validsize", "buffersize", "queuesize", "data"))
247247 resp.data = resp.data[:resp.validsize].decode("latin_1")
248 - resp.maxsize = cin_maxsize
 248+ resp.maxsize = maxsize
249249 return resp
250250
251251 @command()
Index: emcore/trunk/usb/usbdebug.c
@@ -64,8 +64,10 @@
6565 DBGSTATE_IDLE = 0,
6666 DBGSTATE_SETUP,
6767 DBGSTATE_WRITE_MEM,
 68+ DBGSTATE_WRITE_CONSOLE,
6869 DBGSTATE_ASYNC,
6970 DBGSTATE_RESPOND,
 71+ DBGSTATE_READ_CONSOLE,
7072 };
7173
7274 static struct scheduler_thread dbgthread_handle IBSS_ATTR;
@@ -157,39 +159,21 @@
158160 break;
159161 }
160162 case 10: // READ CONSOLE
161 - dbgconsoleattached = true;
162 - int bytes = dbgconsendwriteidx - dbgconsendreadidx;
163 - int used = 0;
164 - if (bytes)
165 - {
166 - if (bytes < 0) bytes += sizeof(dbgconsendbuf);
167 - used = bytes;
168 - if (bytes > buf[1]) bytes = buf[1];
169 - int readbytes = bytes;
170 - char* outptr = (char*)&dbgbuf[4];
171 - if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
172 - {
173 - readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
174 - memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
175 - dbgconsendreadidx = 0;
176 - outptr = &outptr[readbytes];
177 - readbytes = bytes - readbytes;
178 - }
179 - if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
180 - dbgconsendreadidx += readbytes;
181 - wakeup_signal(&dbgconsendwakeup);
182 - }
183 - dbgbuf[0] = 1;
184 - dbgbuf[1] = bytes;
185 - dbgbuf[2] = sizeof(dbgconsendbuf);
186 - dbgbuf[3] = used - bytes;
 163+ dbgmemaddr = (void*)buf[1];
 164+ dbgstate = DBGSTATE_READ_CONSOLE;
 165+ usb_ep0_start_tx(dbgusb, NULL, 0, true, NULL);
 166+ return true;
187167 break;
188168 case 11: // WRITE CONSOLE
189 - bytes = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
 169+ {
 170+ int total = 0;
 171+ int bytes = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
190172 if (bytes < 0) bytes += sizeof(dbgconrecvbuf);
191173 if (bytes)
192174 {
193175 if (bytes > buf[1]) bytes = buf[1];
 176+ total = bytes;
 177+ if (bytes > 48) bytes = 48;
194178 int writebytes = bytes;
195179 char* readptr = (char*)&buf[4];
196180 if (dbgconrecvwriteidx + bytes >= sizeof(dbgconrecvbuf))
@@ -205,10 +189,18 @@
206190 wakeup_signal(&dbgconrecvwakeup);
207191 }
208192 dbgbuf[0] = 1;
209 - dbgbuf[1] = bytes;
 193+ dbgbuf[1] = total;
210194 dbgbuf[2] = sizeof(dbgconrecvbuf);
 195+ if (total > 48)
 196+ {
 197+ dbgmemlen = total - 48;
 198+ dbgstate = DBGSTATE_WRITE_CONSOLE;
 199+ usb_ep0_start_rx(dbgusb, 1, usbdebug_handle_data);
 200+ return true;
 201+ }
211202 dbgbuf[3] = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
212203 break;
 204+ }
213205 case 15: // GET PROCESS INFO
214206 dbgbuf[0] = 1;
215207 dbgbuf[1] = SCHEDULER_THREAD_INFO_VERSION;
@@ -267,10 +259,10 @@
268260 break;
269261 case DBGSTATE_WRITE_MEM:
270262 {
271 - int len = MIN(64, dbgmemlen);
 263+ int len = MIN(64 - bytesleft, dbgmemlen);
272264 dbgmemlen -= len;
273265 memcpy(dbgmemaddr, buf, len);
274 - if (dbgmemlen)
 266+ if (dbgmemlen && !bytesleft)
275267 {
276268 dbgmemaddr += len;
277269 usb_ep0_start_rx(dbgusb, 1, usbdebug_handle_data);
@@ -279,6 +271,34 @@
280272 dbgbuf[0] = 1;
281273 break;
282274 }
 275+ case DBGSTATE_WRITE_CONSOLE:
 276+ {
 277+ int bytes = MIN(64 - bytesleft, dbgmemlen);
 278+ dbgmemlen -= bytes;
 279+ if (bytes)
 280+ {
 281+ int writebytes = bytes;
 282+ char* readptr = (char*)buf;
 283+ if (dbgconrecvwriteidx + bytes >= sizeof(dbgconrecvbuf))
 284+ {
 285+ writebytes = sizeof(dbgconrecvbuf) - dbgconrecvwriteidx;
 286+ memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
 287+ dbgconrecvwriteidx = 0;
 288+ readptr = &readptr[writebytes];
 289+ writebytes = bytes - writebytes;
 290+ }
 291+ if (writebytes) memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
 292+ dbgconrecvwriteidx += writebytes;
 293+ wakeup_signal(&dbgconrecvwakeup);
 294+ if (dbgmemlen && !bytesleft)
 295+ {
 296+ usb_ep0_start_rx(dbgusb, 1, usbdebug_handle_data);
 297+ return true;
 298+ }
 299+ }
 300+ dbgbuf[3] = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
 301+ if (dbgbuf[3] < 0) dbgbuf[3] += sizeof(dbgconrecvbuf);
 302+ }
283303 default: break;
284304 }
285305 dbgstate = DBGSTATE_RESPOND;
@@ -286,6 +306,39 @@
287307 return true;
288308 }
289309
 310+bool read_console_callback(const struct usb_instance* data, int bytesleft)
 311+{
 312+ if (bytesleft || !dbgmemaddr) return false;
 313+ dbgconsoleattached = true;
 314+ int bytes = MIN(64, dbgmemlen);
 315+ if (bytes)
 316+ {
 317+ int readbytes = bytes;
 318+ char* outptr = (char*)dbgbuf;
 319+ if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
 320+ {
 321+ readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
 322+ memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
 323+ dbgconsendreadidx = 0;
 324+ outptr = &outptr[readbytes];
 325+ readbytes = bytes - readbytes;
 326+ }
 327+ if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
 328+ dbgconsendreadidx += readbytes;
 329+ wakeup_signal(&dbgconsendwakeup);
 330+ dbgmemlen -= bytes;
 331+ }
 332+ bytes = MIN(64, (int)dbgmemaddr);
 333+ dbgmemaddr -= bytes;
 334+ if (dbgmemaddr)
 335+ {
 336+ usb_ep0_start_rx(dbgusb, 0, NULL);
 337+ usb_ep0_start_tx(dbgusb, dbgbuf, bytes, false, read_console_callback);
 338+ }
 339+ else usb_ep0_start_tx(dbgusb, dbgbuf, bytes, true, NULL);
 340+ return true;
 341+}
 342+
290343 int usbdebug_handle_setup(const struct usb_instance* data, int interface, union usb_ep0_buffer* request, const void** response)
291344 {
292345 int size = -1;
@@ -315,7 +368,7 @@
316369 usb_ep0_start_rx(dbgusb, 0, NULL);
317370 data->state->ep0_tx_ptr = dbgmemaddr - 16;
318371 data->state->ep0_tx_len = dbgmemlen;
319 - usb_ep0_start_tx(dbgusb, dbgbuf, len + 16, !data->state->ep0_tx_len, usb_ep0_tx_callback);
 372+ usb_ep0_start_tx(dbgusb, dbgbuf, len + 16, false, usb_ep0_tx_callback);
320373 return -3;
321374 }
322375 dbgstate = DBGSTATE_IDLE;
@@ -322,6 +375,47 @@
323376 *response = dbgbuf;
324377 return len + 16;
325378 }
 379+ case DBGSTATE_READ_CONSOLE:
 380+ {
 381+ dbgconsoleattached = true;
 382+ dbgmemlen = dbgconsendwriteidx - dbgconsendreadidx;
 383+ if (dbgmemlen < 0) dbgmemlen += sizeof(dbgconsendbuf);
 384+ int used = dbgmemlen;
 385+ if (dbgmemlen > (int)dbgmemaddr) dbgmemlen = (int)dbgmemaddr;
 386+ int bytes = MIN(48, dbgmemlen);
 387+ dbgbuf[0] = 1;
 388+ dbgbuf[1] = dbgmemlen;
 389+ dbgbuf[2] = sizeof(dbgconsendbuf);
 390+ dbgbuf[3] = used - dbgmemlen;
 391+ if (bytes)
 392+ {
 393+ int readbytes = bytes;
 394+ char* outptr = (char*)&dbgbuf[4];
 395+ if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
 396+ {
 397+ readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
 398+ memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
 399+ dbgconsendreadidx = 0;
 400+ outptr = &outptr[readbytes];
 401+ readbytes = bytes - readbytes;
 402+ }
 403+ if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
 404+ dbgconsendreadidx += readbytes;
 405+ wakeup_signal(&dbgconsendwakeup);
 406+ }
 407+ dbgmemlen -= bytes;
 408+ bytes = MIN(48, (int)dbgmemaddr);
 409+ dbgmemaddr -= bytes;
 410+ if (dbgmemaddr)
 411+ {
 412+ usb_ep0_start_rx(dbgusb, 0, NULL);
 413+ usb_ep0_start_tx(dbgusb, dbgbuf, bytes + 16, false, read_console_callback);
 414+ return -3;
 415+ }
 416+ dbgstate = DBGSTATE_IDLE;
 417+ *response = dbgbuf;
 418+ return bytes + 16;
 419+ }
326420 default: return -2;
327421 }
328422 break;