Skip to content

Commit 4b3df71

Browse files
committed
Apply 6b6d9c4 to GD32F1 side
1 parent 7eddaa0 commit 4b3df71

File tree

5 files changed

+140
-16
lines changed

5 files changed

+140
-16
lines changed

config.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
// The USB clock is the same for all boards
4646
#define RCC_APB1ENR_USB_CLK 0x00800000
4747

48+
// Clocks for the backup domain registers
49+
#define RCC_APB1ENR_PWR_CLK 0x10000000
50+
#define RCC_APB1ENR_BKP_CLK 0x08000000
4851

4952

5053
// Use the usb_description_strings_util.html to make new strngs for the next 3 arrays if you need to change the text.
@@ -134,4 +137,7 @@
134137
#define PROD_ID0 0x03
135138
#define PROD_ID1 0x00
136139

140+
// Value to place in RTC backup register 10 for persistent bootloader mode
141+
#define RTC_BOOTLOADER_FLAG 0x424C
142+
#define RTC_BOOTLOADER_JUST_UPLOADED 0x424D
137143
#endif

dfu.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ bool dfuUpdateByRequest(void) {
101101
break;
102102
*/
103103
case 1:
104+
104105
userAppAddr = USER_CODE_FLASH0X8005000;
105106
userUploadType = DFU_UPLOAD_FLASH_0X8005000;
106107

@@ -109,13 +110,17 @@ bool dfuUpdateByRequest(void) {
109110
flashUnlock();
110111
// Clear lower memory so that we can check on cold boot, whether the last upload was to 0x8002000 or 0x8005000
111112
flashErasePage((u32)USER_CODE_FLASH0X8002000);
113+
bkp10Write(RTC_BOOTLOADER_JUST_UPLOADED);
114+
112115
break;
113116
case 2:
114117
userUploadType = DFU_UPLOAD_FLASH_0X8002000;
115118
userAppAddr = USER_CODE_FLASH0X8002000;
116119
/* make sure the flash is setup properly, unlock it */
117120
setupFLASH();
118121
flashUnlock();
122+
bkp10Write(RTC_BOOTLOADER_JUST_UPLOADED);
123+
119124
break;
120125
default:
121126
// Roger Clark. Report error

hardware.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,52 @@ void jumpToUser(u32 usrAddr) {
227227
setMspAndJump(usrAddr);
228228
}
229229

230+
void bkp10Write(u16 value)
231+
{
232+
// Enable clocks for the backup domain registers
233+
pRCC->APB1ENR |= (RCC_APB1ENR_PWR_CLK | RCC_APB1ENR_BKP_CLK);
234+
235+
// Disable backup register write protection
236+
pPWR->CR |= PWR_CR_DBP;
237+
238+
// store value in pBK DR10
239+
pBKP->DR10 = value;
240+
241+
// Re-enable backup register write protection
242+
pPWR->CR &=~ PWR_CR_DBP;
243+
}
244+
245+
int checkAndClearBootloaderFlag()
246+
{
247+
bool flagSet = 0x00;// Flag not used
248+
249+
// Enable clocks for the backup domain registers
250+
pRCC->APB1ENR |= (RCC_APB1ENR_PWR_CLK | RCC_APB1ENR_BKP_CLK);
251+
252+
switch (pBKP->DR10)
253+
{
254+
case RTC_BOOTLOADER_FLAG:
255+
flagSet = 0x01;
256+
break;
257+
case RTC_BOOTLOADER_JUST_UPLOADED:
258+
flagSet = 0x02;
259+
break;
260+
}
261+
262+
if (flagSet!=0x00)
263+
{
264+
bkp10Write(0x0000);// Clear the flag
265+
// Disable clocks
266+
pRCC->APB1ENR &= ~(RCC_APB1ENR_PWR_CLK | RCC_APB1ENR_BKP_CLK);
267+
}
268+
269+
270+
271+
return flagSet;
272+
}
273+
274+
275+
230276
void nvicInit(NVIC_InitTypeDef *NVIC_InitStruct) {
231277
u32 tmppriority = 0x00;
232278
u32 tmpreg = 0x00;

hardware.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,52 @@ typedef struct {
191191
} SCB_TypeDef;
192192

193193

194+
/** Power interface register map. */
195+
typedef struct pwr_reg_map {
196+
vu32 CR; /**< Control register */
197+
vu32 CSR; /**< Control and status register */
198+
} pwr_reg_map;
199+
200+
/** Power peripheral register map base pointer. */
201+
#define pPWR ((struct pwr_reg_map*)0x40007000)
202+
203+
/** Disable backup domain write protection bit */
204+
#define PWR_CR_DBP (1 << 8)
205+
206+
207+
/** Backup peripheral register map type. */
208+
typedef struct bkp_reg_map {
209+
const u32 RESERVED1; ///< Reserved
210+
vu16 DR1; ///< Data register 1
211+
const u16 RESERVED2;
212+
vu16 DR2; ///< Data register 2
213+
const u16 RESERVED3;
214+
vu16 DR3; ///< Data register 3
215+
const u16 RESERVED4;
216+
vu16 DR4; ///< Data register 4
217+
const u16 RESERVED5;
218+
vu16 DR5; ///< Data register 5
219+
const u16 RESERVED6;
220+
vu16 DR6; ///< Data register 6
221+
const u16 RESERVED7;
222+
vu16 DR7; ///< Data register 7
223+
const u16 RESERVED8;
224+
vu16 DR8; ///< Data register 8
225+
const u16 RESERVED9;
226+
vu16 DR9; ///< Data register 9
227+
const u16 RESERVED10;
228+
vu16 DR10; ///< Data register 10
229+
const u16 RESERVED11;
230+
vu32 RTCCR; ///< RTC control register
231+
vu32 CR; ///< Control register
232+
vu32 CSR; ///< Control and status register
233+
} bkp_reg_map;
234+
235+
/** Backup peripheral register map base pointer. */
236+
#define pBKP ((struct bkp_reg_map*)0x40006C00)
237+
238+
void bkp10Write(u16 value);
239+
194240
//void setPin(u32 bank, u8 pin);
195241
//void resetPin(u32 bank, u8 pin);
196242
void gpio_write_bit(u32 bank, u8 pin, u8 val);
@@ -206,6 +252,7 @@ void setupLEDAndButton(void);
206252
void setupFLASH(void);
207253
bool checkUserCode(u32 usrAddr);
208254
void jumpToUser(u32 usrAddr);
255+
int checkAndClearBootloaderFlag();
209256

210257
bool flashWriteWord(u32 addr, u32 word);
211258
bool flashErasePage(u32 addr);

main.c

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,52 @@ extern volatile dfuUploadTypes_t userUploadType;
3737

3838
int main()
3939
{
40+
bool no_user_jump = FALSE;
41+
bool dont_wait=FALSE;
42+
4043
systemReset(); // peripherals but not PC
4144
setupCLK();
4245
setupLEDAndButton();
4346
setupUSB();
4447
setupFLASH();
48+
49+
switch(checkAndClearBootloaderFlag())
50+
{
51+
case 0x01:
52+
no_user_jump = TRUE;
53+
strobePin(LED_BANK, LED_PIN, STARTUP_BLINKS, BLINK_FAST,LED_ON_STATE);
54+
break;
55+
case 0x02:
56+
dont_wait=TRUE;
57+
break;
58+
default:
59+
strobePin(LED_BANK, LED_PIN, STARTUP_BLINKS, BLINK_FAST,LED_ON_STATE);
60+
if (!checkUserCode(USER_CODE_FLASH0X8005000) && !checkUserCode(USER_CODE_FLASH0X8002000))
61+
{
62+
no_user_jump = TRUE;
63+
}
64+
else if (readButtonState())
65+
{
66+
no_user_jump = TRUE;
67+
}
68+
break;
69+
}
4570

46-
47-
strobePin(LED_BANK, LED_PIN, STARTUP_BLINKS, BLINK_FAST,LED_ON_STATE);
48-
49-
50-
/* wait for host to upload program or halt bootloader */
51-
bool no_user_jump = (!checkUserCode(USER_CODE_FLASH0X8005000) && !checkUserCode(USER_CODE_FLASH0X8002000)) || readButtonState() ;
52-
53-
int delay_count = 0;
54-
55-
while ((delay_count++ < BOOTLOADER_WAIT) || no_user_jump)
71+
if (!dont_wait)
5672
{
73+
int delay_count = 0;
5774

58-
strobePin(LED_BANK, LED_PIN, 1, BLINK_SLOW,LED_ON_STATE);
59-
60-
if (dfuUploadStarted())
75+
while ((delay_count++ < BOOTLOADER_WAIT) || no_user_jump)
6176
{
62-
dfuFinishUpload(); // systemHardReset from DFU once done
63-
}
64-
}
6577

78+
strobePin(LED_BANK, LED_PIN, 1, BLINK_SLOW,LED_ON_STATE);
79+
80+
if (dfuUploadStarted())
81+
{
82+
dfuFinishUpload(); // systemHardReset from DFU once done
83+
}
84+
}
85+
}
6686

6787
if (checkUserCode(USER_CODE_FLASH0X8002000))
6888
{

0 commit comments

Comments
 (0)