spi_test.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <dirent.h>
  9. #include "ftcfg.h"
  10. #include "ftypes.h"
  11. #include "utils.h"
  12. #include "ark1668ft.h"
  13. void *spi_test_thread(void *arg)
  14. {
  15. struct ft_runtime *rt = (struct ft_runtime *)arg;
  16. int timeout = TEST_TIMEOUT / 2 / 100;
  17. char command[128];
  18. char filename0[32] = "/media/spinor/";
  19. char filename1[32] = "/media/spinor/";
  20. int fddata = -1, fdspi = -1;
  21. char *buf0 = NULL, *buf1 = NULL;
  22. int ret;
  23. printf("spi test start.\n");
  24. fddata = open(SPINOR_TESTFILE_PATH, O_RDONLY);
  25. if (fddata < 0) {
  26. printf("open spidata test file fail.\n");
  27. goto err;
  28. }
  29. fdspi = open("/dev/mtd9", O_RDONLY | O_SYNC);
  30. if (fddata < 0) {
  31. printf("open spi device file fail.\n");
  32. goto err;
  33. }
  34. buf0 = malloc(SPINOR_TESTFILE_SIZE);
  35. buf1 = malloc(SPINOR_TESTFILE_SIZE);
  36. if (!buf0 || !buf1) {
  37. printf("spi test malloc fail.\n");
  38. goto err;
  39. }
  40. if (read(fddata, buf0, SPINOR_TESTFILE_SIZE) != SPINOR_TESTFILE_SIZE) {
  41. printf("read spi data file err.\n");
  42. goto err;
  43. }
  44. if (read(fdspi, buf1, SPINOR_TESTFILE_SIZE) != SPINOR_TESTFILE_SIZE) {
  45. printf("read spiflash data err.\n");
  46. goto err;
  47. }
  48. if (memcmp(buf0, buf1, SPINOR_TESTFILE_SIZE) != 0) {
  49. printf("compare spinor data fail.\n");
  50. goto err;
  51. }
  52. free(buf0);
  53. free(buf1);
  54. close(fddata);
  55. close(fdspi);
  56. rt->finish = 1;
  57. rt->pass = 1;
  58. printf("spi test ok.\n");
  59. return (void*)0;
  60. err:
  61. if (buf0) free(buf0);
  62. if (buf1) free(buf1);
  63. if (fddata > 0) close(fddata);
  64. rt->finish = 1;
  65. printf("spi test fail.\n");
  66. return (void*)-1;
  67. }