-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
266 lines (221 loc) · 5.94 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* main.c
*
* Created on: 2012. 4. 12.
* Author: East
*/
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "CMSIS/stm32f10x.h"
#include "CMSIS/stm32f10x_it.h"
#include "CMSIS/misc.h"
#include "CMSIS/stm32f10x_usart.h"
#include "CMSIS/stm32f10x_i2c.h"
#include "CMSIS/stm32f10x_rcc.h"
#include "CMSIS/stm32f10x_gpio.h"
#include "CMSIS/stm32f10x_exti.h"
#include "CMSIS/stm32f10x_tim.h"
#include "USB/hw_config.h"
#include "USB/Interface/usb_istr.h"
#include "USB/Driver/usb_core.h"
#include "USB/Driver/usb_init.h"
#include "USB/Interface/usb_pwr.h"
#include "motor_encoder.h"
#include "motor_control.h"
#include "i2c.h"
#include "system_timer.h"
#define I2C_CLOCK_SPEED 300000
#define NT_S_DCDM1210_0_I2C_ADRESS 0x07
volatile BOOL hasRebootRequest = TRUE;
volatile BOOL hasSystemError = FALSE;
void UsartRccConfig();
void UsartNvicConfig();
void UsartInit(uint32_t baudrate);
void LedRccConfig();
void LedInit();
void Reboot();
void ApplyUpdatedMotionControllerCommand();
int main()
{
SystemInit();
#ifndef DEBUG
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);
#endif
SystemTimerInit();
LedRccConfig();
UsartRccConfig();
I2cRccConfig();
MotorEncoderRccConfig();
MotorControlTimerRccConfig();
Set_USBClock();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // 선점형 인터럽트
UsartNvicConfig();
I2cNvicConfig();
MotorEncoderNvicConfig();
MotorControlTimerNvicConfig();
USB_Interrupts_Config();
LedInit();
UsartInit(230400);
I2cInit(I2C_CLOCK_SPEED);
MotorEncoderInit();
USB_GPIO_Configuration();
USB_Init();
GPIO_ResetBits(GPIOC, GPIO_Pin_8); // LED on
Delay_ms(3000); //Waiting for NT-S-DCDM1210
int i;
char* accelerationMotorDriveCommand = "<0E000>"; //가감속 설정
I2c2WriteRequest(NT_S_DCDM1210_0_I2C_ADRESS,
(unsigned char*) accelerationMotorDriveCommand,
strlen(accelerationMotorDriveCommand));
for (i = 0; IsI2c2OnGoing() && i < 10; i++)
{
Delay_ms(10);
}
assert_param(IsI2c2Error() == FALSE && "Setting Acceleration Error");
char* deadbandMotorDriveCommand = "<0D000>"; //데드밴드
I2c2WriteRequest(NT_S_DCDM1210_0_I2C_ADRESS,
(unsigned char*) deadbandMotorDriveCommand,
strlen(deadbandMotorDriveCommand));
for (i = 0; IsI2c2OnGoing() && i < 10; i++)
{
Delay_ms(10);
}
assert_param(IsI2c2Error() == FALSE && "Setting Dead Band Error");
char* stopMotorDriveCommand = "<0R0000L0000>"; //정지
I2c2WriteRequest(NT_S_DCDM1210_0_I2C_ADRESS,
(unsigned char*) stopMotorDriveCommand,
strlen(stopMotorDriveCommand));
for (i = 0; IsI2c2OnGoing() && i < 10; i++)
{
Delay_ms(10);
}
assert_param(IsI2c2Error() == FALSE && "Stop Error");
///////////////////////////////////////////////////////////
MotorControlTimerInit();
hasRebootRequest = FALSE;
uint32_t tickCount = GetSystemTickCount();
for (;;)
{
if (hasRebootRequest == TRUE)
{
for (i = 0; i < 100 && IsI2c2OnGoing() == TRUE; i++)
{
GPIOC->ODR ^= GPIO_Pin_8; // LED Blink
Delay_ms(10);
}
Reboot();
}
else
{
ApplyUpdatedMotionControllerCommand();
MotorControlProcess();
}
if (TimeInterval(tickCount) >= 100000)
{
tickCount = GetSystemTickCount();
GPIOC->ODR ^= GPIO_Pin_8; // LED Blink
}
}
return 0;
}
void USART1_IRQHandler()
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint16_t data = USART_ReceiveData(USART1);
if (data == 27)
{
printf("Reboot \r\n");
Reboot();
}
}
}
void USB_LP_CAN1_RX0_IRQHandler()
{
USB_Istr();
}
void assert_failed(char* message, uint8_t* file, uint32_t line)
{
hasSystemError = TRUE;
uint32_t count = 0;
char* resetMotorDriveCommand = "<0r>"; //정지
I2c2WriteRequest(NT_S_DCDM1210_0_I2C_ADRESS,
(unsigned char*) resetMotorDriveCommand,
strlen(resetMotorDriveCommand));
for (;;)
{
printf("%lu // %s : %s(%lu)\r\n", ++count, message, (char*) file, line);
GPIOC->ODR ^= GPIO_Pin_8; // LED Blink
Delay_ms(3000);
}
}
void Reboot()
{
PowerOff();
char* resetMotorDriveCommand = "<0r>"; //정지
I2c2WriteRequest(NT_S_DCDM1210_0_I2C_ADRESS,
(unsigned char*) resetMotorDriveCommand,
strlen(resetMotorDriveCommand));
int i;
for (i = 0; i < 10 && IsI2c2OnGoing() == TRUE; i++)
{
Delay_ms(10);
}
NVIC_SystemReset();
}
void UsartRccConfig(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
}
void LedRccConfig()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
}
void UsartNvicConfig()
{
//USART Interrupt
NVIC_InitTypeDef nvicUsartInit;
nvicUsartInit.NVIC_IRQChannel = USART1_IRQn;
nvicUsartInit.NVIC_IRQChannelPreemptionPriority = 2;
nvicUsartInit.NVIC_IRQChannelSubPriority = 0;
nvicUsartInit.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicUsartInit);
}
void UsartInit(uint32_t baudrate)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USARTx_Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USARTx_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = baudrate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void LedInit()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void _init()
{
}