void i2c_write_all(unsigned char COMMAND[]){ //{"SUP:CLOCk OFF\n"}; unsigned int i = 0; while(COMMAND[i]!='\0'){ i++; } i2c1_write(I2C_ADDR.g, COMMAND,i); i2c1_write(I2C_ADDR.p, COMMAND, i); //OS_Delay(250); OS_Delay(250); //i2c1_write(I2C_ADDR.p, COMMAND, i-1); //OS_Delay(250); OS_Delay(250); //i2c1_write(I2C_ADDR.b, COMMAND, i-1); //OS_Delay(250); OS_Delay(250); }
/* ----------------------------------------------------------------------------* * * Function Name : setMode(uint8_t mode, uint8_t enable) * * Description :Enables or disables a feature in the APDS-9960 * * Input : mode which feature to enable,enable ON (1) or OFF (0) * * Output : None * * Return : True if operation success. False otherwise. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int setMode(uint8_t mode, uint8_t enable) { uint8_t reg_val; /* Read current ENABLE register */ reg_val = getMode(); if( reg_val == ERROR ) { return false; } /* Change bit(s) in ENABLE register */ enable = enable & 0x01; if( mode >= 0 && mode <= 6 ) { if (enable) { reg_val |= (1 << mode); } else { reg_val &= ~(1 << mode); } } else if( mode == ALL ) { if (enable) { reg_val = 0x7F; } else { reg_val = 0x00; } } /* Write value back to ENABLE register */ if( !i2c1_write(APDS9960_ENABLE, reg_val) ) { return false; } return true; }/* End of this function */
/****************************************************************************** **** **** ** ** gps_pow_off() gps_pow_on() ** ** **** **** ******************************************************************************/ void gps_pow_off(unsigned int show) { i2c1_write(I2C_ADDR.g, STR_POW_OFF, sizeof(STR_POW_OFF)-1); if(show) { user_debug_msg(STR_TASK_GPS_QA "Verify: TP41 (+5V_GPS) is 0V."); user_debug_msg(STR_TASK_GPS_QA "Verify: TP42 (3V3_GPS) is 0V."); } } /* gps_pow_off() */
/* ----------------------------------------------------------------------------* * * Function Name : setProxIntHighThresh(uint8_t threshold) * * Description :Sets the high threshold for proximity detection * * Input : threshold the high proximity threshold * * Output : None * * Return : True if operation successful. False otherwise. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int setProxIntHighThresh(uint8_t threshold) { if( !i2c1_write(APDS9960_PIHT, threshold) ) { return false; } return true; }/* End of this function */
/* ----------------------------------------------------------------------------* * * Function Name : enableGestureSensor(int interrupts) * * Description :Starts the gesture recognition engine on the APDS-9960 * * Input : interrupts true to enable hardware external interrupt on gesture * * Output : None * * Return : True if engine enabled correctly. False on error. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int enableGestureSensor(int interrupts) { /* Enable gesture mode Set ENABLE to 0 (power off) Set WTIME to 0xFF Set AUX to LED_BOOST_300 Enable PON, WEN, PEN, GEN in ENABLE */ resetGestureParameters(); if( !i2c1_write(APDS9960_WTIME, 0xFF) ) { return false; } if( !i2c1_write(APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) ) { return false; } if( !setLEDBoost(LED_BOOST_300) ) { return false; } if( interrupts ) { if( !setGestureIntEnable(1) ) { return false; } } else { if( !setGestureIntEnable(0) ) { return false; } } if( !setGestureMode(1) ) { return false; } if( !enablePower() ){ return false; } if( !setMode(WAIT, 1) ) { return false; } if( !setMode(PROXIMITY, 1) ) { return false; } if( !setMode(GESTURE, 1) ) { return false; } return true; }/* End of this function */
/* ----------------------------------------------------------------------------* * * Function Name : setGestureEnterThresh(uint8_t threshold) * * Description :Sets the entry proximity threshold for gesture sensing * * Input : threshold proximity value needed to start gesture mode * * Output : None * * Return : True if operation successful. False otherwise. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int setGestureEnterThresh(uint8_t threshold) { if( !i2c1_write(APDS9960_GPENTH, threshold) ) { return false; } return true; }/* End of this function */
void sup_clk_on(unsigned int show, unsigned int divider) { sprintf(STR_CLK_ON+14, "%2d\n", divider); i2c1_write(I2C_ADDR, STR_CLK_ON, sizeof(STR_CLK_ON)-1); if(show) { sprintf(strTmp, STR_TASK_SUPMCU_QA "Verify: CLK Out (J6) at ca. %7ldHz (div=%2d)", MCLK/power(2,divider), divider); user_debug_msg(strTmp); } } /* gps_sup_clk_on() */
// return: 0 if success, otherwise failed int cs8556_i2c_write(int addr, char data) { #ifdef CONFIG_KERNEL // i2c_api_register_write(CS8556_ADDR, 2, addr, data); vpp_i2c_write(CS8556_ID, CS8556_ADDR, addr, &data, 1); return 0; #else return i2c1_write(CS8556_ADDR, addr, 2, &data, 1); #endif }
/* ----------------------------------------------------------------------------* * * Function Name : setLightIntHighThreshold(uint16_t threshold) * * Description :Sets the high threshold for ambient light interrupts * * Input : threshold high threshold value for interrupt to trigger * * Output : None * * Return : True if operation successful. False otherwise. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int setLightIntHighThreshold(uint16_t threshold) { uint8_t val_low; uint8_t val_high; /* Break 16-bit threshold into 2 8-bit values */ val_low = threshold & 0x00FF; val_high = (threshold & 0xFF00) >> 8; /* Write low byte */ if( !i2c1_write(APDS9960_AIHTL, val_low) ) { return false; } /* Write high byte */ if( !i2c1_write(APDS9960_AIHTH, val_high) ) { return false; } return true; }/* End of this function */
/* ----------------------------------------------------------------------------* * * Function Name : setAmbientLightGain(uint8_t drive) * * Description :Sets the receiver gain for the ambient light sensor (ALS) * Value Gain * 0 1x * 1 4x * 2 16x * 3 64x * Input : drive the value (0-3) for the gain * * Output : None * * Return : True if operation successful. False otherwise. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int setAmbientLightGain(uint8_t drive) { uint8_t val; /* Read value from CONTROL register */ if( !i2c1_read(APDS9960_CONTROL, &val,1) ) { return false; } /* Set bits in register to given value */ drive &= 0x03; val &= 0xFC; val |= drive; /* Write register value back into CONTROL register */ if( !i2c1_write(APDS9960_CONTROL, val) ) { return false; } return true; }/* End of this function */
/* ----------------------------------------------------------------------------* * * Function Name : setGestureMode(uint8_t mode) * * Description :Tells the state machine to either enter or exit gesture state machine * * Input : mode 1 to enter gesture state machine, 0 to exit. * * Output : None * * Return : True if operation successful. False otherwise. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int setGestureMode(uint8_t mode) { uint8_t val; /* Read value from GCONF4 register */ if( !i2c1_read(APDS9960_GCONF4, &val,1) ) { return false; } /* Set bits in register to given value */ mode &= 0x01; val &= 0xFE; val |= mode; /* Write register value back into GCONF4 register */ if( !i2c1_write(APDS9960_GCONF4, val) ) { return false; } return true; }/* End of this function */
/* ----------------------------------------------------------------------------* * * Function Name : setGestureWaitTime(uint8_t time) * * Description :Sets the time in low power mode between gesture detections * Value Wait time * 0 0 ms * 1 2.8 ms * 2 5.6 ms * 3 8.4 ms * 4 14.0 ms * 5 22.4 ms * 6 30.8 ms * 7 39.2 ms * Input : the value for the wait time * * Output : None * * Return : True if operation successful. False otherwise. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int setGestureWaitTime(uint8_t time) { uint8_t val; /* Read value from GCONF2 register */ if( !i2c1_read(APDS9960_GCONF2, &val,1) ) { return false; } /* Set bits in register to given value */ time &= 0x07; val &= 0xF8; val |= time; /* Write register value back into GCONF2 register */ if( !i2c1_write(APDS9960_GCONF2, val) ) { return false; } return true; }/* End of this function */
/* ----------------------------------------------------------------------------* * * Function Name : setGestureIntEnable(uint8_t enable) * * Description :Turns gesture-related interrupts on or off * * Input : enable 1 to enable interrupts, 0 to turn them off * * Output : None * * Return : True if operation successful. False otherwise. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int setGestureIntEnable(uint8_t enable) { uint8_t val; /* Read value from GCONF4 register */ if( !i2c1_read(APDS9960_GCONF4, &val,1) ) { return false; } /* Set bits in register to given value */ enable &= 0x01; enable = enable << 1; val &= 0xFD; val |= enable; /* Write register value back into GCONF4 register */ if( !i2c1_write(APDS9960_GCONF4, val) ) { return false; } return true; }/* End of this function */
/* ----------------------------------------------------------------------------* * * Function Name : setLEDBoost(uint8_t boost) * * Description :Sets the LED current boost value * Value Boost Current * 0 100% * 1 150% * 2 200% * 3 300% * Input : drive the value (0-3) for current boost (100-300%) * * Output : None * * Return : True if operation successful. False otherwise. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int setLEDBoost(uint8_t boost) { uint8_t val; /* Read value from CONFIG2 register */ if( !i2c1_read(APDS9960_CONFIG2, &val,1) ) { return false; } /* Set bits in register to given value */ boost &= 0x03; boost = boost << 4; val &= 0xCF; val |= boost; /* Write register value back into CONFIG2 register */ if( !i2c1_write(APDS9960_CONFIG2, val) ) { return false; } return true; }/* End of this function */
/* ----------------------------------------------------------------------------* * * Function Name : setGestureGain(uint8_t gain) * * Description :Sets the gain of the photodiode during gesture mode * Value Gain * 0 1x * 1 2x * 2 4x * 3 8x * Input : gain the value for the photodiode gain * * Output : None * * Return : True if operation successful. False otherwise. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int setGestureGain(uint8_t gain) { uint8_t val; /* Read value from GCONF2 register */ if( !i2c1_read(APDS9960_GCONF2, &val,1) ) { return false; } /* Set bits in register to given value */ gain &= 0x03; gain = gain << 5; val &= 0x9F; val |= gain; /* Write register value back into GCONF2 register */ if( !i2c1_write(APDS9960_GCONF2, val) ) { return false; } return true; }/* End of this function */
/* ----------------------------------------------------------------------------* * * Function Name : setGestureLEDDrive(uint8_t drive) * * Description :Sets the LED drive current during gesture mode * Value LED Current * 0 100 mA * 1 50 mA * 2 25 mA * 3 12.5 mA * * Input : drive the value for the LED drive current * * Output : None * * Return : True if operation successful. False otherwise. * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int setGestureLEDDrive(uint8_t drive) { uint8_t val; /* Read value from GCONF2 register */ if( !i2c1_read(APDS9960_GCONF2, &val,1) ) { return false; } /* Set bits in register to given value */ drive &= 0x03; drive = drive << 3; val &= 0xE7; val |= drive; /* Write register value back into GCONF2 register */ if( !i2c1_write(APDS9960_GCONF2, val) ) { return false; } return true; }/* End of this function */
void sup_led_flash(unsigned int show) { i2c1_write(I2C_ADDR, STR_LED_FLASH, sizeof(STR_LED_FLASH)-1); if(show) { user_debug_msg(STR_TASK_SUPMCU_QA "Verify: Status LED (D2) is flashing."); } } /* gps_led_flash() */
void sup_led_on(unsigned int show) { i2c1_write(I2C_ADDR, STR_LED_ON, sizeof(STR_LED_OFF)-1); if(show) { user_debug_msg(STR_TASK_SUPMCU_QA "Verify: Status LED (D2) is ON."); } } /* gps_led_on() */
/****************************************************************************** **** **** ** ** gps_res_off() gps_res_on() ** ** **** **** ******************************************************************************/ void gps_res_off(unsigned int show) { i2c1_write(I2C_ADDR.g, STR_RES_OFF, sizeof(STR_RES_OFF)-1); if(show) { user_debug_msg(STR_TASK_GPS_QA "OEM615 -RESET is inactive (i.e., HIGH)."); } } /* gps_res_off() */
/****************************************************************************** **** **** ** ** sup_clk_off() sup_clk_on(divider) ** ** **** **** ******************************************************************************/ void sup_clk_off(unsigned int show) { i2c1_write(I2C_ADDR, STR_CLK_OFF, sizeof(STR_CLK_OFF)-1); if(show) { user_debug_msg(STR_TASK_SUPMCU_QA "Verify: CLK Out (J6) is at 0Hz (off)"); } } /* gps_sup_clk_off() */
void gps_res_on(unsigned int show) { i2c1_write(I2C_ADDR.g, STR_RES_ON, sizeof(STR_RES_ON)-1); if(show) { user_debug_msg(STR_TASK_GPS_QA "OEM615 -RESET is active (i.e., LOW)."); } } /* gps_res_on() */
void gps_pass_on(unsigned int show) { i2c1_write(I2C_ADDR.g, STR_PASS_ON, sizeof(STR_PASS_ON)-1); if(show) { user_debug_msg(STR_TASK_GPS_QA "OEM615 passthrough is enabled."); } } /* gps_pass_on() */
/****************************************************************************** **** **** ** ** gps_log_off() gps_log_gga() ** ** **** **** ******************************************************************************/ void gps_log_off(unsigned int show) { i2c1_write(I2C_ADDR.g, STR_LOG_OFF, sizeof(STR_LOG_OFF)-1); if(show) { user_debug_msg(STR_TASK_GPS_QA "OEM615 logging is disabled."); } } /* gps_log_off() */
void gps_log_gga(unsigned int show) { i2c1_write(I2C_ADDR.g, STR_LOG_GGA, sizeof(STR_LOG_GGA)-1); if(show) { user_debug_msg(STR_TASK_GPS_QA "OEM615 logging of GGA is enabled."); } } /* gps_log_gga() */
/* ----------------------------------------------------------------------------* * * Function Name : apds9960init * * Description : Configures I2C communications and initializes registers to defaults * * Input : None * * Output : None * * Return : True if initialized successfully. False otherwise * ----------------------------------------------------------------------------* * Authors: Sarath S * Date: May 17, 2017 * ---------------------------------------------------------------------------*/ int apds9960init(void) { uint8_t id; //ledSetLeftLed(LED_ON); /* Initialize I2C */ I2C1_init(); delayms(700); /* Read ID register and check against known values for APDS-9960 */ if( !i2c1_read(APDS9960_ID, &id,1) ) { ledSetRightLed(LED_ON); return false; } ledSetLeftLed(LED_ON); if( !(id == APDS9960_ID_1 || id == APDS9960_ID_2) ) { return false; } /* Set ENABLE register to 0 (disable all features) */ if( !setMode(ALL, OFF) ) { return false; } /* Set default values for ambient light and proximity registers */ if( !i2c1_write(APDS9960_ATIME, DEFAULT_ATIME) ) { return false; } if( !i2c1_write(APDS9960_WTIME, DEFAULT_WTIME) ) { return false; } if( !i2c1_write(APDS9960_PPULSE, DEFAULT_PROX_PPULSE) ) { return false; } if( !i2c1_write(APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR) ) { return false; } if( !i2c1_write(APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL) ) { return false; } if( !i2c1_write(APDS9960_CONFIG1, DEFAULT_CONFIG1) ) { return false; } if( !setLEDDrive(DEFAULT_LDRIVE) ) { return false; } if( !setProximityGain(DEFAULT_PGAIN) ) { return false; } if( !setAmbientLightGain(DEFAULT_AGAIN) ) { return false; } if( !setProxIntLowThresh(DEFAULT_PILT) ) { return false; } if( !setProxIntHighThresh(DEFAULT_PIHT) ) { return false; } if( !setLightIntLowThreshold(DEFAULT_AILT) ) { return false; } if( !setLightIntHighThreshold(DEFAULT_AIHT) ) { return false; } if( !i2c1_write(APDS9960_PERS, DEFAULT_PERS) ) { return false; } if( !i2c1_write(APDS9960_CONFIG2, DEFAULT_CONFIG2) ) { return false; } if( !i2c1_write(APDS9960_CONFIG3, DEFAULT_CONFIG3) ) { return false; } /* Set default values for gesture sense registers */ if( !setGestureEnterThresh(DEFAULT_GPENTH) ) { return false; } if( !setGestureExitThresh(DEFAULT_GEXTH) ) { return false; } if( !i2c1_write(APDS9960_GCONF1, DEFAULT_GCONF1) ) { return false; } if( !setGestureGain(DEFAULT_GGAIN) ) { return false; } if( !setGestureLEDDrive(DEFAULT_GLDRIVE) ) { return false; } if( !setGestureWaitTime(DEFAULT_GWTIME) ) { return false; } if( !i2c1_write(APDS9960_GOFFSET_U, DEFAULT_GOFFSET) ) { return false; } if( !i2c1_write(APDS9960_GOFFSET_D, DEFAULT_GOFFSET) ) { return false; } if( !i2c1_write(APDS9960_GOFFSET_L, DEFAULT_GOFFSET) ) { return false; } if( !i2c1_write(APDS9960_GOFFSET_R, DEFAULT_GOFFSET) ) { return false; } if( !i2c1_write(APDS9960_GPULSE, DEFAULT_GPULSE) ) { return false; } if( !i2c1_write(APDS9960_GCONF3, DEFAULT_GCONF3) ) { return false; } if( !setGestureIntEnable(DEFAULT_GIEN) ) { return false; } #if 0 /* Gesture config register dump */ uint8_t reg; uint8_t val; for(reg = 0x80; reg <= 0xAF; reg++) { if( (reg != 0x82) && \ (reg != 0x8A) && \ (reg != 0x91) && \ (reg != 0xA8) && \ (reg != 0xAC) && \ (reg != 0xAD) ) { i2c1_read(reg, val,1); //debugPutChar(reg); debugPutString(": 0x"); //debugPutChar(val); } } for(reg = 0xE4; reg <= 0xE7; reg++) { i2c1_read(reg, val,1); //debugPutChar(reg); debugPutString(": 0x"); //debugPutChar(val); } #endif return true; }/* End of this function */
void supmcu_selftest_tel(unsigned int show) { i2c1_write(I2C_ADDR.g, STR_SELFTEST_TEL, sizeof(STR_SELFTEST_TEL)-1); if(show) { user_debug_msg(STR_TELEMETRY_QA "Getting Selftest results."); } }
void gps_led_gps(unsigned int show) { i2c1_write(I2C_ADDR.g, STR_LED_GPS, sizeof(STR_LED_GPS)-1); if(show) { user_debug_msg(STR_TASK_GPS_QA "GPSRM LED (D2) now indicates power to GPS."); } } /* gps_led_gps() */
void pim_port4_dis(unsigned int show) { i2c1_write(I2C_ADDR, STR_PORT4_OFF, sizeof(STR_PORT4_OFF)-1); if(show) { user_debug_msg(STR_TASK_PIM_QA "PORT4 is turned OFF."); } }
void supmcu_selftest_start(unsigned int show) { i2c1_write(I2C_ADDR.g, STR_SELFTEST_START, sizeof(STR_SELFTEST_START)-1); if(show) { user_debug_msg(STR_TELEMETRY_QA "Starting selftests. Please wait until all 6 tests are run."); } }