/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */


#include <eCxx/eCxx.h>

#include "../AVR_Common.h"


//
// Check the MCU
//
#ifndef __AVR_ARCH_X__
#error "this test application is meant for AVRX platform only"
#endif


//
// For convenience
//
using EvSys = eCxx::EventSystem;


//
// The init function
//
void eCxx_user_init()
{
    // Initialize application
    initBasicAVRApp();

    // Initialize SPI
    App::spi().begin(eCxx::SPI::SPIMode::Master);

    // Initialize event system - route SPI0 master clock to EVOUTA default pin
    EvSys::bindChannelToGenerator(0, EvSys::Generator::SPI0_SCK);
    EvSys::bindUserToChannel(EvSys::User::EVOUTA_Def, 0);
}


//
// The task function
//
void eCxx_user_task()
{
    // Process event and delay
    static eCxx::uint32_t prvMillis = 0;
           eCxx::uint32_t curMillis = App::processEvent();
    if(curMillis - prvMillis < 100) return;
    else                            prvMillis = curMillis;

    // Send SPI data so that it will generate the master clock
    App::spi().beginTransaction(125000UL);
    for(uint8_t i = 0; i < 128; ++i) App::spi().write(0x55);
    App::spi().endTransaction();
}
