8000 atmel-samd: Reboot to bootloader when a 1200 baud serial connection is · sparkfun/circuitpython@5d732f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d732f4

Browse files
committed
atmel-samd: Reboot to bootloader when a 1200 baud serial connection is
closed. This is how Arduino triggers the bootloader.
1 parent 0c4b273 commit 5d732f4

File tree

12 files changed

+127
-26
lines changed

12 files changed

+127
-26
lines changed

atmel-samd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ SRC_C = \
190190
main.c \
191191
moduos.c \
192192
mphalport.c \
193+
reset.c \
193194
samd21_pins.c \
194195
shared_dma.c \
195196
rgb_led_status.c \

atmel-samd/boards/arduino_zero/conf_usb.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdbool.h>
33
#include <stdint.h>
44

5+
#include "asf/common/services/usb/class/cdc/usb_protocol_cdc.h"
6+
57
#ifndef CONF_USB_H_INCLUDED
68
#define CONF_USB_H_INCLUDED
79

@@ -55,7 +57,8 @@ extern void mp_cdc_disable(uint8_t port);
5557

5658
#define UDI_CDC_RX_NOTIFY(port) usb_rx_notify()
5759
void usb_rx_notify(void);
58-
#define UDI_CDC_SET_CODING_EXT(port,cfg)
60+
#define UDI_CDC_SET_CODING_EXT(port,cfg) usb_coding_notify(port, cfg)
61+
void usb_coding_notify(uint8_t port, usb_cdc_line_coding_t* coding);
5962
#define UDI_CDC_SET_DTR_EXT(port,set) usb_dtr_notify(port, set)
6063
void usb_dtr_notify(uint8_t port, bool set);
6164
#define UDI_CDC_SET_RTS_EXT(port,set) usb_rts_notify(port, set)

atmel-samd/boards/circuitplayground_express/conf_usb.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdbool.h>
33
#include <stdint.h>
44

5+
#include "asf/common/services/usb/class/cdc/usb_protocol_cdc.h"
6+
57
#ifndef CONF_USB_H_INCLUDED
68
#define CONF_USB_H_INCLUDED
79

@@ -55,7 +57,8 @@ extern void mp_cdc_disable(uint8_t port);
5557

5658
#define UDI_CDC_RX_NOTIFY(port) usb_rx_notify()
5759
void usb_rx_notify(void);
58-
#define UDI_CDC_SET_CODING_EXT(port,cfg)
60+
#define UDI_CDC_SET_CODING_EXT(port,cfg) usb_coding_notify(port, cfg)
61+
void usb_coding_notify(uint8_t port, usb_cdc_line_coding_t* coding);
5962
#define UDI_CDC_SET_DTR_EXT(port,set) usb_dtr_notify(port, set)
6063
void usb_dtr_notify(uint8_t port, bool set);
6164
#define UDI_CDC_SET_RTS_EXT(port,set) usb_rts_notify(port, set)

atmel-samd/boards/feather_m0_adalogger/conf_usb.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdbool.h>
33
#include <stdint.h>
44

5+
#include "asf/common/services/usb/class/cdc/usb_protocol_cdc.h"
6+
57
#ifndef CONF_USB_H_INCLUDED
68
#define CONF_USB_H_INCLUDED
79

@@ -55,7 +57,8 @@ extern void mp_cdc_disable(uint8_t port);
5557

5658
#define UDI_CDC_RX_NOTIFY(port) usb_rx_notify()
5759
void usb_rx_notify(void);
58-
#define UDI_CDC_SET_CODING_EXT(port,cfg)
60+
#define UDI_CDC_SET_CODING_EXT(port,cfg) usb_coding_notify(port, cfg)
61+
void usb_coding_notify(uint8_t port, usb_cdc_line_coding_t* coding);
5962
#define UDI_CDC_SET_DTR_EXT(port,set) usb_dtr_notify(port, set)
6063
void usb_dtr_notify(uint8_t port, bool set);
6164
#define UDI_CDC_SET_RTS_EXT(port,set) usb_rts_notify(port, set)

atmel-samd/boards/feather_m0_basic/conf_usb.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdbool.h>
33
#include <stdint.h>
44

5+
#include "asf/common/services/usb/class/cdc/usb_protocol_cdc.h"
6+
57
#ifndef CONF_USB_H_INCLUDED
68
#define CONF_USB_H_INCLUDED
79

