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 #ifndef _SPI_MASTER_H_
RedBearLab 1:a7087bcd5c28 21 #define _SPI_MASTER_H_
RedBearLab 0:281fa0c39c15 22
RedBearLab 0:281fa0c39c15 23 #include "mbed.h"
RedBearLab 0:281fa0c39c15 24
RedBearLab 0:281fa0c39c15 25 #define SPI_FREQUENCY_125K 0x02000000UL
RedBearLab 0:281fa0c39c15 26 #define SPI_FREQUENCY_250K 0x04000000UL
RedBearLab 0:281fa0c39c15 27 #define SPI_FREQUENCY_500K 0x08000000UL
RedBearLab 0:281fa0c39c15 28 #define SPI_FREQUENCY_1M 0x10000000UL
RedBearLab 0:281fa0c39c15 29 #define SPI_FREQUENCY_2M 0x20000000UL
RedBearLab 0:281fa0c39c15 30 #define SPI_FREQUENCY_4M 0x40000000UL
RedBearLab 0:281fa0c39c15 31 #define SPI_FREQUENCY_8M 0x80000000UL
RedBearLab 0:281fa0c39c15 32
RedBearLab 0:281fa0c39c15 33 #define SPI_125K 0
RedBearLab 0:281fa0c39c15 34 #define SPI_250K 1
RedBearLab 0:281fa0c39c15 35 #define SPI_500K 2
RedBearLab 0:281fa0c39c15 36 #define SPI_1M 3
RedBearLab 0:281fa0c39c15 37 #define SPI_2M 4
RedBearLab 0:281fa0c39c15 38 #define SPI_4M 5
RedBearLab 0:281fa0c39c15 39 #define SPI_8M 6
RedBearLab 0:281fa0c39c15 40
RedBearLab 0:281fa0c39c15 41 #define SPI_MODE0 0
RedBearLab 0:281fa0c39c15 42 #define SPI_MODE1 1
RedBearLab 0:281fa0c39c15 43 #define SPI_MODE2 2
RedBearLab 0:281fa0c39c15 44 #define SPI_MODE3 3
RedBearLab 0:281fa0c39c15 45
RedBearLab 0:281fa0c39c15 46 #define CS P0_14
RedBearLab 0:281fa0c39c15 47 #define SCK P0_25
RedBearLab 0:281fa0c39c15 48 #define MOSI P0_20
RedBearLab 0:281fa0c39c15 49 #define MISO P0_22
RedBearLab 0:281fa0c39c15 50
RedBearLab 0:281fa0c39c15 51 typedef enum{
RedBearLab 0:281fa0c39c15 52
RedBearLab 0:281fa0c39c15 53 MSBFIRST = 0,
RedBearLab 0:281fa0c39c15 54 LSBFIRST = 1
RedBearLab 0:281fa0c39c15 55
RedBearLab 0:281fa0c39c15 56 }BitOrder;
RedBearLab 0:281fa0c39c15 57
RedBearLab 0:281fa0c39c15 58 class SPIClass
RedBearLab 0:281fa0c39c15 59 {
RedBearLab 0:281fa0c39c15 60 public:
RedBearLab 0:281fa0c39c15 61 SPIClass(NRF_SPI_Type *_spi);
RedBearLab 0:281fa0c39c15 62
RedBearLab 0:281fa0c39c15 63 void begin();
RedBearLab 0:281fa0c39c15 64 void begin(uint32_t sck, uint32_t mosi, uint32_t miso);
RedBearLab 0:281fa0c39c15 65 uint8_t transfer(uint8_t data);
RedBearLab 0:281fa0c39c15 66 void endTransfer();
RedBearLab 0:281fa0c39c15 67
RedBearLab 0:281fa0c39c15 68 void setSPIMode( uint8_t mode );
RedBearLab 0:281fa0c39c15 69 void setFrequency(uint8_t speed );
RedBearLab 0:281fa0c39c15 70 void setBitORDER( BitOrder bit);
RedBearLab 0:281fa0c39c15 71 void setCPHA( bool trailing);
RedBearLab 0:281fa0c39c15 72 void setCPOL( bool active_low);
RedBearLab 0:281fa0c39c15 73
RedBearLab 0:281fa0c39c15 74
RedBearLab 0:281fa0c39c15 75 private:
RedBearLab 0:281fa0c39c15 76 NRF_SPI_Type *spi;
RedBearLab 0:281fa0c39c15 77
RedBearLab 0:281fa0c39c15 78 uint32_t SCK_Pin;
RedBearLab 0:281fa0c39c15 79 uint32_t MOSI_Pin;
RedBearLab 0:281fa0c39c15 80 uint32_t MISO_Pin;
RedBearLab 0:281fa0c39c15 81
RedBearLab 0:281fa0c39c15 82 };
RedBearLab 0:281fa0c39c15 83
RedBearLab 0:281fa0c39c15 84 #endif