Skip to content

Commit 4ec3ee4

Browse files
🎉 Created package for libhal linux on the new repo
1 parent 7326ad1 commit 4ec3ee4

26 files changed

+914
-125
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ libhal_test_and_make_library(
2121

2222
SOURCES
2323
src/output_pin.cpp
24+
src/input_pin.cpp
25+
src/i2c.cpp
26+
src/serial.cpp
2427

2528
TEST_SOURCES
2629
tests/output_pin.test.cpp
@@ -33,4 +36,4 @@ libhal_test_and_make_library(
3336
LINK_LIBRARIES
3437
libhal::libhal
3538
libhal::util
36-
)
39+
)

Dockerfile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
FROM --platform=arm64 ubuntu:22.04
2+
3+
RUN apt update && apt upgrade -y
4+
RUN apt install gcc g++ valgrind neofetch git wget python3-pip clang-format software-properties-common locales pkg-config automake autoconf autoconf-archive libtool m4 -y
5+
6+
RUN wget https://apt.llvm.org/llvm.sh
7+
RUN chmod +x llvm.sh
8+
RUN ./llvm.sh 17
9+
RUN apt install libc++-17-dev libc++abi-17-dev -y
10+
11+
RUN add-apt-repository -y ppa:ubuntu-toolchain-r/test
12+
RUN apt install -y build-essential g++-12
13+
RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -
14+
RUN add-apt-repository "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main"
15+
RUN apt-get install clang-tidy-17 -y
16+
RUN python3 -m pip install "conan>=2.2.2" cmake
17+
18+
# Compile gpiod
19+
WORKDIR /opt
20+
RUN git clone https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git
21+
RUN locale-gen "en_US.UTF-8"
22+
WORKDIR /opt/libgpiod
23+
RUN ./autogen.sh --enable-bindings-cxx
24+
RUN make -j
25+
RUN make install
26+
27+
# Configure conan for libhal
28+
RUN conan remote add libhal-trunk https://libhal.jfrog.io/artifactory/api/conan/trunk-conan
29+
RUN conan config install -sf profiles/baremetal/v2 https://github.com/libhal/conan-config.git
30+
RUN conan profile detect --force
31+
# Set profile based on arch x86 or arm64
32+
# RUN if [[ -z "$arg" ]] ; then echo Argument not provided ; else echo Argument is $arg ; fi
33+
RUN conan config install -sf profiles/armv8/linux/ -tf profiles https://github.com/libhal/conan-config.git
34+
35+
# Test by building demos
36+
RUN mkdir /test_libhal
37+
WORKDIR /test_libhal
38+
RUN git clone https://github.com/libhal/libhal-lpc40
39+
WORKDIR /test_libhal/libhal-lpc40
40+
RUN conan config install -sf conan/profiles/v2 -tf profiles https://github.com/libhal/libhal-lpc40.git
41+
RUN conan config install -tf profiles -sf conan/profiles/v1 https://github.com/libhal/arm-gnu-toolchain.git
42+
RUN conan build demos -pr lpc4078 -pr arm-gcc-12.3 -s build_type=MinSizeRel -b missing
43+
44+
RUN mkdir /code
45+
WORKDIR /code
46+
47+
CMD ["/bin/bash"]

conanfile.py

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,22 @@ class libhal_linux_conan(ConanFile):
3333
python_requires = "libhal-bootstrap/[^1.0.0]"
3434
python_requires_extend = "libhal-bootstrap.library"
3535

36-
options = {
37-
"platform": [
38-
"profile1",
39-
"profile2",
40-
"ANY"
41-
],
42-
}
43-
44-
default_options = {
45-
"platform": "ANY",
46-
}
47-
48-
@property
49-
def _use_linker_script(self):
50-
return (self.options.platform == "profile1" or
51-
self.options.platform == "profile2")
52-
53-
def add_linker_scripts_to_link_flags(self):
54-
platform = str(self.options.platform)
55-
self.cpp_info.exelinkflags = [
56-
"-L" + os.path.join(self.package_folder, "linker_scripts"),
57-
"-T" + os.path.join("libhal-linux", platform + ".ld"),
58-
]
59-
6036
def requirements(self):
6137
# Replace with appropriate processor library
62-
self.requires("libhal-armcortex/[^3.0.2]")
38+
self.requires("libhal/[^3.3.0]", transitive_headers=True)
39+
self.requires("libhal-util/[^4.1.0]", transitive_headers=True)
40+
6341

