mtd_nor.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * File : mtd_nor.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, Shanghai Real-Thread Technology Co., Ltd
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-5-30 Bernard the first version
  23. */
  24. #ifndef __MTD_NOR_H__
  25. #define __MTD_NOR_H__
  26. #include <rtdevice.h>
  27. struct rt_mtd_nor_driver_ops;
  28. #define RT_MTD_NOR_DEVICE(device) ((struct rt_mtd_nor_device*)(device))
  29. struct rt_mtd_nor_device
  30. {
  31. struct rt_device parent;
  32. rt_uint32_t block_size; /* The Block size in the flash */
  33. rt_uint32_t block_start; /* The start of available block*/
  34. rt_uint32_t block_end; /* The end of available block */
  35. /* operations interface */
  36. const struct rt_mtd_nor_driver_ops* ops;
  37. };
  38. struct rt_mtd_nor_driver_ops
  39. {
  40. rt_err_t (*read_id) (struct rt_mtd_nor_device* device);
  41. rt_size_t (*read) (struct rt_mtd_nor_device* device, rt_off_t offset, rt_uint8_t* data, rt_uint32_t length);
  42. rt_size_t (*write) (struct rt_mtd_nor_device* device, rt_off_t offset, const rt_uint8_t* data, rt_uint32_t length);
  43. rt_err_t (*erase_block)(struct rt_mtd_nor_device* device, rt_off_t offset, rt_uint32_t length);
  44. };
  45. rt_err_t rt_mtd_nor_register_device(const char* name, struct rt_mtd_nor_device* device);
  46. rt_inline rt_uint32_t rt_mtd_nor_read_id(struct rt_mtd_nor_device* device)
  47. {
  48. return device->ops->read_id(device);
  49. }
  50. rt_inline rt_size_t rt_mtd_nor_read(
  51. struct rt_mtd_nor_device* device,
  52. rt_off_t offset, rt_uint8_t* data, rt_uint32_t length)
  53. {
  54. return device->ops->read(device, offset, data, length);
  55. }
  56. rt_inline rt_size_t rt_mtd_nor_write(
  57. struct rt_mtd_nor_device* device,
  58. rt_off_t offset, const rt_uint8_t* data, rt_uint32_t length)
  59. {
  60. return device->ops->write(device, offset, data, length);
  61. }
  62. rt_inline rt_err_t rt_mtd_nor_erase_block(struct rt_mtd_nor_device* device, rt_off_t offset, rt_size_t length)
  63. {
  64. return device->ops->erase_block(device, offset, length);
  65. }
  66. #endif