One thing pops out to me in start():

     ptr = (char*)buffer[bufferIndex];

This is turning a value in your buffer into a pointer
and it probably points to nothing. I think you want this:

     ptr = (char*)buffer;

which will make "ptr" point to the beginning of your buffer.
Have a look at a C reference, like
http://en.wikibooks.org/wiki/C_Programming
for details of pointer and array use.

It looks as if "bufferIndex" is meant to be used to do
some kind of buffer swapping. e.g., so you could fill one
while emptying another. But you only allocate one buffer
so it's not fully implemented and done incorrectly in
you example anyway.

Also check that you are reading/writing the right number
of bytes. ints are usually 2 bytes long in our micro-controllers.

MS

Li Zhao wrote:
> Hi,
> 
> Sorry to border and sorry for the long email.
> 
> I tried my best to build a sample for nesC. Since I have never done C 
> before, some of the coding may be unreasonable. Anyway, the program is:
> first, to automatically create an array buffer[maxdata] with value.
> second, in the start(), to write the buffer[] into EEPROM with Logger 
> component.
> third, when event Logger.writeDone() signalled, post task readTask() to 
> read that data from EEPROM into dataEEPROM. Here is the source code:
> 
> =================================================================================================
> FlashTest.nc
> configuration FlashTest {
> }
> 
> implementation {
>   components Main, FlashTestM, Logger, LedsC;
> 
>   Main.StdControl -> FlashTestM;
>  
>   FlashTestM.Leds -> LedsC;
>  
>   FlashTestM.LoggerControl -> Logger.StdControl;
>   FlashTestM.LoggerWrite -> Logger;
>   FlashTestM.LoggerRead -> Logger;
> }
> 
> FlashTestM.nc
> module FlashTestM {
>    provides {
>       interface StdControl;
>    }
>   
>    uses {
>       interface StdControl as LoggerControl;
>       interface Leds;
>       interface LoggerRead;
>       interface LoggerWrite;
>     }
> }
> 
> implementation
> {  
>    enum {
>       maxdata = 16       //circular buffer size
>    };
>   
>    char head;           // index to the head of the circular buffer
>    uint8_t bufferIndex;    // current index to the circular double buffer
>    int buffer[maxdata];    // circular double buffer
>   
>    uint16_t lineEEPROM;
>    uint8_t* dataEEPROM;
> 
>     command result_t StdControl.init() {
> 
>      atomic {
>            int i;    
>            bufferIndex = 0;
>            for (i=0;i<maxdata;i++) {
>               buffer[i]=(i*2);
>            }    //give initial value to the array     
>       }
>       return rcombine(call LoggerControl.init(), call Leds.init());
>    }
>  
>     command result_t StdControl.start(){    
>       char* ptr;
>       lineEEPROM = 13;
>       atomic {
>         ptr = (char*)buffer[bufferIndex];
>         bufferIndex ^= 0x01;
>       }
>       call LoggerWrite.append(ptr);
>       //call LoggerWrite.write(lineEEPROM, ptr);     
>       return call LoggerControl.start();
>     }
>  
>     command result_t StdControl.stop(){
>       return SUCCESS;
>     }
>    
>     task void readTask() {
>       atomic {
>         call LoggerRead.resetPointer();
>         call LoggerRead.readNext(dataEEPROM);
>         //call LoggerRead.read(lineEERPOM, dataEEPROM);
>       }
>     } 
> 
>    event result_t LoggerWrite.writeDone( result_t status ) {
>      if (status) call Leds.yellowToggle();
>      post readTask();
>      return SUCCESS;
>    }
>   
>   event result_t LoggerRead.readDone(uint8_t * packet, result_t status) {
>     if (status) call Leds.greenToggle();
>     return SUCCESS;
>   }  
> } // end of implementation
> 
> =====================================================================================
> However, I got errors during the simulation. It posted that:
> =====================================================================================
> $ ./build/pc/main.exe -t=10 1
> SIM: EEPROM system initialized.
> SIM: event queue initialized.
> SIM: Random seed is 502500
> SIM: Initializing sockets
> SIM: Created server socket listening on port 10584.
> SIM: Created server socket listening on port 10585.
> SIM: eventAcceptThread running.
> SIM: Time for mote 0 initialized to 33630025.
> SIM: commandReadThread running.
> 0: BOOT: Scheduling for boot at 0:0:8.40750625.
> 0: Popping event for mote 0 with time 0:0:8.40750625.
> 0: Setting TOS_LOCAL_ADDRESS to 0
> 0: BOOT: Mote booting at time 0:0:8.40750625.
> 0: LEDS: initialized.
> 0: Logger initialized.
> 0: Popping event for mote 0 with time 0:0:8.43127450.
> 0: Setting TOS_LOCAL_ADDRESS to 0
> 0: ERROR: Write to EEPROM failed: Bad address.
> 0: LOGGER: Log write to line 16 completed
> Segmentation fault (core dumped)
> ==========================================================================================
> After that, I changed my code. I replace code in StdControl.start()
> ==========================================================================================
>       call LoggerWrite.append(ptr);
>       //call LoggerWrite.write(lineEEPROM, ptr);  
>                                 with
>       //call LoggerWrite.append(ptr);
>       call LoggerWrite.write(lineEEPROM, ptr);  
> ===========================================================================================
> Than the simulation just ceased with no responding.
> ============================================================================================
> administra...@www-8e2ac5f0416 /cygdrive/c/program 
> files/ucb/cygwin/opt/tinyos-1.
> x/apps/flashtest
> $ ./build/pc/main.exe -t=10 1
> SIM: EEPROM system initialized.
> SIM: event queue initialized.
> SIM: Random seed is 502500
> SIM: Initializing sockets
> SIM: Created server socket listening on port 10584.
> SIM: Created server socket listening on port 10585.
> SIM: eventAcceptThread running.
> SIM: Time for mote 0 initialized to 33630025.
> SIM: commandReadThread running.
> 0: BOOT: Scheduling for boot at 0:0:8.40750625.
> 0: Popping event for mote 0 with time 0:0:8.40750625.
> 0: Setting TOS_LOCAL_ADDRESS to 0
> 0: BOOT: Mote booting at time 0:0:8.40750625.
> 0: LEDS: initialized.
> 0: Logger initialized.
>       just ceased here...have to ctrl+C to stop
> Exiting on SIGINT at 0:0:8.40750625.
> SIM: commandReadThreadFunc: error in select(): Interrupted system call
> =====================================================================================
> Sorry for that long email. But I really appreciate that you help me out. 
> Looking forward to your quick response.
> -- 
> Thanks
> Li ZHAO
> 
> (0044) 07500804257
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tinyos-help mailing list
> [email protected]
> https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

-- 
Platform: WinXP/Cygwin
TinyOS version: 1.x, Boomerang
Programmer: MIB510
Device(s): Mica2, MicaZ, Tmote
Sensor board: homebrew

_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to