6442
def package_info(self):
6543
self.cpp_info.set_property("cmake_target_name", "libhal::linux")
6644
self.cpp_info.libs = ["libhal-linux"]
6745

68-
if self.settings.os == "baremetal" and self._use_linker_script:
69-
self.add_linker_scripts_to_link_flags()
70-
71-
self.buildenv_info.define("LIBHAL_PLATFORM",
72-
str(self.options.platform))
73-
self.buildenv_info.define("LIBHAL_PLATFORM_LIBRARY",
74-
"linux")
7546

7647
def package_id(self):
7748
if self.info.options.get_safe("platform"):
7849
del self.info.options.platform
50+
51+
self.buildenv_info.define("LIBHAL_PLATFORM",
52+
"linux")
53+
self.buildenv_info.define("LIBHAL_PLATFORM_LIBRARY",
54+
"linux")

datasheets/placeholder.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I am building a (free) operating system, just a small project, not meant to be anything large

demos/CMakeLists.txt

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,29 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
cmake_minimum_required(VERSION 3.15)
15+
cmake_minimum_required(VERSION 3.20)
1616

17-
# Set project name to demos
18-
project(demos LANGUAGES CXX)
17+
project(linux_demos VERSION 0.0.1 LANGUAGES CXX)
1918

20-
libhal_build_demos(
21-
DEMOS
22-
blinker
19+
# Generate compile commands for anyone using our libraries.
20+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2321

24-
PACKAGES
25-
libhal-linux
22+
# Always run this custom target by making it depend on ALL
23+
# add_custom_target(copy_compile_commands ALL
24+
# COMMAND ${CMAKE_COMMAND} -E copy_if_different
25+
# ${CMAKE_BINARY_DIR}/compile_commands.json
26+
# ${CMAKE_SOURCE_DIR}/compile_commands.json
27+
# DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json)
2628

27-
LINK_LIBRARIES
28-
libhal::linux
29-
)
29+
30+
find_package(libhal-linux REQUIRED CONFIG)
31+
32+
set(DEMOS gpio blinker i2c_test uart steady_clock_test)
33+
foreach(DEMO ${DEMOS})
34+
message(STATUS "Generating Demo for \"${PROJECT_NAME}_${DEMO}")
35+
add_executable(${PROJECT_NAME}_${DEMO} main.cpp applications/${DEMO}.cpp)
36+
target_include_directories(${PROJECT_NAME}_${DEMO} PUBLIC .)
37+
target_compile_features(${PROJECT_NAME}_${DEMO} PRIVATE cxx_std_23)
38+
target_link_libraries(${PROJECT_NAME}_${DEMO} PRIVATE libhal::linux -static-libstdc++)
39+
40+
endforeach()

demos/applications/blinker.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
// limitations under the License.
1414

1515
#include <libhal-linux/output_pin.hpp>
16-
#include <libhal-armcortex/dwt_counter.hpp>
16+
#include <libhal-linux/steady_clock.hpp>
1717
#include <libhal-util/steady_clock.hpp>
18+
#include <libhal/steady_clock.hpp>
1819

