Skip to content

Commit

Permalink
Merge branch 'develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightwalker-87 authored Nov 23, 2023
2 parents 5df53ad + e212349 commit ed558c9
Show file tree
Hide file tree
Showing 18 changed files with 363 additions and 53 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ name: "CodeQL"

on:
push:
branches: [ testing, develop, master ]
branches: [testing, develop, master]
pull_request:
# The branches below must be a subset of the branches above
branches: [ testing, develop ]
branches: [testing, develop]
schedule:
- cron: '00 20 * * 1'
- cron: "00 20 * * 1"

jobs:
analyze:
Expand All @@ -28,7 +28,7 @@ jobs:
strategy:
fail-fast: false
matrix:
language: [ 'cpp' ]
language: ["cpp"]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
Expand All @@ -41,7 +41,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -52,7 +52,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -66,4 +66,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
14 changes: 14 additions & 0 deletions config/chips/C011xx.chip
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Chip-ID file for STM32C011xx device
#
dev_type STM32C011xx
ref_manual_id 0490
chip_id 0x453 // STM32_CHIPID_C011xx
flash_type C0
flash_size_reg 0x1fff75a0
flash_pagesize 0x800 // 2 KB
sram_size 0x1800 // 6 KB
bootrom_base 0x1fff0000
bootrom_size 0x1800 // 6 KB
option_base 0x1fff7800 // STM32_C0_OPTION_BYTES_BASE
option_size 0x80 // 128 B
flags none
14 changes: 14 additions & 0 deletions config/chips/C031xx.chip
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Chip-ID file for STM32C031xx device
#
dev_type STM32C031xx
ref_manual_id 0490
chip_id 0x453 // STM32_CHIPID_C031xx
flash_type C0
flash_size_reg 0x1fff75a0
flash_pagesize 0x800 // 2 KB
sram_size 0x3000 // 12 KB
bootrom_base 0x1fff0000
bootrom_size 0x1800 // 6 KB
option_base 0x1fff7800 // STM32_C0_OPTION_BYTES_BASE
option_size 0x80 // 128 B
flags none
2 changes: 2 additions & 0 deletions config/chips/L45x_L46x.chip
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ bootrom_size 0x7000 // 28 KB
option_base 0x0
option_size 0x0
flags swo
otp_base 0x1fff7000
otp_size 0x400 // 1 KB
2 changes: 2 additions & 0 deletions config/chips/L496x_L4A6x.chip
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ bootrom_size 0x7000 // 28 KB
option_base 0x1fff7800 // STM32_L4_OPTION_BYTES_BASE
option_size 0x4 // 4 B
flags swo
otp_base 0x1fff7000
otp_size 0x400 // 1 KB
1 change: 1 addition & 0 deletions doc/supported_devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ More commonly these are:
| Product-Family | ARM Cortex Core | Product Line |
| -------------- | --------------- | ---------------------------------------------------------- |
| STM32F0 | M0 | |
| STM32C0 | M0+ | |
| STM32G0 | M0+ | |
| STM32L0 | M0+ | |
| STM32F10**0** | M3 | Value line |
Expand Down
3 changes: 3 additions & 0 deletions inc/stlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ struct _stlink {
uint32_t chip_flags; // stlink_chipid_params.flags, set by stlink_load_device_params(), values: CHIP_F_xxx

uint32_t max_trace_freq; // set by stlink_open_usb()

uint32_t otp_base;
uint32_t otp_size;
};

