mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_NORDIC/TARGET_NRF51822/spi_api.c@251:de9a1e4ffd79, 2014-07-08 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Jul 08 14:15:07 2014 +0100
- Revision:
- 251:de9a1e4ffd79
- Parent:
- 250:a49055e7a707
- Child:
- 257:9258cc0a200d
Synchronized with git revision b0ac1e3485a9aa1cb98d1ca68df9d1cdbcd6ce63
Full URL: https://github.com/mbedmicro/mbed/commit/b0ac1e3485a9aa1cb98d1ca68df9d1cdbcd6ce63/
Revert "error.h -> mbed_error.h"
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> |
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 | 251:de9a1e4ffd79 | 21 | #include "error.h" |
mbed_official | 85:e1a8e879a6a9 | 22 | |
mbed_official | 85:e1a8e879a6a9 | 23 | static const PinMap PinMap_SPI_SCLK[] = { |
mbed_official | 85:e1a8e879a6a9 | 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 | 85:e1a8e879a6a9 | 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 | 85:e1a8e879a6a9 | 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 | 85:e1a8e879a6a9 | 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 | 85:e1a8e879a6a9 | 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 | 85:e1a8e879a6a9 | 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 | 85:e1a8e879a6a9 | 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 | 85:e1a8e879a6a9 | 54 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { |
mbed_official | 85:e1a8e879a6a9 | 55 | // determine the SPI to use |
mbed_official | 85:e1a8e879a6a9 | 56 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
mbed_official | 85:e1a8e879a6a9 | 57 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
mbed_official | 85:e1a8e879a6a9 | 58 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
mbed_official | 85:e1a8e879a6a9 | 59 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
mbed_official | 85:e1a8e879a6a9 | 60 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
mbed_official | 85:e1a8e879a6a9 | 61 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
mbed_official | 85:e1a8e879a6a9 | 62 | SPIName spi = (SPIName)pinmap_merge(spi_data, spi_cntl); |
mbed_official | 227:7bd0639b8911 | 63 | //SPIName |
mbed_official | 85:e1a8e879a6a9 | 64 | if(ssel==NC){ |
mbed_official | 85:e1a8e879a6a9 | 65 | obj->spi = (NRF_SPI_Type*)spi; |
mbed_official | 85:e1a8e879a6a9 | 66 | obj->spis = (NRF_SPIS_Type*)NC; |
mbed_official | 85:e1a8e879a6a9 | 67 | } |
mbed_official | 85:e1a8e879a6a9 | 68 | else{ |
mbed_official | 85:e1a8e879a6a9 | 69 | obj->spi = (NRF_SPI_Type*)NC; |
mbed_official | 227:7bd0639b8911 | 70 | obj->spis = (NRF_SPIS_Type*)spi; |
mbed_official | 85:e1a8e879a6a9 | 71 | } |
mbed_official | 227:7bd0639b8911 | 72 | MBED_ASSERT((int)obj->spi != NC && (int)obj->spis != NC); |
mbed_official | 227:7bd0639b8911 | 73 | |
mbed_official | 227:7bd0639b8911 | 74 | // pin out the spi pins |
mbed_official | 85:e1a8e879a6a9 | 75 | if (ssel != NC) {//slave |
mbed_official | 85:e1a8e879a6a9 | 76 | obj->spis->POWER=0; |
mbed_official | 85:e1a8e879a6a9 | 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 | 85:e1a8e879a6a9 | 99 | |
mbed_official | 85:e1a8e879a6a9 | 100 | obj->spis->PSELMOSI = mosi; |
mbed_official | 85:e1a8e879a6a9 | 101 | obj->spis->PSELMISO = miso; |
mbed_official | 85:e1a8e879a6a9 | 102 | obj->spis->PSELSCK = sclk; |
mbed_official | 85:e1a8e879a6a9 | 103 | obj->spis->PSELCSN = ssel; |
mbed_official | 85:e1a8e879a6a9 | 104 | |
mbed_official | 85:e1a8e879a6a9 | 105 | obj->spis->EVENTS_END=0; |
mbed_official | 85:e1a8e879a6a9 | 106 | obj->spis->EVENTS_ACQUIRED=0; |
mbed_official | 85:e1a8e879a6a9 | 107 | obj->spis->MAXRX=SPIS_MESSAGE_SIZE; |
mbed_official | 85:e1a8e879a6a9 | 108 | obj->spis->MAXTX=SPIS_MESSAGE_SIZE; |
mbed_official | 85:e1a8e879a6a9 | 109 | obj->spis->TXDPTR = (uint32_t)&m_tx_buf[0]; |
mbed_official | 227:7bd0639b8911 | 110 | obj->spis->RXDPTR = (uint32_t)&m_rx_buf[0]; |
mbed_official | 85:e1a8e879a6a9 | 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 | 85:e1a8e879a6a9 | 114 | } |
mbed_official | 85:e1a8e879a6a9 | 115 | else{//master |
mbed_official | 85:e1a8e879a6a9 | 116 | obj->spi->POWER=0; |
mbed_official | 85:e1a8e879a6a9 | 117 | obj->spi->POWER=1; |
mbed_official | 85:e1a8e879a6a9 | 118 | |
mbed_official | 227:7bd0639b8911 | 119 | //NRF_GPIO->DIR |= (1<<mosi); |
mbed_official | 85:e1a8e879a6a9 | 120 | NRF_GPIO->PIN_CNF[mosi] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 121 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 122 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
mbed_official | 85:e1a8e879a6a9 | 123 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
mbed_official | 85:e1a8e879a6a9 | 124 | | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); |
mbed_official | 85:e1a8e879a6a9 | 125 | obj->spi->PSELMOSI = mosi; |
mbed_official | 85:e1a8e879a6a9 | 126 | |
mbed_official | 85:e1a8e879a6a9 | 127 | NRF_GPIO->PIN_CNF[sclk] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 128 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 129 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
mbed_official | 85:e1a8e879a6a9 | 130 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
mbed_official | 85:e1a8e879a6a9 | 131 | | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); |
mbed_official | 85:e1a8e879a6a9 | 132 | obj->spi->PSELSCK = sclk; |
mbed_official | 85:e1a8e879a6a9 | 133 | |
mbed_official | 85:e1a8e879a6a9 | 134 | //NRF_GPIO->DIR &= ~(1<<miso); |
mbed_official | 85:e1a8e879a6a9 | 135 | NRF_GPIO->PIN_CNF[miso] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 136 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 137 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
mbed_official | 85:e1a8e879a6a9 | 138 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
mbed_official | 85:e1a8e879a6a9 | 139 | | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
mbed_official | 85:e1a8e879a6a9 | 140 | |
mbed_official | 85:e1a8e879a6a9 | 141 | obj->spi->PSELMISO = miso; |
mbed_official | 85:e1a8e879a6a9 | 142 | |
mbed_official | 227:7bd0639b8911 | 143 | obj->spi->EVENTS_READY = 0U; |
mbed_official | 85:e1a8e879a6a9 | 144 | |
mbed_official | 85:e1a8e879a6a9 | 145 | spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master |
mbed_official | 227:7bd0639b8911 | 146 | spi_frequency(obj, 1000000); |
mbed_official | 85:e1a8e879a6a9 | 147 | } |
mbed_official | 85:e1a8e879a6a9 | 148 | |
mbed_official | 85:e1a8e879a6a9 | 149 | } |
mbed_official | 85:e1a8e879a6a9 | 150 | |
mbed_official | 85:e1a8e879a6a9 | 151 | void spi_free(spi_t *obj) {} |
mbed_official | 85:e1a8e879a6a9 | 152 | |
mbed_official | 85:e1a8e879a6a9 | 153 | static inline void spi_disable(spi_t *obj, int slave) { |
mbed_official | 85:e1a8e879a6a9 | 154 | if(slave){ |
mbed_official | 85:e1a8e879a6a9 | 155 | obj->spis->ENABLE = (SPIS_ENABLE_ENABLE_Disabled << SPIS_ENABLE_ENABLE_Pos); |
mbed_official | 85:e1a8e879a6a9 | 156 | } |
mbed_official | 85:e1a8e879a6a9 | 157 | else{ |
mbed_official | 85:e1a8e879a6a9 | 158 | obj->spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); |
mbed_official | 85:e1a8e879a6a9 | 159 | } |
mbed_official | 85:e1a8e879a6a9 | 160 | } |
mbed_official | 85:e1a8e879a6a9 | 161 | |
mbed_official | 85:e1a8e879a6a9 | 162 | static inline void spi_enable(spi_t *obj, int slave) { |
mbed_official | 85:e1a8e879a6a9 | 163 | if(slave){ |
mbed_official | 85:e1a8e879a6a9 | 164 | obj->spis->ENABLE = (SPIS_ENABLE_ENABLE_Enabled << SPIS_ENABLE_ENABLE_Pos); |
mbed_official | 85:e1a8e879a6a9 | 165 | } |
mbed_official | 85:e1a8e879a6a9 | 166 | else{ |
mbed_official | 85:e1a8e879a6a9 | 167 | obj->spi->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos); |
mbed_official | 85:e1a8e879a6a9 | 168 | } |
mbed_official | 85:e1a8e879a6a9 | 169 | } |
mbed_official | 85:e1a8e879a6a9 | 170 | |
mbed_official | 85:e1a8e879a6a9 | 171 | void spi_format(spi_t *obj, int bits, int mode, int slave) { |
mbed_official | 209:137057ef9d1e | 172 | uint32_t config_mode = 0; |
mbed_official | 85:e1a8e879a6a9 | 173 | spi_disable(obj,slave); |
mbed_official | 85:e1a8e879a6a9 | 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 | 85:e1a8e879a6a9 | 178 | |
mbed_official | 85:e1a8e879a6a9 | 179 | switch (mode) |
mbed_official | 85:e1a8e879a6a9 | 180 | { |
mbed_official | 85:e1a8e879a6a9 | 181 | case 0: |
mbed_official | 85:e1a8e879a6a9 | 182 | config_mode = (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); |
mbed_official | 85:e1a8e879a6a9 | 183 | break; |
mbed_official | 85:e1a8e879a6a9 | 184 | case 1: |
mbed_official | 85:e1a8e879a6a9 | 185 | config_mode = (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); |
mbed_official | 85:e1a8e879a6a9 | 186 | break; |
mbed_official | 85:e1a8e879a6a9 | 187 | case 2: |
mbed_official | 85:e1a8e879a6a9 | 188 | config_mode = (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); |
mbed_official | 85:e1a8e879a6a9 | 189 | break; |
mbed_official | 85:e1a8e879a6a9 | 190 | case 3: |
mbed_official | 85:e1a8e879a6a9 | 191 | config_mode = (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); |
mbed_official | 85:e1a8e879a6a9 | 192 | break; |
mbed_official | 85:e1a8e879a6a9 | 193 | default: |
mbed_official | 85:e1a8e879a6a9 | 194 | error("SPI format error"); |
mbed_official | 85:e1a8e879a6a9 | 195 | break; |
mbed_official | 85:e1a8e879a6a9 | 196 | } |
mbed_official | 85:e1a8e879a6a9 | 197 | //default to msb first |
mbed_official | 85:e1a8e879a6a9 | 198 | if(slave){ |
mbed_official | 227:7bd0639b8911 | 199 | obj->spis->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos) ); |
mbed_official | 85:e1a8e879a6a9 | 200 | } |
mbed_official | 85:e1a8e879a6a9 | 201 | else{ |
mbed_official | 227:7bd0639b8911 | 202 | obj->spi->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos) ); |
mbed_official | 85:e1a8e879a6a9 | 203 | } |
mbed_official | 85:e1a8e879a6a9 | 204 | |
mbed_official | 85:e1a8e879a6a9 | 205 | spi_enable(obj,slave); |
mbed_official | 85:e1a8e879a6a9 | 206 | } |
mbed_official | 85:e1a8e879a6a9 | 207 | |
mbed_official | 85:e1a8e879a6a9 | 208 | void spi_frequency(spi_t *obj, int hz) { |
mbed_official | 85:e1a8e879a6a9 | 209 | if((int)obj->spi==NC) |
mbed_official | 85:e1a8e879a6a9 | 210 | return; |
mbed_official | 85:e1a8e879a6a9 | 211 | spi_disable(obj,0); |
mbed_official | 85:e1a8e879a6a9 | 212 | |
mbed_official | 85:e1a8e879a6a9 | 213 | if(hz<250000) { //125Kbps |
mbed_official | 227:7bd0639b8911 | 214 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K125; |
mbed_official | 85:e1a8e879a6a9 | 215 | } |
mbed_official | 85:e1a8e879a6a9 | 216 | else if(hz<500000){//250Kbps |
mbed_official | 85:e1a8e879a6a9 | 217 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K250; |
mbed_official | 85:e1a8e879a6a9 | 218 | } |
mbed_official | 85:e1a8e879a6a9 | 219 | else if(hz<1000000){//500Kbps |
mbed_official | 85:e1a8e879a6a9 | 220 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K500; |
mbed_official | 85:e1a8e879a6a9 | 221 | } |
mbed_official | 85:e1a8e879a6a9 | 222 | else if(hz<2000000){//1Mbps |
mbed_official | 85:e1a8e879a6a9 | 223 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M1; |
mbed_official | 85:e1a8e879a6a9 | 224 | } |
mbed_official | 85:e1a8e879a6a9 | 225 | else if(hz<4000000){//2Mbps |
mbed_official | 85:e1a8e879a6a9 | 226 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M2; |
mbed_official | 85:e1a8e879a6a9 | 227 | } |
mbed_official | 85:e1a8e879a6a9 | 228 | else if(hz<8000000){//4Mbps |
mbed_official | 85:e1a8e879a6a9 | 229 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M4; |
mbed_official | 85:e1a8e879a6a9 | 230 | } |
mbed_official | 85:e1a8e879a6a9 | 231 | else{//8Mbps |
mbed_official | 227:7bd0639b8911 | 232 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M8; |
mbed_official | 85:e1a8e879a6a9 | 233 | } |
mbed_official | 85:e1a8e879a6a9 | 234 | |
mbed_official | 85:e1a8e879a6a9 | 235 | spi_enable(obj,0); |
mbed_official | 85:e1a8e879a6a9 | 236 | } |
mbed_official | 85:e1a8e879a6a9 | 237 | |
mbed_official | 85:e1a8e879a6a9 | 238 | static inline int spi_readable(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 239 | return (obj->spi->EVENTS_READY == 1); |
mbed_official | 85:e1a8e879a6a9 | 240 | } |
mbed_official | 85:e1a8e879a6a9 | 241 | |
mbed_official | 85:e1a8e879a6a9 | 242 | static inline int spi_writeable(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 243 | return (obj->spi->EVENTS_READY == 0); |
mbed_official | 85:e1a8e879a6a9 | 244 | } |
mbed_official | 85:e1a8e879a6a9 | 245 | |
mbed_official | 85:e1a8e879a6a9 | 246 | |
mbed_official | 85:e1a8e879a6a9 | 247 | static inline int spi_read(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 248 | while (!spi_readable(obj)){ |
mbed_official | 85:e1a8e879a6a9 | 249 | } |
mbed_official | 85:e1a8e879a6a9 | 250 | |
mbed_official | 85:e1a8e879a6a9 | 251 | obj->spi->EVENTS_READY =0; |
mbed_official | 85:e1a8e879a6a9 | 252 | return (int)obj->spi->RXD; |
mbed_official | 85:e1a8e879a6a9 | 253 | } |
mbed_official | 85:e1a8e879a6a9 | 254 | |
mbed_official | 85:e1a8e879a6a9 | 255 | int spi_master_write(spi_t *obj, int value) { |
mbed_official | 85:e1a8e879a6a9 | 256 | while (!spi_writeable(obj)){ |
mbed_official | 85:e1a8e879a6a9 | 257 | } |
mbed_official | 85:e1a8e879a6a9 | 258 | obj->spi->TXD = (uint32_t)value; |
mbed_official | 85:e1a8e879a6a9 | 259 | return spi_read(obj); |
mbed_official | 85:e1a8e879a6a9 | 260 | } |
mbed_official | 85:e1a8e879a6a9 | 261 | |
mbed_official | 227:7bd0639b8911 | 262 | //static inline int spis_writeable(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 263 | // return (obj->spis->EVENTS_ACQUIRED==1); |
mbed_official | 85:e1a8e879a6a9 | 264 | //} |
mbed_official | 85:e1a8e879a6a9 | 265 | |
mbed_official | 85:e1a8e879a6a9 | 266 | int spi_slave_receive(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 267 | return obj->spis->EVENTS_END; |
mbed_official | 85:e1a8e879a6a9 | 268 | }; |
mbed_official | 85:e1a8e879a6a9 | 269 | |
mbed_official | 227:7bd0639b8911 | 270 | int spi_slave_read(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 271 | return m_rx_buf[0]; |
mbed_official | 85:e1a8e879a6a9 | 272 | } |
mbed_official | 85:e1a8e879a6a9 | 273 | |
mbed_official | 227:7bd0639b8911 | 274 | void spi_slave_write(spi_t *obj, int value) { |
mbed_official | 227:7bd0639b8911 | 275 | m_tx_buf[0]= value & 0xFF; |
mbed_official | 85:e1a8e879a6a9 | 276 | obj->spis->TASKS_RELEASE=1; |
mbed_official | 85:e1a8e879a6a9 | 277 | obj->spis->EVENTS_ACQUIRED=0; |
mbed_official | 85:e1a8e879a6a9 | 278 | obj->spis->EVENTS_END=0; |
mbed_official | 85:e1a8e879a6a9 | 279 | } |