Skip to content
Permalink
5a8611aef1
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
192 lines (162 sloc) 6.79 KB
/******************************************************************************
* File Name: main.c
*
* Description: This code example features how a LVD block in PSoC 6 can be
configured to generate an interrupt when voltage drops below
a certain level.
*
* Related Document: See README.md
*
*******************************************************************************
* Copyright 2022, 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.
*******************************************************************************/
/*******************************************************************************
* Include header files
******************************************************************************/
#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"
/*******************************************************************************
* Macros
********************************************************************************/
#define LVD_INTERRUPT_PRIORITY 2UL
#define DELAY 1000UL
/*******************************************************************************
* Function Prototypes
********************************************************************************/
void lvd_init(void);
void lvd_interrupt_handler(void);
/*******************************************************************************
* Global Variables
********************************************************************************/
/* LVD interrupt configuration */
const cy_stc_sysint_t lvd_interrupt_cfg =
{
.intrSrc = srss_interrupt_IRQn,
.intrPriority = LVD_INTERRUPT_PRIORITY
};
/* Flag to check the status of LVD interrupt */
uint32_t interrupt_flag = 0;
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function for LVD example. It initializes the LVD block
* and the user led.
*
* Parameters:
* void
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
/* Initialize the device and board peripherals */
result = cybsp_init() ;
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Enable global interrupts */
__enable_irq();
/* Initialize user led as output with strong drive mode and initial value = true*/
cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, true);
/* Initialize the LVD block */
lvd_init();
for(;;)
{
/* Verify if the LVD interrupt was generated by checking if the interrupt flag is set */
if (interrupt_flag)
{
/* Blink the led in case of LVD interrupt */
cyhal_gpio_write(CYBSP_USER_LED, false);
cyhal_system_delay_ms(DELAY);
cyhal_gpio_write(CYBSP_USER_LED, true);
/* Reset the interrupt flag to zero */
interrupt_flag = 0;
}
}
}
/***************************************************************************
* Function Name: lvd_init
****************************************************************************
* Summary:
* This function initializes and configures the LVD block
*
**************************************************************************/
void lvd_init()
{
/* Disable LVD interrupts */
Cy_LVD_ClearInterruptMask();
/* Set the threshold value for monitoring the VDDD voltage */
Cy_LVD_SetThreshold(CY_LVD_THRESHOLD_2_7_V) ;
/* Set configuration for LVD interrupt */
Cy_LVD_SetInterruptConfig(CY_LVD_INTR_BOTH);
/* Enable the LVD Block */
Cy_LVD_Enable();
/* Provide delay to avoid false interrupt detection */
Cy_SysLib_DelayUs(20U);
/* Clear any false interrupt */
Cy_LVD_ClearInterrupt();
/* Initialize the LVD interrupt */
(void)Cy_SysInt_Init(&lvd_interrupt_cfg, lvd_interrupt_handler);
/* Enables LVD interrupts */
Cy_LVD_SetInterruptMask();
NVIC_EnableIRQ(lvd_interrupt_cfg.intrSrc);
}
/***************************************************************************
* Function Name: lvd_interrupt_handler
****************************************************************************
* Summary:
* This function processes the LVD interrupts
*
***************************************************************************/
void lvd_interrupt_handler()
{
cy_en_lvd_status_t voltage_status = 0;
uint32_t interrupt_status = 0;
/* Get the status of interrupt and Voltage */
interrupt_status = Cy_LVD_GetInterruptStatus();
voltage_status = Cy_LVD_GetStatus();
/* Set the flag in case of an interrupt */
interrupt_flag = 1;
if((interrupt_status == CY_LVD_INTR ) && (voltage_status == CY_LVD_STATUS_BELOW))
{
/*Do the necessary processing if the voltage drops below the set threshold */
}
/* Clear the interrupt */
Cy_LVD_ClearInterrupt();
}
/* [] END OF FILE */