lib_ringfs_w25q.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
  3. * This program is free software. It comes without any warranty, to the extent
  4. * permitted by applicable law. You can redistribute it and/or modify it under
  5. * the terms of the Do What The Fuck You Want To Public License, Version 2, as
  6. * published by Sam Hocevar. See the COPYING file for more details.
  7. */
  8. #include "lib_ringfs.h"
  9. #include "lib_ringfs_func.h"
  10. #if USE_LIB_RINGFS_W25Q==1
  11. #include "obj_spi_w25qxx.h"
  12. #include "obj_hal_w25qxx.h"
  13. #include "obj_soft_w25qxx.h"
  14. #include "func_spi_w25qxx.h"
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <inttypes.h>
  19. #ifdef FLASHSIM_LOG
  20. #define logprintf(args...) printf(args)
  21. #else
  22. #define logprintf(args...) do {} while (0)
  23. #endif
  24. #define FLASH_W25Q_SECTOR_SIZE 4096
  25. #define FLASH_W25Q_PARTITION_OFFSET 16
  26. #define FLASH_W25Q_PARTITION_SIZE 6
  27. #define FLASH_W25Q_TOTAL_SECTORS (FLASH_W25Q_PARTITION_OFFSET + \
  28. FLASH_W25Q_PARTITION_SIZE)
  29. struct rfs_dev_w25q {
  30. char name[32];
  31. int total_size;
  32. int sector_size;
  33. };
  34. struct rfs_dev_w25q *rfs_dev_w25q_open(const char *name, int size, int sector_size);
  35. static ssize_t op_program(struct ringfs_flash_partition *flash, int address, const void *data, size_t size);
  36. static ssize_t op_read(struct ringfs_flash_partition *flash, int address, void *data, size_t size);
  37. static int op_sector_erase(struct ringfs_flash_partition *flash, int address);
  38. struct rfs_dev_w25q rfs_dev_w25q_obj = {0};
  39. struct rfs_dev_w25q *h_rfs_w25q = 0;
  40. struct ringfs_flash_partition rfs_disk_w25q = {
  41. .sector_size = FLASH_W25Q_SECTOR_SIZE,
  42. .sector_offset = FLASH_W25Q_PARTITION_OFFSET,
  43. .sector_count = FLASH_W25Q_PARTITION_SIZE,
  44. .sector_erase = op_sector_erase,
  45. .program = op_program,
  46. .read = op_read,
  47. };
  48. void init_flash_driver_w25q(void)
  49. {
  50. if(W25QXX_ID_OK() == true)
  51. {
  52. rfs_disk_w25q.sector_size = w25qxx_obj.SectorSize;
  53. //ringfs 的扇区数量为,总扇区数减去偏移量;偏移量暂定为16个扇区
  54. rfs_disk_w25q.sector_count = w25qxx_obj.SectorCount - rfs_disk_w25q.sector_offset;
  55. }
  56. else
  57. {
  58. return;
  59. }
  60. #if 0
  61. h_rfs_w25q = rfs_dev_w25q_open("rfs_disk.w25q",
  62. FLASH_W25Q_TOTAL_SECTORS*FLASH_W25Q_SECTOR_SIZE,
  63. FLASH_W25Q_SECTOR_SIZE);
  64. #else
  65. h_rfs_w25q = rfs_dev_w25q_open("rfs_disk.w25q",
  66. w25qxx_obj.SectorSize*w25qxx_obj.SectorCount,
  67. w25qxx_obj.SectorSize);
  68. #endif
  69. }
  70. static int op_sector_erase(struct ringfs_flash_partition *flash, int address)
  71. {
  72. (void) flash;
  73. int sector_start = address - (address % flash->sector_size);
  74. UNUSED(sector_start);
  75. logprintf("rfs_dev_w25q_erase (0x%08x) * erasing sector at 0x%08x\n", addr, sector_start);
  76. W25QXX_Erase_Sector(address/4096);
  77. return 0;
  78. }
  79. static ssize_t op_program(struct ringfs_flash_partition *flash, int address, const void *data, size_t size)
  80. {
  81. (void) flash;
  82. W25QXX_Write_NoCheck((uint8_t*)data, address, size);
  83. return size;
  84. }
  85. static ssize_t op_read(struct ringfs_flash_partition *flash, int address, void *data, size_t size)
  86. {
  87. (void) flash;
  88. W25QXX_Read((uint8_t*)data, address, size);;
  89. return size;
  90. }
  91. struct rfs_dev_w25q *rfs_dev_w25q_open(const char *name, int size, int sector_size)
  92. {
  93. func_w25q_init();
  94. func_w25q_test();
  95. struct rfs_dev_w25q *h_w25q = &rfs_dev_w25q_obj;
  96. h_w25q = &rfs_dev_w25q_obj;
  97. if(strlen(name) < sizeof(rfs_dev_w25q_obj.name))
  98. {
  99. strcpy(rfs_dev_w25q_obj.name,name);
  100. }
  101. h_w25q->total_size = size;
  102. h_w25q->sector_size = sector_size;
  103. return h_w25q;
  104. }
  105. void rfs_dev_w25q_close(struct rfs_dev_w25q *ram)
  106. {
  107. return;
  108. }
  109. #endif //---------------------USE_LIB_RINGFS_W25Q==1--------------------//
  110. /* vim: set ts=4 sw=4 et: */