Reading byte array in C++ [closed]











0















I have following code in C++



#define NO_SECTORS  4
#define HEADER_SIZE 6
#define SECTOR_SIZE 64

union flash_rec_u{
struct record{
union header_u{
struct header{
uint32_t sector_number;
uint16_t no_written_bytes;
}header_items __attribute__((packed));
uint8_t header_bytes[HEADER_SIZE];
}header;
uint8_t data_bytes[SECTOR_SIZE - HEADER_SIZE];
}rec_items;
uint8_t rec_bytes[SECTOR_SIZE];
};

typedef uint8_t SECTOR[SECTOR_SIZE];
typedef SECTOR FLASH[NO_SECTORS];

FLASH flash;

SECTOR initialStateSector_00[SECTOR_SIZE] = {
0x01, 0x00, 0x00, 0x00, 0x25, 0x00,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00
};

void PrintSector(uint8_t sector){
for(uint8_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
std::cout << std::setw(2);
std::cout << showbase << dec;
std::cout << hex << (uint16_t)flash[sector][curByte] << ", ";
if(curByte % 16 == 15){
std::cout << "n";
}
}
std::cout << "n";
}

void PrintFlash(void){
for(uint8_t curSector = 0; curSector < NO_SECTORS; curSector++){
std::cout << "SECTOR: " << (uint16_t)curSector << "n";
PrintSector(curSector);
}
}

void EraseSector(uint16_t sector){
for(uint16_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
flash[sector][curByte] = ERASED_BYTE;
}
}

void EraseFlash(void){
for(uint16_t curSector = 0; curSector < NO_SECTORS; curSector++){
EraseSector(curSector);
}
}

void WriteBytes2Sector(uint8_t sector, uint8_t *srcBytes){
for(uint16_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
flash[sector][curByte] = *(srcBytes + curByte);
}
}

void ReadSector(uint8_t sector, uint8_t *readBytes, uint16_t noBytes2bRead){

for(uint16_t curSrcByte = 0; curSrcByte < noBytes2bRead; curSrcByte++){
*(readBytes + curSrcByte) = flash[sector][curSrcByte];
}
}

void StoreEventLog(uint8_t *eventBytes, uint16_t noEventBytes){

flash_rec_u buffer;


ReadSector(0, buffer.rec_bytes, 1);

for(uint8_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
std::cout << std::setw(2);
std::cout << showbase << dec;
std::cout << hex << (uint16_t)(buffer.rec_bytes[curByte]) << ", ";
if(curByte % 16 == 15){
std::cout << "n";
}
}
std::cout << "n";

}


int main(int argc, char** argv) {

uint8_t bytes2bStored = {
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB
};

EraseFlash();
PrintFlash();
WriteBytes2Sector(0, *initialStateSector_00);
PrintFlash();

StoreEventLog(bytes2bStored, sizeof(bytes2bStored));
PrintFlash();

return 0;
}


The flash content after erasing:



SECTOR: 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

SECTOR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0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

SECTOR: 0x2
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

SECTOR: 0x3
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,


Whenever I call the ReadSector function from the StoreEventLog function for reading
content of the sector 0 into temporary variable buffer and then print content of
the buffer variable I always get totally wrong content:



0x1, 0xcb, 0xff, 0xff,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0x67, 0x5b, 0x4c, 0xfe, 0x3, 0, 0, 0,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0, 0, 0, 0, 0, 0, 0,0,


It seems that the problem is in the ReadSector function, but I don't know where. Could anybody tell me what I do wrong? Thanks for any ideas.









share













migration rejected from electronics.stackexchange.com Jan 23 at 10:02


This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.





closed as off-topic by Ramhound, music2myear, DrMoishe Pippik, fixer1234, PeterH Jan 23 at 10:02


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question is not about computer hardware or software, within the scope defined in the help center." – Ramhound, music2myear, DrMoishe Pippik, fixer1234, PeterH

If this question can be reworded to fit the rules in the help center, please edit the question.

















  • Type punning to unions isn't well-defined behavior in C++, which is one of the reasons why that language is unsuitable for embedded/hardware-related programming. It is well-defined in C though, so you could try to convert this snippet to C and re-compile in a C compiler. This code also relies on constructors getting called by the "CRT" during start-up, you should verify that they are indeed called. In addition, you have the struct padding issue in C and C++ both, which will be a problem if this is a 32 bit CPU. You'll have to ensure there's no padding.

    – Lundin
    Jan 14 at 9:45











  • What does this have to do with Electronic Design? which is what this site is for.

    – Bimpelrekkie
    Jan 14 at 11:08











  • @Lundin Thank you very much for your reaction. I haven't taken into account padding at all. Do you have any experience how to solve the padding issue?

    – Steve
    Jan 14 at 11:47






  • 1





    @Bimpelrekkie The question is obviously about writing a flash driver for a MCU. Hardware-related programming of micrcontrollers is perfectly on-topic.

    – Lundin
    Jan 14 at 11:53






  • 1





    Why was a pure programming question migrated to Super User instead of Stack Overflow?

    – Ramhound
    Jan 21 at 19:22
















