drv_spi.c 7.3 KB
Newer Older
S
ShermanShao11 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date             Author          Notes
 * 2020-05-22       Sherman         first version
 */

#include <stdint.h>
#include <string.h>
#include "board.h"
#include "drv_spi.h"

#define DBG_LEVEL   DBG_LOG
#include <rtdbg.h>
#define LOG_TAG                "drv.spi"

#ifdef BSP_USING_SPI

static struct nrfx_drv_spi_config spi_config[] =
{
#ifdef BSP_USING_SPI0
    NRFX_SPI0_CONFIG,
#endif

#ifdef BSP_USING_SPI1
    NRFX_SPI1_CONFIG,
#endif

#ifdef BSP_USING_SPI2
    NRFX_SPI2_CONFIG,
#endif

};

static struct nrfx_drv_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])];

//Configure SPI bus pins using the menuconfig
static struct nrfx_drv_spi_pin_config bsp_spi_pin[] = 
{
#ifdef BSP_USING_SPI0
    {
        .sck_pin = BSP_SPI0_SCK_PIN,
        .mosi_pin = BSP_SPI0_MOSI_PIN,
        .miso_pin = BSP_SPI0_MISO_PIN,
        .ss_pin = BSP_SPI0_SS_PIN
    },
#endif

#ifdef BSP_USING_SPI1
    {
        .sck_pin = BSP_SPI1_SCK_PIN,
        .mosi_pin = BSP_SPI1_MOSI_PIN,
        .miso_pin = BSP_SPI1_MISO_PIN,
        .ss_pin = BSP_SPI1_SS_PIN
    },
#endif

#ifdef BSP_USING_SPI2
    {
        .sck_pin = BSP_SPI2_SCK_PIN,
        .mosi_pin = BSP_SPI2_MOSI_PIN,
        .miso_pin = BSP_SPI2_MISO_PIN,
        .ss_pin = BSP_SPI2_SS_PIN
    },
#endif
};


static rt_uint8_t spi_index_find(struct rt_spi_bus *spi_bus)
{
    for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
    {
        if(spi_bus == &spi_bus_obj[i].spi_bus)
            return i;
    }
    return 0xFF;
}

/**
 * spi event handler function
 */
static void spi0_handler(const nrfx_spi_evt_t *p_event, void *p_context)
{
    LOG_I("\nspi0_handler");
}

static void spi1_handler(const nrfx_spi_evt_t *p_event, void *p_context)
{
    LOG_I("\nspi1_handler");
}

static void spi2_handler(const nrfx_spi_evt_t *p_event, void *p_context)
{
    LOG_I("\nspi2_handler");
}
nrfx_spi_evt_handler_t spi_handler[] = {spi0_handler, spi1_handler, spi2_handler};

/**
  * @brief  This function config spi bus
  * @param  device    
  * @param  configuration
  * @retval RT_EOK / RT_ERROR
  */
