Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

139 рядки
3.3 KiB

  1. /*
  2. * Lua RTOS, SPIFFS low access
  3. *
  4. * Copyright (C) 2015 - 2017
  5. * IBEROXARXA SERVICIOS INTEGRALES, S.L. & CSS IBÉRICA, S.L.
  6. *
  7. * Author: Jaume Olivé (jolive@iberoxarxa.com / jolive@whitecatboard.org)
  8. *
  9. * All rights reserved.
  10. *
  11. * Permission to use, copy, modify, and distribute this software
  12. * and its documentation for any purpose and without fee is hereby
  13. * granted, provided that the above copyright notice appear in all
  14. * copies and that both that the copyright notice and this
  15. * permission notice and warranty disclaimer appear in supporting
  16. * documentation, and that the name of the author not be used in
  17. * advertising or publicity pertaining to distribution of the
  18. * software without specific, written prior permission.
  19. *
  20. * The author disclaim all warranties with regard to this
  21. * software, including all implied warranties of merchantability
  22. * and fitness. In no event shall the author be liable for any
  23. * special, indirect or consequential damages or any damages
  24. * whatsoever resulting from loss of use, data or profits, whether
  25. * in an action of contract, negligence or other tortious action,
  26. * arising out of or in connection with the use or performance of
  27. * this software.
  28. */
  29. #include <stdlib.h>
  30. #include "esp_spiffs.h"
  31. #include "esp_attr.h"
  32. #include "spiffs.h"
  33. #include <esp_spi_flash.h>
  34. s32_t IRAM_ATTR esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) {
  35. u32_t aaddr;
  36. u8_t *buff = NULL;
  37. u8_t *abuff = NULL;
  38. u32_t asize;
  39. asize = size;
  40. // Align address to 4 byte
  41. aaddr = (addr + (4 - 1)) & (u32_t)-4;
  42. if (aaddr != addr) {
  43. aaddr -= 4;
  44. asize += (addr - aaddr);
  45. }
  46. // Align size to 4 byte
  47. asize = (asize + (4 - 1)) & (u32_t)-4;
  48. if ((aaddr != addr) || (asize != size)) {
  49. // Align buffer
  50. buff = malloc(asize + 4);
  51. if (!buff) {
  52. return SPIFFS_ERR_INTERNAL;
  53. }
  54. abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & (u32_t)-4);
  55. if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) {
  56. free(buff);
  57. return SPIFFS_ERR_INTERNAL;
  58. }
  59. memcpy(dst, abuff + (addr - aaddr), size);
  60. free(buff);
  61. } else {
  62. if (spi_flash_read(addr, (void *)dst, size) != 0) {
  63. return SPIFFS_ERR_INTERNAL;
  64. }
  65. }
  66. return SPIFFS_OK;
  67. }
  68. s32_t IRAM_ATTR esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) {
  69. u32_t aaddr;
  70. u8_t *buff = NULL;
  71. u8_t *abuff = NULL;
  72. u32_t asize;
  73. asize = size;
  74. // Align address to 4 byte
  75. aaddr = (addr + (4 - 1)) & -4;
  76. if (aaddr != addr) {
  77. aaddr -= 4;
  78. asize += (addr - aaddr);
  79. }
  80. // Align size to 4 byte
  81. asize = (asize + (4 - 1)) & -4;
  82. if ((aaddr != addr) || (asize != size)) {
  83. // Align buffer
  84. buff = malloc(asize + 4);
  85. if (!buff) {
  86. return SPIFFS_ERR_INTERNAL;
  87. }
  88. abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & -4);
  89. if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) {
  90. free(buff);
  91. return SPIFFS_ERR_INTERNAL;
  92. }
  93. memcpy(abuff + (addr - aaddr), src, size);
  94. if (spi_flash_write(aaddr, (uint32_t *)abuff, asize) != 0) {
  95. free(buff);
  96. return SPIFFS_ERR_INTERNAL;
  97. }
  98. free(buff);
  99. } else {
  100. if (spi_flash_write(addr, (uint32_t *)src, size) != 0) {
  101. return SPIFFS_ERR_INTERNAL;
  102. }
  103. }
  104. return SPIFFS_OK;
  105. }
  106. s32_t IRAM_ATTR esp32_spi_flash_erase(u32_t addr, u32_t size) {
  107. if (spi_flash_erase_sector(addr >> 12) != 0) {
  108. return SPIFFS_ERR_INTERNAL;
  109. }
  110. return SPIFFS_OK;
  111. }