sd-card-test.cpp 27 KB

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