|
|
|
#include <esp_log.h>
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
#include <freertos/queue.h>
|
|
|
|
#include <freertos/semphr.h>
|
|
|
|
#include <freertos/task.h>
|
|
|
|
#include <stdatomic.h>
|
|
|
|
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
|
|
|
#define SX127X_TASK_STACK_SIZE CONFIG_SX127X_TASK_STACK_SIZE
|
|
|
|
#define SX127X_TASK_PRIORITY CONFIG_SX127X_TASK_PRIORITY
|
|
|
|
#define SX127X_RX_QUEUE_LEN CONFIG_SX127X_RX_QUEUE_LEN
|
|
|
|
#define SX127X_TX_QUEUE_LEN CONFIG_SX127X_TX_QUEUE_LEN
|
|
|
|
#define SX127X_SPI_QUEUE_SIZE CONFIG_SX127X_SPI_QUEUE_SIZE
|
|
|
|
#define SX127X_SPI_DMA_CHAN CONFIG_SX127X_SPI_DMA_CHAN
|
|
|
|
#define SX127X_SPI_CLOCK_HZ CONFIG_SX127X_SPI_CLOCK_HZ
|
|
|
|
|
|
|
|
#define SX127X_MAX_TRANSFER (256)
|
|
|
|
#define SX127X_RESET_DELAY (pdMS_TO_TICKS(15))
|
|
|
|
#define SX127X_MUTEX_TIMOUT (pdMS_TO_TICKS(100))
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef NODEBUG
|
|
|
|
#define SX127X_CHECK(check, str, ret_val, ...) \
|
|
|
|
if (!(check)) { \
|
|
|
|
ESP_LOGE(SX127X_TAG, str, ##__VA_ARGS__); \
|
|
|
|
return (ret_val); \
|
|
|
|
}
|
|
|
|
#define SX127X_ERROR_CHECK(ret) \
|
|
|
|
{ \
|
|
|
|
esp_err_t _error_code = (ret); \
|
|
|
|
if (_error_code != ESP_OK) { \
|
|
|
|
return _error_code; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
#define SX127X_ERROR_CHECK2(ret, fun) SX127X_CHECK(ret)
|
|
|
|
#else
|
|
|
|
#define SX127X_CHECK(check, str, ret_val, ...) \
|
|
|
|
if (!(check)) { \
|
|
|
|
ESP_LOGE(SX127X_TAG, "%s:%d: check %s failed: " str, __FUNCTION__, \
|
|
|
|
__LINE__, #check, ##__VA_ARGS__); \
|
|
|
|
return (ret_val); \
|
|
|
|
}
|
|
|
|
#define SX127X_ERROR_CHECK(ret) \
|
|
|
|
{ \
|
|
|
|
esp_err_t _error_code = (ret); \
|
|
|
|
if (_error_code != ESP_OK) { \
|
|
|
|
const char *_error_name = esp_err_to_name(_error_code); \
|
|
|
|
ESP_LOGE(SX127X_TAG, "%s:%d: %s (%d)", __FUNCTION__, __LINE__, \
|
|
|
|
_error_name, _error_code); \
|
|
|
|
return _error_code; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
#define SX127X_ERROR_CHECK2(ret, fun) \
|
|
|
|
{ \
|
|
|
|
esp_err_t _error_code = (ret); \
|
|
|
|
if (_error_code != ESP_OK) { \
|
|
|
|
const char *_error_name = esp_err_to_name(_error_code); \
|
|
|
|
ESP_LOGE(SX127X_TAG, "%s:%d: %s failed: %s (%d)", __FUNCTION__, \
|
|
|
|
__LINE__, #fun, _error_name, _error_code); \
|
|
|
|
return _error_code; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef enum sx127x_task_state {
|
|
|
|
SX127X_TASK_STOPPED = 0,
|
|
|
|
SX127X_TASK_RUNNING = 1,
|
|
|
|
SX127X_TASK_STOPPING = 2,
|
|
|
|
} sx127x_task_state_t;
|
|
|
|
|
|
|
|
typedef struct sx127x {
|
|
|
|
sx127x_config_t config;
|
|
|
|
spi_device_handle_t device_handle;
|
|
|
|
SemaphoreHandle_t spi_mutex;
|
|
|
|
TaskHandle_t task_handle;
|
|
|
|
_Atomic(sx127x_task_state_t) task_state;
|
|
|
|
// binary semaphore which is given when an interrupt is received from the
|
|
|
|
// radio
|
|
|
|
SemaphoreHandle_t intr_semaphore;
|
|
|
|
// queue of packets received from the radio
|
|
|
|
QueueHandle_t rx_packet_queue;
|
|
|
|
// queue of packets to send to the radio
|
|
|
|
QueueHandle_t tx_packet_queue;
|
|
|
|
} sx127x_t;
|
|
|
|
|
|
|
|
const char *SX127X_TAG;
|