Skip to content

Commit 5b187ca

Browse files
add fevopen for avr-libc compatibility
1 parent f76435e commit 5b187ca

File tree

6 files changed

+124
-39
lines changed

6 files changed

+124
-39
lines changed

cores/stm32l4/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LD = $(TOOLS)/bin/arm-none-eabi-ld
88
CFLAGS = -mcpu=cortex-m4 -mthumb -c -g3 -Os $(WARNINGS) -std=gnu11 -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -MMD $(EXTRAS) $(DEFINES) $(INCLUDES)
99
CXXFLAGS = -mcpu=cortex-m4 -mthumb -c -g3 -Os $(WARNINGS) -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD $(EXTRAS) $(DEFINES) $(INCLUDES)
1010
ASFLAGS = -c -g -x assembler-with-cpp $(EXTRAS) $(DEFINES) $(INCLUDES)
11-
LDFLAGS = -L../../variants/dragonfly -Os -Wl,--gc-sections -save-temps $(EXTRAS) -T../../variants/dragonfly/linker_scripts/flash.ld -specs=nano.specs --specs=nosys.specs -mcpu=cortex-m4 -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align
11+
LDFLAGS = -L../../variants/dragonfly -Os -Wl,--gc-sections -save-temps $(EXTRAS) -T../../variants/dragonfly/linker_scripts/flash.ld --specs=nano.specs -mcpu=cortex-m4 -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align
1212
WARNINGS = -Wall -Wextra -Wno-unused-parameter
1313
EXTRAS = -DSTM32L476xx -D__FPU_PRESENT=1 -march=armv7e-m -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mabi=aapcs -mslow-flash-data
1414
DEFINES = -DF_CPU=80000000L -DARDUINO=10606 -D_ARDUINO_STM32L4_DRAGONFLY -DARDUINO_ARCH_STM32L4
@@ -86,7 +86,7 @@ all:: flash.bin
8686
$(CC) $(ASFLAGS) -c $< -o $@
8787

8888
flash.elf:: $(OBJS)
89-
$(CC) $(LDFLAGS) -Wl,-Map,flash.map -Wl,--start-group $(OBJS) -Wl,--end-group -lstm32l4_dragonfly -lm -o flash.elf
89+
$(CC) $(LDFLAGS) -Wl,-Map,flash.map -Wl,--start-group $(OBJS) -Wl,--end-group -lstm32l4_dragonfly -lc -lm -lstm32l4_dragonfly -o flash.elf
9090

9191
flash.bin:: flash.elf
9292
arm-none-eabi-objcopy -O binary flash.elf flash.bin

cores/stm32l4/avr/pgmspace.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#ifndef __PGMSPACE_H_
2828
#define __PGMSPACE_H_ 1
2929

30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
3034
#include <inttypes.h>
3135

3236
#define PROGMEM
@@ -124,4 +128,10 @@ typedef const void* uint_farptr_t;
124128

125129
#define pgm_get_far_address(addr) (&(addr))
126130

131+
extern struct __sFILE * fdevopen(int(*put)(char, struct __sFILE *), int(*get)(struct __sFILE *));
132+
133+
#ifdef __cplusplus
134+
}
135+
#endif
136+
127137
#endif

cores/stm32l4/stdlib_noniso.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <string.h>
2727
#include <stdbool.h>
2828
#include <stdint.h>
29+
#include <stdio.h>
2930
#include <math.h>
3031
#include "stdlib_noniso.h"
3132

@@ -220,3 +221,25 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
220221
*out = 0;
221222
return s;
222223
}
224+
225+
extern int (*stm32l4_stdio_put)(char, FILE*);
226+
extern int (*stm32l4_stdio_get)(FILE*);
227+
228+
FILE * fdevopen(int(*put)(char, FILE *), int(*get)(FILE *))
229+
{
230+
if (put != NULL)
231+
{
232+
stm32l4_stdio_put = (void*)put;
233+
234+
return stdout;
235+
}
236+
237+
if (get != NULL)
238+
{
239+
stm32l4_stdio_get = get;
240+
241+
return stdin;
242+
}
243+
244+
return NULL;
245+
}

platform.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ archive_file_path={build.path}/{archive_file}
8282
recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{archive_file_path}" "{object_file}"
8383

