bldc/lora/SX1278_hw.c

104 lines
2.2 KiB
C
Raw Normal View History

/**
* Author Wojciech Domski <Wojciech.Domski@gmail.com>
* www: www.Domski.pl
*
* Hardware layer for SX1278 LoRa module
* modfied vor GESC schardt@team-ctech.de
*/
#include "conf_general.h"
2021-04-11 03:18:54 -07:00
#ifdef HW_HAS_LORA
#include "SX1278_hw.h"
static void spi_transfer(uint8_t *in_buf, const uint8_t *out_buf, int length);
static void spi_delay(void);
void SX1278_hw_init() {
2021-03-31 10:46:31 -07:00
SX1278_hw_SetNSS(0);
2021-04-11 03:18:54 -07:00
palSetPad(HW_LORA_SPI_PORT_RESET, HW_LORA_SPI_PIN_RESET);
}
void SX1278_hw_SetNSS(int value) {
2021-04-11 03:18:54 -07:00
palWritePad(HW_LORA_SPI_PORT_NSS, HW_LORA_SPI_PIN_NSS, value);
}
void SX1278_hw_Reset() {
2021-03-31 10:46:31 -07:00
SX1278_hw_SetNSS(1);
2021-04-11 03:18:54 -07:00
palClearPad(HW_LORA_SPI_PORT_RESET, HW_LORA_SPI_PIN_RESET);
2021-03-31 10:46:31 -07:00
SX1278_hw_DelayMs(1);
2021-04-11 03:18:54 -07:00
palSetPad(HW_LORA_SPI_PORT_RESET, HW_LORA_SPI_PIN_RESET);
2021-03-31 10:46:31 -07:00
SX1278_hw_DelayMs(100);
}
void SX1278_hw_SPICommand(uint8_t cmd) {
2021-03-31 10:46:31 -07:00
uint8_t rx;
SX1278_hw_SetNSS(0);
spi_transfer(&rx, &cmd, 1);
}
uint8_t SX1278_hw_SPIReadByte() {
2021-03-31 10:46:31 -07:00
uint8_t rxByte = 0x00;
2021-03-31 10:46:31 -07:00
SX1278_hw_SetNSS(0);
spi_transfer(&rxByte, NULL, 1);
return rxByte;
}
void SX1278_hw_DelayMs(uint32_t msec) {
2021-03-31 10:46:31 -07:00
chThdSleepMilliseconds(msec);
}
int SX1278_hw_GetDIO0() {
2021-04-11 03:18:54 -07:00
return (palReadPad(HW_LORA_SPI_PORT_DIO0,HW_LORA_SPI_PIN_DIO0));
}
// Software SPI
static void spi_transfer(uint8_t *in_buf, const uint8_t *out_buf, int length) {
2021-03-31 10:46:31 -07:00
for (int i = 0;i < length;i++) {
uint8_t send = out_buf ? out_buf[i] : 0xFF;
uint8_t receive = 0;
for (int bit = 0;bit < 8;bit++) {
2021-04-11 03:18:54 -07:00
palWritePad(HW_LORA_SPI_PORT_MOSI, HW_LORA_SPI_PIN_MOSI, send >> 7);
2021-03-31 10:46:31 -07:00
send <<= 1;
2021-04-11 03:18:54 -07:00
palSetPad(HW_LORA_SPI_PORT_SCK, HW_LORA_SPI_PIN_SCK);
2021-03-31 10:46:31 -07:00
spi_delay();
int samples = 0;
2021-04-11 03:18:54 -07:00
samples += palReadPad(HW_LORA_SPI_PORT_MISO, HW_LORA_SPI_PIN_MISO);
2021-03-31 10:46:31 -07:00
__NOP();
2021-04-11 03:18:54 -07:00
samples += palReadPad(HW_LORA_SPI_PORT_MISO, HW_LORA_SPI_PIN_MISO);
2021-03-31 10:46:31 -07:00
__NOP();
2021-04-11 03:18:54 -07:00
samples += palReadPad(HW_LORA_SPI_PORT_MISO, HW_LORA_SPI_PIN_MISO);
2021-03-31 10:46:31 -07:00
__NOP();
2021-04-11 03:18:54 -07:00
samples += palReadPad(HW_LORA_SPI_PORT_MISO, HW_LORA_SPI_PIN_MISO);
2021-03-31 10:46:31 -07:00
__NOP();
2021-04-11 03:18:54 -07:00
samples += palReadPad(HW_LORA_SPI_PORT_MISO, HW_LORA_SPI_PIN_MISO);
2021-03-31 10:46:31 -07:00
receive <<= 1;
if (samples > 2) {
receive |= 1;
}
2021-04-11 03:18:54 -07:00
palClearPad(HW_LORA_SPI_PORT_SCK, HW_LORA_SPI_PIN_SCK);
2021-03-31 10:46:31 -07:00
spi_delay();
}
if (in_buf) {
in_buf[i] = receive;
}
}
}
static void spi_delay(void) {
2021-03-31 10:46:31 -07:00
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
}
#endif