Hello,
I have a question regarding the correct way/good practice of programming with the use of SPI. I am using SDK 15.0.0.
I've written some code in a source file peripheral.c to control a peripheral (e.g. initiliazations, writing/reading registers). To do this it makes use of SPI. My main.c creates the SPI instance, defines the pinout, and calls de initialization.
main.c:
#define SPI_INSTANCE 0 /**< SPI instance index */
#define SPI_SCK_PIN 3 /**< SPI_SCK_PIN - pin number */
#define SPI_MOSI_PIN 4 /**< SPI_SCK_PIN - pin number */
#define SPI_MISO_PIN 28 /**< SPI_SCK_PIN - pin number */
#define SPI_SS_PIN 29 /**< SPI_SCK_PIN - pin number */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance */
static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer */
static uint8_t m_tx_buf[2] = {}; /**< TX buffer */
static uint8_t m_rx_buf[sizeof(m_tx_buf) + 1]; /**< RX buffer */
static const int8_t m_length = sizeof(m_tx_buf); /**< Transfer length */
void spi_config(void)
{
//Configures SPI instance
}
void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
void * p_context)
{
//Event handler code
}
peripheral.c:
static void register_read(int register_to_access, int rx_length)
{
uint8_t command;
command = command_byte_set(register_to_access, read);
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, tx_length, m_rx_buf, rx_length));
}
//lots of other code
Due to the SPI instance being declared as static I cannot access it in peripheral.c. I assume that the SPI instance should stay static though.
Should I move the declaration and initialization of the SPI instance and event handler over to peripheral.c, or should and can I make the instance available to peripheral.c somehow?
Thank you in advance