SPI Slave Test.

Dependencies:   mbed-rtos mbed

main.cpp

Committer:
ryood
Date:
2016-10-07
Revision:
5:ca0be3b8b8a6
Parent:
4:0051fa8b5475

File content as of revision 5:ca0be3b8b8a6:

/*
 * SPI Slave Test for BaseMachine UI Controller
 *
 * 2016.10.02
 *
 */

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

#define SPI_SPEED   (3000000)

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

enum SequencerCmd {
    CMD_RCV_PLAYING_STEP = 0x01,
    CMD_RUN              = 0x11,
    CMD_BPM              = 0x12,
    CMD_ACCENT_LEVEL     = 0x13,
    CMD_WAVE_SHAPE       = 0x21,
    CMD_PULSE_WIDTH      = 0x22,
    CMD_CUTOFF           = 0x31,
    CMD_RESONANCE        = 0x32,
    CMD_LEVEL            = 0x41,
    CMD_DURATION         = 0x42,
    CMD_DECAY            = 0x43,
    CMD_SUSTAIN          = 0x44,
    CMD_NOTE             = 0x51,
    CMD_PITCH            = 0x52, 
};

unsigned int step = 0;

void stepUp(void const* arg)
{
    step++;
    
    
    // Masterに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(125);
    
    while(1) {
        if(SpiS.receive()) {
            SpiS.reply(step % 16);
            uint16_t v = SpiS.read();
            Leds.write(v);

            if (v >> 8 == CMD_RUN) {
                printf("CMD_RUN\r\n");
                if ((v & 0xff) == 1) {
                    stepTimer.start(125);
                } else {
                    stepTimer.stop();
                }
            }
                
            printf("%04x\r\n", v);
        }
    }
}