Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(PeriphDrivers): Fix -Wcast-align warning for MAX78000 & MAX78002 SYS Drivers #706

Merged
merged 4 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions Libraries/PeriphDrivers/Source/SYS/sys_ai85.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,31 @@ int MXC_SYS_GetUSN(uint8_t *usn, uint8_t *checksum)
/* Read the USN from the info block */
MXC_FLC_UnlockInfoBlock(MXC_INFO0_MEM_BASE);

memset(usn, 0, MXC_SYS_USN_CHECKSUM_LEN);

usn[0] = (infoblock[0] & 0x007F8000) >> 15;
usn[1] = (infoblock[0] & 0x7F800000) >> 23;
usn[2] = (infoblock[1] & 0x0000007F) << 1;
usn[2] |= (infoblock[0] & 0x80000000) >> 31;
usn[3] = (infoblock[1] & 0x00007F80) >> 7;
usn[4] = (infoblock[1] & 0x007F8000) >> 15;
usn[5] = (infoblock[1] & 0x7F800000) >> 23;
usn[6] = (infoblock[2] & 0x007F8000) >> 15;
usn[7] = (infoblock[2] & 0x7F800000) >> 23;
usn[8] = (infoblock[3] & 0x0000007F) << 1;
usn[8] |= (infoblock[2] & 0x80000000) >> 31;
usn[9] = (infoblock[3] & 0x00007F80) >> 7;
usn[10] = (infoblock[3] & 0x007F8000) >> 15;
uint32_t _usn_32[MXC_SYS_USN_CHECKSUM_LEN / 4];
// ^ Declare as uint32_t to preserve mem alignment
uint8_t *_usn_8 = (uint8_t *)_usn_32;
memset(_usn_8, 0, MXC_SYS_USN_CHECKSUM_LEN);

_usn_8[0] = (infoblock[0] & 0x007F8000) >> 15;
_usn_8[1] = (infoblock[0] & 0x7F800000) >> 23;
_usn_8[2] = (infoblock[1] & 0x0000007F) << 1;
_usn_8[2] |= (infoblock[0] & 0x80000000) >> 31;
_usn_8[3] = (infoblock[1] & 0x00007F80) >> 7;
_usn_8[4] = (infoblock[1] & 0x007F8000) >> 15;
_usn_8[5] = (infoblock[1] & 0x7F800000) >> 23;
_usn_8[6] = (infoblock[2] & 0x007F8000) >> 15;
_usn_8[7] = (infoblock[2] & 0x7F800000) >> 23;
_usn_8[8] = (infoblock[3] & 0x0000007F) << 1;
_usn_8[8] |= (infoblock[2] & 0x80000000) >> 31;
_usn_8[9] = (infoblock[3] & 0x00007F80) >> 7;
_usn_8[10] = (infoblock[3] & 0x007F8000) >> 15;

/* If requested, verify and return the checksum */
if (checksum != NULL) {
uint8_t check_csum[MXC_SYS_USN_CHECKSUM_LEN];
uint32_t _check_csum_32[MXC_SYS_USN_CHECKSUM_LEN / 4];
// ^ Declare as uint32_t to preserve mem alignment
memset(_check_csum_32, 0, (MXC_SYS_USN_CHECKSUM_LEN / 4) * sizeof(uint32_t));
uint8_t *check_csum = (uint8_t *)_check_csum_32;
uint8_t aes_key[MXC_SYS_USN_CHECKSUM_LEN] = { 0 }; // NULL Key (per checksum spec)

// Read Checksum from the infoblock
Expand All @@ -117,8 +123,8 @@ int MXC_SYS_GetUSN(uint8_t *usn, uint8_t *checksum)
// Compute Checksum
mxc_aes_req_t aes_req;
aes_req.length = MXC_SYS_USN_CHECKSUM_LEN / 4;
aes_req.inputData = (uint32_t *)usn;
aes_req.resultData = (uint32_t *)check_csum;
aes_req.inputData = _usn_32;
aes_req.resultData = _check_csum_32;
aes_req.keySize = MXC_AES_128BITS;
aes_req.encryption = MXC_AES_ENCRYPT_EXT_KEY;
aes_req.callback = NULL;
Expand All @@ -132,18 +138,22 @@ int MXC_SYS_GetUSN(uint8_t *usn, uint8_t *checksum)
MXC_AES_Shutdown();

// Verify Checksum
// The checksum results will be in the least significant bytes of the aes output.
if (check_csum[0] != checksum[1] || check_csum[1] != checksum[0]) {
MXC_FLC_LockInfoBlock(MXC_INFO0_MEM_BASE);
return E_INVALID;
}
}

/* Add the info block checksum to the USN */
usn[11] = ((infoblock[3] & 0x7F800000) >> 23);
usn[12] = ((infoblock[4] & 0x007F8000) >> 15);
_usn_8[11] = ((infoblock[3] & 0x7F800000) >> 23);
_usn_8[12] = ((infoblock[4] & 0x007F8000) >> 15);

