ch32v20x_dbgmcu.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v20x_dbgmcu.c
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2021/06/06
  6. * Description : This file provides all the DBGMCU firmware functions.
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. #include "ch32v20x_dbgmcu.h"
  13. #define IDCODE_DEVID_MASK ((uint32_t)0x0000FFFF)
  14. /*********************************************************************
  15. * @fn DBGMCU_GetREVID
  16. *
  17. * @brief Returns the device revision identifier.
  18. *
  19. * @return Revision identifier.
  20. */
  21. uint32_t DBGMCU_GetREVID(void)
  22. {
  23. return ((*(uint32_t *)0x1FFFF704) >> 16);
  24. }
  25. /*********************************************************************
  26. * @fn DBGMCU_GetDEVID
  27. *
  28. * @brief Returns the device identifier.
  29. *
  30. * @return Device identifier.
  31. */
  32. uint32_t DBGMCU_GetDEVID(void)
  33. {
  34. return ((*(uint32_t *)0x1FFFF704) & IDCODE_DEVID_MASK);
  35. }
  36. /*********************************************************************
  37. * @fn __get_DEBUG_CR
  38. *
  39. * @brief Return the DEBUGE Control Register
  40. *
  41. * @return DEBUGE Control value
  42. */
  43. uint32_t __get_DEBUG_CR(void)
  44. {
  45. uint32_t result;
  46. __asm volatile("csrr %0,""0x7C0" : "=r"(result));
  47. return (result);
  48. }
  49. /*********************************************************************
  50. * @fn __set_DEBUG_CR
  51. *
  52. * @brief Set the DEBUGE Control Register
  53. *
  54. * @param value - set DEBUGE Control value
  55. *
  56. * @return none
  57. */
  58. void __set_DEBUG_CR(uint32_t value)
  59. {
  60. __asm volatile("csrw 0x7C0, %0" : : "r"(value));
  61. }
  62. /*********************************************************************
  63. * @fn DBGMCU_Config
  64. *
  65. * @brief Configures the specified peripheral and low power mode behavior
  66. * when the MCU under Debug mode.
  67. *
  68. * @param DBGMCU_Periph - specifies the peripheral and low power mode.
  69. * DBGMCU_IWDG_STOP - Debug IWDG stopped when Core is halted
  70. * DBGMCU_WWDG_STOP - Debug WWDG stopped when Core is halted
  71. * DBGMCU_TIM1_STOP - TIM1 counter stopped when Core is halted
  72. * DBGMCU_TIM2_STOP - TIM2 counter stopped when Core is halted
  73. * NewState - ENABLE or DISABLE.
  74. *
  75. * @return none
  76. */
  77. void DBGMCU_Config(uint32_t DBGMCU_Periph, FunctionalState NewState)
  78. {
  79. uint32_t val;
  80. if(NewState != DISABLE)
  81. {
  82. __set_DEBUG_CR(DBGMCU_Periph);
  83. }
  84. else
  85. {
  86. val = __get_DEBUG_CR();
  87. val &= ~(uint32_t)DBGMCU_Periph;
  88. __set_DEBUG_CR(val);
  89. }
  90. }
  91. /*********************************************************************
  92. * @fn DBGMCU_GetCHIPID
  93. *
  94. * @brief Returns the CHIP identifier.
  95. *
  96. * @return Device identifier.
  97. * ChipID List-
  98. * CH32V203C8U6-0x203005x0
  99. * CH32V203C8T6-0x203105x0
  100. * CH32V203K8T6-0x203205x0
  101. * CH32V203C6T6-0x203305x0
  102. * CH32V203G6U6-0x203605x0
  103. * CH32V203G8R6-0x203B05x0
  104. * CH32V203F8U6-0x203E05x0
  105. * CH32V203F6P6-0x203705x0-0x203905x0
  106. * CH32V203F8P6-0x203A05x0
  107. * CH32V203RBT6-0x203405xC
  108. * CH32V208WBU6-0x208005xC
  109. * CH32V208RBT6-0x208105xC
  110. * CH32V208CBU6-0x208205xC
  111. * CH32V208GBU6-0x208305xC
  112. */
  113. uint32_t DBGMCU_GetCHIPID( void )
  114. {
  115. return( *( uint32_t * )0x1FFFF704 );
  116. }