Base code for dtplug : Core, drivers, lib and some external drivers.
[dtplug] / core / rom_helpers.c
1 /****************************************************************************
2  *   core/rom_helpers.c
3  *
4  *
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 2 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  *************************************************************************** */
21 #include <stdint.h>
22 #include "core/system.h"
23 #include "core/lpc_core_cm3.h"
26 /*******************************************************************************/
27 /*            In Application Programming ROM based routines                    */
28 /*******************************************************************************/
30 enum iap_status {
31         IAP_STATUS_CMD_SUCCESS = 0,
32         IAP_STATUS_INVALID_COMMAND,
33         IAP_STATUS_SRC_ADDR_ERROR,
34         IAP_STATUS_DST_ADDR_ERROR,
35         IAP_STATUS_SRC_ADDR_NOT_MAPPED,
36         IAP_STATUS_DST_ADDR_NOT_MAPPED,
37         IAP_STATUS_COUNT_ERROR,
38         IAP_STATUS_INVALID_SECTOR,
39         IAP_STATUS_SECTOR_NOT_BLANK,
40         IAP_STATUS_SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION,
41         IAP_STATUS_COMPARE_ERROR,
42         IAP_STATUS_BUSY,
43 };
45 enum iap_commands {
46         IAP_CMD_PREPARE_SECTORS_FOR_WRITE = 50,
47         IAP_CMD_COPY_RAM_TO_FLASH = 51,
48         IAP_CMD_ERASE_SECTORS = 52,
49         IAP_CMD_BLANK_CHECK_SECTORS = 53,
50         IAP_CMD_READ_PART_ID = 54,
51         IAP_CMD_READ_BOOT_CODE_VERSION = 55,
52         IAP_CMD_COMPARE = 56,
53         IAP_CMD_REINVOQUE_ISP = 57,
54         IAP_CMD_READ_UID = 58,
55 };
57 typedef void (*iap_entry_func)(uint32_t*, uint32_t*);
58 iap_entry_func iap_entry;
60 static uint32_t params[5];
61 static uint32_t results[4];
63 int iap_prepare_flash(uint32_t start_sector, uint32_t end_sector)
64 {
65         params[0] = IAP_CMD_PREPARE_SECTORS_FOR_WRITE;
66         params[1] = start_sector;
67         params[2] = end_sector;
68         iap_entry(params, results);
69         return results[0];
70 }
72 int iap_erase_flash_sectors(uint32_t start_sector, uint32_t end_sector)
73 {
74         params[0] = IAP_CMD_ERASE_SECTORS;
75         params[1] = start_sector;
76         params[2] = end_sector;
77         params[3] = (get_main_clock() / 1000);
78         lpc_disable_irq();
79         iap_entry(params, results);
80         lpc_enable_irq();
81         return results[0];
82 }
84 int iap_copy_ram_to_flash(uint32_t dest, uint32_t src, uint32_t size)
85 {
86         params[0] = IAP_CMD_COPY_RAM_TO_FLASH;
87         params[1] = dest & ~(0x03);
88         params[2] = src & ~(0x03);
89         params[3] = size & ~(0x03);
90         params[4] = (get_main_clock() / 1000);
91         lpc_disable_irq();
92         iap_entry(params, results);
93         lpc_enable_irq();
94         return results[0];
95 }
97 uint32_t iap_read_part_id(void)
98 {
99         params[0] = IAP_CMD_READ_PART_ID;
100         iap_entry(params, results);
101         return results[1];
105 /*******************************************************************************/
106 /*            Rom based routines initialisation                                */
107 /*******************************************************************************/
108 #define LPC_176x_IAP_ROM_LOC (0x1FFF1FF1)
110 void rom_helpers_init(void)
112         iap_entry = (iap_entry_func)LPC_176x_IAP_ROM_LOC;