air105_qspi.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef AIRMCU_FLASH
  22. #define AIRMCU_FLASH
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. #include <stdint.h>
  28. #include "air105.h"
  29. /* Size of the flash */
  30. #define X25Q_PAGE_SIZE 0x100
  31. // Identification Operations
  32. /***Command Definitions***/
  33. // Program Operations
  34. #define PAGE_PROG_CMD 0x02
  35. #define QUAD_INPUT_PAGE_PROG_CMD 0x32
  36. // Erase Operations
  37. #define SECTOR_ERASE_CMD 0x20
  38. #define CHIP_ERASE_CMD 0xC7
  39. #define PROG_ERASE_RESUME_CMD 0x7A
  40. #define PROG_ERASE_SUSPEND_CMD 0x75
  41. #define SET_BURST_WITH_WRAP 0x77
  42. #define RELEASE_FROM_DEEP_POWER_DOWN 0xAB
  43. #define DEEP_POWER_DOWN 0xB9
  44. /***End Cmd***/
  45. #define QSPI_DEVICE_PARA_SAMPLE_DLY_Pos 15
  46. #define QSPI_DEVICE_PARA_SAMPLE_PHA_Pos 14
  47. #define QSPI_DEVICE_PARA_PROTOCOL_Pos 8
  48. #define QSPI_DEVICE_PARA_DUMMY_CYCLE_Pos 4
  49. #define ROM_QSPI_Init (*((void (*)(QSPI_InitTypeDef *))(*(uint32_t *)0x8010)))
  50. #define ROM_QSPI_ReadID (*((uint32_t (*)(QSPI_CommandTypeDef *))(*(uint32_t *)0x8014)))
  51. #define ROM_QSPI_WriteParam (*((uint8_t (*)(QSPI_CommandTypeDef *, uint16_t))(*(uint32_t *)0x8018)))
  52. #define ROM_QSPI_EraseSector (*((uint8_t (*)(QSPI_CommandTypeDef *, uint32_t))(*(uint32_t *)0x801C)))
  53. #define ROM_QSPI_ProgramPage (*((uint8_t (*)(QSPI_CommandTypeDef *, DMA_TypeDef *, uint32_t, uint32_t, uint8_t *))(*(uint32_t *)0x8024)))
  54. #define ROM_QSPI_ReleaseDeepPowerDown (*((uint8_t (*)(QSPI_CommandTypeDef *))(*(uint32_t *)0x802C)))
  55. #define ROM_QSPI_StatusReg (*((uint16_t (*)(QSPI_CommandTypeDef *))(*(uint32_t *)0x8034)))
  56. typedef enum
  57. {
  58. QSPI_BUSMODE_111 = 0x00, //CMD-ADDR-DATA = 1-1-1
  59. QSPI_BUSMODE_114 = 0x01, //CMD-ADDR-DATA = 1-1-4
  60. QSPI_BUSMODE_144 = 0x02, //CMD-ADDR-DATA = 1-4-4
  61. QSPI_BUSMODE_444 = 0x03, //CMD-ADDR-DATA = 4-4-4
  62. }QSPI_BusModeTypeDef;
  63. typedef enum
  64. {
  65. QSPI_CMDFORMAT_CMD8 = 0x00,
  66. QSPI_CMDFORMAT_CMD8_RREG8 = 0x01,
  67. QSPI_CMDFORMAT_CMD8_RREG16 = 0x02,
  68. QSPI_CMDFORMAT_CMD8_RREG24 = 0x03,
  69. QSPI_CMDFORMAT_CMD8_DMY24_WREG8 = 0x04,
  70. QSPI_CMDFORMAT_CMD8_ADDR24_RREG8 = 0x05,
  71. QSPI_CMDFORMAT_CMD8_ADDR24_RREG16 = 0x06,
  72. QSPI_CMDFORMAT_CMD8_WREG8 = 0x07,
  73. QSPI_CMDFORMAT_CMD8_WREG16 = 0x08,
  74. QSPI_CMDFORMAT_CMD8_ADDR24 = 0x09,
  75. QSPI_CMDFORMAT_CMD8_ADDR24_RDAT = 0x0A,
  76. QSPI_CMDFORMAT_CMD8_ADDR24_DMY_RDAT = 0x0B,
  77. QSPI_CMDFORMAT_CMD8_ADDR24_M8_DMY_RDAT = 0x0C,
  78. QSPI_CMDFORMAT_CMD8_ADDR24_PDAT = 0x0D
  79. }QSPI_CmdFormatTypeDef;
  80. typedef enum
  81. {
  82. QSPI_PROTOCOL_CLPL = 0x00,
  83. QSPI_PROTOCOL_CHPH = 0x03
  84. }QSPI_ProtocolTypedef;
  85. typedef enum
  86. {
  87. QSPI_FREQSEL_HCLK_DIV2 = 0x01,
  88. QSPI_FREQSEL_HCLK_DIV3 = 0x02,
  89. QSPI_FREQSEL_HCLK_DIV4 = 0x03
  90. }QSPI_FreqSelTypeDef;
  91. typedef struct
  92. {
  93. //Device Para
  94. uint8_t SampleDly; //Default:0
  95. uint8_t SamplePha; //Default:0
  96. uint8_t ProToCol; //Defualt: QSPI_PROTOCOL_CLPL
  97. uint8_t DummyCycles; //Include M7:0 Defualt: 6
  98. uint8_t FreqSel; //Defualt: QSPI_FREQSEL_HCLK_DIV4
  99. //Setting Cache
  100. uint8_t Cache_Cmd_ReleaseDeepInstruction; //Defualt: 0xAB
  101. uint8_t Cache_Cmd_DeepInstruction; //Defualt: 0xB9
  102. uint8_t Cache_Cmd_ReadBusMode; //Defualt: QSPI_BUSMODE_144
  103. uint8_t Cache_Cmd_ReadFormat; //Defualt: QSPI_CMDFORMAT_CMD8_ADDR24_DMY_RDAT
  104. uint8_t Cache_Cmd_ReadInstruction; //Defualt: 0xEB
  105. }QSPI_InitTypeDef;
  106. typedef struct
  107. {
  108. uint8_t Instruction;
  109. uint32_t BusMode;
  110. uint32_t CmdFormat;
  111. }QSPI_CommandTypeDef;
  112. void QSPI_Init(QSPI_InitTypeDef *mhqspi);
  113. void QSPI_SetLatency(uint32_t u32UsClk);
  114. uint8_t FLASH_EraseSector(uint32_t sectorAddress);
  115. uint8_t FLASH_ProgramPage(uint32_t addr, uint32_t size, uint8_t *buffer);
  116. #ifdef __cplusplus
  117. }
  118. #endif
  119. #endif