sfud_port.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * This file is part of the Serial Flash Universal Driver Library.
  3. *
  4. * Copyright (c) 2016-2018, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: Portable interface for each platform.
  26. * Created on: 2016-04-23
  27. */
  28. #include <sfud.h>
  29. #include <stdarg.h>
  30. #include "luat_spi.h"
  31. #include "luat_rtos.h"
  32. // static char log_buf[256];
  33. // void sfud_log_debug(const char *file, const long line, const char *format, ...);
  34. /**
  35. * SPI write data then read data
  36. */
  37. static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
  38. size_t read_size) {
  39. sfud_err result = SFUD_SUCCESS;
  40. if (write_size) {
  41. SFUD_ASSERT(write_buf);
  42. }
  43. if (read_size) {
  44. SFUD_ASSERT(read_buf);
  45. }
  46. int type = (*(luat_sfud_flash_t*)(spi->user_data)).luat_spi;
  47. if ( type == LUAT_TYPE_SPI ) {
  48. luat_spi_t* spi_flash = (luat_spi_t*) ((*(luat_sfud_flash_t*)(spi->user_data)).user_data);
  49. if (write_size && read_size) {
  50. if (luat_spi_transfer(spi_flash -> id, (const char*)write_buf, write_size, (char*)read_buf, read_size) <= 0) {
  51. result = SFUD_ERR_TIMEOUT;
  52. }
  53. } else if (write_size) {
  54. if (luat_spi_send(spi_flash -> id, (const char*)write_buf, write_size) <= 0) {
  55. result = SFUD_ERR_WRITE;
  56. }
  57. } else {
  58. if (luat_spi_recv(spi_flash -> id, (char*)read_buf, read_size) <= 0) {
  59. result = SFUD_ERR_READ;
  60. }
  61. }
  62. }
  63. else if ( type == LUAT_TYPE_SPI_DEVICE ) {
  64. luat_spi_device_t* spi_dev = (luat_spi_device_t*) ((*(luat_sfud_flash_t*)(spi->user_data)).user_data);
  65. if (write_size && read_size) {
  66. if (luat_spi_device_transfer(spi_dev , (const char*)write_buf, write_size, (char*)read_buf, read_size) <= 0) {
  67. result = SFUD_ERR_TIMEOUT;
  68. }
  69. } else if (write_size) {
  70. if (luat_spi_device_send(spi_dev , (const char*)write_buf, write_size) <= 0) {
  71. result = SFUD_ERR_WRITE;
  72. }
  73. } else {
  74. if (luat_spi_device_recv(spi_dev , (char*)read_buf, read_size) <= 0) {
  75. result = SFUD_ERR_READ;
  76. }
  77. }
  78. }
  79. return result;
  80. }
  81. #ifdef SFUD_USING_QSPI
  82. /**
  83. * read flash data by QSPI
  84. */
  85. static sfud_err qspi_read(const struct __sfud_spi *spi, uint32_t addr, sfud_qspi_read_cmd_format *qspi_read_cmd_format,
  86. uint8_t *read_buf, size_t read_size) {
  87. sfud_err result = SFUD_SUCCESS;
  88. /**
  89. * add your qspi read flash data code
  90. */
  91. return result;
  92. }
  93. #endif /* SFUD_USING_QSPI */
  94. /* about 100 microsecond delay */
  95. static void retry_delay_100us(void) {
  96. uint32_t delay = 120;
  97. while(delay--);
  98. }
  99. static void luat_sfud_lock(const sfud_spi *spi)
  100. {
  101. luat_sfud_flash_t *sfud_flash = (luat_sfud_flash_t*)(spi->user_data);
  102. luat_mutex_lock(sfud_flash->sem);
  103. }
  104. static void luat_sfud_unlock(const sfud_spi *spi)
  105. {
  106. luat_sfud_flash_t *sfud_flash = (luat_sfud_flash_t*)(spi->user_data);
  107. luat_mutex_unlock(sfud_flash->sem);
  108. }
  109. sfud_err sfud_spi_port_init(sfud_flash *flash) {
  110. sfud_err result = SFUD_SUCCESS;
  111. /* port SPI device interface */
  112. flash->spi.wr = spi_write_read;
  113. flash->spi.user_data = &(flash->luat_sfud);
  114. flash->luat_sfud.sem = luat_mutex_create();
  115. flash->spi.lock = luat_sfud_lock;
  116. flash->spi.unlock = luat_sfud_unlock;
  117. /* 100 microsecond delay */
  118. flash->retry.delay = retry_delay_100us;
  119. /* 60 seconds timeout */
  120. flash->retry.times = 60 * 10000;
  121. return result;
  122. }
  123. // /**
  124. // * This function is print debug info.
  125. // *
  126. // * @param file the file which has call this function
  127. // * @param line the line number which has call this function
  128. // * @param format output format
  129. // * @param ... args
  130. // */
  131. // void sfud_log_debug(const char *file, const long line, const char *format, ...) {
  132. // va_list args;
  133. // /* args point to the first variable parameter */
  134. // va_start(args, format);
  135. // printf("[SFUD](%s:%ld) ", file, line);
  136. // /* must use vprintf to print */
  137. // vsnprintf(log_buf, sizeof(log_buf), format, args);
  138. // printf("%s\n", log_buf);
  139. // va_end(args);
  140. // }
  141. // /**
  142. // * This function is print routine info.
  143. // *
  144. // * @param format output format
  145. // * @param ... args
  146. // */
  147. // void sfud_log_info(const char *format, ...) {
  148. // va_list args;
  149. // /* args point to the first variable parameter */
  150. // va_start(args, format);
  151. // printf("[SFUD]");
  152. // /* must use vprintf to print */
  153. // vsnprintf(log_buf, sizeof(log_buf), format, args);
  154. // printf("%s\n", log_buf);
  155. // va_end(args);
  156. // }