/** function loads calibration table from EEPROM, validate it and if OK uses it */ static void touch_LoadCalibration(void) { I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT; uint32_t temp, checksum; int count; MATRIX new_matrix; /* Initialize I2C driver, using standard rate. Devices on DK itself */ /* supports fast mode, but in case some slower devices are added on */ /* prototype board, we use standard mode. */ I2CDRV_Init(&i2cInit); count = EEPROM_Read(I2C0, EEPROM_DVK_ADDR, CALIBRATION_EEPROM_OFFSET, (uint8_t*) &temp, sizeof(temp)); count += EEPROM_Read(I2C0, EEPROM_DVK_ADDR, CALIBRATION_EEPROM_OFFSET + 4, (uint8_t*) &new_matrix, sizeof(new_matrix)); if (count == sizeof(new_matrix) + 4) { if (temp == CALIBRATION_MAGIC_NUMBER) { checksum = touch_CountChecksum(temp, (uint32_t*) &new_matrix, sizeof(new_matrix) / 4); count = EEPROM_Read(I2C0, EEPROM_DVK_ADDR, CALIBRATION_EEPROM_OFFSET + 4 + sizeof(new_matrix), (uint8_t*) &temp, sizeof(temp)); if (temp == checksum) { /* looks like calibration table is valid */ ADC_IntDisable(ADC0, ADC_IF_SINGLE); /* we need to disable ADC interrupt to avoid current_pos structure update for a while */ memcpy(&calibrationMatrix, &new_matrix, sizeof(calibrationMatrix)); ADC_IntEnable(ADC0, ADC_IF_SINGLE); } } } }
/**************************************************************************//** * @brief Main function *****************************************************************************/ int main(void) { I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT; TEMPSENS_Temp_TypeDef temp; /* Define previous temp to invalid, just to ensure update first time */ TEMPSENS_Temp_TypeDef prevTemp = {1000, 0}; int prevShowFahrenheit = showFahrenheit; /* Initialize DK board register access */ BSP_Init(BSP_INIT_DEFAULT); /* If first word of user data page is non-zero, enable eA Profiler trace */ BSP_TraceProfilerSetup(); /* Initialize TFT */ RETARGET_SerialInit(); printf("\nEFM32 I2C temperature sensor example\n\n"); /* Enable board control interrupts */ BSP_InterruptDisable(0xffff); BSP_InterruptFlagsClear(0xffff); BSP_InterruptEnable(BC_INTEN_JOYSTICK); temperatureIRQInit(); /* Initialize I2C driver, using standard rate. Devices on DK itself */ /* supports fast mode, but in case some slower devices are added on */ /* prototype board, we use standard mode. */ I2CDRV_Init(&i2cInit); /* Main loop - just read temperature and update LCD */ while (1) { if (TEMPSENS_TemperatureGet(I2C0, TEMPSENS_DK_ADDR, &temp) < 0) { printf("ERROR\n"); /* Enter EM2, no wakeup scheduled */ EMU_EnterEM2(true); } /* Update LCD display if any change. This is just an example of how */ /* to save some energy, since the temperature normally is quite static. */ /* The compare overhead is much smaller than actually updating the display. */ if ((prevTemp.i != temp.i) || (prevTemp.f != temp.f) || (prevShowFahrenheit != showFahrenheit)) { temperatureUpdate(&temp); } prevTemp = temp; prevShowFahrenheit = showFahrenheit; /* Read every 2 seconds which is more than it takes worstcase to */ /* finish measurement inside sensor. */ RTCDRV_Trigger(2000, NULL); EMU_EnterEM2(true); } }
/**************************************************************************//** * @brief Main function *****************************************************************************/ int main(void) { I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT; uint8_t data[3]; /* Initialize DK board register access */ BSP_Init(BSP_INIT_DEFAULT); /* If first word of user data page is non-zero, enable eA Profiler trace */ BSP_TraceProfilerSetup(); /* Initialize TFT */ RETARGET_SerialInit(); printf("\nEFM32 I2C EEPROM example\n\n"); /* Enable board control interrupts */ BSP_InterruptDisable(0xffff); BSP_InterruptFlagsClear(0xffff); BSP_InterruptEnable(BC_INTEN_JOYSTICK); BSP_InterruptEnable(BC_INTEN_PB); eepromtestIRQInit(); /* Initialize I2C driver, using standard rate. Devices on DK itself */ /* supports fast mode, but in case some slower devices are added on */ /* prototype board, we use standard mode. */ I2CDRV_Init(&i2cInit); /* Main loop - just read data and update LCD */ while (1) { /* Should data be reset to factory default? */ if (eepromReset) { eepromReset = false; /* Data changed by user? */ data[0] = 0xFF; data[1] = 0xFF; data[2] = 0xFF; if (EEPROM_Write(I2C0, EEPROM_DVK_ADDR, 0, data, 3) < 0) { printf("RST ERR\n"); /* Enter EM2, no wakeup scheduled */ EMU_EnterEM2(true); } } if (EEPROM_Read(I2C0, EEPROM_DVK_ADDR, 0, data, 3) < 0) { printf("RD ERR\n"); /* Enter EM2, no wakeup scheduled */ EMU_EnterEM2(true); } eepromData = data[0]; eepromtestUpdateLCD(data); /* Just enter EM2 until joystick pressed */ EMU_EnterEM2(true); /* Data changed by user? */ if (eepromData != data[0]) { data[0] = eepromData; data[1] = eepromData + 1; data[2] = eepromData + 2; if (EEPROM_Write(I2C0, EEPROM_DVK_ADDR, 0, data, 3) < 0) { printf("WR ERR\n"); /* Enter EM2, no wakeup scheduled */ EMU_EnterEM2(true); } } } }