0















I have following code in C++



#define NO_SECTORS  4
#define HEADER_SIZE 6
#define SECTOR_SIZE 64

union flash_rec_u{
struct record{
union header_u{
struct header{
uint32_t sector_number;
uint16_t no_written_bytes;
}header_items __attribute__((packed));
uint8_t header_bytes[HEADER_SIZE];
}header;
uint8_t data_bytes[SECTOR_SIZE - HEADER_SIZE];
}rec_items;
uint8_t rec_bytes[SECTOR_SIZE];
};

typedef uint8_t SECTOR[SECTOR_SIZE];
typedef SECTOR FLASH[NO_SECTORS];

FLASH flash;

SECTOR initialStateSector_00[SECTOR_SIZE] = {
0x01, 0x00, 0x00, 0x00, 0x25, 0x00,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00
};

void PrintSector(uint8_t sector){
for(uint8_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
std::cout << std::setw(2);
std::cout << showbase << dec;
std::cout << hex << (uint16_t)flash[sector][curByte] << ", ";
if(curByte % 16 == 15){
std::cout << "n";
}
}
std::cout << "n";
}

void PrintFlash(void){
for(uint8_t curSector = 0; curSector < NO_SECTORS; curSector++){
std::cout << "SECTOR: " << (uint16_t)curSector << "n";
PrintSector(curSector);
}
}

void EraseSector(uint16_t sector){
for(uint16_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
flash[sector][curByte] = ERASED_BYTE;
}
}

void EraseFlash(void){
for(uint16_t curSector = 0; curSector < NO_SECTORS; curSector++){
EraseSector(curSector);
}
}

void WriteBytes2Sector(uint8_t sector, uint8_t *srcBytes){
for(uint16_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
flash[sector][curByte] = *(srcBytes + curByte);
}
}

void ReadSector(uint8_t sector, uint8_t *readBytes, uint16_t noBytes2bRead){

for(uint16_t curSrcByte = 0; curSrcByte < noBytes2bRead; curSrcByte++){
*(readBytes + curSrcByte) = flash[sector][curSrcByte];
}
}

void StoreEventLog(uint8_t *eventBytes, uint16_t noEventBytes){

flash_rec_u buffer;


ReadSector(0, buffer.rec_bytes, 1);

for(uint8_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
std::cout << std::setw(2);
std::cout << showbase << dec;
std::cout << hex << (uint16_t)(buffer.rec_bytes[curByte]) << ", ";
if(curByte % 16 == 15){
std::cout << "n";
}
}
std::cout << "n";

}


int main(int argc, char** argv) {

uint8_t bytes2bStored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xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB
};

EraseFlash();
PrintFlash();
WriteBytes2Sector(0, *initialStateSector_00);
PrintFlash();

StoreEventLog(bytes2bStored, sizeof(bytes2bStored));
PrintFlash();

return 0;
}


The flash content after erasing:



SECTOR: 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

SECTOR: 0x1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

SECTOR: 0x2
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

SECTOR: 0x3
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,


Whenever I call the ReadSector function from the StoreEventLog function for reading
content of the sector 0 into temporary variable buffer and then print content of
the buffer variable I always get totally wrong content:



0x1, 0xcb, 0xff, 0xff,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0x67, 0x5b, 0x4c, 0xfe, 0x3, 0, 0, 0,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0, 0, 0, 0, 0, 0, 0,0,


It seems that the problem is in the ReadSector function, but I don't know where. Could anybody tell me what I do wrong? Thanks for any ideas.









share













migration rejected from electronics.stackexchange.com Jan 23 at 10:02


This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.





closed as off-topic by Ramhound, music2myear, DrMoishe Pippik, fixer1234, PeterH Jan 23 at 10:02


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question is not about computer hardware or software, within the scope defined in the help center." – Ramhound, music2myear, DrMoishe Pippik, fixer1234, PeterH

