nRF52832 寄存器操作 SPI Slave

#include "nrf.h"
#include "nrf_gpio.h"

#define SPIS_IRQ_LEVEL  7

#define SPIS_CSN_PIN    12
#define SPIS_SCK_PIN    11
#define SPIS_MOSI_PIN   13
#define SPIS_MISO_PIN   10

#define SPIS_BUF_SIZE 16

static uint8_t spis_tx_buf[SPIS_BUF_SIZE];
static uint8_t spis_rx_buf[SPIS_BUF_SIZE];

void SPI0_TWI0_IRQHandler(void)
{
    if (NRF_SPIS0->EVENTS_ACQUIRED != 0)
    {
        NRF_SPIS0->EVENTS_ACQUIRED = 0;

        /* TODO: 处理数据 */
        
        NRF_SPIS0->RXD.PTR = (uint32_t)spis_rx_buf;
        NRF_SPIS0->RXD.MAXCNT = SPIS_BUF_SIZE;
        NRF_SPIS0->TXD.PTR = (uint32_t)spis_tx_buf;
        NRF_SPIS0->TXD.MAXCNT = SPIS_BUF_SIZE;

        NRF_SPIS0->TASKS_RELEASE = 1;
    }

    if (NRF_SPIS0->EVENTS_END != 0)
    {
        NRF_SPIS0->EVENTS_END = 0;
    }
}

uint32_t spis_init(void)
{
    nrf_gpio_cfg_input(SPIS_CSN_PIN, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(SPIS_SCK_PIN, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_cfg_input(SPIS_MOSI_PIN, NRF_GPIO_PIN_PULLUP);

    NRF_SPIS0->PSEL.CSN = SPIS_CSN_PIN;
    NRF_SPIS0->PSEL.SCK = SPIS_SCK_PIN;
    NRF_SPIS0->PSEL.MOSI = SPIS_MOSI_PIN;
    NRF_SPIS0->PSEL.MISO = SPIS_MISO_PIN;

    NRF_SPIS0->RXD.PTR = (uint32_t)spis_rx_buf;
    NRF_SPIS0->RXD.MAXCNT = SPIS_BUF_SIZE;
    NRF_SPIS0->TXD.PTR = (uint32_t)spis_tx_buf;
    NRF_SPIS0->TXD.MAXCNT = SPIS_BUF_SIZE;

    NRF_SPIS0->CONFIG = (SPIS_CONFIG_CPOL_ActiveLow << SPIS_CONFIG_CPOL_Pos) | (SPIS_CONFIG_CPHA_Trailing << SPIS_CONFIG_CPHA_Pos);

    NRF_SPIS0->EVENTS_END = 0;
    NRF_SPIS0->EVENTS_ACQUIRED = 0;

    NRF_SPIS0->SHORTS = (SPIS_SHORTS_END_ACQUIRE_Enabled << SPIS_SHORTS_END_ACQUIRE_Pos);

    NRF_SPIS0->INTENSET = (SPIS_INTENSET_END_Enabled << SPIS_INTENSET_END_Pos);
    NRF_SPIS0->INTENSET = (SPIS_INTENCLR_ACQUIRED_Enabled << SPIS_INTENCLR_ACQUIRED_Pos);

    NVIC_SetPriority(SPI0_TWI0_IRQn, SPIS_IRQ_LEVEL);
    NVIC_EnableIRQ(SPI0_TWI0_IRQn);

    NRF_SPIS0->ENABLE = (SPIS_ENABLE_ENABLE_Enabled << SPIS_ENABLE_ENABLE_Pos);

    NRF_SPIS0->TASKS_RELEASE = 1;

    return NRF_SUCCESS;
}

你可能感兴趣的:(arm,蓝牙)