Fix comment and add wizard info.
[bertos.git] / bertos / drv / flash25.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Function library for serial Flash memory.
34  *
35  *
36  * \version $Id$
37  * \author Daniele Basile <asterix@develer.com>
38  *
39  * $wizard_module = {
40  * "name" : "flash25",
41  * "depends" : ["kfile"],
42  * "configuration" : "bertos/cfg/cfg_flash25.h"
43  * }
44  */
45
46
47 #ifndef DRV_FLASH25_H
48 #define DRV_FLASH25_H
49
50 #include "cfg/cfg_flash25.h"
51 #include <cfg/compiler.h>
52
53 #include <kern/kfile.h>
54
55 /**
56  * Type definition for serial flash memory.
57  */
58 typedef uint32_t flash25Addr_t;
59 typedef uint32_t flash25Size_t;
60 typedef uint8_t flash25Offset_t;
61
62 /**
63  * Flash25 KFile context structure.
64  */
65 typedef struct Flash25
66 {
67         KFile fd;                       ///< File descriptor.
68         KFile *channel;                 ///< Dataflash comm channel (usually SPI).
69 } Flash25;
70
71 /**
72  * ID for dataflash.
73  */
74 #define KFT_FLASH25 MAKE_ID('F', 'L', '2', '5')
75
76
77 /**
78  * Convert + ASSERT from generic KFile to Flash25.
79  */
80 INLINE Flash25 * FLASH25_CAST(KFile *fd)
81 {
82         ASSERT(fd->_type == KFT_FLASH25);
83         return (Flash25 *)fd;
84 }
85
86 /**
87  * Memory definition.
88  *
89  * \note Below are defined valid serial flash memory support to
90  * this drive. Every time we call flash25_init() function we check
91  * if memory defined are right (see flash25.c form more detail).
92  *
93  * $WIZARD_LIST = {
94  * "flash25_list" : ["FLASH25_AT25F2048"]
95  * }
96  */
97 #define FLASH25_AT25F2048         1
98
99 #if CONFIG_FLASH25 == FLASH25_AT25F2048
100         #define FLASH25_MANUFACTURER_ID    0x1F  // ATMEL
101         #define FLASH25_DEVICE_ID          0x63  // Device ID
102         #define FLASH25_PAGE_SIZE          256   // Page size in byte
103         #define FLASH25_NUM_SECTOR         4     // Number of section in serial memory
104         #define FLASH25_SECTOR_SIZE        65536UL // Section size in byte
105         #define FLASH25_MEM_SIZE           FLASH25_NUM_SECTOR * FLASH25_SECTOR_SIZE
106         #define FLASH25_NUM_PAGE           FLASH25_MEM_SIZE / FLASH25_PAGE_SIZE
107 #elif
108         #error Nothing memory defined in CONFIG_FLASH25 are support.
109 #endif
110
111 #define RDY_BIT     0x1 // Statuts of write cycle
112
113 /**
114  * Serial flash opcode commands.
115  */
116 typedef enum {
117         FLASH25_WREN            = 0x6,  ///< Set write enable latch
118         FLASH25_WRDI            = 0x4,  ///< Reset enable write latch
119         FLASH25_RDSR            = 0x5,  ///< Read status register
120         FLASH25_WRSR            = 0x1,  ///< Write status register
121         FLASH25_READ            = 0x3,  ///< Read data from memory array
122         FLASH25_PROGRAM         = 0x2,  ///< Program data into memory array
123         FLASH25_SECTORE_ERASE   = 0x52, ///< Erase one sector in memory array
124         FLASH25_CHIP_ERASE      = 0x62, ///< Erase all sector in memory array
125         FLASH25_RDID            = 0x15 ///< Read Manufacturer and product ID
126 } Flash25Opcode;
127
128 /**
129  * Serial flash sector memory address.
130  */
131 #if CONFIG_FLASH25 != FLASH25_AT25F2048
132         #error Nothing memory defined in CONFIG_FLASH25 are support.
133 #endif
134
135 typedef enum {
136         FLASH25_SECT1            = 0x0,      ///< Sector 1 (0x0 -0xFFFF)
137         FLASH25_SECT2            = 0x10000,  ///< Sector 2 (0x10000 -0x1FFFF)
138         FLASH25_SECT3            = 0x20000,  ///< Sector 3 (0x20000 -0x2FFFF)
139         FLASH25_SECT4            = 0x30000,  ///< Sector 4 (0x30000 -0x3FFFF)
140 } Flash25Sector;
141
142 void flash25_init(Flash25 *fd, KFile *ch);
143 void flash25_chipErase(Flash25 *fd);
144 void flash25_sectorErase(Flash25 *fd, Flash25Sector sector);
145 bool flash25_test(KFile *channel);
146
147 #endif /* DRV_FLASH25_H */
148