| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
main.c
Go to the documentation of this file.
1 /*
2 * @brief Startup file for SPIFI programmer example
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2012
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 <string.h>
33
34 #include "flash_config.h"
35
67 /*****************************************************************************
68 * Private types/enumerations/variables
69 ****************************************************************************/
70
71 static uint8_t data_buffer[PROG_SIZE];
72 static SPIFIobj spifi_obj;
73
74 /*****************************************************************************
75 * Public types/enumerations/variables
76 ****************************************************************************/
77
78 /*****************************************************************************
79 * Private functions
80 ****************************************************************************/
81
82 /* Verify the spifi data */
83 static int verify_spifi_data(const uint8_t *buff, int size)
84 {
85 int i;
86 for (i = 0; i < size; i++) {
87 if (*buff++ == (uint8_t) i) {
88 break;
89 }
90 }
91 return i == size ? 0 : i;
92 }
93
94 /* Initializes the buffer from 00 to FF */
95 static void prepare_write_data(uint8_t *buff, int size)
96 {
97 int i;
98 for (i = 0; i < size; i++) {
99 *buff++ = (uint8_t) i;
100 }
101 }
102
103 /*****************************************************************************
104 * Public functions
105 ****************************************************************************/
106
111 int main(void)
112 {
113 SPIFIobj *obj = &spifi_obj;
114 uint32_t spifi_clk_mhz;
115 SPIFIopers opers;
116 int ret;
117
118 spifi_rom_init();
119
120 #ifndef USE_SPIFI_LIB
121 /* Decrease the CPU Clock for accessing ROM */
122 Chip_Clock_SetCPUClockDiv(2);
123 #endif
124
125 /* Initialize the board & LEDs for error indication */
126 Board_Init();
127
128 DEBUGSTR("SPIFI Demo\r\n");
129
130 /* Initialize SPIFI interface */
131 Board_SPIFI_Init();
132
133 /* Enable SPIFI Clocking */
134 Chip_SPIFI_Init();
135
136 /* Since this code runs from SPIFI no special initialization required here */
137 prepare_write_data(data_buffer, sizeof(data_buffer));
138
139 spifi_clk_mhz = Chip_Clock_GetSPIFIClockRate() / 1000000;
140
141 /* Typical time tCS is 20 ns min, we give 200 ns to be on safer side */
142 ret = spifi_init(obj, spifi_clk_mhz / 5, S_RCVCLK | S_FULLCLK, spifi_clk_mhz);
143 if (ret) {
144 DEBUGOUT("Error 0x%x: Initializing SPIFI interface!\r\n", ret);
145 Board_LED_Set(1, 1);
146 goto end_prog;
147 }
148
149 /* Prepare the operations structure */
150 memset(&opers, 0, sizeof(SPIFIopers));
151 opers.dest = (char *) SPIFI_WRITE_SECTOR_OFFSET;
152 opers.length = sizeof(data_buffer);
153 opers.scratch = NULL;
154 opers.protect = 0;
155 opers.options = S_VERIFY_PROG;
156
157 /* NOTE: All interrupts must be disabled before calling program as
158 * any triggered interrupts might attempt to run a code from SPIFI area
159 */
160 ret = spifi_program(obj, (char *) data_buffer, &opers);
161 if (ret) {
162 DEBUGOUT("Error 0x%x: Programming of data buffer to SPIFI Failed!\r\n", ret);
163 Board_LED_Set(1, 1);
164 goto end_prog;
165 }
166 DEBUGSTR("SPIFI Programming successful!\r\n");
167
168 if (verify_spifi_data((uint8_t *) SPIFI_WRITE_SECTOR_ADDRESS, sizeof(data_buffer))) {
169 DEBUGSTR("Error: Verifying the SPIFI data\r\n");
170 Board_LED_Set(1, 1);
171 goto end_prog;
172 }
173
174 Board_LED_Set(0, 1);
175 DEBUGSTR("SPIFI Data verified!\r\n");
176
177 end_prog:
178 while (1) {
179 __WFI();
180 }
181 }
182