/**
**************************************************************************
* @file arm_defines.h
* @brief Заголовочный файл портирующий ARM дефайны.
**************************************************************************
@details
Данный файл переопределяет ARM дефайны так, чтобы они компилировались
MSVC. Дефайны представляют собой или заглушку или заменены выражением 
с аналогичным ARM компилятору функционалом.
**************************************************************************/

#define __disable_irq()

/* CMSIS compiler specific defines */


#ifndef   __ASM
  #define __ASM                                  __asm
#endif

#ifndef   __inline
  #define __inline                               inline
#endif
#ifndef   __INLINE
  #define __INLINE                               __inline
#endif

#ifndef   __STATIC_INLINE
  #define __STATIC_INLINE                        static __inline
#endif

#ifndef   __STATIC_FORCEINLINE                 
  #define __STATIC_FORCEINLINE                   static __forceinline
#endif        

#ifndef   __NO_RETURN
  #define __NO_RETURN                            __declspec(noreturn)
#endif

#ifndef   __USED
  #define __USED                                 __attribute__((used))
#endif

#ifndef   __WEAK
  #define __WEAK                                 __declspec(selectany)
#endif
#ifndef   __weak
#define __weak
#endif


#ifndef   __PACKED
  #define __PACKED                               __attribute__((packed))
#endif

#ifndef   __PACKED_STRUCT
  #define __PACKED_STRUCT                        __packed struct
#endif

#ifndef   __PACKED_UNION
  #define __PACKED_UNION                         __packed union
#endif

#ifndef   __UNALIGNED_UINT32        /* deprecated */
  #define __UNALIGNED_UINT32(x)                  (*((__packed uint32_t *)(x)))
#endif

#ifndef   __UNALIGNED_UINT16_WRITE
  #define __UNALIGNED_UINT16_WRITE(addr, val)    ((*((__packed uint16_t *)(addr))) = (val))
#endif

#ifndef   __UNALIGNED_UINT16_READ
  #define __UNALIGNED_UINT16_READ(addr)          (*((const __packed uint16_t *)(addr)))
#endif

#ifndef   __UNALIGNED_UINT32_WRITE
  #define __UNALIGNED_UINT32_WRITE(addr, val)    ((*((__packed uint32_t *)(addr))) = (val))
#endif

#ifndef   __UNALIGNED_UINT32_READ
  #define __UNALIGNED_UINT32_READ(addr)          (*((const __packed uint32_t *)(addr)))
#endif

#ifndef   __RESTRICT
  #define __RESTRICT                             __restrict
#endif

/* ##########################  Core Instruction Access  ######################### */
/**
  \brief   No Operation
  \details No Operation does nothing. This instruction can be used for code alignment purposes.
 */
#define __NOP()
 /**
   \brief   Wait For Interrupt
   \details Wait For Interrupt is a hint instruction that suspends execution until one of a number of events occurs.
  */
#define __WFI()

  /**
    \brief   Wait For Event
    \details Wait For Event is a hint instruction that permits the processor to enter
             a low-power state until one of a number of events occurs.
   */
#define __WFE()

   /**
     \brief   Send Event
     \details Send Event is a hint instruction. It causes an event to be signaled to the CPU.
    */
#define __SEV()

    /**
      \brief   Instruction Synchronization Barrier
      \details Instruction Synchronization Barrier flushes the pipeline in the processor,
               so that all instructions following the ISB are fetched from cache or memory,
               after the instruction has been completed.
     */
#define __ISB()

     /**
       \brief   Data Synchronization Barrier
       \details Acts as a special kind of Data Memory Barrier.
                It completes when all explicit memory accesses before this instruction complete.
      */
#define __DSB()


      /**
        \brief   Data Memory Barrier
        \details Ensures the apparent order of the explicit memory operations before
                 and after the instruction, without ensuring their completion.
       */
#define __DMB() 


       /**
         \brief   Reverse byte order (32 bit)
         \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
         \param [in]    value  Value to reverse
         \return               Reversed value
        */
