This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
forked from libhal-google/libhal-soft
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ 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
1 parent
1f66ead
commit 9a547cd
Showing
13 changed files
with
676 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+2.32 MB
demos/seleae_captures/bit_bang_i2c_100kHz_capture_without_calibration.sal
Binary file not shown.
Binary file not shown.
Oops, something went wrong.