LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 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 
76 // FIXME - needs standards formatting
77 
78 static uint8_t data_buffer[PROG_SIZE];
79 static SPIFIobj spifi_obj;
80 
81 /* Verify the spifi data */
82 static int verify_spifi_data(const uint8_t *buff, int size)
83 {
84  int i;
85  for (i = 0; i < size; i++) {
86  if (*buff++ == (uint8_t) i) break;
87  }
88  return i == size ? 0 : i;
89 }
90 
91 /* Initializes the buffer from 00 to FF */
92 static void prepare_write_data(uint8_t *buff, int size)
93 {
94  int i;
95  for (i = 0; i < size; i ++) {
96  *buff++ = (uint8_t) i;
97  }
98 }
99 
104 int main(void)
105 {
106  SPIFIobj *obj = &spifi_obj;
107  uint32_t spifi_clk_mhz;
108  SPIFIopers opers;
109  int ret;
110  spifi_rom_init(spifi);
111 
112  /* Initialize the board & LEDs for error indication */
113  Board_Init();
114 
115  /* Since this code runs from SPIFI no special initialization required here */
117 
118  spifi_clk_mhz = Chip_Clock_GetRate(CLK_MX_SPIFI) / 1000000;
119 
120  /* Typical time tCS is 20 ns min, we give 200 ns to be on safer side */
121  if (spifi_init(obj, spifi_clk_mhz / 5, S_RCVCLK | S_FULLCLK, spifi_clk_mhz)) {
122  DEBUGSTR("Error initializing SPIFI interface!\r\n");
123  Board_LED_Set(1, 1);
124  goto end_prog;
125  }
126 
127  /* Prepare the operations structure */
128  memset(&opers, 0, sizeof(SPIFIopers));
129  opers.dest = (char *) SPIFI_WRITE_SECTOR_OFFSET;
130  opers.length = sizeof(data_buffer);
131  /* opers.options = S_VERIFY_PROG; */
132 
133  /* NOTE: All interrupts must be disabled before calling program as
134  * any triggered interrupts might attempt to run a code from SPIFI area
135  */
136  ret = spifi_program(obj, (char *) data_buffer, &opers);
137  if (ret) {
138  DEBUGOUT("Error 0x%x: Programming of data buffer to SPIFI Failed!\r\n", ret);
139  Board_LED_Set(1, 1);
140  goto end_prog;
141  }
142  DEBUGSTR("SPIFI Programming successful!\r\n");
143 
144  if (verify_spifi_data((uint8_t *) SPIFI_WRITE_SECTOR_ADDRESS, sizeof(data_buffer))) {
145  DEBUGSTR("Error verifying the SPIFI data\r\n");
146  Board_LED_Set(1, 1);
147  goto end_prog;
148  }
149  Board_LED_Set(0, 1);
150  DEBUGSTR("SPIFI Data verified!\r\n");
151 
152 end_prog:
153  while(1) {__WFI();}
154 }