Example #1
0
int main(void) {
    // TODO disable JTAG

    /* STM32F4xx HAL library initialization:
         - Configure the Flash prefetch, instruction and Data caches
         - Configure the Systick to generate an interrupt each 1 msec
         - Set NVIC Group Priority to 4
         - Global MSP (MCU Support Package) initialization
       */
    HAL_Init();

    // set the system clock to be HSE
    SystemClock_Config();

    // enable GPIO clocks
    __GPIOA_CLK_ENABLE();
    __GPIOB_CLK_ENABLE();
    __GPIOC_CLK_ENABLE();
    __GPIOD_CLK_ENABLE();

    // enable the CCM RAM
    __CCMDATARAMEN_CLK_ENABLE();

#if 0
#if defined(NETDUINO_PLUS_2)
    {
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

#if MICROPY_HW_HAS_SDCARD
        // Turn on the power enable for the sdcard (PB1)
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET);
#endif

        // Turn on the power for the 5V on the expansion header (PB2)
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_WriteBit(GPIOB, GPIO_Pin_2, Bit_SET);
    }
#endif
#endif

    // basic sub-system init
    pendsv_init();
    led_init();
    switch_init0();

    int first_soft_reset = true;
    uint reset_mode;

soft_reset:

    // check if user switch held to select the reset mode
    reset_mode = 1;
    led_state(1, 0);
    led_state(2, 1);
    led_state(3, 0);
    led_state(4, 0);

#if MICROPY_HW_HAS_SWITCH
    if (switch_get()) {
        for (uint i = 0; i < 3000; i++) {
            if (!switch_get()) {
                break;
            }
            HAL_Delay(20);
            if (i % 30 == 29) {
                reset_mode = (reset_mode + 1) & 7;
                led_state(2, reset_mode & 1);
                led_state(3, reset_mode & 2);
                led_state(4, reset_mode & 4);
            }
        }
        // flash the selected reset mode
        for (uint i = 0; i < 6; i++) {
            led_state(2, 0);
            led_state(3, 0);
            led_state(4, 0);
            HAL_Delay(50);
            led_state(2, reset_mode & 1);
            led_state(3, reset_mode & 2);
            led_state(4, reset_mode & 4);
            HAL_Delay(50);
        }
        HAL_Delay(400);
    }
#endif

#if MICROPY_HW_ENABLE_RTC
    if (first_soft_reset) {
        rtc_init();
    }
#endif

    // more sub-system init
#if MICROPY_HW_HAS_SDCARD
    if (first_soft_reset) {
        sdcard_init();
    }
#endif
    if (first_soft_reset) {
        storage_init();
    }

    // GC init
    gc_init(&_heap_start, &_heap_end);

    // Change #if 0 to #if 1 if you want REPL on USART_6 (or another usart)
    // as well as on USB VCP
#if 0
    pyb_usart_global_debug = pyb_Usart(MP_OBJ_NEW_SMALL_INT(PYB_USART_YA),
                                       MP_OBJ_NEW_SMALL_INT(115200));
#else
    pyb_usart_global_debug = NULL;
#endif

    // Micro Python init
    qstr_init();
    mp_init();
    mp_obj_t def_path[3];
    def_path[0] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_);
    def_path[1] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_src);
    def_path[2] = MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib);
    mp_sys_path = mp_obj_new_list(3, def_path);

    readline_init();

    exti_init();

#if MICROPY_HW_HAS_SWITCH
    // must come after exti_init
    switch_init();
#endif

#if MICROPY_HW_HAS_LCD
    // LCD init (just creates class, init hardware by calling LCD())
    lcd_init();
#endif

    pin_map_init();

    // local filesystem init
    {
        // try to mount the flash
        FRESULT res = f_mount(&fatfs0, "0:", 1);
        if (reset_mode == 3 || res == FR_NO_FILESYSTEM) {
            // no filesystem, or asked to reset it, so create a fresh one

            // LED on to indicate creation of LFS
            led_state(PYB_LED_R2, 1);
            uint32_t start_tick = HAL_GetTick();

            res = f_mkfs("0:", 0, 0);
            if (res == FR_OK) {
                // success creating fresh LFS
            } else {
                __fatal_error("could not create LFS");
            }

            // create src directory
            res = f_mkdir("0:/src");
            // ignore result from mkdir

            // create empty main.py
            FIL fp;
            f_open(&fp, "0:/src/main.py", FA_WRITE | FA_CREATE_ALWAYS);
            UINT n;
            f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n);
            // TODO check we could write n bytes
            f_close(&fp);

            // keep LED on for at least 200ms
            sys_tick_wait_at_least(start_tick, 200);
            led_state(PYB_LED_R2, 0);
        } else if (res == FR_OK) {
            // mount sucessful
        } else {
            __fatal_error("could not access LFS");
        }
    }

    // make sure we have a /boot.py
    {
        FILINFO fno;
#if _USE_LFN
        fno.lfname = NULL;
        fno.lfsize = 0;
#endif
        FRESULT res = f_stat("0:/boot.py", &fno);
        if (res == FR_OK) {
            if (fno.fattrib & AM_DIR) {
                // exists as a directory
                // TODO handle this case
                // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
            } else {
                // exists as a file, good!
            }
        } else {
            // doesn't exist, create fresh file

            // LED on to indicate creation of boot.py
            led_state(PYB_LED_R2, 1);
            uint32_t start_tick = HAL_GetTick();

            FIL fp;
            f_open(&fp, "0:/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
            UINT n;
            f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n);
            // TODO check we could write n bytes
            f_close(&fp);

            // keep LED on for at least 200ms
            sys_tick_wait_at_least(start_tick, 200);
            led_state(PYB_LED_R2, 0);
        }
    }

    // run /boot.py
    if (reset_mode == 1) {
        if (!pyexec_file("0:/boot.py")) {
            flash_error(4);
        }
    }

    // turn boot-up LEDs off
    led_state(2, 0);
    led_state(3, 0);
    led_state(4, 0);

#if defined(USE_DEVICE_MODE)
    usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH;
#endif

#if MICROPY_HW_HAS_SDCARD
    // if an SD card is present then mount it on 1:/
    if (reset_mode == 1 && sdcard_is_present()) {
        FRESULT res = f_mount(&fatfs1, "1:", 1);
        if (res != FR_OK) {
            printf("[SD] could not mount SD card\n");
        } else {
            if (first_soft_reset) {
                // use SD card as medium for the USB MSD
#if defined(USE_DEVICE_MODE)
                usb_medium = USB_STORAGE_MEDIUM_SDCARD;
#endif
            }
        }
    }
#else
    // Get rid of compiler warning if no SDCARD is configured.
    (void)first_soft_reset;
#endif

#if defined(USE_HOST_MODE)
    // USB host
    pyb_usb_host_init();
#elif defined(USE_DEVICE_MODE)
    // USB device
    if (reset_mode == 1) {
        usb_device_mode_t usb_mode = USB_DEVICE_MODE_CDC_MSC;
        if (pyb_config_usb_mode != MP_OBJ_NULL) {
            if (strcmp(mp_obj_str_get_str(pyb_config_usb_mode), "CDC+HID") == 0) {
                usb_mode = USB_DEVICE_MODE_CDC_HID;
            }
        }
        pyb_usb_dev_init(usb_mode, usb_medium);
    } else {
        pyb_usb_dev_init(USB_DEVICE_MODE_CDC_MSC, usb_medium);
    }
#endif

#if MICROPY_HW_ENABLE_RNG
    // RNG
    rng_init();
#endif

    // I2C
    i2c_init();

#if MICROPY_HW_HAS_MMA7660
    // MMA accel: init and reset
    accel_init();
#endif

#if MICROPY_HW_ENABLE_SERVO
    // servo
    servo_init();
#endif

#if 0
#if MICROPY_HW_ENABLE_TIMER
    // timer
    timer_init();
#endif
#endif

#if MICROPY_HW_ENABLE_DAC
    // DAC
    dac_init();
#endif

    // run main script
    if (reset_mode == 1) {
        vstr_t *vstr = vstr_new();
        vstr_add_str(vstr, "0:/");
        if (pyb_config_source_dir == MP_OBJ_NULL) {
            vstr_add_str(vstr, "src");
        } else {
            vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_source_dir));
        }
        vstr_add_char(vstr, '/');
        if (pyb_config_main == MP_OBJ_NULL) {
            vstr_add_str(vstr, "main.py");
        } else {
            vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main));
        }
        if (!pyexec_file(vstr_str(vstr))) {
            flash_error(3);
        }
        vstr_free(vstr);
    }

#if 0
#if MICROPY_HW_HAS_WLAN
    // wifi
    pyb_wlan_init();
    pyb_wlan_start();
