goodfet.cc/carrier works when there's an excisting C app to set the oscillator.
[goodfet] / client / GoodFETCC.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 import sys;
9 import binascii;
10
11 from GoodFET import GoodFET;
12 from intelhex import IntelHex;
13
14 import xml.dom.minidom, time;
15
16 class GoodFETCC(GoodFET):
17     """A GoodFET variant for use with Chipcon 8051 Zigbee SoC."""
18     APP=0x30;
19     
20     
21     
22     
23     smartrfpath="/opt/smartrf7";
24     def loadsymbols(self):
25         try: self.SRF_loadsymbols();
26         except:
27             if self.verbose>0: print "SmartRF not found at %s." % self.smartrfpath;
28     def SRF_chipdom(self,chip="cc1110", doc="register_definition.xml"):
29         fn="%s/config/xml/%s/%s" % (self.smartrfpath,chip,doc);
30         #print "Opening %s" % fn;
31         return xml.dom.minidom.parse(fn)
32         
33     def CMDrs(self,args=[]):
34         """Chip command to grab the radio state."""
35         try:
36             self.SRF_radiostate();
37         except:
38             print "Error printing radio state.";
39             print "SmartRF not found at %s." % self.smartrfpath;
40     def SRF_bitfieldstr(self,bf):
41         name="unused";
42         start=0;
43         stop=0;
44         access="";
45         reset="0x00";
46         description="";
47         for e in bf.childNodes:
48             if e.localName=="Name" and e.childNodes: name= e.childNodes[0].nodeValue;
49             elif e.localName=="Start": start=e.childNodes[0].nodeValue;
50             elif e.localName=="Stop": stop=e.childNodes[0].nodeValue;
51         return "   [%s:%s] %30s " % (start,stop,name);
52     def SRF_radiostate(self):
53         ident=self.CCident();
54         chip=self.CCversions.get(ident&0xFF00);
55         dom=self.SRF_chipdom(chip,"register_definition.xml");
56         for e in dom.getElementsByTagName("registerdefinition"):
57             for f in e.childNodes:
58                 if f.localName=="DeviceName":
59                     print "// %s RadioState" % (f.childNodes[0].nodeValue);
60                 elif f.localName=="Register":
61                     name="unknownreg";
62                     address="0xdead";
63                     description="";
64                     bitfields="";
65                     for g in f.childNodes:
66                         if g.localName=="Name":
67                             name=g.childNodes[0].nodeValue;
68                         elif g.localName=="Address":
69                             address=g.childNodes[0].nodeValue;
70                         elif g.localName=="Description":
71                             if g.childNodes:
72                                 description=g.childNodes[0].nodeValue;
73                         elif g.localName=="Bitfield":
74                             bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
75                     #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
76                     print "%-10s=0x%02x; /* %-50s */" % (
77                         name,self.CCpeekdatabyte(eval(address)), description);
78                     if bitfields!="": print bitfields.rstrip();
79     def RF_setfreq(self,frequency):
80         """Set the frequency in Hz."""
81         #FIXME CC1110 specific
82         
83         hz=frequency;
84         freq=int(hz/396.728515625);
85         
86         freq0=freq&0xFF;
87         freq1=(freq&0xFF00)>>8;
88         freq2=(freq&0xFF0000)>>16;
89         
90         self.pokebysym("FREQ2",freq2);
91         self.pokebysym("FREQ1",freq1);
92         self.pokebysym("FREQ0",freq0);
93         
94
95     def RF_getfreq(self):
96         """Get the frequency in Hz."""
97         #FIXME CC1110 specific
98         
99         #return (2400+self.peek(0x05))*10**6
100         #self.poke(0x05,chan);
101         
102         #freq2=self.CCpeekdatabyte(0xdf09);
103         #freq1=self.CCpeekdatabyte(0xdf0a);
104         #freq0=self.CCpeekdatabyte(0xdf0b);
105         freq=0;
106         try:
107             freq2=self.peekbysym("FREQ2");
108             freq1=self.peekbysym("FREQ1");
109             freq0=self.peekbysym("FREQ0");
110             freq=(freq2<<16)+(freq1<<8)+freq0;
111         except:
112             freq=0;
113             
114         hz=freq*396.728515625;
115         
116         return hz;
117     
118     
119     def CC1110_crystal(self):
120         """Start the main crystal of the CC1110 oscillating, needed for radio use."""
121         
122         #//C code for the same.
123         #SLEEP &= ~SLEEP_OSC_PD;
124         #while( !(SLEEP & SLEEP_XOSC_S) ); 
125         #CLKCON = (CLKCON & ~(CLKCON_CLKSPD | CLKCON_OSC)) | CLKSPD_DIV_1;
126         #while (CLKCON & CLKCON_OSC); 
127         #SLEEP |= SLEEP_OSC_PD;
128         
129         #registers and constants.
130         #FIXME cc1110 specific
131         SLEEP=0xDFBE;
132         SLEEP_OSC_PD=0x04;
133         CLKCON=0xDFC6;
134         SLEEP_XOSC_S=0x40;
135         CLKCON_CLKSPD=0x07
136         CLKCON_OSC=0x40;
137         CLKSPD_DIV_1=0x00;
138         
139         sleep=self.peekbyte(SLEEP);
140         sleep&=~SLEEP_XOSC_S;
141         self.pokebyte(SLEEP,sleep);
142         while(0==(self.peekbyte(SLEEP)&SLEEP_XOSC_S)):
143             time.sleep(0.1);
144         clkcon=self.peekbyte(CLKCON);
145         clkcon=(clkcon & ~(CLKCON_CLKSPD | CLKCON_OSC)) | CLKSPD_DIV_1
146         self.pokebyte(CLKCON,clkcon);
147         clkcon=0;
148         while(clkcon&CLKCON_OSC):
149             clkcon=self.peekbyte(CLKCON);
150         sleep=self.peekbyte(SLEEP);
151         sleep|=SLEEP_OSC_PD;
152         self.pokebyte(SLEEP,sleep);
153         
154         return;
155     def RF_idle(self):
156         RFST=0xDFE1
157         self.pokebyte(RFST,0x04); #Return to idle state.
158         
159     def RF_carrier(self):
160         """Hold a carrier wave on the present frequency."""
161         
162         self.CC1110_crystal(); #FIXME, '1110 specific.
163         self.RF_idle();
164         
165         #self.resume();
166         #time.sleep(1);
167         #self.halt();
168         
169         RFST=0xDFE1;
170         
171         
172         #0a00
173         #self.pokebysym("FSCTRL1"  , 0x12)   # Frequency synthesizer control.
174         #self.pokebysym("FSCTRL0"  , 0x00)   # Frequency synthesizer control.
175         self.pokebysym("FSCTRL1"  , 0x0a)   # Frequency synthesizer control.
176         self.pokebysym("FSCTRL0"  , 0x00)   # Frequency synthesizer control.
177         
178         #Don't change these while the radio is active.
179         self.pokebysym("FSCAL3"   , 0xA9)   # Frequency synthesizer calibration.
180         self.pokebysym("FSCAL2"   , 0x0A)   # Frequency synthesizer calibration.
181         self.pokebysym("FSCAL1"   , 0x00)   # Frequency synthesizer calibration.
182         self.pokebysym("FSCAL0"   , 0x11)   # Frequency synthesizer calibration.
183         
184         #Ossmann's settings, not yet sure how they differ.
185         #self.pokebysym("FSCAL3"   , 0xEA)   # Frequency synthesizer calibration.
186         #self.pokebysym("FSCAL2"   , 0x2A)   # Frequency synthesizer calibration.
187         #self.pokebysym("FSCAL1"   , 0x00)   # Frequency synthesizer calibration.
188         #self.pokebysym("FSCAL0"   , 0x1F)   # Frequency synthesizer calibration.
189         
190         
191         #self.pokebysym("FREQ2"    , 0x10)   # Frequency control word, high byte.
192         #self.pokebysym("FREQ1"    , 0xEC)   # Frequency control word, middle byte.
193         #self.pokebysym("FREQ0"    , 0x4E)   # Frequency control word, low byte.
194         self.pokebysym("MDMCFG4"  , 0x86)   # Modem configuration.
195         self.pokebysym("MDMCFG3"  , 0x83)   # Modem configuration.
196         self.pokebysym("MDMCFG2"  , 0x30)   # Modem configuration.
197         self.pokebysym("MDMCFG1"  , 0x22)   # Modem configuration.
198         self.pokebysym("MDMCFG0"  , 0xF8)   # Modem configuration.
199         self.pokebysym("CHANNR"   , 0x00)   # Channel number.
200         self.pokebysym("DEVIATN"  , 0x00)   # Modem deviation setting (when FSK modulation is enabled).
201         self.pokebysym("FREND1"   , 0x56)   # Front end RX configuration.
202         
203         self.pokebysym("FREND0"   , 0x10)   # Front end RX configuration.
204         self.pokebysym("MCSM0"    , 0x14)   # Main Radio Control State Machine configuration.
205         self.pokebysym("FOCCFG"   , 0x16)   # Frequency Offset Compensation Configuration.
206         self.pokebysym("BSCFG"    , 0x6C)   # Bit synchronization Configuration.
207         
208         self.pokebysym("AGCCTRL2" , 0x03)   # AGC control.
209         self.pokebysym("AGCCTRL1" , 0x40)   # AGC control.
210         self.pokebysym("AGCCTRL0" , 0x91)   # AGC control.
211         
212         
213         
214         
215         
216         
217         self.pokebysym("TEST2"    , 0x88)   # Various test settings.
218         self.pokebysym("TEST1"    , 0x31)   # Various test settings.
219         self.pokebysym("TEST0"    , 0x09)   # Various test settings.
220         self.pokebysym("PA_TABLE0", 0xC0)   # PA output power setting.
221         self.pokebysym("PKTCTRL1" , 0x04)   # Packet automation control.
222         self.pokebysym("PKTCTRL0" , 0x22)   # Packet automation control.
223         self.pokebysym("ADDR"     , 0x00)   # Device address.
224         self.pokebysym("PKTLEN"   , 0xFF)   # Packet length.
225         
226         self.pokebysym("SYNC1",0xAA);
227         self.pokebysym("SYNC0",0xAA);
228         
229         
230                 
231         #while ((MARCSTATE & MARCSTATE_MARC_STATE) != MARC_STATE_TX); 
232         state=0;
233         
234         while((state!=0x13)):
235             self.pokebyte(RFST,0x03); #RFST=RFST_STX
236             time.sleep(0.1);
237             state=self.peekbysym("MARCSTATE")&0x1F;
238             print "state=%02x" % state;
239         print "Holding a carrier on %f MHz." % (self.RF_getfreq()/10**6);
240         
241         #Not needed, radio works when CPU is halted.
242         #self.resume();
243         
244         return;
245             
246             
247     def RF_getrssi(self):
248         """Returns the received signal strenght, with a weird offset."""
249         try:
250             rssireg=self.symbols.get("RSSI");
251             return self.CCpeekdatabyte(rssireg);
252         except:
253             if self.verbose>0: print "RSSI reg doesn't exist.";
254         try:
255             #RSSI doesn't exist on 2.4GHz devices.  Maybe RSSIL and RSSIH?
256             rssilreg=self.symbols.get("RSSIL");
257             rssil=self.CCpeekdatabyte(rssilreg);
258             rssihreg=self.symbols.get("RSSIL");
259             rssih=self.CCpeekdatabyte(rssihreg);
260             return (rssih<<8)|rssil;
261         except:
262             if self.verbose>0: print "RSSIL/RSSIH regs don't exist.";
263         
264         return 0;
265             
266     
267     def SRF_loadsymbols(self):
268         ident=self.CCident();
269         chip=self.CCversions.get(ident&0xFF00);
270         dom=self.SRF_chipdom(chip,"register_definition.xml");
271         for e in dom.getElementsByTagName("registerdefinition"):
272             for f in e.childNodes:
273                 if f.localName=="Register":
274                     name="unknownreg";
275                     address="0xdead";
276                     description="";
277                     bitfields="";
278                     for g in f.childNodes:
279                         if g.localName=="Name":
280                             name=g.childNodes[0].nodeValue;
281                         elif g.localName=="Address":
282                             address=g.childNodes[0].nodeValue;
283                         elif g.localName=="Description":
284                             if g.childNodes:
285                                 description=g.childNodes[0].nodeValue;
286                         elif g.localName=="Bitfield":
287                             bitfields+="%17s/* %-50s */\n" % ("",self.SRF_bitfieldstr(g));
288                     #print "SFRX(%10s, %s); /* %50s */" % (name,address, description);
289                     self.symbols.define(eval(address),name,description,"data");
290     def halt(self):
291         """Halt the CPU."""
292         self.CChaltcpu();
293     def CChaltcpu(self):
294         """Halt the CPU."""
295         self.writecmd(self.APP,0x86,0,self.data);
296     def resume(self):
297         self.CCreleasecpu();
298     def CCreleasecpu(self):
299         """Resume the CPU."""
300         self.writecmd(self.APP,0x87,0,self.data);
301     def test(self):
302         self.CCreleasecpu();
303         self.CChaltcpu();
304         #print "Status: %s" % self.CCstatusstr();
305         
306         #Grab ident three times, should be equal.
307         ident1=self.CCident();
308         ident2=self.CCident();
309         ident3=self.CCident();
310         if(ident1!=ident2 or ident2!=ident3):
311             print "Error, repeated ident attempts unequal."
312             print "%04x, %04x, %04x" % (ident1, ident2, ident3);
313         
314         #Single step, printing PC.
315         print "Tracing execution at startup."
316         for i in range(1,15):
317             pc=self.CCgetPC();
318             byte=self.CCpeekcodebyte(i);
319             #print "PC=%04x, %02x" % (pc, byte);
320             self.CCstep_instr();
321         
322         print "Verifying that debugging a NOP doesn't affect the PC."
323         for i in range(1,15):
324             pc=self.CCgetPC();
325             self.CCdebuginstr([0x00]);
326             if(pc!=self.CCgetPC()):
327                 print "ERROR: PC changed during CCdebuginstr([NOP])!";
328         
329         print "Checking pokes to XRAM."
330         for i in range(0xf000,0xf020):
331             self.CCpokedatabyte(i,0xde);
332             if(self.CCpeekdatabyte(i)!=0xde):
333                 print "Error in XDATA at 0x%04x" % i;
334         
335         #print "Status: %s." % self.CCstatusstr();
336         #Exit debugger
337         self.stop();
338         print "Done.";
339
340     def setup(self):
341         """Move the FET into the CC2430/CC2530 application."""
342         #print "Initializing Chipcon.";
343         self.writecmd(self.APP,0x10,0,self.data);
344     def CCrd_config(self):
345         """Read the config register of a Chipcon."""
346         self.writecmd(self.APP,0x82,0,self.data);
347         return ord(self.data[0]);
348     def CCwr_config(self,config):
349         """Write the config register of a Chipcon."""
350         self.writecmd(self.APP,0x81,1,[config&0xFF]);
351     def CClockchip(self):
352         """Set the flash lock bit in info mem."""
353         self.writecmd(self.APP, 0x9A, 0, None);
354     def lock(self):
355         """Set the flash lock bit in info mem."""
356         self.CClockchip();
357     
358
359     CCversions={0x0100:"cc1110",
360                 0x1100:"cc1111",
361                 0x8500:"cc2430",
362                 0x8900:"cc2431",
363                 0x8100:"cc2510",
364                 0x9100:"cc2511",
365                 0xA500:"cc2530", #page 52 of SWRU191
366                 0xB500:"cc2531",
367                 0xFF00:"CCmissing"};
368     CCpagesizes={0x01: 1024, #"CC1110",
369                  0x11: 1024, #"CC1111",
370                  0x85: 2048, #"CC2430",
371                  0x89: 2048, #"CC2431",
372                  0x81: 1024, #"CC2510",
373                  0x91: 1024, #"CC2511",
374                  0xA5: 2048, #"CC2530", #page 52 of SWRU191
375                  0xB5: 2048, #"CC2531",
376                  0xFF: 0    } #"CCmissing"};
377     def infostring(self):
378         return self.CCidentstr();
379     def CCidentstr(self):
380         ident=self.CCident();
381         chip=self.CCversions.get(ident&0xFF00);
382         pagesize=self.CCpagesizes.get(ident>0xFF);
383         try:
384             return "%s/r%0.4x/ps0x%0.4x" % (chip, ident, pagesize); 
385         except:
386             return "%04x" % ident;
387     def CCident(self):
388         """Get a chipcon's ID."""
389         self.writecmd(self.APP,0x8B,0,None);
390         chip=ord(self.data[0]);
391         rev=ord(self.data[1]);
392         return (chip<<8)+rev;
393     def CCpagesize(self):
394         """Get a chipcon's ID."""
395         self.writecmd(self.APP,0x8B,0,None);
396         chip=ord(self.data[0]);
397         size=self.CCpagesizes.get(chip);
398         if(size<10):
399             print "ERROR: Pagesize undefined.";
400             print "chip=%0.4x" %chip;
401             sys.exit(1);
402             #return 2048;
403         return size;
404     def getpc(self):
405         return self.CCgetPC();
406     def CCgetPC(self):
407         """Get a chipcon's PC."""
408         self.writecmd(self.APP,0x83,0,None);
409         hi=ord(self.data[0]);
410         lo=ord(self.data[1]);
411         return (hi<<8)+lo;
412     def CCcmd(self,phrase):
413         self.writecmd(self.APP,0x00,len(phrase),phrase);
414         val=ord(self.data[0]);
415         print "Got %02x" % val;
416         return val;
417     def CCdebuginstr(self,instr):
418         self.writecmd(self.APP,0x88,len(instr),instr);
419         return ord(self.data[0]);
420     def peekblock(self,adr,length,memory="vn"):
421         """Return a block of data."""
422         data=[adr&0xff, (adr&0xff00)>>8,
423               length&0xFF,(length&0xFF00)>>8];
424         self.writecmd(self.APP,0x91,4,data);
425         return [ord(x) for x in self.data]
426     def peek8(self,address, memory="code"):
427         if(memory=="code" or memory=="flash" or memory=="vn"):
428             return self.CCpeekcodebyte(address);
429         elif(memory=="data" or memory=="xdata" or memory=="ram"):
430             return self.CCpeekdatabyte(address);
431         elif(memory=="idata" or memory=="iram"):
432             return self.CCpeekirambyte(address);
433         print "%s is an unknown memory." % memory;
434         return 0xdead;
435     def CCpeekcodebyte(self,adr):
436         """Read the contents of code memory at an address."""
437         self.data=[adr&0xff, (adr&0xff00)>>8];
438         self.writecmd(self.APP,0x90,2,self.data);
439         return ord(self.data[0]);
440     def CCpeekdatabyte(self,adr):
441         """Read the contents of data memory at an address."""
442         self.data=[adr&0xff, (adr&0xff00)>>8];
443         self.writecmd(self.APP,0x91, 2, self.data);
444         return ord(self.data[0]);
445     def CCpeekirambyte(self,adr):
446         """Read the contents of IRAM at an address."""
447         self.data=[adr&0xff];
448         self.writecmd(self.APP,0x02, 1, self.data);
449         return ord(self.data[0]);
450     def CCpeekiramword(self,adr):
451         """Read the little-endian contents of IRAM at an address."""
452         return self.CCpeekirambyte(adr)+(
453             self.CCpeekirambyte(adr+1)<<8);
454     def CCpokeiramword(self,adr,val):
455         self.CCpokeirambyte(adr,val&0xff);
456         self.CCpokeirambyte(adr+1,(val>>8)&0xff);
457     def CCpokeirambyte(self,adr,val):
458         """Write the contents of IRAM at an address."""
459         self.data=[adr&0xff, val&0xff];
460         self.writecmd(self.APP,0x02, 2, self.data);
461         return ord(self.data[0]);
462     def pokebyte(self,adr,val,mem="data"):
463         if mem!="data":
464             print "FIXME: poking of non data bytes not yet supported.";
465         self.CCpokedatabyte(adr,val);
466     def CCpokedatabyte(self,adr,val):
467         """Write a byte to data memory."""
468         self.data=[adr&0xff, (adr&0xff00)>>8, val];
469         self.writecmd(self.APP, 0x92, 3, self.data);
470         return ord(self.data[0]);
471     def CCchiperase(self):
472         """Erase all of the target's memory."""
473         self.writecmd(self.APP,0x80,0,None);
474     def erase(self):
475         """Erase all of the target's memory."""
476         self.CCchiperase();
477         self.start();
478     
479     def CCstatus(self):
480         """Check the status."""
481         self.writecmd(self.APP,0x84,0,None);
482         return ord(self.data[0])
483     #Same as CC2530
484     CCstatusbits={0x80 : "erase_busy",
485                   0x40 : "pcon_idle",
486                   0x20 : "cpu_halted",
487                   0x10 : "pm0",
488                   0x08 : "halt_status",
489                   0x04 : "locked",
490                   0x02 : "oscstable",
491                   0x01 : "overflow"
492                   };
493     CCconfigbits={0x20 : "soft_power_mode",   #new for CC2530
494                   0x08 : "timers_off",
495                   0x04 : "dma_pause",
496                   0x02 : "timer_suspend",
497                   0x01 : "sel_flash_info_page" #stricken from CC2530
498                   };
499                   
500     def status(self):
501         """Check the status as a string."""
502         status=self.CCstatus();
503         str="";
504         i=1;
505         while i<0x100:
506             if(status&i):
507                 str="%s %s" %(self.CCstatusbits[i],str);
508             i*=2;
509         return str;
510     def start(self):
511         """Start debugging."""
512         self.setup();
513         self.writecmd(self.APP,0x20,0,self.data);
514         ident=self.CCidentstr();
515         #print "Target identifies as %s." % ident;
516         #print "Status: %s." % self.status();
517         self.CCreleasecpu();
518         self.CChaltcpu();
519         #Get SmartRF Studio regs if they exist.
520         self.loadsymbols(); 
521         
522     def stop(self):
523         """Stop debugging."""
524         self.writecmd(self.APP,0x21,0,self.data);
525     def CCstep_instr(self):
526         """Step one instruction."""
527         self.writecmd(self.APP,0x89,0,self.data);
528     def CCeraseflashbuffer(self):
529         """Erase the 2kB flash buffer"""
530         self.writecmd(self.APP,0x99);
531     def CCflashpage(self,adr):
532         """Flash 2kB a page of flash from 0xF000 in XDATA"""
533         data=[adr&0xFF,
534               (adr>>8)&0xFF,
535               (adr>>16)&0xFF,
536               (adr>>24)&0xFF];
537         print "Flashing buffer to 0x%06x" % adr;
538         self.writecmd(self.APP,0x95,4,data);
539     
540     def setsecret(self,value):
541         """Set a secret word for later retreival.  Used by glitcher."""
542         page = 0x0000;
543         pagelen = self.CCpagesize(); #Varies by chip.
544         print "page=%04x, pagelen=%04x" % (page,pagelen);
545         
546         self.CCeraseflashbuffer();
547         print "Setting secret to %x" % value;
548         self.CCpokedatabyte(0xF000,value);
549         self.CCpokedatabyte(0xF800,value);
550         print "Setting secret to %x==%x" % (value,
551                                             self.CCpeekdatabyte(0xf000));
552         self.CCflashpage(0);
553         print "code[0]=%x" % self.CCpeekcodebyte(0);
554     def getsecret(self):
555         """Get a secret word.  Used by glitcher."""
556         secret=self.CCpeekcodebyte(0);
557         #print "Got secret %02x" % secret;
558         return secret;
559     
560     def dump(self,file,start=0,stop=0xffff):
561         """Dump an intel hex file from code memory."""
562         print "Dumping code from %04x to %04x as %s." % (start,stop,file);
563         h = IntelHex(None);
564         i=start;
565         while i<=stop:
566             h[i]=self.CCpeekcodebyte(i);
567             if(i%0x100==0):
568                 print "Dumped %04x."%i;
569                 h.write_hex_file(file); #buffer to disk.
570             i+=1;
571         h.write_hex_file(file);
572
573     def flash(self,file):
574         """Flash an intel hex file to code memory."""
575         print "Flashing %s" % file;
576         
577         h = IntelHex(file);
578         page = 0x0000;
579         pagelen = self.CCpagesize(); #Varies by chip.
580         
581         #print "page=%04x, pagelen=%04x" % (page,pagelen);
582         
583         bcount = 0;
584         
585         #Wipe the RAM buffer for the next flash page.
586         self.CCeraseflashbuffer();
587         for i in h._buf.keys():
588             while(i>=page+pagelen):
589                 if bcount>0:
590                     self.CCflashpage(page);
591                     #client.CCeraseflashbuffer();
592                     bcount=0;
593                     print "Flashed page at %06x" % page
594                 page+=pagelen;
595                     
596             #Place byte into buffer.
597             self.CCpokedatabyte(0xF000+i-page,
598                                 h[i]);
599             bcount+=1;
600             if(i%0x100==0):
601                 print "Buffering %04x toward %06x" % (i,page);
602         #last page
603         self.CCflashpage(page);
604         print "Flashed final page at %06x" % page;
605