From 97688b913e44f2db3081adb5279c7fcb01db7181 Mon Sep 17 00:00:00 2001 From: Matthew Lai Date: Wed, 25 Sep 2019 12:47:55 +0100 Subject: [PATCH] stm32: desig: refactor to allow targets to have different addresses In this commit, support for the different base addresses for different F7 parts is added, but the mechanism is now in place for L1 and others. Reviewed-by: Karl Palsson (whitespace fixed, commit msg reworded) --- include/libopencm3/stm32/desig.h | 3 - include/libopencm3/stm32/f7/memorymap.h | 13 ++-- .../{desig.c => common/desig_common_all.c} | 46 +++++------- lib/stm32/common/desig_common_v1.c | 32 +++++++++ lib/stm32/f0/Makefile | 2 +- lib/stm32/f1/Makefile | 2 +- lib/stm32/f1/flash.c | 20 +++--- lib/stm32/f2/Makefile | 2 +- lib/stm32/f3/Makefile | 2 +- lib/stm32/f4/Makefile | 2 +- lib/stm32/f7/Makefile | 2 +- lib/stm32/f7/desig.c | 72 +++++++++++++++++++ lib/stm32/l0/Makefile | 2 +- lib/stm32/l1/Makefile | 2 +- 14 files changed, 148 insertions(+), 54 deletions(-) rename lib/stm32/{desig.c => common/desig_common_all.c} (62%) create mode 100644 lib/stm32/common/desig_common_v1.c create mode 100644 lib/stm32/f7/desig.c diff --git a/include/libopencm3/stm32/desig.h b/include/libopencm3/stm32/desig.h index 55e0045c..c24d64cd 100644 --- a/include/libopencm3/stm32/desig.h +++ b/include/libopencm3/stm32/desig.h @@ -25,9 +25,6 @@ /* --- Device Electronic Signature -------------------------------- */ -/* Flash size register */ -#define DESIG_FLASH_SIZE MMIO16(DESIG_FLASH_SIZE_BASE + 0x00) - BEGIN_DECLS /** diff --git a/include/libopencm3/stm32/f7/memorymap.h b/include/libopencm3/stm32/f7/memorymap.h index 0ce998c0..5cb42128 100644 --- a/include/libopencm3/stm32/f7/memorymap.h +++ b/include/libopencm3/stm32/f7/memorymap.h @@ -154,11 +154,14 @@ #define DBGMCU_BASE (PPBI_BASE + 0x00042000) /* Device Electronic Signature */ -#define DESIG_FLASH_SIZE_BASE (0x1FF0F422U) -#define DESIG_UNIQUE_ID_BASE (0x1FF0F420U) -#define DESIG_UNIQUE_ID0 MMIO32(DESIG_UNIQUE_ID_BASE) -#define DESIG_UNIQUE_ID1 MMIO32(DESIG_UNIQUE_ID_BASE + 4) -#define DESIG_UNIQUE_ID2 MMIO32(DESIG_UNIQUE_ID_BASE + 8) +/* On F7 the base address are different depending on the device ID in DBBGMCU. */ +#define DESIG_FLASH_SIZE_BASE_449 (0x1FF0F422U) +#define DESIG_FLASH_SIZE_BASE_451 (0x1FF0F422U) +#define DESIG_FLASH_SIZE_BASE_452 (0x1FF07A22U) + +#define DESIG_UNIQUE_ID_BASE_449 (0x1FF0F420U) +#define DESIG_UNIQUE_ID_BASE_451 (0x1FF0F420U) +#define DESIG_UNIQUE_ID_BASE_452 (0x1FF07A10U) /* ST provided factory calibration values @ 3.3V */ #define ST_VREFINT_CAL MMIO16(0x1FF07A4A) diff --git a/lib/stm32/desig.c b/lib/stm32/common/desig_common_all.c similarity index 62% rename from lib/stm32/desig.c rename to lib/stm32/common/desig_common_all.c index 80665c6b..35293963 100644 --- a/lib/stm32/desig.c +++ b/lib/stm32/common/desig_common_all.c @@ -19,50 +19,40 @@ #include -uint16_t desig_get_flash_size(void) -{ - return DESIG_FLASH_SIZE; -} - -void desig_get_unique_id(uint32_t *result) -{ - *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) +void desig_get_unique_id_as_string(char *string, unsigned int string_len) { int i, len; - uint32_t dev_id_buf[3]; - uint8_t *device_id = (uint8_t *)dev_id_buf; + uint32_t uid_buf[3]; + uint8_t *uid = (uint8_t *)uid_buf; const char chars[] = "0123456789ABCDEF"; - desig_get_unique_id(dev_id_buf); + desig_get_unique_id(uid_buf); /* Each byte produces two characters */ - len = (2 * sizeof(dev_id_buf) < string_len) ? - 2 * sizeof(dev_id_buf) : string_len - 1; + len = (2 * sizeof(uid_buf) < string_len) ? + 2 * sizeof(uid_buf) : string_len - 1; for (i = 0; i < len; i += 2) { - string[i] = chars[(device_id[i / 2] >> 4) & 0x0F]; - string[i + 1] = chars[(device_id[i / 2] >> 0) & 0x0F]; + string[i] = chars[(uid[i / 2] >> 4) & 0x0F]; + string[i + 1] = chars[(uid[i / 2] >> 0) & 0x0F]; } string[len] = '\0'; } void desig_get_unique_id_as_dfu(char *string) { - uint8_t *id = (uint8_t *)DESIG_UNIQUE_ID_BASE; + uint32_t uid_buf[3]; + uint8_t *uid = (uint8_t *)uid_buf; + + desig_get_unique_id(uid_buf); uint8_t serial[6]; - serial[0] = id[11]; - serial[1] = id[10] + id[2]; - serial[2] = id[9]; - serial[3] = id[8] + id[0]; - serial[4] = id[7]; - serial[5] = id[6]; + serial[0] = uid[11]; + serial[1] = uid[10] + uid[2]; + serial[2] = uid[9]; + serial[3] = uid[8] + uid[0]; + serial[4] = uid[7]; + serial[5] = uid[6]; uint8_t *ser = &serial[0]; uint8_t *end = &serial[6]; diff --git a/lib/stm32/common/desig_common_v1.c b/lib/stm32/common/desig_common_v1.c new file mode 100644 index 00000000..c7b9881e --- /dev/null +++ b/lib/stm32/common/desig_common_v1.c @@ -0,0 +1,32 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2012 Karl Palsson + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include + +uint16_t desig_get_flash_size(void) +{ + return *((uint32_t*)DESIG_FLASH_SIZE_BASE); +} + +void desig_get_unique_id(uint32_t *result) +{ + *result++ = DESIG_UNIQUE_ID2; + *result++ = DESIG_UNIQUE_ID1; + *result = DESIG_UNIQUE_ID0; +} diff --git a/lib/stm32/f0/Makefile b/lib/stm32/f0/Makefile index da4e0326..d7af8e69 100644 --- a/lib/stm32/f0/Makefile +++ b/lib/stm32/f0/Makefile @@ -40,7 +40,7 @@ OBJS += comparator.o OBJS += crc_common_all.o crc_v2.o OBJS += crs_common_all.o OBJS += dac_common_all.o -OBJS += desig.o +OBJS += desig_common_all.o desig_common_v1.o OBJS += dma_common_l1f013.o dma_common_csel.o OBJS += exti_common_all.o OBJS += flash.o flash_common_all.o flash_common_f.o flash_common_f01.o diff --git a/lib/stm32/f1/Makefile b/lib/stm32/f1/Makefile index aa3d28d0..eefb28a7 100755 --- a/lib/stm32/f1/Makefile +++ b/lib/stm32/f1/Makefile @@ -38,7 +38,7 @@ OBJS += adc.o adc_common_v1.o OBJS += can.o OBJS += crc_common_all.o OBJS += dac_common_all.o -OBJS += desig.o +OBJS += desig_common_all.o desig_common_v1.o OBJS += dma_common_l1f013.o OBJS += exti_common_all.o OBJS += flash.o flash_common_all.o flash_common_f.o flash_common_f01.o diff --git a/lib/stm32/f1/flash.c b/lib/stm32/f1/flash.c index c990b1e6..feae6e0e 100644 --- a/lib/stm32/f1/flash.c +++ b/lib/stm32/f1/flash.c @@ -97,7 +97,7 @@ It is locked by default on reset. void flash_unlock_upper(void) { - if (DESIG_FLASH_SIZE > 512) { + if (desig_get_flash_size() > 512) { /* Clear the unlock state. */ FLASH_CR2 |= FLASH_CR_LOCK; @@ -126,7 +126,7 @@ void flash_lock_upper(void) void flash_clear_pgerr_flag_upper(void) { - if (DESIG_FLASH_SIZE > 512) { + if (desig_get_flash_size() > 512) { FLASH_SR2 |= FLASH_SR_PGERR; } } @@ -138,7 +138,7 @@ void flash_clear_pgerr_flag_upper(void) void flash_clear_eop_flag_upper(void) { - if (DESIG_FLASH_SIZE > 512) { + if (desig_get_flash_size() > 512) { FLASH_SR2 |= FLASH_SR_EOP; } } @@ -150,7 +150,7 @@ void flash_clear_eop_flag_upper(void) void flash_clear_wrprterr_flag_upper(void) { - if (DESIG_FLASH_SIZE > 512) { + if (desig_get_flash_size() > 512) { FLASH_SR2 |= FLASH_SR_WRPRTERR; } } @@ -166,7 +166,7 @@ void flash_clear_status_flags(void) flash_clear_pgerr_flag(); flash_clear_eop_flag(); flash_clear_wrprterr_flag(); - if (DESIG_FLASH_SIZE > 512) { + if (desig_get_flash_size() > 512) { flash_clear_pgerr_flag_upper(); flash_clear_eop_flag_upper(); flash_clear_wrprterr_flag_upper(); @@ -192,7 +192,7 @@ uint32_t flash_get_status_flags(void) FLASH_SR_EOP | FLASH_SR_WRPRTERR | FLASH_SR_BSY)); - if (DESIG_FLASH_SIZE > 512) { + if (desig_get_flash_size() > 512) { flags |= (FLASH_SR2 & (FLASH_SR_PGERR | FLASH_SR_EOP | FLASH_SR_WRPRTERR | @@ -219,7 +219,7 @@ void flash_program_half_word(uint32_t address, uint16_t data) { flash_wait_for_last_operation(); - if ((DESIG_FLASH_SIZE > 512) && (address >= FLASH_BASE+0x00080000)) { + if ((desig_get_flash_size() > 512) && (address >= FLASH_BASE+0x00080000)) { FLASH_CR2 |= FLASH_CR_PG; } else { FLASH_CR |= FLASH_CR_PG; @@ -229,7 +229,7 @@ void flash_program_half_word(uint32_t address, uint16_t data) flash_wait_for_last_operation(); - if ((DESIG_FLASH_SIZE > 512) && (address >= FLASH_BASE+0x00080000)) { + if ((desig_get_flash_size() > 512) && (address >= FLASH_BASE+0x00080000)) { FLASH_CR2 &= ~FLASH_CR_PG; } else { FLASH_CR &= ~FLASH_CR_PG; @@ -253,7 +253,7 @@ void flash_erase_page(uint32_t page_address) { flash_wait_for_last_operation(); - if ((DESIG_FLASH_SIZE > 512) + if ((desig_get_flash_size() > 512) && (page_address >= FLASH_BASE+0x00080000)) { FLASH_CR2 |= FLASH_CR_PER; FLASH_AR2 = page_address; @@ -266,7 +266,7 @@ void flash_erase_page(uint32_t page_address) flash_wait_for_last_operation(); - if ((DESIG_FLASH_SIZE > 512) + if ((desig_get_flash_size() > 512) && (page_address >= FLASH_BASE+0x00080000)) { FLASH_CR2 &= ~FLASH_CR_PER; } else { diff --git a/lib/stm32/f2/Makefile b/lib/stm32/f2/Makefile index 6f4a677c..83ab0748 100644 --- a/lib/stm32/f2/Makefile +++ b/lib/stm32/f2/Makefile @@ -37,7 +37,7 @@ ARFLAGS = rcs OBJS += crc_common_all.o OBJS += crypto_common_f24.o OBJS += dac_common_all.o -OBJS += desig.o +OBJS += desig_common_all.o desig_common_v1.o OBJS += dma_common_f24.o OBJS += exti_common_all.o OBJS += flash.o flash_common_all.o flash_common_f.o flash_common_f24.o flash_common_idcache.o diff --git a/lib/stm32/f3/Makefile b/lib/stm32/f3/Makefile index 1a56ba6a..6f768322 100644 --- a/lib/stm32/f3/Makefile +++ b/lib/stm32/f3/Makefile @@ -39,7 +39,7 @@ OBJS += adc.o adc_common_v2.o adc_common_v2_multi.o OBJS += can.o OBJS += crc_common_all.o crc_v2.o OBJS += dac_common_all.o -OBJS += desig.o +OBJS += desig_common_all.o desig_common_v1.o OBJS += dma_common_l1f013.o OBJS += exti_common_all.o OBJS += flash.o flash_common_all.o flash_common_f.o diff --git a/lib/stm32/f4/Makefile b/lib/stm32/f4/Makefile index 047a72e8..c560f56b 100644 --- a/lib/stm32/f4/Makefile +++ b/lib/stm32/f4/Makefile @@ -42,7 +42,7 @@ OBJS += can.o OBJS += crc_common_all.o OBJS += crypto_common_f24.o crypto.o OBJS += dac_common_all.o -OBJS += desig.o +OBJS += desig_common_all.o desig_common_v1.o OBJS += dma_common_f24.o OBJS += dma2d_common_f47.o OBJS += dsi_common_f47.o diff --git a/lib/stm32/f7/Makefile b/lib/stm32/f7/Makefile index ff078655..30c6c44b 100644 --- a/lib/stm32/f7/Makefile +++ b/lib/stm32/f7/Makefile @@ -44,7 +44,7 @@ OBJS += adc_common_v1.o adc_common_v1_multi.o adc_common_f47.o OBJS += can.o OBJS += crc_common_all.o crc_v2.o OBJS += dac_common_all.o -OBJS += desig.o +OBJS += desig_common_all.o desig.o OBJS += dma_common_f24.o OBJS += dma2d_common_f47.o OBJS += dsi_common_f47.o diff --git a/lib/stm32/f7/desig.c b/lib/stm32/f7/desig.c new file mode 100644 index 00000000..098b716d --- /dev/null +++ b/lib/stm32/f7/desig.c @@ -0,0 +1,72 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2019 Matthew Lai + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include + +uint16_t desig_get_flash_size(void) +{ + uint32_t device_id = DBGMCU_IDCODE & DBGMCU_IDCODE_DEV_ID_MASK; + uint32_t* flash_size_base = 0; + switch (device_id) { + case 0x0449: + flash_size_base = (uint32_t*) DESIG_FLASH_SIZE_BASE_449; + break; + case 0x0451: + flash_size_base = (uint32_t*) DESIG_FLASH_SIZE_BASE_451; + break; + case 0x0452: + flash_size_base = (uint32_t*) DESIG_FLASH_SIZE_BASE_452; + break; + } + + if (!flash_size_base) { + /* We don't know the address for this device. Hang here to help debugging. */ + cm3_assert_not_reached(); + } + + return *flash_size_base; +} + +void desig_get_unique_id(uint32_t *result) +{ + uint32_t device_id = DBGMCU_IDCODE & DBGMCU_IDCODE_DEV_ID_MASK; + uint32_t* uid_base = 0; + switch (device_id) { + case 0x0449: + uid_base = (uint32_t*) DESIG_UNIQUE_ID_BASE_449; + break; + case 0x0451: + uid_base = (uint32_t*) DESIG_UNIQUE_ID_BASE_451; + break; + case 0x0452: + uid_base = (uint32_t*) DESIG_UNIQUE_ID_BASE_452; + break; + } + + if (!uid_base) { + /* We don't know the address for this device. Hang here to help debugging. */ + cm3_assert_not_reached(); + } + + result[0] = uid_base[2]; + result[1] = uid_base[1]; + result[2] = uid_base[0]; +} diff --git a/lib/stm32/l0/Makefile b/lib/stm32/l0/Makefile index 4950e602..68448ac4 100644 --- a/lib/stm32/l0/Makefile +++ b/lib/stm32/l0/Makefile @@ -37,7 +37,7 @@ ARFLAGS = rcs OBJS += adc_common_v2.o OBJS += crc_common_all.o crc_v2.o OBJS += crs_common_all.o -OBJS += desig.o +OBJS += desig_common_all.o desig_common_v1.o OBJS += dma_common_l1f013.o dma_common_csel.o OBJS += exti_common_all.o OBJS += flash_common_all.o flash_common_l01.o diff --git a/lib/stm32/l1/Makefile b/lib/stm32/l1/Makefile index a076e5bf..00d76fd2 100644 --- a/lib/stm32/l1/Makefile +++ b/lib/stm32/l1/Makefile @@ -37,7 +37,7 @@ OBJS += adc.o adc_common_v1.o adc_common_v1_multi.o OBJS += flash.o OBJS += crc_common_all.o OBJS += dac_common_all.o -OBJS += desig.o +OBJS += desig_common_all.o desig_common_v1.o OBJS += dma_common_l1f013.o OBJS += exti_common_all.o OBJS += flash_common_all.o flash_common_l01.o