Skip to content

Commit e163e3c

Browse files
author
rogerclarkmelbourne
committed
Bootloader and also boards.txt, so that its backwards compatible with the original bootloader. The new bootloader now has an additional altID for the new upload type (ID2) which is set in boards.txt for the maple mini on the Bootloader version menu. Note there was also a bug in the old version of boards.txt where the upload size params were not being set by the Bootloader version menu. Binaries folder has also been updated with the revised bootloader
1 parent dc1262c commit e163e3c

8 files changed

Lines changed: 145 additions & 58 deletions

File tree

STM32F1/boards.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ maple_mini.upload.file_type=bin
1818

1919

2020
maple_mini.upload.usbID=1EAF:0003
21-
maple_mini.upload.altID=1
2221
maple_mini.upload.auto_reset=true
2322

2423
maple_mini.build.mcu=cortex-m3
2524
maple_mini.build.f_cpu=72000000L
2625
maple_mini.build.board=MAPLE_MINI
2726
maple_mini.build.core=maple
2827
maple_mini.build.extra_flags=-DMCU_STM32F103CB -mthumb -DSERIAL_USB -march=armv7-m -D__STM32F1__
29-
maple_mini.build.ldscript=ld/flash.ld
3028
maple_mini.build.variant=maple_mini
3129
maple_mini.build.variant_system_lib=libmaple.a
3230
maple_mini.build.density=STM32_MEDIUM_DENSITY
@@ -37,16 +35,18 @@ maple_mini.build.gcc_ver=gcc-arm-none-eabi-4.8.3-2014q1
3735
maple_mini.menu.bootloader_version.original = Original
3836
maple_mini.menu.bootloader_version.original.build.vect=VECT_TAB_ADDR=0x8005000
3937
maple_mini.menu.bootloader_version.original.build.ldscript=ld/flash.ld
40-
maple_mini.menu.bootloader_version.upload.ram.maximum_size=17408
41-
maple_mini.menu.bootloader_version.upload.flash.maximum_size=110592
42-
maple_mini.menu.bootloader_version.upload.maximum_size=110592
38+
maple_mini.menu.bootloader_version.originalupload.ram.maximum_size=17408
39+
maple_mini.menu.bootloader_version.original.upload.flash.maximum_size=110592
40+
maple_mini.menu.bootloader_version.original.upload.maximum_size=110592
41+
maple_mini.menu.bootloader_version.original.upload.altID=1
4342

4443
maple_mini.menu.bootloader_version.bootloader20 = Bootloader 2.0
4544
maple_mini.menu.bootloader_version.bootloader20.build.vect=VECT_TAB_ADDR=0x8002000
4645
maple_mini.menu.bootloader_version.bootloader20.build.ldscript=ld/bootloader_20.ld
47-
maple_mini.menu.bootloader_version.upload.ram.maximum_size=20480
48-
maple_mini.menu.bootloader_version.upload.flash.maximum_size=122880
49-
maple_mini.menu.bootloader_version.upload.maximum_size=122880
46+
maple_mini.menu.bootloader_version.bootloader20.upload.ram.maximum_size=20480
47+
maple_mini.menu.bootloader_version.bootloader20.upload.flash.maximum_size=122880
48+
maple_mini.menu.bootloader_version.bootloader20.upload.maximum_size=122880
49+
maple_mini.menu.bootloader_version.bootloader20.upload.altID=2
5050

5151
##############################################################
5252
maple.name=LeafLabs Maple Rev 3+ to Flash
196 Bytes
Binary file not shown.

usb_bootloader/STM32F1/config.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@
6464
#define STARTUP_BLINKS 5
6565
#define BOOTLOADER_WAIT 6
6666

67-
#define USER_CODE_RAM ((u32)0x20000C00)
68-
#define RAM_END ((u32)0x20005000)
69-
#define USER_CODE_FLASH ((u32)0x08002000)
70-
#define FLASH_END ((u32)0x08020000)
67+
#define USER_CODE_RAM ((u32)0x20000C00)
68+
#define RAM_END ((u32)0x20005000)
69+
#define USER_CODE_FLASH0X8005000 ((u32)0x08005000)
70+
#define USER_CODE_FLASH0X8002000 ((u32)0x08002000)
71+
#define FLASH_END ((u32)0x08020000)
7172

7273
#define VEND_ID0 0xAF
7374
#define VEND_ID1 0x1E

