STM32 PVD: Adds support for STM32's Programmable Voltage Detector feature. I put register access behind CONFIG_STM32_ENERGYLITE as have not checked F1/F2/F4 etc. manuals. Tested on STM32L1. PVD interrupt looks generic, at least #defines it needs are in headers for every chip variant. By Dmitry Nikolaev, submitted by Juha Niskanen.

This commit is contained in:
Gregory Nutt 2015-04-28 06:37:59 -06:00
parent 55ac01399b
commit 398c7757e3
7 changed files with 351 additions and 6 deletions

View File

@ -177,7 +177,7 @@ CHIP_CSRCS += stm32_eth.c
endif
ifeq ($(CONFIG_STM32_PWR),y)
CHIP_CSRCS += stm32_pwr.c
CHIP_CSRCS += stm32_pwr.c stm32_exti_pwr.c
endif
ifeq ($(CONFIG_RTC),y)

View File

@ -73,7 +73,7 @@
# define PWR_CR_1p9V (0 << PWR_CR_PLS_SHIFT) /* 000: 1.9 V */
# define PWR_CR_2p1V (1 << PWR_CR_PLS_SHIFT) /* 001: 2.1 V */
# define PWR_CR_2p3V (2 << PWR_CR_PLS_SHIFT) /* 010: 2.3 V */
# define PWR_CR_2pvV (3 << PWR_CR_PLS_SHIFT) /* 011: 2.5 V */
# define PWR_CR_2p5V (3 << PWR_CR_PLS_SHIFT) /* 011: 2.5 V */
# define PWR_CR_2p7V (4 << PWR_CR_PLS_SHIFT) /* 100: 2.7 V */
# define PWR_CR_2p9V (5 << PWR_CR_PLS_SHIFT) /* 101: 2.9 V */
# define PWR_CR_3p1V (6 << PWR_CR_PLS_SHIFT) /* 110: 3.1 V */

View File

