progmem.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /****************************************************************************
  2. * include/nuttx/progmem.h
  3. *
  4. * Copyright(C) 2011 Uros Platise. All rights reserved.
  5. * Author: Uros Platise <uros.platise@isotel.eu>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef __INCLUDE_NUTTX_PROGMEM_H
  36. #define __INCLUDE_NUTTX_PROGMEM_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <sys/types.h>
  42. #include <stdbool.h>
  43. #ifdef CONFIG_ARCH_HAVE_PROGMEM
  44. /****************************************************************************
  45. * Pre-processor Definitions
  46. ****************************************************************************/
  47. #undef EXTERN
  48. #if defined(__cplusplus)
  49. #define EXTERN extern "C"
  50. extern "C"
  51. {
  52. #else
  53. #define EXTERN extern
  54. #endif
  55. /****************************************************************************
  56. * Public Functions
  57. ****************************************************************************/
  58. /****************************************************************************
  59. * Name: up_progmem_neraseblocks
  60. *
  61. * Description:
  62. * Return number of erase blocks
  63. *
  64. ****************************************************************************/
  65. size_t up_progmem_neraseblocks(void);
  66. /****************************************************************************
  67. * Name: up_progmem_isuniform
  68. *
  69. * Description:
  70. * Is program memory uniform or erase page and read/write page size differs?
  71. *
  72. ****************************************************************************/
  73. bool up_progmem_isuniform(void);
  74. /****************************************************************************
  75. * Name: up_progmem_pagesize
  76. *
  77. * Description:
  78. * Return read/write page size
  79. *
  80. ****************************************************************************/
  81. size_t up_progmem_pagesize(size_t page);
  82. /****************************************************************************
  83. * Name: up_progmem_erasesize
  84. *
  85. * Description:
  86. * Return erase block size. Must be a multiple of the read/write page size.
  87. *
  88. ****************************************************************************/
  89. size_t up_progmem_erasesize(size_t block);
  90. /****************************************************************************
  91. * Name: up_progmem_getpage
  92. *
  93. * Description:
  94. * Address to read/write page conversion
  95. *
  96. * Input Parameters:
  97. * addr - Address with or without flash offset (absolute or aligned to page0)
  98. *
  99. * Returned Value:
  100. * Page or negative value on error. The following errors are reported
  101. * (errno is not set!):
  102. *
  103. * -EFAULT: On invalid address
  104. *
  105. ****************************************************************************/
  106. ssize_t up_progmem_getpage(size_t addr);
  107. /****************************************************************************
  108. * Name: up_progmem_getaddress
  109. *
  110. * Description:
  111. * Read/write page to address conversion
  112. *
  113. * Input Parameters:
  114. * page - page index
  115. *
  116. * Returned Value:
  117. * Base address of given page, SIZE_MAX if page index is not valid.
  118. *
  119. ****************************************************************************/
  120. size_t up_progmem_getaddress(size_t page);
  121. /****************************************************************************
  122. * Name: up_progmem_eraseblock
  123. *
  124. * Description:
  125. * Erase selected erase block.
  126. *
  127. * Input Parameters:
  128. * block - The erase block index to be erased.
  129. *
  130. * Returned Value:
  131. * block size or negative value on error. The following errors are reported
  132. * (errno is not set!):
  133. *
  134. * -EFAULT: On invalid page
  135. * -EIO: On unsuccessful erase
  136. * -EROFS: On access to write protected area
  137. * -EACCES: Insufficient permissions (read/write protected)
  138. * -EPERM: If operation is not permitted due to some other constraints
  139. * (i.e. some internal block is not running etc.)
  140. *
  141. ****************************************************************************/
  142. ssize_t up_progmem_eraseblock(size_t block);
  143. /****************************************************************************
  144. * Name: up_progmem_ispageerased
  145. *
  146. * Description:
  147. * Checks whether erase page is erased
  148. *
  149. * Input Parameters:
  150. * page - The page index to be checked.
  151. *
  152. * Returned Value:
  153. * Returns number of bytes NOT erased or negative value on error. If it
  154. * returns zero then complete page is erased.
  155. *
  156. * The following errors are reported:
  157. * -EFAULT: On invalid page
  158. *
  159. ****************************************************************************/
  160. ssize_t up_progmem_ispageerased(size_t page);
  161. /****************************************************************************
  162. * Name: up_progmem_write
  163. *
  164. * Description:
  165. * Program data at given address
  166. *
  167. * Note: this function is not limited to single page and nor it requires
  168. * the address be aligned inside the page boundaries.
  169. *
  170. * Input Parameters:
  171. * addr - Address with or without flash offset (absolute or aligned to page0)
  172. * buf - Pointer to buffer
  173. * count - Number of bytes to write
  174. *
  175. * Returned Value:
  176. * Bytes written or negative value on error. The following errors are
  177. * reported (errno is not set!)
  178. *
  179. * EINVAL: If count is not aligned with the flash boundaries (i.e.
  180. * some MCU's require per half-word or even word access)
  181. * EFAULT: On invalid address
  182. * EIO: On unsuccessful write
  183. * EROFS: On access to write protected area
  184. * EACCES: Insufficient permissions (read/write protected)
  185. * EPERM: If operation is not permitted due to some other constraints
  186. * (i.e. some internal block is not running etc.)
  187. *
  188. ****************************************************************************/
  189. ssize_t up_progmem_write(size_t addr, FAR const void *buf, size_t count);
  190. #undef EXTERN
  191. #if defined(__cplusplus)
  192. }
  193. #endif
  194. #endif /* CONFIG_ARCH_HAVE_PROGMEM */
  195. #endif /* __INCLUDE_NUTTX_PROGMEM_H */