Chris Mumford | 6ec2deb | 2022-10-07 12:21:52 +0000 | [diff] [blame] | 1 | // 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 | |
| 20 | namespace pw::digital_io { |
| 21 | |
| 22 | namespace { |
| 23 | |
Anthony DiGirolamo | 0c1e0ce | 2024-02-13 21:03:05 +0000 | [diff] [blame] | 24 | void InitGpio(Stm32CubeConfig config) { |
Chris Mumford | 6ec2deb | 2022-10-07 12:21:52 +0000 | [diff] [blame] | 25 | GPIO_InitTypeDef init_data = { |
Anthony DiGirolamo | 0c1e0ce | 2024-02-13 21:03:05 +0000 | [diff] [blame] | 26 | .Pin = config.pin, |
Chris Mumford | 6ec2deb | 2022-10-07 12:21:52 +0000 | [diff] [blame] | 27 | .Mode = GPIO_MODE_OUTPUT_PP, |
| 28 | .Pull = GPIO_NOPULL, |
| 29 | .Speed = GPIO_SPEED_FREQ_LOW, |
| 30 | }; |
Anthony DiGirolamo | 0c1e0ce | 2024-02-13 21:03:05 +0000 | [diff] [blame] | 31 | HAL_GPIO_WritePin(config.port, config.pin, GPIO_PIN_RESET); |
| 32 | HAL_GPIO_Init(config.port, &init_data); |
Chris Mumford | 6ec2deb | 2022-10-07 12:21:52 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | } // namespace |
| 36 | |
Anthony DiGirolamo | 0c1e0ce | 2024-02-13 21:03:05 +0000 | [diff] [blame] | 37 | Stm32CubeDigitalOut::Stm32CubeDigitalOut(Stm32CubeConfig config) |
| 38 | : config_(config) {} |
Chris Mumford | 6ec2deb | 2022-10-07 12:21:52 +0000 | [diff] [blame] | 39 | |
| 40 | // pw::digital_io::DigitalOut implementation: |
| 41 | Status Stm32CubeDigitalOut::DoEnable(bool enable) { |
| 42 | if (!enable) { |
| 43 | // Doesn't seem to be supported by the SDK. |
| 44 | return Status::Unavailable(); |
| 45 | } |
Anthony DiGirolamo | 0c1e0ce | 2024-02-13 21:03:05 +0000 | [diff] [blame] | 46 | InitGpio(config_); |
Chris Mumford | 6ec2deb | 2022-10-07 12:21:52 +0000 | [diff] [blame] | 47 | return OkStatus(); |
| 48 | } |
| 49 | |
| 50 | Status Stm32CubeDigitalOut::DoSetState(State level) { |
| 51 | HAL_GPIO_WritePin( |
Anthony DiGirolamo | 0c1e0ce | 2024-02-13 21:03:05 +0000 | [diff] [blame] | 52 | config_.port, |
| 53 | config_.pin, |
| 54 | config_.LogicalToPhysical(level) ? GPIO_PIN_SET : GPIO_PIN_RESET); |
Chris Mumford | 6ec2deb | 2022-10-07 12:21:52 +0000 | [diff] [blame] | 55 | return OkStatus(); |
| 56 | } |
| 57 | |
| 58 | } // namespace pw::digital_io |