blob: 05a06e991eea92175de423e50b54d401c201cd57 [file] [log] [blame]
Varun Sharma8f6dbe52021-04-01 21:21:54 -07001// Copyright 2021 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_board_led/led.h"
16
17#include <cinttypes>
18
Varun Sharmae361b532021-04-29 08:54:54 -070019#include "stm32cube/stm32cube.h"
Varun Sharma8f6dbe52021-04-01 21:21:54 -070020
21namespace pw::board_led {
22
23#define _CAT(A, B, C) A##B##C
24#define CAT(A, B, C) _CAT(A, B, C)
25
26#define LED_PORT CAT(GPIO, LED_PORT_CHAR, )
27#define LED_PIN CAT(GPIO_PIN_, LED_PIN_NUM, )
28
29// This is hacky, but works. It is needed because there is no function to
30// initialize an arbitrary GPIO port.
31#define LED_PORT_ENABLE CAT(__HAL_RCC_GPIO, LED_PORT_CHAR, _CLK_ENABLE)
32
33void Init() {
34 GPIO_InitTypeDef GPIO_InitStruct = {};
35
36 LED_PORT_ENABLE();
37
38 HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET);
39
40 GPIO_InitStruct.Pin = LED_PIN;
41 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
42 GPIO_InitStruct.Pull = GPIO_NOPULL;
43 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
44 HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
45}
46
47void TurnOff() { HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_RESET); }
48
49void TurnOn() { HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET); }
50
51void Toggle() { HAL_GPIO_TogglePin(LED_PORT, LED_PIN); }
52
53} // namespace pw::board_led