Go Down

Topic: Problem with bootloader and BOD MCUCR (Read 705 times) previous topic - next topic

scalz

Jul 16, 2015, 04:52 pm Last Edit: Jul 16, 2015, 07:21 pm by scalz
Hi,

I am new to this forum, and quite rather new to atmel world (I started few months ago before I was playing with pic)

Actually, I would like to test some sleep mode. And am trying very interesting Nick Gammon sketches.

I am facing a problem. Here the little story :
- I removed regulator and led from arduino mini pro
- I changed optiboot config v6.2 : InternalRc 8Mhz, BOD=1.8V, compiled with arduino v1.0. Fuses settings by usbasp and avrdudess. upload bootloader with Arduino ide 1.6.0 It is working ok. Upload sketch by ftdi. Ok.
- Now I would like to test sketch J from Nick Gammon, to wake up from a pin. It wakes up ok and go to sleep like it should do.

Here my problem, I noticed that after going into sleep mode, I cannot reupload a sketch by ftdi : error recv!!! If I try to reset with reset button of arduino. Same thing! So I have to reconnect usbasp and reprogram bootloader as the fuses have not changed (checked with avrdudess).

If i comment the lines about MCUCR, I have no problem. Uploading sketches works ok, but power consumption is not same (with bod:18uA, without BOD: 140nA).
So, I think these 2 lines are scraping my bootloader config, registers or RST interrupt... so I cannot reupload by ftdi...I am not sure what happens.
I will check in datasheet if I can find my answer, but in case someone knows it, could you explain me what I am doing wrong (maybe I must restore some params, int ???, when is RST int detected???).

Here example code :
Code: [Select]
#include <avr/sleep.h>

    // MOSFETS   
    // A1 : POWER RF
    // D4 : POWER SENSORS, VCC3.3

     // D3 : Supervisor Interrupt for wake up chip
    // D5 : Output -> DC Booster 3V Enable (true=enable, false=disable)
    // A0 : sense storage capacitor voltage on vcc3
    // A2 : sense VIN voltage
    #define SUPERVISOR_IRQ_PIN 3
   
    #define SUPERVISOR_IRQ SUPERVISOR_IRQ_PIN-2
   
    #define POWER_RF A1
    #define POWER_SENSORS 4   
    #define ENABLE_BOOSTER3 5
    #define CAPAVCC_ADC A0
    #define VIN_ADC A2

    volatile unsigned long wakeUpCounter=0; 
   
  //*************************************************************************************
 
    void wakefromPinInt()
  {
    // cancel sleep as a precaution
    sleep_disable();
    // must do this as the pin will probably stay low for a while
    detachInterrupt (SUPERVISOR_IRQ);
    wakeUpCounter++;
  }
 

    void setup()
    {

    // D3 : Supervisor Interrupt for wake up chip
    // A1 : POWER RF
    // D4 : POWER SENSORS, VCC3.3
    // A0 : sense storage capacitor voltage on vcc3
    // A2 : sense VIN voltage   
   
    pinMode(SUPERVISOR_IRQ_PIN, INPUT_PULLUP);   

    pinMode(POWER_RF, OUTPUT);   
    digitalWrite(POWER_RF, 1);
    pinMode(ENABLE_BOOSTER3, OUTPUT);   
    digitalWrite(ENABLE_BOOSTER3, 1);     
     
    pinMode(POWER_SENSORS, OUTPUT);   
    digitalWrite(POWER_SENSORS, 1);


      Serial.begin(115200);
      //Serial.println("Setup end");
   }

    void loop()     
    {
     
     Serial.println("Go To Sleep");
     
     UlpPowerDown();

    Serial.begin(115200);
    Serial.println("Im wake up.");
    ADCSRA = 135;
   
    delay(100);
    int vinValue = analogRead(VIN_ADC);

    // print out the value you read:
    Serial.println(vinValue);
 
    // delay, so I can test power consumption on wake up
    delay(10000) ;           

    }
   
void UlpPowerDown() {

  Serial.flush();
  Serial.end();
  delay(100);

  // disable ADC
  ADCSRA = 0; 
 
  set_sleep_mode (SLEEP_MODE_PWR_DOWN); 
  sleep_enable();

  // Do not interrupt before we go to sleep, or the
  // ISR will detach interrupts and we won't wake.
  noInterrupts ();
 
  // will be called when pin D2 or D3 goes low 
  attachInterrupt (SUPERVISOR_IRQ, wakefromPinInt, LOW);
 
 
  // turn off brown-out enable in software
  // BODS must be set to one and BODSE must be set to zero within four clock cycles
  MCUCR = bit (BODS) | bit (BODSE);
  // The BODS bit is automatically cleared after three clock cycles
  MCUCR = bit (BODS);
 
  // We are guaranteed that the sleep_cpu call will be done
  // as the processor executes the next instruction after
  // interrupts are turned on.
  interrupts ();  // one cycle
  sleep_cpu ();   // one cycle 
  }
 
 


See you soon

Go Up