Merge PCB updates
[SCSI2SD.git] / software / SCSI2SD / v4 / SCSI2SD.cydsn / Generated_Source / PSoC5 / CFG_EEPROM.c
1 /*******************************************************************************
2 * File Name: CFG_EEPROM.c
3 * Version 2.10
4 *
5 * Description:
6 *  Provides the source code to the API for the EEPROM component.
7 *
8 ********************************************************************************
9 * Copyright 2008-2012, Cypress Semiconductor Corporation.  All rights reserved.
10 * You may use this file only in accordance with the license, terms, conditions,
11 * disclaimers, and limitations in the end user license agreement accompanying
12 * the software package with which this file was provided.
13 *******************************************************************************/
14
15 #include "CFG_EEPROM.h"
16
17
18 #if (CY_PSOC3 || CY_PSOC5LP)
19
20     /*******************************************************************************
21     * Function Name: CFG_EEPROM_Enable
22     ********************************************************************************
23     *
24     * Summary:
25     *  Enable the EEPROM.
26     *
27     * Parameters:
28     *  None
29     *
30     * Return:
31     *  None
32     *
33     *******************************************************************************/
34     void CFG_EEPROM_Enable(void) 
35     {
36         CyEEPROM_Start();
37     }
38
39
40     /*******************************************************************************
41     * Function Name: CFG_EEPROM_Start
42     ********************************************************************************
43     *
44     * Summary:
45     *  Starts EEPROM.
46     *
47     * Parameters:
48     *  None
49     *
50     * Return:
51     *  None
52     *
53     *******************************************************************************/
54     void CFG_EEPROM_Start(void) 
55     {
56         /* Enable the EEPROM */
57         CFG_EEPROM_Enable();
58     }
59
60
61     /*******************************************************************************
62     * Function Name: CFG_EEPROM_Stop
63     ********************************************************************************
64     *
65     * Summary:
66     *  Stops and powers down EEPROM.
67     *
68     * Parameters:
69     *  None
70     *
71     * Return:
72     *  None
73     *
74     *******************************************************************************/
75     void CFG_EEPROM_Stop (void) 
76     {
77         /* Disable EEPROM */
78         CyEEPROM_Stop();
79     }
80
81 #endif /* (CY_PSOC3 || CY_PSOC5LP) */
82
83
84 /*******************************************************************************
85 * Function Name: CFG_EEPROM_EraseSector
86 ********************************************************************************
87 *
88 * Summary:
89 *  Erases a sector of memory. This function blocks until the operation is
90 *  complete.
91 *
92 * Parameters:
93 *  sectorNumber:  Sector number to erase.
94 *
95 * Return:
96 *  CYRET_SUCCESS, if the operation was successful.
97 *  CYRET_BAD_PARAM, if the parameter sectorNumber out of range.
98 *  CYRET_LOCKED, if the spc is being used.
99 *  CYRET_UNKNOWN, if there was an SPC error.
100 *
101 *******************************************************************************/
102 cystatus CFG_EEPROM_EraseSector(uint8 sectorNumber) 
103 {
104     cystatus status;
105
106     /* Start the SPC */
107     CySpcStart();
108
109     if(sectorNumber < (uint8) CY_EEPROM_NUMBER_ARRAYS)
110     {
111         /* See if we can get the SPC. */
112         if(CySpcLock() == CYRET_SUCCESS)
113         {
114             #if(CY_PSOC5A)
115
116                 /* Plan for failure */
117                 status = CYRET_UNKNOWN;
118
119                 /* Command to load a row of data */
120                 if(CySpcLoadRow(CY_SPC_FIRST_EE_ARRAYID, 0, CYDEV_EEPROM_ROW_SIZE) == CYRET_STARTED)
121                 {
122                     while(CY_SPC_BUSY)
123                     {
124                         /* Wait until SPC becomes idle */
125                     }
126
127                     /* SPC is idle now */
128                     if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS)
129                     {
130                         status = CYRET_SUCCESS;
131                     }
132                 }
133
134                 /* Command to erase a sector */
135                 if(status == CYRET_SUCCESS)
136                 {
137
138             #endif /* (CY_PSOC5A) */
139
140                     if(CySpcEraseSector(CY_SPC_FIRST_EE_ARRAYID, sectorNumber) == CYRET_STARTED)
141                     {
142                         /* Plan for failure */
143                         status = CYRET_UNKNOWN;
144
145                         while(CY_SPC_BUSY)
146                         {
147                             /* Wait until SPC becomes idle */
148                         }
149
150                         /* SPC is idle now */
151                         if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS)
152                         {
153                             status = CYRET_SUCCESS;
154                         }
155                     }
156                     else
157                     {
158                         status = CYRET_UNKNOWN;
159                     }
160
161             #if(CY_PSOC5A)
162
163                 }
164                 else
165                 {
166                     status = CYRET_UNKNOWN;
167                 }
168
169             #endif /* (CY_PSOC5A) */
170
171                 /* Unlock the SPC so someone else can use it. */
172                 CySpcUnlock();
173         }
174         else
175         {
176             status = CYRET_LOCKED;
177         }
178     }
179     else
180     {
181         status = CYRET_BAD_PARAM;
182     }
183
184     return(status);
185 }
186
187
188 /*******************************************************************************
189 * Function Name: CFG_EEPROM_Write
190 ********************************************************************************
191 *
192 * Summary:
193 *  Writes a row, CYDEV_EEPROM_ROW_SIZE of data to the EEPROM. This is
194 *  a blocking call. It will not return until the function succeeds or fails.
195 *
196 * Parameters:
197 *  rowData:  Address of the data to write to the EEPROM.
198 *  rowNumber:  EEPROM row number to program.
199 *
200 * Return:
201 *  CYRET_SUCCESS, if the operation was successful.
202 *  CYRET_BAD_PARAM, if the parameter rowNumber out of range.
203 *  CYRET_LOCKED, if the spc is being used.
204 *  CYRET_UNKNOWN, if there was an SPC error.
205 *
206 *******************************************************************************/
207 cystatus CFG_EEPROM_Write(const uint8 * rowData, uint8 rowNumber) 
208 {
209     cystatus status;
210
211     /* Start the SPC */
212     CySpcStart();
213
214     if(rowNumber < (uint8) CY_EEPROM_NUMBER_ROWS)
215     {
216         /* See if we can get the SPC. */
217         if(CySpcLock() == CYRET_SUCCESS)
218         {
219             /* Plan for failure */
220             status = CYRET_UNKNOWN;
221
222             /* Command to load a row of data */
223             if(CySpcLoadRow(CY_SPC_FIRST_EE_ARRAYID, rowData, CYDEV_EEPROM_ROW_SIZE) == CYRET_STARTED)
224             {
225                 while(CY_SPC_BUSY)
226                 {
227                     /* Wait until SPC becomes idle */
228                 }
229
230                 /* SPC is idle now */
231                 if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS)
232                 {
233                     status = CYRET_SUCCESS;
234                 }
235
236                 /* Command to erase and program the row. */
237                 if(status == CYRET_SUCCESS)
238                 {
239                     if(CySpcWriteRow(CY_SPC_FIRST_EE_ARRAYID, (uint16)rowNumber, dieTemperature[0],
240                     dieTemperature[1]) == CYRET_STARTED)
241                     {
242                         /* Plan for failure */
243                         status = CYRET_UNKNOWN;
244
245                         while(CY_SPC_BUSY)
246                         {
247                             /* Wait until SPC becomes idle */
248                         }
249
250                         /* SPC is idle now */
251                         if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS)
252                         {
253                             status = CYRET_SUCCESS;
254                         }
255                     }
256                     else
257                     {
258                         status = CYRET_UNKNOWN;
259                     }
260                 }
261                 else
262                 {
263                     status = CYRET_UNKNOWN;
264                 }
265             }
266
267             /* Unlock the SPC so someone else can use it. */
268             CySpcUnlock();
269         }
270         else
271         {
272             status = CYRET_LOCKED;
273         }
274     }
275     else
276     {
277         status = CYRET_BAD_PARAM;
278     }
279
280     return(status);
281 }
282
283
284 /*******************************************************************************
285 * Function Name: CFG_EEPROM_StartWrite
286 ********************************************************************************
287 *
288 * Summary:
289 *  Starts the SPC write function. This function does not block, it returns
290 *  once the command has begun the SPC write function. This function must be used
291 *  in combination with CFG_EEPROM_QueryWrite(). Once this function has
292 *  been called the SPC will be locked until CFG_EEPROM_QueryWrite()
293 *  returns CYRET_SUCCESS.
294 *
295 * Parameters:
296 *  rowData:  Address of buffer containing a row of data to write to the EEPROM.
297 *  rowNumber:  EEPROM row number to program.
298 *
299 * Return:
300 *  CYRET_STARTED, if the spc command to write was successfuly started.
301 *  CYRET_BAD_PARAM, if the parameter rowNumber out of range.
302 *  CYRET_LOCKED, if the spc is being used.
303 *  CYRET_UNKNOWN, if there was an SPC error.
304 *
305 *******************************************************************************/
306 cystatus CFG_EEPROM_StartWrite(const uint8 * rowData, uint8 rowNumber) \
307
308 {
309     cystatus status;
310
311     if(rowNumber < (uint8) CY_EEPROM_NUMBER_ROWS)
312     {
313         /* See if we can get the SPC. */
314         if(CySpcLock() == CYRET_SUCCESS)
315         {
316             /* Plan for failure */
317             status = CYRET_UNKNOWN;
318
319             /* Command to load a row of data */
320             if(CySpcLoadRow(CY_SPC_FIRST_EE_ARRAYID, rowData, CYDEV_EEPROM_ROW_SIZE) == CYRET_STARTED)
321             {
322                 while(CY_SPC_BUSY)
323                 {
324                     /* Wait until SPC becomes idle */
325                 }
326
327                 /* SPC is idle now */
328                 if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS)
329                 {
330                     status = CYRET_SUCCESS;
331                 }
332
333                 /* Command to erase and program the row. */
334                 if(status == CYRET_SUCCESS)
335                 {
336                     if(CySpcWriteRow(CY_SPC_FIRST_EE_ARRAYID, (uint16)rowNumber, dieTemperature[0],
337                     dieTemperature[1]) == CYRET_STARTED)
338                     {
339                         status = CYRET_STARTED;
340                     }
341                     else
342                     {
343                         status = CYRET_UNKNOWN;
344                     }
345                 }
346                 else
347                 {
348                     status = CYRET_UNKNOWN;
349                 }
350             }
351         }
352         else
353         {
354             status = CYRET_LOCKED;
355         }
356     }
357     else
358     {
359         status = CYRET_BAD_PARAM;
360     }
361
362     return(status);
363 }
364
365
366 /*******************************************************************************
367 * Function Name: CFG_EEPROM_QueryWrite
368 ********************************************************************************
369 *
370 * Summary:
371 *  Checks the state of write to EEPROM. This function must be called until
372 *  the return value is not CYRET_STARTED.
373 *
374 * Parameters:
375 *  None
376 *
377 * Return:
378 *  CYRET_STARTED, if the spc command is still processing.
379 *  CYRET_SUCCESS, if the operation was successful.
380 *  CYRET_UNKNOWN, if there was an SPC error.
381 *
382 *******************************************************************************/
383 cystatus CFG_EEPROM_QueryWrite(void) 
384 {
385     cystatus status;
386
387     /* Check if SPC is idle */
388     if(CY_SPC_IDLE)
389     {
390         /* SPC is idle now */
391         if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS)
392         {
393             status = CYRET_SUCCESS;
394         }
395         else
396         {
397             status = CYRET_UNKNOWN;
398         }
399
400         /* Unlock the SPC so someone else can use it. */
401         CySpcUnlock();
402     }
403     else
404     {
405         status = CYRET_STARTED;
406     }
407
408     return(status);
409 }
410
411
412 /*******************************************************************************
413 * Function Name: CFG_EEPROM_ByteWrite
414 ********************************************************************************
415 *
416 * Summary:
417 *  Writes a byte of data to the EEPROM. This is a blocking call. It will not
418 *  return until the function succeeds or fails.
419 *
420 * Parameters:
421 *  dataByte:  Byte of data to write to the EEPROM.
422 *  rowNumber:  EEPROM row number to program.
423 *  byteNumber:  Byte number within the row to program.
424 *
425 * Return:
426 *  CYRET_SUCCESS, if the operation was successful.
427 *  CYRET_BAD_PARAM, if the parameter rowNumber or byteNumber out of range.
428 *  CYRET_LOCKED, if the spc is being used.
429 *  CYRET_UNKNOWN, if there was an SPC error.
430 *
431 *******************************************************************************/
432 cystatus CFG_EEPROM_ByteWrite(uint8 dataByte, uint8 rowNumber, uint8 byteNumber) \
433
434 {
435     cystatus status;
436
437     /* Start the SPC */
438     CySpcStart();
439
440     if((rowNumber < (uint8) CY_EEPROM_NUMBER_ROWS) && (byteNumber < (uint8) SIZEOF_EEPROM_ROW))
441     {
442         /* See if we can get the SPC. */
443         if(CySpcLock() == CYRET_SUCCESS)
444         {
445             /* Plan for failure */
446             status = CYRET_UNKNOWN;
447
448             /* Command to load a byte of data */
449             if(CySpcLoadMultiByte(CY_SPC_FIRST_EE_ARRAYID, (uint16)byteNumber, &dataByte,\
450                                                                 CFG_EEPROM_SPC_BYTE_WRITE_SIZE) == CYRET_STARTED)
451             {
452                 while(CY_SPC_BUSY)
453                 {
454                     /* Wait until SPC becomes idle */
455                 }
456
457                 /* SPC is idle now */
458                 if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS)
459                 {
460                     status = CYRET_SUCCESS;
461                 }
462
463                 /* Command to erase and program the row. */
464                 if(status == CYRET_SUCCESS)
465                 {
466                     if(CySpcWriteRow(CY_SPC_FIRST_EE_ARRAYID, (uint16)rowNumber, dieTemperature[0],
467                     dieTemperature[1]) == CYRET_STARTED)
468                     {
469                         /* Plan for failure */
470                         status = CYRET_UNKNOWN;
471
472                         while(CY_SPC_BUSY)
473                         {
474                             /* Wait until SPC becomes idle */
475                         }
476
477                         /* SPC is idle now */
478                         if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS)
479                         {
480                             status = CYRET_SUCCESS;
481                         }
482                     }
483                     else
484                     {
485                         status = CYRET_UNKNOWN;
486                     }
487                 }
488                 else
489                 {
490                     status = CYRET_UNKNOWN;
491                 }
492             }
493
494             /* Unlock the SPC so someone else can use it. */
495             CySpcUnlock();
496         }
497         else
498         {
499             status = CYRET_LOCKED;
500         }
501     }
502     else
503     {
504         status = CYRET_BAD_PARAM;
505     }
506
507     return(status);
508 }
509
510
511 /* [] END OF FILE */