If this question can be reworded to fit the rules in the help center, please edit the question.

















  • Type punning to unions isn't well-defined behavior in C++, which is one of the reasons why that language is unsuitable for embedded/hardware-related programming. It is well-defined in C though, so you could try to convert this snippet to C and re-compile in a C compiler. This code also relies on constructors getting called by the "CRT" during start-up, you should verify that they are indeed called. In addition, you have the struct padding issue in C and C++ both, which will be a problem if this is a 32 bit CPU. You'll have to ensure there's no padding.

    – Lundin
    Jan 14 at 9:45











  • What does this have to do with Electronic Design? which is what this site is for.

    – Bimpelrekkie
    Jan 14 at 11:08











  • @Lundin Thank you very much for your reaction. I haven't taken into account padding at all. Do you have any experience how to solve the padding issue?

    – Steve
    Jan 14 at 11:47






  • 1





    @Bimpelrekkie The question is obviously about writing a flash driver for a MCU. Hardware-related programming of micrcontrollers is perfectly on-topic.

    – Lundin
    Jan 14 at 11:53






  • 1





    Why was a pure programming question migrated to Super User instead of Stack Overflow?

    – Ramhound
    Jan 21 at 19:22














0












0








0








I have following code in C++



#define NO_SECTORS  4
#define HEADER_SIZE 6
#define SECTOR_SIZE 64

union flash_rec_u{
struct record{
union header_u{
struct header{
uint32_t sector_number;
uint16_t no_written_bytes;
}header_items __attribute__((packed));
uint8_t header_bytes[HEADER_SIZE];
}header;
uint8_t data_bytes[SECTOR_SIZE - HEADER_SIZE];
}rec_items;
uint8_t rec_bytes[SECTOR_SIZE];
};

typedef uint8_t SECTOR[SECTOR_SIZE];
typedef SECTOR FLASH[NO_SECTORS];

FLASH flash;

SECTOR initialStateSector_00[SECTOR_SIZE] = {
0x01, 0x00, 0x00, 0x00, 0x25, 0x00,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00
};

void PrintSector(uint8_t sector){
for(uint8_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
std::cout << std::setw(2);
std::cout << showbase << dec;
std::cout << hex << (uint16_t)flash[sector][curByte] << ", ";
if(curByte % 16 == 15){
std::cout << "n";
}
}
std::cout << "n";
}

void PrintFlash(void){
for(uint8_t curSector = 0; curSector < NO_SECTORS; curSector++){
std::cout << "SECTOR: " << (uint16_t)curSector << "n";
PrintSector(curSector);
}
}

void EraseSector(uint16_t sector){
for(uint16_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
flash[sector][curByte] = ERASED_BYTE;
}
}

void EraseFlash(void){
for(uint16_t curSector = 0; curSector < NO_SECTORS; curSector++){
EraseSector(curSector);
}
}

void WriteBytes2Sector(uint8_t sector, uint8_t *srcBytes){
for(uint16_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
flash[sector][curByte] = *(srcBytes + curByte);
}
}

void ReadSector(uint8_t sector, uint8_t *readBytes, uint16_t noBytes2bRead){

for(uint16_t curSrcByte = 0; curSrcByte < noBytes2bRead; curSrcByte++){
*(readBytes + curSrcByte) = flash[sector][curSrcByte];
}
}

void StoreEventLog(uint8_t *eventBytes, uint16_t noEventBytes){

flash_rec_u buffer;


ReadSector(0, buffer.rec_bytes, 1);

for(uint8_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
std::cout << std::setw(2);
std::cout << showbase << dec;
std::cout << hex << (uint16_t)(buffer.rec_bytes[curByte]) << ", ";
if(curByte % 16 == 15){
std::cout << "n";
}
}
std::cout << "n";

}


int main(int argc, char** argv) {

uint8_t bytes2bStored = {
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB
};

EraseFlash();
PrintFlash();
WriteBytes2Sector(0, *initialStateSector_00);
PrintFlash();

StoreEventLog(bytes2bStored, sizeof(bytes2bStored));
PrintFlash();

return 0;
}


The flash content after erasing:



SECTOR: 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0, 0, 0, 0,

SECTOR: 0x1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

SECTOR: 0x2
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

SECTOR: 0x3
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,


Whenever I call the ReadSector function from the StoreEventLog function for reading
content of the sector 0 into temporary variable buffer and then print content of
the buffer variable I always get totally wrong content:



0x1, 0xcb, 0xff, 0xff,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0x67, 0x5b, 0x4c, 0xfe, 0x3, 0, 0, 0,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0, 0, 0, 0, 0, 0, 0,0,


