cpu_cache.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-04-02 tanek first implementation
  9. * 2019-04-27 misonyo update to cortex-m7 series
  10. */
  11. #include <rthw.h>
  12. #include <rtdef.h>
  13. #include <board.h>
  14. /* The L1-caches on all Cortex®-M7s are divided into lines of 32 bytes. */
  15. #define L1CACHE_LINESIZE_BYTE (32)
  16. void rt_hw_cpu_icache_enable(void)
  17. {
  18. SCB_EnableICache();
  19. }
  20. void rt_hw_cpu_icache_disable(void)
  21. {
  22. SCB_DisableICache();
  23. }
  24. rt_base_t rt_hw_cpu_icache_status(void)
  25. {
  26. return 0;
  27. }
  28. void rt_hw_cpu_icache_ops(int ops, void* addr, int size)
  29. {
  30. rt_uint32_t address = (rt_uint32_t)addr & (rt_uint32_t) ~(L1CACHE_LINESIZE_BYTE - 1);
  31. rt_int32_t size_byte = size + address - (rt_uint32_t)addr;
  32. rt_uint32_t linesize = 32U;
  33. if (ops & RT_HW_CACHE_INVALIDATE)
  34. {
  35. __DSB();
  36. while (size_byte > 0)
  37. {
  38. SCB->ICIMVAU = address;
  39. address += linesize;
  40. size_byte -= linesize;
  41. }
  42. __DSB();
  43. __ISB();
  44. }
  45. }
  46. void rt_hw_cpu_dcache_enable(void)
  47. {
  48. SCB_EnableDCache();
  49. }
  50. void rt_hw_cpu_dcache_disable(void)
  51. {
  52. SCB_DisableDCache();
  53. }
  54. rt_base_t rt_hw_cpu_dcache_status(void)
  55. {
  56. return 0;
  57. }
  58. void rt_hw_cpu_dcache_ops(int ops, void* addr, int size)
  59. {
  60. rt_uint32_t startAddr = (rt_uint32_t)addr & (rt_uint32_t)~(L1CACHE_LINESIZE_BYTE - 1);
  61. rt_uint32_t size_byte = size + (rt_uint32_t)addr - startAddr;
  62. rt_uint32_t clean_invalid = RT_HW_CACHE_FLUSH | RT_HW_CACHE_INVALIDATE;
  63. if ((ops & clean_invalid) == clean_invalid)
  64. {
  65. SCB_CleanInvalidateDCache_by_Addr((uint32_t *)startAddr, size_byte);
  66. }
  67. else if (ops & RT_HW_CACHE_FLUSH)
  68. {
  69. SCB_CleanDCache_by_Addr((uint32_t *)startAddr, size_byte);
  70. }
  71. else if (ops & RT_HW_CACHE_INVALIDATE)
  72. {
  73. SCB_InvalidateDCache_by_Addr((uint32_t *)startAddr, size_byte);
  74. }
  75. else
  76. {
  77. RT_ASSERT(0);
  78. }
  79. }