Skip to content

Commit 1aa4785

Browse files
larocheaggargUbuntu
authored
Various Qemu Cortex M3 ports now support picolibc (FreeRTOS#1364)
* Various Qemu Cortex M3 ports now support picolibc Allow various Qemu Cortex M3 ports to compile against picolibc. Also support "-flto" to work correctly. Tested with picolibc in current Debian 13. Just use "PICOLIBC=1 make" to switch over from default newlib. Signed-off-by: Florian La Roche <[email protected]> * Add ffreestanding Signed-off-by: Ubuntu <[email protected]> * Fix formatting check Signed-off-by: Ubuntu <[email protected]> --------- Signed-off-by: Florian La Roche <[email protected]> Signed-off-by: Ubuntu <[email protected]> Co-authored-by: Gaurav-Aggarwal-AWS <[email protected]> Co-authored-by: Ubuntu <[email protected]>
1 parent 51467d8 commit 1aa4785

File tree

10 files changed

+96
-34
lines changed

10 files changed

+96
-34
lines changed

FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Echo_Qemu_mps2/Makefile

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,38 @@ SOURCE_FILES += ${FREERTOS_TCP}/source/portable/BufferManagement/BufferAllocatio
5151
SOURCE_FILES += ${FREERTOS_TCP}/source/portable/NetworkInterface/MPS2_AN385/NetworkInterface.c
5252
SOURCE_FILES += ${FREERTOS_TCP}/source/portable/NetworkInterface/MPS2_AN385/ether_lan9118/smsc9220_eth_drv.c
5353

54-
DEFINES := -DHEAP3
55-
CPPFLAGS += $(DEFINES)
54+
CPPFLAGS += -DHEAP3
5655

56+
ifeq ($(PICOLIBC), 1)
57+
CFLAGS += --specs=picolibc.specs -DPICOLIBC_INTEGER_PRINTF_SCANF --oslib=semihost
58+
endif
5759
CFLAGS += -mthumb -mcpu=cortex-m3
5860
ifeq ($(DEBUG), 1)
5961
CFLAGS += -g3 -Og -ffunction-sections -fdata-sections
6062
else
6163
CFLAGS += -Os -ffunction-sections -fdata-sections
6264
endif
63-
CFLAGS += -MMD
65+
#CFLAGS += -flto
6466
CFLAGS += -Wall -Wextra -Wshadow
6567
#CFLAGS += -Wpedantic -fanalyzer
66-
#CFLAGS += -flto
68+
CFLAGS += -MMD
6769
CFLAGS += $(INCLUDE_DIRS)
6870

6971
LDFLAGS = -T mps2_m3.ld
7072
LDFLAGS += -Xlinker -Map=${BUILD_DIR}/output.map
7173
LDFLAGS += -Xlinker --gc-sections
72-
LDFLAGS += -nostartfiles -specs=nano.specs -specs=nosys.specs -specs=rdimon.specs
74+
#LDFLAGS += -Xlinker --print-gc-sections
75+
ifeq ($(PICOLIBC), 1)
76+
LDFLAGS += -nostartfiles
77+
else
78+
LDFLAGS += -nostartfiles -specs=nano.specs -specs=nosys.specs -specs=rdimon.specs
79+
endif
7380

7481
OBJ_FILES := $(SOURCE_FILES:%.c=$(BUILD_DIR)/%.o)
7582

7683
.PHONY: clean
7784

78-
$(BUILD_DIR)/$(BIN) : $(OBJ_FILES)
85+
$(BUILD_DIR)/$(BIN): $(OBJ_FILES)
7986
$(CC) $(CFLAGS) $(LDFLAGS) $+ -o $(@)
8087
$(SIZE) $(@)
8188

@@ -88,7 +95,7 @@ $(BUILD_DIR)/$(BIN) : $(OBJ_FILES)
8895
INCLUDES := $(SOURCE_FILES:%.c=$(BUILD_DIR)/%.d)
8996
-include $(INCLUDES)
9097

91-
${BUILD_DIR}/%.o : %.c Makefile
98+
${BUILD_DIR}/%.o: %.c Makefile
9299
-mkdir -p $(@D)
93100
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
94101

FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Echo_Qemu_mps2/main_networking.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ static UBaseType_t ulNextRand;
142142
void main_tcp_echo_client_tasks( void )
143143
{
144144
BaseType_t xReturn;
145-
const uint32_t ulLongTime_ms = pdMS_TO_TICKS( 1000UL );
145+
146+
#ifndef __PICOLIBC__
147+
const uint32_t ulLongTime_ms = pdMS_TO_TICKS( 1000UL );
148+
#endif
146149

147150
/*
148151
* Instructions for using this project are provided on:
@@ -203,7 +206,9 @@ void main_tcp_echo_client_tasks( void )
203206
* really applicable to the Linux simulator port). */
204207
for( ; ; )
205208
{
206-
usleep( ulLongTime_ms * 1000 );
209+
#ifndef __PICOLIBC__
210+
usleep( ulLongTime_ms * 1000 );
211+
#endif
207212
}
208213
}
209214
/*-----------------------------------------------------------*/

FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Echo_Qemu_mps2/mps2_m3.ld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ SECTIONS
124124
PROVIDE ( end = . );
125125
PROVIDE ( _end = . );
126126
_heap_bottom = .;
127+
__heap_start = .;
127128
. = . + _Min_Heap_Size;
128129
_heap_top = .;
130+
__heap_end = .;
129131
. = . + _Min_Stack_Size;
130132
. = ALIGN(8);
131133
} >RAM

FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Echo_Qemu_mps2/syscalls.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern "C" {
3030
#include <sys/types.h>
3131

3232
void uart_init( void );
33+
#ifndef __PICOLIBC__
3334
__attribute__( ( used ) ) int _fstat( int file );
3435
int _read( int file,
3536
char * buf,
@@ -39,6 +40,7 @@ int _write( int file,
3940
int len );
4041

4142
void * _sbrk( int incr );
43+
#endif
4244

4345
typedef struct UART_t
4446
{
@@ -55,10 +57,12 @@ typedef struct UART_t
5557
#define UART_CTRL_TX_EN ( 1 << 0 )
5658

5759

60+
#ifndef __PICOLIBC__
5861
extern unsigned long _heap_bottom;
5962
extern unsigned long _heap_top;
6063

6164
static char * heap_end = ( char * ) &_heap_bottom;
65+
#endif
6266

6367
/**
6468
* @brief initializes the UART emulated hardware
@@ -69,6 +73,24 @@ void uart_init( void )
6973
UART0_ADDR->CTRL = UART_CTRL_TX_EN;
7074
}
7175

76+
#ifdef __PICOLIBC__
77+
78+
#include <stdio.h>
79+
80+
int
81+
_uart_putc(char c, FILE *file)
82+
{
83+
( void ) file;
84+
85+
UART_DR( UART0_ADDR ) = c;
86+
return (unsigned char) c;
87+
}
88+
89+
static FILE __stdio = FDEV_SETUP_STREAM(_uart_putc, NULL, NULL, _FDEV_SETUP_WRITE);
90+
__attribute__( ( used ) ) FILE *const stdout = &__stdio;
91+
92+
#else
93+
7294
/**
7395
* @brief not used anywhere in the code
7496
* @todo implement if necessary
@@ -138,6 +160,7 @@ void * _sbrk( int incr )
138160

139161
return prev_heap_end;
140162
}
163+
#endif
141164

142165
#ifdef __cplusplus
143166
}

FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/build/gcc/Makefile

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ LD = arm-none-eabi-gcc
99
SIZE = arm-none-eabi-size
1010
MAKE = make
1111

12-
CFLAGS += -ffreestanding -mthumb -mcpu=cortex-m3
13-
CFLAGS += -Wall -Wextra -Wshadow -Wno-unused-value
12+
ifeq ($(PICOLIBC), 1)
13+
CFLAGS += --specs=picolibc.specs -DPICOLIBC_INTEGER_PRINTF_SCANF
14+
endif
15+
CFLAGS += -mthumb -mcpu=cortex-m3 -ffreestanding
1416
CFLAGS += -g3 -Os -ffunction-sections -fdata-sections
15-
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" -MT $@
16-
#CFLAGS += -std=c99
17-
#CFLAGS += -Wpedantic -fanalyzer
1817
#CFLAGS += -flto
18+
CFLAGS += -Wall -Wextra -Wshadow -Wno-unused-value
19+
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" -MT $@
1920
CFLAGS += $(INCLUDE_DIRS)
2021

2122
LDFLAGS = -T ./mps2_m3.ld
2223
LDFLAGS += -Xlinker -Map=$(OUTPUT_DIR)/RTOSDemo.map
2324
LDFLAGS += -Xlinker --gc-sections
24-
LDFLAGS += -nostartfiles
25-
LDFLAGS += -specs=nano.specs -specs=nosys.specs # -specs=rdimon.specs
25+
#LDFLAGS += -Xlinker --print-gc-sections
26+
ifneq ($(PICOLIBC), 1)
27+
LDFLAGS += -nostartfiles -specs=nano.specs -specs=nosys.specs # -specs=rdimon.specs
28+
endif
2629

2730
#
2831
# Kernel build.
@@ -99,7 +102,7 @@ VPATH += $(TRACERECORDER_DIR)
99102
VPATH += $(TRACERECORDER_DIR)/kernelports/FreeRTOS
100103
VPATH += $(TRACERECORDER_DIR)/streamports/RingBuffer
101104
INCLUDE_DIRS += -I$(TRACERECORDER_CFG_DIR)
102-
INCLUDE_DIRS += -I$(TRACERECORDER_DIR)/include
105+
INCLUDE_DIRS += -I$(TRACERECORDER_DIR)/include
103106
INCLUDE_DIRS += -I$(TRACERECORDER_DIR)/kernelports/FreeRTOS/include
104107
INCLUDE_DIRS += -I$(TRACERECORDER_DIR)/streamports/RingBuffer/include
105108
SOURCE_FILES += (TRACERECORDER_DIR)/kernelports/FreeRTOS/trcKernelPort.c

FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/build/gcc/startup_gcc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const uint32_t* isr_vector[] __attribute__((section(".isr_vector"), used)) =
7979

8080
void Reset_Handler( void )
8181
{
82-
main();
82+
(void) main();
8383
}
8484

8585
/* Variables used to store the value of registers at the time a hardfault

FreeRTOS/Demo/CORTEX_MPS2_QEMU_IAR_GCC/main.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void prvUARTInit( void );
103103

104104
/*-----------------------------------------------------------*/
105105

106-
void main( void )
106+
int main( void )
107107
{
108108
/* See https://www.freertos.org/freertos-on-qemu-mps2-an385-model.html for
109109
* instructions. */
@@ -141,6 +141,7 @@ void main( void )
141141
main_full();
142142
}
143143
#endif
144+
return 0;
144145
}
145146
/*-----------------------------------------------------------*/
146147

@@ -313,6 +314,25 @@ static void prvUARTInit( void )
313314
}
314315
/*-----------------------------------------------------------*/
315316

317+
#ifdef __PICOLIBC__
318+
int
319+
_uart_putc(char c, FILE *file)
320+
{
321+
( void ) file;
322+
323+
while( ( UART0_STATE & TX_BUFFER_MASK ) != 0 )
324+
{
325+
}
326+
327+
UART0_DATA = c;
328+
return (unsigned char) c;
329+
}
330+
331+
static FILE __stdio = FDEV_SETUP_STREAM(_uart_putc, NULL, NULL, _FDEV_SETUP_WRITE);
332+
__attribute__( ( used ) ) FILE *const stdout = &__stdio;
333+
#else
334+
/*-----------------------------------------------------------*/
335+
316336
int __write( int iFile,
317337
char * pcString,
318338
int iStringLength )
@@ -351,3 +371,4 @@ void * malloc( size_t size )
351371
{
352372
}
353373
}
374+
#endif

