summaryrefslogtreecommitdiff
path: root/digital/avr/modules/flash/flash.host.c
blob: 1d984f32d9e292eeba0d9a973e9d459ad0946e8b (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
202
203
204
205
206
207
208
209
210
211
212
213
/* flash.host.c */
/* avr.flash - AVR Flash SPI use. {{{
 *
 * Copyright (C) 2009 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 "flash.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

struct flash_t
{
    /** Status of the flash memory. */
    uint8_t status;
    /** File to store, read the data. */
    int file;
};
static struct flash_t flash_global;

static void
flash_deactivate (void)
{
    flash_global.status = 0;

    if (flash_global.file)
	close (flash_global.file);
}

void
flash_send_command (uint8_t cmd) {}

void
flash_erase (uint8_t cmd, uint32_t start_addr)
{
    if (flash_global.status)
      {
	uint32_t length;
	switch (cmd)
	  {
	  case FLASH_ERASE_4K:
	    length = 4096;
	    break;
	  case FLASH_ERASE_32K:
	    length = 32768;
	    break;
	  case FLASH_ERASE_64K:
	    length = 65535;
	    break;
	  case FLASH_ERASE_FULL:
	    length = FLASH_SIZE;
	    break;
	  default:
	    length = 0;
	  }

	if (length)
	  {
	    uint8_t data = 0xFF;
	    int res;

	    /* Set the stream to the correct position. */
	    lseek (flash_global.file, start_addr, SEEK_SET);
	    for (res = 1; (res != -1) && length; length --)
		res = write (flash_global.file, &data, sizeof (uint8_t));

	    if ((res < 0) && length)
	      {
		flash_deactivate ();
	      }
	  }
      }
}

uint8_t
flash_read_status (void)
{
    return 0x0;
}

uint8_t
flash_init (void)
{
    memset (&flash_global, 0, sizeof (struct flash_t));

    flash_global.file = open ("flash.apb", O_CREAT | O_RDWR, S_IREAD |
			      S_IWRITE);
    if (flash_global.file > 0)
      {
	struct stat fstats;
	fstat (flash_global.file, &fstats);

	if (fstats.st_size == FLASH_SIZE)
	  {
	    /* Set the stream to the beginning of the file. */
	    lseek (flash_global.file, 0, SEEK_SET);
	    flash_global.status = 1;
	  }
	else
	  {
	    flash_global.status = 1;
	    flash_erase (FLASH_ERASE_FULL, 0);
	    fstat (flash_global.file, &fstats);
	    if (fstats.st_size != FLASH_SIZE)
		flash_deactivate ();
	  }
      }
    else
	flash_deactivate ();

    return flash_global.status;
}

void
flash_write (uint32_t addr, uint8_t data)
{
    if (flash_global.status)
      {
	uint8_t res;

	/* Set the stream to the position of address. */
	lseek (flash_global.file, addr, SEEK_SET);
	res = write (flash_global.file, &data, sizeof (uint8_t));

	if (res == 0)
	    flash_deactivate ();
      }
}

uint8_t
flash_read (uint32_t addr)
{
    if (flash_global.status)
      {
	uint8_t res;
	uint8_t data;

	/* Set the stream to the position of address. */
	lseek (flash_global.file, addr, SEEK_SET);
	res = read (flash_global.file, &data, sizeof (uint8_t));

	if (res == 0)
	    flash_global.status = 0;

	return data;
      }

    return 0xff;
}

void
flash_read_array (uint32_t addr, uint8_t *buffer, uint32_t length)
{
    if (flash_global.status)
      {
	uint32_t nb;
	int8_t res;
	/* Set the stream to the position required. */
	res = lseek (flash_global.file, addr, SEEK_SET);
	if (res == -1)
	  {
	    flash_deactivate();
	    return;
	  }

	nb = read (flash_global.file, buffer, length);

	if (nb != length)
	    flash_deactivate();
      }
}

void
flash_write_array (uint32_t addr, uint8_t *data, uint32_t length)
{
    if (flash_global.status)
      {
	uint8_t res;

	if (sizeof (data) >= length)
	  {
	    /* Set the stream to the position required. */
	    lseek (flash_global.file, addr, SEEK_SET);
	    res = write (flash_global.file, data, length);

	    if (res != length)
		flash_deactivate();
	  }
	else
	    flash_deactivate();
      }
}