Contiki 3.x
adf7023-contiki.c
1 /*
2  * Copyright (c) 2014, Analog Devices, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /**
32  * \author Ian Martin <martini@redwirellc.com>
33  */
34 
35 #include <string.h> /* for memcpy(). */
36 
37 #include "radio.h"
38 
39 #include "ADF7023.h"
40 #include "adf7023-contiki.h"
41 #include "contiki.h" /* for LED definitions. */
42 
43 #define ADF7023_MAX_PACKET_SIZE 255
44 
45 static unsigned char tx_buf[ADF7023_MAX_PACKET_SIZE];
46 static unsigned char rx_buf[ADF7023_MAX_PACKET_SIZE];
47 
48 /*---------------------------------------------------------------------------*/
49 static radio_result_t
50 get_value(radio_param_t param, radio_value_t *value)
51 {
52  return RADIO_RESULT_NOT_SUPPORTED;
53 }
54 /*---------------------------------------------------------------------------*/
55 static radio_result_t
56 set_value(radio_param_t param, radio_value_t value)
57 {
58  return RADIO_RESULT_NOT_SUPPORTED;
59 }
60 /*---------------------------------------------------------------------------*/
61 static radio_result_t
62 get_object(radio_param_t param, void *dest, size_t size)
63 {
64  return RADIO_RESULT_NOT_SUPPORTED;
65 }
66 /*---------------------------------------------------------------------------*/
67 static radio_result_t
68 set_object(radio_param_t param, const void *src, size_t size)
69 {
70  return RADIO_RESULT_NOT_SUPPORTED;
71 }
72 /*---------------------------------------------------------------------------*/
73 const struct radio_driver adf7023_driver = {
74 
75  .init = adf7023_init,
76 
77  /** Prepare the radio with a packet to be sent. */
78  .prepare = adf7023_prepare,
79 
80  /** Send the packet that has previously been prepared. */
81  .transmit = adf7023_transmit,
82 
83  /** Prepare & transmit a packet. */
84  .send = adf7023_send,
85 
86  /** Read a received packet into a buffer. */
87  .read = adf7023_read,
88 
89  /** Perform a Clear-Channel Assessment (CCA) to find out if there is
90  a packet in the air or not. */
91  .channel_clear = adf7023_channel_clear,
92 
93  /** Check if the radio driver is currently receiving a packet */
94  .receiving_packet = adf7023_receiving_packet,
95 
96  /** Check if the radio driver has just received a packet */
97  .pending_packet = adf7023_pending_packet,
98 
99  /** Turn the radio on. */
100  .on = adf7023_on,
101 
102  /** Turn the radio off. */
103  .off = adf7023_off,
104 
105  .get_value = get_value,
106  .set_value = set_value,
107  .get_object = get_object,
108  .set_object = set_object
109 };
110 
111 int
112 adf7023_init(void)
113 {
114  ADF7023_Init();
115  return 1;
116 }
117 int
118 adf7023_prepare(const void *payload, unsigned short payload_len)
119 {
120  /* Prepare the radio with a packet to be sent. */
121  memcpy(tx_buf, payload, (payload_len <= sizeof(tx_buf)) ? payload_len : sizeof(tx_buf));
122  return 0;
123 }
124 int
125 adf7023_transmit(unsigned short transmit_len)
126 {
127  /* Send the packet that has previously been prepared. */
128 
129  RADIO_TX_LED = 1;
130  ADF7023_TransmitPacket(tx_buf, transmit_len);
131  RADIO_TX_LED = 0;
132 
133  /* TODO: Error conditions (RADIO_TX_ERR, RADIO_TX_COLLISION, RADIO_TX_NOACK)? */
134  return RADIO_TX_OK;
135 }
136 int
137 adf7023_send(const void *payload, unsigned short payload_len)
138 {
139  /* Prepare & transmit a packet. */
140 
141  RADIO_TX_LED = 1;
142  ADF7023_TransmitPacket((void *)payload, payload_len);
143  RADIO_TX_LED = 0;
144 
145  /* TODO: Error conditions (RADIO_TX_ERR, RADIO_TX_COLLISION, RADIO_TX_NOACK)? */
146  return RADIO_TX_OK;
147 }
148 int
149 adf7023_read(void *buf, unsigned short buf_len)
150 {
151  unsigned char num_bytes;
152  /* Read a received packet into a buffer. */
153 
154  RADIO_RX_LED = 1;
155  ADF7023_ReceivePacket(rx_buf, &num_bytes);
156  RADIO_RX_LED = 0;
157 
158  memcpy(buf, rx_buf, (num_bytes <= buf_len) ? num_bytes : buf_len);
159  return num_bytes;
160 }
161 int
162 adf7023_channel_clear(void)
163 {
164  /* Perform a Clear-Channel Assessment (CCA) to find out if there is a packet in the air or not. */
165  return 1;
166 }
167 int
168 adf7023_receiving_packet(void)
169 {
170  /* Check if the radio driver is currently receiving a packet. */
171  return 0;
172 }
173 int
174 adf7023_pending_packet(void)
175 {
176  /* Check if the radio driver has just received a packet. */
177  return ADF7023_ReceivePacketAvailable();
178 }
179 int
180 adf7023_on(void)
181 {
182  /* Turn the radio on. */
183  return 1;
184 }
185 int
186 adf7023_off(void)
187 {
188  /* Turn the radio off. */
189  return 0;
190 }
The structure of a device driver for a radio in Contiki.
Definition: radio.h:225
int radio_value_t
Each radio has a set of parameters that designate the current configuration and state of the radio...
Definition: radio.h:88
radio_result_t(* get_value)(radio_param_t param, radio_value_t *value)
Get a radio parameter value.
Definition: radio.h:258
radio_result_t(* get_object)(radio_param_t param, void *dest, size_t size)
Get a radio parameter object.
Definition: radio.h:268
radio_result_t(* set_object)(radio_param_t param, const void *src, size_t size)
Set a radio parameter object.
Definition: radio.h:274
radio_result_t(* set_value)(radio_param_t param, radio_value_t value)
Set a radio parameter value.
Definition: radio.h:261