| 1 |
9 |
nussgipfel |
/***********************************************************
|
| 2 |
|
|
* Gecko3 SoC HW/SW Development Board
|
| 3 |
|
|
* ___ ___ _ _
|
| 4 |
|
|
* ( _`\ ( __)( ) ( )
|
| 5 |
|
|
* | (_) )| ( | |_| | Berne University of Applied Sciences
|
| 6 |
|
|
* | _ <'| _) | _ | School of Engineering and
|
| 7 |
|
|
* | (_) )| | | | | | Information Technology
|
| 8 |
|
|
* (____/'(_) (_) (_)
|
| 9 |
|
|
*
|
| 10 |
|
|
*
|
| 11 |
|
|
* This program is free software: you can redistribute it and/or modify
|
| 12 |
|
|
* it under the terms of the GNU General Public License as published by
|
| 13 |
|
|
* the Free Software Foundation, either version 3 of the License, or
|
| 14 |
|
|
* (at your option) any later version.
|
| 15 |
|
|
*
|
| 16 |
|
|
* This program is distributed in the hope that it will be useful,
|
| 17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 19 |
|
|
* GNU General Public License for more details.
|
| 20 |
|
|
* You should have received a copy of the GNU General Public License
|
| 21 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 22 |
|
|
*/
|
| 23 |
|
|
|
| 24 |
|
|
/************************************************************/
|
| 25 |
|
|
/** \file spi_flash.h
|
| 26 |
|
|
*************************************************************
|
| 27 |
|
|
* \author Christoph Zimmermann
|
| 28 |
|
|
* \date Date of creation: 17.09.2007
|
| 29 |
|
|
* \brief Headerfile for the spi-flash Library
|
| 30 |
|
|
*
|
| 31 |
|
|
* \details Library to access the SPI Flash devices from ST
|
| 32 |
|
|
* Microelectronics (now Numonyx) or Spansion.
|
| 33 |
|
|
* Supported densities:
|
| 34 |
|
|
* 8, 16, 32 Mbit
|
| 35 |
|
|
*
|
| 36 |
|
|
* \note to use this SPI/SPI Flash library you have to define the following
|
| 37 |
|
|
* keywords in your pinmapping header file. For GECKO3COM
|
| 38 |
|
|
* this is the "gecko3com_regs.h" file. \n
|
| 39 |
|
|
* \li SPI_PORT, SPI signals are connected to this port
|
| 40 |
|
|
* \li SPI_OE, SPI port direction register
|
| 41 |
|
|
* \li bmSPI_CLK, bitmask for SPI serial clock pin
|
| 42 |
|
|
* \li bmSPI_MOSI, bitmask for SPI MOSI pin, Master Out, Slave In
|
| 43 |
|
|
* \li bmSPI_MISO, bitmask for SPI MISO pin, Master In, Slave Out
|
| 44 |
|
|
* \li bitSPI_CLK, bitadress of the SPI CLK pin
|
| 45 |
|
|
* \li bitSPI_MOSI, bitadress of the SPI MOSI pin
|
| 46 |
|
|
* \li bitSPI_MISO, bitadress of the SPI MISO pin
|
| 47 |
|
|
* \li SPI_CS_PORT, SPI chip select signals are connected to this port
|
| 48 |
|
|
* \li bmSPI_CS_FLASH, bitmask to enable the SPI Flash
|
| 49 |
|
|
* \li bmSPI_CS_MASK, bit mask to select the SPI chip select pins
|
| 50 |
|
|
*
|
| 51 |
|
|
* \date 17.09.2007 first version
|
| 52 |
|
|
*
|
| 53 |
|
|
*/
|
| 54 |
|
|
|
| 55 |
|
|
#ifndef SPI_FLASH /* prevent circular inclusions */
|
| 56 |
|
|
#define SPI_FLASH /* by using protection macros */
|
| 57 |
|
|
|
| 58 |
|
|
#include <stdint.h>
|
| 59 |
|
|
//#include "spi.h"
|
| 60 |
|
|
|
| 61 |
|
|
/**************************** Type Definitions *******************************/
|
| 62 |
|
|
|
| 63 |
|
|
/**
|
| 64 |
|
|
* \brief This typedef contains configuration information for the device.
|
| 65 |
|
|
*/
|
| 66 |
|
|
typedef struct
|
| 67 |
|
|
{
|
| 68 |
|
|
uint32_t maxAdress; /**< highest available adress */
|
| 69 |
|
|
uint32_t capacity; /**< memory capacity in bytes */
|
| 70 |
|
|
uint16_t pages; /**< number of available pages */
|
| 71 |
|
|
uint8_t sectors; /**< number of available sectors */
|
| 72 |
|
|
uint8_t isBusy; /**< acivity indicator */
|
| 73 |
|
|
} SPI_flash;
|
| 74 |
|
|
|
| 75 |
|
|
|
| 76 |
|
|
#define FLASH_WRITE_BUFFER_SIZE 0x100 /**< Write Buffer = 256 bytes */
|
| 77 |
|
|
|
| 78 |
|
|
#define MANUFACTURER_STM 0x20 /**< id of ST microelectronics */
|
| 79 |
|
|
#define MEMTYPE_STM 0x20 /**< Memory type of M25PXX */
|
| 80 |
|
|
#define MEMCAPACITY_8MBIT_STM 0x14 /**< 8 MBit memory capacity */
|
| 81 |
|
|
#define MEMCAPACITY_16MBIT_STM 0x15 /**< 16 MBit memory capacity */
|
| 82 |
|
|
#define MEMCAPACITY_32MBIT_STM 0x16 /**< 32 MBit memory capacity */
|
| 83 |
|
|
|
| 84 |
|
|
#define MANUFACTURER_SPA 0x01 /**< id of SPANSION */
|
| 85 |
|
|
#define MEMTYPE_SPA 0x02 /**< Memory type of S25FLXX */
|
| 86 |
|
|
#define MEMCAPACITY_8MBIT_SPA 0x13 /**< 8 MBit memory capacity */
|
| 87 |
|
|
#define MEMCAPACITY_16MBIT_SPA 0x14 /**< 16 MBit memory capacity */
|
| 88 |
|
|
#define MEMCAPACITY_32MBIT_SPA 0x15 /**< 32 MBit memory capacity */
|
| 89 |
|
|
|
| 90 |
|
|
|
| 91 |
|
|
#define MAXADRESS_8MBIT 0x0FFFFF
|
| 92 |
|
|
#define FLASH_SIZE_8MBIT (0x0100000) /**< Total 8 Mbit device size in Bytes */
|
| 93 |
|
|
#define FLASH_PAGE_COUNT_8MBIT (0x01000) /**< Total 8 Mbit device size in Pages */
|
| 94 |
|
|
#define FLASH_SECTOR_COUNT_8MBIT (0x10) /**< Total 8 Mbit device size in Sectors */
|
| 95 |
|
|
|
| 96 |
|
|
#define MAXADRESS_16MBIT 0x1FFFFF
|
| 97 |
|
|
#define FLASH_SIZE_16MBIT (0x0200000) /**< Total 16 Mbit device size in Bytes */
|
| 98 |
|
|
#define FLASH_PAGE_COUNT_16MBIT (0x02000) /**< Total 16 Mbit device size in Pages */
|
| 99 |
|
|
#define FLASH_SECTOR_COUNT_16MBIT (0x20) /**< Total 16 Mbit device size in Sectors */
|
| 100 |
|
|
|
| 101 |
|
|
#define MAXADRESS_32MBIT 0x3FFFFF
|
| 102 |
|
|
#define FLASH_SIZE_32MBIT (0x0400000) /**< Total 32 Mbit device size in Bytes */
|
| 103 |
|
|
#define FLASH_PAGE_COUNT_32MBIT (0x04000) /**< Total 32 Mbit device size in Pages */
|
| 104 |
|
|
#define FLASH_SECTOR_COUNT_32MBIT (0x40) /**< Total 32 Mbit device size in Sectors */
|
| 105 |
|
|
|
| 106 |
|
|
/* Return Codes */
|
| 107 |
|
|
#define GOOD 0 /**< anything ok */
|
| 108 |
|
|
#define BAD 1 /**< error */
|
| 109 |
|
|
#define NOINSTANCE 99 /**< no device found */
|
| 110 |
|
|
#define UNSUPPORTED_TYPE 5 /**< a device was found but it is not supported */
|
| 111 |
|
|
#define OVERFLOW 10 /**< an overflow occoured */
|
| 112 |
|
|
|
| 113 |
|
|
|
| 114 |
|
|
/* flash memory opcodes */
|
| 115 |
|
|
#define WREN 0x06 /**< Flash Opcode: Write Enable */
|
| 116 |
|
|
#define WRDI 0x04 /**< Flash Opcode: Write Disable */
|
| 117 |
|
|
#define RDID 0x9F /**< Flash Opcode: Read Identification */
|
| 118 |
|
|
#define RDSR 0x05 /**< Flash Opcode: Read Status Register */
|
| 119 |
|
|
#define WRSR 0x01 /**< Flash Opcode: Write Status Register */
|
| 120 |
|
|
#define READ 0x03 /**< Flash Opcode: Read Data bytes */
|
| 121 |
|
|
#define FAST_READ 0x0B /**< Flash Opcode: Read Data bytes at higher speed */
|
| 122 |
|
|
#define PP 0x02 /**< Flash Opcode: Page Programm */
|
| 123 |
|
|
#define SE 0xD8 /**< Flash Opcode: Sector Erase */
|
| 124 |
|
|
#define BE 0xC7 /**< Flash Opcode: Bulk Erase */
|
| 125 |
|
|
#define DP 0xB9 /**< Flash Opcode: Deep Power-down */
|
| 126 |
|
|
#define RES 0xAB /**< Flash Opcode: Release from Deep Power-down */
|
| 127 |
|
|
|
| 128 |
|
|
|
| 129 |
|
|
/***************** Macros (Inline Functions) Definitions *********************/
|
| 130 |
|
|
|
| 131 |
|
|
/** \brief check if it is a sane pointer
|
| 132 |
|
|
* \param[in] ptr pointer to a SPI_flash struct
|
| 133 |
|
|
*/
|
| 134 |
|
|
#define ptrCheck(ptr) \
|
| 135 |
|
|
{ \
|
| 136 |
|
|
if (ptr == 0) \
|
| 137 |
|
|
{ \
|
| 138 |
|
|
return NOINSTANCE; \
|
| 139 |
|
|
} \
|
| 140 |
|
|
}
|
| 141 |
|
|
|
| 142 |
|
|
|
| 143 |
|
|
/** \brief check if a overflow would occour with the given parameters
|
| 144 |
|
|
*
|
| 145 |
|
|
* \param[in] flashPtr pointer to a initialized SPI_flash struct
|
| 146 |
|
|
* \param[in] adr adress somewhere in the flash
|
| 147 |
|
|
* \param[in] byteCount number of bytes to process
|
| 148 |
|
|
* \exception breaks the operations and returns the OVERFLOW error code
|
| 149 |
|
|
*/
|
| 150 |
|
|
#define adrCheck(flashPtr, adr, byteCount) \
|
| 151 |
|
|
{ \
|
| 152 |
|
|
if (adr + byteCount >= flashPtr->maxAdress) \
|
| 153 |
|
|
{ \
|
| 154 |
|
|
return OVERFLOW; \
|
| 155 |
|
|
} \
|
| 156 |
|
|
}
|
| 157 |
|
|
|
| 158 |
|
|
|
| 159 |
|
|
|
| 160 |
|
|
/** returns the start adress of the sector that belongs to this adress
|
| 161 |
|
|
* \param[in] adr adress somewhere in the flash
|
| 162 |
|
|
*/
|
| 163 |
|
|
#define sectorStart(adr) ((adr) & 0xFFFF0000)
|
| 164 |
|
|
|
| 165 |
|
|
|
| 166 |
|
|
/** returns the end adress of the sector that belongs to this adress
|
| 167 |
|
|
* \param[in] adr adress somewhere in the flash
|
| 168 |
|
|
*/
|
| 169 |
|
|
#define sectorEnd(adr) ((adr) | 0x0000FFFF)
|
| 170 |
|
|
|
| 171 |
|
|
|
| 172 |
|
|
/** returns the start adress of the page that belongs to this adress
|
| 173 |
|
|
* \param[in] adr adress somewhere in the flash
|
| 174 |
|
|
*/
|
| 175 |
|
|
#define pageStart(adr) ((adr) & 0xFFFFFF00)
|
| 176 |
|
|
|
| 177 |
|
|
|
| 178 |
|
|
/** returns the end adress of the page that belongs to this adress
|
| 179 |
|
|
* \param[in] adr adress somewhere in the flash
|
| 180 |
|
|
*/
|
| 181 |
|
|
#define pageEnd(adr) ((adr) | 0x000000FF)
|
| 182 |
|
|
|
| 183 |
|
|
|
| 184 |
|
|
extern SPI_flash xdata flash_dr;
|
| 185 |
|
|
|
| 186 |
|
|
/************************** Function Prototypes ******************************/
|
| 187 |
|
|
|
| 188 |
|
|
/** \brief Checks if the SPI flash is busy
|
| 189 |
|
|
*
|
| 190 |
|
|
* \param[in] flashPtr pointer to an SPI_flash struct
|
| 191 |
|
|
* \return returns non-zero if SPI flash is busy, else 0
|
| 192 |
|
|
*/
|
| 193 |
|
|
int8_t spiflash_is_busy(xdata SPI_flash *flashPtr);
|
| 194 |
|
|
|
| 195 |
|
|
|
| 196 |
|
|
/** \brief Initalizes the values in the SPI_flash struct after reading
|
| 197 |
|
|
* the device ID
|
| 198 |
|
|
*
|
| 199 |
|
|
* \param[in] flashPtr pointer to an uninitialized SPI_flash struct
|
| 200 |
|
|
* \return returns GOOD (0) or an Error Code (non-zero)
|
| 201 |
|
|
*/
|
| 202 |
|
|
int8_t init_spiflash(xdata SPI_flash *flashPtr);
|
| 203 |
|
|
|
| 204 |
|
|
|
| 205 |
|
|
/** \brief Reads data from the SPI flash
|
| 206 |
|
|
*
|
| 207 |
|
|
* \param[in] xdata SPI_flash flashPtr pointer to an SPI_flash struct
|
| 208 |
|
|
* \param[in] xdata uint32_t *adress pointer to the flash start adress to read
|
| 209 |
|
|
* from
|
| 210 |
|
|
* \param[out] xdata uint8_t *buffer, pointer to a buffer to write the data into
|
| 211 |
|
|
* \param[in] length of the data to read
|
| 212 |
|
|
* \return returns non-zero if successful, else 0
|
| 213 |
|
|
*/
|
| 214 |
|
|
int8_t spiflash_read(xdata SPI_flash *flashPtr, xdata uint32_t *adress, xdata uint8_t *buffer, const idata uint16_t length);
|
| 215 |
|
|
|
| 216 |
|
|
|
| 217 |
|
|
/** \brief deletes the whole SPI flash
|
| 218 |
|
|
*
|
| 219 |
|
|
* \param[in] *flashPtr pointer to an SPI_flash struct
|
| 220 |
|
|
* \return returns non-zero if successful, else 0
|
| 221 |
|
|
*/
|
| 222 |
|
|
int8_t spiflash_erase_bulk(xdata SPI_flash *flashPtr);
|
| 223 |
|
|
|
| 224 |
|
|
|
| 225 |
|
|
/** \brief deletes one sector (64 kbyte) of the SPI flash
|
| 226 |
|
|
*
|
| 227 |
|
|
* \param[in] *flashPtr flashPtr pointer to an SPI_flash struct
|
| 228 |
|
|
* \param[in] *adress pointer to the flash adress in the sector to be erased
|
| 229 |
|
|
* \return returns non-zero if successful, else 0
|
| 230 |
|
|
*/
|
| 231 |
|
|
int8_t spiflash_erase(xdata SPI_flash *flashPtr, xdata uint32_t *adress);
|
| 232 |
|
|
|
| 233 |
|
|
|
| 234 |
|
|
/** \brief Writes data to the SPI flash
|
| 235 |
|
|
*
|
| 236 |
|
|
* This write function handles if you write data over the page adress
|
| 237 |
|
|
* boundary. It splits it into seperate page program commands.
|
| 238 |
|
|
* \note Don't forget to erase a sector before you try to write to it!
|
| 239 |
|
|
*
|
| 240 |
|
|
* \param[in] *flashPtr pointer to an SPI_flash struct
|
| 241 |
|
|
* \param[in] *adress pointer to the flash start adress to
|
| 242 |
|
|
* write to
|
| 243 |
|
|
* \param[out] *buffer pointer to a buffer to write the data into
|
| 244 |
|
|
* \param[in] length of the data to read
|
| 245 |
|
|
* \return returns non-zero if successful, else 0
|
| 246 |
|
|
*/
|
| 247 |
|
|
int8_t spiflash_write(xdata SPI_flash *flashPtr, xdata uint32_t *adress, xdata uint8_t *buffer, uint16_t length);
|
| 248 |
|
|
|
| 249 |
|
|
#ifdef __cplusplus
|
| 250 |
|
|
}
|
| 251 |
|
|
#endif
|
| 252 |
|
|
|
| 253 |
|
|
#endif
|