1 /*
2  * @brief Common FLASH support functions
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2013
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products.  This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights.  NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers.  This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include "chip.h"
33 
34 /*****************************************************************************
35  * Private types/enumerations/variables
36  ****************************************************************************/
37 
38 /*****************************************************************************
39  * Public types/enumerations/variables
40  ****************************************************************************/
41 
42 /*****************************************************************************
43  * Private functions
44  ****************************************************************************/
45 
46 /*****************************************************************************
47  * Public functions
48  ****************************************************************************/
49 
50 /* Prepare sector for write operation */
Chip_IAP_PreSectorForReadWrite(uint32_t strSector,uint32_t endSector)51 uint8_t Chip_IAP_PreSectorForReadWrite(uint32_t strSector, uint32_t endSector)
52 {
53 	uint32_t command[5], result[4];
54 
55 	command[0] = IAP_PREWRRITE_CMD;
56 	command[1] = strSector;
57 	command[2] = endSector;
58 	iap_entry(command, result);
59 
60 	return result[0];
61 }
62 
63 /* Copy RAM to flash */
Chip_IAP_CopyRamToFlash(uint32_t dstAdd,uint32_t * srcAdd,uint32_t byteswrt)64 uint8_t Chip_IAP_CopyRamToFlash(uint32_t dstAdd, uint32_t *srcAdd, uint32_t byteswrt)
65 {
66 	uint32_t command[5], result[4];
67 
68 	command[0] = IAP_WRISECTOR_CMD;
69 	command[1] = dstAdd;
70 	command[2] = (uint32_t) srcAdd;
71 	command[3] = byteswrt;
72 	command[4] = SystemCoreClock / 1000;
73 	iap_entry(command, result);
74 
75 	return result[0];
76 }
77 
78 /* Erase sector */
Chip_IAP_EraseSector(uint32_t strSector,uint32_t endSector)79 uint8_t Chip_IAP_EraseSector(uint32_t strSector, uint32_t endSector)
80 {
81 	uint32_t command[5], result[4];
82 
83 	command[0] = IAP_ERSSECTOR_CMD;
84 	command[1] = strSector;
85 	command[2] = endSector;
86 	command[3] = SystemCoreClock / 1000;
87 	iap_entry(command, result);
88 
89 	return result[0];
90 }
91 
92 /* Blank check sector */
Chip_IAP_BlankCheckSector(uint32_t strSector,uint32_t endSector)93 uint8_t Chip_IAP_BlankCheckSector(uint32_t strSector, uint32_t endSector)
94 {
95 	uint32_t command[5], result[4];
96 
97 	command[0] = IAP_BLANK_CHECK_SECTOR_CMD;
98 	command[1] = strSector;
99 	command[2] = endSector;
100 	iap_entry(command, result);
101 
102 	return result[0];
103 }
104 
105 /* Read part identification number */
Chip_IAP_ReadPID()106 uint32_t Chip_IAP_ReadPID()
107 {
108 	uint32_t command[5], result[4];
109 
110 	command[0] = IAP_REPID_CMD;
111 	iap_entry(command, result);
112 
113 	return result[1];
114 }
115 
116 /* Read boot code version number */
Chip_IAP_ReadBootCode()117 uint8_t Chip_IAP_ReadBootCode()
118 {
119 	uint32_t command[5], result[4];
120 
121 	command[0] = IAP_READ_BOOT_CODE_CMD;
122 	iap_entry(command, result);
123 
124 	return result[0];
125 }
126 
127 /* IAP compare */
Chip_IAP_Compare(uint32_t dstAdd,uint32_t srcAdd,uint32_t bytescmp)128 uint8_t Chip_IAP_Compare(uint32_t dstAdd, uint32_t srcAdd, uint32_t bytescmp)
129 {
130 	uint32_t command[5], result[4];
131 
132 	command[0] = IAP_COMPARE_CMD;
133 	command[1] = dstAdd;
134 	command[2] = srcAdd;
135 	command[3] = bytescmp;
136 	iap_entry(command, result);
137 
138 	return result[0];
139 }
140 
141 /* Reinvoke ISP */
Chip_IAP_ReinvokeISP()142 uint8_t Chip_IAP_ReinvokeISP()
143 {
144 	uint32_t command[5], result[4];
145 
146 	command[0] = IAP_REINVOKE_ISP_CMD;
147 	iap_entry(command, result);
148 
149 	return result[0];
150 }
151 
152 /* Read the unique ID */
Chip_IAP_ReadUID()153 uint32_t Chip_IAP_ReadUID()
154 {
155 	uint32_t command[5], result[4];
156 
157 	command[0] = IAP_READ_UID_CMD;
158 	iap_entry(command, result);
159 
160 	return result[1];
161 }
162 
163 /* Erase page */
Chip_IAP_ErasePage(uint32_t strPage,uint32_t endPage)164 uint8_t Chip_IAP_ErasePage(uint32_t strPage, uint32_t endPage)
165 {
166 	uint32_t command[5], result[4];
167 
168 	command[0] = IAP_ERASE_PAGE_CMD;
169 	command[1] = strPage;
170 	command[2] = endPage;
171 	command[3] = SystemCoreClock / 1000;
172 	iap_entry(command, result);
173 
174 	return result[0];
175 }
176