FreeRTOS/Demo/CORTEX_MPU_M3_MPS2_QEMU_GCC/Makefile

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,29 @@ INCLUDE_DIRS += -I$(FREERTOS_DIR)/Demo/CORTEX_MPU_M3_MPS2_QEMU_GCC/C
3535
INCLUDE_DIRS += -I$(KERNEL_DIR)/include
3636
INCLUDE_DIRS += -I$(KERNEL_DIR)/portable/GCC/ARM_CM3_MPU
3737

38-
DEFINES := -DHEAP4
39-
CPPFLAGS += $(DEFINES)
38+
CPPFLAGS += -DHEAP4
4039

40+
ifeq ($(PICOLIBC), 1)
41+
CFLAGS += --specs=picolibc.specs -DPICOLIBC_INTEGER_PRINTF_SCANF
42+
endif
4143
CFLAGS += -mthumb -mcpu=cortex-m3
44+
ifeq ($(DEBUG), 1)
45+
CFLAGS += -g3 -Og -ffunction-sections -fdata-sections -save-temps=obj
46+
else
47+
CFLAGS += -Os -ffunction-sections -fdata-sections
48+
endif
49+
#CFLAGS += -flto
4250
CFLAGS += -Wall -Wextra -Wshadow -Wno-unused-parameter
43-
#CFLAGS += -Wpedantic -fanalyzer
4451
CFLAGS += $(INCLUDE_DIRS)
4552

