|
33 | 33 | #include "ExceptionInternal.h" |
34 | 34 | #include "ReadoutCard/RegisterReadWriteInterface.h" |
35 | 35 |
|
| 36 | +#include <boost/optional/optional_io.hpp> |
| 37 | + |
36 | 38 | using namespace std::chrono_literals; |
37 | 39 | using std::this_thread::sleep_for; |
38 | 40 | namespace b = boost; |
@@ -80,6 +82,8 @@ constexpr int MAX_WAIT = 1000000; |
80 | 82 | constexpr int REGISTER_DATA_STATUS = Rorc::Flash::IFDSR; |
81 | 83 | constexpr int REGISTER_ADDRESS = Rorc::Flash::IADR; |
82 | 84 | constexpr int REGISTER_READY = Rorc::Flash::LRD; |
| 85 | +constexpr int DDL_MAX_HW_ID = 64; |
| 86 | +constexpr int SN_POS = 33; |
83 | 87 |
|
84 | 88 | // TODO figure out what these are/do |
85 | 89 | constexpr int MAGIC_VALUE_0 = 0x80; |
@@ -168,13 +172,13 @@ void eraseBlock(RegisterReadWriteInterface& bar0, uint32_t address) |
168 | 172 | } |
169 | 173 |
|
170 | 174 | /// Currently unused, but we'll keep it as "documentation" |
171 | | -/*void writeWord(RegisterReadWriteInterface& bar0, uint32_t address, int value) |
| 175 | +void writeWord(RegisterReadWriteInterface& bar0, uint32_t address, int value) |
172 | 176 | { |
173 | 177 | writeStatusSleep(bar0, address); |
174 | 178 | writeStatusSleep(bar0, MAGIC_VALUE_9); |
175 | 179 | writeStatusSleep(bar0, value); |
176 | 180 | checkStatus(bar0); |
177 | | -}*/ |
| 181 | +} |
178 | 182 |
|
179 | 183 | /// Reads a 16-bit flash word and writes it into the given buffer |
180 | 184 | void readWord(RegisterReadWriteInterface& bar0, uint32_t address, char* data) |
@@ -1030,6 +1034,39 @@ boost::optional<int32_t> getSerial(RegisterReadWriteInterface& bar0) |
1030 | 1034 | return { int32_t(serial) }; |
1031 | 1035 | } |
1032 | 1036 |
|
| 1037 | +void setSerial(RegisterReadWriteInterface& bar0, int serial) |
| 1038 | +{ |
| 1039 | + |
| 1040 | + uint32_t address = Rorc::Serial::FLASH_ADDRESS; |
| 1041 | + Flash::init(bar0, address); |
| 1042 | + Flash::unlockBlock(bar0, address); |
| 1043 | + Flash::eraseBlock(bar0, address); |
| 1044 | + |
| 1045 | + // Prepare the data string |
| 1046 | + // It needs to be DDL_MAX_HW_ID long and |
| 1047 | + // initialized with ' ' |
| 1048 | + char serialCString[Flash::DDL_MAX_HW_ID + 1]; |
| 1049 | + serialCString[0] = '\0'; |
| 1050 | + |
| 1051 | + for (int i = 0; i < Flash::DDL_MAX_HW_ID - 1; i++) { |
| 1052 | + serialCString[i] = ' '; |
| 1053 | + } |
| 1054 | + |
| 1055 | + // "S/N: vwxyz" needs to start at SN_POS |
| 1056 | + // followed by a ' ' |
| 1057 | + sprintf(&serialCString[Flash::SN_POS - 5], "S/N: %05d", serial); |
| 1058 | + serialCString[Flash::SN_POS + 5] = ' '; |
| 1059 | + serialCString[Flash::DDL_MAX_HW_ID - 1] = '\0'; |
| 1060 | + |
| 1061 | + // Write the data to the flash |
| 1062 | + uint32_t hexValue = 0x0; |
| 1063 | + for (int i = 0; i < Flash::DDL_MAX_HW_ID; i += 2, address++) { |
| 1064 | + hexValue = Flash::MAGIC_VALUE_13 + |
| 1065 | + (serialCString[i] << 8) + serialCString[i + 1]; |
| 1066 | + Flash::writeWord(bar0, address, hexValue); |
| 1067 | + } |
| 1068 | +} |
| 1069 | + |
1033 | 1070 | uint8_t Crorc::ddlReadHw(int destination, int address, long long int time) |
1034 | 1071 | { |
1035 | 1072 | // prepare and send DDL command |
|
0 commit comments