soft_flash.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. #include "soft_flash.h"
  2. #include "auto_pilot.h"
  3. #include "bsp_V8M_flash.h"
  4. #include "control_rate.h"
  5. #include "control_throttle.h"
  6. #include "data_save.h"
  7. #include "gcs_vklink_v30.h"
  8. #include "geomatry.h"
  9. #include "my_math.h"
  10. #include "params.h"
  11. #include "pilot_navigation.h"
  12. #include "soft_delay.h"
  13. #include "soft_gs.h"
  14. #include "soft_motor_output.h"
  15. #include "soft_port_uart4.h"
  16. #include "soft_rc_input.h"
  17. #include "soft_time.h"
  18. #include "ver_config.h"
  19. #include "bsp_V8M_YY_pwm.h"
  20. #include "hard_system.h"
  21. #include <string.h>
  22. #include "test.h"
  23. #ifdef GD25Q16_FLASH
  24. #include "hard_flash_gd25q16.h"
  25. #else
  26. #include "hard_flash_at45db.h"
  27. #endif
  28. /**
  29. * @brief flash 接口初始化
  30. *
  31. */
  32. void flash_at45db_initial(void) { flash_at45db_init(); }
  33. /**
  34. * @brief 检查 flash 是否挂载
  35. *
  36. * @return true 已挂载
  37. * @return false 不存在
  38. */
  39. bool flash_isexist(void)
  40. {
  41. if (AT45DB_Check() == 0)
  42. return false;
  43. else
  44. return true;
  45. }
  46. /**
  47. * @brief 检查 flash 是否正在忙
  48. *
  49. * @return true 正在忙
  50. * @return false 可用
  51. */
  52. bool flash_isbusy(void)
  53. {
  54. if (AT45DB_IS_BUSY() == 0)
  55. return true;
  56. else
  57. return false;
  58. }
  59. /**
  60. * @brief flash 读取指定地址的 n bytes 数据
  61. *
  62. * @param add 要读取的地址
  63. * @param pdata 读出数据存放的 buffer 指针
  64. * @param len 读取的字节数
  65. */
  66. void flash_read_bytes(unsigned int add, unsigned char *pdata,
  67. unsigned short len)
  68. {
  69. AT45DB_Read_Bytes(add, pdata, len);
  70. }
  71. /**
  72. * @brief flash 指定地址写入 n bytes 的数据
  73. *
  74. * @param add 要写入的地址
  75. * @param pdata 指向待写入的数据的指针
  76. * @param len 待写入的字节数
  77. */
  78. void flash_write_bytes(unsigned int add, unsigned char *pdata,
  79. unsigned short len)
  80. {
  81. AT45DB_Write_Bytes(add, pdata, len);
  82. }
  83. /**
  84. * @brief flash 读取一个 int16 数据
  85. *
  86. * @param add 读取的地址
  87. * @return short 读取的数据
  88. */
  89. short flash_read_int16(unsigned int add) { return AT45DB_Read_int16(add); }
  90. /**
  91. * @brief flash 写入一个 int16 数据
  92. *
  93. * @param add 写入的地址
  94. * @param wvalue 待写入的数据
  95. */
  96. void flash_write_int16(unsigned int add, short wvalue)
  97. {
  98. AT45DB_Write_int16(add, wvalue);
  99. }
  100. void flash_read_page(uint16_t Page_Add, uint8_t *pdata)
  101. {
  102. AT45DB_ReadPage(Page_Add, pdata);
  103. }
  104. void flash_write_page(uint16_t page, uint8_t *Data)
  105. {
  106. AT45DB_WritePage(page, Data);
  107. }
  108. void flash_write_bytes_ononepage(uint16_t page, uint8_t *pdata, uint16_t len)
  109. {
  110. AT45DB_WriteBytes_OnOnePage(page, pdata, len);
  111. }
  112. // @brief 装载默认的 PID 参数
  113. void load_default_pidinf(void)
  114. {
  115. unsigned char i = 0;
  116. pidinf._pid_flag = (int16_t)0xA12A;
  117. pidinf._Roll_Angle_P = 450;
  118. pidinf._Roll_Gyro_P = 60;
  119. pidinf._Roll_Gyro_I = 5;
  120. pidinf._Roll_Gyro_D = 5;
  121. pidinf._Roll_Gyro_Imax = 150;
  122. pidinf._Pitch_Angle_P = 450;
  123. pidinf._Pitch_Gyro_P = 60;
  124. pidinf._Pitch_Gyro_I = 5;
  125. pidinf._Pitch_Gyro_D = 5;
  126. pidinf._Pitch_Gyro_Imax = 150;
  127. pidinf._Yaw_Angle_P = 450;
  128. pidinf._Yaw_Gyro_P = 350;
  129. pidinf._Yaw_Gyro_I = 10;
  130. pidinf._Yaw_Gyro_D = 0;
  131. pidinf._Yaw_Gyro_Imax = 150;
  132. pidinf._AltHold_Dist_P = 60;
  133. pidinf._AltHold_Speed_P = 350;
  134. pidinf._AltHold_Acc_P = 25;
  135. pidinf._AltHold_Acc_I = 15;
  136. pidinf._AltHold_Acc_Imax = 200;
  137. pidinf._Loiter_Dist_P = 70;
  138. pidinf._Loiter_Speed_P = 120;
  139. pidinf._Loiter_Acc_P = 40;
  140. pidinf._Loiter_Acc_I = 20;
  141. pidinf._Loiter_Brake_Gyro = 20;
  142. for (i = 0; i < RC_CALIB_CH_NUM; i++)
  143. {
  144. pidinf._rc_cal_offset[i] = 1500; // 初始中立值 1500
  145. pidinf._rc_cal_factor_up[i] = 1000; // 初始补偿因数1000 /1000 = 1
  146. pidinf._rc_cal_factor_down[i] = 1000; // 初始补偿因数1000 /1000 = 1
  147. }
  148. }
  149. // @brief flash 读取 pid 参数信息
  150. void flash_read_pidinf(void)
  151. {
  152. unsigned char i = 0;
  153. // unsigned int testi[2] = {0};
  154. // testi[0] = 0x12345678;
  155. // testi[1] = 0x12345678;
  156. // flash_write_bytes(pidinf_addr, (uint8_t*)&testi, sizeof(testi));
  157. flash_read_bytes(pidinf_addr, (uint8_t *)&pidinf, sizeof(pidinf));
  158. if (pidinf._pid_flag != (int16_t)0xA12A)
  159. { // 配置表是否有效?
  160. load_default_pidinf(); // 装载默认的配置,并写入Flash
  161. flash_write_bytes(pidinf_addr, (uint8_t *)&pidinf, sizeof(pidinf));
  162. }
  163. pid_v_roll.angle_p = pidinf._Roll_Angle_P / 100.0f; // 1.2
  164. pid_v_roll.gyro_p = pidinf._Roll_Gyro_P / 100.0f; // 4.5
  165. pid_v_roll.gyro_i = 0.05f;
  166. pid_v_roll.gyro_d = pidinf._Roll_Gyro_D / 100.0f; // 0.06
  167. pid_v_roll.gyro_imax = 150.0f;
  168. pid_v_pitch.angle_p = pidinf._Pitch_Angle_P / 100.0f;
  169. pid_v_pitch.gyro_p = pidinf._Pitch_Gyro_P / 100.0f;
  170. pid_v_pitch.gyro_i = 0.05f;
  171. pid_v_pitch.gyro_d = pidinf._Pitch_Gyro_D / 100.0f;
  172. pid_v_pitch.gyro_imax = 150.0f;
  173. pid_v_yaw.angle_p = pidinf._Yaw_Angle_P / 100.0f;
  174. pid_v_yaw.gyro_p = pidinf._Yaw_Gyro_P / 100.0f;
  175. pid_v_yaw.gyro_i = 0.10f;
  176. pid_v_yaw.gyro_d = pidinf._Yaw_Gyro_D / 100.0f;
  177. pid_v_yaw.gyro_imax = 150.0f;
  178. pid_v_alt.dist_p = pidinf._AltHold_Dist_P / 100.0f;
  179. pid_v_alt.speed_p = pidinf._AltHold_Speed_P / 100.0f;
  180. pid_v_alt.acc_p = pidinf._AltHold_Acc_P / 100.0f;
  181. pid_v_alt.acc_i = pidinf._AltHold_Acc_I / 100.0f;
  182. pid_v_alt.brake_gyro = pidinf._AltHold_Acc_Imax / 10.0f;
  183. pid_v_pos.dist_p = pidinf._Loiter_Dist_P / 100.0f;
  184. pid_v_pos.speed_p = pidinf._Loiter_Speed_P / 100.0f;
  185. pid_v_pos.acc_p = pidinf._Loiter_Acc_P / 100.0f;
  186. pid_v_pos.acc_i = pidinf._Loiter_Acc_I / 100.0f;
  187. pidinf._Loiter_Brake_Gyro =
  188. constrain_int16(pidinf._Loiter_Brake_Gyro, 10, 50);
  189. pid_v_pos.brake_gyro = pidinf._Loiter_Brake_Gyro / 10.0f;
  190. // 如果中立位值超过范围,视为错误,恢复默认值
  191. if (((pidinf._rc_cal_offset[RC_ROLL] > 1800) ||
  192. (pidinf._rc_cal_offset[RC_ROLL] < 1200)) ||
  193. ((pidinf._rc_cal_offset[RC_PITCH] > 1800) ||
  194. (pidinf._rc_cal_offset[RC_PITCH] < 1200)) ||
  195. ((pidinf._rc_cal_offset[RC_THR] > 1800) ||
  196. (pidinf._rc_cal_offset[RC_THR] < 1200)) ||
  197. ((pidinf._rc_cal_offset[RC_YAW] > 1800) ||
  198. (pidinf._rc_cal_offset[RC_YAW] < 1200)))
  199. {
  200. for (i = 0; i < RC_CALIB_CH_NUM; i++)
  201. {
  202. rc_cal_offset[i] = 1500;
  203. rc_cal_factor_up[i] = 1000;
  204. rc_cal_factor_down[i] = 1000;
  205. }
  206. }
  207. else
  208. {
  209. for (i = 0; i < RC_CALIB_CH_NUM; i++)
  210. {
  211. rc_cal_offset[i] = pidinf._rc_cal_offset[i];
  212. rc_cal_factor_up[i] = pidinf._rc_cal_factor_up[i];
  213. rc_cal_factor_down[i] = pidinf._rc_cal_factor_down[i];
  214. }
  215. }
  216. }
  217. // @brief flash 写入 pid 信息发
  218. void flash_write_pidinf(void)
  219. {
  220. unsigned char i = 0;
  221. pidinf._pid_flag = (int16_t)0xA12A; // 数据有效标志
  222. //================数据赋值部分=============
  223. pid_v_roll.angle_p = pidinf._Roll_Angle_P / 100.0f;
  224. pid_v_roll.gyro_p = pidinf._Roll_Gyro_P / 100.0f;
  225. pid_v_roll.gyro_i = 0.05f;
  226. pid_v_roll.gyro_d = pidinf._Roll_Gyro_D / 100.0f;
  227. pid_v_roll.gyro_imax = 150.0f;
  228. pid_v_pitch.angle_p = pidinf._Pitch_Angle_P / 100.0f;
  229. pid_v_pitch.gyro_p = pidinf._Pitch_Gyro_P / 100.0f;
  230. pid_v_pitch.gyro_i = 0.05f;
  231. pid_v_pitch.gyro_d = pidinf._Pitch_Gyro_D / 100.0f;
  232. pid_v_pitch.gyro_imax = 150.0f;
  233. pid_v_yaw.angle_p = pidinf._Yaw_Angle_P / 100.0f;
  234. pid_v_yaw.gyro_p = pidinf._Yaw_Gyro_P / 100.0f;
  235. pid_v_yaw.gyro_i = 0.10f;
  236. pid_v_yaw.gyro_d = pidinf._Yaw_Gyro_D / 100.0f;
  237. pid_v_yaw.gyro_imax = 150.0f;
  238. pid_v_alt.dist_p = pidinf._AltHold_Dist_P / 100.0f;
  239. pid_v_alt.speed_p = pidinf._AltHold_Speed_P / 100.0f;
  240. pid_v_alt.acc_p = pidinf._AltHold_Acc_P / 100.0f;
  241. pid_v_alt.acc_i = pidinf._AltHold_Acc_I / 100.0f;
  242. pid_v_alt.brake_gyro = pidinf._AltHold_Acc_Imax / 10.0f;
  243. pid_v_pos.dist_p = pidinf._Loiter_Dist_P / 100.0f;
  244. pid_v_pos.speed_p = pidinf._Loiter_Speed_P / 100.0f;
  245. pid_v_pos.acc_p = pidinf._Loiter_Acc_P / 100.0f;
  246. pid_v_pos.acc_i = pidinf._Loiter_Acc_I / 100.0f;
  247. pidinf._Loiter_Brake_Gyro =
  248. constrain_int16(pidinf._Loiter_Brake_Gyro, 10, 50);
  249. pid_v_pos.brake_gyro = pidinf._Loiter_Brake_Gyro / 10.0f;
  250. //====================================================
  251. for (i = 0; i < RC_CALIB_CH_NUM; i++)
  252. {
  253. pidinf._rc_cal_offset[i] = rc_cal_offset[i];
  254. pidinf._rc_cal_factor_up[i] = rc_cal_factor_up[i];
  255. pidinf._rc_cal_factor_down[i] = rc_cal_factor_down[i];
  256. }
  257. if (thr_lock_status == LOCKED)
  258. {
  259. flash_write_bytes(pidinf_addr, (uint8_t *)&pidinf, sizeof(pidinf));
  260. }
  261. else
  262. {
  263. return;
  264. }
  265. }
  266. // @brief 装载默认的版本信息
  267. void load_default_verinf(void)
  268. {
  269. verinf._ver_flag = (int16_t)0xA11A;
  270. memcpy(verinf._ap_name, NM_VER, sizeof(verinf._ap_name));
  271. verinf._serial_id = 001;
  272. verinf._sysid = 0;
  273. }
  274. // @brief flash 读取版本信息
  275. void flash_read_verinf(void)
  276. {
  277. flash_read_bytes(verinf_addr, (uint8_t *)&verinf, sizeof(verinf));
  278. if (verinf._ver_flag != (int16_t)0xA11A)
  279. { // 配置表是否有效?
  280. load_default_verinf(); // 装载默认的配置,并写入Flash
  281. flash_write_bytes(verinf_addr, (uint8_t *)&verinf, sizeof(verinf));
  282. }
  283. memcpy(ver_par.ap_name, verinf._ap_name, sizeof(verinf._ap_name));
  284. ver_par.serial_id = verinf._serial_id;
  285. ver_par.hardware_id = HW_VER;
  286. ver_par.firmware_id = FW_VER;
  287. }
  288. // @brief flash 写入版本信息
  289. void flash_write_verinf(void)
  290. {
  291. verinf._ver_flag = (int16_t)0xA11A; // 数据有效标志
  292. //==================数据赋值部分===============
  293. memcpy(ver_par.ap_name, verinf._ap_name, sizeof(verinf._ap_name));
  294. ver_par.serial_id = verinf._serial_id;
  295. ver_par.hardware_id = HW_VER;
  296. ver_par.firmware_id = FW_VER;
  297. //==============================================
  298. flash_write_bytes(verinf_addr, (uint8_t *)&verinf, sizeof(verinf));
  299. }
  300. // @brief 装载默认的机型配置信息
  301. void load_default_confinf(void)
  302. {
  303. confinf._conf_flag = (int16_t)0xA14A;
  304. confinf._jixing = 42; // 默认机型为4旋翼X型
  305. confinf._lowvolt_flag = 2; // 默认一级电压保护返航
  306. confinf._lowvolt_value = 111; // 11.1V
  307. confinf._lowvolt_flag2 = 3;
  308. confinf._lowvolt_value2 = 0;
  309. confinf._lostlink_time = 0; // 默认不开启,失去联系 返航 单位s
  310. confinf._circle_radius = 12; // 默认盘旋半径12m
  311. confinf._takeoff_alt = 5; // 默认起飞高度5m
  312. confinf._rth_alt = 10; // 默认返航高度10m
  313. confinf._mag_offset = 0; // 默认磁偏角补偿0
  314. confinf._enable_infrared = 10; // 默认雷达灵敏度10
  315. confinf._idle_speed_level = 2; // 默认怠速2档 1150
  316. confinf._port4_af_function = 0; // 默认串口 4 复用功能透传
  317. confinf._rc_fail_loiter_time = 10; // 默认遥控器失控后的悬停时间 10s
  318. }
  319. // @brief flash 读取机型配置信息
  320. void flash_read_confinf(void)
  321. {
  322. flash_read_bytes(confinf_addr, (uint8_t *)&confinf, sizeof(confinf));
  323. if (confinf._conf_flag != (int16_t)0xA14A)
  324. { // 配置表是否有效?
  325. load_default_confinf(); // 装载默认的配置,并写入Flash
  326. flash_write_bytes(confinf_addr, (uint8_t *)&confinf, sizeof(confinf));
  327. }
  328. conf_par.jixing = confinf._jixing;
  329. conf_par.lowvolt_flag = confinf._lowvolt_flag;
  330. conf_par.lowvolt_value = confinf._lowvolt_value;
  331. conf_par.lostlink_time = confinf._lostlink_time;
  332. conf_par.circle_radius = confinf._circle_radius * 100;
  333. // 环绕半径不小于500cm
  334. if (conf_par.circle_radius < 500)
  335. {
  336. conf_par.circle_radius = 500;
  337. }
  338. conf_par.rth_alt_cm = confinf._rth_alt * 100;
  339. conf_par.takeoff_alt_cm = confinf._takeoff_alt * 100;
  340. conf_par.mag_offset = confinf._mag_offset;
  341. conf_par.enable_infrared = confinf._enable_infrared;
  342. // 根据怠速级别设置怠速数值
  343. if (confinf._idle_speed_level >= 1 && confinf._idle_speed_level <= 5)
  344. conf_par.idle_speed = 1050 + (confinf._idle_speed_level - 1) * 50;
  345. else
  346. {
  347. conf_par.idle_speed = 1100;
  348. confinf._idle_speed_level = 2;
  349. }
  350. conf_par.port4_af_function = confinf._port4_af_function;
  351. // conf_par.dpgs_direction = confinf._dgps_direction;
  352. conf_par.lowvolt_flag2 = confinf._lowvolt_flag2;
  353. conf_par.lowvolt_value2 = confinf._lowvolt_value2;
  354. conf_par.rc_fail_loiter_time = confinf._rc_fail_loiter_time;
  355. }
  356. // @brief flash 写入机型配置信息
  357. void flash_write_confinf(void)
  358. {
  359. confinf._conf_flag = (int16_t)0xA14A; // 数据有效标志
  360. //===============数据赋值部分=======================
  361. conf_par.jixing = confinf._jixing;
  362. conf_par.lowvolt_flag = confinf._lowvolt_flag;
  363. conf_par.lowvolt_value = confinf._lowvolt_value;
  364. conf_par.lostlink_time = confinf._lostlink_time;
  365. conf_par.circle_radius = confinf._circle_radius * 100;
  366. // 环绕半径不小于500cm
  367. if (conf_par.circle_radius < 500)
  368. {
  369. conf_par.circle_radius = 500;
  370. }
  371. conf_par.rth_alt_cm = confinf._rth_alt * 100;
  372. conf_par.takeoff_alt_cm = confinf._takeoff_alt * 100;
  373. conf_par.mag_offset = confinf._mag_offset;
  374. conf_par.enable_infrared = confinf._enable_infrared;
  375. // 根据怠速级别设置怠速数值
  376. if (confinf._idle_speed_level >= 1 && confinf._idle_speed_level <= 5)
  377. conf_par.idle_speed = 1050 + (confinf._idle_speed_level - 1) * 50;
  378. else
  379. {
  380. conf_par.idle_speed = 1100;
  381. confinf._idle_speed_level = 2;
  382. }
  383. // conf_par.center_compensation_x = confinf._center_compensation_x;
  384. // conf_par.center_compensation_y = confinf._center_compensation_y;
  385. // conf_par.center_compensation_z = confinf._center_compensation_z;
  386. conf_par.port4_af_function = confinf._port4_af_function;
  387. // conf_par.dpgs_direction = confinf._dgps_direction;
  388. //====================================================
  389. conf_par.lowvolt_flag2 = confinf._lowvolt_flag2;
  390. conf_par.lowvolt_value2 = confinf._lowvolt_value2;
  391. conf_par.rc_fail_loiter_time = confinf._rc_fail_loiter_time;
  392. flash_write_bytes(confinf_addr, (uint8_t *)&confinf, sizeof(confinf));
  393. }
  394. // @brief flash 写入自定义禁飞区信息
  395. // @param custom_no_fly_zone_total_num 自定义禁飞区总条数
  396. // @param current_no_fly_zone_num 要写入的禁飞区是第几条
  397. // @param current_no_fly_zone_vertex_num 要写入的禁飞区的顶点个数
  398. // @param *pdata 要写入的禁飞区顶点的坐标信息指针
  399. void flash_write_custom_no_fly_zone(uint8_t custom_no_fly_zone_total_num,
  400. uint8_t current_no_fly_zone_num,
  401. uint8_t current_no_fly_zone_vertex_num,
  402. Coords3dint *pdata)
  403. {
  404. static uint8_t write_buffer[SIZE_OF_ONE_NO_FLY_ZONE] = {0};
  405. uint16_t no_fly_zone_useable_flag = (uint16_t)0xACCA;
  406. uint16_t index = 0;
  407. uint32_t write_addr =
  408. NO_FLY_ZONE_ADDR + (current_no_fly_zone_num)*SIZE_OF_ONE_NO_FLY_ZONE;
  409. short2buf(&write_buffer[index], (short *)&no_fly_zone_useable_flag);
  410. index += sizeof(uint16_t);
  411. write_buffer[index++] = custom_no_fly_zone_total_num;
  412. write_buffer[index++] = current_no_fly_zone_vertex_num;
  413. if (current_no_fly_zone_vertex_num <= 8)
  414. {
  415. memcpy(&write_buffer[index], pdata,
  416. current_no_fly_zone_vertex_num * sizeof(Coords3dint));
  417. flash_write_bytes(write_addr, write_buffer, sizeof(write_buffer));
  418. }
  419. }
  420. // @brief flash 读取禁飞区信息
  421. // @param read_no_fly_zone_num 要读取的禁飞区的序号 0 开始数
  422. // @param *custom_no_fly_zone_total_num 读出来的禁飞区总个数
  423. // @param *current_no_fly_zone_vertex_num 读出来的当前禁飞区顶点个数
  424. // @param *pdata 读出来的顶点数据存放地址指针
  425. static bool flash_read_custom_no_fly_zone(
  426. uint8_t read_no_fly_zone_num, uint8_t *custom_no_fly_zone_total_num,
  427. uint8_t *current_no_fly_zone_vertex_num, Coords3dint *pdata)
  428. {
  429. uint32_t read_addr =
  430. NO_FLY_ZONE_ADDR + (read_no_fly_zone_num)*SIZE_OF_ONE_NO_FLY_ZONE;
  431. uint16_t useableFlag;
  432. // 预备存储读取出来的总条数,顶点数
  433. uint8_t totalNum, currentVertexNum;
  434. flash_read_bytes(read_addr, (uint8_t *)&useableFlag, sizeof(useableFlag));
  435. flash_read_bytes(read_addr + sizeof(useableFlag), &totalNum,
  436. sizeof(totalNum));
  437. flash_read_bytes(read_addr + sizeof(useableFlag) + sizeof(totalNum),
  438. &currentVertexNum, sizeof(currentVertexNum));
  439. // 如果存储有效
  440. if (useableFlag == 0xACCA && totalNum <= 5 && currentVertexNum <= 8)
  441. {
  442. // 如果是在读第一个位置存储的禁飞区,则更新一下自定义禁飞区的总条数
  443. if (read_no_fly_zone_num == 0)
  444. {
  445. *custom_no_fly_zone_total_num = totalNum;
  446. }
  447. // 更新一下自定义禁飞区的定点个数
  448. *current_no_fly_zone_vertex_num = currentVertexNum;
  449. // 读出顶点的坐标数据
  450. flash_read_bytes(read_addr + sizeof(useableFlag) + sizeof(totalNum) +
  451. sizeof(currentVertexNum),
  452. (uint8_t *)pdata,
  453. currentVertexNum * sizeof(Coords3dint));
  454. return true;
  455. }
  456. else
  457. {
  458. useableFlag = 0x0000;
  459. flash_write_bytes(read_addr, (uint8_t *)&useableFlag,
  460. sizeof(useableFlag));
  461. return false;
  462. }
  463. }
  464. /**
  465. * @brief 装载默认的 par 参数
  466. */
  467. void load_default_parinf(void)
  468. {
  469. parinf._par_flag = (int16_t)0xAAEE; // 数据有效标志
  470. parinf._par_maxangle = 25; // 最大角度 30 deg
  471. parinf._par_maxhspeed = 80; // GPS 推杆最大水平速度 dm/s
  472. parinf._par_maxupspeed = 40; // GPS 推杆最大上升速度 dm/s
  473. parinf._par_maxdownspeed = 30; // GPS 推杆最大下降速度 dm/s
  474. parinf._par_hover_throttle = 1400; // 默认悬停油门
  475. parinf._par_alt_fs_rth = 2000; // 高度限制,1级保护。返航
  476. parinf._par_alt_fs_land = 2500; // 高度限制,2级保护。迫降
  477. parinf._par_maxyawrate = 30; // 最大自动转航向角速度
  478. parinf._par_volt_offset[0] = 0; // 底板电压采集有压降, 0.3v
  479. parinf._par_volt_offset[1] = 0;
  480. parinf._par_volt_offset[2] = 0;
  481. parinf._par_volt_offset_b[0] = 0; // 底板电压采集有压降, 0.3v
  482. parinf._par_volt_offset_b[1] = 0;
  483. parinf._par_volt_offset_b[2] = 0;
  484. parinf._fllow_dist = 10;
  485. // 自动模式最大爬升速率 dm/s
  486. parinf._par_max_climb_rate_automode = 20;
  487. // 自动模式最大下降目标速率 dm/s
  488. parinf._par_max_approach_rate_automode = 15;
  489. // 自动模式降落最小目标垂直速率 dm/s
  490. parinf._par_min_landing_rate_automode = 4;
  491. // 自动模式巡航最大水平速率 dm/s
  492. parinf._par_max_rth_horizontal_speed = 80;
  493. // 电压校准系数
  494. parinf._par_volt_kp[0] = 1000;
  495. parinf._par_volt_kp[1] = 1000;
  496. parinf._par_volt_kp[2] = 1000;
  497. // 最大水平加加速度
  498. parinf._max_hor_jet = 15;
  499. parinf._bms_low_capacity_percentage = 30;
  500. parinf._motor_failsafe_action = 0;
  501. }
  502. /**
  503. * @brief 从 flash 读取 par 参数
  504. */
  505. void flash_read_parinf(void)
  506. {
  507. flash_read_bytes(parinf_addr, (uint8_t *)&parinf, sizeof(parinf));
  508. if (parinf._par_flag != (int16_t)0xAAEE)
  509. { // 配置表是否有效?
  510. load_default_parinf(); // 装载默认的配置,并写入Flash
  511. flash_write_bytes(parinf_addr, (uint8_t *)&parinf, sizeof(parinf));
  512. }
  513. //=============数据赋值部分================
  514. if (params_get_value(ParamNum_APMaxTilteAngleDeg) < 15)
  515. {
  516. params_set_value(ParamNum_APMaxTilteAngleDeg, 15);
  517. }
  518. else if (params_get_value(ParamNum_APMaxTilteAngleDeg) > 45)
  519. {
  520. params_set_value(ParamNum_APMaxTilteAngleDeg, 45);
  521. }
  522. if (parinf._par_maxhspeed < 30)
  523. {
  524. parinf._par_maxhspeed = 30;
  525. }
  526. else if (parinf._par_maxhspeed > 150)
  527. {
  528. parinf._par_maxhspeed = 150;
  529. }
  530. // 对跟随距离做限制
  531. if (parinf._fllow_dist < 3)
  532. {
  533. parinf._fllow_dist = 3;
  534. }
  535. else if (parinf._fllow_dist > 100)
  536. {
  537. parinf._fllow_dist = 100;
  538. }
  539. // 最大自动目标爬升速率在 1~5 m/s
  540. if (parinf._par_max_climb_rate_automode < 10)
  541. {
  542. parinf._par_max_climb_rate_automode = 10;
  543. }
  544. else if (parinf._par_max_climb_rate_automode > 50)
  545. {
  546. parinf._par_max_climb_rate_automode = 50;
  547. }
  548. // 最大自动目标下降速率在 1~5 m/s
  549. if (parinf._par_max_approach_rate_automode < 10)
  550. {
  551. parinf._par_max_approach_rate_automode = 10;
  552. }
  553. else if (parinf._par_max_approach_rate_automode > 50)
  554. {
  555. parinf._par_max_approach_rate_automode = 50;
  556. }
  557. // 最小目标降落速度在 0.2 ~ 0.5 m/s
  558. if (parinf._par_min_landing_rate_automode < 2)
  559. {
  560. parinf._par_min_landing_rate_automode = 2;
  561. }
  562. else if (parinf._par_min_landing_rate_automode > 5)
  563. {
  564. parinf._par_min_landing_rate_automode = 5;
  565. }
  566. // 最大目标航线水平速度在 10~20 m/s
  567. if (parinf._par_max_rth_horizontal_speed < 20 ||
  568. parinf._par_max_rth_horizontal_speed > 300)
  569. {
  570. parinf._par_max_rth_horizontal_speed = 80;
  571. }
  572. if (parinf._max_hor_jet < 4 || parinf._max_hor_jet > 50)
  573. {
  574. parinf._max_hor_jet = 50;
  575. }
  576. //=========================================
  577. }
  578. /**************************实现函数********************************************
  579. *函数原型: void flash_write_jfqinf(void)
  580. *功  能: 将当前的拍照参数设置保存到Flash,永久保存
  581. *******************************************************************************/
  582. void flash_write_parinf(void)
  583. {
  584. parinf._par_flag = (int16_t)0xAAEE; // 数据有效标志
  585. //=============数据赋值部分================
  586. if (params_get_value(ParamNum_APMaxTilteAngleDeg) < 15)
  587. {
  588. params_set_value(ParamNum_APMaxTilteAngleDeg, 15);
  589. }
  590. else if (params_get_value(ParamNum_APMaxTilteAngleDeg) > 45)
  591. {
  592. params_set_value(ParamNum_APMaxTilteAngleDeg, 45);
  593. }
  594. if (parinf._par_maxhspeed < 30)
  595. {
  596. parinf._par_maxhspeed = 30;
  597. }
  598. else if (parinf._par_maxhspeed > 150)
  599. {
  600. parinf._par_maxhspeed = 150;
  601. }
  602. // 对跟随距离做限制
  603. if (parinf._fllow_dist < 3)
  604. {
  605. parinf._fllow_dist = 3;
  606. }
  607. else if (parinf._fllow_dist > 100)
  608. {
  609. parinf._fllow_dist = 100;
  610. }
  611. // 最大自动目标爬升速率在 1~5 m/s
  612. if (parinf._par_max_climb_rate_automode < 10)
  613. {
  614. parinf._par_max_climb_rate_automode = 10;
  615. }
  616. else if (parinf._par_max_climb_rate_automode > 50)
  617. {
  618. parinf._par_max_climb_rate_automode = 50;
  619. }
  620. // 最大自动目标下降速率在 1~5 m/s
  621. if (parinf._par_max_approach_rate_automode < 10)
  622. {
  623. parinf._par_max_approach_rate_automode = 10;
  624. }
  625. else if (parinf._par_max_approach_rate_automode > 50)
  626. {
  627. parinf._par_max_approach_rate_automode = 50;
  628. }
  629. // 最小目标降落速度在 0.2 ~ 0.5 m/s
  630. if (parinf._par_min_landing_rate_automode < 2)
  631. {
  632. parinf._par_min_landing_rate_automode = 2;
  633. }
  634. else if (parinf._par_min_landing_rate_automode > 5)
  635. {
  636. parinf._par_min_landing_rate_automode = 5;
  637. }
  638. // 最大目标航线水平速度在 10~20 m/s
  639. if (parinf._par_max_rth_horizontal_speed < 20 ||
  640. parinf._par_max_rth_horizontal_speed > 300)
  641. {
  642. parinf._par_max_rth_horizontal_speed = 80;
  643. }
  644. if (parinf._max_hor_jet < 4 || parinf._max_hor_jet > 50)
  645. {
  646. parinf._max_hor_jet = 50;
  647. }
  648. flash_write_bytes(parinf_addr, (uint8_t *)&parinf, sizeof(parinf));
  649. }
  650. /**************************实现函数********************************************
  651. *函数原型: void initial_parameters(void)
  652. *功  能: 初始化四轴的数据参数。
  653. 读取PID参数,以便在四轴控制中进行运算
  654. *******************************************************************************/
  655. void initial_parameters(void)
  656. {
  657. // 读取PID参数
  658. flash_read_pidinf();
  659. // 读取飞控版本信息
  660. flash_read_verinf();
  661. // 读取机型配置信息
  662. flash_read_confinf();
  663. // 读取备用参数信息
  664. flash_read_parinf();
  665. }
  666. /* 需要记录参数的标志位 */
  667. bool write_pid_information = false, write_conf_information = false,
  668. write_cam_information = false, write_ver_information = false,
  669. write_breakpoint_information = false,
  670. write_par_information = false, write_pos_information = false,
  671. write_postotal_information = false, write_iap_flag = false,
  672. write_rcfactor_flag = false, write_custom_noflyzone = false;
  673. /**
  674. * @brief 参数写入 flash
  675. *
  676. */
  677. void write_par_to_flash(void)
  678. {
  679. if (thr_lock_status != LOCKED)
  680. {
  681. if (write_pid_information == true) // 写PID参数信息
  682. {
  683. // FLASH忙则先返回。
  684. flash_write_pidinf();
  685. }
  686. return;
  687. }
  688. if (write_pid_information == true) // 写PID参数信息
  689. {
  690. // FLASH忙则先返回。
  691. if (flash_isbusy())
  692. return;
  693. flash_write_pidinf();
  694. write_pid_information = false;
  695. }
  696. else if (write_ver_information == true) // 写版本信息
  697. {
  698. // FLASH忙则先返回。
  699. if (flash_isbusy())
  700. return;
  701. flash_write_verinf();
  702. write_ver_information = false;
  703. }
  704. else if (write_conf_information == true) // 写机型配置信息
  705. {
  706. // FLASH忙则先返回。
  707. if (flash_isbusy())
  708. return;
  709. flash_write_confinf();
  710. write_conf_information = false;
  711. }
  712. else if (write_par_information == true) // 写备用参数信息
  713. {
  714. // FLASH忙则先返回。
  715. if (flash_isbusy())
  716. return;
  717. flash_write_parinf();
  718. write_par_information = false;
  719. }
  720. else if (write_rcfactor_flag == true) // 写遥控器校准系数信息
  721. {
  722. // 计算遥控器补偿的系数
  723. calc_rcfactor_calibration();
  724. // 写入flash
  725. flash_write_pidinf();
  726. delay_ms(200);
  727. write_rcfactor_flag = false;
  728. pilot_mode = PILOT_NORMAL;
  729. }
  730. else if (write_iap_flag == true) // 写IAP升级信息
  731. {
  732. switch (ver_par.hardware_id)
  733. {
  734. case HW_V8M_YY:
  735. V8M_record_iap_flag();
  736. break;
  737. default:
  738. return;
  739. }
  740. write_iap_flag = false;
  741. // 升级前失能电平芯片输出,PWM脉宽给0
  742. PWM_IS_DISABLE();
  743. for (uint8_t i = 1; i <= params_get_value(ParamNum_APType) / 10; i++)
  744. {
  745. set_motor_pwm(i, 0);
  746. }
  747. delay_ms(200);
  748. // 软件复位
  749. sys_reset();
  750. }
  751. }
  752. //------------------End of File----------------------------
  753. #ifdef SOFT_FLASH_TEST
  754. //============================================================================
  755. // GD25Q16 芯片参数
  756. //============================================================================
  757. #define GD25Q16_TOTAL_SIZE (2 * 1024 * 1024) // 2MB
  758. #define GD25Q16_PAGE_SIZE 256 // 页大小 256 字节
  759. #define GD25Q16_SECTOR_SIZE (4 * 1024) // 扇区大小 4KB
  760. #define GD25Q16_BLOCK_SIZE (64 * 1024) // 块大小 64KB
  761. #define GD25Q16_TOTAL_PAGES (GD25Q16_TOTAL_SIZE / GD25Q16_PAGE_SIZE) // 8192 页
  762. #define GD25Q16_TOTAL_SECTORS (GD25Q16_TOTAL_SIZE / GD25Q16_SECTOR_SIZE) // 512 扇区
  763. //============================================================================
  764. // 根据您的地址划分重新计算(基于实际地址,不是页索引)
  765. //============================================================================
  766. // 您的地址定义(基于字节地址)
  767. #define IAP_ADDR (512 * 5) // 0x00000A00
  768. #define pidinf_addr (512 * 16) // 0x00002000
  769. #define verinf_addr (512 * 24) // 0x00003000
  770. #define confinf_addr (512 * 32) // 0x00004000
  771. #define caminf_addr (512 * 40) // 0x00005000
  772. #define parinf_addr (512 * 48) // 0x00006000
  773. #define POS_TOTAL_ADDR (512 * 56) // 0x00007000
  774. #define NO_FLY_ZONE_ADDR (512 * 64) // 0x00008000
  775. #define FPALTCHANGE_ADDR (512 * 80) // 0x0000A000
  776. #define BREAKPOINT_ADDR (512 * 88) // 0x0000B000
  777. #define WAYPOINT_ADDR (512 * 96) // 0x0000C000
  778. #define POS_START_PAGE 200 // 页索引 200 = 0x0000C800? 需要重新计算
  779. //============================================================================
  780. // 测试区域定义 - 使用 Flash 末尾的安全区域(最后 256KB)
  781. //============================================================================
  782. #define TEST_AREA_START (GD25Q16_TOTAL_SIZE - 256 * 1024) // 最后 256KB (0x1C0000)
  783. #define TEST_AREA_SIZE (256 * 1024) // 256KB 测试空间
  784. #define TEST_AREA_END (TEST_AREA_START + TEST_AREA_SIZE)
  785. // 测试地址验证
  786. #define IS_TEST_ADDR(addr) \
  787. ((addr) >= TEST_AREA_START && (addr) < TEST_AREA_END)
  788. //============================================================================
  789. // 全局测试变量
  790. //============================================================================
  791. static bool test_passed = true;
  792. static uint32_t test_count = 0;
  793. static uint32_t fail_count = 0;
  794. // 缓冲区
  795. static uint8_t g_test_buffer[GD25Q16_PAGE_SIZE * 4]; // 4页缓冲区
  796. //============================================================================
  797. // 辅助函数
  798. //============================================================================
  799. /**
  800. * @brief 打印测试结果
  801. */
  802. static void print_result(const char* test_name, bool passed)
  803. {
  804. if (passed) {
  805. printf("[PASS] %s\n", test_name);
  806. test_count++;
  807. } else {
  808. printf("[FAIL] %s\n", test_name);
  809. fail_count++;
  810. test_passed = false;
  811. }
  812. }
  813. /**
  814. * @brief 打印十六进制数据
  815. */
  816. static void print_hex(const uint8_t* data, uint16_t len, const char* title)
  817. {
  818. printf("%s:\n", title);
  819. for (uint16_t i = 0; i < len; i++) {
  820. if (i % 16 == 0 && i != 0) printf("\n");
  821. printf("%02X ", data[i]);
  822. }
  823. printf("\n");
  824. }
  825. /**
  826. * @brief 保存测试区域数据
  827. */
  828. static void save_test_area(uint32_t addr, uint32_t len)
  829. {
  830. if (len > sizeof(g_test_buffer)) {
  831. len = sizeof(g_test_buffer);
  832. }
  833. printf(" Saving data at 0x%06X...\n", addr);
  834. flash_read_bytes(addr, g_test_buffer, len);
  835. }
  836. /**
  837. * @brief 恢复测试区域数据
  838. */
  839. static void restore_test_area(uint32_t addr, uint32_t len)
  840. {
  841. if (len > sizeof(g_test_buffer)) {
  842. len = sizeof(g_test_buffer);
  843. }
  844. printf(" Restoring data at 0x%06X...\n", addr);
  845. flash_write_bytes(addr, g_test_buffer, len);
  846. delay_ms(20);
  847. }
  848. //============================================================================
  849. // 测试用例
  850. //============================================================================
  851. /**
  852. * @brief 测试0: 显示 Flash 信息和地址布局
  853. */
  854. void test_flash_info(void)
  855. {
  856. printf("\n=== Test 0: Flash Information ===\n");
  857. printf("Chip: GD25Q16 (16Mbit SPI Flash)\n");
  858. printf("Total Size: %d bytes (%d MB)\n", GD25Q16_TOTAL_SIZE, GD25Q16_TOTAL_SIZE / 1024 / 1024);
  859. printf("Page Size: %d bytes\n", GD25Q16_PAGE_SIZE);
  860. printf("Sector Size: %d bytes (%d KB)\n", GD25Q16_SECTOR_SIZE, GD25Q16_SECTOR_SIZE / 1024);
  861. printf("Block Size: %d bytes (%d KB)\n", GD25Q16_BLOCK_SIZE, GD25Q16_BLOCK_SIZE / 1024);
  862. printf("Total Pages: %d\n", GD25Q16_TOTAL_PAGES);
  863. printf("Total Sectors: %d\n", GD25Q16_TOTAL_SECTORS);
  864. printf("\n--- User Config Areas ---\n");
  865. printf(" IAP_ADDR : 0x%06X\n", IAP_ADDR);
  866. printf(" pidinf_addr : 0x%06X\n", pidinf_addr);
  867. printf(" verinf_addr : 0x%06X\n", verinf_addr);
  868. printf(" confinf_addr : 0x%06X\n", confinf_addr);
  869. printf(" caminf_addr : 0x%06X\n", caminf_addr);
  870. printf(" parinf_addr : 0x%06X\n", parinf_addr);
  871. printf(" POS_TOTAL_ADDR : 0x%06X\n", POS_TOTAL_ADDR);
  872. printf(" NO_FLY_ZONE_ADDR : 0x%06X\n", NO_FLY_ZONE_ADDR);
  873. printf(" WAYPOINT_ADDR : 0x%06X\n", WAYPOINT_ADDR);
  874. printf("\n--- Test Safe Area ---\n");
  875. printf(" TEST_AREA_START : 0x%06X\n", TEST_AREA_START);
  876. printf(" TEST_AREA_END : 0x%06X\n", TEST_AREA_END);
  877. printf(" TEST_AREA_SIZE : %d bytes (%d KB)\n", TEST_AREA_SIZE, TEST_AREA_SIZE / 1024);
  878. print_result("Flash info display", true);
  879. }
  880. /**
  881. * @brief 测试1: 页读写测试 (256字节)
  882. */
  883. void test_page_rw(void)
  884. {
  885. uint32_t test_addr = TEST_AREA_START;
  886. uint8_t write_buf[GD25Q16_PAGE_SIZE];
  887. uint8_t read_buf[GD25Q16_PAGE_SIZE];
  888. printf("\n=== Test 1: Page (256 bytes) Read/Write ===\n");
  889. // 准备数据
  890. for (int i = 0; i < GD25Q16_PAGE_SIZE; i++) {
  891. write_buf[i] = i & 0xFF;
  892. }
  893. // 保存原始数据
  894. save_test_area(test_addr, GD25Q16_PAGE_SIZE);
  895. // 写入
  896. printf(" Writing page at 0x%06X...\n", test_addr);
  897. flash_write_bytes(test_addr, write_buf, GD25Q16_PAGE_SIZE);
  898. delay_ms(10);
  899. // 读取
  900. memset(read_buf, 0, sizeof(read_buf));
  901. flash_read_bytes(test_addr, read_buf, GD25Q16_PAGE_SIZE);
  902. // 验证
  903. bool match = (memcmp(write_buf, read_buf, GD25Q16_PAGE_SIZE) == 0);
  904. if (!match) {
  905. printf(" Mismatch detected!\n");
  906. for (int i = 0; i < 16; i++) {
  907. if (write_buf[i] != read_buf[i]) {
  908. printf(" Byte %d: 0x%02X vs 0x%02X\n", i, write_buf[i], read_buf[i]);
  909. break;
  910. }
  911. }
  912. }
  913. // 恢复
  914. restore_test_area(test_addr, GD25Q16_PAGE_SIZE);
  915. print_result("Page read/write", match);
  916. }
  917. /**
  918. * @brief 测试2: 多页读写测试 (4页 = 1KB)
  919. */
  920. void test_multipage_rw(void)
  921. {
  922. uint32_t test_addr = TEST_AREA_START + GD25Q16_PAGE_SIZE;
  923. uint16_t page_count = 4;
  924. uint32_t test_len = page_count * GD25Q16_PAGE_SIZE;
  925. uint8_t* write_buf = g_test_buffer;
  926. uint8_t read_buf[GD25Q16_PAGE_SIZE * 4];
  927. printf("\n=== Test 2: Multi-Page (%d pages) Read/Write ===\n", page_count);
  928. // 准备数据
  929. for (int i = 0; i < test_len; i++) {
  930. write_buf[i] = (i + 0x80) & 0xFF;
  931. }
  932. // 保存原始数据
  933. save_test_area(test_addr, test_len);
  934. // 写入
  935. printf(" Writing %d bytes at 0x%06X...\n", test_len, test_addr);
  936. flash_write_bytes(test_addr, write_buf, test_len);
  937. delay_ms(20);
  938. // 读取
  939. memset(read_buf, 0, sizeof(read_buf));
  940. flash_read_bytes(test_addr, read_buf, test_len);
  941. // 验证
  942. bool match = (memcmp(write_buf, read_buf, test_len) == 0);
  943. // 恢复
  944. restore_test_area(test_addr, test_len);
  945. print_result("Multi-page read/write", match);
  946. }
  947. /**
  948. * @brief 测试3: 扇区擦除测试 (4KB)
  949. */
  950. void test_sector_erase(void)
  951. {
  952. uint32_t test_addr = TEST_AREA_START + GD25Q16_SECTOR_SIZE;
  953. uint32_t sector_start = (test_addr / GD25Q16_SECTOR_SIZE) * GD25Q16_SECTOR_SIZE;
  954. uint8_t* read_buf = g_test_buffer;
  955. printf("\n=== Test 3: Sector (4KB) Erase Test ===\n");
  956. printf(" Sector start: 0x%06X\n", sector_start);
  957. // 先写入非0xFF数据
  958. memset(g_test_buffer, 0xA5, GD25Q16_SECTOR_SIZE);
  959. flash_write_bytes(sector_start, g_test_buffer, GD25Q16_SECTOR_SIZE);
  960. delay_ms(20);
  961. // 保存原始数据用于恢复
  962. save_test_area(sector_start, GD25Q16_SECTOR_SIZE);
  963. // 擦除扇区
  964. printf(" Erasing sector...\n");
  965. // 注意: 这里需要调用扇区擦除函数,如果您的驱动没有单独的函数,
  966. // 可以通过写入0xFF来实现,或者调用底层擦除函数
  967. // AT45DB_EraseSector(sector_start); // 如果有这个函数
  968. // 暂时用写入0xFF模拟(实际应该用擦除命令)
  969. memset(g_test_buffer, 0xFF, GD25Q16_SECTOR_SIZE);
  970. flash_write_bytes(sector_start, g_test_buffer, GD25Q16_SECTOR_SIZE);
  971. delay_ms(50);
  972. // 读取验证
  973. memset(read_buf, 0, GD25Q16_SECTOR_SIZE);
  974. flash_read_bytes(sector_start, read_buf, GD25Q16_SECTOR_SIZE);
  975. // 验证是否全为0xFF
  976. bool all_ff = true;
  977. for (int i = 0; i < GD25Q16_SECTOR_SIZE; i++) {
  978. if (read_buf[i] != 0xFF) {
  979. all_ff = false;
  980. printf(" Non-0xFF at offset %d: 0x%02X\n", i, read_buf[i]);
  981. break;
  982. }
  983. }
  984. // 恢复原始数据
  985. restore_test_area(sector_start, GD25Q16_SECTOR_SIZE);
  986. print_result("Sector erase", all_ff);
  987. }
  988. /**
  989. * @brief 测试4: 边界地址测试
  990. */
  991. void test_boundary(void)
  992. {
  993. uint8_t write_buf[32];
  994. uint8_t read_buf[32];
  995. uint32_t boundary_addr = TEST_AREA_END - 16;
  996. printf("\n=== Test 4: Boundary Address Test ===\n");
  997. printf(" Boundary address: 0x%06X\n", boundary_addr);
  998. if (!IS_TEST_ADDR(boundary_addr) || !IS_TEST_ADDR(boundary_addr + 16)) {
  999. printf(" Skipping (address out of test area)\n");
  1000. print_result("Boundary test", true);
  1001. return;
  1002. }
  1003. // 准备数据
  1004. for (int i = 0; i < sizeof(write_buf); i++) {
  1005. write_buf[i] = 0x55 + i;
  1006. }
  1007. // 保存
  1008. save_test_area(boundary_addr, sizeof(write_buf));
  1009. // 写入
  1010. flash_write_bytes(boundary_addr, write_buf, sizeof(write_buf));
  1011. delay_ms(10);
  1012. // 读取
  1013. flash_read_bytes(boundary_addr, read_buf, sizeof(read_buf));
  1014. // 验证
  1015. bool match = (memcmp(write_buf, read_buf, sizeof(write_buf)) == 0);
  1016. // 恢复
  1017. restore_test_area(boundary_addr, sizeof(write_buf));
  1018. print_result("Boundary test", match);
  1019. }
  1020. /**
  1021. * @brief 测试5: 16位整数读写测试
  1022. */
  1023. void test_int16_rw(void)
  1024. {
  1025. uint32_t test_addr = TEST_AREA_START + 0x2000;
  1026. int16_t write_vals[] = {0, 1, -1, 32767, -32768, 0x1234, 0x5678, 0x9ABC, 0xDEF0};
  1027. int16_t read_vals[9];
  1028. printf("\n=== Test 5: Int16 Read/Write Test ===\n");
  1029. // 保存原始数据
  1030. save_test_area(test_addr, sizeof(write_vals));
  1031. bool all_match = true;
  1032. for (int i = 0; i < sizeof(write_vals)/sizeof(write_vals[0]); i++) {
  1033. uint32_t addr = test_addr + i * 2;
  1034. flash_write_int16(addr, write_vals[i]);
  1035. delay_ms(5);
  1036. read_vals[i] = flash_read_int16(addr);
  1037. if (read_vals[i] != write_vals[i]) {
  1038. printf(" Mismatch at 0x%06X: %d vs %d\n", addr, write_vals[i], read_vals[i]);
  1039. all_match = false;
  1040. }
  1041. }
  1042. if (all_match) {
  1043. printf(" All %d values match\n", (int)(sizeof(write_vals)/sizeof(write_vals[0])));
  1044. }
  1045. // 恢复
  1046. restore_test_area(test_addr, sizeof(write_vals));
  1047. print_result("Int16 read/write", all_match);
  1048. }
  1049. /**
  1050. * @brief 测试6: 压力测试
  1051. */
  1052. void test_stress(void)
  1053. {
  1054. uint32_t test_addr = TEST_AREA_START + 0x3000;
  1055. uint32_t test_len = 256;
  1056. uint8_t* write_buf = g_test_buffer;
  1057. uint8_t read_buf[256];
  1058. const int cycles = 30;
  1059. printf("\n=== Test 6: Stress Test (%d cycles) ===\n", cycles);
  1060. // 保存原始数据
  1061. save_test_area(test_addr, test_len);
  1062. int failed = 0;
  1063. for (int cycle = 0; cycle < cycles; cycle++) {
  1064. // 准备数据
  1065. for (int i = 0; i < test_len; i++) {
  1066. if (cycle % 3 == 0) write_buf[i] = cycle & 0xFF;
  1067. else if (cycle % 3 == 1) write_buf[i] = 0x55;
  1068. else write_buf[i] = 0xAA;
  1069. }
  1070. flash_write_bytes(test_addr, write_buf, test_len);
  1071. delay_ms(5);
  1072. flash_read_bytes(test_addr, read_buf, test_len);
  1073. if (memcmp(write_buf, read_buf, test_len) != 0) {
  1074. failed++;
  1075. printf(" Cycle %d failed\n", cycle);
  1076. }
  1077. if ((cycle + 1) % 10 == 0) {
  1078. printf(" Progress: %d/%d\n", cycle + 1, cycles);
  1079. }
  1080. }
  1081. printf(" Failed: %d/%d\n", failed, cycles);
  1082. // 恢复
  1083. restore_test_area(test_addr, test_len);
  1084. print_result("Stress test", failed == 0);
  1085. }
  1086. /**
  1087. * @brief 测试7: 随机模式测试
  1088. */
  1089. void test_random_pattern(void)
  1090. {
  1091. uint32_t test_addr = TEST_AREA_START + 0x4000;
  1092. uint32_t test_len = 512;
  1093. uint8_t* write_buf = g_test_buffer;
  1094. uint8_t read_buf[512];
  1095. printf("\n=== Test 7: Random Pattern Test ===\n");
  1096. // 生成随机数据
  1097. for (int i = 0; i < test_len; i++) {
  1098. write_buf[i] = (i * 0x9E3779B9) ^ (i >> 3);
  1099. write_buf[i] = (write_buf[i] ^ (write_buf[i] >> 4)) & 0xFF;
  1100. }
  1101. // 保存
  1102. save_test_area(test_addr, test_len);
  1103. // 写入
  1104. flash_write_bytes(test_addr, write_buf, test_len);
  1105. delay_ms(15);
  1106. // 读取
  1107. flash_read_bytes(test_addr, read_buf, test_len);
  1108. // 验证
  1109. bool match = (memcmp(write_buf, read_buf, test_len) == 0);
  1110. if (!match) {
  1111. for (int i = 0; i < 20; i++) {
  1112. if (write_buf[i] != read_buf[i]) {
  1113. printf(" First mismatch at %d: 0x%02X vs 0x%02X\n",
  1114. i, write_buf[i], read_buf[i]);
  1115. break;
  1116. }
  1117. }
  1118. }
  1119. // 恢复
  1120. restore_test_area(test_addr, test_len);
  1121. print_result("Random pattern", match);
  1122. }
  1123. /**
  1124. * @brief 测试8: 数据完整性测试(写入后立即读取多次)
  1125. */
  1126. void test_integrity(void)
  1127. {
  1128. uint32_t test_addr = TEST_AREA_START + 0x5000;
  1129. uint16_t test_values[] = {0x1234, 0x5678, 0x9ABC, 0xDEF0, 0xABCD, 0xFFFF, 0x0000};
  1130. printf("\n=== Test 8: Data Integrity Test ===\n");
  1131. // 保存原始数据
  1132. save_test_area(test_addr, sizeof(test_values));
  1133. bool all_match = true;
  1134. for (int i = 0; i < sizeof(test_values)/sizeof(test_values[0]); i++) {
  1135. uint32_t addr = test_addr + i * 2;
  1136. // 写入
  1137. flash_write_int16(addr, test_values[i]);
  1138. delay_ms(5);
  1139. // 多次读取验证
  1140. for (int read_count = 0; read_count < 3; read_count++) {
  1141. int16_t read_val = flash_read_int16(addr);
  1142. if (read_val != test_values[i]) {
  1143. printf(" Value 0x%04X at 0x%06X: read %d/3 = 0x%04X\n",
  1144. test_values[i], addr, read_count + 1, read_val);
  1145. all_match = false;
  1146. break;
  1147. }
  1148. }
  1149. }
  1150. if (all_match) {
  1151. printf(" All values passed integrity check\n");
  1152. }
  1153. // 恢复
  1154. restore_test_area(test_addr, sizeof(test_values));
  1155. print_result("Data integrity", all_match);
  1156. }
  1157. //============================================================================
  1158. // 主测试函数
  1159. //============================================================================
  1160. /**
  1161. * @brief 运行完整测试
  1162. */
  1163. void run_flash_test(void)
  1164. {
  1165. printf("\n");
  1166. printf("╔══════════════════════════════════════════════════════════════╗\n");
  1167. printf("║ GD25Q16 Flash Test Suite (Safe Area Mode) ║\n");
  1168. printf("╚══════════════════════════════════════════════════════════════╝\n");
  1169. printf("Chip: GD25Q16 (16Mbit = 2MB)\n");
  1170. printf("Test Area: 0x%06X - 0x%06X (%d KB)\n",
  1171. TEST_AREA_START, TEST_AREA_END, TEST_AREA_SIZE / 1024);
  1172. // 初始化
  1173. printf("\nInitializing Flash...\n");
  1174. flash_at45db_initial(); // 实际是 GD25Q16 驱动
  1175. delay_ms(100);
  1176. if (!flash_isexist()) {
  1177. printf("ERROR: Flash not detected!\n");
  1178. return;
  1179. }
  1180. printf("Flash detected, status: %s\n", flash_isbusy() ? "Busy" : "Ready");
  1181. // 重置计数
  1182. test_count = 0;
  1183. fail_count = 0;
  1184. test_passed = true;
  1185. // 运行测试
  1186. test_flash_info();
  1187. test_page_rw();
  1188. test_multipage_rw();
  1189. test_sector_erase();
  1190. test_boundary();
  1191. test_int16_rw();
  1192. test_random_pattern();
  1193. test_integrity();
  1194. test_stress();
  1195. // 结果
  1196. printf("\n");
  1197. printf("══════════════════════════════════════════════════════════════\n");
  1198. printf("Test Summary:\n");
  1199. printf(" Total: %d\n", test_count + fail_count);
  1200. printf(" Passed: %d\n", test_count);
  1201. printf(" Failed: %d\n", fail_count);
  1202. printf("══════════════════════════════════════════════════════════════\n");
  1203. if (test_passed) {
  1204. printf("\n✓ ALL TESTS PASSED!\n");
  1205. } else {
  1206. printf("\n✗ SOME TESTS FAILED!\n");
  1207. }
  1208. }
  1209. /**
  1210. * @brief 快速功能测试
  1211. */
  1212. void run_flash_quick_test(void)
  1213. {
  1214. uint8_t test_data[] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0};
  1215. uint8_t read_data[8];
  1216. uint32_t test_addr = TEST_AREA_START;
  1217. printf("\n=== Quick Test ===\n");
  1218. flash_at45db_initial();
  1219. delay_ms(100);
  1220. if (!flash_isexist()) {
  1221. printf("Flash not detected!\n");
  1222. return;
  1223. }
  1224. // 保存
  1225. uint8_t original[8];
  1226. flash_read_bytes(test_addr, original, sizeof(original));
  1227. // 测试
  1228. flash_write_bytes(test_addr, test_data, sizeof(test_data));
  1229. delay_ms(10);
  1230. flash_read_bytes(test_addr, read_data, sizeof(read_data));
  1231. if (memcmp(test_data, read_data, sizeof(test_data)) == 0) {
  1232. printf("TEST PASSED\n");
  1233. printf("Data: ");
  1234. for (int i = 0; i < sizeof(test_data); i++) {
  1235. printf("%02X ", read_data[i]);
  1236. }
  1237. printf("\n");
  1238. } else {
  1239. printf("TEST FAILED\n");
  1240. }
  1241. // 恢复
  1242. flash_write_bytes(test_addr, original, sizeof(original));
  1243. }
  1244. #endif