예제 #1
0
/////////////////////////////////////////////////////////////////////////////
// Checks if the device <device_addr> is present (blocking)
// IN: -
// OUT: returns 0 if device is present; < 0 on errors 
//      (see README for error codes)
/////////////////////////////////////////////////////////////////////////////
s32 FRAM_CheckAvailable(u8 device_addr){
  return FRAM_Read(device_addr, 0, NULL, 0);
  }
예제 #2
0
파일: app.c 프로젝트: gillspice/mios32
/////////////////////////////////////////////////////////////////////////////
// This task is running endless in background
/////////////////////////////////////////////////////////////////////////////
void APP_Background(void){
  u8 i;
  while(1){
    switch(phase){
      case BS_CHECK_PHASE_INIT:
	bs = 0;
	block = 0;
	last_error = 0;
	last_error_code = 0;
	time_read = 0xff;
	time_write = 0xff;
        // initialize IIC..
        if(run == 0){
	  MIOS32_MIDI_SendDebugMessage("Bankstick check init...");
#if BS_CHECK_USE_FRAM_LAYER == 0
	  if( (last_error_code = MIOS32_IIC_BS_Init(0)) != 0){
#else
	  if( (last_error_code = FRAM_Init(0)) != 0){ 
#endif
	    last_error =  BS_CHECK_ERROR_INIT;
	    phase = BS_CHECK_PHASE_FINISHED;
	    }
	  else{
	    for(i = 0;i < BS_CHECK_NUM_DEVICES; i++){
#if BS_CHECK_USE_FRAM_LAYER == 0
	      if ( (MIOS32_IIC_BS_CheckAvailable(i)) == 0) {
#else
	      if ( (FRAM_CheckAvailable(i)) != 0) {
#endif
		report_iic_error();
		last_error = BS_CHECK_ERROR_AVAILABLE;
		last_error_code = i;
		MIOS32_MIDI_SendDebugMessage("Bankstick %d not available",i);
		phase = BS_CHECK_PHASE_FINISHED;
		break;
		}
	      } 
            }
          }
        else{
          MIOS32_MIDI_SendDebugMessage("Start next check, run %d",run+1);
          } 
        if(!last_error)
          phase = BS_CHECK_PHASE_WRITE;
        break;
      case BS_CHECK_PHASE_WRITE:
        // write blocks
        if(bs == 0 && block == 0)
          MIOS32_MIDI_SendDebugMessage("Start writing blocks...");
        init_buffer();
#if BS_CHECK_USE_FRAM_LAYER == 0
        if( (last_error_code = MIOS32_IIC_BS_Write(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){
#else
        if( (last_error_code = FRAM_Write(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){
#endif
          report_iic_error();
          last_error =  BS_CHECK_ERROR_WRITE;
	  phase = BS_CHECK_PHASE_FINISHED;
          }
        else{
	  if( (last_error_code = wait_write_finished()) < 0){
	    report_iic_error();
            last_error =  BS_CHECK_ERROR_CHECK_WRITE_FINISHED;
	    phase = BS_CHECK_PHASE_FINISHED;
	    }
	  else if( ++block >= BS_CHECK_NUM_BLOCKS_PER_DEVICE ){
            MIOS32_MIDI_SendDebugMessage("Writing bankstick %d finished",bs);
	    block = 0;
	    if( ++bs >= BS_CHECK_NUM_DEVICES ){
	      bs = 0;
	      phase = BS_CHECK_PHASE_READ;
	      }
	    }
          }
        break;
      case BS_CHECK_PHASE_READ:
        // read blocks
        if(bs == 0 && block == 0)
          MIOS32_MIDI_SendDebugMessage("Start reading / comparing blocks...");
        clear_buffer();
#if BS_CHECK_USE_FRAM_LAYER == 0
        if( (last_error_code = MIOS32_IIC_BS_Read(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){
#else
        if( (last_error_code = FRAM_Read(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){
#endif
          report_iic_error();
	  last_error =  BS_CHECK_ERROR_READ;
	  phase = BS_CHECK_PHASE_FINISHED;
          }
	else{
	  if( !check_buffer() ){
	    last_error =  BS_CHECK_ERROR_COMPARE;
	    last_error_code = 0;// no further error specification
	    phase = BS_CHECK_PHASE_FINISHED;	   
	    }
	  else if( ++block >= BS_CHECK_NUM_BLOCKS_PER_DEVICE ){
            MIOS32_MIDI_SendDebugMessage("Reading / comparing bankstick %d finished",bs);
	    block = 0;
	    if( ++bs >= BS_CHECK_NUM_DEVICES ){
              MIOS32_MIDI_SendDebugMessage("Check run %d finished",run+1);
              if(++run >= BS_CHECK_NUM_RUNS)
                phase = BS_CHECK_PHASE_FINISHED;
              else{
                bs = 0;
                phase = BS_CHECK_PHASE_START;
                }
	      }
	    }
          }
        break;
      }
    }
  }


static s32 wait_write_finished(void){
#if BS_CHECK_USE_FRAM_LAYER == 0
  s32 err_code;
   do{
     err_code = MIOS32_IIC_BS_CheckWriteFinished(bs);
     } while( err_code > 0 );
  return err_code;
#else
  return 0;
#endif
  }

static void report_iic_error(void){
#if BS_CHECK_USE_FRAM_LAYER == 0
  MIOS32_MIDI_SendDebugMessage("IIC error %d",MIOS32_IIC_LastErrorGet(MIOS32_IIC_BS_PORT));
#else
  MIOS32_MIDI_SendDebugMessage("IIC error %d",MIOS32_IIC_LastErrorGet(FRAM_IIC_PORT));
#endif
  }