Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
✨ Add support for i2c bit bang (#7)
Browse files Browse the repository at this point in the history
* ✨ add support for i2c bit bang

- implements read and write operations
- supports speeds up to about 200kHz

* Update include/libhal-soft/i2c_bit_bang.hpp

Co-authored-by: Khalil Estell <[email protected]>

* 📝 ⚡ applied requested changes

* 📝 🥅 Applied additional change requests

---------

Co-authored-by: Khalil Estell <[email protected]>
  • Loading branch information
Coreyboy1820 and kammce authored Mar 19, 2024
1 parent 1f66ead commit 9a547cd
Show file tree
Hide file tree
Showing 13 changed files with 676 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ libhal_test_and_make_library(
LIBRARY_NAME libhal-soft

SOURCES
src/bit_bang_i2c.cpp
src/rc_servo.cpp
src/i2c_minimum_speed.cpp
src/adc_mux.cpp
Expand Down
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def build_requirements(self):

def requirements(self):
self.requires("libhal/[^3.0.0]", transitive_headers=True)
self.requires("libhal-util/[^4.0.0]")
self.requires("libhal-util/[^4.0.1]")

def layout(self):
cmake_layout(self)
Expand Down
29 changes: 29 additions & 0 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2024 Khalil Estell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.15)

project(demos LANGUAGES CXX)

libhal_build_demos(
DEMOS
bit_bang_i2c

PACKAGES
libhal-soft
libhal-stm-imu

LINK_LIBRARIES
libhal::soft
libhal::stm-imu
)
52 changes: 52 additions & 0 deletions demos/applications/bit_bang_i2c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <libhal-soft/bit_bang_i2c.hpp>
#include <libhal-stm-imu/lis3dhtr_i2c.hpp>
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal-util/units.hpp>

#include "../hardware_map.hpp"

void application(hardware_map_t& p_map)
{
using namespace std::chrono_literals;
using namespace hal::literals;

auto& clock = *p_map.clock;
auto& console = *p_map.console;
auto& scl = *p_map.scl;
auto& sda = *p_map.sda;

hal::print(console, "Starting lis3dhtr_i2c Application...\n");
hal::delay(clock, 50ms);

hal::bit_bang_i2c::pins pins{ .sda = &sda, .scl = &scl };

hal::bit_bang_i2c bit_bang_i2c(pins, clock);
bit_bang_i2c.configure(hal::i2c::settings{ .clock_rate = 100.0_kHz });

hal::stm_imu::lis3dhtr_i2c lis(bit_bang_i2c);

while (true) {
hal::delay(clock, 500ms);
auto acceleration = lis.read();
hal::print<128>(console,
"Scale: 2g \t x = %fg, y = %fg, z = %fg \n",
acceleration.x,
acceleration.y,
acceleration.z);
}
}
26 changes: 26 additions & 0 deletions demos/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2024 Khalil Estell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from conan import ConanFile


class demos(ConanFile):
python_requires = "libhal-bootstrap/[^1.0.0]"
python_requires_extend = "libhal-bootstrap.demo"

def requirements(self):
bootstrap = self.python_requires["libhal-bootstrap"]
bootstrap.module.add_demo_requirements(self)
self.requires("libhal-soft/[>=4.0.0]")
self.requires("libhal-stm-imu/[>=1.0.0]")
34 changes: 34 additions & 0 deletions demos/hardware_map.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <libhal/functional.hpp>
#include <libhal/output_pin.hpp>
#include <libhal/serial.hpp>
#include <libhal/steady_clock.hpp>

struct hardware_map_t
{
hal::serial* console;
hal::output_pin* scl;
hal::output_pin* sda;
hal::steady_clock* clock;
hal::callback<void()> reset;
};

// Application function must be implemented by one of the compilation units
// (.cpp) files.
void application(hardware_map_t& p_map);
hardware_map_t initialize_platform();
32 changes: 32 additions & 0 deletions demos/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <libhal/error.hpp>

#include "hardware_map.hpp"

hardware_map_t hardware_map{};

int main()
{
try {
hardware_map = initialize_platform();
} catch (...) {
hal::halt();
}

application(hardware_map);
hardware_map.reset();
return 0;
}
15 changes: 15 additions & 0 deletions demos/platforms/lpc4074.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "lpc4078.cpp"
53 changes: 53 additions & 0 deletions demos/platforms/lpc4078.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 Khalil Estell
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <libhal-armcortex/dwt_counter.hpp>
#include <libhal-armcortex/startup.hpp>
#include <libhal-armcortex/system_control.hpp>

#include <libhal-lpc40/clock.hpp>
#include <libhal-lpc40/constants.hpp>
#include <libhal-lpc40/output_pin.hpp>
#include <libhal-lpc40/uart.hpp>

#include "../hardware_map.hpp"

hardware_map_t initialize_platform()
{
using namespace hal::literals;

// Set the MCU to the maximum clock speed
hal::lpc40::maximum(12.0_MHz);

static hal::cortex_m::dwt_counter counter(
hal::lpc40::get_frequency(hal::lpc40::peripheral::cpu));

static std::array<hal::byte, 64> receive_buffer{};
static hal::lpc40::uart uart0(0,
receive_buffer,
hal::serial::settings{
.baud_rate = 38400,
});

static hal::lpc40::output_pin scl(1, 15); // scl
static hal::lpc40::output_pin sda(1, 23); // sda

return hardware_map_t{
.console = &uart0,
.scl = &scl,
.sda = &sda,
.clock = &counter,
.reset = []() { hal::cortex_m::reset(); },
};
}
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 9a547cd

Please sign in to comment.