-
Notifications
You must be signed in to change notification settings - Fork 1
/
stm32f0xx_nucleo_32.c
443 lines (374 loc) · 12.4 KB
/
stm32f0xx_nucleo_32.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
******************************************************************************
* @file stm32f0xx_nucleo_32.c
* @author MCD Application Team
* @brief This file provides set of firmware functions to manage:
* - LED available on STM32F0XX-Nucleo Kit
* from STMicroelectronics.
* - Gravitech 7segment shield available separately.
******************************************************************************
* @attention
*
* Copyright (c) 2016 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_nucleo_32.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32F0XX_NUCLEO_32 NUCLEO 32
* @{
*/
/** @defgroup STM32F0XX_NUCLEO_32_Private_Constants Private Constants
* @{
*/
/**
* @brief STM32F0XX NUCLEO BSP Driver version number V1.0.5
*/
#define __STM32F0XX_NUCLEO_32_BSP_VERSION_MAIN (0x01) /*!< [31:24] main version */
#define __STM32F0XX_NUCLEO_32_BSP_VERSION_SUB1 (0x00) /*!< [23:16] sub1 version */
#define __STM32F0XX_NUCLEO_32_BSP_VERSION_SUB2 (0x05) /*!< [15:8] sub2 version */
#define __STM32F0XX_NUCLEO_32_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
#define __STM32F0XX_NUCLEO_32_BSP_VERSION ((__STM32F0XX_NUCLEO_32_BSP_VERSION_MAIN << 24)\
|(__STM32F0XX_NUCLEO_32_BSP_VERSION_SUB1 << 16)\
|(__STM32F0XX_NUCLEO_32_BSP_VERSION_SUB2 << 8 )\
|(__STM32F0XX_NUCLEO_32_BSP_VERSION_RC))
/** @defgroup STM32F0XX_NUCLEO_32_Private_Variables Private Variables
* @{
*/
GPIO_TypeDef* LED_PORT[LEDn] = {LED3_GPIO_PORT};
const uint16_t LED_PIN[LEDn] = {LED3_PIN};
/**
* @brief BUS variables
*/
#if defined(HAL_I2C_MODULE_ENABLED)
uint32_t I2c1Timeout = BSP_I2C1_TIMEOUT_MAX; /*<! Value of Timeout when I2C1 communication fails */
I2C_HandleTypeDef nucleo32_I2c1;
#endif /* HAL_I2C_MODULE_ENABLED */
/**
* @}
*/
/** @defgroup STM32F0XX_NUCLEO_32_Private_Function_Prototypes Private Function Prototypes
* @{
*/
#if defined(HAL_I2C_MODULE_ENABLED)
/* I2C1 bus function */
/* Link function for I2C peripherals */
void I2C1_Init(void);
void I2C1_Error (void);
void I2C1_MspInit(I2C_HandleTypeDef *hi2c);
void I2C1_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
uint8_t I2C1_Read(uint8_t Addr, uint8_t Reg);
HAL_StatusTypeDef I2C1_WriteBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length);
HAL_StatusTypeDef I2C1_ReadBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length);
HAL_StatusTypeDef I2C1_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
#endif /* HAL_I2C_MODULE_ENABLED */
/**
* @}
*/
/** @defgroup STM32F0XX_NUCLEO_32_Private_Functions Private Functions
* @{
*/
/**
* @brief This method returns the STM32F0XX NUCLEO BSP Driver revision
* @retval version : 0xXYZR (8bits for each decimal, R for RC)
*/
uint32_t BSP_GetVersion(void)
{
return __STM32F0XX_NUCLEO_32_BSP_VERSION;
}
/**
* @brief Configures LED GPIO.
* @param Led Specifies the Led to be configured.
* This parameter can be one of following parameters:
* @arg LED3
* @retval None
*/
void BSP_LED_Init(Led_TypeDef Led)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable the GPIO_LED Clock */
LEDx_GPIO_CLK_ENABLE(Led);
/* Configure the GPIO_LED pin */
GPIO_InitStruct.Pin = LED_PIN[Led];
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LED_PORT[Led], &GPIO_InitStruct);
HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET);
}
/**
* @brief Turns selected LED On.
* @param Led Specifies the Led to be set on.
* This parameter can be one of following parameters:
* @arg LED3
* @retval None
*/
void BSP_LED_On(Led_TypeDef Led)
{
HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_SET);
}
/**
* @brief Turns selected LED Off.
* @param Led Specifies the Led to be set off.
* This parameter can be one of following parameters:
* @arg LED3
* @retval None
*/
void BSP_LED_Off(Led_TypeDef Led)
{
HAL_GPIO_WritePin(LED_PORT[Led], LED_PIN[Led], GPIO_PIN_RESET);
}
/**
* @brief Toggles the selected LED.
* @param Led Specifies the Led to be toggled.
* This parameter can be one of following parameters:
* @arg LED3
* @retval None
*/
void BSP_LED_Toggle(Led_TypeDef Led)
{
HAL_GPIO_TogglePin(LED_PORT[Led], LED_PIN[Led]);
}
/******************************************************************************
BUS OPERATIONS
*******************************************************************************/
#if defined(HAL_I2C_MODULE_ENABLED)
/******************************* I2C Routines *********************************/
/**
* @brief I2C Bus initialization
* @retval None
*/
void I2C1_Init(void)
{
if(HAL_I2C_GetState(&nucleo32_I2c1) == HAL_I2C_STATE_RESET)
{
nucleo32_I2c1.Instance = BSP_I2C1;
nucleo32_I2c1.Init.Timing = I2C1_TIMING;
nucleo32_I2c1.Init.OwnAddress1 = 0;
nucleo32_I2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
nucleo32_I2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
nucleo32_I2c1.Init.OwnAddress2 = 0;
nucleo32_I2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
nucleo32_I2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
nucleo32_I2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
/* Init the I2C */
I2C1_MspInit(&nucleo32_I2c1);
HAL_I2C_Init(&nucleo32_I2c1);
}
}
/**
* @brief Writes a single data.
* @param Addr I2C address
* @param Reg Register address
* @param Value Data to be written
* @retval None
*/
void I2C1_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Write(&nucleo32_I2c1, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, 100);
/* Check the communication status */
if(status != HAL_OK)
{
/* Execute user timeout callback */
I2C1_Error();
}
}
/**
* @brief Reads a single data.
* @param Addr I2C address
* @param Reg Register address
* @retval Read data
*/
uint8_t I2C1_Read(uint8_t Addr, uint8_t Reg)
{
HAL_StatusTypeDef status = HAL_OK;
uint8_t Value = 0;
status = HAL_I2C_Mem_Read(&nucleo32_I2c1, Addr, Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, 1000);
/* Check the communication status */
if(status != HAL_OK)
{
/* Execute user timeout callback */
I2C1_Error();
}
return Value;
}
/**
* @brief Reads multiple data on the BUS.
* @param Addr I2C Address
* @param Reg Reg Address
* @param RegSize The target register size (can be 8BIT or 16BIT)
* @param pBuffer pointer to read data buffer
* @param Length length of the data
* @retval 0 if no problems to read multiple data
*/
HAL_StatusTypeDef I2C1_ReadBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Read(&nucleo32_I2c1, Addr, Reg, RegSize, pBuffer, Length, I2c1Timeout);
/* Check the communication status */
if(status != HAL_OK)
{
/* Re-Initiaize the BUS */
I2C1_Error();
}
return status;
}
/**
* @brief Checks if target device is ready for communication.
* @note This function is used with Memory devices
* @param DevAddress Target device address
* @param Trials Number of trials
* @retval HAL status
*/
HAL_StatusTypeDef I2C1_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
{
return (HAL_I2C_IsDeviceReady(&nucleo32_I2c1, DevAddress, Trials, I2c1Timeout));
}
/**
* @brief Write a value in a register of the device through BUS.
* @param Addr Device address on BUS Bus.
* @param Reg The target register address to write
* @param RegSize The target register size (can be 8BIT or 16BIT)
* @param pBuffer The target register value to be written
* @param Length buffer size to be written
* @retval None
*/
HAL_StatusTypeDef I2C1_WriteBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Write(&nucleo32_I2c1, Addr, Reg, RegSize, pBuffer, Length, I2c1Timeout);
/* Check the communication status */
if(status != HAL_OK)
{
/* Re-Initiaize the BUS */
I2C1_Error();
}
return status;
}
/**
* @brief Manages error callback by re-initializing I2C.
* @retval None
*/
void I2C1_Error(void)
{
/* De-initialize the I2C communication BUS */
HAL_I2C_DeInit(&nucleo32_I2c1);
/* Re-Initiaize the I2C communication BUS */
I2C1_Init();
}
/**
* @brief I2C MSP Initialization
* @param hi2c I2C handle
* @retval None
*/
void I2C1_MspInit(I2C_HandleTypeDef *hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct;
/*##-1- Set source clock to SYSCLK for I2C1 ################################################*/
RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
RCC_PeriphCLKInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
/*##-2- Configure the GPIOs ################################################*/
/* Enable GPIO clock */
BSP_I2C1_GPIO_CLK_ENABLE();
/* Configure I2C SCL & SDA as alternate function */
GPIO_InitStruct.Pin = (BSP_I2C1_SCL_PIN| BSP_I2C1_SDA_PIN);
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = BSP_I2C1_SCL_SDA_AF;
HAL_GPIO_Init(BSP_I2C1_GPIO_PORT, &GPIO_InitStruct);
/*##-3- Configure the Eval I2C peripheral #######################################*/
/* Enable I2C clock */
BSP_I2C1_CLK_ENABLE();
/* Force the I2C peripheral clock reset */
BSP_I2C1_FORCE_RESET();
/* Release the I2C peripheral clock reset */
BSP_I2C1_RELEASE_RESET();
}
#endif /*HAL_I2C_MODULE_ENABLED*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/** @addtogroup STM32F0XX_NUCLEO_32_GRAVITECH_4DIGITS
* @{
*/
/** @addtogroup STM32_GRAVITECH_4DIGITS_Exported_Functions
* @{
*/
#if defined(HAL_I2C_MODULE_ENABLED)
/**
* @brief BSP of the 4 digits 7 Segment Display shield for Arduino Nano - Gravitech.
init
* @retval HAL_StatusTypeDef
*/
HAL_StatusTypeDef BSP_DIGIT4_SEG7_Init(void)
{
uint8_t control[1] = {0x47};
/* Init I2C */
I2C1_Init();
/* Configure the SAA1064 component */
return I2C1_WriteBuffer(0x70, 0, 1, control, sizeof(control));
}
/**
* @brief BSP of the 4 digits 7 Segment Display shield for Arduino Nano - Gravitech.
Display the value if value belong to [0-9999]
* @param Value A number between 0 and 9999 will be displayed on the screen.
* DIGIT4_SEG7_RESET will reset the screen (any value above 9999 will reset the screen also)
* @retval HAL_StatusTypeDef
*/
HAL_StatusTypeDef BSP_DIGIT4_SEG7_Display(uint32_t Value)
{
const uint8_t lookup[10] = {0x3F,0x06,0x5B,0x4F,0x66,
0x6D,0x7D,0x07,0x7F,0x6F};
uint32_t thousands, hundreds, tens, base;
HAL_StatusTypeDef status = HAL_ERROR;
uint8_t d1d2d3d4[4];
if (Value < 10000)
{
thousands = Value / 1000;
hundreds = (Value - (thousands * 1000)) / 100;
tens = (Value - ((thousands * 1000) + (hundreds * 100))) / 10;
base = Value - ((thousands * 1000) + (hundreds * 100) + (tens * 10));
d1d2d3d4[3] = lookup[thousands];
d1d2d3d4[2] = lookup[hundreds];
d1d2d3d4[1] = lookup[tens];
d1d2d3d4[0] = lookup[base];
}
else
{
d1d2d3d4[3] = 0;
d1d2d3d4[2] = 0;
d1d2d3d4[1] = 0;
d1d2d3d4[0] = 0;
}
/* Send the four digits to the SAA1064 component */
status = I2C1_WriteBuffer(0x70, 1, 1, d1d2d3d4, sizeof(d1d2d3d4));
return status;
}
#endif /*HAL_I2C_MODULE_ENABLED*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/