misc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. File Name : board_config.h
  3. Author : Yichip
  4. Version : V1.0
  5. Date : 2020/07/17
  6. Description : misc file.
  7. */
  8. #include "misc.h"
  9. /* following defines should be used for structure members */
  10. #define __IM volatile const /*! Defines 'read only' structure member permissions */
  11. #define __OM volatile /*! Defines 'write only' structure member permissions */
  12. #define __IOM volatile /*! Defines 'read / write' structure member permissions */
  13. /* Memory mapping of Cortex-M0 Hardware */
  14. #define SCS_BASE (0xE000E000UL) /*!< System Control Space Base Address */
  15. #define NVIC_BASE (SCS_BASE + 0x0100UL) /*!< NVIC Base Address */
  16. #define SCB_BASE (SCS_BASE + 0x0D00UL) /*!< System Control Block Base Address */
  17. typedef struct
  18. {
  19. __IM uint32_t CPUID; /*!< Offset: 0x000 (R/ ) CPUID Base Register */
  20. __IOM uint32_t ICSR; /*!< Offset: 0x004 (R/W) Interrupt Control and State Register */
  21. uint32_t RESERVED0;
  22. __IOM uint32_t AIRCR; /*!< Offset: 0x00C (R/W) Application Interrupt and Reset Control Register */
  23. __IOM uint32_t SCR; /*!< Offset: 0x010 (R/W) System Control Register */
  24. __IOM uint32_t CCR; /*!< Offset: 0x014 (R/W) Configuration Control Register */
  25. uint32_t RESERVED1;
  26. __IOM uint32_t SHP[2U]; /*!< Offset: 0x01C (R/W) System Handlers Priority Registers. [0] is RESERVED */
  27. __IOM uint32_t SHCSR; /*!< Offset: 0x024 (R/W) System Handler Control and State Register */
  28. } SCB_Type;
  29. /**
  30. \brief Structure type to access the Nested Vectored Interrupt Controller (NVIC).
  31. */
  32. typedef struct
  33. {
  34. __IOM uint32_t ISER[1U]; /*!< Offset: 0x000 (R/W) Interrupt Set Enable Register */
  35. uint32_t RESERVED0[31U];
  36. __IOM uint32_t ICER[1U]; /*!< Offset: 0x080 (R/W) Interrupt Clear Enable Register */
  37. uint32_t RSERVED1[31U];
  38. __IOM uint32_t ISPR[1U]; /*!< Offset: 0x100 (R/W) Interrupt Set Pending Register */
  39. uint32_t RESERVED2[31U];
  40. __IOM uint32_t ICPR[1U]; /*!< Offset: 0x180 (R/W) Interrupt Clear Pending Register */
  41. uint32_t RESERVED3[31U];
  42. uint32_t RESERVED4[64U];
  43. __IOM uint32_t IP[8U]; /*!< Offset: 0x300 (R/W) Interrupt Priority Register */
  44. } NVIC_Type;
  45. #define SCB ((SCB_Type *)SCB_BASE) /*!< SCB configuration struct */
  46. #define NVIC ((NVIC_Type *)NVIC_BASE) /*!< NVIC configuration struct */
  47. /* The following MACROS handle generation of the register offset and byte masks */
  48. #define _BIT_SHIFT(IRQn) (((((uint32_t)(int32_t)(IRQn))) & 0x03UL) * 8UL)
  49. #define _SHP_IDX(IRQn) ((((((uint32_t)(int32_t)(IRQn)) & 0x0FUL) - 8UL) >> 2UL))
  50. #define _IP_IDX(IRQn) ((((uint32_t)(int32_t)(IRQn)) >> 2UL))
  51. /* ----------------Configuration of the Cortex-M0 Processor and Core Peripherals---------------- */
  52. #define __CM0_REV 0x0000 /*!< Cortex-M0 Core Revision */
  53. #define __MPU_PRESENT 0 /*!< MPU present or not */
  54. #define __NVIC_PRIO_BITS 2 /*!< Number of Bits used for Priority Levels */
  55. #define __Vendor_SysTickConfig 0 /*!< Set to 1 if different SysTick Config is used */
  56. /* End of group Configuration_of_CMSIS */
  57. uint32_t NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
  58. {
  59. if ((int32_t)(IRQn) < 0)
  60. {
  61. SCB->SHP[_SHP_IDX(IRQn)] = ((uint32_t)(SCB->SHP[_SHP_IDX(IRQn)] & ~(0xFFUL << _BIT_SHIFT(IRQn))) |
  62. (((priority << (8U - __NVIC_PRIO_BITS)) & (uint32_t)0xFFUL) << _BIT_SHIFT(IRQn)));
  63. }
  64. else
  65. {
  66. NVIC->IP[_IP_IDX(IRQn)] = ((uint32_t)(NVIC->IP[_IP_IDX(IRQn)] & ~(0xFFUL << _BIT_SHIFT(IRQn))) |
  67. (((priority << (8U - __NVIC_PRIO_BITS)) & (uint32_t)0xFFUL) << _BIT_SHIFT(IRQn)));
  68. }
  69. return SUCCESS;
  70. }
  71. #define SBC_ICSR_PENDSV_IRQ 28
  72. void trigger_PendSV(void)
  73. {
  74. SCB->ICSR |= (1 << SBC_ICSR_PENDSV_IRQ);
  75. }
  76. void NVIC_EnableIRQ(IRQn_Type IRQnx)
  77. {
  78. enable_intr((int)IRQnx);
  79. }
  80. void NVIC_DisableIRQ(IRQn_Type IRQnx)
  81. {
  82. disable_intr((int)IRQnx);
  83. }
  84. void soft_reset(void)
  85. {
  86. SYSCTRL_RST_EN |= 0x01;
  87. SYSCTRL_RESET = 0x55;
  88. while (1);
  89. }