/* Configure ADC ROM Driver and pripheral */ static int adcrom_config(void) { ADC_CFG_T cfg = { ADC_SEQ_A_CONFIG, ADC_SEQ_B_CONFIG, ADC_CONFIG, 0 /* Divider will be calculated during run time */ }; cfg.clkDiv = 0xFF; /* Configure the ADC */ ROM_ADC_Configure(hADC, &cfg); /* Calibrate the ADC */ if (ROM_ADC_Calibrate(hADC, Chip_Clock_GetSystemClockRate()) != LPC_OK) { DEBUGSTR("ERROR: Calibrating ADC\r\n"); while (1) {} } DEBUGSTR("ADC Initialized and Calibrated successfully!\r\n"); /* Channel configurations */ ROM_ADC_ConfigureCh(hADC, 0, ADC_CH_THRES_DATA | ADC_CH_THRES_SEL1 | ADC_CH_THRES_CROSSING); ROM_ADC_ConfigureCh(hADC, 1, ADC_CH_THRES_DATA); return 0; }
/** * @brief Initialization 32-bit Timer 0 * @return Nothing */ void Timer32_0_Init(uint32_t tickrate) { uint32_t timerFreq; /* Initialize 32-bit timer 0 clock */ Chip_TIMER_Init(LPC_TIMER32_0); /* Timer rate is system clock rate */ timerFreq = Chip_Clock_GetSystemClockRate(); /* Timer setup for match and interrupt at TICKRATE_HZ */ Chip_TIMER_Reset(LPC_TIMER32_0); /* Enable timer to generate interrupts when time matches */ Chip_TIMER_MatchEnableInt(LPC_TIMER32_0, 1); /* Setup 32-bit timer's duration (32-bit match time) */ Chip_TIMER_SetMatch(LPC_TIMER32_0, 1, (timerFreq / tickrate)); /* Setup timer to restart when match occurs */ Chip_TIMER_ResetOnMatchEnable(LPC_TIMER32_0, 1); /* Start timer */ Chip_TIMER_Enable(LPC_TIMER32_0); /* Clear timer of any pending interrupts */ NVIC_ClearPendingIRQ(TIMER_32_0_IRQn); /* Enable timer interrupt */ NVIC_EnableIRQ(TIMER_32_0_IRQn); }
/* Get divider value */ STATIC uint8_t getClkDiv(LPC_ADC_T *pADC, bool burstMode, uint32_t adcRate, uint8_t clks) { uint32_t adcBlockFreq; uint32_t fullAdcRate; uint8_t div; /* The APB clock (PCLK_ADC0) is divided by (CLKDIV+1) to produce the clock for A/D converter, which should be less than or equal to 4.5MHz. A fully conversion requires (bits_accuracy+1) of these clocks. ADC Clock = PCLK_ADC0 / (CLKDIV + 1); ADC rate = ADC clock / (the number of clocks required for each conversion); */ adcBlockFreq = Chip_Clock_GetSystemClockRate(); if (burstMode) { fullAdcRate = adcRate * clks; } else { fullAdcRate = adcRate * getFullConvClk(); } /* Get the round value by fomular: (2*A + B)/(2*B) */ div = ((adcBlockFreq * 2 + fullAdcRate) / (fullAdcRate * 2)) - 1; return div; }
void InicTimers(void){ uint32_t timerFreq; //inicializo los timers para la maquina de estado Chip_TIMER_Init(LPC_TIMER2); //tomo la frecuencia del clok timerFreq = Chip_Clock_GetSystemClockRate(); //confinfiguro el timer 2 y el match 1 Chip_TIMER_Reset(LPC_TIMER2); Chip_TIMER_MatchEnableInt(LPC_TIMER2, 1); Chip_TIMER_SetMatch(LPC_TIMER2, 1, 250); Chip_TIMER_ResetOnMatchEnable(LPC_TIMER2, 1); Chip_TIMER_Enable(LPC_TIMER2); //habilito interrupciones NVIC_ClearPendingIRQ(TIMER2_IRQn); NVIC_EnableIRQ(TIMER2_IRQn); }
void IrTherm_Init(void){ /* Enable clocks to SWM and IOCON to save power */ Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM); Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_IOCON); Chip_SWM_MovablePinAssign(SWM_I2C_SDA_IO, 11); Chip_SWM_MovablePinAssign(SWM_I2C_SCL_IO, 10); #if (I2C_BITRATE > 400000) /* Enable Fast Mode Plus for I2C pins */ Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO10, PIN_I2CMODE_FASTPLUS); Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO11, PIN_I2CMODE_FASTPLUS); #else Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO10, PIN_I2CMODE_STDFAST); Chip_IOCON_PinSetI2CMode(LPC_IOCON, IOCON_PIO11, PIN_I2CMODE_STDFAST); #endif /* Enable I2C clock and reset I2C peripheral - the boot ROM does not do this */ Chip_I2C_Init(); /* Setup the I2C handle */ i2cHandleMaster = LPC_I2CD_API->i2c_setup(LPC_I2C_BASE, i2cMasterHandleMEM); /* Set I2C bitrate */ LPC_I2CD_API->i2c_set_bitrate(i2cHandleMaster, Chip_Clock_GetSystemClockRate(), I2C_BITRATE); /* Disable the interrupt for the I2C */ NVIC_DisableIRQ(I2C_IRQn); /* Disable clocks to SWM and IOCON to save power */ Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM); Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_IOCON); }
/* Setup I2C handle and parameters */ static void setupI2CMaster() { /* Enable I2C clock and reset I2C peripheral - the boot ROM does not do this */ Chip_I2C_Init(LPC_I2C); /* Perform a sanity check on the storage allocation */ if (LPC_I2CD_API->i2c_get_mem_size() > sizeof(i2cMasterHandleMEM)) { /* Example only: this should never happen and probably isn't needed for most I2C code. */ errorI2C(); } /* Setup the I2C handle */ i2cHandleMaster = LPC_I2CD_API->i2c_setup(LPC_I2C_BASE, i2cMasterHandleMEM); if (i2cHandleMaster == NULL) { errorI2C(); } /* Set I2C bitrate */ if (LPC_I2CD_API->i2c_set_bitrate(i2cHandleMaster, Chip_Clock_GetSystemClockRate(), I2C_BITRATE) != LPC_OK) { errorI2C(); } }
void initClock() { sysctlPowerUp(SYSCTL_POWERDOWN_SYSOSC_PD); // Enable system oscillator for (volatile int i = 0; i < 1000; i++) { } Chip_Clock_SetSystemPLLSource(SYSCTL_PLLCLKSRC_MAINOSC); sysctlPowerDown(SYSCTL_POWERDOWN_SYSPLL_PD); /* * Setup PLL for main oscillator rate (FCLKIN = 12MHz) * 4 = 48MHz * MSEL = 3 (this is pre-decremented), PSEL = 1 (for P = 2) * FCLKOUT = FCLKIN * (MSEL + 1) = 12MHz * 4 = 48MHz * FCCO = FCLKOUT * 2 * P = 48MHz * 2 * 2 = 192MHz (within FCCO range) */ Chip_Clock_SetupSystemPLL(3, 1); sysctlPowerUp(SYSCTL_POWERDOWN_SYSPLL_PD); while (!Chip_Clock_IsSystemPLLLocked()) { } Chip_Clock_SetSysClockDiv(1); Chip_FMC_SetFLASHAccess(FLASHTIM_50MHZ_CPU); Chip_Clock_SetMainClockSource(SYSCTL_MAINCLKSRC_PLLOUT); SystemCoreClock = Chip_Clock_GetSystemClockRate(); while (SystemCoreClock != 48000000) { } // Loop forever if the clock failed to initialize properly }
// Set the sample frequency (pitch) void Master_Let_SampleFreq(void) { // Has the pitch actually changed? if (Pitch_Get_PitchChanged()==true) { // Calculate new sample frequency. master_sf = Pitch_Get_FreqCalc(); #if PRODUCT!=ATMEGADRUM master_sf *= WAVE_LEN; #endif #if PRODUCT==ATCYCLOTRON master_sf *= TOT_WAVES; //CPV #endif // Convert sample frequency to interrupt ticks. master_ocr1 = Chip_Clock_GetSystemClockRate()/master_sf; //master_ocr1 = F_CPU/master_sf; //convert to interrupt time if (master_ocr1>65535) { // Highest possible value for ocr1 (lowest freq) master_ocr1 = 65535; } if (master_ocr1<MASTER_OCR1_MIN) { // Lowest possible value for ocr1. Any lower and code in interrupt doesn't have time to complete. // Worked out by trial and error. master_ocr1 = MASTER_OCR1_MIN; } //master_ocr1 >>= 2; // cpv } }
/** * @brief main routine for blinky example * @return Function should not exit. */ int main(void) { uint32_t timerFreq; /* Generic Initialization */ SystemCoreClockUpdate(); Board_Init(); /* Enable timer 1 clock */ Chip_TIMER_Init(LPC_TIMER0); /* Timer rate is system clock rate */ timerFreq = Chip_Clock_GetSystemClockRate(); /* Timer setup for match and interrupt at TICKRATE_HZ */ Chip_TIMER_Reset(LPC_TIMER0); Chip_TIMER_MatchEnableInt(LPC_TIMER0, 1); Chip_TIMER_SetMatch(LPC_TIMER0, 1, (timerFreq / TICKRATE_HZ1)); Chip_TIMER_ResetOnMatchEnable(LPC_TIMER0, 1); Chip_TIMER_Enable(LPC_TIMER0); /* Enable timer interrupt */ NVIC_ClearPendingIRQ(TIMER0_IRQn); NVIC_EnableIRQ(TIMER0_IRQn); /* LEDs toggle in interrupt handlers */ while (1) { __WFI(); } return 0; }
/* Calculate the Clock Rate Divider for SPI Peripheral */ uint32_t Chip_SPI_CalClkRateDivider(LPC_SPI_T *pSPI, uint32_t bitRate) { uint32_t SPIClk; uint32_t DivVal = 1; /* Get SPI clock rate */ SPIClk = Chip_Clock_GetSystemClockRate(); /*The peripheral clock for both SPIs is the system clock*/ DivVal = SPIClk / bitRate; return DivVal; }
/* Initialize stopwatch */ void StopWatch_Init(void) { /* Use timer 1. Set prescaler to divide by 8 */ const uint32_t prescaleDivisor = 8; Chip_TIMER_Init(LPC_TIMER32_1); Chip_TIMER_PrescaleSet(LPC_TIMER32_1, prescaleDivisor - 1); Chip_TIMER_Enable(LPC_TIMER32_1); /* Pre-compute tick rate. */ ticksPerSecond = Chip_Clock_GetSystemClockRate() / prescaleDivisor; ticksPerMs = ticksPerSecond / 1000; ticksPerUs = ticksPerSecond / 1000000; }
void setup() { const uint32 system_clock_hz = Chip_Clock_GetSystemClockRate(); const uint32 prescale = system_clock_hz / 1000000; Chip_TIMER_Init(SYS_CLOCK_TIMER32); // Set prescaler for 1 usec/count. Chip_TIMER_PrescaleSet(SYS_CLOCK_TIMER32, prescale - 1); // Reset count Chip_TIMER_Reset(SYS_CLOCK_TIMER32); // Start counting. Chip_TIMER_Enable(SYS_CLOCK_TIMER32); }
/* Set ADC clock rate */ void Chip_ADC_SetClockRate(LPC_ADC_T *pADC, uint32_t rate) { uint32_t div; /* Get ADC clock source to determine base ADC rate. IN sychronous mode, the ADC base clock comes from the system clock. In ASYNC mode, it comes from the ASYNC ADC clock and this function doesn't work. */ div = Chip_Clock_GetSystemClockRate() / rate; if (div == 0) { div = 1; } Chip_ADC_SetDivider(pADC, (uint8_t) div - 1); }
/* Returns the system tick rate as used with the system tick divider */ uint32_t Chip_Clock_GetSysTickClockRate(void) { uint32_t sysRate, div; div = LPC_SYSCON->SYSTICKCLKDIV; /* If divider is 0, the system tick clock is disabled */ if (div == 0) { sysRate = 0; } else { sysRate = Chip_Clock_GetSystemClockRate() / LPC_SYSCON->SYSTICKCLKDIV; } return sysRate; }
/* Initialize stopwatch */ void StopWatch_Init(void) { /* Use timer 1. Set prescaler to divide by 8, should give ticks at 3.75 MHz. That gives a useable stopwatch measurement range of about 19 minutes (if system clock is running at 120 MHz). */ const uint32_t prescaleDivisor = 8; Chip_TIMER_Init(LPC_TIMER1); Chip_TIMER_PrescaleSet(LPC_TIMER1, prescaleDivisor - 1); Chip_TIMER_Enable(LPC_TIMER1); /* Pre-compute tick rate. Note that peripheral clock supplied to the timer includes a fixed divide by 4. */ ticksPerSecond = Chip_Clock_GetSystemClockRate() / prescaleDivisor / 4; ticksPerMs = ticksPerSecond / 1000; ticksPerUs = ticksPerSecond / 1000000; }
void call_init (void) { transmit_call = false; Chip_TIMER_Init (LPC_TIMER32_0); Chip_TIMER_Reset (LPC_TIMER32_0); Chip_TIMER_MatchEnableInt (LPC_TIMER32_0, 0); Chip_TIMER_PrescaleSet (LPC_TIMER32_0, Chip_Clock_GetSystemClockRate ()); Chip_TIMER_SetMatch (LPC_TIMER32_0, 0, CALL_INTERVAL_SEC); Chip_TIMER_ResetOnMatchEnable (LPC_TIMER32_0, 0); Chip_TIMER_Enable (LPC_TIMER32_0); NVIC_ClearPendingIRQ (TIMER_32_0_IRQn); NVIC_EnableIRQ (TIMER_32_0_IRQn); }
/* Setup a timer for a periodic (repeat mode) rate */ static void setupMRT(uint8_t ch, MRT_MODE_T mode, uint32_t rate) { LPC_MRT_CH_T *pMRT; /* Get pointer to timer selected by ch */ pMRT = Chip_MRT_GetRegPtr(ch); /* Setup timer with rate based on MRT clock */ Chip_MRT_SetInterval(pMRT, (Chip_Clock_GetSystemClockRate() / rate) | MRT_INTVAL_LOAD); /* Timer mode */ Chip_MRT_SetMode(pMRT, mode); /* Clear pending interrupt and enable timer */ Chip_MRT_IntClear(pMRT); Chip_MRT_SetEnabled(pMRT); }
/* Set the PWM frequency */ void Chip_SCTPWM_SetRate(LPC_SCT_T *pSCT, uint32_t freq) { uint32_t rate; rate = Chip_Clock_GetSystemClockRate() / freq;; /* Stop the SCT before configuration */ Chip_SCTPWM_Stop(pSCT); /* Set MATCH0 for max limit */ pSCT->REGMODE_U = 0; Chip_SCT_SetMatchCount(pSCT, SCT_MATCH_0, 0); Chip_SCT_SetMatchReload(pSCT, SCT_MATCH_0, rate); pSCT->EV[0].CTRL = 1 << 12; pSCT->EV[0].STATE = 1; /* Set SCT Counter to count 32-bits and reset to 0 after reaching MATCH0 */ Chip_SCT_Config(pSCT, SCT_CONFIG_32BIT_COUNTER | SCT_CONFIG_AUTOLIMIT_L); }
void systeminit_timer0(){ //TODO: sync to beginning of RTC second struct timespec ts; clock_gettime(0,&ts); unixtime = ts.tv_sec; unixtime_nsec_offset = unixtime * (uint64_t)1e9; tickspersecond = Chip_Clock_GetSystemClockRate(); LPC_SYSCON->PCONP|=(1<<1);//powerup Timer 0 LPC_SYSCON->PCLKSEL[0]|=(1<<2);//bits 3:2 = 01 to set PCLK_TIMER0 to run at full speed. reset=00 LPC_TIMER0->MR[1] = tickspersecond; //1s LPC_TIMER0->MR[2] = ~0; LPC_TIMER0->MCR = (0x3<<3)|(0x01<<6);//reset+interrupt on mr1, interrupt on mr2 LPC_TIMER0->CTCR=0;//Timer0 is running in timer mode. LPC_TIMER0->PR=0;//Prescaler=0; timerarm = &timerarm_cb_systime; LPC_TIMER0->IR = (~0);//clear interrupts LPC_TIMER0->TCR=1;//enable NVIC_SetPriority(TIMER0_IRQn,0); NVIC_EnableIRQ(TIMER0_IRQn); timerarm = &timerarm_cb_systime; clock_gettime_cb = &clock_gettime_timer0; nsec_clock_gettime = &nsec_clock_gettime_timer0; }
void main (void) { SystemInit (); Chip_IOCON_PinMuxSet (LPC_IOCON, LED_STATUS_PIO, IOCON_MODE_INACT | IOCON_FUNC0); Chip_GPIO_SetPinDIROutput (LPC_GPIO, LED_STATUS_PORT, LED_STATUS_PIN); Chip_GPIO_SetPinState (LPC_GPIO, LED_STATUS_PORT, LED_STATUS_PIN, false); Chip_TIMER_Init (LPC_TIMER32_0); Chip_TIMER_Reset (LPC_TIMER32_0); Chip_TIMER_MatchEnableInt (LPC_TIMER32_0, 1); Chip_TIMER_SetMatch (LPC_TIMER32_0, 1, (Chip_Clock_GetSystemClockRate () / (1 * 32))); Chip_TIMER_ResetOnMatchEnable (LPC_TIMER32_0, 1); Chip_TIMER_Enable (LPC_TIMER32_0); /* Enable timer interrupt */ NVIC_ClearPendingIRQ (TIMER_32_0_IRQn); NVIC_EnableIRQ (TIMER_32_0_IRQn); while (1) { __WFI (); } }
/* * Wind Speed translation : V = p*(2,25/t) * V: speed in mph * p: pulses per sampelperiod * t: sample period in sec */ void setupirq(){ DBG("Initialize Wind Cups...\n"); Chip_GPIOINT_Init(LPC_GPIOINT); Chip_GPIO_SetPinDIRInput(LPC_GPIO,2,13); Chip_GPIO_SetPinState(LPC_GPIO, 2, 13, true); Chip_IOCON_PinMux(LPC_IOCON, 2, 13, IOCON_MODE_PULLUP, IOCON_FUNC0); const uint32_t prescaleDivisor = 8; Chip_TIMER_Init(LPC_TIMER2); Chip_TIMER_PrescaleSet(LPC_TIMER2, prescaleDivisor - 1); Chip_TIMER_Enable(LPC_TIMER2); ticksPerSecond = Chip_Clock_GetSystemClockRate() / prescaleDivisor / 4; Chip_GPIOINT_SetIntFalling(LPC_GPIOINT, GPIOINT_PORT2, (1 << 13)); Chip_GPIOINT_ClearIntStatus(LPC_GPIOINT, GPIOINT_PORT2, (1 << 13)); NVIC_ClearPendingIRQ(EINT3_IRQn); NVIC_EnableIRQ(EINT3_IRQn); DBG("Initialize Wind Cups complete...\n"); }
/** * @brief Main routine for WWDT example * @return Nothing */ int main(void) { uint32_t wdtFreq; /* Board Setup */ Board_Init(); /* Initialize WWDT (also enables WWDT clock) */ Chip_WWDT_Init(LPC_WWDT); /* Prior to initializing the watchdog driver, the clocking for the watchdog must be enabled. This example uses the watchdog oscillator set at a 50KHz (1Mhz / 20) clock rate. */ Chip_SYSCTL_PowerUp(SYSCTL_POWERDOWN_WDTOSC_PD); Chip_Clock_SetWDTOSC(WDTLFO_OSC_1_05, 20); /* The WDT divides the input frequency into it by 4 */ wdtFreq = Chip_Clock_GetWDTOSCRate() / 4; /* LPC1102/4, LPC11XXLV, and LPC11CXX devices select the watchdog clock source from the SYSCLK block, while LPC11AXX, LPC11EXX, and LPC11UXX devices select the clock as part of the watchdog block. */ /* Select watchdog oscillator for WDT clock source */ #if defined(CHIP_LPC110X) || defined(CHIP_LPC11XXLV) || defined(CHIP_LPC11CXX) || defined(CHIP_LPC11EXX) Chip_Clock_SetWDTClockSource(SYSCTL_WDTCLKSRC_WDTOSC, 1); #else Chip_WWDT_SelClockSource(LPC_WWDT, WWDT_CLKSRC_WATCHDOG_WDOSC); #endif Board_LED_Set(0, false); /* Set watchdog feed time constant to approximately 2s Set watchdog warning time to 512 ticks after feed time constant Set watchdog window time to 3s */ Chip_WWDT_SetTimeOut(LPC_WWDT, wdtFreq * 2); #if !defined(CHIP_LPC11CXX) Chip_WWDT_SetWarning(LPC_WWDT, 512); Chip_WWDT_SetWindow(LPC_WWDT, wdtFreq * 3); #endif #if !defined(CHIP_LPC11CXX) /* Configure WWDT to reset on timeout */ Chip_WWDT_SetOption(LPC_WWDT, WWDT_WDMOD_WDRESET); #endif /* Setup Systick to feed the watchdog timer. This needs to be done * at a rate faster than the WDT warning. */ SysTick_Config(Chip_Clock_GetSystemClockRate() / 50); /* Clear watchdog warning and timeout interrupts */ #if !defined(CHIP_LPC11CXX) Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDTOF | WWDT_WDMOD_WDINT); #else Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDTOF); #endif /* Clear and enable watchdog interrupt */ NVIC_ClearPendingIRQ(WDT_IRQn); NVIC_EnableIRQ(WDT_IRQn); /* Start watchdog */ Chip_WWDT_Start(LPC_WWDT); /* Idle while waiting */ while (1) { __WFI(); } }
/* Update system core clock rate, should be called if the system has a clock rate change */ void SystemCoreClockUpdate(void) { /* CPU core speed */ SystemCoreClock = Chip_Clock_GetSystemClockRate(); }
/* Initialize the LCD controller */ void Chip_LCD_Init(LPC_LCD_T *pLCD, LCD_CONFIG_T *LCD_ConfigStruct) { uint32_t i, regValue, *pPal, proc_clk; uint32_t pcd; Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_LCD); // // Translate and check params from EEPROM values to device values // /* disable the display */ pLCD->CTRL &= ~CLCDC_LCDCTRL_ENABLE; /* Setting LCD_TIMH register */ regValue = ( ((((LCD_ConfigStruct->PPL / 16) - 1) & 0x3F) << 2) | (( (LCD_ConfigStruct->HSW - 1) & 0xFF) << 8) | (( (LCD_ConfigStruct->HFP - 1) & 0xFF) << 16) | (( (LCD_ConfigStruct->HBP - 1) & 0xFF) << 24) ); pLCD->TIMH = regValue; /* Setting LCD_TIMV register */ regValue = ((((LCD_ConfigStruct->LPP - 1) & 0x3FF) << 0) | (((LCD_ConfigStruct->VSW - 1) & 0x03F) << 10) | (((LCD_ConfigStruct->VFP) & 0x0FF) << 16) | (((LCD_ConfigStruct->VBP) & 0x0FF) << 24) ); pLCD->TIMV = regValue; /* Generate the clock and signal polarity control word */ regValue = 0; regValue = (((LCD_ConfigStruct->ACB - 1) & 0x1F) << 6); //regValue |= (LCD_ConfigStruct->IOE & 1) << 14; regValue |= (LCD_ConfigStruct->IPC & 1) << 13; regValue |= (LCD_ConfigStruct->IHS & 1) << 12; regValue |= (LCD_ConfigStruct->IVS & 1) << 11; /* Compute clocks per line based on panel type */ switch (LCD_ConfigStruct->LCD) { case LCD_MONO_4: regValue |= ((((LCD_ConfigStruct->PPL / 4) - 1) & 0x3FF) << 16); break; case LCD_MONO_8: regValue |= ((((LCD_ConfigStruct->PPL / 8) - 1) & 0x3FF) << 16); break; case LCD_CSTN: regValue |= (((((LCD_ConfigStruct->PPL * 3) / 8) - 1) & 0x3FF) << 16); break; case LCD_TFT: default: regValue |= /*1<<26 |*/ (((LCD_ConfigStruct->PPL - 1) & 0x3FF) << 16); } /* panel clock divisor */ //find system clock proc_clk = Chip_Clock_GetSystemClockRate(); pcd = (proc_clk / LCD_ConfigStruct->CLKFREQU + 1) - 2; //12;// LCD_ConfigStruct->pcd; // TODO: should be calculated from LCDDCLK pcd &= 0x3FF; regValue |= pcd;//((pcd >> 5) << 27) | ((pcd) & 0x1F); pLCD->POL = regValue; /* disable interrupts */ pLCD->INTMSK = 0; /* set bits per pixel */ regValue = LCD_ConfigStruct->BPP << 1; /* set color format RGB */ regValue |= LCD_ConfigStruct->color_format << 8; regValue |= LCD_ConfigStruct->LCD << 4;//4; if (LCD_ConfigStruct->Dual == 1) { regValue |= 1 << 7; } pLCD->CTRL = regValue; /* clear palette */ pPal = (uint32_t *) (&(pLCD->PAL)); for (i = 0; i < 128; i++) { *pPal = 0; pPal++; } //pLCD->UPBASE = FRAMEBUFFER_ADDR; //pLCD->CTRL |= (1 << 0); // Enable LCD signals //pLCD->CTRL |= (1 << 11); // Enable LCD power }
int main(void) { char str_buffer[16]; SystemCoreClockUpdate(); gpioInit(); interruptInit(); adcInit(); Chip_PMU_GetSleepFlags(LPC_PMU); Chip_PMU_ClearSleepFlags(LPC_PMU, PMU_PCON_DPDFLAG); Chip_SPI_Init(SPI_PORT); SysTick_Config(Chip_Clock_GetSystemClockRate() / TICKRATE_HZ); //StuckI2CHack(); delayms(10); MoonLander_I2C_Init(SENSOR_I2C, MOONLANDER_I2C_100K); delayms(100); // Initialize sensors: HTU21D_Init(&g_HTU21D, SENSOR_I2C); delayms(10); HMC5883L_Init(&g_HMC5883L, SENSOR_I2C); delayms(10); HMC5883L_SetRange(&g_HMC5883L, HMC5883L_RANGE_2_5); eGFX_InitDriver(); C12832A_Init(&g_C12832A, SPI_PORT, LCD_A0_PIN, LCD_RST_PIN, LCD_SSEL); delayms(10); Plot_Init(&g_plot_temp, -10, 40, "Temp (C)", 0); Plot_Init(&g_plot_rh, 0, 100, "RH", 0); Plot_Init(&g_plot_mag, -400, 300, "uTesla", 1); Plot_SetSpecialValue(&g_plot_mag, 9000, "OL"); Compass_Init(&g_compass); g_left_display = DISPLAY_TEMP; g_right_display = DISPLAY_COMPASS; // Straight to sleep on boot: g_go_to_sleep = 1; // Or not: //g_go_to_sleep = 0; //wakeup(); while(1) { fillScreen(0x0); if (g_go_to_sleep) { // Write the empty back buffer to the screen: eGFX_Dump(&eGFX_BackBuffer, &g_C12832A); g_ignore_switches = 1; // Sleep! goToSleep(); // Processor has been woken up, restart clocks, etc.: wakeup(); delayms(SW_DEBOUNCE_MS); g_ignore_switches = 0; } switch (g_left_display) { case DISPLAY_TEMP: Plot_Draw(&eGFX_BackBuffer, &g_plot_temp, PLOT_LEFT); break; case DISPLAY_RH: Plot_Draw(&eGFX_BackBuffer, &g_plot_rh, PLOT_LEFT); break; case DISPLAY_MAG: Plot_Draw(&eGFX_BackBuffer, &g_plot_mag, PLOT_LEFT); break; case DISPLAY_COMPASS: Compass_Draw(&eGFX_BackBuffer, &g_compass, COMPASS_LEFT); break; case DISPLAY_RANGE: eGFX_DrawString(&eGFX_BackBuffer, "Range", 24, 1, &FONT_3_5_1BPP); sprintf(str_buffer, "%0.2f cm", getRangeCentimeters()); eGFX_DrawString(&eGFX_BackBuffer, str_buffer, 24, 13, &FONT_3_5_1BPP); break; default: break; } switch (g_right_display) { case DISPLAY_TEMP: Plot_Draw(&eGFX_BackBuffer, &g_plot_temp, PLOT_RIGHT); break; case DISPLAY_RH: Plot_Draw(&eGFX_BackBuffer, &g_plot_rh, PLOT_RIGHT); break; case DISPLAY_MAG: Plot_Draw(&eGFX_BackBuffer, &g_plot_mag, PLOT_RIGHT); break; case DISPLAY_COMPASS: Compass_Draw(&eGFX_BackBuffer, &g_compass, COMPASS_RIGHT); break; case DISPLAY_RANGE: eGFX_DrawString(&eGFX_BackBuffer, "Range", 88, 1, &FONT_3_5_1BPP); sprintf(str_buffer, "%0.2f cm", getRangeCentimeters()); eGFX_DrawString(&eGFX_BackBuffer, str_buffer, 88, 13, &FONT_3_5_1BPP); break; default: break; } eGFX_Dump(&eGFX_BackBuffer, &g_C12832A); } return 0 ; }
/* Get current clock rate for LPC_I2C peripheral */ uint32_t Chip_I2C_GetClockRate(I2C_ID_T id) { return Chip_Clock_GetSystemClockRate() / IP_I2C_GetClockDiv(i2c[id].ip); }
/* Set ADC clock rate */ void Chip_ADC_SetClockRate(LPC_ADC_T *pADC, uint32_t rate) { Chip_ADC_SetDivider(pADC, (Chip_Clock_GetSystemClockRate() / rate) - 1); }
/** * @brief Main entry point * @return Nothing */ int main(void) { uint32_t i, j; int cursor_x = 100, cursor_y = 150; int16_t tmp_x = -1, tmp_y = -1; DEBUGOUT("Start\r\n"); Board_Init(); DEBUGOUT("Board init\r\n"); Board_LCD_Init(); DEBUGOUT("LCD init\r\n"); Chip_GPIO_Init(LPC_GPIO); DEBUGOUT("Chip init\r\n"); SysTick_Config(Chip_Clock_GetSystemClockRate() / 1000); DEBUGOUT("Tick init, clock %d\r\n",Chip_Clock_GetSystemClockRate()); msec = 5; while (msec) {} DEBUGOUT("After wait\r\n"); /* Fill Colorbar only*/ for (i = 0; i < LCD_WIDTH * LCD_HEIGHT / 4; i++) framebuffer[i] = 0x1F; for (i = LCD_WIDTH * LCD_HEIGHT / 4; i < LCD_WIDTH * LCD_HEIGHT * 2 / 4; i++) framebuffer[i] = 0x3F << 5; for (i = LCD_WIDTH * LCD_HEIGHT * 2 / 4; i < LCD_WIDTH * LCD_HEIGHT * 3 / 4; i++) framebuffer[i] = 0x1F << 11; for (i = LCD_WIDTH * LCD_HEIGHT * 3 / 4; i < LCD_WIDTH * LCD_HEIGHT; i++) framebuffer[i] = 0xFFFF; /* Fill NXP logo */ for (j = 0; j < LOGO_HEIGHT; j++) for (i = 0; i < LOGO_WIDTH; i++) putpixel(i, j, image[(i + j * LOGO_WIDTH)]); Chip_LCD_Init(LPC_LCD, (LCD_CONFIG_T *) &BOARD_LCD); Chip_LCD_SetUPFrameBuffer(LPC_LCD, (void *) framebuffer); Board_InitTouchController(); Chip_LCD_PowerOn(LPC_LCD); msec = 100; while (msec) {} Chip_LCD_Cursor_Disable(LPC_LCD, 0); Chip_LCD_Cursor_Config(LPC_LCD, LCD_CURSOR_32x32, true); Chip_LCD_Cursor_WriteImage(LPC_LCD, 0, (void *) Cursor); Chip_LCD_Cursor_SetClip(LPC_LCD, CURSOR_H_OFS, CURSOR_V_OFS); Chip_LCD_Cursor_SetPos(LPC_LCD, cursor_x, cursor_y); Chip_LCD_Cursor_Enable(LPC_LCD, 0); /* Turn on backlight */ Board_SetLCDBacklight(1); DEBUGOUT("EEPROM write passed %d\r\n",5); msec = 20; while (msec) {} while (1) { //msec = 100; //while (msec) {} Board_GetTouchPos((int16_t *) &tmp_x, (int16_t *) &tmp_y); if ((tmp_x >= 0) && (tmp_y >= 0)) { cursor_x = tmp_x; cursor_y = tmp_y; } if (LCD_WIDTH < cursor_x) { cursor_x = LCD_WIDTH - CURSOR_H_OFS; } if (LCD_HEIGHT < cursor_y) { cursor_y = (LCD_HEIGHT - CURSOR_V_OFS); } Chip_LCD_Cursor_SetPos(LPC_LCD, cursor_x, cursor_y); } }
// MAIN PROGRAM int main(void) { uint16_t Giro = 0, Giro0 = 0, CloseLoop=0, Inertia=0xFF; frecuencia=Chip_Clock_GetSystemClockRate(); //Init All //----------------------------------------------------------------------------------------------- Stop_and_Default(); //Condiciones iniciales InitGPIO(); //Llamo función para inicializar GPIO InitPWM(); //Función inicialización modulo PWM Start_Up_Brushless(); //Arranque del motor **********************7777777777777777777777777777744444444444*************** Giro=1; //Main Loop //----------------------------------------------------------------------------------------------- while (1) { //Lectura Pulsadores //------------------------------------------------------------------------------------------- if (Chip_GPIO_ReadPortBit(LPC_GPIO, PULS_PORT, PULS1)==0 && Chip_GPIO_ReadPortBit(LPC_GPIO, PULS_PORT, PULS2)==0) { //Detencion y valores de reinicio Stop_and_Default(); //Detencion del motor Giro = 0; //Flag que no siga girando Giro0 = 0; //Flag para arranque CloseLoop=0; //Flag para lazo cerrado Inertia=0xFF; //Contador para mantener velocidad hasta encontrar BEMF AntiRebo = REBOTE_; //Reestablezco anti rebote } if (Chip_GPIO_ReadPortBit(LPC_GPIO, PULS_PORT, PULS1) == 0 && AntiRebo == 0) { //Arranque Motor PWM + Period if (Giro0 == 0) { //Primer pulso: Start_Up_Brushless(); //Arranque del motor Giro = 1; //Flag que continue girando } else { if (DutyCycle > 20) DutyCycle = DutyCycle - 5; //Decrementar ciclo actividad } AntiRebo = REBOTE_; //Restablezco anti rebote } if (Chip_GPIO_ReadPortBit(LPC_GPIO, PULS_PORT, PULS2) == 0 && AntiRebo == 0) { if (DutyCycle < 980 && Giro0 == 1) DutyCycle = DutyCycle + 5; //Incremento ciclo actividad AntiRebo = REBOTE_; //Restablezco anti rebote } //------------------------------------------------------------------------------------------- if (AntiRebo > 0) AntiRebo--; //Antirebote a lo croto //Test PWM //------------------------------------------------------------------------------------------- if(CloseLoop==0){ //Lazo abierto if (Match_Cnt>=StepPeriod && Giro) { NextPWM(); //Conmutación Giro0 = 1; //Flag para incrementar o decrementar duty Inertia--; //Contador decreciente para encontrar BEMF if(Inertia==0) CloseLoop=1; //Final del contador -> entro en lazo cerrado } }else{ //Lazo cerrado Zero_Detect(); //Detección de cruces por cero (cincronismo) } //------------------------------------------------------------------------------------------- //End Test } return 1; }
/* Set up clock rate for LPC_I2C peripheral */ void Chip_I2C_SetClockRate(I2C_ID_T id, uint32_t clockrate) { IP_I2C_SetClockRate(i2c[id].ip, (Chip_Clock_GetSystemClockRate() / clockrate)); }