1 /*
2   stm32flash - Open Source ST STM32 flash program for *nix
3   Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
4 
5   This program is free software; you can redistribute it and/or
6   modify it under the terms of the GNU General Public License
7   as published by the Free Software Foundation; either version 2
8   of the License, or (at your option) any later version.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 */
19 
20 #include <stdint.h>
21 #include "utils.h"
22 
23 /* detect CPU endian */
cpu_le()24 char cpu_le() {
25 	const uint32_t cpu_le_test = 0x12345678;
26 	return ((const unsigned char*)&cpu_le_test)[0] == 0x78;
27 }
28 
be_u32(const uint32_t v)29 uint32_t be_u32(const uint32_t v) {
30 	if (cpu_le())
31 		return	((v & 0xFF000000) >> 24) |
32 			((v & 0x00FF0000) >>  8) |
33 			((v & 0x0000FF00) <<  8) |
34 			((v & 0x000000FF) << 24);
35 	return v;
36 }
37 
le_u32(const uint32_t v)38 uint32_t le_u32(const uint32_t v) {
39         if (!cpu_le())
40                 return  ((v & 0xFF000000) >> 24) |
41                         ((v & 0x00FF0000) >>  8) |
42                         ((v & 0x0000FF00) <<  8) |
43                         ((v & 0x000000FF) << 24);
44         return v;
45 }
46 
printStatus(FILE * fd,int condition)47 void printStatus(FILE *fd, int condition){
48 	if(condition)
49 		fprintf(fd, "Error!\n");
50 	else
51 		fprintf(fd, "OK\n");
52 }
53