mc_config.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #include "foc/mc_config.h"
  2. #include "libs/utils.h"
  3. #include "libs/crc16.h"
  4. #include "app/nv_storage.h"
  5. #include "libs/logger.h"
  6. mc_config conf;
  7. void mc_conf_default(void);
  8. static u8 *conf_buff = NULL;
  9. static int conf_len = 0;
  10. static int conf_idx = 0;
  11. bool mc_conf_begin_recv(int len) {
  12. if (conf_buff) {
  13. os_free(conf_buff);
  14. conf_buff = NULL;
  15. }
  16. if (len > sizeof(mc_config)) {
  17. return false;
  18. }
  19. conf_len = len;
  20. conf_idx = 0;
  21. conf_buff = os_alloc(len);
  22. return conf_buff != NULL;
  23. }
  24. bool mc_conf_recv_data(u8 *buff, int offset, int len) {
  25. if (offset != conf_idx || conf_buff == NULL) {
  26. return false;
  27. }
  28. if (offset + len > conf_len) {
  29. return false;
  30. }
  31. memcpy(conf_buff + offset, buff, len);
  32. conf_idx = offset + len;
  33. return true;
  34. }
  35. bool mc_conf_finish_recv(u16 crc) {
  36. if (crc != crc16_get(conf_buff, conf_len)) {
  37. return false;
  38. }
  39. mc_conf_decode_configs();
  40. conf.crc = crc16_get((u8 *)&conf, (u8 *)&conf.crc - (u8 *)&conf);
  41. mc_conf_save();
  42. if (conf_buff) {
  43. os_free(conf_buff);
  44. conf_buff = NULL;
  45. }
  46. return true;
  47. }
  48. static u16 crc16;
  49. bool mc_conf_begin_send(void) {
  50. if (conf_buff) {
  51. os_free(conf_buff);
  52. conf_buff = NULL;
  53. }
  54. conf_idx = 0;
  55. conf_buff = os_alloc(sizeof(mc_config));
  56. if (conf_buff) {
  57. conf_len = mc_conf_encode_configs(conf_buff);
  58. crc16 = crc16_get(conf_buff, conf_len);
  59. }
  60. sys_debug("%x, %d, %d\n", conf_buff, conf_len, crc16);
  61. return conf_buff != NULL;
  62. }
  63. int mc_conf_send_data(u8 *buff, int offset, int len) {
  64. sys_debug("off %d, %d\n", offset, conf_idx);
  65. if (offset == 0 && !mc_conf_begin_send()) {
  66. return -2;
  67. }
  68. if (conf_len == conf_idx) {
  69. return 0;
  70. }
  71. if (offset != conf_idx || conf_buff == NULL) {
  72. return -1;
  73. }
  74. len = min(len, conf_len - conf_idx);
  75. memcpy(buff, conf_buff + offset, len);
  76. conf_idx = (offset + len);
  77. return len;
  78. }
  79. int mc_conf_finish_send(u8 *buff) {
  80. if (conf_buff) {
  81. os_free(conf_buff);
  82. conf_buff = NULL;
  83. }
  84. encode_u16(buff, crc16);
  85. return sizeof(u16);
  86. }
  87. void mc_conf_save(void) {
  88. nv_write_config_block(0, (u8 *)&conf, sizeof(conf));
  89. nv_write_config_block(1, (u8 *)&conf, sizeof(conf));
  90. }
  91. void mc_conf_load(void) {
  92. mc_config temp;
  93. nv_read_config_block(0, (u8 *)&temp, sizeof(temp));
  94. u16 crc16 = crc16_get((u8 *)&temp, (u8 *)&temp.crc - (u8 *)&temp);
  95. bool idx0_success = crc16 == temp.crc;
  96. if (idx0_success) {
  97. sys_debug("conf block 0 OK!\n");
  98. memcpy(&conf, &temp, sizeof(temp));
  99. }
  100. nv_read_config_block(1, (u8 *)&temp, sizeof(temp));
  101. crc16 = crc16_get((u8 *)&temp, (u8 *)&temp.crc - (u8 *)&temp);
  102. if (crc16 == temp.crc && !idx0_success) {
  103. sys_debug("conf block0 Bad!\n");
  104. sys_debug("conf block 1 OK!\n");
  105. memcpy(&conf, &temp, sizeof(temp));
  106. nv_write_config_block(0, (u8 *)&temp, sizeof(temp));
  107. }else if (crc16 != temp.crc) {
  108. if (idx0_success) {
  109. sys_debug("conf block1 Bad!\n");
  110. nv_write_config_block(1, (u8 *)&conf, sizeof(conf));
  111. }else {
  112. sys_debug("conf block0&1 Bad!\n");
  113. mc_conf_default();
  114. nv_write_config_block(0, (u8 *)&conf, sizeof(conf));
  115. nv_write_config_block(1, (u8 *)&conf, sizeof(conf));
  116. }
  117. }else {
  118. sys_debug("conf block 1 OK!\n");
  119. }
  120. }
  121. #define Gear_torque(l, t) conf.g_n[l].torque[t] = CONFIG_Gear##l##_Torque##t
  122. #define Gear_Config_default(l) \
  123. do { \
  124. conf.g_n[l].max_speed = CONFIG_Gear##l##_MaxSpeed;\
  125. conf.g_n[l].max_idc = CONFIG_Gear##l##_MaxIdc;\
  126. conf.g_n[l].max_trq = CONFIG_Gear##l##_MaxTorque;\
  127. conf.g_n[l].zero_accl = CONFIG_Gear##l##_ZeroAccl;\
  128. conf.g_n[l].accl_time = CONFIG_Gear##l##_NormalAccl;\
  129. Gear_torque(l,0);Gear_torque(l,1);Gear_torque(l,2);Gear_torque(l,3);Gear_torque(l,4); \
  130. Gear_torque(l,5);Gear_torque(l,6);Gear_torque(l,7);Gear_torque(l,8);Gear_torque(l,9); \
  131. }while(0);
  132. #define GearLow_torque(l, t) conf.g_l[l].torque[t] = CONFIG_GearLow##l##_Torque##t
  133. #define GearLow_Config_default(l) \
  134. do { \
  135. conf.g_l[l].max_speed = CONFIG_GearLow##l##_MaxSpeed;\
  136. conf.g_l[l].max_idc = CONFIG_GearLow##l##_MaxIdc;\
  137. conf.g_l[l].max_trq = CONFIG_GearLow##l##_MaxTorque;\
  138. conf.g_l[l].zero_accl = CONFIG_GearLow##l##_ZeroAccl;\
  139. conf.g_l[l].accl_time = CONFIG_GearLow##l##_NormalAccl;\
  140. GearLow_torque(l,0);GearLow_torque(l,1);GearLow_torque(l,2);GearLow_torque(l,3);GearLow_torque(l,4); \
  141. GearLow_torque(l,5);GearLow_torque(l,6);GearLow_torque(l,7);GearLow_torque(l,8);GearLow_torque(l,9); \
  142. }while(0);
  143. #define Limter_Config_default(item, type, n) \
  144. do { \
  145. conf.item[n].enter_pointer = CONFIG_Protect_##type##_Level##n##_Entry;\
  146. conf.item[n].exit_pointer = CONFIG_Protect_##type##_Level##n##_Exit;\
  147. conf.item[n].limit_value = CONFIG_Protect_##type##_Level##n##_Value;\
  148. }while(0);
  149. #define EnReco_Config_default(n) \
  150. do {\
  151. conf.e_r[n].torque = CONFIG_EnergyRecovery_Level##n##_Torque;\
  152. conf.e_r[n].time = CONFIG_EnergyRecovery_Level##n##_Time;\
  153. }while(0);
  154. void mc_conf_default(void) {
  155. conf.version = CONFIG_Version;
  156. conf.m.poles = CONFIG_Motor_Poles;
  157. conf.m.ld = CONFIG_Motor_Ld;
  158. conf.m.lq = CONFIG_Motor_Lq;
  159. conf.m.rs = CONFIG_Motor_Rs;
  160. conf.m.flux = CONFIG_Motor_Flux;
  161. conf.m.nor_pll_band = CONFIG_Motor_PLLBand;
  162. conf.m.epm_pll_band = CONFIG_Motor_EpmPLL;
  163. conf.m.pos_pll_band = CONFIG_Motor_PosPLL;
  164. conf.m.vehicle_w = CONFIG_Motor_VehicleW;
  165. conf.m.wheel_c = CONFIG_Motor_WheelC;
  166. conf.m.gear_ratio = CONFIG_Motor_GearRatio;
  167. conf.m.max_fw_id = CONFIG_Motor_MaxFwDCurr;
  168. conf.m.max_torque = CONFIG_Motor_MaxTorque;
  169. conf.c.max_dc_vol = CONFIG_Foc_MaxDCVol;
  170. conf.c.min_dc_vol = CONFIG_Foc_MinDCVol;
  171. conf.c.max_phase_curr = CONFIG_Foc_MaxPhaseCurr;
  172. conf.c.max_torque = CONFIG_Foc_MaxTorque;
  173. conf.c.max_rpm = CONFIG_Foc_MaxRPM;
  174. conf.c.max_epm_rpm = CONFIG_Foc_MaxEPMRPM;
  175. conf.c.max_epm_torque = CONFIG_Foc_MaxEPMTorque;
  176. conf.c.max_epm_back_rpm = CONFIG_Foc_MaxEPMRPMBk;
  177. conf.c.max_epm_back_torque = CONFIG_Foc_MaxEPMTorqueBk;
  178. conf.c.max_ebrk_torque = CONFIG_Foc_MaxEbrkTorque;
  179. conf.c.max_idc = CONFIG_Foc_MaxIDC;
  180. conf.c.dq_loop_bandwith = CONFIG_Foc_CurrCtrlBandWith;
  181. conf.c.thro_start_vol = CONFIG_Foc_ThroStartVol;
  182. conf.c.thro_end_vol = CONFIG_Foc_ThroEndVol;
  183. conf.c.thro_max_vol = CONFIG_Foc_ThroMaxVol;
  184. conf.c.thro_min_vol = CONFIG_Foc_ThroMinVol;
  185. conf.c.thro_dec_time = CONFIG_Foc_ThroDecTime;
  186. conf.c.max_autohold_torque = CONFIG_Foc_MaxAutoHoldTorque;
  187. conf.c.pid[PID_IQ_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.ld;
  188. conf.c.pid[PID_IQ_ID].ki = conf.m.rs / conf.m.ld;
  189. conf.c.pid[PID_IQ_ID].kd = 0;
  190. conf.c.pid[PID_IQ_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.lq;
  191. conf.c.pid[PID_IQ_ID].ki = conf.m.rs / conf.m.lq;
  192. conf.c.pid[PID_IQ_ID].kd = 0;
  193. conf.c.pid[PID_VelLim_ID].kp = CONFIG_Foc_PID_VelLim_Kp;
  194. conf.c.pid[PID_VelLim_ID].ki = CONFIG_Foc_PID_VelLim_Ki;
  195. conf.c.pid[PID_VelLim_ID].kd = CONFIG_Foc_PID_VelLim_Kd;
  196. conf.c.pid[PID_Vel_ID].kp = CONFIG_Foc_PID_VelCtrl_Kp;
  197. conf.c.pid[PID_Vel_ID].ki = CONFIG_Foc_PID_VelCtrl_Ki;
  198. conf.c.pid[PID_Vel_ID].kd = CONFIG_Foc_PID_VelCtrl_Kd;
  199. conf.c.pid[PID_AutoHold_ID].kp = CONFIG_Foc_PID_Autohold_Kp;
  200. conf.c.pid[PID_AutoHold_ID].ki = CONFIG_Foc_PID_Autohold_Ki;
  201. conf.c.pid[PID_AutoHold_ID].kd = CONFIG_Foc_PID_Autohold_Kd;
  202. conf.c.pid[PID_IDCLim_ID].kp = CONFIG_Foc_PID_IDCLim_Kp;
  203. conf.c.pid[PID_IDCLim_ID].ki = CONFIG_Foc_PID_IDCLim_Ki;
  204. conf.c.pid[PID_IDCLim_ID].kd = CONFIG_Foc_PID_IDCLim_Kd;
  205. conf.s.auto_hold = CONFIG_Settings_AutoHold;
  206. conf.s.brk_shut_power = CONFIG_Settings_BrkShutPower;
  207. conf.s.tcs_enable = CONFIG_Settings_TcsEnable;
  208. Gear_Config_default(0);
  209. Gear_Config_default(1);
  210. Gear_Config_default(2);
  211. Gear_Config_default(3);
  212. GearLow_Config_default(0);
  213. GearLow_Config_default(1);
  214. GearLow_Config_default(2);
  215. GearLow_Config_default(3);
  216. Limter_Config_default(p_mot, Motor, 0);
  217. Limter_Config_default(p_mot, Motor, 1);
  218. Limter_Config_default(p_mot, Motor,2);
  219. Limter_Config_default(p_mos, MosFet, 0);
  220. Limter_Config_default(p_mos, MosFet, 1);
  221. Limter_Config_default(p_mos, MosFet, 2);
  222. conf.p_vol.enter_pointer = CONFIG_Protect_Voltage_Level0_Entry;
  223. conf.p_vol.exit_pointer = CONFIG_Protect_Voltage_Level0_Exit;
  224. conf.p_vol.limit_value = CONFIG_Protect_Voltage_Level0_Value;
  225. EnReco_Config_default(0);
  226. EnReco_Config_default(1);
  227. EnReco_Config_default(2);
  228. EnReco_Config_default(3);
  229. EnReco_Config_default(4);
  230. EnReco_Config_default(5);
  231. EnReco_Config_default(6);
  232. EnReco_Config_default(7);
  233. EnReco_Config_default(8);
  234. EnReco_Config_default(9);
  235. conf.crc = crc16_get((u8 *)&conf, (u8 *)&conf.crc - (u8 *)&conf);
  236. }
  237. void mc_conf_init(void) {
  238. mc_conf_load();
  239. }
  240. int mc_conf_decode_motor(u8 *buff) {
  241. u8 *ori = buff;
  242. conf.m.poles = decode_u8(buff++);
  243. conf.m.ld = decode_float(buff);buff += 4;
  244. conf.m.lq = decode_float(buff);buff += 4;
  245. conf.m.rs = decode_float(buff);buff += 4;
  246. conf.m.flux = decode_float(buff);buff +=4;
  247. conf.m.nor_pll_band = (float)decode_u16(buff); buff += 2;
  248. conf.m.epm_pll_band = (float)decode_u16(buff); buff += 2;
  249. conf.m.pos_pll_band = (float)decode_u16(buff); buff += 2;
  250. conf.m.vehicle_w = decode_u16(buff);buff += 2;
  251. conf.m.wheel_c = decode_u16(buff);buff += 2;
  252. conf.m.gear_ratio = decode_float(buff);buff += 4;
  253. conf.m.max_fw_id = decode_u16(buff); buff += 2;
  254. conf.m.max_torque = decode_u16(buff); buff += 2;
  255. return buff - ori;
  256. }
  257. int mc_conf_encode_motor(u8 *buff) {
  258. u8 *ori = buff;
  259. encode_u8(buff++, conf.m.poles);
  260. encode_float(buff, conf.m.ld);buff += 4;
  261. encode_float(buff, conf.m.lq);buff += 4;
  262. encode_float(buff, conf.m.rs);buff += 4;
  263. encode_float(buff, conf.m.flux);buff +=4;
  264. encode_u16(buff, (u16)conf.m.nor_pll_band); buff += 2;
  265. encode_u16(buff, (u16)conf.m.epm_pll_band); buff += 2;
  266. encode_u16(buff, (u16)conf.m.pos_pll_band); buff += 2;
  267. encode_u16(buff, conf.m.vehicle_w);buff += 2;
  268. encode_u16(buff, conf.m.wheel_c);buff += 2;
  269. encode_float(buff, conf.m.gear_ratio);buff += 4;
  270. encode_u16(buff, conf.m.max_fw_id); buff += 2;
  271. encode_u16(buff, conf.m.max_torque); buff += 2;
  272. return buff - ori;
  273. }
  274. int mc_conf_decode_pid(pid_t *pid, u8 *buff) {
  275. u8 *ori = buff;
  276. pid->kp = decode_float(buff);buff += 4;
  277. pid->ki = decode_float(buff);buff += 4;
  278. pid->kd = decode_float(buff);buff += 4;
  279. return buff - ori;
  280. }
  281. int mc_conf_encode_pid(pid_t *pid, u8 *buff) {
  282. u8 *ori = buff;
  283. encode_float(buff, pid->kp);buff += 4;
  284. encode_float(buff, pid->ki);buff += 4;
  285. encode_float(buff, pid->kd);buff += 4;
  286. return buff - ori;
  287. }
  288. int mc_conf_decode_controler(u8 *buff) {
  289. u8 *ori = buff;
  290. conf.c.max_dc_vol = decode_s16(buff);buff += 2;
  291. conf.c.min_dc_vol = decode_s16(buff);buff += 2;
  292. conf.c.max_phase_curr = decode_s16(buff);buff += 2;
  293. conf.c.max_torque = decode_s16(buff);buff += 2;
  294. conf.c.max_rpm = decode_s16(buff);buff += 2;
  295. conf.c.max_epm_rpm = decode_s16(buff);buff += 2;
  296. conf.c.max_epm_torque = decode_s16(buff);buff += 2;
  297. conf.c.max_epm_back_rpm = decode_s16(buff);buff += 2;
  298. conf.c.max_epm_back_torque = decode_s16(buff);buff += 2;
  299. conf.c.max_ebrk_torque = decode_s16(buff);buff += 2;
  300. conf.c.max_idc = decode_s16(buff);buff += 2;
  301. conf.c.max_autohold_torque = decode_s16(buff);buff += 2;
  302. conf.c.dq_loop_bandwith = decode_s16(buff);buff += 2;
  303. conf.c.thro_start_vol = decode_float(buff);buff += 4;
  304. conf.c.thro_end_vol = decode_float(buff);buff += 4;
  305. conf.c.thro_max_vol = decode_float(buff);buff += 4;
  306. conf.c.thro_min_vol = decode_float(buff);buff += 4;
  307. conf.c.thro_dec_time = decode_u16(buff);buff += 2;
  308. buff += mc_conf_decode_pid(&conf.c.pid[PID_VelLim_ID], buff);
  309. buff += mc_conf_decode_pid(&conf.c.pid[PID_Vel_ID], buff);
  310. buff += mc_conf_decode_pid(&conf.c.pid[PID_AutoHold_ID], buff);
  311. buff += mc_conf_decode_pid(&conf.c.pid[PID_IDCLim_ID], buff);
  312. return buff - ori;
  313. }
  314. int mc_conf_encode_controler(u8 *buff) {
  315. u8 *ori = buff;
  316. encode_s16(buff, conf.c.max_dc_vol);buff += 2;
  317. encode_s16(buff, conf.c.min_dc_vol);buff += 2;
  318. encode_s16(buff, conf.c.max_phase_curr);buff += 2;
  319. encode_s16(buff, conf.c.max_torque);buff += 2;
  320. encode_s16(buff, conf.c.max_rpm);buff += 2;
  321. encode_s16(buff, conf.c.max_epm_rpm);buff += 2;
  322. encode_s16(buff, conf.c.max_epm_torque);buff += 2;
  323. encode_s16(buff, conf.c.max_epm_back_rpm);buff += 2;
  324. encode_s16(buff, conf.c.max_epm_back_torque);buff += 2;
  325. encode_s16(buff, conf.c.max_ebrk_torque);buff += 2;
  326. encode_s16(buff, conf.c.max_idc);buff += 2;
  327. encode_s16(buff, conf.c.max_autohold_torque);buff += 2;
  328. encode_s16(buff, conf.c.dq_loop_bandwith);buff += 2;
  329. encode_float(buff, conf.c.thro_start_vol);buff += 4;
  330. encode_float(buff, conf.c.thro_end_vol);buff += 4;
  331. encode_float(buff, conf.c.thro_max_vol);buff += 4;
  332. encode_float(buff, conf.c.thro_min_vol);buff += 4;
  333. encode_u16(buff, conf.c.thro_dec_time);buff += 2;
  334. buff += mc_conf_encode_pid(&conf.c.pid[PID_VelLim_ID], buff);
  335. buff += mc_conf_encode_pid(&conf.c.pid[PID_Vel_ID], buff);
  336. buff += mc_conf_encode_pid(&conf.c.pid[PID_AutoHold_ID], buff);
  337. buff += mc_conf_encode_pid(&conf.c.pid[PID_IDCLim_ID], buff);
  338. return buff - ori;
  339. }
  340. int mc_conf_decode_setting(u8 *buff) {
  341. u8 *ori = buff;
  342. conf.s.auto_hold = decode_u8(buff++)==1?true:false;
  343. conf.s.brk_shut_power = decode_u8(buff++)==1?true:false;
  344. conf.s.tcs_enable = decode_u8(buff++)==1?true:false;
  345. return buff - ori;
  346. }
  347. int mc_conf_encode_setting(u8 *buff) {
  348. u8 *ori = buff;
  349. encode_u8(buff++, conf.s.auto_hold?1:0);
  350. encode_u8(buff++, conf.s.brk_shut_power?1:0);
  351. encode_u8(buff++, conf.s.tcs_enable?1:0);
  352. return buff - ori;
  353. }
  354. int mc_conf_decode_gear(gear_t *g , u8 *buff) {
  355. u8 *ori = buff;
  356. for (int i = 0; i < CONFIG_MAX_GEARS; i++) {
  357. g[i].max_speed = decode_s16(buff); buff += 2;
  358. g[i].max_trq = decode_s16(buff); buff += 2;
  359. g[i].max_idc = decode_s16(buff); buff += 2;
  360. g[i].zero_accl = decode_u16(buff); buff += 2;
  361. g[i].accl_time = decode_u16(buff); buff += 2;
  362. for (int j = 0; j < CONFIG_GEAR_SPEED_TRQ_NUM; j++) {
  363. g[i].torque[j] = decode_u8(buff++);
  364. }
  365. }
  366. return buff - ori;
  367. }
  368. int mc_conf_encode_gear(gear_t *g, u8 *buff) {
  369. u8 *ori = buff;
  370. for (int i = 0; i < CONFIG_MAX_GEARS; i++) {
  371. encode_s16(buff, g[i].max_speed); buff += 2;
  372. encode_s16(buff, g[i].max_trq); buff += 2;
  373. encode_s16(buff, g[i].max_idc); buff += 2;
  374. encode_u16(buff, g[i].zero_accl); buff += 2;
  375. encode_u16(buff, g[i].accl_time); buff += 2;
  376. for (int j = 0; j < CONFIG_GEAR_SPEED_TRQ_NUM; j++) {
  377. encode_u8(buff++, g[i].torque[j]);
  378. }
  379. }
  380. return buff - ori;
  381. }
  382. int mc_conf_decode_limiter(limiter_t *l, int size, u8 *buff) {
  383. u8 *ori = buff;
  384. for (int i = 0; i < size; i++) {
  385. l->enter_pointer = decode_s16(buff);buff += 2;
  386. l->exit_pointer = decode_s16(buff);buff += 2;
  387. l->limit_value = decode_s16(buff);buff += 2;
  388. l++;
  389. }
  390. return buff - ori;
  391. }
  392. int mc_conf_encode_limiter(limiter_t *l, int size, u8 *buff) {
  393. u8 *ori = buff;
  394. for (int i = 0; i < size; i++) {
  395. encode_s16(buff, l->enter_pointer);buff += 2;
  396. encode_s16(buff, l->exit_pointer);buff += 2;
  397. encode_s16(buff, l->limit_value);buff += 2;
  398. l++;
  399. }
  400. return buff - ori;
  401. }
  402. int mc_conf_decode_engreco(u8 *buff) {
  403. u8 *ori = buff;
  404. for (int i = 0; i < CONFIG_EBRK_LVL_NUM; i++) {
  405. conf.e_r[i].torque = decode_s16(buff); buff += 2;
  406. conf.e_r[i].time = decode_s16(buff); buff += 2;
  407. }
  408. return buff - ori;
  409. }
  410. int mc_conf_encode_engreco(u8 *buff) {
  411. u8 *ori = buff;
  412. for (int i = 0; i < CONFIG_EBRK_LVL_NUM; i++) {
  413. encode_s16(buff, conf.e_r[i].torque); buff += 2;
  414. encode_s16(buff, conf.e_r[i].time); buff += 2;
  415. }
  416. return buff - ori;
  417. }
  418. void mc_conf_decode_configs(void) {
  419. u8 *buff = conf_buff;
  420. conf.version = decode_u8(buff++);
  421. buff += mc_conf_decode_motor(buff);
  422. buff += mc_conf_decode_controler(buff);
  423. buff += mc_conf_decode_setting(buff);
  424. buff += mc_conf_decode_gear(conf.g_n, buff);
  425. buff += mc_conf_decode_gear(conf.g_l, buff);
  426. buff += mc_conf_decode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, buff);
  427. buff += mc_conf_decode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, buff);
  428. buff += mc_conf_decode_limiter(&conf.p_vol, 1, buff);
  429. buff += mc_conf_decode_engreco(buff);
  430. }
  431. int mc_conf_encode_configs(u8 *buff) {
  432. u8 *ori = buff;
  433. encode_u8(buff++, conf.version);
  434. buff += mc_conf_encode_motor(buff);
  435. buff += mc_conf_encode_controler(buff);
  436. buff += mc_conf_encode_setting(buff);
  437. buff += mc_conf_encode_gear(conf.g_n, buff);
  438. buff += mc_conf_encode_gear(conf.g_l, buff);
  439. buff += mc_conf_encode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, buff);
  440. buff += mc_conf_encode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, buff);
  441. buff += mc_conf_encode_limiter(&conf.p_vol, 1, buff);
  442. buff += mc_conf_encode_engreco(buff);
  443. return buff - ori;
  444. }
  445. void mc_conf_set_pid(u8 id, pid_t *pid) {
  446. conf.c.pid[id] = *pid;
  447. }
  448. void mc_conf_get_pid(u8 id, pid_t *pid) {
  449. *pid = conf.c.pid[id];
  450. }
  451. bool mc_conf_set_gear(u8 mode, u8 *data, int len) {
  452. gear_t *g = conf.g_n;
  453. if (mode == 0) {
  454. g = conf.g_l;
  455. }
  456. if (mc_conf_decode_gear(g, data) > len) {
  457. return false;
  458. }
  459. return true;
  460. }
  461. int mc_conf_get_gear(u8 mode, u8 *data) {
  462. gear_t *g = conf.g_n;
  463. if (mode == 0) {
  464. g = conf.g_l;
  465. }
  466. return mc_conf_encode_gear(g, data);
  467. }
  468. bool mc_conf_set_limter(u8 *data, int len) {
  469. int l = mc_conf_decode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, data);
  470. l += mc_conf_decode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, data + l);
  471. l += mc_conf_decode_limiter(&conf.p_vol, 1, data + l);
  472. return len>=l;
  473. }
  474. int mc_conf_get_limter(u8 *data) {
  475. u8 *ori = data;
  476. data += mc_conf_encode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, data);
  477. data += mc_conf_encode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, data);
  478. data += mc_conf_encode_limiter(&conf.p_vol, 1, data);
  479. return data-ori;
  480. }