forked from Arlet/Freecut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash.c
150 lines (135 loc) · 3.03 KB
/
flash.c
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
/*
* flash.c
*
* Driver for serial dataflash AT45DB041B on main board, attached as follows:
*
* Pin | Func | AVR
*------+-------+---------
* 1 | SI | PB7
* 2 | SCK | PB1
* 4 | !CS | PB4
* 8 | SO | PB0
*
* only contains a test/dump function for now.
*
*
* Copyright 2010 <freecutfirmware@gmail.com>
*
* This file is part of Freecut.
*
* Freecut is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2.
*
* Freecut 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 Freecut. If not, see http://www.gnu.org/licenses/.
*
*/
#include <avr/io.h>
#include <stdio.h>
#include <inttypes.h>
#include "flash.h"
#include "usb.h"
#define MISO (1 << 0)
#define SCK (1 << 1)
#define CS (1 << 4)
#define MOSI (1 << 7)
#define cs_low( ) do { PORTB &= ~CS; } while(0)
#define cs_high( ) do { PORTB |= CS; } while(0)
#define mosi_low( ) do { PORTB &= ~MOSI; } while(0)
#define mosi_high( ) do { PORTB |= MOSI; } while(0)
#define sck_low( ) do { PORTB &= ~SCK; } while(0)
#define sck_high( ) do { PORTB |= SCK; } while(0)
#define get_miso( ) (PINB & 1)
#define AT_STATUS 0xd7 // status byte
#define AT_CAR 0xe8 // continuous array read
/*
* write a single byte to flash chip
*/
static void flash_write_byte( uint8_t data )
{
char i;
for( i = 0; i < 8; i++ )
{
if( data & 0x80 )
mosi_high( );
else
mosi_low( );
sck_high( );
data <<= 1;
sck_low( );
}
}
/*
* read a single byte to flash chip
*/
static uint8_t flash_read_byte( void )
{
uint8_t data = 0;
char i;
for( i = 0; i < 8; i++ )
{
sck_high( );
data = (data << 1) + get_miso( );
sck_low( );
}
return data;
}
/*
* provide 'count' dummy clocks
*/
static void flash_clocks( char count )
{
while( --count >= 0 )
{
sck_high( );
sck_low( );
}
}
/*
* read the Flash status byte
*/
static uint8_t flash_read_status( void )
{
uint8_t status;
cs_low( );
flash_write_byte( AT_STATUS );
status = flash_read_byte( );
cs_high( );
return status;
}
/*
* write a command, consisting of 8 cmd bits, and 24 address bits.
*/
static void flash_write_cmd( uint8_t cmd, uint32_t addr )
{
cs_low( );
flash_write_byte( cmd );
flash_write_byte( addr >> 24 );
flash_write_byte( addr >> 16 );
flash_write_byte( addr >> 8 );
}
/*
* simple test: dump all contents of built-in cartridge
*/
void flash_test( void )
{
uint8_t i = 0;
long size = 2048L * 264L;
flash_write_cmd( AT_CAR, 0 );
flash_clocks( 32 );
while( --size >= 0 && usb_peek() != 3 )
{
printf( "%02x%c", flash_read_byte(), ++i % 16 ? ' ' : '\n' );
}
cs_high( );
}
void flash_init( void )
{
DDRB |= MOSI | SCK | CS;
flash_read_status( );
}