It seems that the problem is in the ReadSector function, but I don't know where. Could anybody tell me what I do wrong? Thanks for any ideas.









share














I have following code in C++



#define NO_SECTORS  4
#define HEADER_SIZE 6
#define SECTOR_SIZE 64

union flash_rec_u{
struct record{
union header_u{
struct header{
uint32_t sector_number;
uint16_t no_written_bytes;
}header_items __attribute__((packed));
uint8_t header_bytes[HEADER_SIZE];
}header;
uint8_t data_bytes[SECTOR_SIZE - HEADER_SIZE];
}rec_items;
uint8_t rec_bytes[SECTOR_SIZE];
};

typedef uint8_t SECTOR[SECTOR_SIZE];
typedef SECTOR FLASH[NO_SECTORS];

FLASH flash;

SECTOR initialStateSector_00[SECTOR_SIZE] = {
0x01, 0x00, 0x00, 0x00, 0x25, 0x00,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00
};

void PrintSector(uint8_t sector){
for(uint8_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
std::cout << std::setw(2);
std::cout << showbase << dec;
std::cout << hex << (uint16_t)flash[sector][curByte] << ", ";
if(curByte % 16 == 15){
std::cout << "n";
}
}
std::cout << "n";
}

void PrintFlash(void){
for(uint8_t curSector = 0; curSector < NO_SECTORS; curSector++){
std::cout << "SECTOR: " << (uint16_t)curSector << "n";
PrintSector(curSector);
}
}

void EraseSector(uint16_t sector){
for(uint16_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
flash[sector][curByte] = ERASED_BYTE;
}
}

void EraseFlash(void){
for(uint16_t curSector = 0; curSector < NO_SECTORS; curSector++){
EraseSector(curSector);
}
}

void WriteBytes2Sector(uint8_t sector, uint8_t *srcBytes){
for(uint16_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
flash[sector][curByte] = *(srcBytes + curByte);
}
}

void ReadSector(uint8_t sector, uint8_t *readBytes, uint16_t noBytes2bRead){

for(uint16_t curSrcByte = 0; curSrcByte < noBytes2bRead; curSrcByte++){
*(readBytes + curSrcByte) = flash[sector][curSrcByte];
}
}

void StoreEventLog(uint8_t *eventBytes, uint16_t noEventBytes){

flash_rec_u buffer;


ReadSector(0, buffer.rec_bytes, 1);

for(uint8_t curByte = 0; curByte < SECTOR_SIZE; curByte++){
std::cout << std::setw(2);
std::cout << showbase << dec;
std::cout << hex << (uint16_t)(buffer.rec_bytes[curByte]) << ", ";
if(curByte % 16 == 15){
std::cout << "n";
}
}
std::cout << "n";

}


int main(int argc, char** argv) {

uint8_t bytes2bStored = {
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB
};

EraseFlash();
PrintFlash();
WriteBytes2Sector(0, *initialStateSector_00);
PrintFlash();

StoreEventLog(bytes2bStored, sizeof(bytes2bStored));
PrintFlash();

return 0;
}


The flash content after erasing:



SECTOR: 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

SECTOR: 0x1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

SECTOR: 0x2
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

SECTOR: 0x3
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,


Whenever I call the ReadSector function from the StoreEventLog function for reading
content of the sector 0 into temporary variable buffer and then print content of
the buffer variable I always get totally wrong content:



0x1, 0xcb, 0xff, 0xff,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0x67, 0x5b, 0x4c, 0xfe, 0x3, 0, 0, 0,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0, 0, 0, 0, 0, 0, 0,0,


It seems that the problem is in the ReadSector function, but I don't know where. Could anybody tell me what I do wrong? Thanks for any ideas.







embedded c++





share












share










share



share










asked Jan 14 at 8:39







Steve











migration rejected from electronics.stackexchange.com Jan 23 at 10:02


This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.





closed as off-topic by Ramhound, music2myear, DrMoishe Pippik, fixer1234, PeterH Jan 23 at 10:02


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question is not about computer hardware or software, within the scope defined in the help center." – Ramhound, music2myear, DrMoishe Pippik, fixer1234, PeterH

If this question can be reworded to fit the rules in the help center, please edit the question.







migration rejected from electronics.stackexchange.com Jan 23 at 10:02


This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.





closed as off-topic by Ramhound, music2myear, DrMoishe Pippik, fixer1234, PeterH Jan 23 at 10:02


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question is not about computer hardware or software, within the scope defined in the help center." – Ramhound, music2myear, DrMoishe Pippik, fixer1234, PeterH

