ch32v30x_dbgmcu.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v30x_dbgmcu.c
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2024/05/28
  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 "ch32v30x_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) & IDCODE_DEVID_MASK);
  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) >> 16);
  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. * CH32V303CBT6-0x303305x4
  99. * CH32V303RBT6-0x303205x4
  100. * CH32V303RCT6-0x303105x4
  101. * CH32V303VCT6-0x303005x4
  102. * CH32V305FBP6-0x305205x8
  103. * CH32V305RBT6-0x305005x8
  104. * CH32V305GBU6-0x305B05x8
  105. * CH32V305CCT6-0x305C05x8
  106. * CH32V307WCU6-0x307305x8
  107. * CH32V307FBP6-0x307205x8
  108. * CH32V307RCT6-0x307105x8
  109. * CH32V307VCT6-0x307005x8
  110. * CH32V317VCT6-0x3170B5X8
  111. * CH32V317WCU6-0x3173B5X8
  112. * CH32V317TCU6-0x3175B5X8
  113. */
  114. uint32_t DBGMCU_GetCHIPID( void )
  115. {
  116. return( *( uint32_t * )0x1FFFF704 );
  117. }