3 #include "cc112x_spi.h"
6 #include "../firmware.h"
8 #define CC_XO_FREQ 38400000
16 bool cc1125_pollGPIO(uint8_t gpio_pin)
32 uint8_t radio_set_rxbw_param(uint8_t radio_id, uint32_t *rxbw){
41 d_fact[0] = (uint8_t)((uint32_t)CC_XO_FREQ/(*rxbw)/8/20);
42 d_fact[1] = (uint8_t)(d_fact[0]+1);
45 d_fact[2] = (uint8_t)((uint32_t)CC_XO_FREQ/(*rxbw)/8/32);
46 d_fact[3] = (uint8_t)(d_fact[2]+1);
49 for (i = 0; i < 4; i++){
57 bw[0] = (int32_t)CC_XO_FREQ/d_fact[0]/8/20;
58 bw[1] = (int32_t)CC_XO_FREQ/d_fact[1]/8/20;
59 bw[2] = (int32_t)CC_XO_FREQ/d_fact[2]/8/32;
60 bw[3] = (int32_t)CC_XO_FREQ/d_fact[3]/8/32;
65 for (i = 0; i < 4; i++){
66 err = (uint32_t)abs(bw[0]-(int32_t)(*rxbw));
81 reg = (uint8_t)(reg | (d_fact[sm] & 0x3F));
83 cc112xSpiWriteReg(radio_id, CC112X_CHAN_BW, ®);
85 *rxbw = (uint32_t)bw[sm];
112 uint8_t radio_set_fsk_param(uint8_t radio_id, uint32_t *symbol_rate, uint32_t *deviation){
118 d = ((uint64_t)(*deviation) * (1<<24))/CC_XO_FREQ;
124 m = (uint8_t)(((uint64_t)(*deviation) * (1<<23))/CC_XO_FREQ);
132 m = (uint8_t)(((((uint64_t)(*deviation) * (1<<24))/CC_XO_FREQ) >> e) - 256);
142 s = ((uint64_t)(*symbol_rate) * ((uint64_t)1<<39))/((uint64_t)CC_XO_FREQ);
147 ms = (uint32_t)(((uint64_t)(*symbol_rate) * ((uint64_t)1<<38))/((uint64_t)CC_XO_FREQ));
156 ms = (uint32_t)(((((uint64_t)(*symbol_rate) * ((uint64_t)1<<39))/((uint64_t)CC_XO_FREQ)) >> es) - ((uint64_t)1<<20));
165 *deviation = (uint32_t)(((uint64_t)CC_XO_FREQ * m ) >> 23);
167 *deviation = (uint32_t)((CC_XO_FREQ * (256+(uint64_t)m) ) >> (24-e));
170 *symbol_rate = (uint32_t)((((uint64_t)CC_XO_FREQ) * ms ) >> 38);
172 *symbol_rate = (uint32_t)((((uint64_t)CC_XO_FREQ) * ((1<<20)+(uint64_t)ms) ) >> (39-es));
176 writebyte = (uint8_t)(ms & 0xFF);
177 cc112xSpiWriteReg(radio_id, CC112X_SYMBOL_RATE0, &writebyte);
178 writebyte = (uint8_t)((ms>>8) & 0xFF);
179 cc112xSpiWriteReg(radio_id, CC112X_SYMBOL_RATE1, &writebyte);
180 writebyte = (uint8_t)(((ms>>16) & 0x0F) | ((es<<4) & 0xF0));
181 cc112xSpiWriteReg(radio_id, CC112X_SYMBOL_RATE2, &writebyte);
183 cc112xSpiWriteReg(radio_id, CC112X_DEVIATION_M , &m);
184 writebyte = (uint8_t)((e & 0x07) | (0 << 3) | (0<<6));
185 cc112xSpiWriteReg(radio_id, CC112X_MODCFG_DEV_E , &writebyte);
207 uint8_t radio_set_pwr_f(uint8_t radio_id,
double *pwr, uint8_t *reg_value){
209 if ((*pwr < -11) || (*pwr > 15))
215 uint32_t pwr_reg = (uint32_t)p;
219 pwr_reg = pwr_reg & 0x3F;
222 p = radio_pwr_reg_to_dbm((uint8_t)pwr_reg);
225 *reg_value = (uint8_t)pwr_reg;
230 writebyte = (uint8_t)((1<<6) | pwr_reg);
231 cc112xSpiWriteReg(radio_id, CC112X_PA_CFG2, &writebyte);
247 double radio_pwr_reg_to_dbm(uint8_t reg_value){
248 return ((
double)reg_value+1)/2-18;;
263 uint8_t radio_set_pwr_reg(uint8_t radio_id, uint8_t reg_value){
265 if ((reg_value < 3) || (reg_value > 0x3F))
270 writebyte = (uint8_t)((1<<6) | (reg_value&0x3F));
271 cc112xSpiWriteReg(radio_id, CC112X_PA_CFG2, &writebyte);
289 uint8_t radio_set_freq_f(uint8_t radio_id,
double *freq){
291 uint8_t div, bandsel;
292 if ((*freq >= 136.7) && (*freq <= 160)){
295 else if ((*freq >= 164) && (*freq <= 192)){
298 else if ((*freq >= 205) && (*freq <= 240)){
301 else if ((*freq >= 273.3) && (*freq <= 320)){
304 else if ((*freq >= 410) && (*freq <= 480)){
307 else if ((*freq >= 820) && (*freq <= 960)){
314 f = div * ((*freq)*1000000) * 65536 / CC_XO_FREQ;
316 uint32_t freq_reg = (uint32_t)f;
319 f = (double)freq_reg*CC_XO_FREQ;
328 writebyte = freq_reg & 0xFF;
329 cc112xSpiWriteReg(radio_id, CC112X_FREQ0, &writebyte);
330 writebyte = (freq_reg >> 8) & 0xFF;
331 cc112xSpiWriteReg(radio_id, CC112X_FREQ1, &writebyte);
332 writebyte = (freq_reg >> 16) & 0xFF;
333 cc112xSpiWriteReg(radio_id, CC112X_FREQ2, &writebyte);
334 writebyte = 0x10 | bandsel;
335 cc112xSpiWriteReg(radio_id, CC112X_FS_CFG, &writebyte);
353 void radio_reset_config(uint8_t radio_id,
const registerSetting_t *cfg, uint16_t len){
355 SPI_cmd(radio_id, CC112X_SRES);
357 for(uint16_t i = 0; i < len; i++) {
358 writeByte = cfg[i].data;
359 cc112xSpiWriteReg(radio_id, cfg[i].addr, &writeByte);
374 #define VCDAC_START_OFFSET 2
375 #define FS_VCO2_INDEX 0
376 #define FS_VCO4_INDEX 1
377 #define FS_CHP_INDEX 2
378 void manualCalibration(uint8_t radio_id) {
380 uint8_t original_fs_cal2;
381 uint8_t calResults_for_vcdac_start_high[3];
382 uint8_t calResults_for_vcdac_start_mid[3];
388 cc112xSpiWriteReg(radio_id, CC112X_FS_VCO2, &writeByte);
391 cc112xSpiReadReg(radio_id, CC112X_FS_CAL2, &original_fs_cal2);
392 writeByte = (uint8_t)(original_fs_cal2 + VCDAC_START_OFFSET);
393 cc112xSpiWriteReg(radio_id, CC112X_FS_CAL2, &writeByte);
397 SPI_cmd(radio_id, CC112X_SCAL);
400 cc112xSpiReadReg(radio_id, CC112X_MARCSTATE, &marcstate);
401 }
while (marcstate != 0x41);
405 cc112xSpiReadReg(radio_id, CC112X_FS_VCO2,
406 &calResults_for_vcdac_start_high[FS_VCO2_INDEX]);
407 cc112xSpiReadReg(radio_id, CC112X_FS_VCO4,
408 &calResults_for_vcdac_start_high[FS_VCO4_INDEX]);
409 cc112xSpiReadReg(radio_id, CC112X_FS_CHP,
410 &calResults_for_vcdac_start_high[FS_CHP_INDEX]);
414 cc112xSpiWriteReg(radio_id, CC112X_FS_VCO2, &writeByte);
417 writeByte = original_fs_cal2;
418 cc112xSpiWriteReg(radio_id, CC112X_FS_CAL2, &writeByte);
423 SPI_cmd(radio_id, CC112X_SCAL);
426 cc112xSpiReadReg(radio_id, CC112X_MARCSTATE, &marcstate);
427 }
while (marcstate != 0x41);
431 cc112xSpiReadReg(radio_id, CC112X_FS_VCO2,
432 &calResults_for_vcdac_start_mid[FS_VCO2_INDEX]);
433 cc112xSpiReadReg(radio_id, CC112X_FS_VCO4,
434 &calResults_for_vcdac_start_mid[FS_VCO4_INDEX]);
435 cc112xSpiReadReg(radio_id, CC112X_FS_CHP,
436 &calResults_for_vcdac_start_mid[FS_CHP_INDEX]);
441 if (calResults_for_vcdac_start_high[FS_VCO2_INDEX] >
442 calResults_for_vcdac_start_mid[FS_VCO2_INDEX]) {
443 writeByte = calResults_for_vcdac_start_high[FS_VCO2_INDEX];
444 cc112xSpiWriteReg(radio_id, CC112X_FS_VCO2, &writeByte);
445 writeByte = calResults_for_vcdac_start_high[FS_VCO4_INDEX];
446 cc112xSpiWriteReg(radio_id, CC112X_FS_VCO4, &writeByte);
447 writeByte = calResults_for_vcdac_start_high[FS_CHP_INDEX];
448 cc112xSpiWriteReg(radio_id, CC112X_FS_CHP, &writeByte);
450 writeByte = calResults_for_vcdac_start_mid[FS_VCO2_INDEX];
451 cc112xSpiWriteReg(radio_id, CC112X_FS_VCO2, &writeByte);
452 writeByte = calResults_for_vcdac_start_mid[FS_VCO4_INDEX];
453 cc112xSpiWriteReg(radio_id, CC112X_FS_VCO4, &writeByte);
454 writeByte = calResults_for_vcdac_start_mid[FS_CHP_INDEX];
455 cc112xSpiWriteReg(radio_id, CC112X_FS_CHP, &writeByte);
uint8_t SPI_cmd(uint8_t spi_num, uint8_t cmd)
bool GPIO_read(uint8_t gpio_num)