@ -0,0 +1,169 @@
/****************************************************************************
* arch/arm/src/stm32/stm32_exti_pwr.c
*
* Copyright (C) 2009, 2011-2012, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2015 Haltian Ltd. All rights reserved.
* Authors: Gregory Nutt <gnutt@nuttx.org>
* Dmitry Nikolaev <dmitry.nikolaev@haltian.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/irq.h>
#include <nuttx/arch.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <arch/irq.h>
#include "up_arch.h"
#include "chip.h"
#include "stm32_gpio.h"
#include "stm32_exti.h"
#include "stm32_exti_pwr.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/* Interrupt handlers attached to the PVD EXTI */
static xcpt_t stm32_exti_pvd_callback;
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: stm32_exti_pvd_isr
*
* Description:
* EXTI PVD interrupt service routine/dispatcher
*
****************************************************************************/
static int stm32_exti_pvd_isr(int irq, void *context)
{
int ret = OK;
/* Clear the pending EXTI interrupt */
putreg32(EXTI_PVD_LINE, STM32_EXTI_PR);
/* And dispatch the interrupt to the handler */
if (stm32_exti_pvd_callback)
{
ret = stm32_exti_pvd_callback(irq, context);
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stm32_exti_pvd
*
* Description:
* Sets/clears EXTI PVD interrupt.
*
* Parameters:
* - rising/falling edge: enables interrupt on rising/falling edge
* - event: generate event when set
* - func: when non-NULL, generate interrupt
*
* Returns:
* The previous value of the interrupt handler function pointer. This
* value may, for example, be used to restore the previous handler when
* multiple handlers are used.
*
****************************************************************************/
xcpt_t stm32_exti_pvd(bool risingedge, bool fallingedge, bool event,
xcpt_t func)
{
xcpt_t oldhandler;
/* Get the previous GPIO IRQ handler; Save the new IRQ handler. */
oldhandler = stm32_exti_pvd_callback;
stm32_exti_pvd_callback = func;
/* Install external interrupt handlers (if not already attached) */
if (func)
{
irq_attach(STM32_IRQ_PVD, stm32_exti_pvd_isr);
up_enable_irq(STM32_IRQ_PVD);
}
else
{
up_disable_irq(STM32_IRQ_PVD);
}
/* Configure rising/falling edges */
modifyreg32(STM32_EXTI_RTSR,
risingedge ? 0 : EXTI_PVD_LINE,
risingedge ? EXTI_PVD_LINE : 0);
modifyreg32(STM32_EXTI_FTSR,
fallingedge ? 0 : EXTI_PVD_LINE,
fallingedge ? EXTI_PVD_LINE : 0);
/* Enable Events and Interrupts */
modifyreg32(STM32_EXTI_EMR,
event ? 0 : EXTI_PVD_LINE,
event ? EXTI_PVD_LINE : 0);
modifyreg32(STM32_EXTI_IMR,
func ? 0 : EXTI_PVD_LINE,
func ? EXTI_PVD_LINE : 0);
/* Return the old IRQ handler */
return oldhandler;
}

View File

@ -0,0 +1,71 @@
/****************************************************************************
* arch/arm/src/stm32/stm32_exti_pwr.h
*
* Copyright (C) 2015 Haltian Ltd. All rights reserved.
* Authors: Dmitry Nikolaev <dmitry.nikolaev@haltian.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef STM32_EXTI_PWR_H_
#define STM32_EXTI_PWR_H_
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdbool.h>
#include <nuttx/irq.h>
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: stm32_exti_pvd
*
* Description:
* Sets/clears EXTI PVD interrupt.
*
* Parameters:
* - rising/falling edge: enables interrupt on rising/falling edge
* - event: generate event when set
* - func: when non-NULL, generate interrupt
*
* Returns:
* The previous value of the interrupt handler function pointer. This
* value may, for example, be used to restore the previous handler when
* multiple handlers are used.
*
****************************************************************************/
xcpt_t stm32_exti_pvd(bool risingedge, bool fallingedge, bool event,
xcpt_t func);
#endif /* STM32_EXTI_PWR_H_ */

0
arch/arm/src/stm32/stm32_iwdg.c Executable file → Normal file
View File

View File

@ -167,7 +167,7 @@ void stm32_pwr_enablebreg(bool regon)
* Name: stm32_pwr_setvos
*
* Description:
* Set voltage scaling for EneryLite devices.
* Set voltage scaling for EnergyLite devices.
*
* Input Parameters:
* vos - Properly aligned voltage scaling select bits for the PWR_CR register.
@ -204,6 +204,71 @@ void stm32_pwr_setvos(uint16_t vos)
while ((stm32_pwr_getreg(STM32_PWR_CSR_OFFSET) & PWR_CSR_VOSF) != 0);
}
#endif
/************************************************************************************
* Name: stm32_pwr_setpvd
*
* Description:
* Sets power voltage detector
*
* Input Parameters:
* pls - PVD level
*
* Returned Values:
* None
*
* Assumptions:
* At present, this function is called only from initialization logic. If used
* for any other purpose that protection to assure that its operation is atomic
* will be required.
*
************************************************************************************/
void stm32_pwr_setpvd(uint16_t pls)
{
uint16_t regval;
/* Set PLS */
regval = stm32_pwr_getreg(STM32_PWR_CR_OFFSET);
regval &= ~PWR_CR_PLS_MASK;
regval |= (pls & PWR_CR_PLS_MASK);
/* Write value to register */
stm32_pwr_putreg(STM32_PWR_CR_OFFSET, regval);
}
/************************************************************************************
* Name: stm32_pwr_enablepvd
*
* Description:
* Enable the Programmable Voltage Detector
*
************************************************************************************/
void stm32_pwr_enablepvd(void)
{
/* Enable PVD by setting the PVDE bit in PWR_CR register. */
stm32_pwr_modifyreg(STM32_PWR_CR_OFFSET, 0, PWR_CR_PVDE);
}
/************************************************************************************
* Name: stm32_pwr_disablepvd
*
* Description:
* Disable the Programmable Voltage Detector
*
************************************************************************************/
void stm32_pwr_disablepvd(void)
{
/* Disable PVD by clearing the PVDE bit in PWR_CR register. */
stm32_pwr_modifyreg(STM32_PWR_CR_OFFSET, PWR_CR_PVDE, 0);
}
#endif /* CONFIG_STM32_ENERGYLITE */
#endif /* CONFIG_STM32_PWR */

View File

@ -112,7 +112,7 @@ void stm32_pwr_enablebreg(bool regon);
* Name: stm32_pwr_setvos
*
* Description:
* Set voltage scaling for EneryLite devices.
* Set voltage scaling for EnergyLite devices.
*
* Input Parameters:
* vos - Properly aligned voltage scaling select bits for the PWR_CR register.
@ -129,7 +129,47 @@ void stm32_pwr_enablebreg(bool regon);
#ifdef CONFIG_STM32_ENERGYLITE
void stm32_pwr_setvos(uint16_t vos);
#endif
/************************************************************************************
* Name: stm32_pwr_setpvd
*
* Description:
* Sets power voltage detector for EnergyLite devices.
*
* Input Parameters:
* pls - PVD level
*
* Returned Values:
* None
*
* Assumptions:
* At present, this function is called only from initialization logic.
*
************************************************************************************/
void stm32_pwr_setpvd(uint16_t pls);
/************************************************************************************
* Name: stm32_pwr_enablepvd
*
* Description:
* Enable the Programmable Voltage Detector
*
************************************************************************************/
void stm32_pwr_enablepvd(void);
/************************************************************************************
* Name: stm32_pwr_disablepvd
*
* Description:
* Disable the Programmable Voltage Detector
*
************************************************************************************/
void stm32_pwr_disablepvd(void);
#endif /* CONFIG_STM32_ENERGYLITE */
#undef EXTERN
#if defined(__cplusplus)