main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /******************************************************************************
  2. 版权所有: @copyright (C) 2024-2034 HaiYang Technology Corp. All rights reserved.
  3. 文件名称: main.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 "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. int main(int argc, char *argv[]){
  80. pthread_attr_t attr;
  81. struct sched_param param;
  82. pthread_t thread;
  83. ccu_init();
  84. ustimer_init();
  85. shm_init();
  86. #ifdef UES_THREAD_RT
  87. // 初始化线程属性
  88. pthread_attr_init(&attr);
  89. // 设置调度策略为 SCHED_FIFO
  90. pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
  91. // 设置线程优先级
  92. param.sched_priority = 90;
  93. pthread_attr_setschedparam(&attr, &param);
  94. // 显式指定调度策略
  95. pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
  96. // 创建线程
  97. pthread_create(&thread, &attr, thread_function, NULL);
  98. // 等待线程结束
  99. pthread_join(thread, NULL);
  100. // 销毁线程属性
  101. pthread_attr_destroy(&attr);
  102. #endif
  103. while (true) {
  104. unsigned long origin = ustimer_get_origin();
  105. printf("Current timer value: %lu\n", origin);
  106. shm_comm_test();
  107. usleep(1000 * 2000);
  108. }
  109. ustimer_exit();
  110. ccu_exit();
  111. shm_exit();
  112. return 0;
  113. }