int Buttons_GetStatusByNumber(int BUTTONNumber) { switch(BUTTONNumber) { case 0: if (Chip_GPIO_GetPinState(LPC_GPIO_PORT, BUTTONS_BUTTON1_GPIO_PORT_NUM, BUTTONS_BUTTON1_GPIO_BIT_NUM) == 0) { return 1; } return 0; case 1: if (Chip_GPIO_GetPinState(LPC_GPIO_PORT, BUTTONS_BUTTON2_GPIO_PORT_NUM, BUTTONS_BUTTON2_GPIO_BIT_NUM) == 0) { return 1; } return 0; case 2: if (Chip_GPIO_GetPinState(LPC_GPIO_PORT, BUTTONS_BUTTON3_GPIO_PORT_NUM, BUTTONS_BUTTON3_GPIO_BIT_NUM) == 0) { return 1; } return 0; case 3: if (Chip_GPIO_GetPinState(LPC_GPIO_PORT, BUTTONS_BUTTON4_GPIO_PORT_NUM, BUTTONS_BUTTON4_GPIO_BIT_NUM) == 0) { return 1; } return 0; } return -1; }
int main(void) { Dice noppa; uint32_t sysTickRate; #if defined (__USE_LPCOPEN) // Read clock settings and update SystemCoreClock variable SystemCoreClockUpdate(); #if !defined(NO_BOARD_LIB) // Set up and initialize all required blocks and // functions related to the board hardware Board_Init(); // Set the LED to the state of "On" Board_LED_Set(0, true); #endif #endif sysTickRate = Chip_Clock_GetSysTickClockRate(); // Määritellään nappi PB1 toimimaan inputtina Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 0, (IOCON_MODE_PULLUP | IOCON_DIGMODE_EN | IOCON_INV_EN)); Chip_GPIO_SetPinDIRInput(LPC_GPIO, 1, 0); // Määritellään nappi PC0 toimimaan inputtina Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 8, (IOCON_MODE_PULLUP | IOCON_DIGMODE_EN | IOCON_INV_EN)); Chip_GPIO_SetPinDIRInput(LPC_GPIO, 0, 8); /* Enable and setup SysTick Timer at a periodic rate */ SysTick_Config(sysTickRate / TICKRATE_HZ1); noppa.SetValue(0); while(1) { if (Chip_GPIO_GetPinState(LPC_GPIO, 1,0)) { noppa.SetValue(0); while(Chip_GPIO_GetPinState(LPC_GPIO, 1,0)) { } if(!Chip_GPIO_GetPinState(LPC_GPIO, 1,0)) { noppa.SetValue(calc); } } // Testinappi, kaikki ledit syttyy if (Chip_GPIO_GetPinState(LPC_GPIO, 0,8)) { while(Chip_GPIO_GetPinState(LPC_GPIO, 0,8)) { noppa.SetValue(7); } noppa.SetValue(0); } } return 0; }
bool Board_LED_Test(uint8_t LEDNumber) { if (LEDNumber == 0) { return (bool) !Chip_GPIO_GetPinState(LPC_GPIO_PORT, LED1_GPIO_PORT_NUM, LED1_GPIO_BIT_NUM); } else if (LEDNumber == 1) { return (bool) !Chip_GPIO_GetPinState(LPC_GPIO_PORT, LED2_GPIO_PORT_NUM, LED2_GPIO_BIT_NUM); } return false; }
/** @brief funcion que lee los datos del sensor DHT22 * primero realiza la inicializacion con los tiempos especificados * *luego lee 40 bits de datos que envia el sensor * *@param dhthum parametro pasado como referencia donde se modificara el valor de la humedad *@param dhttemp parametro pasado como referencia donde se modificara el valor de la temperatura */ void leer_dht22(float *dhthum,float *dhttemp){ int dht_pin_in; float dhth,dhtt; int i; byte dht22_dat[5],dht22_pin_checksum; for(i=0;i<5;i++){ dht22_dat[i]=0; } //START SIGNAL Chip_SCU_PinMux(dht22_port,dht22_pin,SCU_MODE_PULLUP | SCU_MODE_ZIF_DIS,FUNC0); //dth22 como output Chip_GPIO_SetDir(LPC_GPIO_PORT,dht22_gpioPort,(1<<dht22_gpioPin),!0); dht22_delay_us(20); Chip_GPIO_ClearValue(LPC_GPIO_PORT,dht22_gpioPort,(1<<dht22_gpioPin)); dht22_delay_ms(5); Chip_GPIO_SetValue(LPC_GPIO_PORT,dht22_gpioPort,(1<<dht22_gpioPin)); dht22_delay_us(30); //WAIT RESPONSE Chip_SCU_PinMux(dht22_port,dht22_pin,SCU_MODE_PULLUP | SCU_MODE_INBUFF_EN | SCU_MODE_ZIF_DIS,FUNC0); Chip_GPIO_SetDir(LPC_GPIO_PORT,dht22_gpioPort,(1<<dht22_gpioPin),!1);//dth22 como input dht_pin_in=Chip_GPIO_GetPinState(LPC_GPIO_PORT,dht22_gpioPort,dht22_gpioPin); if(dht_pin_in){ ciaaPOSIX_printf("Error de inicializacion nro 1"); } dht22_delay_us(80); dht_pin_in=Chip_GPIO_GetPinState(LPC_GPIO_PORT,dht22_gpioPort,dht22_gpioPin); if(!dht_pin_in){ ciaaPOSIX_printf("Error de inicializacion nro 1"); } dht22_delay_us(70); for(i=0;i<5;i++){ dht22_dat[i]=leer_datos_dht(); } dht22_pin_checksum= dht22_pin_dat[0]+dht22_pin_dat[1]+dht22_pin_dat[2]+dht22_pin_dat[3]; if(dht22_pin_dat[4]!=dht22_pin_checksum){ ciaaPOSIX_printf("\r\nDHT checksum error"); } dhth = (dht22_dat[0]<<8) | dht22_dat[1]; dhth /= 10; dhtt = (dht22_dat[2]<<8) | dht22_dat[3]; dhtt /= 10; *dhttemp = dhtt; *dhthum = dhth; }
bool Board_LED_Test(uint8_t LEDNumber) { if (LEDNumber < (sizeof(gpioLEDBits) / sizeof(io_port_t))) return (bool) !Chip_GPIO_GetPinState(LPC_GPIO_PORT, gpioLEDBits[LEDNumber].port, gpioLEDBits[LEDNumber].pin); return false; }
int SDCardSendCommand(uint8_t command, uint32_t param, uint8_t crc, void* buffer, size_t recvSize) { Chip_GPIO_SetPinState(LPC_GPIO, 0, 2, !Chip_GPIO_GetPinState(LPC_GPIO, 0, 2)); if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) xSemaphoreTake(xMutexSDCard, portMAX_DELAY); int result = SDCARD_ERROR_GENERIC; int wait = SDCARD_SPI_MAX_WAIT; int i; uint8_t read = 0; uint8_t* data = (uint8_t*) buffer; command += 0x40; SDCardSetSS(); for (i = 0; i < SDCARD_IDLE_PRE_WAIT_ATTEMPTS; i++) { if (spi_transceive_byte(SDCARD_SPI_DEVICE, 0xff) == 0xff) break; } result = SDCARD_ERROR_TRANSMIT_INTERRUPTED; CommandBuffer[0] = command; CommandBuffer[1] = param >> 24; CommandBuffer[2] = param >> 16; CommandBuffer[3] = param >> 8; CommandBuffer[4] = param; CommandBuffer[5] = (crc_crc7(CommandBuffer, 5) << 1) | 1; spi_transceive(SDCARD_SPI_DEVICE, CommandBuffer, 6); for (i = 0; i < 6; i++) { if (CommandBuffer[i] != 0xff) { // MSS_GPIO_set_output(MSS_GPIO_27, 0); __asm volatile ("nop"); // MSS_GPIO_set_output(MSS_GPIO_27, 1); goto fail; } }
void PIN_INT0_IRQHandler(void) { Chip_PININT_ClearIntStatus(LPC_PININT, PININTCH(PININTSELECT0)); bool PDMBit = Chip_GPIO_GetPinState(LPC_GPIO, 0, 9); if (AccumulatePCMData(PDMBit, &PCMValue)) { PCM_FIFO_PostValue(PCM_FIFO, PCMValue); } }
uint32_t Buttons_GetStatus(void) { uint8_t ret = NO_BUTTON_PRESSED; if (Chip_GPIO_GetPinState(LPC_GPIO_PORT, BUTTONS_BUTTON1_GPIO_PORT_NUM, BUTTONS_BUTTON1_GPIO_BIT_NUM) == 0) { ret |= BUTTONS_BUTTON1; } return ret; }
void SysTick_Handler(void) { if(Chip_GPIO_GetPinState(LPC_GPIO, 1,0)) { calc++; if (calc > 6){ calc = 1; } } }
/* Return the state of LEDNumber */ bool Board_LED_Test(uint8_t LEDNumber) { if (LEDNumber < sizeof(ledBits)) { return (bool) !Chip_GPIO_GetPinState(LPC_GPIO, 0, ledBits[LEDNumber]); } return false; }
uint8_t TeclaPulsada (void){ /* 0:Ninguna, 1..4:tecla que está siendo pulsada */ uint8_t tecla; if (0==Chip_GPIO_GetPinState(LPC_GPIO_PORT, GPIO_TECLA_1, NUM_BIT_TECLA_1)){ tecla= TECLA1; }else if (0==Chip_GPIO_GetPinState(LPC_GPIO_PORT, GPIO_TECLA_2, NUM_BIT_TECLA_2)){ tecla=TECLA2; }else if (0==Chip_GPIO_GetPinState(LPC_GPIO_PORT, GPIO_TECLA_3, NUM_BIT_TECLA_3)){ tecla=TECLA3; }else if (0==Chip_GPIO_GetPinState(LPC_GPIO_PORT, GPIO_TECLA_4, NUM_BIT_TECLA_4)){ tecla=TECLA4; }else tecla=NOTECLA; return tecla; }
/* Returns the current state of a board LED */ bool Board_LED_Test(uint8_t LEDNumber) { bool On = false; if (LEDNumber == 0) On = (bool) Chip_GPIO_GetPinState(LPC_GPIO_PORT, LED0_PORT, LED0_PIN); return On; }
static void vBootSystem(void* pvParameters) { int result; LOG_INFO("Wait for voltage stabilization"); vTaskDelay(1000); { int sdcard_retry_limit = SDCARD_START_RETRY_LIMIT; while (sdcard_retry_limit > 0) { LOG_INFO("Attempting to mount FAT on SDCARD"); result = f_mount(&root_fs, "0:", 1); if (result == FR_OK) { break; } Chip_GPIO_SetPinState(LPC_GPIO, 0, 20, !Chip_GPIO_GetPinState(LPC_GPIO, 0, 20)); vTaskDelay(200); sdcard_retry_limit --; } if (sdcard_retry_limit == 0) { LOG_ERROR("SDCard Mount failed"); exit_error(ERROR_CODE_SDCARD_MOUNT_FAILED); } Chip_GPIO_SetPinState(LPC_GPIO, 0, 20, false); } result = logging_init_persistent(); if (result != 0) { exit_error(ERROR_CODE_SDCARD_LOGGING_INIT_FAILED); } LOG_INFO("Starting real tasks"); xTaskCreate(vFlushLogs, (signed char *) "vFlushLogs", 256, NULL, (tskIDLE_PRIORITY + 2), NULL); xTaskCreate(vLEDTask1, (signed char *) "vTaskLed1", 256, NULL, (tskIDLE_PRIORITY + 1UL), (xTaskHandle *) NULL); /* LED2 toggle thread */ xTaskCreate(vLEDTask2, (signed char *) "vTaskLed2", 256, NULL, (tskIDLE_PRIORITY + 1UL), (xTaskHandle *) NULL); /* LED0 toggle thread */ xTaskCreate(vLEDTask0, (signed char *) "vTaskLed0", configMINIMAL_STACK_SIZE, NULL, (tskIDLE_PRIORITY + 1UL), (xTaskHandle *) NULL); mutex_i2c = xSemaphoreCreateMutex(); xTaskCreate(vBaro, (signed char*) "Baro", 256, NULL, (tskIDLE_PRIORITY + 1UL), NULL); // xTaskCreate(xRadio, (signed char*) "Radio", 256, NULL, (taskIDLE_PRIORITY + 1UL), NULL); LOG_INFO("Initialization Complete. Clock speed is %d", SystemCoreClock); vTaskSuspend(boot_handle); }
/** @brief Funcion que lee 1 byte de datos del dht *El sensor devuelve 1s y 0s con el siguiente formato: *1 -> 50 uS en bajo y 70 uS en alto *0 -> 50 uS en bajo y 30 uS en alto @return un byte de datos de los 5 que devuelve el sensor */ byte leer_datos_dht(){ int i = 0; byte result=0; int res[8]={0,0,0,0,0,0,0,0,0}; for (i=0; i< 8; i++) { //We enter this during the first start bit (low for 50uS) of the byte while(!Chip_GPIO_GetPinState(LPC_GPIO_PORT,dht22_gpioPort,dht22_gpioPin)); dht22_delay_us(30); if (Chip_GPIO_GetPinState(LPC_GPIO_PORT,dht22_gpioPort,dht22_gpioPin)){//Was: if(PINC * _BV(dht_PIN)) result |=(1<<(7-i)); } while (Chip_GPIO_GetPinState(LPC_GPIO_PORT,dht22_gpioPort,dht22_gpioPin)); } return result; }
/* LED1 toggle thread */ static void vLEDTask1(void *pvParameters) { bool LedState = false; while (1) { Board_LED_Set(1, LedState); LedState = (bool) !LedState; Chip_GPIO_SetPinState(LPC_GPIO, 0, 2, !Chip_GPIO_GetPinState(LPC_GPIO, 0, 2)); LOG_INFO("Test float %.4f\r\n", 1.2424125); vTaskDelay(configTICK_RATE_HZ * 2); } }
/* Returns the current state of a board LED */ bool Board_LED_Test(uint8_t LEDNumber) { bool state = false; if (LEDNumber < MAXLEDS) { state = !Chip_GPIO_GetPinState(LPC_GPIO, ledports[LEDNumber], ledpins[LEDNumber]); } return state; }
/* Return the state of LEDNumber */ bool Board_LED_Test(uint8_t LEDNumber) { bool state = false; if (LEDNumber < LEDSAVAIL) { state = (bool) !Chip_GPIO_GetPinState(LPC_GPIO_PORT, 0, ledBits[LEDNumber]); } return state; }
/* Get Joystick status */ uint8_t Joystick_GetStatus(void) { uint8_t i, ret = 0; for (i = 0; i < NUM_BUTTONS; i++) { if ((Chip_GPIO_GetPinState(LPC_GPIO, portButton[i], pinButton[i])) == false) { ret |= stateButton[i]; } } return ret; }
/** \brief read managed output * \param[in] outputNumber number of output to read (0 to ciaaDriverDio_OutputCount) * \return 1 if gpio is high, 0 if it's low, -1 if incorrect pin number */ static int32_t ciaa_lpc4337_readOutput(uint32_t outputNumber) { int32_t rv = -1; if (outputNumber < ciaaDriverDio_OutputCount) { rv = Chip_GPIO_GetPinState(LPC_GPIO_PORT, ciaaDriverDio_Outputs[outputNumber].port, ciaaDriverDio_Outputs[outputNumber].pin) ? 1 : 0; } return rv; }
uint8_t get_ipmb_addr( void ) { uint8_t ga0, ga1, ga2; uint8_t index; /* Set the test pin and read all GA pins */ Chip_GPIO_SetPinState(LPC_GPIO, GA_TEST_PORT, GA_TEST_PIN, 1); /* when using NAMC-EXT-RTM at least 11 instruction cycles required * to have correct GA value after GA_TEST_PIN changes */ { uint8_t i; for (i = 0; i < GPIO_GA_DELAY; i++) asm volatile ("nop"); } ga0 = Chip_GPIO_GetPinState(LPC_GPIO, GA0_PORT, GA0_PIN); ga1 = Chip_GPIO_GetPinState(LPC_GPIO, GA1_PORT, GA1_PIN); ga2 = Chip_GPIO_GetPinState(LPC_GPIO, GA2_PORT, GA2_PIN); /* Clear the test pin and see if any GA pin has changed is value, * meaning that it is unconnected */ Chip_GPIO_SetPinState(LPC_GPIO, GA_TEST_PORT, GA_TEST_PIN, 0); /* when using NAMC-EXT-RTM at least 11 instruction cycles required * to have correct GA value after GA_TEST_PIN changes */ { uint8_t i; for (i = 0; i < GPIO_GA_DELAY; i++) asm volatile ("nop"); } if ( ga0 != Chip_GPIO_GetPinState(LPC_GPIO, GA0_PORT, GA0_PIN) ) { ga0 = UNCONNECTED; } if ( ga1 != Chip_GPIO_GetPinState(LPC_GPIO, GA1_PORT, GA1_PIN) ) { ga1 = UNCONNECTED; } if ( ga2 != Chip_GPIO_GetPinState(LPC_GPIO, GA2_PORT, GA2_PIN) ) { ga2 = UNCONNECTED; } /* Transform the 3-based code in a decimal number */ index = (9 * ga2) + (3 * ga1) + (1 * ga0); if ( index >= IPMBL_TABLE_SIZE ) { return 0; } return IPMBL_TABLE[index]; }
/**Called in the interrupt to handle a button press * To debounce the buttons, the button interrupt is set to trigger on a high-to-low interrupt. When the interrupt triggers, the button number * that triggered the interrupt is saved into the global 'ButtonWaiting' variable. The button interrupts are disabled and the debounce timer * is started. When the debounce timer interrupts (based on the DEBOUNCE_TIME global define) the state of the button is polled again. If the * button is still low, the device treats the button press as legitimate and passes the button press to the RTOS. If the button interrupts * are then reenabled and the device starts to wait for another button press. */ void App_HandleButtonPress(void) { if(App_GetStatus() != APP_STATUS_INIT) { DisplayCommand CommandToSend; if( ((ButtonWaiting == BUTTON_1_PININT_INDEX) && (Chip_GPIO_GetPinState(LPC_GPIO, BUTTON_1_PORT, BUTTON_1_PIN) == false)) || ((ButtonWaiting == BUTTON_2_PININT_INDEX) && (Chip_GPIO_GetPinState(LPC_GPIO, BUTTON_2_PORT, BUTTON_2_PIN) == false)) || ((ButtonWaiting == BUTTON_3_PININT_INDEX) && (Chip_GPIO_GetPinState(LPC_GPIO, BUTTON_3_PORT, BUTTON_3_PIN) == false)) || ((ButtonWaiting == BUTTON_4_PININT_INDEX) && (Chip_GPIO_GetPinState(LPC_GPIO, BUTTON_4_PORT, BUTTON_4_PIN) == false)) || ((ButtonWaiting == BUTTON_5_PININT_INDEX) && (Chip_GPIO_GetPinState(LPC_GPIO, BUTTON_5_PORT, BUTTON_5_PIN) == false))) { CommandToSend.CommandName = OLED_CMD_BUTTON_IN; CommandToSend.CommandData[0] = ButtonWaiting; xQueueSendFromISR(xDisplayCommands, (void *)&CommandToSend, NULL); } } else { App_EnableButtons(); } return; }
/* poll button status */ void Poll_Buttons(void) { static uint8_t stateEnc; uint8_t tmp; uint8_t currentState = 0; //check state of pins if (!Chip_GPIO_GetPinState(LPC_GPIO, BUTTONS_ENCA_GPIO_PORT_NUM, BUTTONS_ENCA_GPIO_BIT_NUM)) { SetBit(currentState,0); } if (!Chip_GPIO_GetPinState(LPC_GPIO, BUTTONS_ENCB_GPIO_PORT_NUM, BUTTONS_ENCB_GPIO_BIT_NUM)) { SetBit(currentState,1); } tmp = stateEnc; if (currentState == (tmp & 0x03)) return; // 00000011b tmp = (tmp<<2)|currentState; stateEnc = tmp; tmp = (tmp & 0x0F); if(tmp == 0x0B) bufEnc = LEFT_SPIN; if(tmp == 0x0D) bufEnc = LEFT_SPIN; if(tmp == 0x04) bufEnc = LEFT_SPIN; if(tmp == 0x02) bufEnc = LEFT_SPIN; if(tmp == 0x01) bufEnc = RIGHT_SPIN; if(tmp == 0x07) bufEnc = RIGHT_SPIN; if(tmp == 0x0E) bufEnc = RIGHT_SPIN; if(tmp == 0x08) bufEnc = RIGHT_SPIN; return; }
static bool get_switch(void) { static unsigned int cnt = SENSITIVITY / 2; while(1) { if(Chip_GPIO_GetPinState(LPC_GPIO_PORT, 0, 4)) { if(cnt > 0) cnt--; } else if(cnt < SENSITIVITY) { cnt++; } if(cnt == 0) return 0; else if(cnt >= SENSITIVITY) { cnt = SENSITIVITY; return 1; } } }
void Board_KEYBOARD_tick_ms(void) { if(keyboardInfo.enable==1) { switch(keyboardInfo.state) { case KEYBOARD_SCAN_STATE_START: { keyboardInfo.currentCol=0; keyboardInfo.state=KEYBOARD_SCAN_STATE_COL_LOW; break; } case KEYBOARD_SCAN_STATE_COL_LOW: { if(keyboardInfo.currentCol>=keyboardInfo.columns) { keyboardInfo.state=KEYBOARD_SCAN_STATE_START; } else { // set col low Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, keyboardColsPinInfo[keyboardInfo.currentCol].gpio, keyboardColsPinInfo[keyboardInfo.currentCol].gpioBit); keyboardInfo.state=KEYBOARD_SCAN_STATE_READ_ROWS; } break; } case KEYBOARD_SCAN_STATE_READ_ROWS: { // read rows int row; for(row=0; row<keyboardInfo.rows;row++) { int rowVal = Chip_GPIO_GetPinState(LPC_GPIO_PORT, keyboardRowsPinInfo[row].gpio, keyboardRowsPinInfo[row].gpioBit); keyboardInfo.stateMatrix[row][keyboardInfo.currentCol] = rowVal; } // set col high Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT, keyboardColsPinInfo[keyboardInfo.currentCol].gpio, keyboardColsPinInfo[keyboardInfo.currentCol].gpioBit); keyboardInfo.currentCol++; keyboardInfo.state=KEYBOARD_SCAN_STATE_COL_LOW; break; } } } }
// Timer interrupt, used to track press time for long press detection void MRT0_IRQHandler(void){ if(pbState == READY){ if(Chip_GPIO_GetPinState(LPC_GPIO, 0, PWR_PB_SENSE)){ pbState = TRIGGERED; PININT_EnableLevelInt(LPC_GPIO_PIN_INT, 1 << 0); PININT_LowActive(LPC_GPIO_PIN_INT, 1 << 0); Chip_MRT_SetMode(LPC_MRT_CH(0), MRT_MODE_REPEAT); Chip_MRT_SetInterval(LPC_MRT_CH(0), (100 * (SystemCoreClock / 1000)) | MRT_INTVAL_LOAD); //100ms pbTenths = 0; }else{ PININT_EnableLevelInt(LPC_GPIO_PIN_INT, 1 << 0); PININT_HighActive(LPC_GPIO_PIN_INT, 1 << 0); } }else if(pbState == TRIGGERED){ pbTenths++; if(pbTenths >= 10){ pbState = LONGPRESS; pbLongPress = true; Chip_MRT_SetInterval(LPC_MRT_CH(0), MRT_INTVAL_LOAD); } } }
/* Get button status */ uint32_t Buttons_GetStatus(void) { uint8_t ret = NO_BUTTON_PRESSED; uint8_t encodeState = Buttons_GetStateEncoder(); if (encodeState == RIGHT_SPIN) { ret |= BUTTONS_RIGHT; } if (encodeState == LEFT_SPIN) { ret |= BUTTONS_LEFT; } if (!Chip_GPIO_GetPinState(LPC_GPIO, BUTTONS_ENCC_GPIO_PORT_NUM, BUTTONS_ENCC_GPIO_BIT_NUM)) { ret |= BUTTONS_ENCC; } return ret; }
static int pushed(void* args) { // activo bajo return !Chip_GPIO_GetPinState(LPC_GPIO, 1, 31); }
/** * @brief Main entry point * @return Nothing */ int main(void) { SystemCoreClockUpdate(); Board_Init(); setupClock(); SystemCoreClockUpdate(); On = true; enableOut = false; controlFlag = false; Board_LED_Set(0, On); DEBUGOUT("Starting\n"); /* Initialize RITimer */ Chip_RIT_Init(LPC_RITIMER); LPC_IOCON->PINSEL[4] |= 0x00000555; //Change this after you know which pwm outputs are needed. LPC_IOCON->PINMODE[3] |= (3 << 6); LPC_IOCON->PINMODE[3] |= (3 << 12); LPC_IOCON->PINSEL[1] |= (1 << 14); LPC_IOCON->PINSEL[1] |= (1 << 16); LPC_IOCON->PINSEL[1] |= (1 << 18); LPC_IOCON->PINSEL[1] |= (1 << 20); LPC_IOCON->PINMODE[1] |= (2 << 14); LPC_IOCON->PINMODE[1] |= (2 << 16); LPC_IOCON->PINMODE[1] |= (2 << 18); LPC_IOCON->PINMODE[1] |= (2 << 20); LPC_SYSCTL->PCLKSEL[0] |= (1 << 12); //PCLK_PWM1 = CCLK LPC_IOCON->PINMODE[4] |= (3 << 26); LPC_SYSCTL->PCONP |= (1 << 17); //Enable clock LPC_SYSCTL->PCLKSEL[1] |= (1 << 30); //PCLKMPWM = CCLK LPC_SYSCTL->PCLKSEL[0] |= (1 << 24); Chip_PWM_Init(LPC_PWM1); LPC_PWM1->PR = 0; Chip_PWM_SetMatch(LPC_PWM1, 0, 3000); Chip_PWM_SetMatch(LPC_PWM1, 1, 1500); Chip_PWM_SetMatch(LPC_PWM1, 2, 1500); Chip_PWM_SetMatch(LPC_PWM1, 3, 1500); Chip_PWM_ResetOnMatchEnable(LPC_PWM1, 0); Chip_PWM_SetCountClockSrc(LPC_PWM1, PWM_CAPSRC_RISING_PCLK, 0); Chip_PWM_SetControlMode(LPC_PWM1, 0, PWM_SINGLE_EDGE_CONTROL_MODE, PWM_OUT_ENABLED); Chip_PWM_SetControlMode(LPC_PWM1, 1, PWM_SINGLE_EDGE_CONTROL_MODE, PWM_OUT_ENABLED); Chip_PWM_SetControlMode(LPC_PWM1, 2, PWM_SINGLE_EDGE_CONTROL_MODE, PWM_OUT_ENABLED); Chip_PWM_LatchEnable(LPC_PWM1, 0, PWM_OUT_ENABLED); Chip_PWM_LatchEnable(LPC_PWM1, 1, PWM_OUT_ENABLED); Chip_PWM_LatchEnable(LPC_PWM1, 2, PWM_OUT_ENABLED); Chip_PWM_LatchEnable(LPC_PWM1, 3, PWM_OUT_ENABLED); Chip_PWM_Enable(LPC_PWM1); Chip_PWM_Reset(LPC_PWM1); Chip_GPIO_Init(LPC_GPIO); LPC_MCPWM->CON_SET |= (1 <<3); DCACSetFreq(1074); LPC_MCPWM->DT = 12; LPC_MCPWM->INTEN_SET |= 1; LPC_MCPWM->INTF_SET |= 1; LPC_MCPWM->CON_SET |= 1; freq = 1074; NVIC_EnableIRQ(RITIMER_IRQn); Chip_ADC_Init(LPC_ADC, &ADCSetup); Chip_ADC_SetBurstCmd(LPC_ADC, DISABLE); /* Configure RIT for a 1s interrupt tick rate */ Chip_RIT_SetTimerInterval(LPC_RITIMER, TIME_INTERVAL); /* LED is toggled in interrupt handler */ vout = 0; voutOldest = 0; voutOld = 0; while (1) { if(controlFlag) { bool emergency = !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency |= !Chip_GPIO_GetPinState(LPC_GPIO,2,13); emergency = !emergency; if(emergency) { enableOut = false; vout = 0; } else { #ifdef enableLoad enableOut = Chip_GPIO_GetPinState(LPC_GPIO,0,28); #else enableOut = true; #endif } Board_LED_Set(0, enableOut); DCDCControl(); DCACControl(); Vmeasure += readADC(VIN_PIN); Imeasure += readADC(CURRENT_PIN); times++; if(times >= delayFactor && enableOut) { DEBUGOUT("%d %d %d %d\n",readADC(VIN_PIN), readADC(VOUT_PIN), readADC(CURRENT_PIN), vout); times = 0; cycles++; if(cycles < ncycles) { #ifdef enableMPPT MPPT(Vmeasure/delayFactor, Imeasure/delayFactor); #endif Vmeasure = 0; Imeasure = 0; } else { cycles = 0; } } if(enablePrev != enableOut) { DEBUGOUT("TOGGLING %d\n",enableOut); } enablePrev = enableOut; controlFlag = false; if(emergency) return 0; } } }
void SysTick_Handler(void){ static bool lowBat; // Set when battery voltage drops below VBAT_LOW static uint32_t sysTickCounter; sysTickCounter++; // Used to schedule less frequent tasks switch(system_state){ case STATE_IDLE: // Enable USB if VBUS is disconnected ;bool vBus = Chip_GPIO_GetPinState(LPC_GPIO, 0, VBUS); if(!vBus){ msc_state = MSC_ENABLED; } // If MSC enabled, VBUS is connected, and SD card is ready, try to connect as MSC if (msc_state == MSC_ENABLED && vBus && sd_state == SD_READY){ f_mount(NULL,"",0); // unmount file system if (msc_init() == MSC_OK){ Board_LED_Color(LED_YELLOW); system_state = STATE_MSC; break; }else{ // Error on MSC initialization error(ERROR_MSC_INIT); } } // If user has short pressed PB and SD card is ready, initiate acquisition if (pb_shortPress() && sd_state == SD_READY){ daq_init(); system_state = STATE_DAQ; break; } // Blink LED if in low battery state, otherwise solid green if (lowBat && sysTickCounter % TICKRATE_HZ1 < TICKRATE_HZ1/2){ Board_LED_Color(LED_OFF); } else { Board_LED_Color(LED_GREEN); } break; case STATE_MSC: // If VBUS is disconnected or button is short pressed ;bool pb; if (Chip_GPIO_GetPinState(LPC_GPIO, 0, VBUS) == 0 || (pb = pb_shortPress())){ if(pb){ msc_state = MSC_DISABLED; } msc_stop(); f_mount(fatfs,"",0); // mount file system Board_LED_Color(LED_GREEN); system_state = STATE_IDLE; enterIdleTime = Chip_RTC_GetCount(LPC_RTC); } break; case STATE_DAQ: // Perform the current asynchronous daq action daq_loop(); // If user has short pressed PB to stop acquisition if (pb_shortPress()){ Board_LED_Color(LED_PURPLE); daq_stop(); Board_LED_Color(LED_GREEN); system_state = STATE_IDLE; enterIdleTime = Chip_RTC_GetCount(LPC_RTC); msc_state = MSC_DISABLED; } break; } // Initialize SD card after every insertion if (Chip_GPIO_GetPinState(LPC_GPIO, 0, CARD_DETECT)){ // Card out Board_LED_Color(LED_CYAN); sd_state = SD_OUT; }else{ // Card in if (sd_state == SD_OUT){ // Delay 100ms to let connections and power stabilize DWT_Delay(100000); if(init_sd_spi(&cardinfo) != SD_OK) { error(ERROR_SD_INIT); } switch(system_state){ case STATE_IDLE: Board_LED_Color(LED_GREEN); break; case STATE_MSC: Board_LED_Color(LED_YELLOW); break; case STATE_DAQ: Board_LED_Color(LED_RED); break; } sd_state = SD_READY; } } /* Run once per second */ if(sysTickCounter % TICKRATE_HZ1 == 0){ float vBat = read_vBat(10); lowBat = vBat < VBAT_LOW ? true : false; // Set low battery state if (vBat < VBAT_SHUTDOWN){ shutdown_message("Low Battery"); } if ((Chip_RTC_GetCount(LPC_RTC) - enterIdleTime > TIMEOUT_SECS && system_state == STATE_IDLE) ){ shutdown_message("Idle Time Out"); } } /* Shut down conditions */ if (pb_longPress()){ shutdown_message("Power Button Pressed"); } /* Handle errors */ error_handler(); }
uint32_t Board_GPIOs_readValue(int32_t gpioNumber) { return Chip_GPIO_GetPinState(LPC_GPIO_PORT, gpiosInfo[gpioNumber].gpio, gpiosInfo[gpioNumber].gpioBit); }