The program demonstrates how to implement SPI on BLE Nano 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_7);
00024 
00025 SPIClass SPI1(NRF_SPI1);
00026 
00027 Serial pc(USBTX, USBRX);
00028 
00029 void Flash_Buff_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t len)
00030 {
00031     uint16_t index;
00032     
00033     spi_cs = 0;
00034     wait_us(200);
00035     
00036     SPI1.transfer(0x84);   
00037     SPI1.transfer(0x00);    
00038     SPI1.transfer((uint8_t)(addr>>8));    
00039     SPI1.transfer((uint8_t)addr);     
00040     for(index=0; index<len; index++)
00041     {
00042         SPI1.transfer(*pbuf);
00043         pbuf++;        
00044     }
00045     
00046     wait_us(200);
00047     spi_cs = 1;
00048 }
00049 
00050 void Flash_Buff_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t len)
00051 {
00052     uint16_t index;
00053     
00054     spi_cs = 0;
00055     wait_us(200);
00056     
00057     SPI1.transfer(0xD1);   
00058     SPI1.transfer(0x00);    
00059     SPI1.transfer((uint8_t)(addr>>8));    
00060     SPI1.transfer((uint8_t)addr);     
00061     for(index=0; index<len; index++)
00062     {
00063         *pbuf = SPI1.transfer(0x00);
00064         pbuf++;        
00065     }
00066     
00067     wait_us(200);
00068     spi_cs = 1;
00069 }
00070 
00071 uint8_t i;
00072 uint8_t wt_buf[7] = {'H','e','l','l', 'o', '\r', '\n'};
00073 uint8_t rd_buf[7];
00074 
00075 int main(void)
00076 {   
00077     pc.baud(9600);
00078     wait(8);
00079     spi_cs = 1;
00080     pc.printf("SPI Demo Start \r\n");
00081     
00082     //SPI1.begin();
00083     SPI1.begin(P0_6, P0_15, P0_29);//SCK, MOSI, MOSI
00084     
00085     wait(1);
00086     Flash_Buff_WriteBytes(0, wt_buf, 7);
00087     while(1)
00088     {
00089          memset(rd_buf, 0x00, 7);
00090          Flash_Buff_ReadBytes(0, rd_buf, 7);
00091          for(i=0; i<7; i++)
00092             pc.putc(rd_buf[i]);
00093          wait(1);
00094     }
00095 }
00096 
00097 
00098 
00099 
00100 
00101 
00102