mbed w/ spi bug fig
Fork of mbed-src by
targets/hal/TARGET_NORDIC/TARGET_NRF51822/spi_api.c@209:137057ef9d1e, 2014-05-23 (annotated)
- Committer:
- mbed_official
- Date:
- Fri May 23 11:30:06 2014 +0100
- Revision:
- 209:137057ef9d1e
- Parent:
- 104:a6a92e2e5a92
- Child:
- 227:7bd0639b8911
Synchronized with git revision f2b4c367e8b91e5b6acdf0058629d801456269a2
Full URL: https://github.com/mbedmicro/mbed/commit/f2b4c367e8b91e5b6acdf0058629d801456269a2/
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 | 85:e1a8e879a6a9 | 17 | #include "spi_api.h" |
mbed_official | 85:e1a8e879a6a9 | 18 | #include "cmsis.h" |
mbed_official | 85:e1a8e879a6a9 | 19 | #include "pinmap.h" |
mbed_official | 85:e1a8e879a6a9 | 20 | #include "error.h" |
mbed_official | 85:e1a8e879a6a9 | 21 | |
mbed_official | 85:e1a8e879a6a9 | 22 | static const PinMap PinMap_SPI_SCLK[] = { |
mbed_official | 85:e1a8e879a6a9 | 23 | {SPI_PSELSCK0 , SPI_0, 0x01}, |
mbed_official | 85:e1a8e879a6a9 | 24 | {SPI_PSELSCK1, SPI_1, 0x02}, |
mbed_official | 85:e1a8e879a6a9 | 25 | {SPIS_PSELSCK, SPIS, 0x03}, |
mbed_official | 85:e1a8e879a6a9 | 26 | {NC , NC , 0} |
mbed_official | 85:e1a8e879a6a9 | 27 | }; |
mbed_official | 85:e1a8e879a6a9 | 28 | |
mbed_official | 85:e1a8e879a6a9 | 29 | static const PinMap PinMap_SPI_MOSI[] = { |
mbed_official | 85:e1a8e879a6a9 | 30 | {SPI_PSELMOSI0 , SPI_0, 0x01}, |
mbed_official | 85:e1a8e879a6a9 | 31 | {SPI_PSELMOSI1, SPI_1, 0x02}, |
mbed_official | 85:e1a8e879a6a9 | 32 | {SPIS_PSELMOSI, SPIS, 0x03}, |
mbed_official | 85:e1a8e879a6a9 | 33 | {NC , NC , 0} |
mbed_official | 85:e1a8e879a6a9 | 34 | }; |
mbed_official | 85:e1a8e879a6a9 | 35 | |
mbed_official | 85:e1a8e879a6a9 | 36 | static const PinMap PinMap_SPI_MISO[] = { |
mbed_official | 85:e1a8e879a6a9 | 37 | {SPI_PSELMISO0 , SPI_0, 0x01}, |
mbed_official | 85:e1a8e879a6a9 | 38 | {SPI_PSELMISO1, SPI_1, 0x02}, |
mbed_official | 85:e1a8e879a6a9 | 39 | {SPIS_PSELMISO, SPIS, 0x03}, |
mbed_official | 85:e1a8e879a6a9 | 40 | {NC , NC , 0} |
mbed_official | 85:e1a8e879a6a9 | 41 | }; |
mbed_official | 85:e1a8e879a6a9 | 42 | |
mbed_official | 85:e1a8e879a6a9 | 43 | static const PinMap PinMap_SPI_SSEL[] = { |
mbed_official | 85:e1a8e879a6a9 | 44 | {SPIS_PSELSS, SPIS, 0x03}, |
mbed_official | 85:e1a8e879a6a9 | 45 | {NC , NC , 0} |
mbed_official | 85:e1a8e879a6a9 | 46 | }; |
mbed_official | 85:e1a8e879a6a9 | 47 | // {SPI_PSELSS0 , SPI_0, 0x01}, |
mbed_official | 85:e1a8e879a6a9 | 48 | #define SPIS_MESSAGE_SIZE 1 |
mbed_official | 85:e1a8e879a6a9 | 49 | volatile uint8_t m_tx_buf[SPIS_MESSAGE_SIZE] = {0}; |
mbed_official | 85:e1a8e879a6a9 | 50 | volatile uint8_t m_rx_buf[SPIS_MESSAGE_SIZE] = {0}; |
mbed_official | 85:e1a8e879a6a9 | 51 | |
mbed_official | 85:e1a8e879a6a9 | 52 | |
mbed_official | 85:e1a8e879a6a9 | 53 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { |
mbed_official | 85:e1a8e879a6a9 | 54 | // determine the SPI to use |
mbed_official | 85:e1a8e879a6a9 | 55 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
mbed_official | 85:e1a8e879a6a9 | 56 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
mbed_official | 85:e1a8e879a6a9 | 57 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
mbed_official | 85:e1a8e879a6a9 | 58 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
mbed_official | 85:e1a8e879a6a9 | 59 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
mbed_official | 85:e1a8e879a6a9 | 60 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
mbed_official | 85:e1a8e879a6a9 | 61 | SPIName spi = (SPIName)pinmap_merge(spi_data, spi_cntl); |
mbed_official | 85:e1a8e879a6a9 | 62 | //SPIName |
mbed_official | 85:e1a8e879a6a9 | 63 | if(ssel==NC){ |
mbed_official | 85:e1a8e879a6a9 | 64 | obj->spi = (NRF_SPI_Type*)spi; |
mbed_official | 85:e1a8e879a6a9 | 65 | obj->spis = (NRF_SPIS_Type*)NC; |
mbed_official | 85:e1a8e879a6a9 | 66 | } |
mbed_official | 85:e1a8e879a6a9 | 67 | else{ |
mbed_official | 85:e1a8e879a6a9 | 68 | obj->spi = (NRF_SPI_Type*)NC; |
mbed_official | 85:e1a8e879a6a9 | 69 | obj->spis = (NRF_SPIS_Type*)spi; |
mbed_official | 85:e1a8e879a6a9 | 70 | } |
mbed_official | 85:e1a8e879a6a9 | 71 | |
mbed_official | 85:e1a8e879a6a9 | 72 | if ((int)obj->spi == NC && (int)obj->spis == NC) { |
mbed_official | 85:e1a8e879a6a9 | 73 | error("SPI pinout mapping failed"); |
mbed_official | 85:e1a8e879a6a9 | 74 | } |
mbed_official | 85:e1a8e879a6a9 | 75 | // pin out the spi pins |
mbed_official | 85:e1a8e879a6a9 | 76 | if (ssel != NC) {//slave |
mbed_official | 85:e1a8e879a6a9 | 77 | obj->spis->POWER=0; |
mbed_official | 85:e1a8e879a6a9 | 78 | obj->spis->POWER=1; |
mbed_official | 85:e1a8e879a6a9 | 79 | |
mbed_official | 85:e1a8e879a6a9 | 80 | NRF_GPIO->PIN_CNF[mosi] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 81 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 82 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
mbed_official | 85:e1a8e879a6a9 | 83 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
mbed_official | 85:e1a8e879a6a9 | 84 | | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
mbed_official | 85:e1a8e879a6a9 | 85 | NRF_GPIO->PIN_CNF[miso] = (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[sclk] = (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[ssel] = (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 | |
mbed_official | 85:e1a8e879a6a9 | 101 | obj->spis->PSELMOSI = mosi; |
mbed_official | 85:e1a8e879a6a9 | 102 | obj->spis->PSELMISO = miso; |
mbed_official | 85:e1a8e879a6a9 | 103 | obj->spis->PSELSCK = sclk; |
mbed_official | 85:e1a8e879a6a9 | 104 | obj->spis->PSELCSN = ssel; |
mbed_official | 85:e1a8e879a6a9 | 105 | |
mbed_official | 85:e1a8e879a6a9 | 106 | obj->spis->EVENTS_END=0; |
mbed_official | 85:e1a8e879a6a9 | 107 | obj->spis->EVENTS_ACQUIRED=0; |
mbed_official | 85:e1a8e879a6a9 | 108 | obj->spis->MAXRX=SPIS_MESSAGE_SIZE; |
mbed_official | 85:e1a8e879a6a9 | 109 | obj->spis->MAXTX=SPIS_MESSAGE_SIZE; |
mbed_official | 85:e1a8e879a6a9 | 110 | obj->spis->TXDPTR = (uint32_t)&m_tx_buf[0]; |
mbed_official | 85:e1a8e879a6a9 | 111 | obj->spis->RXDPTR = (uint32_t)&m_rx_buf[0]; |
mbed_official | 85:e1a8e879a6a9 | 112 | obj->spis->SHORTS = (SPIS_SHORTS_END_ACQUIRE_Enabled<<SPIS_SHORTS_END_ACQUIRE_Pos); |
mbed_official | 85:e1a8e879a6a9 | 113 | |
mbed_official | 85:e1a8e879a6a9 | 114 | spi_format(obj, 8, 0, 1); // 8 bits, mode 0, slave |
mbed_official | 85:e1a8e879a6a9 | 115 | } |
mbed_official | 85:e1a8e879a6a9 | 116 | else{//master |
mbed_official | 85:e1a8e879a6a9 | 117 | obj->spi->POWER=0; |
mbed_official | 85:e1a8e879a6a9 | 118 | obj->spi->POWER=1; |
mbed_official | 85:e1a8e879a6a9 | 119 | |
mbed_official | 85:e1a8e879a6a9 | 120 | //NRF_GPIO->DIR |= (1<<mosi); |
mbed_official | 85:e1a8e879a6a9 | 121 | NRF_GPIO->PIN_CNF[mosi] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 122 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 123 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
mbed_official | 85:e1a8e879a6a9 | 124 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
mbed_official | 85:e1a8e879a6a9 | 125 | | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); |
mbed_official | 85:e1a8e879a6a9 | 126 | obj->spi->PSELMOSI = mosi; |
mbed_official | 85:e1a8e879a6a9 | 127 | |
mbed_official | 85:e1a8e879a6a9 | 128 | NRF_GPIO->PIN_CNF[sclk] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 129 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 130 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
mbed_official | 85:e1a8e879a6a9 | 131 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
mbed_official | 85:e1a8e879a6a9 | 132 | | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); |
mbed_official | 85:e1a8e879a6a9 | 133 | obj->spi->PSELSCK = sclk; |
mbed_official | 85:e1a8e879a6a9 | 134 | |
mbed_official | 85:e1a8e879a6a9 | 135 | //NRF_GPIO->DIR &= ~(1<<miso); |
mbed_official | 85:e1a8e879a6a9 | 136 | NRF_GPIO->PIN_CNF[miso] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 137 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
mbed_official | 85:e1a8e879a6a9 | 138 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
mbed_official | 85:e1a8e879a6a9 | 139 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
mbed_official | 85:e1a8e879a6a9 | 140 | | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
mbed_official | 85:e1a8e879a6a9 | 141 | |
mbed_official | 85:e1a8e879a6a9 | 142 | obj->spi->PSELMISO = miso; |
mbed_official | 85:e1a8e879a6a9 | 143 | |
mbed_official | 85:e1a8e879a6a9 | 144 | obj->spi->EVENTS_READY = 0U; |
mbed_official | 85:e1a8e879a6a9 | 145 | |
mbed_official | 85:e1a8e879a6a9 | 146 | spi_format(obj, 8, 0, 0); // 8 bits, mode 0, master |
mbed_official | 85:e1a8e879a6a9 | 147 | spi_frequency(obj, 1000000); |
mbed_official | 85:e1a8e879a6a9 | 148 | } |
mbed_official | 85:e1a8e879a6a9 | 149 | |
mbed_official | 85:e1a8e879a6a9 | 150 | } |
mbed_official | 85:e1a8e879a6a9 | 151 | |
mbed_official | 85:e1a8e879a6a9 | 152 | void spi_free(spi_t *obj) {} |
mbed_official | 85:e1a8e879a6a9 | 153 | |
mbed_official | 85:e1a8e879a6a9 | 154 | static inline void spi_disable(spi_t *obj, int slave) { |
mbed_official | 85:e1a8e879a6a9 | 155 | if(slave){ |
mbed_official | 85:e1a8e879a6a9 | 156 | obj->spis->ENABLE = (SPIS_ENABLE_ENABLE_Disabled << SPIS_ENABLE_ENABLE_Pos); |
mbed_official | 85:e1a8e879a6a9 | 157 | } |
mbed_official | 85:e1a8e879a6a9 | 158 | else{ |
mbed_official | 85:e1a8e879a6a9 | 159 | obj->spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); |
mbed_official | 85:e1a8e879a6a9 | 160 | } |
mbed_official | 85:e1a8e879a6a9 | 161 | } |
mbed_official | 85:e1a8e879a6a9 | 162 | |
mbed_official | 85:e1a8e879a6a9 | 163 | static inline void spi_enable(spi_t *obj, int slave) { |
mbed_official | 85:e1a8e879a6a9 | 164 | if(slave){ |
mbed_official | 85:e1a8e879a6a9 | 165 | obj->spis->ENABLE = (SPIS_ENABLE_ENABLE_Enabled << SPIS_ENABLE_ENABLE_Pos); |
mbed_official | 85:e1a8e879a6a9 | 166 | } |
mbed_official | 85:e1a8e879a6a9 | 167 | else{ |
mbed_official | 85:e1a8e879a6a9 | 168 | obj->spi->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos); |
mbed_official | 85:e1a8e879a6a9 | 169 | } |
mbed_official | 85:e1a8e879a6a9 | 170 | } |
mbed_official | 85:e1a8e879a6a9 | 171 | |
mbed_official | 85:e1a8e879a6a9 | 172 | void spi_format(spi_t *obj, int bits, int mode, int slave) { |
mbed_official | 209:137057ef9d1e | 173 | uint32_t config_mode = 0; |
mbed_official | 85:e1a8e879a6a9 | 174 | spi_disable(obj,slave); |
mbed_official | 85:e1a8e879a6a9 | 175 | |
mbed_official | 85:e1a8e879a6a9 | 176 | if (bits != 8) { |
mbed_official | 85:e1a8e879a6a9 | 177 | error("Only 8bits SPI supported"); |
mbed_official | 85:e1a8e879a6a9 | 178 | } |
mbed_official | 85:e1a8e879a6a9 | 179 | |
mbed_official | 85:e1a8e879a6a9 | 180 | switch (mode) |
mbed_official | 85:e1a8e879a6a9 | 181 | { |
mbed_official | 85:e1a8e879a6a9 | 182 | case 0: |
mbed_official | 85:e1a8e879a6a9 | 183 | config_mode = (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); |
mbed_official | 85:e1a8e879a6a9 | 184 | break; |
mbed_official | 85:e1a8e879a6a9 | 185 | case 1: |
mbed_official | 85:e1a8e879a6a9 | 186 | config_mode = (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); |
mbed_official | 85:e1a8e879a6a9 | 187 | break; |
mbed_official | 85:e1a8e879a6a9 | 188 | case 2: |
mbed_official | 85:e1a8e879a6a9 | 189 | config_mode = (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); |
mbed_official | 85:e1a8e879a6a9 | 190 | break; |
mbed_official | 85:e1a8e879a6a9 | 191 | case 3: |
mbed_official | 85:e1a8e879a6a9 | 192 | config_mode = (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos) | (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); |
mbed_official | 85:e1a8e879a6a9 | 193 | break; |
mbed_official | 85:e1a8e879a6a9 | 194 | default: |
mbed_official | 85:e1a8e879a6a9 | 195 | error("SPI format error"); |
mbed_official | 85:e1a8e879a6a9 | 196 | break; |
mbed_official | 85:e1a8e879a6a9 | 197 | } |
mbed_official | 85:e1a8e879a6a9 | 198 | //default to msb first |
mbed_official | 85:e1a8e879a6a9 | 199 | if(slave){ |
mbed_official | 85:e1a8e879a6a9 | 200 | obj->spis->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos) ); |
mbed_official | 85:e1a8e879a6a9 | 201 | } |
mbed_official | 85:e1a8e879a6a9 | 202 | else{ |
mbed_official | 85:e1a8e879a6a9 | 203 | obj->spi->CONFIG = (config_mode | (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos) ); |
mbed_official | 85:e1a8e879a6a9 | 204 | } |
mbed_official | 85:e1a8e879a6a9 | 205 | |
mbed_official | 85:e1a8e879a6a9 | 206 | spi_enable(obj,slave); |
mbed_official | 85:e1a8e879a6a9 | 207 | } |
mbed_official | 85:e1a8e879a6a9 | 208 | |
mbed_official | 85:e1a8e879a6a9 | 209 | void spi_frequency(spi_t *obj, int hz) { |
mbed_official | 85:e1a8e879a6a9 | 210 | if((int)obj->spi==NC) |
mbed_official | 85:e1a8e879a6a9 | 211 | return; |
mbed_official | 85:e1a8e879a6a9 | 212 | spi_disable(obj,0); |
mbed_official | 85:e1a8e879a6a9 | 213 | |
mbed_official | 85:e1a8e879a6a9 | 214 | if(hz<250000) { //125Kbps |
mbed_official | 85:e1a8e879a6a9 | 215 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K125; |
mbed_official | 85:e1a8e879a6a9 | 216 | } |
mbed_official | 85:e1a8e879a6a9 | 217 | else if(hz<500000){//250Kbps |
mbed_official | 85:e1a8e879a6a9 | 218 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K250; |
mbed_official | 85:e1a8e879a6a9 | 219 | } |
mbed_official | 85:e1a8e879a6a9 | 220 | else if(hz<1000000){//500Kbps |
mbed_official | 85:e1a8e879a6a9 | 221 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_K500; |
mbed_official | 85:e1a8e879a6a9 | 222 | } |
mbed_official | 85:e1a8e879a6a9 | 223 | else if(hz<2000000){//1Mbps |
mbed_official | 85:e1a8e879a6a9 | 224 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M1; |
mbed_official | 85:e1a8e879a6a9 | 225 | } |
mbed_official | 85:e1a8e879a6a9 | 226 | else if(hz<4000000){//2Mbps |
mbed_official | 85:e1a8e879a6a9 | 227 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M2; |
mbed_official | 85:e1a8e879a6a9 | 228 | } |
mbed_official | 85:e1a8e879a6a9 | 229 | else if(hz<8000000){//4Mbps |
mbed_official | 85:e1a8e879a6a9 | 230 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M4; |
mbed_official | 85:e1a8e879a6a9 | 231 | } |
mbed_official | 85:e1a8e879a6a9 | 232 | else{//8Mbps |
mbed_official | 85:e1a8e879a6a9 | 233 | obj->spi->FREQUENCY = (uint32_t) SPI_FREQUENCY_FREQUENCY_M8; |
mbed_official | 85:e1a8e879a6a9 | 234 | } |
mbed_official | 85:e1a8e879a6a9 | 235 | |
mbed_official | 85:e1a8e879a6a9 | 236 | spi_enable(obj,0); |
mbed_official | 85:e1a8e879a6a9 | 237 | } |
mbed_official | 85:e1a8e879a6a9 | 238 | |
mbed_official | 85:e1a8e879a6a9 | 239 | static inline int spi_readable(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 240 | return (obj->spi->EVENTS_READY == 1); |
mbed_official | 85:e1a8e879a6a9 | 241 | } |
mbed_official | 85:e1a8e879a6a9 | 242 | |
mbed_official | 85:e1a8e879a6a9 | 243 | static inline int spi_writeable(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 244 | return (obj->spi->EVENTS_READY == 0); |
mbed_official | 85:e1a8e879a6a9 | 245 | } |
mbed_official | 85:e1a8e879a6a9 | 246 | |
mbed_official | 85:e1a8e879a6a9 | 247 | |
mbed_official | 85:e1a8e879a6a9 | 248 | static inline int spi_read(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 249 | while (!spi_readable(obj)){ |
mbed_official | 85:e1a8e879a6a9 | 250 | } |
mbed_official | 85:e1a8e879a6a9 | 251 | |
mbed_official | 85:e1a8e879a6a9 | 252 | obj->spi->EVENTS_READY =0; |
mbed_official | 85:e1a8e879a6a9 | 253 | return (int)obj->spi->RXD; |
mbed_official | 85:e1a8e879a6a9 | 254 | } |
mbed_official | 85:e1a8e879a6a9 | 255 | |
mbed_official | 85:e1a8e879a6a9 | 256 | int spi_master_write(spi_t *obj, int value) { |
mbed_official | 85:e1a8e879a6a9 | 257 | while (!spi_writeable(obj)){ |
mbed_official | 85:e1a8e879a6a9 | 258 | } |
mbed_official | 85:e1a8e879a6a9 | 259 | obj->spi->TXD = (uint32_t)value; |
mbed_official | 85:e1a8e879a6a9 | 260 | return spi_read(obj); |
mbed_official | 85:e1a8e879a6a9 | 261 | } |
mbed_official | 85:e1a8e879a6a9 | 262 | |
mbed_official | 85:e1a8e879a6a9 | 263 | //static inline int spis_writeable(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 264 | // return (obj->spis->EVENTS_ACQUIRED==1); |
mbed_official | 85:e1a8e879a6a9 | 265 | //} |
mbed_official | 85:e1a8e879a6a9 | 266 | |
mbed_official | 85:e1a8e879a6a9 | 267 | int spi_slave_receive(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 268 | return obj->spis->EVENTS_END; |
mbed_official | 85:e1a8e879a6a9 | 269 | }; |
mbed_official | 85:e1a8e879a6a9 | 270 | |
mbed_official | 85:e1a8e879a6a9 | 271 | int spi_slave_read(spi_t *obj) { |
mbed_official | 85:e1a8e879a6a9 | 272 | return m_rx_buf[0]; |
mbed_official | 85:e1a8e879a6a9 | 273 | } |
mbed_official | 85:e1a8e879a6a9 | 274 | |
mbed_official | 85:e1a8e879a6a9 | 275 | void spi_slave_write(spi_t *obj, int value) { |
mbed_official | 85:e1a8e879a6a9 | 276 | m_tx_buf[0]= value & 0xFF; |
mbed_official | 85:e1a8e879a6a9 | 277 | obj->spis->TASKS_RELEASE=1; |
mbed_official | 85:e1a8e879a6a9 | 278 | obj->spis->EVENTS_ACQUIRED=0; |
mbed_official | 85:e1a8e879a6a9 | 279 | obj->spis->EVENTS_END=0; |
mbed_official | 85:e1a8e879a6a9 | 280 | } |