sd-card-test.cpp 28 KB

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