-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstm32l475e_iot01.c
805 lines (680 loc) · 24.1 KB
/
stm32l475e_iot01.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
443
444
445
446
447
448
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
/**
******************************************************************************
* @file stm32l475e_iot01.c
* @author MCD Application Team
* @brief STM32L475E-IOT01 board support package
******************************************************************************
* @attention
*
* Copyright (c) 2017 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 "stm32l475e_iot01.h"
/** @defgroup BSP BSP
* @{
*/
/** @defgroup STM32L475E_IOT01 STM32L475E_IOT01
* @{
*/
/** @defgroup STM32L475E_IOT01_LOW_LEVEL LOW LEVEL
* @{
*/
/** @defgroup STM32L475E_IOT01_LOW_LEVEL_Private_Defines LOW LEVEL Private Def
* @{
*/
/**
* @brief STM32L475E IOT01 BSP Driver version number
*/
#define __STM32L475E_IOT01_BSP_VERSION_MAIN (0x01) /*!< [31:24] main version */
#define __STM32L475E_IOT01_BSP_VERSION_SUB1 (0x01) /*!< [23:16] sub1 version */
#define __STM32L475E_IOT01_BSP_VERSION_SUB2 (0x08) /*!< [15:8] sub2 version */
#define __STM32L475E_IOT01_BSP_VERSION_RC (0x00) /*!< [7:0] release candidate */
#define __STM32L475E_IOT01_BSP_VERSION ((__STM32L475E_IOT01_BSP_VERSION_MAIN << 24)\
|(__STM32L475E_IOT01_BSP_VERSION_SUB1 << 16)\
|(__STM32L475E_IOT01_BSP_VERSION_SUB2 << 8 )\
|(__STM32L475E_IOT01_BSP_VERSION_RC))
/**
* @}
*/
/** @defgroup STM32L475E_IOT01_LOW_LEVEL_Private_Variables LOW LEVEL Variables
* @{
*/
const uint32_t GPIO_PIN[LEDn] = {LED2_PIN};
GPIO_TypeDef* GPIO_PORT[LEDn] = {LED2_GPIO_PORT};
GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {USER_BUTTON_GPIO_PORT};
const uint16_t BUTTON_PIN[BUTTONn] = {USER_BUTTON_PIN};
const uint16_t BUTTON_IRQn[BUTTONn] = {USER_BUTTON_EXTI_IRQn};
USART_TypeDef* COM_USART[COMn] = {DISCOVERY_COM1};
GPIO_TypeDef* COM_TX_PORT[COMn] = {DISCOVERY_COM1_TX_GPIO_PORT};
GPIO_TypeDef* COM_RX_PORT[COMn] = {DISCOVERY_COM1_RX_GPIO_PORT};
const uint16_t COM_TX_PIN[COMn] = {DISCOVERY_COM1_TX_PIN};
const uint16_t COM_RX_PIN[COMn] = {DISCOVERY_COM1_RX_PIN};
const uint16_t COM_TX_AF[COMn] = {DISCOVERY_COM1_TX_AF};
const uint16_t COM_RX_AF[COMn] = {DISCOVERY_COM1_RX_AF};
I2C_HandleTypeDef hI2cHandler;
UART_HandleTypeDef hDiscoUart;
/**
* @}
*/
/** @defgroup STM32L475E_IOT01_LOW_LEVEL_Private_FunctionPrototypes LOW LEVEL Private Function Prototypes
* @{
*/
static void I2Cx_MspInit(I2C_HandleTypeDef *i2c_handler);
static void I2Cx_MspDeInit(I2C_HandleTypeDef *i2c_handler);
static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler);
static void I2Cx_DeInit(I2C_HandleTypeDef *i2c_handler);
static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddSize, uint8_t *Buffer, uint16_t Length);
static HAL_StatusTypeDef I2Cx_IsDeviceReady(I2C_HandleTypeDef *i2c_handler, uint16_t DevAddress, uint32_t Trials);
static void I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr);
/* Sensors IO functions */
void SENSOR_IO_Init(void);
void SENSOR_IO_DeInit(void);
void SENSOR_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
uint8_t SENSOR_IO_Read(uint8_t Addr, uint8_t Reg);
uint16_t SENSOR_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
void SENSOR_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length);
HAL_StatusTypeDef SENSOR_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials);
void SENSOR_IO_Delay(uint32_t Delay);
void NFC_IO_Init(uint8_t GpoIrqEnable);
void NFC_IO_DeInit(void);
uint16_t NFC_IO_ReadMultiple (uint8_t Addr, uint8_t *pBuffer, uint16_t Length );
uint16_t NFC_IO_WriteMultiple (uint8_t Addr, uint8_t *pBuffer, uint16_t Length);
uint16_t NFC_IO_IsDeviceReady (uint8_t Addr, uint32_t Trials);
void NFC_IO_ReadState(uint8_t * pPinState);
void NFC_IO_RfDisable(uint8_t PinState);
void NFC_IO_Delay(uint32_t Delay);
/**
* @}
*/
/** @defgroup STM32L475E_IOT01_LOW_LEVEL_Private_Functions LOW LEVEL Private Functions
* @{
*/
/**
* @brief This method returns the STM32L475E IOT01 BSP Driver revision
* @retval version 0xXYZR (8bits for each decimal, R for RC)
*/
uint32_t BSP_GetVersion(void)
{
return __STM32L475E_IOT01_BSP_VERSION;
}
/**
* @brief Initializes LED GPIO.
* @param Led LED to be initialized.
* This parameter can be one of the following values:
* @arg LED2
*/
void BSP_LED_Init(Led_TypeDef Led)
{
GPIO_InitTypeDef gpio_init_structure;
LEDx_GPIO_CLK_ENABLE(Led);
/* Configure the GPIO_LED pin */
gpio_init_structure.Pin = GPIO_PIN[Led];
gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIO_PORT[Led], &gpio_init_structure);
}
/**
* @brief DeInitializes LED GPIO.
* @param Led LED to be deinitialized.
* This parameter can be one of the following values:
* @arg LED2
*/
void BSP_LED_DeInit(Led_TypeDef Led)
{
GPIO_InitTypeDef gpio_init_structure;
/* DeInit the GPIO_LED pin */
gpio_init_structure.Pin = GPIO_PIN[Led];
/* Turn off LED */
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
HAL_GPIO_DeInit(GPIO_PORT[Led], gpio_init_structure.Pin);
}
/**
* @brief Turns the selected LED On.
* @param Led LED to be set on
* This parameter can be one of the following values:
* @arg LED2
*/
void BSP_LED_On(Led_TypeDef Led)
{
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
}
/**
* @brief Turns the selected LED Off.
* @param Led LED to be set off
* This parameter can be one of the following values:
* @arg LED2
*/
void BSP_LED_Off(Led_TypeDef Led)
{
HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
}
/**
* @brief Toggles the selected LED.
* @param Led LED to be toggled
* This parameter can be one of the following values:
* @arg LED2
*/
void BSP_LED_Toggle(Led_TypeDef Led)
{
HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
}
/**
* @brief Initializes push button GPIO and EXTI Line.
* @param Button Button to be configured
* This parameter can be one of the following values:
* @arg BUTTON_USER User Push Button
* @param ButtonMode Button mode
* This parameter can be one of the following values:
* @arg BUTTON_MODE_GPIO Button will be used as simple IO
* @arg BUTTON_MODE_EXTI Button will be connected to EXTI line
* with interrupt generation capability
*/
void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef ButtonMode)
{
GPIO_InitTypeDef gpio_init_structure;
/* Enable the BUTTON clock */
USER_BUTTON_GPIO_CLK_ENABLE();
if(ButtonMode == BUTTON_MODE_GPIO)
{
/* Configure Button pin as input */
gpio_init_structure.Pin = BUTTON_PIN[Button];
gpio_init_structure.Mode = GPIO_MODE_INPUT;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(BUTTON_PORT[Button], &gpio_init_structure);
}
if(ButtonMode == BUTTON_MODE_EXTI)
{
/* Configure Button pin as input with External interrupt */
gpio_init_structure.Pin = BUTTON_PIN[Button];
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio_init_structure.Mode = GPIO_MODE_IT_RISING;
HAL_GPIO_Init(BUTTON_PORT[Button], &gpio_init_structure);
/* Enable and set Button EXTI Interrupt to the lowest priority */
HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0x00);
HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
}
}
/**
* @brief DeInitializes push button.
* @param Button Button to be configured
* This parameter can be one of the following values:
* @arg BUTTON_USER User Push Button
* @note PB DeInit does not disable the GPIO clock
*/
void BSP_PB_DeInit(Button_TypeDef Button)
{
GPIO_InitTypeDef gpio_init_structure;
gpio_init_structure.Pin = BUTTON_PIN[Button];
HAL_NVIC_DisableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
HAL_GPIO_DeInit(BUTTON_PORT[Button], gpio_init_structure.Pin);
}
/**
* @brief Returns the selected button state.
* @param Button Button to be checked
* This parameter can be one of the following values:
* @arg BUTTON_USER User Push Button
* @retval The Button GPIO pin value (GPIO_PIN_RESET = button pressed)
*/
uint32_t BSP_PB_GetState(Button_TypeDef Button)
{
return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
}
/**
* @brief Configures COM port.
* @param COM COM port to be initialized.
* This parameter can be one of the following values:
* @arg COM1
* @param huart Pointer to a UART_HandleTypeDef structure that contains the
* configuration information for the specified USART peripheral.
*/
void BSP_COM_Init(COM_TypeDef COM, UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef gpio_init_structure;
/* Enable GPIO clock */
DISCOVERY_COMx_TX_GPIO_CLK_ENABLE(COM);
DISCOVERY_COMx_RX_GPIO_CLK_ENABLE(COM);
/* Enable USART clock */
DISCOVERY_COMx_CLK_ENABLE(COM);
/* Configure USART Tx as alternate function */
gpio_init_structure.Pin = COM_TX_PIN[COM];
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Alternate = COM_TX_AF[COM];
HAL_GPIO_Init(COM_TX_PORT[COM], &gpio_init_structure);
/* Configure USART Rx as alternate function */
gpio_init_structure.Pin = COM_RX_PIN[COM];
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Alternate = COM_RX_AF[COM];
HAL_GPIO_Init(COM_RX_PORT[COM], &gpio_init_structure);
/* USART configuration */
huart->Instance = COM_USART[COM];
HAL_UART_Init(huart);
}
/**
* @brief DeInitializes COM port.
* @param COM COM port to be deinitialized.
* This parameter can be one of the following values:
* @arg COM1
* @param huart Pointer to a UART_HandleTypeDef structure that contains the
* configuration information for the specified USART peripheral.
*/
void BSP_COM_DeInit(COM_TypeDef COM, UART_HandleTypeDef *huart)
{
/* USART configuration */
huart->Instance = COM_USART[COM];
HAL_UART_DeInit(huart);
/* Enable USART clock */
DISCOVERY_COMx_CLK_DISABLE(COM);
/* DeInit GPIO pins can be done in the application
(by surcharging this __weak function) */
/* GPIO pins clock, FMC clock and DMA clock can be shut down in the application
by surcharging this __weak function */
}
/*******************************************************************************
BUS OPERATIONS
*******************************************************************************/
/******************************* I2C Routines *********************************/
/**
* @brief Initializes I2C MSP.
* @param i2c_handler I2C handler
* @retval None
*/
static void I2Cx_MspInit(I2C_HandleTypeDef *i2c_handler)
{
GPIO_InitTypeDef gpio_init_structure;
/*** Configure the GPIOs ***/
/* Enable GPIO clock */
DISCOVERY_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
/* Configure I2C Tx, Rx as alternate function */
gpio_init_structure.Pin = DISCOVERY_I2Cx_SCL_PIN | DISCOVERY_I2Cx_SDA_PIN;
gpio_init_structure.Mode = GPIO_MODE_AF_OD;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio_init_structure.Alternate = DISCOVERY_I2Cx_SCL_SDA_AF;
HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);
/*** Configure the I2C peripheral ***/
/* Enable I2C clock */
DISCOVERY_I2Cx_CLK_ENABLE();
/* Force the I2C peripheral clock reset */
DISCOVERY_I2Cx_FORCE_RESET();
/* Release the I2C peripheral clock reset */
DISCOVERY_I2Cx_RELEASE_RESET();
/* Enable and set I2Cx Interrupt to a lower priority */
HAL_NVIC_SetPriority(DISCOVERY_I2Cx_EV_IRQn, 0x0F, 0);
HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_EV_IRQn);
/* Enable and set I2Cx Interrupt to a lower priority */
HAL_NVIC_SetPriority(DISCOVERY_I2Cx_ER_IRQn, 0x0F, 0);
HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_ER_IRQn);
}
/**
* @brief DeInitializes I2C MSP.
* @param i2c_handler I2C handler
* @retval None
*/
static void I2Cx_MspDeInit(I2C_HandleTypeDef *i2c_handler)
{
GPIO_InitTypeDef gpio_init_structure;
/* Configure I2C Tx, Rx as alternate function */
gpio_init_structure.Pin = DISCOVERY_I2Cx_SCL_PIN | DISCOVERY_I2Cx_SDA_PIN;
HAL_GPIO_DeInit(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, gpio_init_structure.Pin);
/* Disable GPIO clock */
DISCOVERY_I2Cx_SCL_SDA_GPIO_CLK_DISABLE();
/* Disable I2C clock */
DISCOVERY_I2Cx_CLK_DISABLE();
}
/**
* @brief Initializes I2C HAL.
* @param i2c_handler I2C handler
* @retval None
*/
static void I2Cx_Init(I2C_HandleTypeDef *i2c_handler)
{
/* I2C configuration */
i2c_handler->Instance = DISCOVERY_I2Cx;
i2c_handler->Init.Timing = DISCOVERY_I2Cx_TIMING;
i2c_handler->Init.OwnAddress1 = 0;
i2c_handler->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
i2c_handler->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
i2c_handler->Init.OwnAddress2 = 0;
i2c_handler->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
i2c_handler->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
/* Init the I2C */
I2Cx_MspInit(i2c_handler);
HAL_I2C_Init(i2c_handler);
/**Configure Analogue filter */
HAL_I2CEx_ConfigAnalogFilter(i2c_handler, I2C_ANALOGFILTER_ENABLE);
}
/**
* @brief DeInitializes I2C HAL.
* @param i2c_handler I2C handler
* @retval None
*/
static void I2Cx_DeInit(I2C_HandleTypeDef *i2c_handler)
{ /* DeInit the I2C */
I2Cx_MspDeInit(i2c_handler);
HAL_I2C_DeInit(i2c_handler);
}
/**
* @brief Reads multiple data.
* @param i2c_handler I2C handler
* @param Addr I2C address
* @param Reg Reg address
* @param MemAddress memory address
* @param Buffer Pointer to data buffer
* @param Length Length of the data
* @retval HAL status
*/
static HAL_StatusTypeDef I2Cx_ReadMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddress, uint8_t *Buffer, uint16_t Length)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Read(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
/* Check the communication status */
if(status != HAL_OK)
{
/* I2C error occurred */
I2Cx_Error(i2c_handler, Addr);
}
return status;
}
/**
* @brief Writes a value in a register of the device through BUS in using DMA mode.
* @param i2c_handler I2C handler
* @param Addr Device address on BUS Bus.
* @param Reg The target register address to write
* @param MemAddress memory address
* @param Buffer The target register value to be written
* @param Length buffer size to be written
* @retval HAL status
*/
static HAL_StatusTypeDef I2Cx_WriteMultiple(I2C_HandleTypeDef *i2c_handler, uint8_t Addr, uint16_t Reg, uint16_t MemAddress, uint8_t *Buffer, uint16_t Length)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Write(i2c_handler, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, 1000);
/* Check the communication status */
if(status != HAL_OK)
{
/* Re-Initiaize the I2C Bus */
I2Cx_Error(i2c_handler, Addr);
}
return status;
}
/**
* @brief Checks if target device is ready for communication.
* @note This function is used with Memory devices
* @param i2c_handler I2C handler
* @param DevAddress Target device address
* @param Trials Number of trials
* @retval HAL status
*/
static HAL_StatusTypeDef I2Cx_IsDeviceReady(I2C_HandleTypeDef *i2c_handler, uint16_t DevAddress, uint32_t Trials)
{
return (HAL_I2C_IsDeviceReady(i2c_handler, DevAddress, Trials, 1000));
}
/**
* @brief Manages error callback by re-initializing I2C.
* @param i2c_handler I2C handler
* @param Addr I2C Address
* @retval None
*/
static void I2Cx_Error(I2C_HandleTypeDef *i2c_handler, uint8_t Addr)
{
/* De-initialize the I2C communication bus */
HAL_I2C_DeInit(i2c_handler);
/* Re-Initialize the I2C communication bus */
I2Cx_Init(i2c_handler);
}
/**
* @}
*/
/*******************************************************************************
LINK OPERATIONS
*******************************************************************************/
/******************************** LINK Sensors ********************************/
/**
* @brief Initializes Sensors low level.
* @retval None
*/
void SENSOR_IO_Init(void)
{
I2Cx_Init(&hI2cHandler);
}
/**
* @brief DeInitializes Sensors low level.
* @retval None
*/
void SENSOR_IO_DeInit(void)
{
I2Cx_DeInit(&hI2cHandler);
}
/**
* @brief Writes a single data.
* @param Addr I2C address
* @param Reg Reg address
* @param Value Data to be written
* @retval None
*/
void SENSOR_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value)
{
I2Cx_WriteMultiple(&hI2cHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Value, 1);
}
/**
* @brief Reads a single data.
* @param Addr I2C address
* @param Reg Reg address
* @retval Data to be read
*/
uint8_t SENSOR_IO_Read(uint8_t Addr, uint8_t Reg)
{
uint8_t read_value = 0;
I2Cx_ReadMultiple(&hI2cHandler, Addr, Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t*)&read_value, 1);
return read_value;
}
/**
* @brief Reads multiple data with I2C communication
* channel from TouchScreen.
* @param Addr I2C address
* @param Reg Register address
* @param Buffer Pointer to data buffer
* @param Length Length of the data
* @retval HAL status
*/
uint16_t SENSOR_IO_ReadMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
{
return I2Cx_ReadMultiple(&hI2cHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
}
/**
* @brief Writes multiple data with I2C communication
* channel from MCU to TouchScreen.
* @param Addr I2C address
* @param Reg Register address
* @param Buffer Pointer to data buffer
* @param Length Length of the data
* @retval None
*/
void SENSOR_IO_WriteMultiple(uint8_t Addr, uint8_t Reg, uint8_t *Buffer, uint16_t Length)
{
I2Cx_WriteMultiple(&hI2cHandler, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, Buffer, Length);
}
/**
* @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 SENSOR_IO_IsDeviceReady(uint16_t DevAddress, uint32_t Trials)
{
return (I2Cx_IsDeviceReady(&hI2cHandler, DevAddress, Trials));
}
/**
* @brief Delay function used in Sensor low level driver.
* @param Delay Delay in ms
* @retval None
*/
void SENSOR_IO_Delay(uint32_t Delay)
{
HAL_Delay(Delay);
}
/******************************** LINK NFC ********************************/
/**
* @brief Initializes Sensors low level.
* @param GpoIrqEnable 0x0 is disable, otherwise enabled
* @retval None
*/
void NFC_IO_Init(uint8_t GpoIrqEnable)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* I2C init */
I2Cx_Init(&hI2cHandler);
/* GPIO Ports Clock Enable */
NFC_GPIO_CLK_ENABLE();
/* Configure GPIO pins for GPO (PE4) */
if(GpoIrqEnable == 0)
{
GPIO_InitStruct.Pin = NFC_GPIO_GPO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(NFC_GPIO_GPO_PIN_PORT, &GPIO_InitStruct);
}
else
{
GPIO_InitStruct.Pin = NFC_GPIO_GPO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(NFC_GPIO_GPO_PIN_PORT, &GPIO_InitStruct);
/* Enable and set EXTI4_IRQn Interrupt to the lowest priority */
HAL_NVIC_SetPriority(EXTI4_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(EXTI4_IRQn);
}
/* Configure GPIO pins for DISABLE (PE2)*/
GPIO_InitStruct.Pin = NFC_GPIO_RFDISABLE_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(NFC_GPIO_RFDISABLE_PIN_PORT, &GPIO_InitStruct);
}
/**
* @brief DeInitializes Sensors low level.
* @retval None
*/
void NFC_IO_DeInit(void)
{
I2Cx_DeInit(&hI2cHandler);
}
/**
* @brief This functions reads a response of the M24SR device
* @param Addr M24SR I2C address
* @param pBuffer Pointer on the buffer to retrieve M24SR response
* @param Length Length of the data
* @retval Status Success or Timeout
*/
uint16_t NFC_IO_ReadMultiple (uint8_t Addr, uint8_t *pBuffer, uint16_t Length )
{
uint16_t status ;
/* Before calling this function M24SR must be ready: check to detect potential issues */
status = NFC_IO_IsDeviceReady(Addr, NFC_I2C_TRIALS);
if (status != NFC_I2C_STATUS_SUCCESS)
{
return NFC_I2C_ERROR_TIMEOUT;
}
if( HAL_I2C_Master_Receive(&hI2cHandler, Addr, (uint8_t*)pBuffer, Length, NFC_I2C_TIMEOUT_STD) != HAL_OK)
{
return NFC_I2C_ERROR_TIMEOUT;
}
return NFC_I2C_STATUS_SUCCESS;
}
/**
* @brief This functions sends the command buffer
* @param Addr M24SR I2C address
* @param pBuffer pointer to the buffer to send to the M24SR
* @param Length Length of the data
* @retval Status Success or Timeout
*/
uint16_t NFC_IO_WriteMultiple (uint8_t Addr, uint8_t *pBuffer, uint16_t Length)
{
uint16_t status ;
/* Before calling this function M24SR must be ready: check to detect potential issues */
status = NFC_IO_IsDeviceReady(Addr, NFC_I2C_TRIALS);
if (status != NFC_I2C_STATUS_SUCCESS)
{
return NFC_I2C_ERROR_TIMEOUT;
}
if( HAL_I2C_Master_Transmit(&hI2cHandler, Addr, (uint8_t*)pBuffer, Length, NFC_I2C_TIMEOUT_STD) != HAL_OK)
{
return NFC_I2C_ERROR_TIMEOUT;
}
return NFC_I2C_STATUS_SUCCESS;
}
/**
* @brief Checks if target device is ready for communication.
* @param Addr M24SR I2C address
* @param Trials Number of trials (currently not present in M24sr)
* @retval Status Success or Timeout
*/
uint16_t NFC_IO_IsDeviceReady (uint8_t Addr, uint32_t Trials)
{
HAL_StatusTypeDef status;
uint32_t tickstart = 0;
uint32_t currenttick = 0;
/* Get tick */
tickstart = HAL_GetTick();
/* Wait until M24SR is ready or timeout occurs */
do
{
status = HAL_I2C_IsDeviceReady(&hI2cHandler, Addr, Trials, NFC_I2C_TIMEOUT_STD);
currenttick = HAL_GetTick();
} while( ( (currenttick - tickstart) < NFC_I2C_TIMEOUT_MAX) && (status != HAL_OK) );
if (status != HAL_OK)
{
return NFC_I2C_ERROR_TIMEOUT;
}
return NFC_I2C_STATUS_SUCCESS;
}
/**
* @brief This function read the state of the M24SR GPO
* @retval GPIO_PinState state of the M24SR GPO
*/
void NFC_IO_ReadState(uint8_t * pPinState)
{
*pPinState = (uint8_t)HAL_GPIO_ReadPin(NFC_GPIO_GPO_PIN_PORT,NFC_GPIO_GPO_PIN);
}
/**
* @brief This function sets the state of the M24SR RF disable pin
* @param PinState put RF disable pin of M24SR in PinState (1 or 0)
*/
void NFC_IO_RfDisable(uint8_t PinState)
{
HAL_GPIO_WritePin(NFC_GPIO_RFDISABLE_PIN_PORT,NFC_GPIO_RFDISABLE_PIN,(GPIO_PinState)PinState);
}
/**
* @brief Delay function used in M24SR low level driver.
* @param Delay Delay in ms
* @retval None
*/
void NFC_IO_Delay(uint32_t Delay)
{
HAL_Delay(Delay);
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/