dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_iap.c
Go to the documentation of this file.
1 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  */
30 
31 /****************************************************************************************
32  * Header files
33  ****************************************************************************************/
34 #include <pios.h>
35 
36 /****************************************************************************************
37  * Private Definitions/Macros
38  ****************************************************************************************/
39 
40 /* these definitions reside here for protection and privacy. */
41 #define IAP_MAGIC_WORD_1 0x1122
42 #define IAP_MAGIC_WORD_2 0xAA55
43 #define IAP_MAGIC_WORD_3 0xBB11
44 
45 #define UPPERWORD16(lw) (uint16_t)((uint32_t)(lw)>>16)
46 #define LOWERWORD16(lw) (uint16_t)((uint32_t)(lw)&0x0000ffff)
47 #define UPPERBYTE(w) (uint8_t)((w)>>8)
48 #define LOWERBYTE(w) (uint8_t)((w)&0x00ff)
49 
50 /****************************************************************************************
51  * Private Functions
52  ****************************************************************************************/
53 
54 /****************************************************************************************
55  * Private (static) Data
56  ****************************************************************************************/
57 
58 /****************************************************************************************
59  * Public/Global Data
60  ****************************************************************************************/
61 
69 void PIOS_IAP_Init( void )
70 {
71  /* Enable CRC clock */
72  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);
73 
74  /* Enable PWR and BKP clock */
75  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
76 
77  /* Enable write access to Backup domain */
78  PWR_BackupAccessCmd(ENABLE);
79 
80  /* Clear Tamper pin Event(TE) pending flag */
81  RTC_ClearFlag(RTC_FLAG_TAMP1F);
82 }
83 
88 uint32_t PIOS_IAP_CheckRequest( void )
89 {
90  uint32_t retval = false;
91  uint16_t reg1;
92  uint16_t reg2;
93 
94  reg1 = RTC_ReadBackupRegister( MAGIC_REG_1 );
95  reg2 = RTC_ReadBackupRegister( MAGIC_REG_2 );
96 
97  if( reg1 == IAP_MAGIC_WORD_1 && reg2 == IAP_MAGIC_WORD_2 ) {
98  // We have a match.
99  retval = true;
100  } else {
101  retval = false;
102  }
103  return retval;
104 }
105 
110 uint32_t PIOS_Boot_CheckRequest( void )
111 {
112  uint32_t retval = false;
113  uint16_t reg1;
114  uint16_t reg2;
115 
116  reg1 = RTC_ReadBackupRegister( MAGIC_REG_1 );
117  reg2 = RTC_ReadBackupRegister( MAGIC_REG_2 );
118 
119  if( reg1 == IAP_MAGIC_WORD_1 && reg2 == IAP_MAGIC_WORD_3 ) {
120  // We have a match.
121  retval = true;
122  } else {
123  retval = false;
124  }
125  return retval;
126 }
127 
134 void PIOS_IAP_SetRequest1(void)
135 {
136  RTC_WriteBackupRegister( MAGIC_REG_1, IAP_MAGIC_WORD_1);
137 }
138 
139 void PIOS_IAP_SetRequest2(void)
140 {
141  RTC_WriteBackupRegister( MAGIC_REG_2, IAP_MAGIC_WORD_2);
142 }
143 
144 void PIOS_IAP_SetRequest3(void)
145 {
146  RTC_WriteBackupRegister( MAGIC_REG_2, IAP_MAGIC_WORD_3);
147 }
148 
149 void PIOS_IAP_ClearRequest(void)
150 {
151  RTC_WriteBackupRegister( MAGIC_REG_1, 0);
152  RTC_WriteBackupRegister( MAGIC_REG_2, 0);
153 }
154 
155 uint16_t PIOS_IAP_ReadBootCount(void)
156 {
157  return RTC_ReadBackupRegister ( IAP_BOOTCOUNT );
158 }
159 
160 void PIOS_IAP_WriteBootCount (uint16_t boot_count)
161 {
162  RTC_WriteBackupRegister ( IAP_BOOTCOUNT, boot_count );
163 }
uint32_t PIOS_IAP_CheckRequest(void)
Determines if an In-Application-Programming request has been made.
Definition: pios_iap.c:57
#define MAGIC_REG_1
Definition: pios_iap.h:47
#define IAP_MAGIC_WORD_1
Definition: pios_iap.c:41
Main PiOS header to include all the compiled in PiOS options.
#define IAP_BOOTCOUNT
Definition: pios_iap.h:49
uint16_t PIOS_IAP_ReadBootCount(void)
Definition: pios_iap.c:93
void PIOS_IAP_Init(void)
PIOS_IAP_Init - performs required initializations for iap module.
Definition: pios_iap.c:44
#define MAGIC_REG_2
Definition: pios_iap.h:48
void PIOS_IAP_SetRequest3(void)
Definition: pios_iap.c:84
#define IAP_MAGIC_WORD_3
Definition: pios_iap.c:43
void PIOS_IAP_ClearRequest(void)
Definition: pios_iap.c:89
uint32_t PIOS_Boot_CheckRequest(void)
Determines if a boot request has been made.
Definition: pios_iap.c:63
void PIOS_IAP_WriteBootCount(uint16_t)
Definition: pios_iap.c:98
void PIOS_IAP_SetRequest2(void)
Definition: pios_iap.c:79
void PIOS_IAP_SetRequest1(void)
Sets the 1st word of the request sequence.
Definition: pios_iap.c:74
#define IAP_MAGIC_WORD_2
Definition: pios_iap.c:42