mtd.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * File : mtd.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-12-05 Bernard the first version
  13. */
  14. /*
  15. * COPYRIGHT (C) 2012, Shanghai Real Thread
  16. */
  17. #ifndef __MTD_H__
  18. #define __MTD_H__
  19. #include <rtdevice.h>
  20. struct rt_mtd_driver_ops;
  21. #define RT_MTD_DEVICE(device) ((struct rt_mtd_device*)(device))
  22. struct rt_mtd_device
  23. {
  24. struct rt_device parent;
  25. rt_uint32_t page_size; /* The Page size in the flash */
  26. rt_uint32_t block_size; /* The Block size in the flash */
  27. rt_uint16_t oob_size; /* Out of bank size */
  28. rt_uint16_t reserve;
  29. rt_uint32_t block_start; /* The start of available block*/
  30. rt_uint32_t block_end; /* The end of available block */
  31. /* operations interface */
  32. const struct rt_mtd_driver_ops* ops;
  33. };
  34. struct rt_mtd_driver_ops
  35. {
  36. rt_uint32_t (*read_id) (struct rt_mtd_device* device);
  37. int (*read) (struct rt_mtd_device* device, rt_off_t position, rt_uint8_t* data, rt_size_t size);
  38. int (*write)(struct rt_mtd_device* device, rt_off_t position, const rt_uint8_t* data, rt_size_t size);
  39. int (*oob_read) (struct rt_mtd_device* device, rt_off_t position, rt_uint8_t* spare);
  40. int (*oob_write)(struct rt_mtd_device* device, rt_off_t position, const rt_uint8_t* spare);
  41. rt_err_t (*erase_block)(struct rt_mtd_device* device, rt_uint32_t block);
  42. };
  43. rt_err_t rt_mtd_register_device(const char* name, struct rt_mtd_device* device);
  44. rt_inline rt_uint32_t rt_mtd_read_id(struct rt_mtd_device* device)
  45. {
  46. return device->ops->read_id(device);
  47. }
  48. rt_inline int rt_mtd_read(struct rt_mtd_device* device, rt_off_t position,
  49. rt_uint8_t* data, rt_size_t size)
  50. {
  51. return device->ops->read(device, position, data, size);
  52. }
  53. rt_inline int rt_mtd_write(struct rt_mtd_device* device, rt_off_t position,
  54. const rt_uint8_t* data, rt_size_t size)
  55. {
  56. return device->ops->write(device, position, data, size);
  57. }
  58. rt_inline int rt_mtd_oob_read(struct rt_mtd_device* device, rt_off_t position,
  59. rt_uint8_t* data)
  60. {
  61. return device->ops->oob_read(device, position, data);
  62. }
  63. rt_inline int rt_mtd_oob_write(struct rt_mtd_device* device, rt_off_t position,
  64. const rt_uint8_t* data)
  65. {
  66. return device->ops->oob_write(device, position, data);
  67. }
  68. rt_inline rt_err_t rt_mtd_erase_block(struct rt_mtd_device* device, rt_uint32_t block)
  69. {
  70. return device->ops->erase_block(device, block);
  71. }
  72. #endif