at45db041.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * at45db041.h
  3. *
  4. * Created on: Apr 26, 2024
  5. * Author: ngrigoriadis
  6. */
  7. #ifndef AT45DB041_H_
  8. #define AT45DB041_H_
  9. #include "main.h"
  10. /*Define the page size*/
  11. #define BINARY_PAGE_SIZE
  12. /*Define status register bits*/
  13. #define RDY_BIT 0x80 /*Device is busy=0, Device is ready=1*/
  14. #define RES_BIT 0x40 /*Reserved for future use*/
  15. #define EPE_BIT 0x20 /*Success program/erase=0, Failure erase/program=1*/
  16. #define SLE_BIT 0x08 /*Sector LockDown disable=0, Sector LockDown enable=1*/
  17. #define PS2_BIT 0x04 /*Program suspend status (Buffer 2)*/
  18. #define PS1_BIT 0x02 /*Program suspend status (Buffer 1)*/
  19. #define ES_BIT 0x01 /*Erase suspend*/
  20. #define PGS_BIT 0x01 /*Page size configuration, 0=264, 1=256*/
  21. #define PRCT_BIT 0x02 /*Sector protection status*/
  22. #define COMP_BIT 0x40 /*Compare result*/
  23. /*Define OPCODES for external flash memory*/
  24. #define MAN_ID_OPCD 0x9F
  25. #define SOFT_RST_CMD_1 0xF0
  26. #define SOFT_RST_CMD_2 0x00
  27. #define SOFT_RST_CMD_3 0x00
  28. #define SOFT_RST_CMD_4 0x00
  29. #define CHIP_ERASE_1 0xC7
  30. #define CHIP_ERASE_2 0x94
  31. #define CHIP_ERASE_3 0x80
  32. #define CHIP_ERASE_4 0x9A
  33. #define DEEP_SLEEP 0xB9
  34. #define WAKE_UP_DEEP_SLEEP 0xAB
  35. #define ULTRA_DEEP_SLEEP 0x79
  36. #define STATUS_REGISTER 0xD7
  37. #define CONTINUOUS_ARRAY_READ 0xE8
  38. #define CONTINUOUS_ARRAY_READ_H_MODE1 0x1B
  39. #define CONTINUOUS_ARRAY_READ_H_MODE2 0x0B
  40. #define ENABLE_SECTOR_PROTECTION_1 0x3D
  41. #define ENABLE_SECTOR_PROTECTION_2 0x2A
  42. #define ENABLE_SECTOR_PROTECTION_3 0x7F
  43. #define ENABLE_SECTOR_PROTECTION_4 0x9A
  44. #define DISABLE_SECTOR_PROTECTION_1 0X3D
  45. #define DISABLE_SECTOR_PROTECTION_2 0x2A
  46. #define DISABLE_SECTOR_PROTECTION_3 0x7F
  47. #define DISABLE_SECTOR_PROTECTION_4 0x9A
  48. #define INTERNAL_BUFFER_1 0x84
  49. #define SECTOR_ERASE 0x7C
  50. #define BUFFER_TO_MAIN_1 0x83
  51. #define BUFFER_TO_MAIN_2 0x86
  52. #define PAGE_ERASE_CMD 0x81
  53. #define BIN_PG_SIZE_1 0x3D
  54. #define BIN_PG_SIZE_2 0x2A
  55. #define BIN_PG_SIZE_3 0x80
  56. #define BIN_PG_SIZE_4 0xA6
  57. #define BUFFER_2_READ_BINARY 0xD6
  58. #define FLASH_CS_PORT GPIOB
  59. #define FLASH_CS_PIN GPIO_PIN_6
  60. #define FLASH_CS_ENABLE() HAL_GPIO_WritePin(FLASH_CS_PORT, FLASH_CS_PIN, GPIO_PIN_RESET)
  61. #define FLASH_CS_DISABLE() HAL_GPIO_WritePin(FLASH_CS_PORT, FLASH_CS_PIN, GPIO_PIN_SET)
  62. #define PAGE_SHIFT 8 // если binary mode
  63. //#define PAGE_SHIFT 9 // если standard mode
  64. /**
  65. * @brief Enumerators about MAN ID.
  66. */
  67. typedef enum {
  68. DEV_ID_ERROR = 0,
  69. DEV_ID_OK = 1,
  70. }dev_id_t;
  71. /**
  72. * @brief Information about the operation of the external flash.
  73. */
  74. typedef enum {
  75. _DEEP_SLEEP = 0,
  76. _U_DEEP_SLEEP = 1,
  77. _NORMAL_MODE = 2,
  78. }at45db_op_info_t;
  79. /**
  80. * @brief Basic parameters for the external flash memory.
  81. */
  82. typedef struct at45db_ {
  83. at45db_op_info_t state;
  84. int flash_mbit;
  85. int page_size;
  86. int blocks;
  87. int block_size;
  88. }at45db_t;
  89. void at45db_init(void);
  90. void at45db_disable_protection(void);
  91. dev_id_t at45db_read_man_id(void);
  92. uint8_t at45db_get_status(void);
  93. uint16_t at45db_get_page_size(void);
  94. void at45db_chip_erase(void);
  95. void at45db_sector_erase(uint8_t sector_num);
  96. void at45db_page_erase(uint16_t page);
  97. void at45db_deep_sleep(at45db_t *info);
  98. void at45db_page_size_conf(uint16_t mode);
  99. uint16_t at45db_wake_up_from_deep_sleep(at45db_t info);
  100. void at45db_ultra_deep_sleep(at45db_t info);
  101. uint16_t at45db_wake_up_from_ultra_deep_sleep(at45db_t info);
  102. void at45db_read(uint16_t page, uint8_t *data, uint16_t size);
  103. int at45db_is_ready(void);
  104. uint16_t at45db_fault_check(void);
  105. void at45db_write(uint16_t page, uint8_t *data, uint16_t size);
  106. void at45db_write_page(uint16_t page, uint8_t *data);
  107. void at45db_read_continuous(uint32_t addr, uint8_t *buffer, uint32_t size);
  108. void at45db_wait_ready(void);
  109. #endif /* AT45DB041_H_ */