GlobLib
HAL and API libraries for MCUs and hardware.
stm32f103cb_power.c
Go to the documentation of this file.
1 /*!**************************************************************************
2  @file stm32f103cb_power.c
3  @brief Source file for stm32f103cb power
4  @author Stuart Ianna
5  @version 0.1
6  @date July 2018
7  @copyright GNU GPLv3
8  @warning None
9  @bug
10 
11  @details
12 
13  @par Compilers
14  - arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
15 ******************************************************************************/
16 
17 #include "stm32f103cb_power.h"
18 
19 
20 void hard_fault_handler(void){
21 
22  //Enable backup domain clocks
23  rcc_periph_clock_enable(RCC_BKP);
24  rcc_periph_clock_enable(RCC_PWR);
25 
26  if(BKP_DR1 != 1){
27 
28  //No bootloader used, stall here
29  while(1);
30 
31  }
32 
33  //Bootloader used
34 
35  //Bootloader uses 5678 to detect reset by hard fault
36  pwr_disable_backup_domain_write_protect();
37  BKP_DR1 = 5678;
38  pwr_enable_backup_domain_write_protect();
39 
40  //Disconnect USB and reset
42  POWER_reset();
43 
44  //Never getting here
45  while(1);
46 }
47 
48 void POWER_reset(void){
49 
50  scb_reset_system();
51 }
52 
53 void POWER_sleep(void){
54 
55  SCB_SCR |= SCB_SCR_SLEEPONEXIT;
56  __WFI();
57 }
58 
59 void POWER_noSleep(void){
60 
61  //Clear flag
62  SCB_SCR &= ~SCB_SCR_SLEEPONEXIT;
63 }
64 
65 void POWER_standby(void){
66 
67  rcc_periph_clock_enable(RCC_PWR);
68  PWR_CR |= PWR_CR_CWUF;
69  SCB_SCR |= SCB_SCR_SLEEPDEEP;
70  PWR_CR |= PWR_CR_PDDS;
71  PWR_CSR &= ~PWR_CSR_WUF;
72  __WFI();
73 }
74 
75 void POWER_stop(void){
76 
77  rcc_periph_clock_enable(RCC_PWR);
78  SCB_SCR |= SCB_SCR_SLEEPDEEP;
79  PWR_CR &= ~PWR_CR_PDDS;
80  PWR_CR |= PWR_CR_LPDS;
81  __WFI();
82 }
83 
84 uint8_t POWER_fromStop(void){
85 
86  rcc_periph_clock_enable(RCC_PWR);
87 
88  if((PWR_CSR & PWR_CSR_WUF) || (PWR_CSR & PWR_CSR_SBF)){
89 
90  //Clear flag
91  PWR_CR |= PWR_CR_CWUF;
92  PWR_CR |= PWR_CR_CSBF;
93  return 1;
94  }
95  return 0;
96 
97 }
98 
99 uint8_t POWER_fromStandby(void){
100 
101  rcc_periph_clock_enable(RCC_PWR);
102 
103  if(PWR_CSR & PWR_CSR_WUF){
104 
105  //Clear flag
106  PWR_CR |= PWR_CR_CWUF;
107  PWR_CR |= PWR_CR_CSBF;
108  return 1;
109  }
110 
111  return 0;
112 }
113 
void POWER_noSleep(void)
Disable sleep mode.
uint8_t POWER_fromStandby(void)
Check if the system is restarting from standby mode.
void POWER_standby(void)
Put the device into standby mode.
void USB_disconnect(void)
Dissconnect the USB device from the HOST.
void POWER_reset(void)
Resets the system.
uint8_t POWER_fromStop(void)
Check if MCU was in stop mode and reset flags.
void POWER_stop(void)
Put the device into stop mode.
void POWER_sleep(void)
Put the system in sleep mode.
Header file for stm32f103cb Power.