commands.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. #include "os/os_task.h"
  2. #include "os/queue.h"
  3. #include "libs/logger.h"
  4. #include "libs/utils.h"
  5. #include "prot/can_message.h"
  6. #include "bsp/bsp_driver.h"
  7. #include "foc/motor/motor.h"
  8. #include "foc/commands.h"
  9. #include "prot/can_foc_msg.h"
  10. #include "app/nv_storage.h"
  11. #include "foc/core/foc_observer.h"
  12. #ifdef CONFIG_DQ_STEP_RESPONSE
  13. extern float target_d;
  14. extern float target_q;
  15. #endif
  16. extern int plot_type;
  17. static void _reboot_timer_handler(shark_timer_t *);
  18. static shark_timer_t _reboot_timer = TIMER_INIT(_reboot_timer, _reboot_timer_handler);
  19. static u32 foc_command_task(void *args);
  20. static void process_foc_command(foc_cmd_body_t *command);
  21. static co_queue_t _cmd_queue;
  22. void foc_command_init(void) {
  23. _cmd_queue = queue_create(16, sizeof(foc_cmd_body_t));
  24. shark_task_create(foc_command_task, NULL);
  25. }
  26. bool foc_send_command(foc_cmd_body_t *command) {
  27. if (!queue_put(_cmd_queue, command)) {
  28. if (command->data) {
  29. os_free(command->data);
  30. }
  31. return false;
  32. }
  33. return true;
  34. }
  35. static u32 foc_command_task(void *args) {
  36. foc_cmd_body_t command;
  37. if (queue_get(_cmd_queue, &command)) {
  38. process_foc_command(&command);
  39. if (command.data) {
  40. os_free(command.data);
  41. }
  42. }
  43. return 0;
  44. }
  45. static void process_ext_command(foc_cmd_body_t *command) {
  46. if (command->ext_key == 0x1A01) {
  47. return;
  48. }else if (command->ext_key == 0x1A02) {
  49. u8 b0 = decode_u8(command->data);
  50. u8 p_mode = decode_8bits(b0, 0, 1);
  51. if (p_mode == 1) {
  52. mc_start(CTRL_MODE_TRQ);
  53. }else if (p_mode == 2) {
  54. mc_stop();
  55. }
  56. s8 ext_gear = decode_8bits(b0, 5, 7);
  57. if (ext_gear >= 0 && ext_gear <= 5) {
  58. sys_debug("gear %d\n", ext_gear);
  59. if (ext_gear == 0) {
  60. mc_set_gear(3);
  61. }else {
  62. mc_set_gear(ext_gear - 1);
  63. }
  64. }
  65. u8 b1 = decode_u8((u8 *)command->data + 1);
  66. u8 cruise = decode_8bits(b1, 0, 1);
  67. if (cruise == 2) {
  68. PMSM_FOC_EnableCruise(true);
  69. }else if (cruise == 1) {
  70. PMSM_FOC_EnableCruise(false);
  71. }
  72. u8 epm = decode_8bits(b0, 2, 3);
  73. if (epm == 2) {
  74. mc_start_epm(true);
  75. }else if(epm == 1) {
  76. mc_start_epm(false);
  77. }
  78. u8 m_4896 = decode_8bits(b1, 4, 5);
  79. u8 epm_dir = decode_8bits(b1, 6, 7);
  80. if (epm_dir == 0) {
  81. mc_command_epm_move(EPM_Dir_None);
  82. }else if (epm_dir == 1) {
  83. mc_command_epm_move(EPM_Dir_Back);
  84. }else if (epm_dir == 2) {
  85. mc_command_epm_move(EPM_Dir_Forward);
  86. }
  87. u16 cruise_spd = decode_u16((u8 *)command->data + 3);
  88. if ((cruise_spd > 0) && (cruise_spd != 0xFFFF)) {
  89. PMSM_FOC_Set_CruiseSpeed((float)cruise_spd);
  90. }
  91. u8 response[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  92. response[0] &= 0xFC;
  93. response[0] |= (mc_is_start()?1:2);
  94. response[0] |= (mc_get_gear() << 5);
  95. response[1] &= 0xC0;
  96. response[1] |= (PMSM_FOC_Is_CruiseEnabled()?2:1);
  97. response[1] |= (mc_is_epm()?1:2) << 2;
  98. response[1] |= m_4896<<4;
  99. shark_can0_send_ext_message(0x1A024D43, response, sizeof(response));
  100. }else if (command->ext_key == 0x1A05) {
  101. shark_can0_send_ext_message(0x1A054D43, command->data, command->len);
  102. }
  103. }
  104. static u8 ignore_with_speed[] = {Foc_Set_Adrc_Params, Foc_Set_Gear_Limit, Foc_Conf_Pid, Foc_Set_Throttle_throld, Foc_Set_Config, Foc_Set_eBrake_Throld, Foc_Set_Limiter_Config, Foc_End_Write_TRQ_Table, Foc_SN_Write};
  105. static bool _can_process_with_speed(u8 cmd) {
  106. int size = ARRAY_SIZE(ignore_with_speed);
  107. if (!mc_is_start() || PMSM_FOC_GetSpeed() < 0.1f) {
  108. return true;
  109. }
  110. for (int i = 0; i < size; i++) {
  111. if (ignore_with_speed[i] == cmd) {
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. static void process_foc_command(foc_cmd_body_t *command) {
  118. u8 erroCode = 0;
  119. u8 response[32];
  120. int len = 3;
  121. if ((command->ext_key != 0) && (command->cmd == 0)) {
  122. process_ext_command(command);
  123. return;
  124. }
  125. if (!_can_process_with_speed(command->cmd)) {
  126. erroCode = FOC_NowAllowed_With_Speed;
  127. goto cmd_end;
  128. }
  129. switch (command->cmd) {
  130. case Foc_Start_Motor:
  131. {
  132. bool success;
  133. foc_start_cmd_t *scmd = (foc_start_cmd_t *)command->data;
  134. sys_debug("start cmd %d\n", scmd->start_stop);
  135. if (scmd->start_stop == Foc_Start) {
  136. success = mc_start(CTRL_MODE_TRQ);
  137. }else if (scmd->start_stop == Foc_Stop) {
  138. success = mc_stop();
  139. }
  140. if (!success) {
  141. erroCode = PMSM_FOC_GetErrCode();
  142. }
  143. sys_debug("start motor %d\n", erroCode);
  144. break;
  145. }
  146. case Foc_Set_DQ_Current:
  147. {
  148. #ifdef CONFIG_DQ_STEP_RESPONSE
  149. if (command->len == 2) {
  150. target_d = (float)decode_s08(command->data);
  151. target_q = (float)decode_s08((u8 *)command->data + 1);
  152. sys_debug("step res %f, %f\n", target_d, target_q);
  153. }else {
  154. erroCode = FOC_Param_Err;
  155. }
  156. #else
  157. erroCode = FOC_NotAllowed;
  158. #endif
  159. break;
  160. }
  161. case Foc_Set_Gear_Mode:
  162. {
  163. u8 gear = decode_u8(command->data);
  164. if (gear > 3) {
  165. erroCode = FOC_Param_Err;
  166. }else {
  167. sys_debug("set gear %d\n", gear);
  168. mc_set_gear(gear);
  169. response[3] = gear;
  170. len += 1;
  171. }
  172. break;
  173. }
  174. case Foc_Set_Cruise_Mode:
  175. {
  176. u8 enable = decode_u8(command->data);
  177. if (!mc_enable_cruise(enable)) {
  178. erroCode = PMSM_FOC_GetErrCode();
  179. }
  180. break;
  181. }
  182. case Foc_Set_Cruise_Speed:
  183. {
  184. u8 mode = decode_u8(command->data);
  185. float rpm = (float)decode_s16((u8 *)command->data + 1);
  186. if (!mc_set_cruise_speed(mode?true:false, rpm)) {
  187. erroCode = PMSM_FOC_GetErrCode();
  188. }
  189. sys_debug("Cruise RPM %d\n", (int)rpm);
  190. encode_u16(response + 3, (s16)rpm);
  191. len += 2;
  192. break;
  193. }
  194. case Foc_Set_Ctrl_Mode:
  195. {
  196. u8 mode = decode_u8(command->data);
  197. sys_debug("ctl mode = %d, len = %d\n", mode, command->len);
  198. if (!mc_set_foc_mode(mode)) {
  199. erroCode = PMSM_FOC_GetErrCode();
  200. }
  201. response[len++] = PMSM_FOC_GetCtrlMode();
  202. break;
  203. }
  204. case Foc_Set_Gear_Limit:
  205. {
  206. sys_debug("len = %d\n", command->len);
  207. u8 mode = decode_u8(command->data);
  208. if (!nv_set_gear_config(mode, (u8 *)command->data+1, command->len-1)){
  209. erroCode = FOC_Param_Err;
  210. }
  211. break;
  212. }
  213. case Foc_Get_Gear_Limit:
  214. {
  215. u8 mode = decode_u8(command->data);
  216. int config_len = 0;
  217. void *config = nv_get_gear_config(mode, &config_len);
  218. u8 *data = os_alloc(config_len + 3);
  219. data[0] = command->cmd;
  220. data[1] = CAN_MY_ADDRESS;
  221. data[2] = 0;
  222. memcpy(data + 3, config, config_len);
  223. can_send_response(command->can_src, data, config_len + 3);
  224. os_free(data);
  225. return;
  226. }
  227. case Foc_Set_Speed_Limit:
  228. {
  229. s16 speed = decode_s16(((u8 *)command->data));
  230. PMSM_FOC_SpeedLimit(speed);
  231. encode_u16(response + 3, (u16)PMSM_FOC_GetSpeedLimit());
  232. len += 2;
  233. break;
  234. }
  235. case Foc_Set_iDC_Limit:
  236. {
  237. u16 current = decode_u16(((u8 *)command->data));
  238. mc_set_idc_limit((float)current);
  239. encode_u16(response + 3, (u16)PMSM_FOC_GetDCCurrLimit());
  240. len += 2;
  241. break;
  242. }
  243. case Foc_Set_Phase_CurrLim:
  244. {
  245. s16 curr = decode_s16(((u8 *)command->data));
  246. PMSM_FOC_PhaseCurrLim((float)curr);
  247. encode_u16(response + 3, (u16)PMSM_FOC_GetPhaseCurrLim());
  248. len += 2;
  249. break;
  250. }
  251. case Foc_Cali_Hall_Phase:
  252. {
  253. s16 vd = decode_s16((u8 *)command->data);
  254. sys_debug("cali encoder %d\n", vd);
  255. erroCode = mc_encoder_zero_calibrate(vd)? FOC_Success:FOC_Param_Err;
  256. break;
  257. }
  258. case Foc_Enc_Zero_Cali_Result:
  259. {
  260. response[2] = encoder_get_cali_error()?1:0;
  261. u32 off = encoder_get_cnt_offset();
  262. encode_u32(response + 3, off);
  263. len += 4;
  264. break;
  265. }
  266. case Foc_Force_Open_Run:
  267. {
  268. s16 vd = decode_s16((u8 *)command->data);
  269. mc_force_run_open(vd, 0);
  270. break;
  271. }
  272. case Foc_Set_Open_Dq_Vol:
  273. {
  274. s16 vd = decode_s16(((u8 *)command->data));
  275. s16 vq = decode_s16(((u8 *)command->data) + 2);
  276. sys_debug("set v_q %d, %d\n", vd, vq);
  277. PMSM_FOC_SetOpenVdq(vd, (vq));
  278. break;
  279. }
  280. case Foc_Conf_Pid:
  281. {
  282. if (command->len < 13) {
  283. erroCode = FOC_Param_Err;
  284. break;
  285. }
  286. pid_conf_t pid;
  287. u8 id = decode_u8((u8 *)command->data);
  288. memcpy((char *)&pid, (char *)command->data + 1, sizeof(pid_conf_t));
  289. sys_debug("set id = %d, kp = %f, ki = %f, kd = %f\n", id, pid.kp, pid.ki, pid.kd);
  290. PMSM_FOC_SetPid(id, pid.kp, pid.ki, pid.kd);
  291. nv_set_pid(id, &pid);
  292. break;
  293. }
  294. case Foc_Get_Pid:
  295. {
  296. pid_conf_t pid;
  297. u8 id = decode_u8((u8 *)command->data);
  298. if (id < PID_Max_id) {
  299. nv_get_pid(id, &pid);
  300. erroCode = id;
  301. memcpy(response+3, &pid, sizeof(pid));
  302. len = sizeof(pid) + 3;
  303. sys_debug("get id = %d, kp = %f, ki = %f, kd = %f\n", id, pid.kp, pid.ki, pid.kd);
  304. }else {
  305. erroCode = 1;
  306. len = 3;
  307. }
  308. break;
  309. }
  310. case Foc_Set_Adrc_Params:
  311. {
  312. if (command->len < 24) {
  313. erroCode = FOC_Param_Err;
  314. break;
  315. }
  316. nv_get_foc_params()->f_adrc_vel_lim_Wo = decode_float(command->data);
  317. nv_get_foc_params()->f_adrc_vel_lim_Wcv = decode_float((u8 *)command->data + 4);
  318. nv_get_foc_params()->f_adrc_vel_lim_B0 = decode_float((u8 *)command->data + 8);
  319. nv_get_foc_params()->f_adrc_vel_Wo = decode_float((u8 *)command->data + 12);
  320. nv_get_foc_params()->f_adrc_vel_Wcv = decode_float((u8 *)command->data + 16);
  321. nv_get_foc_params()->f_adrc_vel_B0 = decode_float((u8 *)command->data + 20);
  322. nv_save_foc_params();
  323. break;
  324. }
  325. case Foc_Get_Adrc_Params:
  326. {
  327. encode_float(response+3, nv_get_foc_params()->f_adrc_vel_lim_Wo);
  328. encode_float(response+7, nv_get_foc_params()->f_adrc_vel_lim_Wcv);
  329. encode_float(response+11, nv_get_foc_params()->f_adrc_vel_lim_B0);
  330. encode_float(response+15, nv_get_foc_params()->f_adrc_vel_Wo);
  331. encode_float(response+19, nv_get_foc_params()->f_adrc_vel_Wcv);
  332. encode_float(response+23, nv_get_foc_params()->f_adrc_vel_B0);
  333. len += 24;
  334. break;
  335. }
  336. case Foc_Set_EPM_Mode:
  337. {
  338. bool mode = decode_u8((u8 *)command->data) == 0?false:true;
  339. if (!mc_start_epm(mode)) {
  340. erroCode = PMSM_FOC_GetErrCode();
  341. }
  342. break;
  343. }
  344. case Foc_Set_Thro_Ration:
  345. {
  346. if (command->len >= 2) {
  347. bool use = decode_u8(command->data)==0?false:true;
  348. u8 r = decode_u8((u8 *)command->data + 1);
  349. mc_set_throttle_r(use, r);
  350. }
  351. break;
  352. }
  353. case Foc_Lock_Motor:
  354. {
  355. u8 lock = decode_u8((u8 *)command->data);
  356. if (lock == Foc_Start) {
  357. mc_lock_motor(true);
  358. }else {
  359. mc_lock_motor(false);
  360. }
  361. erroCode = PMSM_FOC_GetErrCode();
  362. break;
  363. }
  364. case Foc_Auto_Hold:
  365. {
  366. u8 hold = decode_u8((u8 *)command->data);
  367. if (hold == Foc_Start) {
  368. mc_auto_hold(true);
  369. }else {
  370. mc_auto_hold(false);
  371. }
  372. erroCode = PMSM_FOC_GetErrCode();
  373. break;
  374. }
  375. case Foc_Start_EPM_Move:
  376. {
  377. EPM_Dir_t dir = (EPM_Dir_t)decode_u8((u8 *)command->data);
  378. if(!mc_command_epm_move(dir)) {
  379. erroCode = PMSM_FOC_GetErrCode();
  380. }
  381. break;
  382. }
  383. case Foc_Start_DQ_Calibrate:
  384. {
  385. u8 start = decode_u8((u8 *)command->data);
  386. if (start == Foc_Start) {
  387. sys_debug("start mpta cali\n");
  388. mc_set_foc_mode(CTRL_MODE_CURRENT);
  389. PMSM_FOC_MTPA_Calibrate(true);
  390. }else {
  391. PMSM_FOC_MTPA_Calibrate(false);
  392. mc_set_foc_mode(CTRL_MODE_TRQ);
  393. }
  394. break;
  395. }
  396. case Foc_Set_IS_Curr_Angle:
  397. {
  398. if (command->len != 4) {
  399. erroCode = FOC_Param_Err;
  400. }else {
  401. s16 is_curr = decode_s16((u8 *)command->data);
  402. s16 is_angle = decode_s16((u8 *)command->data + 2);
  403. sys_debug("curr %d, angle %d\n", is_curr, is_angle);
  404. PMSM_FOC_Set_Current(is_curr);
  405. PMSM_FOC_Set_Angle(is_angle);
  406. }
  407. break;
  408. }
  409. case Foc_Set_Plot_Type:
  410. {
  411. plot_type = (int)decode_u8((u8 *)command->data);
  412. sys_debug("plot type %d\n", plot_type);
  413. break;
  414. }
  415. case Foc_Set_Throttle_throld:
  416. {
  417. if (mc_is_start()) {
  418. erroCode = FOC_NotAllowed;
  419. }else {
  420. u16 start = decode_u16((u8 *)command->data);
  421. u16 end = decode_u16((u8 *)command->data + 2);
  422. nv_get_foc_params()->n_startThroVol = (float)start/100.0f;
  423. nv_get_foc_params()->n_endThroVol = (float)end/100.0f;
  424. nv_save_foc_params();
  425. }
  426. break;
  427. }
  428. case Foc_Get_Config:
  429. {
  430. int config_len = 128;
  431. int len = 0;
  432. u8 *config = os_alloc(config_len);
  433. if (config == NULL) {
  434. erroCode = FOC_MEM_Err;
  435. break;
  436. }
  437. config[0] = command->cmd;
  438. config[1] = CAN_MY_ADDRESS;
  439. config[2] = 0;
  440. len = 3;
  441. encode_s16(config+len, (s16)nv_get_foc_params()->s_PhaseCurrLim);
  442. len += 2;
  443. encode_s16(config+len, (s16)nv_get_foc_params()->s_maxDCVol);
  444. len += 2;
  445. encode_s16(config+len, (s16)nv_get_foc_params()->s_minDCVol);
  446. len += 2;
  447. encode_s16(config+len, (s16)nv_get_foc_params()->s_maxRPM);
  448. len += 2;
  449. encode_s16(config+len, (s16)nv_get_foc_params()->s_maxEpmRPM);
  450. len += 2;
  451. encode_s16(config+len, (s16)nv_get_foc_params()->s_maxEpmTorqueLim);
  452. len += 2;
  453. encode_s16(config+len, (s16)nv_get_foc_params()->s_maxTorque);
  454. len += 2;
  455. encode_s16(config+len, (s16)nv_get_foc_params()->s_TorqueBrkLim);
  456. len += 2;
  457. encode_s16(config+len, (s16)nv_get_foc_params()->s_iDCeBrkLim);
  458. len += 2;
  459. encode_s16(config+len, (s16)nv_get_foc_params()->s_LimitiDC);
  460. len += 2;
  461. encode_s16(config+len, (s16)(nv_get_foc_params()->n_startThroVol * 100.0f));
  462. len += 2;
  463. encode_s16(config+len, (s16)(nv_get_foc_params()->n_endThroVol * 100.0f));
  464. len += 2;
  465. encode_u8(config+len, (u8)nv_get_foc_params()->n_brkShutPower);
  466. len += 1;
  467. encode_u8(config+len, (u8)nv_get_foc_params()->n_autoHold);
  468. len += 1;
  469. encode_u32(config+len, nv_get_foc_params()->n_dec_time);
  470. len += 4;
  471. encode_u32(config+len, nv_get_foc_params()->n_ebrk_time);
  472. len += 4;
  473. can_send_response(command->can_src, config, len);
  474. os_free(config);
  475. return;
  476. }
  477. case Foc_Set_Config:
  478. {
  479. if (mc_is_start()) {
  480. erroCode = FOC_NotAllowed;
  481. }else if (command->len < 24) {
  482. erroCode = FOC_Param_Err;
  483. }else {
  484. nv_get_foc_params()->s_TorqueBrkLim = decode_s16((u8 *)command->data);
  485. nv_get_foc_params()->s_iDCeBrkLim = decode_s16((u8 *)command->data + 2);
  486. nv_get_foc_params()->s_LimitiDC = decode_s16((u8 *)command->data + 4);
  487. nv_get_foc_params()->n_startThroVol = (float)decode_s16((u8 *)command->data + 6)/100.0f;
  488. nv_get_foc_params()->n_endThroVol = (float)decode_s16((u8 *)command->data + 8)/100.0f;
  489. nv_get_foc_params()->s_maxEpmRPM = decode_s16((u8 *)command->data + 10);
  490. nv_get_foc_params()->s_maxEpmTorqueLim = decode_s16((u8 *)command->data + 12);
  491. nv_get_foc_params()->n_brkShutPower = decode_u8((u8 *)command->data + 14);
  492. nv_get_foc_params()->n_autoHold = decode_u8((u8 *)command->data + 15);
  493. nv_get_foc_params()->n_dec_time = decode_u32((u8 *)command->data + 16);
  494. nv_get_foc_params()->n_ebrk_time = decode_u32((u8 *)command->data + 20);
  495. nv_save_foc_params();
  496. shark_timer_post(&_reboot_timer, 200);
  497. }
  498. break;
  499. }
  500. case Foc_Fan_Duty:
  501. {
  502. u8 duty = decode_u8(command->data);
  503. mc_set_fan_duty(duty);
  504. break;
  505. }
  506. case Foc_Set_eBrake_Throld:
  507. {
  508. if (command->len >= 4) {
  509. float phase_curr = (float)decode_s16((u8 *)command->data);
  510. float dc_curr = (float)decode_s16((u8 *)command->data + 2);
  511. PMSM_FOC_SetEbrkTorque(phase_curr, dc_curr);
  512. }
  513. break;
  514. }
  515. case Foc_Use_SensorLess_Angle:
  516. {
  517. bool sensorless = decode_u8((u8 *)command->data)?true:false;
  518. if (sensorless && mc_is_start() && PMSM_FOC_GetSpeed() >= CONFIG_SMO_MIN_SPEED) {
  519. sys_debug("use smo %d\n", sensorless);
  520. foc_observer_use_smo(sensorless);
  521. }else {
  522. sys_debug("unuse smo\n");
  523. foc_observer_use_smo(false);
  524. }
  525. break;
  526. }
  527. case Foc_Set_Limiter_Config:
  528. {
  529. sys_debug("limter %d\n", command->len);
  530. if (!nv_set_limit_config((u8 *)command->data, command->len)) {
  531. erroCode = FOC_Param_Err;
  532. }
  533. break;
  534. }
  535. case Foc_Get_Limiter_Config:
  536. {
  537. int config_len;
  538. u8 *config = nv_get_limit_config(&config_len);
  539. u8 *data = os_alloc(config_len + 3);
  540. data[0] = command->cmd;
  541. data[1] = CAN_MY_ADDRESS;
  542. data[2] = 0;
  543. memcpy(data + 3, config, config_len);
  544. can_send_response(command->can_src, data, config_len+3);
  545. os_free(data);
  546. return;
  547. }
  548. case Foc_SN_Write:
  549. {
  550. if (command->len < 18) {
  551. erroCode = FOC_Param_Err;
  552. }else{
  553. erroCode = nv_write_sn((u8 *)command->data, command->len)>0?0:1 ;
  554. }
  555. break;
  556. }
  557. case Foc_SN_Read:
  558. {
  559. if (nv_read_sn(response + 3, 18) == 0) {
  560. memset(response + 3, '0', 18);
  561. }
  562. len += 18;
  563. break;
  564. }
  565. default:
  566. {
  567. erroCode = FOC_Unknow_Cmd;
  568. break;
  569. }
  570. }
  571. cmd_end:
  572. sys_debug("err = %d\n", erroCode);
  573. response[0] = command->cmd;
  574. response[1] = CAN_MY_ADDRESS;
  575. response[2] = erroCode;
  576. can_send_response(command->can_src, response, len);
  577. }
  578. static void _reboot_timer_handler(shark_timer_t *t) {
  579. system_reboot();
  580. }