Hello.
This post is part of this topic; RAK4630 Custom Firmware
I am having a problem with SPI.
I have created my own software based on the libraries in here: https://github.com/beegee-tokyo/SX126x-Arduino But I could not succeed.
Here is my main function;
/**@brief Function for application main entry. */
int main (void)
{
uint32_t err_code;
// Initialize logs.
log_init();
NRF_LOG_INFO("LoRaWan Class A example started.");
// Initialize clocks
nrf_drv_clock_init();
nrf_drv_clock_lfclk_request(NULL);
nrf_drv_clock_hfclk_request(NULL);
// Enable nRF52 DCDC
NRF_POWER->DCDCEN = 1;
// Initialize Scheduler and timer
err_code = timers_init();
if (err_code != NRF_SUCCESS)
{
NRF_LOG_ERROR("timers_init failed - %d", err_code);
return err_code;
}
// Initialize LoRa chip.
err_code = lora_hardware_init();
if (err_code != NRF_SUCCESS)
{
NRF_LOG_ERROR("lora_hardware_init failed - %d", err_code);
return err_code;
}
// Initialize LoRaWan
err_code = lmh_init(&lora_callbacks);
if (err_code != NRF_SUCCESS)
{
NRF_LOG_ERROR("lmh_init failed - %d", err_code);
return err_code;
}
// Start Join procedure
lmh_join(OVER_THE_AIR_ACTIVATION);
app_timer_start(lora_tx_timer_id, APP_TIMER_TICKS(LORAWAN_APP_TX_DUTYCYCLE), NULL);
// Enter main loop.
for (;;)
{
app_sched_execute(),
Radio.IrqProcess();
if (NRF_LOG_PROCESS() == false)
{
power_manage();
}
}
}
Here is my lora_hardware_init function;
uint32_t lora_hardware_init (void)
{
TimerConfig();
SpiInit(&SX126x.Spi, PIN_LORA_MOSI, PIN_LORA_MISO, PIN_LORA_SCLK, NRF_SPI_PIN_NOT_CONNECTED);
SX126xIoInit();
SX126xReset();
volatile uint16_t readSyncWord = 0;
SX126xReadRegisters(REG_LR_SYNCWORD, (uint8_t *)&readSyncWord, 2);
return NRF_SUCCESS;
}
Here is my spi-board.c file;
#include "spi-board.h"
#include "board.h"
volatile bool xferDone;
/** @brief SPI master driver event handler type. */
static void nrf_drv_spi_evt_handler(nrf_drv_spi_evt_t const * p_event, void *p_context)
{
xferDone = true;
}
void SpiInit (Spi_t *obj, uint32_t mosi, uint32_t miso, uint32_t sclk, uint32_t nss)
{
uint32_t err_code;
nrf_drv_spi_config_t config = NRF_DRV_SPI_DEFAULT_CONFIG;
const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);
obj->Spi = spi;
config.frequency = NRF_DRV_SPI_FREQ_8M;
config.miso_pin = miso;
config.mosi_pin = mosi;
config.sck_pin = sclk;
config.ss_pin = nss;
err_code = nrf_drv_spi_init (&(obj->Spi), &config, nrf_drv_spi_evt_handler, NULL);
APP_ERROR_CHECK (err_code);
}
//
void SpiDeInit (Spi_t *obj)
{
nrf_drv_spi_uninit (&(obj->Spi));
}
//
uint16_t SpiInOut (Spi_t *obj, uint16_t outData)
{
uint32_t err_code;
uint8_t inData ;
if ((obj == NULL) || (&(obj->Spi) == NULL))
{
APP_ERROR_CHECK_BOOL (false);
}
xferDone = false;
err_code = nrf_drv_spi_transfer (&(obj->Spi), (uint8_t*)&outData, 1, (uint8_t*)&inData, 1);
APP_ERROR_CHECK(err_code);
return (uint16_t)inData;
}
//
Here is my problem. When I try to read REG_LR_SYNCWORD register, I get 0x00 and it does not seem to be correct. Do you have any idea what am I missing here?
Side note: I am using RAK4630 with my own PCB.
Side note 2: Here are my pin definitions;
#define PIN_LORA_DIO_1 NRF_GPIO_PIN_MAP(1, 15) // LORA DIO_1
#define PIN_LORA_DIO_2 NRF_GPIO_PIN_MAP(1, 07) // LORA DIO_2
#define PIN_LORA_BUSY NRF_GPIO_PIN_MAP(1, 14) // LORA BUSY
#define PIN_LORA_RESET NRF_GPIO_PIN_MAP(1, 06) // LORA RESET
#define PIN_LORA_ANTSW NRF_GPIO_PIN_MAP(1, 05) // LORA ANTSW
#define PIN_LORA_NSS NRF_GPIO_PIN_MAP(1, 10) // LORA SPI CS
#define PIN_LORA_MISO NRF_GPIO_PIN_MAP(1, 13) // LORA SPI MISO
#define PIN_LORA_MOSI NRF_GPIO_PIN_MAP(1, 12) // LORA SPI MOSI
#define PIN_LORA_SCLK NRF_GPIO_PIN_MAP(1, 11) // LORA SPI CLK
Do you have any idea? I could not be able to start the Lora module successfully without the Arduino library.
Side note 3: I did not try to run arduino library. I tried the RAK wireless Arduino library with their suggestions on the web.