2010-05-08 17:03:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
|
|
|
|
* to the MC1322x project (http://mc1322x.devl.org)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of libmc1322x: see http://mc1322x.devl.org
|
|
|
|
* for details.
|
|
|
|
*
|
2011-02-12 23:12:45 +00:00
|
|
|
*
|
2010-05-08 17:03:36 +00:00
|
|
|
*/
|
|
|
|
|
2010-02-26 21:37:20 +00:00
|
|
|
#include <mc1322x.h>
|
|
|
|
#include <board.h>
|
2009-05-11 21:01:31 +00:00
|
|
|
|
2010-02-26 21:37:20 +00:00
|
|
|
#include "tests.h"
|
|
|
|
#include "config.h"
|
2009-05-12 21:19:57 +00:00
|
|
|
|
2010-02-26 21:37:20 +00:00
|
|
|
void main(void) {
|
2009-05-11 21:01:31 +00:00
|
|
|
|
2012-10-24 01:50:32 +00:00
|
|
|
uart_init(UART1, 115200);
|
2009-05-12 21:19:57 +00:00
|
|
|
|
2010-02-26 21:37:20 +00:00
|
|
|
*mem32(0x00401ffc) = 0x01234567;
|
|
|
|
*mem32(0x00407ffc) = 0xdeadbeef;
|
|
|
|
*mem32(0x0040fffc) = 0xface00ff;
|
|
|
|
*mem32(0x00410000) = 0xabcd0123;
|
2009-05-12 21:19:57 +00:00
|
|
|
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("sleep test\n\r");
|
|
|
|
putstr("0x00401ffc: ");
|
2010-02-26 21:37:20 +00:00
|
|
|
put_hex32(*mem32(0x00401ffc));
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("\r\n");
|
|
|
|
putstr("0x00407ffc: ");
|
2010-02-26 21:37:20 +00:00
|
|
|
put_hex32(*mem32(0x00407ffc));
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("\r\n");
|
|
|
|
putstr("0x0040fffc: ");
|
2010-02-26 21:37:20 +00:00
|
|
|
put_hex32(*mem32(0x0040fffc));
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("\r\n");
|
|
|
|
putstr("0x00410000: ");
|
2010-02-26 21:37:20 +00:00
|
|
|
put_hex32(*mem32(0x00410000));
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("\r\n");
|
2009-05-12 21:19:57 +00:00
|
|
|
|
|
|
|
/* radio must be OFF before sleeping */
|
|
|
|
/* otherwise MCU will not wake up properly */
|
|
|
|
/* this is undocumented behavior */
|
2010-02-26 21:37:20 +00:00
|
|
|
// radio_off();
|
2009-05-11 19:52:30 +00:00
|
|
|
|
2009-05-21 18:52:27 +00:00
|
|
|
#if USE_32KHZ
|
|
|
|
/* turn on the 32kHz crystal */
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("enabling 32kHz crystal\n\r");
|
2009-05-21 18:52:27 +00:00
|
|
|
/* you have to hold it's hand with this on */
|
|
|
|
/* once you start the 32xHz crystal it can only be stopped with a reset (hard or soft) */
|
|
|
|
/* first, disable the ring osc */
|
2010-02-26 21:37:20 +00:00
|
|
|
clear_bit(*CRM_RINGOSC_CNTL,0);
|
2009-05-21 18:52:27 +00:00
|
|
|
/* enable the 32kHZ crystal */
|
2010-02-26 21:37:20 +00:00
|
|
|
set_bit(*CRM_XTAL32_CNTL,0);
|
2009-05-21 18:52:27 +00:00
|
|
|
|
|
|
|
/* set the XTAL32_EXISTS bit */
|
|
|
|
/* the datasheet says to do this after you've check that RTC_COUNT is changing */
|
|
|
|
/* the datasheet is not correct */
|
2010-02-26 21:37:20 +00:00
|
|
|
set_bit(*CRM_SYS_CNTL,5);
|
2009-05-21 18:52:27 +00:00
|
|
|
{
|
|
|
|
static volatile uint32_t old;
|
2010-02-26 21:37:20 +00:00
|
|
|
old = *CRM_RTC_COUNT;
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("waiting for xtal\n\r");
|
2010-02-26 21:37:20 +00:00
|
|
|
while(*CRM_RTC_COUNT == old) {
|
2009-05-21 18:52:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* RTC has started up */
|
|
|
|
|
2010-02-26 21:37:20 +00:00
|
|
|
set_bit(*CRM_SYS_CNTL,5);
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("32kHZ xtal started\n\r");
|
2009-05-21 18:52:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-05-11 19:52:30 +00:00
|
|
|
/* go to sleep */
|
2009-05-21 21:30:00 +00:00
|
|
|
|
2010-02-26 21:37:20 +00:00
|
|
|
// *CRM_WU_CNTL = 0; /* don't wake up */
|
|
|
|
*CRM_WU_CNTL = 0x1; /* enable wakeup from wakeup timer */
|
|
|
|
// *CRM_WU_TIMEOUT = 1875000; /* wake 10 sec later if doze */
|
2009-05-21 21:30:00 +00:00
|
|
|
#if USE_32KHZ
|
2010-02-26 21:37:20 +00:00
|
|
|
*CRM_WU_TIMEOUT = 327680*2;
|
2009-05-21 21:30:00 +00:00
|
|
|
#else
|
2010-02-26 21:37:20 +00:00
|
|
|
*CRM_WU_TIMEOUT = 20000; /* wake 10 sec later if hibernate ring osc */
|
2009-05-21 21:30:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* hobby board: 2kHz = 11uA; 32kHz = 11uA */
|
2010-02-26 21:37:20 +00:00
|
|
|
// *CRM_SLEEP_CNTL = 1; /* hibernate, RAM page 0 only, don't retain state, don't power GPIO */ /* approx. 2kHz = 2.0uA */
|
2009-05-21 21:30:00 +00:00
|
|
|
/* hobby board: 2kHz = 18uA; 32kHz = 19uA */
|
2010-02-26 21:37:20 +00:00
|
|
|
// *CRM_SLEEP_CNTL = 0x41; /* hibernate, RAM page 0 only, retain state, don't power GPIO */ /* approx. 2kHz = 10.0uA */
|
2009-05-21 21:30:00 +00:00
|
|
|
/* hobby board: 2kHz = 20uA; 32kHz = 21uA */
|
2010-02-26 21:37:20 +00:00
|
|
|
// *CRM_SLEEP_CNTL = 0x51; /* hibernate, RAM page 0&1 only, retain state, don't power GPIO */ /* approx. 2kHz = 11.7uA */
|
2009-05-21 21:30:00 +00:00
|
|
|
/* hobby board: 2kHz = 22uA; 32kHz = 22.5uA */
|
2010-02-26 21:37:20 +00:00
|
|
|
// *CRM_SLEEP_CNTL = 0x61; /* hibernate, RAM page 0,1,2 only, retain state, don't power GPIO */ /* approx. 2kHz = 13.9uA */
|
2009-05-21 21:30:00 +00:00
|
|
|
/* hobby board: 2kHz = 24uA; 32kHz = 25uA */
|
2010-02-26 21:37:20 +00:00
|
|
|
*CRM_SLEEP_CNTL = 0x71; /* hibernate, all RAM pages, retain state, don't power GPIO */ /* approx. 2kHz = 16.1uA */
|
|
|
|
// *CRM_SLEEP_CNTL = 0xf1; /* hibernate, all RAM pages, retain state, power GPIO */ /* consumption depends on GPIO hookup */
|
2009-05-12 21:19:57 +00:00
|
|
|
|
2010-02-26 21:37:20 +00:00
|
|
|
// *CRM_SLEEP_CNTL = 2; /* doze , RAM page 0 only, don't retain state, don't power GPIO */ /* approx. 69.2 uA */
|
|
|
|
// *CRM_SLEEP_CNTL = 0x42; /* doze , RAM page 0 only, retain state, don't power GPIO */ /* approx. 77.3uA */
|
|
|
|
// *CRM_SLEEP_CNTL = 0x52; /* doze , RAM page 0&1 only, retain state, don't power GPIO */ /* approx. 78.9uA */
|
|
|
|
// *CRM_SLEEP_CNTL = 0x62; /* doze , RAM page 0,1,2 only, retain state, don't power GPIO */ /* approx. 81.2uA */
|
|
|
|
// *CRM_SLEEP_CNTL = 0x72; /* doze , all RAM pages, retain state, don't power GPIO */ /* approx. 83.4uA - possibly with periodic refresh*/
|
|
|
|
// *CRM_SLEEP_CNTL = 0xf2; /* doze , all RAM pages, retain state, power GPIO */ /* consumption depends on GPIO hookup */
|
2009-05-12 21:19:57 +00:00
|
|
|
|
|
|
|
|
2009-05-11 21:03:20 +00:00
|
|
|
/* wait for the sleep cycle to complete */
|
2010-02-26 21:37:20 +00:00
|
|
|
while((*CRM_STATUS & 0x1) == 0) { continue; }
|
2009-05-11 21:03:20 +00:00
|
|
|
/* write 1 to sleep_sync --- this clears the bit (it's a r1wc bit) and powers down */
|
2010-02-26 21:37:20 +00:00
|
|
|
*CRM_STATUS = 1;
|
2009-05-11 19:52:30 +00:00
|
|
|
|
2009-05-11 21:01:31 +00:00
|
|
|
/* asleep */
|
|
|
|
|
2009-05-11 21:03:20 +00:00
|
|
|
/* wait for the awake cycle to complete */
|
2010-02-26 21:37:20 +00:00
|
|
|
while((*CRM_STATUS & 0x1) == 0) { continue; }
|
2009-05-11 21:03:20 +00:00
|
|
|
/* write 1 to sleep_sync --- this clears the bit (it's a r1wc bit) and finishes wakeup */
|
2010-02-26 21:37:20 +00:00
|
|
|
*CRM_STATUS = 1;
|
2009-05-11 19:52:30 +00:00
|
|
|
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("\n\r\n\r\n\r");
|
|
|
|
putstr("0x00401ffc: ");
|
2010-02-26 21:37:20 +00:00
|
|
|
put_hex32(*mem32(0x00401ffc));
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("\r\n");
|
|
|
|
putstr("0x00407ffc: ");
|
2010-02-26 21:37:20 +00:00
|
|
|
put_hex32(*mem32(0x00407ffc));
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("\r\n");
|
|
|
|
putstr("0x0040fffc: ");
|
2010-02-26 21:37:20 +00:00
|
|
|
put_hex32(*mem32(0x0040fffc));
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("\r\n");
|
|
|
|
putstr("0x00410000: ");
|
2010-02-26 21:37:20 +00:00
|
|
|
put_hex32(*mem32(0x00410000));
|
2010-03-02 15:39:47 +00:00
|
|
|
putstr("\r\n");
|
2009-05-12 21:19:57 +00:00
|
|
|
|
2010-02-26 21:37:20 +00:00
|
|
|
*GPIO_PAD_DIR0 = LED_RED;
|
|
|
|
#define DELAY 400000
|
2009-05-12 21:19:57 +00:00
|
|
|
|
2009-05-11 19:52:30 +00:00
|
|
|
volatile uint32_t i;
|
|
|
|
while(1) {
|
|
|
|
|
2010-02-26 21:37:20 +00:00
|
|
|
*GPIO_DATA0 = LED_RED;
|
2009-05-11 19:52:30 +00:00
|
|
|
|
|
|
|
for(i=0; i<DELAY; i++) { continue; }
|
|
|
|
|
2010-02-26 21:37:20 +00:00
|
|
|
*GPIO_DATA0 = 0;
|
2009-05-11 19:52:30 +00:00
|
|
|
|
|
|
|
for(i=0; i<DELAY; i++) { continue; }
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
2009-05-12 21:19:57 +00:00
|
|
|
|