phase_current.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. #include <math.h>
  2. #include "bsp/adc.h"
  3. #include "phase_current.h"
  4. #include "libs/utils.h"
  5. #include "libs/logger.h"
  6. #define NB_OFFSET_SAMPLES 32
  7. #define Rvbus 0.0005f
  8. #define Gvbus (13.1f) //母线电流的运放
  9. #define Rds_Defualt 0.0025f//欧
  10. #define Gmos (1.7f)//mos 电流的运放
  11. #define Sample_R Rvbus
  12. #define Lower_Pass_p 0.2f
  13. #define VBUS_VOL(adc) (((float)(adc)) * 3.3f / 4096.0f / Gvbus)
  14. #define MOSds_VOL(adc) (((float)(adc)) * 3.3f / 4096.0f / Gmos)
  15. #define current_i(v, r) ((v)/(r))
  16. void phase_current_init(current_samp_t *cs) {
  17. cs->offset_sample_count = NB_OFFSET_SAMPLES;
  18. }
  19. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  20. void phase_current_offset(current_samp_t *cs) {
  21. s32 phase_current1, phase_current2;
  22. adc_phase_current_read(cs->sector, &phase_current1, &phase_current2);
  23. if (cs->offset_sample_count > 0) {
  24. cs->offset_sample_count--;
  25. if (cs->sector == SECTOR_5 && cs->offset_sample_count >= 0) {
  26. cs->adc_offset_a += phase_current1;
  27. cs->adc_offset_c += phase_current2;
  28. if (cs->offset_sample_count == 0) {
  29. cs->adc_offset_a = cs->adc_offset_a / NB_OFFSET_SAMPLES;
  30. cs->adc_offset_c = cs->adc_offset_c / NB_OFFSET_SAMPLES;
  31. }
  32. }
  33. if (cs->sector == SECTOR_1 && cs->offset_sample_count >= 0) {
  34. cs->adc_offset_b += phase_current1;
  35. if (cs->offset_sample_count == 0) {
  36. cs->adc_offset_b = cs->adc_offset_b / NB_OFFSET_SAMPLES;
  37. }
  38. }
  39. }
  40. }
  41. void phase_current_sample(current_samp_t *cs){
  42. s32 phase_current1, phase_current2;
  43. adc_phase_current_read(cs->sector, &phase_current1, &phase_current2);
  44. if (cs->sector == SECTOR_4 || cs->sector == SECTOR_5) {
  45. /* Current on Phase C is not accessible */
  46. /* Ia = PhaseAOffset - ADC converted value) */
  47. cs->Ib = current_i(MOSds_VOL(phase_current1 - cs->adc_offset_b), Rds_Defualt);
  48. cs->Ia = current_i(MOSds_VOL(phase_current2 - cs->adc_offset_a), Rds_Defualt);
  49. cs->Ic = -(cs->Ia + cs->Ib);
  50. }else if (cs->sector == SECTOR_1 || cs->sector == SECTOR_6) {
  51. /* Current on Phase A is not accessible */
  52. /* Ib = PhaseBOffset - ADC converted value) */
  53. cs->Ib = current_i(MOSds_VOL(phase_current1 - cs->adc_offset_b), Rds_Defualt);
  54. cs->Ic = current_i(MOSds_VOL(phase_current2 - cs->adc_offset_c), Rds_Defualt);
  55. cs->Ia = -(cs->Ib + cs->Ic);
  56. }else if (cs->sector == SECTOR_2 || cs->sector == SECTOR_3) {
  57. /* Current on Phase B is not accessible */
  58. /* Ia = PhaseAOffset - ADC converted value) */
  59. cs->Ia = current_i(MOSds_VOL(phase_current1 - cs->adc_offset_a), Rds_Defualt);
  60. cs->Ic = current_i(MOSds_VOL(phase_current2 - cs->adc_offset_c), Rds_Defualt);
  61. cs->Ib = -(cs->Ia + cs->Ic);
  62. }
  63. {
  64. static int count = 0;
  65. if (count++ % 3 == 0) {
  66. log_chan_value(1, (int)(cs->Ia * 1000));
  67. }
  68. }
  69. }
  70. void get_phase_sample_point(current_samp_t *cs, u8 sector){
  71. phase_time_t *time = &cs->time;
  72. u32 low_side_low_duty = FOC_PWM_Half_Period - time->low;
  73. #if 0
  74. u32 low_side_mid_duty = FOC_PWM_Half_Period - time->midle;
  75. cs->sector = sector;
  76. time->Samp_p1 = FOC_PWM_Half_Period + 1;
  77. time->Samp_p2 = FOC_PWM_Half_Period + 1;
  78. /*底边开mos的时间是2倍的 low_side_low_duty(一个周期)*/
  79. if (low_side_low_duty * 2 >= TSampleMIN) { //可以采样
  80. if (low_side_low_duty >= (TADC + TDead)) {//可以在pwm的中心点采样
  81. time->Samp_p1 = FOC_PWM_Half_Period - 1;
  82. }else {
  83. u32 Samp_p = time->low + TSampleBefore;
  84. if (Samp_p >= FOC_PWM_Half_Period) { //需要在pwm中心点过后采样,需要配置PWM0模式
  85. time->Samp_p2 = ( 2u * FOC_PWM_Half_Period ) - Samp_p - (uint16_t) 1;
  86. }else {
  87. time->Samp_p1 = Samp_p;
  88. }
  89. }
  90. }else if (low_side_mid_duty * 2 >= TSampleMIN){
  91. if (low_side_mid_duty >= (TADC + TDead)) {//可以在pwm的中心点采样
  92. time->Samp_p1 = FOC_PWM_Half_Period - 1;
  93. }else {
  94. u32 Samp_p = time->midle + TSampleBefore;
  95. if (Samp_p >= FOC_PWM_Half_Period) { //需要在pwm中心点过后采样,需要配置PWM0模式
  96. time->Samp_p2 = ( 2u * FOC_PWM_Half_Period ) - Samp_p - (uint16_t) 1;
  97. }else {
  98. time->Samp_p1 = Samp_p;
  99. }
  100. }
  101. }else {
  102. //not reached there
  103. sys_debug("ERROR!!!!!\n");
  104. }
  105. #else
  106. if (low_side_low_duty > (TDead + MAX(TRise, TNoise))) {
  107. cs->sector = SECTOR_1;
  108. time->Samp_p1 = time->low + MAX(TRise, TNoise);
  109. }else {
  110. u32 low_side_mid_duty = FOC_PWM_Half_Period - time->midle;
  111. u32 delta_duty = low_side_mid_duty - low_side_low_duty;
  112. if (delta_duty > low_side_low_duty * 2) {
  113. time->Samp_p1 = time->low - TADC;
  114. }else {
  115. u32 sample_point = time->low + (TDead + MAX(TRise, TNoise));
  116. if (sample_point >= FOC_PWM_Half_Period) {
  117. //这里需要修改触发方式,GD不支持adc设置上升或下降沿触发,考虑切换pwm模式???
  118. sample_point = ( 2u * FOC_PWM_Half_Period ) - sample_point - (uint16_t) 1;
  119. }
  120. time->Samp_p1 = sample_point;
  121. }
  122. }
  123. #endif
  124. }
  125. #else
  126. #define TBEFOR (TDead + MAX(TRise, TNoise))
  127. #define TMIN (TDead + MAX(TRise, TNoise) + TADC)
  128. static __inline u8 _get_sample_boundary(current_samp_t *cs, phase_time_t *time) {
  129. #if 0
  130. s32 delta_duty0 = (s32)time->midle - (s32)time->high;
  131. s32 delta_duty1 = (s32)time->low - (s32)time->midle;
  132. if (delta_duty0 <= TMIN && delta_duty1 <= TMIN) {
  133. return BOUNDARY_3;
  134. }else if (delta_duty0 <= TMIN && delta_duty1 > TMIN) {
  135. return BOUNDARY_2;
  136. }else if (delta_duty0 > TMIN && delta_duty1 <= TMIN) {
  137. return BOUNDARY_1;
  138. }else {
  139. return REGULAR;
  140. }
  141. #else
  142. return REGULAR;
  143. #endif
  144. }
  145. static __inline void _get_boundary1_samp(current_samp_t *cs, phase_time_t *time) {
  146. s32 delta_duty1 = (s32)time->low - (s32)time->midle;
  147. s32 delta_time_inc = (TMIN - delta_duty1);
  148. s32 delta_time_dec = min(delta_time_inc, delta_duty1);
  149. s32 sample_p;
  150. switch(cs->sector) {
  151. case SECTOR_1: //AB big and delta small
  152. time->Samp_p1 = time->B - TADC;
  153. sample_p = time->A;
  154. time->A = sample_p + delta_time_inc;
  155. time->A_next = sample_p - delta_time_dec;
  156. time->Samp_p2 = time->A - TADC;
  157. time->sampe_phase_1 = SAMP_NIC;
  158. time->sampe_phase_2 = SAMP_IA;
  159. break;
  160. case SECTOR_2://BA big and delta small
  161. time->Samp_p1 = time->A - TADC;
  162. sample_p = time->B;
  163. time->B = sample_p + delta_time_inc;
  164. time->B_next = sample_p - delta_time_dec;
  165. time->Samp_p2 = time->B - TADC;
  166. time->sampe_phase_1 = SAMP_NIC;
  167. time->sampe_phase_2 = SAMP_IB;
  168. break;
  169. case SECTOR_3://BC big and delta small
  170. time->Samp_p1 = time->C - TADC;
  171. sample_p = time->B;
  172. time->B = sample_p + delta_time_inc;
  173. time->B_next = sample_p - delta_time_dec;
  174. time->Samp_p2 = time->B - TADC;
  175. time->sampe_phase_1 = SAMP_NIA;
  176. time->sampe_phase_2 = SAMP_IB;
  177. break;
  178. case SECTOR_4://CB big and delta small
  179. time->Samp_p1 = time->B - TADC;
  180. sample_p = time->C;
  181. time->C = sample_p + delta_time_inc;
  182. time->C_next = sample_p - delta_time_dec;
  183. time->Samp_p2 = time->C - TADC;
  184. time->sampe_phase_1 = SAMP_NIA;
  185. time->sampe_phase_2 = SAMP_IC;
  186. break;
  187. case SECTOR_5://CA big and delta small
  188. time->Samp_p1 = time->A - TADC;
  189. sample_p = time->C;
  190. time->C = sample_p + delta_time_inc;
  191. time->C_next = sample_p - delta_time_dec;
  192. time->Samp_p2 = time->C - TADC;
  193. time->sampe_phase_1 = SAMP_NIB;
  194. time->sampe_phase_2 = SAMP_IC;
  195. break;
  196. case SECTOR_6://AC big and delta small
  197. time->Samp_p1 = time->C - TADC;
  198. sample_p = time->A;
  199. time->A = sample_p + delta_time_inc;
  200. time->A_next = sample_p - delta_time_dec;
  201. time->Samp_p2 = time->A - TADC;
  202. time->sampe_phase_1 = SAMP_NIB;
  203. time->sampe_phase_2 = SAMP_IA;
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209. static __inline void _get_boundary2_samp(current_samp_t *cs, phase_time_t *time) {
  210. s32 delta_duty0 = (s32)time->midle - (s32)time->high;
  211. s32 delta_time_dec = (TMIN - delta_duty0);
  212. s32 delta_time_inc = min(delta_time_dec, delta_duty0);
  213. s32 sample_p;
  214. switch(cs->sector) {
  215. case SECTOR_1: //BC samll and delta small
  216. time->Samp_p2 = time->A - TADC;
  217. sample_p = time->C;
  218. time->C = sample_p - delta_time_dec;
  219. time->C_next = sample_p + delta_time_inc;
  220. time->Samp_p1 = time->B - TADC;
  221. time->sampe_phase_2 = SAMP_IA;
  222. time->sampe_phase_1 = SAMP_NIC;
  223. break;
  224. case SECTOR_2://AC samll and delta small
  225. time->Samp_p2 = time->B - TADC;
  226. sample_p = time->C;
  227. time->C = sample_p - delta_time_dec;
  228. time->C_next = sample_p + delta_time_inc;
  229. time->Samp_p1 = time->A - TADC;
  230. time->sampe_phase_2 = SAMP_IB;
  231. time->sampe_phase_1 = SAMP_NIC;
  232. break;
  233. case SECTOR_3://CA samll and delta small
  234. time->Samp_p2 = time->B - TADC;
  235. sample_p = time->A;
  236. time->A = sample_p - delta_time_dec;
  237. time->A_next = sample_p + delta_time_inc;
  238. time->Samp_p1 = time->C - TADC;
  239. time->sampe_phase_2 = SAMP_IB;
  240. time->sampe_phase_1 = SAMP_NIA;
  241. break;
  242. case SECTOR_4://BA samll and delta small
  243. time->Samp_p2 = time->C - TADC;
  244. sample_p = time->A;
  245. time->A = sample_p - delta_time_dec;
  246. time->A_next = sample_p + delta_time_inc;
  247. time->Samp_p1 = time->B - TADC;
  248. time->sampe_phase_2 = SAMP_IC;
  249. time->sampe_phase_1 = SAMP_NIA;
  250. break;
  251. case SECTOR_5://AB samll and delta small
  252. time->Samp_p2 = time->C - TADC;
  253. sample_p = time->B;
  254. time->B = sample_p - delta_time_dec;
  255. time->B_next = sample_p + delta_time_inc;
  256. time->Samp_p1 = time->A - TADC;
  257. time->sampe_phase_2 = SAMP_IC;
  258. time->sampe_phase_1 = SAMP_NIB;
  259. break;
  260. case SECTOR_6://CB samll and delta small
  261. time->Samp_p2 = time->A - TADC;
  262. sample_p = time->B;
  263. time->B = sample_p - delta_time_dec;
  264. time->B_next = sample_p + delta_time_inc;
  265. time->Samp_p1 = time->C - TADC;
  266. time->sampe_phase_2 = SAMP_IA;
  267. time->sampe_phase_1 = SAMP_NIB;
  268. break;
  269. default:
  270. break;
  271. }
  272. }
  273. static __inline void _get_boundary3_samp(current_samp_t *cs, phase_time_t *time) {
  274. #if 1
  275. s32 sample_p;
  276. if ((time->boundary3_flags & 1) == 0) {
  277. time->boundary3_flags |= 1;
  278. sample_p = time->A;
  279. time->A = sample_p + TMIN;
  280. time->A_next = sample_p - TMIN;
  281. time->Samp_p1 = time->A - (2 * TADC + TRise);//dumy trigger
  282. time->Samp_p2 = time->A - TADC;
  283. time->sampe_phase_1 = SAMP_OLDB;
  284. time->sampe_phase_2 = SAMP_IA;
  285. }else {
  286. time->boundary3_flags &= ~1;
  287. sample_p = time->B;
  288. time->B = sample_p + TMIN;
  289. time->B_next = sample_p - TMIN;
  290. time->Samp_p1 = time->B - (2 * TADC + TRise);//dumy trigger
  291. time->Samp_p2 = time->B - TADC;
  292. time->sampe_phase_1 = SAMP_OLDA;
  293. time->sampe_phase_2 = SAMP_IB;
  294. }
  295. #else
  296. s32 delta_duty0 = (s32)time->midle - (s32)time->high;
  297. s32 delta_duty1 = (s32)time->low - (s32)time->midle;
  298. s32 delta_time_inc1 = (TMIN - delta_duty1);
  299. s32 delta_time_dec1 = min(delta_time_inc1, delta_duty1);
  300. s32 delta_time_dec2 = (TMIN - delta_duty0);
  301. s32 delta_time_inc2 = min(delta_time_dec2, delta_duty0);
  302. s32 sample_p;
  303. switch(cs->sector) {
  304. case SECTOR_1: //deltaBC > deltaAB
  305. if (delta_duty0 > delta_duty1) {
  306. sample_p = time->C;
  307. time->C = sample_p - delta_time_dec2;
  308. time->C_next = sample_p + delta_time_inc2;
  309. time->Samp_p1 = time->B - (2 * TADC + TRise);//dumy trigger
  310. time->Samp_p2 = time->B - TADC;
  311. time->sampe_phase_1 = SAMP_OLDB;
  312. time->sampe_phase_2 = SAMP_NIC;
  313. }else {
  314. sample_p = time->A;
  315. time->A = sample_p + delta_time_inc1;
  316. time->A_next = sample_p - delta_time_dec1;
  317. time->Samp_p1 = time->A - (2 * TADC + TRise);//dumy trigger
  318. time->Samp_p2 = time->A - TADC;
  319. time->sampe_phase_1 = SAMP_OLDC;
  320. time->sampe_phase_2 = SAMP_IA;
  321. }
  322. break;
  323. case SECTOR_2: //deltaAC > deltaBA
  324. if (delta_duty0 > delta_duty1) {
  325. sample_p = time->C;
  326. time->C = sample_p - delta_time_dec2;
  327. time->C_next = sample_p + delta_time_inc2;
  328. time->Samp_p1 = time->A - (2 * TADC + TRise);//dumy trigger
  329. time->Samp_p2 = time->A - TADC;
  330. time->sampe_phase_1 = SAMP_OLDA;
  331. time->sampe_phase_2 = SAMP_NIC;
  332. }else {
  333. sample_p = time->B;
  334. time->B = sample_p + delta_time_inc1;
  335. time->B_next = sample_p - delta_time_dec1;
  336. time->Samp_p1 = time->B - (2 * TADC + TRise);//dumy trigger
  337. time->Samp_p2 = time->B - TADC;
  338. time->sampe_phase_1 = SAMP_OLDB;
  339. time->sampe_phase_2 = SAMP_IB;
  340. }
  341. break;
  342. case SECTOR_3: //deltaCA > deltaBC
  343. if (delta_duty0 > delta_duty1) {
  344. sample_p = time->A;
  345. time->A = sample_p - delta_time_dec2;
  346. time->A_next = sample_p + delta_time_inc2;
  347. time->Samp_p1 = time->C - (2 * TADC + TRise);//dumy trigger
  348. time->Samp_p2 = time->C - TADC;
  349. time->sampe_phase_1 = SAMP_OLDC;
  350. time->sampe_phase_2 = SAMP_NIA;
  351. }else {
  352. sample_p = time->B;
  353. time->B = sample_p + delta_time_inc1;
  354. time->B_next = sample_p - delta_time_dec1;
  355. time->Samp_p1 = time->B - (2 * TADC + TRise);//dumy trigger
  356. time->Samp_p2 = time->B - TADC;
  357. time->sampe_phase_1 = SAMP_OLDA;
  358. time->sampe_phase_2 = SAMP_IB;
  359. }
  360. break;
  361. case SECTOR_4: //CBA, //deltaBA > deltaCB
  362. if (delta_duty0 > delta_duty1) {
  363. sample_p = time->A;
  364. time->A = sample_p - delta_time_dec2;
  365. time->A_next = sample_p + delta_time_inc2;
  366. time->Samp_p1 = time->B - (2 * TADC + TRise);//dumy trigger
  367. time->Samp_p2 = time->B - TADC;
  368. time->sampe_phase_1 = SAMP_OLDB;
  369. time->sampe_phase_2 = SAMP_NIA;
  370. }else {
  371. sample_p = time->C;
  372. time->C = sample_p + delta_time_inc1;
  373. time->C_next = sample_p - delta_time_dec1;
  374. time->Samp_p1 = time->C - (2 * TADC + TRise);//dumy trigger
  375. time->Samp_p2 = time->C - TADC;
  376. time->sampe_phase_1 = SAMP_OLDC;
  377. time->sampe_phase_2 = SAMP_IC;
  378. }
  379. break;
  380. case SECTOR_5: //CAB, //deltaAB > deltaCA
  381. if (delta_duty0 > delta_duty1) {
  382. sample_p = time->B;
  383. time->B = sample_p - delta_time_dec2;
  384. time->B_next = sample_p + delta_time_inc2;
  385. time->Samp_p1 = time->A - (2 * TADC + TRise);//dumy trigger
  386. time->Samp_p2 = time->A - TADC;
  387. time->sampe_phase_1 = SAMP_OLDA;
  388. time->sampe_phase_2 = SAMP_NIB;
  389. }else {
  390. sample_p = time->C;
  391. time->C = sample_p + delta_time_inc1;
  392. time->C_next = sample_p - delta_time_dec1;
  393. time->Samp_p1 = time->C - (2 * TADC + TRise);//dumy trigger
  394. time->Samp_p2 = time->C - TADC;
  395. time->sampe_phase_1 = SAMP_OLDB;
  396. time->sampe_phase_2 = SAMP_IC;
  397. }
  398. break;
  399. case SECTOR_6: //ACB, //deltaCB > deltaAC
  400. if (delta_duty0 > delta_duty1) {
  401. sample_p = time->B;
  402. time->B = sample_p - delta_time_dec2;
  403. time->B_next = sample_p + delta_time_inc2;
  404. time->Samp_p1 = time->C - (2 * TADC + TRise);//dumy trigger
  405. time->Samp_p2 = time->C - TADC;
  406. time->sampe_phase_1 = SAMP_OLDC;
  407. time->sampe_phase_2 = SAMP_NIB;
  408. }else {
  409. sample_p = time->A;
  410. time->A = sample_p + delta_time_inc1;
  411. time->A_next = sample_p - delta_time_dec1;
  412. time->Samp_p1 = time->A - (2 * TADC + TRise);//dumy trigger
  413. time->Samp_p2 = time->A - TADC;
  414. time->sampe_phase_1 = SAMP_OLDA;
  415. time->sampe_phase_2 = SAMP_IA;
  416. }
  417. break;
  418. default:
  419. break;
  420. }
  421. #endif
  422. }
  423. static __inline void _get_regular_samp(current_samp_t *cs, phase_time_t *time) {
  424. time->Samp_p1 = time->midle - TADC;
  425. time->Samp_p2 = time->low - TADC;
  426. switch(cs->sector) {
  427. case SECTOR_1: //ABC
  428. time->sampe_phase_1 = SAMP_NIC;
  429. time->sampe_phase_2 = SAMP_IA;
  430. break;
  431. case SECTOR_2: //BAC
  432. time->sampe_phase_1 = SAMP_NIC;
  433. time->sampe_phase_2 = SAMP_IB;
  434. break;
  435. case SECTOR_3: //BCA
  436. time->sampe_phase_1 = SAMP_NIA;
  437. time->sampe_phase_2 = SAMP_IB;
  438. break;
  439. case SECTOR_4: //CBA
  440. time->sampe_phase_1 = SAMP_NIA;
  441. time->sampe_phase_2 = SAMP_IC;
  442. break;
  443. case SECTOR_5: //CAB
  444. time->sampe_phase_1 = SAMP_NIB;
  445. time->sampe_phase_2 = SAMP_IC;
  446. break;
  447. case SECTOR_6: //ACB
  448. time->sampe_phase_1 = SAMP_NIB;
  449. time->sampe_phase_2 = SAMP_IA;
  450. break;
  451. default:
  452. break;
  453. }
  454. }
  455. void phase_current_offset(current_samp_t *cs) {
  456. s32 phase_current1, phase_current2;
  457. adc_phase_current_read(cs->sector, &phase_current1, &phase_current2);
  458. if (cs->offset_sample_count > 0) {
  459. cs->offset_sample_count--;
  460. if (cs->offset_sample_count >= 0) {
  461. cs->adc_offset_a += phase_current1;
  462. cs->adc_offset_b += phase_current2;
  463. if (cs->offset_sample_count == 0) {
  464. cs->adc_offset_a = cs->adc_offset_a / NB_OFFSET_SAMPLES;
  465. cs->adc_offset_b = cs->adc_offset_b / NB_OFFSET_SAMPLES;
  466. }
  467. }
  468. }
  469. }
  470. void phase_current_sample(current_samp_t *cs){
  471. s32 phase_current1, phase_current2;
  472. u8 b_curr_a = 0;
  473. u8 b_curr_b = 0;
  474. u8 b_curr_c = 0;
  475. phase_time_t *time = &cs->time;
  476. adc_phase_current_read(cs->sector, &phase_current1, &phase_current2);
  477. phase_current1 -= cs->adc_offset_a;
  478. phase_current2 -= cs->adc_offset_b;
  479. float current = current_i(VBUS_VOL(abs(phase_current1)), Sample_R);
  480. switch (time->sampe_phase_1) {
  481. case SAMP_IA:
  482. cs->Ia = current;
  483. b_curr_a = 1;
  484. break;
  485. case SAMP_IB:
  486. cs->Ib = current;
  487. b_curr_b = 1;
  488. break;
  489. case SAMP_IC:
  490. cs->Ic = current;
  491. b_curr_c = 1;
  492. break;
  493. case SAMP_NIA:
  494. cs->Ia = -current;
  495. b_curr_a = 1;
  496. break;
  497. case SAMP_NIB:
  498. cs->Ib = -current;
  499. b_curr_b = 1;
  500. break;
  501. case SAMP_NIC:
  502. cs->Ic = -current;
  503. b_curr_c = 1;
  504. break;
  505. case SAMP_OLDA:
  506. cs->Ia = cs->old_Ia;
  507. b_curr_a = 1;
  508. break;
  509. case SAMP_OLDB:
  510. cs->Ib = cs->old_Ib;
  511. b_curr_b = 1;
  512. break;
  513. case SAMP_OLDC:
  514. cs->Ic = cs->old_Ic;
  515. b_curr_c = 1;
  516. break;
  517. default:
  518. break;
  519. }
  520. current = current_i(VBUS_VOL(abs(phase_current2)), Sample_R);
  521. switch (time->sampe_phase_2) {
  522. case SAMP_IA:
  523. cs->Ia = current;
  524. b_curr_a = 1;
  525. break;
  526. case SAMP_IB:
  527. cs->Ib = current;
  528. b_curr_b = 1;
  529. break;
  530. case SAMP_IC:
  531. cs->Ic = current;
  532. b_curr_c = 1;
  533. break;
  534. case SAMP_NIA:
  535. cs->Ia = -current;
  536. b_curr_a = 1;
  537. break;
  538. case SAMP_NIB:
  539. cs->Ib = -current;
  540. b_curr_b = 1;
  541. break;
  542. case SAMP_NIC:
  543. cs->Ic = -current;
  544. b_curr_c = 1;
  545. break;
  546. case SAMP_OLDA:
  547. cs->Ia = cs->old_Ia;
  548. b_curr_a = 1;
  549. break;
  550. case SAMP_OLDB:
  551. cs->Ib = cs->old_Ib;
  552. b_curr_b = 1;
  553. break;
  554. case SAMP_OLDC:
  555. cs->Ic = cs->old_Ic;
  556. b_curr_c = 1;
  557. break;
  558. default:
  559. break;
  560. }
  561. if (b_curr_a == 0) {
  562. cs->Ia = -(cs->Ib + cs->Ic);
  563. }
  564. if (b_curr_b == 0) {
  565. cs->Ib = -(cs->Ia + cs->Ic);
  566. }
  567. if (b_curr_c == 0) {
  568. cs->Ic = -(cs->Ia + cs->Ib);
  569. }
  570. cs->old_Ia = cs->Ia;
  571. cs->old_Ib = cs->Ib;
  572. cs->old_Ic = cs->Ic;
  573. {
  574. static int count = 0;
  575. if (count++ % 3 == 0) {
  576. log_chan_value(1, (int)(cs->Ia * 1000));
  577. }
  578. }
  579. }
  580. void get_phase_sample_point(current_samp_t *cs, u8 sector){
  581. phase_time_t *time = &cs->time;
  582. if (cs->is_calibrating_offset) {
  583. time->Samp_p1 = FOC_PWM_Half_Period - 2 * TMIN;
  584. time->Samp_p2 = FOC_PWM_Half_Period - 1;
  585. return;
  586. }
  587. cs->sector = sector;
  588. time->A_next = time->A;
  589. time->B_next = time->B;
  590. time->C_next = time->C;
  591. u8 boundary = _get_sample_boundary(cs, time);
  592. if (boundary == BOUNDARY_1) {
  593. _get_boundary1_samp(cs, time);
  594. }else if (boundary == BOUNDARY_2) {
  595. _get_boundary2_samp(cs, time);
  596. }else if (boundary == BOUNDARY_3) {
  597. _get_boundary3_samp(cs, time);
  598. }else { //REGULAR, 直接可以采样
  599. _get_regular_samp(cs, time);
  600. }
  601. }
  602. #endif
  603. void phase_current_adc_triger(current_samp_t *cs){
  604. //adc_enable_ext_trigger();
  605. }