//
// Created by lydxh on 2024/4/8.
//
#include <stdio.h>
#include "t_spi.h"
#include "spi.h"
#include "log.h"
#include "tool.h"

static uint8_t p;

uint8_t Mosi_Pin(uint8_t l) {
//    LOGT("MOSI","P:%d",l);
    p = l;
    return l;
}

uint8_t Sck_Pin(uint8_t l) {
//    LOGT("SCK","P:%d",l);
    return l;
}

uint8_t Cs_Pin(uint8_t l) {
//    LOGT("CS","P:%d",l);
    return l;
}

uint8_t Miso_Pin() {
//    uint8_t l=rand() % 2;
    uint8_t l = p;
//    LOGT("MISO","P:%d",l);
    return l;
}

int Test_spi(void *pVoid) {
    SW_Dev_Spi ltl = {
            .MOSI_SET=Mosi_Pin,
            .SCK_SET=Sck_Pin,
            .MISO=Miso_Pin,
            .CS_SET=Cs_Pin,
            .MODE=Mode0,
            .CS=LOW,
            .ENDIAN=LTL,
    };
    uint8_t r, t = rand() % 10;
    r = SW_SPI_RW(ltl, t);
    LOGI("<SPI> Tx:%d,Rx:%d", t, r);
    uint8_t rbuf[64], tbuf[64];
    for (int i = 0; i < 64; ++i) {
        tbuf[i] = rand() % 10;
    }
    SW_SPI_RWL(ltl, rbuf, tbuf, 64);
    BufPrint("<SPI> TX", tbuf, TYPE_T(tbuf), 64, 8);
    BufPrint("<SPI> RX", rbuf, TYPE_T(rbuf), 64, 8);
    uint16_t rbuf16[64], tbuf16[64];
    for (int i = 0; i < 64; ++i) {
        tbuf16[i] = rand() % 1000;
    }
    SW_SPI_RWL16(ltl, rbuf16, tbuf16, 64);
    BufPrint("<SPI> TX[16]", tbuf16, TYPE_T(tbuf16), 64, 16);
    BufPrint("<SPI> RX[16]", rbuf16, TYPE_T(rbuf16), 64, 16);
}
