sfud_port.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 "FreeRTOS.h"
  31. #include "spi.h"
  32. #include "errno.h"
  33. #define SFUD_SPI_MAX_HZ 35000000
  34. #define SFUD_QSPI_MAX_HZ SFUD_SPI_MAX_HZ
  35. /* read the JEDEC SFDP command must run at 50 MHz or less */
  36. #define SFUD_DEFAULT_SPI_CFG \
  37. { \
  38. .mode = SPI_MODE_0, \
  39. .data_width = 8, \
  40. .max_hz = SFUD_SPI_MAX_HZ, \
  41. .qspi_max_hz = SFUD_QSPI_MAX_HZ, \
  42. }
  43. static char log_buf[256];
  44. void sfud_log_debug(const char *file, const long line, const char *format, ...);
  45. /**
  46. * SPI write data then read data
  47. */
  48. static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
  49. size_t read_size) {
  50. sfud_err result = SFUD_SUCCESS;
  51. struct spi_slave *slave;
  52. configASSERT(spi);
  53. if (write_size) {
  54. configASSERT(write_buf);
  55. }
  56. if (read_size) {
  57. configASSERT(read_buf);
  58. }
  59. slave = spi->user_data;
  60. configASSERT(slave);
  61. if (write_size && read_size) {
  62. if (spi_send_then_recv(slave, write_buf, write_size, read_buf, read_size) != ENOERR) {
  63. result = SFUD_ERR_TIMEOUT;
  64. }
  65. } else if (write_size) {
  66. if (spi_send(slave, write_buf, write_size) < 0) {
  67. result = SFUD_ERR_TIMEOUT;
  68. }
  69. } else {
  70. if (spi_recv(slave, read_buf, read_size) < 0) {
  71. result = SFUD_ERR_TIMEOUT;
  72. }
  73. }
  74. return result;
  75. }
  76. #ifdef SFUD_USING_QSPI
  77. static sfud_err enter_qspi_mode(const sfud_spi *spi)
  78. {
  79. sfud_err result = SFUD_SUCCESS;
  80. #if 0
  81. uint8_t status;
  82. uint8_t cmd[2];
  83. result = sfud_read_status(spi->flash, &status, SFUD_CMD_READ_STATUS_REGISTER2);
  84. if (result != SFUD_SUCCESS) {
  85. printf("Error: Read_status register2 failed.");
  86. return result;
  87. }
  88. status |= (1 << 1);
  89. sfud_write_status(spi->flash, false, status, SFUD_CMD_WRITE_STATUS_REGISTER2);
  90. if (result != SFUD_SUCCESS) {
  91. printf("Error: Write_status register2 failed.");
  92. return result;
  93. }
  94. #endif
  95. return result;
  96. }
  97. static sfud_err exit_qspi_mode(const sfud_spi *spi)
  98. {
  99. sfud_err result = SFUD_SUCCESS;
  100. #if 0
  101. uint8_t status;
  102. uint8_t cmd[2];
  103. result = sfud_read_status(spi->flash, &status, SFUD_CMD_READ_STATUS_REGISTER2);
  104. if (result != SFUD_SUCCESS) {
  105. printf("Error: Read_status register2 failed.");
  106. return result;
  107. }
  108. status &= ~(1 << 1);
  109. sfud_write_status(spi->flash, false, status, SFUD_CMD_WRITE_STATUS_REGISTER2);
  110. if (result != SFUD_SUCCESS) {
  111. printf("Error: Write_status register2 failed.");
  112. return result;
  113. }
  114. #endif
  115. return result;
  116. }
  117. /**
  118. * read flash data by QSPI
  119. */
  120. static sfud_err qspi_read(const sfud_spi *spi, uint32_t addr, sfud_qspi_read_cmd_format *qspi_read_cmd_format,
  121. uint8_t *read_buf, size_t read_size) {
  122. sfud_err result = SFUD_SUCCESS;
  123. struct spi_slave *slave;
  124. struct qspi_message qspi_message = {0};
  125. uint32_t align_size, left_size;
  126. uint32_t retries = 0;
  127. configASSERT(spi);
  128. configASSERT(qspi_read_cmd_format);
  129. configASSERT(read_buf);
  130. slave = spi->user_data;
  131. configASSERT(slave);
  132. configASSERT(slave != NULL);
  133. xSemaphoreTake(slave->xMutex, portMAX_DELAY);
  134. retry:
  135. align_size = read_size & ~31;
  136. left_size = read_size & 31;
  137. if (align_size) {
  138. enter_qspi_mode(spi);
  139. /* initial message */
  140. qspi_message.message.recv_buf = read_buf;
  141. qspi_message.message.length = align_size;
  142. qspi_message.message.cs_take = 1;
  143. qspi_message.message.cs_release = 1;
  144. qspi_message.instruction.content = qspi_read_cmd_format->instruction;
  145. qspi_message.instruction.qspi_lines = qspi_read_cmd_format->instruction_lines;
  146. qspi_message.address.content = addr;
  147. qspi_message.address.size = qspi_read_cmd_format->address_size;
  148. qspi_message.address.qspi_lines = qspi_read_cmd_format->address_lines;
  149. qspi_message.dummy_cycles = qspi_read_cmd_format->dummy_cycles;
  150. qspi_message.qspi_data_lines = qspi_read_cmd_format->data_lines;
  151. /* transfer message */
  152. result = slave->qspi_read(slave, &qspi_message);
  153. exit_qspi_mode(spi);
  154. if (result) {
  155. if (retries++ < 3) {
  156. goto retry;
  157. }
  158. result = SFUD_ERR_READ;
  159. goto __exit;
  160. }
  161. }
  162. if (left_size) {
  163. uint8_t cmd_data[5], cmd_size;
  164. int i;
  165. cmd_data[0] = SFUD_CMD_READ_DATA;
  166. cmd_size = spi->flash->addr_in_4_byte ? 5 : 4;
  167. addr += align_size;
  168. for (i = 1; i < cmd_size; i++)
  169. cmd_data[i] = (addr >> ((cmd_size - (i + 1)) * 8)) & 0xFF;
  170. result = spi->wr(spi, cmd_data, cmd_size, read_buf + align_size, left_size);
  171. }
  172. __exit:
  173. xSemaphoreGive(slave->xMutex);
  174. return result;
  175. }
  176. #endif /* SFUD_USING_QSPI */
  177. static void spi_lock(const sfud_spi *spi)
  178. {
  179. struct spi_slave *slave;
  180. configASSERT(spi);
  181. slave = spi->user_data;
  182. configASSERT(slave);
  183. xSemaphoreTake(slave->xSfudMutex, portMAX_DELAY);
  184. }
  185. static void spi_unlock(const sfud_spi *spi)
  186. {
  187. struct spi_slave *slave;
  188. configASSERT(spi);
  189. slave = spi->user_data;
  190. configASSERT(slave);
  191. xSemaphoreGive(slave->xSfudMutex);
  192. }
  193. static void retry_delay_100us(void) {
  194. /* 100 microsecond delay */
  195. vTaskDelay((configTICK_RATE_HZ * 1 + 9999) / 10000);
  196. }
  197. sfud_err sfud_spi_port_init(sfud_flash *flash) {
  198. sfud_err result = SFUD_SUCCESS;
  199. struct spi_slave *slave;
  200. struct spi_configuration config = SFUD_DEFAULT_SPI_CFG;
  201. /**
  202. * add your port spi bus and device object initialize code like this:
  203. * 1. rcc initialize
  204. * 2. gpio initialize
  205. * 3. spi device initialize
  206. * 4. flash->spi and flash->retry item initialize
  207. * flash->spi.wr = spi_write_read; //Required
  208. * flash->spi.qspi_read = qspi_read; //Required when QSPI mode enable
  209. * flash->spi.lock = spi_lock;
  210. * flash->spi.unlock = spi_unlock;
  211. * flash->spi.user_data = &spix;
  212. * flash->retry.delay = null;
  213. * flash->retry.times = 10000; //Required
  214. */
  215. slave = spi_open(flash->spi.name);
  216. if (!slave) {
  217. printf("%s open fail.\n", flash->spi.name);
  218. return SFUD_ERR_NOT_FOUND;
  219. }
  220. slave->xSfudMutex = xSemaphoreCreateMutex();
  221. spi_configure(slave, &config);
  222. flash->spi.wr = spi_write_read;
  223. #ifdef SFUD_USING_QSPI
  224. flash->spi.qspi_read = qspi_read;
  225. #endif
  226. flash->spi.lock = spi_lock;
  227. flash->spi.unlock = spi_unlock;
  228. flash->spi.user_data = slave;
  229. /* 100 microsecond delay */
  230. flash->retry.delay = retry_delay_100us;
  231. /* 60 seconds timeout */
  232. flash->retry.times = 60 * 10000;
  233. flash->spi.flash = flash;
  234. return result;
  235. }
  236. /**
  237. * This function is print debug info.
  238. *
  239. * @param file the file which has call this function
  240. * @param line the line number which has call this function
  241. * @param format output format
  242. * @param ... args
  243. */
  244. void sfud_log_debug(const char *file, const long line, const char *format, ...) {
  245. va_list args;
  246. /* args point to the first variable parameter */
  247. va_start(args, format);
  248. printf("[SFUD](%s:%ld) ", file, line);
  249. /* must use vprintf to print */
  250. vsnprintf(log_buf, sizeof(log_buf), format, args);
  251. printf("%s\n", log_buf);
  252. va_end(args);
  253. }
  254. /**
  255. * This function is print routine info.
  256. *
  257. * @param format output format
  258. * @param ... args
  259. */
  260. void sfud_log_info(const char *format, ...) {
  261. va_list args;
  262. /* args point to the first variable parameter */
  263. va_start(args, format);
  264. printf("[SFUD]");
  265. /* must use vprintf to print */
  266. vsnprintf(log_buf, sizeof(log_buf), format, args);
  267. printf("%s\n", log_buf);
  268. va_end(args);
  269. }