Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
mtb-example-btsdk-hal-gpio/hal_gpio.c
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
257 lines (221 sloc)
9.66 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2016-2023, Cypress Semiconductor Corporation (an Infineon company) or | |
* an affiliate of Cypress Semiconductor Corporation. All rights reserved. | |
* | |
* This software, including source code, documentation and related | |
* materials ("Software") is owned by Cypress Semiconductor Corporation | |
* or one of its affiliates ("Cypress") and is protected by and subject to | |
* worldwide patent protection (United States and foreign), | |
* United States copyright laws and international treaty provisions. | |
* Therefore, you may use this Software only as provided in the license | |
* agreement accompanying the software package from which you | |
* obtained this Software ("EULA"). | |
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive, | |
* non-transferable license to copy, modify, and compile the Software | |
* source code solely for use in connection with Cypress's | |
* integrated circuit products. Any reproduction, modification, translation, | |
* compilation, or representation of this Software except as specified | |
* above is prohibited without the express written permission of Cypress. | |
* | |
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND, | |
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED | |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress | |
* reserves the right to make changes to the Software without notice. Cypress | |
* does not assume any liability arising out of the application or use of the | |
* Software or any product or circuit described in the Software. Cypress does | |
* not authorize its products for use in any products where a malfunction or | |
* failure of the Cypress product may reasonably be expected to result in | |
* significant property damage, injury or death ("High Risk Product"). By | |
* including Cypress's product in a High Risk Product, the manufacturer | |
* of such system or application assumes all risk of such use and in doing | |
* so agrees to indemnify Cypress against all liability. | |
*/ | |
/** @file | |
* | |
* WICED sample application for GPIO | |
* | |
* This application demonstrates how to use AIROC GPIO APIs to, | |
* | |
* a) configure GPIO to be an input pin (and receive interrupt upon change of pin state) | |
* b) configure GPIO to be an output pin (by toggling LED's available on reference board) | |
* | |
* The GPIO's in the array platform_led[] (generated by configurator) are configured as an output | |
* pin and toggled at predefined frequency(APP_TIMEOUT_IN_SECONDS_A). | |
* The GPIO's in the array platform_button[] (generated by configurator) are configured as an interrupt | |
* enabled input pin, upon pressing this button the blink rate | |
* of LED's is toggled between LED_BLINK_FREQ_A_IN_SECONDS and | |
* LED_BLINK_FREQ_B_IN_SECONDS | |
* | |
* To demonstrate the snip, work through the following steps. | |
* 1. Plug the AIROC evaluation board to your computer. | |
* 2. Build and download the application. | |
* 3. Use Terminal emulation tools like Teraterm or Putty to view the trace messages(See Kit User Guide). | |
* | |
*/ | |
#ifndef CYW20706A2 | |
#include "sparcommon.h" | |
#include "wiced_bt_dev.h" | |
#include "wiced_hal_gpio.h" | |
#include "wiced_timer.h" | |
#include "wiced_bt_trace.h" | |
#include "wiced_platform.h" | |
#if !defined(CYW20706A2) | |
#include "cycfg_pins.h" | |
#endif | |
#ifdef BTSTACK_VER | |
#include "wiced_memory.h" | |
#include "bt_types.h" | |
#endif | |
/****************************************************************************** | |
* Constants | |
******************************************************************************/ | |
/* Delay timer for LED blinking (frequency of blinking is toggled between | |
* the below two values upon button press ) */ | |
#define LED_BLINK_FREQ_A_IN_SECONDS 2 /* Seconds timer */ | |
#define LED_BLINK_FREQ_B_IN_SECONDS 1 /* Seconds timer */ | |
#ifdef BTSTACK_VER | |
#define BT_STACK_HEAP_SIZE 1024 * 6 | |
wiced_bt_heap_t *p_default_heap = NULL; | |
#endif | |
/****************************************************************************** | |
* Structures | |
******************************************************************************/ | |
/****************************************************************************** | |
* Variables Definitions | |
******************************************************************************/ | |
static wiced_timer_t hal_gpio_app_timer; | |
/****************************************************************************** | |
* Function Definitions | |
******************************************************************************/ | |
static void hal_gpio_app_test_input(void); | |
static void hal_gpio_app_test_output(void); | |
static void hal_gpio_app_timer_cb(TIMER_PARAM_TYPE arg); | |
static void hal_gpio_app_interrrupt_handler(void *data, uint8_t port_pin); | |
/* Entry point to the application. */ | |
APPLICATION_START() | |
{ | |
uint8_t index; | |
wiced_set_debug_uart(WICED_ROUTE_DEBUG_TO_PUART); | |
#ifdef BTSTACK_VER | |
/* Create default heap */ | |
p_default_heap = wiced_bt_create_heap("default_heap", NULL, BT_STACK_HEAP_SIZE, NULL, WICED_TRUE); | |
#endif | |
// Initialize timer to control the pin toggle frequency | |
wiced_init_timer(&hal_gpio_app_timer, &hal_gpio_app_timer_cb, 0, WICED_SECONDS_PERIODIC_TIMER); | |
wiced_start_timer(&hal_gpio_app_timer, LED_BLINK_FREQ_A_IN_SECONDS); | |
// initialize all the output pins selected in output_pin_list | |
hal_gpio_app_test_output(); | |
// initialize all the input pins selected in input_pin_list | |
hal_gpio_app_test_input(); | |
WICED_BT_TRACE("**** hal_gpio_app **** \n\r"); | |
} | |
/* | |
* initialize all the input pins selected in input_pin_list to be input, enable interrupts and register | |
* a interrupt handler | |
*/ | |
void hal_gpio_app_test_input(void) | |
{ | |
uint8_t index = 0; | |
// Configure all the selected pins to be input | |
for (index = 0; index < button_count; index++) | |
{ | |
WICED_BT_TRACE("Found button configured for pin %d, index %d\n", WICED_GET_PIN_FOR_BUTTON(index), index); | |
wiced_hal_gpio_register_pin_for_interrupt(WICED_GET_PIN_FOR_BUTTON(index), hal_gpio_app_interrrupt_handler, NULL); | |
wiced_hal_gpio_configure_pin(WICED_GET_PIN_FOR_BUTTON(index), WICED_GPIO_BUTTON_SETTINGS(GPIO_EN_INT_RISING_EDGE), GPIO_PIN_OUTPUT_LOW); | |
} | |
// Configure all the gpio input pins to be input | |
for (index = 0; index < gpio_count; index++) | |
{ | |
if(!(wiced_hal_gpio_get_pin_config(WICED_GET_PIN_FOR_IO(index)) & GPIO_OUTPUT_ENABLE)) | |
{ | |
WICED_BT_TRACE("Found GPIO input configured for pin %d, index %d\n", WICED_GET_PIN_FOR_IO(index), index); | |
wiced_hal_gpio_register_pin_for_interrupt(WICED_GET_PIN_FOR_IO(index), hal_gpio_app_interrrupt_handler, NULL); | |
wiced_hal_gpio_configure_pin(WICED_GET_PIN_FOR_IO(index), WICED_GPIO_BUTTON_SETTINGS(GPIO_EN_INT_RISING_EDGE), GPIO_PIN_OUTPUT_LOW); | |
} | |
} | |
} | |
/* | |
* initialize all the output pins selected in output_pin_list to be output pins | |
*/ | |
void hal_gpio_app_test_output(void) | |
{ | |
uint8_t index = 0; | |
// Configure all the selected pins to be output | |
for (index = 0; index < led_count; index++) | |
{ | |
WICED_BT_TRACE("Found LED configured for pin %d, index %d\n", WICED_GET_PIN_FOR_LED(index), index); | |
wiced_hal_gpio_configure_pin(WICED_GET_PIN_FOR_LED(index), GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_HIGH); | |
} | |
// Configure all the gpio output pins to be output | |
for (index = 0; index < gpio_count; index++) | |
{ | |
if(wiced_hal_gpio_get_pin_config(WICED_GET_PIN_FOR_IO(index)) & GPIO_OUTPUT_ENABLE) | |
{ | |
WICED_BT_TRACE("Found GPIO output configured for pin %d, index %d\n", WICED_GET_PIN_FOR_IO(index), index); | |
wiced_hal_gpio_configure_pin(WICED_GET_PIN_FOR_IO(index), GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_HIGH); | |
} | |
} | |
} | |
/* | |
* The function invoked on timeout of app. seconds timer. | |
*/ | |
void hal_gpio_app_timer_cb(TIMER_PARAM_TYPE arg) | |
{ | |
static uint32_t wiced_seconds = 0; /* number of seconds elapsed */ | |
WICED_BT_TRACE("hal_gpio_app_timer_cb %d\n", wiced_seconds); | |
uint8_t index = 0; | |
wiced_seconds++; | |
if (wiced_seconds & 1) | |
{ | |
for (index = 0; index < led_count; index++) | |
{ | |
wiced_hal_gpio_set_pin_output(WICED_GET_PIN_FOR_LED(index), GPIO_PIN_OUTPUT_LOW); | |
} | |
for (index = 0; index < gpio_count; index++) | |
{ | |
if(wiced_hal_gpio_get_pin_config(WICED_GET_PIN_FOR_IO(index)) & GPIO_OUTPUT_ENABLE) | |
{ | |
wiced_hal_gpio_configure_pin(WICED_GET_PIN_FOR_IO(index), GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_LOW); | |
} | |
} | |
} | |
else | |
{ | |
for (index = 0; index < led_count; index++) | |
{ | |
wiced_hal_gpio_set_pin_output(WICED_GET_PIN_FOR_LED(index), GPIO_PIN_OUTPUT_HIGH); | |
} | |
for (index = 0; index < gpio_count; index++) | |
{ | |
if(wiced_hal_gpio_get_pin_config(WICED_GET_PIN_FOR_IO(index)) & GPIO_OUTPUT_ENABLE) | |
{ | |
wiced_hal_gpio_configure_pin(WICED_GET_PIN_FOR_IO(index), GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_HIGH); | |
} | |
} | |
} | |
} | |
/* | |
* Handle interrupt generated due to change in the GPIO state | |
*/ | |
void hal_gpio_app_interrrupt_handler(void *data, uint8_t pin) | |
{ | |
static uint32_t blink_freq = LED_BLINK_FREQ_A_IN_SECONDS; | |
WICED_BT_TRACE("Button pressed pin %d\n\r", pin); | |
// toggle LED blink rate upon each button press | |
if (blink_freq == LED_BLINK_FREQ_A_IN_SECONDS) | |
{ | |
blink_freq = LED_BLINK_FREQ_B_IN_SECONDS; | |
} | |
else | |
{ | |
blink_freq = LED_BLINK_FREQ_A_IN_SECONDS; | |
} | |
if (wiced_is_timer_in_use(&hal_gpio_app_timer)) | |
{ | |
wiced_stop_timer(&hal_gpio_app_timer); | |
} | |
wiced_start_timer(&hal_gpio_app_timer, blink_freq); | |
// clear the interrupt status | |
wiced_hal_gpio_clear_pin_interrupt_status(pin); | |
} | |
#endif |