Satellite Observers Workbench. NOT yet complete, just published for forum posters to \"cherry pick\" pieces of code as requiered as an example.
config/config.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 | /* Need to come back and finish this. */ |
AjK | 0:0a841b89d614 | 24 | |
AjK | 0:0a841b89d614 | 25 | #include "sowb.h" |
AjK | 0:0a841b89d614 | 26 | #include "user.h" |
AjK | 0:0a841b89d614 | 27 | #include "debug.h" |
AjK | 0:0a841b89d614 | 28 | #include "config.h" |
AjK | 0:0a841b89d614 | 29 | #include "ff.h" |
AjK | 0:0a841b89d614 | 30 | |
AjK | 0:0a841b89d614 | 31 | CONFIG_UNION system_config; |
AjK | 0:0a841b89d614 | 32 | |
AjK | 0:0a841b89d614 | 33 | bool config_block_process; |
AjK | 0:0a841b89d614 | 34 | bool config_loaded; |
AjK | 0:0a841b89d614 | 35 | bool config_reload; |
AjK | 0:0a841b89d614 | 36 | |
AjK | 0:0a841b89d614 | 37 | /** config_init |
AjK | 0:0a841b89d614 | 38 | */ |
AjK | 0:0a841b89d614 | 39 | void config_init(void) { |
AjK | 0:0a841b89d614 | 40 | int i, j; |
AjK | 0:0a841b89d614 | 41 | |
AjK | 0:0a841b89d614 | 42 | DEBUG_INIT_START; |
AjK | 0:0a841b89d614 | 43 | |
AjK | 0:0a841b89d614 | 44 | /* |
AjK | 0:0a841b89d614 | 45 | for (i = CONFIG_FLASH_PAGE_BASE, j = 0; i < 4096; i++, j++) { |
AjK | 0:0a841b89d614 | 46 | flash_read_page(i, system_config.buffers[j], true); |
AjK | 0:0a841b89d614 | 47 | } |
AjK | 0:0a841b89d614 | 48 | |
AjK | 0:0a841b89d614 | 49 | config_loaded = true; |
AjK | 0:0a841b89d614 | 50 | config_reload = false; |
AjK | 0:0a841b89d614 | 51 | config_block_process = false; |
AjK | 0:0a841b89d614 | 52 | */ |
AjK | 0:0a841b89d614 | 53 | |
AjK | 0:0a841b89d614 | 54 | DEBUG_INIT_END; |
AjK | 0:0a841b89d614 | 55 | } |
AjK | 0:0a841b89d614 | 56 | |
AjK | 0:0a841b89d614 | 57 | /** config_process |
AjK | 0:0a841b89d614 | 58 | */ |
AjK | 0:0a841b89d614 | 59 | void config_process(void) { |
AjK | 0:0a841b89d614 | 60 | |
AjK | 0:0a841b89d614 | 61 | /* For long period operations (e.g. config_save()) that may call |
AjK | 0:0a841b89d614 | 62 | system _process() functions, block ourselves from re-entering. */ |
AjK | 0:0a841b89d614 | 63 | if (config_block_process) return; |
AjK | 0:0a841b89d614 | 64 | |
AjK | 0:0a841b89d614 | 65 | } |
AjK | 0:0a841b89d614 | 66 | |
AjK | 0:0a841b89d614 | 67 | void config_save(void) { |
AjK | 0:0a841b89d614 | 68 | int i, j; |
AjK | 0:0a841b89d614 | 69 | char buffer[FLASH_PAGE_SIZE]; |
AjK | 0:0a841b89d614 | 70 | |
AjK | 0:0a841b89d614 | 71 | /* Don't re-enter this function from _process(). */ |
AjK | 0:0a841b89d614 | 72 | config_block_process = true; |
AjK | 0:0a841b89d614 | 73 | |
AjK | 0:0a841b89d614 | 74 | /* Sector 15 is used as a "scratch area". It allows us to store |
AjK | 0:0a841b89d614 | 75 | pages from other sectors that we need to "restore" since the |
AjK | 0:0a841b89d614 | 76 | LPC1768 doesn't have enough space to store entire sectors. */ |
AjK | 0:0a841b89d614 | 77 | while (flash_sector_erase_in_progress()) WHILE_WAITING_DO_PROCESS_FUNCTIONS; |
AjK | 0:0a841b89d614 | 78 | flash_erase_sector(15); |
AjK | 0:0a841b89d614 | 79 | while (flash_sector_erase_in_progress()) WHILE_WAITING_DO_PROCESS_FUNCTIONS; |
AjK | 0:0a841b89d614 | 80 | |
AjK | 0:0a841b89d614 | 81 | /* We need to make a copy of all the pages below our config area |
AjK | 0:0a841b89d614 | 82 | before we store our configuration. */ |
AjK | 0:0a841b89d614 | 83 | for (i = 4096 - 256; i < CONFIG_FLASH_PAGE_BASE; i++) { |
AjK | 0:0a841b89d614 | 84 | flash_read_page(i, buffer, true); |
AjK | 0:0a841b89d614 | 85 | flash_page_write(3840 + i, buffer); |
AjK | 0:0a841b89d614 | 86 | while(flash_write_in_progress()) WHILE_WAITING_DO_PROCESS_FUNCTIONS; |
AjK | 0:0a841b89d614 | 87 | } |
AjK | 0:0a841b89d614 | 88 | |
AjK | 0:0a841b89d614 | 89 | /* Now erase the sector in which our config resides. */ |
AjK | 0:0a841b89d614 | 90 | while (flash_sector_erase_in_progress()) WHILE_WAITING_DO_PROCESS_FUNCTIONS; |
AjK | 0:0a841b89d614 | 91 | flash_erase_sector(14); |
AjK | 0:0a841b89d614 | 92 | while (flash_sector_erase_in_progress()) WHILE_WAITING_DO_PROCESS_FUNCTIONS; |
AjK | 0:0a841b89d614 | 93 | |
AjK | 0:0a841b89d614 | 94 | |
AjK | 0:0a841b89d614 | 95 | for (i = CONFIG_FLASH_PAGE_BASE, j = 0; i < CONFIG_FLASH_PAGE_BASE + CONFIG_FLASH_PAGES; i++, j++) { |
AjK | 0:0a841b89d614 | 96 | while(flash_write_in_progress() || flash_sector_erase_in_progress()) { |
AjK | 0:0a841b89d614 | 97 | WHILE_WAITING_DO_PROCESS_FUNCTIONS; |
AjK | 0:0a841b89d614 | 98 | } |
AjK | 0:0a841b89d614 | 99 | flash_page_write(i, system_config.buffers[j]); |
AjK | 0:0a841b89d614 | 100 | } |
AjK | 0:0a841b89d614 | 101 | |
AjK | 0:0a841b89d614 | 102 | config_block_process = false; |
AjK | 0:0a841b89d614 | 103 | } |
AjK | 0:0a841b89d614 | 104 | |
AjK | 0:0a841b89d614 | 105 | /** config_copy_flash_page |
AjK | 0:0a841b89d614 | 106 | * |
AjK | 0:0a841b89d614 | 107 | * Used to copy the raw config struct, page by page |
AjK | 0:0a841b89d614 | 108 | * to an external memory buffer. |
AjK | 0:0a841b89d614 | 109 | * |
AjK | 0:0a841b89d614 | 110 | * @param int page The page to copy. |
AjK | 0:0a841b89d614 | 111 | * @param char* buffer The buffer to copy the page to. |
AjK | 0:0a841b89d614 | 112 | */ |
AjK | 0:0a841b89d614 | 113 | void config_copy_flash_page(int page, char *buffer) { |
AjK | 0:0a841b89d614 | 114 | memcpy(buffer, system_config.buffers[page], FLASH_PAGE_SIZE); |
AjK | 0:0a841b89d614 | 115 | } |
AjK | 0:0a841b89d614 | 116 | |
AjK | 0:0a841b89d614 | 117 | /** config_get_page |
AjK | 0:0a841b89d614 | 118 | * |
AjK | 0:0a841b89d614 | 119 | * Get the base address of a specific page of config data. |
AjK | 0:0a841b89d614 | 120 | * |
AjK | 0:0a841b89d614 | 121 | * @param int page The page to get the address of. |
AjK | 0:0a841b89d614 | 122 | * @return char* The address of the page. |
AjK | 0:0a841b89d614 | 123 | */ |
AjK | 0:0a841b89d614 | 124 | char * config_get_page(int page) { |
AjK | 0:0a841b89d614 | 125 | return system_config.buffers[page]; |
AjK | 0:0a841b89d614 | 126 | } |
AjK | 0:0a841b89d614 | 127 | |
AjK | 0:0a841b89d614 | 128 | |
AjK | 0:0a841b89d614 | 129 |