/* Functions defined in common.c */
Expand Down
30 changes: 19 additions & 11 deletions inc/stm32.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,18 @@ enum stm32_core_id {
/* STM32 flash types */
enum stm32_flash_type {
STM32_FLASH_TYPE_UNKNOWN = 0,
STM32_FLASH_TYPE_F0_F1_F3 = 1,
STM32_FLASH_TYPE_F1_XL = 2,
STM32_FLASH_TYPE_F2_F4 = 3,
STM32_FLASH_TYPE_F7 = 4,
STM32_FLASH_TYPE_G0 = 5,
STM32_FLASH_TYPE_G4 = 6,
STM32_FLASH_TYPE_H7 = 7,
STM32_FLASH_TYPE_L0_L1 = 8,
STM32_FLASH_TYPE_L4 = 9,
STM32_FLASH_TYPE_L5_U5_H5 = 10,
STM32_FLASH_TYPE_WB_WL = 11,
STM32_FLASH_TYPE_C0 = 1,
STM32_FLASH_TYPE_F0_F1_F3 = 2,
STM32_FLASH_TYPE_F1_XL = 3,
STM32_FLASH_TYPE_F2_F4 = 4,
STM32_FLASH_TYPE_F7 = 5,
STM32_FLASH_TYPE_G0 = 6,
STM32_FLASH_TYPE_G4 = 7,
STM32_FLASH_TYPE_H7 = 8,
STM32_FLASH_TYPE_L0_L1 = 9,
STM32_FLASH_TYPE_L4 = 10,
STM32_FLASH_TYPE_L5_U5_H5 = 11,
STM32_FLASH_TYPE_WB_WL = 12,
};

/* STM32 chip-ids */
Expand Down Expand Up @@ -102,6 +103,7 @@ enum stm32_chipids {
STM32_CHIPID_F0 = 0x440,
STM32_CHIPID_F412 = 0x441,
STM32_CHIPID_F09x = 0x442,
STM32_CHIPID_C011xx = 0x443, /* RM0490 (revision 3), section 26.10.1 "DBG device ID code register (DBG_IDCODE)" */
STM32_CHIPID_F0xx_SMALL = 0x444,
STM32_CHIPID_F04 = 0x445,
STM32_CHIPID_F303_HD = 0x446, /* high density */
Expand All @@ -111,6 +113,7 @@ enum stm32_chipids {
STM32_CHIPID_H74xxx = 0x450, /* RM0433, p.3189 */
STM32_CHIPID_F76xxx = 0x451,
STM32_CHIPID_F72xxx = 0x452, /* Nucleo F722ZE board */
STM32_CHIPID_C031xx = 0x453, /* RM0490 (revision 3), section 26.10.1 "DBG device ID code register (DBG_IDCODE)" */
STM32_CHIPID_U535_U545 = 0x455, /* RM0456, p.3604 */
STM32_CHIPID_G0_CAT4 = 0x456, /* G051/G061 */
STM32_CHIPID_L0_CAT1 = 0x457,
Expand Down Expand Up @@ -139,6 +142,8 @@ enum stm32_chipids {
};

/* Constant STM32 option bytes base memory address */
#define STM32_C0_OPTION_BYTES_BASE ((uint32_t)0x1fff7800)

#define STM32_F4_OPTION_BYTES_BASE ((uint32_t)0x40023c14)

#define STM32_H7_OPTION_BYTES_BASE ((uint32_t)0x5200201c)
Expand Down Expand Up @@ -192,6 +197,9 @@ enum stm32_chipids {
#define STM32WB_DBGMCU_APB1FZR1_WWDG_STOP 11
#define STM32WB_DBGMCU_APB1FZR1_IWDG_STOP 12

#define STM32C0_RCC_AHBENR 0x40021038 // RM0490 (revision 3), section 5.4.25 "RCC register map"
#define STM32C0_RCC_DMAEN 0x00000001 // DMAEN // RM0490 (revision 3), section 5.4.25 "RCC register map"

#define STM32F1_RCC_AHBENR 0x40021014
#define STM32F1_RCC_DMAEN 0x00000003 // DMA2EN | DMA1EN

Expand Down
24 changes: 24 additions & 0 deletions inc/stm32flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@
#define FLASH_OBR_OFF ((uint32_t)0x1c)
#define FLASH_WRPR_OFF ((uint32_t)0x20)

// == STM32C0 == (RM0490)
// C0 Flash registers
#define FLASH_C0_REGS_ADDR ((uint32_t)0x40022000)
#define FLASH_C0_KEYR (FLASH_C0_REGS_ADDR + 0x08)
#define FLASH_C0_OPT_KEYR (FLASH_C0_REGS_ADDR + 0x0C)
#define FLASH_C0_SR (FLASH_C0_REGS_ADDR + 0x10)
#define FLASH_C0_CR (FLASH_C0_REGS_ADDR + 0x14)
#define FLASH_C0_OPTR (FLASH_C0_REGS_ADDR + 0x20)

// C0 Flash control register
#define FLASH_C0_CR_PNB 3
#define FLASH_C0_CR_STRT 16
#define FLASH_C0_CR_OPTSTRT 17
#define FLASH_C0_CR_OBL_LAUNCH 27
#define FLASH_C0_CR_OPTLOCK 30
#define FLASH_C0_CR_LOCK 31

// C0 Flash status register
#define FLASH_C0_SR_ERROR_MASK 0xC3F8 // [15:14], [9:3]
#define FLASH_C0_SR_PROGERR 3
#define FLASH_C0_SR_WRPERR 4
#define FLASH_C0_SR_PGAERR 5
#define FLASH_C0_SR_BSY 16

// == STM32F0 ==
#define FLASH_F0_OPTKEY1 0x45670123
#define FLASH_F0_OPTKEY2 0xcdef89ab
Expand Down
25 changes: 25 additions & 0 deletions src/st-flash/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ static void usage(void) {
puts("example write option control register1 byte: ./st-flash --area=optcr write 0xXXXXXXXX");
puts("example read option control register1 byte: ./st-flash --area=optcr1 read");
puts("example write option control register1 byte: ./st-flash --area=optcr1 write 0xXXXXXXXX");
puts("example read OTP area: ./st-flash --area=otp read [path]");
puts("example write OTP area: ./st-flash --area=otp write [path] 0xXXXXXXXX");
}

int32_t main(int32_t ac, char** av) {
Expand Down Expand Up @@ -180,6 +182,18 @@ int32_t main(int32_t ac, char** av) {
DLOG("@@@@ Write %d (%0#10x) to option bytes boot address\n", o.val, o.val);

err = stlink_write_option_bytes_boot_add32(sl, o.val);
} else if (o.area == FLASH_OTP) {
if(sl->otp_base == 0) {
err = -1;
printf("OTP Write NOT implemented\n");
goto on_error;
}
err = stlink_fwrite_flash(sl, o.filename, o.addr);

if (err == -1) {
printf("stlink_fwrite_flash() == -1\n");
goto on_error;
}
} else {
err = -1;
printf("Unknown memory region\n");
Expand Down Expand Up @@ -284,6 +298,17 @@ int32_t main(int32_t ac, char** av) {
} else {
printf("%08x\n",option_byte);
}
} else if (o.area == FLASH_OTP) {
if(sl->otp_base == 0) {
err = -1;
printf("OTP Read NOT implemented\n");
goto on_error;
}
err = stlink_fread(sl, o.filename, 0, sl->otp_base, sl->otp_size);
if (err == -1) {
printf("could not read OTP area (%d)\n", err);
goto on_error;
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/st-flash/flash_opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ int32_t flash_get_opts(struct flash_opts* o, int32_t ac, char** av) {

break;
} else if (o->area == FLASH_OTP) {
return bad_arg("TODO: otp not implemented yet");
if (ac > 1) { return invalid_args("otp read: [path]"); }
if (ac > 1 || ac ==0 ) { return invalid_args("otp read: [path]"); }
if (ac > 0) { o->filename = av[0]; }
break;
} else if (o->area == FLASH_OPTION_BYTES) {
Expand Down
18 changes: 17 additions & 1 deletion src/stlink-lib/chipid.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ void dump_a_chip(struct stlink_chipid_params *dev) {
DLOG("option_base 0x%x\n", dev->option_base);
DLOG("option_size 0x%x\n", dev->option_size);
DLOG("flags %d\n\n", dev->flags);
DLOG("otp_base %d\n\n", dev->otp_base);
DLOG("otp_size %d\n\n", dev->otp_size);
}

struct stlink_chipid_params *stlink_chipid_get_params(uint32_t chip_id) {
Expand Down Expand Up @@ -97,7 +99,9 @@ void process_chipfile(char *fname) {
buf[strlen(buf) - 1] = 0; // chomp newline
sscanf(buf, "%*s %n", &nc);
// Match human readable flash_type with enum stm32_flash_type { }.
if (strcmp(value, "F0_F1_F3") == 0) {
if(strcmp(value, "C0") == 0) {
ts->flash_type = STM32_FLASH_TYPE_C0;
} else if (strcmp(value, "F0_F1_F3") == 0) {
ts->flash_type = STM32_FLASH_TYPE_F0_F1_F3;
} else if (strcmp(value, "F1_XL") == 0) {
ts->flash_type = STM32_FLASH_TYPE_F1_XL;
Expand Down Expand Up @@ -182,6 +186,18 @@ void process_chipfile(char *fname) {
}

sscanf(value, "%x", &ts->flags);
} else if (strcmp(word, "otp_base") == 0) {
buf[strlen(buf) - 1] = 0; // chomp newline
sscanf(buf, "%*s %n", &nc);
if (sscanf(value, "%i", &ts->otp_base) < 1) {
fprintf(stderr, "Failed to parse option size\n");
}
} else if (strcmp(word, "otp_size") == 0) {
buf[strlen(buf) - 1] = 0; // chomp newline
sscanf(buf, "%*s %n", &nc);
if (sscanf(value, "%i", &ts->otp_size) < 1) {
fprintf(stderr, "Failed to parse option size\n");
}
} else {
fprintf(stderr, "Unknown keyword in %s: %s\n", fname, word);
}
Expand Down
2 changes: 2 additions & 0 deletions src/stlink-lib/chipid.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct stlink_chipid_params {
uint32_t option_base;
uint32_t option_size;
uint32_t flags;
uint32_t otp_base;
uint32_t otp_size;
struct stlink_chipid_params *next;
};

Expand Down
2 changes: 2 additions & 0 deletions src/stlink-lib/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ int32_t stlink_load_device_params(stlink_t *sl) {
sl->option_base = params->option_base;
sl->option_size = params->option_size;
sl->chip_flags = params->flags;
sl->otp_base = params->otp_base;
sl->otp_size = params->otp_size;

// medium and low devices have the same chipid. ram size depends on flash
// size. STM32F100xx datasheet Doc ID 16455 Table 2
Expand Down
Loading

0 comments on commit ed558c9

Please sign in to comment.