1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
25
26
29
30
35
36
39
42
43
46
47
50
61
62
63
66
69
70
73
77
81
82
83
84
85
86
87
88
89
90
91
94
95
98
101
102
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
130
131
134
135
139
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
184
185
186
187
188
189
190
191
192
193
194
198
199
200
201
202
203
204
205
206
207
208
212
213
216
217
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
270
274
275
278
279
282
283
286
287
288
289
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
321
322
323
324
325
326
327
331
332
333
334
335
338
339
340
341
342
343
344
345
346
347
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
397
398
399
400
401
402
403
404
409
410
411
417
418
419
420
421
422
423
424
425
430
431
432
433
437
438
439
440
441
442
443
444
445
446
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
485
486
487
488
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
541
542
543
547
551
552
553
554
555
556
557
558
564
565
566
567
568
569
575
576
577
581
585
586
587
588
589
590
591
592
599
600
601
602
603
604
605
606
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
641
645
646
652
656
657
663
667
668
671
672
675
676
679
680
/* ... */
#include "stm32f4_discovery.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
#define __STM32F4_DISCO_BSP_VERSION_MAIN (0x02)
#define __STM32F4_DISCO_BSP_VERSION_SUB1 (0x01)
#define __STM32F4_DISCO_BSP_VERSION_SUB2 (0x05)
#define __STM32F4_DISCO_BSP_VERSION_RC (0x00)
#define __STM32F4_DISCO_BSP_VERSION ((__STM32F4_DISCO_BSP_VERSION_MAIN << 24)\
|(__STM32F4_DISCO_BSP_VERSION_SUB1 << 16)\
|(__STM32F4_DISCO_BSP_VERSION_SUB2 << 8 )\
|(__STM32F4_DISCO_BSP_VERSION_RC)) ...
5 defines
/* ... */
/* ... */
/* ... */
/* ... */
GPIO_TypeDef* GPIO_PORT[LEDn] = {LED4_GPIO_PORT,
LED3_GPIO_PORT,
LED5_GPIO_PORT,
LED6_GPIO_PORT...};
const uint16_t GPIO_PIN[LEDn] = {LED4_PIN,
LED3_PIN,
LED5_PIN,
LED6_PIN...};
GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT};
const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN};
const uint8_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn};
uint32_t I2cxTimeout = I2Cx_TIMEOUT_MAX;
uint32_t SpixTimeout = SPIx_TIMEOUT_MAX;
static SPI_HandleTypeDef SpiHandle;
static I2C_HandleTypeDef I2cHandle;
/* ... */
/* ... */
/* ... */
/* ... */
static void I2Cx_Init(void);
static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value);
static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg);
static void I2Cx_MspInit(void);
static void I2Cx_Error(uint8_t Addr);
static void SPIx_Init(void);
static void SPIx_MspInit(void);
static uint8_t SPIx_WriteRead(uint8_t Byte);
static void SPIx_Error(void);
void ACCELERO_IO_Init(void);
void ACCELERO_IO_ITConfig(void);
void ACCELERO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite);
void ACCELERO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead);
void AUDIO_IO_Init(void);
void AUDIO_IO_DeInit(void);
void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg);
/* ... */
/* ... */
/* ... */
uint32_t BSP_GetVersion(void)
{
return __STM32F4_DISCO_BSP_VERSION;
}{ ... }
/* ... */
void BSP_LED_Init(Led_TypeDef Led)
{
GPIO_InitTypeDef GPIO_InitStruct;
LEDx_GPIO_CLK_ENABLE(Led);
GPIO_InitStruct.Pin = GPIO_PIN[Led];
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
}{ ... }
/* ... */
void BSP_LED_On(Led_TypeDef Led)
{
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
}{ ... }
/* ... */
void BSP_LED_Off(Led_TypeDef Led)
{
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
}{ ... }
/* ... */
void BSP_LED_Toggle(Led_TypeDef Led)
{
HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
}{ ... }
/* ... */
/* ... */
/* ... */
void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef Mode)
{
GPIO_InitTypeDef GPIO_InitStruct;
BUTTONx_GPIO_CLK_ENABLE(Button);
if (Mode == BUTTON_MODE_GPIO)
{
GPIO_InitStruct.Pin = BUTTON_PIN[Button];
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
}if (Mode == BUTTON_MODE_GPIO) { ... }
if (Mode == BUTTON_MODE_EXTI)
{
GPIO_InitStruct.Pin = BUTTON_PIN[Button];
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0);
HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
}if (Mode == BUTTON_MODE_EXTI) { ... }
}{ ... }
/* ... */
uint32_t BSP_PB_GetState(Button_TypeDef Button)
{
return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */
static void SPIx_Init(void)
{
if(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET)
{
SpiHandle.Instance = DISCOVERY_SPIx;
SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
SpiHandle.Init.CRCPolynomial = 7;
SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SpiHandle.Init.NSS = SPI_NSS_SOFT;
SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
SpiHandle.Init.Mode = SPI_MODE_MASTER;
SPIx_MspInit();
HAL_SPI_Init(&SpiHandle);
}if (HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET) { ... }
}{ ... }
/* ... */
static uint8_t SPIx_WriteRead(uint8_t Byte)
{
uint8_t receivedbyte = 0;
if(HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*) &Byte, (uint8_t*) &receivedbyte, 1, SpixTimeout) != HAL_OK)
{
SPIx_Error();
}if (HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*) &Byte, (uint8_t*) &receivedbyte, 1, SpixTimeout) != HAL_OK) { ... }
return receivedbyte;
}{ ... }
/* ... */
static void SPIx_Error(void)
{
HAL_SPI_DeInit(&SpiHandle);
SPIx_Init();
}{ ... }
/* ... */
static void SPIx_MspInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DISCOVERY_SPIx_CLK_ENABLE();
DISCOVERY_SPIx_GPIO_CLK_ENABLE();
GPIO_InitStructure.Pin = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MISO_PIN | DISCOVERY_SPIx_MOSI_PIN);
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_PULLDOWN;
GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF;
HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure);
}{ ... }
/* ... */
static void I2Cx_Init(void)
{
if(HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET)
{
I2cHandle.Init.ClockSpeed = BSP_I2C_SPEED;
I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
I2cHandle.Init.OwnAddress1 = 0x33;
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2cHandle.Instance = DISCOVERY_I2Cx;
I2Cx_MspInit();
HAL_I2C_Init(&I2cHandle);
}if (HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET) { ... }
}{ ... }
/* ... */
static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Write(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, I2cxTimeout);
if(status != HAL_OK)
{
I2Cx_Error(Addr);
}if (status != HAL_OK) { ... }
}{ ... }
/* ... */
static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg)
{
HAL_StatusTypeDef status = HAL_OK;
uint8_t value = 0;
status = HAL_I2C_Mem_Read(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &value, 1,I2cxTimeout);
if(status != HAL_OK)
{
I2Cx_Error(Addr);
}if (status != HAL_OK) { ... }
return value;
}{ ... }
/* ... */
static void I2Cx_Error(uint8_t Addr)
{
HAL_I2C_DeInit(&I2cHandle);
I2Cx_Init();
}{ ... }
/* ... */
static void I2Cx_MspInit(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
DISCOVERY_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
GPIO_InitStruct.Pin = DISCOVERY_I2Cx_SCL_PIN | DISCOVERY_I2Cx_SDA_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = DISCOVERY_I2Cx_SCL_SDA_AF;
HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, &GPIO_InitStruct);
DISCOVERY_I2Cx_CLK_ENABLE();
DISCOVERY_I2Cx_FORCE_RESET();
DISCOVERY_I2Cx_RELEASE_RESET();
HAL_NVIC_SetPriority(DISCOVERY_I2Cx_EV_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_EV_IRQn);
HAL_NVIC_SetPriority(DISCOVERY_I2Cx_ER_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_ER_IRQn);
}{ ... }
/* ... */
I2C Routines
/* ... */
void ACCELERO_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ACCELERO_CS_GPIO_CLK_ENABLE();
GPIO_InitStructure.Pin = ACCELERO_CS_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
HAL_GPIO_Init(ACCELERO_CS_GPIO_PORT, &GPIO_InitStructure);
ACCELERO_CS_HIGH();
SPIx_Init();
}{ ... }
/* ... */
void ACCELERO_IO_ITConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ACCELERO_INT_GPIO_CLK_ENABLE();
GPIO_InitStructure.Pin = ACCELERO_INT2_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(ACCELERO_INT_GPIO_PORT, &GPIO_InitStructure);
HAL_NVIC_SetPriority((IRQn_Type)ACCELERO_INT2_EXTI_IRQn, 0x0F, 0);
HAL_NVIC_EnableIRQ((IRQn_Type)ACCELERO_INT2_EXTI_IRQn);
}{ ... }
/* ... */
void ACCELERO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite)
{
/* ... */
if(NumByteToWrite > 0x01)
{
WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD;
}if (NumByteToWrite > 0x01) { ... }
ACCELERO_CS_LOW();
SPIx_WriteRead(WriteAddr);
while(NumByteToWrite >= 0x01)
{
SPIx_WriteRead(*pBuffer);
NumByteToWrite--;
pBuffer++;
}while (NumByteToWrite >= 0x01) { ... }
ACCELERO_CS_HIGH();
}{ ... }
/* ... */
void ACCELERO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
{
if(NumByteToRead > 0x01)
{
ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD);
}if (NumByteToRead > 0x01) { ... }
else
{
ReadAddr |= (uint8_t)READWRITE_CMD;
}else { ... }
ACCELERO_CS_LOW();
SPIx_WriteRead(ReadAddr);
while(NumByteToRead > 0x00)
{
*pBuffer = SPIx_WriteRead(DUMMY_BYTE);
NumByteToRead--;
pBuffer++;
}while (NumByteToRead > 0x00) { ... }
ACCELERO_CS_HIGH();
}{ ... }
LINK ACCELEROMETER
/* ... */
void AUDIO_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
AUDIO_RESET_GPIO_CLK_ENABLE();
GPIO_InitStruct.Pin = AUDIO_RESET_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(AUDIO_RESET_GPIO, &GPIO_InitStruct);
I2Cx_Init();
HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_RESET);
HAL_Delay(5);
HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_SET);
HAL_Delay(5);
}{ ... }
/* ... */
void AUDIO_IO_DeInit(void)
{
}{ ... }
/* ... */
void AUDIO_IO_Write (uint8_t Addr, uint8_t Reg, uint8_t Value)
{
I2Cx_WriteData(Addr, Reg, Value);
}{ ... }
/* ... */
uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg)
{
return I2Cx_ReadData(Addr, Reg);
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */