Hi everyone!
#include "nrf_drv_spi.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.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; /**< Flag used to indicate that SPI instance completed the transfer. */
#define TEST_STRING "Nordic"
uint8_t m_tx_buf[6];// = TEST_STRING; /**< TX buffer. */
uint8_t m_rx_buf[6];//[sizeof(TEST_STRING) + 1]; /**< RX buffer. */
uint8_t m_length;// = sizeof(m_tx_buf); /**< Transfer length. */
#define SPI_SS_PIN 9 //29 //
#define SPI_MISO_PIN 4 //28 //
#define SPI_MOSI_PIN 5 //4 //
#define SPI_SCK_PIN 10 //3 //
/**
* @brief SPI user event handler.
* @param event
*/
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 spi_init(void)
{
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_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;
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
}
void spi_send(unsigned char *sendData ,int sendLen , unsigned char *readData, int readLen)
{
memset(m_rx_buf, 0, sendLen + readLen);
spi_xfer_done = false;
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, sendData, sendLen, readData, sendLen + readLen));
}
int main(void)
{
bsp_board_leds_init();
//APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
//NRF_LOG_DEFAULT_BACKENDS_INIT();
//NRF_LOG_INFO("SPI example.");
spi_init();
nrf_delay_ms(47);
m_tx_buf[0] = 0x20;
m_tx_buf[1] = 0x4f;
spi_send( &m_tx_buf[0] ,2,m_rx_buf,0);
while (1)
{
// Reset rx buffer and transfer done flag
NRF_LOG_FLUSH();
bsp_board_led_invert(BSP_BOARD_LED_0);
nrf_delay_ms(47);
m_tx_buf[0] = 0x28 | 0xc0;
spi_send( &m_tx_buf[0] ,1,m_rx_buf,6);
nrf_delay_us(100);
while (!spi_xfer_done){
__WFE();
}
}
}
So that's my test code.
using SPI to resd the register of LIS3DH
when I use the default communication port,Reading and writing is normal.
#define SPI_SS_PIN 29 //
#define SPI_MISO_PIN 28 //
#define SPI_MOSI_PIN 4 //
#define SPI_SCK_PIN 3 //
when I change the communication port like this,there is a error has happened.(app_error_fault_handler)
#define SPI_SS_PIN 9//
#define SPI_MISO_PIN 4//
#define SPI_MOSI_PIN 5//
#define SPI_SCK_PIN 10 //
what can I do?if I must to change the communication port.
thanks!