//! Main C entry point //! //! Assumes ramBuffer is set up beforehand //! void entry(void) { FlashData_t *flashData; // Handle on programming data // Disable COP *(gFlashProgramHeader.soptAddress+0x0A) = 0x00; // Handle on programming data flashData = gFlashProgramHeader.flashData; // Indicate not complete flashData->flags &= ~IS_COMPLETE; // No errors so far flashData->errorCode = FLASH_ERR_OK; if (flashData->controller == NULL) { setErrorCode(FLASH_ERR_ILLEGAL_PARAMS); } // Clear invalid/unused address bits // A23 is used for Flash block number flashData->address &= 0x008FFFFFUL; initFlash(flashData); eraseFlashBlock(flashData); programPartition(flashData) ; eraseRange(flashData); blankCheckRange(flashData); programRange(flashData); verifyRange(flashData); // Indicate completed setErrorCode(FLASH_ERR_OK); }
void wcycle_init () { initPins (); initClocks (); initFlash (); initUART (); initPWM (); initDHT (); wcycle_pwm_ctl (readFlash()); }
//Init function void ICACHE_FLASH_ATTR user_init() { //uart_init(BIT_RATE_115200,BIT_RATE_115200); flashData = &_flashData; IPStation = ipstation; stdoutInit(); char ssid[32] = SSID; char password[64] = SSID_PASSWORD; struct station_config stationConf; os_printf("\ninit\n"); user_init_gpio(); //Set station mode & AP mode // wifi_set_opmode(STATION_MODE); wifi_set_opmode(STATIONAP_MODE); initFlash(); flash_read(); //if (flashData->magic != MAGIC_NUM) { //Set ap settings os_memcpy(&stationConf.ssid, ssid, 32); os_memcpy(&stationConf.password, password, 64); } // else // { // os_memcpy(&stationConf.ssid, flashData->ssid, 32); // os_memcpy(&stationConf.password, flashData->password, 64); // // } wifi_station_set_config(&stationConf); os_printf("\nConnecting to %s, %s\n", stationConf.ssid, stationConf.password); SetSetverMode(); //Start os task system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen); system_os_post(user_procTaskPrio, 0, 0 ); os_printf("\nInitializing Network\n"); network_init(); }
void ICACHE_FLASH_ATTR flash_read() { os_printf("flashRead() size-%d\n", sizeof(FlashData)); flashData->magic =0; ETS_UART_INTR_DISABLE(); spi_flash_read((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE, (uint32 *)flashData, sizeof(FlashData)); ETS_UART_INTR_ENABLE(); if (flashData->magic != MAGIC_NUM) { os_printf("ReadFlash ERROR!\n"); initFlash(); } }
/** @brief The main top level init * * The main init function to be called from main.c before entering the main * loop. This function is simply a delegator to the finer grained special * purpose init functions. * * @author Fred Cooke */ void init(){ ATOMIC_START(); /* Disable ALL interrupts while we configure the board ready for use */ initPLL(); /* Set up the PLL and use it */ initIO(); /* TODO make this config dependent. Set up all the pins and modules to be in low power harmless states */ initAllPagedRAM(); /* Copy table and config blocks of data from flash to the paged ram blocks for fast data lookup */ initAllPagedAddresses(); /* Save the paged memory addresses to variables such that we can access them from another paged block with no warnings */ initVariables(); /* Initialise the rest of the running variables etc */ initFlash(); /* TODO, finalise this */ initECTTimer(); /* TODO move this to inside config in an organised way. Set up the timer module and its various aspects */ initPITTimer(); /* TODO ditto... */ initSCIStuff(); /* Setup the sci module(s) that we will use. */ initConfiguration(); /* TODO Set user/feature/config up here! */ initInterrupts(); /* still last, reset timers, enable interrupts here TODO move this to inside config in an organised way. Set up the rest of the individual interrupts */ ATOMIC_END(); /* Re-enable any configured interrupts */ }
void init() { disableWatchDog(); initClocks(); //Copy RAM functions memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize); //Setup flash waitstates initFlash(); initPie(); analogInit(); EINT; }
/** @brief The main top level init * * The main init function to be called from main.c before entering the main * loop. This function is simply a delegator to the finer grained special * purpose init functions. */ void init(){ ATOMIC_START(); /* Disable ALL interrupts while we configure the board ready for use */ initPLL(); /* Set up the PLL and use it */ initGPIO(); initPWM(); initADC(); initAllPagedRAM(); /* Copy table and config blocks of data from flash to the paged RAM blocks for fast data lookup */ initVariables(); /* Initialise the rest of the running variables etc */ initFlash(); /* TODO, finalise this */ initECTTimer(); /* TODO move this to inside config in an organised way. Set up the timer module and its various aspects */ // initPITTimer(); /* TODO ditto... */ initSCIStuff(); /* Setup the sci module(s) that we will use. */ initConfiguration(); /* TODO Set user/feature/config up here! */ #ifdef XGATE initXgate(); /* Fred is a legend, for good reason as of now */ #endif initInterrupts(); /* still last, reset timers, enable interrupts here TODO move this to inside config in an organised way. Set up the rest of the individual interrupts */ ATOMIC_END(); /* Re-enable any configured interrupts */ }
/** * Main C entry point * * Assumes ramBuffer is set up beforehand */ void entry(void) { // Set the interrupt vector table position SCB_VTOR = (uint32_t)__vector_table; // Handle on programming data FlashData_t *flashData = gFlashProgramHeader.flashData; initFlash(flashData); eraseFlashBlock(flashData); programPartition(flashData) ; eraseRange(flashData); blankCheckRange(flashData); programRange(flashData); verifyRange(flashData); #ifndef DEBUG // Indicate completed & stop setErrorCode(FLASH_ERR_OK); #endif }
/*! \fn main(void) * \brief Main function */ int main(void) { uint16_t current_bootkey_val = eeprom_read_word((uint16_t*)EEP_BOOTKEY_ADDR); RET_TYPE flash_init_result; RET_TYPE touch_init_result; RET_TYPE card_detect_ret; uint8_t fuse_ok = TRUE; // Disable JTAG to gain access to pins, set prescaler to 1 (fuses not set) #ifndef PRODUCTION_KICKSTARTER_SETUP disableJTAG(); CPU_PRESCALE(0); #endif // Check fuse settings: boot reset vector, 2k words, SPIEN, BOD 2.6V, programming & ver disabled if ((boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS) != 0xFF) || (boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS) != 0xD8) || (boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS) != 0xFB) || (boot_lock_fuse_bits_get(GET_LOCK_BITS) != 0xFC)) { fuse_ok = FALSE; } // Check if PB5 is low to start electrical test DDRB &= ~(1 << 5); PORTB |= (1 << 5); smallForLoopBasedDelay(); if (!(PINB & (1 << 5))) { // Test result, true by default uint8_t test_result = TRUE; // Leave flash nS off DDR_FLASH_nS |= (1 << PORTID_FLASH_nS); PORT_FLASH_nS |= (1 << PORTID_FLASH_nS); // Set PORTD as output, leave PORTID_OLED_SS high DDRD |= 0xFF; PORTD |= 0xFF; // All other pins are input by default, run our test for (uint8_t i = 0; i < 4; i++) { PORTD |= 0xFF; smallForLoopBasedDelay(); if (!(PINF & (0xC3)) || !(PINC & (1 << 6)) || !(PINE & (1 << 6)) || !(PINB & (1 << 4))) { test_result = FALSE; } PORTD &= (1 << PORTID_OLED_SS); smallForLoopBasedDelay(); if ((PINF & (0xC3)) || (PINC & (1 << 6)) || (PINE & (1 << 6)) || (PINB & (1 << 4))) { test_result = FALSE; } } // PB6 as test result output DDRB |= (1 << 6); // If test successful, light green LED if ((test_result == TRUE) && (fuse_ok == TRUE)) { PORTB |= (1 << 6); } else { PORTB &= ~(1 << 6); } while(1); } // This code will only be used for developers and beta testers #if !defined(PRODUCTION_SETUP) && !defined(PRODUCTION_KICKSTARTER_SETUP) // Check if we were reset and want to go to the bootloader if (current_bootkey_val == BOOTLOADER_BOOTKEY) { // Disable WDT wdt_reset(); wdt_clear_flag(); wdt_change_enable(); wdt_stop(); // Store correct bootkey eeprom_write_word((uint16_t*)EEP_BOOTKEY_ADDR, CORRECT_BOOTKEY); // Jump to bootloader start_bootloader(); } // Check if there was a change in the mooltipass setting storage to reset the parameters to their correct values if (getMooltipassParameterInEeprom(USER_PARAM_INIT_KEY_PARAM) != USER_PARAM_CORRECT_INIT_KEY) { mooltipassParametersInit(); setMooltipassParameterInEeprom(USER_PARAM_INIT_KEY_PARAM, USER_PARAM_CORRECT_INIT_KEY); } #endif // First time initializations for Eeprom (first boot at production or flash layout changes for beta testers) if (current_bootkey_val != CORRECT_BOOTKEY) { // Erase Mooltipass parameters mooltipassParametersInit(); // Set bootloader password bool to FALSE eeprom_write_byte((uint8_t*)EEP_BOOT_PWD_SET, FALSE); } /* Check if a card is inserted in the Mooltipass to go to the bootloader */ #ifdef AVR_BOOTLOADER_PROGRAMMING /* Disable JTAG to get access to the pins */ disableJTAG(); /* Init SMC port */ initPortSMC(); /* Delay for detection */ smallForLoopBasedDelay(); #if defined(HARDWARE_V1) if (PIN_SC_DET & (1 << PORTID_SC_DET)) #elif defined(HARDWARE_OLIVIER_V1) if (!(PIN_SC_DET & (1 << PORTID_SC_DET))) #endif { uint16_t tempuint16; /* What follows is a copy from firstDetectFunctionSMC() */ /* Enable power to the card */ PORT_SC_POW &= ~(1 << PORTID_SC_POW); /* Default state: PGM to 0 and RST to 1 */ PORT_SC_PGM &= ~(1 << PORTID_SC_PGM); DDR_SC_PGM |= (1 << PORTID_SC_PGM); PORT_SC_RST |= (1 << PORTID_SC_RST); DDR_SC_RST |= (1 << PORTID_SC_RST); /* Activate SPI port */ PORT_SPI_NATIVE &= ~((1 << SCK_SPI_NATIVE) | (1 << MOSI_SPI_NATIVE)); DDRB |= (1 << SCK_SPI_NATIVE) | (1 << MOSI_SPI_NATIVE); setSPIModeSMC(); /* Let the card come online */ smallForLoopBasedDelay(); /* Check smart card FZ */ readFabricationZone((uint8_t*)&tempuint16); if ((swap16(tempuint16)) != SMARTCARD_FABRICATION_ZONE) { removeFunctionSMC(); start_bootloader(); } else { removeFunctionSMC(); } } #endif initPortSMC(); // Initialize smart card port initPwm(); // Initialize PWM controller initIRQ(); // Initialize interrupts powerSettlingDelay(); // Let the power settle initUsb(); // Initialize USB controller powerSettlingDelay(); // Let the USB 3.3V LDO rise initI2cPort(); // Initialize I2C interface rngInit(); // Initialize avrentropy library oledInitIOs(); // Initialize OLED input/outputs spiUsartBegin(SPI_RATE_8_MHZ); // Start USART SPI at 8MHz // If offline mode isn't enabled, wait for device to be enumerated if (getMooltipassParameterInEeprom(OFFLINE_MODE_PARAM) == FALSE) { while(!isUsbConfigured()); // Wait for host to set configuration } // Set correct timeout_enabled val mp_timeout_enabled = getMooltipassParameterInEeprom(LOCK_TIMEOUT_ENABLE_PARAM); // Launch the before flash initialization tests #ifdef TESTS_ENABLED beforeFlashInitTests(); #endif // Check if we can initialize the Flash memory flash_init_result = initFlash(); // Launch the after flash initialization tests #ifdef TESTS_ENABLED afterFlashInitTests(); #endif // Set up OLED now that USB is receiving full 500mA. oledBegin(FONT_DEFAULT); // First time initializations for Flash (first time power up at production) if (current_bootkey_val != CORRECT_BOOTKEY) { // Erase everything non graphic in flash eraseFlashUsersContents(); // Erase # of cards and # of users firstTimeUserHandlingInit(); } // Check if we can initialize the touch sensing element touch_init_result = initTouchSensing(); // Enable proximity detection #ifndef HARDWARE_V1 activateProxDetection(); #endif // Launch the after touch initialization tests #ifdef TESTS_ENABLED afterTouchInitTests(); #endif // Test procedure to check that all HW is working #if defined(PRODUCTION_SETUP) || defined(PRODUCTION_KICKSTARTER_SETUP) if (current_bootkey_val != CORRECT_BOOTKEY) { RET_TYPE temp_rettype; // Wait for USB host to upload bundle, which then sets USER_PARAM_INIT_KEY_PARAM //#ifdef PRODUCTION_KICKSTARTER_SETUP while(getMooltipassParameterInEeprom(USER_PARAM_INIT_KEY_PARAM) != 0xF1) { usbProcessIncoming(USB_CALLER_MAIN); } //#endif // Bundle uploaded, start the screen oledBegin(FONT_DEFAULT); oledWriteActiveBuffer(); oledSetXY(0,0); // LEDs ON, to check setPwmDc(MAX_PWM_VAL); switchOnButtonWheelLeds(); guiDisplayRawString(ID_STRING_TEST_LEDS_CH); // Check flash init if (flash_init_result != RETURN_OK) { guiDisplayRawString(ID_STRING_TEST_FLASH_PB); } // Check touch init if (touch_init_result != RETURN_OK) { guiDisplayRawString(ID_STRING_TEST_TOUCH_PB); } // Touch instructions guiDisplayRawString(ID_STRING_TEST_INST_TCH); // Check prox while(!(touchDetectionRoutine(0) & RETURN_PROX_DETECTION)); guiDisplayRawString(ID_STRING_TEST_DET); activateGuardKey(); // Check left while(!(touchDetectionRoutine(0) & RETURN_LEFT_PRESSED)); guiDisplayRawString(ID_STRING_TEST_LEFT); // Check wheel while(!(touchDetectionRoutine(0) & RETURN_WHEEL_PRESSED)); guiDisplayRawString(ID_STRING_TEST_WHEEL); // Check right while(!(touchDetectionRoutine(0) & RETURN_RIGHT_PRESSED)); guiDisplayRawString(ID_STRING_TEST_RIGHT); // Insert card guiDisplayRawString(ID_STRING_TEST_CARD_INS); while(isCardPlugged() != RETURN_JDETECT); temp_rettype = cardDetectedRoutine(); // Check card if (!((temp_rettype == RETURN_MOOLTIPASS_BLANK) || (temp_rettype == RETURN_MOOLTIPASS_USER))) { guiDisplayRawString(ID_STRING_TEST_CARD_PB); } // Display result uint8_t script_return = RETURN_OK; if ((flash_init_result == RETURN_OK) && (touch_init_result == RETURN_OK) && ((temp_rettype == RETURN_MOOLTIPASS_BLANK) || (temp_rettype == RETURN_MOOLTIPASS_USER))) { // Inform script of success usbSendMessage(CMD_FUNCTIONAL_TEST_RES, 1, &script_return); // Wait for password to be set while(eeprom_read_byte((uint8_t*)EEP_BOOT_PWD_SET) != BOOTLOADER_PWDOK_KEY) { usbProcessIncoming(USB_CALLER_MAIN); } // Display test result guiDisplayRawString(ID_STRING_TEST_OK); timerBasedDelayMs(3000); } else { // Set correct bool script_return = RETURN_NOK; // Display test result guiDisplayRawString(ID_STRING_TEST_NOK); // Inform script of failure usbSendMessage(CMD_FUNCTIONAL_TEST_RES, 1, &script_return); while(1) { usbProcessIncoming(USB_CALLER_MAIN); } } } #endif // Stop the Mooltipass if we can't communicate with the flash or the touch interface #if defined(HARDWARE_OLIVIER_V1) #ifdef PRODUCTION_KICKSTARTER_SETUP while ((flash_init_result != RETURN_OK) || (touch_init_result != RETURN_OK) || (fuse_ok != TRUE)); #else while ((flash_init_result != RETURN_OK) || (touch_init_result != RETURN_OK)); #endif #endif // First time initializations done.... write correct value in eeprom if (current_bootkey_val != CORRECT_BOOTKEY) { // Store correct bootkey eeprom_write_word((uint16_t*)EEP_BOOTKEY_ADDR, CORRECT_BOOTKEY); } // Write inactive buffer & go to startup screen oledWriteInactiveBuffer(); guiSetCurrentScreen(SCREEN_DEFAULT_NINSERTED); guiGetBackToCurrentScreen(); // Launch the after HaD logo display tests #ifdef TESTS_ENABLED afterHadLogoDisplayTests(); #endif // Let's fade in the LEDs for (uint16_t i = 0; i < MAX_PWM_VAL; i++) { setPwmDc(i); timerBasedDelayMs(0); } activityDetectedRoutine(); launchCalibrationCycle(); touchClearCurrentDetections(); // Inhibit touch inputs for the first 3 seconds activateTimer(TIMER_TOUCH_INHIBIT, 3000); while (1) { // Process possible incoming USB packets usbProcessIncoming(USB_CALLER_MAIN); // Call GUI routine once the touch input inhibit timer is finished if (hasTimerExpired(TIMER_TOUCH_INHIBIT, FALSE) == TIMER_EXPIRED) { guiMainLoop(); } // Check if a card just got inserted / removed card_detect_ret = isCardPlugged(); // Do appropriate actions on smartcard insertion / removal if (card_detect_ret == RETURN_JDETECT) { // Light up the Mooltipass and call the dedicated function activityDetectedRoutine(); handleSmartcardInserted(); } else if (card_detect_ret == RETURN_JRELEASED) { // Light up the Mooltipass and call the dedicated function activityDetectedRoutine(); handleSmartcardRemoved(); // Set correct screen guiDisplayInformationOnScreen(ID_STRING_CARD_REMOVED); guiSetCurrentScreen(SCREEN_DEFAULT_NINSERTED); userViewDelay(); guiGetBackToCurrentScreen(); } // Two quick caps lock presses wakes up the device if ((hasTimerExpired(TIMER_CAPS, FALSE) == TIMER_EXPIRED) && (getKeyboardLeds() & HID_CAPS_MASK) && (wasCapsLockTimerArmed == FALSE)) { wasCapsLockTimerArmed = TRUE; activateTimer(TIMER_CAPS, CAPS_LOCK_DEL); } else if ((hasTimerExpired(TIMER_CAPS, FALSE) == TIMER_RUNNING) && !(getKeyboardLeds() & HID_CAPS_MASK)) { activityDetectedRoutine(); } else if ((hasTimerExpired(TIMER_CAPS, FALSE) == TIMER_EXPIRED) && !(getKeyboardLeds() & HID_CAPS_MASK)) { wasCapsLockTimerArmed = FALSE; } // If we have a timeout lock if ((mp_timeout_enabled == TRUE) && (hasTimerExpired(SLOW_TIMER_LOCKOUT, TRUE) == TIMER_EXPIRED)) { guiSetCurrentScreen(SCREEN_DEFAULT_INSERTED_LCK); guiGetBackToCurrentScreen(); handleSmartcardRemoved(); } } }
void HudGaugeWingmanStatus::initialize() { initFlash(); HudGauge::initialize(); }
void init() { initFlash(); // set up initial flash info }
void initHardware(Logging *l) { efiAssertVoid(CUSTOM_IH_STACK, getRemainingStack(chThdGetSelfX()) > 256, "init h"); sharedLogger = l; engine_configuration_s *engineConfiguration = engine->engineConfigurationPtr; efiAssertVoid(CUSTOM_EC_NULL, engineConfiguration!=NULL, "engineConfiguration"); board_configuration_s *boardConfiguration = &engineConfiguration->bc; printMsg(sharedLogger, "initHardware()"); // todo: enable protection. it's disabled because it takes // 10 extra seconds to re-flash the chip //flashProtect(); chMtxObjectInit(&spiMtx); #if EFI_HISTOGRAMS /** * histograms is a data structure for CPU monitor, it does not depend on configuration */ initHistogramsModule(); #endif /* EFI_HISTOGRAMS */ /** * We need the LED_ERROR pin even before we read configuration */ initPrimaryPins(); if (hasFirmwareError()) { return; } #if EFI_INTERNAL_FLASH palSetPadMode(CONFIG_RESET_SWITCH_PORT, CONFIG_RESET_SWITCH_PIN, PAL_MODE_INPUT_PULLUP); initFlash(sharedLogger); /** * this call reads configuration from flash memory or sets default configuration * if flash state does not look right. */ if (SHOULD_INGORE_FLASH()) { engineConfiguration->engineType = DEFAULT_ENGINE_TYPE; resetConfigurationExt(sharedLogger, engineConfiguration->engineType PASS_ENGINE_PARAMETER_SUFFIX); writeToFlashNow(); } else { readFromFlash(); } #else engineConfiguration->engineType = DEFAULT_ENGINE_TYPE; resetConfigurationExt(sharedLogger, engineConfiguration->engineType PASS_ENGINE_PARAMETER_SUFFIX); #endif /* EFI_INTERNAL_FLASH */ #if EFI_HD44780_LCD // initI2Cmodule(); lcd_HD44780_init(sharedLogger); if (hasFirmwareError()) return; lcd_HD44780_print_string(VCS_VERSION); #endif /* EFI_HD44780_LCD */ if (hasFirmwareError()) { return; } #if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__) initTriggerDecoder(); #endif bool isBoardTestMode_b; if (CONFIGB(boardTestModeJumperPin) != GPIO_UNASSIGNED) { efiSetPadMode("board test", CONFIGB(boardTestModeJumperPin), PAL_MODE_INPUT_PULLUP); isBoardTestMode_b = (!efiReadPin(CONFIGB(boardTestModeJumperPin))); // we can now relese this pin, it is actually used as output sometimes unmarkPin(CONFIGB(boardTestModeJumperPin)); } else { isBoardTestMode_b = false; } #if HAL_USE_ADC || defined(__DOXYGEN__) initAdcInputs(isBoardTestMode_b); #endif if (isBoardTestMode_b) { // this method never returns initBoardTest(); } initRtc(); initOutputPins(); #if EFI_MAX_31855 initMax31855(sharedLogger, getSpiDevice(CONFIGB(max31855spiDevice)), CONFIGB(max31855_cs)); #endif /* EFI_MAX_31855 */ #if EFI_CAN_SUPPORT initCan(); #endif /* EFI_CAN_SUPPORT */ // init_adc_mcp3208(&adcState, &SPID2); // requestAdcValue(&adcState, 0); #if EFI_SHAFT_POSITION_INPUT || defined(__DOXYGEN__) // todo: figure out better startup logic initTriggerCentral(sharedLogger); #endif /* EFI_SHAFT_POSITION_INPUT */ turnOnHardware(sharedLogger); #if HAL_USE_SPI || defined(__DOXYGEN__) initSpiModules(boardConfiguration); #endif #if EFI_HIP_9011 || defined(__DOXYGEN__) initHip9011(sharedLogger); #endif /* EFI_HIP_9011 */ #if EFI_FILE_LOGGING || defined(__DOXYGEN__) initMmcCard(); #endif /* EFI_FILE_LOGGING */ #if EFI_MEMS || defined(__DOXYGEN__) initAccelerometer(PASS_ENGINE_PARAMETER_SIGNATURE); #endif // initFixedLeds(); #if EFI_BOSCH_YAW || defined(__DOXYGEN__) initBoschYawRateSensor(); #endif /* EFI_BOSCH_YAW */ // initBooleanInputs(); #if EFI_UART_GPS || defined(__DOXYGEN__) initGps(); #endif #if EFI_SERVO initServo(); #endif #if ADC_SNIFFER || defined(__DOXYGEN__) initAdcDriver(); #endif #if HAL_USE_I2C || defined(__DOXYGEN__) addConsoleActionII("i2c", sendI2Cbyte); #endif // USBMassStorageDriver UMSD1; // while (true) { // for (int addr = 0x20; addr < 0x28; addr++) { // sendI2Cbyte(addr, 0); // int err = i2cGetErrors(&I2CD1); // print("I2C: err=%x from %d\r\n", err, addr); // chThdSleepMilliseconds(5); // sendI2Cbyte(addr, 255); // chThdSleepMilliseconds(5); // } // } #if EFI_VEHICLE_SPEED || defined(__DOXYGEN__) initVehicleSpeed(sharedLogger); #endif #if EFI_CDM_INTEGRATION cdmIonInit(); #endif #if HAL_USE_EXT || defined(__DOXYGEN__) initJoystick(sharedLogger); #endif calcFastAdcIndexes(); printMsg(sharedLogger, "initHardware() OK!"); }
/*! \fn flashInitTest() * \brief A Testing wrapper that tests flash init (reading manf id's) * THIS TEST MUST BE RUN -> Sets up SPI * \return Test Status */ RET_TYPE flashInitTest() { // simply call initFlash return initFlash(); } // End flashInitTest
void main(void) { unsigned char state, menuState, x, y, menuButtonTouched, currentProgramEditing, currentPeriodEditing, currentMenuViewing = 0; unsigned char tempTime; char currentTempMenuViewing; unsigned char released = 1; // Semaphore unsigned char touched = 0; extern struct room rooms[2]; // For programs Tab extern unsigned char weeklySchedule[]; // For days tab //EnableInterrupts; /* enable interrupts */ /* include your code here */ CONFIG1_COPD = 1; INTSCR_MODE = 1; INTSCR_IMASK = 1; initI2C(); initPortX(); initADC(); initSCI(); configureTemp(); startTemp(); initFlash(); initializeDisplay(); initializeThermostat(); // Pass this function before enabling interrupts drawMainScreen(); drawSolidButton(homeButton); state = MAIN; menuState = NONE; //setModeMain(82,I_HEAT); EnableInterrupts; INTSCR_IMASK = 0; for(;;) { //sendModeAux(72, I_COOL); saveProgramData(); // Poll clock drawTopBar(); switch (state) { case MAIN: updateTemp(); break; case PROGRAMS_TAB: break; case DAYS_TAB: break; case SETTINGS: break; } // Poll touchscreen if (!isScreenTouched()) { released = 1; } if (isScreenTouched() && released) { released = 0; x = getX(); y = getY(); x = mapToXPixel(x); y = mapToYPixel(y); switch (state) { case MAIN: if (isOverride[currentRoom]) { if (isButtonTouched(x, y, overrideButton)) { isOverride[currentRoom] = 0; drawMainScreen(); } } if (isButtonTouched(x, y, programButton)) { drawProgramsTab(); state = PROGRAMS_TAB; menuState = NONE; } else if (isButtonTouched(x, y, settingsButton)) { drawSettingsScreen(); state = SETTINGS; menuState = NONE; } else { switch (menuState) { case NONE: if (isButtonTouched(x, y, setToButton)) { switch (isOverride[currentRoom]) { case 0: currentMenuViewing = determineTempMenu(currentPeriod[currentRoom].temperature); break; case 1: currentMenuViewing = determineTempMenu(overridePeriod[currentRoom].temperature); break; } goToText(20, 1); printNum(menuButtonTouched); drawMainSetToMenu(currentMenuViewing); menuState = SET_TO_MENU; } else if (isButtonTouched(x, y, roomButton)) { drawMainRoomMenu(); menuState = ROOM_MENU; } else if (isButtonTouched(x, y, fanButton)) { drawMainFanMenu(); menuState = FAN_MENU; } else if (isButtonTouched(x, y, modeButton)) { drawMainModeMenu(); menuState = MAIN_MODE_MENU; } break; case SET_TO_MENU: menuButtonTouched = isVerticalMenuButtonTouched(x, y, setToMenu); if(menuButtonTouched) { switch (currentMenuViewing) { case 1: if (menuButtonTouched == 5) drawMainSetToMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 59 + menuButtonTouched); drawMainScreen(); menuState = NONE; } break; case 2: if (menuButtonTouched == 1) drawMainSetToMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawMainSetToMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 62 + menuButtonTouched); drawMainScreen(); menuState = NONE; } break; case 3: if (menuButtonTouched == 1) drawMainSetToMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawMainSetToMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 65 + menuButtonTouched); drawMainScreen(); menuState = NONE; } break; case 4: if (menuButtonTouched == 1) drawMainSetToMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawMainSetToMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 68 + menuButtonTouched); drawMainScreen(); menuState = NONE; } break; case 5: if (menuButtonTouched == 1) drawMainSetToMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawMainSetToMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 71 + menuButtonTouched); drawMainScreen(); menuState = NONE; } break; case 6: if (menuButtonTouched == 1) drawMainSetToMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawMainSetToMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 74 + menuButtonTouched); drawMainScreen(); menuState = NONE; } break; case 7: if (menuButtonTouched == 1) drawMainSetToMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawMainSetToMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 77 + menuButtonTouched); drawMainScreen(); menuState = NONE; } break; case 8: if (menuButtonTouched == 1) { drawMainSetToMenu(--currentMenuViewing); } else if (menuButtonTouched == 5) drawMainSetToMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 80 + menuButtonTouched); drawMainScreen(); menuState = NONE; } break; case 9: if (menuButtonTouched == 1) { drawMainSetToMenu(--currentMenuViewing); } else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 83 + menuButtonTouched); drawMainScreen(); menuState = NONE; } break; } } break; case ROOM_MENU: menuButtonTouched = isHorizontalMenuButtonTouched(x, y, roomMenu); switch (menuButtonTouched) { case 1: currentRoom = ROOM_MAIN; drawMainScreen(); menuState = NONE; break; case 2: currentRoom = ROOM_AUX; drawMainScreen(); menuState = NONE; break; } break; case FAN_MENU: menuButtonTouched = isHorizontalMenuButtonTouched(x, y, fanMenu); switch (menuButtonTouched) { case 1: fanMode[currentRoom] = FAN_ON; drawMainScreen(); menuState = NONE; break; case 2: fanMode[currentRoom] = FAN_OFF; drawMainScreen(); menuState = NONE; break; case 3: fanMode[currentRoom] = FAN_AUTO; drawMainScreen(); menuState = NONE; break; } break; // Will cause manual override case MAIN_MODE_MENU: menuButtonTouched = isHorizontalMenuButtonTouched(x, y, modeMenu); switch (menuButtonTouched) { case 1: overridePeriod[currentRoom].mode = HEAT; menuState = NONE; break; case 2: overridePeriod[currentRoom].mode = COOL; menuState = NONE; break; case 3: overridePeriod[currentRoom].mode = OFF; menuState = NONE; break; } if (overridePeriod[currentRoom].mode != currentPeriod[currentRoom].mode) { isOverride[currentRoom] = 1; } else { isOverride[currentRoom] = 0; } if (menuButtonTouched) { drawMainScreen(); } break; } } /****END MENUSTATE****/ break; /****END MAIN****/ case PROGRAMS_TAB: if (isButtonTouched(x, y, homeButton)) { drawMainScreen(); state = MAIN; } else if (isButtonTouched(x, y, settingsButton)) { drawSettingsScreen(); state = SETTINGS; } else if (isButtonTouched(x, y, daysTab)) { drawDaysTab(); state = DAYS_TAB; } else if (isButtonTouched(x, y, program1Button)) { drawProgrammingScreen(0); state = PROGRAMMING_SCREEN; currentProgramEditing = 0; } else if (isButtonTouched(x, y, program2Button)) { drawProgrammingScreen(1); state = PROGRAMMING_SCREEN; currentProgramEditing = 1; } else if (isButtonTouched(x, y, program3Button)) { drawProgrammingScreen(2); state = PROGRAMMING_SCREEN; currentProgramEditing = 2; } else if (isButtonTouched(x, y, program4Button)) { drawProgrammingScreen(3); state = PROGRAMMING_SCREEN; currentProgramEditing = 3; } break; case PROGRAMMING_SCREEN: if (isButtonTouched(x, y, homeButton)) { drawMainScreen(); state = MAIN; menuState = NONE; } else if (isButtonTouched(x, y, settingsButton)) { drawSettingsScreen(); state = SETTINGS; menuState = NONE; } else if (isButtonTouched(x, y, okButton)) { drawProgramsTab(); state = PROGRAMS_TAB; menuState = NONE; } switch (menuState) { case NONE: if (isButtonTouched(x, y, hour1Button)) { currentPeriodEditing = 0; currentMenuViewing = determineHoursMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime); drawHoursMenu(currentMenuViewing); menuState = HOURS_MENU; } else if (isButtonTouched(x, y, hour2Button)) { currentPeriodEditing = 1; currentMenuViewing = determineHoursMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime); drawHoursMenu(currentMenuViewing); menuState = HOURS_MENU; } else if (isButtonTouched(x, y, hour3Button)) { currentPeriodEditing = 2; currentMenuViewing = determineHoursMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime); drawHoursMenu(currentMenuViewing); menuState = HOURS_MENU; } else if (isButtonTouched(x, y, hour4Button)) { currentPeriodEditing = 3; currentMenuViewing = determineHoursMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime); drawHoursMenu(currentMenuViewing); menuState = HOURS_MENU; } else if (isButtonTouched(x, y, minutes1Button)) { currentPeriodEditing = 0; drawMinutesMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime); menuState = MINUTES_MENU; } else if (isButtonTouched(x, y, minutes2Button)) { currentPeriodEditing = 1; drawMinutesMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime); menuState = MINUTES_MENU; } else if (isButtonTouched(x, y, minutes3Button)) { currentPeriodEditing = 2; drawMinutesMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime); menuState = MINUTES_MENU; } else if (isButtonTouched(x, y, minutes4Button)) { currentPeriodEditing = 3; drawMinutesMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime); menuState = MINUTES_MENU; } else if (isButtonTouched(x, y, amPm1Button)) { toggleAmPm(currentProgramEditing, 0); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } else if (isButtonTouched(x, y, amPm2Button)) { toggleAmPm(currentProgramEditing, 1); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } else if (isButtonTouched(x, y, amPm3Button)) { toggleAmPm(currentProgramEditing, 2); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } else if (isButtonTouched(x, y, amPm4Button)) { toggleAmPm(currentProgramEditing, 3); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } else if (isButtonTouched(x, y, temp1Button)) { currentPeriodEditing = 0; currentMenuViewing = determineTempMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature); drawProgrammingTempMenu(currentMenuViewing); menuState = TEMP_MENU; } else if (isButtonTouched(x, y, temp2Button)) { currentPeriodEditing = 1; currentMenuViewing = determineTempMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature); drawProgrammingTempMenu(currentMenuViewing); menuState = TEMP_MENU; } else if (isButtonTouched(x, y, temp3Button)) { currentPeriodEditing = 2; currentMenuViewing = determineTempMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature); drawProgrammingTempMenu(currentMenuViewing); menuState = TEMP_MENU; } else if (isButtonTouched(x, y, temp4Button)) { currentPeriodEditing = 3; currentMenuViewing = determineTempMenu(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature); drawProgrammingTempMenu(currentMenuViewing); menuState = TEMP_MENU; } else if (isButtonTouched(x, y, mode1Button)) { currentPeriodEditing = 0; drawProgrammingModeMenu(currentPeriodEditing); menuState = PROGRAM_MODE_MENU; } else if (isButtonTouched(x, y, mode2Button)) { currentPeriodEditing = 1; drawProgrammingModeMenu(currentPeriodEditing); menuState = PROGRAM_MODE_MENU; } else if (isButtonTouched(x, y, mode3Button)) { currentPeriodEditing = 2; drawProgrammingModeMenu(currentPeriodEditing); menuState = PROGRAM_MODE_MENU; } else if (isButtonTouched(x, y, mode4Button)) { currentPeriodEditing = 3; drawProgrammingModeMenu(currentPeriodEditing); menuState = PROGRAM_MODE_MENU; } break; case HOURS_MENU: menuButtonTouched = isVerticalMenuButtonTouched(x, y, hoursMenu); if (menuButtonTouched) { switch (currentMenuViewing) { case 1: if (menuButtonTouched == 5) { drawHoursMenu(++currentMenuViewing); } else { updateHours(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime), menuButtonTouched); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case 2: if (menuButtonTouched == 1) drawHoursMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawHoursMenu(++currentMenuViewing); else { updateHours(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime), menuButtonTouched + 3); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case 3: if (menuButtonTouched == 1) drawHoursMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawHoursMenu(++currentMenuViewing); else { updateHours(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime), menuButtonTouched + 6); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case 4: if (menuButtonTouched == 1) drawHoursMenu(--currentMenuViewing); else if (menuButtonTouched == 2) { updateHours(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime), 11); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } else if (menuButtonTouched == 3) { updateHours(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime), 12); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; } } break; case MINUTES_MENU: menuButtonTouched = isVerticalMenuButtonTouched(x, y, minutesMenu); if(menuButtonTouched) { updateMinutes(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].startTime), menuButtonTouched - 1); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case TEMP_MENU: menuButtonTouched = isVerticalMenuButtonTouched(x, y, programmingTempMenu); if(menuButtonTouched) { switch (currentMenuViewing) { case 1: if (menuButtonTouched == 5) drawProgrammingTempMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 59 + menuButtonTouched); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case 2: if (menuButtonTouched == 1) drawProgrammingTempMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawProgrammingTempMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 62 + menuButtonTouched); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case 3: if (menuButtonTouched == 1) drawProgrammingTempMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawProgrammingTempMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 65 + menuButtonTouched); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case 4: if (menuButtonTouched == 1) drawProgrammingTempMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawProgrammingTempMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 68 + menuButtonTouched); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case 5: if (menuButtonTouched == 1) drawProgrammingTempMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawProgrammingTempMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 71 + menuButtonTouched); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case 6: if (menuButtonTouched == 1) drawProgrammingTempMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawProgrammingTempMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 74 + menuButtonTouched); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case 7: if (menuButtonTouched == 1) drawProgrammingTempMenu(--currentMenuViewing); else if (menuButtonTouched == 5) drawProgrammingTempMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 77 + menuButtonTouched); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case 8: if (menuButtonTouched == 1) { drawProgrammingTempMenu(--currentMenuViewing); } else if (menuButtonTouched == 5) drawProgrammingTempMenu(++currentMenuViewing); else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 80 + menuButtonTouched); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; case 9: if (menuButtonTouched == 1) { drawProgrammingTempMenu(--currentMenuViewing); } else { updateProgrammingTemp(&(rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].temperature), 83 + menuButtonTouched); drawProgrammingScreen(currentProgramEditing); menuState = NONE; } break; } } break; case PROGRAM_MODE_MENU: programmingModeMenu[1] = (1 + 3*(currentPeriodEditing+1)); menuButtonTouched = isHorizontalMenuButtonTouched(x, y, programmingModeMenu); switch (menuButtonTouched) { case 1: rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].mode = HEAT; drawProgrammingScreen(currentProgramEditing); menuState = NONE; break; case 2: rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].mode = COOL; drawProgrammingScreen(currentProgramEditing); menuState = NONE; break; case 3: rooms[currentRoom].programs[currentProgramEditing].periods[currentPeriodEditing].mode = OFF; drawProgrammingScreen(currentProgramEditing); menuState = NONE; break; } programmingModeMenu[1] = 1; break; } break; case DAYS_TAB: if (isButtonTouched(x, y, homeButton)) { drawMainScreen(); state = MAIN; menuState = NONE; } else if (isButtonTouched(x, y, settingsButton)) { drawSettingsScreen(); state = SETTINGS; menuState = NONE; } else if (isButtonTouched(x, y, programsTab)) { drawProgramsTab(); state = PROGRAMS_TAB; menuState = NONE; } switch (menuState) { case NONE: if (isButtonTouched(x, y, monButton)) { drawDaysMenu(monMenu); menuState = MONDAY; } else if (isButtonTouched(x, y, tueButton)) { drawDaysMenu(tueMenu); menuState = TUESDAY; } else if (isButtonTouched(x, y, wedButton)) { drawDaysMenu(wedMenu); menuState = WEDNESDAY; } else if (isButtonTouched(x, y, thuButton)) { drawDaysMenu(thuMenu); menuState = THURSDAY; } else if (isButtonTouched(x, y, friButton)) { drawDaysMenu(friMenu); menuState = FRIDAY; } else if (isButtonTouched(x, y, satButton)) { drawDaysMenu(satMenu); menuState = SATURDAY; } else if (isButtonTouched(x, y, sunButton)) { drawDaysMenu(sunMenu); menuState = SUNDAY; } break; case MONDAY: menuButtonTouched = isVerticalMenuButtonTouched(x, y, monMenu); if (menuButtonTouched) { weeklySchedule[0] = menuButtonTouched - 1; drawDaysTab(); menuState = NONE; } break; case TUESDAY: menuButtonTouched = isVerticalMenuButtonTouched(x, y, tueMenu); if (menuButtonTouched) { weeklySchedule[1] = menuButtonTouched - 1; drawDaysTab(); menuState = NONE; } break; case WEDNESDAY: menuButtonTouched = isVerticalMenuButtonTouched(x, y, wedMenu); if (menuButtonTouched) { weeklySchedule[2] = menuButtonTouched - 1; drawDaysTab(); menuState = NONE; } break; case THURSDAY: menuButtonTouched = isVerticalMenuButtonTouched(x, y, thuMenu); if (menuButtonTouched) { weeklySchedule[3] = menuButtonTouched - 1; drawDaysTab(); menuState = NONE; } break; case FRIDAY: menuButtonTouched = isVerticalMenuButtonTouched(x, y, friMenu); if (menuButtonTouched) { weeklySchedule[4] = menuButtonTouched - 1; drawDaysTab(); menuState = NONE; } break; case SATURDAY: menuButtonTouched = isVerticalMenuButtonTouched(x, y, satMenu); if (menuButtonTouched) { weeklySchedule[5] = menuButtonTouched - 1; drawDaysTab(); menuState = NONE; } break; case SUNDAY: menuButtonTouched = isVerticalMenuButtonTouched(x, y, sunMenu); if (menuButtonTouched) { weeklySchedule[6] = menuButtonTouched - 1; drawDaysTab(); menuState = NONE; } break; } break; case SETTINGS: if (isButtonTouched(x, y, homeButton)) { drawMainScreen(); state = MAIN; } else if (isButtonTouched(x, y, programButton)) { drawProgramsTab(); state = PROGRAMS_TAB; } break; } refreshThermostat(); } __RESET_WATCHDOG(); /* feeds the dog */ } /* loop forever */ /* please make sure that you never leave main */ }
void flash_main(void){ #else void main(void){ #endif BYTE buffer[30]; char inputBuffer[STRING_LENGTH]; INT8 pointer = 0; BOOL stop = FALSE; BOOL write = FALSE; char c; char *menuText[] = {(char*)" CPU write?", (char*)" DMA write?"}; BYTE command; BOOL unUsed; initFlash(); // Clearing buffers memset(buffer,0,sizeof(buffer)); memset(inputBuffer,0,sizeof(inputBuffer)); // Setting up UART UART_SETUP(0,57600,HIGH_STOP); UTX0IF = 1; // Set UART 0 TX interrupt flag while(getJoystickDirection() != CENTRED); //Displaying the stored flash message. lcdUpdateLine(LINE1,(char*)"Last written:"); if((unUsed = flashUnused((BYTE*)testData, STRING_LENGTH))) { lcdUpdateLine(LINE2,(char*)"Unused"); } else { scrollText((char*) testData, STRING_LENGTH); } while(getJoystickDirection() != CENTRED); while(getJoystickDirection() == CENTRED); while(getJoystickDirection() != CENTRED); // User decides whether to use CPU or DMA to write flash or to abort. command = lcdMenu(menuText,2); if(command == ABORT_MENU) { return; } // Uart communication lcdUpdate((char*)"Enter UART", (char*)"data"); printf((char*)"\n\nFlash Programming\n"); printf((char*)"Press a key\n\n"); uartGetkey (); // wait for a key to be pressed or the application to be ended if (stopApplication() ) return; else { inputBuffer[0] = U0DBUF; halWait(5); USART0_FLUSH(); inputBuffer[1] = U0DBUF; } // Printing the previously written data printf((char*)"\nLast written:\n"); if(unUsed) { printf((char*)"Unused\n"); } else { printf((char*)"%s\n",&testData); } //Aquiring new data: printf((char*)"\n\nType data to be written.\nWill be printed to the LCD next time."); printf((char*)"\n(ENTER: store in flash, ESC: abort)\n\n"); memset(inputBuffer,0,STRING_LENGTH); while(!stop) { c = getkey(); U0DBUF = c; switch (c){ case ENTER: inputBuffer[pointer] = 0; printf((char*)"\n\nTo write: %s\nENTER if OK.\n",inputBuffer); if(getkey() == ENTER) { // Write data to flash; stop = TRUE; write = TRUE; } else { // Reaquire data. printf((char*)"\nEnter text:\n"); pointer = 0; } break; case BACK_SPACE: // Erasing the last typed data. if (pointer > 0) { pointer--; inputBuffer[pointer] = ' '; } break; case ESC: // Abort Flash write. stop = TRUE; write = FALSE; break; default: // Add typed data to buffer. if (pointer < STRING_LENGTH-1) { inputBuffer[pointer] = c; pointer++; } break; } } INT_GLOBAL_ENABLE(INT_OFF); // Updating the flash if asked to. if(write == TRUE) { if(command == 0) { halFlashWritePage((BYTE*) &inputBuffer, buffer, PAGE_NUMBER); } else { writeFlashUsingDMA((BYTE*) &inputBuffer, STRING_LENGTH, PAGE_ADDRESS, TRUE); } printf((char*)"\nUpdated:"); printf((char*)" %s\n",(char __code*) (PAGE_NUMBER << 10)); lcdUpdateLine(LINE1,(char*)"Updated"); } else { printf((char*)"\nNot updated\n"); lcdUpdateLine(LINE1,(char*)"Not updated"); } lcdUpdateLine(LINE2,(char*)"LEFT to continue"); // Done haltApplicationWithLED(); return; }
/*! \fn main(void) * \brief Main function */ int main(void) { RET_TYPE flash_init_result = RETURN_NOK; RET_TYPE card_detect_ret; RET_TYPE temp_rettype; uint8_t temp_buffer[200]; char temp_string[20]; CPU_PRESCALE(0); // Set for 16MHz clock _delay_ms(500); // Let the power settle initPortSMC(); // Init smart card Port initIRQ(); // Init interrupts usb_init(); // Init USB controller while(!usb_configured()); // Wait for host to set configuration initOLED(); // Init OLED screen after enum flash_init_result = initFlash(); // Init flash memory //#define TEST_HID_AND_CDC #ifdef TEST_HID_AND_CDC //Show_String("Z",FALSE,2,0); //usb_keyboard_press(KEY_S, 0); while(1) { int n = usb_serial_getchar(); if (n >= 0) { usb_serial_putchar(n); Show_String((char*)&n,FALSE,2,0); //usb_keyboard_press(n,0); } } #endif /* TEST_HID_AND_CDC */ //lcd_display_grayscale(); if(flash_init_result == RETURN_OK) Show_String("Flash init ok", FALSE, 2, 0); else Show_String("Problem flash init", FALSE, 2, 250); //display_picture(HACKADAY_BMP, 20, 0); //draw_screen_frame(); //Show_String("Mooltipass", FALSE, 32, 10); Show_String("No card detected", FALSE, 25, 45); //If no card is inserted at boot time while(1) { card_detect_ret = isCardPlugged(); if(card_detect_ret == RETURN_JDETECT) // Card just detected { temp_rettype = cardDetectedRoutine(); if(temp_rettype == RETURN_MOOLTIPASS_INVALID) // Invalid card { _delay_ms(3000); printSMCDebugInfoToScreen(); removeFunctionSMC(); // Shut down card reader } else if(temp_rettype == RETURN_MOOLTIPASS_PB) // Problem with card { _delay_ms(3000); printSMCDebugInfoToScreen(); removeFunctionSMC(); // Shut down card reader } else if(temp_rettype == RETURN_MOOLTIPASS_BLOCKED) // Card blocked { _delay_ms(3000); printSMCDebugInfoToScreen(); removeFunctionSMC(); // Shut down card reader } else if(temp_rettype == RETURN_MOOLTIPASS_BLANK) // Blank mooltipass card { // Here we should ask the user to setup his mooltipass card _delay_ms(3000); printSMCDebugInfoToScreen(); removeFunctionSMC(); // Shut down card reader } else if(temp_rettype == RETURN_MOOLTIPASS_USER) // Configured mooltipass card { // Here we should ask the user for his pin and call mooltipassdetect _delay_ms(3000); printSMCDebugInfoToScreen(); removeFunctionSMC(); // Shut down card reader } /*read_credential_block_within_flash_page(2,1,temp_buffer); for(i = 0; i < 10; i++) { hexachar_to_string(temp_buffer[i], temp_string); Show_String(temp_string, FALSE, 2+i*5, 0); } temp_buffer[3] = 0x0A; write_credential_block_within_flash_page(2,1, temp_buffer); read_credential_block_within_flash_page(2,1,temp_buffer); for(i = 0; i < 10; i++) { hexachar_to_string(temp_buffer[i], temp_string); Show_String(temp_string, FALSE, 2+i*5, 8); }*/ } else if(card_detect_ret == RETURN_JRELEASED) //card just released { removeFunctionSMC(); clear_screen(); Show_String("Please insert card", FALSE, 2, 8); } } }
void initHardware(Logging *logger, Engine *engine) { engine_configuration_s *engineConfiguration = engine->engineConfiguration; efiAssertVoid(engineConfiguration!=NULL, "engineConfiguration"); board_configuration_s *boardConfiguration = &engineConfiguration->bc; printMsg(logger, "initHardware()"); // todo: enable protection. it's disabled because it takes // 10 extra seconds to re-flash the chip //flashProtect(); chMtxInit(&spiMtx); #if EFI_HISTOGRAMS /** * histograms is a data structure for CPU monitor, it does not depend on configuration */ initHistogramsModule(); #endif /* EFI_HISTOGRAMS */ /** * This is so early because we want to init logger * which would be used while finding trigger synch index * while config read */ initTriggerDecoder(); /** * We need the LED_ERROR pin even before we read configuration */ initPrimaryPins(); if (hasFirmwareError()) { return; } initDataStructures(PASS_ENGINE_PARAMETER_F); #if EFI_INTERNAL_FLASH palSetPadMode(CONFIG_RESET_SWITCH_PORT, CONFIG_RESET_SWITCH_PIN, PAL_MODE_INPUT_PULLUP); initFlash(engine); /** * this call reads configuration from flash memory or sets default configuration * if flash state does not look right. */ if (SHOULD_INGORE_FLASH()) { engineConfiguration->engineType = FORD_ASPIRE_1996; resetConfigurationExt(logger, engineConfiguration->engineType, engine); writeToFlash(); } else { readFromFlash(); } #else engineConfiguration->engineType = FORD_ASPIRE_1996; resetConfigurationExt(logger, engineConfiguration->engineType, engineConfiguration, engineConfiguration2, boardConfiguration); #endif /* EFI_INTERNAL_FLASH */ if (hasFirmwareError()) { return; } mySetPadMode2("board test", boardConfiguration->boardTestModeJumperPin, PAL_MODE_INPUT_PULLUP); bool isBoardTestMode_b = GET_BOARD_TEST_MODE_VALUE(); initAdcInputs(isBoardTestMode_b); if (isBoardTestMode_b) { // this method never returns initBoardTest(); } initRtc(); initOutputPins(); #if EFI_HIP_9011 initHip9011(); #endif /* EFI_HIP_9011 */ #if EFI_MAX_31855 initMax31855(boardConfiguration); #endif /* EFI_MAX_31855 */ #if EFI_CAN_SUPPORT initCan(); #endif /* EFI_CAN_SUPPORT */ // init_adc_mcp3208(&adcState, &SPID2); // requestAdcValue(&adcState, 0); // todo: figure out better startup logic initTriggerCentral(engine); #if EFI_SHAFT_POSITION_INPUT initShaftPositionInputCapture(); #endif /* EFI_SHAFT_POSITION_INPUT */ initSpiModules(boardConfiguration); #if EFI_FILE_LOGGING initMmcCard(); #endif /* EFI_FILE_LOGGING */ // initFixedLeds(); // initBooleanInputs(); #if EFI_UART_GPS initGps(); #endif #if ADC_SNIFFER initAdcDriver(); #endif #if EFI_HD44780_LCD // initI2Cmodule(); lcd_HD44780_init(); if (hasFirmwareError()) return; lcd_HD44780_print_string(VCS_VERSION); #endif /* EFI_HD44780_LCD */ addConsoleActionII("i2c", sendI2Cbyte); // while (true) { // for (int addr = 0x20; addr < 0x28; addr++) { // sendI2Cbyte(addr, 0); // int err = i2cGetErrors(&I2CD1); // print("I2C: err=%x from %d\r\n", err, addr); // chThdSleepMilliseconds(5); // sendI2Cbyte(addr, 255); // chThdSleepMilliseconds(5); // } // } printMsg(logger, "initHardware() OK!"); }
void initHardware() { // todo: enable protection. it's disabled because it takes // 10 extra seconds to re-flash the chip //flashProtect(); /** * histograms is a data structure for CPU monitor, it does not depend on configuration */ initHistogramsModule(); /** * We need the LED_ERROR pin even before we read configuration */ initPrimaryPins(); /** * this call reads configuration from flash memory or sets default configuration * if flash state does not look right. */ initFlash(); initRtc(); initOutputPins(); initAdcInputs(); #if EFI_HIP_9011 initHip9011(); #endif /* EFI_HIP_9011 */ #if EFI_CAN_SUPPORT initCan(); #endif /* EFI_CAN_SUPPORT */ // init_adc_mcp3208(&adcState, &SPID2); // requestAdcValue(&adcState, 0); // todo: figure out better startup logic initTriggerCentral(); initShaftPositionInputCapture(); initSpiModules(); #if EFI_FILE_LOGGING initMmcCard(); #endif /* EFI_FILE_LOGGING */ // initFixedLeds(); // initBooleanInputs(); #if EFI_UART_GPS initGps(); #endif #if ADC_SNIFFER initAdcDriver(); #endif #if EFI_HD44780_LCD // initI2Cmodule(); lcd_HD44780_init(); char buffer[16]; itoa10(buffer, SVN_VERSION); lcd_HD44780_print_string(buffer); #endif addConsoleActionII("i2c", sendI2Cbyte); // while (true) { // for (int addr = 0x20; addr < 0x28; addr++) { // sendI2Cbyte(addr, 0); // int err = i2cGetErrors(&I2CD1); // print("I2C: err=%x from %d\r\n", err, addr); // chThdSleepMilliseconds(5); // sendI2Cbyte(addr, 255); // chThdSleepMilliseconds(5); // } // } initBoardTest(); }