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