-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
256 lines (218 loc) · 7.17 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
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
#include "include.h"
// These are defined by the linker (see F28335.cmd)
extern Uint16 RamfuncsLoadStart;
extern Uint16 RamfuncsLoadEnd;
extern Uint16 RamfuncsRunStart;
extern Uint16 RamfuncsLoadSize;
static Uint16 cnt_500ms = 0;
//static Uint16 current_normal_cnt = 0,current_fault_cnt = 0, cnt_current = 0;
//#define TERMINAL_DEBUG
__interrupt void time0_isr(void);
void PowerUpReset(void)
{
Uint16 Temp_WD = 0;
if((SysCtrlRegs.WDCR & 0x80) == 0)//如果是上电复位
{
EALLOW;
Temp_WD = SysCtrlRegs.SCSR;
SysCtrlRegs.SCSR = (Temp_WD & 0xFE);//启动看门狗复位
SysCtrlRegs.WDCR = 0x00B8;
EDIS;
}
else
{
EALLOW;
SysCtrlRegs.WDCR = 0x00E8;//关闭看门狗
EDIS;
}
}
int main(void) {
PowerUpReset();
// Step 1. Initialize System Control:
InitSysCtrl();
// Step 2. Initialize GPIO:
LedInit();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2833x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2833x_DefaultIsr.c.
// This function is found in DSP2833x_PieVect.c.
InitPieVectTable();
#ifdef _FLASH
// Copy time critical code and Flash setup code to RAM
// This includes the following ISR functions: epwm1_timer_isr(), epwm2_timer_isr()
// epwm3_timer_isr and and InitFlash();
// The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
// symbols are created by the linker. Refer to the F28335.cmd file.
memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (Uint32)&RamfuncsLoadSize);
// Call Flash Initialization to setup flash waitstates
// This function must reside in RAM
InitFlash();
#endif
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TINT0 = &time0_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Step 4. Initialize the Device Peripheral.
//-Timer1 initialize
InitCpuTimers();
//-Configure CPU-Timer 0, 1, and 2 to interrupt every second:
//-100MHz CPU Freq, 1 second Period (in uSeconds)
ConfigCpuTimer(&CpuTimer0, 150, 5000);//5ms
//-XINTF Module Initialize for AD2S1210
Init_Zone();
//-ADC Module Init
Steering_ADC_Init();
Steering_ADC_EPwm();
// Steerint_ADC_DMA_Init();
//-Init the ePWM
BLDC_IO_Init();
InitEPwmBLDC();
Control_Init();
//-SCI Module Initialize for 422
Steering_SCIB_Init();
ExternalInterruptInit();
// Step 5. User specific code, enable interrupts:
//-Initialize the variable
StateMachine_Init(); // state machine initial
// initial the ad2s1210
AD2S1210_Init();
//-System Check
SelfCheck();
// send_test1();
// To ensure precise timing, use write-only instructions to write to the entire register. Therefore, if any
// of the configuration bits are changed in ConfigCpuTimer and InitCpuTimers (in DSP2833x_CpuTimers.h), the
// below settings must also be updated.
CpuTimer0Regs.TCR.all = 0x4000; // Use write-only instruction to set TSS bit = 0
PieCtrlRegs.PIEIER1.bit.INTx4 = 1;
// Enable ADCINT in PIE
PieCtrlRegs.PIEIER1.bit.INTx6 = 1;
// Enable TINT0 in the PIE: Group 1 interrupt 7
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
// Enable CPU int1 which is connected to CPU-Timer 0
IER |= M_INT1;
EnableInterrupts();
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
// Step 6. IDLE loop. Just sit and loop forever (optional):
while(1)
{
if (0xAABB == TelemetrySendFlag)
{
BLDC_TelemetrySend();
TelemetrySendFlag = 0;
}
if (0xABCD == Time5msSendFlag)
{
BLDC_CycleSend500ms();
Time5msSendFlag = 0;
}
#ifdef TERMINAL_DEBUG
if (0x1234 == TimeDQCurrentSendFlag)
{
// BLDC_TreePhaseCurrent();
// BLDC_FeedbackCurrent();
// if (0x1234 == update_flag)
// {
// BLDC_TreePhaseCurrentTest(CurrentA[cnt_current], CurrentB[cnt_current], CurrentC[cnt_current]);
// BLDC_FeedbackCurrentTest(CurrentA[cnt_current], CurrentB[cnt_current]);
// cnt_current = (cnt_current + 1) % 500;
// if (0 == cnt_current)
// {
// update_flag = 0xABCD;
// }
// }
TimeDQCurrentSendFlag = 0;
}
#endif
if (SELFCHECK == m2d_Messege.Commond)
{
BLDC_SelfCheck();
m2d_Messege.Commond = 0;
}
else if (DELIVERRY == m2d_Messege.Commond)
{
CommandResponse(DELIVERRY_REACT);
m2d_Messege.Commond = 0;
}
else if (RESET == m2d_Messege.Commond)
{
CommandResponse(RESET_REACT);
m2d_Messege.Commond = 0;
}
else if (APROXMT_ZERO == m2d_Messege.Commond)
{
CommandResponse(APROXMT_ZERO_REACT);
m2d_Messege.Commond = 0;
}
else
{
// do nothing
}
}
}
__interrupt void time0_isr(void)
{
// Insert ISR Code here
// BLDC_RotateTurnControl(d2m_Messege.ControlPhaseState);
// BLDC_RotateTurnControlPro(d2m_Messege.ControlPhaseState);
// BLDC_RotateTurnControlPosition(d2m_Messege.ControlPhaseState);
// if (0xABCD == CurrentOverLoad)
// {
// if ( (d2m_Messege.MotorDriver_IA > 13.0f)
// || (d2m_Messege.MotorDriver_IB > 13.0f)
// || (d2m_Messege.MotorDriver_IC > 13.0f))
// {
// current_fault_cnt++;
// }
// else
// {
// current_normal_cnt++;
// }
// if (current_fault_cnt >= 2)
// {
// BLDC_Stop();
// LedErr();
// CurrentOverLoad = 0;
// }
// if (current_normal_cnt >= 10)
// {
// CurrentOverLoad = 0;
// }
// }
// else
// {
// current_fault_cnt = 0;
// current_normal_cnt = 0;
// }
#ifdef TERMINAL_DEBUG
cnt_500ms = (cnt_500ms + 1) % 10;
if(1 == cnt_500ms)
{
TimeDQCurrentSendFlag = 0x1234;
}
#else
cnt_500ms = (cnt_500ms + 1) % 100;
#endif
if(0 == cnt_500ms)
{
LedRunning();
Time5msSendFlag = 0xABCD;
}
// To receive more interrupts from this PIE group, acknowledge this interrupt
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}