MXC_FLC_LockInfoBlock(MXC_INFO0_MEM_BASE);

if (usn != NULL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check for a null pointer is redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memcpy(usn, _usn_8, MXC_SYS_USN_LEN);

return err;
}

Expand Down
49 changes: 29 additions & 20 deletions Libraries/PeriphDrivers/Source/SYS/sys_ai87.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,31 @@ int MXC_SYS_GetUSN(uint8_t *usn, uint8_t *checksum)
/* Read the USN from the info block */
MXC_FLC_UnlockInfoBlock(MXC_INFO0_MEM_BASE);

memset(usn, 0, MXC_SYS_USN_CHECKSUM_LEN);

usn[0] = (infoblock[0] & 0x007F8000) >> 15;
usn[1] = (infoblock[0] & 0x7F800000) >> 23;
usn[2] = (infoblock[1] & 0x0000007F) << 1;
usn[2] |= (infoblock[0] & 0x80000000) >> 31;
usn[3] = (infoblock[1] & 0x00007F80) >> 7;
usn[4] = (infoblock[1] & 0x007F8000) >> 15;
usn[5] = (infoblock[1] & 0x7F800000) >> 23;
usn[6] = (infoblock[2] & 0x007F8000) >> 15;
usn[7] = (infoblock[2] & 0x7F800000) >> 23;
usn[8] = (infoblock[3] & 0x0000007F) << 1;
usn[8] |= (infoblock[2] & 0x80000000) >> 31;
usn[9] = (infoblock[3] & 0x00007F80) >> 7;
usn[10] = (infoblock[3] & 0x007F8000) >> 15;
uint32_t _usn_32[MXC_SYS_USN_CHECKSUM_LEN / 4];
// ^ Declare as uint32_t to preserve mem alignment
uint8_t *_usn_8 = (uint8_t *)_usn_32;
memset(_usn_8, 0, MXC_SYS_USN_CHECKSUM_LEN);

_usn_8[0] = (infoblock[0] & 0x007F8000) >> 15;
_usn_8[1] = (infoblock[0] & 0x7F800000) >> 23;
_usn_8[2] = (infoblock[1] & 0x0000007F) << 1;
_usn_8[2] |= (infoblock[0] & 0x80000000) >> 31;
_usn_8[3] = (infoblock[1] & 0x00007F80) >> 7;
_usn_8[4] = (infoblock[1] & 0x007F8000) >> 15;
_usn_8[5] = (infoblock[1] & 0x7F800000) >> 23;
_usn_8[6] = (infoblock[2] & 0x007F8000) >> 15;
_usn_8[7] = (infoblock[2] & 0x7F800000) >> 23;
_usn_8[8] = (infoblock[3] & 0x0000007F) << 1;
_usn_8[8] |= (infoblock[2] & 0x80000000) >> 31;
_usn_8[9] = (infoblock[3] & 0x00007F80) >> 7;
_usn_8[10] = (infoblock[3] & 0x007F8000) >> 15;

/* If requested, verify and return the checksum */
if (checksum != NULL) {
uint8_t check_csum[MXC_SYS_USN_CHECKSUM_LEN];
uint32_t _check_csum_32[MXC_SYS_USN_CHECKSUM_LEN / 4];
// ^ Declare as uint32_t to preserve mem alignment
uint8_t *check_csum = (uint8_t *)_check_csum_32;
memset(check_csum, 0, MXC_SYS_USN_CHECKSUM_LEN);
uint8_t aes_key[MXC_SYS_USN_CHECKSUM_LEN] = { 0 }; // NULL Key (per checksum spec)

// Read Checksum from the infoblock
Expand All @@ -117,8 +123,8 @@ int MXC_SYS_GetUSN(uint8_t *usn, uint8_t *checksum)
// Compute Checksum
mxc_aes_req_t aes_req;
aes_req.length = MXC_SYS_USN_CHECKSUM_LEN / 4;
aes_req.inputData = (uint32_t *)usn;
aes_req.resultData = (uint32_t *)check_csum;
aes_req.inputData = _usn_32;
aes_req.resultData = _check_csum_32;
aes_req.keySize = MXC_AES_128BITS;
aes_req.encryption = MXC_AES_ENCRYPT_EXT_KEY;
aes_req.callback = NULL;
Expand All @@ -139,11 +145,14 @@ int MXC_SYS_GetUSN(uint8_t *usn, uint8_t *checksum)
}

/* Add the info block checksum to the USN */
usn[11] = ((infoblock[3] & 0x7F800000) >> 23);
usn[12] = ((infoblock[4] & 0x007F8000) >> 15);
_usn_8[11] = ((infoblock[3] & 0x7F800000) >> 23);
_usn_8[12] = ((infoblock[4] & 0x007F8000) >> 15);

MXC_FLC_LockInfoBlock(MXC_INFO0_MEM_BASE);

if (usn != NULL)
memcpy(usn, _usn_8, MXC_SYS_USN_LEN);

return err;
}

Expand Down
Loading