SPI Slave Recieve 16bit Test.

Dependencies:   mbed-rtos mbed

Fork of Nucleo_rtos_SPISlave_Test by Ryo Od

main.cpp

Committer:
ryood
Date:
2016-10-04
Revision:
4:3617af415d4c
Parent:
3:e35c6a9ad906

File content as of revision 4:3617af415d4c:

#include "mbed.h"
#include "rtos.h"
#include "SPISlave.h"

#define SPI_SPEED   (4000000)
#define SPI_HEADER  (0x55)

BusOut Leds(PA_10, PB_3, PB_5, PB_4, PB_10, PA_8);
//DigitalOut StepChangePin(PC_7);
DigitalOut StepChangePin(PB_1);

//SPISlave SpiS(PA_7, PA_6, PA_5, PA_4);    // mosi, miso, sclk, ssel
// SPI2
SPISlave SpiS(PB_15, PB_14, PB_13, PB_12);    // mosi, miso, sclk, ssel

unsigned int step = 0;

void stepUp(void const* arg)
{
    step++;
    
    // Slaveにinterruptをかける。
    StepChangePin.write(1);
    StepChangePin.write(0);
}

int main()
{
    printf("\r\n\nNucleo rtos SPISlave Test..\r\n");
    
    // Setup LED
    for (int i = 0; i < 5; i++) {
        Leds.write(0x3f);
        Thread::wait(100);
        Leds.write(0x00);
        Thread::wait(100);
    }

    // Setup SPISlave
    SpiS.format(16, 0);
    SpiS.frequency(SPI_SPEED);

    // RtosTimer
    RtosTimer stepTimer(stepUp, osTimerPeriodic, (void *)0);
    stepTimer.start(500);   // BPM:30
    
    SpiS.reply(0);
    uint8_t prevVal = 0;
    uint8_t errCount = 0;
    while(1) {
        if(SpiS.receive()) {
            uint16_t val = SpiS.read();
            prevVal++;
            if ((val & 0xff00) != (SPI_HEADER << 8) || (val & 0xff) != prevVal) {
                errCount++;
                Leds.write(errCount);
            }
            prevVal = val & 0xff;
            SpiS.reply(step % 16);
        }
    }
}