Adem Bayraktar
/
STM32-NRF-SPI_07112016
A
Fork of SPI_Demo_Nano by
spi_master.cpp@3:b4887e2837ab, 2016-11-14 (annotated)
- Committer:
- Adembay
- Date:
- Mon Nov 14 14:23:03 2016 +0000
- Revision:
- 3:b4887e2837ab
- Parent:
- 0:5810870d2b29
M
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 0:5810870d2b29 | 1 | /* |
RedBearLab | 0:5810870d2b29 | 2 | |
RedBearLab | 0:5810870d2b29 | 3 | Copyright (c) 2012-2014 RedBearLab |
RedBearLab | 0:5810870d2b29 | 4 | |
RedBearLab | 0:5810870d2b29 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
RedBearLab | 0:5810870d2b29 | 6 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
RedBearLab | 0:5810870d2b29 | 7 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
RedBearLab | 0:5810870d2b29 | 8 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
RedBearLab | 0:5810870d2b29 | 9 | subject to the following conditions: |
RedBearLab | 0:5810870d2b29 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
RedBearLab | 0:5810870d2b29 | 11 | |
RedBearLab | 0:5810870d2b29 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
RedBearLab | 0:5810870d2b29 | 13 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
RedBearLab | 0:5810870d2b29 | 14 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
RedBearLab | 0:5810870d2b29 | 15 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
RedBearLab | 0:5810870d2b29 | 16 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
RedBearLab | 0:5810870d2b29 | 17 | |
RedBearLab | 0:5810870d2b29 | 18 | */ |
RedBearLab | 0:5810870d2b29 | 19 | |
RedBearLab | 0:5810870d2b29 | 20 | #include "spi_master.h" |
RedBearLab | 0:5810870d2b29 | 21 | |
RedBearLab | 0:5810870d2b29 | 22 | /********************************************************************** |
RedBearLab | 0:5810870d2b29 | 23 | name : |
RedBearLab | 0:5810870d2b29 | 24 | function : |
RedBearLab | 0:5810870d2b29 | 25 | **********************************************************************/ |
RedBearLab | 0:5810870d2b29 | 26 | SPIClass::SPIClass(NRF_SPI_Type *_spi) : spi(_spi) |
RedBearLab | 0:5810870d2b29 | 27 | { |
RedBearLab | 0:5810870d2b29 | 28 | //do nothing |
RedBearLab | 0:5810870d2b29 | 29 | } |
RedBearLab | 0:5810870d2b29 | 30 | |
RedBearLab | 0:5810870d2b29 | 31 | /********************************************************************** |
RedBearLab | 0:5810870d2b29 | 32 | name : |
RedBearLab | 0:5810870d2b29 | 33 | function : |
RedBearLab | 0:5810870d2b29 | 34 | **********************************************************************/ |
RedBearLab | 0:5810870d2b29 | 35 | void SPIClass::setCPOL( bool active_low) |
RedBearLab | 0:5810870d2b29 | 36 | { |
RedBearLab | 0:5810870d2b29 | 37 | if(active_low) |
RedBearLab | 0:5810870d2b29 | 38 | { |
RedBearLab | 0:5810870d2b29 | 39 | spi->CONFIG |= (SPI_CONFIG_CPOL_ActiveLow << SPI_CONFIG_CPOL_Pos); |
RedBearLab | 0:5810870d2b29 | 40 | } |
RedBearLab | 0:5810870d2b29 | 41 | else |
RedBearLab | 0:5810870d2b29 | 42 | { |
RedBearLab | 0:5810870d2b29 | 43 | spi->CONFIG |= (SPI_CONFIG_CPOL_ActiveHigh << SPI_CONFIG_CPOL_Pos); |
RedBearLab | 0:5810870d2b29 | 44 | } |
RedBearLab | 0:5810870d2b29 | 45 | } |
RedBearLab | 0:5810870d2b29 | 46 | |
RedBearLab | 0:5810870d2b29 | 47 | /********************************************************************** |
RedBearLab | 0:5810870d2b29 | 48 | name : |
RedBearLab | 0:5810870d2b29 | 49 | function : |
RedBearLab | 0:5810870d2b29 | 50 | **********************************************************************/ |
RedBearLab | 0:5810870d2b29 | 51 | void SPIClass::setCPHA( bool trailing) |
RedBearLab | 0:5810870d2b29 | 52 | { |
RedBearLab | 0:5810870d2b29 | 53 | if(trailing) |
RedBearLab | 0:5810870d2b29 | 54 | { |
RedBearLab | 0:5810870d2b29 | 55 | spi->CONFIG |= (SPI_CONFIG_CPHA_Trailing << SPI_CONFIG_CPHA_Pos); |
RedBearLab | 0:5810870d2b29 | 56 | } |
RedBearLab | 0:5810870d2b29 | 57 | else |
RedBearLab | 0:5810870d2b29 | 58 | { |
RedBearLab | 0:5810870d2b29 | 59 | spi->CONFIG |= (SPI_CONFIG_CPHA_Leading << SPI_CONFIG_CPHA_Pos); |
RedBearLab | 0:5810870d2b29 | 60 | } |
RedBearLab | 0:5810870d2b29 | 61 | |
RedBearLab | 0:5810870d2b29 | 62 | } |
RedBearLab | 0:5810870d2b29 | 63 | |
RedBearLab | 0:5810870d2b29 | 64 | /********************************************************************** |
RedBearLab | 0:5810870d2b29 | 65 | name : |
RedBearLab | 0:5810870d2b29 | 66 | function : MSBFIRST, LSBFIRST |
RedBearLab | 0:5810870d2b29 | 67 | **********************************************************************/ |
RedBearLab | 0:5810870d2b29 | 68 | void SPIClass::setBitORDER( BitOrder bit) |
RedBearLab | 0:5810870d2b29 | 69 | { |
RedBearLab | 0:5810870d2b29 | 70 | if(bit == MSBFIRST) |
RedBearLab | 0:5810870d2b29 | 71 | { |
RedBearLab | 0:5810870d2b29 | 72 | spi->CONFIG |= (SPI_CONFIG_ORDER_MsbFirst << SPI_CONFIG_ORDER_Pos); |
RedBearLab | 0:5810870d2b29 | 73 | } |
RedBearLab | 0:5810870d2b29 | 74 | else |
RedBearLab | 0:5810870d2b29 | 75 | { |
RedBearLab | 0:5810870d2b29 | 76 | spi->CONFIG |= (SPI_CONFIG_ORDER_LsbFirst << SPI_CONFIG_ORDER_Pos); |
RedBearLab | 0:5810870d2b29 | 77 | } |
RedBearLab | 0:5810870d2b29 | 78 | } |
RedBearLab | 0:5810870d2b29 | 79 | |
RedBearLab | 0:5810870d2b29 | 80 | /********************************************************************** |
RedBearLab | 0:5810870d2b29 | 81 | name : |
RedBearLab | 0:5810870d2b29 | 82 | function : |
RedBearLab | 0:5810870d2b29 | 83 | **********************************************************************/ |
RedBearLab | 0:5810870d2b29 | 84 | void SPIClass::setFrequency(uint8_t speed) |
RedBearLab | 0:5810870d2b29 | 85 | { |
RedBearLab | 0:5810870d2b29 | 86 | if (speed == 0) |
RedBearLab | 0:5810870d2b29 | 87 | { |
RedBearLab | 0:5810870d2b29 | 88 | spi->FREQUENCY = SPI_FREQUENCY_125K; |
RedBearLab | 0:5810870d2b29 | 89 | } |
RedBearLab | 0:5810870d2b29 | 90 | else if (speed == 1) |
RedBearLab | 0:5810870d2b29 | 91 | { |
RedBearLab | 0:5810870d2b29 | 92 | spi->FREQUENCY = SPI_FREQUENCY_250K; |
RedBearLab | 0:5810870d2b29 | 93 | } |
RedBearLab | 0:5810870d2b29 | 94 | else if (speed == 2) |
RedBearLab | 0:5810870d2b29 | 95 | { |
RedBearLab | 0:5810870d2b29 | 96 | spi->FREQUENCY = SPI_FREQUENCY_500K; |
RedBearLab | 0:5810870d2b29 | 97 | } |
RedBearLab | 0:5810870d2b29 | 98 | else if (speed == 3) |
RedBearLab | 0:5810870d2b29 | 99 | { |
RedBearLab | 0:5810870d2b29 | 100 | spi->FREQUENCY = SPI_FREQUENCY_1M; |
RedBearLab | 0:5810870d2b29 | 101 | } |
RedBearLab | 0:5810870d2b29 | 102 | else if (speed == 4) |
RedBearLab | 0:5810870d2b29 | 103 | { |
RedBearLab | 0:5810870d2b29 | 104 | spi->FREQUENCY = SPI_FREQUENCY_2M; |
RedBearLab | 0:5810870d2b29 | 105 | } |
RedBearLab | 0:5810870d2b29 | 106 | else if (speed == 5) |
RedBearLab | 0:5810870d2b29 | 107 | { |
RedBearLab | 0:5810870d2b29 | 108 | spi->FREQUENCY = SPI_FREQUENCY_4M; |
RedBearLab | 0:5810870d2b29 | 109 | } |
RedBearLab | 0:5810870d2b29 | 110 | else if (speed == 6) |
RedBearLab | 0:5810870d2b29 | 111 | { |
RedBearLab | 0:5810870d2b29 | 112 | spi->FREQUENCY = SPI_FREQUENCY_8M; |
RedBearLab | 0:5810870d2b29 | 113 | } |
RedBearLab | 0:5810870d2b29 | 114 | else |
RedBearLab | 0:5810870d2b29 | 115 | { |
RedBearLab | 0:5810870d2b29 | 116 | spi->FREQUENCY = SPI_FREQUENCY_4M; |
RedBearLab | 0:5810870d2b29 | 117 | } |
RedBearLab | 0:5810870d2b29 | 118 | } |
RedBearLab | 0:5810870d2b29 | 119 | |
RedBearLab | 0:5810870d2b29 | 120 | /********************************************************************** |
RedBearLab | 0:5810870d2b29 | 121 | name : |
RedBearLab | 0:5810870d2b29 | 122 | function : |
RedBearLab | 0:5810870d2b29 | 123 | **********************************************************************/ |
RedBearLab | 0:5810870d2b29 | 124 | void SPIClass::setSPIMode( uint8_t mode ) |
RedBearLab | 0:5810870d2b29 | 125 | { |
RedBearLab | 0:5810870d2b29 | 126 | if (SPI_MODE0 == mode) |
RedBearLab | 0:5810870d2b29 | 127 | { |
RedBearLab | 0:5810870d2b29 | 128 | setCPOL(0); |
RedBearLab | 0:5810870d2b29 | 129 | setCPHA(0); |
RedBearLab | 0:5810870d2b29 | 130 | } |
RedBearLab | 0:5810870d2b29 | 131 | else if (mode == SPI_MODE1) |
RedBearLab | 0:5810870d2b29 | 132 | { |
RedBearLab | 0:5810870d2b29 | 133 | setCPOL(0); |
RedBearLab | 0:5810870d2b29 | 134 | setCPHA(1); |
RedBearLab | 0:5810870d2b29 | 135 | } |
RedBearLab | 0:5810870d2b29 | 136 | else if (mode == SPI_MODE2) |
RedBearLab | 0:5810870d2b29 | 137 | { |
RedBearLab | 0:5810870d2b29 | 138 | setCPOL(1); |
RedBearLab | 0:5810870d2b29 | 139 | setCPHA(0); |
RedBearLab | 0:5810870d2b29 | 140 | } |
RedBearLab | 0:5810870d2b29 | 141 | else if (mode == SPI_MODE3) |
RedBearLab | 0:5810870d2b29 | 142 | { |
RedBearLab | 0:5810870d2b29 | 143 | setCPOL(1); |
RedBearLab | 0:5810870d2b29 | 144 | setCPHA(1); |
RedBearLab | 0:5810870d2b29 | 145 | } |
RedBearLab | 0:5810870d2b29 | 146 | else |
RedBearLab | 0:5810870d2b29 | 147 | { |
RedBearLab | 0:5810870d2b29 | 148 | setCPOL(0); |
RedBearLab | 0:5810870d2b29 | 149 | setCPHA(0); |
RedBearLab | 0:5810870d2b29 | 150 | } |
RedBearLab | 0:5810870d2b29 | 151 | } |
RedBearLab | 0:5810870d2b29 | 152 | |
RedBearLab | 0:5810870d2b29 | 153 | /********************************************************************** |
RedBearLab | 0:5810870d2b29 | 154 | name : |
RedBearLab | 0:5810870d2b29 | 155 | function : |
RedBearLab | 0:5810870d2b29 | 156 | **********************************************************************/ |
RedBearLab | 0:5810870d2b29 | 157 | void SPIClass::begin(uint32_t sck, uint32_t mosi, uint32_t miso) |
RedBearLab | 0:5810870d2b29 | 158 | { |
RedBearLab | 0:5810870d2b29 | 159 | |
RedBearLab | 0:5810870d2b29 | 160 | SCK_Pin = sck; |
RedBearLab | 0:5810870d2b29 | 161 | MOSI_Pin = mosi; |
RedBearLab | 0:5810870d2b29 | 162 | MISO_Pin = miso; |
RedBearLab | 0:5810870d2b29 | 163 | |
RedBearLab | 0:5810870d2b29 | 164 | NRF_GPIO->PIN_CNF[SCK_Pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
RedBearLab | 0:5810870d2b29 | 165 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
RedBearLab | 0:5810870d2b29 | 166 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
RedBearLab | 0:5810870d2b29 | 167 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
RedBearLab | 0:5810870d2b29 | 168 | | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); |
RedBearLab | 0:5810870d2b29 | 169 | |
RedBearLab | 0:5810870d2b29 | 170 | NRF_GPIO->PIN_CNF[MOSI_Pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
RedBearLab | 0:5810870d2b29 | 171 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
RedBearLab | 0:5810870d2b29 | 172 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
RedBearLab | 0:5810870d2b29 | 173 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
RedBearLab | 0:5810870d2b29 | 174 | | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); |
RedBearLab | 0:5810870d2b29 | 175 | |
RedBearLab | 0:5810870d2b29 | 176 | NRF_GPIO->PIN_CNF[MISO_Pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
RedBearLab | 0:5810870d2b29 | 177 | | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
RedBearLab | 0:5810870d2b29 | 178 | | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
RedBearLab | 0:5810870d2b29 | 179 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
RedBearLab | 0:5810870d2b29 | 180 | | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
RedBearLab | 0:5810870d2b29 | 181 | spi->PSELSCK = SCK_Pin; |
RedBearLab | 0:5810870d2b29 | 182 | spi->PSELMOSI = MOSI_Pin; |
RedBearLab | 0:5810870d2b29 | 183 | spi->PSELMISO = MISO_Pin; |
RedBearLab | 0:5810870d2b29 | 184 | |
RedBearLab | 0:5810870d2b29 | 185 | setFrequency(SPI_2M); |
RedBearLab | 0:5810870d2b29 | 186 | setSPIMode(SPI_MODE0); |
RedBearLab | 0:5810870d2b29 | 187 | setBitORDER(MSBFIRST); |
RedBearLab | 0:5810870d2b29 | 188 | |
RedBearLab | 0:5810870d2b29 | 189 | spi->EVENTS_READY = 0; |
RedBearLab | 0:5810870d2b29 | 190 | spi->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos); |
RedBearLab | 0:5810870d2b29 | 191 | |
RedBearLab | 0:5810870d2b29 | 192 | } |
RedBearLab | 0:5810870d2b29 | 193 | |
RedBearLab | 0:5810870d2b29 | 194 | /********************************************************************** |
RedBearLab | 0:5810870d2b29 | 195 | name : |
RedBearLab | 0:5810870d2b29 | 196 | function : |
RedBearLab | 0:5810870d2b29 | 197 | **********************************************************************/ |
RedBearLab | 0:5810870d2b29 | 198 | void SPIClass::begin() |
RedBearLab | 0:5810870d2b29 | 199 | { |
RedBearLab | 0:5810870d2b29 | 200 | begin(SCK, MOSI, MISO); |
RedBearLab | 0:5810870d2b29 | 201 | } |
RedBearLab | 0:5810870d2b29 | 202 | |
RedBearLab | 0:5810870d2b29 | 203 | /********************************************************************** |
RedBearLab | 0:5810870d2b29 | 204 | name : |
RedBearLab | 0:5810870d2b29 | 205 | function : |
RedBearLab | 0:5810870d2b29 | 206 | **********************************************************************/ |
RedBearLab | 0:5810870d2b29 | 207 | uint8_t SPIClass::transfer(uint8_t data) |
RedBearLab | 0:5810870d2b29 | 208 | { |
RedBearLab | 0:5810870d2b29 | 209 | while( spi->EVENTS_READY != 0U ); |
RedBearLab | 0:5810870d2b29 | 210 | |
RedBearLab | 0:5810870d2b29 | 211 | spi->TXD = (uint32_t)data; |
RedBearLab | 0:5810870d2b29 | 212 | |
RedBearLab | 0:5810870d2b29 | 213 | while(spi->EVENTS_READY == 0); |
RedBearLab | 0:5810870d2b29 | 214 | |
RedBearLab | 0:5810870d2b29 | 215 | data = (uint8_t)spi->RXD; |
RedBearLab | 0:5810870d2b29 | 216 | |
RedBearLab | 0:5810870d2b29 | 217 | spi->EVENTS_READY = 0; |
RedBearLab | 0:5810870d2b29 | 218 | |
RedBearLab | 0:5810870d2b29 | 219 | return data; |
RedBearLab | 0:5810870d2b29 | 220 | } |
RedBearLab | 0:5810870d2b29 | 221 | |
RedBearLab | 0:5810870d2b29 | 222 | /********************************************************************** |
RedBearLab | 0:5810870d2b29 | 223 | name : |
RedBearLab | 0:5810870d2b29 | 224 | function : |
RedBearLab | 0:5810870d2b29 | 225 | **********************************************************************/ |
RedBearLab | 0:5810870d2b29 | 226 | void SPIClass::endTransfer() |
RedBearLab | 0:5810870d2b29 | 227 | { |
RedBearLab | 0:5810870d2b29 | 228 | spi->ENABLE = (SPI_ENABLE_ENABLE_Disabled << SPI_ENABLE_ENABLE_Pos); |
RedBearLab | 0:5810870d2b29 | 229 | } |
RedBearLab | 0:5810870d2b29 | 230 | |
RedBearLab | 0:5810870d2b29 | 231 | /********************************************************************** |
RedBearLab | 0:5810870d2b29 | 232 | name : |
RedBearLab | 0:5810870d2b29 | 233 | function : |
RedBearLab | 0:5810870d2b29 | 234 | **********************************************************************/ |
RedBearLab | 0:5810870d2b29 | 235 | |
RedBearLab | 0:5810870d2b29 | 236 | //SPIClass SPI(NRF_SPI1); |