sd-card-test.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. #include "app-template.h"
  2. #include "stm32_adafruit_sd.h"
  3. #include "fnv1a-hash.h"
  4. #include "filesystem-api.h"
  5. #include "cli/cli.h"
  6. #include "callback-connector.h"
  7. // event enumeration type
  8. typedef uint8_t event_t;
  9. class SdTestState {
  10. public:
  11. // state data
  12. static const uint8_t lines_count = 6;
  13. const char* line[lines_count];
  14. // state initializer
  15. SdTestState() {
  16. for(uint8_t i = 0; i < lines_count; i++) {
  17. line[i] = "";
  18. }
  19. }
  20. };
  21. // events class
  22. class SdTestEvent {
  23. public:
  24. // events enum
  25. static const event_t EventTypeTick = 0;
  26. static const event_t EventTypeKey = 1;
  27. // payload
  28. union {
  29. InputEvent input;
  30. } value;
  31. // event type
  32. event_t type;
  33. };
  34. // our app derived from base AppTemplate class
  35. // with template variables <state, events>
  36. class SdTest : public AppTemplate<SdTestState, SdTestEvent> {
  37. public:
  38. // vars
  39. const uint32_t benchmark_data_size = 4096;
  40. uint8_t* benchmark_data;
  41. FS_Api* fs_api;
  42. // consts
  43. static const uint32_t BENCHMARK_ERROR = UINT_MAX;
  44. // funcs
  45. void run();
  46. void render(Canvas* canvas);
  47. template <class T> void set_text(std::initializer_list<T> list);
  48. template <class T> void set_error(std::initializer_list<T> list);
  49. void wait_for_button(InputKey input_button);
  50. bool ask(InputKey input_button_cancel, InputKey input_button_ok);
  51. void blink_red();
  52. void set_red();
  53. void blink_green();
  54. // "tests"
  55. void detect_sd_card();
  56. void show_warning();
  57. void get_sd_card_info();
  58. bool prepare_benchmark_data();
  59. void free_benchmark_data();
  60. void write_benchmark();
  61. uint32_t
  62. write_benchmark_internal(const uint32_t size, const uint32_t tcount, bool silent = false);
  63. void read_benchmark();
  64. uint32_t read_benchmark_internal(
  65. const uint32_t size,
  66. const uint32_t count,
  67. File* file,
  68. bool silent = false);
  69. void hash_benchmark();
  70. // cli tests
  71. void cli_read_benchmark(string_t args, void* _ctx);
  72. void cli_write_benchmark(string_t args, void* _ctx);
  73. };
  74. // start app
  75. void SdTest::run() {
  76. app_ready();
  77. fs_api = static_cast<FS_Api*>(furi_record_open("sdcard"));
  78. if(fs_api == NULL) {
  79. set_error({"cannot get sdcard api"});
  80. exit();
  81. }
  82. Cli* cli = static_cast<Cli*>(furi_record_open("cli"));
  83. // read_benchmark and write_benchmark signatures are same. so we must use tags
  84. auto cli_read_cb = cbc::obtain_connector<0>(this, &SdTest::cli_read_benchmark);
  85. cli_add_command(cli, "sd_read_test", cli_read_cb, this);
  86. auto cli_write_cb = cbc::obtain_connector<1>(this, &SdTest::cli_write_benchmark);
  87. cli_add_command(cli, "sd_write_test", cli_write_cb, this);
  88. detect_sd_card();
  89. get_sd_card_info();
  90. show_warning();
  91. set_text({"preparing benchmark data"});
  92. bool data_prepared = prepare_benchmark_data();
  93. if(data_prepared) {
  94. set_text({"benchmark data prepared"});
  95. } else {
  96. set_error({"cannot allocate buffer", "for benchmark data"});
  97. }
  98. write_benchmark();
  99. read_benchmark();
  100. hash_benchmark();
  101. free_benchmark_data();
  102. set_text({
  103. "test complete",
  104. "",
  105. "",
  106. "",
  107. "",
  108. "press BACK to exit",
  109. });
  110. wait_for_button(InputKeyBack);
  111. exit();
  112. }
  113. // detect sd card insertion
  114. void SdTest::detect_sd_card() {
  115. const uint8_t str_buffer_size = 40;
  116. const uint8_t dots_animation_size = 4;
  117. char str_buffer[str_buffer_size];
  118. const char dots[dots_animation_size][4] = {"", ".", "..", "..."};
  119. uint8_t i = 0;
  120. // detect sd card pin
  121. while(fs_api->common.get_fs_info(NULL, NULL) == FSE_NOT_READY) {
  122. delay(100);
  123. snprintf(str_buffer, str_buffer_size, "Waiting%s", dots[i]);
  124. set_text({static_cast<const char*>(str_buffer), "Please insert sd card"});
  125. if(i < (dots_animation_size - 1)) {
  126. i++;
  127. } else {
  128. i = 0;
  129. }
  130. }
  131. blink_green();
  132. }
  133. // show warning about test
  134. void SdTest::show_warning() {
  135. set_text(
  136. {"!!Warning!!",
  137. "during the tests",
  138. "files can be overwritten",
  139. "or data on card may be lost",
  140. "",
  141. "press UP DOWN OK to continue"});
  142. wait_for_button(InputKeyUp);
  143. wait_for_button(InputKeyDown);
  144. wait_for_button(InputKeyOk);
  145. }
  146. // get info about sd card, label, sn
  147. // sector, cluster, total and free size
  148. void SdTest::get_sd_card_info() {
  149. const uint8_t str_buffer_size = 26;
  150. char str_buffer[2][str_buffer_size];
  151. FS_Error result;
  152. uint64_t bytes_total, bytes_free;
  153. int __attribute__((unused)) snprintf_count = 0;
  154. result = fs_api->common.get_fs_info(&bytes_total, &bytes_free);
  155. if(result != FSE_OK) set_error({"get_fs_info error", fs_api->error.get_desc(result)});
  156. snprintf(
  157. str_buffer[0], str_buffer_size, "%lu KB total", static_cast<uint32_t>(bytes_total / 1024));
  158. snprintf(
  159. str_buffer[1], str_buffer_size, "%lu KB free", static_cast<uint32_t>(bytes_free / 1024));
  160. set_text(
  161. {static_cast<const char*>(str_buffer[0]),
  162. static_cast<const char*>(str_buffer[1]),
  163. "",
  164. "",
  165. "",
  166. "press OK to continue"});
  167. blink_green();
  168. wait_for_button(InputKeyOk);
  169. }
  170. // prepare benchmark data (allocate data in ram)
  171. bool SdTest::prepare_benchmark_data() {
  172. bool result = true;
  173. benchmark_data = static_cast<uint8_t*>(malloc(benchmark_data_size));
  174. if(benchmark_data == NULL) {
  175. result = false;
  176. }
  177. for(size_t i = 0; i < benchmark_data_size; i++) {
  178. benchmark_data[i] = static_cast<uint8_t>(i);
  179. }
  180. return result;
  181. }
  182. void SdTest::free_benchmark_data() {
  183. free(benchmark_data);
  184. }
  185. // write speed test
  186. void SdTest::write_benchmark() {
  187. const uint32_t b1_size = 1;
  188. const uint32_t b8_size = 8;
  189. const uint32_t b32_size = 32;
  190. const uint32_t b256_size = 256;
  191. const uint32_t b4096_size = 4096;
  192. const uint32_t benchmark_data_size = 16384 * 4;
  193. uint32_t benchmark_bps = 0;
  194. const uint8_t str_buffer_size = 32;
  195. char str_buffer[6][str_buffer_size] = {"", "", "", "", "", ""};
  196. auto string_list = {
  197. static_cast<const char*>(str_buffer[0]),
  198. static_cast<const char*>(str_buffer[1]),
  199. static_cast<const char*>(str_buffer[2]),
  200. static_cast<const char*>(str_buffer[3]),
  201. static_cast<const char*>(str_buffer[4]),
  202. static_cast<const char*>(str_buffer[5])};
  203. set_text({"write speed test", "procedure can be lengthy", "please wait"});
  204. delay(100);
  205. // 1b test
  206. benchmark_bps = write_benchmark_internal(b1_size, benchmark_data_size / b1_size);
  207. snprintf(str_buffer[0], str_buffer_size, "1-byte: %lu bps", benchmark_bps);
  208. set_text(string_list);
  209. delay(100);
  210. // 8b test
  211. benchmark_bps = write_benchmark_internal(b8_size, benchmark_data_size / b8_size);
  212. snprintf(str_buffer[1], str_buffer_size, "8-byte: %lu bps", benchmark_bps);
  213. set_text(string_list);
  214. delay(100);
  215. // 32b test
  216. benchmark_bps = write_benchmark_internal(b32_size, benchmark_data_size / b32_size);
  217. snprintf(str_buffer[2], str_buffer_size, "32-byte: %lu bps", benchmark_bps);
  218. set_text(string_list);
  219. delay(100);
  220. // 256b test
  221. benchmark_bps = write_benchmark_internal(b256_size, benchmark_data_size / b256_size);
  222. snprintf(str_buffer[3], str_buffer_size, "256-byte: %lu bps", benchmark_bps);
  223. set_text(string_list);
  224. delay(100);
  225. // 4096b test
  226. benchmark_bps = write_benchmark_internal(b4096_size, benchmark_data_size / b4096_size);
  227. snprintf(str_buffer[4], str_buffer_size, "4096-byte: %lu bps", benchmark_bps);
  228. snprintf(str_buffer[5], str_buffer_size, "press OK to continue");
  229. set_text(string_list);
  230. blink_green();
  231. wait_for_button(InputKeyOk);
  232. }
  233. uint32_t SdTest::write_benchmark_internal(const uint32_t size, const uint32_t count, bool silent) {
  234. uint32_t start_tick, stop_tick, benchmark_bps = 0, benchmark_time, bytes_written;
  235. File file;
  236. const uint8_t str_buffer_size = 32;
  237. char str_buffer[str_buffer_size];
  238. if(!fs_api->file.open(&file, "write.test", FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
  239. if(!silent) {
  240. snprintf(str_buffer, str_buffer_size, "in %lu-byte write test", size);
  241. set_error({"cannot open file ", static_cast<const char*>(str_buffer)});
  242. } else {
  243. benchmark_bps = BENCHMARK_ERROR;
  244. }
  245. }
  246. start_tick = osKernelGetTickCount();
  247. for(size_t i = 0; i < count; i++) {
  248. bytes_written = fs_api->file.write(&file, benchmark_data, size);
  249. if(bytes_written != size || file.error_id != FSE_OK) {
  250. if(!silent) {
  251. snprintf(str_buffer, str_buffer_size, "in %lu-byte write test", size);
  252. set_error({"cannot write to file ", static_cast<const char*>(str_buffer)});
  253. } else {
  254. benchmark_bps = BENCHMARK_ERROR;
  255. break;
  256. }
  257. }
  258. }
  259. stop_tick = osKernelGetTickCount();
  260. if(!fs_api->file.close(&file)) {
  261. if(!silent) {
  262. snprintf(str_buffer, str_buffer_size, "in %lu-byte write test", size);
  263. set_error({"cannot close file ", static_cast<const char*>(str_buffer)});
  264. } else {
  265. benchmark_bps = BENCHMARK_ERROR;
  266. }
  267. }
  268. if(benchmark_bps != BENCHMARK_ERROR) {
  269. benchmark_time = stop_tick - start_tick;
  270. benchmark_bps = (count * size) * osKernelGetTickFreq() / benchmark_time;
  271. }
  272. return benchmark_bps;
  273. }
  274. // read speed test
  275. void SdTest::read_benchmark() {
  276. const uint32_t benchmark_data_size = 16384 * 8;
  277. uint32_t bytes_written;
  278. uint32_t benchmark_bps = 0;
  279. const uint8_t str_buffer_size = 32;
  280. char str_buffer[6][str_buffer_size] = {"", "", "", "", "", ""};
  281. auto string_list = {
  282. static_cast<const char*>(str_buffer[0]),
  283. static_cast<const char*>(str_buffer[1]),
  284. static_cast<const char*>(str_buffer[2]),
  285. static_cast<const char*>(str_buffer[3]),
  286. static_cast<const char*>(str_buffer[4]),
  287. static_cast<const char*>(str_buffer[5])};
  288. File file;
  289. const uint32_t b1_size = 1;
  290. const uint32_t b8_size = 8;
  291. const uint32_t b32_size = 32;
  292. const uint32_t b256_size = 256;
  293. const uint32_t b4096_size = 4096;
  294. // prepare data for read test
  295. set_text({"prepare data", "for read speed test", "procedure can be lengthy", "please wait"});
  296. delay(100);
  297. if(!fs_api->file.open(&file, "read.test", FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
  298. set_error({"cannot open file ", "in prepare read"});
  299. }
  300. for(size_t i = 0; i < benchmark_data_size / b4096_size; i++) {
  301. bytes_written = fs_api->file.write(&file, benchmark_data, b4096_size);
  302. if(bytes_written != b4096_size || file.error_id != FSE_OK) {
  303. set_error({"cannot write to file ", "in prepare read"});
  304. }
  305. }
  306. if(!fs_api->file.close(&file)) {
  307. set_error({"cannot close file ", "in prepare read"});
  308. }
  309. // test start
  310. set_text({"read speed test", "procedure can be lengthy", "please wait"});
  311. delay(100);
  312. // open file
  313. if(!fs_api->file.open(&file, "read.test", FSAM_READ, FSOM_OPEN_EXISTING)) {
  314. set_error({"cannot open file ", "in read benchmark"});
  315. }
  316. // 1b test
  317. benchmark_bps = read_benchmark_internal(b1_size, benchmark_data_size / b1_size, &file);
  318. snprintf(str_buffer[0], str_buffer_size, "1-byte: %lu bps", benchmark_bps);
  319. set_text(string_list);
  320. delay(100);
  321. // 8b test
  322. benchmark_bps = read_benchmark_internal(b8_size, benchmark_data_size / b8_size, &file);
  323. snprintf(str_buffer[1], str_buffer_size, "8-byte: %lu bps", benchmark_bps);
  324. set_text(string_list);
  325. delay(100);
  326. // 32b test
  327. benchmark_bps = read_benchmark_internal(b32_size, benchmark_data_size / b32_size, &file);
  328. snprintf(str_buffer[2], str_buffer_size, "32-byte: %lu bps", benchmark_bps);
  329. set_text(string_list);
  330. delay(100);
  331. // 256b test
  332. benchmark_bps = read_benchmark_internal(b256_size, benchmark_data_size / b256_size, &file);
  333. snprintf(str_buffer[3], str_buffer_size, "256-byte: %lu bps", benchmark_bps);
  334. set_text(string_list);
  335. delay(100);
  336. // 4096b test
  337. benchmark_bps = read_benchmark_internal(b4096_size, benchmark_data_size / b4096_size, &file);
  338. snprintf(str_buffer[4], str_buffer_size, "4096-byte: %lu bps", benchmark_bps);
  339. snprintf(str_buffer[5], str_buffer_size, "press OK to continue");
  340. set_text(string_list);
  341. // close file
  342. if(!fs_api->file.close(&file)) {
  343. set_error({"cannot close file ", "in read test"});
  344. }
  345. blink_green();
  346. wait_for_button(InputKeyOk);
  347. }
  348. uint32_t SdTest::read_benchmark_internal(
  349. const uint32_t size,
  350. const uint32_t count,
  351. File* file,
  352. bool silent) {
  353. uint32_t start_tick, stop_tick, benchmark_bps = 0, benchmark_time, bytes_readed;
  354. const uint8_t str_buffer_size = 32;
  355. char str_buffer[str_buffer_size];
  356. uint8_t* read_buffer;
  357. read_buffer = static_cast<uint8_t*>(malloc(size));
  358. if(read_buffer == NULL) {
  359. if(!silent) {
  360. snprintf(str_buffer, str_buffer_size, "in %lu-byte read test", size);
  361. set_error({"cannot allocate memory", static_cast<const char*>(str_buffer)});
  362. } else {
  363. benchmark_bps = BENCHMARK_ERROR;
  364. }
  365. }
  366. fs_api->file.seek(file, 0, true);
  367. start_tick = osKernelGetTickCount();
  368. for(size_t i = 0; i < count; i++) {
  369. bytes_readed = fs_api->file.read(file, read_buffer, size);
  370. if(bytes_readed != size || file->error_id != FSE_OK) {
  371. if(!silent) {
  372. snprintf(str_buffer, str_buffer_size, "in %lu-byte read test", size);
  373. set_error({"cannot read from file ", static_cast<const char*>(str_buffer)});
  374. } else {
  375. benchmark_bps = BENCHMARK_ERROR;
  376. break;
  377. }
  378. }
  379. }
  380. stop_tick = osKernelGetTickCount();
  381. free(read_buffer);
  382. if(benchmark_bps != BENCHMARK_ERROR) {
  383. benchmark_time = stop_tick - start_tick;
  384. benchmark_bps = (count * size) * osKernelGetTickFreq() / benchmark_time;
  385. }
  386. return benchmark_bps;
  387. }
  388. // hash benchmark, store data to sd with known hash
  389. // then read, calculate hash and compare both hashes
  390. void SdTest::hash_benchmark() {
  391. uint32_t mcu_data_hash = FNV_1A_INIT;
  392. uint32_t sdcard_data_hash = FNV_1A_INIT;
  393. uint8_t* read_buffer;
  394. uint32_t bytes_readed;
  395. uint32_t bytes_written;
  396. const uint8_t str_buffer_size = 32;
  397. char str_buffer[3][str_buffer_size] = {"", "", ""};
  398. File file;
  399. const uint32_t b4096_size = 4096;
  400. const uint32_t benchmark_count = 20;
  401. // prepare data for hash test
  402. set_text({"prepare data", "for hash test"});
  403. delay(100);
  404. // write data to test file and calculate hash
  405. if(!fs_api->file.open(&file, "hash.test", FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
  406. set_error({"cannot open file ", "in prepare hash"});
  407. }
  408. for(uint32_t i = 0; i < benchmark_count; i++) {
  409. mcu_data_hash = fnv1a_buffer_hash(benchmark_data, b4096_size, mcu_data_hash);
  410. bytes_written = fs_api->file.write(&file, benchmark_data, b4096_size);
  411. if(bytes_written != b4096_size || file.error_id != FSE_OK) {
  412. set_error({"cannot write to file ", "in prepare hash"});
  413. }
  414. snprintf(str_buffer[0], str_buffer_size, "writing %lu of %lu x 4k", i, benchmark_count);
  415. set_text({"prepare data", "for hash test", static_cast<const char*>(str_buffer[0])});
  416. delay(100);
  417. }
  418. if(!fs_api->file.close(&file)) {
  419. set_error({"cannot close file ", "in prepare hash"});
  420. }
  421. // show hash of data located in mcu memory
  422. snprintf(str_buffer[0], str_buffer_size, "hash in mcu 0x%lx", mcu_data_hash);
  423. set_text({str_buffer[0]});
  424. delay(100);
  425. // read data from sd card and calculate hash
  426. read_buffer = static_cast<uint8_t*>(malloc(b4096_size));
  427. if(read_buffer == NULL) {
  428. set_error({"cannot allocate memory", "in hash test"});
  429. }
  430. if(!fs_api->file.open(&file, "hash.test", FSAM_READ, FSOM_OPEN_EXISTING)) {
  431. set_error({"cannot open file ", "in hash test"});
  432. }
  433. for(uint32_t i = 0; i < benchmark_count; i++) {
  434. bytes_readed = fs_api->file.read(&file, read_buffer, b4096_size);
  435. sdcard_data_hash = fnv1a_buffer_hash(read_buffer, b4096_size, sdcard_data_hash);
  436. if(bytes_readed != b4096_size || file.error_id != FSE_OK) {
  437. set_error({"cannot read from file ", "in hash test"});
  438. }
  439. snprintf(str_buffer[1], str_buffer_size, "reading %lu of %lu x 4k", i, benchmark_count);
  440. set_text({str_buffer[0], str_buffer[1]});
  441. delay(100);
  442. }
  443. if(!fs_api->file.close(&file)) {
  444. set_error({"cannot close file ", "in hash test"});
  445. }
  446. free(read_buffer);
  447. snprintf(str_buffer[1], str_buffer_size, "hash in sdcard 0x%lx", sdcard_data_hash);
  448. if(mcu_data_hash == sdcard_data_hash) {
  449. snprintf(str_buffer[2], str_buffer_size, "hashes are equal, press OK");
  450. set_text(
  451. {static_cast<const char*>(str_buffer[0]),
  452. static_cast<const char*>(str_buffer[1]),
  453. "",
  454. "",
  455. "",
  456. static_cast<const char*>(str_buffer[2])});
  457. } else {
  458. snprintf(str_buffer[2], str_buffer_size, "hash error, press BACK to exit");
  459. set_error(
  460. {static_cast<const char*>(str_buffer[0]),
  461. static_cast<const char*>(str_buffer[1]),
  462. "",
  463. "",
  464. "",
  465. static_cast<const char*>(str_buffer[2])});
  466. }
  467. blink_green();
  468. wait_for_button(InputKeyOk);
  469. }
  470. void SdTest::cli_read_benchmark(string_t args, void* _ctx) {
  471. SdTest* _this = static_cast<SdTest*>(_ctx);
  472. const uint32_t benchmark_data_size = 16384 * 8;
  473. uint32_t bytes_written;
  474. uint32_t benchmark_bps = 0;
  475. File file;
  476. const uint32_t b1_size = 1;
  477. const uint32_t b8_size = 8;
  478. const uint32_t b32_size = 32;
  479. const uint32_t b256_size = 256;
  480. const uint32_t b4096_size = 4096;
  481. const uint8_t str_buffer_size = 64;
  482. char str_buffer[str_buffer_size];
  483. printf("preparing benchmark data\r\n");
  484. bool data_prepared = _this->prepare_benchmark_data();
  485. if(data_prepared) {
  486. printf("benchmark data prepared\r\n");
  487. } else {
  488. printf("error: cannot allocate buffer for benchmark data\r\n");
  489. }
  490. // prepare data for read test
  491. printf("prepare data for read speed test, procedure can be lengthy, please wait\r\n");
  492. if(!_this->fs_api->file.open(&file, "read.test", FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
  493. printf("error: cannot open file in prepare read\r\n");
  494. }
  495. for(size_t i = 0; i < benchmark_data_size / b4096_size; i++) {
  496. bytes_written = _this->fs_api->file.write(&file, benchmark_data, b4096_size);
  497. if(bytes_written != b4096_size || file.error_id != FSE_OK) {
  498. printf("error: cannot write to file in prepare read\r\n");
  499. }
  500. }
  501. if(!_this->fs_api->file.close(&file)) {
  502. printf("error: cannot close file in prepare read\r\n");
  503. }
  504. // test start
  505. printf("read speed test, procedure can be lengthy, please wait\r\n");
  506. // open file
  507. if(!_this->fs_api->file.open(&file, "read.test", FSAM_READ, FSOM_OPEN_EXISTING)) {
  508. printf("error: cannot open file in read benchmark\r\n");
  509. }
  510. // 1b test
  511. benchmark_bps =
  512. _this->read_benchmark_internal(b1_size, benchmark_data_size / b1_size, &file, true);
  513. if(benchmark_bps == BENCHMARK_ERROR) {
  514. printf("error: in 1-byte read test\r\n");
  515. } else {
  516. snprintf(str_buffer, str_buffer_size, "1-byte: %lu bytes per second\r\n", benchmark_bps);
  517. printf(str_buffer);
  518. }
  519. // 8b test
  520. benchmark_bps =
  521. _this->read_benchmark_internal(b8_size, benchmark_data_size / b8_size, &file, true);
  522. if(benchmark_bps == BENCHMARK_ERROR) {
  523. printf("error: in 8-byte read test\r\n");
  524. } else {
  525. snprintf(str_buffer, str_buffer_size, "8-byte: %lu bytes per second\r\n", benchmark_bps);
  526. printf(str_buffer);
  527. }
  528. // 32b test
  529. benchmark_bps =
  530. _this->read_benchmark_internal(b32_size, benchmark_data_size / b32_size, &file, true);
  531. if(benchmark_bps == BENCHMARK_ERROR) {
  532. printf("error: in 32-byte read test\r\n");
  533. } else {
  534. snprintf(str_buffer, str_buffer_size, "32-byte: %lu bytes per second\r\n", benchmark_bps);
  535. printf(str_buffer);
  536. }
  537. // 256b test
  538. benchmark_bps =
  539. _this->read_benchmark_internal(b256_size, benchmark_data_size / b256_size, &file, true);
  540. if(benchmark_bps == BENCHMARK_ERROR) {
  541. printf("error: in 256-byte read test\r\n");
  542. } else {
  543. snprintf(str_buffer, str_buffer_size, "256-byte: %lu bytes per second\r\n", benchmark_bps);
  544. printf(str_buffer);
  545. }
  546. // 4096b test
  547. benchmark_bps =
  548. _this->read_benchmark_internal(b4096_size, benchmark_data_size / b4096_size, &file, true);
  549. if(benchmark_bps == BENCHMARK_ERROR) {
  550. printf("error: in 4096-byte read test\r\n");
  551. } else {
  552. snprintf(
  553. str_buffer, str_buffer_size, "4096-byte: %lu bytes per second\r\n", benchmark_bps);
  554. printf(str_buffer);
  555. }
  556. // close file
  557. if(!_this->fs_api->file.close(&file)) {
  558. printf("error: cannot close file\r\n");
  559. }
  560. _this->free_benchmark_data();
  561. printf("test completed\r\n");
  562. }
  563. void SdTest::cli_write_benchmark(string_t args, void* _ctx) {
  564. SdTest* _this = static_cast<SdTest*>(_ctx);
  565. const uint32_t b1_size = 1;
  566. const uint32_t b8_size = 8;
  567. const uint32_t b32_size = 32;
  568. const uint32_t b256_size = 256;
  569. const uint32_t b4096_size = 4096;
  570. const uint32_t benchmark_data_size = 16384 * 4;
  571. uint32_t benchmark_bps = 0;
  572. const uint8_t str_buffer_size = 64;
  573. char str_buffer[str_buffer_size];
  574. printf("preparing benchmark data\r\n");
  575. bool data_prepared = _this->prepare_benchmark_data();
  576. if(data_prepared) {
  577. printf("benchmark data prepared\r\n");
  578. } else {
  579. printf("error: cannot allocate buffer for benchmark data\r\n");
  580. }
  581. printf("write speed test, procedure can be lengthy, please wait\r\n");
  582. // 1b test
  583. benchmark_bps = _this->write_benchmark_internal(b1_size, benchmark_data_size / b1_size, true);
  584. if(benchmark_bps == BENCHMARK_ERROR) {
  585. printf("error: in 1-byte write test\r\n");
  586. } else {
  587. snprintf(str_buffer, str_buffer_size, "1-byte: %lu bytes per second\r\n", benchmark_bps);
  588. printf(str_buffer);
  589. }
  590. // 8b test
  591. benchmark_bps = _this->write_benchmark_internal(b8_size, benchmark_data_size / b8_size, true);
  592. if(benchmark_bps == BENCHMARK_ERROR) {
  593. printf("error: in 8-byte write test\r\n");
  594. } else {
  595. snprintf(str_buffer, str_buffer_size, "8-byte: %lu bytes per second\r\n", benchmark_bps);
  596. printf(str_buffer);
  597. }
  598. // 32b test
  599. benchmark_bps =
  600. _this->write_benchmark_internal(b32_size, benchmark_data_size / b32_size, true);
  601. if(benchmark_bps == BENCHMARK_ERROR) {
  602. printf("error: in 32-byte write test\r\n");
  603. } else {
  604. snprintf(str_buffer, str_buffer_size, "32-byte: %lu bytes per second\r\n", benchmark_bps);
  605. printf(str_buffer);
  606. }
  607. // 256b test
  608. benchmark_bps =
  609. _this->write_benchmark_internal(b256_size, benchmark_data_size / b256_size, true);
  610. if(benchmark_bps == BENCHMARK_ERROR) {
  611. printf("error: in 256-byte write test\r\n");
  612. } else {
  613. snprintf(str_buffer, str_buffer_size, "256-byte: %lu bytes per second\r\n", benchmark_bps);
  614. printf(str_buffer);
  615. }
  616. // 4096b test
  617. benchmark_bps =
  618. _this->write_benchmark_internal(b4096_size, benchmark_data_size / b4096_size, true);
  619. if(benchmark_bps == BENCHMARK_ERROR) {
  620. printf("error: in 4096-byte write test\r\n");
  621. } else {
  622. snprintf(
  623. str_buffer, str_buffer_size, "4096-byte: %lu bytes per second\r\n", benchmark_bps);
  624. printf(str_buffer);
  625. }
  626. _this->free_benchmark_data();
  627. printf("test completed\r\n");
  628. }
  629. // wait for button press
  630. void SdTest::wait_for_button(InputKey input_button) {
  631. SdTestEvent event;
  632. osMessageQueueReset(event_queue);
  633. while(1) {
  634. osStatus_t result = osMessageQueueGet(event_queue, &event, NULL, osWaitForever);
  635. if(result == osOK && event.type == SdTestEvent::EventTypeKey) {
  636. if(event.value.input.type == InputTypeShort) {
  637. if(event.value.input.key == InputKeyBack) {
  638. exit();
  639. } else {
  640. if(event.value.input.key == input_button) {
  641. blink_green();
  642. break;
  643. } else {
  644. blink_red();
  645. }
  646. }
  647. }
  648. }
  649. }
  650. osMessageQueueReset(event_queue);
  651. }
  652. // ask user to proceed or cancel
  653. bool SdTest::ask(InputKey input_button_cancel, InputKey input_button_ok) {
  654. bool return_result;
  655. SdTestEvent event;
  656. osMessageQueueReset(event_queue);
  657. while(1) {
  658. osStatus_t result = osMessageQueueGet(event_queue, &event, NULL, osWaitForever);
  659. if(result == osOK && event.type == SdTestEvent::EventTypeKey) {
  660. if(event.value.input.type == InputTypeShort) {
  661. if(event.value.input.key == InputKeyBack) {
  662. exit();
  663. } else {
  664. if(event.value.input.key == input_button_ok) {
  665. blink_green();
  666. return_result = true;
  667. break;
  668. } else if(event.value.input.key == input_button_cancel) {
  669. blink_green();
  670. return_result = false;
  671. break;
  672. } else {
  673. blink_red();
  674. }
  675. }
  676. }
  677. }
  678. }
  679. osMessageQueueReset(event_queue);
  680. return return_result;
  681. }
  682. // blink red led
  683. void SdTest::blink_red() {
  684. api_hal_light_set(LightRed, 0xFF);
  685. delay(50);
  686. api_hal_light_set(LightRed, 0x00);
  687. }
  688. // light up red led
  689. void SdTest::set_red() {
  690. api_hal_light_set(LightRed, 0x00);
  691. }
  692. // blink green led
  693. void SdTest::blink_green() {
  694. api_hal_light_set(LightGreen, 0xFF);
  695. delay(50);
  696. api_hal_light_set(LightGreen, 0x00);
  697. }
  698. // set text, but with infinite loop
  699. template <class T> void SdTest::set_error(std::initializer_list<T> list) {
  700. set_text(list);
  701. set_red();
  702. wait_for_button(InputKeyBack);
  703. exit();
  704. }
  705. // set text, sort of variadic function
  706. template <class T> void SdTest::set_text(std::initializer_list<T> list) {
  707. uint8_t line_position = 0;
  708. acquire_state();
  709. printf("------------------------\r\n");
  710. // set line strings from args
  711. for(auto element : list) {
  712. state.line[line_position] = element;
  713. printf("%s\n", element);
  714. line_position++;
  715. if(line_position == state.lines_count) break;
  716. }
  717. // set empty lines
  718. for(; line_position < state.lines_count; line_position++) {
  719. state.line[line_position] = "";
  720. printf("\r\n");
  721. }
  722. printf("------------------------\r\n");
  723. release_state();
  724. update_gui();
  725. }
  726. // render app
  727. void SdTest::render(Canvas* canvas) {
  728. canvas_set_color(canvas, ColorBlack);
  729. canvas_set_font(canvas, FontSecondary);
  730. for(uint8_t i = 0; i < state.lines_count; i++) {
  731. canvas_draw_str(canvas, 0, (i + 1) * 10, state.line[i]);
  732. }
  733. }
  734. // app enter function
  735. extern "C" int32_t sd_card_test(void* p) {
  736. SdTest* app = new SdTest();
  737. app->run();
  738. return 0;
  739. }