Код: Выделить всё
#include "SI4461.h"
#include <SI_EFM8SB1_Register_Enums.h>
#include <SI_EFM8SB1_Defs.h>
#include "pins.h"
uint8_t SPI_buf[16];
uint8_t TX_packet[] = {0x06, 0x66, 0x04, 0xAA, 0x41, 0x42, 0x43, 0x00}; // package
//-------Radio Commands----------------------------------------------
code const uint8_t SI4461_sleep[] = {0x02, 0x34, 0x01}; // put radio on sleep (MOVE_TO_SLEEP)
code const uint8_t SI4461_fifo_info[] = {0x02, 0x15, 0x00}; // FIFO_INFO
code const uint8_t SI4461_clear_fifo[] = {0x02, 0x15, 0x03}; // CLEAR_FIFO
code const uint8_t SI4461_read_cmd_buff[] = {0x44}; // READ_CMD_BUFF
code const uint8_t SI4461_read_rx_fifo[] = {0x77}; // READ_RX_FIFO
code const uint8_t SI4461_frr_a_read[] = {0x50}; // FRR_A_READ
code const uint8_t SI4461_clear_irq[] = {0x01, 0x20}; // CLEAR_IRQ
code const uint8_t SI4461_move_to_rx[] = {0x02, 0x34, 0x08}; // MOVE_TO_RX
code const uint8_t SI4461_start_rx[] = {0x08, 0x32, 0x00, 0x00, 0x00, 0x00, 0x08, 0x07, 0x08}; // START_RX opeation
code const uint8_t SI4461_start_tx[] = {0x06, 0x31, 0x00, 0x10, 0x00, 0x05, 0}; // start TX operation
code const uint8_t SI4461_rqst_device_state[] = {0x01, 0x33}; // REQUEST_DEVICE_STATE
code const uint8_t SI4461_measure_batt[] = {0x03, 0x14, 0x08, 0xC9};
//-------SI4461 Config----------------------------------------------
// 903MHz, 1.2kbps, 5.15KHz, 5 bytes payload + CRC16, 4 bytes pre, D3 91 synch
code const uint8_t SI4461_Config[] = {
0x07, 0x02, 0x01, 0x00, 0x01, 0xC9, 0xC3, 0x80, // RF_POWER_UP
0x08, 0x13, 0x01, 0x08, 0x01, 0x01, 0x01, 0x0B, 0x00, // GPIO_PIN_CFG: int. disabled
0x06, 0x11, 0x00, 0x02, 0x00, 0x52, 0x00, // GLOBAL_XO_TUNE
0x05, 0x11, 0x00, 0x01, 0x03, 0x60, // GLOBAL_CONFIG
0x05, 0x11, 0x01, 0x01, 0x00, 0x00, // INT_CTL_ENABLE: int disable
0x08, 0x11, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, // FRR_CTL_A_MODE
0x07, 0x11, 0x10, 0x03, 0x00, 0x04, 0x14, 0x00, // PREAMBLE_TX_LENGTH
0x09, 0x11, 0x10, 0x05, 0x04, 0x31, 0x00, 0x00, 0x00, 0x00, // PREAMBLE_CONFIG
0x09, 0x11, 0x11, 0x05, 0x00, 0x01, 0xCB, 0x89, 0x00, 0x00, // SYNC_CONFIG (2 bytes 0xD3 0x91)
0x05, 0x11, 0x12, 0x01, 0x00, 0x84, // PKT_CRC_CONFIG
0x08, 0x11, 0x12, 0x04, 0x03, 0xFF, 0xFF, 0x00, 0x02, // PKT_CRC_CONFIG
0x05, 0x11, 0x12, 0x01, 0x0B, 0x30, // PKT_TX_THRESHOLD
0x10, 0x11, 0x12, 0x0C, 0x0D, 0x00, 0x05, 0x04, 0xA2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0C, 0x11, 0x12, 0x08, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // RF_PKT_FIELD_4_LENGTH_12_8_8
0x10, 0x11, 0x20, 0x0C, 0x00, 0x02, 0x00, 0x07, 0x00, 0x2E, 0xE0, 0x01, 0xC9, 0xC3, 0x80, 0x00, 0x00,
0x05, 0x11, 0x20, 0x01, 0x0C, 0xB4, // MODEM_FREQ_DEV (5.15 KHz)
0x0C, 0x11, 0x20, 0x08, 0x18, 0x01, 0x80, 0x08, 0x03, 0xC0, 0x00, 0x24, 0x20,
0x0D, 0x11, 0x20, 0x09, 0x22, 0x03, 0x0D, 0x00, 0xA7, 0xC6, 0x00, 0x54, 0x02, 0xC2, // 903 MHz
0x0B, 0x11, 0x20, 0x07, 0x2C, 0x04, 0x36, 0x80, 0x01, 0x5D, 0x07, 0x80,
0x05, 0x11, 0x20, 0x01, 0x35, 0xE2,
0x0D, 0x11, 0x20, 0x09, 0x38, 0x11, 0xAB, 0xAB, 0x00, 0x02, 0xFF, 0xFF, 0x00, 0x2B,
0x0A, 0x11, 0x20, 0x06, 0x42, 0xA4, 0x02, 0xD6, 0x81, 0x01, 0x29,
0x05, 0x11, 0x20, 0x01, 0x51, 0x08, // RF_MODEM_CLKGEN_BAND
0x08, 0x11, 0x22, 0x04, 0x00, 0x18, 0x19, 0xC0, 0x3D, // RF_PA_MODE
0x0B, 0x11, 0x23, 0x07, 0x00, 0x2C, 0x0E, 0x0B, 0x04, 0x0C, 0x73, 0x03,
0x0B, 0x11, 0x40, 0x07, 0x00, 0x3B, 0x09, 0x99, 0x99, 0x22, 0x22, 0x20,
0};
void SI4461_setup(void)
{
uint8_t* ptr;
// Delay_ms(500); // power-up delay
ptr = (uint8_t*) SI4461_Config;
do
{
ptr = SI4461_write(ptr);
} while(*ptr);
SI4461_write(SI4461_clear_irq); // Send CLEAR_IRQ command
SI4461_write(SI4461_sleep); // put radio on sleep
}
void SI4461_sendPacket(void)
{
SI4461_write(SI4461_clear_fifo); // clear FIFOs
SI4461_write(TX_packet); // load packet into TX FIFO
SI4461_write(SI4461_start_tx); // broadcast the packet
}
uint8_t SI4461_getBattVoltage(void)
{
uint16_t batt;
SI4461_write(SI4461_measure_batt);
SI4461_read(SI4461_read_cmd_buff, 8);
batt = (SPI_buf[4] << 8) + SPI_buf[5];
batt = (batt*3 + 64) >> 7; // batt voltage*10
return(batt);
}
uint8_t* SI4461_write(const uint8_t *buf)
{
uint8_t len;
len = *buf++; // # of bytes to transmit
while (! CTS){} // wait for CTS
SPI0CN0_NSSMD0 = 0; // CS down
while (len != 0)
{
SPI0DAT = *buf++;
while (! SPI0CN0_TXBMT){} // wait for ready
len--;
}
SPI0CN0_SPIF = 0;
while (! SPI0CN0_SPIF){} // wait for the last byte
SPI0CN0_SPIF = 0;
SPI0CN0_NSSMD0 = 1; // CS up
return((uint8_t*)buf);
}
void SI4461_read(const uint8_t *buf, uint8_t rlen)
{
uint8_t i;
while (! CTS){} // wait for CTS
SPI0CN0_NSSMD0 = 0; // CS down
SPI0DAT = *buf;
for (i=0; i< rlen; i++)
{
while (SPI0CN0_TXBMT != 1){}
SPI0CN0_SPIF = 0;
while (SPI0CN0_SPIF != 1){} // wait for the byte
SPI0CN0_SPIF = 0;
SPI_buf[i] = SPI0DAT; // get received data
if (i < rlen-1) // more bytes to receive?
SPI0DAT = 0xFF; // YES - send dummy byte
}
SPI0CN0_NSSMD0 = 1; // CS up
}