Skip to content

Commit f0780cc

Browse files
committed
Made various changes to reduce code size, including making ADC and DAC initialization optional if unused, using VARIANT_MCK instead of SystemCoreClock in init(), and converting some RMW to writes. Added config.h file for configuration. (boards.txt in later commit)
1 parent 29f870a commit f0780cc

File tree

8 files changed

+419
-309
lines changed

8 files changed

+419
-309
lines changed

bootloaders/zero/board_startup.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,9 @@ void Reset_Handler( void )
130130
#if (SAMD21_SERIES || SAMD11_SERIES)
131131
SBMATRIX->SFR[SBMATRIX_SLAVE_HMCRAMC0].reg = 2;
132132

133-
USB->DEVICE.QOSCTRL.bit.CQOS = 2;
134-
USB->DEVICE.QOSCTRL.bit.DQOS = 2;
133+
USB->DEVICE.QOSCTRL.reg = (USB_QOSCTRL_CQOS(2) | USB_QOSCTRL_DQOS(2));
135134

136-
DMAC->QOSCTRL.bit.DQOS = 2;
137-
DMAC->QOSCTRL.bit.FQOS = 2;
138-
DMAC->QOSCTRL.bit.WRBQOS = 2;
135+
DMAC->QOSCTRL.reg = (DMAC_QOSCTRL_WRBQOS(2) | DMAC_QOSCTRL_FQOS(2) | DMAC_QOSCTRL_DQOS(2));
139136
#endif
140137

141138
#if (SAMD51)

config.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2018 MattairTech LLC. All right reserved.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef _CONFIG_H_
20+
#define _CONFIG_H_
21+
22+
/* Currently, there are only two options in the menu, to enable or disable config.h.
23+
* When config.h is enabled, comment out lines to disable individual options.
24+
*/
25+
#if defined(CONFIG_H_ENABLED)
26+
27+
/* The standard PinDescription table uses 12 bytes per pin. Define PIN_DESCRIPTION_TABLE_SIMPLE
28+
* to use a more compact format that uses only 4 bytes per pin (currently only available
29+
* for the D11 chips). In this case, the PinType, PinAttribute, and GCLKCCL columns are not used
30+
* (they are not required). Additionally, the SetPortPin() and SetExtIntADC() macros are used to
31+
* pack Port and Pin into the PortPin column, and ExtInt and ADCChannelNumber into the ExtIntADC
32+
* column. See the variant README.md for more information. Note that external libraries that
33+
* reference the PinDescription table directly (uncommon) will no longer work. This define can be
34+
* combined with the PIN_MAP_COMPACT define, which is available in variant.h of the D11 variants.
35+
* This can save from 10's to over 250 bytes.
36+
*
37+
* Note that when using this define, EXTERNAL_INT_15 and the EXTERNAL_INT_NMI are NOT available
38+
* and only ADC_Channel0 through ADC_Channel14 are available.
39+
*/
40+
//#define PIN_DESCRIPTION_TABLE_SIMPLE
41+
42+
/* Define PIN_PERIPHERAL_CHECKS_DISABLED to disable some sanity and other checks at the beginning
43+
* of pinPeripheral() (ie: out of bounds access, pin isn't usable as PIO, verify hardware peripheral
44+
* requested actually exists on pin). This saves about 100 bytes.
45+
*/
46+
#define PIN_PERIPHERAL_CHECKS_DISABLED
47+
48+
/* Define ADC_NO_INIT_IF_UNUSED and/or REF_NO_INIT_IF_UNUSED to save the code space used by the
49+
* initialization functions of the ADC, DAC, and REF, if they are not used by the core. Do not
50+
* set this define if an external library is used for the ADC, DAC, or REF, as that library probably
51+
* expects the associated hardware to be already initialized. Note that if using a core function
52+
* (ie: analogWrite()), initialization will still occur automatically. This saves about 350 bytes
53+
* with ADC_NO_INIT_IF_UNUSED and 50 bytes with DAC_NO_INIT_IF_UNUSED.
54+
*/
55+
#define ADC_NO_INIT_IF_UNUSED
56+
#define DAC_NO_INIT_IF_UNUSED
57+
58+
/* Define DISABLE_ADC_CALIBRATION to disable ADC calibration using the NVM factory values, which
59+
* occurs during initialization, and save a few bytes. This setting is not generally recommended.
60+
*/
61+
//#define DISABLE_ADC_CALIBRATION
62+
63+
/* Define TRUST_RESET_DEFAULTS to avoid writing default values into registers during hardware
64+
* initialization, saving about 50 bytes (currently).
65+
*/
66+
#define TRUST_RESET_DEFAULTS
67+
68+
/* Define NO_ADDITIONAL_GCLKS to disable initialization of GENERIC_CLOCK_GENERATOR_192MHz (D51) and
69+
* GENERIC_CLOCK_GENERATOR_48MHz (all others), which are currently unused in the core. This currently
70+
* saves about 48 bytes.
71+
*/
72+
#define NO_ADDITIONAL_GCLKS
73+
74+
/* Define NO_OSC_HS_GCLK to disable initialization of GENERIC_CLOCK_GENERATOR_OSC_HS, which is setup
75+
* to run at 8MHz with the D21, D11, and L21, but is unused by the core. This saves about 30 bytes.
76+
*/
77+
#define NO_OSC_HS_GCLK
78+
79+
/* Define NO_DELAY_HIGH_WORD to disable the high word variable (_ulTickCountHighWord) in delay.c,
80+
* which is used to allow counting beyond the 50-day maximum (for a 32-bit variable counting in
81+
* milliseconds). This saves about 64 bytes.
82+
*/
83+
#define NO_DELAY_HIGH_WORD
84+
#endif
85+
86+
#endif // _CONFIG_H_

