/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_InputPinInit * Description : Initialize one GPIO input pin used by board. * *END**************************************************************************/ void GPIO_DRV_InputPinInit(const gpio_input_pin_user_config_t *inputPin) { /* Get actual port and pin number.*/ uint32_t port = GPIO_EXTRACT_PORT(inputPin->pinName); uint32_t pin = GPIO_EXTRACT_PIN(inputPin->pinName); uint32_t gpioBaseAddr = g_gpioBaseAddr[port]; uint32_t portBaseAddr = g_portBaseAddr[port]; /* Un-gate port clock*/ CLOCK_SYS_EnablePortClock(port); /* Set current pin as digital input.*/ GPIO_HAL_SetPinDir(gpioBaseAddr, pin, kGpioDigitalInput); /* Configure GPIO input features. */ PORT_HAL_SetPullCmd(portBaseAddr, pin, inputPin->config.isPullEnable); PORT_HAL_SetPullMode(portBaseAddr, pin, inputPin->config.pullSelect); PORT_HAL_SetPassiveFilterCmd(portBaseAddr, pin, inputPin->config.isPassiveFilterEnabled); #if FSL_FEATURE_PORT_HAS_DIGITAL_FILTER PORT_HAL_SetDigitalFilterCmd(portBaseAddr, pin, inputPin->config.isDigitalFilterEnabled); #endif PORT_HAL_SetPinIntMode(portBaseAddr, pin, inputPin->config.interrupt); /* Configure NVIC */ if ((inputPin->config.interrupt) && (g_portIrqId[port])) { /* Enable GPIO interrupt.*/ INT_SYS_EnableIRQ(g_portIrqId[port]); } }
void gpioEnableWakeUp(void) { // enables falling edge interrupt for switch SWx PORT_HAL_SetPinIntMode(BOARD_SW_LLWU_BASE, BOARD_SW_LLWU_PIN, kPortIntFallingEdge); INT_SYS_EnableIRQ(BOARD_SW_LLWU_IRQ_NUM); LLWU_HAL_ClearExternalPinWakeupFlag(LLWU_BASE_PTR, (llwu_wakeup_pin_t)BOARD_SW_LLWU_EXT_PIN); LLWU_HAL_SetExternalInputPinMode(LLWU_BASE_PTR,kLlwuExternalPinFallingEdge, (llwu_wakeup_pin_t)BOARD_SW_LLWU_EXT_PIN); }
void gpioDisableWakeUp(void) { // disables interrupt PORT_HAL_SetPinIntMode(BOARD_SW_LLWU_BASE, BOARD_SW_LLWU_PIN, kPortIntDisabled); INT_SYS_DisableIRQ(BOARD_SW_LLWU_IRQ_NUM); LLWU_HAL_ClearExternalPinWakeupFlag(LLWU_BASE_PTR, (llwu_wakeup_pin_t)BOARD_SW_LLWU_EXT_PIN); LLWU_HAL_SetExternalInputPinMode(LLWU_BASE_PTR,kLlwuExternalPinDisabled, (llwu_wakeup_pin_t)BOARD_SW_LLWU_EXT_PIN); }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_InputPinInit * Description : Initialize one GPIO input pin used by board. * *END**************************************************************************/ void GPIO_DRV_InputPinInit(const gpio_input_pin_user_config_t *inputPin) { /* Get actual port and pin number.*/ uint32_t port = GPIO_EXTRACT_PORT(inputPin->pinName); uint32_t pin = GPIO_EXTRACT_PIN(inputPin->pinName); GPIO_Type * gpioBase = g_gpioBase[port]; PORT_Type * portBase = g_portBase[port]; /* Un-gate port clock*/ CLOCK_SYS_EnablePortClock(port); /* Set current pin as gpio.*/ PORT_HAL_SetMuxMode(portBase, pin, kPortMuxAsGpio); /* Set current pin as digital input.*/ GPIO_HAL_SetPinDir(gpioBase, pin, kGpioDigitalInput); /* Configure GPIO input features. */ #if FSL_FEATURE_PORT_HAS_PULL_ENABLE PORT_HAL_SetPullCmd(portBase, pin, inputPin->config.isPullEnable); #endif #if FSL_FEATURE_PORT_HAS_PULL_SELECTION PORT_HAL_SetPullMode(portBase, pin, inputPin->config.pullSelect); #endif #if FSL_FEATURE_PORT_HAS_PASSIVE_FILTER PORT_HAL_SetPassiveFilterCmd(portBase, pin, inputPin->config.isPassiveFilterEnabled); #endif #if FSL_FEATURE_PORT_HAS_DIGITAL_FILTER PORT_HAL_SetDigitalFilterCmd(portBase, pin, inputPin->config.isDigitalFilterEnabled); #endif #if FSL_FEATURE_GPIO_HAS_INTERRUPT_VECTOR PORT_HAL_SetPinIntMode(portBase, pin, inputPin->config.interrupt); /* Configure NVIC */ if ((inputPin->config.interrupt) && (g_portIrqId[port])) { /* Enable GPIO interrupt.*/ INT_SYS_EnableIRQ(g_portIrqId[port]); } #endif }
__LINK_C error_t hw_gpio_configure_interrupt(pin_id_t pin_id, gpio_inthandler_t callback, uint8_t event_mask) { assert(PORT_BASE(pin_id.port) == PORTB); // TODO multiple ports not supported yet assert(callback != NULL); portb_interrupt_config[pin_id.pin].callback = callback; port_interrupt_config_t interrupt_config; if(event_mask == 0) interrupt_config = kPortIntDisabled; else if(event_mask & GPIO_FALLING_EDGE && event_mask & GPIO_RISING_EDGE) interrupt_config = kPortIntEitherEdge; else if(event_mask & GPIO_FALLING_EDGE) interrupt_config = kPortIntFallingEdge; else if(event_mask & GPIO_RISING_EDGE) interrupt_config = kPortIntRisingEdge; portb_interrupt_config[pin_id.pin].interrupt_config = interrupt_config; portb_interrupts &= 1 << pin_id.port; PORT_HAL_SetPinIntMode(PORT_BASE(pin_id.port), pin_id.pin, kPortIntDisabled); return SUCCESS; }
__LINK_C error_t hw_gpio_disable_interrupt(pin_id_t pin_id) { PORT_HAL_SetPinIntMode(PORT_BASE(pin_id.port), pin_id.pin, kPortIntDisabled); return SUCCESS; }
__LINK_C error_t hw_gpio_enable_interrupt(pin_id_t pin_id) { assert(portb_interrupt_config[pin_id.pin].callback != NULL); PORT_HAL_SetPinIntMode(PORT_BASE(pin_id.port), pin_id.pin, portb_interrupt_config[pin_id.pin].interrupt_config); return SUCCESS; }
/******************************************************************************* * Main function for application. ******************************************************************************/ void lab2_power(void) { demo_power_modes_t testVal = kDemoRun; volatile uint8_t powerMode; uint8_t clockManagerMode = CLOCK_RUN; uint32_t freq = 0; //******************************************************* //* Step 1: Initialize the Clock Manager configurations. //******************************************************* // Create list of supported clock configurations. These are taken from the // board.c file and will be passed into CLOCK_SYS_Init(). clock_manager_user_config_t const *clockConfigs[] = { NULL, &g_defaultClockConfigVlpr, &g_defaultClockConfigRun, &g_defaultClockConfigHsrun, }; //***************************************************** //* Step 2: Configure Clock Manager callback function. //***************************************************** // Clock Manager callback function. clock_manager_callback_user_config_t clockManagerCallbackCfg = { .callback = clockManagerCallback, .callbackType = kClockManagerCallbackBeforeAfter, .callbackData = NULL }; clock_manager_callback_user_config_t *clockCallbacks[] = { &clockManagerCallbackCfg }; //***************************************************** //* Step 3: Initialize the Clock Manager. //***************************************************** // Pass in configuration and callback data to Clock Manager. CLOCK_SYS_Init(clockConfigs, CLOCK_NUMBER_OF_CONFIGURATIONS, clockCallbacks, ARRAY_SIZE(clockCallbacks)); // Set to RUN mode. CLOCK_SYS_UpdateConfiguration(CLOCK_RUN, kClockManagerPolicyForcible); //***************************************************** //* Step 4: Set up supported power mode structures. //***************************************************** const power_manager_user_config_t runConfig = { .mode = kPowerManagerRun, .sleepOnExitValue = false, }; power_manager_user_config_t hsrunConfig = runConfig; hsrunConfig.mode = kPowerManagerHsrun; power_manager_user_config_t waitConfig = runConfig; waitConfig.mode = kPowerManagerWait; power_manager_user_config_t stopConfig = runConfig; stopConfig.mode = kPowerManagerStop; power_manager_user_config_t vlprConfig = runConfig; vlprConfig.mode = kPowerManagerVlpr; power_manager_user_config_t vlpwConfig = runConfig; vlpwConfig.mode = kPowerManagerVlpw; power_manager_user_config_t vlpsConfig = runConfig; vlpsConfig.mode = kPowerManagerVlps; power_manager_user_config_t lls3Config = runConfig; lls3Config.mode = kPowerManagerLls3; power_manager_user_config_t vlls0Config = runConfig; vlls0Config.mode = kPowerManagerVlls0; //*********************************************************** //* Step 5: Configure managed power configurations structure. //*********************************************************** // Create the list of supported modes to pass into the Power Manager. power_manager_user_config_t const *powerConfigs[] = { &runConfig, &hsrunConfig, &waitConfig, &stopConfig, &vlprConfig, &vlpwConfig, &vlpsConfig, &lls3Config, &vlls0Config }; //***************************************************** //* Step 6: Configure Power Manager callback function. //***************************************************** // Initializes callback configuration structure for the Power Manager. power_manager_callback_user_config_t powerManagerCallbackCfg = { .callback = powerManagerCallback, .callbackType = kPowerManagerCallbackBeforeAfter, .callbackData = NULL }; // Initializes array of pointers to power manager callbacks. power_manager_callback_user_config_t *powerCallbacks[] = { &powerManagerCallbackCfg }; //***************************************************** //* Step 7: Initialize the Power Manager. //***************************************************** // Pass power configurations and callback info to Power Manager. POWER_SYS_Init(powerConfigs, sizeof(powerConfigs) / sizeof(power_manager_user_config_t *), powerCallbacks, ARRAY_SIZE(powerCallbacks)); // Initialize system to RUN mode. powerMode = kDemoRun - kDemoMin - 1; POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement); //************************************************************** //* Step 8: Configure the rest of the chip for this application. //************************************************************** // Configure pin mux for UART functionality. configure_uart_pins(BOARD_DEBUG_UART_INSTANCE); // Initializes GPIO driver for LEDs and buttons GPIO_DRV_Init(switchPins, ledPins); // Initialize the debug UART console. DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_LOW_POWER_UART_BAUD, kDebugConsoleUART); // Enable PORTC clock for GPIO/LLWU interrupt. CLOCK_SYS_EnablePortClock(2); // Enables falling edge interrupt for switch SW2. PORT_HAL_SetMuxMode(BOARD_SW_LLWU_BASE, BOARD_SW_LLWU_PIN, kPortMuxAsGpio); PORT_HAL_SetPinIntMode(BOARD_SW_LLWU_BASE, BOARD_SW_LLWU_PIN, kPortIntFallingEdge); // Enable GPIO interrupt. INT_SYS_EnableIRQ(PORTC_IRQn); // Configure the LLWU. LLWU_HAL_ClearExternalPinWakeupFlag(LLWU, BOARD_SW_LLWU_EXT_PIN); LLWU_HAL_SetExternalInputPinMode(LLWU, kLlwuExternalPinFallingEdge, BOARD_SW_LLWU_EXT_PIN); // Enable LLWU interrupt. INT_SYS_EnableIRQ(LLWU_IRQn); // Main loop. while (1) { // Get the system clock frequency and current clock configuration to print out. CLOCK_SYS_GetFreq(kCoreClock, &freq); clockManagerMode = CLOCK_SYS_GetCurrentConfiguration(); PRINTF("\n\r#################### Power Manager Demo ####################\n\n\r"); PRINTF(" Core Clock = %d MHz \n\r", freq / 1000000); PRINTF(" Current Mode = "); switch(clockManagerMode) { case CLOCK_RUN: PRINTF("RUN\n\r"); break; case CLOCK_VLPR: PRINTF("VLPR\n\r"); break; case CLOCK_HSRUN: PRINTF("HSRUN\n\r"); break; } PRINTF("\n\rSelect the desired operation \n\n\r"); PRINTF("Press %c for enter: RUN - Normal RUN mode\n\r", kDemoRun); PRINTF("Press %c for enter: HSRUN - High Speed RUN mode\n\r", kDemoHsRun); PRINTF("Press %c for enter: Wait - Wait mode\n\r", kDemoWait); PRINTF("Press %c for enter: Stop - Stop mode\n\r", kDemoStop); PRINTF("Press %c for enter: VLPR - Very Low Power Run mode\n\r", kDemoVlpr); PRINTF("Press %c for enter: VLPW - Very Low Power Wait mode\n\r", kDemoVlpw); PRINTF("Press %c for enter: VLPS - Very Low Power Stop mode\n\r", kDemoVlps); PRINTF("Press %c for enter: LLS3 - Low Leakage Stop mode\n\r", kDemoLls3); PRINTF("Press %c for enter: VLLS0 - Very Low Leakage Stop mode 0\n\r", kDemoVlls0); //PRINTF("Press %c for enter: VLLS3 - Very Low Leakage Stop mode 3\n\r", kDemoVlls3); PRINTF("\n\rWaiting for key press...\n\r\n\r"); // Wait for user input. testVal = (demo_power_modes_t)GETCHAR(); if ((testVal >= 'a') && (testVal <= 'z')) { testVal -= 'a' - 'A'; } if (testVal > kDemoMin && testVal < kDemoMax) { // Obtain Power Manager readable version of the value passed in. powerMode = testVal - kDemoMin - 1; // Switch to selected mode. switch (testVal) { case kDemoRun: if (POWER_SYS_GetCurrentMode() == kPowerManagerRun) { PRINTF("Run mode already active.\n\r"); break; } // Update the power configuration. POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement); // Update the clock configuration. CLOCK_SYS_UpdateConfiguration(CLOCK_RUN, kClockManagerPolicyAgreement); break; case kDemoHsRun: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("High Speed Run mode already active.\n\r"); break; } // Update the power configuration. POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement); // Update the clock configuration. CLOCK_SYS_UpdateConfiguration(CLOCK_HSRUN, kClockManagerPolicyAgreement); break; case kDemoWait: if (POWER_SYS_GetCurrentMode() == kPowerManagerVlpr) { PRINTF("Cannot go from VLPR to WAIT directly...\n\r"); break; } if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Cannot go from HSRUN to WAIT directly...\n\r"); break; } PRINTF("Entering WAIT mode. Press SW2 to wake...\n\r"); // Update the power configuration. POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement); break; case kDemoStop: if (POWER_SYS_GetCurrentMode() == kPowerManagerVlpr) { PRINTF("Cannot go from VLPR to STOP directly...\n\r"); break; } if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Cannot go from HSRUN to STOP directly...\n\r"); break; } PRINTF("Entering STOP mode. Press SW2 to wake...\n\r"); // Update the power configuration. POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement); // Update the clock configuration. CLOCK_SYS_UpdateConfiguration(clockManagerMode, kClockManagerPolicyAgreement); break; case kDemoVlpr: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Cannot go from HSRUN to VLPR directly...\n\r"); break; } if(POWER_SYS_GetCurrentMode() != kPowerManagerVlpr) { // Update the clock configuration PRIOR to entering VLPR. CLOCK_SYS_UpdateConfiguration(CLOCK_VLPR, kClockManagerPolicyAgreement); // Update the power configuration. POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement); } else { PRINTF("Very Low Power Run mode already active.\n\r"); } break; case kDemoVlpw: if (POWER_SYS_GetCurrentMode() == kPowerManagerRun) { PRINTF("Cannot go from RUN to VLPW directly...\n\r"); break; } if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Cannot go from HSRUN to VLPW directly...\n\r"); break; } PRINTF("Entering VLPW mode. Press SW2 to wake...\n\r"); // Update the power configuration. POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement); break; case kDemoVlps: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Cannot go from HSRUN to VLPS directly...\n\r"); break; } PRINTF("Entering VLPS mode. Press SW2 to wake...\n\r"); // Update the power configuration. POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement); // If we entered via RUN mode, restore clock settings. if (POWER_SYS_GetCurrentMode() == kPowerManagerRun) { CLOCK_SYS_UpdateConfiguration(clockManagerMode, kClockManagerPolicyAgreement); } break; case kDemoLls3: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Cannot go from HSRUN to LLSx directly...\n\r"); break; } PRINTF("Entering LLS3 mode. Press SW2 to wake...\n\r"); // Update the power configuration. POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement); // Check the mode active mode prior to LLS3 entry. if(POWER_SYS_GetCurrentMode() != kPowerManagerVlpr) { CLOCK_SYS_UpdateConfiguration(clockManagerMode, kClockManagerPolicyAgreement); } break; case kDemoVlls0: if (POWER_SYS_GetCurrentMode() == kPowerManagerHsrun) { PRINTF("Cannot go from HSRUN to VLLSx directly...\n\r"); break; } PRINTF("Press SW2 to wake. VLLSx wake goes through RESET sequence.\n\r"); // Update the power configuration. POWER_SYS_SetMode(powerMode, kPowerManagerPolicyAgreement); break; default: PRINTF("Bad value."); break; } PRINTF("\n\rNext loop\n\r"); } } }
void HEXIWEAR_startup( task_param_t param ) { uint8_t status = 0; /** output GPIO config */ GPIO_DRV_Init( NULL, OLED_cfg ); GPIO_DRV_Init( NULL, FLASH_cfg ); GPIO_DRV_Init( NULL, PWR_cfg ); GPIO_DRV_Init( NULL, VIBRO_cfg ); GPIO_DRV_Init( NULL, RGB_cfg ); GPIO_DRV_Init( NULL, KW40_GPIO_cfg ); GPIO_DRV_Init( NULL, REL_GPIO_cfg ); /** input GPIO config */ GPIO_DRV_Init( BAT_CHG_cfg, NULL ); GPIO_DRV_Init( TAP_cfg, NULL ); // lajkatest // GPIO_DRV_Init( GPIO_TEST_CFG, NULL ); #if defined( HEXIWEAR_DEBUG ) GPIO_DRV_Init( NULL, DEBUG_cfg ); #endif power_ResetKW40(); timer_Init( HEXIWEAR_TIMER_SENSOR ); status |= RTC_Init(); status |= FLASH_Init( &flashModule, &flashSettings ); // intern flash initialization INTFLASH_Init(); // enable power save by default // power_EnablePowerSave(); power_DisablePowerSave(); // set to deep sleep by default // power_SetSleepMode( POWER_SLEEP_TYPE_DEEP ); // RGB off FLASH_SetOFF(); // visual indication for testing HR sensor // MAXIM_Test(); /** create basic tasks */ status |= Notification_Init(); status |= HostInterface_Init(); status |= sensor_Init(); status |= power_Init(); status |= GuiDriver_Init(); haptic_MutexCreate(); sensor_InitAcc(); /** set GPIO interrupt for the tap function */ PORT_HAL_SetPinIntMode( PORTC, 1, kPortIntFallingEdge ); /** set charging battery interrupt */ PORT_HAL_SetPinIntMode( PORTC, 12, kPortIntEitherEdge ); NVIC_SetPriority( PORTC_IRQn, HEXIWEAR_CHG_IRQ_PRIO ); INT_SYS_EnableIRQ( PORTC_IRQn ); // status |= Run_USB_Task(); if ( HEXIWEAR_STATUS_SUCCESS != status ) { catch( CATCH_INIT ); } /** check for settings in flash at startup */ gui_sensorTag_CheckAtStartup(); haptic_CheckAtStartup(); CLOCK_SYS_Init( g_clockManConfigsArr, FSL_CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, FSL_CLOCK_MANAGER_CALLBACK_CNT ); POWER_SYS_Init( powerConfigsArr, 2U, powerStaticCallbacksConfigsArr , 2U ); // make battery readings regular sensor_SetPacketTargets( PACKET_BAT, sensor_GetPacketTargets( PACKET_BAT) | PACKET_PUSH_POWER_MGMT, true ); volatile uint32_t foo = CLOCK_SYS_GetSystemClockFreq(); while (1) { OSA_TaskDestroy( NULL ); } }
void HEXIWEAR_startup( task_param_t param ) { uint8_t status = 0; /** output GPIO configuration */ GPIO_DRV_Init( NULL, OLED_cfg ); GPIO_DRV_Init( NULL, FLASH_cfg ); GPIO_DRV_Init( NULL, PWR_cfg ); GPIO_DRV_Init( NULL, VIBRO_cfg ); GPIO_DRV_Init( NULL, RGB_cfg ); GPIO_DRV_Init( NULL, KW40_GPIO_cfg ); /** input GPIO configuration */ GPIO_DRV_Init( BAT_CHG_cfg, NULL ); GPIO_DRV_Init( TAP_cfg, NULL ); #if defined( HEXIWEAR_DEBUG ) GPIO_DRV_Init( NULL, DEBUG_cfg ); #endif power_ResetKW40(); timer_Init( HEXIWEAR_TIMER_SENSOR ); status |= RTC_Init(); status |= FLASH_Init( &flashModule, &flashSettings ); // intern flash initialization INTFLASH_Init(); // RGB off FLASH_SetOFF(); /** create basic tasks */ status |= Notification_Init(); status |= HostInterface_Init(); status |= sensor_Init(); status |= GuiDriver_Init(); haptic_MutexCreate(); sensor_InitAcc(); /** set GPIO interrupt for the tap function */ PORT_HAL_SetPinIntMode( PORTC, 1, kPortIntFallingEdge ); /** set charging battery interrupt */ PORT_HAL_SetPinIntMode( PORTC, 12, kPortIntEitherEdge ); NVIC_SetPriority( PORTC_IRQn, HEXIWEAR_CHG_IRQ_PRIO ); INT_SYS_EnableIRQ( PORTC_IRQn ); if ( HEXIWEAR_STATUS_SUCCESS != status ) { catch( CATCH_INIT ); } /** check for settings in flash at startup */ gui_sensorTag_CheckAtStartup(); haptic_CheckAtStartup(); CLOCK_SYS_Init( g_clockManConfigsArr, FSL_CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, FSL_CLOCK_MANAGER_CALLBACK_CNT ); POWER_SYS_Init( powerConfigsArr, 2U, powerStaticCallbacksConfigsArr , 2U ); // turn on regular battery readings sensor_SetPacketTargets( PACKET_BAT, sensor_GetPacketTargets( PACKET_BAT) | PACKET_PUSH_POWER_MGMT, true ); while (1) { power_Init(); OSA_TaskDestroy( NULL ); } }