Contiki-NG
data-queue.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
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  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /**
31  * \addtogroup cc13xx-cc26xx-rf-data-queue
32  * @{
33  *
34  * \file
35  * Implementation of the CC13xx/CC26xx RF data queue.
36  * \author
37  * Edvard Pettersen <e.pettersen@ti.com>
38  */
39 /*---------------------------------------------------------------------------*/
40 #include "contiki.h"
41 #include "sys/cc.h"
42 /*---------------------------------------------------------------------------*/
43 #include <ti/devices/DeviceFamily.h>
44 #include DeviceFamily_constructPath(driverlib/rf_mailbox.h)
45 #include DeviceFamily_constructPath(driverlib/rf_data_entry.h)
46 /*---------------------------------------------------------------------------*/
47 #include "rf/data-queue.h"
48 /*---------------------------------------------------------------------------*/
49 #include <stddef.h>
50 #include <stdint.h>
51 #include <string.h>
52 /*---------------------------------------------------------------------------*/
53 /* RX buf configuration */
54 #define RX_BUF_CNT RF_CONF_RX_BUF_CNT
55 #define RX_BUF_SIZE RF_CONF_RX_BUF_SIZE
56 /*---------------------------------------------------------------------------*/
57 /* Receive buffer entries with room for 1 IEEE 802.15.4 frame in each */
58 typedef union {
59  data_entry_t data_entry;
60  uint8_t buf[RX_BUF_SIZE];
61 } rx_buf_t CC_ALIGN (4);
62 /*---------------------------------------------------------------------------*/
63 typedef struct {
64  /* RX bufs */
65  rx_buf_t bufs[RX_BUF_CNT];
66  /* RFC data queue object */
67  data_queue_t data_queue;
68  /* Current data entry in use by RF */
69  data_entry_t *curr_entry;
70  /* Size in bytes of length field in data entry */
71  size_t lensz;
72 } rx_data_queue_t;
73 
74 static rx_data_queue_t rx_data_queue;
75 /*---------------------------------------------------------------------------*/
76 static void
77 rx_bufs_init(void)
78 {
79  data_entry_t *data_entry;
80  size_t i;
81 
82  for(i = 0; i < RX_BUF_CNT; ++i) {
83  data_entry = &(rx_data_queue.bufs[i].data_entry);
84 
85  data_entry->status = DATA_ENTRY_PENDING;
86  data_entry->config.type = DATA_ENTRY_TYPE_GEN;
87  data_entry->config.lenSz = rx_data_queue.lensz;
88  data_entry->length = RX_BUF_SIZE - sizeof(data_entry_t);
89  /* Point to fist entry if this is last entry, else point to next entry */
90  data_entry->pNextEntry = ((i + 1) == RX_BUF_CNT)
91  ? rx_data_queue.bufs[0].buf
92  : rx_data_queue.bufs[i + 1].buf;
93  }
94 }
95 /*---------------------------------------------------------------------------*/
96 static void
97 rx_bufs_reset(void)
98 {
99  size_t i;
100  for(i = 0; i < RX_BUF_CNT; ++i) {
101  data_entry_t *const data_entry = &(rx_data_queue.bufs[i].data_entry);
102 
103  /* Clear length bytes */
104  memset(&(data_entry->data), 0x0, rx_data_queue.lensz);
105  /* Set status to Pending */
106  data_entry->status = DATA_ENTRY_PENDING;
107  }
108 }
109 /*---------------------------------------------------------------------------*/
110 data_queue_t *
111 data_queue_init(size_t lensz)
112 {
113  rx_data_queue.lensz = lensz;
114 
115  /* Initialize RX buffers */
116  rx_bufs_init();
117 
118  /* Configure data queue as circular buffer */
119  rx_data_queue.data_queue.pCurrEntry = rx_data_queue.bufs[0].buf;
120  rx_data_queue.data_queue.pLastEntry = NULL;
121 
122  /* Set current read pointer to first element */
123  rx_data_queue.curr_entry = &(rx_data_queue.bufs[0].data_entry);
124 
125  return &rx_data_queue.data_queue;
126 }
127 /*---------------------------------------------------------------------------*/
128 void
129 data_queue_reset(void)
130 {
131  rx_bufs_reset();
132 
133  /* Only need to reconfigure pCurrEntry */
134  rx_data_queue.data_queue.pCurrEntry = rx_data_queue.bufs[0].buf;
135 
136  /* Set current read pointer to first element */
137  rx_data_queue.curr_entry = &(rx_data_queue.bufs[0].data_entry);
138 }
139 /*---------------------------------------------------------------------------*/
140 data_entry_t *
141 data_queue_current_entry(void)
142 {
143  return rx_data_queue.curr_entry;
144 }
145 /*---------------------------------------------------------------------------*/
146 void
147 data_queue_release_entry(void)
148 {
149  data_entry_t *const curr_entry = rx_data_queue.curr_entry;
150  uint8_t *const frame_ptr = (uint8_t *)&(curr_entry->data);
151 
152  /* Clear length bytes */
153  memset(frame_ptr, 0x0, rx_data_queue.lensz);
154  /* Set status to Pending */
155  curr_entry->status = DATA_ENTRY_PENDING;
156 
157  /* Move current entry to the next entry */
158  rx_data_queue.curr_entry = (data_entry_t *)(curr_entry->pNextEntry);
159 }
160 /*---------------------------------------------------------------------------*/
161 /** @} */
Header file of the CC13xx/CC26xx RF data queue.
Default definitions of C compiler quirk work-arounds.