spi_flash.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file spi_flash.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __SPI_FLASH_H__
  35. #define __SPI_FLASH_H__
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #include <rtthread.h>
  40. #include "n32g45x.h"
  41. /** @addtogroup Utilities
  42. * @{
  43. */
  44. /** @addtogroup
  45. * @{
  46. */
  47. /** @addtogroup Common
  48. * @{
  49. */
  50. /** @addtogroup SPI_FLASH
  51. * @{
  52. */
  53. /** @addtogroup SPI_FLASH_Exported_Types
  54. * @{
  55. */
  56. /**
  57. * @}
  58. */
  59. /** @addtogroup SPI_FLASH_Exported_Constants
  60. * @{
  61. */
  62. /**
  63. * @brief SPI Flash supported commands
  64. */
  65. /* constants definitions */
  66. /* SPI and I2S parameter struct definitions */
  67. typedef struct
  68. {
  69. uint32_t device_mode; /*!< SPI master or slave */
  70. uint32_t trans_mode; /*!< SPI transtype */
  71. uint32_t frame_size; /*!< SPI frame size */
  72. uint32_t nss; /*!< SPI NSS control by handware or software */
  73. uint32_t endian; /*!< SPI big endian or little endian */
  74. uint32_t clock_polarity_phase; /*!< SPI clock phase and polarity */
  75. uint32_t prescale; /*!< SPI prescale factor */
  76. }spi_parameter_struct;
  77. struct spi_flash_device
  78. {
  79. struct rt_device flash_device;
  80. struct rt_device_blk_geometry geometry;
  81. struct rt_spi_device * rt_spi_device;
  82. struct rt_mutex lock;
  83. void * user_data;
  84. };
  85. typedef struct spi_flash_device *rt_spi_flash_device_t;
  86. #ifdef RT_USING_MTD_NOR
  87. struct spi_flash_mtd
  88. {
  89. struct rt_mtd_nor_device mtd_device;
  90. struct rt_spi_device * rt_spi_device;
  91. struct rt_mutex lock;
  92. void * user_data;
  93. };
  94. #endif
  95. typedef enum
  96. {
  97. RT_CMD_W25_ERASE_SECTOR = 1,
  98. RT_CMD_W25_ERASE_CHIP = 2,
  99. RT_CMD_W25_READ_ID = 3,
  100. } W25_COMMAND;
  101. #define sFLASH_CMD_WRITE 0x02 /*!< Write to Memory instruction */
  102. #define sFLASH_CMD_WRSR 0x01 /*!< Write Status Register instruction */
  103. #define sFLASH_CMD_WREN 0x06 /*!< Write enable instruction */
  104. #define sFLASH_CMD_READ 0x03 /*!< Read from Memory instruction */
  105. #define sFLASH_CMD_RDSR 0x05 /*!< Read Status Register instruction */
  106. #define sFLASH_CMD_RDID 0x9F /*!< Read identification */
  107. #define sFLASH_CMD_SE 0xD8 /*!< Sector Erase instruction */
  108. #define sFLASH_CMD_BE 0xC7 /*!< Bulk Erase instruction */
  109. #define sFLASH_WIP_FLAG 0x01 /*!< Write In Progress (WIP) flag */
  110. #define sFLASH_DUMMY_BYTE 0xA5
  111. #define sFLASH_SPI_PAGESIZE 0x100
  112. #define sFLASH_W25Q128_ID 0xEF17
  113. #define sFLASH_M25P64_ID 0x202017
  114. #define sFLASH_SPI SPI1
  115. #define sFLASH_CS_PIN GPIO_PIN_4 /* PA.04 */
  116. #define sFLASH_CS_GPIO_PORT GPIOA /* GPIOA */
  117. #define sFLASH_CS_GPIO_CLK RCC_APB2_PERIPH_GPIOA
  118. /**
  119. * @}
  120. */
  121. /** @addtogroup SPI_FLASH_Exported_Macros
  122. * @{
  123. */
  124. /**
  125. * @brief Select sFLASH: Chip Select pin low
  126. */
  127. #define sFLASH_CS_LOW() GPIO_ResetBits(sFLASH_CS_GPIO_PORT, sFLASH_CS_PIN)
  128. /**
  129. * @brief Deselect sFLASH: Chip Select pin high
  130. */
  131. #define sFLASH_CS_HIGH() GPIO_SetBits(sFLASH_CS_GPIO_PORT, sFLASH_CS_PIN)
  132. /**
  133. * @}
  134. */
  135. rt_err_t rt_flash_register(rt_uint8_t flag);
  136. #ifdef __cplusplus
  137. }
  138. #endif
  139. #endif
  140. /**
  141. * @}
  142. */
  143. /**
  144. * @}
  145. */
  146. /**
  147. * @}
  148. */
  149. /**
  150. * @}
  151. */
  152. /**
  153. * @}
  154. */