#endif
#endif

    // enter REPL
    // REPL mode can change, or it can request a soft reset
    for (;;) {
        if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
            if (pyexec_raw_repl() != 0) {
                break;
            }
        } else {
            if (pyexec_friendly_repl() != 0) {
                break;
            }
        }
    }

    printf("PYB: sync filesystems\n");
    storage_flush();

    printf("PYB: soft reboot\n");

    first_soft_reset = false;
    goto soft_reset;
}
Example #2
0
void i2c_lpc_start(i2c_bus_t* const bus, const void* const _config) {
	const i2c_lpc_config_t* const config = _config;

	const uint32_t port = (uint32_t)bus->obj;
	i2c_init(port, config->duty_cycle_count);
}
int main(void)
{
	uint16_t	adcPVoltages[BUFSIZE] = {0UL};
	uint16_t	adcNVoltages[BUFSIZE] = {0UL};
	uint16_t	adcCurrents[BUFSIZE] = {0UL};
	uint16_t	adcGNDs[BUFSIZE] = {0UL};

	uint16_t	adcValue;
	int16_t	gndValue;
	int16_t	mVoltage, mCurrent;
	uint8_t	idx;
	float	fv, fa;
		
	// ポートの初期化
	DDRD = _BV(2);			 // RSTピンを出力に
	PORTC = _BV(4) | _BV(5); // SCL, SDA内蔵プルアップを有効
	
	
	// デバイスの初期化
	i2c_init();				 // AVR内蔵I2Cモジュールの初期化
	wait_ms(500);
	
	PORTD &= ~_BV(2);		 // RSTをLにします。リセット
	wait_ms(1);
	PORTD |= _BV(2);		 // RSTをHにします。リセット解除
	wait_ms(10);
	
	lcdInit();
	
	//LCD初期表示
	lcdSetPos(0, 0);	lcdPutStr("Volt + / Current");
	lcdSetPos(0, 1);	lcdPutStr("Volt - / GND");
	wait_sec(3);
	
	//消費電力削減のためADC使用ピンをデジタル入力禁止にする(ADC入力(アナログ)専用となる)
	DIDR0 |= (1 << ADC0D) | (1 << ADC1D) | (1 << ADC2D) | (1 << ADC3D);	//PC0,PC1,PC2,PC3
	
	//ADC動作設定
	ADCSRA = 0b10000110		//bit2-0: 110 = 64分周	8MHz/64=125kHz (50k〜200kHzであること)
		| (1 << ADIE);		//割り込み許可(スリープ対応)
	
	//ADCの基準電圧(AVCC)と入力ピンを設定する
	ADMUX =	(1 << REFS0) | ADC_SET_PVOLTAGE;

	
	//A/D変換ノイズ低減のスリープモードを設定する
	set_sleep_mode(SLEEP_MODE_ADC);
	sei();			//割り込み許可
	sleep_mode();	//A/Dコンバータ初期化として初回変換開始…変換中…変換完了
	adcValue = ADC;	//値の読み捨て

	idx = 0;
	
    while(1)
    {
		//===== GND電圧を測定する ===================================================
		//
		ADMUX =	ADC_SET_GND;
		wait_ms(10);	//安定待ち
		sleep_mode();	//スリープモード突入…変換中…変換完了
		
		adcGNDs[idx] = ADC;
		adcValue = total(adcGNDs);
		gndValue = (float)adcValue / BUFSIZE;
		fv = gndValue * F_VCC * 1000 / 1024;	// mV単位
		
		//電圧を表示する
		mVoltage = (int16_t)fv;
		lcdPutVoltage(mVoltage, 1, 1);
		
        //===== 正電圧を測定する ===================================================
		//
		ADMUX =	ADC_SET_PVOLTAGE;
		wait_ms(10);	//安定待ち
		sleep_mode();	//スリープモード突入…変換中…変換完了
		
		adcPVoltages[idx] = ADC;
		adcValue = total(adcPVoltages);
			
		//A/D変換値から電圧を求める
		fv = (((float)adcValue / BUFSIZE) - gndValue) * F_VCC * 1000 / (1024 * F_POSITIVE_V_RATE); // mV単位
			
		//電圧を表示する
		mVoltage = (int16_t)fv;
		lcdPutVoltage(mVoltage, 0, 0);
					
		//===== 負電圧を測定する ===================================================
		//
		ADMUX =	ADC_SET_NVOLTAGE;
		wait_ms(10);	//安定待ち
		sleep_mode();	//スリープモード突入…変換中…変換完了
			
		adcNVoltages[idx] = ADC;
		adcValue = total(adcNVoltages);
			
		//A/D変換値から電圧を求める
		fv = (((float)adcValue / BUFSIZE) - gndValue) * F_VCC * 1000 / (1024 * F_NEGATIVE_V_RATE); // mV単位
					
		//電圧を表示する
		mVoltage = (int16_t)fv;
		lcdPutVoltage(mVoltage, 0, 1);
			
		//===== 電流を測定する ====================================================
		//
		ADMUX =	ADC_SET_CURRENT;
		wait_ms(10);	//安定待ち
		sleep_mode();	//スリープモード突入…変換中…変換完了
			
		adcCurrents[idx] = ADC;
		adcValue = total(adcCurrents);

		// ADCの読み取り値が1000以上の場合測定不可とする
		if ((float)adcValue / BUFSIZE < 1000) {
	
			//A/D変換値から電圧を求め、電流を算出する
			fv = (((float)adcValue / BUFSIZE) - gndValue) * F_VCC * 1000 / (1024 * F_CURRENT_AMP); // mV単位
			fa = fv / F_SHUNT_R;	//mA単位
				
			//電流を表示する
			mCurrent = (int16_t)(fa * 10.0);	//0.1mA単位
			lcdPutCurrent(mCurrent, 1, 0);
		}
		else {		
			lcdLineClear(1, 0);
			lcdSetPos(8, 0);
			lcdPutStr("  OVER");
		}
		
		//次のループの準備
		if (++idx == BUFSIZE) idx = 0;
    }
	
	return 0;
}
Example #4
0
int main (int argc, char * argv[]){
  
  int tst = argc < 2 ? 1 : atoi(argv[1]);

  i2c_init(LOG_PRINT);

  switch(tst){
  case 0: //Explore regs in a given range for a device attached to a given port 
    {
      int port = argc < 3 ? 1 : atoi(argv[2]);
      uint8_t range_ini = argc < 4 ? 0x00 : (uint8_t)atoi(argv[3]);
      uint8_t range_fi = argc < 5 ? 0xff : (uint8_t)atoi(argv[4]);
      int retries = argc < 6 ? 10 : atoi(argv[5]), retry = 0;

      int sda = port == 1 ? SDA_1 : SDA_2;
      int scl = port == 1 ? SCL_1 : SCL_2;
      
      uint8_t data_out[] = {range_ini};
      uint16_t data_in[1];
      bool ret;

      I2C_DVC dvc;
      
      i2c_new_device(&dvc, DVC_ADDR, FREQ, sda, scl, GPPUD_PULLDOWN, GPPUD_PULLUP);

      i2c_set_loglvl(LOG_QUIET);
      
      while(data_out[0] < range_fi) {
	  if (!(ret = i2c_transfer(&dvc, data_out , 1, true, data_in, 1, false, 10000))){
	    printf("NACK sending: 0x%02x\n", data_out[0]);
	    retry ++;
	    sleep(1);
	  }
	  if (retry > retries) {
	    if(data_out[0] <= range_fi-1)
	      printf("switching to REG: 0x%02x\n", data_out[0] +1);
	    data_out[0] += 1; 
	    retry = 0;
	  } else if (ret) {
	    printf("sended: 0x%02x, received: 0x%04x / %u / %c in %d retries\n", data_out[0], data_in[0], data_in[0], data_in[0], retry);
	    data_out[0] += 1; 
	    retry = 0;
	  }
      }
      i2c_shutdown(&dvc);
    }
    break;;
  case 1: //Send command to a device plugged into a given port
    {
      
      int port = argc < 3 ? 1 : atoi(argv[2]);
      int index = argc < 4 ? 2 : atoi(argv[3]);
            
      uint8_t cmd, reg;
      int delay;

      int sda = port == 1 ? SDA_1 : SDA_2;
      int scl = port == 1 ? SCL_1 : SCL_2;
      
      I2C_DVC dvc;
           
      if(argc < 5){
        reg = cmds[index].reg;
	cmd = cmds[index].val;
	delay = cmds[index].delay;
      } else {
	reg = scmds[index].reg;
	cmd = (uint8_t)atoi(argv[4]);
	delay = scmds[index].delay;
      }

      uint8_t data_aux[] = {reg,cmd};

      i2c_new_device(&dvc, DVC_ADDR, FREQ, sda, scl, GPPUD_PULLDOWN, GPPUD_PULLUP);

      i2c_write(&dvc, data_aux, 2, delay);
      
      i2c_shutdown(&dvc);

    }
    break;;
  case 2: //Ultrasonic stuff
    {

      int port = argc < 3 ? 1 : atoi(argv[2]);
      int i, k;
      uint8_t cmd_ss[2];
      uint16_t data_in [] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00};
      uint8_t data_aux[1];
      
      int sda = port == 1 ? SDA_1 : SDA_2;
      int scl = port == 1 ? SCL_1 : SCL_2;
            
      I2C_DVC us;

      i2c_new_device(&us, DVC_ADDR, FREQ, sda, scl, GPPUD_PULLDOWN, GPPUD_PULLUP);
      
      for (i=0; i<US_REGS; i++){ 
	data_aux[0] = usregs[i].base;
	if (i < US_BASE_DIST)
	  i2c_transfer(&us, data_aux, 1, true, data_in, usregs[i].len, false, usregs[i].delay);
	else {
	  cmd_ss [0] = cmds[1].reg;
	  cmd_ss [1] = cmds[1].val;
	  i2c_write(&us, cmd_ss, 2, cmds[1].delay);
	  i2c_transfer(&us, data_aux, 1, true, data_in, usregs[i].len, false, usregs[i].delay);
	}
	if(i == 0 || i == 1 || i == 2 || i == 6 ){
	  printf("%s", usregs[i].def);
	  for (k=0; k<usregs[i].len; k++)
	    printf("%c",data_in[k]);
	  printf("\n");
	} else if (i < US_BASE_DIST)
	  printf("%s0x%02x\n", usregs[i].def, data_in[0]);
	else
	  printf("%s%u\n", usregs[i].def, data_in[0]);

      }
      i2c_shutdown(&us);
    }
    break;;
  case 3://color sensor HiTechnic
    {
      int port = argc < 3 ? 2 : atoi(argv[2]);
      uint8_t data_aux [1];
      uint16_t data_in [1];
      char str_aux [COL_MAX_LEN+1];
      
      int sda = port == 1 ? SDA_1 : SDA_2;
      int scl = port == 1 ? SCL_1 : SCL_2;
      
      I2C_DVC col;

      i2c_new_device(&col, DVC_ADDR, FREQ, sda, scl, GPPUD_PULLUP, GPPUD_PULLUP);	
      
      int i, k;
      uint16_t auxraw = 0;
      
      for (i=0; i < COL_REGS; i++){
	for (k=0; k < cregs[i].len; k++){
	  data_aux[0] = cregs[i].base + k;
	  
	  if(i2c_transfer(&col, data_aux, 1, false, data_in, 1, false, cregs[i].delay)) {
	    
	    if(i < COL_BASE_DATA)
	      str_aux[k] = (char) data_in[0];
	    
	    else if(i>6 && i<10) {
	      if((cregs[i].base + k)%2 == 0)
		auxraw = (data_in[0] & 0xff);
	      else {
		auxraw <<= BYTE_LEN;
		auxraw |= (data_in[0] & 0xff);
	      }
	    }
	    
	  } else 
	    printf("NAK sending >> %s0x%02x\n", cregs[i].def, cregs[i].base + k);  
	  
	}
	if(i < COL_BASE_DATA) {
	  str_aux[k] = '\0';
	  printf("%s%s\n", cregs[i].def, str_aux);
	} else if(i>6 && i<10) 
	  printf("%s0x%04x / %u\n", cregs[i].def, auxraw, auxraw);
	else if(i == 10){
	  printf("%s0x%02x / ", cregs[i].def, data_in[0]);
	  for (k=BYTE_LEN-3; k >= 0; k--) {
	    if (data_in[0] & (0x01 << k))
	      printf("1");
	    else 
	      printf("0");
	  }
	  printf("\n");
	}else
	  printf("%s0x%02x / %u\n", cregs[i].def, data_in[0], data_in[0]);
      }
      
    
    i2c_shutdown(&col);
  }
  break;;  
 case 4://IR seeker HiTechnic
   {
     int port = argc < 3 ? 2 : atoi(argv[2]);
     uint8_t data_aux [1];
     uint16_t data_in [1];
     char str_aux [IRS_MAX_LEN+1];
     
     int sda = port == 1 ? SDA_1 : SDA_2;
     int scl = port == 1 ? SCL_1 : SCL_2;
     
     I2C_DVC irs;
     
     i2c_new_device(&irs, DVC_ADDR, FREQ, sda, scl, GPPUD_PULLUP, GPPUD_PULLUP);	
     
     int i, k;

     for (i=0; i < IRS_REGS; i++){
	for (k=0; k < irsregs[i].len; k++){
	  data_aux[0] = irsregs[i].base + k;
	  
	  if(i2c_transfer(&irs, data_aux, 1, false, data_in, 1, false, irsregs[i].delay)) {
	    
	    if(i < IRS_BASE_DATA)
	      str_aux[k] = (char) data_in[0];
	        
	  } else 
	    printf("NAK sending >> %s0x%02x\n", irsregs[i].def, irsregs[i].base + k);  
	  
	}
	if(i < IRS_BASE_DATA) {
	  str_aux[k] = '\0';
	  printf("%s%s\n", irsregs[i].def, str_aux);
	} else
	  printf("%s0x%02x / %u\n", irsregs[i].def, data_in[0], data_in[0]);
	
     }
     i2c_shutdown(&irs);
   }
   break;; 
  case 5: //Hitechnic accelerometer
    {
      int port = argc < 3 ? 2 : atoi(argv[2]);
      uint8_t data_aux [1];
      uint16_t data_in [1];
      char str_aux [AC_MAX_LEN+1];
      
      int sda = port == 1 ? SDA_1 : SDA_2;
      int scl = port == 1 ? SCL_1 : SCL_2;
      
      int x = 0, y = 0 ,z = 0;
 
      I2C_DVC acc;
      
      i2c_new_device(&acc, DVC_ADDR, FREQ, sda, scl, GPPUD_PULLUP, GPPUD_PULLUP);	
      
      int i, k;
      
      while (1){
      
      for (i=0; i < AC_REGS; i++){
	for (k=0; k < acregs[i].len; k++){
	  data_aux[0] = acregs[i].base + k;
	  
	  if(i2c_transfer(&acc, data_aux, 1, false, data_in, 1, false, acregs[i].delay)) {
	    
	    if(i < AC_BASE_DATA)
	      str_aux[k] = (char) data_in[0];
	    else {
	      switch(acregs[i].base + k){
	      case 0x42:
		{
		  x = data_in[0] & 0x80 ? (data_in[0] - 256) << 2 : data_in[0] << 2;
		}
		break;;
	      case 0x43:
		{
		  y = data_in[0] & 0x80 ? (data_in[0] - 256) << 2 : data_in[0] << 2;
		}
		break;;
	      case 0x44:
		{
		  z = data_in[0] & 0x80 ? (data_in[0] - 256) << 2 : data_in[0] << 2;
		}
		break;;
	      case 0x45:
		{
		  x |= data_in[0] & 0x03;
		}
		break;;
	      case 0x46:
		{
		  y |= data_in[0] & 0x03;
		}
		break;;
	      case 0x47:
		{
		  z |= data_in[0] & 0x03;
		}
		break;;
	      default:
		break;;
	      }

	    }
	  } else 
	    printf("NAK sending >> %s0x%02x\n", acregs[i].def, acregs[i].base + k);  
	  
	}
	if(i < AC_BASE_DATA) {
	  str_aux[k] = '\0';
	  printf("%s%s\n", acregs[i].def, str_aux);
	} 	
      }
      
      printf("X: %d\n", x);
      printf("Y: %d\n", y);
      printf("Z: %d\n", z);
      sleep(1);
      
      }
      i2c_shutdown(&acc);
    }
    break;; 
  case 6://Compass sensor HiTechnic
    {
      int port = argc < 3 ? 2 : atoi(argv[2]);
      uint8_t data_aux [1];
      uint16_t data_in [1];
      char str_aux [COM_MAX_LEN+1];
      
      int sda = port == 1 ? SDA_1 : SDA_2;
      int scl = port == 1 ? SCL_1 : SCL_2;
      
      int dsigned = 0;
      uint16_t dunsigned = 0;

      I2C_DVC com;
     
      i2c_new_device(&com, DVC_ADDR, FREQ, sda, scl, GPPUD_PULLUP, GPPUD_PULLUP);	
      
      int i, k;
     
      for (i=0; i < COM_REGS; i++){
	for (k=0; k < comregs[i].len; k++){
	  data_aux[0] = comregs[i].base + k;
	  
	  if(i2c_transfer(&com, data_aux, 1, false, data_in, 1, false, comregs[i].delay)) {
	    
	    if(i < COM_BASE_DATA)
	      str_aux[k] = (char) data_in[0];
	    else {
	      switch(comregs[i].base + k){
	      case 0x42:
		{
		  dsigned = (int) data_in[0]*2;
		}
		break;;
	      case 0x43:
		{
		  dsigned += data_in[0];
		}
		break;;
	      case 0x44:
		{
		  dunsigned = data_in[0]; 
		}
		break;;
	      case 0x45:
		{
		  dunsigned |= data_in[0] << BYTE_LEN;
		}
		break;;
	      default:
		break;;
	      }
	    }
	    
	  } else 
	    printf("NAK sending >> %s0x%02x\n", comregs[i].def, comregs[i].base + k);  
	  
	}
	if(i < COM_BASE_DATA){
	  str_aux[k] = '\0';
	  printf("%s%s\n", comregs[i].def, str_aux);
	} else if(comregs[i].base == 0x41)
	  printf("%s0x%02x / %u\n", comregs[i].def, data_in[0], data_in[0]);
	
      }
      
      printf("Degree signed: %d\n", dsigned);
      printf("Degree unsigned: %u\n", dunsigned);
      
      i2c_shutdown(&com);
    }
    break;;
  case 7: //useless test ...
    {
      I2C_DVC col;
      
      uint8_t data_aux[] = {0x43};
      uint16_t data_in[3];
 
      i2c_new_device(&col, DVC_ADDR, FREQ, SDA_2, SCL_2, GPPUD_PULLUP, GPPUD_PULLUP);
      
      if(i2c_transfer(&col, data_aux, 1, false, data_in, 3, false, 0))
      
	printf("Received: 0x%02x / %u, 0x%02x / %u, 0x%02x / %u \n", data_in[0], data_in[0], data_in[1], data_in[1], data_in[2], data_in[2]);
      
      else 

	printf("NACK!\n");

      i2c_shutdown(&col);

    }
    break;;
  default:
    break;;
  }
  
  return (EXIT_SUCCESS);
  
}
Example #5
0
static int setup_pmic_voltages(void)
{
	unsigned char value, rev_id = 0;

	i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
	if (!i2c_probe(0x8)) {
		if (i2c_read(0x8, 0, 1, &value, 1)) {
			printf("Read device ID error!\n");
			return -1;
		}
		if (i2c_read(0x8, 3, 1, &rev_id, 1)) {
			printf("Read Rev ID error!\n");
			return -1;
		}
		printf("Found PFUZE100! deviceid=%x,revid=%x\n", value, rev_id);

		/* set SW1AB staby volatage 0.975V */
		if (i2c_read(0x8, 0x21, 1, &value, 1)) {
			printf("Read SW1ABSTBY error!\n");
			return -1;
		}
		value &= ~0x3f;
		value |= 0x1b;
		if (i2c_write(0x8, 0x21, 1, &value, 1)) {
			printf("Set SW1ABSTBY error!\n");
			return -1;
		}

		/* set SW1AB/VDDARM step ramp up time from 16us to 4us/25mV */
		if (i2c_read(0x8, 0x24, 1, &value, 1)) {
			printf("Read SW1ABCONFIG error!\n");
			return -1;
		}
		value &= ~0xc0;
		value |= 0x40;
		if (i2c_write(0x8, 0x24, 1, &value, 1)) {
			printf("Set SW1ABCONFIG error!\n");
			return -1;
		}

		/* set SW1C staby volatage 0.975V */
		if (i2c_read(0x8, 0x2f, 1, &value, 1)) {
			printf("Read SW1CSTBY error!\n");
			return -1;
		}
		value &= ~0x3f;
		value |= 0x1b;
		if (i2c_write(0x8, 0x2f, 1, &value, 1)) {
			printf("Set SW1CSTBY error!\n");
			return -1;
		}

		/* set SW1C/VDDSOC step ramp up time to from 16us to 4us/25mV */
		if (i2c_read(0x8, 0x32, 1, &value, 1)) {
			printf("Read SW1CCONFIG error!\n");
			return -1;
		}
		value &= ~0xc0;
		value |= 0x40;
		if (i2c_write(0x8, 0x32, 1, &value, 1)) {
			printf("Set SW1CCONFIG error!\n");
			return -1;
		}
	}

	return 0;
}
Example #6
0
int main(void) {

	DDRA |= (1<<PA7);
	PORTA |= (1<<PA7); // podœwietlenie wyœwietlacza LCD

	// Przerwanie INT0
	MCUCR |= (1<<ISC01);	// wyzwalanie zboczem opadaj¹cym
	GICR |= (1<<INT0);		// odblokowanie przerwania
	PORTD |= (1<<PD2);		// podci¹gniêcie pinu INT0 do VCC


	// definiujemy sobie dla polepszenia czytelnoœci programu typ wyliczeniowy
	// wskazuj¹cy nam póŸniej na odpowiednie indeksy w tablicy (buforze)
	enum {ss=1, mm, hh};
	uint8_t bufor[4];		// rezerwacja bufora 4 bajty
	uint8_t sekundy, minuty, godziny;


	i2c_init();
	lcd_init();

	sei();

	lcd_str_P(PSTR("start..."));

	// Ustawianie czasu na godzinê: 18:34:27
	/*
	bufor[0] = 0;			// setne czêœci sekundy
	bufor[1] = dec2bcd(27);	// sekundy
	bufor[2] = dec2bcd(34);	// minuty
	bufor[3] = dec2bcd(18);	// godziny
	// zapis 4 bajtów z bufora pod adres 0x01 w pamiêci RAM naszego RTC
	I2C_write_buf( PCF8583_ADDR, 0x01, 4, bufor );
	*/

	// zapis tekstu do pamiêci EEPROM od adresu 253, dlatego aby tekst
	// zosta³ zapisany w jednym i drugim banku pamiêci
	EI2C_write_buf( _24C04_ADDR, 253, sizeof(tekst), tekst );

	while(1) {

		if ( int0_flag ) {
			//odczyt 4 bajtów do bufora od adresu 0x01 z pamiêci RAM naszego RTC
			I2C_read_buf( PCF8583_ADDR, 0x01, 4, bufor );

			sekundy = bcd2dec( bufor[ss] );
			minuty = bcd2dec( bufor[mm] );
			godziny = bcd2dec( bufor[hh] );

			// wyœwietlenie czasu na LCD
			lcd_locate(1,0);
			if( godziny < 10 ) lcd_str("0");
			lcd_int(godziny);
			lcd_str(":");
			if( minuty < 10 ) lcd_str("0");
			lcd_int(minuty);
			lcd_str(":");
			if( sekundy < 10 ) lcd_str("0");
			lcd_int(sekundy);

			// odczyt z EEPROM
			EI2C_read_buf( _24C04_ADDR, 253, sizeof(tekst), bo );

			// wyœwietlenie napisu z EEPROM na LCD
			lcd_locate(0, 9);
			lcd_str( (char*)bo );

			int0_flag=0;
		}

	}
}
Example #7
0
int main(void) {
  // disable watchdog (pin 6 output)
  DDRD |= _BV(PIND6);
  // enable LED (pin 5 output)
  DDRB |= _BV(PINB5);

  // just in case we use I2C2 (second port)
  // enable power (pin 3 output + HIGH)
  DDRB |= _BV(PINB3);
  PORTB |= _BV(PORTB3);

  UART_INIT_STDIO();

  blink(3);
  prompt("press enter to start:");

  i2c_init(I2C_SPEED_50KHZ);

  // reset device
  if (!isl_reset()) {
    puts("could not initialize ISL29125 RGB sensor");
  }

  // set sampling mode, ir filter and interrupt mode
  isl_set(ISL_R_FILTERING, ISL_FILTER_IR_MAX);
  isl_set(ISL_R_COLOR_MODE, ISL_MODE_RGB | ISL_MODE_10KLUX | ISL_MODE_16BIT);

  printf("read mode: ");
  uint8_t color_mode = isl_get(ISL_R_COLOR_MODE);
  print_bits(1, &color_mode);
  puts("");
  printf("read filter: ");
  uint8_t ir_filtering = isl_get(ISL_R_FILTERING);
  print_bits(1, &ir_filtering);
  puts("");
  printf("read interrupts: ");
  uint8_t intr = isl_get(ISL_R_INTERRUPT);
  print_bits(1, &intr);
  puts("");

  puts("reading RGB values from sensor");
  puts("'%' indicates the chip is still in a conversion cyle, so we wait");
  while (1) {
    // reset device
    if (!isl_reset()) {
      puts("could not initialize ISL29125 RGB sensor");
    }


    // set sampling mode, ir filter and interrupt mode
    isl_set(ISL_R_FILTERING, ISL_FILTER_IR_MAX);
    isl_set(ISL_R_COLOR_MODE, ISL_MODE_RGB | ISL_MODE_10KLUX | ISL_MODE_16BIT);

    _delay_ms(600);

    // read the full 36 or 48 bit color
    printf("48bit: ");
    rgb48 rgb = isl_read_rgb();
    printf("0x%04x%04x%04x rgb48(%u,%u,%u)\n", rgb.green, rgb.red, rgb.blue, rgb.red, rgb.green, rgb.blue);

    printf("24bit: ");
    rgb24 rgb8 = isl_read_rgb24();
    printf("0x%02x%02x%02x rgb24(%u,%u,%u)\n", rgb8.red, rgb8.green, rgb8.blue, rgb8.red, rgb8.green, rgb8.blue);

//        prompt("next? ");
    _delay_ms(1000);
  }
}
Example #8
0
/* this happens after cpu_init where exynos resources are set */
static void mainboard_init(device_t dev)
{
	int dp_tries;
	struct s5p_dp_device dp_device = {
		.base = (struct exynos5_dp *)EXYNOS5250_DP1_BASE,
		.video_info = &dp_video_info,
	};
	void *fb_addr = (void *)(get_fb_base_kb() * KiB);

	gpio_init();

	i2c_init(TPS65090_BUS, I2C_0_SPEED, I2C_SLAVE);
	i2c_init(7, I2C_0_SPEED, I2C_SLAVE);

	tmu_init(&exynos5250_tmu_info);

	/* Clock Gating all the unused IP's to save power */
	clock_gate();

	/* Disable USB3.0 PLL to save 250mW of power */
	disable_usb30_pll();

	set_vbe_mode_info_valid(&edid, (uintptr_t)fb_addr);

	lcd_vdd();

	// FIXME: should timeout
	do {
		udelay(50);
	} while (!exynos_dp_hotplug());

	exynos_dp_bridge_setup();
	for (dp_tries = 1; dp_tries <= MAX_DP_TRIES; dp_tries++) {
		exynos_dp_bridge_init();
		if (exynos_dp_hotplug()) {
			exynos_dp_reset();
			continue;
		}

		if (dp_controller_init(&dp_device))
			continue;

		udelay(LCD_T3_DELAY_MS * 1000);

		backlight_vdd();
		backlight_pwm();
		backlight_en();
		/* if we're here, we're successful */
		break;
	}

	if (dp_tries > MAX_DP_TRIES)
		printk(BIOS_ERR, "%s: Failed to set up displayport\n", __func__);

	// Uncomment to get excessive GPIO output:
	// gpio_info();
}

