v2

Dependencies:   mbed

spi_master.cpp

Committer:
Yanagihara
Date:
2021-01-08
Revision:
4:e86f33d05627
Parent:
3:894278e5d918

File content as of revision 4:e86f33d05627:

#include "mbed.h"

SPI to_eps(p5,p6,p7);
DigitalOut cs(p8);

Serial pc(USBTX,USBRX);

#define DTIME 0.1

int main()
{
    pc.printf("--Hi,this is cdh(master).\r\n");
    cs = 1;

    to_eps.format(8,3);
    to_eps.frequency(1000000);
    
    int sdummy = 0xFF;  //2nd frame

    while(1) {
        char c = pc.getc();
        int cmd = c - '0';
        pc.printf("cmd: %d, ",cmd);   // 2桁も打ち込み可能にしたい // cmdを16進数に直す
        
    // 1st frame (send cmd)
        cs=0;
        int rdummy = to_eps.write(cmd); // send command
        cs=1;
        pc.printf("rdummy: %x, ",rdummy);   // receive "dummy" to slave
                 
    // 2nd frame (send "dummy" to receive "slave data")
        if(cmd == 1){
        wait(1);
        cs=0;
        int rdata = to_eps.write(sdummy);       
        cs=1;
        pc.printf("<--rdata: %x\r\n",rdata);
        }
        
        // receive 2byte data       // (現状:2byteの場合mode=0では不具合が発生→プルアップしていないことが原因の可能性)
        if(cmd == 2){        
        wait(3);
        cs=0;
        int rdata1 = to_eps.write(0x00);
        int rdata2 = to_eps.write(0x00);       
        cs=1;
        pc.printf("rdata1: %x\r\n",rdata1);
        pc.printf("rdata2: %x\r\n",rdata2);
        }
    }
}