Files
microchip-application-bmd38…/spi.c
T
2023-12-27 14:43:07 +08:00

65 lines
2.3 KiB
C

#include "nrf_gpio.h"
#include "nrf_log.h"
#include "nrf_drv_spi.h"
#define SPI1_CLK_PIN NRF_GPIO_PIN_MAP(0, 13)
#define SPI1_MOSI_PIN NRF_GPIO_PIN_MAP(0, 14)
#define SPI2_CLK_PIN NRF_GPIO_PIN_MAP(0, 12)
#define SPI2_MOSI_PIN NRF_GPIO_PIN_MAP(0, 7)
#define SPI2_MISO_PIN NRF_GPIO_PIN_MAP(1, 9)
static const nrf_drv_spi_t spim1 = NRF_DRV_SPI_INSTANCE(1); /**< SPI instance. */
static const nrf_drv_spi_t spim2 = NRF_DRV_SPI_INSTANCE(2); /**< SPI instance. */
void spi_init(void)
{
nrf_drv_spi_config_t spi1_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi1_config.ss_pin = NRF_DRV_SPI_PIN_NOT_USED;
spi1_config.miso_pin = NRF_DRV_SPI_PIN_NOT_USED;
spi1_config.mosi_pin = SPI1_MOSI_PIN;
spi1_config.sck_pin = SPI1_CLK_PIN;
spi1_config.mode = NRF_DRV_SPI_MODE_1;
spi1_config.frequency = NRF_DRV_SPI_FREQ_1M;
APP_ERROR_CHECK(nrf_drv_spi_init(&spim1, &spi1_config, NULL, NULL));
nrf_drv_spi_config_t spi2_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi2_config.ss_pin = NRF_DRV_SPI_PIN_NOT_USED;
spi2_config.miso_pin = SPI2_MISO_PIN;
spi2_config.mosi_pin = SPI2_MOSI_PIN;
spi2_config.sck_pin = SPI2_CLK_PIN;
spi2_config.mode = NRF_DRV_SPI_MODE_1;
spi2_config.frequency = NRF_DRV_SPI_FREQ_1M;
APP_ERROR_CHECK(nrf_drv_spi_init(&spim2, &spi2_config, NULL, NULL));
}
void spi1_write(uint8_t *p_tx_buffer, uint8_t tx_buffer_length)
{
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spim1, p_tx_buffer, tx_buffer_length, NULL, 0));
// NRF_LOG_HEXDUMP_INFO(p_tx_buffer, tx_buffer_length);
}
static void virtual_data(uint8_t *p_rx_buf, uint8_t rx_buffer_length)
{
uint8_t virtual_data_buff[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
if (rx_buffer_length == 0)
return;
memcpy(p_rx_buf, virtual_data_buff, rx_buffer_length);
}
void spi2_write(uint8_t *p_tx_buffer, uint8_t tx_buffer_length, uint8_t *p_rx_buf, uint8_t rx_buffer_length)
{
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spim2, p_tx_buffer, tx_buffer_length, p_rx_buf, rx_buffer_length));
NRF_LOG_INFO("spi(W)");
NRF_LOG_HEXDUMP_INFO(p_tx_buffer, tx_buffer_length);
// virtual_data(p_rx_buf, rx_buffer_length);
if (rx_buffer_length > 0)
{
NRF_LOG_INFO("spi(R)");
NRF_LOG_HEXDUMP_INFO(p_rx_buf, rx_buffer_length);
}
}