Contiki 3.x
hal.h
Go to the documentation of this file.
1 /* Copyright (c) 2008, Swedish Institute of Computer Science
2  * All rights reserved.
3  *
4  * Additional fixes for AVR contributed by:
5  *
6  * Colin O'Flynn coflynn@newae.com
7  * Eric Gnoske egnoske@gmail.com
8  * Blake Leverett bleverett@gmail.com
9  * Mike Vidales mavida404@gmail.com
10  * Kevin Brown kbrown3@uccs.edu
11  * Nate Bohlmann nate@elfwerks.com
12  * David Kopf dak664@embarqmail.com
13  *
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions are met:
18  *
19  * * Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  * * Redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in
23  * the documentation and/or other materials provided with the
24  * distribution.
25  * * Neither the name of the copyright holders nor the names of
26  * contributors may be used to endorse or promote products derived
27  * from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 /**
43  * \addtogroup hal
44  * @{
45  */
46 
47 /**
48  * \file
49  * \brief This file contains low-level radio driver code.
50  *
51  * $Id: hal.h,v 1.5 2010/12/03 20:42:01 dak664 Exp $
52  */
53 
54 #ifndef HAL_AVR_H
55 #define HAL_AVR_H
56 
57 /*============================ INCLUDE =======================================*/
58 
59 #include <stdint.h>
60 #include <stdbool.h>
61 #include "K60.h"
62 /* #include <util/crc16.h> */
63 #include "contiki-conf.h"
64 #include "interrupt.h"
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
70 /*============================ MACROS ========================================*/
71 
72 /**
73  * \name Pin macros
74  * \brief These macros convert the platform-specific pin defines into names and functions
75  * that the source code can directly use.
76  * \{
77  */
78 #define SLPTR_GPIO PTE
79 #define SLPTR_PIN 6
80 
81 #define hal_set_slptr_high() (BITBAND_REG(SLPTR_GPIO->PSOR, SLPTR_PIN) = 1) /**< This macro pulls the SLP_TR pin high. */
82 #define hal_set_slptr_low() (BITBAND_REG(SLPTR_GPIO->PCOR, SLPTR_PIN) = 1) /**< This macro pulls the SLP_TR pin low. */
83 #define hal_get_slptr() (BITBAND_REG(SLPTR_GPIO->PDOR, SLPTR_PIN)) /**< Read current state of the SLP_TR pin (High/Low). */
84 /* rst and pwr is the same */
85 #define hal_set_rst_high() PTD->PSOR = (1 << 7); delay_us(0xFFFF) /**< This macro pulls the RST pin high. */
86 #define hal_set_rst_low() (PTD->PCOR = (1 << 7)) /**< This macro pulls the RST pin low. */
87 #define hal_get_rst() ((PTD->PDOR & (1 << 7)) >> 7) /**< Read current state of the RST pin (High/Low). */
88 #define hal_set_pwr_high() (PTD->PSOR = (1 << 7)) /**< This macro pulls the RST pin high. */
89 #define hal_set_pwr_low() (PTD->PCOR = (1 << 7)) /**< This macro pulls the RST pin low. */
90 #define hal_get_pwr() ((PTD->PDOR & (1 << 7)) >> 7) /**< Read current state of the RST pin (High/Low). */
91 #define HAL_SS_PIN 1 /**< The slave select pin. */
92 
93 /** \} */
94 
95 #define HAL_SS_HIGH() /* Done in HW on K60 */
96 #define HAL_SS_LOW() /* Done in HW on K60 */
97 
98 #define HAL_ENABLE_RADIO_INTERRUPT() { PORTB->PCR[9] |= (1 << 24); \
99  NVIC_ClearPendingIRQ(PORTB_IRQn); \
100  NVIC_EnableIRQ(PORTB_IRQn); }
101 #define HAL_DISABLE_RADIO_INTERRUPT() (NVIC_DisableIRQ(PORTB_IRQn))
102 
103 #define HAL_ENABLE_OVERFLOW_INTERRUPT() ()
104 #define HAL_DISABLE_OVERFLOW_INTERRUPT() ()
105 
106 #define HAL_ENTER_CRITICAL_REGION() MK60_ENTER_CRITICAL_REGION()
107 #define HAL_LEAVE_CRITICAL_REGION() MK60_LEAVE_CRITICAL_REGION()
108 
109 /** \brief Enable the interrupt from the radio transceiver.
110  */
111 #define hal_enable_trx_interrupt() HAL_ENABLE_RADIO_INTERRUPT()
112 
113 /** \brief Disable the interrupt from the radio transceiver.
114  *
115  * \retval 0 if the pin is low, 1 if the pin is high.
116  */
117 #define hal_disable_trx_interrupt() HAL_DISABLE_RADIO_INTERRUPT()
118 
119 /*============================ TYPDEFS =======================================*/
120 
121 /*============================ PROTOTYPES ====================================*/
122 
123 /*============================ MACROS ========================================*/
124 
125 /** \name Macros for radio operation.
126  * \{
127  */
128 #define HAL_BAT_LOW_MASK (0x80) /**< Mask for the BAT_LOW interrupt. */
129 #define HAL_TRX_UR_MASK (0x40) /**< Mask for the TRX_UR interrupt. */
130 #define HAL_TRX_END_MASK (0x08) /**< Mask for the TRX_END interrupt. */
131 #define HAL_RX_START_MASK (0x04) /**< Mask for the RX_START interrupt. */
132 #define HAL_PLL_UNLOCK_MASK (0x02) /**< Mask for the PLL_UNLOCK interrupt. */
133 #define HAL_PLL_LOCK_MASK (0x01) /**< Mask for the PLL_LOCK interrupt. */
134 
135 #define HAL_MIN_FRAME_LENGTH (0x03) /**< A frame should be at least 3 bytes. */
136 #define HAL_MAX_FRAME_LENGTH (0x7F) /**< A frame should no more than 127 bytes. */
137 /** \} */
138 
139 /*============================ TYPDEFS =======================================*/
140 /** \struct hal_rx_frame_t
141  * \brief This struct defines the rx data container.
142  *
143  * \see hal_frame_read
144  */
145 typedef struct {
146  uint8_t length; /**< Length of frame. */
147  uint8_t data[HAL_MAX_FRAME_LENGTH]; /**< Actual frame data. */
148  uint8_t lqi; /**< LQI value for received frame. */
149  uint8_t rssi;
150  bool crc; /**< Flag - did CRC pass for received frame? */
152 
153 /** RX_START event handler callback type. Is called with timestamp in IEEE 802.15.4 symbols and frame length. See hal_set_rx_start_event_handler(). */
154 typedef void (*hal_rx_start_isr_event_handler_t)(uint32_t const isr_timestamp, uint8_t const frame_length);
155 
156 /** RRX_END event handler callback type. Is called with timestamp in IEEE 802.15.4 symbols and frame length. See hal_set_trx_end_event_handler(). */
157 typedef void (*hal_trx_end_isr_event_handler_t)(uint32_t const isr_timestamp);
158 
159 typedef void (*rx_callback_t) (uint16_t data);
160 
161 /*============================ PROTOTYPES ====================================*/
162 void hal_init(void);
163 
164 void hal_reset_flags(void);
165 uint8_t hal_get_bat_low_flag(void);
166 void hal_clear_bat_low_flag(void);
167 
171 
175 
176 uint8_t hal_get_pll_lock_flag(void);
177 void hal_clear_pll_lock_flag(void);
178 
179 uint8_t hal_register_read(uint8_t address);
180 void hal_register_write(uint8_t address, uint8_t value);
181 uint8_t hal_subregister_read(uint8_t address, uint8_t mask, uint8_t position);
182 void hal_subregister_write(uint8_t address, uint8_t mask, uint8_t position, uint8_t value);
183 
184 /* For speed RF230BB does not use a callback */
185 void hal_frame_read(hal_rx_frame_t *rx_frame);
186 void hal_frame_write(uint8_t *write_buffer, uint8_t length);
187 void hal_sram_read(uint8_t address, uint8_t length, uint8_t *data);
188 void hal_sram_write(uint8_t address, uint8_t length, uint8_t *data);
189 
190 /* Number of receive buffers in RAM. */
191 #ifndef RF230_CONF_RX_BUFFERS
192 #define RF230_CONF_RX_BUFFERS 1
193 #endif
194 
195 #ifdef __cplusplus
196 } /* extern "C" */
197 #endif
198 
199 #endif
200 
201 /** @} */
202 /*EOF*/
void hal_reset_flags(void)
This function reset the interrupt flags and interrupt event handlers (Callbacks) to their default val...
Definition: hal.c:163
uint8_t hal_get_bat_low_flag(void)
This function returns the current value of the BAT_LOW flag.
Definition: hal.c:186
uint8_t hal_register_read(uint8_t address)
This function reads data from one of the radio transceiver&#39;s registers.
Definition: hal.c:304
void hal_clear_pll_lock_flag(void)
This function clears the PLL_LOCK flag.
Definition: hal.c:286
void hal_clear_bat_low_flag(void)
This function clears the BAT_LOW flag.
Definition: hal.c:195
hal_trx_end_isr_event_handler_t hal_get_trx_end_event_handler(void)
This function is used to set new TRX_END event handler, overriding old handler reference.
Definition: hal.c:207
void hal_clear_rx_start_event_handler(void)
Remove event handler reference.
Definition: hal.c:262
uint8_t hal_get_pll_lock_flag(void)
This function returns the current value of the PLL_LOCK flag.
Definition: hal.c:277
uint8_t hal_subregister_read(uint8_t address, uint8_t mask, uint8_t position)
This function reads the value of a specific subregister.
Definition: hal.c:377
void hal_init(void)
This function initializes the Hardware Abstraction Layer.
Definition: hal.c:133
This struct defines the rx data container.
Definition: hal.h:351
void hal_subregister_write(uint8_t address, uint8_t mask, uint8_t position, uint8_t value)
This function writes a new value to one of the radio transceiver&#39;s subregisters.
Definition: hal.c:400
void hal_sram_read(uint8_t address, uint8_t length, uint8_t *data)
Read SRAM.
Definition: hal.c:558
void hal_frame_read(hal_rx_frame_t *rx_frame, rx_callback_t rx_callback)
This function will upload a frame from the radio transceiver&#39;s frame buffer.
Definition: hal.c:429
void(* hal_trx_end_isr_event_handler_t)(uint32_t const isr_timestamp)
RRX_END event handler callback type.
Definition: hal.h:362
K60 hardware register header wrapper.
void hal_sram_write(uint8_t address, uint8_t length, uint8_t *data)
Write SRAM.
Definition: hal.c:597
void hal_frame_write(uint8_t *write_buffer, uint8_t length)
This function will download a frame to the radio transceiver&#39;s frame buffer.
Definition: hal.c:516
void hal_set_rx_start_event_handler(hal_rx_start_isr_event_handler_t rx_start_callback_handle)
This function is used to set new RX_START event handler, overriding old handler reference.
Definition: hal.c:251
hal_rx_start_isr_event_handler_t hal_get_rx_start_event_handler(void)
This function returns the active RX_START event handler.
Definition: hal.c:241
#define HAL_MAX_FRAME_LENGTH
A frame should no more than 127 bytes.
Definition: hal.h:136
void hal_clear_trx_end_event_handler(void)
Remove event handler reference.
Definition: hal.c:228
void hal_register_write(uint8_t address, uint8_t value)
This function writes a new value to one of the radio transceiver&#39;s registers.
Definition: hal.c:342
K60 interrupt save/restore macros.
void hal_set_trx_end_event_handler(hal_trx_end_isr_event_handler_t trx_end_callback_handle)
This function is used to set new TRX_END event handler, overriding old handler reference.
Definition: hal.c:217
void(* hal_rx_start_isr_event_handler_t)(uint32_t const isr_timestamp, uint8_t const frame_length)
RX_START event handler callback type.
Definition: hal.h:359