4653
LDFLAGS = -T ./scripts/mps2_m3.ld
4754
LDFLAGS += -Xlinker -Map=${BUILD_DIR}/output.map
4855
LDFLAGS += -Xlinker --gc-sections
49-
LDFLAGS += -nostartfiles -nostdlib -nolibc -nodefaultlibs
56+
#LDFLAGS += -Xlinker --print-gc-sections
57+
#LDFLAGS += -nostartfiles -nostdlib -nolibc -nodefaultlibs
58+
LDFLAGS += -nostdlib
5059

51-
ifeq ($(DEBUG), 1)
52-
CFLAGS += -g3 -Og -ffunction-sections -fdata-sections -save-temps=obj
53-
else
54-
CFLAGS += -Os -ffunction-sections -fdata-sections
55-
endif
5660

57-
ifeq ($(PICOLIBC), 1)
58-
CFLAGS += --specs=picolibc.specs -DPICOLIBC_INTEGER_PRINTF_SCANF
59-
LDFLAGS += --specs=picolibc.specs -DPICOLIBC_INTEGER_PRINTF_SCANF
60-
endif
6161

6262
OBJ_FILES := $(SOURCE_FILES:%.c=$(BUILD_DIR)/%.o)
6363

FreeRTOS/Demo/CORTEX_MPU_M3_MPS2_QEMU_GCC/scripts/mps2_m3.ld

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ SECTIONS
6666
*(privileged_functions)
6767
. = ALIGN(4);
6868
FILL(0xDEAD);
69-
/* Ensure that un-privileged code is placed after the region reserved 4 * for privileged kernel code. */
69+
/* Ensure that un-privileged code is placed after the region reserved
70+
* for privileged kernel code. */
7071
/* Note that dot (.) actually refers to the byte offset from the start
7172
* of the current section (.privileged_functions in this case). As a
7273
* result, setting dot (.) to a value sets the size of the section. */

FreeRTOS/Demo/CORTEX_MPU_M3_MPS2_QEMU_GCC/syscall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ _uart_putc(char c, FILE *file)
7676

7777
static FILE __stdio = FDEV_SETUP_STREAM(_uart_putc, NULL, NULL, _FDEV_SETUP_WRITE);
7878

79-
FILE *const stdout = &__stdio;
79+
__attribute__( ( used ) ) FILE *const stdout = &__stdio;
8080

8181
#else
8282

0 commit comments

Comments
 (0)