sfud_port.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #define LUAT_LOG_TAG "sfud"
  32. #include "luat_log.h"
  33. static char log_buf[256];
  34. void sfud_log_debug(const char *file, const long line, const char *format, ...);
  35. /**
  36. * SPI write data then read data
  37. */
  38. static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
  39. size_t read_size) {
  40. sfud_err result = SFUD_SUCCESS;
  41. if (write_size) {
  42. SFUD_ASSERT(write_buf);
  43. }
  44. if (read_size) {
  45. SFUD_ASSERT(read_buf);
  46. }
  47. int type = (*(luat_sfud_flash_t*)(spi->user_data)).luat_spi;
  48. if ( type == 0 ) {
  49. luat_spi_t* spi_flash = (luat_spi_t*) ((*(luat_sfud_flash_t*)(spi->user_data)).user_data);
  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. }
  64. else if ( type == 1 ) {
  65. luat_spi_device_t* spi_dev = (luat_spi_device_t*) ((*(luat_sfud_flash_t*)(spi->user_data)).user_data);
  66. if (write_size && read_size) {
  67. if (luat_spi_device_transfer(spi_dev , (const char*)write_buf, write_size, (char*)read_buf, read_size) <= 0) {
  68. result = SFUD_ERR_TIMEOUT;
  69. }
  70. } else if (write_size) {
  71. if (luat_spi_device_send(spi_dev , (const char*)write_buf, write_size) <= 0) {
  72. result = SFUD_ERR_WRITE;
  73. }
  74. } else {
  75. if (luat_spi_device_recv(spi_dev , (char*)read_buf, read_size) <= 0) {
  76. result = SFUD_ERR_READ;
  77. }
  78. }
  79. }
  80. return result;
  81. }
  82. #ifdef SFUD_USING_QSPI
  83. /**
  84. * read flash data by QSPI
  85. */
  86. static sfud_err qspi_read(const struct __sfud_spi *spi, uint32_t addr, sfud_qspi_read_cmd_format *qspi_read_cmd_format,
  87. uint8_t *read_buf, size_t read_size) {
  88. sfud_err result = SFUD_SUCCESS;
  89. /**
  90. * add your qspi read flash data code
  91. */
  92. return result;
  93. }
  94. #endif /* SFUD_USING_QSPI */
  95. /* about 100 microsecond delay */
  96. static void retry_delay_100us(void) {
  97. uint32_t delay = 120;
  98. while(delay--);
  99. }
  100. sfud_err sfud_spi_port_init(sfud_flash *flash) {
  101. sfud_err result = SFUD_SUCCESS;
  102. extern luat_sfud_flash_t luat_sfud;
  103. /* port SPI device interface */
  104. flash->spi.wr = spi_write_read;
  105. // flash->spi.user_data = flash;
  106. flash->spi.user_data = &luat_sfud;
  107. /* 100 microsecond delay */
  108. flash->retry.delay = retry_delay_100us;
  109. /* 60 seconds timeout */
  110. flash->retry.times = 60 * 10000;
  111. flash->name = "LuatOS-sfud";
  112. return result;
  113. }
  114. /**
  115. * This function is print debug info.
  116. *
  117. * @param file the file which has call this function
  118. * @param line the line number which has call this function
  119. * @param format output format
  120. * @param ... args
  121. */
  122. void sfud_log_debug(const char *file, const long line, const char *format, ...) {
  123. va_list args;
  124. /* args point to the first variable parameter */
  125. va_start(args, format);
  126. // printf("[SFUD](%s:%ld) ", file, line);
  127. /* must use vprintf to print */
  128. vsnprintf(log_buf, sizeof(log_buf), format, args);
  129. // printf("%s\n", log_buf);
  130. LLOGD("%s ", log_buf);
  131. va_end(args);
  132. }
  133. /**
  134. * This function is print routine info.
  135. *
  136. * @param format output format
  137. * @param ... args
  138. */
  139. void sfud_log_info(const char *format, ...) {
  140. va_list args;
  141. /* args point to the first variable parameter */
  142. va_start(args, format);
  143. // printf("[SFUD]");
  144. /* must use vprintf to print */
  145. vsnprintf(log_buf, sizeof(log_buf), format, args);
  146. // printf("%s\n", log_buf);
  147. LLOGD("%s ", log_buf);
  148. va_end(args);
  149. }