Skip to content

Commit

Permalink
stm32/desig: fix/cleanup desig_get_unique_id and to string functionality
Browse files Browse the repository at this point in the history
This was inspired by an Arch Linux provided ARM GCC 5.3.0 bug:
It gave an
"internal compiler error: in expand_expr_addr_expr_1, at expr.c:7736"
with the array version of the desig_get_unique_id.

While I was at it, fixed:
- a potential alignment issue with casting uint8_t* buf to uint32_t*
- a funny static in the string definition that does nothing (given const)
  • Loading branch information
urjaman authored and karlp committed Apr 11, 2016
1 parent 28592a7 commit d3fff11
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/libopencm3/stm32/desig.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ uint16_t desig_get_flash_size(void);
* Note: ST specifies that bits 31..16 are _also_ reserved for future use
* @param result pointer to at least 3xuint32_ts (96 bits)
*/
void desig_get_unique_id(uint32_t result[]);
void desig_get_unique_id(uint32_t *result);

/**
* Read the full 96 bit unique identifier and return it as a
Expand Down
19 changes: 10 additions & 9 deletions lib/stm32/desig.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,26 @@ uint16_t desig_get_flash_size(void)
return DESIG_FLASH_SIZE;
}

void desig_get_unique_id(uint32_t result[])
void desig_get_unique_id(uint32_t *result)
{
result[0] = DESIG_UNIQUE_ID2;
result[1] = DESIG_UNIQUE_ID1;
result[2] = DESIG_UNIQUE_ID0;
*result++ = DESIG_UNIQUE_ID2;
*result++ = DESIG_UNIQUE_ID1;
*result = DESIG_UNIQUE_ID0;
}

void desig_get_unique_id_as_string(char *string,
unsigned int string_len)
{
int i, len;
uint8_t device_id[12];
static const char chars[] = "0123456789ABCDEF";
uint32_t dev_id_buf[3];
uint8_t *device_id = (uint8_t*)dev_id_buf;
const char chars[] = "0123456789ABCDEF";

desig_get_unique_id((uint32_t *)device_id);
desig_get_unique_id(dev_id_buf);

/* Each byte produces two characters */
len = (2 * sizeof(device_id) < string_len) ?
2 * sizeof(device_id) : string_len - 1;
len = (2 * sizeof(dev_id_buf) < string_len) ?
2 * sizeof(dev_id_buf) : string_len - 1;

for (i = 0; i < len; i += 2) {
string[i] = chars[(device_id[i / 2] >> 4) & 0x0F];
Expand Down

0 comments on commit d3fff11

Please sign in to comment.