Appiko
cc112x_utils.c
1 /******************************************************************************
2  * Filename: cc112x_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 "cc112x_def.h"
41 #include "hal_spi_rf.h"
42 #include "log.h"
43 
44 #ifdef USE_CC112X
45 
46 
47 /******************************************************************************
48 * @fn set_tx_unmodulated_test_mode
49 *
50 * @brief Configure transceiver for continuous unmodulated data test mode
51 *
52 * input parameters
53 *
54 * @param void
55 *
56 * output parameters
57 *
58 * @return void
59 */
60 void set_tx_unmodulated_test_mode(void) {
61  uint8_t regs_uint8_t;
62 
63  /* disable FIFO mode*/
64  regs_uint8_t = 0x06;
65  trx8BitRegAccess(RADIO_WRITE_ACCESS , MDMCFG1, &regs_uint8_t, 1);
66 
67  /* configure continuous mode*/
68  trx8BitRegAccess(RADIO_READ_ACCESS , PA_CFG2, &regs_uint8_t, 1);
69  regs_uint8_t = regs_uint8_t | 0x40;
70  trx8BitRegAccess(RADIO_WRITE_ACCESS , PA_CFG2, &regs_uint8_t, 1);
71 
72  /* disable the modulator */
73  regs_uint8_t = 0x01;
74  trx16BitRegAccess(RADIO_WRITE_ACCESS , 0x2F, (0xFF & CFM_DATA_CFG), &regs_uint8_t, 1);
75 
76  return;
77 }
78 
79 /******************************************************************************
80 * @fn set_tx_modulated_test_mode
81 *
82 * @brief Configure transceiver for continuous modulated data test mode
83 *
84 * input parameters
85 *
86 * @param void
87 *
88 * output parameters
89 *
90 * @return void
91 */
92 void set_tx_modulated_test_mode(void) {
93  uint8_t regs_uint8_t;
94 
95  /* disable FIFO mode*/
96  regs_uint8_t = 0x46;
97  trx8BitRegAccess(RADIO_WRITE_ACCESS , MDMCFG1, &regs_uint8_t, 1);
98 
99  /* disable FIFO mode*/
100  regs_uint8_t = 0x05;
101  trx8BitRegAccess(RADIO_WRITE_ACCESS , MDMCFG0, &regs_uint8_t, 1);
102 
103  /* configure set_tx_modulated_test_mode */
104  regs_uint8_t = 0x06;
105  trx8BitRegAccess(RADIO_WRITE_ACCESS , PKT_CFG2, &regs_uint8_t, 1);
106 
107  regs_uint8_t = 0x40;
108  trx8BitRegAccess(RADIO_WRITE_ACCESS , PKT_CFG0, &regs_uint8_t, 1);
109 
110  /* load a random byte into the FIFO, this causes the modulator to start */
111  trx8BitRegAccess(RADIO_WRITE_ACCESS+RADIO_BURST_ACCESS, TXFIFO, &regs_uint8_t, 1);
112 
113  return;
114 }
115 
116 /******************************************************************************
117 * @fn radio_get_rssi
118 *
119 * @brief Get and calculate the RSSI from uint8_t value
120 *
121 * input parameters
122 *
123 * @param void
124 *
125 * output parameters
126 *
127 * @return int - rssi value in dB
128 */
129 int radio_get_rssi(void) {
130  int rssi;
131  uint8_t cc_rssi;
132 
133  trx16BitRegAccess(RADIO_READ_ACCESS , 0x2F, (0xFF & RSSI1), &cc_rssi, 1);
134 
135  rssi = cc_rssi;
136  if (rssi >= 128) {
137  rssi = rssi - 256;
138  }
139  rssi = rssi - 99;
140 
141  return rssi;
142 }
143 
144 /******************************************************************************
145 * @fn get_device_id
146 *
147 * @brief Gets the device ID by reading the version and partnum registers
148 *
149 * input parameters
150 *
151 * @param void
152 *
153 * output parameters
154 *
155 * @return char - device id define
156 */
157 char get_device_id(void) {
158  uint8_t ret_partnum;
159  uint8_t ret_version;
160  uint8_t ret;
161 
162  /* Read the PARTNUM status register */
163  trx16BitRegAccess(RADIO_READ_ACCESS , 0x2F, (0xFF & PARTNUMBER), &ret_partnum, 1);
164 
165  /* Read the PARTNUMBER status register */
166  trx16BitRegAccess(RADIO_READ_ACCESS , 0x2F, (0xFF & PARTVERSION), &ret_version, 1);
167 
168  switch (ret_partnum) {
169  case 0x40:
170  ret = DEV_CC1121;
171  break;
172  case 0x48:
173  ret = DEV_CC1120;
174  break;
175  case 0x58:
176  ret = DEV_CC1125;
177  break;
178  case 0x5A:
179  ret = DEV_CC1175;
180  break;
181  default:
182  ret = DEV_UNKNOWN;
183  break;
184  }
185 
186  return ret;
187 }
188 
189 /******************************************************************************
190 * @fn set_rf_packet_length
191 *
192 * @brief Configure the radio to handle fixed packets with a certain
193 * packet length.
194 *
195 * input parameters
196 *
197 * @param uint8_t length - length of fixed payload
198 *
199 * output parameters
200 *
201 * @return 0 - no_error
202 */
203 uint8_t set_rf_packet_length(uint8_t length) {
204  uint8_t regs_uint8_t;
205 
206  /* configure set_rf_packet_lengthe */
207  regs_uint8_t = 0x00;
208  trx8BitRegAccess(RADIO_WRITE_ACCESS , PKT_CFG0, &regs_uint8_t, 1);
209 
210  /* configure set_rf_packet_length */
211  regs_uint8_t = length;
212  trx8BitRegAccess(RADIO_WRITE_ACCESS , PKT_LEN, &regs_uint8_t, 1);
213 
214  regs_uint8_t = 0;
215  trx8BitRegAccess(RADIO_READ_ACCESS , PKT_LEN, &regs_uint8_t, 1);
216  log_printf("Pkt Len set to : %d\n", regs_uint8_t);
217 
218 
219  return (0);
220 }
221 
222 #endif