-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
205 lines (166 loc) · 5.01 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
**********************************************************************************
* @title main.c
* @platform STM32F107
* @author Anton Novikov
* @version V1.0.0
* @date 13.02.2021
*
* @brief Received CAN messages. It handles timer increments, checks button state.
*
**********************************************************************************
*/
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_can.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
#include "misc.h"
#include "can.h"
#include <stdio.h>
#include <stdbool.h>
void init_timer();
void res_timer(int timer_num, int timer_type);
typedef struct
{
bool run; // Run timer
bool res; // Reset timer
int val; // Current timer value
} tpTimer;
tpTimer timer_1000ms[10] = {{false, false, 0}};
int time_togle_1ms = 0;
int time_togle_1ms_old = 0;
tpTimer timer_10000x100ms[10] = {{false, false, 0}};
int time_togle_100ms = 0;
int time_togle_100ms_old = 0;
// ***************************
const int sendingPeriod = 1; // Period sending time CAN message 100ms
// ***************************
// Current status of parcels
int CAN1_StdOn = 0;
int CAN2_StdOn = 0;
int main(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
SystemInit();
init_timer();
init_CAN();
while (1) {
/* Timer management */
// time_togle_1ms flag is toggled each 1ms
if (time_togle_1ms != time_togle_1ms_old) {
for (int i = 0; i < 10; i++) {
if (timer_1000ms[i].run && timer_1000ms[i].val < 1000) {
timer_1000ms[i].val++;
}
}
time_togle_1ms_old = time_togle_1ms;
}
// time_togle_100ms flag is toggled each 100ms
if (time_togle_100ms != time_togle_100ms_old) {
for (int i = 0; i < 10; i++) {
if (timer_10000x100ms[i].run
&& timer_10000x100ms[i].val < 10000) {
timer_10000x100ms[i].val++;
}
}
time_togle_100ms_old = time_togle_100ms;
}
if (ButtonPress == 1) {
timer_10000x100ms[1].run = true;
}
if (timer_10000x100ms[1].val >= sendingPeriod) {
CAN2_Enable_Surround_View();
timer_10000x100ms[1].run = false;
res_timer(1, 100);
}
// Use the state set by the interrupt.
CAN1_StdOn = CAN1_Std;
CAN2_StdOn = CAN2_Std;
// CAN1 ID 0x050
if ((CAN1_Std != 0)) {
GPIO_SetBits(GPIOB, GPIO_Pin_1);
} else {
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
}
// CAN2 ID 0x040
if ((CAN2_Std != 0)) {
GPIO_SetBits(GPIOB, GPIO_Pin_2);
} else {
GPIO_ResetBits(GPIOB, GPIO_Pin_2);
}
}
}
void res_timer(int timer_num, int timer_type) {
if (timer_type == 1) {
if (timer_num >= 0 && timer_num < 10) {
timer_1000ms[timer_num].res = false;
timer_1000ms[timer_num].val = 0;
}
} else if (timer_type == 100) {
if (timer_num >= 0 && timer_num < 10) {
timer_10000x100ms[timer_num].res = false;
timer_10000x100ms[timer_num].val = 0;
}
}
}
void init_timer() {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseInitTypeDef base_timer;
TIM_TimeBaseStructInit(&base_timer);
base_timer.TIM_Prescaler = (SystemCoreClock / 10000) - 1;
base_timer.TIM_Period = 10;
base_timer.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &base_timer);
base_timer.TIM_Period = 1000;
base_timer.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &base_timer);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM4, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure3;
NVIC_InitStructure3.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure3.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure3.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure3.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure3);
NVIC_InitTypeDef NVIC_InitStructure4;
NVIC_InitStructure4.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure4.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure4.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure4.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure4);
}
void TIM3_IRQHandler() {
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
if (time_togle_1ms == 0) {
time_togle_1ms = 1;
} else {
time_togle_1ms = 0;
}
}
}
void TIM4_IRQHandler() {
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
if (time_togle_100ms == 0) {
time_togle_100ms = 1;
} else {
time_togle_100ms = 0;
}
}
}