mbox.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * File : mbox.c
  3. * Copyright (c) 2006-2018, 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 __attribute__((aligned(16))) mbox[36];
  15. volatile unsigned int *mbox = (volatile unsigned int *) MBOX_ADDR;
  16. /**
  17. * Make a mailbox call. Returns 0 on failure, non-zero on success
  18. */
  19. int mbox_call(unsigned char ch, int mmu_enable)
  20. {
  21. unsigned int r = (((MBOX_ADDR)&~0xF) | (ch&0xF));
  22. if(mmu_enable)
  23. r = BUS_ADDRESS(r);
  24. /* wait until we can write to the mailbox */
  25. do
  26. {
  27. asm volatile("nop");
  28. } while (*MBOX_STATUS & MBOX_FULL);
  29. /* write the address of our message to the mailbox with channel identifier */
  30. *MBOX_WRITE = r;
  31. /* now wait for the response */
  32. // rt_kprintf("mailbox request %x\n",r);
  33. while(1)
  34. {
  35. /* is there a response? */
  36. do
  37. {
  38. asm volatile("nop");
  39. } while (*MBOX_STATUS & MBOX_EMPTY);
  40. /* is it a response to our message? */
  41. if (r == *MBOX_READ){
  42. /* is it a valid successful response? */
  43. // rt_kprintf("mbox: %x, %x, %x, %x, %x, %x, %x, %x\n", mbox[0], mbox[1], mbox[2], mbox[3], mbox[4], mbox[5], mbox[6], mbox[7]);
  44. return mbox[1] == MBOX_RESPONSE;
  45. }
  46. }
  47. return 0;
  48. }