@@ -55,7 +57,8 @@ extern void mp_cdc_disable(uint8_t port);
5557

5658
#define UDI_CDC_RX_NOTIFY(port) usb_rx_notify()
5759
void usb_rx_notify(void);
58-
#define UDI_CDC_SET_CODING_EXT(port,cfg)
60+
#define UDI_CDC_SET_CODING_EXT(port,cfg) usb_coding_notify(port, cfg)
61+
void usb_coding_notify(uint8_t port, usb_cdc_line_coding_t* coding);
5962
#define UDI_CDC_SET_DTR_EXT(port,set) usb_dtr_notify(port, set)
6063
void usb_dtr_notify(uint8_t port, bool set);
6164
#define UDI_CDC_SET_RTS_EXT(port,set) usb_rts_notify(port, set)

atmel-samd/boards/feather_m0_express/conf_usb.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdbool.h>
33
#include <stdint.h>
44

5+
#include "asf/common/services/usb/class/cdc/usb_protocol_cdc.h"
6+
57
#ifndef CONF_USB_H_INCLUDED
68
#define CONF_USB_H_INCLUDED
79

@@ -55,7 +57,8 @@ extern void mp_cdc_disable(uint8_t port);
5557

5658
#define UDI_CDC_RX_NOTIFY(port) usb_rx_notify()
5759
void usb_rx_notify(void);
58-
#define UDI_CDC_SET_CODING_EXT(port,cfg)
60+
#define UDI_CDC_SET_CODING_EXT(port,cfg) usb_coding_notify(port, cfg)
61+
void usb_coding_notify(uint8_t port, usb_cdc_line_coding_t* coding);
5962
#define UDI_CDC_SET_DTR_EXT(port,set) usb_dtr_notify(port, set)
6063
void usb_dtr_notify(uint8_t port, bool set);
6164
#define UDI_CDC_SET_RTS_EXT(port,set) usb_rts_notify(port, set)

atmel-samd/boards/gemma_m0/conf_usb.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdbool.h>
33
#include <stdint.h>
44

5+
#include "asf/common/services/usb/class/cdc/usb_protocol_cdc.h"
6+
57
#ifndef CONF_USB_H_INCLUDED
68
#define CONF_USB_H_INCLUDED
79

@@ -55,7 +57,8 @@ extern void mp_cdc_disable(uint8_t port);
5557

5658
#define UDI_CDC_RX_NOTIFY(port) usb_rx_notify()
5759
void usb_rx_notify(void);
58-
#define UDI_CDC_SET_CODING_EXT(port,cfg)
60+
#define UDI_CDC_SET_CODING_EXT(port,cfg) usb_coding_notify(port, cfg)
61+
void usb_coding_notify(uint8_t port, usb_cdc_line_coding_t* coding);
5962
#define UDI_CDC_SET_DTR_EXT(port,set) usb_dtr_notify(port, set)
6063
void usb_dtr_notify(uint8_t port, bool set);
6164
#define UDI_CDC_SET_RTS_EXT(port,set) usb_rts_notify(port, set)

atmel-samd/boards/metro_m0_express/conf_usb.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdbool.h>
33
#include <stdint.h>
44

5+
#include "asf/common/services/usb/class/cdc/usb_protocol_cdc.h"
6+
57
#ifndef CONF_USB_H_INCLUDED
68
#define CONF_USB_H_INCLUDED
79

@@ -19,7 +21,7 @@
1921
#endif
2022

2123
#ifndef USB_DEVICE_PRODUCT_NAME
22-
# define USB_DEVICE_PRODUCT_NAME "Metro M0 with Flash (Experimental)"
24+
# define USB_DEVICE_PRODUCT_NAME "Metro M0 Express"
2325
#endif
2426
// #define USB_DEVICE_SERIAL_NAME "12...EF"
2527
#define USB_DEVICE_GET_SERIAL_NAME_POINTER serial_number
@@ -55,7 +57,8 @@ extern void mp_cdc_disable(uint8_t port);
5557

5658
#define UDI_CDC_RX_NOTIFY(port) usb_rx_notify()
5759
void usb_rx_notify(void);
58-
#define UDI_CDC_SET_CODING_EXT(port,cfg)
60+
#define UDI_CDC_SET_CODING_EXT(port,cfg) usb_coding_notify(port, cfg)
61+
void usb_coding_notify(uint8_t port, usb_cdc_line_coding_t* coding);
5962
#define UDI_CDC_SET_DTR_EXT(port,set) usb_dtr_notify(port, set)
6063
void usb_dtr_notify(uint8_t port, bool set);
6164
#define UDI_CDC_SET_RTS_EXT(port,set) usb_rts_notify(port, set)