static rt_err_t spi_configure(struct rt_spi_device *device,
                              struct rt_spi_configuration *configuration)
{
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->bus != RT_NULL);
    RT_ASSERT(device->bus->parent.user_data != RT_NULL);
    RT_ASSERT(configuration != RT_NULL);

    rt_uint8_t index = spi_index_find(device->bus);
    RT_ASSERT(index != 0xFF);

    nrfx_spi_t spi = spi_bus_obj[index].spi;
    nrfx_spi_config_t config = NRFX_SPI_DEFAULT_CONFIG(bsp_spi_pin[index].sck_pin, 
        bsp_spi_pin[index].mosi_pin, bsp_spi_pin[index].miso_pin, bsp_spi_pin[index].ss_pin);

    /* spi config ss pin */
    if(device->user_data != RT_NULL)
    {
        config.ss_pin = (rt_uint8_t)device->user_data;
    }
    /* spi config bit order */
    if(configuration->mode & RT_SPI_MSB)
    {
        config.bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST;
    }
    else
    {
        config.bit_order = NRF_SPI_BIT_ORDER_LSB_FIRST;
    }
    /* spi mode config */
    switch (configuration->mode & RT_SPI_MODE_3 )
    {
    case RT_SPI_MODE_0/* RT_SPI_CPOL:0 , RT_SPI_CPHA:0 */:
        config.mode = NRF_SPI_MODE_0;
        break;
    case RT_SPI_MODE_1/* RT_SPI_CPOL:0 , RT_SPI_CPHA:1 */:
        config.mode = NRF_SPI_MODE_1;
        break;
    case RT_SPI_MODE_2/* RT_SPI_CPOL:1 , RT_SPI_CPHA:0 */:
        config.mode = NRF_SPI_MODE_2;
        break;
    case RT_SPI_MODE_3/* RT_SPI_CPOL:1 , RT_SPI_CPHA:1 */:
        config.mode = NRF_SPI_MODE_3;
        break;
    default:
        LOG_E("spi_configure mode error %x\n",configuration->mode);
        return RT_ERROR;
    }
    /* spi frequency config */
    switch (configuration->max_hz / 1000)
    {
    case 125:
        config.frequency = NRF_SPI_FREQ_125K;
        break;
    case 250:
        config.frequency = NRF_SPI_FREQ_250K;
        break;
    case 500:
        config.frequency = NRF_SPI_FREQ_500K;
        break;
    case 1000:
        config.frequency = NRF_SPI_FREQ_1M;
        break;
    case 2000:
        config.frequency = NRF_SPI_FREQ_2M;
        break;
    case 4000:
        config.frequency = NRF_SPI_FREQ_4M;
        break;
    case 8000:
        config.frequency = NRF_SPI_FREQ_8M;
        break;
    default:
        LOG_E("spi_configure rate error %d\n",configuration->max_hz);
        break;
    }

    rt_memcpy((void*)&spi_bus_obj[index].spi_config, (void*)&config, sizeof(nrfx_spi_config_t));
    nrfx_spi_evt_handler_t handler = RT_NULL;    //spi send callback handler ,default NULL
    void * context = RT_NULL;
    nrfx_err_t nrf_ret = nrfx_spi_init(&spi, &config, handler, context);
    if(NRFX_SUCCESS == nrf_ret)
        return RT_EOK;

    return RT_ERROR;
}

static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->bus != RT_NULL);
    RT_ASSERT(device->bus->parent.user_data != RT_NULL);

    rt_uint8_t index = spi_index_find(device->bus);
    RT_ASSERT(index != 0xFF);

    nrfx_spi_t * p_instance =  &spi_bus_obj[index].spi;
    nrfx_spi_xfer_desc_t p_xfer_desc;
    if(message->send_buf == RT_NULL)
    {
        p_xfer_desc.p_rx_buffer = message->recv_buf;
        p_xfer_desc.rx_length = message->length;

        p_xfer_desc.p_tx_buffer = RT_NULL;
        p_xfer_desc.tx_length = 0;
    }
    else
    {
        p_xfer_desc.p_tx_buffer = message->send_buf;
        p_xfer_desc.tx_length = message->length ;

        p_xfer_desc.p_rx_buffer = RT_NULL;
        p_xfer_desc.rx_length = 0;
    }

    nrfx_err_t nrf_ret = nrfx_spi_xfer(p_instance, &p_xfer_desc, 0);
    if( NRFX_SUCCESS == nrf_ret)
        return message->length; 
    else
        return 0;
}

/* spi bus callback function  */
static const struct rt_spi_ops nrfx_spi_ops =
{
    .configure = spi_configure,
    .xfer = spixfer,
};

/*spi bus init*/
static int rt_hw_spi_bus_init(void)
{
    rt_err_t result = RT_ERROR;
    for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
    {
        spi_bus_obj[i].spi = spi_config[i].spi;
        spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i];   //SPI INSTANCE
        result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &nrfx_spi_ops);
        RT_ASSERT(result == RT_EOK);
    }
    return result;
}

int rt_hw_spi_init(void)
{
    return rt_hw_spi_bus_init();
}
INIT_BOARD_EXPORT(rt_hw_spi_init);

/**
  * Attach the spi device to SPI bus, this function must be used after initialization.
  */
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t ss_pin)
{
    RT_ASSERT(bus_name != RT_NULL);
    RT_ASSERT(device_name != RT_NULL);

    rt_err_t result;
    struct rt_spi_device *spi_device;
    /* attach the device to spi bus*/
    spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
    RT_ASSERT(spi_device != RT_NULL);
    /* initialize the cs pin */
    spi_device->user_data = (void*)ss_pin;
    result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, RT_NULL);
    if (result != RT_EOK)
    {
        LOG_E("%s attach to %s faild, %d", device_name, bus_name, result);
        result = RT_ERROR;
    }
    RT_ASSERT(result == RT_EOK);
    return result;
}

#endif /*BSP_USING_SPI*/