freemyipod r29 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r28‎ | r29 | r30 >
Date:23:40, 5 August 2010
Author:theseven
Status:new
Tags:
Comment:
Impelement powering off on iPod Nano 2G and console access via USB
Modified paths:
  • /embios/trunk/SOURCES (modified) (history)
  • /embios/trunk/init.c (modified) (history)
  • /embios/trunk/shutdown.c (added) (history)
  • /embios/trunk/shutdown.h (added) (history)
  • /embios/trunk/target/ipodnano2g/pmu.c (added) (history)
  • /embios/trunk/target/ipodnano2g/pmu.h (added) (history)
  • /embios/trunk/usb/usb.c (modified) (history)

Diff [purge]

Index: embios/trunk/init.c
@@ -28,10 +28,10 @@
2929 #include "lcdconsole.h"
3030 #include "interrupt.h"
3131 #include "i2c.h"
 32+#include "pmu.h"
3233 #include "usb/usb.h"
3334
34 -static const char welcomestring[] INITCONST_ATTR = "emBIOS v" VERSION "\n\nInitializing USB...";
35 -static const char donestring[] INITCONST_ATTR = " done\n";
 35+static const char welcomestring[] INITCONST_ATTR = "emBIOS v" VERSION "\n\n";
3636
3737 void init() INITCODE_ATTR;
3838 void init()
@@ -41,8 +41,8 @@
4242 lcd_init();
4343 lcdconsole_init();
4444 interrupt_init();
 45+ cputs(1, welcomestring);
4546 i2c_init();
46 - cputs(1, welcomestring);
 47+ pmu_init();
4748 usb_init();
48 - cputs(1, donestring);
4949 }
\ No newline at end of file
Index: embios/trunk/SOURCES
@@ -6,6 +6,7 @@
77 target/ipodnano2g/timer.c
88 target/ipodnano2g/i2c.c
99 target/ipodnano2g/interrupt.c
 10+target/ipodnano2g/pmu.c
1011 usb/synopsysotg.c
1112 #endif
1213
@@ -32,3 +33,4 @@
3334 ucl.S
3435 thread.c
3536 usb/usb.c
 37+shutdown.c
Index: embios/trunk/target/ipodnano2g/pmu.c
@@ -0,0 +1,41 @@
 2+//
 3+//
 4+// Copyright 2010 TheSeven
 5+//
 6+//
 7+// This file is part of emBIOS.
 8+//
 9+// emBIOS is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emBIOS is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emBIOS. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#include "global.h"
 26+#include "i2c.h"
 27+#include "pmu.h"
 28+#include "thread.h"
 29+
 30+
 31+static struct mutex pmumutex;
 32+
 33+
 34+void pmu_init()
 35+{
 36+ mutex_init(&pmumutex);
 37+}
 38+
 39+void poweroff()
 40+{
 41+ i2c_sendbyte(0, 0xe6, 0xc, 1);
 42+}
Index: embios/trunk/target/ipodnano2g/pmu.h
@@ -0,0 +1,35 @@
 2+//
 3+//
 4+// Copyright 2010 TheSeven
 5+//
 6+//
 7+// This file is part of emBIOS.
 8+//
 9+// emBIOS is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emBIOS is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emBIOS. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#ifndef __PMU_H__
 26+#define __PMU_H__
 27+
 28+
 29+#include "global.h"
 30+
 31+
 32+void pmu_init();
 33+void poweroff();
 34+
 35+
 36+#endif
Index: embios/trunk/usb/usb.c
@@ -32,6 +32,8 @@
3333 #include "i2c.h"
3434 #include "strlen.h"
3535 #include "contextswitch.h"
 36+#include "pmu.h"
 37+#include "shutdown.h"
3638
3739
3840 static uint8_t ctrlresp[2] CACHEALIGN_ATTR;
@@ -45,7 +47,11 @@
4648 DBGACTION_IDLE = 0,
4749 DBGACTION_I2CSEND,
4850 DBGACTION_I2CRECV,
49 - DBGACTION_POWEROFF
 51+ DBGACTION_RESET,
 52+ DBGACTION_POWEROFF,
 53+ DBGACTION_CWRITE,
 54+ DBGACTION_CREAD,
 55+ DBGACTION_CFLUSH
