Calibration_Stand/backend/Libs/sx1278/Src/sx1278.c

227 lines
6.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "../Inc/sx1278.h"
// Объект для работы с SX1278
SX1278_t sx1278;
// Функция инициализации SX1278
int SX1278_Init(uint8_t PinNss, uint8_t PinReset, uint8_t PinDIO0) {
sx1278.PinNSS = PinNss;
sx1278.PinReset = PinReset;
sx1278.PinDIO0 = PinDIO0;
// Инициализация WiringPi
if (wiringPiSetup() == -1) {
printf("Ошибка инициализации WiringPi\n");
return -1;
}
// Инициализация SPI
if (wiringPiSPISetup(0, 500000) == -1) {
printf("Ошибка инициализации SPI\n");
return -1;
}
/*SETUP GPIO Begin*/
// Инициализация GPIO
if (wiringPiSetupGpio() == -1) {
printf("Ошибка инициализации GPIO\n");
return -1;
}
pinMode(PinNss, OUTPUT);
pinMode(PinReset, OUTPUT);
pinMode(PinDIO0, INPUT);
/*SETUP GPIO End*/
SX1278_reset();
// Проверка связи с модулем (чтение и проверка регистра версии)
uint8_t version = SX1278_ReadRegister(REG_VERSION);
if (version != EXPECTED_VERSION) {
printf("Неверная версия модуля: 0x%x\n", version);
return -1;
}
return 0;
}
void SX1278_FIFO_SendData(uint8_t *data) {
uint8_t SendData[SX1278_PAYLOAD + 1] = {REG_FIFO | 0x80};
memcpy(&SendData[1], data, SX1278_PAYLOAD);
wiringPiSPIDataRW(0, SendData, SX1278_PAYLOAD + 1);
}
void SX1278_FIFO_ReadData(uint8_t *data) {
uint8_t buffer[SX1278_PAYLOAD + 1] = {REG_FIFO & 0x7F};
wiringPiSPIDataRW(0, buffer, SX1278_PAYLOAD + 1);
memcpy(data, &buffer[1], SX1278_PAYLOAD);
}
// Функция для записи в регистр
void SX1278_WriteRegister(uint8_t reg, uint8_t value) {
#ifdef DEBUG
uint8_t read_reg;
read_reg = SX1278_ReadRegister(reg);
printf("(WriteRegister) REG: 0x%02X OLD: 0x%02X", reg, read_reg);
#endif
uint8_t buffer[2] = { reg | 0x80, value };
wiringPiSPIDataRW(0, buffer, 2);
#ifdef DEBUG
read_reg = SX1278_ReadRegister(reg);
printf(" | NEW: 0x%02X\n", read_reg);
#endif
}
// Функция для чтения из регистра
uint8_t SX1278_ReadRegister(uint8_t reg) {
uint8_t buffer[2] = { reg & 0x7F, 0x00 };
wiringPiSPIDataRW(0, buffer, 2);
return buffer[1];
}
/* Аппаратная перезагрузка SX1278 */
void SX1278_reset(){
digitalWrite(sx1278.PinNSS, 1);
digitalWrite(sx1278.PinReset, 0);
delay(100);
digitalWrite(sx1278.PinReset, 1);
delay(100);
}
// Функция установки режима работы SX1278
void SX1278_SetMode(SX1278_Mode mode) {
uint16_t regValue = RF_OPMODE_LONGRANGEMODE_OFF | RF_OPMODE_MODULATIONTYPE_FSK;
switch (mode) {
case SX1278_MODE_SLEEP:
regValue |= RF_OPMODE_MODULATIONSHAPING_01 | RF_OPMODE_SLEEP;
break;
case SX1278_MODE_STANDBY:
regValue |= RF_OPMODE_MODULATIONSHAPING_01 | RF_OPMODE_STANDBY;
break;
case SX1278_MODE_RECEIVER:
regValue |= RF_OPMODE_MODULATIONSHAPING_01 | RF_OPMODE_RECEIVER;
break;
case SX1278_MODE_TRANSMITTER:
regValue |= RF_OPMODE_MODULATIONSHAPING_00 | RF_OPMODE_TRANSMITTER;
break;
default:
printf("Unknown mode!\n");
return;
}
SX1278_WriteRegister(REG_OPMODE, regValue);
}
/* Функция инициализации SX1278 */
void SX1278_load(){
/* Hardware init */
SX1278_reset();
/* FSK_Mode_sleep */
SX1278_SetMode(SX1278_MODE_SLEEP);
/* Bitrate with precision 19199.76 Kkbps */
SX1278_WriteRegister(REG_BITRATEMSB, 0x06);
SX1278_WriteRegister(REG_BITRATELSB, 0x82);
SX1278_WriteRegister(REG_BITRATEFRAC, 11);
/* Deviation */
SX1278_WriteRegister(REG_FDEVMSB, RF_FDEVMSB_45000_HZ);
SX1278_WriteRegister(REG_FDEVLSB, RF_FDEVLSB_45000_HZ);
/* Frequency */
SX1278_WriteRegister(REG_FRFMSB, RFLR_FRFMSB_434_MHZ);
SX1278_WriteRegister(REG_FRFMID, RFLR_FRFMID_434_MHZ);
SX1278_WriteRegister(REG_FRFLSB, RFLR_FRFLSB_434_MHZ);
/* Gain_LNA */
SX1278_WriteRegister(REG_LNA, RF_LNA_GAIN_G6|RF_LNA_BOOST_ON);
/* Intit reciver, Auto Freq on, LNA on, Preamble detect on */
SX1278_WriteRegister(REG_RXCONFIG, RF_RXCONFIG_RESTARTRXONCOLLISION_OFF |
RF_RXCONFIG_AFCAUTO_OFF |
RF_RXCONFIG_AGCAUTO_OFF |
RF_RXCONFIG_RXTRIGER_PREAMBLEDETECT);
/* Intit samplinng RSSI */
SX1278_WriteRegister(REG_RSSICONFIG, RF_RSSICONFIG_SMOOTHING_256);
/* Init power transmiter (revise) */
SX1278_WriteRegister(REG_PACONFIG,0xF0);
/* RampFSK 500micro + gaussian (Rise/Fall time of ramp up/down in FSK, Gaussian filter BT = 1.0) */
SX1278_WriteRegister(REG_PARAMP, RF_PARAMP_1000_US |RF_PARAMP_MODULATIONSHAPING_00);
/* Bandwidth */
SX1278_WriteRegister(REG_RXBW, RF_RXBW_MANT_20 | RF_RXBW_EXP_2);
/* INIT AFC */
SX1278_WriteRegister(REG_AFCFEI, RF_AFCFEI_AFCAUTOCLEAR_ON |
RF_AFCFEI_AFCCLEAR |
RF_AFCFEI_AGCSTART);
/* Preamle detect on, 3bytes, number mistakes */
SX1278_WriteRegister(REG_PREAMBLEDETECT, RF_PREAMBLEDETECT_DETECTOR_ON |
RF_PREAMBLEDETECT_DETECTORSIZE_3 |
RF_PREAMBLEDETECT_DETECTORTOL_30);
/* Init Sync Word */
SX1278_WriteRegister(REG_SYNCCONFIG, RF_SYNCCONFIG_AUTORESTARTRXMODE_WAITPLL_OFF |
RF_SYNCCONFIG_PREAMBLEPOLARITY_55 |
RF_SYNCCONFIG_SYNCSIZE_4 |
RF_SYNCCONFIG_SYNC_ON);
/* Init size preamble */
SX1278_WriteRegister(REG_PREAMBLEMSB, 0x00);
SX1278_WriteRegister(REG_PREAMBLELSB, 0x03);
/* Init sync word */
SX1278_WriteRegister(REG_SYNCVALUE1, 0x01);
SX1278_WriteRegister(REG_SYNCVALUE2, 0x01);
SX1278_WriteRegister(REG_SYNCVALUE3, 0x01);
SX1278_WriteRegister(REG_SYNCVALUE4, 0x01);
/* Fixed length packet,CRC on, */
SX1278_WriteRegister(REG_PACKETCONFIG1, RF_PACKETCONFIG1_PACKETFORMAT_FIXED |
RF_PACKETCONFIG1_DCFREE_OFF |
RF_PACKETCONFIG1_CRC_ON |
RF_PACKETCONFIG1_CRCAUTOCLEAR_ON |
RF_PACKETCONFIG1_ADDRSFILTERING_OFF |
RF_PACKETCONFIG1_CRCWHITENINGTYPE_CCITT);
/* Packet mode on */
SX1278_WriteRegister(REG_PACKETCONFIG2, RF_PACKETCONFIG2_DATAMODE_PACKET );
/* Transmit current overload current */
SX1278_WriteRegister(REG_OCP, RF_OCP_ON| RF_OCP_TRIM_200_MA );
/* Length payload */
SX1278_WriteRegister(REG_PAYLOADLENGTH, SX1278_PAYLOAD);
/* GPIO - DIO_0 - DIO5, config */
SX1278_WriteRegister(REG_DIOMAPPING1, 0x00);
SX1278_WriteRegister(REG_DIOMAPPING2, 0x00);
/* FIFO Interrupt */
SX1278_WriteRegister(REG_FIFOTHRESH, RF_FIFOTHRESH_TXSTARTCONDITION_FIFOTHRESH | (SX1278_PAYLOAD-1));
SX1278_WriteRegister(REG_SEQCONFIG1, RF_SEQCONFIG1_FROMTX_TORX);
}