main_demo.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /******************************************************************************
  2. 版权所有: @copyright (C) 2024-2034 HaiYang Technology Corp. All rights reserved.
  3. 文件名称: main_demo.c
  4. 文件版本: 01.01
  5. 创建作者: zhaoyang
  6. 创建日期: 2025-07-28
  7. 功能说明: 主程序
  8. 其它说明:
  9. 修改记录:
  10. */
  11. #include <stdio.h>
  12. #include <stdbool.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <time.h>
  16. #include <pthread.h>
  17. #include <sched.h>
  18. #include <sys/resource.h>
  19. #include "bsp_ustimer.h"
  20. #include <sys/random.h> // 生成随机数,用于测试
  21. #include "bsp_share.h"
  22. #include <sys/mman.h>
  23. #include "bsp_ccu.h"
  24. #include "bsp_shm.h"
  25. #include "bsp_packet.h"
  26. void *thread_function(void *arg) {
  27. int ret, i = 0;
  28. long diff = 0;
  29. unsigned int *tmp_buf;
  30. if (shmem_init(&shmem_fd)) {
  31. printf("shmem_init failed!\n");
  32. return NULL;
  33. }
  34. tmp_buf = mmap(NULL, shmem_fd.linux_read_len, PROT_READ|PROT_WRITE,
  35. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  36. if (tmp_buf <= 0) {
  37. printf("mmap failed!\n");
  38. return NULL;
  39. }
  40. printf("tmp_buf:%lx\n", (unsigned long)tmp_buf);
  41. while (true) {
  42. static unsigned long tmp = 0;
  43. unsigned int test_data;
  44. unsigned short crc;
  45. int len = shmem_fd.linux_write_data_len;
  46. unsigned long origin = ustimer_get_origin();
  47. diff = (origin > tmp + 1000 * 5) ? (origin - tmp - 1000 * 5) : (tmp + 1000 * 5 - origin);
  48. printf("Current timer value: %lu diff: %lu priority: %d\n", origin, diff, getpriority(PRIO_PROCESS, 0));
  49. tmp = origin;
  50. // 模拟一些工作 [46ms]
  51. // 获取随机数,用于测试
  52. if (getrandom(&test_data, sizeof(test_data), GRND_RANDOM) == -1) {
  53. printf("getrandom failed");
  54. goto done;
  55. }
  56. for (i = 0; i < len/4; i++)
  57. tmp_buf[i] = test_data;
  58. ret = shmem_write(&shmem_fd, tmp_buf, len, 0);
  59. if (ret < 0) {
  60. printf("failed to write shmem %d\n", ret);
  61. // goto done;
  62. } else {
  63. printf("write crc=%04x len=%d\n", crc, ret);
  64. }
  65. ret = shmem_read(&shmem_fd, tmp_buf, len, 0);
  66. if (ret < 0) {
  67. printf("failed to read shmem %d\n", ret);
  68. // goto done;
  69. } else {
  70. printf(" read crc=%04x len=%d\n", crc, ret);
  71. }
  72. usleep((1000 * 5 - diff) > 0 ? (1000 * 5 - diff) : 1000 * 5);
  73. }
  74. done:
  75. munmap(tmp_buf, shmem_fd.linux_read_len);
  76. shmem_exit(&shmem_fd);
  77. return NULL;
  78. }
  79. /* we test used here */
  80. int main_demo(int argc, char *argv[]){
  81. pthread_attr_t attr;
  82. struct sched_param param;
  83. pthread_t thread;
  84. ccu_init();
  85. ustimer_init();
  86. shm_init();
  87. #ifdef UES_THREAD_RT
  88. // 初始化线程属性
  89. pthread_attr_init(&attr);
  90. // 设置调度策略为 SCHED_FIFO
  91. pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
  92. // 设置线程优先级
  93. param.sched_priority = 90;
  94. pthread_attr_setschedparam(&attr, &param);
  95. // 显式指定调度策略
  96. pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
  97. // 创建线程
  98. pthread_create(&thread, &attr, thread_function, NULL);
  99. // 等待线程结束
  100. pthread_join(thread, NULL);
  101. // 销毁线程属性
  102. pthread_attr_destroy(&attr);
  103. #endif
  104. while (true) {
  105. unsigned long origin = ustimer_get_origin();
  106. printf("Current timer value: %lu\n", origin);
  107. shm_comm_test();
  108. usleep(1000 * 2000);
  109. }
  110. ustimer_exit();
  111. ccu_exit();
  112. shm_exit();
  113. return 0;
  114. }