read_mbox.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 = 0, ret = 0;
  19. signal(SIGIO, mailbox_irq_handler);
  20. ret = fcntl(fd, F_SETOWN, getpid());
  21. printf("mailbox fcntl F_SETOWN ret: %d\n",ret);
  22. flags = fcntl(fd, F_GETFL);
  23. fcntl(fd, F_SETFL, flags | FASYNC);
  24. }
  25. void * mailbox_user_read(void *arg)
  26. {
  27. char buf[8] = {0};
  28. int fp = open("/sys/kernel/debug/soc:mailbox_client@0/message", O_RDWR);
  29. while(active) {
  30. read(fp, buf, sizeof(char));
  31. printf("user recv:%c\n", buf[0]);
  32. }
  33. close(fp);
  34. return NULL;
  35. }
  36. int main(int argv,char **argc)
  37. {
  38. int fd = 0, i = 0;
  39. char buf_tx[8];
  40. fd = open("/sys/kernel/debug/soc:mailbox_client@0/message", O_RDWR);
  41. if (fd < 0) {
  42. printf("open device error\n");
  43. return 1;
  44. }
  45. pthread_t thread_read;
  46. if (pthread_create(&thread_read, NULL, mailbox_user_read, NULL) != 0) {
  47. printf("pthread_create error\n");
  48. close(fd);
  49. return 0;
  50. }
  51. buf_tx[0] = 'a';
  52. do {
  53. write(fd, buf_tx, sizeof(char));
  54. printf("user send:%c\n", buf_tx[0]);
  55. sleep(1);
  56. buf_tx[0]++;
  57. i++;
  58. } while (i < 2);
  59. active = 0;
  60. close(fd);
  61. return 0;
  62. }