1 /*
2  * @brief Common IAP 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 licenser 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 #ifndef __IAP_H_
33 #define __IAP_H_
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /** @defgroup COMMON_IAP CHIP: Common Chip ISP/IAP commands and return codes
40  * @ingroup CHIP_Common
41  * @{
42  */
43 
44 /* IAP command definitions */
45 #define IAP_PREWRRITE_CMD           50	/*!< Prepare sector for write operation command */
46 #define IAP_WRISECTOR_CMD           51	/*!< Write Sector command */
47 #define IAP_ERSSECTOR_CMD           52	/*!< Erase Sector command */
48 #define IAP_BLANK_CHECK_SECTOR_CMD  53	/*!< Blank check sector */
49 #define IAP_REPID_CMD               54	/*!< Read PartID command */
50 #define IAP_READ_BOOT_CODE_CMD      55	/*!< Read Boot code version */
51 #define IAP_COMPARE_CMD             56	/*!< Compare two RAM address locations */
52 #define IAP_REINVOKE_ISP_CMD        57	/*!< Reinvoke ISP */
53 #define IAP_READ_UID_CMD            58	/*!< Read UID */
54 #define IAP_ERASE_PAGE_CMD          59	/*!< Erase page */
55 #define IAP_EEPROM_WRITE            61	/*!< EEPROM Write command */
56 #define IAP_EEPROM_READ             62	/*!< EEPROM READ command */
57 
58 /* IAP response definitions */
59 #define IAP_CMD_SUCCESS             0	/*!< Command is executed successfully */
60 #define IAP_INVALID_COMMAND         1	/*!< Invalid command */
61 #define IAP_SRC_ADDR_ERROR          2	/*!< Source address is not on word boundary */
62 #define IAP_DST_ADDR_ERROR          3	/*!< Destination address is not on a correct boundary */
63 #define IAP_SRC_ADDR_NOT_MAPPED     4	/*!< Source address is not mapped in the memory map */
64 #define IAP_DST_ADDR_NOT_MAPPED     5	/*!< Destination address is not mapped in the memory map */
65 #define IAP_COUNT_ERROR             6	/*!< Byte count is not multiple of 4 or is not a permitted value */
66 #define IAP_INVALID_SECTOR          7	/*!< Sector number is invalid or end sector number is greater than start sector number */
67 #define IAP_SECTOR_NOT_BLANK        8	/*!< Sector is not blank */
68 #define IAP_SECTOR_NOT_PREPARED     9	/*!< Command to prepare sector for write operation was not executed */
69 #define IAP_COMPARE_ERROR           10	/*!< Source and destination data not equal */
70 #define IAP_BUSY                    11	/*!< Flash programming hardware interface is busy */
71 #define IAP_PARAM_ERROR             12	/*!< nsufficient number of parameters or invalid parameter */
72 #define IAP_ADDR_ERROR              13	/*!< Address is not on word boundary */
73 #define IAP_ADDR_NOT_MAPPED         14	/*!< Address is not mapped in the memory map */
74 #define IAP_CMD_LOCKED              15	/*!< Command is locked */
75 #define IAP_INVALID_CODE            16	/*!< Unlock code is invalid */
76 #define IAP_INVALID_BAUD_RATE       17	/*!< Invalid baud rate setting */
77 #define IAP_INVALID_STOP_BIT        18	/*!< Invalid stop bit setting */
78 #define IAP_CRP_ENABLED             19	/*!< Code read protection enabled */
79 
80 /* IAP_ENTRY API function type */
81 typedef void (*IAP_ENTRY_T)(unsigned int[5], unsigned int[4]);
82 
83 /**
84  * @brief	Prepare sector for write operation
85  * @param	strSector	: Start sector number
86  * @param	endSector	: End sector number
87  * @return	Status code to indicate the command is executed successfully or not
88  * @note	This command must be executed before executing "Copy RAM to flash"
89  *			or "Erase Sector" command.
90  *			The end sector must be greater than or equal to start sector number
91  */
92 uint8_t Chip_IAP_PreSectorForReadWrite(uint32_t strSector, uint32_t endSector);
93 
94 /**
95  * @brief	Copy RAM to flash
96  * @param	dstAdd		: Destination flash address where data bytes are to be written
97  * @param	srcAdd		: Source flash address where data bytes are to be read
98  * @param	byteswrt	: Number of bytes to be written
99  * @return	Status code to indicate the command is executed successfully or not
100  * @note	The addresses should be a 256 byte boundary and the number of bytes
101  *			should be 256 | 512 | 1024 | 4096
102  */
103 uint8_t Chip_IAP_CopyRamToFlash(uint32_t dstAdd, uint32_t *srcAdd, uint32_t byteswrt);
104 
105 /**
106  * @brief	Erase sector
107  * @param	strSector	: Start sector number
108  * @param	endSector	: End sector number
109  * @return	Status code to indicate the command is executed successfully or not
110  * @note	The end sector must be greater than or equal to start sector number
111  */
112 uint8_t Chip_IAP_EraseSector(uint32_t strSector, uint32_t endSector);
113 
114 /**
115  * @brief Blank check a sector or multiples sector of on-chip flash memory
116  * @param	strSector	: Start sector number
117  * @param	endSector	: End sector number
118  * @return	Offset of the first non blank word location if the status code is SECTOR_NOT_BLANK
119  * @note	The end sector must be greater than or equal to start sector number
120  */
121 // FIXME - There are two return value (result[0] & result[1]
122 // Result0:Offset of the first non blank word location if the Status Code is
123 // SECTOR_NOT_BLANK.
124 // Result1:Contents of non blank word location.
125 uint8_t Chip_IAP_BlankCheckSector(uint32_t strSector, uint32_t endSector);
126 
127 /**
128  * @brief	Read part identification number
129  * @return	Part identification number
130  */
131 uint32_t Chip_IAP_ReadPID(void);
132 
133 /**
134  * @brief	Read boot code version number
135  * @return	Boot code version number
136  */
137 uint8_t Chip_IAP_ReadBootCode(void);
138 
139 /**
140  * @brief	Compare the memory contents at two locations
141  * @param	dstAdd		: Destination of the RAM address of data bytes to be compared
142  * @param	srcAdd		: Source of the RAM address of data bytes to be compared
143  * @param	bytescmp	: Number of bytes to be compared
144  * @return	Offset of the first mismatch of the status code is COMPARE_ERROR
145  * @note	The addresses should be a word boundary and number of bytes should be
146  *			a multiply of 4
147  */
148 uint8_t Chip_IAP_Compare(uint32_t dstAdd, uint32_t srcAdd, uint32_t bytescmp);
149 
150 /**
151  * @brief	IAP reinvoke ISP to invoke the bootloader in ISP mode
152  * @return	none
153  */
154 uint8_t Chip_IAP_ReinvokeISP(void);
155 
156 /**
157  * @brief	Read the unique ID
158  * @return	Status code to indicate the command is executed successfully or not
159  */
160 uint32_t Chip_IAP_ReadUID(void);
161 
162 /**
163  * @brief	Erase a page or multiple papers of on-chip flash memory
164  * @param	strPage	: Start page number
165  * @param	endPage	: End page number
166  * @return	Status code to indicate the command is executed successfully or not
167  * @note	The page number must be greater than or equal to start page number
168  */
169 // FIXME - There are four return value
170 // Result0:The first 32-bit word (at the lowest address)
171 // Result1:The second 32-bit word.
172 // Result2:The third 32-bit word.
173 // Result3:The fourth 32-bit word.
174 uint8_t Chip_IAP_ErasePage(uint32_t strPage, uint32_t endPage);
175 
176 /**
177  * @}
178  */
179 
180 #ifdef __cplusplus
181 }
182 #endif
183 
184 #endif /* __IAP_H_ */
185