Changes ported from modules, to support multiple tmp101 sensors.
[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 /* ROM helpers are functions avalable for the user which do not use space in the
22  *  internal reprogrammable flash.
23  * They are stored in the internal ROM memory and may be called using specific
24  *  calls with defined parameters depending on the ROM call used.
25  * Refer to LPC176x documentation (UM10360.pdf) for more information.
26  */
28 #include <stdint.h>
29 #include "core/system.h"
30 #include "core/lpc_core_cm3.h"
33 /*******************************************************************************/
34 /*            In Application Programming ROM based routines                    */
35 /*******************************************************************************/
37 enum iap_status {
38         IAP_STATUS_CMD_SUCCESS = 0,
39         IAP_STATUS_INVALID_COMMAND,
40         IAP_STATUS_SRC_ADDR_ERROR,
41         IAP_STATUS_DST_ADDR_ERROR,
42         IAP_STATUS_SRC_ADDR_NOT_MAPPED,
43         IAP_STATUS_DST_ADDR_NOT_MAPPED,
44         IAP_STATUS_COUNT_ERROR,
45         IAP_STATUS_INVALID_SECTOR,
46         IAP_STATUS_SECTOR_NOT_BLANK,
47         IAP_STATUS_SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION,
48         IAP_STATUS_COMPARE_ERROR,
49         IAP_STATUS_BUSY,
50 };
52 enum iap_commands {
53         IAP_CMD_PREPARE_SECTORS_FOR_WRITE = 50,
54         IAP_CMD_COPY_RAM_TO_FLASH = 51,
55         IAP_CMD_ERASE_SECTORS = 52,
56         IAP_CMD_BLANK_CHECK_SECTORS = 53,
57         IAP_CMD_READ_PART_ID = 54,
58         IAP_CMD_READ_BOOT_CODE_VERSION = 55,
59         IAP_CMD_COMPARE = 56,
60         IAP_CMD_REINVOQUE_ISP = 57,
61         IAP_CMD_READ_UID = 58,
62 };
64 typedef void (*iap_entry_func)(uint32_t*, uint32_t*);
65 iap_entry_func iap_entry;
67 static uint32_t params[5];
68 static uint32_t results[4];
70 int iap_prepare_flash(uint32_t start_sector, uint32_t end_sector)
71 {
72         params[0] = IAP_CMD_PREPARE_SECTORS_FOR_WRITE;
73         params[1] = start_sector;
74         params[2] = end_sector;
75         iap_entry(params, results);
76         return results[0];
77 }
79 int iap_erase_flash_sectors(uint32_t start_sector, uint32_t end_sector)
80 {
81         params[0] = IAP_CMD_ERASE_SECTORS;
82         params[1] = start_sector;
83         params[2] = end_sector;
84         params[3] = (get_main_clock() / 1000);
85         lpc_disable_irq();
86         iap_entry(params, results);
87         lpc_enable_irq();
88         return results[0];
89 }
91 int iap_copy_ram_to_flash(uint32_t dest, uint32_t src, uint32_t size)
92 {
93         params[0] = IAP_CMD_COPY_RAM_TO_FLASH;
94         params[1] = dest & ~(0x03);
95         params[2] = src & ~(0x03);
96         params[3] = size & ~(0x03);
97         params[4] = (get_main_clock() / 1000);
98         lpc_disable_irq();
99         iap_entry(params, results);
100         lpc_enable_irq();
101         return results[0];
104 uint32_t iap_read_part_id(void)
106         params[0] = IAP_CMD_READ_PART_ID;
107         iap_entry(params, results);
108         return results[1];
112 /*******************************************************************************/
113 /*            Rom based routines initialisation                                */
114 /*******************************************************************************/
115 #define LPC_176x_IAP_ROM_LOC (0x1FFF1FF1)
117 void rom_helpers_init(void)
119         iap_entry = (iap_entry_func)LPC_176x_IAP_ROM_LOC;