|
| 1 | +// Copyright CERN and copyright holders of ALICE O2. This software is |
| 2 | +// distributed under the terms of the GNU General Public License v3 (GPL |
| 3 | +// Version 3), copied verbatim in the file "COPYING". |
| 4 | +// |
| 5 | +// See http://alice-o2.web.cern.ch/license for full licensing information. |
| 6 | +// |
| 7 | +// In applying this license CERN does not waive the privileges and immunities |
| 8 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 9 | +// or submit itself to any jurisdiction. |
| 10 | + |
| 11 | +/// \file Eeprom.cxx |
| 12 | +/// \brief Implementation of the Eeprom class |
| 13 | +/// |
| 14 | +/// \author Kostas Alexopoulos ([email protected]) |
| 15 | + |
| 16 | +#include <regex> |
| 17 | +#include "Constants.h" |
| 18 | +#include "I2c.h" |
| 19 | +#include "Eeprom.h" |
| 20 | + |
| 21 | +namespace AliceO2 |
| 22 | +{ |
| 23 | +namespace roc |
| 24 | +{ |
| 25 | + |
| 26 | +Eeprom::Eeprom(std::shared_ptr<Pda::PdaBar> pdaBar) : mPdaBar(pdaBar) |
| 27 | +{ |
| 28 | +} |
| 29 | + |
| 30 | +std::string Eeprom::readContent() |
| 31 | +{ |
| 32 | + uint32_t chipAddress = 0x50; // fixed address |
| 33 | + I2c i2c = I2c(Cru::Registers::BSP_I2C_EEPROM.address, chipAddress, mPdaBar); |
| 34 | + |
| 35 | + i2c.resetI2c(); |
| 36 | + |
| 37 | + std::string content; |
| 38 | + for (int i = 0; i < 1000/8; i++) { // EEPROM size is 1KB |
| 39 | + uint32_t res = i2c.readI2c(i); |
| 40 | + content += (char)res; |
| 41 | + if ((char)res == '}') { |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return content; |
| 47 | +} |
| 48 | + |
| 49 | +boost::optional<int32_t> Eeprom::getSerial() |
| 50 | +{ |
| 51 | + // parse the serial from the content |
| 52 | + const std::string content = readContent(); |
| 53 | + |
| 54 | + // Example content: |
| 55 | + // {"cn": "FEDD", "dt": "2019-06-17", "io": "24/24", "pn": "p40_fv22b10241", "serial_number_p40": "18-02409 - 0136"} |
| 56 | + // p40_fv22b -> production CRUs |
| 57 | + // p40_tv20pr -> testing CRUs |
| 58 | + |
| 59 | + std::string startDelimiter = "\"pn\": \"p40_(?:fv22b|tv20pr)"; |
| 60 | + std::string endDelimiter = "\", \"serial_number_p40\":"; |
| 61 | + |
| 62 | + std::regex expression(startDelimiter + "(.*)" + endDelimiter); |
| 63 | + std::smatch match; |
| 64 | + |
| 65 | + if (std::regex_search(content.begin(), content.end(), match, expression)) { |
| 66 | + return std::stol(match[1], NULL, 0); |
| 67 | + } |
| 68 | + |
| 69 | + return {}; |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace roc |
| 73 | +} // namespace AliceO2 |
0 commit comments