shm_api.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @file shm_api.c
  3. * @brief 共享内存接口文件
  4. * @author lch (lch_work@foxmail.com)
  5. * @version 1.0
  6. * @date 20250917
  7. *
  8. * @copyright Copyright (c) 2025 by OLE, All Rights Reserved.
  9. *
  10. * @par 修改日志:
  11. * <table>
  12. * <tr><th>Date <th>Version <th>Author <th>Description
  13. * <tr><td>20250917 <td>1.0 <td>lch <td>内容
  14. * </table>
  15. */
  16. /***** 宏定义 *****/
  17. #ifndef DEF_SHM_API_H
  18. #define DEF_SHM_API_H
  19. #define MAX_SHMEM_RD_LEN (CONFIG_SHARE_MEM_LENGTH / 2)
  20. #define MAX_SHMEM_WR_LEN (CONFIG_SHARE_MEM_LENGTH / 2)
  21. #define MAX_SHMEM_RD_DATA_LEN (MAX_SHMEM_RD_LEN)
  22. #define MAX_SHMEM_WR_DATA_LEN (MAX_SHMEM_WR_LEN)
  23. /* 内存地址 */
  24. /* linux核写 RV核读 */
  25. #define SHM_ADDR_R_MACHINETYPE 0x00000000 /* 装置类型 */
  26. #define SHM_ADDR_R_DI_ASTIME 0x00001000 /* 开入防抖时间 */
  27. #define SHM_ADDR_R_TIMESTAMP 0x00002000 /* ms时间戳 */
  28. #define SHM_ADDR_R_MACHINEPARAM 0x00003000 /* 参数数据 */
  29. /* linux核读 RV核写 */
  30. #define SHM_ADDR_W_DI 0x00000000 /* 开入量数据 */
  31. #define SHM_ADDR_W_FREQ 0x00001000 /* 频率 */
  32. #define SHM_ADDR_W_U_ADC_1 0x00020000 /* 第一片ADC采样数据区 */
  33. #define SHM_ADDR_W_U_ADC_2 0x00040000 /* 第二片ADC采样数据区 */
  34. #define SHM_ADDR_W_U_ADC_3 0x00060000 /* 第三片ADC采样数据区 */
  35. #define SHM_ADDR_W_U_ADC_4 0x00080000 /* 第四片ADC采样数据区 */
  36. #define SHM_ADDR_W_U_ADC_5 0x000A0000 /* 第五片ADC采样数据区 */
  37. #define SHM_ADDR_W_FLAG 0x000C0000 /* 标志数据 */
  38. #ifndef DI_MAX
  39. #define DI_MAX (31)
  40. #endif
  41. #ifndef ADC_SAMPLEING_RATE /* ADC采样率 0 : 6400(156.25us) 1 : 12800(78.125us) */
  42. #define ADC_SAMPLEING_RATE 1
  43. #endif
  44. #define ADC_HARDWARE_NUM 5 /* ADC片数 先按最多的5个来 */
  45. #define ADC_CHANNEL 16 /* ADC采样的通道数 */
  46. #if (0 == ADC_SAMPLEING_RATE)
  47. #define ADC_SAMPLE_PER_CYCLE 128 /* 录波每周采样点数 */
  48. /* 每个ADC通道缓存的采样点数 理论上不会溢出 64 */
  49. #define TC_ADC_DOTS_PER_CHANNEL (ADC_SAMPLE_PER_CYCLE / 2)
  50. #else /* ADC_SAMPLEING_RATE */
  51. #define ADC_SAMPLE_PER_CYCLE 256 /* 录波每周采样点数 */
  52. /* 每个ADC通道缓存的采样点数 理论上不会溢出 96 */
  53. #define TC_ADC_DOTS_PER_CHANNEL ((ADC_SAMPLE_PER_CYCLE / 4) + ((ADC_SAMPLE_PER_CYCLE / 8)))
  54. #endif /* ADC_SAMPLEING_RATE */
  55. #define ADC_SAMPLE_UPDATA (ADC_SAMPLE_PER_CYCLE / 4) /* 录波更新点数 */
  56. /***** 头文件 *****/
  57. /***** 枚举 *****/
  58. enum e_shm_type
  59. {
  60. e_shm_machinetype = 0,
  61. e_shm_di_astime,
  62. e_shm_timestamp,
  63. e_shm_type_max,
  64. };
  65. /***** 结构体 *****/
  66. /****************************** A核写, R核读 ******************************/
  67. /* 装置类型 */
  68. struct t_shmdata_machine_type
  69. {
  70. uint16_t us_updata; /* 数据更新标志 */
  71. uint16_t us_op; /* 操作标志 */
  72. uint16_t us_op_bk; /* 操作标志备份 */
  73. uint16_t us_machine_type; /* 数据 */
  74. uint16_t us_crc; /* crc 应用不用处理 底层接口处理 */
  75. };
  76. /* 开入防抖时间 */
  77. struct t_shmdata_di_astime
  78. {
  79. uint16_t us_updata; /* 数据更新标志 */
  80. uint16_t us_op; /* 操作标志 */
  81. uint16_t us_op_bk; /* 操作标志备份 */
  82. uint16_t us_as_time[32]; /* 数据 */
  83. uint16_t us_crc; /* crc 应用不用处理 底层接口处理 */
  84. };
  85. /* ms时间戳 */
  86. struct t_shmdata_timestamp
  87. {
  88. uint16_t us_updata; /* 数据更新标志 */
  89. uint16_t us_op; /* 操作标志 */
  90. uint16_t us_op_bk; /* 操作标志备份 */
  91. uint64_t ull_timestamp; /* 数据 */
  92. uint16_t us_crc; /* crc 应用不用处理 底层接口处理 */
  93. } __attribute__ ((packed)); /* 大小16个字节 */
  94. /****************************** A核读, R核写 ******************************/
  95. /* 开入量 */
  96. struct t_sd_di_status
  97. {
  98. uint8_t uc_state; // 开入量状态
  99. uint64_t ull_timestamp; // 时间戳
  100. };
  101. struct t_shmdata_di
  102. {
  103. uint16_t us_updata; /* 数据更新标志 */
  104. uint16_t us_op; /* 操作标志 */
  105. uint16_t us_op_bk; /* 操作标志备份 */
  106. struct t_sd_di_status di[DI_MAX]; /* 数据 */
  107. uint16_t us_crc; /* crc 应用不用处理 底层接口处理 */
  108. }__attribute__ ((packed)); /* 大小504个字节 */
  109. /* 频率 */
  110. struct t_shmdata_freq
  111. {
  112. uint16_t us_updata; /* 数据更新标志 */
  113. uint16_t us_op; /* 操作标志 */
  114. uint16_t us_op_bk; /* 操作标志备份 */
  115. uint16_t us_freq[2];/*放大1000倍*/
  116. uint16_t us_crc; /* crc 应用不用处理 底层接口处理 */
  117. };
  118. /**< 采样点结构体 */
  119. struct t_sampledata_mc
  120. {
  121. uint16_t us_sample_idx; /* 采样索引下标 */
  122. uint16_t us_ft_idx; /* 频率变化下标 */
  123. uint16_t us_xdl_idx; /* 小电流突变下标 */
  124. uint16_t usa_sample_dots[ADC_CHANNEL][TC_ADC_DOTS_PER_CHANNEL];
  125. };
  126. /* 采样数据 */
  127. struct t_shmdata_adc
  128. {
  129. uint16_t us_updata; /* 数据更新标志 */
  130. uint16_t us_op; /* 操作标志 */
  131. uint16_t us_op_bk; /* 操作标志备份 */
  132. struct t_sampledata_mc data; /* 数据 */
  133. uint16_t us_crc; /* crc 应用不用处理 底层接口处理 */
  134. }; /* 大小3086个字节 */
  135. /* 标志数据 */
  136. struct t_shmdata_flag
  137. {
  138. uint16_t us_updata; /* 数据更新标志 */
  139. uint16_t us_op; /* 操作标志 */
  140. uint16_t us_op_bk; /* 操作标志备份 */
  141. uint16_t us_cfg_adc_err; /* ADC初始化错误 */
  142. uint16_t us_crc; /* crc 应用不用处理 底层接口处理 */
  143. }; /* 大小10个字节 */
  144. struct t_device_shm
  145. {
  146. int (*init) (void);
  147. int (*read) (uint32_t _ul_addr, uint32_t _ul_len, uint8_t *_puc_data_out, uint32_t _uc_data_out_size);
  148. int (*write) (uint32_t _ul_addr, uint8_t *_puc_data, uint32_t _ul_len);
  149. int (*pack_read) (uint32_t _ul_addr, uint32_t _ul_len, uint8_t *_puc_data_out, uint32_t _uc_data_out_size);
  150. int (*pack_write) (uint32_t _ul_addr, uint8_t *_puc_data, uint32_t _ul_len);
  151. int (*scan_data) (enum e_shm_type _e_type);
  152. };
  153. /***** 变量对外声明 *****/
  154. /**
  155. * @brief 共享内存结构体
  156. *
  157. * @note gt_shm.init 初始化
  158. * @note gt_shm.read 从共享内存读取数据
  159. * - {uint32_t} _ul_addr 读取共享内存的地址
  160. * - {uint32_t} _ul_len 需要读取的数据长度
  161. * - {uint8_t *} _puc_data_out 输出buf
  162. * - {uint32_t} _uc_data_out_size 输出buf的长度
  163. * - 成功返回读取数据的长度, 失败返回-1
  164. *
  165. * @note gt_shm.write 往共享内存写入数据
  166. * - {uint32_t} _ul_addr 共享内存的地址
  167. * - {uint8_t *} _puc_data 需要写入的数据
  168. * - {uint32_t} _ul_len 需要写入的数据长度
  169. * - 成功返回读取数据的长度, 失败返回-1
  170. *
  171. * @note gt_shm.pack_read 从共享内存读取数据 带读写标志
  172. * - {uint32_t} _ul_addr 读取共享内存的地址
  173. * - {uint32_t} _ul_len 需要读取的数据长度
  174. * - {uint8_t *} _puc_data_out 输出buf
  175. * - {uint32_t} _uc_data_out_size 输出buf的长度
  176. * - 成功返回读取数据的长度, 失败返回-1
  177. *
  178. * @note gt_shm.pack_write 往共享内存写入数据 带读写标志
  179. * - {uint32_t} _ul_addr 共享内存的地址
  180. * - {uint8_t *} _puc_data 需要写入的数据
  181. * - {uint32_t} _ul_len 需要写入的数据长度
  182. * - 成功返回读取数据的长度, 失败返回-1
  183. *
  184. * @note gt_shm.scan_data 扫描共享内存数据更新
  185. * - {enum e_shm_type} _e_type 共享内存的类型描述
  186. * - 有数据更新返回1
  187. */
  188. extern struct t_device_shm gt_shm;
  189. /***** 函数对外声明 *****/
  190. #endif /* end of file */