47 #include "contiki-conf.h"
49 #define CONTIKI_MACA_DEBUG 0
50 #if CONTIKI_MACA_DEBUG
51 #define PRINTF(...) printf(__VA_ARGS__)
56 #ifndef CONTIKI_MACA_PREPEND_BYTE
57 #define CONTIKI_MACA_PREPEND_BYTE 0xff
64 static volatile uint8_t tx_complete;
65 static volatile uint8_t tx_status;
69 int contiki_maca_init(
void);
70 int contiki_maca_on_request(
void);
71 int contiki_maca_off_request(
void);
72 int contiki_maca_read(
void *buf,
unsigned short bufsize);
73 int contiki_maca_prepare(
const void *payload,
unsigned short payload_len);
74 int contiki_maca_transmit(
unsigned short transmit_len);
75 int contiki_maca_send(
const void *payload,
unsigned short payload_len);
76 int contiki_maca_channel_clear(
void);
77 int contiki_maca_receiving_packet(
void);
78 int contiki_maca_pending_packet(
void);
82 .init = contiki_maca_init,
83 .prepare = contiki_maca_prepare,
84 .transmit = contiki_maca_transmit,
85 .send = contiki_maca_send,
86 .read = contiki_maca_read,
87 .receiving_packet = contiki_maca_receiving_packet,
88 .pending_packet = contiki_maca_pending_packet,
89 .channel_clear = contiki_maca_channel_clear,
90 .on = contiki_maca_on_request,
91 .off = contiki_maca_off_request,
94 static volatile uint8_t contiki_maca_request_on = 0;
95 static volatile uint8_t contiki_maca_request_off = 0;
97 static process_event_t event_data_ready;
99 static volatile packet_t prepped_p;
101 int contiki_maca_init(
void) {
111 int contiki_maca_channel_clear(
void) {
116 int contiki_maca_receiving_packet(
void) {
120 int contiki_maca_pending_packet(
void) {
121 if (rx_head !=
NULL) {
128 int contiki_maca_on_request(
void) {
129 contiki_maca_request_on = 1;
130 contiki_maca_request_off = 0;
134 int contiki_maca_off_request(
void) {
135 contiki_maca_request_on = 0;
136 contiki_maca_request_off = 1;
143 int contiki_maca_read(
void *buf,
unsigned short bufsize) {
145 volatile packet_t *p;
147 if((p = rx_packet())) {
149 #if CONTIKI_MACA_RAW_MODE
156 PRINTF(
": p->length 0x%0x bufsize 0x%0x \n\r", p->length, bufsize);
157 if((p->length) < bufsize) bufsize = (p->length);
158 memcpy(buf, (uint8_t *)(p->data + p->offset), bufsize);
159 packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY,p->lqi);
160 packetbuf_set_attr(PACKETBUF_ATTR_TIMESTAMP,p->rx_time);
161 #if CONTIKI_MACA_DEBUG
162 for( i = p->offset ; i < (bufsize + p->offset) ; i++) {
163 PRINTF(
" %02x",p->data[i]);
178 int contiki_maca_prepare(
const void *payload,
unsigned short payload_len) {
181 PRINTF(
"contiki maca prepare");
182 #if CONTIKI_MACA_RAW_MODE
183 prepped_p.offset = 1;
184 prepped_p.length = payload_len + 1;
186 prepped_p.offset = 0;
187 prepped_p.length = payload_len;
189 if(payload_len > MAX_PACKET_SIZE)
return RADIO_TX_ERR;
190 memcpy((uint8_t *)(prepped_p.data + prepped_p.offset), payload, payload_len);
191 #if CONTIKI_MACA_RAW_MODE
192 prepped_p.offset = 0;
193 prepped_p.data[0] = CONTIKI_MACA_PREPEND_BYTE;
196 #if CONTIKI_MACA_DEBUG
197 PRINTF(
": sending %d bytes\n\r", payload_len);
198 for(i = prepped_p.offset ; i < (prepped_p.length + prepped_p.offset); i++) {
199 PRINTF(
" %02x",prepped_p.data[i]);
211 int contiki_maca_transmit(
unsigned short transmit_len) {
212 volatile packet_t *p;
214 PRINTF(
"contiki maca transmit\n\r");
218 if(p = get_free_packet()) {
219 p->offset = prepped_p.offset;
220 p->length = prepped_p.length;
221 memcpy((uint8_t *)(p->data + p->offset),
222 (
const uint8_t *)(prepped_p.data + prepped_p.offset),
226 PRINTF(
"couldn't get free packet for transmit\n\r");
232 while(!tx_complete && (tx_head != 0));
236 int contiki_maca_send(
const void *payload,
unsigned short payload_len) {
237 contiki_maca_prepare(payload, payload_len);
238 contiki_maca_transmit(payload_len);
246 return RADIO_TX_NOACK;
253 PROCESS(contiki_maca_process,
"maca process");
265 if(contiki_maca_request_on == 1) {
266 contiki_maca_request_on = 0;
270 if(contiki_maca_request_off == 1) {
271 contiki_maca_request_off = 0;
275 if (rx_head !=
NULL) {
280 NETSTACK_RDC.input();
284 if (rx_head !=
NULL) {
293 void maca_rx_callback(
volatile packet_t *p __attribute((unused))) {
299 void maca_tx_callback(
volatile packet_t *p __attribute((unused))) {
301 tx_status = p->status;