Fork of SPI demo to use as a library

Dependencies:   mbed

Dependents:   SPI_GFX_nano

Fork of SPI_Demo_Nano by RedBearLab

Committer:
RedBearLab
Date:
Wed Oct 22 05:08:52 2014 +0000
Revision:
0:5810870d2b29
First commit

Who changed what in which revision?

UserRevisionLine numberNew 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 "mbed.h"
RedBearLab 0:5810870d2b29 21 #include "spi_master.h"
RedBearLab 0:5810870d2b29 22
RedBearLab 0:5810870d2b29 23 DigitalOut spi_cs(P0_7);
RedBearLab 0:5810870d2b29 24
RedBearLab 0:5810870d2b29 25 SPIClass SPI1(NRF_SPI1);
RedBearLab 0:5810870d2b29 26
RedBearLab 0:5810870d2b29 27 Serial pc(USBTX, USBRX);
RedBearLab 0:5810870d2b29 28
RedBearLab 0:5810870d2b29 29 void Flash_Buff_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t len)
RedBearLab 0:5810870d2b29 30 {
RedBearLab 0:5810870d2b29 31 uint16_t index;
RedBearLab 0:5810870d2b29 32
RedBearLab 0:5810870d2b29 33 spi_cs = 0;
RedBearLab 0:5810870d2b29 34 wait_us(200);
RedBearLab 0:5810870d2b29 35
RedBearLab 0:5810870d2b29 36 SPI1.transfer(0x84);
RedBearLab 0:5810870d2b29 37 SPI1.transfer(0x00);
RedBearLab 0:5810870d2b29 38 SPI1.transfer((uint8_t)(addr>>8));
RedBearLab 0:5810870d2b29 39 SPI1.transfer((uint8_t)addr);
RedBearLab 0:5810870d2b29 40 for(index=0; index<len; index++)
RedBearLab 0:5810870d2b29 41 {
RedBearLab 0:5810870d2b29 42 SPI1.transfer(*pbuf);
RedBearLab 0:5810870d2b29 43 pbuf++;
RedBearLab 0:5810870d2b29 44 }
RedBearLab 0:5810870d2b29 45
RedBearLab 0:5810870d2b29 46 wait_us(200);
RedBearLab 0:5810870d2b29 47 spi_cs = 1;
RedBearLab 0:5810870d2b29 48 }
RedBearLab 0:5810870d2b29 49
RedBearLab 0:5810870d2b29 50 void Flash_Buff_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t len)
RedBearLab 0:5810870d2b29 51 {
RedBearLab 0:5810870d2b29 52 uint16_t index;
RedBearLab 0:5810870d2b29 53
RedBearLab 0:5810870d2b29 54 spi_cs = 0;
RedBearLab 0:5810870d2b29 55 wait_us(200);
RedBearLab 0:5810870d2b29 56
RedBearLab 0:5810870d2b29 57 SPI1.transfer(0xD1);
RedBearLab 0:5810870d2b29 58 SPI1.transfer(0x00);
RedBearLab 0:5810870d2b29 59 SPI1.transfer((uint8_t)(addr>>8));
RedBearLab 0:5810870d2b29 60 SPI1.transfer((uint8_t)addr);
RedBearLab 0:5810870d2b29 61 for(index=0; index<len; index++)
RedBearLab 0:5810870d2b29 62 {
RedBearLab 0:5810870d2b29 63 *pbuf = SPI1.transfer(0x00);
RedBearLab 0:5810870d2b29 64 pbuf++;
RedBearLab 0:5810870d2b29 65 }
RedBearLab 0:5810870d2b29 66
RedBearLab 0:5810870d2b29 67 wait_us(200);
RedBearLab 0:5810870d2b29 68 spi_cs = 1;
RedBearLab 0:5810870d2b29 69 }
RedBearLab 0:5810870d2b29 70
RedBearLab 0:5810870d2b29 71 uint8_t i;
RedBearLab 0:5810870d2b29 72 uint8_t wt_buf[7] = {'H','e','l','l', 'o', '\r', '\n'};
RedBearLab 0:5810870d2b29 73 uint8_t rd_buf[7];
RedBearLab 0:5810870d2b29 74
RedBearLab 0:5810870d2b29 75 int main(void)
RedBearLab 0:5810870d2b29 76 {
RedBearLab 0:5810870d2b29 77 pc.baud(9600);
RedBearLab 0:5810870d2b29 78 wait(8);
RedBearLab 0:5810870d2b29 79 spi_cs = 1;
RedBearLab 0:5810870d2b29 80 pc.printf("SPI Demo Start \r\n");
RedBearLab 0:5810870d2b29 81
RedBearLab 0:5810870d2b29 82 //SPI1.begin();
RedBearLab 0:5810870d2b29 83 SPI1.begin(P0_6, P0_15, P0_29);//SCK, MOSI, MOSI
RedBearLab 0:5810870d2b29 84
RedBearLab 0:5810870d2b29 85 wait(1);
RedBearLab 0:5810870d2b29 86 Flash_Buff_WriteBytes(0, wt_buf, 7);
RedBearLab 0:5810870d2b29 87 while(1)
RedBearLab 0:5810870d2b29 88 {
RedBearLab 0:5810870d2b29 89 memset(rd_buf, 0x00, 7);
RedBearLab 0:5810870d2b29 90 Flash_Buff_ReadBytes(0, rd_buf, 7);
RedBearLab 0:5810870d2b29 91 for(i=0; i<7; i++)
RedBearLab 0:5810870d2b29 92 pc.putc(rd_buf[i]);
RedBearLab 0:5810870d2b29 93 wait(1);
RedBearLab 0:5810870d2b29 94 }
RedBearLab 0:5810870d2b29 95 }
RedBearLab 0:5810870d2b29 96
RedBearLab 0:5810870d2b29 97
RedBearLab 0:5810870d2b29 98
RedBearLab 0:5810870d2b29 99
RedBearLab 0:5810870d2b29 100
RedBearLab 0:5810870d2b29 101
RedBearLab 0:5810870d2b29 102