The program demonstrates how to implement SPI on RedBearLab nRF51822 platform
Dependencies: mbed
The program demonstrates how to implement SPI on RedBearLab nRF51822 platform.
main.cpp@1:a7087bcd5c28, 2016-01-07 (annotated)
- 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?
User | Revision | Line number | New 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 | 0:281fa0c39c15 | 20 | #include "mbed.h" |
RedBearLab | 1:a7087bcd5c28 | 21 | #include "spi_master.h" |
RedBearLab | 0:281fa0c39c15 | 22 | |
RedBearLab | 0:281fa0c39c15 | 23 | DigitalOut spi_cs(P0_14);//D10 |
RedBearLab | 0:281fa0c39c15 | 24 | |
RedBearLab | 0:281fa0c39c15 | 25 | SPIClass SPI1(NRF_SPI1); |
RedBearLab | 0:281fa0c39c15 | 26 | |
RedBearLab | 0:281fa0c39c15 | 27 | //SPI spi(P0_20, P0_22, P0_25);//MOSI, MISO, SCLK |
RedBearLab | 0:281fa0c39c15 | 28 | //SPI spi(P0_12, P0_13, P0_15);//D11, D12, D13 |
RedBearLab | 0:281fa0c39c15 | 29 | |
RedBearLab | 0:281fa0c39c15 | 30 | |
RedBearLab | 0:281fa0c39c15 | 31 | |
RedBearLab | 0:281fa0c39c15 | 32 | Serial pc(USBTX, USBRX); |
RedBearLab | 0:281fa0c39c15 | 33 | |
RedBearLab | 0:281fa0c39c15 | 34 | void Flash_Buff_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t len) |
RedBearLab | 0:281fa0c39c15 | 35 | { |
RedBearLab | 0:281fa0c39c15 | 36 | uint16_t index; |
RedBearLab | 0:281fa0c39c15 | 37 | |
RedBearLab | 0:281fa0c39c15 | 38 | spi_cs = 0; |
RedBearLab | 0:281fa0c39c15 | 39 | wait_us(200); |
RedBearLab | 0:281fa0c39c15 | 40 | |
RedBearLab | 0:281fa0c39c15 | 41 | SPI1.transfer(0x84); |
RedBearLab | 0:281fa0c39c15 | 42 | SPI1.transfer(0x00); |
RedBearLab | 0:281fa0c39c15 | 43 | SPI1.transfer((uint8_t)(addr>>8)); |
RedBearLab | 0:281fa0c39c15 | 44 | SPI1.transfer((uint8_t)addr); |
RedBearLab | 0:281fa0c39c15 | 45 | for(index=0; index<len; index++) |
RedBearLab | 0:281fa0c39c15 | 46 | { |
RedBearLab | 0:281fa0c39c15 | 47 | SPI1.transfer(*pbuf); |
RedBearLab | 0:281fa0c39c15 | 48 | pbuf++; |
RedBearLab | 0:281fa0c39c15 | 49 | } |
RedBearLab | 0:281fa0c39c15 | 50 | |
RedBearLab | 0:281fa0c39c15 | 51 | wait_us(200); |
RedBearLab | 0:281fa0c39c15 | 52 | spi_cs = 1; |
RedBearLab | 0:281fa0c39c15 | 53 | } |
RedBearLab | 0:281fa0c39c15 | 54 | |
RedBearLab | 0:281fa0c39c15 | 55 | void Flash_Buff_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t len) |
RedBearLab | 0:281fa0c39c15 | 56 | { |
RedBearLab | 0:281fa0c39c15 | 57 | uint16_t index; |
RedBearLab | 0:281fa0c39c15 | 58 | |
RedBearLab | 0:281fa0c39c15 | 59 | spi_cs = 0; |
RedBearLab | 0:281fa0c39c15 | 60 | wait_us(200); |
RedBearLab | 0:281fa0c39c15 | 61 | |
RedBearLab | 0:281fa0c39c15 | 62 | SPI1.transfer(0xD1); |
RedBearLab | 0:281fa0c39c15 | 63 | SPI1.transfer(0x00); |
RedBearLab | 0:281fa0c39c15 | 64 | SPI1.transfer((uint8_t)(addr>>8)); |
RedBearLab | 0:281fa0c39c15 | 65 | SPI1.transfer((uint8_t)addr); |
RedBearLab | 0:281fa0c39c15 | 66 | for(index=0; index<len; index++) |
RedBearLab | 0:281fa0c39c15 | 67 | { |
RedBearLab | 0:281fa0c39c15 | 68 | *pbuf = SPI1.transfer(0x00); |
RedBearLab | 0:281fa0c39c15 | 69 | pbuf++; |
RedBearLab | 0:281fa0c39c15 | 70 | } |
RedBearLab | 0:281fa0c39c15 | 71 | |
RedBearLab | 0:281fa0c39c15 | 72 | wait_us(200); |
RedBearLab | 0:281fa0c39c15 | 73 | spi_cs = 1; |
RedBearLab | 0:281fa0c39c15 | 74 | } |
RedBearLab | 0:281fa0c39c15 | 75 | |
RedBearLab | 0:281fa0c39c15 | 76 | uint8_t i; |
RedBearLab | 0:281fa0c39c15 | 77 | uint8_t wt_buf[7] = {'H','e','l','l', 'o', '\r', '\n'}; |
RedBearLab | 0:281fa0c39c15 | 78 | uint8_t rd_buf[7]; |
RedBearLab | 0:281fa0c39c15 | 79 | |
RedBearLab | 0:281fa0c39c15 | 80 | int main(void) |
RedBearLab | 0:281fa0c39c15 | 81 | { |
RedBearLab | 0:281fa0c39c15 | 82 | pc.baud(9600); |
RedBearLab | 0:281fa0c39c15 | 83 | wait(8); |
RedBearLab | 0:281fa0c39c15 | 84 | spi_cs = 1; |
RedBearLab | 0:281fa0c39c15 | 85 | pc.printf("SPI Demo Start \r\n"); |
RedBearLab | 0:281fa0c39c15 | 86 | |
RedBearLab | 0:281fa0c39c15 | 87 | //SPI1.begin(); |
RedBearLab | 0:281fa0c39c15 | 88 | SPI1.begin(P0_15, P0_12, P0_13); |
RedBearLab | 0:281fa0c39c15 | 89 | |
RedBearLab | 0:281fa0c39c15 | 90 | wait(1); |
RedBearLab | 0:281fa0c39c15 | 91 | Flash_Buff_WriteBytes(0, wt_buf, 7); |
RedBearLab | 0:281fa0c39c15 | 92 | while(1) |
RedBearLab | 0:281fa0c39c15 | 93 | { |
RedBearLab | 0:281fa0c39c15 | 94 | memset(rd_buf, 0x00, 7); |
RedBearLab | 0:281fa0c39c15 | 95 | Flash_Buff_ReadBytes(0, rd_buf, 7); |
RedBearLab | 0:281fa0c39c15 | 96 | for(i=0; i<7; i++) |
RedBearLab | 0:281fa0c39c15 | 97 | pc.putc(rd_buf[i]); |
RedBearLab | 0:281fa0c39c15 | 98 | wait(1); |
RedBearLab | 0:281fa0c39c15 | 99 | } |
RedBearLab | 0:281fa0c39c15 | 100 | } |
RedBearLab | 0:281fa0c39c15 | 101 | |
RedBearLab | 0:281fa0c39c15 | 102 | |
RedBearLab | 0:281fa0c39c15 | 103 | |
RedBearLab | 0:281fa0c39c15 | 104 | |
RedBearLab | 0:281fa0c39c15 | 105 | |
RedBearLab | 0:281fa0c39c15 | 106 | |
RedBearLab | 0:281fa0c39c15 | 107 | |
RedBearLab | 0:281fa0c39c15 | 108 |