test
Fork of mbed-dev by
targets/TARGET_Realtek/TARGET_AMEBA/spi_api.c@178:d650f5d4c87a, 2017-11-08 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Nov 08 13:50:44 2017 +0000
- Revision:
- 178:d650f5d4c87a
- Parent:
- 171:19eb464bc2be
This updates the lib to the mbed lib v 155
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 168:e84263d55307 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 168:e84263d55307 | 2 | * Copyright (c) 2013-2016 Realtek Semiconductor Corp. |
AnnaBridge | 168:e84263d55307 | 3 | * |
AnnaBridge | 168:e84263d55307 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 168:e84263d55307 | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 168:e84263d55307 | 6 | * You may obtain a copy of the License at |
AnnaBridge | 168:e84263d55307 | 7 | * |
AnnaBridge | 168:e84263d55307 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 168:e84263d55307 | 9 | * |
AnnaBridge | 168:e84263d55307 | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 168:e84263d55307 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 168:e84263d55307 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 168:e84263d55307 | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 168:e84263d55307 | 14 | * limitations under the License. |
AnnaBridge | 168:e84263d55307 | 15 | */ |
AnnaBridge | 168:e84263d55307 | 16 | |
AnnaBridge | 168:e84263d55307 | 17 | #include "objects.h" |
AnnaBridge | 168:e84263d55307 | 18 | #include "spi_api.h" |
AnnaBridge | 168:e84263d55307 | 19 | |
AnnaBridge | 168:e84263d55307 | 20 | #include "PinNames.h" |
AnnaBridge | 168:e84263d55307 | 21 | #include "pinmap.h" |
AnnaBridge | 168:e84263d55307 | 22 | #include "hal_ssi.h" |
AnnaBridge | 168:e84263d55307 | 23 | |
AnnaBridge | 168:e84263d55307 | 24 | extern u32 SystemGetCpuClk(VOID); |
AnnaBridge | 168:e84263d55307 | 25 | extern VOID HAL_GPIO_PullCtrl(u32 pin, u32 mode); |
AnnaBridge | 168:e84263d55307 | 26 | |
AnnaBridge | 168:e84263d55307 | 27 | void spi_tx_done_callback(VOID *obj); |
AnnaBridge | 168:e84263d55307 | 28 | void spi_rx_done_callback(VOID *obj); |
AnnaBridge | 168:e84263d55307 | 29 | void spi_bus_tx_done_callback(VOID *obj); |
AnnaBridge | 168:e84263d55307 | 30 | |
AnnaBridge | 168:e84263d55307 | 31 | |
AnnaBridge | 168:e84263d55307 | 32 | //TODO: Load default Setting: It should be loaded from external setting file. |
AnnaBridge | 168:e84263d55307 | 33 | extern const DW_SSI_DEFAULT_SETTING SpiDefaultSetting; |
AnnaBridge | 168:e84263d55307 | 34 | |
AnnaBridge | 168:e84263d55307 | 35 | static const PinMap PinMap_SSI_MOSI[] = { |
AnnaBridge | 168:e84263d55307 | 36 | {PE_2, RTL_PIN_PERI(SPI0, 0, S0), RTL_PIN_FUNC(SPI0, S0)}, |
AnnaBridge | 168:e84263d55307 | 37 | {PC_2, RTL_PIN_PERI(SPI0, 0, S1), RTL_PIN_FUNC(SPI0, S1)}, |
AnnaBridge | 168:e84263d55307 | 38 | {PA_1, RTL_PIN_PERI(SPI1, 1, S0), RTL_PIN_FUNC(SPI1, S0)}, |
AnnaBridge | 168:e84263d55307 | 39 | {PB_6, RTL_PIN_PERI(SPI1, 1, S1), RTL_PIN_FUNC(SPI1, S1)}, |
AnnaBridge | 168:e84263d55307 | 40 | {PD_6, RTL_PIN_PERI(SPI1, 1, S2), RTL_PIN_FUNC(SPI1, S2)}, |
AnnaBridge | 168:e84263d55307 | 41 | {PG_2, RTL_PIN_PERI(SPI2, 2, S0), RTL_PIN_FUNC(SPI2, S0)}, |
AnnaBridge | 168:e84263d55307 | 42 | {PE_6, RTL_PIN_PERI(SPI2, 2, S1), RTL_PIN_FUNC(SPI2, S1)}, |
AnnaBridge | 168:e84263d55307 | 43 | {PD_2, RTL_PIN_PERI(SPI2, 2, S2), RTL_PIN_FUNC(SPI2, S2)}, |
AnnaBridge | 168:e84263d55307 | 44 | {NC, NC, 0} |
AnnaBridge | 168:e84263d55307 | 45 | }; |
AnnaBridge | 168:e84263d55307 | 46 | |
AnnaBridge | 168:e84263d55307 | 47 | static const PinMap PinMap_SSI_MISO[] = { |
AnnaBridge | 168:e84263d55307 | 48 | {PE_3, RTL_PIN_PERI(SPI0, 0, S0), RTL_PIN_FUNC(SPI0, S0)}, |
AnnaBridge | 168:e84263d55307 | 49 | {PC_3, RTL_PIN_PERI(SPI0, 0, S1), RTL_PIN_FUNC(SPI0, S1)}, |
AnnaBridge | 168:e84263d55307 | 50 | {PA_0, RTL_PIN_PERI(SPI1, 1, S0), RTL_PIN_FUNC(SPI1, S0)}, |
AnnaBridge | 168:e84263d55307 | 51 | {PB_7, RTL_PIN_PERI(SPI1, 1, S1), RTL_PIN_FUNC(SPI1, S1)}, |
AnnaBridge | 168:e84263d55307 | 52 | {PD_7, RTL_PIN_PERI(SPI1, 1, S2), RTL_PIN_FUNC(SPI1, S2)}, |
AnnaBridge | 168:e84263d55307 | 53 | {PG_3, RTL_PIN_PERI(SPI2, 2, S0), RTL_PIN_FUNC(SPI2, S0)}, |
AnnaBridge | 168:e84263d55307 | 54 | {PE_7, RTL_PIN_PERI(SPI2, 2, S1), RTL_PIN_FUNC(SPI2, S1)}, |
AnnaBridge | 168:e84263d55307 | 55 | {PD_3, RTL_PIN_PERI(SPI2, 2, S2), RTL_PIN_FUNC(SPI2, S2)}, |
AnnaBridge | 168:e84263d55307 | 56 | {NC, NC, 0} |
AnnaBridge | 168:e84263d55307 | 57 | }; |
AnnaBridge | 168:e84263d55307 | 58 | |
AnnaBridge | 168:e84263d55307 | 59 | |
AnnaBridge | 168:e84263d55307 | 60 | void spi_init (spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) |
AnnaBridge | 168:e84263d55307 | 61 | { |
AnnaBridge | 168:e84263d55307 | 62 | SSI_DBG_ENTRANCE("spi_init()\n"); |
AnnaBridge | 168:e84263d55307 | 63 | |
AnnaBridge | 168:e84263d55307 | 64 | uint32_t ssi_mosi, ssi_miso, ssi_peri; |
AnnaBridge | 168:e84263d55307 | 65 | uint8_t ssi_idx, ssi_pinmux; |
AnnaBridge | 168:e84263d55307 | 66 | PHAL_SSI_ADAPTOR pHalSsiAdaptor; |
AnnaBridge | 168:e84263d55307 | 67 | PHAL_SSI_OP pHalSsiOp; |
AnnaBridge | 168:e84263d55307 | 68 | |
AnnaBridge | 168:e84263d55307 | 69 | _memset((void*)obj, 0, sizeof(spi_t)); |
AnnaBridge | 168:e84263d55307 | 70 | obj->state = 0; |
AnnaBridge | 168:e84263d55307 | 71 | uint32_t SystemClock = SystemGetCpuClk(); |
AnnaBridge | 168:e84263d55307 | 72 | uint32_t MaxSsiFreq = (SystemClock >> 2) >> 1; |
AnnaBridge | 168:e84263d55307 | 73 | |
AnnaBridge | 168:e84263d55307 | 74 | /* SsiClockDivider doesn't support odd number */ |
AnnaBridge | 168:e84263d55307 | 75 | |
AnnaBridge | 168:e84263d55307 | 76 | DBG_SSI_INFO("SystemClock: %d\n", SystemClock); |
AnnaBridge | 168:e84263d55307 | 77 | DBG_SSI_INFO("MaxSsiFreq : %d\n", MaxSsiFreq); |
AnnaBridge | 168:e84263d55307 | 78 | |
AnnaBridge | 168:e84263d55307 | 79 | ssi_mosi = pinmap_peripheral(mosi, PinMap_SSI_MOSI); |
AnnaBridge | 168:e84263d55307 | 80 | ssi_miso = pinmap_peripheral(miso, PinMap_SSI_MISO); |
AnnaBridge | 168:e84263d55307 | 81 | |
AnnaBridge | 168:e84263d55307 | 82 | ssi_peri = pinmap_merge(ssi_mosi, ssi_miso); |
AnnaBridge | 168:e84263d55307 | 83 | if (unlikely(ssi_peri == NC)) { |
AnnaBridge | 168:e84263d55307 | 84 | DBG_SSI_ERR("spi_init(): Cannot find matched SSI index.\n"); |
AnnaBridge | 168:e84263d55307 | 85 | return; |
AnnaBridge | 168:e84263d55307 | 86 | } |
AnnaBridge | 168:e84263d55307 | 87 | obj->sclk = (u8)sclk; |
AnnaBridge | 168:e84263d55307 | 88 | ssi_idx = RTL_GET_PERI_IDX(ssi_peri); |
AnnaBridge | 168:e84263d55307 | 89 | ssi_pinmux = RTL_GET_PERI_SEL(ssi_peri); |
AnnaBridge | 168:e84263d55307 | 90 | DBG_SSI_INFO("ssi_peri: %d, ssi_idx: %d, ssi_pinmux: %d\n", ssi_peri, ssi_idx, ssi_pinmux); |
AnnaBridge | 168:e84263d55307 | 91 | |
AnnaBridge | 168:e84263d55307 | 92 | pHalSsiAdaptor = &obj->spi_adp; |
AnnaBridge | 168:e84263d55307 | 93 | pHalSsiOp = &obj->spi_op; |
AnnaBridge | 168:e84263d55307 | 94 | |
AnnaBridge | 168:e84263d55307 | 95 | pHalSsiAdaptor->Index = ssi_idx; |
AnnaBridge | 168:e84263d55307 | 96 | pHalSsiAdaptor->PinmuxSelect = ssi_pinmux; |
AnnaBridge | 168:e84263d55307 | 97 | pHalSsiAdaptor->Role = SSI_MASTER; |
AnnaBridge | 168:e84263d55307 | 98 | |
AnnaBridge | 168:e84263d55307 | 99 | HalSsiOpInit((VOID*)pHalSsiOp); |
AnnaBridge | 168:e84263d55307 | 100 | |
AnnaBridge | 168:e84263d55307 | 101 | pHalSsiOp->HalSsiSetDeviceRole(pHalSsiAdaptor, pHalSsiAdaptor->Role); |
AnnaBridge | 168:e84263d55307 | 102 | |
AnnaBridge | 168:e84263d55307 | 103 | /* Pinmux workaround */ |
AnnaBridge | 168:e84263d55307 | 104 | if ((ssi_idx == 0) && (ssi_pinmux == SSI0_MUX_TO_GPIOC)) { |
AnnaBridge | 168:e84263d55307 | 105 | EEPROM_PIN_CTRL(OFF); |
AnnaBridge | 168:e84263d55307 | 106 | } |
AnnaBridge | 168:e84263d55307 | 107 | |
AnnaBridge | 168:e84263d55307 | 108 | if ((ssi_idx == 0) && (ssi_pinmux == SSI0_MUX_TO_GPIOE)) { |
AnnaBridge | 168:e84263d55307 | 109 | DBG_SSI_WARN(ANSI_COLOR_MAGENTA"SPI0 Pin may conflict with JTAG\r\n"ANSI_COLOR_RESET); |
AnnaBridge | 168:e84263d55307 | 110 | } |
AnnaBridge | 168:e84263d55307 | 111 | |
AnnaBridge | 168:e84263d55307 | 112 | //TODO: Implement default setting structure. |
AnnaBridge | 168:e84263d55307 | 113 | pHalSsiOp->HalSsiLoadSetting(pHalSsiAdaptor, (void*)&SpiDefaultSetting); |
AnnaBridge | 168:e84263d55307 | 114 | pHalSsiAdaptor->DefaultRxThresholdLevel = SpiDefaultSetting.RxThresholdLevel; |
AnnaBridge | 168:e84263d55307 | 115 | |
AnnaBridge | 168:e84263d55307 | 116 | if(HalSsiInit(pHalSsiAdaptor) != HAL_OK){ |
AnnaBridge | 168:e84263d55307 | 117 | DBG_SSI_ERR(ANSI_COLOR_RED"spi_init(): SPI %x init fails.\n"ANSI_COLOR_RESET,pHalSsiAdaptor->Index); |
AnnaBridge | 168:e84263d55307 | 118 | return; |
AnnaBridge | 168:e84263d55307 | 119 | } |
AnnaBridge | 168:e84263d55307 | 120 | osDelay(1); |
AnnaBridge | 168:e84263d55307 | 121 | } |
AnnaBridge | 168:e84263d55307 | 122 | |
AnnaBridge | 168:e84263d55307 | 123 | void spi_free (spi_t *obj) |
AnnaBridge | 168:e84263d55307 | 124 | { |
AnnaBridge | 168:e84263d55307 | 125 | PHAL_SSI_ADAPTOR pHalSsiAdaptor; |
AnnaBridge | 168:e84263d55307 | 126 | pHalSsiAdaptor = &obj->spi_adp; |
AnnaBridge | 168:e84263d55307 | 127 | HalSsiDeInit(pHalSsiAdaptor); |
AnnaBridge | 168:e84263d55307 | 128 | |
AnnaBridge | 168:e84263d55307 | 129 | SPI0_MULTI_CS_CTRL(OFF); |
AnnaBridge | 168:e84263d55307 | 130 | } |
AnnaBridge | 168:e84263d55307 | 131 | |
AnnaBridge | 168:e84263d55307 | 132 | void spi_format (spi_t *obj, int bits, int mode, int slave) |
AnnaBridge | 168:e84263d55307 | 133 | { |
AnnaBridge | 168:e84263d55307 | 134 | PHAL_SSI_ADAPTOR pHalSsiAdaptor; |
AnnaBridge | 168:e84263d55307 | 135 | PHAL_SSI_OP pHalSsiOp; |
AnnaBridge | 168:e84263d55307 | 136 | |
AnnaBridge | 168:e84263d55307 | 137 | pHalSsiAdaptor = &obj->spi_adp; |
AnnaBridge | 168:e84263d55307 | 138 | pHalSsiOp = &obj->spi_op; |
AnnaBridge | 168:e84263d55307 | 139 | |
AnnaBridge | 168:e84263d55307 | 140 | pHalSsiAdaptor->DataFrameSize = (bits - 1); |
AnnaBridge | 168:e84263d55307 | 141 | |
AnnaBridge | 168:e84263d55307 | 142 | /* |
AnnaBridge | 168:e84263d55307 | 143 | * mode | POL PHA |
AnnaBridge | 168:e84263d55307 | 144 | * -----+-------- |
AnnaBridge | 168:e84263d55307 | 145 | * 0 | 0 0 |
AnnaBridge | 168:e84263d55307 | 146 | * 1 | 0 1 |
AnnaBridge | 168:e84263d55307 | 147 | * 2 | 1 0 |
AnnaBridge | 168:e84263d55307 | 148 | * 3 | 1 1 |
AnnaBridge | 168:e84263d55307 | 149 | * |
AnnaBridge | 168:e84263d55307 | 150 | * SCPOL_INACTIVE_IS_LOW = 0, |
AnnaBridge | 168:e84263d55307 | 151 | * SCPOL_INACTIVE_IS_HIGH = 1 |
AnnaBridge | 168:e84263d55307 | 152 | * |
AnnaBridge | 168:e84263d55307 | 153 | * SCPH_TOGGLES_IN_MIDDLE = 0, |
AnnaBridge | 168:e84263d55307 | 154 | * SCPH_TOGGLES_AT_START = 1 |
AnnaBridge | 168:e84263d55307 | 155 | */ |
AnnaBridge | 168:e84263d55307 | 156 | switch (mode) |
AnnaBridge | 168:e84263d55307 | 157 | { |
AnnaBridge | 168:e84263d55307 | 158 | case 0: |
AnnaBridge | 168:e84263d55307 | 159 | pHalSsiAdaptor->SclkPolarity = SCPOL_INACTIVE_IS_LOW; |
AnnaBridge | 168:e84263d55307 | 160 | pHalSsiAdaptor->SclkPhase = SCPH_TOGGLES_IN_MIDDLE; |
AnnaBridge | 168:e84263d55307 | 161 | break; |
AnnaBridge | 168:e84263d55307 | 162 | case 1: |
AnnaBridge | 168:e84263d55307 | 163 | pHalSsiAdaptor->SclkPolarity = SCPOL_INACTIVE_IS_LOW; |
AnnaBridge | 168:e84263d55307 | 164 | pHalSsiAdaptor->SclkPhase = SCPH_TOGGLES_AT_START; |
AnnaBridge | 168:e84263d55307 | 165 | break; |
AnnaBridge | 168:e84263d55307 | 166 | case 2: |
AnnaBridge | 168:e84263d55307 | 167 | pHalSsiAdaptor->SclkPolarity = SCPOL_INACTIVE_IS_HIGH; |
AnnaBridge | 168:e84263d55307 | 168 | pHalSsiAdaptor->SclkPhase = SCPH_TOGGLES_IN_MIDDLE; |
AnnaBridge | 168:e84263d55307 | 169 | break; |
AnnaBridge | 168:e84263d55307 | 170 | case 3: |
AnnaBridge | 168:e84263d55307 | 171 | pHalSsiAdaptor->SclkPolarity = SCPOL_INACTIVE_IS_HIGH; |
AnnaBridge | 168:e84263d55307 | 172 | pHalSsiAdaptor->SclkPhase = SCPH_TOGGLES_AT_START; |
AnnaBridge | 168:e84263d55307 | 173 | break; |
AnnaBridge | 168:e84263d55307 | 174 | default: // same as 3 |
AnnaBridge | 168:e84263d55307 | 175 | pHalSsiAdaptor->SclkPolarity = SCPOL_INACTIVE_IS_HIGH; |
AnnaBridge | 168:e84263d55307 | 176 | pHalSsiAdaptor->SclkPhase = SCPH_TOGGLES_AT_START; |
AnnaBridge | 168:e84263d55307 | 177 | break; |
AnnaBridge | 168:e84263d55307 | 178 | } |
AnnaBridge | 168:e84263d55307 | 179 | |
AnnaBridge | 168:e84263d55307 | 180 | if (slave == 1) { |
AnnaBridge | 168:e84263d55307 | 181 | if (pHalSsiAdaptor->Index == 0) { |
AnnaBridge | 168:e84263d55307 | 182 | pHalSsiAdaptor->Role = SSI_SLAVE; |
AnnaBridge | 168:e84263d55307 | 183 | pHalSsiAdaptor->SlaveOutputEnable = SLV_TXD_ENABLE; // <-- Slave only |
AnnaBridge | 168:e84263d55307 | 184 | DBG_SSI_INFO("SPI0 is as slave\n"); |
AnnaBridge | 168:e84263d55307 | 185 | } else { |
AnnaBridge | 168:e84263d55307 | 186 | DBG_SSI_ERR("The SPI%d cannot work as Slave mode, only SPI0 does.\r\n", pHalSsiAdaptor->Index); |
AnnaBridge | 168:e84263d55307 | 187 | pHalSsiAdaptor->Role = SSI_MASTER; |
AnnaBridge | 168:e84263d55307 | 188 | } |
AnnaBridge | 168:e84263d55307 | 189 | } else { |
AnnaBridge | 168:e84263d55307 | 190 | pHalSsiAdaptor->Role = SSI_MASTER; |
AnnaBridge | 168:e84263d55307 | 191 | } |
AnnaBridge | 168:e84263d55307 | 192 | pHalSsiOp->HalSsiSetDeviceRole(pHalSsiAdaptor, pHalSsiAdaptor->Role); |
AnnaBridge | 168:e84263d55307 | 193 | |
AnnaBridge | 168:e84263d55307 | 194 | #ifdef CONFIG_GPIO_EN |
AnnaBridge | 168:e84263d55307 | 195 | if (pHalSsiAdaptor->Role == SSI_SLAVE) { |
AnnaBridge | 168:e84263d55307 | 196 | if (pHalSsiAdaptor->SclkPolarity == SCPOL_INACTIVE_IS_LOW) { |
AnnaBridge | 168:e84263d55307 | 197 | HAL_GPIO_PullCtrl((u32)obj->sclk, hal_PullDown); |
AnnaBridge | 168:e84263d55307 | 198 | } else { |
AnnaBridge | 168:e84263d55307 | 199 | HAL_GPIO_PullCtrl((u32)obj->sclk, hal_PullUp); |
AnnaBridge | 168:e84263d55307 | 200 | } |
AnnaBridge | 168:e84263d55307 | 201 | } |
AnnaBridge | 168:e84263d55307 | 202 | #endif |
AnnaBridge | 168:e84263d55307 | 203 | HalSsiSetFormat(pHalSsiAdaptor); |
AnnaBridge | 168:e84263d55307 | 204 | } |
AnnaBridge | 168:e84263d55307 | 205 | |
AnnaBridge | 168:e84263d55307 | 206 | void spi_frequency (spi_t *obj, int hz) |
AnnaBridge | 168:e84263d55307 | 207 | { |
AnnaBridge | 168:e84263d55307 | 208 | PHAL_SSI_ADAPTOR pHalSsiAdaptor; |
AnnaBridge | 168:e84263d55307 | 209 | |
AnnaBridge | 168:e84263d55307 | 210 | pHalSsiAdaptor = &obj->spi_adp; |
AnnaBridge | 168:e84263d55307 | 211 | HalSsiSetSclk(pHalSsiAdaptor, (u32)hz); |
AnnaBridge | 168:e84263d55307 | 212 | } |
AnnaBridge | 168:e84263d55307 | 213 | |
AnnaBridge | 168:e84263d55307 | 214 | static inline void ssi_write (spi_t *obj, int value) |
AnnaBridge | 168:e84263d55307 | 215 | { |
AnnaBridge | 168:e84263d55307 | 216 | PHAL_SSI_ADAPTOR pHalSsiAdaptor; |
AnnaBridge | 168:e84263d55307 | 217 | PHAL_SSI_OP pHalSsiOp; |
AnnaBridge | 168:e84263d55307 | 218 | |
AnnaBridge | 168:e84263d55307 | 219 | pHalSsiAdaptor = &obj->spi_adp; |
AnnaBridge | 168:e84263d55307 | 220 | pHalSsiOp = &obj->spi_op; |
AnnaBridge | 168:e84263d55307 | 221 | |
AnnaBridge | 168:e84263d55307 | 222 | while (!pHalSsiOp->HalSsiWriteable(pHalSsiAdaptor)); |
AnnaBridge | 168:e84263d55307 | 223 | pHalSsiOp->HalSsiWrite((VOID*)pHalSsiAdaptor, value); |
AnnaBridge | 168:e84263d55307 | 224 | } |
AnnaBridge | 168:e84263d55307 | 225 | |
AnnaBridge | 168:e84263d55307 | 226 | static inline int ssi_read(spi_t *obj) |
AnnaBridge | 168:e84263d55307 | 227 | { |
AnnaBridge | 168:e84263d55307 | 228 | PHAL_SSI_ADAPTOR pHalSsiAdaptor; |
AnnaBridge | 168:e84263d55307 | 229 | PHAL_SSI_OP pHalSsiOp; |
AnnaBridge | 168:e84263d55307 | 230 | |
AnnaBridge | 168:e84263d55307 | 231 | pHalSsiAdaptor = &obj->spi_adp; |
AnnaBridge | 168:e84263d55307 | 232 | pHalSsiOp = &obj->spi_op; |
AnnaBridge | 168:e84263d55307 | 233 | |
AnnaBridge | 168:e84263d55307 | 234 | while (!pHalSsiOp->HalSsiReadable(pHalSsiAdaptor)); |
AnnaBridge | 168:e84263d55307 | 235 | return (int)pHalSsiOp->HalSsiRead(pHalSsiAdaptor); |
AnnaBridge | 168:e84263d55307 | 236 | } |
AnnaBridge | 168:e84263d55307 | 237 | |
AnnaBridge | 168:e84263d55307 | 238 | int spi_master_write (spi_t *obj, int value) |
AnnaBridge | 168:e84263d55307 | 239 | { |
AnnaBridge | 168:e84263d55307 | 240 | ssi_write(obj, value); |
AnnaBridge | 168:e84263d55307 | 241 | return ssi_read(obj); |
AnnaBridge | 168:e84263d55307 | 242 | } |
AnnaBridge | 168:e84263d55307 | 243 | |
Kojto | 171:19eb464bc2be | 244 | int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, |
Kojto | 171:19eb464bc2be | 245 | char *rx_buffer, int rx_length, char write_fill) |
AnnaBridge | 168:e84263d55307 | 246 | { |
AnnaBridge | 168:e84263d55307 | 247 | int total = (tx_length > rx_length) ? tx_length : rx_length; |
AnnaBridge | 168:e84263d55307 | 248 | |
AnnaBridge | 168:e84263d55307 | 249 | for (int i = 0; i < total; i++) { |
Kojto | 171:19eb464bc2be | 250 | char out = (i < tx_length) ? tx_buffer[i] : write_fill; |
AnnaBridge | 168:e84263d55307 | 251 | char in = spi_master_write(obj, out); |
AnnaBridge | 168:e84263d55307 | 252 | if (i < rx_length) { |
AnnaBridge | 168:e84263d55307 | 253 | rx_buffer[i] = in; |
AnnaBridge | 168:e84263d55307 | 254 | } |
AnnaBridge | 168:e84263d55307 | 255 | } |
AnnaBridge | 168:e84263d55307 | 256 | |
AnnaBridge | 168:e84263d55307 | 257 | return total; |
AnnaBridge | 168:e84263d55307 | 258 | } |
AnnaBridge | 168:e84263d55307 | 259 | |
AnnaBridge | 168:e84263d55307 | 260 | int spi_slave_receive (spi_t *obj) |
AnnaBridge | 168:e84263d55307 | 261 | { |
AnnaBridge | 168:e84263d55307 | 262 | PHAL_SSI_ADAPTOR pHalSsiAdaptor; |
AnnaBridge | 168:e84263d55307 | 263 | PHAL_SSI_OP pHalSsiOp; |
AnnaBridge | 168:e84263d55307 | 264 | int Readable; |
AnnaBridge | 168:e84263d55307 | 265 | int Busy; |
AnnaBridge | 168:e84263d55307 | 266 | |
AnnaBridge | 168:e84263d55307 | 267 | pHalSsiAdaptor = &obj->spi_adp; |
AnnaBridge | 168:e84263d55307 | 268 | pHalSsiOp = &obj->spi_op; |
AnnaBridge | 168:e84263d55307 | 269 | |
AnnaBridge | 168:e84263d55307 | 270 | Readable = pHalSsiOp->HalSsiReadable(pHalSsiAdaptor); |
AnnaBridge | 168:e84263d55307 | 271 | Busy = (int)pHalSsiOp->HalSsiBusy(pHalSsiAdaptor); |
AnnaBridge | 168:e84263d55307 | 272 | return ((Readable && !Busy) ? 1 : 0); |
AnnaBridge | 168:e84263d55307 | 273 | } |
AnnaBridge | 168:e84263d55307 | 274 | |
AnnaBridge | 168:e84263d55307 | 275 | int spi_slave_read (spi_t *obj) |
AnnaBridge | 168:e84263d55307 | 276 | { |
AnnaBridge | 168:e84263d55307 | 277 | return ssi_read(obj); |
AnnaBridge | 168:e84263d55307 | 278 | } |
AnnaBridge | 168:e84263d55307 | 279 | |
AnnaBridge | 168:e84263d55307 | 280 | void spi_slave_write (spi_t *obj, int value) |
AnnaBridge | 168:e84263d55307 | 281 | { |
AnnaBridge | 168:e84263d55307 | 282 | ssi_write(obj, value); |
AnnaBridge | 168:e84263d55307 | 283 | } |
AnnaBridge | 168:e84263d55307 | 284 | |
AnnaBridge | 168:e84263d55307 | 285 | int spi_busy (spi_t *obj) |
AnnaBridge | 168:e84263d55307 | 286 | { |
AnnaBridge | 168:e84263d55307 | 287 | PHAL_SSI_ADAPTOR pHalSsiAdaptor; |
AnnaBridge | 168:e84263d55307 | 288 | PHAL_SSI_OP pHalSsiOp; |
AnnaBridge | 168:e84263d55307 | 289 | |
AnnaBridge | 168:e84263d55307 | 290 | pHalSsiAdaptor = &obj->spi_adp; |
AnnaBridge | 168:e84263d55307 | 291 | pHalSsiOp = &obj->spi_op; |
AnnaBridge | 168:e84263d55307 | 292 | |
AnnaBridge | 168:e84263d55307 | 293 | return (int)pHalSsiOp->HalSsiBusy(pHalSsiAdaptor); |
AnnaBridge | 168:e84263d55307 | 294 | } |
AnnaBridge | 168:e84263d55307 | 295 | |
AnnaBridge | 168:e84263d55307 | 296 |