mbox.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * File : mbox.c
  3. * Copyright (c) 2006-2021, RT-Thread Development Team
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2019-08-29 zdzn first version
  10. */
  11. /* mailbox message buffer */
  12. #include "mbox.h"
  13. #include "mmu.h"
  14. volatile unsigned int *mbox = (volatile unsigned int *) MBOX_ADDR;
  15. /**
  16. * Make a mailbox call. Returns 0 on failure, non-zero on success
  17. */
  18. void init_mbox_mmu_map()
  19. {
  20. rt_hw_change_mmu_table(MBOX_ADDR, 96, MBOX_ADDR, STRONG_ORDER_MEM);
  21. }
  22. int mbox_call(unsigned char ch, int mmu_enable)
  23. {
  24. unsigned int r = (((MBOX_ADDR)&~0xF) | (ch&0xF));
  25. if (mmu_enable)
  26. r = BUS_ADDRESS(r);
  27. /* wait until we can write to the mailbox */
  28. do
  29. {
  30. asm volatile("nop");
  31. } while (*MBOX_STATUS & MBOX_FULL);
  32. /* write the address of our message to the mailbox with channel identifier */
  33. *MBOX_WRITE = r;
  34. /* now wait for the response */
  35. while (1)
  36. {
  37. /* is there a response? */
  38. do
  39. {
  40. asm volatile("nop");
  41. }
  42. while (*MBOX_STATUS & MBOX_EMPTY);
  43. /* is it a response to our message? */
  44. if (r == *MBOX_READ)
  45. {
  46. /* is it a valid successful response? */
  47. return mbox[1] == MBOX_RESPONSE;
  48. }
  49. }
  50. return 0;
  51. }