sfud_port.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "luat.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. luat_spi_t* spi_flash = (luat_spi_t*) (spi->user_data);
  42. if (write_size) {
  43. SFUD_ASSERT(write_buf);
  44. }
  45. if (read_size) {
  46. SFUD_ASSERT(read_buf);
  47. }
  48. if (write_size && read_size) {
  49. if (luat_spi_transfer(spi_flash -> id, write_buf, read_buf, read_size) <= 0) {
  50. result = SFUD_ERR_TIMEOUT;
  51. }
  52. } else if (write_size) {
  53. if (luat_spi_send(spi_flash -> id, write_buf, write_size) <= 0) {
  54. result = SFUD_ERR_WRITE;
  55. }
  56. } else {
  57. if (luat_spi_recv(spi_flash -> id, read_buf, read_size) <= 0) {
  58. result = SFUD_ERR_READ;
  59. }
  60. }
  61. return result;
  62. }
  63. #ifdef SFUD_USING_QSPI
  64. /**
  65. * read flash data by QSPI
  66. */
  67. static sfud_err qspi_read(const struct __sfud_spi *spi, uint32_t addr, sfud_qspi_read_cmd_format *qspi_read_cmd_format,
  68. uint8_t *read_buf, size_t read_size) {
  69. sfud_err result = SFUD_SUCCESS;
  70. /**
  71. * add your qspi read flash data code
  72. */
  73. return result;
  74. }
  75. #endif /* SFUD_USING_QSPI */
  76. /* about 100 microsecond delay */
  77. static void retry_delay_100us(void) {
  78. uint32_t delay = 120;
  79. while(delay--);
  80. }
  81. sfud_err sfud_spi_port_init(sfud_flash *flash) {
  82. sfud_err result = SFUD_SUCCESS;
  83. extern luat_spi_t sfud_spi_flash;
  84. /* port SPI device interface */
  85. flash->spi.wr = spi_write_read;
  86. // flash->spi.user_data = flash;
  87. flash->spi.user_data = &sfud_spi_flash;
  88. /* 100 microsecond delay */
  89. flash->retry.delay = retry_delay_100us;
  90. /* 60 seconds timeout */
  91. flash->retry.times = 60 * 10000;
  92. return result;
  93. }
  94. /**
  95. * This function is print debug info.
  96. *
  97. * @param file the file which has call this function
  98. * @param line the line number which has call this function
  99. * @param format output format
  100. * @param ... args
  101. */
  102. void sfud_log_debug(const char *file, const long line, const char *format, ...) {
  103. va_list args;
  104. /* args point to the first variable parameter */
  105. va_start(args, format);
  106. // printf("[SFUD](%s:%ld) ", file, line);
  107. /* must use vprintf to print */
  108. vsnprintf(log_buf, sizeof(log_buf), format, args);
  109. // printf("%s\n", log_buf);
  110. LLOGD("%s ", log_buf);
  111. va_end(args);
  112. }
  113. /**
  114. * This function is print routine info.
  115. *
  116. * @param format output format
  117. * @param ... args
  118. */
  119. void sfud_log_info(const char *format, ...) {
  120. va_list args;
  121. /* args point to the first variable parameter */
  122. va_start(args, format);
  123. // printf("[SFUD]");
  124. /* must use vprintf to print */
  125. vsnprintf(log_buf, sizeof(log_buf), format, args);
  126. // printf("%s\n", log_buf);
  127. LLOGD("%s ", log_buf);
  128. va_end(args);
  129. }