Satellite Observers Workbench. NOT yet complete, just published for forum posters to \"cherry pick\" pieces of code as requiered as an example.
flash/flash.c@0:0a841b89d614, 2010-10-11 (annotated)
- Committer:
- AjK
- Date:
- Mon Oct 11 10:34:55 2010 +0000
- Revision:
- 0:0a841b89d614
Totally Alpha quality as this project isn\t completed. Just publishing it as it answers many questions asked in the forums
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 0:0a841b89d614 | 1 | /**************************************************************************** |
AjK | 0:0a841b89d614 | 2 | * Copyright 2010 Andy Kirkham, Stellar Technologies Ltd |
AjK | 0:0a841b89d614 | 3 | * |
AjK | 0:0a841b89d614 | 4 | * This file is part of the Satellite Observers Workbench (SOWB). |
AjK | 0:0a841b89d614 | 5 | * |
AjK | 0:0a841b89d614 | 6 | * SOWB is free software: you can redistribute it and/or modify |
AjK | 0:0a841b89d614 | 7 | * it under the terms of the GNU General Public License as published by |
AjK | 0:0a841b89d614 | 8 | * the Free Software Foundation, either version 3 of the License, or |
AjK | 0:0a841b89d614 | 9 | * (at your option) any later version. |
AjK | 0:0a841b89d614 | 10 | * |
AjK | 0:0a841b89d614 | 11 | * SOWB is distributed in the hope that it will be useful, |
AjK | 0:0a841b89d614 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
AjK | 0:0a841b89d614 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
AjK | 0:0a841b89d614 | 14 | * GNU General Public License for more details. |
AjK | 0:0a841b89d614 | 15 | * |
AjK | 0:0a841b89d614 | 16 | * You should have received a copy of the GNU General Public License |
AjK | 0:0a841b89d614 | 17 | * along with SOWB. If not, see <http://www.gnu.org/licenses/>. |
AjK | 0:0a841b89d614 | 18 | * |
AjK | 0:0a841b89d614 | 19 | * $Id: main.cpp 5 2010-07-12 20:51:11Z ajk $ |
AjK | 0:0a841b89d614 | 20 | * |
AjK | 0:0a841b89d614 | 21 | ***************************************************************************/ |
AjK | 0:0a841b89d614 | 22 | |
AjK | 0:0a841b89d614 | 23 | #include "sowb.h" |
AjK | 0:0a841b89d614 | 24 | #include "debug.h" |
AjK | 0:0a841b89d614 | 25 | #include "ssp0.h" |
AjK | 0:0a841b89d614 | 26 | #include "gpio.h" |
AjK | 0:0a841b89d614 | 27 | #include "flash.h" |
AjK | 0:0a841b89d614 | 28 | #include "user.h" |
AjK | 0:0a841b89d614 | 29 | |
AjK | 0:0a841b89d614 | 30 | extern bool sector_erase_in_progress; |
AjK | 0:0a841b89d614 | 31 | extern bool page_write_in_progress; |
AjK | 0:0a841b89d614 | 32 | |
AjK | 0:0a841b89d614 | 33 | char flash_buffer[2][FLASH_PAGE_SIZE]; |
AjK | 0:0a841b89d614 | 34 | int current_buffer; |
AjK | 0:0a841b89d614 | 35 | unsigned int flash_address; |
AjK | 0:0a841b89d614 | 36 | |
AjK | 0:0a841b89d614 | 37 | /** flash_init |
AjK | 0:0a841b89d614 | 38 | */ |
AjK | 0:0a841b89d614 | 39 | void flash_init(void) { |
AjK | 0:0a841b89d614 | 40 | |
AjK | 0:0a841b89d614 | 41 | /* Initialise the SSP0 to talk to the flash device. */ |
AjK | 0:0a841b89d614 | 42 | SSP0_init(); |
AjK | 0:0a841b89d614 | 43 | |
AjK | 0:0a841b89d614 | 44 | DEBUG_INIT_START; |
AjK | 0:0a841b89d614 | 45 | |
AjK | 0:0a841b89d614 | 46 | /* Clear out the page buffers. */ |
AjK | 0:0a841b89d614 | 47 | memset(flash_buffer, 0, 2 * FLASH_PAGE_SIZE); |
AjK | 0:0a841b89d614 | 48 | |
AjK | 0:0a841b89d614 | 49 | /* Default the buffer in use. */ |
AjK | 0:0a841b89d614 | 50 | current_buffer = 0; |
AjK | 0:0a841b89d614 | 51 | |
AjK | 0:0a841b89d614 | 52 | /* Default pointer set-up. */ |
AjK | 0:0a841b89d614 | 53 | flash_address = 0; |
AjK | 0:0a841b89d614 | 54 | |
AjK | 0:0a841b89d614 | 55 | /* Prime our buffers for expected future access. */ |
AjK | 0:0a841b89d614 | 56 | flash_read_page(0, flash_buffer[0], true); |
AjK | 0:0a841b89d614 | 57 | flash_read_page(1, flash_buffer[1], true); |
AjK | 0:0a841b89d614 | 58 | |
AjK | 0:0a841b89d614 | 59 | /* Although not part of the flash system, SOWB includes |
AjK | 0:0a841b89d614 | 60 | a 25AA02E48 device from Microchip that holds a globally |
AjK | 0:0a841b89d614 | 61 | unique 48bit (6byte) MAC address. We use this for the |
AjK | 0:0a841b89d614 | 62 | SOWB "STL authentic product" serial number and in future |
AjK | 0:0a841b89d614 | 63 | may be used as the ethernet MAC address if we ever write |
AjK | 0:0a841b89d614 | 64 | code to support Ethernet. This device is also connected |
AjK | 0:0a841b89d614 | 65 | to SSP1 and so we'll _init() it here now. */ |
AjK | 0:0a841b89d614 | 66 | //_25AA02E48_init(); |
AjK | 0:0a841b89d614 | 67 | |
AjK | 0:0a841b89d614 | 68 | DEBUG_INIT_END; |
AjK | 0:0a841b89d614 | 69 | } |
AjK | 0:0a841b89d614 | 70 | |
AjK | 0:0a841b89d614 | 71 | /** flash_process |
AjK | 0:0a841b89d614 | 72 | */ |
AjK | 0:0a841b89d614 | 73 | void flash_process(void) { |
AjK | 0:0a841b89d614 | 74 | /* Currently does nothing. */ |
AjK | 0:0a841b89d614 | 75 | } |
AjK | 0:0a841b89d614 | 76 | |
AjK | 0:0a841b89d614 | 77 | /** flash_getc |
AjK | 0:0a841b89d614 | 78 | */ |
AjK | 0:0a841b89d614 | 79 | char flash_getc(bool peek) { |
AjK | 0:0a841b89d614 | 80 | char c; |
AjK | 0:0a841b89d614 | 81 | |
AjK | 0:0a841b89d614 | 82 | /* Flash being deleted, undefined memory. */ |
AjK | 0:0a841b89d614 | 83 | if (sector_erase_in_progress) return 0xFF; |
AjK | 0:0a841b89d614 | 84 | |
AjK | 0:0a841b89d614 | 85 | /* Wait for any page loads to complete. */ |
AjK | 0:0a841b89d614 | 86 | while (page_write_in_progress) user_call_process(); |
AjK | 0:0a841b89d614 | 87 | |
AjK | 0:0a841b89d614 | 88 | /* Get the character from the internal buffer. */ |
AjK | 0:0a841b89d614 | 89 | c = flash_buffer[current_buffer][flash_address]; |
AjK | 0:0a841b89d614 | 90 | |
AjK | 0:0a841b89d614 | 91 | /* If this is just a peek then return the character without |
AjK | 0:0a841b89d614 | 92 | incrementing the memory pointers etc etc. */ |
AjK | 0:0a841b89d614 | 93 | if (peek) return c; |
AjK | 0:0a841b89d614 | 94 | |
AjK | 0:0a841b89d614 | 95 | /* Inc the address pointer and load a new page if needed. Note, |
AjK | 0:0a841b89d614 | 96 | we load the page in background using DMA. */ |
AjK | 0:0a841b89d614 | 97 | flash_address++; |
AjK | 0:0a841b89d614 | 98 | if ((flash_address & 0xFF) == 0) { |
AjK | 0:0a841b89d614 | 99 | flash_read_page(current_buffer >> 8, flash_buffer[current_buffer], false); |
AjK | 0:0a841b89d614 | 100 | current_buffer = current_buffer ? 0 : 1; |
AjK | 0:0a841b89d614 | 101 | } |
AjK | 0:0a841b89d614 | 102 | |
AjK | 0:0a841b89d614 | 103 | return c; |
AjK | 0:0a841b89d614 | 104 | } |
AjK | 0:0a841b89d614 | 105 | |
AjK | 0:0a841b89d614 | 106 | /** flash_seek |
AjK | 0:0a841b89d614 | 107 | */ |
AjK | 0:0a841b89d614 | 108 | void flash_seek(unsigned int addr) { |
AjK | 0:0a841b89d614 | 109 | flash_read_page((addr >> 8) + 0, flash_buffer[0], true); |
AjK | 0:0a841b89d614 | 110 | flash_read_page((addr >> 8) + 1, flash_buffer[1], true); |
AjK | 0:0a841b89d614 | 111 | flash_address = addr; |
AjK | 0:0a841b89d614 | 112 | } |