-
Notifications
You must be signed in to change notification settings - Fork 2
/
wallaby_init.c
318 lines (251 loc) · 10.7 KB
/
wallaby_init.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "wallaby_init.h"
#include "wallaby.h"
#include "stm32f4xx.h"
void init180MHz()
{
RCC_DeInit();
// Enable HSE
RCC_HSEConfig(RCC_HSE_ON);
//RCC_HSEConfig(RCC_HSE_Bypass);
while(RCC_WaitForHSEStartUp() != SUCCESS);
FLASH_PrefetchBufferCmd(ENABLE);
FLASH_SetLatency(FLASH_Latency_5);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div4);
RCC_PCLK2Config(RCC_HCLK_Div2);
uint32_t PLL_M = 24; // ( (24 MHz / 24) * 360 ) / 2 = 180 MHz
uint32_t PLL_N = 360;
uint32_t PLL_P = 2;
uint32_t PLL_Q = 7;
RCC_PLLConfig(RCC_PLLSource_HSE,PLL_M,PLL_N,PLL_P,PLL_Q);
RCC_PLLCmd(ENABLE);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
// Wait till PLL is used as system clock source
while(RCC_GetSYSCLKSource() != 0x08);
}
void init()
{
init180MHz(); // switch over to 180 MHz via the external 24 MHz crystal and a PLL
initSystick(); // get a wallclock started, ticking at 1ms
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE);
// TIM1,8 for Motors
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
// TIM3,9 for Servos
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);
SPI_DMA_Config(BUFFERSIZE);
// Slave board configuration
// Initializes the SPI communication
//SPI_InitTypeDef SPI_InitStructure;
//SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
//SPI_Init(SPI2, &SPI_InitStructure);
DMA_ITConfig(DMA1_Stream3, DMA_IT_TC, ENABLE);
DMA_ITConfig(DMA1_Stream4, DMA_IT_TC, ENABLE);
// Enable DMA SPI TX Stream
DMA_Cmd(DMA1_Stream4,ENABLE);
// Enable DMA SPI RX Stream
DMA_Cmd(DMA1_Stream3,ENABLE);
{
NVIC_InitTypeDef NVIC_InitStructure;
// Enable the TIM1 gloabal Interrupt
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
{
NVIC_InitTypeDef NVIC_InitStructure;
// Enable the TIM1 gloabal Interrupt
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
// Enable SPI DMA TX Requsts
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
// Enable SPI DMA RX Requsts
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);
//debug_printf("enable spi\n");
// Enable the SPI peripheral
SPI_Cmd(SPI2, ENABLE);
// motor 0
configMotorPin(MOT0_DIR1, MOT0_DIR1_PORT);
configMotorPin(MOT0_DIR2, MOT0_DIR2_PORT);
//configMotorPin(MOT0_PWM, MOT0_PWM_PORT);
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = MOT0_PWM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(MOT0_PWM_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(MOT0_PWM_PORT, MOT0_PWM_PinSource, GPIO_AF_TIM1);
}
// motor 1
configMotorPin(MOT1_DIR1, MOT1_DIR1_PORT);
configMotorPin(MOT1_DIR2, MOT1_DIR2_PORT);
//configMotorPin(MOT1_PWM, MOT1_PWM_PORT);
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = MOT1_PWM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(MOT1_PWM_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(MOT1_PWM_PORT, MOT1_PWM_PinSource, GPIO_AF_TIM1);
}
// motor 2
configMotorPin(MOT2_DIR1, MOT2_DIR1_PORT);
configMotorPin(MOT2_DIR2, MOT2_DIR2_PORT);
//configMotorPin(MOT2_PWM, MOT2_PWM_PORT);
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = MOT2_PWM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(MOT2_PWM_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(MOT2_PWM_PORT, MOT2_PWM_PinSource, GPIO_AF_TIM1);
}
// motor 3
configMotorPin(MOT3_DIR1, MOT3_DIR1_PORT);
configMotorPin(MOT3_DIR2, MOT3_DIR2_PORT);
//configMotorPin(MOT3_PWM, MOT3_PWM_PORT);
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = MOT3_PWM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(MOT3_PWM_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(MOT3_PWM_PORT, MOT3_PWM_PinSource, GPIO_AF_TIM8);
}
// motor bemf adc sensing
configBEMFPin(MOT0_BEMF_H, MOT0_BEMF_H_PORT);
configBEMFPin(MOT0_BEMF_L, MOT0_BEMF_L_PORT);
configBEMFPin(MOT1_BEMF_H, MOT1_BEMF_H_PORT);
configBEMFPin(MOT1_BEMF_L, MOT1_BEMF_L_PORT);
configBEMFPin(MOT2_BEMF_H, MOT2_BEMF_H_PORT);
configBEMFPin(MOT2_BEMF_L, MOT2_BEMF_L_PORT);
configBEMFPin(MOT3_BEMF_H, MOT3_BEMF_H_PORT);
configBEMFPin(MOT3_BEMF_L, MOT3_BEMF_L_PORT);
// TODO: cleaner servo init and all 4
// TODO for now just 0,1 on TIM3
// TODO: later, 2,3 on TIM9
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SRV0_PWM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(SRV0_PWM_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(SRV0_PWM_PORT, SRV0_PWM_PinSource, GPIO_AF_TIM3);
}
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SRV1_PWM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(SRV1_PWM_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(SRV1_PWM_PORT, SRV1_PWM_PinSource, GPIO_AF_TIM3);
}
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SRV2_PWM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(SRV2_PWM_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(SRV2_PWM_PORT, SRV2_PWM_PinSource, GPIO_AF_TIM9);
}
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SRV3_PWM;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(SRV3_PWM_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(SRV3_PWM_PORT, SRV3_PWM_PinSource, GPIO_AF_TIM9);
}
TIM1_Configuration(); // motors 0,1,2
// TODO move tim1 interrupt setup for motor control
{
NVIC_InitTypeDef NVIC_InitStructure;
// Enable the TIM1 gloabal Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
TIM8_Configuration(); // motor 3
// TODO move tim8 interrupt setup for motor control
{
NVIC_InitTypeDef NVIC_InitStructure;
// Enable the TIM8 gloabal Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
TIM3_Configuration(); // servos 0,1
// TODO move tim3 interrupt setup for servo control
{
NVIC_InitTypeDef NVIC_InitStructure;
// Enable the TIM3 gloabal Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
TIM9_Configuration(); // servos 2,3
// TODO move tim9 interrupt setup for servo control
{
NVIC_InitTypeDef NVIC_InitStructure;
// Enable the TIM9 gloabal Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
// Button S1 is also a digital in
// TODO: make this another digital pin
configDigitalInPin(BUTTON_S1_PIN, BUTTON_S1_PORT);
// Init SPI3 (plus chip selects) for the IMU sensors
initSPI3();
// Init SPI4 (SPI header)
initSPI4();
setup_I2C1();
// wait a bit
delay_us(5000);
setupAccelMag();
setupGyro();
setupUART2();
setupUART3();
// wait a bit
delay_us(5);
}