read_mbox.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <signal.h>
  7. #include <sys/ioctl.h>
  8. #include <pthread.h>
  9. volatile int msg_done = 0;
  10. int active = 1;
  11. void mailbox_irq_handler(int val)
  12. {
  13. printf("mailbox msg recv %d\n",val);
  14. msg_done = 1;
  15. }
  16. void init_signal(int fd)
  17. {
  18. int flags,ret;
  19. signal(SIGIO, mailbox_irq_handler);
  20. ret = fcntl(fd, F_SETOWN, getpid());
  21. flags = fcntl(fd, F_GETFL);
  22. fcntl(fd, F_SETFL, flags | FASYNC);
  23. }
  24. void * mailbox_user_read(void *arg)
  25. {
  26. char buf[8];
  27. int fp = open("/sys/kernel/debug/soc:mailbox_client@0/message",O_RDWR);
  28. while(active)
  29. {
  30. read(fp,buf,sizeof(char));
  31. printf("user recv:%c\n",buf[0]);
  32. }
  33. close(fp);
  34. }
  35. int main(int argv,char **argc)
  36. {
  37. int fd,i=0,ret;
  38. char buf_tx[8],buf_rx[8];
  39. fd = open("/sys/kernel/debug/soc:mailbox_client@0/message",O_RDWR);
  40. if(fd < 0)
  41. printf("open device error\n");
  42. pthread_t thread_read;
  43. if(pthread_create(&thread_read,NULL,mailbox_user_read,NULL) != 0)
  44. {
  45. printf("pthread_create error\n");
  46. close(fd);
  47. return 0;
  48. }
  49. buf_tx[0] = 'a';
  50. do{
  51. write(fd,buf_tx,sizeof(char));
  52. printf("user send:%c\n",buf_tx[0]);
  53. sleep(1);
  54. buf_tx[0]++;
  55. i++;
  56. }while(i<2);
  57. active = 0;
  58. close(fd);
  59. return 1;
  60. }