mbed_itm_api.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2017 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if defined(DEVICE_ITM)
  17. #include "hal/itm_api.h"
  18. #include "cmsis.h"
  19. #include <stdbool.h>
  20. #ifndef ITM_STIM_FIFOREADY_Msk
  21. #define ITM_STIM_FIFOREADY_Msk 1
  22. #endif
  23. #define ITM_ENABLE_WRITE 0xC5ACCE55
  24. #define SWO_NRZ 0x02
  25. #define SWO_STIMULUS_PORT 0x01
  26. void mbed_itm_init(void)
  27. {
  28. static bool do_init = true;
  29. if (do_init) {
  30. do_init = false;
  31. itm_init();
  32. /* Enable write access to ITM registers. */
  33. ITM->LAR = ITM_ENABLE_WRITE;
  34. /* Trace Port Interface Selected Pin Protocol Register. */
  35. TPI->SPPR = (SWO_NRZ << TPI_SPPR_TXMODE_Pos);
  36. /* Trace Port Interface Formatter and Flush Control Register */
  37. TPI->FFCR = (1 << TPI_FFCR_TrigIn_Pos);
  38. /* Data Watchpoint and Trace Control Register */
  39. DWT->CTRL = (1 << DWT_CTRL_CYCTAP_Pos) |
  40. (0xF << DWT_CTRL_POSTINIT_Pos) |
  41. (0xF << DWT_CTRL_POSTPRESET_Pos) |
  42. (1 << DWT_CTRL_CYCCNTENA_Pos);
  43. /* Trace Privilege Register.
  44. * Disable access to trace channel configuration from non-privileged mode.
  45. */
  46. ITM->TPR = 0x0;
  47. /* Trace Control Register */
  48. ITM->TCR = (1 << ITM_TCR_TraceBusID_Pos) |
  49. (1 << ITM_TCR_DWTENA_Pos) |
  50. (1 << ITM_TCR_SYNCENA_Pos) |
  51. (1 << ITM_TCR_ITMENA_Pos);
  52. /* Trace Enable Register */
  53. ITM->TER = SWO_STIMULUS_PORT;
  54. }
  55. }
  56. static void itm_out8(uint32_t port, uint8_t data)
  57. {
  58. /* Wait until port is available */
  59. while ((ITM->PORT[port].u32 & ITM_STIM_FIFOREADY_Msk) == 0) {
  60. __NOP();
  61. }
  62. /* write data to port */
  63. ITM->PORT[port].u8 = data;
  64. }
  65. static void itm_out32(uint32_t port, uint32_t data)
  66. {
  67. /* Wait until port is available */
  68. while ((ITM->PORT[port].u32 & ITM_STIM_FIFOREADY_Msk) == 0) {
  69. __NOP();
  70. }
  71. /* write data to port */
  72. ITM->PORT[port].u32 = data;
  73. }
  74. uint32_t mbed_itm_send(uint32_t port, uint32_t data)
  75. {
  76. /* Check if ITM and port is enabled */
  77. if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) && /* ITM enabled */
  78. ((ITM->TER & (1UL << port)) != 0UL)) { /* ITM Port enabled */
  79. itm_out32(port, data);
  80. }
  81. return data;
  82. }
  83. void mbed_itm_send_block(uint32_t port, const void *data, size_t len)
  84. {
  85. const char *ptr = data;
  86. /* Check if ITM and port is enabled */
  87. if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) && /* ITM enabled */
  88. ((ITM->TER & (1UL << port)) != 0UL)) { /* ITM Port enabled */
  89. /* Output single byte at a time until data is aligned */
  90. while ((((uintptr_t) ptr) & 3) && len != 0) {
  91. itm_out8(port, *ptr++);
  92. len--;
  93. }
  94. /* Output bulk of data one word at a time */
  95. while (len >= 4) {
  96. itm_out32(port, *(const uint32_t *) ptr);
  97. ptr += 4;
  98. len -= 4;
  99. }
  100. /* Output any trailing bytes */
  101. while (len != 0) {
  102. itm_out8(port, *ptr++);
  103. len--;
  104. }
  105. }
  106. }
  107. #endif // defined(DEVICE_ITM)