DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
WIB_Flash.cpp File Reference
#include "wibmod/WIB1/BNL_UDP_Exception.hh"
#include "wibmod/WIB1/WIB.hh"
#include "wibmod/WIB1/WIBException.hh"
#include <algorithm>
#include <assert.h>
#include <fstream>
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()

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

Definition at line 97 of file WIB_Flash.cpp.

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

◆ LoadIHexFile()

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

Definition at line 79 of file WIB_Flash.cpp.

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

◆ ParseIHexLine()

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

Definition at line 13 of file WIB_Flash.cpp.

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