Appiko
cc1101_utils.c
1 /******************************************************************************
2  * Filename: cc1101_utils.c
3  *
4  * Description: Implementation file for entering various test modes and
5  * support functions needed for the demostration software
6  *
7  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
8  *
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 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  *
21  * Neither the name of Texas Instruments Incorporated nor the names of
22  * its contributors may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  *******************************************************************************/
38 #include "stdio.h"
39 #include "stdlib.h"
40 #include "msp430.h"
41 #include "cc1101_def.h"
42 #include "hal_spi_rf.h"
43 
44 #ifdef USE_CC1101
45 
46 /******************************************************************************
47 * @fn set_tx_unmodulated_test_mode
48 *
49 * @brief Configure transceiver for continuous unmodulated data test mode
50 *
51 * input parameters
52 *
53 * @param void
54 *
55 * output parameters
56 *
57 * @return void
58 */
59 void set_tx_unmodulated_test_mode(void) {
60  unsigned char reg_access;
61 
62  reg_access = 0x32;
63  trx8BitRegAccess(RADIO_WRITE_ACCESS, PKTCTRL0, &reg_access, 1);
64 
65  reg_access = 0x33;
66  trx8BitRegAccess(RADIO_WRITE_ACCESS, MDMCFG2, &reg_access, 1);
67 
68 }
69 
70 /******************************************************************************
71 * @fn set_tx_modulated_test_mode
72 *
73 * @brief Configure transceiver for continuous modulated data test mode
74 *
75 * input parameters
76 *
77 * @param void
78 *
79 * output parameters
80 *
81 * @return void
82 */
83 void set_tx_modulated_test_mode(void) {
84  unsigned char reg_access;
85 
86  reg_access = 0x26;
87  trx8BitRegAccess(RADIO_WRITE_ACCESS, PKTCTRL0, &reg_access, 1);
88 
89 }
90 
91 /******************************************************************************
92 * @fn radio_get_rssi
93 *
94 * @brief Get and calculate the RSSI from unsigned char value
95 *
96 * input parameters
97 *
98 * @param void
99 *
100 * output parameters
101 *
102 * @return int - rssi value in dB
103 */
104 int radio_get_rssi(void) {
105  int rssi;
106  unsigned char cc_rssi;
107 
108  trx8BitRegAccess(RADIO_READ_ACCESS | RADIO_BURST_ACCESS, RSSI, &cc_rssi, 1);
109 
110  if (cc_rssi >= 128) {
111  rssi = ((cc_rssi-256)>>1) - 72;
112  } else {
113  rssi = (cc_rssi>>1) - 72;
114  }
115  return rssi;
116 }
117 
118 /******************************************************************************
119 * @fn get_device_id
120 *
121 * @brief Gets the device ID by reading the version and partnum registers
122 *
123 * input parameters
124 *
125 * @param void
126 *
127 * output parameters
128 *
129 * @return char - device id define
130 */
131 char get_device_id(void) {
132  unsigned char ret_partnum;
133  unsigned char ret_version;
134 
135  trx8BitRegAccess(RADIO_READ_ACCESS+RADIO_BURST_ACCESS, VERSION, &ret_version, 1);
136  trx8BitRegAccess(RADIO_READ_ACCESS+RADIO_BURST_ACCESS, PARTNUM, &ret_partnum, 1);
137 
138  switch (ret_partnum) {
139  case 0:
140  if(ret_version == 0x04) {
141  return DEV_CC1101;
142  }
143  if(ret_version == 0x07) {
144  return DEV_CC1101;
145  }
146  if(ret_version == 0x06) {
147  return DEV_CC430x;
148  }
149  if(ret_version == 0x00) {
150  return DEV_CC1100;
151  }
152  break;
153  case 128:
154  if(ret_version == 0x03) {
155  return DEV_CC2500;
156  }
157  break;
158  default:
159  break;
160  }
161 
162  return DEV_UNKNOWN;
163 }
164 
165 /******************************************************************************
166 * @fn set_rf_packet_length
167 *
168 * @brief Configure the radio to handle fixed packets with a certain
169 * packet length.
170 *
171 * input parameters
172 *
173 * @param unsigned char length - length of fixed payload
174 *
175 * output parameters
176 *
177 * @return 0 - no_error
178 */
179 unsigned char set_rf_packet_length(unsigned char length) {
180  unsigned char reg_value;
181 
182  /* make sure we are in fixed packet mode */
183  reg_value = 0x04;
184  trx8BitRegAccess(RADIO_WRITE_ACCESS, PKTCTRL0, &reg_value, 1);
185 
186  /* set the fixed packet length */
187  trx8BitRegAccess(RADIO_WRITE_ACCESS, PKTLEN, &length, 1);
188 
189  return (0);
190 }
191 
192 #endif