summaryrefslogtreecommitdiff
path: root/digital/avr/modules/flash/flash_sst.avr.c
blob: 5f0a63ecb8f868472c6d73437fac655deb07c23b (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* flash_sst.avr.c */
/* avr.flash - AVR Flash SPI use. {{{
 *
 * Copyright (C) 2009 Nelio 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 "flash_sst.h"
#include "modules/proto/proto.h"
#include "modules/spi/spi.h"
#include "modules/utils/utils.h"

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

void
flash_sst_init (void)
{
    if (flash_sst_status_aai())
	flash_sst_send_command (FLASH_SST_CMD_WEDI);
    /* Enables the flash to be writable. */
    flash_sst_send_command (FLASH_SST_CMD_WREN);
    FLASH_CS_ENABLE;
    spi_send (FLASH_SST_CMD_WRSR);
    spi_send (0);
    FLASH_CS_DISABLE;
}

/** 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.
  */
static inline void
flash_sst_address (uint32_t addr)
{
    if (FLASH_SST_ADDR_IS_VALID (addr))
      {
	/* The address must be sent */
	spi_send ((addr >> 16) & 0x1f);
	spi_send (addr >> 8);
	spi_send (addr);
      }
}

void
flash_sst_erase (flash_erase_cmd_t cmd, uint32_t start_addr)
{
    uint8_t sst_cmd;
    switch (cmd)
      {
      case FLASH_ERASE_PAGE:
	sst_cmd = FLASH_SST_ERASE_4K;
	break;
      case FLASH_ERASE_SECTOR:
	sst_cmd = FLASH_SST_ERASE_32K;
	break;
      case FLASH_ERASE_BLOCK:
	sst_cmd = FLASH_SST_ERASE_64K;
	break;
      default:
	return;
      }
    flash_sst_send_command (FLASH_SST_CMD_WREN);
    FLASH_CS_ENABLE;
    /* send the command. */
    spi_send (cmd);
    /* verify if the cmd is the full erase. */
    if (sst_cmd != FLASH_SST_ERASE_FULL)
        /* Send the start address */
        flash_sst_address (start_addr);
    FLASH_CS_DISABLE;
    while (flash_sst_is_busy());
}

void
flash_sst_send_command (flash_cmd_t cmd)
{
    uint8_t sst_cmd;
    switch (cmd)
      {
      case FLASH_CMD_READ_BYTE:
      case FLASH_CMD_READ:
	sst_cmd = FLASH_SST_CMD_READ;
	break;
      case FLASH_CMD_WRITE_BYTE:
      case FLASH_CMD_WRITE:
	sst_cmd = FLASH_SST_CMD_WRITE;
	break;
      default:
	return;
      }
    FLASH_CS_ENABLE;
    spi_send (cmd);
    FLASH_CS_DISABLE;
}

uint8_t
flash_sst_read_status (void)
{
    uint8_t res;
    FLASH_CS_ENABLE;
    spi_send (FLASH_SST_CMD_RDSR);
    res = spi_recv();
    FLASH_CS_DISABLE;
    return res;
}

uint8_t
flash_sst_is_busy (void)
{
    return flash_sst_read_status () & 0x1;
}

void
flash_sst_write (uint32_t addr, uint8_t data)
{
    if (FLASH_SST_ADDR_IS_VALID (addr))
      {
	while (flash_sst_is_busy ());
	flash_sst_send_command (FLASH_SST_CMD_WREN);
	FLASH_CS_ENABLE;
	/* Write instruction. */
	spi_send (FLASH_SST_CMD_WRITE);
	flash_sst_address (addr);
	spi_send (data);
	FLASH_CS_DISABLE;
	/* Wait for the flash until it is busy */
	while (flash_sst_is_busy());
      }
}

uint8_t
flash_sst_read (uint32_t addr)
{
    if (FLASH_SST_ADDR_IS_VALID (addr))
      {
	uint8_t data;
	while (flash_sst_is_busy ());
	FLASH_CS_ENABLE;
	/* Send the read instruction. */
	spi_send (FLASH_SST_CMD_READ);
	flash_sst_address (addr);
	data = spi_recv ();
	FLASH_CS_DISABLE;
	while (flash_sst_is_busy ());
	return data;
      }
    return 0xFF;
}

void
flash_sst_read_array (uint32_t addr, uint8_t *buffer, uint32_t length)
{
    if (FLASH_SST_ADDR_IS_VALID (addr))
      {
	uint8_t i;
	while (flash_sst_is_busy ());
	FLASH_CS_ENABLE;
	spi_send (FLASH_SST_CMD_READ);
	flash_sst_address (addr);
	for (i = 0; i < length; i++)
	    buffer[i] = spi_recv ();
	FLASH_CS_DISABLE;
	while (flash_sst_is_busy ());
      }
}

void
flash_sst_write_array (uint32_t addr, uint8_t *data, uint32_t length)
{
    if (FLASH_SST_ADDR_IS_VALID (addr))
      {
	uint32_t i;
	for (i = 0; i < length; i++)
	    flash_sst_write (addr + i, data[i]);
      }
}