-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMachineControlPCB_LED.c
167 lines (145 loc) · 4.9 KB
/
MachineControlPCB_LED.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*-----------------------------------------------------------------------------
* Name: LED_32F401Discovery.c
* Purpose: LED interface for STM32F429 Discovery Kit
* Rev.: 1.00
*----------------------------------------------------------------------------*/
/* Copyright (c) 2013 - 2014 ARM LIMITED
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of ARM nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
*
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "MachineControlPCB_LED.h"
/* GPIO Pin identifier */
typedef struct _GPIO_PIN {
GPIO_TypeDef *port;
uint16_t pin;
} GPIO_PIN;
/* LED GPIO Pins */
static const GPIO_PIN LED_PIN[] = {
{ GPIOC, GPIO_PIN_4},
{ GPIOI, GPIO_PIN_10},
{ GPIOB, GPIO_PIN_8}
};
#define LED_COUNT (sizeof(LED_PIN)/sizeof(GPIO_PIN))
/**
\fn int32_t LED_Initialize (void)
\brief Initialize LEDs
\returns
- \b 0: function succeeded
- \b -1: function failed
*/
int32_t LED_Initialize (void) {
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct2;
GPIO_InitTypeDef GPIO_InitStruct3;
/* GPIO Ports Clock Enable */
__GPIOC_CLK_ENABLE();
/* Configure GPIO pins: PG13 PG14 */
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* GPIO Ports Clock Enable */
__GPIOI_CLK_ENABLE();
/* Configure GPIO pins: PG13 PG14 */
GPIO_InitStruct2.Pin = GPIO_PIN_10;
GPIO_InitStruct2.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct2.Pull = GPIO_PULLDOWN;
GPIO_InitStruct2.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct2);
/* GPIO Ports Clock Enable */
__GPIOB_CLK_ENABLE();
/* Configure GPIO pins: PG13 PG14 */
GPIO_InitStruct3.Pin = GPIO_PIN_8;
GPIO_InitStruct3.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct3.Pull = GPIO_PULLDOWN;
GPIO_InitStruct3.Speed = GPIO_SPEED_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct3);
return 0;
}
/**
\fn int32_t LED_Uninitialize (void)
\brief De-initialize LEDs
\returns
- \b 0: function succeeded
- \b -1: function failed
*/
int32_t LED_Uninitialize (void) {
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_4);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8);
HAL_GPIO_DeInit(GPIOI, GPIO_PIN_10);
return 0;
}
/**
\fn int32_t LED_On (uint32_t num)
\brief Turn on requested LED
\param[in] num LED number
\returns
- \b 0: function succeeded
- \b -1: function failed
*/
int32_t LED_On (uint32_t num) {
HAL_GPIO_WritePin(LED_PIN[num].port, LED_PIN[num].pin, GPIO_PIN_SET);
return 0;
}
/**
\fn int32_t LED_Off (uint32_t num)
\brief Turn off requested LED
\param[in] num LED number
\returns
- \b 0: function succeeded
- \b -1: function failed
*/
int32_t LED_Off (uint32_t num) {
HAL_GPIO_WritePin(LED_PIN[num].port, LED_PIN[num].pin, GPIO_PIN_RESET);
return 0;
}
/**
\fn int32_t LED_SetOut (uint32_t val)
\brief Write value to LEDs
\param[in] val value to be displayed on LEDs
\returns
- \b 0: function succeeded
- \b -1: function failed
*/
int32_t LED_SetOut (uint32_t val) {
uint32_t n;
for (n = 0; n < LED_COUNT; n++)
{
if (val & (1 << n))
LED_On (n);
else
LED_Off(n);
}
return 0;
}
/**
\fn uint32_t LED_GetCount (void)
\brief Get number of LEDs
\return Number of available LEDs
*/
uint32_t LED_GetCount (void) {
return LED_COUNT;
}