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:
Mon Nov 10 07:45:06 2014 +0000
Revision:
395:bfce16e86ea4
Parent:
300:55638feb26a4
Synchronized with git revision 8adfd82aa1bf8859ec08537ee7bcd4aaaec1769b

Full URL: https://github.com/mbedmicro/mbed/commit/8adfd82aa1bf8859ec08537ee7bcd4aaaec1769b/

Targets: LPC176X - Add repeater pinmode

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 #define SPIS_MESSAGE_SIZE 1
mbed_official 85:e1a8e879a6a9 24 volatile uint8_t m_tx_buf[SPIS_MESSAGE_SIZE] = {0};
mbed_official 85:e1a8e879a6a9 25 volatile uint8_t m_rx_buf[SPIS_MESSAGE_SIZE] = {0};
mbed_official 85:e1a8e879a6a9 26
mbed_official 395:bfce16e86ea4 27 // nRF51822's I2C_0 and SPI_0 (I2C_1, SPI_1 and SPIS1) share the same address.
mbed_official 395:bfce16e86ea4 28 // They can't be used at the same time. So we use two global variable to track the usage.
mbed_official 395:bfce16e86ea4 29 // See nRF51822 address information at nRF51822_PS v2.0.pdf - Table 15 Peripheral instance reference
mbed_official 395:bfce16e86ea4 30 extern volatile i2c_spi_peripheral_t i2c0_spi0_peripheral; // from i2c_api.c
mbed_official 395:bfce16e86ea4 31 extern volatile i2c_spi_peripheral_t i2c1_spi1_peripheral;
mbed_official 85:e1a8e879a6a9 32
mbed_official 300:55638feb26a4 33 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
mbed_official 300:55638feb26a4 34 {
mbed_official 395:bfce16e86ea4 35 SPIName spi;
mbed_official 395:bfce16e86ea4 36
mbed_official 395:bfce16e86ea4 37 if (ssel == NC && i2c0_spi0_peripheral.usage == I2C_SPI_PERIPHERAL_FOR_SPI &&
mbed_official 395:bfce16e86ea4 38 i2c0_spi0_peripheral.sda_mosi == (uint8_t)mosi &&
mbed_official 395:bfce16e86ea4 39 i2c0_spi0_peripheral.scl_miso == (uint8_t)miso &&
mbed_official 395:bfce16e86ea4 40 i2c0_spi0_peripheral.sclk == (uint8_t)sclk) {
mbed_official 395:bfce16e86ea4 41 // The SPI with the same pins is already initialized
mbed_official 395:bfce16e86ea4 42 spi = SPI_0;
mbed_official 395:bfce16e86ea4 43 obj->peripheral = 0x1;
mbed_official 395:bfce16e86ea4 44 } else if (ssel == NC && i2c1_spi1_peripheral.usage == I2C_SPI_PERIPHERAL_FOR_SPI &&
mbed_official 395:bfce16e86ea4 45 i2c1_spi1_peripheral.sda_mosi == (uint8_t)mosi &&
mbed_official 395:bfce16e86ea4 46 i2c1_spi1_peripheral.scl_miso == (uint8_t)miso &&
mbed_official 395:bfce16e86ea4 47 i2c1_spi1_peripheral.sclk == (uint8_t)sclk) {
mbed_official 395:bfce16e86ea4 48 // The SPI with the same pins is already initialized
mbed_official 395:bfce16e86ea4 49 spi = SPI_1;
mbed_official 395:bfce16e86ea4 50 obj->peripheral = 0x2;
mbed_official 395:bfce16e86ea4 51 } else if (i2c1_spi1_peripheral.usage == 0) {
mbed_official 395:bfce16e86ea4 52 i2c1_spi1_peripheral.usage = I2C_SPI_PERIPHERAL_FOR_SPI;
mbed_official 395:bfce16e86ea4 53 i2c1_spi1_peripheral.sda_mosi = (uint8_t)mosi;
mbed_official 395:bfce16e86ea4 54 i2c1_spi1_peripheral.scl_miso = (uint8_t)miso;
mbed_official 395:bfce16e86ea4 55 i2c1_spi1_peripheral.sclk = (uint8_t)sclk;
mbed_official 395:bfce16e86ea4 56
mbed_official 395:bfce16e86ea4 57 spi = SPI_1;
mbed_official 395:bfce16e86ea4 58 obj->peripheral = 0x2;
mbed_official 395:bfce16e86ea4 59 } else if (i2c0_spi0_peripheral.usage == 0) {
mbed_official 395:bfce16e86ea4 60 i2c0_spi0_peripheral.usage = I2C_SPI_PERIPHERAL_FOR_SPI;
mbed_official 395:bfce16e86ea4 61 i2c0_spi0_peripheral.sda_mosi = (uint8_t)mosi;
mbed_official 395:bfce16e86ea4 62 i2c0_spi0_peripheral.scl_miso = (uint8_t)miso;
mbed_official 395:bfce16e86ea4 63 i2c0_spi0_peripheral.sclk = (uint8_t)sclk;
mbed_official 395:bfce16e86ea4 64
mbed_official 395:bfce16e86ea4 65 spi = SPI_0;
mbed_official 395:bfce16e86ea4 66 obj->peripheral = 0x1;
mbed_official 395:bfce16e86ea4 67 } else {
mbed_official 395:bfce16e86ea4 68 // No available peripheral
mbed_official 395:bfce16e86ea4 69 error("No available SPI");
mbed_official 395:bfce16e86ea4 70 }
mbed_official 395:bfce16e86ea4 71
mbed_official 300:55638feb26a4 72 if (ssel==NC) {
mbed_official 300:55638feb26a4 73 obj->spi = (NRF_SPI_Type *)spi;
mbed_official 300:55638feb26a4 74 obj->spis = (NRF_SPIS_Type *)NC;
mbed_official 300:55638feb26a4 75 } else {
mbed_official 300:55638feb26a4 76 obj->spi = (NRF_SPI_Type *)NC;
mbed_official 300:55638feb26a4 77 obj->spis = (NRF_SPIS_Type *)spi;
mbed_official 85:e1a8e879a6a9 78 }
mbed_official 227:7bd0639b8911 79
mbed_official 300:55638feb26a4 80 // pin out the spi pins
mbed_official 300:55638feb26a4 81 if (ssel != NC) { //slave
mbed_official 300:55638feb26a4 82 obj->spis->POWER = 0;
mbed_official 300:55638feb26a4 83 obj->spis->POWER = 1;
mbed_official 85:e1a8e879a6a9 84
mbed_official 85:e1a8e879a6a9 85 NRF_GPIO->PIN_CNF[mosi] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 86 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 87 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 88 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 89 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 90 NRF_GPIO->PIN_CNF[miso] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 91 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 92 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 93 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 94 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 95 NRF_GPIO->PIN_CNF[sclk] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 96 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 97 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 98 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 99 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 100 NRF_GPIO->PIN_CNF[ssel] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 101 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 102 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 103 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 104 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 300:55638feb26a4 105
mbed_official 85:e1a8e879a6a9 106 obj->spis->PSELMOSI = mosi;
mbed_official 85:e1a8e879a6a9 107 obj->spis->PSELMISO = miso;
mbed_official 300:55638feb26a4 108 obj->spis->PSELSCK = sclk;
mbed_official 300:55638feb26a4 109 obj->spis->PSELCSN = ssel;
mbed_official 300:55638feb26a4 110
mbed_official 300:55638feb26a4 111 obj->spis->EVENTS_END = 0;
mbed_official 300:55638feb26a4 112 obj->spis->EVENTS_ACQUIRED = 0;
mbed_official 300:55638feb26a4 113 obj->spis->MAXRX = SPIS_MESSAGE_SIZE;
mbed_official 300:55638feb26a4 114 obj->spis->MAXTX = SPIS_MESSAGE_SIZE;
mbed_official 300:55638feb26a4 115 obj->spis->TXDPTR = (uint32_t)&m_tx_buf[0];
mbed_official 300:55638feb26a4 116 obj->spis->RXDPTR = (uint32_t)&m_rx_buf[0];
mbed_official 300:55638feb26a4 117 obj->spis->SHORTS = (SPIS_SHORTS_END_ACQUIRE_Enabled << SPIS_SHORTS_END_ACQUIRE_Pos);
mbed_official 85:e1a8e879a6a9 118
mbed_official 85:e1a8e879a6a9 119 spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave
mbed_official 300:55638feb26a4 120 } else { //master
mbed_official 300:55638feb26a4 121 obj->spi->POWER = 0;
mbed_official 300:55638feb26a4 122 obj->spi->POWER = 1;
mbed_official 300:55638feb26a4 123
mbed_official 227:7bd0639b8911 124 //NRF_GPIO->DIR |= (1<<mosi);
mbed_official 85:e1a8e879a6a9 125 NRF_GPIO->PIN_CNF[mosi] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 126 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 127 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 128 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 129 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 130 obj->spi->PSELMOSI = mosi;
mbed_official 300:55638feb26a4 131
mbed_official 85:e1a8e879a6a9 132 NRF_GPIO->PIN_CNF[sclk] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 133 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 134 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 135 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 136 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 137 obj->spi->PSELSCK = sclk;
mbed_official 300:55638feb26a4 138
mbed_official 85:e1a8e879a6a9 139 //NRF_GPIO->DIR &= ~(1<<miso);
mbed_official 85:e1a8e879a6a9 140 NRF_GPIO->PIN_CNF[miso] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)
mbed_official 85:e1a8e879a6a9 141 | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
mbed_official 85:e1a8e879a6a9 142 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
mbed_official 85:e1a8e879a6a9 143 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
mbed_official 85:e1a8e879a6a9 144 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 85:e1a8e879a6a9 145
mbed_official 85:e1a8e879a6a9 146 obj->spi->PSELMISO = miso;
mbed_official 300:55638feb26a4 147
mbed_official 227:7bd0639b8911 148 obj->spi->EVENTS_READY = 0U;
mbed_official 85:e1a8e879a6a9 149
mbed_official 85:e1a8e879a6a9 150 spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master
mbed_official 227:7bd0639b8911 151 spi_frequency(obj, 1000000);
mbed_official 85:e1a8e879a6a9 152 }
mbed_official 300:55638feb26a4 153 }
mbed_official 300:55638feb26a4 154
mbed_official 395:bfce16e86ea4 155 void spi_free(spi_t *obj)
mbed_official 395:bfce16e86ea4 156 {
mbed_official 85:e1a8e879a6a9 157 }
mbed_official 85:e1a8e879a6a9 158
mbed_official 300:55638feb26a4 159 static inline void spi_disable(spi_t *obj, int slave)
mbed_official 300:55638feb26a4 160 {
mbed_official 300:55638feb26a4 161 if (slave) {
mbed_official 85:e1a8e879a6a9 162 obj->spis->ENABLE = (SPIS_ENABLE_ENABLE_Disabled << SPIS_ENABLE_ENABLE_Pos);
mbed_official 300:55638feb26a4 163 } else {
mbed_official 85:e1a8e879a6a9 164 obj->spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos);
mbed_official 85:e1a8e879a6a9 165 }
mbed_official 85:e1a8e879a6a9 166 }
mbed_official 85:e1a8e879a6a9 167
mbed_official 300:55638feb26a4 168 static inline void spi_enable(spi_t *obj, int slave)
mbed_official 300:55638feb26a4 169 {
mbed_official 300:55638feb26a4 170 if (slave) {
mbed_official 85:e1a8e879a6a9 171 obj->spis->ENABLE = (SPIS_ENABLE_ENABLE_Enabled << SPIS_ENABLE_ENABLE_Pos);
mbed_official 300:55638feb26a4 172 } else {
mbed_official 85:e1a8e879a6a9 173 obj->spi->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos);
mbed_official 85:e1a8e879a6a9 174 }
mbed_official 85:e1a8e879a6a9 175 }
mbed_official 85:e1a8e879a6a9 176
mbed_official 300:55638feb26a4 177 void spi_format(spi_t *obj, int bits, int mode, int slave)
mbed_official 300:55638feb26a4 178 {
mbed_official 209:137057ef9d1e 179 uint32_t config_mode = 0;
mbed_official 300:55638feb26a4 180 spi_disable(obj, slave);
mbed_official 300:55638feb26a4 181
mbed_official 85:e1a8e879a6a9 182 if (bits != 8) {
mbed_official 85:e1a8e879a6a9 183 error("Only 8bits SPI supported");
mbed_official 85:e1a8e879a6a9 184 }
mbed_official 300:55638feb26a4 185
mbed_official 300:55638feb26a4 186 switch (mode) {
mbed_official 85:e1a8e879a6a9 187 case 0:
mbed_official 300:55638feb26a4 188 config_mode = (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos);
mbed_official 85:e1a8e879a6a9 189 break;
mbed_official 85:e1a8e879a6a9 190 case 1:
mbed_official 85:e1a8e879a6a9 191 config_mode = (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos);
mbed_official 85:e1a8e879a6a9 192 break;
mbed_official 85:e1a8e879a6a9 193 case 2:
mbed_official 300:55638feb26a4 194 config_mode = (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos);
mbed_official 85:e1a8e879a6a9 195 break;
mbed_official 85:e1a8e879a6a9 196 case 3:
mbed_official 85:e1a8e879a6a9 197 config_mode = (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos);
mbed_official 85:e1a8e879a6a9 198 break;
mbed_official 85:e1a8e879a6a9 199 default:
mbed_official 85:e1a8e879a6a9 200 error("SPI format error");
mbed_official 85:e1a8e879a6a9 201 break;
mbed_official 85:e1a8e879a6a9 202 }
mbed_official 85:e1a8e879a6a9 203 //default to msb first
mbed_official 300:55638feb26a4 204 if (slave) {
mbed_official 300:55638feb26a4 205 obj->spis->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos));
mbed_official 300:55638feb26a4 206 } else {
mbed_official 300:55638feb26a4 207 obj->spi->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos));
mbed_official 85:e1a8e879a6a9 208 }
mbed_official 300:55638feb26a4 209
mbed_official 300:55638feb26a4 210 spi_enable(obj, slave);
mbed_official 85:e1a8e879a6a9 211 }
mbed_official 85:e1a8e879a6a9 212
mbed_official 300:55638feb26a4 213 void spi_frequency(spi_t *obj, int hz)
mbed_official 300:55638feb26a4 214 {
mbed_official 300:55638feb26a4 215 if ((int)obj->spi==NC) {
mbed_official 85:e1a8e879a6a9 216 return;
mbed_official 85:e1a8e879a6a9 217 }
mbed_official 300:55638feb26a4 218 spi_disable(obj, 0);
mbed_official 300:55638feb26a4 219
mbed_official 300:55638feb26a4 220 if (hz<250000) { //125Kbps
mbed_official 300:55638feb26a4 221 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K125;
mbed_official 300:55638feb26a4 222 } else if (hz<500000) { //250Kbps
mbed_official 300:55638feb26a4 223 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K250;
mbed_official 300:55638feb26a4 224 } else if (hz<1000000) { //500Kbps
mbed_official 85:e1a8e879a6a9 225 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K500;
mbed_official 300:55638feb26a4 226 } else if (hz<2000000) { //1Mbps
mbed_official 85:e1a8e879a6a9 227 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M1;
mbed_official 300:55638feb26a4 228 } else if (hz<4000000) { //2Mbps
mbed_official 85:e1a8e879a6a9 229 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M2;
mbed_official 300:55638feb26a4 230 } else if (hz<8000000) { //4Mbps
mbed_official 85:e1a8e879a6a9 231 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M4;
mbed_official 300:55638feb26a4 232 } else { //8Mbps
mbed_official 227:7bd0639b8911 233 obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M8;
mbed_official 85:e1a8e879a6a9 234 }
mbed_official 300:55638feb26a4 235
mbed_official 300:55638feb26a4 236 spi_enable(obj, 0);
mbed_official 85:e1a8e879a6a9 237 }
mbed_official 85:e1a8e879a6a9 238
mbed_official 300:55638feb26a4 239 static inline int spi_readable(spi_t *obj)
mbed_official 300:55638feb26a4 240 {
mbed_official 85:e1a8e879a6a9 241 return (obj->spi->EVENTS_READY == 1);
mbed_official 85:e1a8e879a6a9 242 }
mbed_official 85:e1a8e879a6a9 243
mbed_official 300:55638feb26a4 244 static inline int spi_writeable(spi_t *obj)
mbed_official 300:55638feb26a4 245 {
mbed_official 85:e1a8e879a6a9 246 return (obj->spi->EVENTS_READY == 0);
mbed_official 85:e1a8e879a6a9 247 }
mbed_official 85:e1a8e879a6a9 248
mbed_official 300:55638feb26a4 249 static inline int spi_read(spi_t *obj)
mbed_official 300:55638feb26a4 250 {
mbed_official 300:55638feb26a4 251 while (!spi_readable(obj)) {
mbed_official 300:55638feb26a4 252 }
mbed_official 85:e1a8e879a6a9 253
mbed_official 300:55638feb26a4 254 obj->spi->EVENTS_READY = 0;
mbed_official 85:e1a8e879a6a9 255 return (int)obj->spi->RXD;
mbed_official 85:e1a8e879a6a9 256 }
mbed_official 85:e1a8e879a6a9 257
mbed_official 300:55638feb26a4 258 int spi_master_write(spi_t *obj, int value)
mbed_official 300:55638feb26a4 259 {
mbed_official 300:55638feb26a4 260 while (!spi_writeable(obj)) {
mbed_official 85:e1a8e879a6a9 261 }
mbed_official 85:e1a8e879a6a9 262 obj->spi->TXD = (uint32_t)value;
mbed_official 85:e1a8e879a6a9 263 return spi_read(obj);
mbed_official 85:e1a8e879a6a9 264 }
mbed_official 85:e1a8e879a6a9 265
mbed_official 227:7bd0639b8911 266 //static inline int spis_writeable(spi_t *obj) {
mbed_official 85:e1a8e879a6a9 267 // return (obj->spis->EVENTS_ACQUIRED==1);
mbed_official 85:e1a8e879a6a9 268 //}
mbed_official 85:e1a8e879a6a9 269
mbed_official 300:55638feb26a4 270 int spi_slave_receive(spi_t *obj)
mbed_official 300:55638feb26a4 271 {
mbed_official 85:e1a8e879a6a9 272 return obj->spis->EVENTS_END;
mbed_official 300:55638feb26a4 273 }
mbed_official 85:e1a8e879a6a9 274
mbed_official 300:55638feb26a4 275 int spi_slave_read(spi_t *obj)
mbed_official 300:55638feb26a4 276 {
mbed_official 85:e1a8e879a6a9 277 return m_rx_buf[0];
mbed_official 85:e1a8e879a6a9 278 }
mbed_official 85:e1a8e879a6a9 279
mbed_official 300:55638feb26a4 280 void spi_slave_write(spi_t *obj, int value)
mbed_official 300:55638feb26a4 281 {
mbed_official 300:55638feb26a4 282 m_tx_buf[0] = value & 0xFF;
mbed_official 300:55638feb26a4 283 obj->spis->TASKS_RELEASE = 1;
mbed_official 300:55638feb26a4 284 obj->spis->EVENTS_ACQUIRED = 0;
mbed_official 300:55638feb26a4 285 obj->spis->EVENTS_END = 0;
mbed_official 85:e1a8e879a6a9 286 }