sfud_port.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. luat_spi_lock(spi_flash -> id);
  50. if (write_size && read_size) {
  51. if (luat_spi_transfer(spi_flash -> id, (const char*)write_buf, write_size, (char*)read_buf, read_size) <= 0) {
  52. result = SFUD_ERR_TIMEOUT;
  53. }
  54. } else if (write_size) {
  55. if (luat_spi_send(spi_flash -> id, (const char*)write_buf, write_size) <= 0) {
  56. result = SFUD_ERR_WRITE;
  57. }
  58. } else {
  59. if (luat_spi_recv(spi_flash -> id, (char*)read_buf, read_size) <= 0) {
  60. result = SFUD_ERR_READ;
  61. }
  62. }
  63. luat_spi_unlock(spi_flash -> id);
  64. }
  65. else if ( type == LUAT_TYPE_SPI_DEVICE ) {
  66. luat_spi_device_t* spi_dev = (luat_spi_device_t*) ((*(luat_sfud_flash_t*)(spi->user_data)).user_data);
  67. if (write_size && read_size) {
  68. if (luat_spi_device_transfer(spi_dev , (const char*)write_buf, write_size, (char*)read_buf, read_size) <= 0) {
  69. result = SFUD_ERR_TIMEOUT;
  70. }
  71. } else if (write_size) {
  72. if (luat_spi_device_send(spi_dev , (const char*)write_buf, write_size) <= 0) {
  73. result = SFUD_ERR_WRITE;
  74. }
  75. } else {
  76. if (luat_spi_device_recv(spi_dev , (char*)read_buf, read_size) <= 0) {
  77. result = SFUD_ERR_READ;
  78. }
  79. }
  80. }
  81. return result;
  82. }
  83. #ifdef SFUD_USING_QSPI
  84. /**
  85. * read flash data by QSPI
  86. */
  87. static sfud_err qspi_read(const struct __sfud_spi *spi, uint32_t addr, sfud_qspi_read_cmd_format *qspi_read_cmd_format,
  88. uint8_t *read_buf, size_t read_size) {
  89. sfud_err result = SFUD_SUCCESS;
  90. /**
  91. * add your qspi read flash data code
  92. */
  93. return result;
  94. }
  95. #endif /* SFUD_USING_QSPI */
  96. /* about 100 microsecond delay */
  97. static void retry_delay_1ms(void) {
  98. luat_rtos_task_sleep(1);
  99. }
  100. static void retry_delay_10ms(void) {
  101. luat_rtos_task_sleep(10);
  102. }
  103. static void luat_sfud_lock(const sfud_spi *spi)
  104. {
  105. luat_sfud_flash_t *sfud_flash = (luat_sfud_flash_t*)(spi->user_data);
  106. luat_mutex_lock(sfud_flash->sem);
  107. }
  108. static void luat_sfud_unlock(const sfud_spi *spi)
  109. {
  110. luat_sfud_flash_t *sfud_flash = (luat_sfud_flash_t*)(spi->user_data);
  111. luat_mutex_unlock(sfud_flash->sem);
  112. }
  113. sfud_err sfud_spi_port_init(sfud_flash *flash) {
  114. sfud_err result = SFUD_SUCCESS;
  115. /* port SPI device interface */
  116. flash->spi.wr = spi_write_read;
  117. flash->spi.user_data = &(flash->luat_sfud);
  118. flash->luat_sfud.sem = luat_mutex_create();
  119. flash->spi.lock = luat_sfud_lock;
  120. flash->spi.unlock = luat_sfud_unlock;
  121. flash->retry.delay = retry_delay_1ms;
  122. flash->retry.long_delay = retry_delay_10ms;
  123. flash->retry.times = 20; /* write操作 20ms足够了 */
  124. return result;
  125. }
  126. // /**
  127. // * This function is print debug info.
  128. // *
  129. // * @param file the file which has call this function
  130. // * @param line the line number which has call this function
  131. // * @param format output format
  132. // * @param ... args
  133. // */
  134. // void sfud_log_debug(const char *file, const long line, const char *format, ...) {
  135. // va_list args;
  136. // /* args point to the first variable parameter */
  137. // va_start(args, format);
  138. // printf("[SFUD](%s:%ld) ", file, line);
  139. // /* must use vprintf to print */
  140. // vsnprintf(log_buf, sizeof(log_buf), format, args);
  141. // printf("%s\n", log_buf);
  142. // va_end(args);
  143. // }
  144. // /**
  145. // * This function is print routine info.
  146. // *
  147. // * @param format output format
  148. // * @param ... args
  149. // */
  150. // void sfud_log_info(const char *format, ...) {
  151. // va_list args;
  152. // /* args point to the first variable parameter */
  153. // va_start(args, format);
  154. // printf("[SFUD]");
  155. // /* must use vprintf to print */
  156. // vsnprintf(log_buf, sizeof(log_buf), format, args);
  157. // printf("%s\n", log_buf);
  158. // va_end(args);
  159. // }