/****************************************************************************** * Copyright (C) Cambridge Silicon Radio Limited 2012 * Part of uEnergy SDK 2.0.0 * 2.0.0.0 * * FILE * main.c * * DESCRIPTION * Simple example to show GPIO usage. This application * demonstrates how read the values of a ultrasonic sensor plugged in its GPIO. * ******************************************************************************/ #include #include #include #include #include #include #define PIO_TRIGGER (18) /* PIO connected to the Ultrasonic sensor Trigger */ #define PIO_ECHO (19) /* PIO connected to the Ultrasonic sensor Echo */ #define PIO_DIR_OUTPUT ( TRUE ) /* PIO direction configured as output */ #define PIO_DIR_INPUT ( FALSE ) /* PIO direction configured as input */ /* Maximum number of timers */ #define MAX_APP_TIMERS (5) /* Declare space for application timers. */ static uint16 app_timers[SIZEOF_APP_TIMER * MAX_APP_TIMERS]; /* UART Receive Callback Function */ static uint16 UartDataRxCallback ( void* p_data, uint16 data_count, uint16* p_num_additional_words ); /* Variables of init and final timer */ static uint32 initTime; static uint32 finalTime; extern void startTrigger(void); 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; /* Current state of button */ static BUTTON_STATE_T g_cur_button_state = button_state_unknown; /*----------------------------------------------------------------------------* * NAME * startTrigger * * DESCRIPTION * This function start the trigger pin to measure the echo signal. * * * RETURNS * Nothing. * *---------------------------------------------------------------------------*/ extern void startTrigger(void) { PioSet(PIO_TRIGGER,0x0); TimeDelayUSec(60000); PioSet(PIO_TRIGGER,0x1); } /**************************************************************************** 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. 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. The last sleep state is provided to the application in the parameter. NOTE: In the case of a power-on reset, this function is called after app_power_on_reset(). RETURNS Nothing */ void AppInit(sleep_state last_sleep_state) { /* Set TRIGGER to be controlled directly via PioSet */ PioSetModes((1UL << PIO_TRIGGER), pio_mode_user); /* Configure ECHO to be controlled directly */ PioSetMode(PIO_ECHO, pio_mode_user); /* Configure TRIGGER to be output */ PioSetDir(PIO_TRIGGER, PIO_DIR_OUTPUT); /* Configure ECHO to be input */ PioSetDir(PIO_ECHO, 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_ECHO), pio_mode_strong_pull_down ); /* Set the button to generate sys_event_pio_changed when pressed as well * as released */ PioSetEventMask((1UL << PIO_ECHO), pio_event_mode_both); /* Initialise the application timers */ TimerInit(MAX_APP_TIMERS, (void*)app_timers); /* Initialise communications */ DebugInit(1, UartDataRxCallback, NULL); /* Start the trigger pin to measure the echo signal*/ startTrigger(); } /**************************************************************************** NAME AppProcesSystemEvent DESCRIPTION This user application function is called whenever a system event, such as a battery low notification, is received by the system. 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) { uint32 result; /* The PIO data is defined by struct pio_changed_data */ const pio_changed_data *pPioData; pPioData = (const pio_changed_data *)data; /* If the PIO event comes from the PIO_ECHO pin */ if (pPioData->pio_cause & (1UL << PIO_ECHO)) { /* 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_ECHO)) { /* Read the current system time, that is the initial time*/ initTime = TimeGet32(); 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) { /* Read the current system time, that is the final time*/ finalTime = TimeGet32(); /* subtract the finalTime of the initTime to get the time that the pin ECHO stayed in HIGH */ /* Multiplies the result by the speed of sound and divide per two */ result = ((finalTime - initTime) * 340)/2; DebugWriteString("Distance:\n"); DebugWriteUint32(result); /* At this point the button has been pressed */ g_cur_button_state = button_state_down; } } } } } /**************************************************************************** NAME AppProcessLmEvent DESCRIPTION This user application function is called whenever a LM-specific event is received by the system. RETURNS TRUE if the app has finished with the event data; the control layer will free the buffer. */ extern bool AppProcessLmEvent(lm_event_code event_code, LM_EVENT_T *event_data) { return TRUE; } /**************************************************************************** NAME UartDataRxCallback DESCRIPTION This callback is issued when data is received over UART. Application may ignore the data, if not required. For more information refer to the API documentation for the type "uart_data_out_fn" RETURNS The number of words processed, return data_count if all of the received data had been processed (or if application don't care about the data) */ static uint16 UartDataRxCallback ( void* p_data, uint16 data_count, uint16* p_num_additional_words ) { *p_num_additional_words = 0; /* Application do not need any additional data to be received */ return data_count; }