Skip to content

Commit b37e400

Browse files
committed
piolib: Make piolib a first class library
Reorganise the management of PIO chips so that piolib can be built as a library, adding the necessary install rules for the library and header files. Signed-off-by: Phil Elwell <[email protected]>
1 parent 1050bf1 commit b37e400

File tree

5 files changed

+66
-16
lines changed

5 files changed

+66
-16
lines changed

piolib/CMakeLists.txt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,30 @@ add_subdirectory(examples)
77
#set project name
88
project(piolib)
99

10+
add_compile_definitions(LIBRARY_BUILD=1)
11+
1012
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror")
1113

1214
if (CMAKE_COMPILER_IS_GNUCC)
1315
add_definitions (-ffunction-sections)
1416
endif ()
1517

16-
add_library (pio OBJECT piolib.c pio_rp1.c)
17-
target_include_directories(pio PRIVATE include)
18-
target_link_libraries(pio)
18+
add_library (pio STATIC piolib.c library_piochips.c pio_rp1.c)
19+
target_include_directories(pio PUBLIC include)
20+
21+
set(INCLUDE_FILES
22+
"piolib.h"
23+
"pio_platform.h"
24+
"hardware/clocks.h"
25+
"hardware/gpio.h"
26+
"hardware/pio.h"
27+
"hardware/pio_instructions.h"
28+
"hardware/timer.h"
29+
"pico/stdlib.h"
30+
)
31+
32+
install(TARGETS pio ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
33+
foreach ( file ${INCLUDE_FILES} )
34+
get_filename_component( dir ${file} DIRECTORY )
35+
install( FILES "include/${file}" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/piolib/${dir}" )
36+
endforeach()

piolib/include/piolib_priv.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,22 @@
99

1010
#include "pio_platform.h"
1111

12+
#define PIO_CHIP(name) name ## _pio_chip
13+
14+
#if LIBRARY_BUILD
15+
#define DECLARE_PIO_CHIP(chip) \
16+
const PIO_CHIP_T chip ## _pio_chip =
17+
18+
extern const PIO_CHIP_T *const library_piochips[];
19+
extern const int library_piochips_count;
20+
#else
1221
#define DECLARE_PIO_CHIP(chip) \
13-
const PIO_CHIP_T *__ptr_ ## chip __attribute__ ((section ("piochips"))) __attribute__ ((used)) = \
14-
&chip
22+
const PIO_CHIP_T PIO_CHIP(chip); \
23+
const PIO_CHIP_T *__ptr_ ## chip __attribute__ ((section ("piochips"))) __attribute__ ((used)) = &PIO_CHIP(chip); \
24+
const PIO_CHIP_T PIO_CHIP(chip) =
25+
26+
extern const PIO_CHIP_T *__start_piochips;
27+
extern const PIO_CHIP_T *__stop_piochips;
28+
#endif
1529

1630
#endif

piolib/library_piochips.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "piolib.h"
2+
#include "piolib_priv.h"
3+
4+
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
5+
6+
#define EXTERN_PIO_CHIP(name) extern const PIO_CHIP_T PIO_CHIP(name)
7+
8+
EXTERN_PIO_CHIP(rp1);
9+
10+
const PIO_CHIP_T *const library_piochips[] =
11+
{
12+
&PIO_CHIP(rp1),
13+
};
14+
15+
const int library_piochips_count = ARRAY_SIZE(library_piochips);

piolib/pio_rp1.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ static void rp1_pio_gpio_init(PIO pio, uint pin)
822822
rp1_gpio_set_function(pio, pin, RP1_GPIO_FUNC_PIO);
823823
}
824824

825-
PIO rp1_create_instance(PIO_CHIP_T *chip, uint index)
825+
static PIO rp1_create_instance(PIO_CHIP_T *chip, uint index)
826826
{
827827
char pathbuf[20];
828828
RP1_PIO pio = NULL;
@@ -845,7 +845,7 @@ PIO rp1_create_instance(PIO_CHIP_T *chip, uint index)
845845
return &pio->base;
846846
}
847847

848-
int rp1_open_instance(PIO pio)
848+
static int rp1_open_instance(PIO pio)
849849
{
850850
RP1_PIO rp = (RP1_PIO)pio;
851851
int fd;
@@ -857,13 +857,13 @@ int rp1_open_instance(PIO pio)
857857
return 0;
858858
}
859859

860-
void rp1_close_instance(PIO pio)
860+
static void rp1_close_instance(PIO pio)
861861
{
862862
RP1_PIO rp = (RP1_PIO)pio;
863863
close(rp->fd);
864864
}
865865

866-
static const PIO_CHIP_T rp1_pio_chip = {
866+
DECLARE_PIO_CHIP(rp1) {
867867
.name = "rp1",
868868
.compatible = "raspberrypi,rp1-pio",
869869
.instr_count = RP1_PIO_INSTRUCTION_COUNT,
@@ -970,5 +970,3 @@ static const PIO_CHIP_T rp1_pio_chip = {
970970
.gpio_set_input_enabled = rp1_gpio_set_input_enabled,
971971
.gpio_set_drive_strength = rp1_gpio_set_drive_strength,
972972
};
973-
974-
DECLARE_PIO_CHIP(rp1_pio_chip);

piolib/piolib.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
#define PIO_MAX_INSTANCES 4
2121

22-
extern PIO_CHIP_T *__start_piochips;
23-
extern PIO_CHIP_T *__stop_piochips;
24-
2522
static __thread PIO __pio;
2623

2724
static PIO pio_instances[PIO_MAX_INSTANCES];
@@ -53,6 +50,13 @@ int pio_get_index(PIO pio)
5350

5451
int pio_init(void)
5552
{
53+
#if LIBRARY_BUILD
54+
const PIO_CHIP_T *const *start = &library_piochips[0];
55+
const PIO_CHIP_T *const *end = &library_piochips[0] + library_piochips_count;
56+
#else
57+
const PIO_CHIP_T *const *start = &__start_piochips;
58+
const PIO_CHIP_T *const *end = &__stop_piochips;
59+
#endif
5660
static bool initialised;
5761
const PIO_CHIP_T * const *p;
5862
uint i = 0;
@@ -61,8 +65,9 @@ int pio_init(void)
6165
if (initialised)
6266
return 0;
6367
num_instances = 0;
64-
p = &__start_piochips;
65-
while (p < &__stop_piochips && num_instances < PIO_MAX_INSTANCES)
68+
69+
p = start;
70+
while (p < end)
6671
{
6772
PIO_CHIP_T *chip = *p;
6873
PIO pio = chip->create_instance(chip, i);

0 commit comments

Comments
 (0)