ad7616.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: adc.c
  4. 文件版本: 01.01
  5. 创建作者: sunxi
  6. 创建日期: 2025-06-09
  7. 功能说明: adc驱动程序。
  8. 其它说明:
  9. 修改记录:
  10. */
  11. /*------------------------------ 头文件 ---------------------------------------
  12. */
  13. #include "bsp.h"
  14. #include "rt.h"
  15. #include "head.h"
  16. #include <malloc.h>
  17. #include "rt_printf.h"
  18. // #include "shm_comm_packet.h"
  19. #include <mb.h>
  20. #include "shmem.h"
  21. /*------------------------------ 宏定义 ---------------------------------------
  22. */
  23. #if CFG_BSP_DEBUG
  24. #define _ADC_DEBUG
  25. #endif
  26. #define AC_HZ_NORMAL 50.0 // 交流标准频率
  27. #define ADC_DOT_BUF_SIZE ((CFG_ADC_CHANNEL)*(CFG_ADC_DOTS_PER_CHANNEL*2)*2)
  28. /*------------------------------ 全局变量 -------------------------------------
  29. */
  30. // AD片选地址,8、9、10、11槽对应CS2、CS1、CS3、CS4。
  31. static int g_adc_slot2cs[EQU_SLOT_AC_NUM] = {0};
  32. static u8 g_adc_chip;
  33. static u32 g_adc_range[EQU_SLOT_AC_NUM];
  34. const u8 g_cs_channel_table[CFG_ADC_CHANNEL]={1,9,2,10,3,11,4,12,5,13,6,14,7,15,8,16};
  35. //每个通道占用的空间:CFG_ADC_DOTS_PER_CHANNEL*2*2 = 128*2*2=512
  36. //通道数:CFG_ADC_CHANNEL+2= 64+2=66
  37. //总空间:512*18=9216=0x2400
  38. //保证首地址是buf长度两倍的倍数。以利用MAC计算的MASK寄存器来达到
  39. //循环buf的功能。注意每个通道buf的后半部分目前没有利用,以后可以用来干需要干的事情.
  40. //通道加2,用作开入开出等状态保存。
  41. // 不能使用分配的DMA内存,因为冷火实时linux平台不能使用动态内存映射。
  42. //short (*g_adc_dots)[CFG_ADC_DOTS_PER_CHANNEL*2];
  43. //s16 (*g_adc_dots)[CFG_ADC_DOTS_PER_CHANNEL*2] = (short (*)[CFG_ADC_DOTS_PER_CHANNEL*2])0x8ff00000;
  44. s16 (*g_adc_dots)[CFG_ADC_DOTS_PER_CHANNEL*2];
  45. struct timespec ts_adc_dots[CFG_ADC_DOTS_PER_CHANNEL];//时间戳
  46. // 128点录波,32点计算
  47. // 注意此buf由于是DMA类型,不能在实时中断中使用,只能在线程中使用。
  48. //s16 g_adc_dots_rec[CFG_ADC_DOTS_PER_CHANNEL*4][CFG_ADC_CHANNEL];
  49. s16 (*g_adc_dots_rec)[CFG_ADC_CHANNEL];
  50. u32 g_adc_dots_rec_dma;
  51. // 直流量测量采样点BUF
  52. u16 g_adc_dots_dc[CFG_ADC_CHANNEL_DC][CFG_ADC_DOTS_PER_CHANNEL];
  53. //ADC同步标志,表示ADC采样点BUFFER中的数据是否有效,AD7616转换的第一次数据也无效。
  54. int g_adc_sync = -(CFG_ADC_DOTS_PER_CHANNEL+1);
  55. // ADC点BUF的索引,当前索引下无值。
  56. volatile unsigned long g_adc_dots_count;
  57. int g_adc_channel;
  58. unsigned long g_adZero=14790;
  59. char g_adc_name[CFG_ADC_CHANNEL_ALL][20];
  60. struct rt_stat g_stat_adc[CFG_ADC_CHANNEL_ALL];
  61. // 跟踪频率
  62. float g_freq = 50.0 ;
  63. // 直流量测量采样点BUF
  64. u16 g_adc_dots_dc[CFG_ADC_CHANNEL_DC][CFG_ADC_DOTS_PER_CHANNEL];
  65. uint32_t g_yx_buf[2] = {0xffffffff,0xffffffff}; //预留64个遥信
  66. int g_adc_stat_flag=0;
  67. volatile unsigned int adc_wp;
  68. volatile unsigned int adc_rp;
  69. bool prt_flag;
  70. volatile unsigned long g_adc_dots_data_count;
  71. SHM_VER_T LuoHe_version;
  72. /*------------------------------ 函数声明 -------------------------------------
  73. */
  74. void dido_di_poll(SHM_SAMPLE_T *sample_data);
  75. /*------------------------------ 外部函数 -------------------------------------
  76. */
  77. //设置各通道的参考电压
  78. static int write_v_default(uint8_t no, uint8_t *data)
  79. {
  80. int ret = 0;
  81. int cnt = 0;
  82. if(data == NULL)
  83. return -1;
  84. while(1)
  85. {
  86. // ret = shm_comm_packet_write(SHM_ADDR_D_RFV_1+(SHM_ADDR_D_RFV_2-SHM_ADDR_D_RFV_1)*no, data, sizeof(SHM_RFV_T));
  87. if(ret > 0)
  88. {
  89. break;
  90. }
  91. if(++cnt > 3)
  92. break;
  93. usleep(30);
  94. }
  95. return ret;
  96. }
  97. int adc_set_v_default(int no, u32 value)
  98. {
  99. int ret = 0;
  100. SHM_RFV_T v;
  101. v.rc = value;
  102. ret = write_v_default(no,(uint8_t *)&v);
  103. return ret;
  104. }
  105. void adc_get_range_config(int no,int slot)
  106. {
  107. int i,scale,channel,shift;
  108. u32 rc;
  109. rc = 0;
  110. for(i=0;i<EQU_SLOT_AC_CHN;i++)
  111. {
  112. channel = equ_get_ac_channel(slot,i)&0x0f;
  113. // 确定需要移位的数量
  114. // 双数是通道A,在低16位
  115. // 单数是通道B,在高16位
  116. shift = (channel/2)*2;
  117. if((channel&0x01))
  118. {
  119. shift += 16;
  120. }
  121. if(channel == AD7616_DC0_CHANNEL || channel == AD7616_DC1_CHANNEL)
  122. {
  123. rc |= 2<<shift;// 5V
  124. continue;
  125. }
  126. scale = equ_get_ac_scale(slot,i);
  127. switch(scale)
  128. {
  129. case EQU_SCALE_PT_DEFAULT:
  130. case EQU_SCALE_PT_120K:
  131. case EQU_SCALE_PT_172K:
  132. case EQU_SCALE_PT_204K:
  133. case EQU_SCALE_CT_10A_51:
  134. case EQU_SCALE_CT_10A_120:
  135. rc |= 1<<shift; // 2.5V
  136. break;
  137. case EQU_SCALE_PT_102K:
  138. case EQU_SCALE_PT_120K_604:
  139. case EQU_SCALE_PT_264V_3V53:
  140. case EQU_SCALE_CT_100A_3V53:
  141. case EQU_SCALE_CT_DEFAULT:
  142. case EQU_SCALE_CT_10A_249:
  143. case EQU_SCALE_CT_10A_604:
  144. case EQU_SCALE_EVT_3V25_100V:
  145. case EQU_SCALE_PT_400V_3V53:
  146. case EQU_SCALE_CT_20A_3V53:
  147. rc |= 2<<shift;// 5V
  148. break;
  149. case EQU_SCALE_EVT_6V50_100V:
  150. case EQU_SCALE_ECT_1V_1A:
  151. case EQU_SCALE_ECT_0V2_1A: // 零序电子CT采用2.5V的范围
  152. rc |= 3<<shift; // 10V
  153. break;
  154. }
  155. }
  156. adc_set_v_default(no,rc);
  157. mb_notice_RfV_update(0);
  158. }
  159. static int get_adc_wp(void)
  160. {
  161. WP_T wp;
  162. int ret = 0;
  163. int cnt = 0;
  164. while(1)
  165. {
  166. ret = shm_comm_packet_read(SHM_ADDR_U_WP, sizeof(wp), (uint8_t *)&wp, sizeof(wp));
  167. if(ret > 0)
  168. {
  169. return wp.write_flag_A;
  170. }
  171. if(++cnt > 3)
  172. break;
  173. usleep(30);
  174. }
  175. return ret;
  176. }
  177. static int get_adc_sample(uint32_t addr, int index, uint8_t *out_data)
  178. {
  179. SHM_SAMPLE_T sample;
  180. int ret = 0;
  181. int cnt = 0;
  182. if(out_data == NULL)
  183. return -1;
  184. while(1)
  185. {
  186. ret = shm_comm_packet_read_noABcrc(addr + index*(sizeof(sample)), sizeof(sample), out_data, sizeof(sample));
  187. if(ret > 0)
  188. {
  189. break;
  190. }
  191. if(++cnt > 3)
  192. break;
  193. usleep(30);
  194. }
  195. return ret;
  196. }
  197. static int get_e907_version(SHM_VER_T* par)
  198. {
  199. SHM_VER_T ver;
  200. int ret = 0;
  201. int cnt = 0;
  202. if(par == NULL)
  203. return -1;
  204. while(1)
  205. {
  206. ret = shm_comm_packet_read(SHM_ADDR_U_VER, sizeof(ver), (uint8_t *)&ver, sizeof(ver));
  207. if(ret > 0)
  208. {
  209. par->ver = ver.ver;
  210. par->v_crc = ver.v_crc;
  211. strcpy(par->time,ver.time);
  212. return 0;
  213. }
  214. if(++cnt > 3)
  215. break;
  216. usleep(30);
  217. }
  218. return -1;
  219. }
  220. int adc_init(void)
  221. {
  222. int ret = 0;
  223. int i;
  224. /*
  225. ret = get_adc_wp();
  226. if(ret < 0)
  227. {
  228. rt_printf("get_adc_wp 返回错误值,ret=%d!\r\n",adc_wp);
  229. return -1;
  230. }
  231. adc_wp = adc_rp = ret;
  232. */
  233. memset(&LuoHe_version,0,sizeof(LuoHe_version));
  234. g_adc_dots_count=0;
  235. prt_flag=false;
  236. // 采样计算点BUF初始化
  237. // 32;周波数
  238. g_adc_dots = malloc(ADC_DOT_BUF_SIZE);
  239. if(g_adc_dots == NULL)
  240. {
  241. rt_printf("g_adc_dots 分配失败!\r\n");
  242. return -1;
  243. }
  244. memset(g_adc_dots, 0x55, ADC_DOT_BUF_SIZE);
  245. g_adc_dots_data_count=0;
  246. // 录波点BUF初始化
  247. // 32;周波数
  248. g_adc_dots_rec = malloc(CFG_ADC_CHANNEL*2*ADC_REC_DOTS_CHANNEL);
  249. if(g_adc_dots_rec == NULL)
  250. {
  251. rt_printf("g_adc_dots_rec 分配失败!\r\n");
  252. rt_free(g_adc_dots);
  253. g_adc_dots = NULL;
  254. return -1;
  255. }
  256. // 初始化统计变量
  257. for(i=0; i<CFG_ADC_CHANNEL_ALL; i++)
  258. {
  259. sprintf(g_adc_name[i],"adc%02d[%08lx]",i,(u32)g_adc_dots + i*sizeof(g_adc_dots[0]));
  260. rt_stat_init(&g_stat_adc[i],g_adc_name[i]);
  261. }
  262. #if 0
  263. for(i=0; i<EQU_SLOT_AC_NUM; i++) //配置通道的采样参数 直接写死 lch
  264. {
  265. printf("[%s:%d]\r\n", __func__, __LINE__);
  266. adc_get_range_config(i,g_ac_slot_begin+g_adc_slot2cs[i]);
  267. }
  268. // 检查和槽对应的ADC芯片是否安装或损坏
  269. // printk("g_adc_chip=%02x.\r\n",g_adc_chip);
  270. for(i=0; i< EQU_SLOT_AC_NUM; i++)
  271. {
  272. if(equ_get_ac_num(g_ac_slot_begin+i))
  273. {
  274. if(adc_slot_is_ok(i) == 0)
  275. {
  276. rt_err_set(ERR_CODE_INIT_ADC,i);
  277. rt_printf("槽%02ld:ADC 芯片未安装或错误!\r\n",g_ac_slot_begin+i);
  278. }
  279. }
  280. }
  281. #endif
  282. return ret;
  283. }
  284. int adc_exit(void)
  285. {
  286. int ret=0;
  287. if(g_adc_dots_rec)
  288. {
  289. free(g_adc_dots_rec);
  290. g_adc_dots_rec=0;
  291. }
  292. if(g_adc_dots)
  293. {
  294. free(g_adc_dots);
  295. g_adc_dots=0;
  296. }
  297. return ret;
  298. }
  299. /**
  300. * @brief 旧方式获取共享内存数据(已抛弃)
  301. *
  302. * @param mod
  303. * @return int
  304. * @author zhl
  305. */
  306. int adc_isr(int mod)
  307. {
  308. int opt,i;
  309. SHM_SAMPLE_T sample_data[2];
  310. int ret = 0;
  311. int cnt = 0;
  312. int index = 0;
  313. static int start_flag = 0;
  314. static int recv_init = 0;
  315. static int get_version_flag = 0;
  316. ret = get_adc_wp();
  317. if(ret < 0)
  318. {
  319. // rt_printf("get_adc_wp 返回错误值, ret=%d!\r\n",ret);
  320. return -1;
  321. }
  322. if(get_version_flag == 0)
  323. {
  324. ret = get_e907_version(&LuoHe_version);
  325. if(ret == 0)
  326. {
  327. get_version_flag = 1;//noted by sunxi:这个应该要读成功后,才置为1.
  328. rt_printf("裸核版本号: ver=v%d.%02d,%s,crc=%X\r\n",LuoHe_version.ver/100,LuoHe_version.ver%100,LuoHe_version.time,LuoHe_version.v_crc);
  329. }
  330. }
  331. if(start_flag == 0)
  332. {
  333. //第1次运行,初始化 adc_rp
  334. start_flag++;
  335. adc_wp = ret;
  336. adc_rp = ret;
  337. }
  338. else
  339. {
  340. adc_wp = ret;
  341. }
  342. if(adc_wp>=SHM_ADC_WR_NUM)
  343. {
  344. adc_wp=0;
  345. }
  346. if(adc_rp>=SHM_ADC_WR_NUM)
  347. {
  348. adc_rp=0;
  349. }
  350. while(adc_wp!=adc_rp)
  351. {
  352. opt=((g_adc_dots_data_count&(ADC_REC_SAMPLE_RATE-1))==0)?TRUE:FALSE;
  353. ret = get_adc_sample(SHM_ADDR_U_ADC_1, adc_rp,(uint8_t *)&sample_data[0]);
  354. ret = get_adc_sample(SHM_ADDR_U_ADC_2, adc_rp,(uint8_t *)&sample_data[1]);
  355. if(ret < 0)
  356. {
  357. if(++cnt > 3)
  358. {
  359. rt_printf("get_adc_sample 返回错误值, ret=%d!\r\n",ret);
  360. break;
  361. }
  362. continue;
  363. }
  364. //保存yx值
  365. g_yx_buf[0] = sample_data[0].yx_buf[0];
  366. g_yx_buf[1] = sample_data[1].yx_buf[1];
  367. if(!recv_init)
  368. {
  369. recv_init = 1;
  370. //开机时裸核还没有把遥信送上下,这里重新初始化plc_init
  371. // plc_init(); TODO EWen
  372. }
  373. // 标准化DTU遥信已经经过开入子板处理防抖
  374. // dido_di_poll(&sample_data[0]);
  375. for(i=0; i<16; i++)
  376. {
  377. g_adc_dots_rec[g_adc_dots_index_rec][i] = sample_data[0].sample_buf[i];
  378. if(opt==TRUE)
  379. {
  380. g_adc_dots[i][g_adc_dots_index] = sample_data[0].sample_buf[i];
  381. ts_adc_dots[g_adc_dots_index] = sample_data[0].ts;
  382. }
  383. }
  384. for(i=16; i<CFG_ADC_CHANNEL; i++)
  385. {
  386. g_adc_dots_rec[g_adc_dots_index_rec][i] = sample_data[1].sample_buf[i - 16];
  387. if(opt==TRUE)
  388. {
  389. g_adc_dots[i][g_adc_dots_index] = sample_data[1].sample_buf[i - 16];
  390. ts_adc_dots[g_adc_dots_index] = sample_data[1].ts;
  391. }
  392. }
  393. g_adc_dots_dc[0][g_adc_dots_data_count&CFG_ADC_DOTS_MASK] = sample_data[0].sample_buf[AD7616_DC0_CHANNEL];
  394. // g_adc_dots_dc[1][g_adc_dots_data_count&CFG_ADC_DOTS_MASK] = sample_data.sample_buf[AD7616_DC0_CHANNEL];
  395. if(opt==TRUE)
  396. {
  397. index = g_adc_dots_index;
  398. // 统计数据
  399. if(g_adc_stat_flag)
  400. {
  401. for(i=0; i<CFG_ADC_CHANNEL; i++)
  402. {
  403. rt_stat_in(&g_stat_adc[i],(unsigned short)(g_adc_dots[i][index]+32768));
  404. }
  405. }
  406. // 通道取反
  407. for(i=0; i<g_equ_adc_inv_num; i++)
  408. {
  409. g_adc_dots[g_equ_adc_inv[i]][index] = -(g_adc_dots[g_equ_adc_inv[i]][index]);
  410. }
  411. // 全部BUFFER都是新数据,才能算同步好。
  412. if(g_adc_sync < 1)
  413. {
  414. g_adc_sync ++;
  415. }
  416. // 索引值加一
  417. g_adc_dots_count++;
  418. //保护计数+突变量
  419. {
  420. dTCounter++;
  421. if(dTCounter%8==0)
  422. {
  423. prt_flag=true;
  424. }
  425. //突变量
  426. if(g_adc_sync == 1 && g_sw_init == 1)
  427. {
  428. protect_tbl_qd();
  429. }
  430. }
  431. }
  432. g_adc_dots_data_count++;
  433. if(++adc_rp>=SHM_ADC_WR_NUM)
  434. adc_rp=0;
  435. }
  436. return 0;
  437. }
  438. static void updata_adc(struct t_shmdata_adc *sample_data0, uint16_t us_adc_dot_index0)
  439. {
  440. int i = 0;
  441. int index = 0;
  442. static int mod = 0;
  443. int opt = ((g_adc_dots_data_count & (ADC_REC_SAMPLE_RATE - 1)) == 0) ? TRUE : FALSE;
  444. // 每片16路
  445. for (uint8_t j = 0; j < CFG_ADC_CHANNEL; j++)
  446. {
  447. if (AD7616_DC0_CHANNEL == j)
  448. {
  449. g_adc_dots_dc[0][g_adc_dots_data_count & CFG_ADC_DOTS_MASK] = sample_data0->data.usa_sample_dots[AD7616_DC0_CHANNEL][us_adc_dot_index0];
  450. }
  451. else if(AD7616_DC1_CHANNEL == j)
  452. {
  453. g_adc_dots_dc[1][g_adc_dots_data_count & CFG_ADC_DOTS_MASK] = sample_data0->data.usa_sample_dots[AD7616_DC0_CHANNEL][us_adc_dot_index0];
  454. }
  455. else
  456. {
  457. g_adc_dots_rec[g_adc_dots_index_rec][j] = sample_data0->data.usa_sample_dots[j][us_adc_dot_index0];
  458. if (opt == TRUE)
  459. {
  460. g_adc_dots[j][g_adc_dots_index] = sample_data0->data.usa_sample_dots[j][us_adc_dot_index0];
  461. }
  462. }
  463. }
  464. if (opt == TRUE)
  465. {
  466. index = g_adc_dots_index;
  467. // 统计数据
  468. if (g_adc_stat_flag)
  469. {
  470. for (i = 0; i < CFG_ADC_CHANNEL; i++)
  471. {
  472. rt_stat_in(&g_stat_adc[i], (unsigned short)(g_adc_dots[i][index] + 32768));
  473. }
  474. }
  475. // 通道取反
  476. for (i = 0; i < g_equ_adc_inv_num; i++)
  477. {
  478. g_adc_dots[g_equ_adc_inv[i]][index] = -(g_adc_dots[g_equ_adc_inv[i]][index]);
  479. }
  480. // 全部BUFFER都是新数据,才能算同步好。
  481. if (g_adc_sync < 1)
  482. {
  483. g_adc_sync++;
  484. }
  485. // 索引值加一
  486. g_adc_dots_count++;
  487. // 保护计数+突变量
  488. {
  489. dTCounter++;
  490. if (dTCounter % 8 == 0)
  491. {
  492. prt_flag = true;
  493. }
  494. // 突变量
  495. if (g_adc_sync == 1 && g_sw_init == 1)
  496. {
  497. // protect_tbl_qd();
  498. }
  499. }
  500. }
  501. #ifdef XDL_ZT
  502. xdl_tbl_qd(mod);
  503. #endif
  504. mod++;
  505. mod &=(CFG_ADC_MOD_NUM-1);
  506. g_adc_dots_data_count++;
  507. }
  508. /**
  509. * @brief 新方式获取共享内存数据
  510. *
  511. * @param mod
  512. * @return int
  513. * @author zhl
  514. */
  515. int adc_isr_new(int mod)
  516. {
  517. int ret0 = 0;
  518. struct t_shmdata_adc sample_data0 = {0};
  519. static volatile uint16_t smus_sample_read_index0 = 0; /* 读取adc1索引 */
  520. ret0 = shm_packet_read_v2(SHM_ADDR_W_U_ADC_1, sizeof(sample_data0), (uint8_t *)&sample_data0, sizeof(sample_data0));
  521. // printf("ret0=%d, sample_idx=%d, read_index=%d, us_updata=%d, us_op=%d, us_op_bk=%d\n",
  522. // ret0, sample_data0.data.us_sample_idx, smus_sample_read_index0, sample_data0.us_updata, sample_data0.us_op, sample_data0.us_op_bk);
  523. if (0 > ret0)
  524. {
  525. return -1;
  526. }
  527. if (ret0 > 0 && (smus_sample_read_index0 != sample_data0.data.us_sample_idx))
  528. {
  529. uint16_t us_adc_dot_num0 = 0;
  530. if (sample_data0.data.us_sample_idx > smus_sample_read_index0)
  531. {
  532. us_adc_dot_num0 = sample_data0.data.us_sample_idx - smus_sample_read_index0;
  533. }
  534. else
  535. {
  536. us_adc_dot_num0 = (sample_data0.data.us_sample_idx + TC_ADC_DOTS_PER_CHANNEL) - smus_sample_read_index0;
  537. }
  538. // printf("us_adc_dot_num0:%d\n", us_adc_dot_num0);
  539. // us_adc_dot_num0 与 us_adc_dot_num1的数量一般相同
  540. for (uint16_t i = 0; i < us_adc_dot_num0; i++)
  541. {
  542. /* 注意采样下标索引不能超过采样缓存 TC_ADC_DOTS_PER_CHANNEL */
  543. uint16_t us_adc_dot_index0 = (smus_sample_read_index0 + i) % TC_ADC_DOTS_PER_CHANNEL;
  544. // test zhl
  545. // int32_t physical_value0 = (int16_t)(sample_data0.data.usa_sample_dots[9][us_adc_dot_index0]);
  546. // printf("%d, ", physical_value0);
  547. // int32_t physical_value1 = (int16_t)(sample_data1.data.usa_sample_dots[1][us_adc_dot_index1]);
  548. // printf("%d, ", physical_value1);
  549. updata_adc(&sample_data0, us_adc_dot_index0);
  550. }
  551. smus_sample_read_index0 = (smus_sample_read_index0 + us_adc_dot_num0) % TC_ADC_DOTS_PER_CHANNEL;
  552. }
  553. return 0;
  554. }
  555. /******************************************************************************
  556. 函数名称: adc_slot_is_ok
  557. 函数版本: 01.01
  558. 创建作者: sunxi
  559. 创建日期: 2016-02-01
  560. 函数说明: 检查对应槽的ADC芯片是否安装或损坏。
  561. 参数说明:
  562. slot: 0~3;从g_ac_slot_begin开始
  563. 返回值: 成功返回0.
  564. 修改记录:
  565. */
  566. int adc_slot_is_ok(int slot)
  567. {
  568. if((u32)slot >= EQU_SLOT_AC_NUM)
  569. {
  570. return 0;
  571. }
  572. if(g_adc_chip & (1<<g_adc_slot2cs[slot]))
  573. {
  574. return 1;
  575. }
  576. return 0;
  577. }
  578. /******************************************************************************
  579. 函数名称: adc_fourier_address
  580. 函数版本: 01.01
  581. 创建作者: sunxi
  582. 创建日期: 2008-06-26
  583. 函数说明: 返回指定的通道进行傅立叶计算的首地址。首地址保证4bytes对齐。
  584. 参数说明:
  585. channel:ADC的通道,取值0~7。
  586. index: 要进行傅立叶计算的32点中的最后一个点的索引。这么设置是为了和
  587. g_adc_dots_index匹配。保证g_adc_dots_index可以直接作为参数传进来。
  588. 返回值: 成功返回0.
  589. 修改记录:
  590. */
  591. #if 0
  592. short * adc_fourier_address(int channel,int index)
  593. {
  594. short *p;
  595. //{index + CFG_ADC_DOTS_PER_CHANNEL}是为了确保索引在相应的范围之内.
  596. index = index + CFG_ADC_DOTS_PER_CHANNEL - CFG_ADC_DOTS_PER_PERIOD;
  597. //使用偶数索引,保证4bytes对齐
  598. index &= (~0x1);
  599. //得到首地址,注意地址必须和CFG_ADC_BUF_MASK相与。
  600. p = (short *)&g_adc_dots[channel][index];
  601. p = (short *)((unsigned int)p&CFG_ADC_BUF_MASK);
  602. return p;
  603. }
  604. #else
  605. short sam_buf[CFG_ADC_CHANNEL][CFG_ADC_DOTS_PER_PERIOD];
  606. short * adc_fourier_address(int channel,int index)
  607. {
  608. short *p;
  609. int i;
  610. if(index >= CFG_ADC_DOTS_PER_PERIOD)
  611. {
  612. index = index - CFG_ADC_DOTS_PER_PERIOD;
  613. index &= (~0x1);
  614. p = (short *)&g_adc_dots[channel][index];
  615. }
  616. else
  617. {
  618. index = CFG_ADC_DOTS_PER_CHANNEL-CFG_ADC_DOTS_PER_PERIOD+index;
  619. index &= (~0x1);
  620. for(i = 0;i < CFG_ADC_DOTS_PER_CHANNEL - index;i++)
  621. {
  622. sam_buf[channel][i] = g_adc_dots[channel][index+i];
  623. }
  624. for(i = 0;i <CFG_ADC_DOTS_PER_PERIOD +index-CFG_ADC_DOTS_PER_CHANNEL;i++)
  625. {
  626. sam_buf[channel][i+CFG_ADC_DOTS_PER_CHANNEL - index] = g_adc_dots[channel][i];
  627. }
  628. p = sam_buf[channel];
  629. }
  630. return p;
  631. }
  632. #endif
  633. /******************************************************************************
  634. 函数名称: adc_fourier_address_half
  635. 函数版本: 01.01
  636. 创建作者: sunxi
  637. 创建日期: 2008-06-26
  638. 函数说明: 返回指定的通道进行傅立叶计算的首地址。首地址保证4bytes对齐。
  639. 参数说明:
  640. channel:ADC的通道,取值0~7。
  641. index: 要进行傅立叶计算的32点中的最后一个点的索引。这么设置是为了和
  642. g_adc_dots_index匹配。保证g_adc_dots_index可以直接作为参数传进来。
  643. 返回值: 成功返回0.
  644. 修改记录:
  645. */
  646. short * adc_fourier_address_half(int channel,int index)
  647. {
  648. short *p;
  649. //{index - (CFG_ADC_DOTS_PER_PERIOD - 1)}是因为索引下有实际的采样值,所以不是
  650. // index - CFG_ADC_DOTS_PER_PERIOD.
  651. //{index + CFG_ADC_DOTS_PER_CHANNEL}是为了确保索引在相应的范围之内.
  652. index = index + CFG_ADC_DOTS_PER_CHANNEL - (CFG_ADC_DOTS_PER_PERIOD/2 - 1);
  653. //使用偶数索引,保证4bytes对齐
  654. index &= (~0x1);
  655. //得到首地址,注意地址必须和CFG_ADC_BUF_MASK相与。
  656. p = (short *)&g_adc_dots[channel][index];
  657. p = (short *)((unsigned int)p&CFG_ADC_BUF_MASK);
  658. return p;
  659. }
  660. int adc_stat_printf(void)
  661. {
  662. int i;
  663. unsigned long avg = 0;
  664. struct rt_stat *stat;
  665. if(g_adc_stat_flag == 0)
  666. {
  667. g_adc_stat_flag = 1;
  668. rt_printf("\r\n!!!ADC采样统计启动, 执行reset命令关闭。!!!\r\n");
  669. }
  670. rt_printf("\r\n频率跟踪:%fHZ.\r\n",g_freq);
  671. rt_printf("g_adc_chip:0x%02x.\r\n",g_adc_chip);
  672. for(i=0;i<EQU_SLOT_AC_NUM;i++)
  673. {
  674. rt_printf("槽[%02lu]: 是否有对应芯片:%d,输入范围:0x%08lx.\r\n",g_ac_slot_begin+i,adc_slot_is_ok(i),g_adc_range[g_adc_slot2cs[i]]);
  675. }
  676. rt_printf("\r\n[ADC中断周期统计]\r\n");
  677. rt_printf("name\t\t\tmin\tmax\tavg\tsum\t\tcnt\n");
  678. // rt_stat_printf(&g_stat_adc_time);
  679. rt_printf("\r\n[ADC抖动统计]\r\n");
  680. rt_printf("name\t\t\tmin\tmax\tdif\tavg\tsum\t\tcnt\n");
  681. rt_printf("\r\n");
  682. for(i=0; i<CFG_ADC_CHANNEL_ALL; i++)
  683. {
  684. stat = &g_stat_adc[i];
  685. if(stat->cnt)
  686. {
  687. avg = stat->sum/stat->cnt;
  688. }
  689. rt_printf("%-24s%ld\t%ld\t%ld\t%ld\t%-16ld%ld\r\n",stat->name,stat->min,stat->max,stat->max - stat->min,avg,stat->sum,stat->cnt);
  690. }
  691. return 0;
  692. }
  693. int adc_stat_reset(void)
  694. {
  695. int i;
  696. g_adc_stat_flag = 0;
  697. for(i=0; i<CFG_ADC_CHANNEL_ALL; i++)
  698. {
  699. rt_stat_init(&g_stat_adc[i],g_stat_adc[i].name);
  700. }
  701. // rt_stat_init(&g_stat_adc_time,g_stat_adc_time.name);
  702. return 0;
  703. }
  704. #if 0
  705. static int get_adc_dc(uint8_t *out_data)
  706. {
  707. int ret = 0;
  708. int cnt = 0;
  709. if(out_data == NULL)
  710. return -1;
  711. while(1)
  712. {
  713. ret = shm_comm_packet_read(SHM_ADDR_U_DC, sizeof(SHM_DC_T), out_data, sizeof(SHM_DC_T));
  714. if(ret > 0)
  715. {
  716. break;
  717. }
  718. if(++cnt > 3)
  719. break;
  720. usleep(30);
  721. }
  722. return ret;
  723. }
  724. #endif
  725. float dc_get(unsigned int index)
  726. {
  727. u32 i,v;
  728. float f;
  729. v = 0;
  730. for(i=0;i<CFG_ADC_DOTS_PER_CHANNEL;i++)
  731. {
  732. v += (u16)g_adc_dots_dc[index][i];
  733. }
  734. f = (float)v/CFG_ADC_DOTS_PER_CHANNEL;
  735. // 乘理论系数
  736. f /= 129.791f;
  737. return f;
  738. }
  739. /*------------------------------ 测试函数 -------------------------------------
  740. */
  741. #ifdef _ADC_DEBUG
  742. int adc_test(void)
  743. {
  744. return 0;
  745. }
  746. #endif
  747. /*------------------------------ 文件结束 -------------------------------------
  748. */