//----------------------------------------------------------------------------
///
/// \file   main.cpp
///
/// \brief  main file for pinmux
///
/// \date   20180129
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Documentation------------------------------------------------------------


//---Includes -----------------------------------------------------------------


//---General--------------------------

#if defined ( CATCH2 )
#include <catch2/catch_test_macros.hpp>
#elif defined ( CATCH )
#include <catch/catch.hpp>
#else
#error "Either 'catch' or 'catch2' has to be installed"
#endif

//--- Own ----------------------------

#include "../src/board.h"
#include "../src/app.h"
#include "../src/pinmux.h"
#include "writerPlatform.h"
#include "writerConfigHeader.h"
#include "writerDesc.h"
#include "formaterCpp.h"

#define TEST_ALL
#define STOP_ON_FAIL

//---Implementation------------------------------------------------------------


TEST_CASE( "Shared", "[default]") {
   CPinmux pinmux;
   CApp app;
   CBoard board;

   SECTION( "Shared  periphery" )
   {
      const char *boardString=
      R"JSON(
      {
          "name": "myboard_shared",
          "version": "1",
          "overloadingAttributes": [ "speedo" ],
          "peripheries": [
                  { "type": "GND", "name": "GND", "pin": "GND", "relation": "fabricate", "attributes": { "pseudo": "yes" } },
                  { "type": "3V3", "name": "3V3", "pin": "3V3", "relation": "fabricate", "attributes": { "pseudo": "yes" } },
                  { "type": "AGND", "name": "AGND", "pin": "AGND", "relation": "fabricate", "attributes": { "pseudo": "yes" } },
                  { "type": "OPT_GND", "name": "OPT_GND", "pin": "GND", "relation": "fabricate", "attributes": { "pseudo": "yes" } },
                  { "type": "GPIO",   "periphery": "PA13", "pin": "D1" },
                  { "type": "GPIO",   "periphery": "PA14", "pin": "D0" },
                  { "type": "GPIO",   "periphery": "PA15", "pin": "D2" },
                  { "type": "GPIO",   "periphery": "PB1", "pin": "D2" },
                  { "type": "GPIO",   "periphery": "PB2", "pin": "D2" },
                  { "type": "GPIO",   "periphery": "PB3", "pin": "D2" },
                  {
                     "type": "SPI", "periphery": "SPI2.PA13", "irqs": "SPI_IRQHandler", "relation": "shared",
                     "consumes": [
                         { "periphery": "PA13",   "name": "CLK", "_mode": "skip" },
                         { "periphery": "PA14",   "name": "MOSI", "_mode": "skip" },
                         { "periphery": "PA15",   "name": "MISO", "_mode": "skip" },
                         { "type": "GND", "name": "A1" },
                         { "type": "GND", "name": "A1" }
                     ]
                  },
                  {
                     "type": "FLASH_SPI", "name": "FLASH_SPI", "relation": "fabricate",
                     "consumes": [
                         { "type": "SPI",   "name": "", "attributes": { "pin": "[SCL@#6 MOSI@#5 MISO@#2]", "relation": "shared" }, "relation": "shared" },
                         { "type": "GND",  "name": "GND", "attributes": { "pin": "#4" } },
                         {
                           "type": "OPT_GND",
                           "name": "WP",
                           "attributes": {
                              "pin": "#3",
                              "comment": "High: enable write; Low: write protected"
                           }
                        },
                         { "type": "GPIO",  "name": "BP", "_name": "CS", "attributes": { "pin": "#1" } },
                         { "type": "3V3",  "name": "RST", "attributes": { "pin": "#7" } },
                         { "type": "3V3",  "name": "VDD", "attributes": { "pin": "#8" } }
                     ]
                 }
         ],
         "constructors": [
                 {
                     "type": "SPI",
                     "arguments": [
                             { "type": "fixed", "value": "spiConfig_<attribute:configBlock>" },
                             { "type": "fixed", "value": "spiConfig_<attribute:speedo>" }
                     ]
                 },
                 {
                     "type": "GND",
                     "arguments": [
                         { "type": "GND", "name": "A1" },
                         { "type": "GND", "name": "A1" }
                     ]
                 }
         ],
         "final": "yes"
      }
      )JSON";

      app.clear();
      board.clear();
      app.setBoardId("myboard");
      board.loadByteArray( QByteArray(boardString), "intern_SPI" );
      app.loadByteArray(
               QByteArray(R"JSON(
               {
                   "name": "canio",
                   "periphery": [
                      { "type": "FLASH_SPI", "name": "1" },
                      { "type": "FLASH_SPI", "name": "2" },
                      { "type": "FLASH_SPI", "name": "3" }
                    ]
               }
      )JSON" ), "intern" );

      pinmux.setBoardApp(board, app);
      pinmux.resolveApp();
      int status=0;
      REQUIRE( status == 0 );

   }
}


//---fin-----------------------------------------------------------------------
