Daisuke Yanagihara
/
ref_spi_simplest_master
v2
spi_master.cpp@4:e86f33d05627, 2021-01-08 (annotated)
- Committer:
- Yanagihara
- Date:
- Fri Jan 08 08:04:52 2021 +0000
- Revision:
- 4:e86f33d05627
- Parent:
- 3:894278e5d918
v3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
taiga_prelude | 0:382b3838a100 | 1 | #include "mbed.h" |
taiga_prelude | 0:382b3838a100 | 2 | |
taiga_prelude | 0:382b3838a100 | 3 | SPI to_eps(p5,p6,p7); |
taiga_prelude | 0:382b3838a100 | 4 | DigitalOut cs(p8); |
taiga_prelude | 0:382b3838a100 | 5 | |
taiga_prelude | 0:382b3838a100 | 6 | Serial pc(USBTX,USBRX); |
taiga_prelude | 0:382b3838a100 | 7 | |
taiga_prelude | 0:382b3838a100 | 8 | #define DTIME 0.1 |
taiga_prelude | 0:382b3838a100 | 9 | |
taiga_prelude | 0:382b3838a100 | 10 | int main() |
taiga_prelude | 0:382b3838a100 | 11 | { |
taiga_prelude | 0:382b3838a100 | 12 | pc.printf("--Hi,this is cdh(master).\r\n"); |
taiga_prelude | 0:382b3838a100 | 13 | cs = 1; |
taiga_prelude | 0:382b3838a100 | 14 | |
Yanagihara | 2:6d9572f5523e | 15 | to_eps.format(8,3); |
taiga_prelude | 0:382b3838a100 | 16 | to_eps.frequency(1000000); |
Yanagihara | 3:894278e5d918 | 17 | |
Yanagihara | 3:894278e5d918 | 18 | int sdummy = 0xFF; //2nd frame |
taiga_prelude | 0:382b3838a100 | 19 | |
taiga_prelude | 0:382b3838a100 | 20 | while(1) { |
taiga_prelude | 0:382b3838a100 | 21 | char c = pc.getc(); |
Yanagihara | 3:894278e5d918 | 22 | int cmd = c - '0'; |
Yanagihara | 3:894278e5d918 | 23 | pc.printf("cmd: %d, ",cmd); // 2桁も打ち込み可能にしたい // cmdを16進数に直す |
Yanagihara | 3:894278e5d918 | 24 | |
Yanagihara | 3:894278e5d918 | 25 | // 1st frame (send cmd) |
taiga_prelude | 0:382b3838a100 | 26 | cs=0; |
Yanagihara | 3:894278e5d918 | 27 | int rdummy = to_eps.write(cmd); // send command |
taiga_prelude | 0:382b3838a100 | 28 | cs=1; |
Yanagihara | 3:894278e5d918 | 29 | pc.printf("rdummy: %x, ",rdummy); // receive "dummy" to slave |
Yanagihara | 1:5a1355e57aed | 30 | |
Yanagihara | 3:894278e5d918 | 31 | // 2nd frame (send "dummy" to receive "slave data") |
Yanagihara | 3:894278e5d918 | 32 | if(cmd == 1){ |
Yanagihara | 1:5a1355e57aed | 33 | wait(1); |
Yanagihara | 1:5a1355e57aed | 34 | cs=0; |
Yanagihara | 3:894278e5d918 | 35 | int rdata = to_eps.write(sdummy); |
Yanagihara | 1:5a1355e57aed | 36 | cs=1; |
Yanagihara | 3:894278e5d918 | 37 | pc.printf("<--rdata: %x\r\n",rdata); |
Yanagihara | 1:5a1355e57aed | 38 | } |
Yanagihara | 1:5a1355e57aed | 39 | |
Yanagihara | 4:e86f33d05627 | 40 | // receive 2byte data // (現状:2byteの場合mode=0では不具合が発生→プルアップしていないことが原因の可能性) |
Yanagihara | 1:5a1355e57aed | 41 | if(cmd == 2){ |
Yanagihara | 1:5a1355e57aed | 42 | wait(3); |
Yanagihara | 1:5a1355e57aed | 43 | cs=0; |
Yanagihara | 1:5a1355e57aed | 44 | int rdata1 = to_eps.write(0x00); |
Yanagihara | 1:5a1355e57aed | 45 | int rdata2 = to_eps.write(0x00); |
Yanagihara | 1:5a1355e57aed | 46 | cs=1; |
Yanagihara | 1:5a1355e57aed | 47 | pc.printf("rdata1: %x\r\n",rdata1); |
Yanagihara | 1:5a1355e57aed | 48 | pc.printf("rdata2: %x\r\n",rdata2); |
Yanagihara | 1:5a1355e57aed | 49 | } |
taiga_prelude | 0:382b3838a100 | 50 | } |
taiga_prelude | 0:382b3838a100 | 51 | } |