when i try to configure spi1 i am getting error, so how can i configure different spi ?
when i try to configure spi1 i am getting error, so how can i configure different spi ?
Hi,
Can you explain which SDK you are using, how you configure your SPI and which error you get? Please elaborate.
/*
* DAC.c
*
* Created on: Nov 2, 2021
* Author: 91889
*/
#include "DAC.h"
#include "stdio.h"
//#include "main.h"
#include "nrf_drv_spi.h"
#include "nrf_gpio.h"
#include <string.h>
#define SPI_INSTANCE 0 /**< SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
static volatile bool spi_xfer_done;
#define SPI_SS_PIN 31
#define SPI_MISO_PIN 30
#define SPI_MOSI_PIN 29
#define SPI_SCK_PIN 26
#define CS_Pin 5
#define VREF 2500
#define MULTIPLIER (65536 / VREF)
//extern SPI_HandleTypeDef hspi1;
void spi_event_handler(nrf_drv_spi_evt_t const *p_event,
void *p_context)
{
spi_xfer_done = true;
// NRF_LOG_INFO("Transfer completed.");
/* if (m_rx_buf[0] != 0)
{
NRF_LOG_INFO(" Received:");
NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
}*/
}
void Spi0_init(void)
{
nrf_drv_spi_config_t spi_config;
spi_config.ss_pin = SPI_SS_PIN;
spi_config.miso_pin = SPI_MISO_PIN;
spi_config.mosi_pin = SPI_MOSI_PIN;
spi_config.sck_pin = SPI_SCK_PIN;
spi_config.frequency = NRF_SPI_FREQ_4M;
spi_config.mode = NRF_SPI_MODE_0;
spi_config.bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST;
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
}
void Dac_Input(uint8_t chanel, uint16_t value)
{
static uint8_t m_tx_buf[] = {0};
static uint8_t m_rx_buf[] = {0};
static const uint8_t m_length = sizeof(m_tx_buf);
float multipiler = (float)65536 / VREF;
uint8_t tx_Buffer;
uint16_t dac_Value;
if (value == 2500)
{
dac_Value = 65535;
}
else
{
dac_Value = (uint16_t)(value * multipiler);
}
nrf_gpio_cfg_output(CS_Pin);
nrf_gpio_pin_clear(CS_Pin);
//HAL_GPIO_WritePin(GPIOA, CS_Pin, GPIO_PIN_RESET);
if (chanel == channel_A)
{
memset(m_rx_buf, 0, m_length);
spi_xfer_done = false;
m_tx_buf[0] = 0x30;
//HAL_SPI_Transmit(&hspi1, &tx_Buffer, 1, 100);
nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, NULL);
//HAL_Delay(50);
//power_down channel_B
// tx_Buffer = 0x41;
// HAL_SPI_Transmit(&hspi1, &tx_Buffer, 1, 100);
}
else
{
memset(m_rx_buf, 0, m_length);
spi_xfer_done = false;
m_tx_buf[0] = 0x31;
//HAL_SPI_Transmit(&hspi1, &tx_Buffer, 1, 100);
nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, NULL);
//HAL_Delay(50);
//power_down channel_A
// tx_Buffer = 0x40;
// HAL_SPI_Transmit(&hspi1, &tx_Buffer, 1, 100);
}
memset(m_rx_buf, 0, m_length);
spi_xfer_done = false;
m_tx_buf[0] = (uint8_t)((dac_Value & 0xFF00) >> 8);
//HAL_SPI_Transmit(&hspi1, &tx_Buffer, 1, 100);
nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, NULL);
memset(m_rx_buf, 0, m_length);
spi_xfer_done = false;
m_tx_buf[0] = (uint8_t)(dac_Value & 0x00FF);
//HAL_SPI_Transmit(&hspi1, &tx_Buffer, 1, 100);
nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, NULL);
//HAL_GPIO_WritePin(GPIOA, CS_Pin, GPIO_PIN_SET);
nrf_gpio_pin_set(CS_Pin);
}Hi,
I took the liberty to format your C code for improved readability.
You write that the clock was not generated. That means that no SPI transaction was started, so I expect an error has occured. I notice in your code that you ignore most return values. That is a very bad idea, and the first thing you should do is to improve your code so that you check all return values using APP_ERROR_CHECK (particularly the return values from nrf_drv_spi_transfer()). Are there any errors here? What else do you see from debugging?
code was hanged here
while (!nrf_spim_event_check(p_spim, NRF_SPIM_EVENT_END))
How to transfer a data in blocking mode?
To use blocking mode you do not provide a event handler when you initialize the driver using nrf_drv_spi_init(). So in your code, you would change the call to nrf_drv_spi_init() like this:
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, NULL, NULL));
Then call nrf_drv_spi_transfer() like before (but fix it so that you check return values!) and the function will not return until the transaction has completed.
what are the functions i am suppose to enable in sdk_config file ?
and i need sdk_config file
what are the functions i am suppose to enable in sdk_config file ?
and i need sdk_config file
Please refer to example I linked to before to see an example of sdk_config.h when using SPI. The relevant section (copy-pasted from the example) is this:
// <e> SPI_ENABLED - nrf_drv_spi - SPI/SPIM peripheral driver - legacy layer //========================================================== #ifndef SPI_ENABLED #define SPI_ENABLED 1 #endif // <o> SPI_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority // <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice // <0=> 0 (highest) // <1=> 1 // <2=> 2 // <3=> 3 // <4=> 4 // <5=> 5 // <6=> 6 // <7=> 7 #ifndef SPI_DEFAULT_CONFIG_IRQ_PRIORITY #define SPI_DEFAULT_CONFIG_IRQ_PRIORITY 6 #endif // <o> NRF_SPI_DRV_MISO_PULLUP_CFG - MISO PIN pull-up configuration. // <0=> NRF_GPIO_PIN_NOPULL // <1=> NRF_GPIO_PIN_PULLDOWN // <3=> NRF_GPIO_PIN_PULLUP #ifndef NRF_SPI_DRV_MISO_PULLUP_CFG #define NRF_SPI_DRV_MISO_PULLUP_CFG 1 #endif // <e> SPI0_ENABLED - Enable SPI0 instance //========================================================== #ifndef SPI0_ENABLED #define SPI0_ENABLED 1 #endif // <q> SPI0_USE_EASY_DMA - Use EasyDMA #ifndef SPI0_USE_EASY_DMA #define SPI0_USE_EASY_DMA 1 #endif // </e> // <e> SPI1_ENABLED - Enable SPI1 instance //========================================================== #ifndef SPI1_ENABLED #define SPI1_ENABLED 0 #endif // <q> SPI1_USE_EASY_DMA - Use EasyDMA #ifndef SPI1_USE_EASY_DMA #define SPI1_USE_EASY_DMA 1 #endif // </e> // <e> SPI2_ENABLED - Enable SPI2 instance //========================================================== #ifndef SPI2_ENABLED #define SPI2_ENABLED 0 #endif // <q> SPI2_USE_EASY_DMA - Use EasyDMA #ifndef SPI2_USE_EASY_DMA #define SPI2_USE_EASY_DMA 1 #endif // </e> // </e>