cores/arduino/cortex_handlers.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,9 @@ void Reset_Handler(void)
530530
#if (SAMD21 || SAMD11)
531531
SBMATRIX->SFR[SBMATRIX_SLAVE_HMCRAMC0].reg = 2;
532532

533-
USB->DEVICE.QOSCTRL.bit.CQOS = 2;
534-
USB->DEVICE.QOSCTRL.bit.DQOS = 2;
533+
USB->DEVICE.QOSCTRL.reg = (USB_QOSCTRL_CQOS(2) | USB_QOSCTRL_DQOS(2));
535534

536-
DMAC->QOSCTRL.bit.DQOS = 2;
537-
DMAC->QOSCTRL.bit.FQOS = 2;
538-
DMAC->QOSCTRL.bit.WRBQOS = 2;
535+
DMAC->QOSCTRL.reg = (DMAC_QOSCTRL_WRBQOS(2) | DMAC_QOSCTRL_FQOS(2) | DMAC_QOSCTRL_DQOS(2));
539536
#endif
540537

541538
#if (SAMD51)

cores/arduino/delay.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818

1919
#include "delay.h"
2020
#include "Arduino.h"
21+
#include "../../config.h"
2122

2223
#ifdef __cplusplus
2324
extern "C" {
2425
#endif
2526

2627
/** Tick Counter united by ms */
2728
static volatile uint32_t _ulTickCount=0 ;
29+
#if !defined(NO_DELAY_HIGH_WORD)
2830
static volatile uint32_t _ulTickCountHighWord=0 ;
31+
#endif
2932

3033
unsigned long millis( void )
3134
{
@@ -66,10 +69,13 @@ void delay( unsigned long ms )
6669
{
6770
if (ms)
6871
{
72+
#if !defined(NO_DELAY_HIGH_WORD)
6973
uint8_t enableInterrupts = ((__get_PRIMASK() & 0x1) == 0);
7074
__disable_irq();
75+
#endif
7176

7277
uint32_t start = _ulTickCount ;
78+
#if !defined(NO_DELAY_HIGH_WORD)
7379
uint32_t targetTickCountHighWord = _ulTickCountHighWord;
7480

7581
if (enableInterrupts) {
@@ -83,11 +89,16 @@ void delay( unsigned long ms )
8389
start = 0;
8490
targetTickCountHighWord++;
8591
}
92+
#endif
8693

8794
do
8895
{
8996
yield() ;
97+
#if !defined(NO_DELAY_HIGH_WORD)
9098
} while (_ulTickCountHighWord < targetTickCountHighWord || (_ulTickCount - start) < ms ) ;
99+
#else
100+
} while (_ulTickCount - start < ms ) ;
101+
#endif
91102
}
92103
}
93104

@@ -98,10 +109,12 @@ void SysTick_DefaultHandler(void)
98109
// Increment tick count each ms
99110
_ulTickCount++;
100111

112+
#if !defined(NO_DELAY_HIGH_WORD)
101113
if ( _ulTickCount == 0 )
102114
{
103115
_ulTickCountHighWord++;
104116
}
117+
#endif
105118
#if defined(CDC_ONLY) || defined(CDC_HID) || defined(WITH_CDC)
106119
tickReset();
107120
#endif

0 commit comments

Comments
 (0)