Skip to content

Commit 6da854a

Browse files
author
Jim Lindblom
committed
Updating cores to Arduino samd 1.6.11 cores
1 parent 8df19bb commit 6da854a

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

sparkfun/samd/cores/arduino/Arduino.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ extern "C"{
5555

5656
void yield( void ) ;
5757

58+
/* system functions */
59+
int main( void );
60+
void init( void );
61+
5862
/* sketch */
5963
void setup( void ) ;
6064
void loop( void ) ;

sparkfun/samd/cores/arduino/Reset.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ extern "C" {
2424
#endif
2525

2626
#define NVM_MEMORY ((volatile uint16_t *)0x000000)
27+
28+
#if (ARDUINO_SAMD_VARIANT_COMPLIANCE >= 10610)
29+
30+
extern const uint32_t __text_start__;
31+
#define APP_START ((volatile uint32_t)(&__text_start__) + 4)
32+
33+
#else
2734
#define APP_START 0x00002004
35+
#endif
2836

2937
static inline bool nvmReady(void) {
3038
return NVMCTRL->INTFLAG.reg & NVMCTRL_INTFLAG_READY;
@@ -35,6 +43,13 @@ static void banzai() {
3543
// Disable all interrupts
3644
__disable_irq();
3745

46+
// Avoid erasing the application if APP_START is < than the minimum bootloader size
47+
// This could happen if without_bootloader linker script was chosen
48+
// Minimum bootloader size in SAMD21 family is 512bytes (RM section 22.6.5)
49+
if (APP_START < (0x200 + 4)) {
50+
goto reset;
51+
}
52+
3853
// Erase application
3954
while (!nvmReady())
4055
;
@@ -44,6 +59,7 @@ static void banzai() {
4459
while (!nvmReady())
4560
;
4661

62+
reset:
4763
// Reset the device
4864
NVIC_SystemReset() ;
4965

sparkfun/samd/cores/arduino/USB/SAMD21_USBDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class USBDevice_SAMD21G18x {
106106
inline void epBank0EnableStalled(ep_t ep) { usb.DeviceEndpoint[ep].EPINTENSET.bit.STALL0 = 1; }
107107
inline void epBank1EnableStalled(ep_t ep) { usb.DeviceEndpoint[ep].EPINTENSET.bit.STALL1 = 1; }
108108
inline void epBank0EnableTransferComplete(ep_t ep) { usb.DeviceEndpoint[ep].EPINTENSET.bit.TRCPT0 = 1; }
109-
inline void epBank1EnableTransferComplete(ep_t ep) { usb.DeviceEndpoint[ep].EPINTENSET.bit.TRCPT0 = 1; }
109+
inline void epBank1EnableTransferComplete(ep_t ep) { usb.DeviceEndpoint[ep].EPINTENSET.bit.TRCPT1 = 1; }
110110

111111
inline void epBank0DisableSetupReceived(ep_t ep) { usb.DeviceEndpoint[ep].EPINTENCLR.bit.RXSTP = 1; }
112112
inline void epBank0DisableStalled(ep_t ep) { usb.DeviceEndpoint[ep].EPINTENCLR.bit.STALL0 = 1; }

sparkfun/samd/cores/arduino/USB/USBDesc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#define CDC_TX CDC_ENDPOINT_IN
4242
#endif
4343

44-
#define ISERIAL_MAX_LEN 20
44+
#define ISERIAL_MAX_LEN 33
4545

4646
// Defined string description
4747
#define IMANUFACTURER 1

sparkfun/samd/cores/arduino/startup.c

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
// Constants for Clock generators
3939
#define GENERIC_CLOCK_GENERATOR_MAIN (0u)
4040
#define GENERIC_CLOCK_GENERATOR_XOSC32K (1u)
41+
#define GENERIC_CLOCK_GENERATOR_OSC32K (1u)
4142
#define GENERIC_CLOCK_GENERATOR_OSCULP32K (2u) /* Initialized at reset for WDT */
4243
#define GENERIC_CLOCK_GENERATOR_OSC8M (3u)
4344
// Constants for Clock multiplexers
@@ -51,6 +52,24 @@ void SystemInit( void )
5152
/* Turn on the digital interface clock */
5253
PM->APBAMASK.reg |= PM_APBAMASK_GCLK ;
5354

55+
56+
#if defined(CRYSTALLESS)
57+
58+
/* ----------------------------------------------------------------------------------------------
59+
* 1) Enable OSC32K clock (Internal 32.768Hz oscillator)
60+
*/
61+
62+
uint32_t calib = (*((uint32_t *) FUSES_OSC32K_CAL_ADDR) & FUSES_OSC32K_CAL_Msk) >> FUSES_OSC32K_CAL_Pos;
63+
64+
SYSCTRL->OSC32K.reg = SYSCTRL_OSC32K_CALIB(calib) |
65+
SYSCTRL_OSC32K_STARTUP( 0x6u ) | // cf table 15.10 of product datasheet in chapter 15.8.6
66+
SYSCTRL_OSC32K_EN32K |
67+
SYSCTRL_OSC32K_ENABLE;
68+
69+
while ( (SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_OSC32KRDY) == 0 ); // Wait for oscillator stabilization
70+
71+
#else // has crystal
72+
5473
/* ----------------------------------------------------------------------------------------------
5574
* 1) Enable XOSC32K clock (External on-board 32.768Hz oscillator)
5675
*/
@@ -63,6 +82,8 @@ void SystemInit( void )
6382
/* Wait for oscillator stabilization */
6483
}
6584

85+
#endif
86+
6687
/* Software reset the module to ensure it is re-initialized correctly */
6788
/* Note: Due to synchronization, there is a delay from writing CTRL.SWRST until the reset is complete.
6889
* CTRL.SWRST and STATUS.SYNCBUSY will both be cleared when the reset is complete, as described in chapter 13.8.1
@@ -85,8 +106,12 @@ void SystemInit( void )
85106
}
86107

87108
/* Write Generic Clock Generator 1 configuration */
88-
GCLK->GENCTRL.reg = GCLK_GENCTRL_ID( GENERIC_CLOCK_GENERATOR_XOSC32K ) | // Generic Clock Generator 1
109+
GCLK->GENCTRL.reg = GCLK_GENCTRL_ID( GENERIC_CLOCK_GENERATOR_OSC32K ) | // Generic Clock Generator 1
110+
#if defined(CRYSTALLESS)
111+
GCLK_GENCTRL_SRC_OSC32K | // Selected source is Internal 32KHz Oscillator
112+
#else
89113
GCLK_GENCTRL_SRC_XOSC32K | // Selected source is External 32KHz Oscillator
114+
#endif
90115
// GCLK_GENCTRL_OE | // Output clock to a pin for tests
91116
GCLK_GENCTRL_GENEN ;
92117

@@ -130,6 +155,41 @@ void SystemInit( void )
130155
/* Wait for synchronization */
131156
}
132157

158+
#if defined(CRYSTALLESS)
159+
160+
#define NVM_SW_CALIB_DFLL48M_COARSE_VAL 58
161+
#define NVM_SW_CALIB_DFLL48M_FINE_VAL 64
162+
163+
// Turn on DFLL
164+
uint32_t coarse =( *((uint32_t *)(NVMCTRL_OTP4) + (NVM_SW_CALIB_DFLL48M_COARSE_VAL / 32)) >> (NVM_SW_CALIB_DFLL48M_COARSE_VAL % 32) )
165+
& ((1 << 6) - 1);
166+
if (coarse == 0x3f) {
167+
coarse = 0x1f;
168+
}
169+
uint32_t fine =( *((uint32_t *)(NVMCTRL_OTP4) + (NVM_SW_CALIB_DFLL48M_FINE_VAL / 32)) >> (NVM_SW_CALIB_DFLL48M_FINE_VAL % 32) )
170+
& ((1 << 10) - 1);
171+
if (fine == 0x3ff) {
172+
fine = 0x1ff;
173+
}
174+
175+
SYSCTRL->DFLLVAL.bit.COARSE = coarse;
176+
SYSCTRL->DFLLVAL.bit.FINE = fine;
177+
/* Write full configuration to DFLL control register */
178+
SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_USBCRM | /* USB correction */
179+
SYSCTRL_DFLLCTRL_CCDIS |
180+
SYSCTRL_DFLLCTRL_WAITLOCK |
181+
SYSCTRL_DFLLCTRL_QLDIS ; /* Disable Quick lock */
182+
183+
while ( (SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_DFLLRDY) == 0 )
184+
{
185+
/* Wait for synchronization */
186+
}
187+
188+
/* Enable the DFLL */
189+
SYSCTRL->DFLLCTRL.reg |= SYSCTRL_DFLLCTRL_ENABLE ;
190+
191+
#else // has crystal
192+
133193
/* Write full configuration to DFLL control register */
134194
SYSCTRL->DFLLCTRL.reg |= SYSCTRL_DFLLCTRL_MODE | /* Enable the closed loop mode */
135195
SYSCTRL_DFLLCTRL_WAITLOCK |
@@ -149,6 +209,8 @@ void SystemInit( void )
149209
/* Wait for locks flags */
150210
}
151211

212+
#endif
213+
152214
while ( (SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_DFLLRDY) == 0 )
153215
{
154216
/* Wait for synchronization */

0 commit comments

Comments
 (0)