cpu_cache.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2006-2023, 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 <rtthread.h>
  12. #include <rthw.h>
  13. #include <rtdef.h>
  14. #include <board.h>
  15. /* The L1-caches on all Cortex®-M7s are divided into lines of 32 bytes. */
  16. #define L1CACHE_LINESIZE_BYTE (32)
  17. #ifdef RT_USING_CACHE
  18. void rt_hw_cpu_icache_enable(void)
  19. {
  20. SCB_EnableICache();
  21. }
  22. void rt_hw_cpu_icache_disable(void)
  23. {
  24. SCB_DisableICache();
  25. }
  26. rt_base_t rt_hw_cpu_icache_status(void)
  27. {
  28. return 0;
  29. }
  30. void rt_hw_cpu_icache_ops(int ops, void* addr, int size)
  31. {
  32. rt_uint32_t address = (rt_uint32_t)addr & (rt_uint32_t) ~(L1CACHE_LINESIZE_BYTE - 1);
  33. rt_int32_t size_byte = size + address - (rt_uint32_t)addr;
  34. rt_uint32_t linesize = 32U;
  35. if (ops & RT_HW_CACHE_INVALIDATE)
  36. {
  37. __DSB();
  38. while (size_byte > 0)
  39. {
  40. SCB->ICIMVAU = address;
  41. address += linesize;
  42. size_byte -= linesize;
  43. }
  44. __DSB();
  45. __ISB();
  46. }
  47. }
  48. void rt_hw_cpu_dcache_enable(void)
  49. {
  50. SCB_EnableDCache();
  51. }
  52. void rt_hw_cpu_dcache_disable(void)
  53. {
  54. SCB_DisableDCache();
  55. }
  56. rt_base_t rt_hw_cpu_dcache_status(void)
  57. {
  58. return 0;
  59. }
  60. void rt_hw_cpu_dcache_ops(int ops, void* addr, int size)
  61. {
  62. rt_uint32_t startAddr = (rt_uint32_t)addr & (rt_uint32_t)~(L1CACHE_LINESIZE_BYTE - 1);
  63. rt_uint32_t size_byte = size + (rt_uint32_t)addr - startAddr;
  64. rt_uint32_t clean_invalid = RT_HW_CACHE_FLUSH | RT_HW_CACHE_INVALIDATE;
  65. if ((ops & clean_invalid) == clean_invalid)
  66. {
  67. SCB_CleanInvalidateDCache_by_Addr((void *)startAddr, size_byte);
  68. }
  69. else if (ops & RT_HW_CACHE_FLUSH)
  70. {
  71. SCB_CleanDCache_by_Addr((void *)startAddr, size_byte);
  72. }
  73. else if (ops & RT_HW_CACHE_INVALIDATE)
  74. {
  75. SCB_InvalidateDCache_by_Addr((void *)startAddr, size_byte);
  76. }
  77. else
  78. {
  79. RT_ASSERT(0);
  80. }
  81. }
  82. #endif /* RT_USING_CACHE */