summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/modules/flash/flash.h
blob: 06c64f5423b5997253be146664f70cf8a4ac98c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#ifndef flash_h
#define flash_h
/* flash.h */
/* avr.modules - AVR modules. {{{
 *
 * Copyright (C) 2008 Nélio Laranjeiro
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "io.h"

#define FLASH_ADDRESS_HIGH 0x1FFFFF
#define FLASH_ADDRESS_ERROR 0xFFFFFF

#define FLASH_PAGE_SIZE  0x1000
#define FLASH_PAGE_MASK (FLASH_ADDRESS_HIGH & FLASH_PAGE_SIZE)
#define FLASH_PAGE(val) ((val) & FLASH_PAGE_MASK)

#define FLASH_ERASE_FULL 0x60
#define FLASH_ERASE_4K 0x20
#define FLASH_ERASE_32K 0x52
#define FLASH_ERASE_64K 0xD8

#define FLASH_READ_ID 0x9F
#define FLASH_READ 0x03
#define FLASH_WRITE 0x2
#define FLASH_WRSR 0x1
#define FLASH_RDSR 0x5
#define FLASH_WREN 0x6
#define FLASH_WEDI 0x4
#define FLASH_EWSR 0x50
#define FLASH_AAI 0xAD

#define FLASH_TBP_US 10

/** Flash access.
  * The flash contains an address of 21 bits in a range from 0x0-0x1fffff.
  * This function shall access the memory directly by the SPI.
  * \param  addr  the address to provide to the flash memory.
  */
void
flash_address (uint32_t addr);

/** Erase the memory.
  * \param  erase_type  the erase type..
  * \param  start_addr  the start address.
  */
void
flash_erase (uint8_t cmd, uint32_t start_addr);

/* Send a flash command to the flash memory (only a command).
 * \param  cmd  the command to send.
 */
void
flash_send_command (uint8_t cmd);

/** Poll the busy bit in the Software Status Register of the flash memory.
  * \return  the status register.
  */
uint8_t
flash_read_status (void);

/** Poll the busy bit in the Software Status Register of the flash memory.
  * \return  the busy bit state.
  */
extern inline uint8_t
flash_is_busy (void)
{
    return flash_read_status () & 0x1;
}

/** Return the AAI status flag of the register.
  * \return  the AAI status.
  */
extern inline uint8_t
flash_status_aai (void)
{
    return flash_read_status () >> 6;
}

/** Initialise the flash memory.
  * \return true if the flash is present, false otherwise.
  */
uint8_t
flash_init (void);

/** Find the next sector to write.
 * \param  addr  the address to start the research.
 * \return  the address of the next sector.
 */
uint32_t
flash_sector_next (uint32_t addr);

/** Write in the flash byte provided in parameter.
  * \param  data  the buffer to store the data.
  */
void
flash_write (uint32_t addr, uint8_t data);

/** Read the data at the address provided.
  * \param  addr  the address of the data to read.
  * \return  the data read.
  */
uint8_t
flash_read (uint32_t addr);

/** Read a data from the flash memory from the address provided and for a
 * length of the number of bytes provided.
 * \param  address at which the data should be read.
 * \param  buffer  the buffer to fill with the read data.
 * \param  length  the length of the data to read.
 */
void
flash_read_array (uint32_t addr, uint8_t *buffer, uint32_t length);

/** Write in the flash byte provided in parameter.
  * \param  addr  the address to store the data.
  * \param  data  the array to store.
  * \param  length  the array length
  */
void
flash_write_array (uint32_t addr, uint8_t *data, uint32_t length);

#endif /* flash_h */