8484
## Combine gc-sections, archives, and objects
85-
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" "-L{build.variant.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} {build.extra_flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" --specs=nano.specs --specs=nosys.specs {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" {object_files} -Wl,--start-group "{build.path}/{archive_file}" -Wl,--end-group "-l{build.variant_system_lib}" -lm
85+
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" "-L{build.variant.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} {build.extra_flags} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" --specs=nano.specs {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" {object_files} -Wl,--start-group "{build.path}/{archive_file}" -Wl,--end-group "-l{build.variant_system_lib}" -lc -lm "-l{build.variant_system_lib}"
8686

8787
## Create output (bin file)
8888
recipe.objcopy.bin.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.bin"

system/libstm32l4_dragonfly/stm32l4_clib.c

Lines changed: 88 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@
2929
#include <errno.h>
3030
#include <stdint.h>
3131
#include <stdbool.h>
32+
#include <stdio.h>
3233
#include <sys/stat.h>
3334
#include <sys/times.h>
3435
#include <sys/unistd.h>
3536

3637
#include "armv7m.h"
3738
#include "stm32l4_usbd_cdc.h"
3839

40+
int (*stm32l4_stdio_put)(char, FILE*) = NULL;
41+
int (*stm32l4_stdio_get)(FILE*) = NULL;
42+
3943
#undef errno
4044
extern int errno;
4145

@@ -110,9 +114,32 @@ int _lseek(int file, int offset, int whence)
110114

111115
int _read(int file, char *buf, int nbytes)
112116
{
117+
int c, n;
118+
113119
switch (file) {
114120
case STDIN_FILENO:
115-
return 0;
121+
n = 0;
122+
123+
if (nbytes != 0)
124+
{
125+
if (stm32l4_stdio_get != NULL)
126+
{
127+
do
128+
{
129+
c = (*stm32l4_stdio_get)(stdin);
130+
131+
if (c == -1)
132+
{
133+
break;
134+
}
135+
136+
buf[n++] = c;
137+
nbytes--;
138+
}
139+
while (nbytes != 0);
140+
}
141+
}
142+
return n;
116143

117144
default:
118145
errno = EBADF;
@@ -122,62 +149,87 @@ int _read(int file, char *buf, int nbytes)
122149

123150
int _write(int file, char *buf, int nbytes)
124151
{
152+
int n;
153+
125154
switch (file) {
126155
case STDOUT_FILENO:
127156
case STDERR_FILENO:
128-
if (stm32l4_usbd_cdc.state == USBD_CDC_STATE_NONE)
129-
{
130-
stm32l4_usbd_cdc_create(&stm32l4_usbd_cdc);
131-
}
157+
n = 0;
132158

133-
if (stm32l4_usbd_cdc.state == USBD_CDC_STATE_INIT)
159+
if (nbytes != 0)
134160
{
135-
stm32l4_usbd_cdc_enable(&stm32l4_usbd_cdc, 0, NULL, NULL, 0);
136-
}
137-
138-
if (stm32l4_usbd_cdc_connected(&stm32l4_usbd_cdc))
139-
{
140-
if (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc))
161+
if (stm32l4_stdio_put != NULL)
141162
{
142-
if (armv7m_core_priority() <= (int)NVIC_GetPriority(OTG_FS_IRQn))
163+
do
143164
{
144-
while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc))
165+
if (!(*stm32l4_stdio_put)(buf[n], stdout))
145166
{
146-
stm32l4_usbd_cdc_poll(&stm32l4_usbd_cdc);
167+
break;
147168
}
169+
170+
n++;
171+
nbytes--;
148172
}
149-
else
173+
while (nbytes != 0);
174+
}
175+
else
176+
{
177+
if (stm32l4_usbd_cdc.state == USBD_CDC_STATE_NONE)
150178
{
151-
while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc))
152-
{
153-
armv7m_core_yield();
154-
}
179+
stm32l4_usbd_cdc_create(&stm32l4_usbd_cdc);
155180
}
156-
}
157181

158-
stm32l4_usbd_cdc_transmit(&stm32l4_usbd_cdc, (const uint8_t*)buf, nbytes);
159-
160-
if (armv7m_core_priority() <= (int)NVIC_GetPriority(OTG_FS_IRQn))
161-
{
162-
while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc))
182+
if (stm32l4_usbd_cdc.state == USBD_CDC_STATE_INIT)
163183
{
164-
stm32l4_usbd_cdc_poll(&stm32l4_usbd_cdc);
184+
stm32l4_usbd_cdc_enable(&stm32l4_usbd_cdc, 0, NULL, NULL, 0);
165185
}
166-
}
167-
else
168-
{
169-
while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc))
186+
187+
if (stm32l4_usbd_cdc_connected(&stm32l4_usbd_cdc))
170188
{
171-
armv7m_core_yield();
189+
if (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc))
190+
{
191+
if (armv7m_core_priority() <= (int)NVIC_GetPriority(OTG_FS_IRQn))
192+
{
193+
while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc))
194+
{
195+
stm32l4_usbd_cdc_poll(&stm32l4_usbd_cdc);
196+
}
197+
}
198+
else
199+
{
200+
while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc))
201+
{
202+
armv7m_core_yield();
203+
}
204+
}
205+
}
206+
207+
stm32l4_usbd_cdc_transmit(&stm32l4_usbd_cdc, (const uint8_t*)buf, nbytes);
208+
209+
if (armv7m_core_priority() <= (int)NVIC_GetPriority(OTG_FS_IRQn))
210+
{
211+
while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc))
212+
{
213+
stm32l4_usbd_cdc_poll(&stm32l4_usbd_cdc);
214+
}
215+
}
216+
else
217+
{
218+
while (!stm32l4_usbd_cdc_done(&stm32l4_usbd_cdc))
219+
{
220+
armv7m_core_yield();
221+
}
222+
}
172223
}
224+
225+
n = nbytes;
173226
}
174227
}
175-
176-
return nbytes;
228+
return n;
177229

178230
default:
179-
errno = EBADF;
180-
return -1;
231+
errno = EBADF;
232+
return -1;
181233
}
182234
}
183235

5.65 KB
Binary file not shown.

0 commit comments

Comments
 (0)