2020#include "Arduino.h"
2121
2222#ifdef __cplusplus
23- extern "C" {
23+ extern "C"
24+ {
2425#endif
2526
26-
2727#if defined(__SAMD51__ )
28- uint32_t SystemCoreClock = F_CPU ;
28+ uint32_t SystemCoreClock = F_CPU ;
2929#else
3030/*
3131 * System Core Clock is at 1MHz (8MHz/8) at Reset.
3232 * It is switched to 48MHz in the Reset Handler (startup.c)
3333 */
34- uint32_t SystemCoreClock = 1000000ul ;
34+ uint32_t SystemCoreClock = 1000000ul ;
3535#endif
3636
37- /*
37+ /*
3838void calibrateADC()
3939{
4040 volatile uint32_t valeur = 0;
@@ -53,55 +53,55 @@ void calibrateADC()
5353 valeur = valeur/5;
5454}*/
5555
56- /*
56+ /*
5757 * Arduino Zero board initialization
5858 *
5959 * Good to know:
6060 * - At reset, ResetHandler did the system clock configuration. Core is running at 48MHz.
6161 * - Watchdog is disabled by default, unless someone plays with NVM User page
6262 * - During reset, all PORT lines are configured as inputs with input buffers, output buffers and pull disabled.
6363 */
64- void init ( void )
65- {
66- // Set Systick to 1ms interval, common to all Cortex-M variants
67- if ( SysTick_Config ( SystemCoreClock / 1000 ) )
64+ void init (void )
6865 {
69- // Capture error
70- while ( 1 ) ;
71- }
72- NVIC_SetPriority (SysTick_IRQn , (1 << __NVIC_PRIO_BITS ) - 2 ); /* set Priority for Systick Interrupt (2nd lowest) */
66+ // Set Systick to 1ms interval, common to all Cortex-M variants
67+ if (SysTick_Config (SystemCoreClock / 1000 ))
68+ {
69+ // Capture error
70+ while (1 )
71+ ;
72+ }
73+ NVIC_SetPriority (SysTick_IRQn , (1 << __NVIC_PRIO_BITS ) - 2 ); /* set Priority for Systick Interrupt (2nd lowest) */
7374
74- // Clock PORT for Digital I/O
75- // PM->APBBMASK.reg |= PM_APBBMASK_PORT ;
76- //
77- // // Clock EIC for I/O interrupts
78- // PM->APBAMASK.reg |= PM_APBAMASK_EIC ;
75+ // Clock PORT for Digital I/O
76+ // PM->APBBMASK.reg |= PM_APBBMASK_PORT ;
77+ //
78+ // // Clock EIC for I/O interrupts
79+ // PM->APBAMASK.reg |= PM_APBAMASK_EIC ;
7980
8081#if defined(__SAMD51__ )
81- MCLK -> APBAMASK .reg |= MCLK_APBAMASK_SERCOM0 | MCLK_APBAMASK_SERCOM1 | MCLK_APBAMASK_TC0 | MCLK_APBAMASK_TC1 ;
82-
83- MCLK -> APBBMASK .reg |= MCLK_APBBMASK_SERCOM2 | MCLK_APBBMASK_SERCOM3 | MCLK_APBBMASK_TCC0 | MCLK_APBBMASK_TCC1 | MCLK_APBBMASK_TC3 | MCLK_APBBMASK_TC2 ;
84-
85- MCLK -> APBCMASK .reg |= MCLK_APBCMASK_TCC2 | MCLK_APBCMASK_TCC3 | MCLK_APBCMASK_TC4 | MCLK_APBCMASK_TC5 ;
86-
87- MCLK -> APBDMASK .reg |= MCLK_APBDMASK_DAC | MCLK_APBDMASK_SERCOM4 | MCLK_APBDMASK_SERCOM5 | MCLK_APBDMASK_ADC0 | MCLK_APBDMASK_ADC1 | MCLK_APBDMASK_TCC4
88- | MCLK_APBDMASK_TC6 | MCLK_APBDMASK_TC7 | MCLK_APBDMASK_SERCOM6 | MCLK_APBDMASK_SERCOM7 ;
82+ MCLK -> APBAMASK .reg |= MCLK_APBAMASK_SERCOM0 | MCLK_APBAMASK_SERCOM1 | MCLK_APBAMASK_TC0 | MCLK_APBAMASK_TC1 ;
83+
84+ MCLK -> APBBMASK .reg |= MCLK_APBBMASK_SERCOM2 | MCLK_APBBMASK_SERCOM3 | MCLK_APBBMASK_TCC0 | MCLK_APBBMASK_TCC1 | MCLK_APBBMASK_TC3 | MCLK_APBBMASK_TC2 ;
85+
86+ MCLK -> APBCMASK .reg |= MCLK_APBCMASK_TCC2 | MCLK_APBCMASK_TCC3 | MCLK_APBCMASK_TC4 | MCLK_APBCMASK_TC5 ;
87+
88+ MCLK -> APBDMASK .reg |= MCLK_APBDMASK_DAC | MCLK_APBDMASK_SERCOM4 | MCLK_APBDMASK_SERCOM5 | MCLK_APBDMASK_ADC0 | MCLK_APBDMASK_ADC1 | MCLK_APBDMASK_TCC4 | MCLK_APBDMASK_TC6 | MCLK_APBDMASK_TC7 | MCLK_APBDMASK_SERCOM6 | MCLK_APBDMASK_SERCOM7 ;
8989
9090#else
9191 // Clock SERCOM for Serial
92- PM -> APBCMASK .reg |= PM_APBCMASK_SERCOM0 | PM_APBCMASK_SERCOM1 | PM_APBCMASK_SERCOM2 | PM_APBCMASK_SERCOM3 | PM_APBCMASK_SERCOM4 | PM_APBCMASK_SERCOM5 ;
92+ PM -> APBCMASK .reg |= PM_APBCMASK_SERCOM0 | PM_APBCMASK_SERCOM1 | PM_APBCMASK_SERCOM2 | PM_APBCMASK_SERCOM3 | PM_APBCMASK_SERCOM4 | PM_APBCMASK_SERCOM5 ;
9393
9494 // Clock TC/TCC for Pulse and Analog
9595 PM -> APBCMASK .reg |= PM_APBCMASK_TCC0 | PM_APBCMASK_TCC1 | PM_APBCMASK_TCC2 | PM_APBCMASK_TC3 | PM_APBCMASK_TC4 | PM_APBCMASK_TC5 | PM_APBCMASK_TC6 | PM_APBCMASK_TC7 ;
9696
97- // ATSAMR, for example, doesn't have a DAC
98- #ifdef PM_APBCMASK_DAC
99- // Clock ADC/DAC for Analog
100- PM -> APBCMASK .reg |= PM_APBCMASK_ADC | PM_APBCMASK_DAC ;
101- #endif
97+ // ATSAMR, for example, doesn't have a DAC
98+ #ifdef PM_APBCMASK_DAC
99+ // Clock ADC/DAC for Analog
100+ PM -> APBCMASK .reg |= PM_APBCMASK_ADC | PM_APBCMASK_DAC ;
101+ #endif
102102#endif
103103
104- /*
104+ /*
105105 Commented out to leave pins in default tri-state. This is
106106 aimed at avoiding power consumption in DeepSleep.
107107
@@ -112,103 +112,118 @@ void init( void )
112112 }
113113*/
114114
115- // Initialize Analog Controller
116- // Setting clock
115+ // Initialize Analog Controller
116+ // Setting clock
117117#if defined(__SAMD51__ )
118- //set to 1/(1/(48000000/32) * 6) = 250000 SPS
119- GCLK -> PCHCTRL [ADC0_GCLK_ID ].reg = GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos ); //use clock generator 1 (48Mhz)
120- GCLK -> PCHCTRL [ADC1_GCLK_ID ].reg = GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos ); //use clock generator 1 (48Mhz)
121- Adc * adcs [] = {ADC0 , ADC1 };
122- for (int i = 0 ; i < 2 ; i ++ ){
123-
124- adcs [i ]-> CTRLA .bit .PRESCALER = ADC_CTRLA_PRESCALER_DIV32_Val ;
125- adcs [i ]-> CTRLB .bit .RESSEL = ADC_CTRLB_RESSEL_10BIT_Val ;
126-
127- while ( adcs [i ]-> SYNCBUSY .reg & ADC_SYNCBUSY_CTRLB ); //wait for sync
128-
129- adcs [i ]-> SAMPCTRL .reg = 5 ; // sampling Time Length
130-
131- while ( adcs [i ]-> SYNCBUSY .reg & ADC_SYNCBUSY_SAMPCTRL ); //wait for sync
132-
133- adcs [i ]-> INPUTCTRL .reg = ADC_INPUTCTRL_MUXNEG_GND ; // No Negative input (Internal Ground)
134-
135- while ( adcs [i ]-> SYNCBUSY .reg & ADC_SYNCBUSY_INPUTCTRL ); //wait for sync
136-
137- // Averaging (see datasheet table in AVGCTRL register description)
138- adcs [i ]-> AVGCTRL .reg = ADC_AVGCTRL_SAMPLENUM_1 | // 1 sample only (no oversampling nor averaging)
139- ADC_AVGCTRL_ADJRES (0x0ul ); // Adjusting result by 0
140-
141- while ( adcs [i ]-> SYNCBUSY .reg & ADC_SYNCBUSY_AVGCTRL ); //wait for sync
142- }
143-
144- analogReference ( AR_DEFAULT ) ; // Analog Reference is AREF pin (3.3v)
145-
146- GCLK -> PCHCTRL [DAC_GCLK_ID ].reg = GCLK_PCHCTRL_GEN_GCLK4_Val | (1 << GCLK_PCHCTRL_CHEN_Pos ); //use clock generator 4 (12mhz)
147- while (GCLK -> PCHCTRL [DAC_GCLK_ID ].bit .CHEN == 0 );
148-
149- while ( DAC -> SYNCBUSY .bit .SWRST == 1 ); // Wait for synchronization of registers between the clock domains
150- DAC -> CTRLA .bit .SWRST = 1 ;
151- while ( DAC -> SYNCBUSY .bit .SWRST == 1 ); // Wait for synchronization of registers between the clock domains
152-
153- #ifdef VREFLESS
154- DAC -> CTRLB .reg = DAC_CTRLB_REFSEL_INTREF ; // TODO: fix this once silicon bug is fixed
155- #else
156- DAC -> CTRLB .reg = DAC_CTRLB_REFSEL_VREFPU ;
157- #endif
158-
159- //set refresh rates
160- DAC -> DACCTRL [0 ].bit .REFRESH = 2 ;
161- DAC -> DACCTRL [1 ].bit .REFRESH = 2 ;
118+ //set to 1/(1/(48000000/32) * 6) = 250000 SPS
119+ GCLK -> PCHCTRL [ADC0_GCLK_ID ].reg = GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos ); //use clock generator 1 (48Mhz)
120+ GCLK -> PCHCTRL [ADC1_GCLK_ID ].reg = GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos ); //use clock generator 1 (48Mhz)
121+ Adc * adcs [] = {ADC0 , ADC1 };
122+ for (int i = 0 ; i < 2 ; i ++ )
123+ {
124+
125+ adcs [i ]-> CTRLA .bit .PRESCALER = ADC_CTRLA_PRESCALER_DIV32_Val ;
126+ adcs [i ]-> CTRLB .bit .RESSEL = ADC_CTRLB_RESSEL_10BIT_Val ;
127+
128+ while (adcs [i ]-> SYNCBUSY .reg & ADC_SYNCBUSY_CTRLB )
129+ ; //wait for sync
130+
131+ adcs [i ]-> SAMPCTRL .reg = 5 ; // sampling Time Length
132+
133+ while (adcs [i ]-> SYNCBUSY .reg & ADC_SYNCBUSY_SAMPCTRL )
134+ ; //wait for sync
135+
136+ adcs [i ]-> INPUTCTRL .reg = ADC_INPUTCTRL_MUXNEG_GND ; // No Negative input (Internal Ground)
137+
138+ while (adcs [i ]-> SYNCBUSY .reg & ADC_SYNCBUSY_INPUTCTRL )
139+ ; //wait for sync
140+
141+ // Averaging (see datasheet table in AVGCTRL register description)
142+ adcs [i ]-> AVGCTRL .reg = ADC_AVGCTRL_SAMPLENUM_1 | // 1 sample only (no oversampling nor averaging)
143+ ADC_AVGCTRL_ADJRES (0x0ul ); // Adjusting result by 0
144+
145+ while (adcs [i ]-> SYNCBUSY .reg & ADC_SYNCBUSY_AVGCTRL )
146+ ; //wait for sync
147+ }
148+
149+ analogReference (AR_DEFAULT ); // Analog Reference is AREF pin (3.3v)
150+
151+ GCLK -> PCHCTRL [DAC_GCLK_ID ].reg = GCLK_PCHCTRL_GEN_GCLK4_Val | (1 << GCLK_PCHCTRL_CHEN_Pos ); //use clock generator 4 (12mhz)
152+ while (GCLK -> PCHCTRL [DAC_GCLK_ID ].bit .CHEN == 0 )
153+ ;
154+
155+ while (DAC -> SYNCBUSY .bit .SWRST == 1 )
156+ ; // Wait for synchronization of registers between the clock domains
157+ DAC -> CTRLA .bit .SWRST = 1 ;
158+ while (DAC -> SYNCBUSY .bit .SWRST == 1 )
159+ ; // Wait for synchronization of registers between the clock domains
160+
161+ #ifdef VREFLESS
162+ DAC -> CTRLB .reg = DAC_CTRLB_REFSEL_INTREF ; // TODO: fix this once silicon bug is fixed
163+ #else
164+ DAC -> CTRLB .reg = DAC_CTRLB_REFSEL_VREFPU ;
165+ #endif
166+
167+ //set refresh rates
168+ DAC -> DACCTRL [0 ].bit .REFRESH = 2 ;
169+ DAC -> DACCTRL [1 ].bit .REFRESH = 2 ;
162170
163171#else
164172 //set to 1/(1/(48000000/32) * 6) = 250000 SPS
165173
166- while (GCLK -> STATUS .reg & GCLK_STATUS_SYNCBUSY );
174+ while (GCLK -> STATUS .reg & GCLK_STATUS_SYNCBUSY )
175+ ;
167176
168- GCLK -> CLKCTRL .reg = GCLK_CLKCTRL_ID ( GCM_ADC ) | // Generic Clock ADC
169- GCLK_CLKCTRL_GEN_GCLK0 | // Generic Clock Generator 0 is source
170- GCLK_CLKCTRL_CLKEN ;
177+ GCLK -> CLKCTRL .reg = GCLK_CLKCTRL_ID (GCM_ADC ) | // Generic Clock ADC
178+ GCLK_CLKCTRL_GEN_GCLK0 | // Generic Clock Generator 0 is source
179+ GCLK_CLKCTRL_CLKEN ;
171180
172- while ( ADC -> STATUS .bit .SYNCBUSY == 1 ); // Wait for synchronization of registers between the clock domains
181+ while (ADC -> STATUS .bit .SYNCBUSY == 1 )
182+ ; // Wait for synchronization of registers between the clock domains
173183
174- ADC -> CTRLB .reg = ADC_CTRLB_PRESCALER_DIV32 | // Divide Clock by 32.
175- ADC_CTRLB_RESSEL_10BIT ; // 10 bits resolution as default
184+ ADC -> CTRLB .reg = ADC_CTRLB_PRESCALER_DIV32 | // Divide Clock by 32.
185+ ADC_CTRLB_RESSEL_10BIT ; // 10 bits resolution as default
176186
177- ADC -> SAMPCTRL .reg = 5 ; // Sampling Time Length
187+ ADC -> SAMPCTRL .reg = 5 ; // Sampling Time Length
178188
179- while ( ADC -> STATUS .bit .SYNCBUSY == 1 ); // Wait for synchronization of registers between the clock domains
189+ while (ADC -> STATUS .bit .SYNCBUSY == 1 )
190+ ; // Wait for synchronization of registers between the clock domains
180191
181- ADC -> INPUTCTRL .reg = ADC_INPUTCTRL_MUXNEG_GND ; // No Negative input (Internal Ground)
192+ ADC -> INPUTCTRL .reg = ADC_INPUTCTRL_MUXNEG_GND ; // No Negative input (Internal Ground)
182193
183194 // Averaging (see datasheet table in AVGCTRL register description)
184- ADC -> AVGCTRL .reg = ADC_AVGCTRL_SAMPLENUM_1 | // 1 sample only (no oversampling nor averaging)
185- ADC_AVGCTRL_ADJRES (0x0ul ); // Adjusting result by 0
195+ ADC -> AVGCTRL .reg = ADC_AVGCTRL_SAMPLENUM_1 | // 1 sample only (no oversampling nor averaging)
196+ ADC_AVGCTRL_ADJRES (0x0ul ); // Adjusting result by 0
186197
187- analogReference ( AR_DEFAULT ) ; // Analog Reference is AREF pin (3.3v)
198+ analogReference (AR_DEFAULT ) ; // Analog Reference is AREF pin (3.3v)
188199
189200 // Initialize DAC
190201 // Setting clock
191- while ( GCLK -> STATUS .reg & GCLK_STATUS_SYNCBUSY );
192- GCLK -> CLKCTRL .reg = GCLK_CLKCTRL_ID ( GCM_DAC ) | // Generic Clock ADC
193- GCLK_CLKCTRL_GEN_GCLK0 | // Generic Clock Generator 0 is source
194- GCLK_CLKCTRL_CLKEN ;
195-
196- // ATSAMR, for example, doesn't have a DAC
197- #ifdef DAC
198- while ( DAC -> STATUS .bit .SYNCBUSY == 1 ); // Wait for synchronization of registers between the clock domains
202+ while (GCLK -> STATUS .reg & GCLK_STATUS_SYNCBUSY )
203+ ;
204+ GCLK -> CLKCTRL .reg = GCLK_CLKCTRL_ID (GCM_DAC ) | // Generic Clock ADC
205+ GCLK_CLKCTRL_GEN_GCLK0 | // Generic Clock Generator 0 is source
206+ GCLK_CLKCTRL_CLKEN ;
207+
208+ // ATSAMR, for example, doesn't have a DAC
209+ #ifdef DAC
210+ while (DAC -> STATUS .bit .SYNCBUSY == 1 )
211+ ; // Wait for synchronization of registers between the clock domains
199212 DAC -> CTRLB .reg = DAC_CTRLB_REFSEL_AVCC | // Using the 3.3V reference
200- DAC_CTRLB_EOEN ; // External Output Enable (Vout)
201- #endif
213+ DAC_CTRLB_EOEN ; // External Output Enable (Vout)
214+ #endif
202215
203216#endif //SAMD51
204217
205218#ifdef WIO_TERMINAL
206- pinMode (OUTPUT_CTR_5V , OUTPUT );
207- digitalWrite (OUTPUT_CTR_5V , HIGH );
208- pinMode (OUTPUT_CTR_3V3 , OUTPUT );
209- digitalWrite (OUTPUT_CTR_3V3 , LOW );
219+ pinMode (OUTPUT_CTR_5V , OUTPUT );
220+ digitalWrite (OUTPUT_CTR_5V , HIGH );
221+ pinMode (OUTPUT_CTR_3V3 , OUTPUT );
222+ digitalWrite (OUTPUT_CTR_3V3 , LOW );
223+ pinMode (PIN_USB_HOST_ENABLE , OUTPUT );
224+ digitalWrite (PIN_USB_HOST_ENABLE , LOW );
210225#endif
211- }
226+ }
212227
213228#ifdef __cplusplus
214229}
0 commit comments