///////////////////////////////////////////////////////////////////////////// // This task is running endless in background ///////////////////////////////////////////////////////////////////////////// void APP_Background(void){ u8 i; while(1){ switch(phase){ case BS_CHECK_PHASE_INIT: bs = 0; block = 0; last_error = 0; last_error_code = 0; time_read = 0xff; time_write = 0xff; // initialize IIC.. if(run == 0){ MIOS32_MIDI_SendDebugMessage("Bankstick check init..."); #if BS_CHECK_USE_FRAM_LAYER == 0 if( (last_error_code = MIOS32_IIC_BS_Init(0)) != 0){ #else if( (last_error_code = FRAM_Init(0)) != 0){ #endif last_error = BS_CHECK_ERROR_INIT; phase = BS_CHECK_PHASE_FINISHED; } else{ for(i = 0;i < BS_CHECK_NUM_DEVICES; i++){ #if BS_CHECK_USE_FRAM_LAYER == 0 if ( (MIOS32_IIC_BS_CheckAvailable(i)) == 0) { #else if ( (FRAM_CheckAvailable(i)) != 0) { #endif report_iic_error(); last_error = BS_CHECK_ERROR_AVAILABLE; last_error_code = i; MIOS32_MIDI_SendDebugMessage("Bankstick %d not available",i); phase = BS_CHECK_PHASE_FINISHED; break; } } } } else{ MIOS32_MIDI_SendDebugMessage("Start next check, run %d",run+1); } if(!last_error) phase = BS_CHECK_PHASE_WRITE; break; case BS_CHECK_PHASE_WRITE: // write blocks if(bs == 0 && block == 0) MIOS32_MIDI_SendDebugMessage("Start writing blocks..."); init_buffer(); #if BS_CHECK_USE_FRAM_LAYER == 0 if( (last_error_code = MIOS32_IIC_BS_Write(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){ #else if( (last_error_code = FRAM_Write(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){ #endif report_iic_error(); last_error = BS_CHECK_ERROR_WRITE; phase = BS_CHECK_PHASE_FINISHED; } else{ if( (last_error_code = wait_write_finished()) < 0){ report_iic_error(); last_error = BS_CHECK_ERROR_CHECK_WRITE_FINISHED; phase = BS_CHECK_PHASE_FINISHED; } else if( ++block >= BS_CHECK_NUM_BLOCKS_PER_DEVICE ){ MIOS32_MIDI_SendDebugMessage("Writing bankstick %d finished",bs); block = 0; if( ++bs >= BS_CHECK_NUM_DEVICES ){ bs = 0; phase = BS_CHECK_PHASE_READ; } } } break; case BS_CHECK_PHASE_READ: // read blocks if(bs == 0 && block == 0) MIOS32_MIDI_SendDebugMessage("Start reading / comparing blocks..."); clear_buffer(); #if BS_CHECK_USE_FRAM_LAYER == 0 if( (last_error_code = MIOS32_IIC_BS_Read(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){ #else if( (last_error_code = FRAM_Read(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){ #endif report_iic_error(); last_error = BS_CHECK_ERROR_READ; phase = BS_CHECK_PHASE_FINISHED; } else{ if( !check_buffer() ){ last_error = BS_CHECK_ERROR_COMPARE; last_error_code = 0;// no further error specification phase = BS_CHECK_PHASE_FINISHED; } else if( ++block >= BS_CHECK_NUM_BLOCKS_PER_DEVICE ){ MIOS32_MIDI_SendDebugMessage("Reading / comparing bankstick %d finished",bs); block = 0; if( ++bs >= BS_CHECK_NUM_DEVICES ){ MIOS32_MIDI_SendDebugMessage("Check run %d finished",run+1); if(++run >= BS_CHECK_NUM_RUNS) phase = BS_CHECK_PHASE_FINISHED; else{ bs = 0; phase = BS_CHECK_PHASE_START; } } } } break; } } } static s32 wait_write_finished(void){ #if BS_CHECK_USE_FRAM_LAYER == 0 s32 err_code; do{ err_code = MIOS32_IIC_BS_CheckWriteFinished(bs); } while( err_code > 0 ); return err_code; #else return 0; #endif } static void report_iic_error(void){ #if BS_CHECK_USE_FRAM_LAYER == 0 MIOS32_MIDI_SendDebugMessage("IIC error %d",MIOS32_IIC_LastErrorGet(MIOS32_IIC_BS_PORT)); #else MIOS32_MIDI_SendDebugMessage("IIC error %d",MIOS32_IIC_LastErrorGet(FRAM_IIC_PORT)); #endif }
///////////////////////////////////////////////////////////////////////////// // Main function ///////////////////////////////////////////////////////////////////////////// int main(void) { // initialize hardware and MIOS32 modules #ifndef MIOS32_DONT_USE_SYS MIOS32_SYS_Init(0); #endif #ifndef MIOS32_DONT_USE_DELAY MIOS32_DELAY_Init(0); #endif #ifndef MIOS32_DONT_USE_TIMESTAMP MIOS32_TIMESTAMP_Init(0); #endif #ifndef MIOS32_DONT_USE_BOARD MIOS32_BOARD_Init(0); #endif #ifndef MIOS32_DONT_USE_SPI MIOS32_SPI_Init(0); #endif #ifndef MIOS32_DONT_USE_SRIO MIOS32_SRIO_Init(0); #endif #if !defined(MIOS32_DONT_USE_DIN) && !defined(MIOS32_DONT_USE_SRIO) MIOS32_DIN_Init(0); #endif #if !defined(MIOS32_DONT_USE_DOUT) && !defined(MIOS32_DONT_USE_SRIO) MIOS32_DOUT_Init(0); #endif #if !defined(MIOS32_DONT_USE_ENC) && !defined(MIOS32_DONT_USE_SRIO) MIOS32_ENC_Init(0); #endif #if !defined(MIOS32_DONT_USE_MF) MIOS32_MF_Init(0); #endif #if !defined(MIOS32_DONT_USE_AIN) MIOS32_AIN_Init(0); #endif #ifndef MIOS32_DONT_USE_IIC_BS MIOS32_IIC_BS_Init(0); #endif #ifndef MIOS32_DONT_USE_MIDI MIOS32_MIDI_Init(0); #endif #ifndef MIOS32_DONT_USE_USB MIOS32_USB_Init(0); #endif #ifndef MIOS32_DONT_USE_OSC MIOS32_OSC_Init(0); #endif #ifndef MIOS32_DONT_USE_COM MIOS32_COM_Init(0); #endif #ifndef MIOS32_DONT_USE_LCD MIOS32_LCD_Init(0); # if defined(MIOS32_BOARD_MBHP_CORE_STM32) || defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_STM32F4DISCOVERY) || defined(MIOS32_BOARD_MBHP_CORE_STM32F4) // init second LCD as well (if available) MIOS32_LCD_DeviceSet(1); APP_LCD_Init(0); MIOS32_LCD_DeviceSet(0); # endif #endif #ifdef MIOS32_USE_I2S MIOS32_I2S_Init(0); #endif // call C++ constructors __libc_init_array(); // initialize application APP_Init(); #if MIOS32_LCD_BOOT_MSG_DELAY // print boot message # ifndef MIOS32_DONT_USE_LCD MIOS32_LCD_PrintBootMessage(); # endif // wait for given delay (usually 2 seconds) # ifndef MIOS32_DONT_USE_DELAY int delay = 0; for(delay=0; delay<MIOS32_LCD_BOOT_MSG_DELAY; ++delay) MIOS32_DELAY_Wait_uS(1000); # endif #endif // start the task which calls the application hooks xTaskCreate(TASK_Hooks, (signed portCHAR *)"Hooks", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_HOOKS, NULL); #if !defined(MIOS32_DONT_USE_MIDI) xTaskCreate(TASK_MIDI_Hooks, (signed portCHAR *)"MIDI_Hooks", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_HOOKS, NULL); #endif // start the scheduler vTaskStartScheduler(); // Will only get here if there was not enough heap space to create the idle task return 0; }