Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:b720fba8540a, 2015-07-28 (annotated)
- Committer:
- jossebrys
- Date:
- Tue Jul 28 17:20:58 2015 +0000
- Revision:
- 0:b720fba8540a
1
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jossebrys | 0:b720fba8540a | 1 | /* |
| jossebrys | 0:b720fba8540a | 2 | |
| jossebrys | 0:b720fba8540a | 3 | Copyright (c) 2012-2014 RedBearLab |
| jossebrys | 0:b720fba8540a | 4 | |
| jossebrys | 0:b720fba8540a | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| jossebrys | 0:b720fba8540a | 6 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
| jossebrys | 0:b720fba8540a | 7 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| jossebrys | 0:b720fba8540a | 8 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
| jossebrys | 0:b720fba8540a | 9 | subject to the following conditions: |
| jossebrys | 0:b720fba8540a | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| jossebrys | 0:b720fba8540a | 11 | |
| jossebrys | 0:b720fba8540a | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| jossebrys | 0:b720fba8540a | 13 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| jossebrys | 0:b720fba8540a | 14 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| jossebrys | 0:b720fba8540a | 15 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| jossebrys | 0:b720fba8540a | 16 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| jossebrys | 0:b720fba8540a | 17 | |
| jossebrys | 0:b720fba8540a | 18 | */ |
| jossebrys | 0:b720fba8540a | 19 | |
| jossebrys | 0:b720fba8540a | 20 | #include "mbed.h" |
| jossebrys | 0:b720fba8540a | 21 | #include "spi_master.h" |
| jossebrys | 0:b720fba8540a | 22 | |
| jossebrys | 0:b720fba8540a | 23 | DigitalOut spi_cs(P0_7); |
| jossebrys | 0:b720fba8540a | 24 | |
| jossebrys | 0:b720fba8540a | 25 | SPIClass SPI1(NRF_SPI1); |
| jossebrys | 0:b720fba8540a | 26 | |
| jossebrys | 0:b720fba8540a | 27 | Serial pc(USBTX, USBRX); |
| jossebrys | 0:b720fba8540a | 28 | |
| jossebrys | 0:b720fba8540a | 29 | void Flash_Buff_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t len) |
| jossebrys | 0:b720fba8540a | 30 | { |
| jossebrys | 0:b720fba8540a | 31 | uint16_t index; |
| jossebrys | 0:b720fba8540a | 32 | |
| jossebrys | 0:b720fba8540a | 33 | spi_cs = 0; |
| jossebrys | 0:b720fba8540a | 34 | wait_us(200); |
| jossebrys | 0:b720fba8540a | 35 | |
| jossebrys | 0:b720fba8540a | 36 | SPI1.transfer(0x84); |
| jossebrys | 0:b720fba8540a | 37 | SPI1.transfer(0x00); |
| jossebrys | 0:b720fba8540a | 38 | SPI1.transfer((uint8_t)(addr>>8)); |
| jossebrys | 0:b720fba8540a | 39 | SPI1.transfer((uint8_t)addr); |
| jossebrys | 0:b720fba8540a | 40 | for(index=0; index<len; index++) |
| jossebrys | 0:b720fba8540a | 41 | { |
| jossebrys | 0:b720fba8540a | 42 | SPI1.transfer(*pbuf); |
| jossebrys | 0:b720fba8540a | 43 | pbuf++; |
| jossebrys | 0:b720fba8540a | 44 | } |
| jossebrys | 0:b720fba8540a | 45 | |
| jossebrys | 0:b720fba8540a | 46 | wait_us(200); |
| jossebrys | 0:b720fba8540a | 47 | spi_cs = 1; |
| jossebrys | 0:b720fba8540a | 48 | } |
| jossebrys | 0:b720fba8540a | 49 | |
| jossebrys | 0:b720fba8540a | 50 | void Flash_Buff_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t len) |
| jossebrys | 0:b720fba8540a | 51 | { |
| jossebrys | 0:b720fba8540a | 52 | uint16_t index; |
| jossebrys | 0:b720fba8540a | 53 | |
| jossebrys | 0:b720fba8540a | 54 | spi_cs = 0; |
| jossebrys | 0:b720fba8540a | 55 | wait_us(200); |
| jossebrys | 0:b720fba8540a | 56 | |
| jossebrys | 0:b720fba8540a | 57 | SPI1.transfer(0xD1); |
| jossebrys | 0:b720fba8540a | 58 | SPI1.transfer(0x00); |
| jossebrys | 0:b720fba8540a | 59 | SPI1.transfer((uint8_t)(addr>>8)); |
| jossebrys | 0:b720fba8540a | 60 | SPI1.transfer((uint8_t)addr); |
| jossebrys | 0:b720fba8540a | 61 | for(index=0; index<len; index++) |
| jossebrys | 0:b720fba8540a | 62 | { |
| jossebrys | 0:b720fba8540a | 63 | *pbuf = SPI1.transfer(0x00); |
| jossebrys | 0:b720fba8540a | 64 | pbuf++; |
| jossebrys | 0:b720fba8540a | 65 | } |
| jossebrys | 0:b720fba8540a | 66 | |
| jossebrys | 0:b720fba8540a | 67 | wait_us(200); |
| jossebrys | 0:b720fba8540a | 68 | spi_cs = 1; |
| jossebrys | 0:b720fba8540a | 69 | } |
| jossebrys | 0:b720fba8540a | 70 | |
| jossebrys | 0:b720fba8540a | 71 | uint8_t i; |
| jossebrys | 0:b720fba8540a | 72 | uint8_t wt_buf[7] = {'H','e','l','l', 'o', '\r', '\n'}; |
| jossebrys | 0:b720fba8540a | 73 | uint8_t rd_buf[7]; |
| jossebrys | 0:b720fba8540a | 74 | |
| jossebrys | 0:b720fba8540a | 75 | int main(void) |
| jossebrys | 0:b720fba8540a | 76 | { |
| jossebrys | 0:b720fba8540a | 77 | pc.baud(9600); |
| jossebrys | 0:b720fba8540a | 78 | wait(8); |
| jossebrys | 0:b720fba8540a | 79 | spi_cs = 1; |
| jossebrys | 0:b720fba8540a | 80 | pc.printf("SPI Demo Start \r\n"); |
| jossebrys | 0:b720fba8540a | 81 | |
| jossebrys | 0:b720fba8540a | 82 | //SPI1.begin(); |
| jossebrys | 0:b720fba8540a | 83 | SPI1.begin(P0_6, P0_15, P0_29);//SCK, MOSI, MOSI |
| jossebrys | 0:b720fba8540a | 84 | |
| jossebrys | 0:b720fba8540a | 85 | wait(1); |
| jossebrys | 0:b720fba8540a | 86 | Flash_Buff_WriteBytes(0, wt_buf, 7); |
| jossebrys | 0:b720fba8540a | 87 | while(1) |
| jossebrys | 0:b720fba8540a | 88 | { |
| jossebrys | 0:b720fba8540a | 89 | memset(rd_buf, 0x00, 7); |
| jossebrys | 0:b720fba8540a | 90 | Flash_Buff_ReadBytes(0, rd_buf, 7); |
| jossebrys | 0:b720fba8540a | 91 | for(i=0; i<7; i++) |
| jossebrys | 0:b720fba8540a | 92 | pc.putc(rd_buf[i]); |
| jossebrys | 0:b720fba8540a | 93 | wait(1); |
| jossebrys | 0:b720fba8540a | 94 | } |
| jossebrys | 0:b720fba8540a | 95 | } |
| jossebrys | 0:b720fba8540a | 96 | |
| jossebrys | 0:b720fba8540a | 97 | |
| jossebrys | 0:b720fba8540a | 98 | |
| jossebrys | 0:b720fba8540a | 99 | |
| jossebrys | 0:b720fba8540a | 100 | |
| jossebrys | 0:b720fba8540a | 101 | |
| jossebrys | 0:b720fba8540a | 102 |