atmel-samd/boards/trinket_m0/conf_usb.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdbool.h>
33
#include <stdint.h>
44

5+
#include "asf/common/services/usb/class/cdc/usb_protocol_cdc.h"
6+
57
#ifndef CONF_USB_H_INCLUDED
68
#define CONF_USB_H_INCLUDED
79

@@ -55,7 +57,9 @@ extern void mp_cdc_disable(uint8_t port);
5557

5658
#define UDI_CDC_RX_NOTIFY(port) usb_rx_notify()
5759
void usb_rx_notify(void);
58-
#define UDI_CDC_SET_CODING_EXT(port,cfg)
60+
61+
#define UDI_CDC_SET_CODING_EXT(port,cfg) usb_coding_notify(port, cfg)
62+
void usb_coding_notify(uint8_t port, usb_cdc_line_coding_t* coding);
5963
#define UDI_CDC_SET_DTR_EXT(port,set) usb_dtr_notify(port, set)
6064
void usb_dtr_notify(uint8_t port, bool set);
6165
#define UDI_CDC_SET_RTS_EXT(port,set) usb_rts_notify(port, set)

atmel-samd/mphalport.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,42 @@ extern struct usart_module usart_instance;
3939

4040
// Read by main to know when USB is connected.
4141
volatile bool mp_msc_enabled = false;
42-
bool mp_msc_enable()
43-
{
44-
mp_msc_enabled = true;
45-
return true;
42+
bool mp_msc_enable() {
43+
mp_msc_enabled = true;
44+
return true;
4645
}
4746

48-
void mp_msc_disable()
49-
{
50-
mp_msc_enabled = false;
47+
void mp_msc_disable() {
48+
mp_msc_enabled = false;
5149
}
5250

53-
bool mp_cdc_enable(uint8_t port)
54-
{
55-
mp_cdc_enabled = false;
56-
return true;
51+
bool mp_cdc_enable(uint8_t port) {
52+
mp_cdc_enabled = false;
53+
return true;
5754
}
5855

59-
void mp_cdc_disable(uint8_t port)
60-
{
61-
mp_cdc_enabled = false;
56+
void mp_cdc_disable(uint8_t port) {
57+
mp_cdc_enabled = false;
6258
}
6359

60+
volatile bool reset_on_disconnect = false;
61+
6462
void usb_dtr_notify(uint8_t port, bool set) {
65-
mp_cdc_enabled = set;
63+
mp_cdc_enabled = set;
64+
if (!set && reset_on_disconnect) {
65+
reset_to_bootloader();
66+
}
6667
}
6768

6869
void usb_rts_notify(uint8_t port, bool set) {
6970
return;
7071
}
7172

72-
void usb_rx_notify(void)
73-
{
73+
void usb_coding_notify(uint8_t port, usb_cdc_line_coding_t* coding) {
74+
reset_on_disconnect = coding->dwDTERate == 1200;
75+
}
76+
77+
void usb_rx_notify(void) {
7478
irqflags_t flags;
7579
if (mp_cdc_enabled) {
7680
while (udi_cdc_is_rx_ready()) {

atmel-samd/reset.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "flash_api.h"
28+
#include "reset.h"
29+
30+
#include "asf/sam0/utils/cmsis/samd21/include/samd21.h"
31+
32+
// Copied from inc/uf2.h in https://github.com/Microsoft/uf2-samd21
33+
#define DBL_TAP_PTR ((volatile uint32_t *)(HMCRAMC0_ADDR + HMCRAMC0_SIZE - 4))
34+
#define DBL_TAP_MAGIC 0xf01669ef // Randomly selected, adjusted to have first and last bit set
35+
36+
void reset_to_bootloader(void) {
37+
flash_flush();
38+
*DBL_TAP_PTR = DBL_TAP_MAGIC;
39+
NVIC_SystemReset();
40+
}

atmel-samd/reset.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALI 6B2C NGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_RESET_H__
27+
#define __MICROPY_INCLUDED_ATMEL_SAMD_RESET_H__
28+
29+
void reset_to_bootloader(void);
30+
31+
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_RESET_H__

0 commit comments

Comments
 (0)
0