2c631a4ada508e63c29b24c85f487ddd7cfcc0e1
1 /*
2 *
3 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35 /**********************************************************************************
36 * FILE PURPOSE: General purpose CCS utility functions
37 **********************************************************************************
38 * FILE NAME: ccsutil.c
39 *
40 * DESCRIPTION: Common functions which make use of ccs files
41 *
42 **********************************************************************************/
43 #include <stdio.h>
44 #include <malloc.h>
45 #include "ccsutil.h"
48 /**********************************************************************************
49 * FUNCTION PURPOSE: Read a CCS data file
50 **********************************************************************************
51 * DESCRIPTION: An array is allocated and the CCS data file is read
52 **********************************************************************************/
53 unsigned int *readCcsFile (FILE *str, int *nwords)
54 {
55 int a, b, c, d, e, i;
56 unsigned int *cf;
57 char iline[132];
59 fgets (iline, 132, str);
60 sscanf (iline, "%x %x %x %x %x", &a, &b, &c, &d, &e);
62 cf = malloc (e * sizeof (unsigned int));
63 if (cf == NULL) {
64 *nwords = -1;
65 return (NULL);
66 }
68 for (i = 0; i < e; i++) {
69 fgets (iline, 132, str);
70 sscanf (&iline[2], "%x", &cf[i]);
71 }
73 *nwords = e;
74 return (cf);
76 }
79 /****************************************************************************************
80 * FUNCTION PURPOSE: Write a CCS data file
81 ****************************************************************************************
82 * DESCRIPTION: Data in the the array is written out
83 ****************************************************************************************/
84 int writeCcsFile (FILE *str, unsigned int *data, int nwords)
85 {
86 int i;
88 fprintf (str, "1651 1 10000 1 %x\n", nwords);
90 for (i = 0; i < nwords; i++)
91 fprintf (str, "0x%08x\n", data[i]);
93 return (0);
95 }
98 /****************************************************************************************
99 * FUNCTION PURPOSE: Write a CCS data file in upper case
100 ****************************************************************************************
101 * DESCRIPTION: Data in the the array is written out in upper case
102 ****************************************************************************************/
103 int writeCcsFileUp (FILE *str, unsigned int *data, int nwords)
104 {
105 int i;
107 fprintf (str, "1651 1 10000 1 %X\n", nwords);
109 for (i = 0; i < nwords; i++)
110 fprintf (str, "0x%08X\n", data[i]);
112 return (0);
114 }
117 /******************************************************************************************
118 * FUNCTION PURPOSE: Endian swap a data array
119 ******************************************************************************************
120 * DESCRIPTION: All the values in the array are endian swapped
121 ******************************************************************************************/
122 void ccsEndianSwap (unsigned int *data, int nwords)
123 {
124 int i;
125 unsigned int v;
127 for (i = 0; i < nwords; i++) {
129 v = (((data[i] >> 24) & 0xff) << 0) |
130 (((data[i] >> 16) & 0xff) << 8) |
131 (((data[i] >> 8) & 0xff) << 16) |
132 (((data[i] >> 0) & 0xff) << 24) ;
134 data[i] = v;
135 }
137 }
140 /******************************************************************************************
141 * FUNCTION PURPOSE: Open a file, read the CCS data, close the file
142 ******************************************************************************************
143 * DESCRIPTION: Reads a CCS data file
144 ******************************************************************************************/
145 unsigned int *openReadCloseCcsFile (char *fname, int *size)
146 {
147 unsigned int *dat;
149 FILE *str;
151 str = fopen (fname, "r");
153 if (str == NULL) {
154 *size = -1;
155 return (NULL);
156 }
159 dat = readCcsFile (str, size);
161 fclose (str);
163 return (dat);
165 }
168 /******************************************************************************************
169 * FUNCTION PURPOSE: Open a file, write CCS data, close the file
170 ******************************************************************************************
171 * DESCRIPTION: Writes a CCS data file
172 ******************************************************************************************/
173 int openWriteCloseCcsFile (char *fname, unsigned int *data, int nwords)
174 {
175 FILE *str;
177 str = fopen (fname, "w");
179 if (str == NULL)
180 return (-1);
183 writeCcsFile (str, data, nwords);
185 fclose (str);
187 return (0);
189 }