1920
void application()
2021
{
2122
using namespace hal::literals;
2223
// TODO(libhal-target): Set the correct frequency and output pin driver
23-
hal::cortex_m::dwt_counter clock(1.0_MHz);
24-
hal::linux::output_pin led;
24+
hal::gnu_linux::output_pin led("/dev/gpiochip0", 2);
25+
auto clock = hal::gnu_linux::steady_clock<std::chrono::steady_clock>();
2526

2627
while (true) {
2728
using namespace std::chrono_literals;

demos/applications/gpio.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2024 Khalil Estell
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <iostream>
16+
#include <libhal-linux/input_pin.hpp>
17+
#include <libhal-linux/output_pin.hpp>
18+
#include <libhal/error.hpp>
19+
#include <unistd.h>
20+
21+
void application()
22+
{
23+
auto output_gpio = hal::gnu_linux::output_pin("/dev/gpiochip0", 2);
24+
auto input_gpio = hal::gnu_linux::input_pin("/dev/gpiochip0", 3);
25+
std::cout << "blinking gpio 2 on gpiochip0\n";
26+
bool state = output_gpio.level();
27+
bool saved_state = false;
28+
while (true) {
29+
output_gpio.level(state);
30+
saved_state = output_gpio.level();
31+
std::cout << "current state: " << saved_state << std::endl;
32+
sleep(1);
33+
state ^= 1;
34+
if (!input_gpio.level()) {
35+
std::cout << "quiting, bye bye\n";
36+
break;
37+
}
38+
}
39+
}

demos/applications/i2c_test.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2024 Khalil Estell
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <array>
16+
#include <iostream>
17+
#include <libhal-linux/i2c.hpp>
18+
#include <libhal-util/i2c.hpp>
19+
#include <libhal/error.hpp>
20+
#include <unistd.h>
21+
22+
double rad_to_deg(double rad)
23+
{
24+
return rad * 180 / 3.14;
25+
}
26+
27+
void print_data(std::array<hal::byte, 6>& data)
28+
{
29+
uint16_t x = data[0] << 8 | data[1];
30+
uint16_t y = data[2] << 8 | data[3];
31+
uint16_t z = data[4] << 8 | data[5];
32+
std::cout << "X: " << x << " Y: " << y << " Z: " << z << std::endl;
33+
}
34+
35+
void application()
36+
{
37+
auto bus = hal::gnu_linux::i2c("/dev/i2c-1");
38+
const auto addr = 0x68;
39+
const auto wake_sensor = std::array<hal::byte, 2>{ 0x6B, 0 };
40+
hal::write(bus, addr, wake_sensor);
41+
const auto set_scale = std::array<hal::byte, 2>{ 0xC1, 1 };
42+
while (true) {
43+
auto read_buffer = std::array<hal::byte, 6>{};
44+
auto write_op = std::array<hal::byte, 1>{ 0x3B };
45+
std::cout << "write_then_read:\n";
46+
hal::write_then_read(bus, addr, write_op, read_buffer);
47+
// hal::write(bus, addr, write_op);
48+
// hal::read(bus, addr, read_buffer);
49+
print_data(read_buffer);
50+
}
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2024 Khalil Estell
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <chrono>
16+
#include <iostream>
17+
#include <libhal-linux/steady_clock.hpp>
18+
#include <libhal-util/steady_clock.hpp>
19+
#include <libhal/error.hpp>
20+
#include <unistd.h>
21+
22+
void application()
23+
{
24+
using namespace std::chrono_literals;
25+
using namespace hal::literals;
26+
auto sc = hal::gnu_linux::steady_clock<std::chrono::steady_clock>();
27+
std::cout << "Clock made!\n";
28+
for (int i = 0; i < 10; i++) {
29+
hal::delay(sc, 5s);
30+
std::cout << "Delayed for a second\n";
31+
}
32+
}

demos/applications/uart.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
#include <libhal-linux/serial.hpp>
3+
#include <libhal-util/serial.hpp>
4+
#include <span>
5+
#include <unistd.h>
6+
7+
void application()
8+
{
9+
std::cout << "UART test\n";
10+
auto serial_file_path = "/dev/serial0";
11+
auto serial_bus = hal::gnu_linux::serial(serial_file_path);
12+
std::string test_str = "Hello from libhal\n";
13+
std::array<hal::byte, 255> input_buffer = { 0 };
14+
while (true) {
15+
hal::print(serial_bus, test_str);
16+
sleep(1);
17+
auto read_res = serial_bus.read(input_buffer);
18+
if (input_buffer.at(0) == '\0') {
19+
std::cout << "Nothing to read\n";
20+
continue;
21+
}
22+
std::cout << "Len of res buffer: " << read_res.data.size()
23+
<< " len of input buffer: " << input_buffer.size() << "\n";
24+
auto subspan = read_res.data.subspan(0, read_res.data.size());
25+
auto read_string = std::string(subspan.begin(), subspan.end());
26+
sleep(1);
27+
std::cout << "Read from serial:" << read_string;
28+
}
29+
}

0 commit comments

Comments
 (0)