#define __REV(value)    value


        /**
          \brief   Reverse byte order (16 bit)
          \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
          \param [in]    value  Value to reverse
          \return               Reversed value
         */
#define __REV16(value)  value


         /**
           \brief   Reverse byte order (16 bit)
           \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
           \param [in]    value  Value to reverse
           \return               Reversed value
          */
#define __REVSH(value)  value


//          /**
//            \brief   Rotate Right in unsigned value (32 bit)
//            \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
//            \param [in]    op1  Value to rotate
//            \param [in]    op2  Number of Bits to rotate
//            \return               Rotated value
//           */
//#define __ROR()


/**
  \brief   Breakpoint
  \details Causes the processor to enter Debug state.
           Debug tools can use this to investigate system state when the instruction at a particular address is reached.
  \param [in]    value  is ignored by the processor.
                 If required, a debugger can use it to store additional information about the breakpoint.
 */
#define __BKPT(value)   value


 /**
   \brief   Reverse bit order of value
   \details Reverses the bit order of the given value.
   \param [in]    value  Value to reverse
   \return               Reversed value
  */
#define __RBIT(value)  _byteswap_ulong(_rotr(value, 16))

//  /**
//    \brief   Count leading zeros
//    \details Counts the number of leading zeros of a data value.
//    \param [in]  value  Value to count the leading zeros
//    \return             number of leading zeros in value
//   */
//#define __CLZ(value) __lzcnt(value)


   /**
     \brief   LDR Exclusive (8 bit)
     \details Executes a exclusive LDR instruction for 8 bit value.
     \param [in]    ptr  Pointer to data
     \return             value of type uint8_t at (*ptr)
    */
#define __LDREXB(ptr) (*(volatile uint8_t *)(ptr))


    /**
      \brief   LDR Exclusive (16 bit)
      \details Executes a exclusive LDR instruction for 16 bit values.
      \param [in]    ptr  Pointer to data
      \return        value of type uint16_t at (*ptr)
     */
#define __LDREXH(ptr) (*(volatile uint16_t *)(ptr))


     /**
       \brief   LDR Exclusive (32 bit)
       \details Executes a exclusive LDR instruction for 32 bit values.
       \param [in]    ptr  Pointer to data
       \return        value of type uint32_t at (*ptr)
      */
#define __LDREXW(ptr) (*(volatile uint32_t *)(ptr))


      /**
        \brief   STR Exclusive (8 bit)
        \details Executes a exclusive STR instruction for 8 bit values.
        \param [in]  value  Value to store
        \param [in]    ptr  Pointer to location
        \return          0  Function succeeded
        \return          1  Function failed
       */
#define __STREXB(value, ptr) (*(volatile uint8_t *)(ptr) = (value), 0)


       /**
         \brief   STR Exclusive (16 bit)
         \details Executes a exclusive STR instruction for 16 bit values.
         \param [in]  value  Value to store
         \param [in]    ptr  Pointer to location
         \return          0  Function succeeded
         \return          1  Function failed
        */
#define __STREXH(value, ptr) (*(volatile uint16_t *)(ptr) = (value), 0)


        /**
          \brief   STR Exclusive (32 bit)
          \details Executes a exclusive STR instruction for 32 bit values.
          \param [in]  value  Value to store
          \param [in]    ptr  Pointer to location
          \return          0  Function succeeded
          \return          1  Function failed
         */
#define __STREXW(value, ptr) (*(volatile uint32_t *)(ptr) = (value), 0)


         /**
           \brief   Remove the exclusive lock
           \details Removes the exclusive lock which is created by LDREX.
          */
#define __CLREX             



//          /**
//            \brief   Signed Saturate
//            \details Saturates a signed value.
//            \param [in]  value  Value to be saturated
//            \param [in]    sat  Bit position to saturate to (1..32)
//            \return             Saturated value
//           */
//#define __SSAT             


//           /**
//             \brief   Unsigned Saturate
//             \details Saturates an unsigned value.
//             \param [in]  value  Value to be saturated
//             \param [in]    sat  Bit position to saturate to (0..31)
//             \return             Saturated value
//            */
//#define __USAT             

