Satellite Observers Workbench. NOT yet complete, just published for forum posters to \"cherry pick\" pieces of code as requiered as an example.
flash/flash_erase.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 "flash.h" |
AjK | 0:0a841b89d614 | 25 | #include "ssp0.h" |
AjK | 0:0a841b89d614 | 26 | #include "dma.h" |
AjK | 0:0a841b89d614 | 27 | #include "gpio.h" |
AjK | 0:0a841b89d614 | 28 | #include "rit.h" |
AjK | 0:0a841b89d614 | 29 | #include "user.h" |
AjK | 0:0a841b89d614 | 30 | #include "utils.h" |
AjK | 0:0a841b89d614 | 31 | #include "debug.h" |
AjK | 0:0a841b89d614 | 32 | |
AjK | 0:0a841b89d614 | 33 | /* Flags to show what state we are in. */ |
AjK | 0:0a841b89d614 | 34 | bool sector_erase_in_progress = false; |
AjK | 0:0a841b89d614 | 35 | extern bool page_write_in_progress; |
AjK | 0:0a841b89d614 | 36 | |
AjK | 0:0a841b89d614 | 37 | bool flash_sector_erase_in_progress(void) { |
AjK | 0:0a841b89d614 | 38 | return sector_erase_in_progress; |
AjK | 0:0a841b89d614 | 39 | } |
AjK | 0:0a841b89d614 | 40 | |
AjK | 0:0a841b89d614 | 41 | /** flash_erase_sector |
AjK | 0:0a841b89d614 | 42 | */ |
AjK | 0:0a841b89d614 | 43 | int flash_erase_sector(int sector) { |
AjK | 0:0a841b89d614 | 44 | |
AjK | 0:0a841b89d614 | 45 | /* If a sector erase is in progress already |
AjK | 0:0a841b89d614 | 46 | we return zero rather than wait (block) |
AjK | 0:0a841b89d614 | 47 | because an erase can take so long to complete |
AjK | 0:0a841b89d614 | 48 | we don't want to hang around waiting. Let the |
AjK | 0:0a841b89d614 | 49 | caller reschedule it sometime later. */ |
AjK | 0:0a841b89d614 | 50 | if (sector_erase_in_progress) { |
AjK | 0:0a841b89d614 | 51 | return 0; |
AjK | 0:0a841b89d614 | 52 | } |
AjK | 0:0a841b89d614 | 53 | |
AjK | 0:0a841b89d614 | 54 | /* Request use of SSP0. */ |
AjK | 0:0a841b89d614 | 55 | while(!SSP0_request()) WHILE_WAITING_DO_PROCESS_FUNCTIONS; |
AjK | 0:0a841b89d614 | 56 | |
AjK | 0:0a841b89d614 | 57 | sector_erase_in_progress = true; |
AjK | 0:0a841b89d614 | 58 | |
AjK | 0:0a841b89d614 | 59 | FLASH_CS_ASSERT; |
AjK | 0:0a841b89d614 | 60 | FLASH_SHORT_COMMAND(FLASH_WREN); |
AjK | 0:0a841b89d614 | 61 | FLASH_CS_DEASSERT; |
AjK | 0:0a841b89d614 | 62 | |
AjK | 0:0a841b89d614 | 63 | SSP0_FLUSH_RX_FIFO; |
AjK | 0:0a841b89d614 | 64 | |
AjK | 0:0a841b89d614 | 65 | /* Wait until the flash device has the WEL bit on. */ |
AjK | 0:0a841b89d614 | 66 | while ((LPC_SSP0->DR & 0x2) == 0) { |
AjK | 0:0a841b89d614 | 67 | FLASH_CS_ASSERT; |
AjK | 0:0a841b89d614 | 68 | FLASH_SHORT_COMMAND(FLASH_RDSR); |
AjK | 0:0a841b89d614 | 69 | SSP0_FLUSH_RX_FIFO; |
AjK | 0:0a841b89d614 | 70 | SSP0_WRITE_BYTE(0); |
AjK | 0:0a841b89d614 | 71 | while (SSP0_IS_BUSY); |
AjK | 0:0a841b89d614 | 72 | FLASH_CS_DEASSERT; |
AjK | 0:0a841b89d614 | 73 | } |
AjK | 0:0a841b89d614 | 74 | |
AjK | 0:0a841b89d614 | 75 | SSP0_FLUSH_RX_FIFO; |
AjK | 0:0a841b89d614 | 76 | |
AjK | 0:0a841b89d614 | 77 | FLASH_CS_ASSERT; |
AjK | 0:0a841b89d614 | 78 | FLASH_LONG_COMMAND(FLASH_SE, sector); |
AjK | 0:0a841b89d614 | 79 | FLASH_CS_DEASSERT; |
AjK | 0:0a841b89d614 | 80 | |
AjK | 0:0a841b89d614 | 81 | /* Note, a sector erase takes much longer than |
AjK | 0:0a841b89d614 | 82 | a page write (typical 600ms by the datasheet) |
AjK | 0:0a841b89d614 | 83 | so there's no point making the first timeout |
AjK | 0:0a841b89d614 | 84 | very short and producing a lot of uneeded |
AjK | 0:0a841b89d614 | 85 | interrupts. So we set the first timeout to |
AjK | 0:0a841b89d614 | 86 | be 600 and then it'll switch to a much shorter |
AjK | 0:0a841b89d614 | 87 | time in the ISR. */ |
AjK | 0:0a841b89d614 | 88 | rit_timer_set_counter(FLASH_WRITE_CB, 600); |
AjK | 0:0a841b89d614 | 89 | return 1; |
AjK | 0:0a841b89d614 | 90 | } |
AjK | 0:0a841b89d614 | 91 | |
AjK | 0:0a841b89d614 | 92 | /** flash_erase_bulk |
AjK | 0:0a841b89d614 | 93 | */ |
AjK | 0:0a841b89d614 | 94 | int flash_erase_bulk(void) { |
AjK | 0:0a841b89d614 | 95 | |
AjK | 0:0a841b89d614 | 96 | /* If a sector erase is in progress already |
AjK | 0:0a841b89d614 | 97 | we return zero rather than wait (block) |
AjK | 0:0a841b89d614 | 98 | because an erase can take so long to complete |
AjK | 0:0a841b89d614 | 99 | we don't want to hang around waiting. Let the |
AjK | 0:0a841b89d614 | 100 | caller reschedule it sometime later. */ |
AjK | 0:0a841b89d614 | 101 | |
AjK | 0:0a841b89d614 | 102 | if (sector_erase_in_progress || page_write_in_progress) { |
AjK | 0:0a841b89d614 | 103 | return 0; |
AjK | 0:0a841b89d614 | 104 | } |
AjK | 0:0a841b89d614 | 105 | |
AjK | 0:0a841b89d614 | 106 | /* Request use of SSP0. */ |
AjK | 0:0a841b89d614 | 107 | while(!SSP0_request()) WHILE_WAITING_DO_PROCESS_FUNCTIONS; |
AjK | 0:0a841b89d614 | 108 | |
AjK | 0:0a841b89d614 | 109 | sector_erase_in_progress = true; |
AjK | 0:0a841b89d614 | 110 | |
AjK | 0:0a841b89d614 | 111 | FLASH_CS_ASSERT; |
AjK | 0:0a841b89d614 | 112 | FLASH_SHORT_COMMAND(FLASH_WREN); |
AjK | 0:0a841b89d614 | 113 | FLASH_CS_DEASSERT; |
AjK | 0:0a841b89d614 | 114 | |
AjK | 0:0a841b89d614 | 115 | SSP0_FLUSH_RX_FIFO; |
AjK | 0:0a841b89d614 | 116 | |
AjK | 0:0a841b89d614 | 117 | /* Wait until the flash device has the WEL bit on. */ |
AjK | 0:0a841b89d614 | 118 | while ((LPC_SSP0->DR & 0x2) == 0) { |
AjK | 0:0a841b89d614 | 119 | FLASH_CS_ASSERT; |
AjK | 0:0a841b89d614 | 120 | FLASH_SHORT_COMMAND(FLASH_RDSR); |
AjK | 0:0a841b89d614 | 121 | SSP0_FLUSH_RX_FIFO; |
AjK | 0:0a841b89d614 | 122 | SSP0_WRITE_BYTE(0); |
AjK | 0:0a841b89d614 | 123 | while (SSP0_IS_BUSY); |
AjK | 0:0a841b89d614 | 124 | FLASH_CS_DEASSERT; |
AjK | 0:0a841b89d614 | 125 | } |
AjK | 0:0a841b89d614 | 126 | |
AjK | 0:0a841b89d614 | 127 | FLASH_CS_ASSERT; |
AjK | 0:0a841b89d614 | 128 | FLASH_SHORT_COMMAND(FLASH_BE); |
AjK | 0:0a841b89d614 | 129 | FLASH_CS_DEASSERT; |
AjK | 0:0a841b89d614 | 130 | |
AjK | 0:0a841b89d614 | 131 | /* Note, a bulk erase takes much longer than |
AjK | 0:0a841b89d614 | 132 | a page write (typical 8s by the datasheet) |
AjK | 0:0a841b89d614 | 133 | so there's no point making the first timeout |
AjK | 0:0a841b89d614 | 134 | very short and producing a lot of uneeded |
AjK | 0:0a841b89d614 | 135 | interrupts. So we set the first timeout to |
AjK | 0:0a841b89d614 | 136 | be 8000 and then it'll switch to a much shorter |
AjK | 0:0a841b89d614 | 137 | time in the ISR. */ |
AjK | 0:0a841b89d614 | 138 | rit_timer_set_counter(FLASH_WRITE_CB, 8000); |
AjK | 0:0a841b89d614 | 139 | return 1; |
AjK | 0:0a841b89d614 | 140 | } |
AjK | 0:0a841b89d614 | 141 |