Microcontroller Wireless Solutions


pal.c
Go to the documentation of this file.
00001 
00014 /*
00015  * Copyright (c) 2009, Atmel Corporation All rights reserved.
00016  *
00017  * Licensed under Atmel's Limited License Agreement --> EULA.txt
00018  */
00019 /* === Includes ============================================================ */
00020 
00021 #include <stdint.h>
00022 #include <stdbool.h>
00023 #include "pal.h"
00024 #include "pal_config.h"
00025 #include "pal_timer.h"
00026 #include "pal_internal.h"
00027 #include "app_config.h"
00028 
00029 #ifdef USB0
00030 #include "pal_usb.h"
00031 #endif /* USB0 */
00032 
00033 /* === Globals ============================================================= */
00034 
00035 /* === Prototypes ========================================================== */
00036 
00037 #ifdef WATCHDOG
00038 static inline void wdt_init(void);
00039 #endif
00040 
00041 /* === Implementation ====================================================== */
00042 
00050 retval_t pal_init(void)
00051 {
00052     /* Clear any pending watchdog reset flag */
00053     uint8_t temp = MCUSR;
00054     temp++; /* keep compiler happy */
00055     MCUSR = 0;
00056 
00057 #ifdef WATCHDOG
00058     /* Watchdog Initialization. Enables the watchdog and sets the timeout period */
00059     wdt_init();
00060 #else
00061     WDT_RESET();
00062     WDT_DISABLE();
00063 #endif
00064 
00065     mcu_init();     // see pal_mcu_generic.c
00066     timer_init();   // see pal_timer.c
00067     gpio_init();    // see pal_board.c
00068 
00069     return MAC_SUCCESS;
00070 }
00071 
00072 
00073 
00079 void pal_task(void)
00080 {
00081 #if (TOTAL_NUMBER_OF_TIMERS > 0)
00082     timer_service();
00083 #endif
00084 
00085 #ifdef USB0
00086     /*
00087      * If the serial communication is done using USB, only then the 'usb_handler'
00088      * needs to be called. For UART, the data handling is done in the UART ISR.
00089      */
00090     usb_handler();
00091 #endif /* USB0 */
00092 
00093 #ifdef WATCHDOG
00094     WDT_RESET();
00095 #endif
00096 }
00097 
00098 
00099 
00110 retval_t pal_ps_get(ps_type_t ps_type, uint16_t start_addr, uint16_t length, void *value)
00111 {
00112 #if (EXTERN_EEPROM_AVAILABLE == 1)
00113     if (ps_type == EXTERN_EEPROM)
00114     {
00115         return extern_eeprom_get(start_addr, length, value);
00116     }
00117     else
00118 #endif
00119 
00120     if (ps_type == INTERN_EEPROM)
00121     {
00122         uint16_t index;
00123         uint8_t *data_ptr;
00124 
00125         if ((start_addr + length) > (E2END + 1))
00126         {
00127             return FAILURE;
00128         }
00129 
00130         data_ptr = (uint8_t *)(value);
00131         for (index = 0; index < length; index++)
00132         {
00133             EEGET(*data_ptr, start_addr);
00134             start_addr++;
00135             data_ptr++;
00136         }
00137     }
00138     else    // no internal eeprom and no external eeprom available
00139     {
00140         return MAC_INVALID_PARAMETER;
00141     }
00142 
00143     return MAC_SUCCESS;
00144 }
00145 
00146 
00147 
00157 retval_t pal_ps_set(uint16_t start_addr, uint16_t length, void *value)
00158 {
00159     uint8_t *data_ptr;
00160     uint16_t i;
00161     uint8_t read_data;
00162 
00163     if ((start_addr + length) > (E2END + 1))
00164     {
00165         return FAILURE;
00166     }
00167 
00168     data_ptr = (uint8_t *)(value);
00169     for (i = 0; i < length; i++)
00170     {
00171         EEGET(read_data, start_addr);
00172         if (read_data != *data_ptr)
00173         {
00174             EEPUT(start_addr, *data_ptr);
00175         }
00176         data_ptr++;
00177         start_addr++;
00178     }
00179 
00180     return MAC_SUCCESS;
00181 }
00182 
00183 
00184 
00185 /*
00186  * @brief Alert indication
00187  *
00188  * This Function can be used by any application to indicate an error condition.
00189  * The function is blocking and does never return.
00190  */
00191 void pal_alert(void)
00192 {
00193 #if (DEBUG > 0)
00194     bool debug_flag = false;
00195 #endif
00196     ALERT_INIT();
00197 
00198     while (1)
00199     {
00200         pal_timer_delay(0xFFFF);
00201         ALERT_INDICATE();
00202 
00203 #if (DEBUG > 0)
00204         /* Used for debugging purposes only */
00205         if (debug_flag == true)
00206         {
00207             break;
00208         }
00209 #endif
00210     }
00211 }
00212 
00213 
00217 #if defined(WATCHDOG) || defined(DOXYGEN)
00218 static inline void wdt_init(void)
00219 {
00220     /* Initialize the Watchdog with the mode and the time-out period */
00221 #ifdef __ICCAVR__
00222     WDT_SYSTEM_RESET_MODE(WDT_TIMEOUT_PERIOD);
00223 #endif
00224 #ifdef __GNUC__
00225     wdt_enable(WDT_TIMEOUT_PERIOD);
00226 #endif
00227 }
00228 #endif
00229 
00230 
00231 /* EOF */