If this question can be reworded to fit the rules in the help center, please edit the question.













  • Type punning to unions isn't well-defined behavior in C++, which is one of the reasons why that language is unsuitable for embedded/hardware-related programming. It is well-defined in C though, so you could try to convert this snippet to C and re-compile in a C compiler. This code also relies on constructors getting called by the "CRT" during start-up, you should verify that they are indeed called. In addition, you have the struct padding issue in C and C++ both, which will be a problem if this is a 32 bit CPU. You'll have to ensure there's no padding.

    – Lundin
    Jan 14 at 9:45











  • What does this have to do with Electronic Design? which is what this site is for.

    – Bimpelrekkie
    Jan 14 at 11:08











  • @Lundin Thank you very much for your reaction. I haven't taken into account padding at all. Do you have any experience how to solve the padding issue?

    – Steve
    Jan 14 at 11:47






  • 1





    @Bimpelrekkie The question is obviously about writing a flash driver for a MCU. Hardware-related programming of micrcontrollers is perfectly on-topic.

    – Lundin
    Jan 14 at 11:53






  • 1





    Why was a pure programming question migrated to Super User instead of Stack Overflow?

    – Ramhound
    Jan 21 at 19:22



















  • Type punning to unions isn't well-defined behavior in C++, which is one of the reasons why that language is unsuitable for embedded/hardware-related programming. It is well-defined in C though, so you could try to convert this snippet to C and re-compile in a C compiler. This code also relies on constructors getting called by the "CRT" during start-up, you should verify that they are indeed called. In addition, you have the struct padding issue in C and C++ both, which will be a problem if this is a 32 bit CPU. You'll have to ensure there's no padding.

    – Lundin
    Jan 14 at 9:45











  • What does this have to do with Electronic Design? which is what this site is for.

    – Bimpelrekkie
    Jan 14 at 11:08











  • @Lundin Thank you very much for your reaction. I haven't taken into account padding at all. Do you have any experience how to solve the padding issue?

    – Steve
    Jan 14 at 11:47






  • 1





    @Bimpelrekkie The question is obviously about writing a flash driver for a MCU. Hardware-related programming of micrcontrollers is perfectly on-topic.

    – Lundin
    Jan 14 at 11:53






  • 1





    Why was a pure programming question migrated to Super User instead of Stack Overflow?

    – Ramhound
    Jan 21 at 19:22

















Type punning to unions isn't well-defined behavior in C++, which is one of the reasons why that language is unsuitable for embedded/hardware-related programming. It is well-defined in C though, so you could try to convert this snippet to C and re-compile in a C compiler. This code also relies on constructors getting called by the "CRT" during start-up, you should verify that they are indeed called. In addition, you have the struct padding issue in C and C++ both, which will be a problem if this is a 32 bit CPU. You'll have to ensure there's no padding.

– Lundin
Jan 14 at 9:45





Type punning to unions isn't well-defined behavior in C++, which is one of the reasons why that language is unsuitable for embedded/hardware-related programming. It is well-defined in C though, so you could try to convert this snippet to C and re-compile in a C compiler. This code also relies on constructors getting called by the "CRT" during start-up, you should verify that they are indeed called. In addition, you have the struct padding issue in C and C++ both, which will be a problem if this is a 32 bit CPU. You'll have to ensure there's no padding.

– Lundin
Jan 14 at 9:45













What does this have to do with Electronic Design? which is what this site is for.

– Bimpelrekkie
Jan 14 at 11:08





What does this have to do with Electronic Design? which is what this site is for.

– Bimpelrekkie
Jan 14 at 11:08













@Lundin Thank you very much for your reaction. I haven't taken into account padding at all. Do you have any experience how to solve the padding issue?

– Steve
Jan 14 at 11:47





@Lundin Thank you very much for your reaction. I haven't taken into account padding at all. Do you have any experience how to solve the padding issue?

– Steve
Jan 14 at 11:47




1




1





@Bimpelrekkie The question is obviously about writing a flash driver for a MCU. Hardware-related programming of micrcontrollers is perfectly on-topic.

– Lundin
Jan 14 at 11:53





@Bimpelrekkie The question is obviously about writing a flash driver for a MCU. Hardware-related programming of micrcontrollers is perfectly on-topic.

– Lundin
Jan 14 at 11:53




1




1





Why was a pure programming question migrated to Super User instead of Stack Overflow?

– Ramhound
Jan 21 at 19:22





Why was a pure programming question migrated to Super User instead of Stack Overflow?

– Ramhound
Jan 21 at 19:22










0






active

oldest

votes
















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

 ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