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

Commit

Permalink
✨ Add Input Pins (#14)
Browse files Browse the repository at this point in the history
* ✨
Add Input Pins

* 🔖 Add 1.0.0.yml
  • Loading branch information
FluffyFTW authored Mar 27, 2024
1 parent 94aa0be commit a405803
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/0.0.1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ jobs:
uses: ./.github/workflows/deploy-version.yml
with:
version: 0.0.1
secrets: inherit
secrets: inherit

12 changes: 12 additions & 0 deletions .github/workflows/1.0.0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: 🚀 Release 1.0.0

on:
workflow_dispatch:

jobs:
deploy:
uses: ./.github/workflows/deploy-version.yml
with:
version: 1.0.0
secrets: inherit

1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ libhal_test_and_make_library(
src/output_pin.cpp
src/pin.cpp
src/power.cpp
src/input_pin.cpp

TEST_SOURCES
tests/output_pin.test.cpp
Expand Down
1 change: 1 addition & 0 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ project(demos LANGUAGES CXX)
libhal_build_demos(
DEMOS
blinker
button

PACKAGES
libhal-stm32f4
Expand Down
2 changes: 2 additions & 0 deletions demos/applications/blinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <libhal-util/serial.hpp>
#include <libhal-util/steady_clock.hpp>

// TODO(#15): Replace with `hal::cortex_m::dwt_counter`

void delay_by_cycles(int p_cycles)
{
volatile int i = 0;
Expand Down
46 changes: 46 additions & 0 deletions demos/applications/button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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-stm32f4/input_pin.hpp>
#include <libhal-stm32f4/output_pin.hpp>
#include <libhal-util/steady_clock.hpp>
#include <libhal/units.hpp>

// TODO(#15): Replace with `hal::cortex_m::dwt_counter`

void delay_by_cycles(int p_cycles)
{
volatile int i = 0;
while (i < p_cycles) {
i = i + 1;
}
}

void application()
{
hal::stm32f4::input_pin button(hal::stm32f4::peripheral::gpio_c,
13,
{ .resistor = hal::pin_resistor::pull_up });
hal::stm32f4::output_pin led(hal::stm32f4::peripheral::gpio_a, 5);
bool led_val = false;
while (true) {
if (!button.level()) {
led_val = !led_val;
}
led.level(led_val);
delay_by_cycles(200000);
}
}
2 changes: 1 addition & 1 deletion demos/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ class demos(ConanFile):
def requirements(self):
bootstrap = self.python_requires["libhal-bootstrap"]
bootstrap.module.add_demo_requirements(self, is_platform=True)
self.requires("libhal-stm32f4/[>=0.0.1]")
self.requires("libhal-stm32f4/[^1.0.0 || latest]")
53 changes: 53 additions & 0 deletions include/libhal-stm32f4/input_pin.hpp
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.

#pragma once

#include <libhal/input_pin.hpp>

#include "pin.hpp"

namespace hal::stm32f4 {
/**
* @brief Input pin implementation for the stm32f4
*
*/
class input_pin : public hal::input_pin
{
public:
/**
* @brief Construct a new input pin object
*
* @param p_port - selects pin port to use
* @param p_pin - selects pin within the port to use
* @param p_settings - initial pin settings
*/
input_pin(hal::stm32f4::peripheral p_port,
std::uint8_t p_pin,
const input_pin::settings& p_settings = {});

input_pin(input_pin& p_other) = delete;
input_pin& operator=(input_pin& p_other) = delete;
input_pin(input_pin&& p_other) noexcept = delete;
input_pin& operator=(input_pin&& p_other) noexcept = delete;
~input_pin() = default;

private:
void driver_configure(const settings& p_settings) override;
bool driver_level() override;

hal::stm32f4::peripheral m_port{};
uint8_t m_pin{};
};
} // namespace hal::stm32f4
56 changes: 56 additions & 0 deletions src/input_pin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 <cstdint>

#include <libhal-stm32f4/input_pin.hpp>

#include "gpio_reg.hpp"
#include "power.hpp"
#include <libhal-util/bit.hpp>

#include "gpio_reg.hpp"

namespace hal::stm32f4 {
input_pin::input_pin(hal::stm32f4::peripheral p_port,
std::uint8_t p_pin,
const settings& p_settings)
: m_port(p_port)
, m_pin(p_pin)
{
power(p_port).on();
input_pin::driver_configure(p_settings);
}

void input_pin::driver_configure(const settings& p_settings)
{
bit_mask pin_mode_mask = { .position = 2 * static_cast<uint32_t>(m_pin),
.width = 2 };

bit_modify(get_reg(m_port)->pin_mode).clear(pin_mode_mask);

pin(m_port, m_pin)
.function(pin::pin_function::input)
.open_drain(false)
.resistor(p_settings.resistor);
}

bool input_pin::driver_level()
{
bit_mask input_data_mask = { .position = static_cast<uint32_t>(m_pin),
.width = 1 };
auto pin_value = bit_extract(input_data_mask, get_reg(m_port)->input_data);
return static_cast<bool>(pin_value);
}
} // namespace hal::stm32f4

0 comments on commit a405803

Please sign in to comment.