5056 };
5157
5258 static uint32_t dbgstack[0x100] STACK_ATTR;
@@ -52,10 +58,12 @@
5359 struct wakeup dbgwakeup IBSS_ATTR;
5460 extern struct scheduler_thread* scheduler_threads;
5561 static enum dbgaction_t dbgaction IBSS_ATTR;
56 -static int dbgi2cbus IBSS_ATTR;
57 -static int dbgi2cslave IBSS_ATTR;
58 -static int dbgi2caddr IBSS_ATTR;
59 -static int dbgi2clen IBSS_ATTR;
 62+static int dbgi2cbus;
 63+static int dbgi2cslave;
 64+static int dbgactionaddr;
 65+static int dbgactionlength;
 66+static int dbgactionconsoles;
 67+static int dbgactiontype;
6068 static char dbgconsendbuf[4096];
6169 static char dbgconrecvbuf[1024];
6270 static int dbgconsendreadidx IBSS_ATTR;
@@ -281,12 +289,12 @@
282290 }
283291 }
284292
285 -bool set_dbgaction(enum dbgaction_t action)
 293+bool set_dbgaction(enum dbgaction_t action, int addsize)
286294 {
287295 if (dbgaction != DBGACTION_IDLE)
288296 {
289297 dbgsendbuf[0] = 3;
290 - usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16);
 298+ usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16 + addsize);
291299 return true;
292300 }
293301 dbgaction = action;
@@ -325,10 +333,19 @@
326334 }
327335 break;
328336 case 2: // RESET
329 - reset();
 337+ if (dbgrecvbuf[1])
 338+ {
 339+ if (set_dbgaction(DBGACTION_RESET, 0)) break;
 340+ dbgsendbuf[0] = 1;
 341+ size = 16;
 342+ }
 343+ else reset();
330344 break;
331345 case 3: // POWER OFF
332 - set_dbgaction(DBGACTION_POWEROFF);
 346+ if (set_dbgaction(DBGACTION_POWEROFF, 0)) break;
 347+ dbgactiontype = dbgrecvbuf[1];
 348+ dbgsendbuf[0] = 1;
 349+ size = 16;
333350 break;
334351 case 4: // READ MEMORY
335352 dbgsendbuf[0] = 1;
@@ -351,19 +368,19 @@
352369 usb_drv_recv(dbgendpoints[2], (void*)dbgrecvbuf[1], dbgrecvbuf[2]);
353370 break;
354371 case 8: // READ I2C
355 - if (set_dbgaction(DBGACTION_I2CRECV)) break;
 372+ if (set_dbgaction(DBGACTION_I2CRECV, dbgrecvbuf[1] >> 24)) break;
356373 dbgi2cbus = dbgrecvbuf[1] & 0xff;
357374 dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
358 - dbgi2caddr = (dbgrecvbuf[1] >> 16) & 0xff;
359 - dbgi2clen = dbgrecvbuf[1] >> 24;
 375+ dbgactionaddr = (dbgrecvbuf[1] >> 16) & 0xff;
 376+ dbgactionlength = dbgrecvbuf[1] >> 24;
360377 break;
361378 case 9: // WRITE I2C
362 - if (set_dbgaction(DBGACTION_I2CSEND)) break;
 379+ if (set_dbgaction(DBGACTION_I2CSEND, 0)) break;
363380 dbgi2cbus = dbgrecvbuf[1] & 0xff;
364381 dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
365 - dbgi2caddr = (dbgrecvbuf[1] >> 16) & 0xff;
366 - dbgi2clen = dbgrecvbuf[1] >> 24;
367 - memcpy(dbgasyncsendbuf, &dbgsendbuf[4], dbgi2clen);
 382+ dbgactionaddr = (dbgrecvbuf[1] >> 16) & 0xff;
 383+ dbgactionlength = dbgrecvbuf[1] >> 24;
 384+ memcpy(dbgasyncsendbuf, &dbgsendbuf[4], dbgactionlength);
