blob: c9f9809d7a596cf2f71a72da693320db6f7848f4 [file] [log] [blame]
Chris Mumford6ec2deb2022-10-07 12:21:52 +00001// Copyright 2022 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#include "pw_digital_io_stm32cube/digital_io.h"
16
17#include "pw_assert/assert.h"
18#include "pw_status/status.h"
19
20namespace pw::digital_io {
21
22namespace {
23
Anthony DiGirolamo0c1e0ce2024-02-13 21:03:05 +000024void InitGpio(Stm32CubeConfig config) {
Chris Mumford6ec2deb2022-10-07 12:21:52 +000025 GPIO_InitTypeDef init_data = {
Anthony DiGirolamo0c1e0ce2024-02-13 21:03:05 +000026 .Pin = config.pin,
Chris Mumford6ec2deb2022-10-07 12:21:52 +000027 .Mode = GPIO_MODE_OUTPUT_PP,
28 .Pull = GPIO_NOPULL,
29 .Speed = GPIO_SPEED_FREQ_LOW,
30 };
Anthony DiGirolamo0c1e0ce2024-02-13 21:03:05 +000031 HAL_GPIO_WritePin(config.port, config.pin, GPIO_PIN_RESET);
32 HAL_GPIO_Init(config.port, &init_data);
Chris Mumford6ec2deb2022-10-07 12:21:52 +000033}
34
35} // namespace
36
Anthony DiGirolamo0c1e0ce2024-02-13 21:03:05 +000037Stm32CubeDigitalOut::Stm32CubeDigitalOut(Stm32CubeConfig config)
38 : config_(config) {}
Chris Mumford6ec2deb2022-10-07 12:21:52 +000039
40// pw::digital_io::DigitalOut implementation:
41Status Stm32CubeDigitalOut::DoEnable(bool enable) {
42 if (!enable) {
43 // Doesn't seem to be supported by the SDK.
44 return Status::Unavailable();
45 }
Anthony DiGirolamo0c1e0ce2024-02-13 21:03:05 +000046 InitGpio(config_);
Chris Mumford6ec2deb2022-10-07 12:21:52 +000047 return OkStatus();
48}
49
50Status Stm32CubeDigitalOut::DoSetState(State level) {
51 HAL_GPIO_WritePin(
Anthony DiGirolamo0c1e0ce2024-02-13 21:03:05 +000052 config_.port,
53 config_.pin,
54 config_.LogicalToPhysical(level) ? GPIO_PIN_SET : GPIO_PIN_RESET);
Chris Mumford6ec2deb2022-10-07 12:21:52 +000055 return OkStatus();
56}
57
58} // namespace pw::digital_io