DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
WIB_Flash.cpp File Reference
#include "wibmod/WIB1/WIB.hh"
#include "wibmod/WIB1/WIBException.hh"
#include "wibmod/WIB1/BNL_UDP_Exception.hh"
#include <fstream>
#include <assert.h>
#include <algorithm>
Include dependency graph for WIB_Flash.cpp:

Go to the source code of this file.

Macros

#define FLASH_TIMEOUT   400
 
#define MIN_LINE_LENGTH   10
 
#define MAX_LINE_LENGTH   266
 

Functions

static void ParseIHexLine (std::string line, std::vector< uint8_t > &data, uint32_t &upperAddr)
 
void LoadIHexFile (std::vector< uint8_t > &data, const char *file)
 
static std::vector< uint32_t > firmwareFromIntelHexFile (std::string const &iHexFileName)
 

Macro Definition Documentation

◆ FLASH_TIMEOUT

#define FLASH_TIMEOUT   400

Definition at line 7 of file WIB_Flash.cpp.

◆ MAX_LINE_LENGTH

#define MAX_LINE_LENGTH   266

Definition at line 10 of file WIB_Flash.cpp.

◆ MIN_LINE_LENGTH

#define MIN_LINE_LENGTH   10

Definition at line 9 of file WIB_Flash.cpp.

Function Documentation

◆ firmwareFromIntelHexFile()

static std::vector< uint32_t > firmwareFromIntelHexFile ( std::string const & iHexFileName)
static

Definition at line 93 of file WIB_Flash.cpp.

93 {
94 //Ihex parser loads intel hex into a vector of bytes.
95 std::vector<uint8_t> rawData;
96 LoadIHexFile(rawData,iHexFileName.c_str());
97
98 //We now need to convert it to a uint32_t
99 //This has the issue of what to do if we are off by 32bit boundary.
100 //For now we'll just die so we don't have to deal with the issue.
101 //Make sure this is called before we erase the flash :-p
102 if((rawData.size()&0x3) != 0x0){
103 BUException::WIB_FLASH_IHEX_ERROR e;
104 e.Append("Flash data is not a multiple of 32bit words");
105 throw e;
106 }else if(0 == rawData.size()){
107 BUException::WIB_FLASH_IHEX_ERROR e;
108 e.Append("Flash data is empty");
109 throw e;
110 }
111
112 //Create a std::vector<uint32_t> and fill it with rawData;
113 std::vector<uint32_t> ret;
114 for(size_t iRawData = 0; iRawData < rawData.size();iRawData+=4){
115 //Build the 32bit word
116 uint32_t tempData = (rawData[iRawData + 0] << 0 |
117 rawData[iRawData + 1] << 8 |
118 rawData[iRawData + 2] << 16 |
119 rawData[iRawData + 3] << 24 );
120 ret.push_back(tempData);
121 }
122 return ret;
123}
void LoadIHexFile(std::vector< uint8_t > &data, const char *file)
Definition WIB_Flash.cpp:77

◆ LoadIHexFile()

void LoadIHexFile ( std::vector< uint8_t > & data,
const char * file )

Definition at line 77 of file WIB_Flash.cpp.

77 {
78 std::string line;
79 std::ifstream datafile(file);
80 uint32_t upperAddr = 0;
81 if(datafile.is_open()){
82 while(getline(datafile, line)){
83 ParseIHexLine(line, data, upperAddr);
84 }
85 } else{
86 BUException::WIB_FLASH_IHEX_ERROR e;
87 e.Append("Unable to open file ");
88 e.Append(file);
89 throw e;
90 }
91 }
static void ParseIHexLine(std::string line, std::vector< uint8_t > &data, uint32_t &upperAddr)
Definition WIB_Flash.cpp:12

◆ ParseIHexLine()

static void ParseIHexLine ( std::string line,
std::vector< uint8_t > & data,
uint32_t & upperAddr )
static

Definition at line 12 of file WIB_Flash.cpp.

12 {
13 if(line.empty()){
14 BUException::WIB_FLASH_IHEX_ERROR e;
15 e.Append("line is empty");
16 throw e;
17 }else if(line[0] != ':'){
18 BUException::WIB_FLASH_IHEX_ERROR e;
19 e.Append("BAD Line in file - does not start with ':'");
20 throw e;
21 }else{
22 line.erase(0,1);
23 }
24 if(line.size()<MIN_LINE_LENGTH){
25 BUException::WIB_FLASH_IHEX_ERROR e;
26 e.Append("BAD Line in file - incomplete intel hex format\n");
27 throw e;
28 }else if(line.size()>MAX_LINE_LENGTH){
29 BUException::WIB_FLASH_IHEX_ERROR e;
30 e.Append("BAD Line in file - intel hex format not supported, contains too many characters\n");
31 throw e;
32 }else if (line.size()&1){
33 BUException::WIB_FLASH_IHEX_ERROR e;
34 e.Append("BAD Line in file - uneven number of characters\n");
35 throw e;
36 }else {
37 char const *entry = line.c_str();
38 uint32_t line_sum = 0, byte_count = 0, address = 0, record_type = 0;
39
40 uint32_t byte = 0;
41 for(uint32_t iSum = 0; iSum < line.size(); iSum+=2){
42 sscanf(entry+iSum, "%2x", &byte);
43 line_sum += byte;
44 }
45 //the content of the line is cancelled out through addition to its
46 //two's complement which is the checksum if all data is intact
47
48 if(0xFF&line_sum){
49 line_sum = 0xFF & line_sum;
50 BUException::WIB_FLASH_IHEX_ERROR e;
51 e.Append("BAD Line in file - content inconsistent with checksum.\n");
52 throw e;
53 } else{
54 line_sum = 0;
55 }
56
57 sscanf(entry,"%2x %4x %2x ", &byte_count, &address, &record_type);
58 if(4 == record_type){
59 upperAddr = 0;
60 sscanf(entry+8,"%4x", &upperAddr);
61 upperAddr = upperAddr << 16;
62 } else if(0 == record_type){
63 address = upperAddr+address;
64 if(data.size() < (address+byte_count)){
65 data.resize(address+byte_count,0xFF);
66 }
67 for(uint32_t iAddr = 0; iAddr < byte_count; iAddr++){
68 uint32_t tempData = 0;
69 sscanf(entry+(iAddr*2)+8,"%02x",&tempData);
70 data[address+iAddr] = (uint8_t)tempData;
71 }
72
73 }
74 }
75}
#define MIN_LINE_LENGTH
Definition WIB_Flash.cpp:9
#define MAX_LINE_LENGTH
Definition WIB_Flash.cpp:10