-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.c
116 lines (93 loc) · 2.45 KB
/
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
/**
******************************************************************************
* @file : led.c
* @brief : LED driver file
******************************************************************************
*/
#include "led.h"
#include "tim.h"
//-----------------------------------------------------
// Global variables
//-----------------------------------------------------
LED_PTRN_t g_led_pattern;
uint8_t g_pattern_parm;
//-----------------------------------------------------
// Function Definitions
//-----------------------------------------------------
/**
* @attention : Definetions of private functions of the led.c driver module
*/
void led_pattern_statemachine(void);
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
//-----------------------------------------------------
// Public functions
//-----------------------------------------------------
/**
* @brief : This function initiate led driver
* @attention : this also startthe timer interrupt on timer3
*/
void led_init(void)
{
g_led_pattern = LED_PTRN_OFF;
HAL_TIM_Base_Start_IT(&htim3);
}
/**
* @brief : This function sets the led patterns
* @params : patern type & parameters related to the paterns
*/
void led_set_pattern(LED_PTRN_t pattern, uint8_t pattern_parm)
{
g_led_pattern = pattern;
switch(g_led_pattern)
{
case LED_PTRN_BLINK:
g_pattern_parm = pattern_parm * 2;
break;
default:
g_pattern_parm = pattern_parm;
break;
}
}
//-----------------------------------------------------
// Private functions
//-----------------------------------------------------
/**
* @brief : State machine to set the different led paterns
*
*/
void led_pattern_statemachine(void)
{
switch(g_led_pattern)
{
case LED_PTRN_OFF:
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
break;
case LED_PTRN_ON:
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_SET);
break;
case LED_PTRN_BLINK:
if(g_pattern_parm > 0)
{
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_8);
g_pattern_parm--;
}
else
{
g_led_pattern = LED_PTRN_OFF;
}
break;
default:
g_led_pattern = LED_PTRN_OFF;
break;
}
}
/**
* @brief : Timer interrupt callback function provided by HAL layer
*
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM3){
led_pattern_statemachine();
}
}