0% found this document useful (0 votes)
31 views3 pages

Firmware Developer Assignment Solutions

Uploaded by

saivigneshpc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Firmware Developer Assignment Solutions

Uploaded by

saivigneshpc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Firmware Developer Assignment - Error

Identification and Answers


This document contains the errors identified in the assignment and their respective
answers.

1. C function to return respective error code:


The first problem requires writing a C function that takes in an 8-bit integer and returns the
respective error code. Considering that each bit in the integer represents an error, the
solution should isolate the bit that is set and return its corresponding error code.

Solution:

c.
#include <stdio.h>

uint8_t readError(uint8_t error_code) {


uint8_t error = 1;
uint8_t count = 1;
while (error_code > error) {
error <<= 1;
count++;
}
return count;
}

int main() {
uint8_t error_code = 0x08; // Example error code
printf("%d", readError(error_code));
return 0;
}
```

Explanation:
- The function `readError` uses a simple loop to determine which bit is set and returns the
position of the set bit.
- Example input: `0x08` (which represents the 4th bit being set).
- Output: 4 (indicating the error at position 4).
2. C function using structures with bit-fields:
The second problem asks for a function that takes a structure with bit-fields and returns the
error as an 8-bit integer. Multiple error codes can be set at once, so the solution should
construct the correct 8-bit integer based on the set bits.

Solution:

```c
#include <stdio.h>

typedef struct {
uint8_t e1: 1;
uint8_t e2: 1;
uint8_t e3: 1;
uint8_t e4: 1;
uint8_t e5: 1;
uint8_t e6: 1;
uint8_t e7: 1;
uint8_t e8: 1;
} errorList;

uint8_t set_error(errorList err) {


uint8_t error_code = 0;
error_code |= (err.e1 << 0);
error_code |= (err.e2 << 1);
error_code |= (err.e3 << 2);
error_code |= (err.e4 << 3);
error_code |= (err.e5 << 4);
error_code |= (err.e6 << 5);
error_code |= (err.e7 << 6);
error_code |= (err.e8 << 7);
return error_code;
}

int main() {
errorList err = {1, 0, 0, 1, 0, 0, 0, 0}; // e1 and e4 are set
printf("%d", set_error(err));
return 0;
}
```

Explanation:
- The `set_error` function combines the bit-fields from the structure into an 8-bit integer.
- Example input: `e1` and `e4` are set.
- Output: 9 (as the bits for `e1` and `e4` correspond to the binary value `00001001`, which
is 9 in decimal).

You might also like