Microcontroller Wireless Solutions


pal.h
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 
00020 /* Prevent double inclusion */
00021 #ifndef PAL_H
00022 #define PAL_H
00023 
00024 /*
00025  * NOTE:- Include 'return_val.h' before this file, as return_val.h has the
00026  *        all return value enums.
00027  */
00028 
00029 /* === Includes ============================================================ */
00030 
00031 #include <stdbool.h>
00032 #include <stdint.h>
00033 #include "pal_types.h"
00034 #include "pal_config.h"
00035 #if (PAL_GENERIC_TYPE == MEGA_RF)
00036 #include "pal_mcu_generic.h"
00037 #endif
00038 #include "app_config.h"
00039 
00040 /* === Macros =============================================================== */
00041 
00045 #define ADD_TIME(a, b)                  ((a) + (b))
00046 
00050 #define SUB_TIME(a, b)                  ((a) - (b))
00051 
00055 #if (PAL_GENERIC_TYPE == AVR32)
00056 #define U16_TO_TARGET(x) ((((x) << 8) & 0xFF00) | (((x) >> 8) & 0x00FF))
00057 #else
00058 #define U16_TO_TARGET(x) (x)
00059 #endif
00060 
00061 /* === Types =============================================================== */
00062 
00066 /* Please dont change this type. */
00067 typedef enum pin_state_tag
00068 {
00070     LOW,
00072     HIGH
00073 } SHORTENUM pin_state_t;
00074 
00075 
00076 #if defined (SIO_HUB) || defined(DOXYGEN)
00077 
00080 typedef enum sio_serial_tag
00081 {
00082     /* Serial unit used by user */
00084     SIO_0,
00086     SIO_1,
00088     SIO_2,
00090     SIO_3,
00092     SIO_4
00093 } SHORTENUM sio_serial_t;
00094 #endif
00095 
00096 
00100 typedef enum
00101 timeout_type_tag
00102 {
00104     TIMEOUT_RELATIVE,
00106     TIMEOUT_ABSOLUTE
00107 } SHORTENUM timeout_type_t;
00108 
00109 
00113 typedef enum source_type_tag
00114 {
00116     TMR_CLK_SRC_DURING_TRX_SLEEP,
00118     TMR_CLK_SRC_DURING_TRX_AWAKE
00119 } SHORTENUM source_type_t;
00120 
00124 typedef enum ps_id_tag
00125 {
00126     PS_IEEE_ADDR,
00127     PS_XTAL_TRIM
00128 } SHORTENUM ps_id_t;
00129 
00130 
00134 typedef enum ps_type_tag
00135 {
00137     INTERN_EEPROM,
00139     EXTERN_EEPROM
00140 } SHORTENUM ps_type_t;
00141 
00142 
00146 typedef enum led_action_tag
00147 {
00149     LED_ON,
00151     LED_OFF,
00153     LED_TOGGLE
00154 } SHORTENUM led_action_t;
00155 
00156 
00160 typedef enum button_state_tag
00161 {
00163     BUTTON_PRESSED,
00165     BUTTON_OFF
00166 } SHORTENUM button_state_t;
00167 
00168 
00172 typedef enum pwr_mode_tag
00173 {
00174     SYSTEM_SLEEP
00175 } pwr_mode_t;
00176 
00177 
00181 typedef void (*irq_handler_t)(void);
00182 
00183 
00184 #ifdef TEST_HARNESS
00185 #if (DEBUG > 0)
00186 
00189 typedef struct assert_tag
00190 {
00192     uint8_t size            ALIGN8BIT;
00194     uint8_t assert_cmdcode  ALIGN8BIT;
00196     uint8_t data[1]         ALIGN8BIT;
00197 } assert_t;
00198 #endif  /* (DEBUG > 0) */
00199 #endif  /* TEST_HARNESS */
00200 
00201 /* === Externals ============================================================ */
00202 
00203 
00204 /* === Prototypes =========================================================== */
00205 
00206 #ifdef __cplusplus
00207 extern "C" {
00208 #endif
00209 
00218     retval_t pal_init(void);
00219 
00220 
00226     void pal_task(void);
00227 
00228 #if (PAL_GENERIC_TYPE != MEGA_RF)
00229 
00238     void pal_trx_irq_init(FUNC_PTR trx_irq_cb);
00239 #endif /* (PAL_GENERIC_TYPE != MEGA_RF) */
00240 
00250     void pal_trx_irq_init_tstamp(FUNC_PTR trx_irq_cb);
00251 
00252 #if (PAL_GENERIC_TYPE == MEGA_RF)
00253 
00261     void pal_trx_irq_init_rx_end(FUNC_PTR trx_irq_cb);
00262 
00271     void pal_trx_irq_init_tx_end(FUNC_PTR trx_irq_cb);
00272 
00281     void pal_trx_irq_init_cca_ed(FUNC_PTR trx_irq_cb);
00282 
00291     void pal_trx_irq_init_awake(FUNC_PTR trx_irq_cb);
00292 
00293 #ifdef ENABLE_ALL_TRX_IRQS
00294 
00302     void pal_trx_irq_init_ami(FUNC_PTR trx_irq_cb);
00303 
00312     void pal_trx_irq_init_batmon(FUNC_PTR trx_irq_cb);
00313 
00322     void pal_trx_irq_init_pll_lock(FUNC_PTR trx_irq_cb);
00323 
00332     void pal_trx_irq_init_pll_unlock(FUNC_PTR trx_irq_cb);
00333 
00342     void pal_trx_irq_init_aes_ready(FUNC_PTR trx_irq_cb);
00343 
00344 #if PAL_TYPE == ATMEGA256RFR1 || PAL_TYPE == ATMEGA256RFR2
00345 
00353     void pal_trx_irq_init_tx_start(FUNC_PTR trx_irq_cb);
00354 
00363     void pal_trx_irq_init_maf_0_ami(FUNC_PTR trx_irq_cb);
00364 
00373     void pal_trx_irq_init_maf_1_ami(FUNC_PTR trx_irq_cb);
00374 
00383     void pal_trx_irq_init_maf_2_ami(FUNC_PTR trx_irq_cb);
00384 
00393     void pal_trx_irq_init_maf_3_ami(FUNC_PTR trx_irq_cb);
00394 #endif  /* ATMEGA256RFR1 */
00395 
00396 #endif  /* ENABLE_ALL_TRX_IRQS */
00397 
00398 #ifdef PAL_XTD_IRQ_API
00399 
00404     FUNC_PTR pal_trx_irq_get_hdlr_rx_end(void);
00405 
00411     FUNC_PTR pal_trx_irq_get_hdlr_tx_end(void);
00412 #endif  /* PAL_XTD_IRQ_API */
00413 #endif  /* #if (PAL_GENERIC_TYPE == MEGA_RF) */
00414 
00415 #if (PAL_GENERIC_TYPE != MEGA_RF)
00416 
00427 #define pal_trx_irq_en()                ENABLE_TRX_IRQ()
00428 
00440 #define pal_trx_irq_en_tstamp()         ENABLE_TRX_IRQ_TSTAMP()
00441 
00453 #define pal_trx_irq_dis()               DISABLE_TRX_IRQ()
00454 
00466 #define pal_trx_irq_dis_tstamp()        DISABLE_TRX_IRQ_TSTAMP()
00467 #endif /* #if (PAL_GENERIC_TYPE != MEGA_RF) */
00468 
00474 #define pal_trx_irq_flag_clr()          CLEAR_TRX_IRQ()
00475 
00481 #define pal_trx_irq_flag_clr_tstamp()   CLEAR_TRX_IRQ_TSTAMP()
00482 
00483 #if (PAL_GENERIC_TYPE == MEGA_RF)
00484 
00489 #define pal_trx_irq_flag_clr_rx_end()   (CLEAR_TRX_IRQ_RX_END())
00490 
00496 #define pal_trx_irq_flag_clr_tx_end()   (CLEAR_TRX_IRQ_TX_END())
00497 
00503 #define pal_trx_irq_flag_clr_cca_ed()   (CLEAR_TRX_IRQ_CCA_ED())
00504 
00510 #define pal_trx_irq_flag_clr_awake()    (CLEAR_TRX_IRQ_AWAKE())
00511 
00512 #ifdef ENABLE_ALL_TRX_IRQS
00513 
00518 #define pal_trx_irq_flag_clr_ami()      (CLEAR_TRX_IRQ_AMI())
00519 
00525 #define pal_trx_irq_flag_clr_batmon()   (CLEAR_TRX_IRQ_BATMON())
00526 
00532 #define pal_trx_irq_flag_clr_pll_lock() (CLEAR_TRX_IRQ_PLL_LOCK())
00533 
00539 #define pal_trx_irq_flag_clr_pll_unlock()   (CLEAR_TRX_IRQ_PLL_UNLOCK())
00540 
00541 #if PAL_TYPE == ATMEGA256RFR1
00542 
00548 #define pal_trx_irq_flag_clr_tx_start()     (CLEAR_TRX_IRQ_TX_START())
00549 
00555 #define pal_trx_irq_flag_clr_maf_0_ami()    (CLEAR_TRX_IRQ_MAF_0_AMI())
00556 
00562 #define pal_trx_irq_flag_clr_maf_1_ami()    (CLEAR_TRX_IRQ_MAF_1_AMI())
00563 
00569 #define pal_trx_irq_flag_clr_maf_2_ami()    (CLEAR_TRX_IRQ_MAF_2_AMI())
00570 
00576 #define pal_trx_irq_flag_clr_maf_3_ami()    (CLEAR_TRX_IRQ_MAF_3_AMI())
00577 
00578 
00579 #endif  /* ATMEGA256RFR1 */
00580 
00581 #endif  /* ENABLE_ALL_TRX_IRQS */
00582 #endif  /* #if (PAL_GENERIC_TYPE == MEGA_RF) */
00583 
00584 
00589     static inline void pal_global_irq_enable(void)
00590     {
00591         ENABLE_GLOBAL_IRQ();
00592     }
00593 
00594 
00599     static inline void pal_global_irq_disable(void)
00600     {
00601         DISABLE_GLOBAL_IRQ();
00602     }
00603 
00604 
00605 
00606 #if defined(ENABLE_HIGH_PRIO_TMR) || defined(DOXYGEN)
00607     /*
00608      * Internal prototypes for pal_timer module
00609      */
00627     retval_t pal_start_high_priority_timer(uint8_t timer_id,
00628                                            uint16_t timer_count,
00629                                            FUNC_PTR timer_cb,
00630                                            void *param_cb);
00631 
00645     retval_t pal_stop_high_priority_timer(uint8_t timer_id);
00646 #endif
00647 
00656     void pal_timer_delay(uint16_t delay);
00657 
00668     void pal_timer_source_select(source_type_t source);
00669 
00670 #if (TOTAL_NUMBER_OF_TIMERS > 0) || defined(DOXYGEN)
00671 
00683     bool pal_is_timer_running(uint8_t timer_id);
00684 #endif
00685 
00687 #if (DEBUG > 0)
00688     bool pal_are_all_timers_stopped(void);
00689 #endif  /* (DEBUG > 0) */
00690 
00702     static inline uint32_t pal_add_time_us(uint32_t a, uint32_t b)
00703     {
00704         return (ADD_TIME(a, b));
00705     }
00706 
00707 
00717     static inline uint32_t pal_sub_time_us(uint32_t a, uint32_t b)
00718     {
00719         return (SUB_TIME(a, b));
00720     }
00721 
00722 
00723     /*
00724      * Macros for TRX GPIO pins access.
00725      */
00727 #define PAL_RST_HIGH()                  RST_HIGH()
00728 
00729 #define PAL_RST_LOW()                   RST_LOW()
00730 
00731 #define PAL_SLP_TR_HIGH()               SLP_TR_HIGH()
00732 
00733 #define PAL_SLP_TR_LOW()                SLP_TR_LOW()
00734 
00735 #define PAL_TRX_IRQ_HIGH()              IRQ_PINGET()
00736 
00746     void pal_trx_read_timestamp(uint32_t *timestamp);
00747 
00748 #ifdef SNIFFER
00749 
00757     void pal_trx_write_timestamp(uint32_t *timestamp);
00758 #endif  /* SNIFFER */
00759 
00760     /*
00761      * Prototypes for transceiver access.  Some PALs define these as
00762      * macros instead of implementing them as functions, so only declare
00763      * them here if they are not implemented as macros.
00764      */
00765 #if defined(PAL_USE_SPI_TRX) || defined(DOXYGEN)
00766 
00776     void pal_trx_frame_read(uint8_t *data, uint8_t length);
00777 
00787     void pal_trx_frame_write(uint8_t *data, uint8_t length);
00788 
00800     uint8_t pal_trx_reg_read(uint8_t addr);
00801 
00812     void pal_trx_reg_write(uint8_t addr, uint8_t data);
00813 
00824     uint8_t pal_trx_bit_read(uint8_t addr, uint8_t mask, uint8_t pos);
00825 
00835     void pal_trx_bit_write(uint8_t reg_addr, uint8_t mask, uint8_t pos, uint8_t new_value);
00836 
00837 #if defined(ENABLE_TRX_SRAM) || defined(ENABLE_TRX_SRAM_READ) || defined(DOXYGEN)
00838 
00848     void pal_trx_sram_read(uint8_t addr, uint8_t *data, uint8_t length);
00849 #endif
00850 
00851 #if defined(ENABLE_TRX_SRAM) || defined(DOXYGEN)
00852 
00862     void pal_trx_sram_write(uint8_t addr, uint8_t *data, uint8_t length);
00863 #endif
00864 
00865 #if defined(ENABLE_TRX_SRAM) || defined(DOXYGEN)
00866 
00877     void pal_trx_aes_wrrd(uint8_t addr, uint8_t *idata, uint8_t length);
00878 #endif  /* #ifdef ENABLE_TRX_SRAM */
00879 
00880 #endif  /* defined(PAL_USE_SPI_TRX) */
00881 
00882 
00883 #if defined(SIO_HUB) || defined(DOXYGEN)
00884     /*
00885      * Prototypes for Stream I/O interface
00886      */
00897     retval_t pal_sio_init(uint8_t sio_unit);
00898 
00911     uint8_t pal_sio_tx(uint8_t sio_unit, uint8_t *data, uint8_t length);
00912 
00925     uint8_t pal_sio_rx(uint8_t sio_unit, uint8_t *data, uint8_t max_length);
00926 #endif  /* SIO_HUB */
00927 
00928 
00935     bool pal_calibrate_rc_osc(void);
00936 
00937 
00938     /*
00939      * Prototypes for persistence storage
00940      */
00952     retval_t pal_ps_get(ps_type_t ps_type, uint16_t start_addr, uint16_t length, void *value);
00953 
00964     retval_t pal_ps_set(uint16_t start_addr, uint16_t length, void *value);
00965 
00966 
00974     void pal_alert(void);
00975 
00976 
00978 #if (DEBUG > 0)
00979     void pal_assert(bool expression,
00980                     FLASH_STRING_T message,
00981                     int8_t *file,
00982                     uint16_t line);
00983 
00984     /* DEBUG only: Test for an assertion, similar to Std-C assert() */
00985 #define ASSERT(expr)    pal_assert(expr, FLASH_STRING( #expr ), (int8_t*)__FILE__, __LINE__)
00986 #else
00987 #define ASSERT(expr)
00988 #endif /* (DEBUG > 0) */
00989 
00992     /*
00993      * Public prototypes for pal_timer module
00994      */
00995 
00996 #if (TOTAL_NUMBER_OF_TIMERS > 0) || defined(DOXYGEN)
00997 
01018     retval_t pal_timer_start(uint8_t timer_id,
01019                              uint32_t timer_count,
01020                              timeout_type_t timeout_type,
01021                              FUNC_PTR timer_cb,
01022                              void *param_cb);
01023 #endif
01024 
01025 #if (TOTAL_NUMBER_OF_TIMERS > 0) || defined(DOXYGEN)
01026 
01039     retval_t pal_timer_stop(uint8_t timer_id);
01040 #endif
01041 
01050     void pal_get_current_time(uint32_t *current_time);
01051 
01052 
01056     void pal_led_init(void);
01057 
01065     void pal_led(led_id_t led_no, led_action_t led_setting);
01066 
01067 
01071     void pal_button_init(void);
01072 
01073 
01084     button_state_t pal_button_read(button_id_t button_no);
01085 
01086 
01096     void pal_sleep_mode(uint8_t sleep_mode);
01097 
01098 
01099 #if defined(NON_BLOCKING_SPI) || defined(DOXYGEN)
01100 
01106     void pal_spi_done_cb_init(void *spi_done_cb);
01107 #endif
01108 
01109 #if defined(WATCHDOG) || defined(SLEEPING_TIMER) || defined(DOXYGEN)
01110 #if (PAL_GENERIC_TYPE == XMEGA)
01111 
01115     void wdt_init(void);
01116 
01121     void wdt_parallel_timer_init(void);
01122 
01127     void sleeping_timer_without_wdt(void);
01128 
01137     void sleeping_timer_with_wdt(void);
01138 
01142     void sleeping_timer_init(void);
01143 
01148     void wdt_clear(void);
01149 
01163     void pal_wdt_enable( WDT_PER_t period );
01164 
01179     void RTC_Initialize( uint16_t period,
01180                          uint16_t count,
01181                          uint16_t compareValue,
01182                          RTC_PRESCALER_t prescaler );
01183 
01190     void RTC_SetIntLevels( RTC_OVFINTLVL_t ovfIntLevel,
01191                            RTC_COMPINTLVL_t compIntLevel );
01192 #endif /* (PAL_GENERIC_TYPE == XMEGA) */
01193 #endif /* (defined(WATCHDOG) || defined(SLEEPING_TIMER) || defined(DOXYGEN)) */
01194 
01202     uint16_t pal_generate_rand_seed(void);
01203 
01204 #ifdef __cplusplus
01205 } /* extern "C" */
01206 #endif
01207 
01208 #endif  /* PAL_H */
01209 /* EOF */