Lange Level richtig laden.
[hackover2013-badge-firmware.git] / tools / examples / chibi / receive / main.c
1 /**************************************************************************/
2 /*!
3 @file main.c
4 @author K. Townsend (microBuilder.eu)
5
6 @section LICENSE
7
8 Software License Agreement (BSD License)
9
10 Copyright (c) 2010, microBuilder SARL
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 3. Neither the name of the copyright holders nor the
21 names of its contributors may be used to endorse or promote products
22 derived from this software without specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 /**************************************************************************/
36 #include "projectconfig.h"
37 #include "sysinit.h"
38
39 #include "core/gpio/gpio.h"
40 #include "drivers/rf/chibi/chb.h"
41 #include "drivers/rf/chibi/chb_drvr.h"
42
43 static chb_rx_data_t rx_data;
44
45 /**************************************************************************/
46 /*!
47 Converts the ED (Energy Detection) value to dBm using the following
48 formula: dBm = RSSI_BASE_VAL + 1.03 * ED
49
50 For more information see section 6.5 of the AT86RF212 datasheet
51 */
52 /**************************************************************************/
53 int edToDBM(uint32_t ed)
54 {
55 #if CFG_CHIBI_MODE == 0 || CFG_CHIBI_MODE == 1 || CFG_CHIBI_MODE == 2
56 // Calculate for OQPSK (RSSI Base Value = -100)
57 int dbm = (103 * ed - 10000);
58 #else
59 // Calculate for BPSK (RSSI Base Value = -98)
60 int dbm = (103 * ed - 9800);
61 #endif
62
63 return dbm / 100;
64 }
65
66 /**************************************************************************/
67 /*!
68 Constantly checks for incoming messages, and displays them using
69 printf when they arrive. This program will display messages sent
70 to the global broadcast address (0xFFFF) or messages addressed to
71 this node using it's unique 16-bit ID.
72
73 projectconfig.h settings:
74 --------------------------------------------------
75 CFG_CHIBI -> Enabled
76 CFG_CHIBI_PROMISCUOUS -> 0
77 CFG_CHIBI_BUFFERSIZE -> 128
78 */
79 /**************************************************************************/
80 int main(void)
81 {
82 // Configure cpu and mandatory peripherals
83 systemInit();
84
85 // Make sure that projectconfig.h is properly configured for this example
86 #if !defined CFG_CHIBI
87 #error "CFG_CHIBI must be enabled in projectconfig.h for this example"
88 #endif
89 #if CFG_CHIBI_PROMISCUOUS != 0
90 #error "CFG_CHIBI_PROMISCUOUS must be set to 0 in projectconfig.h for this example"
91 #endif
92
93 // Get a reference to the Chibi peripheral control block
94 chb_pcb_t *pcb = chb_get_pcb();
95
96 while(1)
97 {
98 // Check for incoming messages
99 while (pcb->data_rcv)
100 {
101 // Enable LED to indicate message reception
102 gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_ON);
103 // get the length of the data
104 rx_data.len = chb_read(&rx_data);
105 // make sure the length is nonzero
106 if (rx_data.len)
107 {
108 int dbm = edToDBM(pcb->ed);
109 printf("Message received from node %02X: %s, len=%d, dBm=%d.%s", rx_data.src_addr, rx_data.data, rx_data.len, dbm, CFG_PRINTF_NEWLINE);
110 }
111 // Disable LED
112 gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF);
113 }
114 }
115
116 return 0;
117 }
This page took 0.067501 seconds and 5 git commands to generate.