The program demonstrates how to implement SPI on RedBearLab nRF51822 platform

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 
00003 Copyright (c) 2012-2014 RedBearLab
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 
00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
00009 subject to the following conditions:
00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00011 
00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 
00018 */
00019 
00020 #include "mbed.h"
00021 #include "spi_master.h"
00022 
00023 DigitalOut spi_cs(P0_14);//D10
00024 
00025 SPIClass SPI1(NRF_SPI1);
00026 
00027 //SPI spi(P0_20, P0_22, P0_25);//MOSI, MISO, SCLK
00028 //SPI spi(P0_12, P0_13, P0_15);//D11, D12, D13
00029 
00030 
00031 
00032 Serial pc(USBTX, USBRX);
00033 
00034 void Flash_Buff_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t len)
00035 {
00036     uint16_t index;
00037     
00038     spi_cs = 0;
00039     wait_us(200);
00040     
00041     SPI1.transfer(0x84);   
00042     SPI1.transfer(0x00);    
00043     SPI1.transfer((uint8_t)(addr>>8));    
00044     SPI1.transfer((uint8_t)addr);     
00045     for(index=0; index<len; index++)
00046     {
00047         SPI1.transfer(*pbuf);
00048         pbuf++;        
00049     }
00050     
00051     wait_us(200);
00052     spi_cs = 1;
00053 }
00054 
00055 void Flash_Buff_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t len)
00056 {
00057     uint16_t index;
00058     
00059     spi_cs = 0;
00060     wait_us(200);
00061     
00062     SPI1.transfer(0xD1);   
00063     SPI1.transfer(0x00);    
00064     SPI1.transfer((uint8_t)(addr>>8));    
00065     SPI1.transfer((uint8_t)addr);     
00066     for(index=0; index<len; index++)
00067     {
00068         *pbuf = SPI1.transfer(0x00);
00069         pbuf++;        
00070     }
00071     
00072     wait_us(200);
00073     spi_cs = 1;
00074 }
00075 
00076 uint8_t i;
00077 uint8_t wt_buf[7] = {'H','e','l','l', 'o', '\r', '\n'};
00078 uint8_t rd_buf[7];
00079 
00080 int main(void)
00081 {   
00082     pc.baud(9600);
00083     wait(8);
00084     spi_cs = 1;
00085     pc.printf("SPI Demo Start \r\n");
00086     
00087     //SPI1.begin();
00088     SPI1.begin(P0_15, P0_12, P0_13);
00089     
00090     wait(1);
00091     Flash_Buff_WriteBytes(0, wt_buf, 7);
00092     while(1)
00093     {
00094          memset(rd_buf, 0x00, 7);
00095          Flash_Buff_ReadBytes(0, rd_buf, 7);
00096          for(i=0; i<7; i++)
00097             pc.putc(rd_buf[i]);
00098          wait(1);
00099     }
00100 }
00101 
00102 
00103 
00104 
00105 
00106 
00107 
00108