使用的是bluechio的开发板。
PIO 9/10/11是红绿蓝三色灯,PIO拉低则灯会亮。
PIO1连接了按键,按键按下时接地。
/****************************************************************************** * Copyright 2012-2015 Qualcomm Technologies International, Ltd. * Part of CSR uEnergy SDK 2.5.1 * Application version 2.5.1.0 * * FILE * main.c * * DESCRIPTION * Simple PIO example to show PIO usage. No explicit debouncing is applied * while processing the button events. * ******************************************************************************/ /*============================================================================* * SDK Header Files *============================================================================*/ #include <main.h> /* Functions relating to powering up the device */ #include <ls_app_if.h> /* Link Supervisor application interface */ #include <debug.h> /* Simple host interface to the UART driver */ #include <pio.h> /* Programmable I/O configuration and control */ /*============================================================================* * Private Definitions *============================================================================*/ #define PIO_BUTTON 1 /* PIO connected to the button on CSR10xx */ /* hardware information: 9:red 10:green 11:blue */ #define PIO_LED0 9 /* PIO connected to the LED0 on CSR10xx */ #define PIO_LED1 10 /* PIO connected to the LED1 on CSR10xx */ #define PIO_LED2 11 /* PIO connected to the LED1 on CSR10xx */ #define PIO_DIR_OUTPUT TRUE /* PIO direction configured as output */ #define PIO_DIR_INPUT FALSE /* PIO direction configured as input */ /*============================================================================* * Private Data Types *============================================================================*/ /* Enumeration to register the known button states */ typedef enum _button_state { button_state_down, /* Button was pressed */ button_state_up, /* Button was released */ button_state_unknown /* Button state is unknown */ } BUTTON_STATE_T; /* Enumeration to track the LED sequence */ typedef enum _led_seq { led_seq_idle = 0, /* LED flash sequence has not started yet * (all LEDs off) */ led_seq_1 = 1, /* LED0 On, LED1 Off */ led_seq_2 = 2, /* LED0 Off, LED1 On */ led_seq_3 = 3, /* LED0 On, LED1 On */ led_seq_4, led_seq_5, led_seq_6, led_seq_7, led_seq_unknown /* Unknown sequence */ } LED_SEQ_T; /*============================================================================* * Private Data *============================================================================*/ /* Current state of button */ static BUTTON_STATE_T g_cur_button_state = button_state_unknown; /* Current state of LED sequence */ static LED_SEQ_T g_cur_led_seq = led_seq_unknown; /*============================================================================* * Private Function Prototypes *============================================================================*/ /* Advance LED flashing sequence to next state */ static void goToNextLedSeq(void); /* Restart LED sequence from the beginning */ static void restartLedSeq(void); /*============================================================================* * Private Function Implementations *============================================================================*/ /*----------------------------------------------------------------------------* * NAME * goToNextLedSeq * * DESCRIPTION * Advance LED flashing sequence to next state. * * PARAMETERS * None * * RETURNS * Nothing *----------------------------------------------------------------------------*/ static void goToNextLedSeq(void) { switch (g_cur_led_seq) { case led_seq_idle: case led_seq_1: case led_seq_2: case led_seq_3: case led_seq_4: case led_seq_5: case led_seq_6: /* The LED state has to advance */ ++g_cur_led_seq; break; case led_seq_7: case led_seq_unknown: default: /* The LED state has to reset to start */ g_cur_led_seq = led_seq_idle; break; } /* Set LED0 according to bit 0 of desired pattern */ PioSet(PIO_LED0, !(g_cur_led_seq & 0x1)); /* Set LED1 according to bit 1 of desired pattern */ PioSet(PIO_LED1, !(g_cur_led_seq & 0x2)); PioSet(PIO_LED2, !(g_cur_led_seq & 0x4)); } /*----------------------------------------------------------------------------* * NAME * restartLedSeq * * DESCRIPTION * Restart LED sequence from the beginning. * * PARAMETERS * None * * RETURNS * Nothing *----------------------------------------------------------------------------*/ static void restartLedSeq(void) { /* Turn off both LEDs by setting output to Low */ PioSets((1UL << PIO_LED0) | (1UL << PIO_LED1) |(1UL<<PIO_LED2), 0UL); /* LED sequence is reset to idle */ g_cur_led_seq = led_seq_idle; } /*============================================================================* * Public Function Implementations *============================================================================*/ /*----------------------------------------------------------------------------* * NAME * AppPowerOnReset * * DESCRIPTION * This user application function is called just after a power-on reset * (including after a firmware panic), or after a wakeup from Hibernate or * Dormant sleep states. * * At the time this function is called, the last sleep state is not yet * known. * * NOTE: this function should only contain code to be executed after a * power-on reset or panic. Code that should also be executed after an * HCI_RESET should instead be placed in the AppInit() function. * * PARAMETERS * None * * RETURNS * Nothing *----------------------------------------------------------------------------*/ void AppPowerOnReset(void) { } /*----------------------------------------------------------------------------* * NAME * AppInit * * DESCRIPTION * This user application function is called after a power-on reset * (including after a firmware panic), after a wakeup from Hibernate or * Dormant sleep states, or after an HCI Reset has been requested. * * NOTE: In the case of a power-on reset, this function is called * after app_power_on_reset(). * * PARAMETERS * last_sleep_state [in] Last sleep state * * RETURNS * Nothing *----------------------------------------------------------------------------*/ void AppInit(sleep_state last_sleep_state) { /* Set LED0 and LED1 to be controlled directly via PioSet */ PioSetModes((1UL << PIO_LED0) | (1UL << PIO_LED1) | (1UL << PIO_LED2), pio_mode_user); /* Configure LED0 and LED1 to be outputs */ PioSetDir(PIO_LED0, PIO_DIR_OUTPUT); PioSetDir(PIO_LED1, PIO_DIR_OUTPUT); PioSetDir(PIO_LED2, PIO_DIR_OUTPUT); /* Set the LED0 and LED1 to have strong internal pull ups */ PioSetPullModes((1UL << PIO_LED0) | (1UL << PIO_LED1)| (1UL << PIO_LED2), pio_mode_strong_pull_up); /* Configure button to be controlled directly */ PioSetMode(PIO_BUTTON, pio_mode_user); /* Configure button to be input */ PioSetDir(PIO_BUTTON, PIO_DIR_INPUT); /* Set weak pull up on button PIO, in order not to draw too much current * while button is pressed */ PioSetPullModes((1UL << PIO_BUTTON), pio_mode_weak_pull_up); /* Set the button to generate sys_event_pio_changed when pressed as well * as released */ PioSetEventMask((1UL << PIO_BUTTON), pio_event_mode_both); /* Reset LED sequence */ restartLedSeq(); } /*----------------------------------------------------------------------------* * NAME * AppProcesSystemEvent * * DESCRIPTION * This user application function is called whenever a system event, such * as a battery low notification, is received by the system. * * PARAMETERS * id [in] System event ID * data [in] Event data * * RETURNS * Nothing *----------------------------------------------------------------------------*/ void AppProcessSystemEvent(sys_event_id id, void *data) { /* If the reported system event is generated by a PIO */ if (id == sys_event_pio_changed) { /* The PIO data is defined by struct pio_changed_data */ const pio_changed_data *pPioData = (const pio_changed_data *)data; /* If the PIO event comes from the button */ if (pPioData->pio_cause & (1UL << PIO_BUTTON)) { bool validStateChange = FALSE; /* If PIO was HIGH when this event was generated (that means * button was released, generating a rising edge causing PIO * to go from LOW to HIGH) */ if (pPioData->pio_state & (1UL << PIO_BUTTON)) { /* At this point the button is released */ g_cur_button_state = button_state_up; } else /* If PIO was LOW when this event was generated (that means * button was pressed, generating a falling edge causing * PIO to go from HIGH to LOW) */ { /* If last recorded state of button was up */ if (g_cur_button_state == button_state_up) { /* This state change is valid */ validStateChange = TRUE; } /* At this point the button has been pressed */ g_cur_button_state = button_state_down; } /* If it was a valid button state change */ if (validStateChange) { /* Advance LED to next state */ goToNextLedSeq(); } } } } /*----------------------------------------------------------------------------* * NAME * AppProcessLmEvent * * DESCRIPTION * This user application function is called whenever a LM-specific event * is received by the system. * * PARAMETERS * event_code [in] LM event ID * event_data [in] LM event data * * RETURNS * TRUE if the app has finished with the event data; the control layer * will free the buffer. *----------------------------------------------------------------------------*/ bool AppProcessLmEvent(lm_event_code event_code, LM_EVENT_T *event_data) { return TRUE; }