Skip to content

Commit c4bb1f7

Browse files
committed
Tighten libc headers and fault handling
Updates the embedded libc headers to improve declarations and compatibility: adds printf-style format attributes, switches `memset` to an out-of-line declaration with optimized implementation settings, aligns `dirent` with FatFs `DIR`, and refreshes header guards and copyright/contact metadata. It also hardens the hard fault handler by making the C linkage explicit, keeping the handler symbol visible, fixing the LR print cast, and using a proper infinite loop.
1 parent 30b8ac2 commit c4bb1f7

16 files changed

Lines changed: 200 additions & 193 deletions

File tree

firmware-template-gd32/hardfault_handler.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
#include "gd32xxxx.h" // IWYU pragma: keep
1414

15-
extern "C" void HardFault_Handler()
16-
{
15+
extern "C" {
16+
void HardFault_Handler() {
1717
__asm volatile(
1818
"TST LR, #4\n"
1919
"ITE EQ\n"
@@ -23,8 +23,7 @@ extern "C" void HardFault_Handler()
2323
"B HardfaultHandler\n");
2424
}
2525

26-
extern "C" void HardfaultHandler(uint32_t* hardfault_args, uint32_t lr_value)
27-
{
26+
__attribute__((used, externally_visible, noinline)) void HardfaultHandler(uint32_t* hardfault_args, uint32_t lr_value) {
2827
uint32_t cfsr;
2928
uint32_t bus_fault_address;
3029
uint32_t memmanage_fault_address;
@@ -57,16 +56,16 @@ extern "C" void HardfaultHandler(uint32_t* hardfault_args, uint32_t lr_value)
5756
printf(" HFSR = %x\n", (unsigned int)SCB->HFSR);
5857
printf(" DFSR = %x\n", (unsigned int)SCB->DFSR);
5958
printf(" AFSR = %x\n", (unsigned int)SCB->AFSR);
60-
if (cfsr & 0x0080)
61-
{
59+
if (cfsr & 0x0080) {
6260
printf(" MMFAR = %x\n", (unsigned int)memmanage_fault_address);
6361
}
64-
if (cfsr & 0x8000)
65-
{
62+
if (cfsr & 0x8000) {
6663
printf(" BFAR = %x\n", (unsigned int)bus_fault_address);
6764
}
6865
printf("- Misc\n");
69-
printf(" LR/EXC_RETURN= %x\n", lr_value);
66+
printf(" LR/EXC_RETURN= %x\n", (unsigned int)lr_value);
7067

71-
while (1);
68+
while (true) {
69+
}
70+
}
7271
}

include/arpa/inet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file inet.h
33
*
44
*/
5-
/* Copyright (C) 2020 by Arjan van Vught mailto:info@orangepi-dmx.nl
5+
/* Copyright (C) 2020 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal

include/assert.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file assert.h
33
*
44
*/
5-
/* Copyright (C) 2017-2020 by Arjan van Vught mailto:info@orangepi-dmx.nl
5+
/* Copyright (C) 2017-2020 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal

include/ctype.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file ctype.h
33
*
44
*/
5-
/* Copyright (C) 2017-2023 by Arjan van Vught mailto:info@orangepi-dmx.nl
5+
/* Copyright (C) 2017-2023 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal

include/dirent.h

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file dirent.h
33
*
44
*/
5-
/* Copyright (C) 2020-2024 by Arjan van Vught mailto:info@orangepi-dmx.nl
5+
/* Copyright (C) 2020-2024 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -28,21 +28,9 @@
2828

2929
#include <stdio.h>
3030

31-
#if !defined (FF_DEFINED)
32-
typedef void *DIR;
33-
#endif
31+
#include "../lib-fatfs/ff14b/source/ff.h"
3432

35-
enum {
36-
DT_UNKNOWN = 0,
37-
DT_FIFO = 1,
38-
DT_CHR = 2,
39-
DT_DIR = 4,
40-
DT_BLK = 6,
41-
DT_REG = 8,
42-
DT_LNK = 10,
43-
DT_SOCK = 12,
44-
DT_WHT = 14
45-
};
33+
enum { DT_UNKNOWN = 0, DT_FIFO = 1, DT_CHR = 2, DT_DIR = 4, DT_BLK = 6, DT_REG = 8, DT_LNK = 10, DT_SOCK = 12, DT_WHT = 14 };
4634

4735
/**
4836
* https://en.wikibooks.org/wiki/C_Programming/POSIX_Reference/dirent.h
@@ -54,8 +42,8 @@ struct dirent {
5442
off_t d_off;
5543
unsigned short d_reclen;
5644
#endif
57-
unsigned char d_type;
58-
char d_name[FILENAME_MAX];
45+
unsigned char d_type;
46+
char d_name[FILENAME_MAX];
5947
};
6048

6149
typedef struct dirent dirent_t;
@@ -64,14 +52,12 @@ typedef struct dirent dirent_t;
6452
extern "C" {
6553
#endif
6654

67-
extern DIR *opendir(const char *dirname);
68-
extern struct dirent *readdir(DIR *dirp);
55+
extern DIR* opendir([[maybe_unused]] const char* dirname);
56+
extern struct dirent* readdir([[maybe_unused]] DIR* dirp);
6957
extern int closedir(DIR* dirp);
7058

7159
#ifdef __cplusplus
7260
}
7361
#endif
7462

75-
76-
7763
#endif /* DIRENT_H_ */

include/errno.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file errno.h
33
*
44
*/
5-
/* Copyright (C) 2020 by Arjan van Vught mailto:info@orangepi-dmx.nl
5+
/* Copyright (C) 2020 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal

include/math.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file math.h
33
*
44
*/
5-
/* Copyright (C) 2017-2019 by Arjan van Vught mailto:info@orangepi-dmx.nl
5+
/* Copyright (C) 2017-2019 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal

include/poll.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file poll.h
33
*
44
*/
5-
/* Copyright (C) 2020 by Arjan van Vught mailto:info@orangepi-dmx.nl
5+
/* Copyright (C) 2020 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal

include/stdio.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file stdio.h
33
*
44
*/
5-
/* Copyright (C) 2017-2024 by Arjan van Vught mailto:info@orangepi-dmx.nl
5+
/* Copyright (C) 2017-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -93,10 +93,14 @@ void clearerr(FILE *stream);
9393
int ferror(FILE *stream);
9494
int feof(FILE *stream);
9595

96-
int printf(const char *format, ...);
96+
int printf(const char* format, ...)
97+
__attribute__((format(printf, 1, 2)));
9798

98-
int sprintf(char *str, const char *format, ...);
99-
int snprintf(char *str, size_t size, const char *format, ...);
99+
int sprintf(char* buffer, const char* format, ...)
100+
__attribute__((format(printf, 2, 3)));
101+
102+
int snprintf(char* buffer, size_t size, const char* format, ...)
103+
__attribute__((format(printf, 3, 4)));
100104

101105
int vprintf(const char *format, va_list ap);
102106
int vsnprintf(char *str, size_t size, const char *format, va_list);

include/stdlib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file stdlib.h
33
*
44
*/
5-
/* Copyright (C) 2017-2023 by Arjan van Vught mailto:info@orangepi-dmx.nl
5+
/* Copyright (C) 2017-2023 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)