mc_config.c 16 KB

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