| 1 |
2 |
idiolatrie |
/******************************************************************************
|
| 2 |
|
|
* Numonyx™ 128 Mbit EMBEDDED FLASH MEMORY J3 Version D *
|
| 3 |
|
|
******************************************************************************
|
| 4 |
|
|
* Copyright (C)2011 Mathias Hörtnagl <mathias.hoertnagl@gmail.com> *
|
| 5 |
|
|
* *
|
| 6 |
|
|
* This program 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 3 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, see <http://www.gnu.org/licenses/>. *
|
| 18 |
|
|
******************************************************************************/
|
| 19 |
|
|
#include "stddef.h"
|
| 20 |
|
|
#include "flash.h"
|
| 21 |
|
|
|
| 22 |
|
|
/* Read the status register. */
|
| 23 |
|
|
uchar flash_read_status() {
|
| 24 |
|
|
FLASH_MEMORY[0] = CMD_READ_SR;
|
| 25 |
|
|
return FLASH_MEMORY[0];
|
| 26 |
|
|
}
|
| 27 |
|
|
|
| 28 |
|
|
/* Clear the status register. */
|
| 29 |
|
|
void flash_clear_sr() {
|
| 30 |
|
|
FLASH_MEMORY[0] = CMD_CLEAR_SR;
|
| 31 |
|
|
}
|
| 32 |
|
|
|
| 33 |
|
|
/* Write a byte of data to a specific device address.
|
| 34 |
|
|
Writing only changes '1' to '0'. If you overwrite data that would change '0'
|
| 35 |
|
|
to '1', erase the block beforhand. */
|
| 36 |
|
|
void flash_write(uint adr, uchar b) {
|
| 37 |
|
|
FLASH_MEMORY[adr] = CMD_BYTE_PROGRAM;
|
| 38 |
|
|
FLASH_MEMORY[adr] = b;
|
| 39 |
|
|
}
|
| 40 |
|
|
|
| 41 |
|
|
/* Read 32bit of data from a specific device address.
|
| 42 |
|
|
Issues a Read Array Command each time, although device stays in Array Read
|
| 43 |
|
|
mode until another command operation takes place. */
|
| 44 |
|
|
uint flash_read(uint adr) {
|
| 45 |
|
|
FLASH_MEMORY[0] = CMD_READ_ARRAY;
|
| 46 |
|
|
return ( (volatile uint *) FLASH_MEMORY )[adr];
|
| 47 |
|
|
}
|
| 48 |
|
|
|
| 49 |
|
|
/* Erase block. Point to an address within the block address space you want to
|
| 50 |
|
|
erase. 16 Mbytes, organized as 128-Kbyte erase blocks. */
|
| 51 |
|
|
void flash_block_erase(uint blk) {
|
| 52 |
|
|
FLASH_MEMORY[blk] = CMD_BLOCK_ERASE_SETUP;
|
| 53 |
|
|
FLASH_MEMORY[blk] = CMD_BLOCK_ERASE_CONFIRM;
|
| 54 |
|
|
}
|
| 55 |
|
|
|
| 56 |
|
|
/* Wait for the end of a operation and return the status register when ready.
|
| 57 |
|
|
Block erasure and writing data takes longer than a WB write operation.
|
| 58 |
|
|
So after each erase or write one should call flash_wait() or do something
|
| 59 |
|
|
else meanwhile. */
|
| 60 |
|
|
uchar flash_wait() {
|
| 61 |
|
|
|
| 62 |
|
|
uchar s;
|
| 63 |
|
|
|
| 64 |
|
|
while( !( (s = flash_read_status()) & FLASH_READY) );
|
| 65 |
|
|
return s;
|
| 66 |
|
|
}
|