Microcontroller Wireless Solutions


pal_mcu_generic.c
Go to the documentation of this file.
00001 
00013 /*
00014  * Copyright (c) 2009, Atmel Corporation All rights reserved.
00015  *
00016  * Licensed under Atmel's Limited License Agreement --> EULA.txt
00017  */
00018 
00019 /* === Includes ============================================================ */
00020 
00021 #include "pal.h"
00022 
00023 #if (PAL_TYPE == ATMEGA128RFA1)
00024 
00025 /* === Macros =============================================================== */
00026 
00027 
00028 /* === Globals ============================================================= */
00029 
00030 
00031 /* === Prototypes =========================================================== */
00032 
00033 static void mcu_clock_init(void);
00034 
00035 /* === Implementation ======================================================= */
00036 
00040 void mcu_init(void)
00041 {
00042     mcu_clock_init();
00043 
00044     /* Enable SRAM Data Retention */
00045     DRTRAM0 = _BV(ENDRT);
00046     DRTRAM1 = _BV(ENDRT);
00047     DRTRAM2 = _BV(ENDRT);
00048     DRTRAM3 = _BV(ENDRT);
00049 }
00050 
00051 
00052 
00059 void mcu_clock_init(void)
00060 {
00061     uint8_t low_fuse, prescaler;
00062 
00063     /*
00064      * Determine which oscillator the CPU is configured for, and
00065      * adjust the clock prescaler appropriately.  For the internal RC
00066      * oscillator, the special prescaler value 0x0F must be used to
00067      * adjust the RC oscillator for full 16 MHz operation.  In all
00068      * other cases, value 0x0 is used to use a prescaler of 1.  This
00069      * makes the operation independent of the CKDIV8 fuse setting.
00070      */
00071     ENTER_CRITICAL_REGION();
00072     low_fuse = read_avr_fuse(0);
00073     LEAVE_CRITICAL_REGION();
00074 
00075     if ((low_fuse & 0x0F) == 0x02)
00076     {
00077         /* Internal RC oscillator used. */
00078 #if (F_CPU == 16000000)
00079         prescaler = 0x0F;
00080 #endif
00081 #if (F_CPU == 8000000)
00082         prescaler = clock_div_1;
00083 #endif
00084 #if (F_CPU == 4000000)
00085         prescaler = clock_div_2;
00086 #endif
00087     }
00088     else
00089     {
00090         /* Any other clock source, usually the 16 MHz crystal oscillator. */
00091         prescaler = clock_div_1;
00092     }
00093 
00094     clock_prescale_set(prescaler);
00095 }
00096 
00097 #endif /* (PAL_TYPE == ATMEGA128RFA1) */
00098 
00099 /* EOF */