static void mainboard_enable(device_t dev)
{
	dev->ops->init = &mainboard_init;

	/* set up dcache and MMU */
	/* FIXME: this should happen via resource allocator */
	exynos5250_config_l2_cache();
	mmu_init();
	mmu_config_range(0, DRAM_START, DCACHE_OFF);
	mmu_config_range(DRAM_START, DRAM_SIZE, DCACHE_WRITEBACK);
	mmu_config_range(DRAM_END, 4096 - DRAM_END, DCACHE_OFF);
	dcache_invalidate_all();
	dcache_mmu_enable();

	/* this is going to move, but we must have it now and we're
	 * not sure where */
	exception_init();

	const unsigned epll_hz = 192000000;
	const unsigned sample_rate = 48000;
	const unsigned lr_frame_size = 256;
	clock_epll_set_rate(epll_hz);
	clock_select_i2s_clk_source();
	clock_set_i2s_clk_prescaler(epll_hz, sample_rate * lr_frame_size);

	power_enable_xclkout();
}
Example #9
0
int main(){
	//It's inits time!
	initUartDebug();
	DDRG = 0xFF;
	acc_init();
	PORTG = 0xFF;
	adc_init();
	//burn_init();
	du_init();
	DHT_Init();
	//digipot_init();
	//GPS_Init();
	i2c_init();
	OneWireInit();
	//OPT_init();
	radio_init();
	sd_init();
	//soilres_init();
	spi_init();
	TimeServiceInit();
	dtpkt_t pkt;
	/*accpkt_t accpkt;
	accpkt.beacon = 0xACCC;
	accpkt.number = 0;
	accpkt.entries = 0;

	uint8_t dataaval=0;*/

	pkt.beacon = 0xFFFF;
	pkt.number = 0;

	cli();
	start_temperature();

	//isBigLux = false;

	sei();
	_delay_ms(1);
//	uint16_t opt_devid = 0;;
//	OPT_read(0x7F, &opt_devid);
//	GR_DEBUG("OPT DevID = 0x%02x\n", opt_devid);TempTime = TimeServiceGet();
	pkt.humidity = 0;
	pkt.temperature2 = 0;
	while(1){
		PORTG ^= 0xFF;
		/*for(int i = 0;i<1;i++) {
			dataaval = acc_read(accpkt.datax,accpkt.datay,accpkt.dataz);
			TempTime = TimeServiceGet();
			accpkt.time = TempTime.seconds;
			accpkt.time_part = TempTime.subseconds;
			if(dataaval) {
				accpkt.entries = dataaval;
				accpkt.number++;
				du_write(&accpkt, 9);
				du_write(accpkt.datax, dataaval);
				du_write(accpkt.datay, dataaval);
				du_write(accpkt.dataz, dataaval);
				for(int i = 0; i < (sizeof(accpkt)-sizeof(accpkt.cntrl)); i++) {
					accpkt.cntrl += *((uint8_t*)(&accpkt)+i);
				}
				du_write(&accpkt.cntrl, 2);
			}
		}*/
		TempTime = TimeServiceGet();
		if(TempTime.seconds-startOfConv_time.seconds){
			cli();
			pkt.temperature1 = get_temperature();
			sei();
			startOfConv_time = TimeServiceGet();
		}
		cli();
		DHT_Read(&pkt.humidity,&pkt.temperature2);
		sei();
//		if (rc != 0)
//			GR_DEBUG("dht22 error = %d\n", rc);
		pkt.pressure = adc_read(ADC_CHANNEL_PRESSURE);
		pkt.O2 = adc_read(ADC_CHANNEL_O2_SENS);
		pkt.CO2 = adc_read(ADC_CHANNEL_CO2_SENS);
		//OPT_result(&pkt.lum);
		TempTime = TimeServiceGet();
		pkt.time = TempTime.seconds;
		pkt.time_part = TempTime.subseconds;
		pkt.number++;
		for(int i = 0; i < (sizeof(pkt)-sizeof(pkt.cntrl)); i++) {
				pkt.cntrl += *((uint8_t*)(&pkt)+i);
		}
		du_write(&pkt, sizeof(pkt));
		//GR_DEBUG("Number %d, %d sec %d subsec, temp1 %d, temp2 %d, pres %d, hum %d, O2 %d, CO2 %d, lum %d\n", pkt.number, pkt.time, pkt.time_part, pkt.temperature1,pkt.temperature2,pkt.pressure,pkt.humidity,pkt.O2,pkt.CO2,pkt.lum);

		/*dataaval = acc_read(accpkt.datax,accpkt.datay,accpkt.dataz);
		TempTime = TimeServiceGet();
		accpkt.time = TempTime.seconds;
		accpkt.time_part = TempTime.subseconds;
		if(dataaval) {
			accpkt.entries = dataaval;
			accpkt.number++;
			du_write(&accpkt, 9);
			du_write(accpkt.datax, dataaval);
			du_write(accpkt.datay, dataaval);
			du_write(accpkt.dataz, dataaval);
			for(int i = 0; i < (sizeof(accpkt)-sizeof(accpkt.cntrl)); i++) {
				accpkt.cntrl += *((uint8_t*)(&accpkt)+i);
			}
			du_write(&accpkt.cntrl, 2);
		}*/
		if(OnLaunchpad) {
			if(StartPres - adc_read(ADC_CHANNEL_PRESSURE) > 20) {
				OnLaunchpad = 0;
				Started = 1;
			}
		}
		if(Started){
			if(WaitLand){
				int treshhold = 1;
				PresBuf[PresBufi] = pkt.pressure;
				bool onePres = 1;
				for(int i = 1;i<5;i++){
					if((PresBuf[0]-PresBuf[i]>treshhold)||(PresBuf[i]-PresBuf[0])>treshhold) onePres=0;
				}
				if(onePres){
					FRPORT |= (1<<FRSEEDS);
					Started = 0;
					SeedStarted = 1;
					startOfSeeds_time = TimeServiceGet();
				}
			}
			PresBuf[PresBufi] = pkt.pressure;
			if(PresBufi == 4) {
				PresBufi = 0;
				WaitLand = 1;
			}
			else PresBufi++;
		}
		if(SeedStarted){
			if(startOfSeeds_time.seconds - TimeServiceGet().seconds) {
				FRPORT &= ~(1<<FRSEEDS);
				SeedStarted = 0;
				pkt.seeds = 1;
			}
		}
		_delay_ms(900);
	}
}
Example #10
0
void main(void)
{
    /* Initialization functions called below */
    ecan_init();
    ConfigureOscillator();
     /* Enable Interrupts */
    RCONbits.IPEN   = 0;    // Enable interrupt priority
    INTCONbits.GIE  = 1;    // Enable interrupts
    INTCONbits.PEIE = 1;    // Enable peripheral interrupts.
    
    PIE3bits.RXB0IE = 1;    // Enable CAN receive buffer 0 interrupt
    PIE3bits.RXB1IE = 1;    // Enable CAN receive buffer 1 interrupt
    
    i2c_init();
    
    //Configure accelerometer CONTRL_REG1_XM
    //char slaveAddr0 = 0x1E;
    //char destAddr0 = 0x20;
    //char sendData0 = 0b01100111; //== 0x67
    //i2c_writeToReg(slaveAddr0, destAddr0, sendData0);

    //CONTROL_REG5_XM
    //slaveAddr0 = 0x1E;
    //destAddr0 = 0x24;
    //sendData0 = 0b11110000;
    //i2c_writeToReg(slaveAddr0, destAddr0, sendData0);
    
    i2c_writeToReg(0x1E, 0x1F, 0x00);
    i2c_writeToReg(0x1E, 0x20, 0x57);
    i2c_writeToReg(0x1E, 0x21, 0x00);
    i2c_writeToReg(0x1E, 0x22, 0x00);
    i2c_writeToReg(0x1E, 0x23, 0x00);
    i2c_writeToReg(0x1E, 0x24, 0x14);
    i2c_writeToReg(0x1E, 0x25, 0x00);
    i2c_writeToReg(0x1E, 0x26, 0x00);
    
    i2c_writeToReg(0x6A, 0x20, 0x0F);
    i2c_writeToReg(0x6A, 0x21, 0x00);
    i2c_writeToReg(0x6A, 0x23, 0x00);
    i2c_writeToReg(0x6A, 0x24, 0x00);
    
     /* While doing recurring functions */
    while(1)
    {
        /*
        short ac1 = i2c_get_data(0x77, 0xAA);
        short ac2 = i2c_get_data(0x77, 0xAC);
        short ac3 = i2c_get_data(0x77, 0xAE);
        unsigned short ac4 = i2c_get_udata(0x77, 0xB0);
        unsigned short ac5 = i2c_get_udata(0x77, 0xB2);
        unsigned short ac6 = i2c_get_udata(0x77, 0xB4);
        short b1 = i2c_get_data(0x77, 0xB6);
        short b2 = i2c_get_data(0x77, 0xB8);
        short mb = i2c_get_data(0x77, 0xBA);
        short mc = i2c_get_data(0x77, 0xBC);
        short md = i2c_get_data(0x77, 0xBF);
        
        i2c_write_raw(0xF4, 0x2E);
        __delay_ms(5);
        long ut = i2c_get_data(0x77, 0xF6);
        
        long x1 = ((long) ut - (unsigned long) ac6) * (unsigned long) ac5 / 32768;
        long x2 = (long) mc * 2048 / (x1 + (long) md);
        long b5 = x1 + x2;
        long temp = (b5 + 8) / 16;
        
        Message tempPack;
        tempPack.sid = 0x101;
        tempPack.len = 4;
		tempPack.data[3] = temp >> 24;
        tempPack.data[2] = temp >> 16;
        tempPack.data[1] = temp >> 8;
        tempPack.data[0] = temp;
        ecan_send(&tempPack);
        __delay_ms(50);
        */
        
        /*short xGyroRaw = i2c_get_byte(0x6A, 0x28);
        xGyroRaw += i2c_get_byte(0x6A,0x29) << 8;
        
        short yGyroRaw = i2c_get_byte(0x6A, 0x2A);
        yGyroRaw += i2c_get_byte(0x6A,0x2B) << 8;
        
        short zGyroRaw = i2c_get_byte(0x6A, 0x2C);
        zGyroRaw += i2c_get_byte(0x6A,0x2D) << 8;
        
        Message gyroPack;
        gyroPack.sid = 0x000;
        gyroPack.len = 6;
        gyroPack.data[5] = xGyroRaw >> 8;
        gyroPack.data[4] = xGyroRaw;
        gyroPack.data[3] = yGyroRaw >> 8;
        gyroPack.data[2] = yGyroRaw;
        gyroPack.data[1] = zGyroRaw >> 8;
        gyroPack.data[0] = zGyroRaw;
        
        ecan_send(&gyroPack); */
        
        short xAccelRaw = i2c_get_byte(0x1E, 0x28);
        xAccelRaw += i2c_get_byte(0x1E,0x29) << 8;
        
        short yAccelRaw = i2c_get_byte(0x1E, 0x2A);
        yAccelRaw += i2c_get_byte(0x1E,0x2B) << 8;
        
        short zAccelRaw = i2c_get_byte(0x1E, 0x2C);
        zAccelRaw += i2c_get_byte(0x1E,0x2D) << 8;
        
        Message accelPack;
        accelPack.sid = 0x000;
        accelPack.len = 6;
        accelPack.data[5] = xAccelRaw >> 8;
        accelPack.data[4] = xAccelRaw;
        accelPack.data[3] = yAccelRaw >> 8;
        accelPack.data[2] = yAccelRaw;
        accelPack.data[1] = zAccelRaw >> 8;
        accelPack.data[0] = zAccelRaw;
        
        ecan_send(&accelPack);
        
    }
}
Example #11
0
// main entry point, go ahead, have fun!
int main(void) {
	// hold the power button for 3 seconds or shutdown
	//_delay_ms(STARTUP_DELAY);
	// keep power pin High unless we want to shutdown
	powerControl = 1;

	// disable JTAG so we can use PF4,PF5,PF6,PF7 for ADC and GPIO
	MCUCSR |=(1<<JTD);MCUCSR |=(1<<JTD); // two times!!

	// init time RTC
	time.init(callback_timeSecond, callback_timeMinute);
	time.startInterval();

	// microcontroller features
	adc.init();	// needed by gp2y10 and more
	spi_init();	// needed by ILI9341
	i2c_init(); // needed by bmp180 and mics-vz-89t
	uart1.init(1, 9600, 1); // needed by esp8266 . Interrupts are hard on parsing, polling would be easier but blocking
	uart0.init(0, 9600, 1);
	// CONFIGURE INTERRUPT INT4  to count pulses from Geiger Counter
	EICRB |= (1<<ISC00) | (1<<ISC01); // Configure INT4 to trigger on RISING EDGE
	EIMSK |= (1<<INT4); // Configure INT4 to fire interrupts

	// CREATE Timer T1 PWM to drive inverter for regulated Geiger tube voltage
	inverter.initPWM();

	// init display
	lcd.init();
	lcd.setRotation(ILI9341::ROT0);
	//lcd.drawClear(BLACK);
	backlight(true);

	// init sensors
	bmp180.init();
	dust.init(&dustFlash, &adc, PF1);

	beep();
	// start UI

	// enter main menu
	// ## touchscreen calibration code
	/*while (1) {
		uint16_t x = 0, y = 0, z = 0;
				if (touch.read(&x, &y , &z)) {
					lcd.drawPixel(x,y, 2, RED);
				}
		lcd.drawStringF(0,0,2,WHITE, BLACK, "%4u %4u %4u", touch.readRawX(), touch.readRawY(), touch.readRawPressure());
	}*/

	// draw GUI first page with self check
	if (!gui.drawPage(PAGE_INIT))
		shutdown();


	_delay_ms(1000);
	gui.drawPage(PAGE_MAIN);
	// ## main code loop
	while (1) {
		// ## beep
		if (cmdBeep && !cmdAlarm && !isMuted) {
			beep();
			cmdBeep = false;
		}
		// ## read sensors
		// read inverter voltage, via 10M/47K resistive divider, connected to pin ADC2
		data.geiger_voltage = readTubeVoltage();
		inverter.adjustDutyCycle(data.geiger_voltage); // do nothing on failure, we can't reset
		// read battery
		data.battery_voltage = readBatVoltage();

		// turn backlight off when timeout is reached
		if (secTimeout > BACKLIGHT_TIMEOUT) {
			backlight(false);
			secTimeout = 0;
		}
		// ## draw titlebar and refresh data display
		if (cmdRefreshText) {
			// sensor BMP180
			bmp180.readAll(&data.bmp180_temp, &data.bmp180_pressure, &data.bmp180_altitude);
			dust.readDust(&data.gp2y10_dust);
			// sensor MICS-VZ-89T
			uint8_t reactivity = 0;
			// repeat until successful read with timeout?
			//int timeout = 10;
			//while (!vz89.read(&data.vz89_co2, &reactivity, &data.vz89_voc) && timeout) { _delay_ms(1500); timeout--; }
			vz89.read(&data.vz89_co2, &reactivity, &data.vz89_voc);
			// geiger readings
			//float dose = aux_CPM2uSVh((uint8_t)DEV_RAD_DETECTOR, geigerCPM);
			data.geiger_cpm = geigerCPM;

			data.time_hour = time.getHour();
			data.time_minute = time.getMin();
			data.time_second = time.getSec();

			data.setLimits(); // must be changed to proper OOP set/get for all fields
			gui.updateValues();

			// ## alarm condition
			if (geigerCPM >= GEIGER_CPM_ALARM) {
				// threshold to sound alarm reached
				cmdAlarm = ALARM_RADIATION;
			} else if (cmdAlarm) {
				// alarm should be turned off
				cmdAlarm = 0;
				speaker = 0;
			}

			cmdRefreshText = false;

		}
		// ## every minute we can dispatch data over serial or over WLAN to uradmonitor
		if (cmdSend) {
			char tmp[200];
			sprintf(tmp,"{\"data\":{ \"id\":\"%08lX\","
					"\"type\":\"%X\",\"detector\":\"%s\","
					"\"cpm\":%lu,\"temperature\":%.2f,\"uptime\": %lu,"
					"\"pressure\":%lu,\"dust\":%.2f,\"co2\":%.2f,\"voc\":%.2f,"
					"\"battery\":%.2f,\"tube\":%u}}",
					deviceID, DEV_MODEL, aux_detectorName(DEV_RAD_DETECTOR), geigerCPM, data.bmp180_temp, time.getTotalSec(),
					data.bmp180_pressure, data.gp2y10_dust,data.vz89_co2, data.vz89_voc, data.battery_voltage, data.geiger_voltage);
			data.serial_sent += strlen(tmp);
			uart0.send(tmp);

			// internet code here

			sprintf(tmp,"id=%08lX&ts=%ld&inv=%d&ind=%d&s1t=%2.2f&cpm=%ld&voc=%.2f&co2=%.2f",
								deviceID,
								time.getTotalSec(),
								data.geiger_voltage,
								data.geiger_duty,
								data.bmp180_temp,
								geigerCPM,
								data.vz89_voc, data.vz89_co2);

						wifi.sendData(tmp);
			cmdSend = false;
		}

		// ## act on the gui elements
		// read a new touch event only if we are done with previous: useful for handling confirmation "modal" "dialogs"
		if (uiResult == 0) {
			uiResult = gui.readTouchEvent();
			// reset backlight timeout on valid touch
			if (uiResult > 0) {
				secTimeout = 0;
				backlight(true);
				// if screen is pressed while alarm is on, stop alarm
				if (cmdAlarm) {
					cmdAlarm = false;
					speaker = 0;
				}
			}
		}
		// handle special cases: click on wlan AP buttons
		if (uiResult >= ID_BUTTON_WLAN_START && uiResult < ID_BUTTON_WLAN_STOP) {
			uint8_t ap_index = uiResult - ID_BUTTON_WLAN_START;
			// connect and return to main screen
			wifi.connectWiFi(data.freeAPList[ap_index], "");
			uiResult = 0;
			gui.drawPage(PAGE_MAIN);
		}
		// handle regular buttons
		switch (uiResult) {
			case  ID_BUTTON_SHUTDOWN: {
				uint16_t result = gui.showYesNoPopup("Are you sure?");
				if (result == ID_YES)
					shutdown();
				else if (result == ID_NO) {
					uiResult = 0;
					gui.drawPage(PAGE_MAIN);
				}
			} break;
			case ID_BUTTON_MEASURE: {
				uiResult = 0;
				gui.drawPage(PAGE_MEASURE);
			} break;
			case ID_BUTTON_MONITOR: {
				uiResult = 0;
				gui.drawPage(PAGE_MONITOR);
			} break;
			case ID_BUTTON_SETTINGS: {
				uiResult = 0;
				gui.drawPage(PAGE_SETTINGS);
			} break;
			case ID_BUTTON_BACK: {
				uiResult = 0;
				gui.drawPage(PAGE_MAIN);
			} break;
			case ID_BUTTON_MUTE: {
				isMuted = !isMuted;
				if (!isMuted) beep(); // test beep that sound is on
				uiResult = 0;
			} break;
			case ID_BUTTON_CALIBRATE: {
				uiResult = 0;
				gui.drawPage(PAGE_CALIBRATE);
			} break;
			case ID_BUTTON_WLAN: {
				// request list of WLAN APs
				data.freeAPCount = 0;
				//wifi.setMode();
				wifi.listAP();
				uiResult = 0;
				gui.drawPage(PAGE_WLAN);
			}
			break;
			// other commands that don't require a popup so we consume asap
			default:
				uiResult = 0;
		}

		//uint16_t x, y,z;
				//gui.getLastTouch(&x, &y, &z);
				//lcd.drawStringF(0,288, 2, RED, BLACK,"%u %d,%d   ", uiResult, x,y);

	}
}
void* main(void)
{
    int i;
    int btn;
    int num_partitions;
    int crc32;
    char sector[512];
    struct partinfo pinfo;

    system_init();
    kernel_init();
    lcd_init();
    font_init();
    button_init();
    i2c_init();
    backlight_hw_on();
    
    lcd_set_foreground(LCD_WHITE);
    lcd_set_background(LCD_BLACK);
    lcd_clear_display();

    btn = button_read_device();
    verbose = true;

    lcd_setfont(FONT_SYSFIXED);

    printf("Rockbox e200R installer");
    printf("Version: %s", rbversion);
    printf(MODEL_NAME);
    printf("");

    i=storage_init();
    filesystem_init();
    num_partitions = disk_mount_all();

    if (num_partitions<=0)
    {
        error(EDISK, num_partitions, true);
    }

    disk_partinfo(1, &pinfo);

#if 0 /* not needed in release builds */
    printf("--- Partition info ---");
    printf("start: %x", pinfo.start);
    printf("size: %x", pinfo.size);
    printf("type: %x", pinfo.type);
    printf("reading: %x", (START_SECTOR_OF_ROM + ROMSECTOR_TO_HACK)*512);
#endif

    storage_read_sectors(pinfo.start + START_SECTOR_OF_ROM + ROMSECTOR_TO_HACK,
                         1 , sector);
    crc32 = chksum_crc32 (sector, 512);

#if 0 /* not needed in release builds */
    printf("--- Hack Status ---");
    printf("Sector checksum: %x", crc32);
#endif

    if (crc32 == PATCHED_CRC32)
    {
        /* Bootloader already patched */
        printf("Already unlocked");
        printf("Proceed to Step 2");
    } else if ((crc32 == KNOWN_CRC32) && 
                !memcmp(&sector[HACK_OFFSET], knownBytes, 
                sizeof(knownBytes)/sizeof(*knownBytes)))
    {
        /* E200R bootloader detected - patch it */
        memcpy(&sector[HACK_OFFSET], changedBytes,
                sizeof(changedBytes)/sizeof(*changedBytes));
        storage_write_sectors(
                        pinfo.start + START_SECTOR_OF_ROM + ROMSECTOR_TO_HACK,
                        1 , sector);
        printf("Firmware unlocked");
        printf("Proceed to Step 2");
    } else if (is_e200(crc32))
    {
        printf("Vanilla E200 detected!");
        printf("Please install using");
        printf("Sansapatcher");
    }
    else
    {
        printf("Unknown bootloader");
        printf("Rockbox installer cannot");
        printf("continue");
    }

    /* Turn button lights off */
    GPIOG_OUTPUT_VAL &=~0x80;

    printf("");

    if (button_hold())
        printf("Release Hold and");

    printf("Press any key to shutdown");

    while(button_read_device() == BUTTON_NONE);

    power_off();

    return NULL;
}
Example #13
0
int board_late_init(void) {
#if defined(ENCLUSTRA_EEPROM_ADDR_TAB) && defined(ENCLUSTRA_EEPROM_HWMAC_REG)
	u8 chip_addr_tab[] = ENCLUSTRA_EEPROM_ADDR_TAB;
#if defined(ENCLUSTRA_EEPROM_ADDR_WAKEY_TAB)
	u8 chip_addr_wakey_tab[] = ENCLUSTRA_EEPROM_ADDR_WAKEY_TAB;
	u8 wake_cmd = 0x0;
	int j;
#endif
	int i, ret;
	u8 hwaddr[6];
	u32 hwaddr_h;
	char hwaddr_str[16];
	bool hwaddr_set;

	hwaddr_set = false;

	if (getenv("ethaddr") == NULL) {
		/* Init i2c */
		i2c_init(0, 0);
		i2c_set_bus_num(0);

		for (i = 0; i < ARRAY_SIZE(chip_addr_tab); i++) {
			/* Probe the chip */
			if (i2c_probe(chip_addr_tab[i]) != 0)
				continue;

#if defined(ENCLUSTRA_EEPROM_ADDR_WAKEY_TAB)
			for (j = 0; j < ARRAY_SIZE(chip_addr_wakey_tab); j++) {
				if(chip_addr_tab[i] != chip_addr_wakey_tab[j])
					continue;

				/* Wake the chip by writing 0x0 to 0x0 reg */
				i2c_write(chip_addr_tab[i],
					  ENCLUSTRA_EEPROM_HWMAC_REG,
					  1,
					  &wake_cmd,
					  1);
				break;
			}
#endif
			/* Attempt to read the mac address */
			ret = i2c_read(chip_addr_tab[i],
				       ENCLUSTRA_EEPROM_HWMAC_REG,
				       1,
				       hwaddr,
				       6);

			/* Do not continue if read failed */
			if (ret)
				continue;

			/* Check if the value is a valid mac registered for
			 * Enclustra GmbH */
			hwaddr_h = hwaddr[0] | hwaddr[1] << 8 | hwaddr[2] << 16;
			if ((hwaddr_h & 0xFFFFFF) != ENCLUSTRA_MAC)
				continue;

			/* Format the address using a string */
			sprintf(hwaddr_str,
				"%02X:%02X:%02X:%02X:%02X:%02X",
				hwaddr[0],
				hwaddr[1],
				hwaddr[2],
				hwaddr[3],
				hwaddr[4],
				hwaddr[5]);

			/* Set the actual env variable */
			setenv("ethaddr", hwaddr_str);
			hwaddr_set = true;
			break;
		}
		if (!hwaddr_set)
			setenv("ethaddr", ENCLUSTRA_ETHADDR_DEFAULT);
	}
#else
	if (getenv("ethaddr") == NULL)
		setenv("ethaddr", ENCLUSTRA_ETHADDR_DEFAULT);
#endif
  return 0;
}
Example #14
0
static void kbd_init (void)
{
    uchar kbd_data[KEYBD_DATALEN];
    uchar tmp_data[KEYBD_DATALEN];
    uchar val, errcd;
    int i;

    i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);

    gd->kbd_status = 0;

    /* Forced by PIC. Delays <= 175us loose */
    udelay(1000);

    /* Read initial keyboard error code */
    val = KEYBD_CMD_READ_STATUS;
    i2c_write (kbd_addr, 0, 0, &val, 1);
    i2c_read (kbd_addr, 0, 0, &errcd, 1);
    /* clear unused bits */
    errcd &= KEYBD_STATUS_MASK;
    /* clear "irrelevant" bits. Recommended by Martin Rajek, LWN */
    errcd &= ~(KEYBD_STATUS_H_RESET|KEYBD_STATUS_BROWNOUT);
    if (errcd) {
        gd->kbd_status |= errcd << 8;
    }
    /* Reset error code and verify */
    val = KEYBD_CMD_RESET_ERRORS;
    i2c_write (kbd_addr, 0, 0, &val, 1);
    udelay(1000);	/* delay NEEDED by keyboard PIC !!! */

    val = KEYBD_CMD_READ_STATUS;
    i2c_write (kbd_addr, 0, 0, &val, 1);
    i2c_read (kbd_addr, 0, 0, &val, 1);

    val &= KEYBD_STATUS_MASK;	/* clear unused bits */
    if (val) {			/* permanent error, report it */
        gd->kbd_status |= val;
        return;
    }

    /*
     * Read current keyboard state.
     *
     * After the error reset it may take some time before the
     * keyboard PIC picks up a valid keyboard scan - the total
     * scan time is approx. 1.6 ms (information by Martin Rajek,
     * 28 Sep 2002). We read a couple of times for the keyboard
     * to stabilize, using a big enough delay.
     * 10 times should be enough. If the data is still changing,
     * we use what we get :-(
     */

    memset (tmp_data, 0xFF, KEYBD_DATALEN);	/* impossible value */
    for (i=0; i<10; ++i) {
        val = KEYBD_CMD_READ_KEYS;
        i2c_write (kbd_addr, 0, 0, &val, 1);
        i2c_read (kbd_addr, 0, 0, kbd_data, KEYBD_DATALEN);

        if (memcmp(kbd_data, tmp_data, KEYBD_DATALEN) == 0) {
            /* consistent state, done */
            break;
        }
        /* remeber last state, delay, and retry */
        memcpy (tmp_data, kbd_data, KEYBD_DATALEN);
        udelay (5000);
    }
}
Example #15
0
void lm75_init(void)
{
	i2c_init();
}
Example #16
0
int32_t main(int argc, char **argv)
{
    int32_t i = 0;
    pthread_t	vPollcmd;
    pthread_t	vPipecmd;

    _gCurrentState=STATE_INIT;

    for(i = 0; i < argc; i++)
    {
        if(0 == memcmp("-d", argv[i], 2))
        {
            if(i+1 < argc)
            {
                _gDebugFlagMask = strtol(argv[i+1], NULL, 0);
                i++;
            }
            else
            {
                _gDebugFlagMask=0xFF;
            }
            printf("enable debug, mask = 0x%04X\n", _gDebugFlagMask);
            _gDebugFlag=1;
        }
        else if(0 == memcmp("-t", argv[i], 2))
        {
            _gTestMode=1;
        }
    }

    _gQuitHandler = signal( SIGQUIT, exit_handler );
    _gKillHandler = signal( SIGKILL, exit_handler );
    _gTermHandler = signal( SIGTERM, exit_handler );
    _gTermHandler = signal( SIGINT, exit_handler );

#if(_DEBUG_ == 1 && _DEBUG_TO_FILE_ == 1)
    if(gDebugToFile == 1)
        util_debug_file_init();
#endif
    pipe_init();
    if(0 != i2c_init())
        return -1;
#ifdef STATUS_LED
    LED_init();
#endif	
#ifdef PUBLIC_GPIO
    GPIO_init();
#endif
    Flags_init();
    sysinfo_update_init();
    menu_init();
    signal( SIG_PROC_CMD, cmd_queue_handler );
    signal( SIG_PROC_MENU, menu_queue_handler );
    sigemptyset(&_gSigMask);
    
    pthread_create(&vPollcmd, NULL, timer_main, NULL);
    pthread_create(&vPipecmd, NULL, pipecmd, NULL);
    pthread_join( vPollcmd, NULL);
    pthread_join( vPipecmd, NULL);

    return (0);
}
Example #17
0
void init(void)
{
	//***********************************************************
	// I/O setup
	//***********************************************************
	// Set port directions
	// KK2.0 and KK2.1 are different
#ifdef KK21
	DDRA		= 0x30;		// Port A
	DDRC		= 0xFC;		// Port C
#else
	DDRA		= 0x00;		// Port A
	DDRC		= 0xFF;		// Port C
#endif
	DDRB		= 0x0A;		// Port B
	DDRD		= 0xF2;		// Port D

	// Hold all PWM outputs low to stop glitches
	// M5 and M6 are on PortA for KK2.1
	MOTORS		= 0;
	M5			= 0;
	M6			= 0;		

	// Preset I/O pins
	LED1 		= 0;		// LED1 off
	LVA 		= 0; 		// LVA alarm OFF
	LCD_SCL		= 1;		// GLCD clock high

	// Set/clear pull-ups (1 = set, 0 = clear)
	PINB		= 0xF5;		// Set PB pull-ups
	PIND		= 0x0C;		// Set PD pull-ups (Don't pull up RX yet)

	//***********************************************************
	// Spektrum receiver binding
	//***********************************************************

	_delay_ms(63);				// Pause while satellite wakes up	
								// and pull-ups have time to rise.
								// Tweak until bind pulses about 68ms after power-up

	// Bind as master if ONLY button 4 pressed
	if ((PINB & 0xf0) == 0xE0)
	{
		DDRD		= 0xF3;		// Switch PD0 to output
		bind_master();
	}

	DDRD		= 0xF2;			// Reset Port D directions

	// Set/clear pull-ups (1 = set, 0 = clear)
	PIND		= 0x0D;			// Set PD pull-ups (now pull up RX as well)

	//***********************************************************
	// Timers
	//***********************************************************
	// Timer0 (8bit) - run @ 20MHz (50ns) - max 12.8us
	// Fast timer for small, precise interval timing
	TCCR0A = 0;								// Normal operation
	TCCR0B = (1 << CS00);					// Clk / 1 = 20MHz = 50ns
	TIMSK0 = 0; 							// No interrupts

	// Timer1 (16bit) - run @ 2.5MHz (400ns) - max 26.2ms
	// Used to measure Rx Signals & control ESC/servo output rate
	TCCR1A = 0;
	TCCR1B = (1 << CS11);					// Clk/8 = 2.5MHz

	// Timer2 8bit - run @ 20MHz / 1024 = 19.531kHz or 51.2us - max 13.1ms
	// Used to time arm/disarm intervals
	TCCR2A = 0;	
	TCCR2B = 0x07;							// Clk/1024 = 19.531kHz
	TIMSK2 = 0;
	TIFR2 = 0;
	TCNT2 = 0;								// Reset counter

	//***********************************************************
	// Interrupts and pin function setup
	//***********************************************************

	// Pin change interrupt enables PCINT1, PCINT2 and PCINT3 (Throttle, AUX and CPPM input)
	PCICR  = 0x0A;							// PCINT8  to PCINT15 (PCINT1 group - AUX)
											// PCINT24 to PCINT31 (PCINT3 group - THR)
	PCIFR  = 0x0F;							// Clear PCIF0 interrupt flag 
											// Clear PCIF1 interrupt flag 
											// Clear PCIF2 interrupt flag 
											// Clear PCIF3 interrupt flag 

	// External interrupts INT0 (Elevator) and INT1 (Aileron) and INT2 (Rudder)
	EICRA = 0x15;							// Any change INT0
											// Any change INT1
											// Any change INT2
	EIFR  = 0x07; 							// Clear INT0 interrupt flag (Elevator)
											// Clear INT1 interrupt flag (Aileron)
											// Clear INT2 interrupt flag (Rudder/CPPM)

	//***********************************************************
	// i2c init for KK2.1
	//***********************************************************	

#ifdef KK21
	i2c_init();
	init_i2c_gyros();
	init_i2c_accs();
#endif

	//***********************************************************
	// Start up
	//***********************************************************

	// Preset important flags
	Interrupted  = false;						
	Main_flags |= (1 << FirstTimeIMU);
	Main_flags |= (1 << FirstTimeFlightMode);

	// Initialise the GLCD
	st7565_init();
	st7565_command(CMD_DISPLAY_ON);
	st7565_command(CMD_SET_ALLPTS_NORMAL);
	st7565_set_brightness(0x26);
	st7565_command(CMD_SET_COM_REVERSE); 	// For logo

	// Make sure the LCD is blank
	clear_screen();

	// This delay prevents the GLCD flashing up a ghost image of old data
	_delay_ms(300);	

	// Reload default eeprom settings if middle two buttons are pressed (or all, for older users)
	if (((PINB & 0xf0) == 0x90) || ((PINB & 0xf0) == 0x00))
	{
		// Display reset message
		st7565_command(CMD_SET_COM_NORMAL); 	// For text (not for logo)
		clear_buffer(buffer);
		LCD_Display_Text(1,(prog_uchar*)Verdana14,40,25);
		write_buffer(buffer,1);
		clear_buffer(buffer);

		Set_EEPROM_Default_Config();
		Save_Config_to_EEPROM();
	}
	// Load "Config" global data structure
	else
	{
		Initial_EEPROM_Config_Load();
	}		

	// Now set contrast to the previously saved value
	st7565_set_brightness((uint8_t)Config.Contrast);
						
#ifdef KK21
	// Write logo from buffer
	write_buffer(buffer,0);
	_delay_ms(500);
#endif

#ifndef KK21
	// Display "Hold steady" message for KK2.0
	st7565_command(CMD_SET_COM_NORMAL); 	// For text (not for logo)
	clear_buffer(buffer);
	LCD_Display_Text(2,(prog_uchar*)Verdana14,18,25);
	write_buffer(buffer,1);
	clear_buffer(buffer);
#endif
		
	// Do startup tasks
	UpdateLimits();							// Update travel limts	
	UpdateIMUvalues();						// Update IMU factors
	Init_ADC();
	init_int();								// Intialise interrupts based on RC input mode

	// Initialise UART
	init_uart();

	// Initial gyro calibration
	CalibrateGyrosSlow();

	// Check to see that gyros are stable
	ReadGyros();

	if ((gyroADC[ROLL] > GYROS_STABLE) || (gyroADC[ROLL] < -GYROS_STABLE) ||
	 	(gyroADC[PITCH] > GYROS_STABLE) || (gyroADC[PITCH] < -GYROS_STABLE) ||
		(gyroADC[YAW] > GYROS_STABLE) || (gyroADC[YAW] < -GYROS_STABLE))
	{
		General_error |= (1 << SENSOR_ERROR); 	// Set sensor error bit
	}

	// Check to see that throttle is low if in serial mode.
	// Don't bother if in CamStab mode
	_delay_ms(100);
	if (
		(
		 (Config.RxMode == CPPM_MODE) ||
		 (Config.RxMode == XTREME) ||
		 (Config.RxMode == SBUS) ||
		 (Config.RxMode == SPEKTRUM)
		)
		&& (Config.CamStab == OFF)
	   )
	{
		RxGetChannels();
		if (RCinputs[THROTTLE] > -900)
		{
			General_error |= (1 << THROTTLE_HIGH); 	// Set throttle high error bit
		}
	}

	// Flash LED
	LED1 = 1;
	_delay_ms(150);
	LED1 = 0;

	// Beep that all sensors have been handled
	menu_beep(1);

	// Set text display mode back to normal
	st7565_command(CMD_SET_COM_NORMAL); 	// For text (not for logo)

} // init()
Example #18
0
/* this happens after cpu_init where exynos resources are set */
static void mainboard_init(device_t dev)
{
	int dp_tries;
	struct s5p_dp_device dp_device = {
		.base = exynos_dp1,
		.video_info = &dp_video_info,
	};
	void *fb_addr = (void *)(get_fb_base_kb() * KiB);

	prepare_usb();
	gpio_init();
	setup_storage();

	i2c_init(TPS65090_BUS, I2C_0_SPEED, I2C_SLAVE);
	i2c_init(7, I2C_0_SPEED, I2C_SLAVE);

	tmu_init(&exynos5250_tmu_info);

	/* Clock Gating all the unused IP's to save power */
	clock_gate();

	/* Disable USB3.0 PLL to save 250mW of power */
	disable_usb30_pll();

	sdmmc_vdd();

	set_vbe_mode_info_valid(&edid, (uintptr_t)fb_addr);

	lcd_vdd();

	// FIXME: should timeout
	do {
		udelay(50);
	} while (!exynos_dp_hotplug());

	exynos_dp_bridge_setup();
	for (dp_tries = 1; dp_tries <= MAX_DP_TRIES; dp_tries++) {
		exynos_dp_bridge_init();
		if (exynos_dp_hotplug()) {
			exynos_dp_reset();
			continue;
		}

		if (dp_controller_init(&dp_device))
			continue;

		udelay(LCD_T3_DELAY_MS * 1000);

		backlight_vdd();
		backlight_pwm();
		backlight_en();
		/* if we're here, we're successful */
		break;
	}

	if (dp_tries > MAX_DP_TRIES)
		printk(BIOS_ERR, "%s: Failed to set up displayport\n", __func__);

	setup_usb();

	// Uncomment to get excessive GPIO output:
	// gpio_info();
}
/**
* @brief Initialize the whole system
*
* All functions that need to be called before the first mainloop iteration
* should be placed here.
*/
void main_init_generic(void)
{

	// Reset to safe values
	global_data_reset();

	// Load default eeprom parameters as fallback
	global_data_reset_param_defaults();

	// LOWLEVEL INIT, ONLY VERY BASIC SYSTEM FUNCTIONS
	hw_init();
	enableIRQ();
	led_init();
	led_on(LED_GREEN);
	buzzer_init();
	sys_time_init();
	sys_time_periodic_init();
	sys_time_clock_init();
	ppm_init();
	pwm_init();

	// Lowlevel periphel support init
	adc_init();
	// FIXME SDCARD
//	MMC_IO_Init();
	spi_init();
	i2c_init();

	// Sensor init
	sensors_init();
	debug_message_buffer("Sensor initialized");

	// Shutter init
	shutter_init();
	shutter_control(0);

	// Debug output init
	debug_message_init();
	debug_message_buffer("Text message buffer initialized");

	// MEDIUM LEVEL INIT, INITIALIZE I2C, EEPROM, WAIT FOR MOTOR CONTROLLERS
	// Try to reach the EEPROM
	eeprom_check_start();

	// WAIT FOR 2 SECONDS FOR THE USER TO NOT TOUCH THE UNIT
	while (sys_time_clock_get_time_usec() < 2000000)
	{
	}

	// Do the auto-gyro calibration for 1 second
	// Get current temperature
	led_on(LED_RED);
	gyro_init();

//	uint8_t timeout = 3;
//	// Check for SD card
//	while (sys_time_clock_get_time_usec() < 2000000)
//	{
//		while (GetDriveInformation() != F_OK && timeout--)
//		  {
//		   debug_message_buffer("MMC/SD-Card not found ! retrying..");
//		  }
//	}
//
//	if (GetDriveInformation() == F_OK)
//	{
//		debug_message_buffer("MMC/SD-Card SUCCESS: FOUND");
//	}
//	else
//	{
//		debug_message_buffer("MMC/SD-Card FAILURE: NOT FOUND");
//	}
	//FIXME redo init because of SD driver decreasing speed
	//spi_init();
	led_off(LED_RED);

	// Stop trying to reach the EEPROM - if it has not been found by now, assume
	// there is no EEPROM mounted
	if (eeprom_check_ok())
	{
		param_read_all();
		debug_message_buffer("EEPROM detected - reading parameters from EEPROM");

		for (int i = 0; i < ONBOARD_PARAM_COUNT * 2 + 20; i++)
		{
			param_handler();
			//sleep 1 ms
			sys_time_wait(1000);
		}
	}
	else
	{
		debug_message_buffer("NO EEPROM - reading onboard parameters from FLASH");
	}

	// Set mavlink system
	mavlink_system.compid = MAV_COMP_ID_IMU;
	mavlink_system.sysid = global_data.param[PARAM_SYSTEM_ID];

	//Magnet sensor
	hmc5843_init();
	acc_init();

	// Comm parameter init
	mavlink_system.sysid = global_data.param[PARAM_SYSTEM_ID]; // System ID, 1-255
	mavlink_system.compid = global_data.param[PARAM_COMPONENT_ID]; // Component/Subsystem ID, 1-255

	// Comm init has to be
	// AFTER PARAM INIT
	comm_init(MAVLINK_COMM_0);
	comm_init(MAVLINK_COMM_1);

	// UART initialized, now initialize COMM peripherals
	communication_init();
	gps_init();

	us_run_init();

	servos_init();

	//position_kalman3_init();

	// Calibration starts (this can take a few seconds)
	//	led_on(LED_GREEN);
	//	led_on(LED_RED);

	// Read out first time battery
	global_data.battery_voltage = battery_get_value();

	global_data.state.mav_mode = MAV_MODE_PREFLIGHT;
	global_data.state.status = MAV_STATE_CALIBRATING;

	send_system_state();

	float_vect3 init_state_accel;
	init_state_accel.x = 0.0f;
	init_state_accel.y = 0.0f;
	init_state_accel.z = -1000.0f;
	float_vect3 init_state_magnet;
	init_state_magnet.x = 1.0f;
	init_state_magnet.y = 0.0f;
	init_state_magnet.z = 0.0f;


	//auto_calibration();


	attitude_observer_init(init_state_accel, init_state_magnet);

	debug_message_buffer("Attitude Filter initialized");
	led_on(LED_RED);

	send_system_state();

	debug_message_buffer("System is initialized");

	// Calibration stopped
	led_off(LED_RED);

	global_data.state.mav_mode = MAV_MODE_FLAG_MANUAL_INPUT_ENABLED;
	global_data.state.status = MAV_STATE_STANDBY;

	send_system_state();

	debug_message_buffer("Checking if remote control is switched on:");
	// Initialize remote control status
	remote_control();
	remote_control();
	if (radio_control_status() == RADIO_CONTROL_ON && global_data.state.remote_ok)
	{
		global_data.state.mav_mode = MAV_MODE_FLAG_MANUAL_INPUT_ENABLED | MAV_MODE_FLAG_TEST_ENABLED;
		debug_message_buffer("RESULT: remote control switched ON");
		debug_message_buffer("Now in MAV_MODE_TEST2 position hold tobi_laurens");
		led_on(LED_GREEN);
	}
	else
	{
		global_data.state.mav_mode = MAV_MODE_FLAG_MANUAL_INPUT_ENABLED;
		debug_message_buffer("RESULT: remote control switched OFF");
		led_off(LED_GREEN);
	}
}
Example #20
0
void platform_init(void)
{
#if defined(TARGET_M805_892X_EVM)
	int ac_charger_detect_flag, usb_detect_flag;
	volatile int i;
	i = 0;
#endif

#if defined(TCC_I2C_USE)
	i2c_init();
#endif

	clock_init();

#if defined(TCC_PCA953X_USE)
	pca953x_init();
#endif

	uart_init();

	adc_init();

#if defined(DISPLAY_SPLASH_SCREEN) || defined(DISPLAY_SPLASH_SCREEN_DIRECT)
	#ifndef DEFAULT_DISPLAY_OUTPUT_DUAL
		display_init();
		dprintf(INFO, "Display initialized\n");
		//disp_init = 1;
	#endif

#endif

#if defined(TARGET_M805_892X_EVM)
	ac_charger_detect_flag = ac_charger_detect();
	usb_detect_flag = usb_detect();
	
	dprintf(INFO, "USB DETECT(%d) AC CHARGER DETECT(%d)\n", usb_detect_flag, ac_charger_detect_flag);

    if( check_fwdn_mode() )
        return;
    
	if(! (ac_charger_detect_flag || usb_detect_flag) ) {
		if( check_low_battery()) {
			while(1) {
				dprintf(CRITICAL, "Low Battery!!!! Power Off\n");
				display_lowbattery_logo();				
				for ( i = 0 ; i < 1000000 ; i ++ );
				for ( i = 0 ; i < 1000000 ; i ++ );
				while(1) powercon_disable();
			}
		}
	} else {
        if( check_low_battery()) {
            display_lowbattery_logo();
        
        
		while( check_low_battery()) {
        	ac_charger_detect_flag = ac_charger_detect();
        	usb_detect_flag = usb_detect();

            for ( i = 0 ; i < 1000000 ; i ++ );
            
            if(! (ac_charger_detect_flag || usb_detect_flag) ) {
				dprintf(CRITICAL, "Low Battery!!!! Power Off\n");
				while(1) powercon_disable();
            }
	    }
        }
		
    }
#endif
    display_booting_logo();

	dprintf(INFO, "platform_init()\n");
}
Example #21
0
int main()
{
	int i;
	char* argv[8];
	int argc;

	printf_register(serial_putchar);
	
	printf("waiting...");

	// Wait a short while
	for(i = 0; i < 10000; ++i)
		asm volatile ("nop");

	printf("configuring...");

	i2c_init();
	argv[0] = "1";
	argc = 1;
	doConfig(argv, argc);

	printf("done.\n");

	while (1)
	{
		printf("\n> ");

		argc = readcmd(argv, 8);
		printf("\r\n> ");

		if(argc > 0)
		{
			switch(argv[0][0])
			{
				case 'S':
					doSet(argv, argc);
					break;
				case 'G':
					doGet(argv, argc);
					break;
				case 'R':
					doResult(argv, argc);
					break;
				case 'W':
					doWrite(argv, argc);
					break;
				case 'X':
					doRead(argv, argc);
					break;
				case 'Z':
					doStatus(argv, argc);
					break;
				case 'I':
					switch(argv[0][1])
					{
						case 'I':
							i2c_init();
							break;
						case 'W':
							doI2CWrite(argv, argc);
							break;
						case 'R':
							doI2CRead(argv, argc);
							break;
						case 'C':
							doConfig(argv, argc);
							break;
					}
					break;
			}
		}

	}
}
Example #22
0
void measures_init(void)
{
	sem_init(&i2c_sem);
	i2c_init(&i2c_bus, I2C_BITBANG0, CONFIG_I2C_FREQ);
	ASSERT(mma845x_init(&i2c_bus, 0, MMADYN_4G));
}
Example #23
0
int ble_i2c( const uint8_t * const pcCommandString,uint8_t* pcWriteBuffer,uint32_t commpare_length)
{
	const int8_t *pcom;
	uint32_t pxParameterStringLength;
	
	uint8_t len;
	uint8_t i2c_addr ;
	uint8_t i2c_reg  ;
	uint8_t i2c_data;

	if(pcCommandString[commpare_length+1] == '=')
	{
		//i2c_addr
		pcom = at_get_parameter((const int8_t*)pcCommandString + commpare_length + 2, 1, &pxParameterStringLength);
		if(pxParameterStringLength > 0)
		{
			i2c_addr = at_HEXstringToNum((const uint8_t *)pcom, pxParameterStringLength); 
		}
		else
		{
			goto pram_err;
		}
		
		//i2c_reg
		pcom = at_get_parameter((const int8_t*)pcCommandString + commpare_length + 2, 2, &pxParameterStringLength);
		if(pxParameterStringLength > 0)
		{
			i2c_reg  = at_HEXstringToNum((const uint8_t *)pcom, pxParameterStringLength); 
		}
		else
		{
			goto pram_err;
		}
		
		// i2c R&W?
		pcom = at_get_parameter((const int8_t*)pcCommandString + commpare_length + 2, 0, &pxParameterStringLength);
		if(pxParameterStringLength == 1)
		{ 
			syscon_SetPMCR1WithMask(QN_SYSCON,P23_MASK_PIN_CTRL | P24_MASK_PIN_CTRL,P23_I2C_SDA_PIN_CTRL | P24_I2C_SCL_PIN_CTRL);	
			i2c_init(I2C_SCL_RATIO(100000), i2c_buff, 4);
			if(pcom[0] == 'R')
			{
				i2c_data = I2C_BYTE_READ(i2c_addr,i2c_reg);
				if(i2c_is_finish())
				{
					len = commpare_length+1;
					memcpy(pcWriteBuffer, pcCommandString, len);
					len += sprintf((char *)pcWriteBuffer + len,":0x%02x\r\nOK\r\n",i2c_data);	
					return len;
				}
				goto pram_err; 
			}
			else if(pcom[0] == 'W')
			{
				//i2c_data
				pcom = at_get_parameter((const int8_t*)pcCommandString + commpare_length + 2, 3, &pxParameterStringLength);
				if(pxParameterStringLength > 0)
				{
					i2c_data = at_HEXstringToNum((const uint8_t *)pcom, pxParameterStringLength); 	
				}
				else
				{
					goto pram_err;
				}

				I2C_BYTE_WRITE(i2c_addr, i2c_reg, i2c_data);
				if(!i2c_is_finish())
				{
					goto pram_err;
				}

			}
		}
		else
		{
			goto pram_err;
		}	
	}
	else
	{
		goto pram_err;
	}
	return sprintf((char*)pcWriteBuffer,"OK\r\n");
	
pram_err:	
	return sprintf((char*)pcWriteBuffer,"ERR\r\n");
}
Example #24
0
int __init
pcf8563_init(void)
{
	static int res;
	static int first = 1;

	if (!first)
		return res;
	first = 0;

	/* Initiate the i2c protocol. */
	res = i2c_init();
	if (res < 0) {
		printk(KERN_CRIT "pcf8563_init: Failed to init i2c.\n");
		return res;
	}

	/*
	 * First of all we need to reset the chip. This is done by
	 * clearing control1, control2 and clk freq and resetting
	 * all alarms.
	 */
	if (rtc_write(RTC_CONTROL1, 0x00) < 0)
		goto err;

	if (rtc_write(RTC_CONTROL2, 0x00) < 0)
		goto err;

	if (rtc_write(RTC_CLOCKOUT_FREQ, 0x00) < 0)
		goto err;

	if (rtc_write(RTC_TIMER_CONTROL, 0x03) < 0)
		goto err;

	/* Reset the alarms. */
	if (rtc_write(RTC_MINUTE_ALARM, 0x80) < 0)
		goto err;

	if (rtc_write(RTC_HOUR_ALARM, 0x80) < 0)
		goto err;

	if (rtc_write(RTC_DAY_ALARM, 0x80) < 0)
		goto err;

	if (rtc_write(RTC_WEEKDAY_ALARM, 0x80) < 0)
		goto err;

	/* Check for low voltage, and warn about it. */
	if (rtc_read(RTC_SECONDS) & 0x80) {
		voltage_low = 1;
		printk(KERN_WARNING "%s: RTC Voltage Low - reliable "
		       "date/time information is no longer guaranteed!\n",
		       PCF8563_NAME);
	}

	return res;

err:
	printk(KERN_INFO "%s: Error initializing chip.\n", PCF8563_NAME);
	res = -1;
	return res;
}
Example #25
0
void sparrow_init() {

	//First: Turn off external power blocks
	set_digital_output(BOOST_5V_EN_PIN);
	write_pin(BOOST_5V_EN_PIN, false);

	set_digital_output(PORT_5V_EN_PIN);
	write_pin(PORT_5V_EN_PIN, false);

//TODO: Disable 5V USB generation (USB0_PPWR, P2_0)

//TODO: Set fault input interrupt handlers to shutdown peripheral and react accordingly
//E.g. USB_PWR_FAULT_HANDLER (P2_1)

	set_digital_output(AUDIO_POWER_EN_PIN);
	write_pin(AUDIO_POWER_EN_PIN, false);

	set_digital_output(WIFI_POWER_EN_PIN );
	write_pin(WIFI_POWER_EN_PIN, false );


//TODO: Why does this pin not work? For now, the input pullup keeps the eth domain powered up permamently.
//Setting OUT and writing this bit caused the device to hang. Might be correct now, check again.
//	set_digital_output(ETH_POWER_EN_PIN);
	write_pin(ETH_POWER_EN_PIN, false);

	//Set all ext devices to !reset
	set_digital_output(AUDIO_NRESET);
	write_pin(AUDIO_NRESET, false);

	//init base clock to xtal, main clock, via pll1
	clock_set_xtal_core_freq(MAIN_CLOCK_MHZ/XTAL_MHZ, 1);

	//enable derived base clocks for shared use. Dedicated clocks are enabled by the
	//respective peripheral interface
	clock_set_source(BASE_APB1_CLK, true, CLKSRC_PLL1);    //I2C0
	clock_set_source(BASE_APB3_CLK, true, CLKSRC_PLL1);    //I2C1

	//GPIO
	set_digital_output(LED1_PIN);
	set_digital_output(LED2_PIN);
	set_digital_output(LED3_PIN);
	set_digital_input(BUTTON_PIN, true, false, false);
	write_pin(LED1_PIN, false);
	write_pin(LED2_PIN, false);
	write_pin(LED3_PIN, false);



	//I2C
	i2c_configure_pin(I2C1_SDA_PIN_GROUP, I2C1_SDA_PIN_IDX, I2C1_SDA_PIN_MODE);
	i2c_configure_pin(I2C1_SCL_PIN_GROUP, I2C1_SCL_PIN_IDX, I2C1_SCL_PIN_MODE);
	i2c_init(0, I2C_MODE_FAST);
	i2c_init(1, I2C_MODE_FAST);
	//TODO: I2C1 might also be used as GPIOs

	//SPI
	scu_set_pin(SPI0_MISO_GROUP, SPI0_MISO_IDX, SPI0_MISO_MODE, false, false, true, true, true);
	scu_set_pin(SPI0_MOSI_GROUP, SPI0_MOSI_IDX, SPI0_MOSI_MODE, false, false, true, false, true);
	scu_set_pin(SPI0_SCK_GROUP, SPI0_SCK_IDX, SPI0_SCK_MODE, false, false, true, false, true);
	scu_set_pin(SPI0_SSEL_GROUP, SPI0_SSEL_IDX, SPI0_SSEL_MODE, false, false, true, false, true);
	
	scu_set_pin(SPI1_MISO_GROUP, SPI1_MISO_IDX, SPI1_MISO_MODE, false, false, true, true, true);
	scu_set_pin(SPI1_MOSI_GROUP, SPI1_MOSI_IDX, SPI1_MOSI_MODE, false, false, true, false, true);
	scu_set_pin(SPI1_SCK_GROUP, SPI1_SCK_IDX, SPI1_SCK_MODE, false, false, true, false, true);
	scu_set_pin(SPI1_SSEL_GROUP, SPI1_SSEL_IDX, SPI1_SSEL_MODE, false, false, true, false, true);



	//I2S
	i2s_configure_pin(I2S0_TX_MCLK_GROUP, I2S0_TX_MCLK_IDX, I2S0_TX_MCLK_MODE);
	i2s_configure_pin(I2S0_TX_WS_GROUP, I2S0_TX_WS_IDX, I2S0_TX_WS_MODE);
	i2s_configure_pin(I2S0_TX_SCK_GROUP, I2S0_TX_SCK_IDX, I2S0_TX_SCK_MODE);
	i2s_configure_pin(I2S0_TX_SD_GROUP, I2S0_TX_SD_IDX, I2S0_TX_SD_MODE);
	//i2s_configure_pin(I2S0_RX_MCLK_GROUP, I2S0_RX_MCLK_IDX, I2S0_RX_MCLK_MODE);
	//i2s_configure_pin(I2S0_RX_WS_GROUP, I2S0_RX_WS_IDX, I2S0_RX_WS_MODE);
	//i2s_configure_pin(I2S0_RX_SCK_GROUP, I2S0_RX_SCK_IDX, I2S0_RX_SCK_MODE);
	//i2s_configure_pin(I2S0_RX_SD_GROUP, I2S0_RX_SD_IDX, I2S0_RX_SD_MODE);


	//SD
	scu_set_pin_mode(SD_CD_GROUP, SD_CD_PIN, SD_CD_MODE);
	scu_enable_pin_in_buffer(SD_CD_GROUP, SD_CD_PIN, true);
	scu_set_pin_mode(SD_WP_GROUP, SD_WP_PIN, SD_WP_MODE);
	//TODO: configure WP pin
	scu_set_pin_mode(SD_CMD_GROUP, SD_CMD_PIN, SD_CMD_MODE);
	scu_set_pin_pullup(SD_CMD_GROUP, SD_CMD_PIN, true);
	scu_enable_pin_in_buffer(SD_CMD_GROUP, SD_CMD_PIN, true);
	scu_set_pin_mode(SD_D0_GROUP, SD_D0_PIN, SD_D0_MODE);
	scu_set_pin_pullup(SD_D0_GROUP, SD_D0_PIN, true);
	scu_enable_pin_in_buffer(SD_D0_GROUP, SD_D0_PIN, true);
	scu_set_pin_mode(SD_D1_GROUP, SD_D1_PIN, SD_D1_MODE);
	scu_set_pin_pullup(SD_D1_GROUP, SD_D1_PIN, true);
	scu_enable_pin_in_buffer(SD_D1_GROUP, SD_D1_PIN, true);
	scu_set_pin_mode(SD_D2_GROUP, SD_D2_PIN, SD_D2_MODE);
	scu_set_pin_pullup(SD_D2_GROUP, SD_D2_PIN, true);
	scu_enable_pin_in_buffer(SD_D2_GROUP, SD_D2_PIN, true);
	scu_set_pin_mode(SD_D3_GROUP, SD_D3_PIN, SD_D3_MODE);
	scu_set_pin_pullup(SD_D3_GROUP, SD_D3_PIN, true);
	scu_enable_pin_in_buffer(SD_D3_GROUP, SD_D3_PIN, true);
	scu_set_clock_pin_mode(SD_CLK_CLK, SD_CLK_MODE, false, false, true, false, false);


	//Ethernet RMII
	creg_init();
	creg_set_eth_interface(true);
	scu_set_clock_pin_mode(ETH_CLK_CLK, ETH_CLK_MODE, false, false, true, true, false);
	scu_set_pin(ETH_RXD0_GROUP,   ETH_RXD0_PIN,   ETH_RXD0_MODE,   false, false, true, true,  false);
	scu_set_pin(ETH_RXD1_GROUP,   ETH_RXD1_PIN,   ETH_RXD1_MODE,   false, false, true, true,  false);
	scu_set_pin(ETH_TXD0_GROUP,   ETH_TXD0_PIN,   ETH_TXD0_MODE,   false, false, true, false, false);
	scu_set_pin(ETH_TXD1_GROUP,   ETH_TXD1_PIN,   ETH_TXD1_MODE,   false, false, true, false, false);
	scu_set_pin(ETH_MDC_GROUP,    ETH_MDC_PIN,    ETH_MDC_MODE,    false, false, true, false, false);
	scu_set_pin(ETH_TXEN_GROUP,   ETH_TXEN_PIN,   ETH_TXEN_MODE,   false, false, true, false, false);
	scu_set_pin(ETH_CRS_DV_GROUP, ETH_CRS_DV_PIN, ETH_CRS_DV_MODE, false, false, true, true,  false);
	scu_set_pin(ETH_MDIO_GROUP,   ETH_MDIO_PIN,   ETH_MDIO_MODE,   false, false, true, true,  false);
	set_digital_input(ETH_NINT_PIN, true, false, true);
	set_digital_output(ETH_NRST_PIN);
	write_pin(ETH_NRST_PIN, false);	//put into reset

	//CC3000 WIFI
	set_digital_output(CC3000_SW_EN_PIN);
	write_pin(CC3000_SW_EN_PIN, false);
	set_digital_input(CC3000_IRQ_PIN, false, false, true);

}
Example #26
0
void main_task(void *data) {
	int nop_i;
	//char tempBuf[256];
	//int ret;

	//uart_print("SCLK:");
	//uart_printInt(calulate_system_clock());
	//uart_print("\n\r");

	//while(1);
	//OM_SELECT : //I2C MASTER[4], UPHY_IN_REG
	//OM_SELECT |= ( OM_SELECT_I2C_M_SELECT_BIT | OM_SELECT_UPHY_IN_REG_BIT );
	OM_SELECT |= ( OM_SELECT_I2C_M_SELECT_BIT );
 	for ( nop_i = 0; nop_i < 100000; nop_i++ ) __asm __volatile("l.nop   0");

	
	//usb init
	init_usb();

	//sensorSetToVGA();

	//GPIO init
	GPIOInit();
	//GPIO12 : LED
	//GPIO13 : AIC RESET
	//GPIO14 : Dummy
	//GPIO15 : I2S_WS
	GPIOSetOutput(GPIO12|GPIO13|GPIO14); // ViDan -> ...
	GPIOSetInput(GPIO15);

	GPIOSetHigh(GPIO12);

	//i2c devider setting
	I2C_M_PRESCALE_LOW = 0x62;
	I2C_M_PRESCALE_HIGH =0x00;

	//i2c init
	i2c_init();
	//uart_print("i2c_initialized\n\r");

	//flash_avata_mode_check();

	//temporary vidan register setting //h.264 only
	//*HIF_REG2   = 0x28; //HIF_REG2   ;640 / 16 ;HIF_REG
	//*HIF_REG3   = 0x1e; //HIF_REG3   ;480 / 16
	//*HIF_REG4   = 0x28; //HIF_REG4   ;640 / 16
	//*HIF_REG5   = 0x1e; //HIF_REG5   ;480 / 16
	//*HIF_REG6   = 0x45; //HIF_REG6   ; for jpeg 20 ;DVP_OUT 60 ;OUTPUT_PATH : 11(sim) 66:jpeg only
	//*HIF_REG10  = 0x22; //HIF_REG10  ;start Mux
	*HIF_REG24  = 0x33; //HIF_REG24  ;qp value i : 28 1c
	*HIF_REG25  = 0x33; //HIF_REG25  ;qp value p : 28 1c
	//*HIF_REG26  = 0x64; //HIF_REG26  ;intra period : 10 0a
	*HIF_REG26  = 0x00; //HIF_REG26  ;intra period : 10 0a
	//*HIF_REG31  = 0x02; //HIF_REG31  ;#############rate control disable 02
	//*HIF_REG31  = 0x00; //HIF_REG31  ;#############rate control disable 02
	//*HIF_REG54  = 0x44; //HIF_REG54  ;no fastme defined in user.mk slice mode 02
	//*HIF_REG55  = 0x10; //HIF_REG55  ;#############safe_mode on [4] , frame_rate_control on[5], audio start[6] 50
	*HIF_REG194 = 0x16; //HIF_REG194 ;afifo_almost_full_size (max = 512 / 16 - 1)
	//*HIF_REG57  = 0xab; //HIF_REG57  ;slice_argument : 12
	*HIF_REG58  = 0xfe; //HIF_REG58  ;LFAlphaC0Offset : -2
	*HIF_REG59  = 0xff; //HIF_REG59  ;LFBetaOffset : -1
	*HIF_REG66  = 0x20; //HIF_REG66  ;rate control bit rate :  51200 / 1024 ( 50k)
	*HIF_REG67  = 0x00; //HIF_REG67  ;rate control bit rate :  51200 / 1024 ( 50k)
	*HIF_REG68  = 0x0f; //HIF_REG68  ;rate control frame rate : 11
	*HIF_REG69  = 0x31; //HIF_REG69  ;osb_fifo_rate_idx : 0, RATE_CONTROL_SEINITIAL_QP_VALUE : 0
	//*HIF_REG70  = 0xe8; //HIF_REG70  ;SPEED_CONTROL_MB_CYCLE_VALUE : 1000
	//*HIF_REG71  = 0x03; //HIF_REG71  ;SPEED_CONTROL_MB_CYCLE_VALUE : 1000
	*HIF_REG50  = 0x32; //HIF_REG50  ; quality factor 30
	*HIF_REG60  = 0x21; //HIF_REG60  ; i2s_ctrl
	*HIF_REG63  = 10; //HIF_REG63  ; audio_gain
	//*HIF_REG64 = 0; //Zoom Level : 0
	
	// Never set SDRAM registers here!!!
	// If you set, SDRAM module will Die ... 
	//*HIF_REG140  = 0xcb;	//0xea;  //HIF_REG40 SDRAMC
	//*HIF_REG141  = 0x30;	//0x40;  //HIF_REG41 SDRAMC
	//*HIF_REG142  = 0x4b;	//0x8c;  //HIF_REG42 SDRAMC
	//*HIF_REG143  = 0x12;	//0x02;  //HIF_REG43 SDRAMC
	//*HIF_REG143  = *HIF_REG143 & (0xef);
	
	
	CONSTANT_QP_VALUE = 32;
	MAX_BIT_RATE_LOW = 0xB0;
	MAX_BIT_RATE_HIGH = 0x04;
	JPEG_FPS = 15;

	//if ( avata_capture_mode )
	//{
	//	uart_print("avata_capture_mode\n\r");
	//	//Sensor2 Setting
	//	*HIF_REG74 = 0x1e; //SENSOR2_OUT_W
	//	*HIF_REG75 = 0x1e; //SENSOR2_OUT_H
	//	*HIF_REG76 = 0x0f; //IMAGE2_OUT_W
	//	*HIF_REG77 = 0x0f; //IMAGE2_OUT_H
	//	*HIF_REG51 = 0x0f; //HIF_REG51  ; jpeg width
	//	*HIF_REG52 = 0x0f; //HIF_REG52  ; jpeg height
	//}
	//else //normal mode
	//{
	//	//Sensor2 Setting
	//	*HIF_REG74 = 0x28; //SENSOR2_OUT_W
	//	*HIF_REG75 = 0x1e; //SENSOR2_OUT_H
	//	*HIF_REG76 = 0x28; //IMAGE2_OUT_W
	//	*HIF_REG77 = 0x1e; //IMAGE2_OUT_H
	//	*HIF_REG51 = 0x14; //HIF_REG51  ; jpeg width
	//	*HIF_REG52 = 0x0f; //HIF_REG52  ; jpeg height
	//}
	//*HIF_REG102 = 0x80; //JPEG from CAM2

	//uart_print("hif_initialized\n\r");

//	if ( !avata_capture_mode )
//	{
//// {{{
//	//Sensor Setting
//	//Sensor Reset
//	tempBuf[0] = 0x30;
//	tempBuf[1] = 0x12;
//	tempBuf[2] = 0x80;
//	ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//	//uart_print("ret:");
//	//uart_printInt(ret);
//	//uart_print("\n\r");
//
// 	for ( nop_i = 0; nop_i < 10000000; nop_i++ ) __asm __volatile("l.nop   0");
//
//	////read Sensor ID
//	//tempBuf[0] = 0x30;
//	//tempBuf[1] = 0x0a;
//	//ret = i2c_write(0x30, 2, tempBuf);
//	////uart_print("ret:");
//	////uart_printInt(ret);
//	////uart_print("\n\r");
//	//ret = i2c_read(0x30, 2, tempBuf);
//	//uart_print("read id ret:");
//	//uart_printInt(ret);
//	//uart_print(",");
//	//uart_printInt(tempBuf[0]);
//	//uart_print(",");
//	//uart_printInt(tempBuf[1]);
//	//uart_print("\n\r");
//	////read Sensor ID
//	for ( nop_i = 0; nop_i < 3; nop_i++ )
//	{
//// {{{
//		tempBuf[0] = 0x30; tempBuf[1] = 0x8c; tempBuf[2] = 0x80; 
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x8d; tempBuf[2] = 0x0e;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x36; tempBuf[1] = 0x0b; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xb0; tempBuf[2] = 0xff;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xb1; tempBuf[2] = 0xff;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xb2; tempBuf[2] = 0x04;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x0e; tempBuf[2] = 0x34;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x0f; tempBuf[2] = 0xa6;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x10; tempBuf[2] = 0x81;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x82; tempBuf[2] = 0x01;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xf4; tempBuf[2] = 0x01;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x90; tempBuf[2] = 0x43;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x91; tempBuf[2] = 0xc0;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xac; tempBuf[2] = 0x42;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xd1; tempBuf[2] = 0x08;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xa8; tempBuf[2] = 0x54;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x15; tempBuf[2] = 0x02;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x93; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x7e; tempBuf[2] = 0xe5;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x79; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xaa; tempBuf[2] = 0x52;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x17; tempBuf[2] = 0x40;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xf3; tempBuf[2] = 0x83;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x6a; tempBuf[2] = 0x0c;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x6d; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x6a; tempBuf[2] = 0x3c;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x76; tempBuf[2] = 0x6a;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xd9; tempBuf[2] = 0x95;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x16; tempBuf[2] = 0x82;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x36; tempBuf[1] = 0x01; tempBuf[2] = 0x30;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x4e; tempBuf[2] = 0x88;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xf1; tempBuf[2] = 0x82;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x6f; tempBuf[2] = 0x14;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x2a; tempBuf[2] = 0x02;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x2b; tempBuf[2] = 0x6a;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x12; tempBuf[2] = 0x10;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x11; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x13; tempBuf[2] = 0xf7;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x18; tempBuf[2] = 0x80;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x19; tempBuf[2] = 0x70;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x1a; tempBuf[2] = 0xd4;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x1c; tempBuf[2] = 0x13;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x1d; tempBuf[2] = 0x17;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x70; tempBuf[2] = 0x5d;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x72; tempBuf[2] = 0x4d;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xaf; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x48; tempBuf[2] = 0x1f;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x49; tempBuf[2] = 0x4e;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x4a; tempBuf[2] = 0x20;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x4f; tempBuf[2] = 0x20;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x4b; tempBuf[2] = 0x02;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x4c; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x4d; tempBuf[2] = 0x02;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x4f; tempBuf[2] = 0x20;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xa3; tempBuf[2] = 0x10;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x13; tempBuf[2] = 0xf7;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x14; tempBuf[2] = 0x44;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x71; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x70; tempBuf[2] = 0x5d;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x73; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x72; tempBuf[2] = 0x4d;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x1c; tempBuf[2] = 0x05;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x1d; tempBuf[2] = 0x06;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x4d; tempBuf[2] = 0x42;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x4a; tempBuf[2] = 0x40;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x4f; tempBuf[2] = 0x40;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x95; tempBuf[2] = 0x07;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x96; tempBuf[2] = 0x16;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x97; tempBuf[2] = 0x1d;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x20; tempBuf[2] = 0x01;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x21; tempBuf[2] = 0x18;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x22; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x23; tempBuf[2] = 0x06;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x24; tempBuf[2] = 0x06;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x25; tempBuf[2] = 0x58;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x26; tempBuf[2] = 0x02;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x27; tempBuf[2] = 0x61;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x88; tempBuf[2] = 0x02;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x89; tempBuf[2] = 0x80;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x8a; tempBuf[2] = 0x01;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x8b; tempBuf[2] = 0xe0;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x16; tempBuf[2] = 0x64;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x17; tempBuf[2] = 0x25;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x18; tempBuf[2] = 0x80;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x19; tempBuf[2] = 0x08;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x1a; tempBuf[2] = 0x28;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x1b; tempBuf[2] = 0x1e;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x1c; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x1d; tempBuf[2] = 0x38;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x31; tempBuf[1] = 0x00; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x20; tempBuf[2] = 0xfa;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x21; tempBuf[2] = 0x11;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x22; tempBuf[2] = 0x92;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x23; tempBuf[2] = 0x01;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x24; tempBuf[2] = 0x97;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x25; tempBuf[2] = 0x02;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x26; tempBuf[2] = 0xff;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x27; tempBuf[2] = 0x10;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x28; tempBuf[2] = 0x10;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x29; tempBuf[2] = 0x1f;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x2a; tempBuf[2] = 0x58;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x2b; tempBuf[2] = 0x50;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x2c; tempBuf[2] = 0xbe;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x2d; tempBuf[2] = 0xce;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x2e; tempBuf[2] = 0x2e;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x2f; tempBuf[2] = 0x36;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x30; tempBuf[2] = 0x4d;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x31; tempBuf[2] = 0x44;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x32; tempBuf[2] = 0xf0;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x33; tempBuf[2] = 0x0a;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x34; tempBuf[2] = 0xf0;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x35; tempBuf[2] = 0xf0;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x36; tempBuf[2] = 0xf0;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x37; tempBuf[2] = 0x40;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x38; tempBuf[2] = 0x40;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x39; tempBuf[2] = 0x40;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x3a; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x3b; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x80; tempBuf[2] = 0x20;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x81; tempBuf[2] = 0x5b;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x82; tempBuf[2] = 0x05;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x83; tempBuf[2] = 0x22;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x84; tempBuf[2] = 0x9d;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x85; tempBuf[2] = 0xc0;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x86; tempBuf[2] = 0xb6;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x87; tempBuf[2] = 0xb5;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x88; tempBuf[2] = 0x02;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x89; tempBuf[2] = 0x98;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x8a; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x40; tempBuf[2] = 0x09;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x41; tempBuf[2] = 0x19;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x42; tempBuf[2] = 0x2f;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x43; tempBuf[2] = 0x45;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x44; tempBuf[2] = 0x5a;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x45; tempBuf[2] = 0x69;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x46; tempBuf[2] = 0x75;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x47; tempBuf[2] = 0x7e;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x48; tempBuf[2] = 0x88;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x49; tempBuf[2] = 0x96;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x4a; tempBuf[2] = 0xa3;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x4b; tempBuf[2] = 0xaf;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x4c; tempBuf[2] = 0xc4;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x4d; tempBuf[2] = 0xd7;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x4e; tempBuf[2] = 0xe8;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x4f; tempBuf[2] = 0x20;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x50; tempBuf[2] = 0x35;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x51; tempBuf[2] = 0x0a;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x52; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x53; tempBuf[2] = 0x16;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x54; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x55; tempBuf[2] = 0x85;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x56; tempBuf[2] = 0x35;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x57; tempBuf[2] = 0x0a;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x58; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x59; tempBuf[2] = 0x19;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x5a; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x5b; tempBuf[2] = 0x85;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x5c; tempBuf[2] = 0x35;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x5d; tempBuf[2] = 0x0a;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x5e; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x5f; tempBuf[2] = 0x14;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x60; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x61; tempBuf[2] = 0x85;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x63; tempBuf[2] = 0x01;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x64; tempBuf[2] = 0x03;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x65; tempBuf[2] = 0x02;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x66; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x01; tempBuf[2] = 0xff;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x8B; tempBuf[2] = 0x1e;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x8c; tempBuf[2] = 0x10;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x8d; tempBuf[2] = 0x40;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x70; tempBuf[2] = 0xd0;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x71; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x72; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x73; tempBuf[2] = 0x40;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x74; tempBuf[2] = 0x10;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x75; tempBuf[2] = 0x10;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x76; tempBuf[2] = 0x04;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x77; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x78; tempBuf[2] = 0x04;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x79; tempBuf[2] = 0x80;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x69; tempBuf[2] = 0x86;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x7c; tempBuf[2] = 0x10;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x87; tempBuf[2] = 0x02;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x90; tempBuf[2] = 0x03;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xaa; tempBuf[2] = 0x52;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xa3; tempBuf[2] = 0x80;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xa1; tempBuf[2] = 0x41;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x00; tempBuf[2] = 0xfc;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x33; tempBuf[1] = 0x02; tempBuf[2] = 0x11;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x34; tempBuf[1] = 0x00; tempBuf[2] = 0x02;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x36; tempBuf[1] = 0x06; tempBuf[2] = 0x20;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x36; tempBuf[1] = 0x01; tempBuf[2] = 0x30;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x0e; tempBuf[2] = 0x34;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xf3; tempBuf[2] = 0x83;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x4e; tempBuf[2] = 0x88;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x36; tempBuf[1] = 0x3b; tempBuf[2] = 0x01;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x36; tempBuf[1] = 0x3c; tempBuf[2] = 0xf2;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0xa1; tempBuf[2] = 0x81;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x86; tempBuf[2] = 0x0f;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//		tempBuf[0] = 0x30; tempBuf[1] = 0x86; tempBuf[2] = 0x00;
//		ret = i2c_write(0x30, 3, tempBuf); if ( ret ) uart_print("sensor set error!!!\n\r");
//
//
//// }}}
//	}
//	uart_print("Sensor Setting Done\n\r");
//// }}}
//	}
	//sensorSetToVGA();

	//AIC Initialize
	//AIC Reset
	GPIOSetHigh(GPIO13);
 	for ( nop_i = 0; nop_i < 1000; nop_i++ ) __asm __volatile("l.nop   0");
	GPIOSetLow(GPIO13);
 	for ( nop_i = 0; nop_i < 1000; nop_i++ ) __asm __volatile("l.nop   0");
	GPIOSetHigh(GPIO13);
 	for ( nop_i = 0; nop_i < 1000; nop_i++ ) __asm __volatile("l.nop   0");
	//uart_print("AIC_reset\n\r");

	AIC3101Init( MASTER, AIC3101_FS_16KHz, MIC, AUDIO_GAIN );
	//i2s init
	init_i2s();

	//ParamToHIFConv(); //jykim_20080715_HIF_CONV++

	//Wait I2C Start Command 
	//while (!COMMAND_START)
//		;

	lencod();
}
Example #27
0
int main(void)
{
uint8_t dots,cnt,f;
uint8_t key,prev_key;
uint16_t dcnt;
	_WDR();
	WDTCR = 0x0f;

	DDRA = 0xff;	// porta 0-7 out
	PORTA = 0;
	DDRB = 0x0f;	// portb 0-3 out
	PORTB = 0x0f;

	ADCSRA = 0x87;	// ADC
        ADMUX = 9;
	i2c_init();
	clock_init();

	TCCR0B = 5;	// cpu clk / 1
	TIMSK = 2;
	ref_pos = 0;
	skip = 0;
	_SEI();
	bme_init();

//	wr_eeprom(11,0x55);

	prev_key = 0;
	timer = 0;
	timer_pos = 0;
	cnt = 0;
	f = 1;
	swcnt = sw = mode = 0;
	dcnt = 0;
	get_date();
	for(dots=0; ; ) {
		if(++cnt >= 10 || f) {
			if(!timer) {
				if(mode) setting(); else show();
			} else {
				uint8_t tm = timer/60;
				uint8_t ts = timer%60;
				putchr(0,tm/10);
				putchr(1,tm%10);
				putchr(2,ts/10);
				putchr(3,ts%10);
				timer--;
				if(timer == 300) beep(); //5min
				else if(!timer) beep();  //end
			}
			cnt = 0;
			f = 0;
			if(mode) {
				if(dots) {
					dsp_buf[0] = chrSP;
					dsp_buf[1] = chrSP;
					dsp_buf[2] = chrSP;
					dsp_buf[3] = chrSP;
				}
			} else {
				if(dots || sw == 1 || sw == 2) {
					dsp_buf[1] |= segDP;
				}
				if(sw == 3) {
					dsp_buf[1] &= ~segDP;
					dsp_buf[3] |= segDP;
				}
			}
			dots ^= 1;
		}
		
		delay(100);

		key = get_key();
		if(key == 0 && prev_key != 0) {
			tocnt = 0;
			switch(prev_key) {
				case KEY_1:
					if(timer) break;
					if(++mode > 5) mode = 0;
					f = 1;
					sw = swcnt = 0;
					break;

				case KEY_2:
					if(timer) break;
					inc();
					f = 1;
					sw = swcnt = 0;
					break;

				case KEY_3:
					if(timer_pos) {
						timer_pos--;
						timer = 60*pgm_read_byte(timer_vals+timer_pos);
					} else {
						timer = 0;
					}
					f = 1;
					break;

				case KEY_4:
					if(timer_pos < NELEM(timer_vals)-1) {
						if(timer != 0) timer_pos++;
						timer = 60*pgm_read_byte(timer_vals+timer_pos);
						f = 1;
					}
					break;

				default:;
			}
		}
		prev_key = key;

		if(++dcnt > 10*60) {
			dcnt = 0;
			get_date();
		}
	}

	return 0;
}
Example #28
0
int max17042_init(int load)
{
	uint16_t data;
	int i;
	static const uint16_t* bufp = (uint16_t*) 0x81000000;

	uint16_t* savestorep;
	int err, retries=2, force_por=0;
	uint16_t designcap;

	type_params = &param_table[get_battery_type(load)];

	designcap = type_params->init_params->Capacity;

	i2c_init(100, MAX17042_ADDR);

	if ( MAX_READ(MAX17042_STATUS, (uchar*)&data) != 0)
	{
	    DEBUG("MAX17042+UBOOT: No battery or 0V battery!\n");
	    return 1;
	}

	DEBUG("MAX17042+UBOOT: gas gauge detected (0x%04x)\n",data);

	//check if we need restore registers inside
	is_power_on_rst();
	if ( is_power_on ) {
		DEBUG("MAX17042+UBOOT:POR detected!\n");
	} else {
		DEBUG("MAX17042+UBOOT:WARM BOOT    \n");
	}

	if (load) {
		run_command("mmcinit 1; fatload mmc 1:5 0x81000000 max17042.bin 0x1000", 0);
	}

	if (*bufp != 0x1234 || !load) {
		DEBUG(" No valid max17042 init data found, assume no battery history \n");
		is_history_exist = 0;
	} else {
		DEBUG(" Valid max17042 init data is loaded into memory \n");
	}		
	
	if ( is_history_exist == 1 ) {

		savestorep = (uint16_t*)&save_store;

		for ( i = 0; i <(sizeof(save_store) / sizeof(uint16_t)); i++) {
			DEBUG (" 0x%04x\n", *bufp);
		
			*savestorep++ = *bufp++;
		}

#define MIN_CAP_AGING 25/100   // allow no less than 25% of design capacity before rejecting
#define MAX_CAP_AGING 13/10   // reject history capacity if it seems overly big
#define MIN_CAPNOM_AGING 25/100   // allow no less than 25% of nominal design capacity before rejecting
#define MAX_CAPNOM_AGING 15/10   // reject history capacity if it seems overly big

	    if ( (save_store.val_FullCAP < (uint16_t)(((uint32_t)designcap)*MIN_CAP_AGING)) ||
		 (save_store.val_FullCAP > (uint16_t)(((uint32_t)designcap)*MAX_CAP_AGING)) ||
	         (save_store.val_FullCAPNom < (uint16_t)(((uint32_t)designcap)*MIN_CAPNOM_AGING)) ||
		 (save_store.val_FullCAPNom > (uint16_t)(((uint32_t)designcap)*MAX_CAPNOM_AGING)) )
		{
			printf("Resetting battery defaults due to faulty CAPACITY (0x%x, 0x%x)\n",
				save_store.val_FullCAP, save_store.val_FullCAPNom);

			force_por = 1;
			is_history_exist = 0;
		}
	    else
		{
		DEBUG(" verify if mem loaded: FullcapNom was saved as %04x\n", 
			    save_store.val_FullCAPNom );
		}
		
		// In case val_DesignCap in history data does not match battery's design capacity,
		// we should throw away the history data.
		if(save_store.val_DesignCap != designcap) {
			printf("Resetting battery defaults because Design Capactiy(0x%04X)in history data"
			" does not match battery's Design Capacity(0x%04X)\n", 
				save_store.val_DesignCap, designcap);
			force_por = 1;
			is_history_exist = 0;
		} 			
	}

	save_store.val_DesignCap = designcap; 	

	i2c_init(100, 0x36);	//no need

	if ( !is_power_on )
	{
	    // when there is no history file, assume it is a POR
	    //if ( is_history_exist && max17042_check_init_config() == 0 )
	    // UPDATE: if history file doesn't exist don't do a POR,
		if (!force_por && max17042_check_init_config() == 0 )
		{
		DEBUG("MAX17042+UBOOT: warm config is okay\n");
		return 0;
	    }
	    else
	    {
		/* when the config is bad but it's not a POR, then something
		 * is quite wrong.
		 */
		DEBUG("MAX17042+UBOOT: warm config bad. soft POR\n");
		is_power_on = 1;

		max17042_soft_por();
	    }
	}

	//1. Delay 500ms
	udelay( 500 * 1000 );
	
	MAX17042_DUMPREG( MAX17042_Version );
	MAX17042_DUMPREG( MAX17042_DesignCap );
	MAX17042_DUMPREG( MAX17042_OCV );
	MAX17042_DUMPREG( MAX17042_FSTAT );
	MAX17042_DUMPREG( MAX17042_SOCvf );

	//2. Init Configuration
	max17042_init_config();

	//3. Save starting para
	max17042_save_start_para();
	
	//4. unlock model access
	max17042_unlock_model();

	do {
		//5. write custom model
		max17042_write_model();
	
		//6. read model
		//7. verify model
		err = max17042_read_verify_model();
	} while ( err != 0 && --retries > 0 );
	if ( retries == 0 ) {
		DEBUG( " writing model failed\n");
		return err;
	}
		
	retries = 2;
	do {		
		//8. lock model access
		max17042_lock_model();
	
		//9. verify model access is locked
		err = max17042_verify_lock_model();
	} while ( err != 0 && --retries > 0 );
	if ( retries == 0 ) {
		DEBUG( " locking model failed\n");
		return err;
	}
	
	//10. write custom parameters
	err = max17042_write_custom_para( );	
	if ( err != 0 ) {
	    DEBUG("write custom parameters failed\n");
	    return err;
	}
	
	//11 update full capacity parameters
	err = max17042_update_cap_para( );
	if ( err != 0 ) {
	    DEBUG("update capacity parameters failed\n");
	    return err;
	}

	//13. delay 350ms;
	udelay ( 350 *1000 );
	
	//14. write VFSOC to VFSCO0
	err = max17042_write_vfsoc();
	if ( err != 0 ) {
	    DEBUG("write vfsoc failed\n");
	    return err;
	}

	/* 15.5 Advance to Colomb-Counter Mode
	 * We do this all the time.  In the factory the battery is fresh (close to
	 * design capacity, and when there is a history file we restore a known good
	 * capacity after this, so that case it's safe to assume we have a good estimate
	 * as well.
	 */
	err = max17042_set_cycles( 0x00A0 );
	if ( err != 0 ) {
	    DEBUG("set cycles 0x00A0 failed\n");
	    return err;
	}

	err = max17042_load_cap_para( );
	if ( err != 0 ) {
	    DEBUG("load capacity parameters failed\n");
	    return err;
	}

	max17042_clear_POR();

	if ( is_history_exist ) {
	    err = max17042_restore_learned_para();
	    if ( err != 0 ) {
		DEBUG("restore learned parameters failed\n");
		return err;
	    }
	}

	is_power_on = 0;

	DEBUG("Max17042 init is done\n");
	
	return 0;
}
Example #29
0
void init(void)
{
	uint8_t i;
	bool	updated;
	
	//***********************************************************
	// I/O setup
	//***********************************************************
	// Set port directions
	DDRA		= 0x30;		// Port A
	DDRB		= 0x0A;		// Port B
	DDRC		= 0xFC;		// Port C
	DDRD		= 0xF2;		// Port D

	// Hold all PWM outputs low to stop glitches
	// M5 and M6 are on PortA for KK2.1
	MOTORS		= 0;
	M5			= 0;
	M6			= 0;

	// Preset I/O pins
	LED1 		= 0;		// LED1 off
	LVA 		= 0; 		// LVA alarm OFF
	LCD_SCL		= 1;		// GLCD clock high

	// Set/clear pull-ups (1 = set, 0 = clear)
	PINB		= 0xF5;		// Set PB pull-ups
	PIND		= 0x0C;		// Set PD pull-ups (Don't pull up RX yet)

	//***********************************************************
	// Spektrum receiver binding. Must be done immediately on power-up
	// 
	// 3 low pulses: DSM2 1024/22ms
	// 5 low pulses: DSM2 2048/11ms
	// 7 low pulses: DSMX 1024/22ms
	// 9 low pulses: DSMX 2048/11ms
	//***********************************************************

	PIND	= 0x0C;			// Release RX pull up on PD0
	_delay_ms(63);			// Pause while satellite wakes up
							// and pull-ups have time to rise.
							// Tweak until bind pulses about 68ms after power-up		
		
	// Bind as master if any single button pressed.
	// NB: Have to wait until the button pull-ups rise before testing for a button press.
	// Button 1
	if ((PINB & 0xf0) == 0x70)
	{
		DDRD	= 0xF3;		// Switch PD0 to output
		bind_master(3);
		
	}
	// Button 2	
	if ((PINB & 0xf0) == 0xb0)
	{
		DDRD	= 0xF3;		// Switch PD0 to output
		bind_master(5);
	}
	// Button 3	
	if ((PINB & 0xf0) == 0xd0)
	{
		DDRD	= 0xF3;		// Switch PD0 to output
		bind_master(7);
	}
	
	// Button 4
	if ((PINB & 0xf0) == 0xE0)
	{
		DDRD	= 0xF3;		// Switch PD0 to output
		bind_master(9);
	}
	
	DDRD	= 0xF2;			// Reset Port D directions
	PIND	= 0x0D;			// Set PD pull-ups (now pull up RX as well)

	//***********************************************************
	// Timers
	//***********************************************************

	// Timer0 (8bit) - run @ 20MHz / 1024 = 19.531kHz or 51.2us - max 13.1ms
	// Slow timer to extend Timer 1
	TCCR0A = 0;								// Normal operation
	TCCR0B = 0x05;							// Clk / 1024 = 19.531kHz or 51.2us - max 13.1ms
	TIMSK0 |= (1 << TOIE0);					// Enable interrupts
	TCNT0 = 0;								// Reset counter
	
	// Timer1 (16bit) - run @ 2.5MHz (400ns) - max 26.2ms
	// Used to measure Rx Signals & control ESC/servo output rate
	TCCR1A = 0;
	TCCR1B |= (1 << CS11);					// Clk/8 = 2.5MHz

	// Timer2 8bit - run @ 20MHz / 1024 = 19.531kHz or 51.2us - max 13.1ms
	// Used to time arm/disarm intervals
	TCCR2A = 0;	
	TCCR2B = 0x07;							// Clk/1024 = 19.531kHz
	TIMSK2 = 0;
	TIFR2 = 0;
	TCNT2 = 0;								// Reset counter

	//***********************************************************
	// Interrupts and pin function setup
	//***********************************************************

	// Pin change interrupt enables PCINT1, PCINT2 and PCINT3 (Throttle, AUX and CPPM input)
	PCICR  = 0x0A;							// PCINT8  to PCINT15 (PCINT1 group - AUX)
											// PCINT24 to PCINT31 (PCINT3 group - THR)
	PCIFR  = 0x0F;							// Clear PCIF0 interrupt flag 
											// Clear PCIF1 interrupt flag 
											// Clear PCIF2 interrupt flag 
											// Clear PCIF3 interrupt flag 

	// External interrupts INT0 (Elevator) and INT1 (Aileron) and INT2 (Rudder)
	EICRA = 0x15;							// Any change INT0
											// Any change INT1
											// Any change INT2
	EIFR  = 0x07; 							// Clear INT0 interrupt flag (Elevator)
											// Clear INT1 interrupt flag (Aileron)
											// Clear INT2 interrupt flag (Rudder/CPPM)

	//***********************************************************
	// Start up
	//***********************************************************

	// Preset important flags
	Interrupted = false;						

	// Load EEPROM settings
	updated = Initial_EEPROM_Config_Load(); // Config now contains valid values

	//***********************************************************
	// RX channel defaults for when no RC connected
	// Not doing this can result in the FC trying (unsuccessfully) to arm
	// and makes entry into the menus very hard
	//***********************************************************

	for (i = 0; i < MAX_RC_CHANNELS; i++)
	{
		RxChannel[i] = 3750;
	}
	
	RxChannel[THROTTLE] = 2500; // Min throttle
	
	//***********************************************************
	// GLCD initialisation
	//***********************************************************

	// Initialise the GLCD
	st7565_init();

	// Make sure the LCD is blank without clearing buffer (and so no logo)
	clear_screen();

	//***********************************************************
	// ESC calibration
	//***********************************************************
	
	// Calibrate ESCs if ONLY buttons 1 and 4 pressed
	if ((PINB & 0xf0) == 0x60)
	{
		// Display calibrating message
		st7565_command(CMD_SET_COM_NORMAL); 	// For text (not for logo)
		clear_buffer(buffer);
		LCD_Display_Text(59,(const unsigned char*)Verdana14,10,25);
		write_buffer(buffer);
		clear_buffer(buffer);
				
		// For each output
		for (i = 0; i < MAX_OUTPUTS; i++)
		{
			// Check for motor marker
			if (Config.Channel[i].Motor_marker == MOTOR)
			{
				// Set output to maximum pulse width
				ServoOut[i] = MOTOR_100;
			}
			else
			{
				ServoOut[i] = SERVO_CENTER;
			}
		}
					
		// Output HIGH pulse (1.9ms) until buttons released
		while ((PINB & 0xf0) == 0x60)
		{
			// Pass address of ServoOut array and select all outputs
			output_servo_ppm_asm(&ServoOut[0], 0xFF);

			// Loop rate = 20ms (50Hz)
			_delay_ms(20);			
		}

		// Output LOW pulse (1.1ms) after buttons released
		// For each output
		for (i = 0; i < MAX_OUTPUTS; i++)
		{
			// Check for motor marker
			if (Config.Channel[i].Motor_marker == MOTOR)
			{
				// Set output to maximum pulse width
				ServoOut[i] = MOTOR_0;
			}
		}		

		// Loop forever here
		while(1)
		{
			// Pass address of ServoOut array and select all outputs
			output_servo_ppm_asm(&ServoOut[0], 0xFF);

			// Loop rate = 20ms (50Hz)
			_delay_ms(20);			
		}
	}

	//***********************************************************
	// Reset EEPROM settings
	//***********************************************************

	// This delay prevents the GLCD flashing up a ghost image of old data
	_delay_ms(300);

	// Reload default eeprom settings if middle two buttons are pressed
	if ((PINB & 0xf0) == 0x90)
	{
		// Display reset message
		st7565_command(CMD_SET_COM_NORMAL); 	// For text (not for logo)
		clear_buffer(buffer);
		LCD_Display_Text(262,(const unsigned char*)Verdana14,40,25); // "Reset"
		write_buffer(buffer);
		clear_buffer(buffer);
		
		// Reset EEPROM settings
		Set_EEPROM_Default_Config();
		Save_Config_to_EEPROM();

		// Set contrast to the default value
		st7565_set_brightness(Config.Contrast);

		_delay_ms(500);		// Save is now too fast to show the "Reset" text long enough
	}

	// Display message in place of logo when updating eeprom structure
	if (updated)
	{
		st7565_command(CMD_SET_COM_NORMAL); 	// For text (not for logo)
		clear_buffer(buffer);
		LCD_Display_Text(259,(const unsigned char*)Verdana14,30,13); // "Updating"
		LCD_Display_Text(260,(const unsigned char*)Verdana14,33,37); // "settings"
		write_buffer(buffer);
		clear_buffer(buffer);		
		_delay_ms(1000);	
	}
	else
	{
		// Write logo from buffer
		write_buffer(buffer);
		_delay_ms(1000);
	}

	clear_buffer(buffer);
	write_buffer(buffer);
	
	st7565_init(); // Seems necessary for KK2 mini
	
	//***********************************************************
	// i2c init
	//***********************************************************	

	i2c_init();
	init_i2c_gyros();
	init_i2c_accs();

	//***********************************************************
	// Remaining init tasks
	//***********************************************************

	// Display "Hold steady" message
	clear_buffer(buffer);
	st7565_command(CMD_SET_COM_NORMAL); 	// For text (not for logo)
	LCD_Display_Text(263,(const unsigned char*)Verdana14,18,25);	// "Hold steady"
	write_buffer(buffer);	
	clear_buffer(buffer);
		
	// Do startup tasks
	Init_ADC();
	init_int();								// Initialise interrupts based on RC input mode
	init_uart();							// Initialise UART

	// Initial gyro calibration
	if (!CalibrateGyrosSlow())
	{
		clear_buffer(buffer);
		LCD_Display_Text(61,(const unsigned char*)Verdana14,25,25); // "Cal. failed"
		write_buffer(buffer);
		_delay_ms(1000);
		
		// Reset
		cli();
		wdt_enable(WDTO_15MS);				// Watchdog on, 15ms
		while(1);							// Wait for reboot
	}

	// Update voltage detection
	SystemVoltage = GetVbat();				// Check power-up battery voltage
	UpdateLimits();							// Update travel and trigger limits

	// Disarm on start-up if Armed setting is ARMABLE
	if (Config.ArmMode == ARMABLE)
	{
		General_error |= (1 << DISARMED); 	// Set disarmed bit
	}

	// Check to see that throttle is low if RC detected
	if (Interrupted)
	{
		RxGetChannels();
		if (MonopolarThrottle > THROTTLEIDLE) // THROTTLEIDLE = 50
		{
			General_error |= (1 << THROTTLE_HIGH); 	// Set throttle high error bit
		}
	}

	// Reset IMU
	reset_IMU();

	// Beep that init is complete
	LVA = 1;
	_delay_ms(25);
	LVA = 0;

#ifdef ERROR_LOG	
	// Log reboot
	add_log(REBOOT);
#endif
} // init()
Example #30
0
static int tegra_i2c_send_recv(int bus, int read,
			       uint32_t *headers, int header_words,
			       uint8_t *data, int data_len)
{
	struct tegra_i2c_bus_info *info = &tegra_i2c_info[bus];
	struct tegra_i2c_regs * const regs = info->base;

	while (data_len) {
		uint32_t status = read32(&regs->fifo_status);
		int tx_empty = status & I2C_FIFO_STATUS_TX_FIFO_EMPTY_CNT_MASK;
		tx_empty >>= I2C_FIFO_STATUS_TX_FIFO_EMPTY_CNT_SHIFT;
		int rx_full = status & I2C_FIFO_STATUS_RX_FIFO_FULL_CNT_MASK;
		rx_full >>= I2C_FIFO_STATUS_RX_FIFO_FULL_CNT_SHIFT;

		while (header_words && tx_empty) {
			write32(&regs->tx_packet_fifo, *headers++);
			header_words--;
			tx_empty--;
		}

		if (!header_words) {
			if (read) {
				while (data_len && rx_full) {
					uint32_t word = read32(&regs->rx_fifo);
					int todo = MIN(data_len, sizeof(word));

					memcpy(data, &word, todo);
					data_len -= todo;
					data += sizeof(word);
					rx_full--;
				}
			} else {
				while (data_len && tx_empty) {
					uint32_t word;
					int todo = MIN(data_len, sizeof(word));

					memcpy(&word, data, todo);
					write32(&regs->tx_packet_fifo, word);
					data_len -= todo;
					data += sizeof(word);
					tx_empty--;
				}
			}
		}

		uint32_t transfer_status =
			read32(&regs->packet_transfer_status);

		if (transfer_status & I2C_PKT_STATUS_NOACK_ADDR) {
			printk(BIOS_ERR,
			       "%s: The address was not acknowledged.\n",
			       __func__);
			info->reset_func(info->reset_bit);
			i2c_init(bus);
			return -1;
		} else if (transfer_status & I2C_PKT_STATUS_NOACK_DATA) {
			printk(BIOS_ERR,
			       "%s: The data was not acknowledged.\n",
			       __func__);
			info->reset_func(info->reset_bit);
			i2c_init(bus);
			return -1;
		} else if (transfer_status & I2C_PKT_STATUS_ARB_LOST) {
			printk(BIOS_ERR,
			       "%s: Lost arbitration.\n",
			       __func__);
			info->reset_func(info->reset_bit);

			/* Use Tegra bus clear registers to unlock SDA */
			do_bus_clear(bus);

			/* re-init i2c controller */
			i2c_init(bus);

			/* Return w/error, let caller decide what to do */
			return -1;
		}
	}

	return 0;
}