mbox.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. void init_mbox_mmu_map(){
  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. // rt_kprintf("mailbox request %x\n",r);
  36. while(1)
  37. {
  38. /* is there a response? */
  39. do
  40. {
  41. asm volatile("nop");
  42. } while (*MBOX_STATUS & MBOX_EMPTY);
  43. /* is it a response to our message? */
  44. if (r == *MBOX_READ){
  45. /* is it a valid successful response? */
  46. // 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]);
  47. return mbox[1] == MBOX_RESPONSE;
  48. }
  49. }
  50. return 0;
  51. }