cpu_cache.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. void rt_hw_cpu_icache_enable(void)
  18. {
  19. SCB_EnableICache();
  20. }
  21. void rt_hw_cpu_icache_disable(void)
  22. {
  23. SCB_DisableICache();
  24. }
  25. rt_base_t rt_hw_cpu_icache_status(void)
  26. {
  27. return 0;
  28. }
  29. void rt_hw_cpu_icache_ops(int ops, void* addr, int size)
  30. {
  31. rt_uint32_t address = (rt_uint32_t)addr & (rt_uint32_t) ~(L1CACHE_LINESIZE_BYTE - 1);
  32. rt_int32_t size_byte = size + address - (rt_uint32_t)addr;
  33. rt_uint32_t linesize = 32U;
  34. if (ops & RT_HW_CACHE_INVALIDATE)
  35. {
  36. __DSB();
  37. while (size_byte > 0)
  38. {
  39. SCB->ICIMVAU = address;
  40. address += linesize;
  41. size_byte -= linesize;
  42. }
  43. __DSB();
  44. __ISB();
  45. }
  46. }
  47. void rt_hw_cpu_dcache_enable(void)
  48. {
  49. SCB_EnableDCache();
  50. }
  51. void rt_hw_cpu_dcache_disable(void)
  52. {
  53. SCB_DisableDCache();
  54. }
  55. rt_base_t rt_hw_cpu_dcache_status(void)
  56. {
  57. return 0;
  58. }
  59. void rt_hw_cpu_dcache_ops(int ops, void* addr, int size)
  60. {
  61. rt_uint32_t startAddr = (rt_uint32_t)addr & (rt_uint32_t)~(L1CACHE_LINESIZE_BYTE - 1);
  62. rt_uint32_t size_byte = size + (rt_uint32_t)addr - startAddr;
  63. rt_uint32_t clean_invalid = RT_HW_CACHE_FLUSH | RT_HW_CACHE_INVALIDATE;
  64. if ((ops & clean_invalid) == clean_invalid)
  65. {
  66. SCB_CleanInvalidateDCache_by_Addr((rt_uint32_t *)startAddr, size_byte);
  67. }
  68. else if (ops & RT_HW_CACHE_FLUSH)
  69. {
  70. SCB_CleanDCache_by_Addr((rt_uint32_t *)startAddr, size_byte);
  71. }
  72. else if (ops & RT_HW_CACHE_INVALIDATE)
  73. {
  74. SCB_InvalidateDCache_by_Addr((rt_uint32_t *)startAddr, size_byte);
  75. }
  76. else
  77. {
  78. RT_ASSERT(0);
  79. }
  80. }