#include #include #include #include #include #include // TODO: these should be in Kconfig #define SX127X_TASK_STACK_SIZE (2 * 1024) #define SX127X_TASK_PRIORITY 3 #define SX127X_RX_QUEUE_LEN 8 #define SX127X_TX_QUEUE_LEN 8 #define SX127X_SPI_QUEUE_SIZE 8 #define SX127X_SPI_DMA_CHAN 1 #define SX127X_MAX_TRANSFER (256) #define RESET_DELAY (pdMS_TO_TICKS(15)) // 8mhz #define SPI_CLOCK_HZ (8 * 1000 * 1000) #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; 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;