The program demonstrates how to implement SPI on RedBearLab nRF51822 platform

Dependencies:   mbed

The program demonstrates how to implement SPI on RedBearLab nRF51822 platform.

Committer:
RedBearLab
Date:
Thu Jan 07 02:56:30 2016 +0000
Revision:
1:a7087bcd5c28
Parent:
0:281fa0c39c15
Update libraries.

Who changed what in which revision?

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