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.
Dependencies: mbed
main.cpp@0:b09a91845bdb, 2019-03-18 (annotated)
- Committer:
- tamamfirdaus
- Date:
- Mon Mar 18 15:03:37 2019 +0000
- Revision:
- 0:b09a91845bdb
spi;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tamamfirdaus | 0:b09a91845bdb | 1 | #include "mbed.h" |
| tamamfirdaus | 0:b09a91845bdb | 2 | |
| tamamfirdaus | 0:b09a91845bdb | 3 | SPISlave device(PA_7, PA_6, PA_5, PA_4); // mosi, miso, sclk, ssel |
| tamamfirdaus | 0:b09a91845bdb | 4 | |
| tamamfirdaus | 0:b09a91845bdb | 5 | Serial pc(USBTX, USBRX); // tx, rx |
| tamamfirdaus | 0:b09a91845bdb | 6 | |
| tamamfirdaus | 0:b09a91845bdb | 7 | int main() { |
| tamamfirdaus | 0:b09a91845bdb | 8 | pc.baud(115200); |
| tamamfirdaus | 0:b09a91845bdb | 9 | int counter = 1; |
| tamamfirdaus | 0:b09a91845bdb | 10 | |
| tamamfirdaus | 0:b09a91845bdb | 11 | device.format(8,3); // Setup: bit data, high steady state clock, 2nd edge capture |
| tamamfirdaus | 0:b09a91845bdb | 12 | device.frequency(1000000); // 1MHz |
| tamamfirdaus | 0:b09a91845bdb | 13 | |
| tamamfirdaus | 0:b09a91845bdb | 14 | int reply = pc.getc()-48; |
| tamamfirdaus | 0:b09a91845bdb | 15 | device.reply(reply); // Prime SPI with first reply |
| tamamfirdaus | 0:b09a91845bdb | 16 | device.reply(reply); // Prime SPI with first reply, again |
| tamamfirdaus | 0:b09a91845bdb | 17 | |
| tamamfirdaus | 0:b09a91845bdb | 18 | pc.printf("======================================================\r\n"); |
| tamamfirdaus | 0:b09a91845bdb | 19 | pc.printf("Startup Next reply will be %d\r\n", reply); |
| tamamfirdaus | 0:b09a91845bdb | 20 | |
| tamamfirdaus | 0:b09a91845bdb | 21 | while (1) { |
| tamamfirdaus | 0:b09a91845bdb | 22 | if (device.receive()) { |
| tamamfirdaus | 0:b09a91845bdb | 23 | int valueFromMaster = device.read(); |
| tamamfirdaus | 0:b09a91845bdb | 24 | pc.printf("%d Something rxvd, and should have replied with %d\n\r", counter++, reply); |
| tamamfirdaus | 0:b09a91845bdb | 25 | device.reply(++reply); // Prime SPI with next reply |
| tamamfirdaus | 0:b09a91845bdb | 26 | pc.printf(" Received value from Master (%d) Next reply will be %d \r\n", valueFromMaster, reply); |
| tamamfirdaus | 0:b09a91845bdb | 27 | } |
| tamamfirdaus | 0:b09a91845bdb | 28 | } |
| tamamfirdaus | 0:b09a91845bdb | 29 | } |