/*
 Tests for the spi_master device driver
*/

#include <esp_types.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "rom/ets_sys.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "freertos/xtensa_api.h"
#include "unity.h"
#include "driver/spi_master.h"
#include "soc/dport_reg.h"
#include "soc/spi_reg.h"
#include "soc/spi_struct.h"


static void check_spi_pre_n_for(int clk, int pre, int n)
{
    esp_err_t ret;
    spi_device_handle_t handle;

    spi_device_interface_config_t devcfg={
        .command_bits=0,
        .address_bits=0,
        .dummy_bits=0,
        .clock_speed_hz=clk,
        .duty_cycle_pos=128,
        .mode=0,
        .spics_io_num=21,
        .queue_size=3
    };
    char sendbuf[16]="";
    spi_transaction_t t;
    memset(&t, 0, sizeof(t));

    ret=spi_bus_add_device(HSPI_HOST, &devcfg, &handle);
    TEST_ASSERT(ret==ESP_OK);

    t.length=16*8;
    t.tx_buffer=sendbuf;
    ret=spi_device_transmit(handle, &t);

    printf("Checking clk rate %dHz. expect pre %d n %d, got pre %d n %d\n", clk, pre, n, SPI2.clock.clkdiv_pre+1, SPI2.clock.clkcnt_n+1);

    TEST_ASSERT(SPI2.clock.clkcnt_n+1==n);
    TEST_ASSERT(SPI2.clock.clkdiv_pre+1==pre);

    ret=spi_bus_remove_device(handle);
    TEST_ASSERT(ret==ESP_OK);
}


TEST_CASE("SPI Master clockdiv calculation routines", "[spi]")
{
    spi_bus_config_t buscfg={
        .mosi_io_num=4,
        .miso_io_num=16,
        .sclk_io_num=25,
        .quadwp_io_num=-1,
        .quadhd_io_num=-1
    };
    esp_err_t ret;
    ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
    TEST_ASSERT(ret==ESP_OK);

    check_spi_pre_n_for(8000000, 1, 10);
    check_spi_pre_n_for(800000, 2, 50);
    check_spi_pre_n_for(100000, 16, 50);
    check_spi_pre_n_for(333333, 4, 60);
    check_spi_pre_n_for(1, 8192, 64); //Actually should generate the minimum clock speed, 152Hz

    ret=spi_bus_free(HSPI_HOST);
    TEST_ASSERT(ret==ESP_OK);
}


static void test_spi_bus_speed(int hz) {
    esp_err_t ret;
    spi_device_handle_t handle;
    spi_device_interface_config_t devcfg={
        .command_bits=8,
        .address_bits=64,
        .dummy_bits=0,
        .clock_speed_hz=hz,
        .duty_cycle_pos=128,
        .mode=0,
        .spics_io_num=21,
        .queue_size=3,
    };

    ret=spi_bus_add_device(HSPI_HOST, &devcfg, &handle);
    TEST_ASSERT(ret==ESP_OK);
    printf("Bus/dev inited.\n");
    spi_transaction_t t;
    char sendbuf[64]="Hello World!";
    char recvbuf[64]="UUUUUUUUUUUUUUU";
    memset(&t, 0, sizeof(t));

    t.length=64*8;
    t.tx_buffer=sendbuf;
    t.rx_buffer=recvbuf;
    t.address=0xA00000000000000FL;
    t.command=0x55;
    printf("Transmit...\n");
    ret=spi_device_transmit(handle, &t);
    TEST_ASSERT(ret==ESP_OK);
    printf("Send vs recv:\n");
    for (int x=0; x<16; x++) printf("%02X ", (int)sendbuf[x]);
    printf("<sent\n");
    for (int x=0; x<16; x++) printf("%02X ", (int)recvbuf[x]);
    printf("<recv\n");

    ret=spi_bus_remove_device(handle);
    TEST_ASSERT(ret==ESP_OK);

    TEST_ASSERT_EQUAL_INT8_ARRAY(sendbuf, recvbuf, 64);
}


TEST_CASE("SPI Master test", "[spi][ignore]")
{
    spi_bus_config_t buscfg={
        .mosi_io_num=4,
        .miso_io_num=16,
        .sclk_io_num=25,
        .quadwp_io_num=-1,
        .quadhd_io_num=-1
    };
    esp_err_t ret;
    printf("THIS TEST NEEDS A JUMPER BETWEEN IO4 AND IO16\n");

    ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
    TEST_ASSERT(ret==ESP_OK);

    int freqs[]={8000, 1000000, 5000000, 10000000, 20000000, 26666666, 0};
    for (int x=0; freqs[x]!=0; x++) {
        printf("Testing clock speed of %dHz...\n", freqs[x]);
        test_spi_bus_speed(freqs[x]);
    }

    ret=spi_bus_free(HSPI_HOST);
    TEST_ASSERT(ret==ESP_OK);
}