mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Sep 23 10:30:11 2014 +0100
Revision:
330:c80ac197fa6a
Parent:
300:55638feb26a4
Child:
395:bfce16e86ea4
Synchronized with git revision 56e9fd597864aab1c8fd92ef1c565e88a04d0a37

Full URL: https://github.com/mbedmicro/mbed/commit/56e9fd597864aab1c8fd92ef1c565e88a04d0a37/

[NUCLEO_F302R8] Migration to STM32Cube driver.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 85:e1a8e879a6a9 1 /* mbed Microcontroller Library
mbed_official 104:a6a92e2e5a92 2 * Copyright (c) 2013 Nordic Semiconductor
mbed_official 85:e1a8e879a6a9 3 *
mbed_official 85:e1a8e879a6a9 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 85:e1a8e879a6a9 5 * you may not use this file except in compliance with the License.
mbed_official 85:e1a8e879a6a9 6 * You may obtain a copy of the License at
mbed_official 85:e1a8e879a6a9 7 *
mbed_official 85:e1a8e879a6a9 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 85:e1a8e879a6a9 9 *
mbed_official 85:e1a8e879a6a9 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 85:e1a8e879a6a9 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 85:e1a8e879a6a9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 85:e1a8e879a6a9 13 * See the License for the specific language governing permissions and
mbed_official 85:e1a8e879a6a9 14 * limitations under the License.
mbed_official 85:e1a8e879a6a9 15 */
mbed_official 85:e1a8e879a6a9 16 //#include <math.h>
mbed_official 227:7bd0639b8911 17 #include "mbed_assert.h"
mbed_official 85:e1a8e879a6a9 18 #include "spi_api.h"
mbed_official 85:e1a8e879a6a9 19 #include "cmsis.h"
mbed_official 85:e1a8e879a6a9 20 #include "pinmap.h"
mbed_official 285:31249416b6f9 21 #include "mbed_error.h"
mbed_official 85:e1a8e879a6a9 22
mbed_official 85:e1a8e879a6a9 23 static const PinMap PinMap_SPI_SCLK[] = {
mbed_official 300:55638feb26a4 24 {SPI_PSELSCK0, SPI_0, 0x01},
mbed_official 85:e1a8e879a6a9 25 {SPI_PSELSCK1, SPI_1, 0x02},
mbed_official 227:7bd0639b8911 26 {SPIS_PSELSCK, SPIS, 0x03},
mbed_official 300:55638feb26a4 27 {NC, NC, 0}
mbed_official 85:e1a8e879a6a9 28 };
mbed_official 85:e1a8e879a6a9 29
mbed_official 85:e1a8e879a6a9 30 static const PinMap PinMap_SPI_MOSI[] = {
mbed_official 300:55638feb26a4 31 {SPI_PSELMOSI0, SPI_0, 0x01},
mbed_official 85:e1a8e879a6a9 32 {SPI_PSELMOSI1, SPI_1, 0x02},
mbed_official 85:e1a8e879a6a9 33 {SPIS_PSELMOSI, SPIS, 0x03},
mbed_official 300:55638feb26a4 34 {NC, NC, 0}
mbed_official 85:e1a8e879a6a9 35 };
mbed_official 85:e1a8e879a6a9 36
mbed_official 85:e1a8e879a6a9 37 static const PinMap PinMap_SPI_MISO[] = {
mbed_official 300:55638feb26a4 38 {SPI_PSELMISO0, SPI_0, 0x01},
mbed_official 85:e1a8e879a6a9 39 {SPI_PSELMISO1, SPI_1, 0x02},
mbed_official 85:e1a8e879a6a9 40 {SPIS_PSELMISO, SPIS, 0x03},
mbed_official 300:55638feb26a4 41 {NC, NC, 0}
mbed_official 85:e1a8e879a6a9 42 };
mbed_official 85:e1a8e879a6a9 43
mbed_official 85:e1a8e879a6a9 44 static const PinMap PinMap_SPI_SSEL[] = {
mbed_official 85:e1a8e879a6a9 45 {SPIS_PSELSS, SPIS, 0x03},
mbed_official 300:55638feb26a4 46 {NC, NC, 0}
mbed_official 85:e1a8e879a6a9 47 };
mbed_official 85:e1a8e879a6a9 48 // {SPI_PSELSS0 , SPI_0, 0x01},
mbed_official 85:e1a8e879a6a9 49 #define SPIS_MESSAGE_SIZE 1
mbed_official 85:e1a8e879a6a9 50 volatile uint8_t m_tx_buf[SPIS_MESSAGE_SIZE] = {0};
mbed_official 85:e1a8e879a6a9 51 volatile uint8_t m_rx_buf[SPIS_MESSAGE_SIZE] = {0};
mbed_official 85:e1a8e879a6a9 52
mbed_official 85:e1a8e879a6a9 53
mbed_official 300:55638feb26a4 54 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
mbed_official 300:55638feb26a4 55 {
mbed_official 85:e1a8e879a6a9 56 // determine the SPI to use
mbed_official 85:e1a8e879a6a9 57 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 85:e1a8e879a6a9 58 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 85:e1a8e879a6a9 59 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 85:e1a8e879a6a9 60 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 85:e1a8e879a6a9 61 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
mbed_official 85:e1a8e879a6a9 62 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
mbed_official 300:55638feb26a4 63 SPIName spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 227:7bd0639b8911 64 //SPIName
mbed_official 300:55638feb26a4 65 if (ssel==NC) {
mbed_official 300:55638feb26a4 66 obj->spi = (NRF_SPI_Type *)spi;
mbed_official 300:55638feb26a4 67 obj->spis = (NRF_SPIS_Type *)NC;
mbed_official 300:55638feb26a4 68 } else {
mbed_official 300:55638feb26a4 69 obj->spi = (NRF_SPI_Type *)NC;
mbed_official 300:55638feb26a4 70 obj->spis = (NRF_SPIS_Type *)spi;
mbed_official 85:e1a8e879a6a9 71 }
mbed_official 257:9258cc0a200d 72 MBED_ASSERT((int)obj->spi != NC || (int)obj->spis != NC);
mbed_official 227:7bd0639b8911 73
mbed_official 300:55638feb26a4 74 // pin out the spi pins
mbed_official 300:55638feb26a4 75 if (ssel != NC) { //slave
mbed_official 300:55638feb26a4 76 obj->spis->POWER = 0;
mbed_official 300:55638feb26a4 77 obj->spis->POWER = 1;
mbed_official 85:e1a8e879a6a9 78
mbed_official 85:e1a8e879a6a9 79 NRF_GPIO->PIN_CNF[mosi] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 80 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 81 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 82 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 83 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 84 NRF_GPIO->PIN_CNF[miso] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 85 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 86 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 87 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 88 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 89 NRF_GPIO->PIN_CNF[sclk] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 90 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 91 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 92 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 93 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 94 NRF_GPIO->PIN_CNF[ssel] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 95 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 96 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 97 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 98 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 300:55638feb26a4 99
mbed_official 85:e1a8e879a6a9 100 obj->spis->PSELMOSI = mosi;
mbed_official 85:e1a8e879a6a9 101 obj->spis->PSELMISO = miso;
mbed_official 300:55638feb26a4 102 obj->spis->PSELSCK = sclk;
mbed_official 300:55638feb26a4 103 obj->spis->PSELCSN = ssel;
mbed_official 300:55638feb26a4 104
mbed_official 300:55638feb26a4 105 obj->spis->EVENTS_END = 0;
mbed_official 300:55638feb26a4 106 obj->spis->EVENTS_ACQUIRED = 0;
mbed_official 300:55638feb26a4 107 obj->spis->MAXRX = SPIS_MESSAGE_SIZE;
mbed_official 300:55638feb26a4 108 obj->spis->MAXTX = SPIS_MESSAGE_SIZE;
mbed_official 300:55638feb26a4 109 obj->spis->TXDPTR = (uint32_t)&m_tx_buf[0];
mbed_official 300:55638feb26a4 110 obj->spis->RXDPTR = (uint32_t)&m_rx_buf[0];
mbed_official 300:55638feb26a4 111 obj->spis->SHORTS = (SPIS_SHORTS_END_ACQUIRE_Enabled << SPIS_SHORTS_END_ACQUIRE_Pos);
mbed_official 85:e1a8e879a6a9 112
mbed_official 85:e1a8e879a6a9 113 spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave
mbed_official 300:55638feb26a4 114 } else { //master
mbed_official 300:55638feb26a4 115 obj->spi->POWER = 0;
mbed_official 300:55638feb26a4 116 obj->spi->POWER = 1;
mbed_official 300:55638feb26a4 117
mbed_official 227:7bd0639b8911 118 //NRF_GPIO->DIR |= (1<<mosi);
mbed_official 85:e1a8e879a6a9 119 NRF_GPIO->PIN_CNF[mosi] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 120 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 121 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 122 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 123 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 124 obj->spi->PSELMOSI = mosi;
mbed_official 300:55638feb26a4 125
mbed_official 85:e1a8e879a6a9 126 NRF_GPIO->PIN_CNF[sclk] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 127 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 128 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 129 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 130 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 131 obj->spi->PSELSCK = sclk;
mbed_official 300:55638feb26a4 132
mbed_official 85:e1a8e879a6a9 133 //NRF_GPIO->DIR &= ~(1<<miso);
mbed_official 85:e1a8e879a6a9 134 NRF_GPIO->PIN_CNF[miso] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 135 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 136 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 137 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 138 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 139
mbed_official 85:e1a8e879a6a9 140 obj->spi->PSELMISO = miso;
mbed_official 300:55638feb26a4 141
mbed_official 227:7bd0639b8911 142 obj->spi->EVENTS_READY = 0U;
mbed_official 85:e1a8e879a6a9 143
mbed_official 85:e1a8e879a6a9 144 spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master
mbed_official 227:7bd0639b8911 145 spi_frequency(obj, 1000000);
mbed_official 85:e1a8e879a6a9 146 }
mbed_official 300:55638feb26a4 147 }
mbed_official 300:55638feb26a4 148
mbed_official 300:55638feb26a4 149 void spi_free(spi_t *obj) {
mbed_official 85:e1a8e879a6a9 150 }
mbed_official 85:e1a8e879a6a9 151
mbed_official 300:55638feb26a4 152 static inline void spi_disable(spi_t *obj, int slave)
mbed_official 300:55638feb26a4 153 {
mbed_official 300:55638feb26a4 154 if (slave) {
mbed_official 85:e1a8e879a6a9 155 obj->spis->ENABLE = (SPIS_ENABLE_ENABLE_Disabled << SPIS_ENABLE_ENABLE_Pos);
mbed_official 300:55638feb26a4 156 } else {
mbed_official 85:e1a8e879a6a9 157 obj->spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos);
mbed_official 85:e1a8e879a6a9 158 }
mbed_official 85:e1a8e879a6a9 159 }
mbed_official 85:e1a8e879a6a9 160
mbed_official 300:55638feb26a4 161 static inline void spi_enable(spi_t *obj, int slave)
mbed_official 300:55638feb26a4 162 {
mbed_official 300:55638feb26a4 163 if (slave) {
mbed_official 85:e1a8e879a6a9 164 obj->spis->ENABLE = (SPIS_ENABLE_ENABLE_Enabled << SPIS_ENABLE_ENABLE_Pos);
mbed_official 300:55638feb26a4 165 } else {
mbed_official 85:e1a8e879a6a9 166 obj->spi->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos);
mbed_official 85:e1a8e879a6a9 167 }
mbed_official 85:e1a8e879a6a9 168 }
mbed_official 85:e1a8e879a6a9 169
mbed_official 300:55638feb26a4 170 void spi_format(spi_t *obj, int bits, int mode, int slave)
mbed_official 300:55638feb26a4 171 {
mbed_official 209:137057ef9d1e 172 uint32_t config_mode = 0;
mbed_official 300:55638feb26a4 173 spi_disable(obj, slave);
mbed_official 300:55638feb26a4 174
mbed_official 85:e1a8e879a6a9 175 if (bits != 8) {
mbed_official 85:e1a8e879a6a9 176 error("Only 8bits SPI supported");
mbed_official 85:e1a8e879a6a9 177 }
mbed_official 300:55638feb26a4 178
mbed_official 300:55638feb26a4 179 switch (mode) {
mbed_official 85:e1a8e879a6a9 180 case 0:
mbed_official 300:55638feb26a4 181 config_mode = (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos);
mbed_official 85:e1a8e879a6a9 182 break;
mbed_official 85:e1a8e879a6a9 183 case 1:
mbed_official 85:e1a8e879a6a9 184 config_mode = (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos);
mbed_official 85:e1a8e879a6a9 185 break;
mbed_official 85:e1a8e879a6a9 186 case 2:
mbed_official 300:55638feb26a4 187 config_mode = (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos);
mbed_official 85:e1a8e879a6a9 188 break;
mbed_official 85:e1a8e879a6a9 189 case 3:
mbed_official 85:e1a8e879a6a9 190 config_mode = (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos);
mbed_official 85:e1a8e879a6a9 191 break;
mbed_official 85:e1a8e879a6a9 192 default:
mbed_official 85:e1a8e879a6a9 193 error("SPI format error");
mbed_official 85:e1a8e879a6a9 194 break;
mbed_official 85:e1a8e879a6a9 195 }
mbed_official 85:e1a8e879a6a9 196 //default to msb first
mbed_official 300:55638feb26a4 197 if (slave) {
mbed_official 300:55638feb26a4 198 obj->spis->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos));
mbed_official 300:55638feb26a4 199 } else {
mbed_official 300:55638feb26a4 200 obj->spi->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos));
mbed_official 85:e1a8e879a6a9 201 }
mbed_official 300:55638feb26a4 202
mbed_official 300:55638feb26a4 203 spi_enable(obj, slave);
mbed_official 85:e1a8e879a6a9 204 }
mbed_official 85:e1a8e879a6a9 205
mbed_official 300:55638feb26a4 206 void spi_frequency(spi_t *obj, int hz)
mbed_official 300:55638feb26a4 207 {
mbed_official 300:55638feb26a4 208 if ((int)obj->spi==NC) {
mbed_official 85:e1a8e879a6a9 209 return;
mbed_official 85:e1a8e879a6a9 210 }
mbed_official 300:55638feb26a4 211 spi_disable(obj, 0);
mbed_official 300:55638feb26a4 212
mbed_official 300:55638feb26a4 213 if (hz<250000) { //125Kbps
mbed_official 300:55638feb26a4 214 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K125;
mbed_official 300:55638feb26a4 215 } else if (hz<500000) { //250Kbps
mbed_official 300:55638feb26a4 216 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K250;
mbed_official 300:55638feb26a4 217 } else if (hz<1000000) { //500Kbps
mbed_official 85:e1a8e879a6a9 218 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K500;
mbed_official 300:55638feb26a4 219 } else if (hz<2000000) { //1Mbps
mbed_official 85:e1a8e879a6a9 220 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M1;
mbed_official 300:55638feb26a4 221 } else if (hz<4000000) { //2Mbps
mbed_official 85:e1a8e879a6a9 222 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M2;
mbed_official 300:55638feb26a4 223 } else if (hz<8000000) { //4Mbps
mbed_official 85:e1a8e879a6a9 224 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M4;
mbed_official 300:55638feb26a4 225 } else { //8Mbps
mbed_official 227:7bd0639b8911 226 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M8;
mbed_official 85:e1a8e879a6a9 227 }
mbed_official 300:55638feb26a4 228
mbed_official 300:55638feb26a4 229 spi_enable(obj, 0);
mbed_official 85:e1a8e879a6a9 230 }
mbed_official 85:e1a8e879a6a9 231
mbed_official 300:55638feb26a4 232 static inline int spi_readable(spi_t *obj)
mbed_official 300:55638feb26a4 233 {
mbed_official 85:e1a8e879a6a9 234 return (obj->spi->EVENTS_READY == 1);
mbed_official 85:e1a8e879a6a9 235 }
mbed_official 85:e1a8e879a6a9 236
mbed_official 300:55638feb26a4 237 static inline int spi_writeable(spi_t *obj)
mbed_official 300:55638feb26a4 238 {
mbed_official 85:e1a8e879a6a9 239 return (obj->spi->EVENTS_READY == 0);
mbed_official 85:e1a8e879a6a9 240 }
mbed_official 85:e1a8e879a6a9 241
mbed_official 300:55638feb26a4 242 static inline int spi_read(spi_t *obj)
mbed_official 300:55638feb26a4 243 {
mbed_official 300:55638feb26a4 244 while (!spi_readable(obj)) {
mbed_official 300:55638feb26a4 245 }
mbed_official 85:e1a8e879a6a9 246
mbed_official 300:55638feb26a4 247 obj->spi->EVENTS_READY = 0;
mbed_official 85:e1a8e879a6a9 248 return (int)obj->spi->RXD;
mbed_official 85:e1a8e879a6a9 249 }
mbed_official 85:e1a8e879a6a9 250
mbed_official 300:55638feb26a4 251 int spi_master_write(spi_t *obj, int value)
mbed_official 300:55638feb26a4 252 {
mbed_official 300:55638feb26a4 253 while (!spi_writeable(obj)) {
mbed_official 85:e1a8e879a6a9 254 }
mbed_official 85:e1a8e879a6a9 255 obj->spi->TXD = (uint32_t)value;
mbed_official 85:e1a8e879a6a9 256 return spi_read(obj);
mbed_official 85:e1a8e879a6a9 257 }
mbed_official 85:e1a8e879a6a9 258
mbed_official 227:7bd0639b8911 259 //static inline int spis_writeable(spi_t *obj) {
mbed_official 85:e1a8e879a6a9 260 // return (obj->spis->EVENTS_ACQUIRED==1);
mbed_official 85:e1a8e879a6a9 261 //}
mbed_official 85:e1a8e879a6a9 262
mbed_official 300:55638feb26a4 263 int spi_slave_receive(spi_t *obj)
mbed_official 300:55638feb26a4 264 {
mbed_official 85:e1a8e879a6a9 265 return obj->spis->EVENTS_END;
mbed_official 300:55638feb26a4 266 }
mbed_official 85:e1a8e879a6a9 267
mbed_official 300:55638feb26a4 268 int spi_slave_read(spi_t *obj)
mbed_official 300:55638feb26a4 269 {
mbed_official 85:e1a8e879a6a9 270 return m_rx_buf[0];
mbed_official 85:e1a8e879a6a9 271 }
mbed_official 85:e1a8e879a6a9 272
mbed_official 300:55638feb26a4 273 void spi_slave_write(spi_t *obj, int value)
mbed_official 300:55638feb26a4 274 {
mbed_official 300:55638feb26a4 275 m_tx_buf[0] = value & 0xFF;
mbed_official 300:55638feb26a4 276 obj->spis->TASKS_RELEASE = 1;
mbed_official 300:55638feb26a4 277 obj->spis->EVENTS_ACQUIRED = 0;
mbed_official 300:55638feb26a4 278 obj->spis->EVENTS_END = 0;
mbed_official 85:e1a8e879a6a9 279 }