368385 break;
369386 case 10: // READ CONSOLE
370387 dbgconsoleattached = true;
@@ -419,6 +436,21 @@
420437 dbgsendbuf[3] = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
421438 size = 16;
422439 break;
 440+ case 12: // CWRITE
 441+ if (set_dbgaction(DBGACTION_CWRITE, 0)) break;
 442+ dbgactionconsoles = dbgrecvbuf[1];
 443+ dbgactionlength = dbgrecvbuf[2];
 444+ memcpy(dbgasyncsendbuf, &dbgrecvbuf[4], dbgactionlength);
 445+ break;
 446+ case 13: // CREAD
 447+ if (set_dbgaction(DBGACTION_CREAD, dbgrecvbuf[2])) break;
 448+ dbgactionconsoles = dbgrecvbuf[1];
 449+ dbgactionlength = dbgrecvbuf[2];
 450+ break;
 451+ case 14: // CFLUSH
 452+ if (set_dbgaction(DBGACTION_CFLUSH, 0)) break;
 453+ dbgactionconsoles = dbgrecvbuf[1];
 454+ break;
423455 default:
424456 dbgsendbuf[0] = 2;
425457 size = 16;
@@ -461,18 +493,40 @@
462494 switch (dbgaction)
463495 {
464496 case DBGACTION_I2CSEND:
465 - i2c_send(dbgi2cbus, dbgi2cslave, dbgi2caddr, (uint8_t*)dbgasyncsendbuf, dbgi2clen);
 497+ i2c_send(dbgi2cbus, dbgi2cslave, dbgactionaddr,
 498+ (uint8_t*)dbgasyncsendbuf, dbgactionlength);
466499 dbgasyncsendbuf[0] = 1;
467500 usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
468501 break;
469502 case DBGACTION_I2CRECV:
470 - i2c_recv(dbgi2cbus, dbgi2cslave, dbgi2caddr,
471 - (uint8_t*)(&dbgasyncsendbuf[4]), dbgi2clen);
 503+ i2c_recv(dbgi2cbus, dbgi2cslave, dbgactionaddr,
 504+ (uint8_t*)(&dbgasyncsendbuf[4]), dbgactionlength);
472505 dbgasyncsendbuf[0] = 1;
473 - usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16 + dbgi2clen);
 506+ usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16 + dbgactionlength);
474507 break;
475508 case DBGACTION_POWEROFF:
 509+ if (dbgactiontype) shutdown();
 510+ poweroff();
476511 break;
 512+ case DBGACTION_RESET:
 513+ shutdown();
 514+ reset();
 515+ break;
 516+ case DBGACTION_CWRITE:
 517+ cwrite(dbgactionconsoles, (const char*)dbgasyncsendbuf, dbgactionlength);
 518+ dbgasyncsendbuf[0] = 1;
 519+ usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
 520+ break;
 521+ case DBGACTION_CREAD:
 522+ cread(dbgactionconsoles, (char*)&dbgasyncsendbuf[4], dbgactionlength, 0);
 523+ dbgasyncsendbuf[0] = 1;
 524+ usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
 525+ break;
 526+ case DBGACTION_CFLUSH:
 527+ cflush(dbgactionconsoles);
 528+ dbgasyncsendbuf[0] = 1;
 529+ usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
 530+ break;
477531 }
478532 dbgaction = DBGACTION_IDLE;
479533 }
Index: embios/trunk/shutdown.c
@@ -0,0 +1,29 @@
 2+//
 3+//
 4+// Copyright 2010 TheSeven
 5+//
 6+//
 7+// This file is part of emBIOS.
 8+//
 9+// emBIOS is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emBIOS is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emBIOS. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#include "global.h"
 26+
 27+
 28+void shutdown()
 29+{
 30+}
Index: embios/trunk/shutdown.h
@@ -0,0 +1,34 @@
 2+//
 3+//
 4+// Copyright 2010 TheSeven
 5+//
 6+//
 7+// This file is part of emBIOS.
 8+//
 9+// emBIOS is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emBIOS is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emBIOS. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#ifndef __SHUTDOWN_H__
 26+#define __SHUTDOWN_H__
 27+
 28+
 29+#include "global.h"
 30+
 31+
 32+void shutdown();
 33+
 34+
 35+#endif