usb_bootloader/STM32F1/dfu.c

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
static volatile u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */
3939
static volatile u32 userAppEnd = RAM_END;
4040
static volatile DFUStatus dfuAppStatus; /* includes state */
41-
static volatile bool userFlash = FALSE;
41+
volatile dfuUploadTypes_t userUploadType = DFU_UPLOAD_NONE;
4242
volatile bool dfuBusy = FALSE;
4343

4444
static volatile u8 recvBuffer[wTransferSize] __attribute__((aligned(4)));
@@ -61,7 +61,7 @@ void dfuInit(void) {
6161
thisBlockLen = 0;;
6262
userAppAddr = USER_CODE_RAM; /* default RAM user code location */
6363
userAppEnd = RAM_END;
64-
userFlash = FALSE;
64+
userUploadType=DFU_UPLOAD_RAM;
6565
code_copy_lock = WAIT;
6666
dfuBusy = FALSE;
6767
}
@@ -83,19 +83,34 @@ bool dfuUpdateByRequest(void) {
8383
if (pInformation->USBwLengths.w > 0) {
8484
userFirmwareLen = 0;
8585
dfuAppStatus.bState = dfuDNLOAD_SYNC;
86-
87-
if (pInformation->Current_AlternateSetting == 1) {
88-
userAppAddr = USER_CODE_FLASH;
89-
userFlash = TRUE;
90-
91-
/* make sure the flash is setup properly, unlock it */
92-
setupFLASH();
93-
flashUnlock();
94-
95-
} else {
96-
userAppAddr = USER_CODE_RAM;
97-
userFlash = FALSE;
98-
}
86+
switch(pInformation->Current_AlternateSetting)
87+
{
88+
case 0:
89+
userAppAddr = USER_CODE_RAM;
90+
userUploadType = DFU_UPLOAD_RAM;
91+
break;
92+
case 1:
93+
userAppAddr = USER_CODE_FLASH0X8005000;
94+
userUploadType = DFU_UPLOAD_FLASH_0X8005000;
95+
96+
/* make sure the flash is setup properly, unlock it */
97+
setupFLASH();
98+
flashUnlock();
99+
// Clear lower memory so that we can check on cold boot, whether the last upload was to 0x8002000 or 0x8005000
100+
flashErasePage((u32)USER_CODE_FLASH0X8002000);
101+
break;
102+
case 2:
103+
userUploadType = DFU_UPLOAD_FLASH_0X8002000;
104+
userAppAddr = USER_CODE_FLASH0X8002000;
105+
/* make sure the flash is setup properly, unlock it */
106+
setupFLASH();
107+
flashUnlock();
108+
break;
109+
default:
110+
userAppAddr = USER_CODE_RAM;
111+
userUploadType = DFU_UPLOAD_RAM;
112+
break;
113+
}
99114
} else {
100115
dfuAppStatus.bState = dfuERROR;
101116
dfuAppStatus.bStatus = errNOTDONE;
@@ -108,13 +123,24 @@ bool dfuUpdateByRequest(void) {
108123
thisBlockLen = uploadBlockLen; /* for this first block as well */
109124
/* calculate where the data should be copied from */
110125
userFirmwareLen = uploadBlockLen * pInformation->USBwValue;
111-
if (pInformation->Current_AlternateSetting == 1) {
112-
userAppAddr = USER_CODE_FLASH;
113-
userAppEnd = FLASH_END;
114-
} else {
115-
userAppAddr = USER_CODE_RAM;
116-
userAppEnd = RAM_END;
117-
}
126+
switch(pInformation->Current_AlternateSetting)
127+
{
128+
case 0:
129+
userAppAddr = USER_CODE_RAM;
130+
userAppEnd = RAM_END;
131+
case 1:
132+
userAppAddr = USER_CODE_FLASH0X8005000;
133+
userAppEnd = FLASH_END;
134+
break;
135+
case 2:
136+
userAppAddr = USER_CODE_FLASH0X8002000;
137+
userAppEnd = FLASH_END;
138+
break;
139+
default:
140+
userAppAddr = USER_CODE_RAM;
141+
userAppEnd = RAM_END;
142+
break;
143+
}
118144
} else if (pInformation->USBbRequest == DFU_ABORT) {
119145
dfuAppStatus.bState = dfuIDLE;
120146
dfuAppStatus.bStatus = OK; /* are we really ok? we were just aborted */
@@ -132,7 +158,7 @@ bool dfuUpdateByRequest(void) {
132158

133159
if (pInformation->USBbRequest == DFU_GETSTATUS) {
134160
/* todo, add routine to wait for last block write to finish */
135-
if (userFlash) {
161+
if (userUploadType == DFU_UPLOAD_RAM) {
136162
if (code_copy_lock == WAIT) {
137163
code_copy_lock = BEGINNING;
138164
dfuAppStatus.bwPollTimeout0 = 0x20; /* 32 ms */
@@ -358,15 +384,25 @@ void dfuCopyBufferToExec() {
358384
int i;
359385
u32 *userSpace;
360386

361-
if (!userFlash) {
387+
if (userUploadType == DFU_UPLOAD_RAM)
388+
{
362389
userSpace = (u32 *)(USER_CODE_RAM + userFirmwareLen);
363390
/* we dont need to handle when thisBlock len is not divisible by 4,
364391
since the linker will align everything to 4B anyway */
365392
for (i = 0; i < thisBlockLen; i = i + 4) {
366393
*userSpace++ = *(u32 *)(recvBuffer + i);
367394
}
368-
} else {
369-
userSpace = (u32 *)(USER_CODE_FLASH + userFirmwareLen);
395+
}
396+
else
397+
{
398+
if (userUploadType == DFU_UPLOAD_FLASH_0X8005000)
399+
{
400+
userSpace = (u32 *)(USER_CODE_FLASH0X8005000 + userFirmwareLen);
401+
}
402+
else
403+
{
404+
userSpace = (u32 *)(USER_CODE_FLASH0X8002000 + userFirmwareLen);
405+
}
370406

371407
flashErasePage((u32)(userSpace));
372408

@@ -394,7 +430,7 @@ bool dfuUploadStarted() {
394430

395431
void dfuFinishUpload() {
396432
while (1) {
397-
if (userFlash) {
433+
if (userUploadType==DFU_UPLOAD_RAM) {
398434
if (code_copy_lock == BEGINNING) {
399435
code_copy_lock = MIDDLE;
400436
strobePin(LED_BANK, LED, 2, 0x1000);

usb_bootloader/STM32F1/dfu.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
#define __DFU_H
2727

2828
#include "common.h"
29+
/*
30+
#define DFU_UPLOAD_NONE 0
31+
#define DFU_UPLOAD_RAM 1
32+
#define DFU_UPLOAD_FLASH_0X8005000 2
33+
#define DFU_UPLOAD_FLASH_0X8002000 3
34+
*/
35+
typedef enum {DFU_UPLOAD_NONE, DFU_UPLOAD_RAM, DFU_UPLOAD_FLASH_0X8005000,DFU_UPLOAD_FLASH_0X8002000} dfuUploadTypes_t;
2936

3037
/* exposed types */
3138
typedef u8 *(*ClassReqCB)(u16);

usb_bootloader/STM32F1/main.c

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333

3434
#include "common.h"
3535
#include "dfu.h"
36+
extern volatile dfuUploadTypes_t userUploadType;
3637

37-
int main() {
38+
int main()
39+
{
3840
systemReset(); // peripherals but not PC
3941
setupCLK();
4042
setupLED();
@@ -44,26 +46,43 @@ int main() {
4446

4547
strobePin(LED_BANK, LED, STARTUP_BLINKS, BLINK_FAST);
4648

47-
/* wait for host to upload program or halt bootloader */
48-
bool no_user_jump = !checkUserCode(USER_CODE_FLASH) || readPin(BUTTON_BANK,BUTTON);
49-
int delay_count = 0;
49+
/* wait for host to upload program or halt bootloader */
50+
bool no_user_jump = (!checkUserCode(USER_CODE_FLASH0X8005000) && !checkUserCode(USER_CODE_FLASH0X8002000)) || readPin(BUTTON_BANK,BUTTON);
51+
int delay_count = 0;
5052

51-
while ((delay_count++ < BOOTLOADER_WAIT)
52-
|| no_user_jump) {
53+
while ((delay_count++ < BOOTLOADER_WAIT) || no_user_jump)
54+
{
5355

5456
strobePin(LED_BANK, LED, 1, BLINK_SLOW);
5557

56-
if (dfuUploadStarted()) {
58+
if (dfuUploadStarted())
59+
{
5760
dfuFinishUpload(); // systemHardReset from DFU once done
5861
}
5962
}
6063

61-
if (checkUserCode(USER_CODE_FLASH))
64+
if (checkUserCode(USER_CODE_FLASH0X8002000))
65+
{
66+
jumpToUser(USER_CODE_FLASH0X8002000);
67+
}
68+
else
6269
{
63-
jumpToUser(USER_CODE_FLASH);
64-
} else {
65-
// some sort of fault occurred, hard reset
66-
strobePin(LED_BANK, LED, 5, BLINK_FAST);
67-
systemHardReset();
70+
if (checkUserCode(USER_CODE_FLASH0X8005000))
71+
{
72+
jumpToUser(USER_CODE_FLASH0X8005000);
73+
}
74+
else
75+
{
76+
if (checkUserCode(USER_CODE_RAM) && userUploadType==DFU_UPLOAD_RAM)
77+
{
78+
jumpToUser(USER_CODE_RAM);
79+
}
80+
else
81+
{
82+
// Nothing to execute in either Flash or RAM
83+
strobePin(LED_BANK, LED, 5, BLINK_FAST);
84+
systemHardReset();
85+
}
86+
}
6887
}
69-
}
88+
}

usb_bootloader/STM32F1/usb_descriptor.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ ONE_DESCRIPTOR usbFunctionalDescriptor = {
8181
};
8282

8383

84-
u8 u8_usbConfigDescriptorDFU[36] = {
84+
u8 u8_usbConfigDescriptorDFU[45] = {
8585
0x09, /* bLength: Configuation Descriptor size */
8686
0x02, /* bDescriptorType: Configuration */
8787
0x24, /* wTotalLength: Bytes returned */
@@ -118,6 +118,20 @@ u8 u8_usbConfigDescriptorDFU[36] = {
118118
0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */
119119

120120
0x05, /* iInterface: */
121+
122+
/************ Descriptor of DFU interface 0 Alternate setting 2 *********/
123+
0x09, /* bLength: Interface Descriptor size */
124+
0x04, /* bDescriptorType: */
125+
0x00, /* bInterfaceNumber: Number of Interface */
126+
0x02, /* bAlternateSetting: Alternate setting */
127+
0x00, /* bNumEndpoints*/
128+
0xFE, /* bInterfaceClass: DFU */
129+
0x01, /* bInterfaceSubClass */
130+
131+
0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */
132+
133+
0x06, /* iInterface: */
134+
121135

122136
/******************** DFU Functional Descriptor********************/
123137
0x09, /*blength = 7 Bytes*/
@@ -172,6 +186,15 @@ u8 u8_usbStringAlt0[0x36] = {
172186
};
173187

174188
u8 u8_usbStringAlt1[0x3A] = {
189+
0x3A,
190+
0x03,
191+
'D', 0, 'F', 0, 'U', 0, ' ', 0, 'P', 0, 'r', 0, 'o', 0, 'g', 0, 'r', 0,
192+
'a', 0, 'm', 0, ' ', 0, 'F', 0, 'L', 0, 'A', 0, 'S', 0, 'H', 0, ' ', 0,
193+
'0', 0, 'x', 0, '0', 0, '8', 0, '0', 0, '0', 0, '5', 0, '0', 0, '0', 0,
194+
'0', 0
195+
};
196+
197+
u8 u8_usbStringAlt2[0x3A] = {
175198
0x3A,
176199
0x03,
177200
'D', 0, 'F', 0, 'U', 0, ' ', 0, 'P', 0, 'r', 0, 'o', 0, 'g', 0, 'r', 0,
@@ -188,6 +211,7 @@ ONE_DESCRIPTOR usbStringDescriptor[STR_DESC_LEN] = {
188211
{ (u8 *)u8_usbStringProduct, 0x20 },
189212
{ (u8 *)u8_usbStringSerial, 0x10 },
190213
{ (u8 *)u8_usbStringAlt0, 0x36 },
191-
{ (u8 *)u8_usbStringAlt1, 0x3A }
214+
{ (u8 *)u8_usbStringAlt1, 0x3A },
215+
{ (u8 *)u8_usbStringAlt2, 0x3A }
192216
};
193217

usb_bootloader/STM32F1/usb_descriptor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
#include "usb_lib.h"
3030
#include "usb.h"
3131

32-
#define NUM_ALT_SETTINGS 2
33-
#define STR_DESC_LEN 6
32+
#define NUM_ALT_SETTINGS 3
33+
#define STR_DESC_LEN 7
3434

3535
extern ONE_DESCRIPTOR usbDeviceDescriptorDFU;
3636
extern ONE_DESCRIPTOR usbConfigDescriptorDFU;
37-
extern ONE_DESCRIPTOR usbStringDescriptor[6];
37+
extern ONE_DESCRIPTOR usbStringDescriptor[STR_DESC_LEN];
3838
extern ONE_DESCRIPTOR usbFunctionalDescriptor;
3939

4040
#endif

0 commit comments

Comments
 (0)