mc_config.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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.velocity_w = CONFIG_Motor_VelocityW;
  165. conf.m.wheel_c = CONFIG_Motor_VelocityC;
  166. conf.m.gear_ratio = CONFIG_Motor_GearRatio;
  167. conf.m.max_fw_id = CONFIG_Motor_MaxFwDCurr;
  168. conf.c.max_dc_vol = CONFIG_Foc_MaxDCVol;
  169. conf.c.min_dc_vol = CONFIG_Foc_MinDCVol;
  170. conf.c.max_phase_curr = CONFIG_Foc_MaxPhaseCurr;
  171. conf.c.max_torque = CONFIG_Foc_MaxTorque;
  172. conf.c.max_rpm = CONFIG_Foc_MaxRPM;
  173. conf.c.max_epm_rpm = CONFIG_Foc_MaxEPMRPM;
  174. conf.c.max_epm_torque = CONFIG_Foc_MaxEPMTorque;
  175. conf.c.max_epm_back_rpm = CONFIG_Foc_MaxEPMRPMBk;
  176. conf.c.max_epm_back_torque = CONFIG_Foc_MaxEPMTorqueBk;
  177. conf.c.max_ebrk_torque = CONFIG_Foc_MaxEbrkTorque;
  178. conf.c.max_idc = CONFIG_Foc_MaxIDC;
  179. conf.c.dq_loop_bandwith = CONFIG_Foc_CurrCtrlBandWith;
  180. conf.c.thro_start_vol = CONFIG_Foc_ThroStartVol;
  181. conf.c.thro_end_vol = CONFIG_Foc_ThroEndVol;
  182. conf.c.thro_max_vol = CONFIG_Foc_ThroMaxVol;
  183. conf.c.thro_min_vol = CONFIG_Foc_ThroMinVol;
  184. conf.c.thro_dec_time = CONFIG_Foc_ThroDecTime;
  185. conf.c.pid[PID_IQ_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.ld;
  186. conf.c.pid[PID_IQ_ID].ki = conf.m.rs / conf.m.ld;
  187. conf.c.pid[PID_IQ_ID].kd = 0;
  188. conf.c.pid[PID_IQ_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.lq;
  189. conf.c.pid[PID_IQ_ID].ki = conf.m.rs / conf.m.lq;
  190. conf.c.pid[PID_IQ_ID].kd = 0;
  191. conf.c.pid[PID_VelLim_ID].kp = CONFIG_Foc_PID_VelLim_Kp;
  192. conf.c.pid[PID_VelLim_ID].ki = CONFIG_Foc_PID_VelLim_Ki;
  193. conf.c.pid[PID_VelLim_ID].kd = CONFIG_Foc_PID_VelLim_Kd;
  194. conf.c.pid[PID_Vel_ID].kp = CONFIG_Foc_PID_VelCtrl_Kp;
  195. conf.c.pid[PID_Vel_ID].ki = CONFIG_Foc_PID_VelCtrl_Ki;
  196. conf.c.pid[PID_Vel_ID].kd = CONFIG_Foc_PID_VelCtrl_Kd;
  197. conf.c.pid[PID_AutoHold_ID].kp = CONFIG_Foc_PID_Autohold_Kp;
  198. conf.c.pid[PID_AutoHold_ID].ki = CONFIG_Foc_PID_Autohold_Ki;
  199. conf.c.pid[PID_AutoHold_ID].kd = CONFIG_Foc_PID_Autohold_Kd;
  200. conf.c.pid[PID_IDCLim_ID].kp = CONFIG_Foc_PID_IDCLim_Kp;
  201. conf.c.pid[PID_IDCLim_ID].ki = CONFIG_Foc_PID_IDCLim_Ki;
  202. conf.c.pid[PID_IDCLim_ID].kd = CONFIG_Foc_PID_IDCLim_Kd;
  203. conf.s.auto_hold = CONFIG_Settings_AutoHold;
  204. conf.s.brk_shut_power = CONFIG_Settings_BrkShutPower;
  205. conf.s.tcs_enable = CONFIG_Settings_TcsEnable;
  206. Gear_Config_default(0);
  207. Gear_Config_default(1);
  208. Gear_Config_default(2);
  209. Gear_Config_default(3);
  210. GearLow_Config_default(0);
  211. GearLow_Config_default(1);
  212. GearLow_Config_default(2);
  213. GearLow_Config_default(3);
  214. Limter_Config_default(p_mot, Motor, 0);
  215. Limter_Config_default(p_mot, Motor, 1);
  216. Limter_Config_default(p_mot, Motor,2);
  217. Limter_Config_default(p_mos, MosFet, 0);
  218. Limter_Config_default(p_mos, MosFet, 1);
  219. Limter_Config_default(p_mos, MosFet, 2);
  220. conf.p_vol.enter_pointer = CONFIG_Protect_Voltage_Level0_Entry;
  221. conf.p_vol.exit_pointer = CONFIG_Protect_Voltage_Level0_Exit;
  222. conf.p_vol.limit_value = CONFIG_Protect_Voltage_Level0_Value;
  223. EnReco_Config_default(0);
  224. EnReco_Config_default(1);
  225. EnReco_Config_default(2);
  226. EnReco_Config_default(3);
  227. EnReco_Config_default(4);
  228. EnReco_Config_default(5);
  229. EnReco_Config_default(6);
  230. EnReco_Config_default(7);
  231. EnReco_Config_default(8);
  232. EnReco_Config_default(9);
  233. conf.crc = crc16_get((u8 *)&conf, (u8 *)&conf.crc - (u8 *)&conf);
  234. }
  235. void mc_conf_init(void) {
  236. mc_conf_load();
  237. }
  238. int mc_conf_decode_motor(u8 *buff) {
  239. u8 *ori = buff;
  240. conf.m.poles = decode_u8(buff++);
  241. conf.m.ld = decode_float(buff);buff += 4;
  242. conf.m.lq = decode_float(buff);buff += 4;
  243. conf.m.rs = decode_float(buff);buff += 4;
  244. conf.m.flux = decode_float(buff);buff +=4;
  245. conf.m.nor_pll_band = (float)decode_u16(buff); buff += 2;
  246. conf.m.epm_pll_band = (float)decode_u16(buff); buff += 2;
  247. conf.m.pos_pll_band = (float)decode_u16(buff); buff += 2;
  248. conf.m.velocity_w = decode_u16(buff);buff += 2;
  249. conf.m.wheel_c = decode_u16(buff);buff += 2;
  250. conf.m.gear_ratio = decode_float(buff);buff += 4;
  251. conf.m.max_fw_id = decode_u16(buff); buff += 2;
  252. return buff - ori;
  253. }
  254. int mc_conf_encode_motor(u8 *buff) {
  255. u8 *ori = buff;
  256. encode_u8(buff++, conf.m.poles);
  257. encode_float(buff, conf.m.ld);buff += 4;
  258. encode_float(buff, conf.m.lq);buff += 4;
  259. encode_float(buff, conf.m.rs);buff += 4;
  260. encode_float(buff, conf.m.flux);buff +=4;
  261. encode_u16(buff, (u16)conf.m.nor_pll_band); buff += 2;
  262. encode_u16(buff, (u16)conf.m.epm_pll_band); buff += 2;
  263. encode_u16(buff, (u16)conf.m.pos_pll_band); buff += 2;
  264. encode_u16(buff, conf.m.velocity_w);buff += 2;
  265. encode_u16(buff, conf.m.wheel_c);buff += 2;
  266. encode_float(buff, conf.m.gear_ratio);buff += 4;
  267. encode_u16(buff, conf.m.max_fw_id); buff += 2;
  268. return buff - ori;
  269. }
  270. int mc_conf_decode_pid(pid_t *pid, u8 *buff) {
  271. u8 *ori = buff;
  272. pid->kp = decode_float(buff);buff += 4;
  273. pid->ki = decode_float(buff);buff += 4;
  274. pid->kd = decode_float(buff);buff += 4;
  275. return buff - ori;
  276. }
  277. int mc_conf_encode_pid(pid_t *pid, u8 *buff) {
  278. u8 *ori = buff;
  279. encode_float(buff, pid->kp);buff += 4;
  280. encode_float(buff, pid->ki);buff += 4;
  281. encode_float(buff, pid->kd);buff += 4;
  282. return buff - ori;
  283. }
  284. int mc_conf_decode_controler(u8 *buff) {
  285. u8 *ori = buff;
  286. conf.c.max_dc_vol = decode_s16(buff);buff += 2;
  287. conf.c.min_dc_vol = decode_s16(buff);buff += 2;
  288. conf.c.max_phase_curr = decode_s16(buff);buff += 2;
  289. conf.c.max_torque = decode_s16(buff);buff += 2;
  290. conf.c.max_rpm = decode_s16(buff);buff += 2;
  291. conf.c.max_epm_rpm = decode_s16(buff);buff += 2;
  292. conf.c.max_epm_torque = decode_s16(buff);buff += 2;
  293. conf.c.max_epm_back_rpm = decode_s16(buff);buff += 2;
  294. conf.c.max_epm_back_torque = decode_s16(buff);buff += 2;
  295. conf.c.max_ebrk_torque = decode_s16(buff);buff += 2;
  296. conf.c.max_idc = decode_s16(buff);buff += 2;
  297. conf.c.dq_loop_bandwith = decode_s16(buff);buff += 2;
  298. conf.c.thro_start_vol = decode_float(buff);buff += 4;
  299. conf.c.thro_end_vol = decode_float(buff);buff += 4;
  300. conf.c.thro_max_vol = decode_float(buff);buff += 4;
  301. conf.c.thro_min_vol = decode_float(buff);buff += 4;
  302. conf.c.thro_dec_time = decode_u16(buff);buff += 2;
  303. buff += mc_conf_decode_pid(&conf.c.pid[PID_VelLim_ID], buff);
  304. buff += mc_conf_decode_pid(&conf.c.pid[PID_Vel_ID], buff);
  305. buff += mc_conf_decode_pid(&conf.c.pid[PID_AutoHold_ID], buff);
  306. buff += mc_conf_decode_pid(&conf.c.pid[PID_IDCLim_ID], buff);
  307. return buff - ori;
  308. }
  309. int mc_conf_encode_controler(u8 *buff) {
  310. u8 *ori = buff;
  311. encode_s16(buff, conf.c.max_dc_vol);buff += 2;
  312. encode_s16(buff, conf.c.min_dc_vol);buff += 2;
  313. encode_s16(buff, conf.c.max_phase_curr);buff += 2;
  314. encode_s16(buff, conf.c.max_torque);buff += 2;
  315. encode_s16(buff, conf.c.max_rpm);buff += 2;
  316. encode_s16(buff, conf.c.max_epm_rpm);buff += 2;
  317. encode_s16(buff, conf.c.max_epm_torque);buff += 2;
  318. encode_s16(buff, conf.c.max_epm_back_rpm);buff += 2;
  319. encode_s16(buff, conf.c.max_epm_back_torque);buff += 2;
  320. encode_s16(buff, conf.c.max_ebrk_torque);buff += 2;
  321. encode_s16(buff, conf.c.max_idc);buff += 2;
  322. encode_s16(buff, conf.c.dq_loop_bandwith);buff += 2;
  323. encode_float(buff, conf.c.thro_start_vol);buff += 4;
  324. encode_float(buff, conf.c.thro_end_vol);buff += 4;
  325. encode_float(buff, conf.c.thro_max_vol);buff += 4;
  326. encode_float(buff, conf.c.thro_min_vol);buff += 4;
  327. encode_u16(buff, conf.c.thro_dec_time);buff += 2;
  328. buff += mc_conf_encode_pid(&conf.c.pid[PID_VelLim_ID], buff);
  329. buff += mc_conf_encode_pid(&conf.c.pid[PID_Vel_ID], buff);
  330. buff += mc_conf_encode_pid(&conf.c.pid[PID_AutoHold_ID], buff);
  331. buff += mc_conf_encode_pid(&conf.c.pid[PID_IDCLim_ID], buff);
  332. return buff - ori;
  333. }
  334. int mc_conf_decode_setting(u8 *buff) {
  335. u8 *ori = buff;
  336. conf.s.auto_hold = decode_u8(buff++)==1?true:false;
  337. conf.s.brk_shut_power = decode_u8(buff++)==1?true:false;
  338. conf.s.tcs_enable = decode_u8(buff++)==1?true:false;
  339. return buff - ori;
  340. }
  341. int mc_conf_encode_setting(u8 *buff) {
  342. u8 *ori = buff;
  343. encode_u8(buff++, conf.s.auto_hold?1:0);
  344. encode_u8(buff++, conf.s.brk_shut_power?1:0);
  345. encode_u8(buff++, conf.s.tcs_enable?1:0);
  346. return buff - ori;
  347. }
  348. int mc_conf_decode_gear(gear_t *g , u8 *buff) {
  349. u8 *ori = buff;
  350. for (int i = 0; i < CONFIG_MAX_GEARS; i++) {
  351. g[i].max_speed = decode_s16(buff); buff += 2;
  352. g[i].max_trq = decode_s16(buff); buff += 2;
  353. g[i].max_idc = decode_s16(buff); buff += 2;
  354. g[i].zero_accl = decode_u16(buff); buff += 2;
  355. g[i].accl_time = decode_u16(buff); buff += 2;
  356. for (int j = 0; j < CONFIG_GEAR_SPEED_TRQ_NUM; j++) {
  357. g[i].torque[j] = decode_u8(buff++);
  358. }
  359. }
  360. return buff - ori;
  361. }
  362. int mc_conf_encode_gear(gear_t *g, u8 *buff) {
  363. u8 *ori = buff;
  364. for (int i = 0; i < CONFIG_MAX_GEARS; i++) {
  365. encode_s16(buff, g[i].max_speed); buff += 2;
  366. encode_s16(buff, g[i].max_trq); buff += 2;
  367. encode_s16(buff, g[i].max_idc); buff += 2;
  368. encode_u16(buff, g[i].zero_accl); buff += 2;
  369. encode_u16(buff, g[i].accl_time); buff += 2;
  370. for (int j = 0; j < CONFIG_GEAR_SPEED_TRQ_NUM; j++) {
  371. encode_u8(buff++, g[i].torque[j]);
  372. }
  373. }
  374. return buff - ori;
  375. }
  376. int mc_conf_decode_limiter(limiter_t *l, int size, u8 *buff) {
  377. u8 *ori = buff;
  378. for (int i = 0; i < size; i++) {
  379. l->enter_pointer = decode_s16(buff);buff += 2;
  380. l->exit_pointer = decode_s16(buff);buff += 2;
  381. l->limit_value = decode_s16(buff);buff += 2;
  382. l++;
  383. }
  384. return buff - ori;
  385. }
  386. int mc_conf_encode_limiter(limiter_t *l, int size, u8 *buff) {
  387. u8 *ori = buff;
  388. for (int i = 0; i < size; i++) {
  389. encode_s16(buff, l->enter_pointer);buff += 2;
  390. encode_s16(buff, l->exit_pointer);buff += 2;
  391. encode_s16(buff, l->limit_value);buff += 2;
  392. l++;
  393. }
  394. return buff - ori;
  395. }
  396. int mc_conf_decode_engreco(u8 *buff) {
  397. u8 *ori = buff;
  398. for (int i = 0; i < CONFIG_EBRK_LVL_NUM; i++) {
  399. conf.e_r[i].torque = decode_s16(buff); buff += 2;
  400. conf.e_r[i].time = decode_s16(buff); buff += 2;
  401. }
  402. return buff - ori;
  403. }
  404. int mc_conf_encode_engreco(u8 *buff) {
  405. u8 *ori = buff;
  406. for (int i = 0; i < CONFIG_EBRK_LVL_NUM; i++) {
  407. encode_s16(buff, conf.e_r[i].torque); buff += 2;
  408. encode_s16(buff, conf.e_r[i].time); buff += 2;
  409. }
  410. return buff - ori;
  411. }
  412. void mc_conf_decode_configs(void) {
  413. u8 *buff = conf_buff;
  414. conf.version = decode_u8(buff++);
  415. buff += mc_conf_decode_motor(buff);
  416. buff += mc_conf_decode_controler(buff);
  417. buff += mc_conf_decode_setting(buff);
  418. buff += mc_conf_decode_gear(conf.g_n, buff);
  419. buff += mc_conf_decode_gear(conf.g_l, buff);
  420. buff += mc_conf_decode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, buff);
  421. buff += mc_conf_decode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, buff);
  422. buff += mc_conf_decode_limiter(&conf.p_vol, 1, buff);
  423. buff += mc_conf_decode_engreco(buff);
  424. }
  425. int mc_conf_encode_configs(u8 *buff) {
  426. u8 *ori = buff;
  427. encode_u8(buff++, conf.version);
  428. buff += mc_conf_encode_motor(buff);
  429. buff += mc_conf_encode_controler(buff);
  430. buff += mc_conf_encode_setting(buff);
  431. buff += mc_conf_encode_gear(conf.g_n, buff);
  432. buff += mc_conf_encode_gear(conf.g_l, buff);
  433. buff += mc_conf_encode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, buff);
  434. buff += mc_conf_encode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, buff);
  435. buff += mc_conf_encode_limiter(&conf.p_vol, 1, buff);
  436. buff += mc_conf_encode_engreco(buff);
  437. return buff - ori;
  438. }
  439. void mc_conf_set_pid(u8 id, pid_t *pid) {
  440. conf.c.pid[id] = *pid;
  441. }
  442. void mc_conf_get_pid(u8 id, pid_t *pid) {
  443. *pid = conf.c.pid[id];
  444. }
  445. bool mc_conf_set_gear(u8 mode, u8 *data, int len) {
  446. gear_t *g = conf.g_n;
  447. if (mode == 0) {
  448. g = conf.g_l;
  449. }
  450. if (mc_conf_decode_gear(g, data) > len) {
  451. return false;
  452. }
  453. return true;
  454. }
  455. int mc_conf_get_gear(u8 mode, u8 *data) {
  456. gear_t *g = conf.g_n;
  457. if (mode == 0) {
  458. g = conf.g_l;
  459. }
  460. return mc_conf_encode_gear(g, data);
  461. }
  462. bool mc_conf_set_limter(u8 *data, int len) {
  463. int l = mc_conf_decode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, data);
  464. l += mc_conf_decode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, data + l);
  465. l += mc_conf_decode_limiter(&conf.p_vol, 1, data + l);
  466. return len>=l;
  467. }
  468. int mc_conf_get_limter(u8 *data) {
  469. u8 *ori = data;
  470. data += mc_conf_encode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, data);
  471. data += mc_conf_encode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, data);
  472. data += mc_conf_encode_limiter(&conf.p_vol, 1, data);
  473. return data-ori;
  474. }