Lowest consumption issue

Here is the updated file with comments & installation path added in the header. I hope this will help to get the low consumption figure on mkrfox1200.

Please take also into account the great benefits of a setup boost 3.3V circuit, which makes batttery life about 10 times longer. My reference is the NCP1402, and its implementation by Sparkfun.

/*
Copyright (c) 2015 Arduino LLC. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Philgeek on www.philoc.fr
// Edited on 2018/03/17 thanks to question / my post on Arduino forum mkrfox1200/lowest consumption issue
// I just hope it can help but I cannot commit :wink:

// This file has been updated as I looked for the lowest consumption on mkrfox1200
// The modification below is tagged with "phg" embedded in the comment
// Only one line had to be placed as comment ...
// Notes :
// The path on my configuration is .arduino15/packages/arduino/hardware/samd/1..6.16/cores/arduino/wiring.c
// Notes / Prerequisites
// All unused components, and especially sigfox and usb must have been reset by your application before entering deep sleep
// And Arduino Low Power library must be in its 1.2.0 version, at least in my configuration
// Lastly check any pin configuration, be careful with pullups and leds ... but you did that already
// And at he end of the day, you should get around 50 mA, as I've got by myself ... because the data sheet figures & the mkrfox1200 schematics are done that way.
*/--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
#include "Arduino.h"

#ifdef __cplusplus
extern "C" {
#endif

/*

  • System Core Clock is at 1MHz (8MHz/8) at Reset.
  • It is switched to 48MHz in the Reset Handler (startup.c)
    */
    uint32_t SystemCoreClock=1000000ul ;

/*
void calibrateADC()
{
volatile uint32_t valeur = 0;

for(int i = 0; i < 5; ++i)
{
ADC->SWTRIG.bit.START = 1;
while( ADC->INTFLAG.bit.RESRDY == 0 || ADC->STATUS.bit.SYNCBUSY == 1 )
{
// Waiting for a complete conversion and complete synchronization
}

valeur += ADC->RESULT.bit.RESULT;
}

valeur = valeur/5;
}*/

/*

  • Arduino Zero board initialization
  • Good to know:
    • At reset, ResetHandler did the system clock configuration. Core is running at 48MHz.
    • Watchdog is disabled by default, unless someone plays with NVM User page
    • During reset, all PORT lines are configured as inputs with input buffers, output buffers and pull disabled.
      /
      void init( void )
      {
      // Set Systick to 1ms interval, common to all Cortex-M variants
      if ( SysTick_Config( SystemCoreClock / 1000 ) )
      {
      // Capture error
      while ( 1 ) ;
      }
      NVIC_SetPriority (SysTick_IRQn, (1 << __NVIC_PRIO_BITS) - 2); /
      set Priority for Systick Interrupt (2nd lowest) */

// Clock PORT for Digital I/O
// PM->APBBMASK.reg |= PM_APBBMASK_PORT ;
//
// // Clock EIC for I/O interrupts
// PM->APBAMASK.reg |= PM_APBAMASK_EIC ;

// Clock SERCOM for Serial
PM->APBCMASK.reg |= PM_APBCMASK_SERCOM0 | PM_APBCMASK_SERCOM1 | PM_APBCMASK_SERCOM2 | PM_APBCMASK_SERCOM3 | PM_APBCMASK_SERCOM4 | PM_APBCMASK_SERCOM5 ;

// Clock TC/TCC for Pulse and Analog
PM->APBCMASK.reg |= PM_APBCMASK_TCC0 | PM_APBCMASK_TCC1 | PM_APBCMASK_TCC2 | PM_APBCMASK_TC3 | PM_APBCMASK_TC4 | PM_APBCMASK_TC5 ;

// Clock ADC/DAC for Analog
PM->APBCMASK.reg |= PM_APBCMASK_ADC | PM_APBCMASK_DAC ;

// Setup all pins (digital and analog) in INPUT mode (default is nothing)
for (uint32_t ul = 0 ; ul < NUM_DIGITAL_PINS ; ul++ )
{
// modification - PHG - 2017-09-13 : line below suppressed in order to get lower consumption in standby /deep sleep mode '<50uA)
// pinMode( ul, INPUT ) ;
}

// Initialize Analog Controller
// Setting clock
while(GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);

GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID( GCM_ADC ) | // Generic Clock ADC
GCLK_CLKCTRL_GEN_GCLK0 | // Generic Clock Generator 0 is source
GCLK_CLKCTRL_CLKEN ;

while( ADC->STATUS.bit.SYNCBUSY == 1 ); // Wait for synchronization of registers between the clock domains

ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV512 | // Divide Clock by 512.
ADC_CTRLB_RESSEL_10BIT; // 10 bits resolution as default

ADC->SAMPCTRL.reg = 0x3f; // Set max Sampling Time Length

while( ADC->STATUS.bit.SYNCBUSY == 1 ); // Wait for synchronization of registers between the clock domains

ADC->INPUTCTRL.reg = ADC_INPUTCTRL_MUXNEG_GND; // No Negative input (Internal Ground)

// Averaging (see datasheet table in AVGCTRL register description)
ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_1 | // 1 sample only (no oversampling nor averaging)
ADC_AVGCTRL_ADJRES(0x0ul); // Adjusting result by 0

analogReference( AR_DEFAULT ) ; // Analog Reference is AREF pin (3.3v)

// Initialize DAC
// Setting clock
while ( GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY );
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID( GCM_DAC ) | // Generic Clock ADC
GCLK_CLKCTRL_GEN_GCLK0 | // Generic Clock Generator 0 is source
GCLK_CLKCTRL_CLKEN ;

while ( DAC->STATUS.bit.SYNCBUSY == 1 ); // Wait for synchronization of registers between the clock domains
DAC->CTRLB.reg = DAC_CTRLB_REFSEL_AVCC | // Using the 3.3V reference
DAC_CTRLB_EOEN ; // External Output Enable (Vout)
}

#ifdef __cplusplus
}
#endif