Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
spi_api_multibyte.c@1:58f0712d9aa2, 2017-05-31 (annotated)
- Committer:
- tsungta
- Date:
- Wed May 31 07:39:37 2017 +0000
- Revision:
- 1:58f0712d9aa2
- Parent:
- 0:b7415ae44dac
- Child:
- 2:c520d7c7739d
Able to receive multiple bytes in single CS cycle;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tsungta | 0:b7415ae44dac | 1 | /* |
tsungta | 0:b7415ae44dac | 2 | * Copyright (c) 2013 Nordic Semiconductor ASA |
tsungta | 0:b7415ae44dac | 3 | * All rights reserved. |
tsungta | 0:b7415ae44dac | 4 | * |
tsungta | 0:b7415ae44dac | 5 | * Redistribution and use in source and binary forms, with or without modification, |
tsungta | 0:b7415ae44dac | 6 | * are permitted provided that the following conditions are met: |
tsungta | 0:b7415ae44dac | 7 | * |
tsungta | 0:b7415ae44dac | 8 | * 1. Redistributions of source code must retain the above copyright notice, this list |
tsungta | 0:b7415ae44dac | 9 | * of conditions and the following disclaimer. |
tsungta | 0:b7415ae44dac | 10 | * |
tsungta | 0:b7415ae44dac | 11 | * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA |
tsungta | 0:b7415ae44dac | 12 | * integrated circuit in a product or a software update for such product, must reproduce |
tsungta | 0:b7415ae44dac | 13 | * the above copyright notice, this list of conditions and the following disclaimer in |
tsungta | 0:b7415ae44dac | 14 | * the documentation and/or other materials provided with the distribution. |
tsungta | 0:b7415ae44dac | 15 | * |
tsungta | 0:b7415ae44dac | 16 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be |
tsungta | 0:b7415ae44dac | 17 | * used to endorse or promote products derived from this software without specific prior |
tsungta | 0:b7415ae44dac | 18 | * written permission. |
tsungta | 0:b7415ae44dac | 19 | * |
tsungta | 0:b7415ae44dac | 20 | * 4. This software, with or without modification, must only be used with a |
tsungta | 0:b7415ae44dac | 21 | * Nordic Semiconductor ASA integrated circuit. |
tsungta | 0:b7415ae44dac | 22 | * |
tsungta | 0:b7415ae44dac | 23 | * 5. Any software provided in binary or object form under this license must not be reverse |
tsungta | 0:b7415ae44dac | 24 | * engineered, decompiled, modified and/or disassembled. |
tsungta | 0:b7415ae44dac | 25 | * |
tsungta | 0:b7415ae44dac | 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
tsungta | 0:b7415ae44dac | 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
tsungta | 0:b7415ae44dac | 28 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
tsungta | 0:b7415ae44dac | 29 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
tsungta | 0:b7415ae44dac | 30 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
tsungta | 0:b7415ae44dac | 31 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
tsungta | 0:b7415ae44dac | 32 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
tsungta | 0:b7415ae44dac | 33 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
tsungta | 0:b7415ae44dac | 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
tsungta | 0:b7415ae44dac | 35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
tsungta | 0:b7415ae44dac | 36 | * |
tsungta | 0:b7415ae44dac | 37 | */ |
tsungta | 0:b7415ae44dac | 38 | |
tsungta | 0:b7415ae44dac | 39 | |
tsungta | 0:b7415ae44dac | 40 | #include "spi_api_multibyte.h" |
tsungta | 0:b7415ae44dac | 41 | |
tsungta | 0:b7415ae44dac | 42 | #if DEVICE_SPI |
tsungta | 0:b7415ae44dac | 43 | |
tsungta | 0:b7415ae44dac | 44 | #include "cmsis.h" |
tsungta | 0:b7415ae44dac | 45 | #include "pinmap.h" |
tsungta | 0:b7415ae44dac | 46 | #include "mbed_assert.h" |
tsungta | 0:b7415ae44dac | 47 | #include "mbed_error.h" |
tsungta | 0:b7415ae44dac | 48 | #include "nrf_drv_spi.h" |
tsungta | 0:b7415ae44dac | 49 | #include "nrf_drv_spis.h" |
tsungta | 0:b7415ae44dac | 50 | #include "app_util_platform.h" |
tsungta | 0:b7415ae44dac | 51 | |
tsungta | 0:b7415ae44dac | 52 | #if DEVICE_SPI_ASYNCH |
tsungta | 0:b7415ae44dac | 53 | #define SPI_IDX(obj) ((obj)->spi.spi_idx) |
tsungta | 0:b7415ae44dac | 54 | #else |
tsungta | 0:b7415ae44dac | 55 | #define SPI_IDX(obj) ((obj)->spi_idx) |
tsungta | 0:b7415ae44dac | 56 | #endif |
tsungta | 0:b7415ae44dac | 57 | #define SPI_INFO(obj) (&m_spi_info[SPI_IDX(obj)]) |
tsungta | 0:b7415ae44dac | 58 | #define MASTER_INST(obj) (&m_instances[SPI_IDX(obj)].master) |
tsungta | 0:b7415ae44dac | 59 | #define SLAVE_INST(obj) (&m_instances[SPI_IDX(obj)].slave) |
tsungta | 0:b7415ae44dac | 60 | |
tsungta | 0:b7415ae44dac | 61 | typedef struct { |
tsungta | 0:b7415ae44dac | 62 | bool initialized; |
tsungta | 0:b7415ae44dac | 63 | bool master; |
tsungta | 0:b7415ae44dac | 64 | uint8_t sck_pin; |
tsungta | 0:b7415ae44dac | 65 | uint8_t mosi_pin; |
tsungta | 0:b7415ae44dac | 66 | uint8_t miso_pin; |
tsungta | 0:b7415ae44dac | 67 | uint8_t ss_pin; |
tsungta | 0:b7415ae44dac | 68 | uint8_t spi_mode; |
tsungta | 0:b7415ae44dac | 69 | nrf_drv_spi_frequency_t frequency; |
tsungta | 0:b7415ae44dac | 70 | volatile union { |
tsungta | 0:b7415ae44dac | 71 | bool busy; // master |
tsungta | 0:b7415ae44dac | 72 | bool readable; // slave |
tsungta | 0:b7415ae44dac | 73 | } flag; |
tsungta | 0:b7415ae44dac | 74 | //volatile uint8_t tx_buf; |
tsungta | 0:b7415ae44dac | 75 | //volatile uint8_t rx_buf; |
tsungta | 0:b7415ae44dac | 76 | |
tsungta | 0:b7415ae44dac | 77 | #if DEVICE_SPI_ASYNCH |
tsungta | 0:b7415ae44dac | 78 | uint32_t handler; |
tsungta | 0:b7415ae44dac | 79 | uint32_t event; |
tsungta | 0:b7415ae44dac | 80 | #endif |
tsungta | 0:b7415ae44dac | 81 | } spi_info_t; |
tsungta | 0:b7415ae44dac | 82 | |
tsungta | 1:58f0712d9aa2 | 83 | // #define TEST_STRING "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" //Tsungta, handle 32 bytes per SPI CS cycle |
tsungta | 1:58f0712d9aa2 | 84 | #define TEST_STRING "012" //Tsungta, handle 32 bytes per SPI CS cycle |
tsungta | 0:b7415ae44dac | 85 | uint8_t m_tx_buf[] = TEST_STRING; /**< TX buffer. */ |
tsungta | 0:b7415ae44dac | 86 | uint8_t m_rx_buf[sizeof(TEST_STRING)+1]; /**< RX buffer. */ |
tsungta | 0:b7415ae44dac | 87 | const uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */ |
tsungta | 0:b7415ae44dac | 88 | |
tsungta | 0:b7415ae44dac | 89 | static spi_info_t m_spi_info[SPI_COUNT]; |
tsungta | 0:b7415ae44dac | 90 | |
tsungta | 0:b7415ae44dac | 91 | typedef struct { |
tsungta | 0:b7415ae44dac | 92 | nrf_drv_spi_t master; |
tsungta | 0:b7415ae44dac | 93 | nrf_drv_spis_t slave; |
tsungta | 0:b7415ae44dac | 94 | } sdk_driver_instances_t; |
tsungta | 0:b7415ae44dac | 95 | |
tsungta | 0:b7415ae44dac | 96 | void SPI0_TWI0_IRQHandler(void); |
tsungta | 0:b7415ae44dac | 97 | void SPI1_TWI1_IRQHandler(void); |
tsungta | 0:b7415ae44dac | 98 | void SPIM2_SPIS2_SPI2_IRQHandler(void); |
tsungta | 0:b7415ae44dac | 99 | |
tsungta | 0:b7415ae44dac | 100 | static const peripheral_handler_desc_t spi_hanlder_desc[SPI_COUNT] = { |
tsungta | 0:b7415ae44dac | 101 | #if SPI0_ENABLED |
tsungta | 0:b7415ae44dac | 102 | { |
tsungta | 0:b7415ae44dac | 103 | SPIS0_IRQ, |
tsungta | 0:b7415ae44dac | 104 | (uint32_t) SPI0_TWI0_IRQHandler |
tsungta | 0:b7415ae44dac | 105 | }, |
tsungta | 0:b7415ae44dac | 106 | #endif |
tsungta | 0:b7415ae44dac | 107 | #if SPI1_ENABLED |
tsungta | 0:b7415ae44dac | 108 | { |
tsungta | 0:b7415ae44dac | 109 | SPIS1_IRQ, |
tsungta | 0:b7415ae44dac | 110 | (uint32_t) SPI1_TWI1_IRQHandler |
tsungta | 0:b7415ae44dac | 111 | }, |
tsungta | 0:b7415ae44dac | 112 | #endif |
tsungta | 0:b7415ae44dac | 113 | #if SPI2_ENABLED |
tsungta | 0:b7415ae44dac | 114 | { |
tsungta | 0:b7415ae44dac | 115 | SPIS2_IRQ, |
tsungta | 0:b7415ae44dac | 116 | (uint32_t) SPIM2_SPIS2_SPI2_IRQHandler |
tsungta | 0:b7415ae44dac | 117 | }, |
tsungta | 0:b7415ae44dac | 118 | #endif |
tsungta | 0:b7415ae44dac | 119 | }; |
tsungta | 0:b7415ae44dac | 120 | |
tsungta | 0:b7415ae44dac | 121 | |
tsungta | 0:b7415ae44dac | 122 | static sdk_driver_instances_t m_instances[SPI_COUNT] = { |
tsungta | 0:b7415ae44dac | 123 | #if SPI0_ENABLED |
tsungta | 0:b7415ae44dac | 124 | { |
tsungta | 0:b7415ae44dac | 125 | NRF_DRV_SPI_INSTANCE(0), |
tsungta | 0:b7415ae44dac | 126 | NRF_DRV_SPIS_INSTANCE(0) |
tsungta | 0:b7415ae44dac | 127 | }, |
tsungta | 0:b7415ae44dac | 128 | #endif |
tsungta | 0:b7415ae44dac | 129 | #if SPI1_ENABLED |
tsungta | 0:b7415ae44dac | 130 | { |
tsungta | 0:b7415ae44dac | 131 | NRF_DRV_SPI_INSTANCE(1), |
tsungta | 0:b7415ae44dac | 132 | NRF_DRV_SPIS_INSTANCE(1) |
tsungta | 0:b7415ae44dac | 133 | }, |
tsungta | 0:b7415ae44dac | 134 | #endif |
tsungta | 0:b7415ae44dac | 135 | #if SPI2_ENABLED |
tsungta | 0:b7415ae44dac | 136 | { |
tsungta | 0:b7415ae44dac | 137 | NRF_DRV_SPI_INSTANCE(2), |
tsungta | 0:b7415ae44dac | 138 | NRF_DRV_SPIS_INSTANCE(2) |
tsungta | 0:b7415ae44dac | 139 | }, |
tsungta | 0:b7415ae44dac | 140 | #endif |
tsungta | 0:b7415ae44dac | 141 | }; |
tsungta | 0:b7415ae44dac | 142 | |
tsungta | 0:b7415ae44dac | 143 | static void master_event_handler(uint8_t spi_idx, |
tsungta | 0:b7415ae44dac | 144 | nrf_drv_spi_evt_t const *p_event) |
tsungta | 0:b7415ae44dac | 145 | { |
tsungta | 0:b7415ae44dac | 146 | spi_info_t *p_spi_info = &m_spi_info[spi_idx]; |
tsungta | 0:b7415ae44dac | 147 | |
tsungta | 0:b7415ae44dac | 148 | if (p_event->type == NRF_DRV_SPI_EVENT_DONE) { |
tsungta | 0:b7415ae44dac | 149 | p_spi_info->flag.busy = false; |
tsungta | 0:b7415ae44dac | 150 | if (p_spi_info->handler) { |
tsungta | 0:b7415ae44dac | 151 | void (*handler)(void) = (void (*)(void))p_spi_info->handler; |
tsungta | 0:b7415ae44dac | 152 | p_spi_info->handler = 0; |
tsungta | 0:b7415ae44dac | 153 | handler(); |
tsungta | 0:b7415ae44dac | 154 | } |
tsungta | 0:b7415ae44dac | 155 | } |
tsungta | 0:b7415ae44dac | 156 | } |
tsungta | 0:b7415ae44dac | 157 | #define MASTER_EVENT_HANDLER(idx) \ |
tsungta | 0:b7415ae44dac | 158 | static void master_event_handler_##idx(nrf_drv_spi_evt_t const *p_event) { \ |
tsungta | 0:b7415ae44dac | 159 | master_event_handler(SPI##idx##_INSTANCE_INDEX, p_event); \ |
tsungta | 0:b7415ae44dac | 160 | } |
tsungta | 0:b7415ae44dac | 161 | #if SPI0_ENABLED |
tsungta | 0:b7415ae44dac | 162 | MASTER_EVENT_HANDLER(0) |
tsungta | 0:b7415ae44dac | 163 | #endif |
tsungta | 0:b7415ae44dac | 164 | #if SPI1_ENABLED |
tsungta | 0:b7415ae44dac | 165 | MASTER_EVENT_HANDLER(1) |
tsungta | 0:b7415ae44dac | 166 | #endif |
tsungta | 0:b7415ae44dac | 167 | #if SPI2_ENABLED |
tsungta | 0:b7415ae44dac | 168 | MASTER_EVENT_HANDLER(2) |
tsungta | 0:b7415ae44dac | 169 | #endif |
tsungta | 0:b7415ae44dac | 170 | |
tsungta | 0:b7415ae44dac | 171 | static nrf_drv_spi_handler_t const m_master_event_handlers[SPI_COUNT] = { |
tsungta | 0:b7415ae44dac | 172 | #if SPI0_ENABLED |
tsungta | 0:b7415ae44dac | 173 | master_event_handler_0, |
tsungta | 0:b7415ae44dac | 174 | #endif |
tsungta | 0:b7415ae44dac | 175 | #if SPI1_ENABLED |
tsungta | 0:b7415ae44dac | 176 | master_event_handler_1, |
tsungta | 0:b7415ae44dac | 177 | #endif |
tsungta | 0:b7415ae44dac | 178 | #if SPI2_ENABLED |
tsungta | 0:b7415ae44dac | 179 | master_event_handler_2, |
tsungta | 0:b7415ae44dac | 180 | #endif |
tsungta | 0:b7415ae44dac | 181 | }; |
tsungta | 0:b7415ae44dac | 182 | |
tsungta | 0:b7415ae44dac | 183 | |
tsungta | 0:b7415ae44dac | 184 | static void slave_event_handler(uint8_t spi_idx, |
tsungta | 0:b7415ae44dac | 185 | nrf_drv_spis_event_t event) |
tsungta | 0:b7415ae44dac | 186 | { |
tsungta | 0:b7415ae44dac | 187 | spi_info_t *p_spi_info = &m_spi_info[spi_idx]; |
tsungta | 0:b7415ae44dac | 188 | |
tsungta | 0:b7415ae44dac | 189 | if (event.evt_type == NRF_DRV_SPIS_XFER_DONE) { |
tsungta | 0:b7415ae44dac | 190 | // Signal that there is some data received that could be read. |
tsungta | 1:58f0712d9aa2 | 191 | if(event.rx_amount == 3)//Tsungta |
tsungta | 1:58f0712d9aa2 | 192 | p_spi_info->flag.readable = true; |
tsungta | 0:b7415ae44dac | 193 | |
tsungta | 0:b7415ae44dac | 194 | // And prepare for the next transfer. |
tsungta | 0:b7415ae44dac | 195 | // Previous data set in 'spi_slave_write' (if any) has been transmitted, |
tsungta | 0:b7415ae44dac | 196 | // now use the default one, until some new is set by 'spi_slave_write'. |
tsungta | 0:b7415ae44dac | 197 | //m_tx_buf = NRF_DRV_SPIS_DEFAULT_ORC; |
tsungta | 0:b7415ae44dac | 198 | nrf_drv_spis_buffers_set(&m_instances[spi_idx].slave, |
tsungta | 0:b7415ae44dac | 199 | m_tx_buf, m_length, m_rx_buf, m_length); |
tsungta | 0:b7415ae44dac | 200 | } |
tsungta | 0:b7415ae44dac | 201 | } |
tsungta | 0:b7415ae44dac | 202 | #define SLAVE_EVENT_HANDLER(idx) \ |
tsungta | 0:b7415ae44dac | 203 | static void slave_event_handler_##idx(nrf_drv_spis_event_t event) { \ |
tsungta | 0:b7415ae44dac | 204 | slave_event_handler(SPIS##idx##_INSTANCE_INDEX, event); \ |
tsungta | 0:b7415ae44dac | 205 | } |
tsungta | 0:b7415ae44dac | 206 | #if SPIS0_ENABLED |
tsungta | 0:b7415ae44dac | 207 | SLAVE_EVENT_HANDLER(0) |
tsungta | 0:b7415ae44dac | 208 | #endif |
tsungta | 0:b7415ae44dac | 209 | #if SPIS1_ENABLED |
tsungta | 0:b7415ae44dac | 210 | SLAVE_EVENT_HANDLER(1) |
tsungta | 0:b7415ae44dac | 211 | #endif |
tsungta | 0:b7415ae44dac | 212 | #if SPIS2_ENABLED |
tsungta | 0:b7415ae44dac | 213 | SLAVE_EVENT_HANDLER(2) |
tsungta | 0:b7415ae44dac | 214 | #endif |
tsungta | 0:b7415ae44dac | 215 | |
tsungta | 0:b7415ae44dac | 216 | static nrf_drv_spis_event_handler_t const m_slave_event_handlers[SPIS_COUNT] = { |
tsungta | 0:b7415ae44dac | 217 | #if SPIS0_ENABLED |
tsungta | 0:b7415ae44dac | 218 | slave_event_handler_0, |
tsungta | 0:b7415ae44dac | 219 | #endif |
tsungta | 0:b7415ae44dac | 220 | #if SPIS1_ENABLED |
tsungta | 0:b7415ae44dac | 221 | slave_event_handler_1, |
tsungta | 0:b7415ae44dac | 222 | #endif |
tsungta | 0:b7415ae44dac | 223 | #if SPIS2_ENABLED |
tsungta | 0:b7415ae44dac | 224 | slave_event_handler_2, |
tsungta | 0:b7415ae44dac | 225 | #endif |
tsungta | 0:b7415ae44dac | 226 | }; |
tsungta | 0:b7415ae44dac | 227 | |
tsungta | 0:b7415ae44dac | 228 | static void prepare_master_config(nrf_drv_spi_config_t *p_config, |
tsungta | 0:b7415ae44dac | 229 | spi_info_t const *p_spi_info) |
tsungta | 0:b7415ae44dac | 230 | { |
tsungta | 0:b7415ae44dac | 231 | p_config->sck_pin = p_spi_info->sck_pin; |
tsungta | 0:b7415ae44dac | 232 | p_config->mosi_pin = p_spi_info->mosi_pin; |
tsungta | 0:b7415ae44dac | 233 | p_config->miso_pin = p_spi_info->miso_pin; |
tsungta | 0:b7415ae44dac | 234 | p_config->ss_pin = p_spi_info->ss_pin; |
tsungta | 0:b7415ae44dac | 235 | p_config->frequency = p_spi_info->frequency; |
tsungta | 0:b7415ae44dac | 236 | p_config->mode = (nrf_drv_spi_mode_t)p_spi_info->spi_mode; |
tsungta | 0:b7415ae44dac | 237 | |
tsungta | 0:b7415ae44dac | 238 | p_config->irq_priority = SPI1_CONFIG_IRQ_PRIORITY; |
tsungta | 0:b7415ae44dac | 239 | p_config->orc = 0xFF; |
tsungta | 0:b7415ae44dac | 240 | p_config->bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST; |
tsungta | 0:b7415ae44dac | 241 | } |
tsungta | 0:b7415ae44dac | 242 | |
tsungta | 0:b7415ae44dac | 243 | static void prepare_slave_config(nrf_drv_spis_config_t *p_config, |
tsungta | 0:b7415ae44dac | 244 | spi_info_t const *p_spi_info) |
tsungta | 0:b7415ae44dac | 245 | { |
tsungta | 0:b7415ae44dac | 246 | p_config->sck_pin = p_spi_info->sck_pin; |
tsungta | 0:b7415ae44dac | 247 | p_config->mosi_pin = p_spi_info->mosi_pin; |
tsungta | 0:b7415ae44dac | 248 | p_config->miso_pin = p_spi_info->miso_pin; |
tsungta | 0:b7415ae44dac | 249 | p_config->csn_pin = p_spi_info->ss_pin; |
tsungta | 0:b7415ae44dac | 250 | p_config->mode = (nrf_drv_spis_mode_t)p_spi_info->spi_mode; |
tsungta | 0:b7415ae44dac | 251 | |
tsungta | 0:b7415ae44dac | 252 | p_config->irq_priority = SPIS1_CONFIG_IRQ_PRIORITY; |
tsungta | 0:b7415ae44dac | 253 | p_config->orc = NRF_DRV_SPIS_DEFAULT_ORC; |
tsungta | 0:b7415ae44dac | 254 | p_config->def = NRF_DRV_SPIS_DEFAULT_DEF; |
tsungta | 0:b7415ae44dac | 255 | p_config->bit_order = NRF_DRV_SPIS_BIT_ORDER_MSB_FIRST; |
tsungta | 0:b7415ae44dac | 256 | p_config->csn_pullup = NRF_DRV_SPIS_DEFAULT_CSN_PULLUP; |
tsungta | 0:b7415ae44dac | 257 | p_config->miso_drive = NRF_DRV_SPIS_DEFAULT_MISO_DRIVE; |
tsungta | 0:b7415ae44dac | 258 | } |
tsungta | 0:b7415ae44dac | 259 | |
tsungta | 0:b7415ae44dac | 260 | void spi_init_multibyte(spi_mb_t *obj, |
tsungta | 0:b7415ae44dac | 261 | PinName mosi, PinName miso, PinName sclk, PinName ssel) |
tsungta | 0:b7415ae44dac | 262 | { |
tsungta | 0:b7415ae44dac | 263 | int i; |
tsungta | 0:b7415ae44dac | 264 | for (i = 0; i < SPI_COUNT; ++i) { |
tsungta | 0:b7415ae44dac | 265 | spi_info_t *p_spi_info = &m_spi_info[i]; |
tsungta | 0:b7415ae44dac | 266 | if (!p_spi_info->initialized) { |
tsungta | 0:b7415ae44dac | 267 | |
tsungta | 0:b7415ae44dac | 268 | NVIC_SetVector(spi_hanlder_desc[i].IRQn, spi_hanlder_desc[i].vector); |
tsungta | 0:b7415ae44dac | 269 | |
tsungta | 0:b7415ae44dac | 270 | p_spi_info->sck_pin = (uint8_t)sclk; |
tsungta | 0:b7415ae44dac | 271 | p_spi_info->mosi_pin = (mosi != NC) ? |
tsungta | 0:b7415ae44dac | 272 | (uint8_t)mosi : NRF_DRV_SPI_PIN_NOT_USED; |
tsungta | 0:b7415ae44dac | 273 | p_spi_info->miso_pin = (miso != NC) ? |
tsungta | 0:b7415ae44dac | 274 | (uint8_t)miso : NRF_DRV_SPI_PIN_NOT_USED; |
tsungta | 0:b7415ae44dac | 275 | p_spi_info->ss_pin = (ssel != NC) ? |
tsungta | 0:b7415ae44dac | 276 | (uint8_t)ssel : NRF_DRV_SPI_PIN_NOT_USED; |
tsungta | 0:b7415ae44dac | 277 | p_spi_info->spi_mode = (uint8_t)NRF_DRV_SPI_MODE_0; |
tsungta | 0:b7415ae44dac | 278 | p_spi_info->frequency = NRF_DRV_SPI_FREQ_1M; |
tsungta | 0:b7415ae44dac | 279 | |
tsungta | 0:b7415ae44dac | 280 | // By default each SPI instance is initialized to work as a master. |
tsungta | 0:b7415ae44dac | 281 | // Should the slave mode be used, the instance will be reconfigured |
tsungta | 0:b7415ae44dac | 282 | // appropriately in 'spi_format'. |
tsungta | 0:b7415ae44dac | 283 | nrf_drv_spi_config_t config; |
tsungta | 0:b7415ae44dac | 284 | prepare_master_config(&config, p_spi_info); |
tsungta | 0:b7415ae44dac | 285 | |
tsungta | 0:b7415ae44dac | 286 | nrf_drv_spi_t const *p_spi = &m_instances[i].master; |
tsungta | 0:b7415ae44dac | 287 | ret_code_t ret_code = nrf_drv_spi_init(p_spi, |
tsungta | 0:b7415ae44dac | 288 | &config, m_master_event_handlers[i]); |
tsungta | 0:b7415ae44dac | 289 | if (ret_code == NRF_SUCCESS) { |
tsungta | 0:b7415ae44dac | 290 | p_spi_info->initialized = true; |
tsungta | 0:b7415ae44dac | 291 | p_spi_info->master = true; |
tsungta | 0:b7415ae44dac | 292 | p_spi_info->flag.busy = false; |
tsungta | 0:b7415ae44dac | 293 | #if DEVICE_SPI_ASYNCH |
tsungta | 0:b7415ae44dac | 294 | p_spi_info->handler = 0; |
tsungta | 0:b7415ae44dac | 295 | #endif |
tsungta | 0:b7415ae44dac | 296 | SPI_IDX(obj) = i; |
tsungta | 0:b7415ae44dac | 297 | |
tsungta | 0:b7415ae44dac | 298 | return; |
tsungta | 0:b7415ae44dac | 299 | } |
tsungta | 0:b7415ae44dac | 300 | } |
tsungta | 0:b7415ae44dac | 301 | } |
tsungta | 0:b7415ae44dac | 302 | |
tsungta | 0:b7415ae44dac | 303 | // No available peripheral |
tsungta | 0:b7415ae44dac | 304 | error("No available SPI peripheral\r\n"); |
tsungta | 0:b7415ae44dac | 305 | } |
tsungta | 0:b7415ae44dac | 306 | |
tsungta | 0:b7415ae44dac | 307 | void spi_free_multibyte(spi_mb_t *obj) |
tsungta | 0:b7415ae44dac | 308 | { |
tsungta | 0:b7415ae44dac | 309 | spi_info_t *p_spi_info = SPI_INFO(obj); |
tsungta | 0:b7415ae44dac | 310 | if (p_spi_info->master) { |
tsungta | 0:b7415ae44dac | 311 | nrf_drv_spi_uninit(MASTER_INST(obj)); |
tsungta | 0:b7415ae44dac | 312 | } |
tsungta | 0:b7415ae44dac | 313 | else { |
tsungta | 0:b7415ae44dac | 314 | nrf_drv_spis_uninit(SLAVE_INST(obj)); |
tsungta | 0:b7415ae44dac | 315 | } |
tsungta | 0:b7415ae44dac | 316 | p_spi_info->initialized = false; |
tsungta | 0:b7415ae44dac | 317 | } |
tsungta | 0:b7415ae44dac | 318 | |
tsungta | 0:b7415ae44dac | 319 | int spi_busy_multibyte(spi_mb_t *obj) |
tsungta | 0:b7415ae44dac | 320 | { |
tsungta | 0:b7415ae44dac | 321 | return (int)(SPI_INFO(obj)->flag.busy); |
tsungta | 0:b7415ae44dac | 322 | } |
tsungta | 0:b7415ae44dac | 323 | |
tsungta | 0:b7415ae44dac | 324 | void spi_format_multibyte(spi_mb_t *obj, int bits, int mode, int slave) |
tsungta | 0:b7415ae44dac | 325 | { |
tsungta | 0:b7415ae44dac | 326 | if (bits != 8) { |
tsungta | 0:b7415ae44dac | 327 | error("Only 8-bits SPI is supported\r\n"); |
tsungta | 0:b7415ae44dac | 328 | } |
tsungta | 0:b7415ae44dac | 329 | if (mode > 3) { |
tsungta | 0:b7415ae44dac | 330 | error("SPI format error\r\n"); |
tsungta | 0:b7415ae44dac | 331 | } |
tsungta | 0:b7415ae44dac | 332 | |
tsungta | 0:b7415ae44dac | 333 | spi_info_t *p_spi_info = SPI_INFO(obj); |
tsungta | 0:b7415ae44dac | 334 | |
tsungta | 0:b7415ae44dac | 335 | if (slave) |
tsungta | 0:b7415ae44dac | 336 | { |
tsungta | 0:b7415ae44dac | 337 | nrf_drv_spis_mode_t spi_modes[4] = { |
tsungta | 0:b7415ae44dac | 338 | NRF_DRV_SPIS_MODE_0, |
tsungta | 0:b7415ae44dac | 339 | NRF_DRV_SPIS_MODE_1, |
tsungta | 0:b7415ae44dac | 340 | NRF_DRV_SPIS_MODE_2, |
tsungta | 0:b7415ae44dac | 341 | NRF_DRV_SPIS_MODE_3, |
tsungta | 0:b7415ae44dac | 342 | }; |
tsungta | 0:b7415ae44dac | 343 | nrf_drv_spis_mode_t new_mode = spi_modes[mode]; |
tsungta | 0:b7415ae44dac | 344 | |
tsungta | 0:b7415ae44dac | 345 | // If the peripheral is currently working as a master, the SDK driver |
tsungta | 0:b7415ae44dac | 346 | // it uses needs to be switched from SPI to SPIS. |
tsungta | 0:b7415ae44dac | 347 | if (p_spi_info->master) { |
tsungta | 0:b7415ae44dac | 348 | nrf_drv_spi_uninit(MASTER_INST(obj)); |
tsungta | 0:b7415ae44dac | 349 | } |
tsungta | 0:b7415ae44dac | 350 | // I the SPI mode has to be changed, the SDK's SPIS driver needs to be |
tsungta | 0:b7415ae44dac | 351 | // re-initialized (there is no other way to change its configuration). |
tsungta | 0:b7415ae44dac | 352 | else if (p_spi_info->spi_mode != (uint8_t)new_mode) { |
tsungta | 0:b7415ae44dac | 353 | nrf_drv_spis_uninit(SLAVE_INST(obj)); |
tsungta | 0:b7415ae44dac | 354 | } |
tsungta | 0:b7415ae44dac | 355 | else { |
tsungta | 0:b7415ae44dac | 356 | return; |
tsungta | 0:b7415ae44dac | 357 | } |
tsungta | 0:b7415ae44dac | 358 | |
tsungta | 0:b7415ae44dac | 359 | p_spi_info->spi_mode = (uint8_t)new_mode; |
tsungta | 0:b7415ae44dac | 360 | p_spi_info->master = false; |
tsungta | 0:b7415ae44dac | 361 | p_spi_info->flag.readable = false; |
tsungta | 0:b7415ae44dac | 362 | |
tsungta | 0:b7415ae44dac | 363 | // Initialize SDK's SPIS driver with the new configuration. |
tsungta | 0:b7415ae44dac | 364 | nrf_drv_spis_config_t config; |
tsungta | 0:b7415ae44dac | 365 | prepare_slave_config(&config, p_spi_info); |
tsungta | 0:b7415ae44dac | 366 | (void)nrf_drv_spis_init(SLAVE_INST(obj), &config, |
tsungta | 0:b7415ae44dac | 367 | m_slave_event_handlers[SPI_IDX(obj)]); |
tsungta | 0:b7415ae44dac | 368 | |
tsungta | 0:b7415ae44dac | 369 | // Prepare the slave for transfer. |
tsungta | 0:b7415ae44dac | 370 | //m_tx_buf = NRF_DRV_SPIS_DEFAULT_ORC; |
tsungta | 0:b7415ae44dac | 371 | nrf_drv_spis_buffers_set(SLAVE_INST(obj), |
tsungta | 0:b7415ae44dac | 372 | m_tx_buf, m_length, m_rx_buf, m_length); |
tsungta | 0:b7415ae44dac | 373 | } |
tsungta | 0:b7415ae44dac | 374 | else // master |
tsungta | 0:b7415ae44dac | 375 | { |
tsungta | 0:b7415ae44dac | 376 | nrf_drv_spi_mode_t spi_modes[4] = { |
tsungta | 0:b7415ae44dac | 377 | NRF_DRV_SPI_MODE_0, |
tsungta | 0:b7415ae44dac | 378 | NRF_DRV_SPI_MODE_1, |
tsungta | 0:b7415ae44dac | 379 | NRF_DRV_SPI_MODE_2, |
tsungta | 0:b7415ae44dac | 380 | NRF_DRV_SPI_MODE_3, |
tsungta | 0:b7415ae44dac | 381 | }; |
tsungta | 0:b7415ae44dac | 382 | nrf_drv_spi_mode_t new_mode = spi_modes[mode]; |
tsungta | 0:b7415ae44dac | 383 | |
tsungta | 0:b7415ae44dac | 384 | // If the peripheral is currently working as a slave, the SDK driver |
tsungta | 0:b7415ae44dac | 385 | // it uses needs to be switched from SPIS to SPI. |
tsungta | 0:b7415ae44dac | 386 | if (!p_spi_info->master) { |
tsungta | 0:b7415ae44dac | 387 | nrf_drv_spis_uninit(SLAVE_INST(obj)); |
tsungta | 0:b7415ae44dac | 388 | } |
tsungta | 0:b7415ae44dac | 389 | // I the SPI mode has to be changed, the SDK's SPI driver needs to be |
tsungta | 0:b7415ae44dac | 390 | // re-initialized (there is no other way to change its configuration). |
tsungta | 0:b7415ae44dac | 391 | else if (p_spi_info->spi_mode != (uint8_t)new_mode) { |
tsungta | 0:b7415ae44dac | 392 | nrf_drv_spi_uninit(MASTER_INST(obj)); |
tsungta | 0:b7415ae44dac | 393 | } |
tsungta | 0:b7415ae44dac | 394 | else { |
tsungta | 0:b7415ae44dac | 395 | return; |
tsungta | 0:b7415ae44dac | 396 | } |
tsungta | 0:b7415ae44dac | 397 | |
tsungta | 0:b7415ae44dac | 398 | p_spi_info->spi_mode = (uint8_t)new_mode; |
tsungta | 0:b7415ae44dac | 399 | p_spi_info->master = true; |
tsungta | 0:b7415ae44dac | 400 | p_spi_info->flag.busy = false; |
tsungta | 0:b7415ae44dac | 401 | |
tsungta | 0:b7415ae44dac | 402 | // Initialize SDK's SPI driver with the new configuration. |
tsungta | 0:b7415ae44dac | 403 | nrf_drv_spi_config_t config; |
tsungta | 0:b7415ae44dac | 404 | prepare_master_config(&config, p_spi_info); |
tsungta | 0:b7415ae44dac | 405 | (void)nrf_drv_spi_init(MASTER_INST(obj), &config, |
tsungta | 0:b7415ae44dac | 406 | m_master_event_handlers[SPI_IDX(obj)]); |
tsungta | 0:b7415ae44dac | 407 | } |
tsungta | 0:b7415ae44dac | 408 | } |
tsungta | 0:b7415ae44dac | 409 | |
tsungta | 0:b7415ae44dac | 410 | static nrf_drv_spi_frequency_t freq_translate(int hz) |
tsungta | 0:b7415ae44dac | 411 | { |
tsungta | 0:b7415ae44dac | 412 | nrf_drv_spi_frequency_t frequency; |
tsungta | 0:b7415ae44dac | 413 | if (hz<250000) { //125Kbps |
tsungta | 0:b7415ae44dac | 414 | frequency = NRF_DRV_SPI_FREQ_125K; |
tsungta | 0:b7415ae44dac | 415 | } else if (hz<500000) { //250Kbps |
tsungta | 0:b7415ae44dac | 416 | frequency = NRF_DRV_SPI_FREQ_250K; |
tsungta | 0:b7415ae44dac | 417 | } else if (hz<1000000) { //500Kbps |
tsungta | 0:b7415ae44dac | 418 | frequency = NRF_DRV_SPI_FREQ_500K; |
tsungta | 0:b7415ae44dac | 419 | } else if (hz<2000000) { //1Mbps |
tsungta | 0:b7415ae44dac | 420 | frequency = NRF_DRV_SPI_FREQ_1M; |
tsungta | 0:b7415ae44dac | 421 | } else if (hz<4000000) { //2Mbps |
tsungta | 0:b7415ae44dac | 422 | frequency = NRF_DRV_SPI_FREQ_2M; |
tsungta | 0:b7415ae44dac | 423 | } else if (hz<8000000) { //4Mbps |
tsungta | 0:b7415ae44dac | 424 | frequency = NRF_DRV_SPI_FREQ_4M; |
tsungta | 0:b7415ae44dac | 425 | } else { //8Mbps |
tsungta | 0:b7415ae44dac | 426 | frequency = NRF_DRV_SPI_FREQ_8M; |
tsungta | 0:b7415ae44dac | 427 | } |
tsungta | 0:b7415ae44dac | 428 | return frequency; |
tsungta | 0:b7415ae44dac | 429 | } |
tsungta | 0:b7415ae44dac | 430 | |
tsungta | 0:b7415ae44dac | 431 | void spi_frequency_multibyte(spi_mb_t *obj, int hz) |
tsungta | 0:b7415ae44dac | 432 | { |
tsungta | 0:b7415ae44dac | 433 | spi_info_t *p_spi_info = SPI_INFO(obj); |
tsungta | 0:b7415ae44dac | 434 | nrf_drv_spi_frequency_t new_frequency = freq_translate(hz); |
tsungta | 0:b7415ae44dac | 435 | |
tsungta | 0:b7415ae44dac | 436 | if (p_spi_info->master) |
tsungta | 0:b7415ae44dac | 437 | { |
tsungta | 0:b7415ae44dac | 438 | if (p_spi_info->frequency != new_frequency) { |
tsungta | 0:b7415ae44dac | 439 | p_spi_info->frequency = new_frequency; |
tsungta | 0:b7415ae44dac | 440 | |
tsungta | 0:b7415ae44dac | 441 | nrf_drv_spi_config_t config; |
tsungta | 0:b7415ae44dac | 442 | prepare_master_config(&config, p_spi_info); |
tsungta | 0:b7415ae44dac | 443 | |
tsungta | 0:b7415ae44dac | 444 | nrf_drv_spi_t const *p_spi = MASTER_INST(obj); |
tsungta | 0:b7415ae44dac | 445 | nrf_drv_spi_uninit(p_spi); |
tsungta | 0:b7415ae44dac | 446 | (void)nrf_drv_spi_init(p_spi, &config, |
tsungta | 0:b7415ae44dac | 447 | m_master_event_handlers[SPI_IDX(obj)]); |
tsungta | 0:b7415ae44dac | 448 | } |
tsungta | 0:b7415ae44dac | 449 | } |
tsungta | 0:b7415ae44dac | 450 | // There is no need to set anything in slaves when it comes to frequency, |
tsungta | 0:b7415ae44dac | 451 | // since slaves just synchronize with the clock provided by a master. |
tsungta | 0:b7415ae44dac | 452 | } |
tsungta | 0:b7415ae44dac | 453 | |
tsungta | 0:b7415ae44dac | 454 | int spi_master_write_multibyte(spi_mb_t *obj, int value) |
tsungta | 0:b7415ae44dac | 455 | { |
tsungta | 0:b7415ae44dac | 456 | spi_info_t *p_spi_info = SPI_INFO(obj); |
tsungta | 0:b7415ae44dac | 457 | |
tsungta | 0:b7415ae44dac | 458 | #if DEVICE_SPI_ASYNCH |
tsungta | 0:b7415ae44dac | 459 | while (p_spi_info->flag.busy) { |
tsungta | 0:b7415ae44dac | 460 | } |
tsungta | 0:b7415ae44dac | 461 | #endif |
tsungta | 0:b7415ae44dac | 462 | |
tsungta | 0:b7415ae44dac | 463 | //m_tx_buf = value; |
tsungta | 0:b7415ae44dac | 464 | p_spi_info->flag.busy = true; |
tsungta | 0:b7415ae44dac | 465 | (void)nrf_drv_spi_transfer(MASTER_INST(obj), |
tsungta | 0:b7415ae44dac | 466 | m_tx_buf, m_length, m_rx_buf, m_length); |
tsungta | 0:b7415ae44dac | 467 | while (p_spi_info->flag.busy) { |
tsungta | 0:b7415ae44dac | 468 | } |
tsungta | 0:b7415ae44dac | 469 | |
tsungta | 0:b7415ae44dac | 470 | return m_rx_buf; |
tsungta | 0:b7415ae44dac | 471 | } |
tsungta | 0:b7415ae44dac | 472 | |
tsungta | 0:b7415ae44dac | 473 | int spi_slave_receive_multibyte(spi_mb_t *obj) |
tsungta | 0:b7415ae44dac | 474 | { |
tsungta | 0:b7415ae44dac | 475 | spi_info_t *p_spi_info = SPI_INFO(obj); |
tsungta | 0:b7415ae44dac | 476 | MBED_ASSERT(!p_spi_info->master); |
tsungta | 0:b7415ae44dac | 477 | return p_spi_info->flag.readable; |
tsungta | 0:b7415ae44dac | 478 | ; |
tsungta | 0:b7415ae44dac | 479 | } |
tsungta | 0:b7415ae44dac | 480 | |
tsungta | 0:b7415ae44dac | 481 | int spi_slave_read_multibyte(spi_mb_t *obj) |
tsungta | 0:b7415ae44dac | 482 | { |
tsungta | 0:b7415ae44dac | 483 | spi_info_t *p_spi_info = SPI_INFO(obj); |
tsungta | 0:b7415ae44dac | 484 | MBED_ASSERT(!p_spi_info->master); |
tsungta | 0:b7415ae44dac | 485 | while (!p_spi_info->flag.readable) { |
tsungta | 0:b7415ae44dac | 486 | } |
tsungta | 0:b7415ae44dac | 487 | p_spi_info->flag.readable = false; |
tsungta | 0:b7415ae44dac | 488 | return m_rx_buf; |
tsungta | 0:b7415ae44dac | 489 | } |
tsungta | 0:b7415ae44dac | 490 | |
tsungta | 0:b7415ae44dac | 491 | void spi_slave_write_multibyte(spi_mb_t *obj, int value) |
tsungta | 0:b7415ae44dac | 492 | { |
tsungta | 0:b7415ae44dac | 493 | spi_info_t *p_spi_info = SPI_INFO(obj); |
tsungta | 0:b7415ae44dac | 494 | MBED_ASSERT(!p_spi_info->master); |
tsungta | 0:b7415ae44dac | 495 | |
tsungta | 0:b7415ae44dac | 496 | //m_tx_buf = (uint8_t)value; |
tsungta | 0:b7415ae44dac | 497 | } |
tsungta | 0:b7415ae44dac | 498 | |
tsungta | 0:b7415ae44dac | 499 | #if DEVICE_SPI_ASYNCH |
tsungta | 0:b7415ae44dac | 500 | |
tsungta | 0:b7415ae44dac | 501 | void spi_master_transfer_multibyte(spi_mb_t *obj, |
tsungta | 0:b7415ae44dac | 502 | const void *tx, size_t tx_length, |
tsungta | 0:b7415ae44dac | 503 | void *rx, size_t rx_length, uint8_t bit_width, |
tsungta | 0:b7415ae44dac | 504 | uint32_t handler, uint32_t event, DMAUsage hint) |
tsungta | 0:b7415ae44dac | 505 | { |
tsungta | 0:b7415ae44dac | 506 | spi_info_t *p_spi_info = SPI_INFO(obj); |
tsungta | 0:b7415ae44dac | 507 | MBED_ASSERT(p_spi_info->master); |
tsungta | 0:b7415ae44dac | 508 | (void)hint; |
tsungta | 0:b7415ae44dac | 509 | (void)bit_width; |
tsungta | 0:b7415ae44dac | 510 | |
tsungta | 0:b7415ae44dac | 511 | p_spi_info->handler = handler; |
tsungta | 0:b7415ae44dac | 512 | p_spi_info->event = event; |
tsungta | 0:b7415ae44dac | 513 | |
tsungta | 0:b7415ae44dac | 514 | p_spi_info->flag.busy = true; |
tsungta | 0:b7415ae44dac | 515 | (void)nrf_drv_spi_transfer(MASTER_INST(obj), |
tsungta | 0:b7415ae44dac | 516 | (uint8_t const *)tx, tx_length, |
tsungta | 0:b7415ae44dac | 517 | (uint8_t *)rx, rx_length); |
tsungta | 0:b7415ae44dac | 518 | } |
tsungta | 0:b7415ae44dac | 519 | |
tsungta | 0:b7415ae44dac | 520 | uint32_t spi_irq_handler_asynch_multibyte(spi_mb_t *obj) |
tsungta | 0:b7415ae44dac | 521 | { |
tsungta | 0:b7415ae44dac | 522 | spi_info_t *p_spi_info = SPI_INFO(obj); |
tsungta | 0:b7415ae44dac | 523 | MBED_ASSERT(p_spi_info->master); |
tsungta | 0:b7415ae44dac | 524 | return p_spi_info->event & SPI_EVENT_COMPLETE; |
tsungta | 0:b7415ae44dac | 525 | } |
tsungta | 0:b7415ae44dac | 526 | |
tsungta | 0:b7415ae44dac | 527 | uint8_t spi_active_multibyte(spi_mb_t *obj) |
tsungta | 0:b7415ae44dac | 528 | { |
tsungta | 0:b7415ae44dac | 529 | spi_info_t *p_spi_info = SPI_INFO(obj); |
tsungta | 0:b7415ae44dac | 530 | MBED_ASSERT(p_spi_info->master); |
tsungta | 0:b7415ae44dac | 531 | return p_spi_info->flag.busy; |
tsungta | 0:b7415ae44dac | 532 | } |
tsungta | 0:b7415ae44dac | 533 | |
tsungta | 0:b7415ae44dac | 534 | void spi_abort_asynch_multibyte(spi_mb_t *obj) |
tsungta | 0:b7415ae44dac | 535 | { |
tsungta | 0:b7415ae44dac | 536 | MBED_ASSERT(SPI_INFO(obj)->master); |
tsungta | 0:b7415ae44dac | 537 | nrf_drv_spi_abort(MASTER_INST(obj)); |
tsungta | 0:b7415ae44dac | 538 | } |
tsungta | 0:b7415ae44dac | 539 | |
tsungta | 0:b7415ae44dac | 540 | #endif // DEVICE_SPI_ASYNCH |
tsungta | 0:b7415ae44dac | 541 | |
tsungta | 0:b7415ae44dac | 542 | #endif // DEVICE_SPI |