static INT32 crushermInit() { INT32 nLen; BurnSetRefreshRate(CAVE_REFRESHRATE); // Find out how much memory is needed Mem = NULL; MemIndex(); nLen = MemEnd - (UINT8 *)0; if ((Mem = (UINT8 *)BurnMalloc(nLen)) == NULL) { return 1; } memset(Mem, 0, nLen); // blank all memory MemIndex(); // Index the allocated memory // Load the roms into memory if (crushermLoadRoms()) { return 1; } EEPROMInit(&eeprom_interface_93C46_8bit); { SekInit(0, 0x68000); // Allocate 68000 SekOpen(0); // Map 68000 memory: SekMapMemory(Rom01, 0x000000, 0x07FFFF, MAP_ROM); // CPU 0 ROM SekMapMemory(CaveTileRAM[0], 0x100000, 0x107FFF, MAP_RAM); SekMapMemory(CaveSpriteRAM, 0x180000, 0x187FFF, MAP_RAM); SekMapMemory(CavePalSrc, 0x200000, 0x207FFF, MAP_RAM); SekMapMemory(Ram01, 0x340000, 0x34FFFF, MAP_RAM); SekSetReadWordHandler(0, korokoroReadWord); SekSetReadByteHandler(0, korokoroReadByte); SekSetWriteWordHandler(0, korokoroWriteWord); SekSetWriteByteHandler(0, korokoroWriteByte); SekClose(); } nCaveRowModeOffset = 1; CavePalInit(0x8000); CaveTileInit(); CaveSpriteInit(1, 0x400000); CaveTileInitLayer(0, 0x200000, 4, 0x4400); YMZ280BInit(16934400, &TriggerSoundIRQ, 0x200000); YMZ280BSetRoute(BURN_SND_YMZ280B_YMZ280B_ROUTE_1, 1.00, BURN_SND_ROUTE_LEFT); YMZ280BSetRoute(BURN_SND_YMZ280B_YMZ280B_ROUTE_2, 1.00, BURN_SND_ROUTE_RIGHT); bDrawScreen = true; DrvDoReset(); // Reset machine return 0; }
static int DrvInit() { int nLen; BurnSetRefreshRate(CAVE_REFRESHRATE); // Find out how much memory is needed Mem = NULL; MemIndex(); nLen = MemEnd - (unsigned char *)0; if ((Mem = (unsigned char *)BurnMalloc(nLen)) == NULL) { return 1; } memset(Mem, 0, nLen); // blank all memory MemIndex(); // Index the allocated memory EEPROMInit(&eeprom_interface_93C46_8bit); // Load the roms into memory if (LoadRoms()) { return 1; } { SekInit(0, 0x68000); // Allocate 68000 SekOpen(0); // Map 68000 memory: SekMapMemory(Rom01, 0x000000, 0x07FFFF, SM_ROM); // CPU 0 ROM SekMapMemory(CaveTileRAM[0], 0x100000, 0x107FFF, SM_RAM); SekMapMemory(CaveSpriteRAM, 0x180000, 0x187FFF, SM_RAM); SekMapMemory(CavePalSrc, 0x200000, 0x207FFF, SM_RAM); SekMapMemory(Ram01, 0x300000, 0x30FFFF, SM_RAM); SekSetReadWordHandler(0, korokoroReadWord); SekSetReadByteHandler(0, korokoroReadByte); SekSetWriteWordHandler(0, korokoroWriteWord); SekSetWriteByteHandler(0, korokoroWriteByte); SekClose(); } nCaveRowModeOffset = 1; CavePalInit(0x8000); CaveTileInit(); CaveSpriteInit(1, 0x300000); CaveTileInitLayer(0, 0x200000, 4, 0x4400); YMZ280BInit(16934400, &TriggerSoundIRQ, 3); bDrawScreen = true; DrvDoReset(); // Reset machine return 0; }
void FreeIMUFormatFlash(void) { char outbuff[20]; memcpy(outbuff, "Formatting FLASH\n", 17); VCP_write(outbuff, 17); FlashChipErase(); EEPROMInit(); memcpy(outbuff, "Done\n", 5); VCP_write(outbuff, 5); }
// // SetupInit - Initialize setup at system start // // Inputs: None. // // Outputs: None. // void SetupInit(void) { EEPROMInit(); // // If uninitialized, or if version mismatch (we're the newer version), initialize // with defaults // if( EEPROM.Version != EEPROM_CURR_VERSION ) { for( CurrSetup = 0; CurrSetup < MAX_SETUPS; CurrSetup++ ) memcpy_P(&EEPROM.Setups[CurrSetup],&SetupDefaults,sizeof(SetupDefaults)); EEPROM.Version = EEPROM_CURR_VERSION; EEPROMWrite(); } LoadSetup(0); }
void zotmain( void ) { unsigned int i; //Cache CacheEnable(); //IO IOInit(); //SPI FLASH INIT AT91F_SpiInit(); //READ F/W Version read_version(); //EEPROM EEPROMInit(); //LWIP zot_network_init(); //MAC LanPktInit(); star_nic_lan_init(); LanPktStart(); //Print Server module IPXInitialize(); NETBEUInit(); ps_init(); //LED LED_Init(); }
static int DrvInit() { int nLen; BurnSetRefreshRate(CAVE_REFRESHRATE); // Find out how much memory is needed Mem = NULL; MemIndex(); nLen = MemEnd - (unsigned char *)0; if ((Mem = (unsigned char *)malloc(nLen)) == NULL) { return 1; } memset(Mem, 0, nLen); // blank all memory MemIndex(); // Index the allocated memory EEPROMInit(1024, 16); // EEPROM has 1024 bits, uses 16-bit words // Load the roms into memory if (LoadRoms()) { return 1; } { SekInit(0, 0x68000); // Allocate 68000 SekOpen(0); // Map 68000 memory: SekMapMemory(Rom01, 0x000000, 0x07FFFF, SM_ROM); // CPU 0 ROM SekMapMemory(Ram01, 0x100000, 0x10FFFF, SM_RAM); SekMapMemory(CaveTileRAM[1], 0x200000, 0x207FFF, SM_RAM); SekMapMemory(CaveTileRAM[0], 0x300000, 0x307FFF, SM_RAM); SekMapMemory(CaveTileRAM[2] + 0x4000, 0x400000, 0x403FFF, SM_RAM); SekMapMemory(CaveTileRAM[2] + 0x4000, 0x404000, 0x407FFF, SM_RAM); SekMapMemory(CaveSpriteRAM, 0x500000, 0x50FFFF, SM_RAM); SekMapMemory(CavePalSrc, 0xA08000, 0xA08FFF, SM_RAM); // Palette RAM SekSetReadWordHandler(0, donpachiReadWord); SekSetReadByteHandler(0, donpachiReadByte); SekSetWriteWordHandler(0, donpachiWriteWord); SekSetWriteByteHandler(0, donpachiWriteByte); SekClose(); } CavePalInit(); CaveTileInit(); CaveSpriteInit(0, 0x0800000); CaveTileInitLayer(0, 0x200000, 8, 0x4000); CaveTileInitLayer(1, 0x200000, 8, 0x4000); CaveTileInitLayer(2, 0x080000, 8, 0x4000); MSM6295Init(0, 8000, 50.0, 0); MSM6295Init(1, 16000, 50.0, 0); MSM6295SampleData[0][0] = MSM6295ROM + 0x100000; MSM6295SampleInfo[0][0] = MSM6295ROM + 0x100000 + 0x0000; MSM6295SampleData[0][1] = MSM6295ROM + 0x100000; MSM6295SampleInfo[0][1] = MSM6295ROM + 0x100000 + 0x0100; MSM6295SampleData[0][2] = MSM6295ROM + 0x100000; MSM6295SampleInfo[0][2] = MSM6295ROM + 0x100000 + 0x0200; MSM6295SampleData[0][3] = MSM6295ROM + 0x100000; MSM6295SampleInfo[0][3] = MSM6295ROM + 0x100000 + 0x0300; bDrawScreen = true; #if defined FBA_DEBUG && defined USE_SPEEDHACKS bprintf(PRINT_IMPORTANT, _T(" * Using speed-hacks (detecting idle loops).\n")); #endif DrvDoReset(); // Reset machine return 0; }
static int DrvInit(int (*LoadCallback)(), int type, int gfx_max, int gfx_min) { AllMem = NULL; MemIndex(gfx_max - gfx_min); int nLen = MemEnd - (unsigned char *)0; if ((AllMem = (unsigned char *)malloc(nLen)) == NULL) return 1; memset(AllMem, 0, nLen); MemIndex(gfx_max - gfx_min); { if (LoadCallback) { if (LoadCallback()) return 1; } BurnSwap32(DrvSh2ROM, 0x100000); BurnSwapEndian(0x200000); DrvGfxDecode(gfx_max - gfx_min); graphics_min_max[0] = gfx_min; graphics_min_max[1] = gfx_max; } if (type == 0) { Sh2Init(1); Sh2Open(0); Sh2MapMemory(DrvSh2ROM, 0x00000000, 0x000fffff, SM_ROM); Sh2MapMemory(DrvSh2ROM + 0x100000, 0x02000000, 0x020fffff, SM_ROM); Sh2MapMemory(DrvSprRAM, 0x03000000, 0x0300ffff, SM_RAM); Sh2MapMemory(DrvPalRAM, 0x03040000, 0x0304ffff, SM_RAM); Sh2MapMemory(DrvZoomRAM, 0x03050000, 0x0305ffff, SM_ROM); Sh2MapMemory(DrvSh2RAM, 0x06000000, 0x060fffff, SM_RAM); Sh2SetReadByteHandler (0, ps3v1_read_byte); Sh2SetWriteByteHandler(0, ps3v1_write_byte); Sh2SetWriteWordHandler(0, ps3v1_write_word); Sh2SetWriteLongHandler(0, psx_write_long); } else { Sh2Init(1); Sh2Open(0); Sh2MapMemory(DrvSh2ROM, 0x00000000, 0x000fffff, SM_ROM); Sh2MapMemory(DrvSprRAM, 0x04000000, 0x0400ffff, SM_RAM); Sh2MapMemory(DrvPalRAM, 0x04040000, 0x0404ffff, SM_RAM); Sh2MapMemory(DrvZoomRAM, 0x04050000, 0x0405ffff, SM_ROM); Sh2MapMemory(DrvSh2ROM + 0x100000, 0x05000000, 0x0507ffff, SM_ROM); Sh2MapMemory(DrvSh2RAM, 0x06000000, 0x060fffff, SM_RAM); Sh2SetReadByteHandler (0, ps5_read_byte); Sh2SetWriteByteHandler(0, ps5_write_byte); Sh2SetWriteWordHandler(0, ps5_write_word); Sh2SetWriteLongHandler(0, psx_write_long); } Sh2MapHandler(1, 0x06000000 | speedhack_address, 0x0600ffff | speedhack_address, SM_ROM); Sh2SetReadByteHandler (1, hack_read_byte); Sh2SetReadWordHandler (1, hack_read_word); Sh2SetReadLongHandler (1, hack_read_long); BurnYMF278BInit(0, DrvSndROM, &DrvIRQCallback, DrvSynchroniseStream); BurnTimerAttachSh2(28636350); EEPROMInit(&eeprom_interface_93C56); PsikyoshVideoInit(gfx_max, gfx_min); DrvDoReset(); return 0; }
void EEPROM_Init() { SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0); EEPROMInit(); }
int main(void) { GOL_MSG msg; // GOL message structure to interact with GOL Nop(); #if defined(PIC24FJ256DA210_DEV_BOARD) _ANSG8 = 0; /* S1 */ _ANSE9 = 0; /* S2 */ _ANSB5 = 0; /* S3 */ #else ///////////////////////////////////////////////////////////////////////////// // ADC Explorer 16 Development Board Errata (work around 2) // RB15 should be output ///////////////////////////////////////////////////////////////////////////// #ifndef MULTI_MEDIA_BOARD_DM00123 LATBbits.LATB15 = 0; TRISBbits.TRISB15 = 0; #endif #endif ///////////////////////////////////////////////////////////////////////////// #if defined(__dsPIC33F__) || defined(__PIC24H__) // Configure Oscillator to operate the device at 40Mhz // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2 // Fosc= 8M*40(2*2)=80Mhz for 8M input clock PLLFBD = 38; // M=40 CLKDIVbits.PLLPOST = 0; // N1=2 CLKDIVbits.PLLPRE = 0; // N2=2 OSCTUN = 0; // Tune FRC oscillator, if FRC is used // Disable Watch Dog Timer RCONbits.SWDTEN = 0; // Clock switching to incorporate PLL __builtin_write_OSCCONH(0x03); // Initiate Clock Switch to Primary // Oscillator with PLL (NOSC=0b011) __builtin_write_OSCCONL(0x01); // Start clock switching while(OSCCONbits.COSC != 0b011); // Wait for Clock switch to occur // Wait for PLL to lock while(OSCCONbits.LOCK != 1) { }; // Set PMD0 pin functionality to digital AD1PCFGL = AD1PCFGL | 0x1000; #elif defined(__PIC32MX__) INTEnableSystemMultiVectoredInt(); SYSTEMConfigPerformance(GetSystemClock()); #ifdef MULTI_MEDIA_BOARD_DM00123 CPLDInitialize(); CPLDSetGraphicsConfiguration(GRAPHICS_HW_CONFIG); CPLDSetSPIFlashConfiguration(SPI_FLASH_CHANNEL); #endif // #ifdef MULTI_MEDIA_BOARD_DM00123 #endif // #if defined(__dsPIC33F__) || defined(__PIC24H__) GOLInit(); // initialize graphics library & // create default style scheme for GOL #if defined (GFX_PICTAIL_V1) || defined (GFX_PICTAIL_V2) EEPROMInit(); // initialize Exp.16 EEPROM SPI BeepInit(); #else #if defined (USE_SST25VF016) SST25Init(); // initialize GFX3 SST25 flash SPI #endif #endif TouchInit(); // initialize touch screen HardwareButtonInit(); // Initialize the hardware buttons // create default style scheme for GOL TickInit(); // initialize tick counter (for random number generation) // create default style scheme for GOL #if defined(__dsPIC33FJ128GP804__) || defined(__PIC24HJ128GP504__) // If S3 button on Explorer 16 board is pressed calibrate touch screen TRISAbits.TRISA9 = 1; if(PORTAbits.RA9 == 0) { TRISAbits.TRISA9 = 0; TouchCalibration(); TouchStoreCalibration(); } TRISAbits.TRISA9 = 0; #else /** * Force a touchscreen calibration by pressing the switch * Explorer 16 + GFX PICTail - S3 (8 bit PMP) * Explorer 16 + GFX PICTail - S5 (16 bit PMP) * Starter Kit + GFX PICTail - S0 (8 bit PMP) * Multimedia Expansion Board - Fire Button * DA210 Developement Board - S1 * NOTE: Starter Kit + GFX PICTail will switches are shared * with the 16 bit PMP data bus. **/ if(GetHWButtonTouchCal() == HW_BUTTON_PRESS) { TouchCalibration(); TouchStoreCalibration(); } #endif // If it's a new board (EEPROM_VERSION byte is not programed) calibrate touch screen #if defined (GFX_PICTAIL_V1) || defined (GFX_PICTAIL_V2) if(GRAPHICS_LIBRARY_VERSION != EEPROMReadWord(ADDRESS_VERSION)) { TouchCalibration(); TouchStoreCalibration(); } #else #if defined (USE_SST25VF016) if(GRAPHICS_LIBRARY_VERSION != SST25ReadWord(ADDRESS_VERSION)) { TouchCalibration(); TouchStoreCalibration(); } #elif defined (USE_SST39LF400) WORD tempArray[12], tempWord = 0x1234; SST39LF400Init(tempArray); tempWord = SST39LF400ReadWord(ADDRESS_VERSION); SST39LF400DeInit(tempArray); if(GRAPHICS_LIBRARY_VERSION != tempWord) { TouchCalibration(); TouchStoreCalibration(); } #endif #endif // Load touch screen calibration parameters from memory TouchLoadCalibration(); GDDDemoCreateFirstScreen(); while(1) { if(GOLDraw()) // Draw GOL object { TouchGetMsg(&msg); // Get message from touch screen #if (NUM_GDD_SCREENS > 1) // GDD Readme: // The following line of code allows a GDD user to touch the touchscreen // to cycle through different static screens for viewing. This is useful as a // quick way to view how each screen looks on the physical target hardware. // This line of code should eventually be commented out for actual development. // Also note that widget/object names can be found in GDD_Screens.h if(msg.uiEvent == EVENT_RELEASE) GDDDemoNextScreen(); #endif GOLMsg(&msg); // Process message } }//end while }
int main(void) { char stringbuffer[17]; int distance = 0; // Enable lazy stacking for interrupt handlers. This allows floating-point // instructions to be used within interrupt handlers, but at the expense of // extra stack usage. ROM_FPUEnable(); ROM_FPULazyStackingEnable(); ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART3); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0); //This wasn't clear at all. Note to self, everything needs enabling on this chip. ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE); ROM_GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, WAKEPIN); ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, REEDPIN); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, V5POWER); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, V3POWER); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, SERVO); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); // ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3); ROM_GPIOPinWrite(GPIO_PORTE_BASE, V3POWER, 0xFF); #ifdef EASYOPEN ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0); #endif GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); GPIOPinConfigure(GPIO_PC6_U3RX); GPIOPinConfigure(GPIO_PC7_U3TX); ROM_GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_6 | GPIO_PIN_7); ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), GPSBAUD, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); ROM_UARTConfigSetExpClk(UART3_BASE, ROM_SysCtlClockGet(), GPSBAUD, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); ROM_IntEnable(INT_UART0); ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT); ROM_IntEnable(INT_UART3); ROM_UARTIntEnable(UART3_BASE, UART_INT_RX | UART_INT_RT); SysTickPeriodSet(SysCtlClockGet()/10000); SysTickIntRegister(&ServoDriver); SysTickIntEnable(); SysTickEnable(); ROM_IntMasterEnable(); GPIOIntTypeSet(GPIO_PORTA_BASE, REEDPIN, GPIO_FALLING_EDGE); GPIOPinIntClear(GPIO_PORTA_BASE, REEDPIN); GPIOPinIntEnable(GPIO_PORTA_BASE, REEDPIN); IntEnable(INT_GPIOA); // while(1){} /* SysCtlDelay(SysCtlClockGet()/1000);//Make sure the servo is going to get a pulse soon. ROM_GPIOPinWrite(GPIO_PORTE_BASE, V5POWER, 0xFF); //Turn on the 5V power to LCD + servo. SysCtlDelay(SysCtlClockGet()/1000);//Make sure the servo is going to get a pulse soon.*/ EEPROMInit(); initLCD(); LCDCommand(0x0c); #ifdef LOOPBACKUART while(1){} #endif #ifdef FIRSTRUN //First run, sets the eeprom to have as many tries as is desired. EEPROMMassErase(); EEPROMProgram(&initialNumTries,eepromAddress,sizeof(initialNumTries)); LCDWriteText("Setup Complete. ", 0, 0); LCDWriteText("Reflash Firmware", 1, 0); while (1){} //Don't want to do anything else now. #endif EEPROMRead(&numTrieslong,eepromAddress,sizeof(numTrieslong)); // numTries=(int)numTrieslong; // openLock(); // numTrieslong=0; if (numTrieslong > initialNumTries-3) //Has already opened once, so just open as needed if stuck. { openLock(); numTrieslong--; EEPROMProgram(&numTrieslong,eepromAddress,sizeof(numTrieslong)); //Decrement EEPROM counter. } else { distance = getDistance(); if(distance==99999){ //No fix :/ LCDWriteText("Location unknown", 0, 0); LCDWriteText("Take me outside ", 1, 0); SysCtlDelay(SysCtlClockGet()); //Waits 3 seconds. } else if (distance>NEARENOUGH) //Valid fix, too far away. { if ((int)numTrieslong>0) //Any attempts remaining? { usnprintf(stringbuffer,17,"Distance: %4dm ",distance); LCDWriteText(stringbuffer, 0, 0); numTrieslong--; // numTries=(int)numTrieslong; EEPROMProgram(&numTrieslong,eepromAddress,sizeof(numTrieslong)); //Decrement EEPROM counter. usnprintf(stringbuffer,17,"%2d Attempts left",(int)numTrieslong); LCDWriteText(stringbuffer, 1, 0); SysCtlDelay(SysCtlClockGet()*2); } else { LCDWriteText("Oh dear... ", 0, 0); //Not really sure what to do, hopefully this code never runs. LCDWriteText("Opening anyway. ", 1, 0); // numTrieslong=initialNumTries+1; // EEPROMProgram(&numTrieslong,eepromAddress,sizeof(initialNumTries)); //Set to big value SysCtlDelay(10*SysCtlClockGet()/3); openLock(); } } else //Found the location! { openLock(); numTrieslong=initialNumTries+1; //numTries=(int)numTrieslong; EEPROMProgram(&numTrieslong,eepromAddress,sizeof(initialNumTries)); //Lock will now open straight away. } } // BLINK(RED); HibernateEnableExpClk(SysCtlClockGet()); HibernateGPIORetentionEnable(); //Enables GPIO retention after wake from hibernate. HibernateClockSelect(HIBERNATE_CLOCK_SEL_RAW); HibernateWakeSet(HIBERNATE_WAKE_PIN); HibernateIntRegister(&HibernateInterrupt); HibernateIntEnable(HIBERNATE_INT_PIN_WAKE); //BLINK(BLUE); ROM_GPIOPinWrite(GPIO_PORTE_BASE, V5POWER, 0); //GPIO pins keep state on hibernate, so make sure to power everything else down. ROM_GPIOPinWrite(GPIO_PORTE_BASE, V3POWER, 0); //GPIO pins keep state on hibernate, so make sure to power everything else down. ROM_GPIOPinWrite(GPIO_PORTB_BASE, RS|E|D4|D5|D6|D7, 0xFF); //Pull all data pins to LCD high so we're not phantom powering it through ESD diodes. ROM_GPIOPinWrite(GPIO_PORTF_BASE, SERVO, 0xFF); //Likewise for the servo SysCtlDelay(SysCtlClockGet()/6); HibernateRequest();// we want to be looping'n'shit. while(1){} //Lalala, I'm a sleeping right now. }
//***************************************************************************** // // Main application entry point. // //***************************************************************************** int main(void) { int_fast32_t i32IPart[17], i32FPart[17]; uint_fast32_t ui32Idx, ui32CompDCMStarted; float pfData[17]; float *pfAccel, *pfGyro, *pfMag, *pfEulers, *pfQuaternion; float *direction; // // Initialize convenience pointers that clean up and clarify the code // meaning. We want all the data in a single contiguous array so that // we can make our pretty printing easier later. // pfAccel = pfData; pfGyro = pfData + 3; pfMag = pfData + 6; pfEulers = pfData + 9; pfQuaternion = pfData + 12; direction = pfData + 16; // // Setup the system clock to run at 40 Mhz from PLL with crystal reference // ROM_SysCtlClockSet( SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); // // Enable port E used for motion interrupt. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); // // Enable port F used for calibration. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // // Initialize the UART. // ConfigureUART(); /* EEPROM SETTINGS */ SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0); // EEPROM activate EEPROMInit(); // EEPROM start // // Print the welcome message to the terminal. // UARTprintf("\033[2JMPU9150 Raw Example\n"); // // Set the color to a purple approximation. // g_pui32Colors[RED] = 0x8000; g_pui32Colors[BLUE] = 0x8000; g_pui32Colors[GREEN] = 0x8000; // // Initialize RGB driver. // RGBInit(0); RGBColorSet(g_pui32Colors); RGBIntensitySet(0.5f); RGBEnable(); // Initialize BGLib bglib_output = output; ConfigureBLE(); // // The I2C3 peripheral must be enabled before use. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); // // Configure the pin muxing for I2C3 functions on port D0 and D1. // ROM_GPIOPinConfigure(GPIO_PD0_I2C3SCL); ROM_GPIOPinConfigure(GPIO_PD1_I2C3SDA); // // Select the I2C function for these pins. This function will also // configure the GPIO pins pins for I2C operation, setting them to // open-drain operation with weak pull-ups. Consult the data sheet // to see which functions are allocated per pin. // GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0); ROM_GPIOPinTypeI2C(GPIO_PORTD_BASE, GPIO_PIN_1); // // Configure and Enable the GPIO interrupt. Used for INT signal from the // MPU9150 // ROM_GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_2); GPIOIntEnable(GPIO_PORTE_BASE, GPIO_PIN_2); ROM_GPIOIntTypeSet(GPIO_PORTE_BASE, GPIO_PIN_2, GPIO_FALLING_EDGE); ROM_IntEnable(INT_GPIOE); // // Keep only some parts of the systems running while in sleep mode. // GPIOE is for the MPU9150 interrupt pin. // UART0 is the virtual serial port // TIMER0, TIMER1 and WTIMER5 are used by the RGB driver // I2C3 is the I2C interface to the ISL29023 // ROM_SysCtlPeripheralClockGating(true); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOE); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_UART0); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER0); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER1); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_I2C3); ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_WTIMER5); // // Enable interrupts to the processor. // ROM_IntMasterEnable(); // // Initialize I2C3 peripheral. // I2CMInit(&g_sI2CInst, I2C3_BASE, INT_I2C3, 0xff, 0xff, ROM_SysCtlClockGet()); // // Initialize the MPU9150 Driver. // MPU9150Init(&g_sMPU9150Inst, &g_sI2CInst, MPU9150_I2C_ADDRESS, MPU9150AppCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MPU9150AppI2CWait(__FILE__, __LINE__); // // Configure the sampling rate to 1000 Hz / (1+24). // g_sMPU9150Inst.pui8Data[0] = 24; MPU9150Write(&g_sMPU9150Inst, MPU9150_O_SMPLRT_DIV, g_sMPU9150Inst.pui8Data, 1, MPU9150AppCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MPU9150AppI2CWait(__FILE__, __LINE__); // // Write application specifice sensor configuration such as filter settings // and sensor range settings. // g_sMPU9150Inst.pui8Data[0] = MPU9150_CONFIG_DLPF_CFG_94_98; g_sMPU9150Inst.pui8Data[1] = MPU9150_GYRO_CONFIG_FS_SEL_250; g_sMPU9150Inst.pui8Data[2] = (MPU9150_ACCEL_CONFIG_ACCEL_HPF_5HZ | MPU9150_ACCEL_CONFIG_AFS_SEL_2G); // g_sMPU9150Inst.pui8Data[2] = MPU9150_ACCEL_CONFIG_AFS_SEL_2G; MPU9150Write(&g_sMPU9150Inst, MPU9150_O_CONFIG, g_sMPU9150Inst.pui8Data, 3, MPU9150AppCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MPU9150AppI2CWait(__FILE__, __LINE__); // // Configure the data ready interrupt pin output of the MPU9150. // g_sMPU9150Inst.pui8Data[0] = MPU9150_INT_PIN_CFG_INT_LEVEL | MPU9150_INT_PIN_CFG_INT_RD_CLEAR | MPU9150_INT_PIN_CFG_LATCH_INT_EN; g_sMPU9150Inst.pui8Data[1] = MPU9150_INT_ENABLE_DATA_RDY_EN; MPU9150Write(&g_sMPU9150Inst, MPU9150_O_INT_PIN_CFG, g_sMPU9150Inst.pui8Data, 2, MPU9150AppCallback, &g_sMPU9150Inst); // // Wait for transaction to complete // MPU9150AppI2CWait(__FILE__, __LINE__); // // Initialize the DCM system. 40 hz sample rate. // accel weight = .2, gyro weight = .8, mag weight = .2 // CompDCMInit(&g_sCompDCMInst, 1.0f / 40.0f, 0.2f, 0.6f, 0.2f); // // Enable blinking indicates config finished successfully // RGBBlinkRateSet(1.0f); // // Configure and Enable the GPIO interrupt. Used for calibration // HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY; HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= 0x01; ROM_GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4); GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); ROM_IntEnable(INT_GPIOF); ROM_GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_FALLING_EDGE); GPIOIntEnable(GPIO_PORTF_BASE, GPIO_PIN_4); g_calibrationState = 0; ui32CompDCMStarted = 0; // Configure the white noise, read the error from EEPROM EEPROMRead((uint32_t *) zeroErrorAccel, EEPROM_ZERO_ERROR_ACCELERATION_ADDRESS, 12); EEPROMRead((uint32_t *) linearErrorAccel, EEPROM_LINEAR_ERROR_ACCELERATION_ADDRESS, 12); EEPROMRead((uint32_t *) zeroErrorGyro, EEPROM_ZERO_ERROR_GYROSCOPE_ADDRESS, 12); while (1) { // // Go to sleep mode while waiting for data ready. // while (!g_vui8I2CDoneFlag) { //ROM_SysCtlSleep(); } // // Clear the flag // g_vui8I2CDoneFlag = 0; // // Get floating point version of the Accel Data in m/s^2. // MPU9150DataAccelGetFloat(&g_sMPU9150Inst, pfAccel, pfAccel + 1, pfAccel + 2); // // Get floating point version of angular velocities in rad/sec // MPU9150DataGyroGetFloat(&g_sMPU9150Inst, pfGyro, pfGyro + 1, pfGyro + 2); // // Get floating point version of magnetic fields strength in tesla // MPU9150DataMagnetoGetFloat(&g_sMPU9150Inst, pfMag, pfMag + 1, pfMag + 2); if (g_calibrationState == 2) { zeroErrorAccel[0] = (pfAccel[0] + zeroErrorAccel[0] * g_calibrationCount) / (g_calibrationCount + 1); zeroErrorAccel[1] = (pfAccel[1] + zeroErrorAccel[1] * g_calibrationCount) / (g_calibrationCount + 1); accelAtGravity[2] = (pfAccel[2] + accelAtGravity[2] * g_calibrationCount) / (g_calibrationCount + 1); zeroErrorGyro[0] = (pfGyro[0] + zeroErrorGyro[0] * g_calibrationCount) / (g_calibrationCount + 1); zeroErrorGyro[1] = (pfGyro[1] + zeroErrorGyro[1] * g_calibrationCount) / (g_calibrationCount + 1); zeroErrorGyro[2] = (pfGyro[2] + zeroErrorGyro[2] * g_calibrationCount) / (g_calibrationCount + 1); g_calibrationCount++; if (g_calibrationCount > 500) { Calibration(); } continue; } else if (g_calibrationState == 4) { zeroErrorAccel[2] = (pfAccel[2] + zeroErrorAccel[2] * g_calibrationCount) / (g_calibrationCount + 1); accelAtGravity[1] = (pfAccel[1] + accelAtGravity[1] * g_calibrationCount) / (g_calibrationCount + 1); g_calibrationCount++; if (g_calibrationCount > 500) { Calibration(); } continue; } else if (g_calibrationState == 6) { accelAtGravity[0] = (pfAccel[0] + accelAtGravity[0] * g_calibrationCount) / (g_calibrationCount + 1); g_calibrationCount++; if (g_calibrationCount > 500) { Calibration(); } continue; } // Cancel out white noise // pfAccel[0] = pfAccel[0] - zeroErrorAccel[0]; // pfAccel[1] = pfAccel[1] - zeroErrorAccel[1]; // pfAccel[2] = pfAccel[2] - zeroErrorAccel[2]; // pfGyro[0] = pfGyro[0] - zeroErrorGyro[0]; // pfGyro[1] = pfGyro[1] - zeroErrorGyro[1]; // pfGyro[2] = pfGyro[2] - zeroErrorGyro[2]; // // Straighten out linear noise // pfAccel[0] = pfAccel[0] * (1 + linearErrorAccel[0]); // pfAccel[1] = pfAccel[1] * (1 + linearErrorAccel[1]); // pfAccel[2] = pfAccel[2] * (1 + linearErrorAccel[2]); // // Check if this is our first data ever. // if (ui32CompDCMStarted == 0) { // // Set flag indicating that DCM is started. // Perform the seeding of the DCM with the first data set. // ui32CompDCMStarted = 1; CompDCMMagnetoUpdate(&g_sCompDCMInst, pfMag[0], pfMag[1], pfMag[2]); CompDCMAccelUpdate(&g_sCompDCMInst, pfAccel[0], pfAccel[1], pfAccel[2]); CompDCMGyroUpdate(&g_sCompDCMInst, pfGyro[0], pfGyro[1], pfGyro[2]); CompDCMStart(&g_sCompDCMInst); } else { // // DCM Is already started. Perform the incremental update. // CompDCMMagnetoUpdate(&g_sCompDCMInst, pfMag[0], pfMag[1], pfMag[2]); CompDCMAccelUpdate(&g_sCompDCMInst, pfAccel[0], pfAccel[1], pfAccel[2]); CompDCMGyroUpdate(&g_sCompDCMInst, -pfGyro[0], -pfGyro[1], -pfGyro[2]); CompDCMUpdate(&g_sCompDCMInst); } // // Increment the skip counter. Skip counter is used so we do not // overflow the UART with data. // g_ui32PrintSkipCounter++; if (g_ui32PrintSkipCounter >= PRINT_SKIP_COUNT) { // // Reset skip counter. // g_ui32PrintSkipCounter = 0; // // Get Euler data. (Roll Pitch Yaw) // CompDCMComputeEulers(&g_sCompDCMInst, pfEulers, pfEulers + 1, pfEulers + 2); // // Get Quaternions. // CompDCMComputeQuaternion(&g_sCompDCMInst, pfQuaternion); // // convert mag data to micro-tesla for better human interpretation. // pfMag[0] *= 1e6; pfMag[1] *= 1e6; pfMag[2] *= 1e6; // // Convert Eulers to degrees. 180/PI = 57.29... // Convert Yaw to 0 to 360 to approximate compass headings. // pfEulers[0] *= 57.295779513082320876798154814105f; pfEulers[1] *= 57.295779513082320876798154814105f; pfEulers[2] *= 57.295779513082320876798154814105f; if (pfEulers[2] < 0) { pfEulers[2] += 360.0f; } // Use pfMag to display degrees of the Magnetomer's x-axis // (y-axis of accelerometer and gyroscope) to the east of // magnetic north pole // direction[0] = 0; // if (pfMag[1] == 0) { // if (pfMag[0] > 0) { // direction[0] = 0; // } else { // direction[0] = 180; // } // } else if (pfMag[1] > 0) { // direction[0] = 90 - atan2f(pfMag[0], pfMag[1]) * 180 / 3.14159265359; // } else if (pfMag[1] < 0) { // direction[0] = 270 - atan2f(pfMag[0], pfMag[1]) * 180 / 3.14159265359; // } // // Now drop back to using the data as a single array for the // purpose of decomposing the float into a integer part and a // fraction (decimal) part. // for (ui32Idx = 0; ui32Idx < 17; ui32Idx++) { // // Conver float value to a integer truncating the decimal part. // i32IPart[ui32Idx] = (int32_t) pfData[ui32Idx]; // // Multiply by 1000 to preserve first three decimal values. // Truncates at the 3rd decimal place. // i32FPart[ui32Idx] = (int32_t) (pfData[ui32Idx] * 1000.0f); // // Subtract off the integer part from this newly formed decimal // part. // i32FPart[ui32Idx] = i32FPart[ui32Idx] - (i32IPart[ui32Idx] * 1000); // // make the decimal part a positive number for display. // if (i32FPart[ui32Idx] < 0) { i32FPart[ui32Idx] *= -1; } } if (g_bleUserFlag == 1) { g_bleFlag = 0; ble_cmd_attributes_write(58, 0, 12, (uint8_t*)pfEuler); while (g_bleFlag == 0) { } } else if (g_bleDisconnectFlag == 1) { ConfigureBLE(); } // // Print the acceleration numbers in the table. // // UARTprintf("%3d.%03d, ", i32IPart[0], i32FPart[0]); // UARTprintf("%3d.%03d, ", i32IPart[1], i32FPart[1]); // UARTprintf("%3d.%03d\n", i32IPart[2], i32FPart[2]); // // // // // Print the angular velocities in the table. // // // UARTprintf("%3d.%03d, ", i32IPart[3], i32FPart[3]); // UARTprintf("%3d.%03d, ", i32IPart[4], i32FPart[4]); // UARTprintf("%3d.%03d\n", i32IPart[5], i32FPart[5]); // // // // // Print the magnetic data in the table. // // // UARTprintf("%3d.%03d, ", i32IPart[6], i32FPart[6]); // UARTprintf("%3d.%03d, ", i32IPart[7], i32FPart[7]); // UARTprintf("%3d.%03d\n", i32IPart[8], i32FPart[8]); // // // // // Print the direction in the table. // // // UARTprintf("%3d.%03d\n", i32IPart[16], i32FPart[16]); // // // // Print the Eulers in a table. // // // UARTprintf("%3d.%03d, ", i32IPart[9], i32FPart[9]); // UARTprintf("%3d.%03d, ", i32IPart[10], i32FPart[10]); // UARTprintf("%3d.%03d\n", i32IPart[11], i32FPart[11]); // // // // // Print the quaternions in a table format. // // // UARTprintf("\033[19;14H%3d.%03d", i32IPart[12], i32FPart[12]); // UARTprintf("\033[19;32H%3d.%03d", i32IPart[13], i32FPart[13]); // UARTprintf("\033[19;50H%3d.%03d", i32IPart[14], i32FPart[14]); // UARTprintf("\033[19;68H%3d.%03d", i32IPart[15], i32FPart[15]); } } }
int main(void) { // Start from displaying of PIC24 banners _display_state = DISP_HELLO; // Setup PortA IOs as digital AD1PCFG = 0xffff; //IO Mapping for PIC24FJ64GA004 #ifdef __PIC24FJ64GA004__ //Defined by MPLAB when using 24FJ64GA004 device ioMap(); lockIO(); #endif // Setup SPI to communicate to EEPROM SPIMPolInit(); // Setup EEPROM IOs EEPROMInit(); // Setup the UART UART2Init(); // Setup the timer TimerInit(); // Setup the LCD mLCDInit(); // Setup debounce processing BtnInit(); // Setup the ADC ADCInit(); // Setup the banner processing BannerStart(); // Setup the RTCC RTCCInit(); while (1) { LCDProcessEvents(); ADCProcessEvents(); if (TimerIsOverflowEvent()){ // Button debounce processing BtnProcessEvents(); // State dependent processing switch (_display_state) { // Show Microchip banners case DISP_HELLO: BannerProcessEvents(); break; // Show clock case DISP_CLOCK: TBannerProcessEvents(); break; // Show voltage and temperature case DISP_VOLTAGE: VBannerProcessEvents(); break; default: _display_state = DISP_HELLO; }// End of switch (_display_state)... // If S6 is pressed show the next example if (BtnIsPressed(4)) { // Change state and clear display if(!TBannerIsSetup()){ _display_state++; if(_display_state > DISP_MAX) _display_state = 0; // Initialize state switch (_display_state) { // Microchip banners case DISP_HELLO: BannerInit(); break; // Clock case DISP_CLOCK: TBannerInit(); break; // Voltage and temperature case DISP_VOLTAGE: VBannerInit(); break; default: _display_state = 0; }// End of switch (_display_state)... mLCDClear(); }else TBannerNext(); // wait for button released while (BtnIsPressed(4)){ BtnProcessEvents(); } }// End of if (BtnIsPressed(4)){... if(_display_state == DISP_CLOCK){ if (BtnIsPressed(1)){ TBannerSetup(); // wait for button released while (BtnIsPressed(1)) BtnProcessEvents(); }// End of if (BtnIsPressed(1 ... if(TBannerIsSetup()){ if (BtnIsPressed(2)) { TBannerChangeField(1); // wait for button released while (BtnIsPressed(2)) BtnProcessEvents(); }// End of if (BtnIsPressed(2)){... if (BtnIsPressed(3)) { // wait for button released TBannerChangeField(0); while (BtnIsPressed(3)) BtnProcessEvents(); }// End of if (BtnIsPressed(3)){... }// End of if(TBannerIsSetup( ... }// End of if(_display_state == DISP_SET_CLOCK ... if(_display_state == DISP_VOLTAGE){ if (BtnIsPressed(2)){ ADCSetFromMemory(); // wait for button released while (BtnIsPressed(2)){ BtnProcessEvents(); } }// End of if (BtnIsPressed(2 ... if (BtnIsPressed(3)){ ADCStoreTemperature(); // wait for button released while (BtnIsPressed(3)){ BtnProcessEvents(); } }// End of if (BtnIsPressed(3)){... }// End of if(_display_state ... }// End of if (TimerIsOverflowEvent()... }// End of while(1)... }// End of main()...