mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins
Fork of mbed-dev by
targets/TARGET_TOSHIBA/TARGET_TMPM46B/spi_api.c@184:08ed48f1de7f, 2018-04-19 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Apr 19 17:12:19 2018 +0100
- Revision:
- 184:08ed48f1de7f
mbed-dev library. Release version 161
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 184:08ed48f1de7f | 1 | /* mbed Microcontroller Library |
AnnaBridge | 184:08ed48f1de7f | 2 | ******************************************************************************* |
AnnaBridge | 184:08ed48f1de7f | 3 | * (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved |
AnnaBridge | 184:08ed48f1de7f | 4 | * All rights reserved. |
AnnaBridge | 184:08ed48f1de7f | 5 | * |
AnnaBridge | 184:08ed48f1de7f | 6 | * Redistribution and use in source and binary forms, with or without |
AnnaBridge | 184:08ed48f1de7f | 7 | * modification, are permitted provided that the following conditions are met: |
AnnaBridge | 184:08ed48f1de7f | 8 | * |
AnnaBridge | 184:08ed48f1de7f | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
AnnaBridge | 184:08ed48f1de7f | 10 | * this list of conditions and the following disclaimer. |
AnnaBridge | 184:08ed48f1de7f | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
AnnaBridge | 184:08ed48f1de7f | 12 | * this list of conditions and the following disclaimer in the documentation |
AnnaBridge | 184:08ed48f1de7f | 13 | * and/or other materials provided with the distribution. |
AnnaBridge | 184:08ed48f1de7f | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
AnnaBridge | 184:08ed48f1de7f | 15 | * may be used to endorse or promote products derived from this software |
AnnaBridge | 184:08ed48f1de7f | 16 | * without specific prior written permission. |
AnnaBridge | 184:08ed48f1de7f | 17 | * |
AnnaBridge | 184:08ed48f1de7f | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
AnnaBridge | 184:08ed48f1de7f | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
AnnaBridge | 184:08ed48f1de7f | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
AnnaBridge | 184:08ed48f1de7f | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
AnnaBridge | 184:08ed48f1de7f | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
AnnaBridge | 184:08ed48f1de7f | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
AnnaBridge | 184:08ed48f1de7f | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
AnnaBridge | 184:08ed48f1de7f | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
AnnaBridge | 184:08ed48f1de7f | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
AnnaBridge | 184:08ed48f1de7f | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
AnnaBridge | 184:08ed48f1de7f | 28 | ******************************************************************************* |
AnnaBridge | 184:08ed48f1de7f | 29 | */ |
AnnaBridge | 184:08ed48f1de7f | 30 | #include "spi_api.h" |
AnnaBridge | 184:08ed48f1de7f | 31 | #include "mbed_error.h" |
AnnaBridge | 184:08ed48f1de7f | 32 | #include "pinmap.h" |
AnnaBridge | 184:08ed48f1de7f | 33 | #include "tmpm46b_ssp.h" |
AnnaBridge | 184:08ed48f1de7f | 34 | |
AnnaBridge | 184:08ed48f1de7f | 35 | static const PinMap PinMap_SPI_SCLK[] = { |
AnnaBridge | 184:08ed48f1de7f | 36 | {PK4, SPI_0, PIN_DATA(2, 1)}, |
AnnaBridge | 184:08ed48f1de7f | 37 | {PF3, SPI_1, PIN_DATA(5, 1)}, |
AnnaBridge | 184:08ed48f1de7f | 38 | {PD3, SPI_2, PIN_DATA(1, 1)}, |
AnnaBridge | 184:08ed48f1de7f | 39 | {NC, NC, 0} |
AnnaBridge | 184:08ed48f1de7f | 40 | }; |
AnnaBridge | 184:08ed48f1de7f | 41 | |
AnnaBridge | 184:08ed48f1de7f | 42 | static const PinMap PinMap_SPI_MOSI[] = { |
AnnaBridge | 184:08ed48f1de7f | 43 | {PK3, SPI_0, PIN_DATA(2, 1)}, |
AnnaBridge | 184:08ed48f1de7f | 44 | {PF4, SPI_1, PIN_DATA(5, 1)}, |
AnnaBridge | 184:08ed48f1de7f | 45 | {PD2, SPI_2, PIN_DATA(1, 1)}, |
AnnaBridge | 184:08ed48f1de7f | 46 | {NC, NC, 0} |
AnnaBridge | 184:08ed48f1de7f | 47 | }; |
AnnaBridge | 184:08ed48f1de7f | 48 | |
AnnaBridge | 184:08ed48f1de7f | 49 | static const PinMap PinMap_SPI_MISO[] = { |
AnnaBridge | 184:08ed48f1de7f | 50 | {PK2, SPI_0, PIN_DATA(2, 0)}, |
AnnaBridge | 184:08ed48f1de7f | 51 | {PF5, SPI_1, PIN_DATA(5, 0)}, |
AnnaBridge | 184:08ed48f1de7f | 52 | {PD1, SPI_2, PIN_DATA(1, 0)}, |
AnnaBridge | 184:08ed48f1de7f | 53 | {NC, NC, 0} |
AnnaBridge | 184:08ed48f1de7f | 54 | }; |
AnnaBridge | 184:08ed48f1de7f | 55 | |
AnnaBridge | 184:08ed48f1de7f | 56 | static const PinMap PinMap_SPI_SSEL[] = { |
AnnaBridge | 184:08ed48f1de7f | 57 | {PK1, SPI_0, PIN_DATA(2, 1)}, |
AnnaBridge | 184:08ed48f1de7f | 58 | {PF6, SPI_1, PIN_DATA(5, 1)}, |
AnnaBridge | 184:08ed48f1de7f | 59 | {PD0, SPI_2, PIN_DATA(1, 1)}, |
AnnaBridge | 184:08ed48f1de7f | 60 | {NC, NC, 0} |
AnnaBridge | 184:08ed48f1de7f | 61 | }; |
AnnaBridge | 184:08ed48f1de7f | 62 | |
AnnaBridge | 184:08ed48f1de7f | 63 | #define TMPM46B_SPI_2_FMAX 20000000 |
AnnaBridge | 184:08ed48f1de7f | 64 | #define TMPM46B_SPI_FMAX 10000000 |
AnnaBridge | 184:08ed48f1de7f | 65 | |
AnnaBridge | 184:08ed48f1de7f | 66 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) |
AnnaBridge | 184:08ed48f1de7f | 67 | { |
AnnaBridge | 184:08ed48f1de7f | 68 | SSP_InitTypeDef config; |
AnnaBridge | 184:08ed48f1de7f | 69 | |
AnnaBridge | 184:08ed48f1de7f | 70 | // Check pin parameters |
AnnaBridge | 184:08ed48f1de7f | 71 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
AnnaBridge | 184:08ed48f1de7f | 72 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
AnnaBridge | 184:08ed48f1de7f | 73 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
AnnaBridge | 184:08ed48f1de7f | 74 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
AnnaBridge | 184:08ed48f1de7f | 75 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
AnnaBridge | 184:08ed48f1de7f | 76 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
AnnaBridge | 184:08ed48f1de7f | 77 | |
AnnaBridge | 184:08ed48f1de7f | 78 | obj->module = (SPIName)pinmap_merge(spi_data, spi_sclk); |
AnnaBridge | 184:08ed48f1de7f | 79 | obj->module = (SPIName)pinmap_merge(spi_data, spi_cntl); |
AnnaBridge | 184:08ed48f1de7f | 80 | MBED_ASSERT((int)obj->module!= NC); |
AnnaBridge | 184:08ed48f1de7f | 81 | |
AnnaBridge | 184:08ed48f1de7f | 82 | // Identify SPI module to use |
AnnaBridge | 184:08ed48f1de7f | 83 | switch ((int)obj->module) { |
AnnaBridge | 184:08ed48f1de7f | 84 | case SPI_0: |
AnnaBridge | 184:08ed48f1de7f | 85 | obj->spi = TSB_SSP0; |
AnnaBridge | 184:08ed48f1de7f | 86 | break; |
AnnaBridge | 184:08ed48f1de7f | 87 | case SPI_1: |
AnnaBridge | 184:08ed48f1de7f | 88 | obj->spi = TSB_SSP1; |
AnnaBridge | 184:08ed48f1de7f | 89 | break; |
AnnaBridge | 184:08ed48f1de7f | 90 | case SPI_2: |
AnnaBridge | 184:08ed48f1de7f | 91 | obj->spi = TSB_SSP2; |
AnnaBridge | 184:08ed48f1de7f | 92 | break; |
AnnaBridge | 184:08ed48f1de7f | 93 | default: |
AnnaBridge | 184:08ed48f1de7f | 94 | obj->spi= NULL; |
AnnaBridge | 184:08ed48f1de7f | 95 | obj->module = (SPIName)NC; |
AnnaBridge | 184:08ed48f1de7f | 96 | error("Cannot found SPI module corresponding with input pins."); |
AnnaBridge | 184:08ed48f1de7f | 97 | break; |
AnnaBridge | 184:08ed48f1de7f | 98 | } |
AnnaBridge | 184:08ed48f1de7f | 99 | |
AnnaBridge | 184:08ed48f1de7f | 100 | // pin out the spi pins |
AnnaBridge | 184:08ed48f1de7f | 101 | pinmap_pinout(mosi, PinMap_SPI_MOSI); |
AnnaBridge | 184:08ed48f1de7f | 102 | pinmap_pinout(miso, PinMap_SPI_MISO); |
AnnaBridge | 184:08ed48f1de7f | 103 | pinmap_pinout(sclk, PinMap_SPI_SCLK); |
AnnaBridge | 184:08ed48f1de7f | 104 | |
AnnaBridge | 184:08ed48f1de7f | 105 | if (ssel != NC) { |
AnnaBridge | 184:08ed48f1de7f | 106 | pinmap_pinout(ssel, PinMap_SPI_SSEL); |
AnnaBridge | 184:08ed48f1de7f | 107 | } |
AnnaBridge | 184:08ed48f1de7f | 108 | |
AnnaBridge | 184:08ed48f1de7f | 109 | // Declare Config |
AnnaBridge | 184:08ed48f1de7f | 110 | config.FrameFormat = SSP_FORMAT_SPI; |
AnnaBridge | 184:08ed48f1de7f | 111 | |
AnnaBridge | 184:08ed48f1de7f | 112 | // bit_rate = Fsys / (clk_prescale * (clk_rate + 1)) |
AnnaBridge | 184:08ed48f1de7f | 113 | config.PreScale = 48; |
AnnaBridge | 184:08ed48f1de7f | 114 | config.ClkRate = 0; |
AnnaBridge | 184:08ed48f1de7f | 115 | |
AnnaBridge | 184:08ed48f1de7f | 116 | config.ClkPolarity = SSP_POLARITY_LOW; |
AnnaBridge | 184:08ed48f1de7f | 117 | config.ClkPhase = SSP_PHASE_FIRST_EDGE; |
AnnaBridge | 184:08ed48f1de7f | 118 | config.DataSize = 0x08; |
AnnaBridge | 184:08ed48f1de7f | 119 | |
AnnaBridge | 184:08ed48f1de7f | 120 | obj->bits = config.DataSize; |
AnnaBridge | 184:08ed48f1de7f | 121 | config.Mode = SSP_MASTER; |
AnnaBridge | 184:08ed48f1de7f | 122 | SSP_Init(obj->spi, &config); |
AnnaBridge | 184:08ed48f1de7f | 123 | |
AnnaBridge | 184:08ed48f1de7f | 124 | // Disable all interrupt |
AnnaBridge | 184:08ed48f1de7f | 125 | |
AnnaBridge | 184:08ed48f1de7f | 126 | SSP_SetINTConfig(obj->spi, SSP_INTCFG_NONE); |
AnnaBridge | 184:08ed48f1de7f | 127 | SSP_Enable(obj->spi); |
AnnaBridge | 184:08ed48f1de7f | 128 | } |
AnnaBridge | 184:08ed48f1de7f | 129 | |
AnnaBridge | 184:08ed48f1de7f | 130 | void spi_free(spi_t *obj) |
AnnaBridge | 184:08ed48f1de7f | 131 | { |
AnnaBridge | 184:08ed48f1de7f | 132 | SSP_Disable(obj->spi); |
AnnaBridge | 184:08ed48f1de7f | 133 | obj->spi = NULL; |
AnnaBridge | 184:08ed48f1de7f | 134 | obj->module = (SPIName)NC; |
AnnaBridge | 184:08ed48f1de7f | 135 | } |
AnnaBridge | 184:08ed48f1de7f | 136 | |
AnnaBridge | 184:08ed48f1de7f | 137 | void spi_format(spi_t *obj, int bits, int mode, int slave) |
AnnaBridge | 184:08ed48f1de7f | 138 | { |
AnnaBridge | 184:08ed48f1de7f | 139 | TSB_SSP_TypeDef* spi; |
AnnaBridge | 184:08ed48f1de7f | 140 | MBED_ASSERT(slave == SSP_MASTER); // Master mode only |
AnnaBridge | 184:08ed48f1de7f | 141 | |
AnnaBridge | 184:08ed48f1de7f | 142 | spi = obj->spi; |
AnnaBridge | 184:08ed48f1de7f | 143 | |
AnnaBridge | 184:08ed48f1de7f | 144 | SSP_Disable(spi); |
AnnaBridge | 184:08ed48f1de7f | 145 | |
AnnaBridge | 184:08ed48f1de7f | 146 | obj->bits = bits; |
AnnaBridge | 184:08ed48f1de7f | 147 | |
AnnaBridge | 184:08ed48f1de7f | 148 | SSP_SetDataSize(spi, bits); |
AnnaBridge | 184:08ed48f1de7f | 149 | SSP_SetClkPolarity(spi, (SSP_ClkPolarity)(mode & 0x1)); |
AnnaBridge | 184:08ed48f1de7f | 150 | SSP_SetClkPhase(spi, (SSP_ClkPhase)((mode >> 1) & 0x1)); |
AnnaBridge | 184:08ed48f1de7f | 151 | |
AnnaBridge | 184:08ed48f1de7f | 152 | SSP_Enable(spi); |
AnnaBridge | 184:08ed48f1de7f | 153 | } |
AnnaBridge | 184:08ed48f1de7f | 154 | |
AnnaBridge | 184:08ed48f1de7f | 155 | void spi_frequency(spi_t *obj, int hz) |
AnnaBridge | 184:08ed48f1de7f | 156 | { |
AnnaBridge | 184:08ed48f1de7f | 157 | TSB_SSP_TypeDef* spi; |
AnnaBridge | 184:08ed48f1de7f | 158 | |
AnnaBridge | 184:08ed48f1de7f | 159 | // Search Freq data |
AnnaBridge | 184:08ed48f1de7f | 160 | int fr_gear = 1; |
AnnaBridge | 184:08ed48f1de7f | 161 | int cur_hz = 1; |
AnnaBridge | 184:08ed48f1de7f | 162 | int32_t best_diff = TMPM46B_SPI_FMAX; |
AnnaBridge | 184:08ed48f1de7f | 163 | int best_cpsdvsr = 254; |
AnnaBridge | 184:08ed48f1de7f | 164 | int best_scr = 255; |
AnnaBridge | 184:08ed48f1de7f | 165 | int cur_cpsdvsr = 48; |
AnnaBridge | 184:08ed48f1de7f | 166 | int cur_scr = 0; |
AnnaBridge | 184:08ed48f1de7f | 167 | int32_t diff; |
AnnaBridge | 184:08ed48f1de7f | 168 | |
AnnaBridge | 184:08ed48f1de7f | 169 | /* Assert Min frequency |
AnnaBridge | 184:08ed48f1de7f | 170 | Hz = Fsys / (CPSDVSR * (SCR + 1)) |
AnnaBridge | 184:08ed48f1de7f | 171 | Domain value of CPSDVSR is an even number between 2 to 254 |
AnnaBridge | 184:08ed48f1de7f | 172 | Domain value of SCR is a number between 0 to 255 |
AnnaBridge | 184:08ed48f1de7f | 173 | Hz Min if CPSDVSR and SCR max (CPSDVSR = 254, SCR = 255) |
AnnaBridge | 184:08ed48f1de7f | 174 | */ |
AnnaBridge | 184:08ed48f1de7f | 175 | MBED_ASSERT((SystemCoreClock / 65024) <= (uint32_t)hz); |
AnnaBridge | 184:08ed48f1de7f | 176 | |
AnnaBridge | 184:08ed48f1de7f | 177 | if (obj->module == SPI_2) { |
AnnaBridge | 184:08ed48f1de7f | 178 | MBED_ASSERT(hz <= TMPM46B_SPI_2_FMAX); |
AnnaBridge | 184:08ed48f1de7f | 179 | } else { |
AnnaBridge | 184:08ed48f1de7f | 180 | MBED_ASSERT(hz <= TMPM46B_SPI_FMAX); // Default value of SPI_0, SPI_1, SPI_2 |
AnnaBridge | 184:08ed48f1de7f | 181 | } |
AnnaBridge | 184:08ed48f1de7f | 182 | |
AnnaBridge | 184:08ed48f1de7f | 183 | spi = obj->spi; |
AnnaBridge | 184:08ed48f1de7f | 184 | fr_gear = SystemCoreClock / hz; |
AnnaBridge | 184:08ed48f1de7f | 185 | if (fr_gear < 48) { |
AnnaBridge | 184:08ed48f1de7f | 186 | cur_cpsdvsr = fr_gear; |
AnnaBridge | 184:08ed48f1de7f | 187 | } |
AnnaBridge | 184:08ed48f1de7f | 188 | while (best_diff != 0 && cur_cpsdvsr <= 254) { |
AnnaBridge | 184:08ed48f1de7f | 189 | cur_scr = fr_gear / cur_cpsdvsr - 1; |
AnnaBridge | 184:08ed48f1de7f | 190 | |
AnnaBridge | 184:08ed48f1de7f | 191 | if (cur_scr < 0) { |
AnnaBridge | 184:08ed48f1de7f | 192 | break; |
AnnaBridge | 184:08ed48f1de7f | 193 | } |
AnnaBridge | 184:08ed48f1de7f | 194 | |
AnnaBridge | 184:08ed48f1de7f | 195 | for (; cur_scr < 256; ++cur_scr) { |
AnnaBridge | 184:08ed48f1de7f | 196 | cur_hz = SystemCoreClock / (cur_cpsdvsr * (1 + cur_scr)); |
AnnaBridge | 184:08ed48f1de7f | 197 | |
AnnaBridge | 184:08ed48f1de7f | 198 | diff = cur_hz - hz; |
AnnaBridge | 184:08ed48f1de7f | 199 | |
AnnaBridge | 184:08ed48f1de7f | 200 | if (diff < 0) { |
AnnaBridge | 184:08ed48f1de7f | 201 | diff = -diff; |
AnnaBridge | 184:08ed48f1de7f | 202 | } |
AnnaBridge | 184:08ed48f1de7f | 203 | |
AnnaBridge | 184:08ed48f1de7f | 204 | if (diff < best_diff) { |
AnnaBridge | 184:08ed48f1de7f | 205 | best_cpsdvsr = cur_cpsdvsr; |
AnnaBridge | 184:08ed48f1de7f | 206 | best_scr = cur_scr; |
AnnaBridge | 184:08ed48f1de7f | 207 | best_diff = diff; |
AnnaBridge | 184:08ed48f1de7f | 208 | } else if (diff >= best_diff) { |
AnnaBridge | 184:08ed48f1de7f | 209 | break; |
AnnaBridge | 184:08ed48f1de7f | 210 | } |
AnnaBridge | 184:08ed48f1de7f | 211 | } |
AnnaBridge | 184:08ed48f1de7f | 212 | |
AnnaBridge | 184:08ed48f1de7f | 213 | cur_cpsdvsr += 2; |
AnnaBridge | 184:08ed48f1de7f | 214 | } |
AnnaBridge | 184:08ed48f1de7f | 215 | |
AnnaBridge | 184:08ed48f1de7f | 216 | SSP_Disable(spi); |
AnnaBridge | 184:08ed48f1de7f | 217 | // Set bit rate of SPI |
AnnaBridge | 184:08ed48f1de7f | 218 | SSP_SetClkPreScale(spi, (uint8_t)best_cpsdvsr, (uint8_t)best_scr); |
AnnaBridge | 184:08ed48f1de7f | 219 | SSP_Enable(spi); |
AnnaBridge | 184:08ed48f1de7f | 220 | } |
AnnaBridge | 184:08ed48f1de7f | 221 | |
AnnaBridge | 184:08ed48f1de7f | 222 | static void spi_clear_FIFOs(TSB_SSP_TypeDef *spi) |
AnnaBridge | 184:08ed48f1de7f | 223 | { |
AnnaBridge | 184:08ed48f1de7f | 224 | SSP_FIFOState tx_buf_state, rx_buf_state; |
AnnaBridge | 184:08ed48f1de7f | 225 | |
AnnaBridge | 184:08ed48f1de7f | 226 | do { |
AnnaBridge | 184:08ed48f1de7f | 227 | while (SSP_GetWorkState(spi) == BUSY); |
AnnaBridge | 184:08ed48f1de7f | 228 | |
AnnaBridge | 184:08ed48f1de7f | 229 | // Get data from receive FIFO |
AnnaBridge | 184:08ed48f1de7f | 230 | SSP_GetRxData(spi); |
AnnaBridge | 184:08ed48f1de7f | 231 | |
AnnaBridge | 184:08ed48f1de7f | 232 | // Check receive FIFO |
AnnaBridge | 184:08ed48f1de7f | 233 | rx_buf_state = SSP_GetFIFOState(spi, SSP_RX); |
AnnaBridge | 184:08ed48f1de7f | 234 | |
AnnaBridge | 184:08ed48f1de7f | 235 | // Check transmit FIFO |
AnnaBridge | 184:08ed48f1de7f | 236 | tx_buf_state = SSP_GetFIFOState(spi, SSP_TX); |
AnnaBridge | 184:08ed48f1de7f | 237 | } while (rx_buf_state != SSP_FIFO_EMPTY || tx_buf_state != SSP_FIFO_EMPTY); |
AnnaBridge | 184:08ed48f1de7f | 238 | } |
AnnaBridge | 184:08ed48f1de7f | 239 | |
AnnaBridge | 184:08ed48f1de7f | 240 | int spi_master_write(spi_t *obj, int value) |
AnnaBridge | 184:08ed48f1de7f | 241 | { |
AnnaBridge | 184:08ed48f1de7f | 242 | TSB_SSP_TypeDef* spi; |
AnnaBridge | 184:08ed48f1de7f | 243 | |
AnnaBridge | 184:08ed48f1de7f | 244 | spi = obj->spi; |
AnnaBridge | 184:08ed48f1de7f | 245 | // Clear all data in transmit FIFO and receive FIFO |
AnnaBridge | 184:08ed48f1de7f | 246 | spi_clear_FIFOs(spi); |
AnnaBridge | 184:08ed48f1de7f | 247 | // Transmit data |
AnnaBridge | 184:08ed48f1de7f | 248 | SSP_SetTxData(spi, value); |
AnnaBridge | 184:08ed48f1de7f | 249 | // Wait for transmitting |
AnnaBridge | 184:08ed48f1de7f | 250 | while (SSP_GetWorkState(spi) == BUSY); |
AnnaBridge | 184:08ed48f1de7f | 251 | // Read received data |
AnnaBridge | 184:08ed48f1de7f | 252 | value = SSP_GetRxData(spi); |
AnnaBridge | 184:08ed48f1de7f | 253 | |
AnnaBridge | 184:08ed48f1de7f | 254 | return value; |
AnnaBridge | 184:08ed48f1de7f | 255 | } |
AnnaBridge | 184:08ed48f1de7f | 256 | |
AnnaBridge | 184:08ed48f1de7f | 257 | int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, |
AnnaBridge | 184:08ed48f1de7f | 258 | char *rx_buffer, int rx_length, char write_fill) |
AnnaBridge | 184:08ed48f1de7f | 259 | { |
AnnaBridge | 184:08ed48f1de7f | 260 | int total = (tx_length > rx_length) ? tx_length : rx_length; |
AnnaBridge | 184:08ed48f1de7f | 261 | |
AnnaBridge | 184:08ed48f1de7f | 262 | for (int i = 0; i < total; i++) { |
AnnaBridge | 184:08ed48f1de7f | 263 | char out = (i < tx_length) ? tx_buffer[i] : write_fill; |
AnnaBridge | 184:08ed48f1de7f | 264 | char in = spi_master_write(obj, out); |
AnnaBridge | 184:08ed48f1de7f | 265 | if (i < rx_length) { |
AnnaBridge | 184:08ed48f1de7f | 266 | rx_buffer[i] = in; |
AnnaBridge | 184:08ed48f1de7f | 267 | } |
AnnaBridge | 184:08ed48f1de7f | 268 | } |
AnnaBridge | 184:08ed48f1de7f | 269 | |
AnnaBridge | 184:08ed48f1de7f | 270 | return total; |
AnnaBridge | 184:08ed48f1de7f | 271 | } |
AnnaBridge | 184:08ed48f1de7f | 272 | |
AnnaBridge | 184:08ed48f1de7f | 273 | int spi_busy(spi_t *obj) |
AnnaBridge | 184:08ed48f1de7f | 274 | { |
AnnaBridge | 184:08ed48f1de7f | 275 | WorkState state; |
AnnaBridge | 184:08ed48f1de7f | 276 | state = SSP_GetWorkState(obj->spi); |
AnnaBridge | 184:08ed48f1de7f | 277 | return (state == BUSY); |
AnnaBridge | 184:08ed48f1de7f | 278 | } |
AnnaBridge | 184:08ed48f1de7f | 279 | |
AnnaBridge | 184:08ed48f1de7f | 280 | uint8_t spi_get_module(spi_t *obj) |
AnnaBridge | 184:08ed48f1de7f | 281 | { |
AnnaBridge | 184:08ed48f1de7f | 282 | return (uint8_t)(obj->module); |
AnnaBridge | 184:08ed48f1de7f | 283 | } |