/**
 * @file tpl_app_interrupts.c
 *
 * @section descr File description
 *
 * Generated functions related to interrupts.
 *
 * Generated from application % !CPUNAME %
 * Automatically generated by goil on % !TIMESTAMP %
 * from root OIL file % !OILFILENAME %
 *
 * @section infos File informations
 *
 * $$Date$$
 * $$Rev$$
 * $$Author$$
 * $$URL$$
 */

#include "tpl_compiler.h"
#include "tpl_cortex_definitions.h"
#include "tpl_os_interrupt_kernel.h"
#include "cmsis_wrapper.h"

%
let ITSOURCES := [INTERRUPT mapBy: "NAME"]

#
# Compute functions to enable/disable ISR2 according to the running ISR2
#
if OS::ISR2_PRIORITY_MASKING & [ISRS2 length] > 0 then
%
#define OS_START_SEC_CODE
#include "tpl_memmap.h"
%
  # Build a set of used priority
  let prioritySet := @!!
  foreach isr in ISRS2 do
    let prioritySet += isr::PRIORITY
  end foreach 
  
  # Compute a vectors corresponding to each priority level
  foreach prio in prioritySet do

    let lowerOrEqualISR :=
      [ISRS2 where: @? __item__::PRIORITY <= PRIO ?, @{ PRIO: [prio unsigned] }]

    let vector := 0
    foreach isr in lowerOrEqualISR do
       if exists ITSOURCES[isr::SOURCE]::SETPRIO_S::NUMBER then
         [!vector setBitAtIndex:
           true,
           ITSOURCES[isr::SOURCE]::SETPRIO_S::NUMBER
         ]
       else
         error isr::SOURCE : "ISR category 2 may use external interrupts only"
       end if
    end foreach
  
    template (vector,"priority " + prio, "priority_" + prio) enable_disable_it
    
  end foreach
%
#define OS_STOP_SEC_CODE
#include "tpl_memmap.h"

/*=============================================================================
 * Tables to access enable and disable functions according to the priority of
 * the ISR
 */
#define OS_START_SEC_CONST_UNSPECIFIED
#include "tpl_memmap.h"
%  
  # Compute the tables
  foreach prio in prioritySet
  before%
CONST(tpl_enable_disable_func, OS_CONST) tpl_enable_table[% ![prioritySet length] %] = {%
  do%
  tpl_enable_priority_% !prio %_interrupts%
  between%,%
  after%
};
%
  end foreach
  foreach prio in prioritySet
  before%
CONST(tpl_enable_disable_func, OS_CONST) tpl_disable_table[% ![prioritySet length] %] = {%
  do%
  tpl_disable_priority_% !prio %_interrupts%
  between%,%
  after%
};
%
  end foreach
%
#define OS_STOP_SEC_CONST_UNSPECIFIED
#include "tpl_memmap.h"
%
end if
%
#define OS_START_SEC_CODE
#include "tpl_memmap.h"

/*=============================================================================
 * Initialize the priorities in the NVIC according to the priorities defined
 * in the OIL/ARXML file
 */
FUNC(void, OS_CODE) tpl_init_it_priority(void)
{%
foreach isr in ISRS1
before
%
  /*
   * Init of ISR category 1 priorities
   */
%
do
%

  /* Init the priority of % !isr::SOURCE % */
  NVIC_SetPriority(% !isr::SOURCE%_Number, % !isr::SOURCE %_Priority);%
end foreach
foreach isr in ISRS2
before
%
  /*
   * Init of ISR category 2 priorities
   */
%
do
%
  /* Init the priority of % !isr::SOURCE % */
  NVIC_SetPriority(% !isr::SOURCE%_Number, OS_ISR_PRIO_UNSHIFTED);%
end foreach
%
}
%

let ISR2_vector := 0

foreach isr in ISRS2 do
  if exists ITSOURCES[isr::SOURCE]::SETPRIO_S::NUMBER then
    [!ISR2_vector setBitAtIndex:
      true,
      ITSOURCES[isr::SOURCE]::SETPRIO_S::NUMBER
    ]
  else
    error isr::SOURCE : "ISR category 2 may use external interrupts only"
  end if
end foreach

template (ISR2_vector, "OS", "os") enable_disable_it

%

#define OS_STOP_SEC_CODE
#include "tpl_memmap.h"

/* End of file tpl_init_it_priority.c */