cpu_cache.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. if (ops & (RT_HW_CACHE_FLUSH | RT_HW_CACHE_INVALIDATE))
  63. {
  64. SCB_CleanInvalidateDCache_by_Addr((uint32_t *)startAddr, size_byte);
  65. }
  66. else if (ops & RT_HW_CACHE_FLUSH)
  67. {
  68. SCB_CleanDCache_by_Addr((uint32_t *)startAddr, size_byte);
  69. }
  70. else if (ops & RT_HW_CACHE_INVALIDATE)
  71. {
  72. SCB_InvalidateDCache_by_Addr((uint32_t *)startAddr, size_byte);
  73. }
  74. else
  75. {
  76. RT_ASSERT(0);
  77. }
  78. }