55#include "esp_err.h"
66#include "esp_log.h"
77
8+ #include "driver/gpio.h"
9+ #include "driver/spi_common.h"
10+ #include "driver/spi_master.h"
11+
812#include "freertos/task.h"
913
1014// from LoRaMac
1317// ANSI
1418#include <string.h>
1519
20+ #define HZ_PER_KHZ 1000
21+ #define KHZ_PER_MHZ 1000
22+ #define HZ_PER_MHZ (HZ_PER_KHZ * KHZ_PER_MHZ)
23+
24+ #define SX1262_MAX_SPI_CLOCK_SPEED_MHZ 16
25+
26+ typedef struct spi_s {
27+ spi_device_handle_t handle ;
28+ gpio_num_t miso ;
29+ gpio_num_t mosi ;
30+ gpio_num_t sclk ;
31+ gpio_num_t cs ;
32+ gpio_num_t reset ;
33+ gpio_num_t irq_dio1 ;
34+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
35+ gpio_num_t busy ;
36+ #endif
37+ } spi_c ;
38+
39+ static spi_c lora_spi ;
1640
1741static const char * TAG = "ESP32Board" ;
1842
@@ -24,7 +48,52 @@ void BoardInitMcu( void )
2448
2549void BoardInitPeriph (void )
2650{
27-
51+ esp_err_t ret ;
52+
53+ lora_spi .miso = CONFIG_LORAWAN_SPI_MISO_GPIO ;
54+ lora_spi .mosi = CONFIG_LORAWAN_SPI_MOSI_GPIO ;
55+ lora_spi .sclk = CONFIG_LORAWAN_SPI_SCLK_GPIO ;
56+ lora_spi .cs = CONFIG_LORAWAN_SPI_CS_GPIO ;
57+ lora_spi .reset = CONFIG_LORAWAN_RESET_GPIO ;
58+ lora_spi .irq_dio1 = CONFIG_LORAWAN_IRQ_DIO1_GPIO ;
59+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
60+ lora_spi .busy = CONFIG_LORAWAN_BUSY_GPIO ;
61+
62+ gpio_config_t busy_conf = {
63+ (1ULL <<lora_spi .busy ),
64+ GPIO_MODE_DISABLE ,
65+ GPIO_PULLUP_ENABLE ,
66+ GPIO_PULLDOWN_DISABLE ,
67+ GPIO_INTR_DISABLE
68+ };
69+
70+ ESP_ERROR_CHECK (gpio_config (& busy_conf ));
71+ #endif
72+
73+ CRITICAL_SECTION_BEGIN ();
74+
75+ // SPI initialization
76+ spi_bus_config_t buscfg = {
77+ .miso_io_num = lora_spi .miso ,
78+ .mosi_io_num = lora_spi .mosi ,
79+ .sclk_io_num = lora_spi .sclk ,
80+ .quadwp_io_num = -1 ,
81+ .quadhd_io_num = -1
82+ };
83+ spi_device_interface_config_t devcfg = {
84+ .clock_speed_hz = SX1262_MAX_SPI_CLOCK_SPEED_MHZ * HZ_PER_MHZ ,
85+ .mode = 0 ,
86+ .spics_io_num = lora_spi .cs ,
87+ .command_bits = 8 ,
88+ .address_bits = 16 ,
89+ .queue_size = 1 // using ESP32 synchronous SPI API, will only ever be one transaction "in the air" at a time.
90+ // https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/spi_master.html#_CPPv4N29spi_device_interface_config_t10queue_sizeE
91+ };
92+ ESP_ERROR_CHECK (spi_bus_initialize ((spi_host_device_t )CONFIG_LORAWAN_SPI_HOST , & buscfg , SPI_DMA_CH_AUTO ));
93+
94+ ESP_ERROR_CHECK (spi_bus_add_device ((spi_host_device_t )CONFIG_LORAWAN_SPI_HOST , & devcfg , & lora_spi .handle ));
95+
96+ CRITICAL_SECTION_END ();
2897}
2998
3099void BoardResetMcu ( void )
0 commit comments