Contiki-Inga 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 const struct radio_driver adf7023_driver = {
49 
50  .init = adf7023_init,
51 
52  /** Prepare the radio with a packet to be sent. */
53  .prepare = adf7023_prepare,
54 
55  /** Send the packet that has previously been prepared. */
56  .transmit = adf7023_transmit,
57 
58  /** Prepare & transmit a packet. */
59  .send = adf7023_send,
60 
61  /** Read a received packet into a buffer. */
62  .read = adf7023_read,
63 
64  /** Perform a Clear-Channel Assessment (CCA) to find out if there is
65  a packet in the air or not. */
66  .channel_clear = adf7023_channel_clear,
67 
68  /** Check if the radio driver is currently receiving a packet */
69  .receiving_packet = adf7023_receiving_packet,
70 
71  /** Check if the radio driver has just received a packet */
72  .pending_packet = adf7023_pending_packet,
73 
74  /** Turn the radio on. */
75  .on = adf7023_on,
76 
77  /** Turn the radio off. */
78  .off = adf7023_off,
79 };
80 
81 int
82 adf7023_init(void)
83 {
84  ADF7023_Init();
85  return 1;
86 }
87 int
88 adf7023_prepare(const void *payload, unsigned short payload_len)
89 {
90  /* Prepare the radio with a packet to be sent. */
91  memcpy(tx_buf, payload, (payload_len <= sizeof(tx_buf)) ? payload_len : sizeof(tx_buf));
92  return 0;
93 }
94 int
95 adf7023_transmit(unsigned short transmit_len)
96 {
97  /* Send the packet that has previously been prepared. */
98 
99  RADIO_TX_LED = 1;
100  ADF7023_TransmitPacket(tx_buf, transmit_len);
101  RADIO_TX_LED = 0;
102 
103  /* TODO: Error conditions (RADIO_TX_ERR, RADIO_TX_COLLISION, RADIO_TX_NOACK)? */
104  return RADIO_TX_OK;
105 }
106 int
107 adf7023_send(const void *payload, unsigned short payload_len)
108 {
109  /* Prepare & transmit a packet. */
110 
111  RADIO_TX_LED = 1;
112  ADF7023_TransmitPacket((void *)payload, payload_len);
113  RADIO_TX_LED = 0;
114 
115  /* TODO: Error conditions (RADIO_TX_ERR, RADIO_TX_COLLISION, RADIO_TX_NOACK)? */
116  return RADIO_TX_OK;
117 }
118 int
119 adf7023_read(void *buf, unsigned short buf_len)
120 {
121  unsigned char num_bytes;
122  /* Read a received packet into a buffer. */
123 
124  RADIO_RX_LED = 1;
125  ADF7023_ReceivePacket(rx_buf, &num_bytes);
126  RADIO_RX_LED = 0;
127 
128  memcpy(buf, rx_buf, (num_bytes <= buf_len) ? num_bytes : buf_len);
129  return num_bytes;
130 }
131 int
132 adf7023_channel_clear(void)
133 {
134  /* Perform a Clear-Channel Assessment (CCA) to find out if there is a packet in the air or not. */
135  return 1;
136 }
137 int
138 adf7023_receiving_packet(void)
139 {
140  /* Check if the radio driver is currently receiving a packet. */
141  return 0;
142 }
143 int
144 adf7023_pending_packet(void)
145 {
146  /* Check if the radio driver has just received a packet. */
147  return ADF7023_ReceivePacketAvailable();
148 }
149 int
150 adf7023_on(void)
151 {
152  /* Turn the radio on. */
153  return 1;
154 }
155 int
156 adf7023_off(void)
157 {
158  /* Turn the radio off. */
159  return 0;
160 }