Contiki-NG
efr32-radio-buffer.c
1/*
2 * Copyright (c) 2018, RISE SICS
3 * Copyright (C) 2022 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
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#include "contiki.h"
33
34#include "efr32-radio-buffer.h"
35/* Allocate 4 rx buffers for bursty receives */
36#define RX_BUF_COUNT 4
37
38rx_buffer_t rx_buf[RX_BUF_COUNT];
39volatile int next_read = 0;
40volatile int next_write = 0;
41
42int
43has_packet(void)
44{
45 return rx_buf[next_read].len > 0;
46}
47rx_buffer_t *
48get_full_rx_buf(void)
49{
50 int nr;
51 if(rx_buf[nr = next_read].len > 0) {
52 /* return buffert and intrease last_read as it was full. */
53 /* Write will have to check if it can allocate new buffers when
54 increating the write pos */
55 next_read = (next_read + 1) % RX_BUF_COUNT;
56 return &rx_buf[nr];
57 }
58 /* nothing to read */
59 return NULL;
60}
61rx_buffer_t *
62get_empty_rx_buf(void)
63{
64 int nw;
65 /* if the next write buf is empty is should be ok to write to it */
66 if(rx_buf[nw = next_write].len == 0) {
67 next_write = (next_write + 1) % RX_BUF_COUNT;
68 return &rx_buf[nw];
69 }
70 /* Full - nothing to write... */
71 return NULL;
72}
73void
74free_rx_buf(rx_buffer_t *rx_buf)
75{
76 /* set len to zero */
77 rx_buf->len = 0;
78}
GPIO HAL header file for the gecko.