android
2024-06-25 aa38e5c1f48e31213ee349aa5cd6f06c85bda70d
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
168
169
170
171
172
173
/**
  ******************************************************************************
  * File Name          : TIM.c
  * Description        : This file provides code for the configuration
  *                      of the TIM instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
#include "tim.h"
#include "board_common.h"
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
TIM_HandleTypeDef htim2;
 
/* TIM2 init function */
void Board_TIM2Init(void)
{
    TIM_ClockConfigTypeDef sClockSourceConfig = {0};
    TIM_MasterConfigTypeDef sMasterConfig = {0};
 
    htim2.Instance = TIM2;
    htim2.Init.Prescaler = 72 -1; /* 72MHz */
    htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim2.Init.Period = 1000 -1;
    htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
    if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
    {
        Error_Handler();
    }
    sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
    if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
    {
        Error_Handler();
    }
    sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
    if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
    {
        Error_Handler();
    }
 
    while (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK);
    
    //while (HAL_TIM_Base_Start(&htim2) != HAL_OK);
    
}
 
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
 
    if(tim_baseHandle->Instance==TIM2)
    {
        /* USER CODE BEGIN TIM2_MspInit 0 */
 
        /* USER CODE END TIM2_MspInit 0 */
        /* TIM2 clock enable */
        __HAL_RCC_TIM2_CLK_ENABLE();
 
        /* TIM2 interrupt Init */
        HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(TIM2_IRQn);
    }
}
 
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
 
    if(tim_baseHandle->Instance==TIM2)
    {
        /* USER CODE BEGIN TIM2_MspDeInit 0 */
 
        /* USER CODE END TIM2_MspDeInit 0 */
        /* Peripheral clock disable */
        __HAL_RCC_TIM2_CLK_DISABLE();
        
        /* TIM2 interrupt Deinit */
        HAL_NVIC_DisableIRQ(TIM2_IRQn);
    }
 
#define HEARTBEAT_PERIOD 1000//1260
static void SystemRunLed(void)
{
    static uint8_t u8Phase = 0;
    static uint32_t u32Timeout = 0;
    static bool bOneTimeFlag = false;
    
    if (!bOneTimeFlag)
    {
        u32Timeout = jiffies+60;
        bOneTimeFlag = true;
        Board_TurnLed(LED_RUN, TURN_ON);
    }
    
    switch (u8Phase)
    {
        case 0:
            if (Timeout_Happened(u32Timeout)) /* turn on 70ms */
            {
                Board_TurnLed(LED_RUN, TURN_OFF);
                u32Timeout = jiffies+(HEARTBEAT_PERIOD/4-60);
                u8Phase++;
            }
            break;
        case 1:
            if (Timeout_Happened(u32Timeout)) /* turn off (HEARTBEAT_PERIOD/4-70)ms */
            {
                Board_TurnLed(LED_RUN, TURN_ON);
                u32Timeout = jiffies+60;
                u8Phase++;
            }
            break;
        case 2:            
            if (Timeout_Happened(u32Timeout)) /* turn on 70ms */
            {
                Board_TurnLed(LED_RUN, TURN_OFF);
                u32Timeout = jiffies+(HEARTBEAT_PERIOD-HEARTBEAT_PERIOD/4-70);
                u8Phase++;
            }
        
            break;
        case 3:
            if (Timeout_Happened(u32Timeout)) /* turn off (HEARTBEAT_PERIOD-HEARTBEAT_PERIOD/4-70)ms */
            {
                Board_TurnLed(LED_RUN, TURN_ON);                
                u32Timeout = jiffies+60;
                u8Phase = 0;
            }
            break;
        default:
            break;
    }
}
 
volatile uint32_t jiffies = 0;
void msTimingDelay_Decrement(void)
{    
    jiffies++;  /* 1ms tick */
}
 
/* TIM2¶¨Ê±Öжϴ¦Àí */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if (htim->Instance == TIM1) 
    {
        HAL_IncTick();
    }
    
    if (htim->Instance == TIM2) 
    {
        msTimingDelay_Decrement();
        ADC_Sample();
        SystemRunLed();
    }
}
 
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/