Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2 | Rev 9 | ||
---|---|---|---|
Line 17... | Line 17... | ||
17 | The I2C HAL driver can be used as follows: |
17 | The I2C HAL driver can be used as follows: |
18 | 18 | ||
19 | (#) Declare a I2C_HandleTypeDef handle structure, for example: |
19 | (#) Declare a I2C_HandleTypeDef handle structure, for example: |
20 | I2C_HandleTypeDef hi2c; |
20 | I2C_HandleTypeDef hi2c; |
21 | 21 | ||
22 | (#)Initialize the I2C low level resources by implementing the HAL_I2C_MspInit() API: |
22 | (#)Initialize the I2C low level resources by implementing the @ref HAL_I2C_MspInit() API: |
23 | (##) Enable the I2Cx interface clock |
23 | (##) Enable the I2Cx interface clock |
24 | (##) I2C pins configuration |
24 | (##) I2C pins configuration |
25 | (+++) Enable the clock for the I2C GPIOs |
25 | (+++) Enable the clock for the I2C GPIOs |
26 | (+++) Configure I2C pins as alternate function open-drain |
26 | (+++) Configure I2C pins as alternate function open-drain |
27 | (##) NVIC configuration if you need to use interrupt process |
27 | (##) NVIC configuration if you need to use interrupt process |
Line 31... | Line 31... | ||
31 | (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel |
31 | (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel |
32 | (+++) Enable the DMAx interface clock using |
32 | (+++) Enable the DMAx interface clock using |
33 | (+++) Configure the DMA handle parameters |
33 | (+++) Configure the DMA handle parameters |
34 | (+++) Configure the DMA Tx or Rx channel |
34 | (+++) Configure the DMA Tx or Rx channel |
35 | (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle |
35 | (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle |
36 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on |
36 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on |
37 | the DMA Tx or Rx channel |
37 | the DMA Tx or Rx channel |
38 | 38 | ||
39 | (#) Configure the Communication Speed, Duty cycle, Addressing mode, Own Address1, |
39 | (#) Configure the Communication Speed, Duty cycle, Addressing mode, Own Address1, |
40 | Dual Addressing mode, Own Address2, General call and Nostretch mode in the hi2c Init structure. |
40 | Dual Addressing mode, Own Address2, General call and Nostretch mode in the hi2c Init structure. |
41 | 41 | ||
42 | (#) Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level Hardware |
42 | (#) Initialize the I2C registers by calling the @ref HAL_I2C_Init(), configures also the low level Hardware |
43 | (GPIO, CLOCK, NVIC...etc) by calling the customized HAL_I2C_MspInit(&hi2c) API. |
43 | (GPIO, CLOCK, NVIC...etc) by calling the customized @ref HAL_I2C_MspInit() API. |
44 | 44 | ||
45 | (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady() |
45 | (#) To check if target device is ready for communication, use the function @ref HAL_I2C_IsDeviceReady() |
46 | 46 | ||
47 | (#) For I2C IO and IO MEM operations, three operation modes are available within this driver : |
47 | (#) For I2C IO and IO MEM operations, three operation modes are available within this driver : |
48 | 48 | ||
49 | *** Polling mode IO operation *** |
49 | *** Polling mode IO operation *** |
50 | ================================= |
50 | ================================= |
51 | [..] |
51 | [..] |
52 | (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit() |
52 | (+) Transmit in master mode an amount of data in blocking mode using @ref HAL_I2C_Master_Transmit() |
53 | (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive() |
53 | (+) Receive in master mode an amount of data in blocking mode using @ref HAL_I2C_Master_Receive() |
54 | (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit() |
54 | (+) Transmit in slave mode an amount of data in blocking mode using @ref HAL_I2C_Slave_Transmit() |
55 | (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive() |
55 | (+) Receive in slave mode an amount of data in blocking mode using @ref HAL_I2C_Slave_Receive() |
56 | 56 | ||
57 | *** Polling mode IO MEM operation *** |
57 | *** Polling mode IO MEM operation *** |
58 | ===================================== |
58 | ===================================== |
59 | [..] |
59 | [..] |
60 | (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write() |
60 | (+) Write an amount of data in blocking mode to a specific memory address using @ref HAL_I2C_Mem_Write() |
61 | (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read() |
61 | (+) Read an amount of data in blocking mode from a specific memory address using @ref HAL_I2C_Mem_Read() |
62 | 62 | ||
63 | 63 | ||
64 | *** Interrupt mode IO operation *** |
64 | *** Interrupt mode IO operation *** |
65 | =================================== |
65 | =================================== |
66 | [..] |
66 | [..] |
67 | (+) Transmit in master mode an amount of data in non blocking mode using HAL_I2C_Master_Transmit_IT() |
67 | (+) Transmit in master mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Transmit_IT() |
68 | (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can |
68 | (+) At transmission end of transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can |
69 | add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback |
69 | add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback() |
70 | (+) Receive in master mode an amount of data in non blocking mode using HAL_I2C_Master_Receive_IT() |
70 | (+) Receive in master mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Receive_IT() |
71 | (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can |
71 | (+) At reception end of transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can |
72 | add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback |
72 | add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback() |
73 | (+) Transmit in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Transmit_IT() |
73 | (+) Transmit in slave mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Transmit_IT() |
74 | (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can |
74 | (+) At transmission end of transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can |
75 | add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback |
75 | add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback() |
76 | (+) Receive in slave mode an amount of data in non blocking mode using HAL_I2C_Slave_Receive_IT() |
76 | (+) Receive in slave mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Receive_IT() |
77 | (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can |
77 | (+) At reception end of transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can |
78 | add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback |
78 | add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback() |
79 | (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can |
79 | (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can |
80 | add his own code by customization of function pointer HAL_I2C_ErrorCallback |
80 | add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback() |
81 | (+) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT() |
81 | (+) Abort a master I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT() |
82 | (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can |
82 | (+) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can |
83 | add his own code by customization of function pointer HAL_I2C_AbortCpltCallback() |
83 | add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback() |
84 | 84 | ||
85 | *** Interrupt mode IO sequential operation *** |
85 | *** Interrupt mode or DMA mode IO sequential operation *** |
86 | ============================================== |
86 | ========================================================== |
87 | [..] |
87 | [..] |
88 | (@) These interfaces allow to manage a sequential transfer with a repeated start condition |
88 | (@) These interfaces allow to manage a sequential transfer with a repeated start condition |
89 | when a direction change during transfer |
89 | when a direction change during transfer |
90 | [..] |
90 | [..] |
91 | (+) A specific option field manage the different steps of a sequential transfer |
91 | (+) A specific option field manage the different steps of a sequential transfer |
92 | (+) Option field values are defined through @ref I2C_XFEROPTIONS and are listed below: |
92 | (+) Option field values are defined through @ref I2C_XferOptions_definition and are listed below: |
93 | (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functionnal is same as associated interfaces in no sequential mode |
93 | (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functionnal is same as associated interfaces in no sequential mode |
94 | (++) I2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address |
94 | (++) I2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address |
95 | and data to transfer without a final stop condition |
95 | and data to transfer without a final stop condition |
- | 96 | (++) I2C_FIRST_AND_NEXT_FRAME: Sequential usage (Master only), this option allow to manage a sequence with start condition, address |
|
- | 97 | and data to transfer without a final stop condition, an then permit a call the same master sequential interface |
|
- | 98 | several times (like @ref HAL_I2C_Master_Seq_Transmit_IT() then @ref HAL_I2C_Master_Seq_Transmit_IT() |
|
- | 99 | or @ref HAL_I2C_Master_Seq_Transmit_DMA() then @ref HAL_I2C_Master_Seq_Transmit_DMA()) |
|
96 | (++) I2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address |
100 | (++) I2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address |
97 | and with new data to transfer if the direction change or manage only the new data to transfer |
101 | and with new data to transfer if the direction change or manage only the new data to transfer |
98 | if no direction change and without a final stop condition in both cases |
102 | if no direction change and without a final stop condition in both cases |
99 | (++) I2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address |
103 | (++) I2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address |
100 | and with new data to transfer if the direction change or manage only the new data to transfer |
104 | and with new data to transfer if the direction change or manage only the new data to transfer |
101 | if no direction change and with a final stop condition in both cases |
105 | if no direction change and with a final stop condition in both cases |
- | 106 | (++) I2C_LAST_FRAME_NO_STOP: Sequential usage (Master only), this option allow to manage a restart condition after several call of the same master sequential |
|
- | 107 | interface several times (link with option I2C_FIRST_AND_NEXT_FRAME). |
|
- | 108 | Usage can, transfer several bytes one by one using HAL_I2C_Master_Seq_Transmit_IT(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME) |
|
- | 109 | or HAL_I2C_Master_Seq_Receive_IT(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME) |
|
- | 110 | or HAL_I2C_Master_Seq_Transmit_DMA(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME) |
|
- | 111 | or HAL_I2C_Master_Seq_Receive_DMA(option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME). |
|
- | 112 | Then usage of this option I2C_LAST_FRAME_NO_STOP at the last Transmit or Receive sequence permit to call the oposite interface Receive or Transmit |
|
- | 113 | without stopping the communication and so generate a restart condition. |
|
- | 114 | (++) I2C_OTHER_FRAME: Sequential usage (Master only), this option allow to manage a restart condition after each call of the same master sequential |
|
- | 115 | interface. |
|
- | 116 | Usage can, transfer several bytes one by one with a restart with slave address between each bytes using HAL_I2C_Master_Seq_Transmit_IT(option I2C_FIRST_FRAME then I2C_OTHER_FRAME) |
|
- | 117 | or HAL_I2C_Master_Seq_Receive_IT(option I2C_FIRST_FRAME then I2C_OTHER_FRAME) |
|
- | 118 | or HAL_I2C_Master_Seq_Transmit_DMA(option I2C_FIRST_FRAME then I2C_OTHER_FRAME) |
|
- | 119 | or HAL_I2C_Master_Seq_Receive_DMA(option I2C_FIRST_FRAME then I2C_OTHER_FRAME). |
|
- | 120 | Then usage of this option I2C_OTHER_AND_LAST_FRAME at the last frame to help automatic generation of STOP condition. |
|
102 | 121 | ||
103 | (+) Differents sequential I2C interfaces are listed below: |
122 | (+) Differents sequential I2C interfaces are listed below: |
104 | (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using HAL_I2C_Master_Sequential_Transmit_IT() |
123 | (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Seq_Transmit_IT() |
- | 124 | or using @ref HAL_I2C_Master_Seq_Transmit_DMA() |
|
105 | (+++) At transmission end of current frame transfer, HAL_I2C_MasterTxCpltCallback() is executed and user can |
125 | (+++) At transmission end of current frame transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can |
106 | add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback() |
126 | add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback() |
107 | (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using HAL_I2C_Master_Sequential_Receive_IT() |
127 | (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Master_Seq_Receive_IT() |
- | 128 | or using @ref HAL_I2C_Master_Seq_Receive_DMA() |
|
108 | (+++) At reception end of current frame transfer, HAL_I2C_MasterRxCpltCallback() is executed and user can |
129 | (+++) At reception end of current frame transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can |
109 | add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback() |
130 | add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback() |
110 | (++) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT() |
131 | (++) Abort a master IT or DMA I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT() |
111 | (+++) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can |
132 | (+++) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can |
112 | add his own code by customization of function pointer HAL_I2C_AbortCpltCallback() |
133 | add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback() |
113 | (++) Enable/disable the Address listen mode in slave I2C mode using HAL_I2C_EnableListen_IT() HAL_I2C_DisableListen_IT() |
134 | (++) Enable/disable the Address listen mode in slave I2C mode using @ref HAL_I2C_EnableListen_IT() @ref HAL_I2C_DisableListen_IT() |
114 | (+++) When address slave I2C match, HAL_I2C_AddrCallback() is executed and user can |
135 | (+++) When address slave I2C match, @ref HAL_I2C_AddrCallback() is executed and user can |
115 | add his own code to check the Address Match Code and the transmission direction request by master (Write/Read). |
136 | add his own code to check the Address Match Code and the transmission direction request by master (Write/Read). |
116 | (+++) At Listen mode end HAL_I2C_ListenCpltCallback() is executed and user can |
137 | (+++) At Listen mode end @ref HAL_I2C_ListenCpltCallback() is executed and user can |
117 | add his own code by customization of function pointer HAL_I2C_ListenCpltCallback() |
138 | add his own code by customization of function pointer @ref HAL_I2C_ListenCpltCallback() |
118 | (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using HAL_I2C_Slave_Sequential_Transmit_IT() |
139 | (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Seq_Transmit_IT() |
- | 140 | or using @ref HAL_I2C_Slave_Seq_Transmit_DMA() |
|
119 | (+++) At transmission end of current frame transfer, HAL_I2C_SlaveTxCpltCallback() is executed and user can |
141 | (+++) At transmission end of current frame transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can |
120 | add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback() |
142 | add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback() |
121 | (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using HAL_I2C_Slave_Sequential_Receive_IT() |
143 | (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using @ref HAL_I2C_Slave_Seq_Receive_IT() |
- | 144 | or using @ref HAL_I2C_Slave_Seq_Receive_DMA() |
|
122 | (+++) At reception end of current frame transfer, HAL_I2C_SlaveRxCpltCallback() is executed and user can |
145 | (+++) At reception end of current frame transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can |
123 | add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback() |
146 | add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback() |
124 | (++) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can |
147 | (++) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can |
125 | add his own code by customization of function pointer HAL_I2C_ErrorCallback() |
148 | add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback() |
126 | 149 | ||
127 | *** Interrupt mode IO MEM operation *** |
150 | *** Interrupt mode IO MEM operation *** |
128 | ======================================= |
151 | ======================================= |
129 | [..] |
152 | [..] |
130 | (+) Write an amount of data in no-blocking mode with Interrupt to a specific memory address using |
153 | (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using |
131 | HAL_I2C_Mem_Write_IT() |
154 | @ref HAL_I2C_Mem_Write_IT() |
132 | (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can |
155 | (+) At Memory end of write transfer, @ref HAL_I2C_MemTxCpltCallback() is executed and user can |
133 | add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback |
156 | add his own code by customization of function pointer @ref HAL_I2C_MemTxCpltCallback() |
134 | (+) Read an amount of data in no-blocking mode with Interrupt from a specific memory address using |
157 | (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using |
135 | HAL_I2C_Mem_Read_IT() |
158 | @ref HAL_I2C_Mem_Read_IT() |
136 | (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can |
159 | (+) At Memory end of read transfer, @ref HAL_I2C_MemRxCpltCallback() is executed and user can |
137 | add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback |
160 | add his own code by customization of function pointer @ref HAL_I2C_MemRxCpltCallback() |
138 | (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can |
161 | (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can |
139 | add his own code by customization of function pointer HAL_I2C_ErrorCallback |
162 | add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback() |
140 | 163 | ||
141 | *** DMA mode IO operation *** |
164 | *** DMA mode IO operation *** |
142 | ============================== |
165 | ============================== |
143 | [..] |
166 | [..] |
144 | (+) Transmit in master mode an amount of data in non blocking mode (DMA) using |
167 | (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using |
145 | HAL_I2C_Master_Transmit_DMA() |
168 | @ref HAL_I2C_Master_Transmit_DMA() |
146 | (+) At transmission end of transfer HAL_I2C_MasterTxCpltCallback is executed and user can |
169 | (+) At transmission end of transfer, @ref HAL_I2C_MasterTxCpltCallback() is executed and user can |
147 | add his own code by customization of function pointer HAL_I2C_MasterTxCpltCallback |
170 | add his own code by customization of function pointer @ref HAL_I2C_MasterTxCpltCallback() |
148 | (+) Receive in master mode an amount of data in non blocking mode (DMA) using |
171 | (+) Receive in master mode an amount of data in non-blocking mode (DMA) using |
149 | HAL_I2C_Master_Receive_DMA() |
172 | @ref HAL_I2C_Master_Receive_DMA() |
150 | (+) At reception end of transfer HAL_I2C_MasterRxCpltCallback is executed and user can |
173 | (+) At reception end of transfer, @ref HAL_I2C_MasterRxCpltCallback() is executed and user can |
151 | add his own code by customization of function pointer HAL_I2C_MasterRxCpltCallback |
174 | add his own code by customization of function pointer @ref HAL_I2C_MasterRxCpltCallback() |
152 | (+) Transmit in slave mode an amount of data in non blocking mode (DMA) using |
175 | (+) Transmit in slave mode an amount of data in non-blocking mode (DMA) using |
153 | HAL_I2C_Slave_Transmit_DMA() |
176 | @ref HAL_I2C_Slave_Transmit_DMA() |
154 | (+) At transmission end of transfer HAL_I2C_SlaveTxCpltCallback is executed and user can |
177 | (+) At transmission end of transfer, @ref HAL_I2C_SlaveTxCpltCallback() is executed and user can |
155 | add his own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback |
178 | add his own code by customization of function pointer @ref HAL_I2C_SlaveTxCpltCallback() |
156 | (+) Receive in slave mode an amount of data in non blocking mode (DMA) using |
179 | (+) Receive in slave mode an amount of data in non-blocking mode (DMA) using |
157 | HAL_I2C_Slave_Receive_DMA() |
180 | @ref HAL_I2C_Slave_Receive_DMA() |
158 | (+) At reception end of transfer HAL_I2C_SlaveRxCpltCallback is executed and user can |
181 | (+) At reception end of transfer, @ref HAL_I2C_SlaveRxCpltCallback() is executed and user can |
159 | add his own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback |
182 | add his own code by customization of function pointer @ref HAL_I2C_SlaveRxCpltCallback() |
160 | (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can |
183 | (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can |
161 | add his own code by customization of function pointer HAL_I2C_ErrorCallback |
184 | add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback() |
162 | (+) Abort a master I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT() |
185 | (+) Abort a master I2C process communication with Interrupt using @ref HAL_I2C_Master_Abort_IT() |
163 | (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and user can |
186 | (+) End of abort process, @ref HAL_I2C_AbortCpltCallback() is executed and user can |
164 | add his own code by customization of function pointer HAL_I2C_AbortCpltCallback() |
187 | add his own code by customization of function pointer @ref HAL_I2C_AbortCpltCallback() |
165 | 188 | ||
166 | *** DMA mode IO MEM operation *** |
189 | *** DMA mode IO MEM operation *** |
167 | ================================= |
190 | ================================= |
168 | [..] |
191 | [..] |
169 | (+) Write an amount of data in no-blocking mode with DMA to a specific memory address using |
192 | (+) Write an amount of data in non-blocking mode with DMA to a specific memory address using |
170 | HAL_I2C_Mem_Write_DMA() |
193 | @ref HAL_I2C_Mem_Write_DMA() |
171 | (+) At MEM end of write transfer HAL_I2C_MemTxCpltCallback is executed and user can |
194 | (+) At Memory end of write transfer, @ref HAL_I2C_MemTxCpltCallback() is executed and user can |
172 | add his own code by customization of function pointer HAL_I2C_MemTxCpltCallback |
195 | add his own code by customization of function pointer @ref HAL_I2C_MemTxCpltCallback() |
173 | (+) Read an amount of data in no-blocking mode with DMA from a specific memory address using |
196 | (+) Read an amount of data in non-blocking mode with DMA from a specific memory address using |
174 | HAL_I2C_Mem_Read_DMA() |
197 | @ref HAL_I2C_Mem_Read_DMA() |
175 | (+) At MEM end of read transfer HAL_I2C_MemRxCpltCallback is executed and user can |
198 | (+) At Memory end of read transfer, @ref HAL_I2C_MemRxCpltCallback() is executed and user can |
176 | add his own code by customization of function pointer HAL_I2C_MemRxCpltCallback |
199 | add his own code by customization of function pointer @ref HAL_I2C_MemRxCpltCallback() |
177 | (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and user can |
200 | (+) In case of transfer Error, @ref HAL_I2C_ErrorCallback() function is executed and user can |
178 | add his own code by customization of function pointer HAL_I2C_ErrorCallback |
201 | add his own code by customization of function pointer @ref HAL_I2C_ErrorCallback() |
179 | 202 | ||
180 | 203 | ||
181 | *** I2C HAL driver macros list *** |
204 | *** I2C HAL driver macros list *** |
182 | ================================== |
205 | ================================== |
183 | [..] |
206 | [..] |
184 | Below the list of most used macros in I2C HAL driver. |
207 | Below the list of most used macros in I2C HAL driver. |
185 | 208 | ||
186 | (+) __HAL_I2C_ENABLE: Enable the I2C peripheral |
209 | (+) @ref __HAL_I2C_ENABLE: Enable the I2C peripheral |
187 | (+) __HAL_I2C_DISABLE: Disable the I2C peripheral |
210 | (+) @ref __HAL_I2C_DISABLE: Disable the I2C peripheral |
188 | (+) __HAL_I2C_GET_FLAG : Checks whether the specified I2C flag is set or not |
211 | (+) @ref __HAL_I2C_GET_FLAG: Checks whether the specified I2C flag is set or not |
189 | (+) __HAL_I2C_CLEAR_FLAG : Clear the specified I2C pending flag |
212 | (+) @ref __HAL_I2C_CLEAR_FLAG: Clear the specified I2C pending flag |
190 | (+) __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt |
213 | (+) @ref __HAL_I2C_ENABLE_IT: Enable the specified I2C interrupt |
191 | (+) __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt |
214 | (+) @ref __HAL_I2C_DISABLE_IT: Disable the specified I2C interrupt |
- | 215 | ||
- | 216 | *** Callback registration *** |
|
- | 217 | ============================================= |
|
- | 218 | [..] |
|
- | 219 | The compilation flag USE_HAL_I2C_REGISTER_CALLBACKS when set to 1 |
|
- | 220 | allows the user to configure dynamically the driver callbacks. |
|
- | 221 | Use Functions @ref HAL_I2C_RegisterCallback() or @ref HAL_I2C_RegisterAddrCallback() |
|
- | 222 | to register an interrupt callback. |
|
- | 223 | [..] |
|
- | 224 | Function @ref HAL_I2C_RegisterCallback() allows to register following callbacks: |
|
- | 225 | (+) MasterTxCpltCallback : callback for Master transmission end of transfer. |
|
- | 226 | (+) MasterRxCpltCallback : callback for Master reception end of transfer. |
|
- | 227 | (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer. |
|
- | 228 | (+) SlaveRxCpltCallback : callback for Slave reception end of transfer. |
|
- | 229 | (+) ListenCpltCallback : callback for end of listen mode. |
|
- | 230 | (+) MemTxCpltCallback : callback for Memory transmission end of transfer. |
|
- | 231 | (+) MemRxCpltCallback : callback for Memory reception end of transfer. |
|
- | 232 | (+) ErrorCallback : callback for error detection. |
|
- | 233 | (+) AbortCpltCallback : callback for abort completion process. |
|
- | 234 | (+) MspInitCallback : callback for Msp Init. |
|
- | 235 | (+) MspDeInitCallback : callback for Msp DeInit. |
|
- | 236 | This function takes as parameters the HAL peripheral handle, the Callback ID |
|
- | 237 | and a pointer to the user callback function. |
|
- | 238 | [..] |
|
- | 239 | For specific callback AddrCallback use dedicated register callbacks : @ref HAL_I2C_RegisterAddrCallback(). |
|
- | 240 | [..] |
|
- | 241 | Use function @ref HAL_I2C_UnRegisterCallback to reset a callback to the default |
|
- | 242 | weak function. |
|
- | 243 | @ref HAL_I2C_UnRegisterCallback takes as parameters the HAL peripheral handle, |
|
- | 244 | and the Callback ID. |
|
- | 245 | This function allows to reset following callbacks: |
|
- | 246 | (+) MasterTxCpltCallback : callback for Master transmission end of transfer. |
|
- | 247 | (+) MasterRxCpltCallback : callback for Master reception end of transfer. |
|
- | 248 | (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer. |
|
- | 249 | (+) SlaveRxCpltCallback : callback for Slave reception end of transfer. |
|
- | 250 | (+) ListenCpltCallback : callback for end of listen mode. |
|
- | 251 | (+) MemTxCpltCallback : callback for Memory transmission end of transfer. |
|
- | 252 | (+) MemRxCpltCallback : callback for Memory reception end of transfer. |
|
- | 253 | (+) ErrorCallback : callback for error detection. |
|
- | 254 | (+) AbortCpltCallback : callback for abort completion process. |
|
- | 255 | (+) MspInitCallback : callback for Msp Init. |
|
- | 256 | (+) MspDeInitCallback : callback for Msp DeInit. |
|
- | 257 | [..] |
|
- | 258 | For callback AddrCallback use dedicated register callbacks : @ref HAL_I2C_UnRegisterAddrCallback(). |
|
- | 259 | [..] |
|
- | 260 | By default, after the @ref HAL_I2C_Init() and when the state is @ref HAL_I2C_STATE_RESET |
|
- | 261 | all callbacks are set to the corresponding weak functions: |
|
- | 262 | examples @ref HAL_I2C_MasterTxCpltCallback(), @ref HAL_I2C_MasterRxCpltCallback(). |
|
- | 263 | Exception done for MspInit and MspDeInit functions that are |
|
- | 264 | reset to the legacy weak functions in the @ref HAL_I2C_Init()/ @ref HAL_I2C_DeInit() only when |
|
- | 265 | these callbacks are null (not registered beforehand). |
|
- | 266 | If MspInit or MspDeInit are not null, the @ref HAL_I2C_Init()/ @ref HAL_I2C_DeInit() |
|
- | 267 | keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state. |
|
- | 268 | [..] |
|
- | 269 | Callbacks can be registered/unregistered in @ref HAL_I2C_STATE_READY state only. |
|
- | 270 | Exception done MspInit/MspDeInit functions that can be registered/unregistered |
|
- | 271 | in @ref HAL_I2C_STATE_READY or @ref HAL_I2C_STATE_RESET state, |
|
- | 272 | thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit. |
|
- | 273 | Then, the user first registers the MspInit/MspDeInit user callbacks |
|
- | 274 | using @ref HAL_I2C_RegisterCallback() before calling @ref HAL_I2C_DeInit() |
|
- | 275 | or @ref HAL_I2C_Init() function. |
|
- | 276 | [..] |
|
- | 277 | When the compilation flag USE_HAL_I2C_REGISTER_CALLBACKS is set to 0 or |
|
- | 278 | not defined, the callback registration feature is not available and all callbacks |
|
- | 279 | are set to the corresponding weak functions. |
|
192 | 280 | ||
193 | [..] |
- | |
194 | (@) You can refer to the I2C HAL driver header file for more useful macros |
- | |
195 | 281 | ||
196 | *** I2C Workarounds linked to Silicon Limitation *** |
282 | *** I2C Workarounds linked to Silicon Limitation *** |
197 | ==================================================== |
283 | ==================================================== |
198 | [..] |
284 | [..] |
199 | Below the list of all silicon limitations implemented for HAL on STM32F1xx product. |
285 | Below the list of all silicon limitations implemented for HAL on STM32F1xx product. |
200 | (@) See ErrataSheet to know full silicon limitation list of your product. |
286 | (@) See ErrataSheet to know full silicon limitation list of your product. |
201 | 287 | ||
202 | (#) Workarounds Implemented inside I2C HAL Driver |
288 | (+) Workarounds Implemented inside I2C HAL Driver |
203 | (##) Wrong data read into data register (Polling and Interrupt mode) |
289 | (++) Wrong data read into data register (Polling and Interrupt mode) |
204 | (##) Start cannot be generated after a misplaced Stop |
290 | (++) Start cannot be generated after a misplaced Stop |
205 | (##) Some software events must be managed before the current byte is being transferred: |
291 | (++) Some software events must be managed before the current byte is being transferred: |
206 | Workaround: Use DMA in general, except when the Master is receiving a single byte. |
292 | Workaround: Use DMA in general, except when the Master is receiving a single byte. |
207 | For Interupt mode, I2C should have the highest priority in the application. |
293 | For Interupt mode, I2C should have the highest priority in the application. |
208 | (##) Mismatch on the "Setup time for a repeated Start condition" timing parameter: |
294 | (++) Mismatch on the "Setup time for a repeated Start condition" timing parameter: |
209 | Workaround: Reduce the frequency down to 88 kHz or use the I2C Fast-mode if |
295 | Workaround: Reduce the frequency down to 88 kHz or use the I2C Fast-mode if |
210 | supported by the slave. |
296 | supported by the slave. |
211 | (##) Data valid time (tVD;DAT) violated without the OVR flag being set: |
297 | (++) Data valid time (tVD;DAT) violated without the OVR flag being set: |
212 | Workaround: If the slave device allows it, use the clock stretching mechanism |
298 | Workaround: If the slave device allows it, use the clock stretching mechanism |
213 | by programming NoStretchMode = I2C_NOSTRETCH_DISABLE in HAL_I2C_Init. |
299 | by programming NoStretchMode = I2C_NOSTRETCH_DISABLE in @ref HAL_I2C_Init. |
- | 300 | ||
- | 301 | [..] |
|
- | 302 | (@) You can refer to the I2C HAL driver header file for more useful macros |
|
214 | 303 | ||
215 | @endverbatim |
304 | @endverbatim |
216 | ****************************************************************************** |
305 | ****************************************************************************** |
217 | * @attention |
306 | * @attention |
218 | * |
307 | * |
219 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
308 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
- | 309 | * All rights reserved.</center></h2> |
|
220 | * |
310 | * |
221 | * Redistribution and use in source and binary forms, with or without modification, |
311 | * This software component is licensed by ST under BSD 3-Clause license, |
222 | * are permitted provided that the following conditions are met: |
312 | * the "License"; You may not use this file except in compliance with the |
223 | * 1. Redistributions of source code must retain the above copyright notice, |
- | |
224 | * this list of conditions and the following disclaimer. |
- | |
225 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
- | |
226 | * this list of conditions and the following disclaimer in the documentation |
- | |
227 | * and/or other materials provided with the distribution. |
313 | * License. You may obtain a copy of the License at: |
228 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
- | |
229 | * may be used to endorse or promote products derived from this software |
314 | * opensource.org/licenses/BSD-3-Clause |
230 | * without specific prior written permission. |
- | |
231 | * |
- | |
232 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
- | |
233 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
- | |
234 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
- | |
235 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
- | |
236 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
- | |
237 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
- | |
238 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
- | |
239 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
- | |
240 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
- | |
241 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
- | |
242 | * |
315 | * |
243 | ****************************************************************************** |
316 | ****************************************************************************** |
244 | */ |
317 | */ |
245 | 318 | ||
246 | /* Includes ------------------------------------------------------------------*/ |
319 | /* Includes ------------------------------------------------------------------*/ |
Line 259... | Line 332... | ||
259 | 332 | ||
260 | /* Private typedef -----------------------------------------------------------*/ |
333 | /* Private typedef -----------------------------------------------------------*/ |
261 | /* Private define ------------------------------------------------------------*/ |
334 | /* Private define ------------------------------------------------------------*/ |
262 | /** @addtogroup I2C_Private_Define |
335 | /** @addtogroup I2C_Private_Define |
263 | * @{ |
336 | * @{ |
264 | */ |
337 | */ |
265 | #define I2C_TIMEOUT_FLAG 35U /*!< Timeout 35 ms */ |
338 | #define I2C_TIMEOUT_FLAG 35U /*!< Timeout 35 ms */ |
266 | #define I2C_TIMEOUT_BUSY_FLAG 25U /*!< Timeout 25 ms */ |
339 | #define I2C_TIMEOUT_BUSY_FLAG 25U /*!< Timeout 25 ms */ |
- | 340 | #define I2C_TIMEOUT_STOP_FLAG 5U /*!< Timeout 5 ms */ |
|
267 | #define I2C_NO_OPTION_FRAME 0xFFFF0000U /*!< XferOptions default value */ |
341 | #define I2C_NO_OPTION_FRAME 0xFFFF0000U /*!< XferOptions default value */ |
268 | 342 | ||
269 | /* Private define for @ref PreviousState usage */ |
343 | /* Private define for @ref PreviousState usage */ |
270 | #define I2C_STATE_MSK ((uint32_t)((HAL_I2C_STATE_BUSY_TX | HAL_I2C_STATE_BUSY_RX) & (~(uint32_t)HAL_I2C_STATE_READY))) /*!< Mask State define, keep only RX and TX bits */ |
344 | #define I2C_STATE_MSK ((uint32_t)((uint32_t)((uint32_t)HAL_I2C_STATE_BUSY_TX | (uint32_t)HAL_I2C_STATE_BUSY_RX) & (uint32_t)(~((uint32_t)HAL_I2C_STATE_READY)))) /*!< Mask State define, keep only RX and TX bits */ |
271 | #define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE)) /*!< Default Value */ |
345 | #define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE)) /*!< Default Value */ |
272 | #define I2C_STATE_MASTER_BUSY_TX ((uint32_t)((HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | HAL_I2C_MODE_MASTER)) /*!< Master Busy TX, combinaison of State LSB and Mode enum */ |
346 | #define I2C_STATE_MASTER_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER)) /*!< Master Busy TX, combinaison of State LSB and Mode enum */ |
273 | #define I2C_STATE_MASTER_BUSY_RX ((uint32_t)((HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | HAL_I2C_MODE_MASTER)) /*!< Master Busy RX, combinaison of State LSB and Mode enum */ |
347 | #define I2C_STATE_MASTER_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER)) /*!< Master Busy RX, combinaison of State LSB and Mode enum */ |
274 | #define I2C_STATE_SLAVE_BUSY_TX ((uint32_t)((HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | HAL_I2C_MODE_SLAVE)) /*!< Slave Busy TX, combinaison of State LSB and Mode enum */ |
348 | #define I2C_STATE_SLAVE_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE)) /*!< Slave Busy TX, combinaison of State LSB and Mode enum */ |
275 | #define I2C_STATE_SLAVE_BUSY_RX ((uint32_t)((HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | HAL_I2C_MODE_SLAVE)) /*!< Slave Busy RX, combinaison of State LSB and Mode enum */ |
349 | #define I2C_STATE_SLAVE_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE)) /*!< Slave Busy RX, combinaison of State LSB and Mode enum */ |
276 | 350 | ||
277 | /** |
351 | /** |
278 | * @} |
352 | * @} |
279 | */ |
353 | */ |
280 | 354 | ||
281 | /* Private macro -------------------------------------------------------------*/ |
355 | /* Private macro -------------------------------------------------------------*/ |
282 | /* Private variables ---------------------------------------------------------*/ |
356 | /* Private variables ---------------------------------------------------------*/ |
283 | /* Private function prototypes -----------------------------------------------*/ |
357 | /* Private function prototypes -----------------------------------------------*/ |
- | 358 | ||
284 | /** @addtogroup I2C_Private_Functions |
359 | /** @defgroup I2C_Private_Functions I2C Private Functions |
285 | * @{ |
360 | * @{ |
286 | */ |
361 | */ |
287 | /* Private functions to handle DMA transfer */ |
362 | /* Private functions to handle DMA transfer */ |
288 | static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma); |
363 | static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma); |
289 | static void I2C_DMAError(DMA_HandleTypeDef *hdma); |
364 | static void I2C_DMAError(DMA_HandleTypeDef *hdma); |
Line 293... | Line 368... | ||
293 | 368 | ||
294 | static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart); |
369 | static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart); |
295 | static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart); |
370 | static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart); |
296 | static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart); |
371 | static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart); |
297 | static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart); |
372 | static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart); |
- | 373 | ||
- | 374 | /* Private functions to handle flags during polling transfer */ |
|
298 | static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart); |
375 | static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart); |
299 | static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart); |
376 | static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart); |
300 | static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart); |
377 | static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart); |
301 | static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart); |
378 | static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart); |
302 | static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart); |
379 | static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart); |
303 | static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart); |
380 | static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart); |
- | 381 | static HAL_StatusTypeDef I2C_WaitOnSTOPRequestThroughIT(I2C_HandleTypeDef *hi2c); |
|
304 | static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c); |
382 | static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c); |
305 | 383 | ||
306 | /* Private functions for I2C transfer IRQ handler */ |
384 | /* Private functions for I2C transfer IRQ handler */ |
307 | static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c); |
385 | static void I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c); |
308 | static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c); |
386 | static void I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c); |
309 | static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c); |
387 | static void I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c); |
310 | static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c); |
388 | static void I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c); |
311 | static HAL_StatusTypeDef I2C_Master_SB(I2C_HandleTypeDef *hi2c); |
389 | static void I2C_Master_SB(I2C_HandleTypeDef *hi2c); |
312 | static HAL_StatusTypeDef I2C_Master_ADD10(I2C_HandleTypeDef *hi2c); |
390 | static void I2C_Master_ADD10(I2C_HandleTypeDef *hi2c); |
313 | static HAL_StatusTypeDef I2C_Master_ADDR(I2C_HandleTypeDef *hi2c); |
391 | static void I2C_Master_ADDR(I2C_HandleTypeDef *hi2c); |
314 | 392 | ||
315 | static HAL_StatusTypeDef I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c); |
393 | static void I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c); |
316 | static HAL_StatusTypeDef I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c); |
394 | static void I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c); |
317 | static HAL_StatusTypeDef I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c); |
395 | static void I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c); |
318 | static HAL_StatusTypeDef I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c); |
396 | static void I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c); |
319 | static HAL_StatusTypeDef I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c); |
397 | static void I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c, uint32_t IT2Flags); |
320 | static HAL_StatusTypeDef I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c); |
398 | static void I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c); |
321 | static HAL_StatusTypeDef I2C_Slave_AF(I2C_HandleTypeDef *hi2c); |
399 | static void I2C_Slave_AF(I2C_HandleTypeDef *hi2c); |
- | 400 | ||
- | 401 | static void I2C_MemoryTransmit_TXE_BTF(I2C_HandleTypeDef *hi2c); |
|
- | 402 | ||
- | 403 | /* Private function to Convert Specific options */ |
|
- | 404 | static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c); |
|
322 | /** |
405 | /** |
323 | * @} |
406 | * @} |
324 | */ |
407 | */ |
325 | 408 | ||
326 | /* Exported functions --------------------------------------------------------*/ |
409 | /* Exported functions --------------------------------------------------------*/ |
- | 410 | ||
327 | /** @defgroup I2C_Exported_Functions I2C Exported Functions |
411 | /** @defgroup I2C_Exported_Functions I2C Exported Functions |
328 | * @{ |
412 | * @{ |
329 | */ |
413 | */ |
330 | 414 | ||
331 | /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions |
415 | /** @defgroup I2C_Exported_Functions_Group1 Initialization and de-initialization functions |
Line 334... | Line 418... | ||
334 | @verbatim |
418 | @verbatim |
335 | =============================================================================== |
419 | =============================================================================== |
336 | ##### Initialization and de-initialization functions ##### |
420 | ##### Initialization and de-initialization functions ##### |
337 | =============================================================================== |
421 | =============================================================================== |
338 | [..] This subsection provides a set of functions allowing to initialize and |
422 | [..] This subsection provides a set of functions allowing to initialize and |
339 | de-initialize the I2Cx peripheral: |
423 | deinitialize the I2Cx peripheral: |
340 | 424 | ||
341 | (+) User must Implement HAL_I2C_MspInit() function in which he configures |
425 | (+) User must Implement HAL_I2C_MspInit() function in which he configures |
342 | all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC). |
426 | all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC). |
343 | 427 | ||
344 | (+) Call the function HAL_I2C_Init() to configure the selected device with |
428 | (+) Call the function HAL_I2C_Init() to configure the selected device with |
Line 359... | Line 443... | ||
359 | * @{ |
443 | * @{ |
360 | */ |
444 | */ |
361 | 445 | ||
362 | /** |
446 | /** |
363 | * @brief Initializes the I2C according to the specified parameters |
447 | * @brief Initializes the I2C according to the specified parameters |
364 | * in the I2C_InitTypeDef and create the associated handle. |
448 | * in the I2C_InitTypeDef and initialize the associated handle. |
365 | * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains |
449 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
366 | * the configuration information for I2C module |
450 | * the configuration information for the specified I2C. |
367 | * @retval HAL status |
451 | * @retval HAL status |
368 | */ |
452 | */ |
369 | HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c) |
453 | HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c) |
370 | { |
454 | { |
371 | uint32_t freqrange = 0U; |
455 | uint32_t freqrange; |
372 | uint32_t pclk1 = 0U; |
456 | uint32_t pclk1; |
373 | 457 | ||
374 | /* Check the I2C handle allocation */ |
458 | /* Check the I2C handle allocation */ |
375 | if(hi2c == NULL) |
459 | if (hi2c == NULL) |
376 | { |
460 | { |
377 | return HAL_ERROR; |
461 | return HAL_ERROR; |
378 | } |
462 | } |
379 | 463 | ||
380 | /* Check the parameters */ |
464 | /* Check the parameters */ |
Line 386... | Line 470... | ||
386 | assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode)); |
470 | assert_param(IS_I2C_DUAL_ADDRESS(hi2c->Init.DualAddressMode)); |
387 | assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2)); |
471 | assert_param(IS_I2C_OWN_ADDRESS2(hi2c->Init.OwnAddress2)); |
388 | assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode)); |
472 | assert_param(IS_I2C_GENERAL_CALL(hi2c->Init.GeneralCallMode)); |
389 | assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode)); |
473 | assert_param(IS_I2C_NO_STRETCH(hi2c->Init.NoStretchMode)); |
390 | 474 | ||
391 | if(hi2c->State == HAL_I2C_STATE_RESET) |
475 | if (hi2c->State == HAL_I2C_STATE_RESET) |
392 | { |
476 | { |
393 | /* Allocate lock resource and initialize it */ |
477 | /* Allocate lock resource and initialize it */ |
394 | hi2c->Lock = HAL_UNLOCKED; |
478 | hi2c->Lock = HAL_UNLOCKED; |
- | 479 | ||
- | 480 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 481 | /* Init the I2C Callback settings */ |
|
- | 482 | hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */ |
|
- | 483 | hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */ |
|
- | 484 | hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */ |
|
- | 485 | hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */ |
|
- | 486 | hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */ |
|
- | 487 | hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */ |
|
- | 488 | hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */ |
|
- | 489 | hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */ |
|
- | 490 | hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ |
|
- | 491 | hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */ |
|
- | 492 | ||
- | 493 | if (hi2c->MspInitCallback == NULL) |
|
- | 494 | { |
|
- | 495 | hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */ |
|
- | 496 | } |
|
- | 497 | ||
- | 498 | /* Init the low level hardware : GPIO, CLOCK, NVIC */ |
|
- | 499 | hi2c->MspInitCallback(hi2c); |
|
- | 500 | #else |
|
395 | /* Init the low level hardware : GPIO, CLOCK, NVIC */ |
501 | /* Init the low level hardware : GPIO, CLOCK, NVIC */ |
396 | HAL_I2C_MspInit(hi2c); |
502 | HAL_I2C_MspInit(hi2c); |
- | 503 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
397 | } |
504 | } |
398 | 505 | ||
399 | hi2c->State = HAL_I2C_STATE_BUSY; |
506 | hi2c->State = HAL_I2C_STATE_BUSY; |
400 | 507 | ||
401 | /* Disable the selected I2C peripheral */ |
508 | /* Disable the selected I2C peripheral */ |
402 | __HAL_I2C_DISABLE(hi2c); |
509 | __HAL_I2C_DISABLE(hi2c); |
403 | 510 | ||
- | 511 | /*Reset I2C*/ |
|
- | 512 | hi2c->Instance->CR1 |= I2C_CR1_SWRST; |
|
- | 513 | hi2c->Instance->CR1 &= ~I2C_CR1_SWRST; |
|
- | 514 | ||
404 | /* Get PCLK1 frequency */ |
515 | /* Get PCLK1 frequency */ |
405 | pclk1 = HAL_RCC_GetPCLK1Freq(); |
516 | pclk1 = HAL_RCC_GetPCLK1Freq(); |
406 | 517 | ||
407 | /* Check the minimum allowed PCLK1 frequency */ |
518 | /* Check the minimum allowed PCLK1 frequency */ |
408 | if (I2C_MIN_PCLK_FREQ(pclk1, hi2c->Init.ClockSpeed) == 1U) |
519 | if (I2C_MIN_PCLK_FREQ(pclk1, hi2c->Init.ClockSpeed) == 1U) |
Line 413... | Line 524... | ||
413 | /* Calculate frequency range */ |
524 | /* Calculate frequency range */ |
414 | freqrange = I2C_FREQRANGE(pclk1); |
525 | freqrange = I2C_FREQRANGE(pclk1); |
415 | 526 | ||
416 | /*---------------------------- I2Cx CR2 Configuration ----------------------*/ |
527 | /*---------------------------- I2Cx CR2 Configuration ----------------------*/ |
417 | /* Configure I2Cx: Frequency range */ |
528 | /* Configure I2Cx: Frequency range */ |
418 | hi2c->Instance->CR2 = freqrange; |
529 | MODIFY_REG(hi2c->Instance->CR2, I2C_CR2_FREQ, freqrange); |
419 | 530 | ||
420 | /*---------------------------- I2Cx TRISE Configuration --------------------*/ |
531 | /*---------------------------- I2Cx TRISE Configuration --------------------*/ |
421 | /* Configure I2Cx: Rise Time */ |
532 | /* Configure I2Cx: Rise Time */ |
422 | hi2c->Instance->TRISE = I2C_RISE_TIME(freqrange, hi2c->Init.ClockSpeed); |
533 | MODIFY_REG(hi2c->Instance->TRISE, I2C_TRISE_TRISE, I2C_RISE_TIME(freqrange, hi2c->Init.ClockSpeed)); |
423 | 534 | ||
424 | /*---------------------------- I2Cx CCR Configuration ----------------------*/ |
535 | /*---------------------------- I2Cx CCR Configuration ----------------------*/ |
425 | /* Configure I2Cx: Speed */ |
536 | /* Configure I2Cx: Speed */ |
426 | hi2c->Instance->CCR = I2C_SPEED(pclk1, hi2c->Init.ClockSpeed, hi2c->Init.DutyCycle); |
537 | MODIFY_REG(hi2c->Instance->CCR, (I2C_CCR_FS | I2C_CCR_DUTY | I2C_CCR_CCR), I2C_SPEED(pclk1, hi2c->Init.ClockSpeed, hi2c->Init.DutyCycle)); |
427 | 538 | ||
428 | /*---------------------------- I2Cx CR1 Configuration ----------------------*/ |
539 | /*---------------------------- I2Cx CR1 Configuration ----------------------*/ |
429 | /* Configure I2Cx: Generalcall and NoStretch mode */ |
540 | /* Configure I2Cx: Generalcall and NoStretch mode */ |
430 | hi2c->Instance->CR1 = (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode); |
541 | MODIFY_REG(hi2c->Instance->CR1, (I2C_CR1_ENGC | I2C_CR1_NOSTRETCH), (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode)); |
431 | 542 | ||
432 | /*---------------------------- I2Cx OAR1 Configuration ---------------------*/ |
543 | /*---------------------------- I2Cx OAR1 Configuration ---------------------*/ |
433 | /* Configure I2Cx: Own Address1 and addressing mode */ |
544 | /* Configure I2Cx: Own Address1 and addressing mode */ |
434 | hi2c->Instance->OAR1 = (hi2c->Init.AddressingMode | hi2c->Init.OwnAddress1); |
545 | MODIFY_REG(hi2c->Instance->OAR1, (I2C_OAR1_ADDMODE | I2C_OAR1_ADD8_9 | I2C_OAR1_ADD1_7 | I2C_OAR1_ADD0), (hi2c->Init.AddressingMode | hi2c->Init.OwnAddress1)); |
435 | 546 | ||
436 | /*---------------------------- I2Cx OAR2 Configuration ---------------------*/ |
547 | /*---------------------------- I2Cx OAR2 Configuration ---------------------*/ |
437 | /* Configure I2Cx: Dual mode and Own Address2 */ |
548 | /* Configure I2Cx: Dual mode and Own Address2 */ |
438 | hi2c->Instance->OAR2 = (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2); |
549 | MODIFY_REG(hi2c->Instance->OAR2, (I2C_OAR2_ENDUAL | I2C_OAR2_ADD2), (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2)); |
439 | 550 | ||
440 | /* Enable the selected I2C peripheral */ |
551 | /* Enable the selected I2C peripheral */ |
441 | __HAL_I2C_ENABLE(hi2c); |
552 | __HAL_I2C_ENABLE(hi2c); |
442 | 553 | ||
443 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
554 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
Line 447... | Line 558... | ||
447 | 558 | ||
448 | return HAL_OK; |
559 | return HAL_OK; |
449 | } |
560 | } |
450 | 561 | ||
451 | /** |
562 | /** |
452 | * @brief DeInitializes the I2C peripheral. |
563 | * @brief DeInitialize the I2C peripheral. |
453 | * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains |
564 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
454 | * the configuration information for I2C module |
565 | * the configuration information for the specified I2C. |
455 | * @retval HAL status |
566 | * @retval HAL status |
456 | */ |
567 | */ |
457 | HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c) |
568 | HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c) |
458 | { |
569 | { |
459 | /* Check the I2C handle allocation */ |
570 | /* Check the I2C handle allocation */ |
460 | if(hi2c == NULL) |
571 | if (hi2c == NULL) |
461 | { |
572 | { |
462 | return HAL_ERROR; |
573 | return HAL_ERROR; |
463 | } |
574 | } |
464 | 575 | ||
465 | /* Check the parameters */ |
576 | /* Check the parameters */ |
Line 468... | Line 579... | ||
468 | hi2c->State = HAL_I2C_STATE_BUSY; |
579 | hi2c->State = HAL_I2C_STATE_BUSY; |
469 | 580 | ||
470 | /* Disable the I2C Peripheral Clock */ |
581 | /* Disable the I2C Peripheral Clock */ |
471 | __HAL_I2C_DISABLE(hi2c); |
582 | __HAL_I2C_DISABLE(hi2c); |
472 | 583 | ||
- | 584 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 585 | if (hi2c->MspDeInitCallback == NULL) |
|
- | 586 | { |
|
- | 587 | hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */ |
|
- | 588 | } |
|
- | 589 | ||
- | 590 | /* DeInit the low level hardware: GPIO, CLOCK, NVIC */ |
|
- | 591 | hi2c->MspDeInitCallback(hi2c); |
|
- | 592 | #else |
|
473 | /* DeInit the low level hardware: GPIO, CLOCK, NVIC */ |
593 | /* DeInit the low level hardware: GPIO, CLOCK, NVIC */ |
474 | HAL_I2C_MspDeInit(hi2c); |
594 | HAL_I2C_MspDeInit(hi2c); |
- | 595 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
475 | 596 | ||
476 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
597 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
477 | hi2c->State = HAL_I2C_STATE_RESET; |
598 | hi2c->State = HAL_I2C_STATE_RESET; |
478 | hi2c->PreviousState = I2C_STATE_NONE; |
599 | hi2c->PreviousState = I2C_STATE_NONE; |
479 | hi2c->Mode = HAL_I2C_MODE_NONE; |
600 | hi2c->Mode = HAL_I2C_MODE_NONE; |
Line 483... | Line 604... | ||
483 | 604 | ||
484 | return HAL_OK; |
605 | return HAL_OK; |
485 | } |
606 | } |
486 | 607 | ||
487 | /** |
608 | /** |
488 | * @brief I2C MSP Init. |
609 | * @brief Initialize the I2C MSP. |
489 | * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains |
610 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
490 | * the configuration information for I2C module |
611 | * the configuration information for the specified I2C. |
491 | * @retval None |
612 | * @retval None |
492 | */ |
613 | */ |
493 | __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c) |
614 | __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c) |
494 | { |
615 | { |
495 | /* Prevent unused argument(s) compilation warning */ |
616 | /* Prevent unused argument(s) compilation warning */ |
496 | UNUSED(hi2c); |
617 | UNUSED(hi2c); |
- | 618 | ||
497 | /* NOTE : This function Should not be modified, when the callback is needed, |
619 | /* NOTE : This function should not be modified, when the callback is needed, |
498 | the HAL_I2C_MspInit could be implemented in the user file |
620 | the HAL_I2C_MspInit could be implemented in the user file |
499 | */ |
621 | */ |
500 | } |
622 | } |
501 | 623 | ||
502 | /** |
624 | /** |
503 | * @brief I2C MSP DeInit |
625 | * @brief DeInitialize the I2C MSP. |
504 | * @param hi2c: pointer to a I2C_HandleTypeDef structure that contains |
626 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
505 | * the configuration information for I2C module |
627 | * the configuration information for the specified I2C. |
506 | * @retval None |
628 | * @retval None |
507 | */ |
629 | */ |
508 | __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c) |
630 | __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c) |
509 | { |
631 | { |
510 | /* Prevent unused argument(s) compilation warning */ |
632 | /* Prevent unused argument(s) compilation warning */ |
511 | UNUSED(hi2c); |
633 | UNUSED(hi2c); |
- | 634 | ||
512 | /* NOTE : This function Should not be modified, when the callback is needed, |
635 | /* NOTE : This function should not be modified, when the callback is needed, |
513 | the HAL_I2C_MspDeInit could be implemented in the user file |
636 | the HAL_I2C_MspDeInit could be implemented in the user file |
514 | */ |
637 | */ |
515 | } |
638 | } |
516 | 639 | ||
- | 640 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 641 | /** |
|
- | 642 | * @brief Register a User I2C Callback |
|
- | 643 | * To be used instead of the weak predefined callback |
|
- | 644 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 645 | * the configuration information for the specified I2C. |
|
- | 646 | * @param CallbackID ID of the callback to be registered |
|
- | 647 | * This parameter can be one of the following values: |
|
- | 648 | * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID |
|
- | 649 | * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID |
|
- | 650 | * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID |
|
- | 651 | * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID |
|
- | 652 | * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID |
|
- | 653 | * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID |
|
- | 654 | * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID |
|
- | 655 | * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID |
|
- | 656 | * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID |
|
- | 657 | * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID |
|
- | 658 | * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID |
|
- | 659 | * @param pCallback pointer to the Callback function |
|
- | 660 | * @retval HAL status |
|
- | 661 | */ |
|
- | 662 | HAL_StatusTypeDef HAL_I2C_RegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID, pI2C_CallbackTypeDef pCallback) |
|
- | 663 | { |
|
- | 664 | HAL_StatusTypeDef status = HAL_OK; |
|
- | 665 | ||
- | 666 | if (pCallback == NULL) |
|
- | 667 | { |
|
- | 668 | /* Update the error code */ |
|
- | 669 | hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; |
|
- | 670 | ||
- | 671 | return HAL_ERROR; |
|
- | 672 | } |
|
- | 673 | /* Process locked */ |
|
- | 674 | __HAL_LOCK(hi2c); |
|
- | 675 | ||
- | 676 | if (HAL_I2C_STATE_READY == hi2c->State) |
|
- | 677 | { |
|
- | 678 | switch (CallbackID) |
|
- | 679 | { |
|
- | 680 | case HAL_I2C_MASTER_TX_COMPLETE_CB_ID : |
|
- | 681 | hi2c->MasterTxCpltCallback = pCallback; |
|
- | 682 | break; |
|
- | 683 | ||
- | 684 | case HAL_I2C_MASTER_RX_COMPLETE_CB_ID : |
|
- | 685 | hi2c->MasterRxCpltCallback = pCallback; |
|
- | 686 | break; |
|
- | 687 | ||
- | 688 | case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID : |
|
- | 689 | hi2c->SlaveTxCpltCallback = pCallback; |
|
- | 690 | break; |
|
- | 691 | ||
- | 692 | case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID : |
|
- | 693 | hi2c->SlaveRxCpltCallback = pCallback; |
|
- | 694 | break; |
|
- | 695 | ||
- | 696 | case HAL_I2C_LISTEN_COMPLETE_CB_ID : |
|
- | 697 | hi2c->ListenCpltCallback = pCallback; |
|
- | 698 | break; |
|
- | 699 | ||
- | 700 | case HAL_I2C_MEM_TX_COMPLETE_CB_ID : |
|
- | 701 | hi2c->MemTxCpltCallback = pCallback; |
|
- | 702 | break; |
|
- | 703 | ||
- | 704 | case HAL_I2C_MEM_RX_COMPLETE_CB_ID : |
|
- | 705 | hi2c->MemRxCpltCallback = pCallback; |
|
- | 706 | break; |
|
- | 707 | ||
- | 708 | case HAL_I2C_ERROR_CB_ID : |
|
- | 709 | hi2c->ErrorCallback = pCallback; |
|
- | 710 | break; |
|
- | 711 | ||
- | 712 | case HAL_I2C_ABORT_CB_ID : |
|
- | 713 | hi2c->AbortCpltCallback = pCallback; |
|
- | 714 | break; |
|
- | 715 | ||
- | 716 | case HAL_I2C_MSPINIT_CB_ID : |
|
- | 717 | hi2c->MspInitCallback = pCallback; |
|
- | 718 | break; |
|
- | 719 | ||
- | 720 | case HAL_I2C_MSPDEINIT_CB_ID : |
|
- | 721 | hi2c->MspDeInitCallback = pCallback; |
|
- | 722 | break; |
|
- | 723 | ||
- | 724 | default : |
|
- | 725 | /* Update the error code */ |
|
- | 726 | hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; |
|
- | 727 | ||
- | 728 | /* Return error status */ |
|
- | 729 | status = HAL_ERROR; |
|
- | 730 | break; |
|
- | 731 | } |
|
- | 732 | } |
|
- | 733 | else if (HAL_I2C_STATE_RESET == hi2c->State) |
|
- | 734 | { |
|
- | 735 | switch (CallbackID) |
|
- | 736 | { |
|
- | 737 | case HAL_I2C_MSPINIT_CB_ID : |
|
- | 738 | hi2c->MspInitCallback = pCallback; |
|
- | 739 | break; |
|
- | 740 | ||
- | 741 | case HAL_I2C_MSPDEINIT_CB_ID : |
|
- | 742 | hi2c->MspDeInitCallback = pCallback; |
|
- | 743 | break; |
|
- | 744 | ||
- | 745 | default : |
|
- | 746 | /* Update the error code */ |
|
- | 747 | hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; |
|
- | 748 | ||
- | 749 | /* Return error status */ |
|
- | 750 | status = HAL_ERROR; |
|
- | 751 | break; |
|
- | 752 | } |
|
- | 753 | } |
|
- | 754 | else |
|
- | 755 | { |
|
- | 756 | /* Update the error code */ |
|
- | 757 | hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; |
|
- | 758 | ||
- | 759 | /* Return error status */ |
|
- | 760 | status = HAL_ERROR; |
|
- | 761 | } |
|
- | 762 | ||
- | 763 | /* Release Lock */ |
|
- | 764 | __HAL_UNLOCK(hi2c); |
|
- | 765 | return status; |
|
- | 766 | } |
|
- | 767 | ||
- | 768 | /** |
|
- | 769 | * @brief Unregister an I2C Callback |
|
- | 770 | * I2C callback is redirected to the weak predefined callback |
|
- | 771 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 772 | * the configuration information for the specified I2C. |
|
- | 773 | * @param CallbackID ID of the callback to be unregistered |
|
- | 774 | * This parameter can be one of the following values: |
|
- | 775 | * This parameter can be one of the following values: |
|
- | 776 | * @arg @ref HAL_I2C_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID |
|
- | 777 | * @arg @ref HAL_I2C_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID |
|
- | 778 | * @arg @ref HAL_I2C_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID |
|
- | 779 | * @arg @ref HAL_I2C_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID |
|
- | 780 | * @arg @ref HAL_I2C_LISTEN_COMPLETE_CB_ID Listen Complete callback ID |
|
- | 781 | * @arg @ref HAL_I2C_MEM_TX_COMPLETE_CB_ID Memory Tx Transfer callback ID |
|
- | 782 | * @arg @ref HAL_I2C_MEM_RX_COMPLETE_CB_ID Memory Rx Transfer completed callback ID |
|
- | 783 | * @arg @ref HAL_I2C_ERROR_CB_ID Error callback ID |
|
- | 784 | * @arg @ref HAL_I2C_ABORT_CB_ID Abort callback ID |
|
- | 785 | * @arg @ref HAL_I2C_MSPINIT_CB_ID MspInit callback ID |
|
- | 786 | * @arg @ref HAL_I2C_MSPDEINIT_CB_ID MspDeInit callback ID |
|
- | 787 | * @retval HAL status |
|
- | 788 | */ |
|
- | 789 | HAL_StatusTypeDef HAL_I2C_UnRegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID) |
|
- | 790 | { |
|
- | 791 | HAL_StatusTypeDef status = HAL_OK; |
|
- | 792 | ||
- | 793 | /* Process locked */ |
|
- | 794 | __HAL_LOCK(hi2c); |
|
- | 795 | ||
- | 796 | if (HAL_I2C_STATE_READY == hi2c->State) |
|
- | 797 | { |
|
- | 798 | switch (CallbackID) |
|
- | 799 | { |
|
- | 800 | case HAL_I2C_MASTER_TX_COMPLETE_CB_ID : |
|
- | 801 | hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */ |
|
- | 802 | break; |
|
- | 803 | ||
- | 804 | case HAL_I2C_MASTER_RX_COMPLETE_CB_ID : |
|
- | 805 | hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */ |
|
- | 806 | break; |
|
- | 807 | ||
- | 808 | case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID : |
|
- | 809 | hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */ |
|
- | 810 | break; |
|
- | 811 | ||
- | 812 | case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID : |
|
- | 813 | hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */ |
|
- | 814 | break; |
|
- | 815 | ||
- | 816 | case HAL_I2C_LISTEN_COMPLETE_CB_ID : |
|
- | 817 | hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */ |
|
- | 818 | break; |
|
- | 819 | ||
- | 820 | case HAL_I2C_MEM_TX_COMPLETE_CB_ID : |
|
- | 821 | hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */ |
|
- | 822 | break; |
|
- | 823 | ||
- | 824 | case HAL_I2C_MEM_RX_COMPLETE_CB_ID : |
|
- | 825 | hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */ |
|
- | 826 | break; |
|
- | 827 | ||
- | 828 | case HAL_I2C_ERROR_CB_ID : |
|
- | 829 | hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */ |
|
- | 830 | break; |
|
- | 831 | ||
- | 832 | case HAL_I2C_ABORT_CB_ID : |
|
- | 833 | hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ |
|
- | 834 | break; |
|
- | 835 | ||
- | 836 | case HAL_I2C_MSPINIT_CB_ID : |
|
- | 837 | hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */ |
|
- | 838 | break; |
|
- | 839 | ||
- | 840 | case HAL_I2C_MSPDEINIT_CB_ID : |
|
- | 841 | hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */ |
|
- | 842 | break; |
|
- | 843 | ||
- | 844 | default : |
|
- | 845 | /* Update the error code */ |
|
- | 846 | hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; |
|
- | 847 | ||
- | 848 | /* Return error status */ |
|
- | 849 | status = HAL_ERROR; |
|
- | 850 | break; |
|
- | 851 | } |
|
- | 852 | } |
|
- | 853 | else if (HAL_I2C_STATE_RESET == hi2c->State) |
|
- | 854 | { |
|
- | 855 | switch (CallbackID) |
|
- | 856 | { |
|
- | 857 | case HAL_I2C_MSPINIT_CB_ID : |
|
- | 858 | hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */ |
|
- | 859 | break; |
|
- | 860 | ||
- | 861 | case HAL_I2C_MSPDEINIT_CB_ID : |
|
- | 862 | hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */ |
|
- | 863 | break; |
|
- | 864 | ||
- | 865 | default : |
|
- | 866 | /* Update the error code */ |
|
- | 867 | hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; |
|
- | 868 | ||
- | 869 | /* Return error status */ |
|
- | 870 | status = HAL_ERROR; |
|
- | 871 | break; |
|
- | 872 | } |
|
- | 873 | } |
|
- | 874 | else |
|
- | 875 | { |
|
- | 876 | /* Update the error code */ |
|
- | 877 | hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; |
|
- | 878 | ||
- | 879 | /* Return error status */ |
|
- | 880 | status = HAL_ERROR; |
|
- | 881 | } |
|
- | 882 | ||
- | 883 | /* Release Lock */ |
|
- | 884 | __HAL_UNLOCK(hi2c); |
|
- | 885 | return status; |
|
- | 886 | } |
|
- | 887 | ||
- | 888 | /** |
|
- | 889 | * @brief Register the Slave Address Match I2C Callback |
|
- | 890 | * To be used instead of the weak HAL_I2C_AddrCallback() predefined callback |
|
- | 891 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 892 | * the configuration information for the specified I2C. |
|
- | 893 | * @param pCallback pointer to the Address Match Callback function |
|
- | 894 | * @retval HAL status |
|
- | 895 | */ |
|
- | 896 | HAL_StatusTypeDef HAL_I2C_RegisterAddrCallback(I2C_HandleTypeDef *hi2c, pI2C_AddrCallbackTypeDef pCallback) |
|
- | 897 | { |
|
- | 898 | HAL_StatusTypeDef status = HAL_OK; |
|
- | 899 | ||
- | 900 | if (pCallback == NULL) |
|
- | 901 | { |
|
- | 902 | /* Update the error code */ |
|
- | 903 | hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; |
|
- | 904 | ||
- | 905 | return HAL_ERROR; |
|
- | 906 | } |
|
- | 907 | /* Process locked */ |
|
- | 908 | __HAL_LOCK(hi2c); |
|
- | 909 | ||
- | 910 | if (HAL_I2C_STATE_READY == hi2c->State) |
|
- | 911 | { |
|
- | 912 | hi2c->AddrCallback = pCallback; |
|
- | 913 | } |
|
- | 914 | else |
|
- | 915 | { |
|
- | 916 | /* Update the error code */ |
|
- | 917 | hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; |
|
- | 918 | ||
- | 919 | /* Return error status */ |
|
- | 920 | status = HAL_ERROR; |
|
- | 921 | } |
|
- | 922 | ||
- | 923 | /* Release Lock */ |
|
- | 924 | __HAL_UNLOCK(hi2c); |
|
- | 925 | return status; |
|
- | 926 | } |
|
- | 927 | ||
- | 928 | /** |
|
- | 929 | * @brief UnRegister the Slave Address Match I2C Callback |
|
- | 930 | * Info Ready I2C Callback is redirected to the weak HAL_I2C_AddrCallback() predefined callback |
|
- | 931 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 932 | * the configuration information for the specified I2C. |
|
- | 933 | * @retval HAL status |
|
- | 934 | */ |
|
- | 935 | HAL_StatusTypeDef HAL_I2C_UnRegisterAddrCallback(I2C_HandleTypeDef *hi2c) |
|
- | 936 | { |
|
- | 937 | HAL_StatusTypeDef status = HAL_OK; |
|
- | 938 | ||
- | 939 | /* Process locked */ |
|
- | 940 | __HAL_LOCK(hi2c); |
|
- | 941 | ||
- | 942 | if (HAL_I2C_STATE_READY == hi2c->State) |
|
- | 943 | { |
|
- | 944 | hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */ |
|
- | 945 | } |
|
- | 946 | else |
|
- | 947 | { |
|
- | 948 | /* Update the error code */ |
|
- | 949 | hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK; |
|
- | 950 | ||
- | 951 | /* Return error status */ |
|
- | 952 | status = HAL_ERROR; |
|
- | 953 | } |
|
- | 954 | ||
- | 955 | /* Release Lock */ |
|
- | 956 | __HAL_UNLOCK(hi2c); |
|
- | 957 | return status; |
|
- | 958 | } |
|
- | 959 | ||
- | 960 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
- | 961 | ||
517 | /** |
962 | /** |
518 | * @} |
963 | * @} |
519 | */ |
964 | */ |
520 | 965 | ||
521 | /** @defgroup I2C_Exported_Functions_Group2 IO operation functions |
966 | /** @defgroup I2C_Exported_Functions_Group2 Input and Output operation functions |
522 | * @brief Data transfers functions |
967 | * @brief Data transfers functions |
523 | * |
968 | * |
524 | @verbatim |
969 | @verbatim |
525 | =============================================================================== |
970 | =============================================================================== |
526 | ##### IO operation functions ##### |
971 | ##### IO operation functions ##### |
Line 551... | Line 996... | ||
551 | (#) No-Blocking mode functions with Interrupt are : |
996 | (#) No-Blocking mode functions with Interrupt are : |
552 | (++) HAL_I2C_Master_Transmit_IT() |
997 | (++) HAL_I2C_Master_Transmit_IT() |
553 | (++) HAL_I2C_Master_Receive_IT() |
998 | (++) HAL_I2C_Master_Receive_IT() |
554 | (++) HAL_I2C_Slave_Transmit_IT() |
999 | (++) HAL_I2C_Slave_Transmit_IT() |
555 | (++) HAL_I2C_Slave_Receive_IT() |
1000 | (++) HAL_I2C_Slave_Receive_IT() |
556 | (++) HAL_I2C_Master_Sequential_Transmit_IT() |
- | |
557 | (++) HAL_I2C_Master_Sequential_Receive_IT() |
- | |
558 | (++) HAL_I2C_Slave_Sequential_Transmit_IT() |
- | |
559 | (++) HAL_I2C_Slave_Sequential_Receive_IT() |
- | |
560 | (++) HAL_I2C_Mem_Write_IT() |
1001 | (++) HAL_I2C_Mem_Write_IT() |
561 | (++) HAL_I2C_Mem_Read_IT() |
1002 | (++) HAL_I2C_Mem_Read_IT() |
- | 1003 | (++) HAL_I2C_Master_Seq_Transmit_IT() |
|
- | 1004 | (++) HAL_I2C_Master_Seq_Receive_IT() |
|
- | 1005 | (++) HAL_I2C_Slave_Seq_Transmit_IT() |
|
- | 1006 | (++) HAL_I2C_Slave_Seq_Receive_IT() |
|
- | 1007 | (++) HAL_I2C_EnableListen_IT() |
|
- | 1008 | (++) HAL_I2C_DisableListen_IT() |
|
- | 1009 | (++) HAL_I2C_Master_Abort_IT() |
|
562 | 1010 | ||
563 | (#) No-Blocking mode functions with DMA are : |
1011 | (#) No-Blocking mode functions with DMA are : |
564 | (++) HAL_I2C_Master_Transmit_DMA() |
1012 | (++) HAL_I2C_Master_Transmit_DMA() |
565 | (++) HAL_I2C_Master_Receive_DMA() |
1013 | (++) HAL_I2C_Master_Receive_DMA() |
566 | (++) HAL_I2C_Slave_Transmit_DMA() |
1014 | (++) HAL_I2C_Slave_Transmit_DMA() |
567 | (++) HAL_I2C_Slave_Receive_DMA() |
1015 | (++) HAL_I2C_Slave_Receive_DMA() |
568 | (++) HAL_I2C_Mem_Write_DMA() |
1016 | (++) HAL_I2C_Mem_Write_DMA() |
569 | (++) HAL_I2C_Mem_Read_DMA() |
1017 | (++) HAL_I2C_Mem_Read_DMA() |
- | 1018 | (++) HAL_I2C_Master_Seq_Transmit_DMA() |
|
- | 1019 | (++) HAL_I2C_Master_Seq_Receive_DMA() |
|
- | 1020 | (++) HAL_I2C_Slave_Seq_Transmit_DMA() |
|
- | 1021 | (++) HAL_I2C_Slave_Seq_Receive_DMA() |
|
570 | 1022 | ||
571 | (#) A set of Transfer Complete Callbacks are provided in non Blocking mode: |
1023 | (#) A set of Transfer Complete Callbacks are provided in non Blocking mode: |
572 | (++) HAL_I2C_MemTxCpltCallback() |
- | |
573 | (++) HAL_I2C_MemRxCpltCallback() |
- | |
574 | (++) HAL_I2C_MasterTxCpltCallback() |
1024 | (++) HAL_I2C_MasterTxCpltCallback() |
575 | (++) HAL_I2C_MasterRxCpltCallback() |
1025 | (++) HAL_I2C_MasterRxCpltCallback() |
576 | (++) HAL_I2C_SlaveTxCpltCallback() |
1026 | (++) HAL_I2C_SlaveTxCpltCallback() |
577 | (++) HAL_I2C_SlaveRxCpltCallback() |
1027 | (++) HAL_I2C_SlaveRxCpltCallback() |
- | 1028 | (++) HAL_I2C_MemTxCpltCallback() |
|
- | 1029 | (++) HAL_I2C_MemRxCpltCallback() |
|
- | 1030 | (++) HAL_I2C_AddrCallback() |
|
- | 1031 | (++) HAL_I2C_ListenCpltCallback() |
|
578 | (++) HAL_I2C_ErrorCallback() |
1032 | (++) HAL_I2C_ErrorCallback() |
579 | (++) HAL_I2C_AbortCpltCallback() |
1033 | (++) HAL_I2C_AbortCpltCallback() |
580 | 1034 | ||
581 | @endverbatim |
1035 | @endverbatim |
582 | * @{ |
1036 | * @{ |
Line 593... | Line 1047... | ||
593 | * @param Timeout Timeout duration |
1047 | * @param Timeout Timeout duration |
594 | * @retval HAL status |
1048 | * @retval HAL status |
595 | */ |
1049 | */ |
596 | HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
1050 | HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
597 | { |
1051 | { |
598 | uint32_t tickstart = 0x00U; |
- | |
599 | - | ||
600 | /* Init tickstart for timeout management*/ |
1052 | /* Init tickstart for timeout management*/ |
601 | tickstart = HAL_GetTick(); |
1053 | uint32_t tickstart = HAL_GetTick(); |
602 | 1054 | ||
603 | if(hi2c->State == HAL_I2C_STATE_READY) |
1055 | if (hi2c->State == HAL_I2C_STATE_READY) |
604 | { |
1056 | { |
605 | /* Wait until BUSY flag is reset */ |
1057 | /* Wait until BUSY flag is reset */ |
606 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
1058 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
607 | { |
1059 | { |
608 | return HAL_BUSY; |
1060 | return HAL_BUSY; |
609 | } |
1061 | } |
610 | 1062 | ||
611 | /* Process Locked */ |
1063 | /* Process Locked */ |
612 | __HAL_LOCK(hi2c); |
1064 | __HAL_LOCK(hi2c); |
613 | 1065 | ||
614 | /* Check if the I2C is already enabled */ |
1066 | /* Check if the I2C is already enabled */ |
615 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1067 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
616 | { |
1068 | { |
617 | /* Enable I2C peripheral */ |
1069 | /* Enable I2C peripheral */ |
618 | __HAL_I2C_ENABLE(hi2c); |
1070 | __HAL_I2C_ENABLE(hi2c); |
619 | } |
1071 | } |
620 | 1072 | ||
621 | /* Disable Pos */ |
1073 | /* Disable Pos */ |
622 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
1074 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
623 | 1075 | ||
624 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
1076 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
625 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
1077 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
626 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1078 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
627 | 1079 | ||
628 | /* Prepare transfer parameters */ |
1080 | /* Prepare transfer parameters */ |
629 | hi2c->pBuffPtr = pData; |
1081 | hi2c->pBuffPtr = pData; |
630 | hi2c->XferCount = Size; |
1082 | hi2c->XferCount = Size; |
631 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
632 | hi2c->XferSize = hi2c->XferCount; |
1083 | hi2c->XferSize = hi2c->XferCount; |
- | 1084 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
633 | 1085 | ||
634 | /* Send Slave Address */ |
1086 | /* Send Slave Address */ |
635 | if(I2C_MasterRequestWrite(hi2c, DevAddress, Timeout, tickstart) != HAL_OK) |
1087 | if (I2C_MasterRequestWrite(hi2c, DevAddress, Timeout, tickstart) != HAL_OK) |
636 | { |
1088 | { |
637 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
638 | { |
- | |
639 | /* Process Unlocked */ |
- | |
640 | __HAL_UNLOCK(hi2c); |
- | |
641 | return HAL_ERROR; |
1089 | return HAL_ERROR; |
642 | } |
- | |
643 | else |
- | |
644 | { |
- | |
645 | /* Process Unlocked */ |
- | |
646 | __HAL_UNLOCK(hi2c); |
- | |
647 | return HAL_TIMEOUT; |
- | |
648 | } |
- | |
649 | } |
1090 | } |
650 | 1091 | ||
651 | /* Clear ADDR flag */ |
1092 | /* Clear ADDR flag */ |
652 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1093 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
653 | 1094 | ||
654 | while(hi2c->XferSize > 0U) |
1095 | while (hi2c->XferSize > 0U) |
655 | { |
1096 | { |
656 | /* Wait until TXE flag is set */ |
1097 | /* Wait until TXE flag is set */ |
657 | if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
1098 | if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
658 | { |
1099 | { |
659 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
1100 | if (hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
660 | { |
1101 | { |
661 | /* Generate Stop */ |
1102 | /* Generate Stop */ |
662 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
1103 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
663 | return HAL_ERROR; |
- | |
664 | } |
- | |
665 | else |
- | |
666 | { |
- | |
667 | return HAL_TIMEOUT; |
- | |
668 | } |
1104 | } |
- | 1105 | return HAL_ERROR; |
|
669 | } |
1106 | } |
670 | 1107 | ||
671 | /* Write data to DR */ |
1108 | /* Write data to DR */ |
672 | hi2c->Instance->DR = (*hi2c->pBuffPtr++); |
1109 | hi2c->Instance->DR = *hi2c->pBuffPtr; |
- | 1110 | ||
- | 1111 | /* Increment Buffer pointer */ |
|
- | 1112 | hi2c->pBuffPtr++; |
|
- | 1113 | ||
- | 1114 | /* Update counter */ |
|
673 | hi2c->XferCount--; |
1115 | hi2c->XferCount--; |
674 | hi2c->XferSize--; |
1116 | hi2c->XferSize--; |
675 | 1117 | ||
676 | if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U)) |
1118 | if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U)) |
677 | { |
1119 | { |
678 | /* Write data to DR */ |
1120 | /* Write data to DR */ |
679 | hi2c->Instance->DR = (*hi2c->pBuffPtr++); |
1121 | hi2c->Instance->DR = *hi2c->pBuffPtr; |
- | 1122 | ||
- | 1123 | /* Increment Buffer pointer */ |
|
- | 1124 | hi2c->pBuffPtr++; |
|
- | 1125 | ||
- | 1126 | /* Update counter */ |
|
680 | hi2c->XferCount--; |
1127 | hi2c->XferCount--; |
681 | hi2c->XferSize--; |
1128 | hi2c->XferSize--; |
682 | } |
1129 | } |
683 | 1130 | ||
684 | /* Wait until BTF flag is set */ |
1131 | /* Wait until BTF flag is set */ |
685 | if(I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
1132 | if (I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
686 | { |
1133 | { |
687 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
1134 | if (hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
688 | { |
1135 | { |
689 | /* Generate Stop */ |
1136 | /* Generate Stop */ |
690 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
1137 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
691 | return HAL_ERROR; |
- | |
692 | } |
- | |
693 | else |
- | |
694 | { |
- | |
695 | return HAL_TIMEOUT; |
- | |
696 | } |
1138 | } |
- | 1139 | return HAL_ERROR; |
|
697 | } |
1140 | } |
698 | } |
1141 | } |
699 | 1142 | ||
700 | /* Generate Stop */ |
1143 | /* Generate Stop */ |
701 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
1144 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
702 | 1145 | ||
703 | hi2c->State = HAL_I2C_STATE_READY; |
1146 | hi2c->State = HAL_I2C_STATE_READY; |
704 | hi2c->Mode = HAL_I2C_MODE_NONE; |
1147 | hi2c->Mode = HAL_I2C_MODE_NONE; |
705 | 1148 | ||
706 | /* Process Unlocked */ |
1149 | /* Process Unlocked */ |
707 | __HAL_UNLOCK(hi2c); |
1150 | __HAL_UNLOCK(hi2c); |
708 | 1151 | ||
709 | return HAL_OK; |
1152 | return HAL_OK; |
710 | } |
1153 | } |
Line 713... | Line 1156... | ||
713 | return HAL_BUSY; |
1156 | return HAL_BUSY; |
714 | } |
1157 | } |
715 | } |
1158 | } |
716 | 1159 | ||
717 | /** |
1160 | /** |
718 | * @brief Receives in master mode an amount of data in blocking mode. |
1161 | * @brief Receives in master mode an amount of data in blocking mode. |
719 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
1162 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
720 | * the configuration information for the specified I2C. |
1163 | * the configuration information for the specified I2C. |
721 | * @param DevAddress Target device address: The device 7 bits address value |
1164 | * @param DevAddress Target device address: The device 7 bits address value |
722 | * in datasheet must be shifted to the left before calling the interface |
1165 | * in datasheet must be shifted to the left before calling the interface |
723 | * @param pData Pointer to data buffer |
1166 | * @param pData Pointer to data buffer |
Line 725... | Line 1168... | ||
725 | * @param Timeout Timeout duration |
1168 | * @param Timeout Timeout duration |
726 | * @retval HAL status |
1169 | * @retval HAL status |
727 | */ |
1170 | */ |
728 | HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
1171 | HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
729 | { |
1172 | { |
730 | uint32_t tickstart = 0x00U; |
1173 | __IO uint32_t count = 0U; |
731 | 1174 | ||
732 | /* Init tickstart for timeout management*/ |
1175 | /* Init tickstart for timeout management*/ |
733 | tickstart = HAL_GetTick(); |
1176 | uint32_t tickstart = HAL_GetTick(); |
734 | 1177 | ||
735 | if(hi2c->State == HAL_I2C_STATE_READY) |
1178 | if (hi2c->State == HAL_I2C_STATE_READY) |
736 | { |
1179 | { |
737 | /* Wait until BUSY flag is reset */ |
1180 | /* Wait until BUSY flag is reset */ |
738 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
1181 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
739 | { |
1182 | { |
740 | return HAL_BUSY; |
1183 | return HAL_BUSY; |
741 | } |
1184 | } |
742 | 1185 | ||
743 | /* Process Locked */ |
1186 | /* Process Locked */ |
744 | __HAL_LOCK(hi2c); |
1187 | __HAL_LOCK(hi2c); |
745 | 1188 | ||
746 | /* Check if the I2C is already enabled */ |
1189 | /* Check if the I2C is already enabled */ |
747 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1190 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
748 | { |
1191 | { |
749 | /* Enable I2C peripheral */ |
1192 | /* Enable I2C peripheral */ |
750 | __HAL_I2C_ENABLE(hi2c); |
1193 | __HAL_I2C_ENABLE(hi2c); |
751 | } |
1194 | } |
752 | 1195 | ||
753 | /* Disable Pos */ |
1196 | /* Disable Pos */ |
754 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
1197 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
755 | 1198 | ||
756 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
1199 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
757 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
1200 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
758 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1201 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
759 | 1202 | ||
760 | /* Prepare transfer parameters */ |
1203 | /* Prepare transfer parameters */ |
761 | hi2c->pBuffPtr = pData; |
1204 | hi2c->pBuffPtr = pData; |
762 | hi2c->XferCount = Size; |
1205 | hi2c->XferCount = Size; |
763 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
764 | hi2c->XferSize = hi2c->XferCount; |
1206 | hi2c->XferSize = hi2c->XferCount; |
- | 1207 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
765 | 1208 | ||
766 | /* Send Slave Address */ |
1209 | /* Send Slave Address */ |
767 | if(I2C_MasterRequestRead(hi2c, DevAddress, Timeout, tickstart) != HAL_OK) |
1210 | if (I2C_MasterRequestRead(hi2c, DevAddress, Timeout, tickstart) != HAL_OK) |
768 | { |
1211 | { |
769 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
770 | { |
- | |
771 | /* Process Unlocked */ |
- | |
772 | __HAL_UNLOCK(hi2c); |
- | |
773 | return HAL_ERROR; |
1212 | return HAL_ERROR; |
774 | } |
- | |
775 | else |
- | |
776 | { |
- | |
777 | /* Process Unlocked */ |
- | |
778 | __HAL_UNLOCK(hi2c); |
- | |
779 | return HAL_TIMEOUT; |
- | |
780 | } |
- | |
781 | } |
1213 | } |
782 | 1214 | ||
783 | if(hi2c->XferSize == 0U) |
1215 | if (hi2c->XferSize == 0U) |
784 | { |
1216 | { |
785 | /* Clear ADDR flag */ |
1217 | /* Clear ADDR flag */ |
786 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1218 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
787 | 1219 | ||
788 | /* Generate Stop */ |
1220 | /* Generate Stop */ |
789 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
1221 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
790 | } |
1222 | } |
791 | else if(hi2c->XferSize == 1U) |
1223 | else if (hi2c->XferSize == 1U) |
792 | { |
1224 | { |
793 | /* Disable Acknowledge */ |
1225 | /* Disable Acknowledge */ |
794 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
1226 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
795 | 1227 | ||
796 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
1228 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
797 | software sequence must complete before the current byte end of transfer */ |
1229 | software sequence must complete before the current byte end of transfer */ |
798 | __disable_irq(); |
1230 | __disable_irq(); |
799 | 1231 | ||
800 | /* Clear ADDR flag */ |
1232 | /* Clear ADDR flag */ |
801 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1233 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
802 | 1234 | ||
803 | /* Generate Stop */ |
1235 | /* Generate Stop */ |
804 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
1236 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
805 | 1237 | ||
806 | /* Re-enable IRQs */ |
1238 | /* Re-enable IRQs */ |
807 | __enable_irq(); |
1239 | __enable_irq(); |
808 | } |
1240 | } |
809 | else if(hi2c->XferSize == 2U) |
1241 | else if (hi2c->XferSize == 2U) |
810 | { |
1242 | { |
811 | /* Enable Pos */ |
1243 | /* Enable Pos */ |
812 | hi2c->Instance->CR1 |= I2C_CR1_POS; |
1244 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
813 | 1245 | ||
814 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
1246 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
815 | software sequence must complete before the current byte end of transfer */ |
1247 | software sequence must complete before the current byte end of transfer */ |
816 | __disable_irq(); |
1248 | __disable_irq(); |
817 | 1249 | ||
818 | /* Clear ADDR flag */ |
1250 | /* Clear ADDR flag */ |
819 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1251 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
820 | 1252 | ||
821 | /* Disable Acknowledge */ |
1253 | /* Disable Acknowledge */ |
822 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
1254 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
823 | 1255 | ||
824 | /* Re-enable IRQs */ |
1256 | /* Re-enable IRQs */ |
825 | __enable_irq(); |
1257 | __enable_irq(); |
826 | } |
1258 | } |
827 | else |
1259 | else |
828 | { |
1260 | { |
829 | /* Enable Acknowledge */ |
1261 | /* Enable Acknowledge */ |
830 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
1262 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
831 | 1263 | ||
832 | /* Clear ADDR flag */ |
1264 | /* Clear ADDR flag */ |
833 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1265 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
834 | } |
1266 | } |
835 | 1267 | ||
836 | while(hi2c->XferSize > 0U) |
1268 | while (hi2c->XferSize > 0U) |
837 | { |
1269 | { |
838 | if(hi2c->XferSize <= 3U) |
1270 | if (hi2c->XferSize <= 3U) |
839 | { |
1271 | { |
840 | /* One byte */ |
1272 | /* One byte */ |
841 | if(hi2c->XferSize == 1U) |
1273 | if (hi2c->XferSize == 1U) |
842 | { |
1274 | { |
843 | /* Wait until RXNE flag is set */ |
1275 | /* Wait until RXNE flag is set */ |
844 | if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
1276 | if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
845 | { |
1277 | { |
846 | if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT) |
- | |
847 | { |
- | |
848 | return HAL_TIMEOUT; |
- | |
849 | } |
- | |
850 | else |
- | |
851 | { |
- | |
852 | return HAL_ERROR; |
1278 | return HAL_ERROR; |
853 | } |
- | |
854 | } |
1279 | } |
855 | 1280 | ||
856 | /* Read data from DR */ |
1281 | /* Read data from DR */ |
857 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
1282 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 1283 | ||
- | 1284 | /* Increment Buffer pointer */ |
|
- | 1285 | hi2c->pBuffPtr++; |
|
- | 1286 | ||
- | 1287 | /* Update counter */ |
|
858 | hi2c->XferSize--; |
1288 | hi2c->XferSize--; |
859 | hi2c->XferCount--; |
1289 | hi2c->XferCount--; |
860 | } |
1290 | } |
861 | /* Two bytes */ |
1291 | /* Two bytes */ |
862 | else if(hi2c->XferSize == 2U) |
1292 | else if (hi2c->XferSize == 2U) |
863 | { |
1293 | { |
864 | /* Wait until BTF flag is set */ |
1294 | /* Wait until BTF flag is set */ |
865 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK) |
1295 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK) |
866 | { |
1296 | { |
867 | return HAL_TIMEOUT; |
1297 | return HAL_ERROR; |
868 | } |
1298 | } |
869 | 1299 | ||
870 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
1300 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
871 | software sequence must complete before the current byte end of transfer */ |
1301 | software sequence must complete before the current byte end of transfer */ |
872 | __disable_irq(); |
1302 | __disable_irq(); |
873 | 1303 | ||
874 | /* Generate Stop */ |
1304 | /* Generate Stop */ |
875 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
1305 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
876 | 1306 | ||
877 | /* Read data from DR */ |
1307 | /* Read data from DR */ |
878 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
1308 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 1309 | ||
- | 1310 | /* Increment Buffer pointer */ |
|
- | 1311 | hi2c->pBuffPtr++; |
|
- | 1312 | ||
- | 1313 | /* Update counter */ |
|
879 | hi2c->XferSize--; |
1314 | hi2c->XferSize--; |
880 | hi2c->XferCount--; |
1315 | hi2c->XferCount--; |
881 | 1316 | ||
882 | /* Re-enable IRQs */ |
1317 | /* Re-enable IRQs */ |
883 | __enable_irq(); |
1318 | __enable_irq(); |
884 | 1319 | ||
885 | /* Read data from DR */ |
1320 | /* Read data from DR */ |
886 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
1321 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 1322 | ||
- | 1323 | /* Increment Buffer pointer */ |
|
- | 1324 | hi2c->pBuffPtr++; |
|
- | 1325 | ||
- | 1326 | /* Update counter */ |
|
887 | hi2c->XferSize--; |
1327 | hi2c->XferSize--; |
888 | hi2c->XferCount--; |
1328 | hi2c->XferCount--; |
889 | } |
1329 | } |
890 | /* 3 Last bytes */ |
1330 | /* 3 Last bytes */ |
891 | else |
1331 | else |
892 | { |
1332 | { |
893 | /* Wait until BTF flag is set */ |
1333 | /* Wait until BTF flag is set */ |
894 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK) |
1334 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK) |
895 | { |
1335 | { |
896 | return HAL_TIMEOUT; |
1336 | return HAL_ERROR; |
897 | } |
1337 | } |
898 | 1338 | ||
899 | /* Disable Acknowledge */ |
1339 | /* Disable Acknowledge */ |
900 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
1340 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
901 | 1341 | ||
902 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
1342 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
903 | software sequence must complete before the current byte end of transfer */ |
1343 | software sequence must complete before the current byte end of transfer */ |
904 | __disable_irq(); |
1344 | __disable_irq(); |
905 | 1345 | ||
906 | /* Read data from DR */ |
1346 | /* Read data from DR */ |
907 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
1347 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 1348 | ||
- | 1349 | /* Increment Buffer pointer */ |
|
- | 1350 | hi2c->pBuffPtr++; |
|
- | 1351 | ||
- | 1352 | /* Update counter */ |
|
908 | hi2c->XferSize--; |
1353 | hi2c->XferSize--; |
909 | hi2c->XferCount--; |
1354 | hi2c->XferCount--; |
910 | 1355 | ||
911 | /* Wait until BTF flag is set */ |
1356 | /* Wait until BTF flag is set */ |
912 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK) |
1357 | count = I2C_TIMEOUT_FLAG * (SystemCoreClock / 25U / 1000U); |
- | 1358 | do |
|
913 | { |
1359 | { |
- | 1360 | count--; |
|
- | 1361 | if (count == 0U) |
|
- | 1362 | { |
|
- | 1363 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 1364 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 1365 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 1366 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
- | 1367 | ||
- | 1368 | /* Re-enable IRQs */ |
|
- | 1369 | __enable_irq(); |
|
- | 1370 | ||
- | 1371 | /* Process Unlocked */ |
|
- | 1372 | __HAL_UNLOCK(hi2c); |
|
- | 1373 | ||
914 | return HAL_TIMEOUT; |
1374 | return HAL_ERROR; |
- | 1375 | } |
|
915 | } |
1376 | } |
- | 1377 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET); |
|
916 | 1378 | ||
917 | /* Generate Stop */ |
1379 | /* Generate Stop */ |
918 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
1380 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
919 | 1381 | ||
920 | /* Read data from DR */ |
1382 | /* Read data from DR */ |
921 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
1383 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 1384 | ||
- | 1385 | /* Increment Buffer pointer */ |
|
- | 1386 | hi2c->pBuffPtr++; |
|
- | 1387 | ||
- | 1388 | /* Update counter */ |
|
922 | hi2c->XferSize--; |
1389 | hi2c->XferSize--; |
923 | hi2c->XferCount--; |
1390 | hi2c->XferCount--; |
924 | 1391 | ||
925 | /* Re-enable IRQs */ |
1392 | /* Re-enable IRQs */ |
926 | __enable_irq(); |
1393 | __enable_irq(); |
927 | 1394 | ||
928 | /* Read data from DR */ |
1395 | /* Read data from DR */ |
929 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
1396 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 1397 | ||
- | 1398 | /* Increment Buffer pointer */ |
|
- | 1399 | hi2c->pBuffPtr++; |
|
- | 1400 | ||
- | 1401 | /* Update counter */ |
|
930 | hi2c->XferSize--; |
1402 | hi2c->XferSize--; |
931 | hi2c->XferCount--; |
1403 | hi2c->XferCount--; |
932 | } |
1404 | } |
933 | } |
1405 | } |
934 | else |
1406 | else |
935 | { |
1407 | { |
936 | /* Wait until RXNE flag is set */ |
1408 | /* Wait until RXNE flag is set */ |
937 | if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
1409 | if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
938 | { |
1410 | { |
939 | if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT) |
- | |
940 | { |
- | |
941 | return HAL_TIMEOUT; |
- | |
942 | } |
- | |
943 | else |
- | |
944 | { |
- | |
945 | return HAL_ERROR; |
1411 | return HAL_ERROR; |
946 | } |
- | |
947 | } |
1412 | } |
948 | 1413 | ||
949 | /* Read data from DR */ |
1414 | /* Read data from DR */ |
950 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
1415 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 1416 | ||
- | 1417 | /* Increment Buffer pointer */ |
|
- | 1418 | hi2c->pBuffPtr++; |
|
- | 1419 | ||
- | 1420 | /* Update counter */ |
|
951 | hi2c->XferSize--; |
1421 | hi2c->XferSize--; |
952 | hi2c->XferCount--; |
1422 | hi2c->XferCount--; |
953 | 1423 | ||
954 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) |
1424 | if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) |
955 | { |
1425 | { |
956 | /* Read data from DR */ |
1426 | /* Read data from DR */ |
957 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
1427 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 1428 | ||
- | 1429 | /* Increment Buffer pointer */ |
|
- | 1430 | hi2c->pBuffPtr++; |
|
- | 1431 | ||
- | 1432 | /* Update counter */ |
|
958 | hi2c->XferSize--; |
1433 | hi2c->XferSize--; |
959 | hi2c->XferCount--; |
1434 | hi2c->XferCount--; |
960 | } |
1435 | } |
961 | } |
1436 | } |
962 | } |
1437 | } |
Line 974... | Line 1449... | ||
974 | return HAL_BUSY; |
1449 | return HAL_BUSY; |
975 | } |
1450 | } |
976 | } |
1451 | } |
977 | 1452 | ||
978 | /** |
1453 | /** |
979 | * @brief Transmits in slave mode an amount of data in blocking mode. |
1454 | * @brief Transmits in slave mode an amount of data in blocking mode. |
980 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
1455 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
981 | * the configuration information for the specified I2C. |
1456 | * the configuration information for the specified I2C. |
982 | * @param pData Pointer to data buffer |
1457 | * @param pData Pointer to data buffer |
983 | * @param Size Amount of data to be sent |
1458 | * @param Size Amount of data to be sent |
984 | * @param Timeout Timeout duration |
1459 | * @param Timeout Timeout duration |
985 | * @retval HAL status |
1460 | * @retval HAL status |
986 | */ |
1461 | */ |
987 | HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
1462 | HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
988 | { |
1463 | { |
989 | uint32_t tickstart = 0x00U; |
- | |
990 | - | ||
991 | /* Init tickstart for timeout management*/ |
1464 | /* Init tickstart for timeout management*/ |
992 | tickstart = HAL_GetTick(); |
1465 | uint32_t tickstart = HAL_GetTick(); |
993 | 1466 | ||
994 | if(hi2c->State == HAL_I2C_STATE_READY) |
1467 | if (hi2c->State == HAL_I2C_STATE_READY) |
995 | { |
1468 | { |
996 | if((pData == NULL) || (Size == 0U)) |
1469 | if ((pData == NULL) || (Size == 0U)) |
997 | { |
1470 | { |
998 | return HAL_ERROR; |
1471 | return HAL_ERROR; |
999 | } |
1472 | } |
1000 | 1473 | ||
1001 | /* Process Locked */ |
1474 | /* Process Locked */ |
1002 | __HAL_LOCK(hi2c); |
1475 | __HAL_LOCK(hi2c); |
1003 | 1476 | ||
1004 | /* Check if the I2C is already enabled */ |
1477 | /* Check if the I2C is already enabled */ |
1005 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1478 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1006 | { |
1479 | { |
1007 | /* Enable I2C peripheral */ |
1480 | /* Enable I2C peripheral */ |
1008 | __HAL_I2C_ENABLE(hi2c); |
1481 | __HAL_I2C_ENABLE(hi2c); |
1009 | } |
1482 | } |
1010 | 1483 | ||
1011 | /* Disable Pos */ |
1484 | /* Disable Pos */ |
1012 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
1485 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
1013 | 1486 | ||
1014 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
1487 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
1015 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
1488 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
1016 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1489 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1017 | 1490 | ||
1018 | /* Prepare transfer parameters */ |
1491 | /* Prepare transfer parameters */ |
1019 | hi2c->pBuffPtr = pData; |
1492 | hi2c->pBuffPtr = pData; |
1020 | hi2c->XferCount = Size; |
1493 | hi2c->XferCount = Size; |
1021 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
1022 | hi2c->XferSize = hi2c->XferCount; |
1494 | hi2c->XferSize = hi2c->XferCount; |
- | 1495 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
1023 | 1496 | ||
1024 | /* Enable Address Acknowledge */ |
1497 | /* Enable Address Acknowledge */ |
1025 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
1498 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
1026 | 1499 | ||
1027 | /* Wait until ADDR flag is set */ |
1500 | /* Wait until ADDR flag is set */ |
1028 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK) |
1501 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK) |
1029 | { |
1502 | { |
1030 | return HAL_TIMEOUT; |
1503 | return HAL_ERROR; |
1031 | } |
1504 | } |
1032 | 1505 | ||
1033 | /* Clear ADDR flag */ |
1506 | /* Clear ADDR flag */ |
1034 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1507 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1035 | 1508 | ||
1036 | /* If 10bit addressing mode is selected */ |
1509 | /* If 10bit addressing mode is selected */ |
1037 | if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT) |
1510 | if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT) |
1038 | { |
1511 | { |
1039 | /* Wait until ADDR flag is set */ |
1512 | /* Wait until ADDR flag is set */ |
1040 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK) |
1513 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK) |
1041 | { |
1514 | { |
1042 | return HAL_TIMEOUT; |
1515 | return HAL_ERROR; |
1043 | } |
1516 | } |
1044 | 1517 | ||
1045 | /* Clear ADDR flag */ |
1518 | /* Clear ADDR flag */ |
1046 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1519 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1047 | } |
1520 | } |
1048 | 1521 | ||
1049 | while(hi2c->XferSize > 0U) |
1522 | while (hi2c->XferSize > 0U) |
1050 | { |
1523 | { |
1051 | /* Wait until TXE flag is set */ |
1524 | /* Wait until TXE flag is set */ |
1052 | if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
1525 | if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
1053 | { |
1526 | { |
1054 | /* Disable Address Acknowledge */ |
1527 | /* Disable Address Acknowledge */ |
1055 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
1528 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
1056 | 1529 | ||
1057 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
1058 | { |
- | |
1059 | return HAL_ERROR; |
1530 | return HAL_ERROR; |
1060 | } |
- | |
1061 | else |
- | |
1062 | { |
- | |
1063 | return HAL_TIMEOUT; |
- | |
1064 | } |
- | |
1065 | } |
1531 | } |
1066 | 1532 | ||
1067 | /* Write data to DR */ |
1533 | /* Write data to DR */ |
1068 | hi2c->Instance->DR = (*hi2c->pBuffPtr++); |
1534 | hi2c->Instance->DR = *hi2c->pBuffPtr; |
- | 1535 | ||
- | 1536 | /* Increment Buffer pointer */ |
|
- | 1537 | hi2c->pBuffPtr++; |
|
- | 1538 | ||
- | 1539 | /* Update counter */ |
|
1069 | hi2c->XferCount--; |
1540 | hi2c->XferCount--; |
1070 | hi2c->XferSize--; |
1541 | hi2c->XferSize--; |
1071 | 1542 | ||
1072 | if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U)) |
1543 | if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U)) |
1073 | { |
1544 | { |
1074 | /* Write data to DR */ |
1545 | /* Write data to DR */ |
1075 | hi2c->Instance->DR = (*hi2c->pBuffPtr++); |
1546 | hi2c->Instance->DR = *hi2c->pBuffPtr; |
- | 1547 | ||
- | 1548 | /* Increment Buffer pointer */ |
|
- | 1549 | hi2c->pBuffPtr++; |
|
- | 1550 | ||
- | 1551 | /* Update counter */ |
|
1076 | hi2c->XferCount--; |
1552 | hi2c->XferCount--; |
1077 | hi2c->XferSize--; |
1553 | hi2c->XferSize--; |
1078 | } |
1554 | } |
1079 | } |
1555 | } |
1080 | 1556 | ||
1081 | /* Wait until AF flag is set */ |
1557 | /* Wait until AF flag is set */ |
1082 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout, tickstart) != HAL_OK) |
1558 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout, tickstart) != HAL_OK) |
1083 | { |
1559 | { |
1084 | return HAL_TIMEOUT; |
1560 | return HAL_ERROR; |
1085 | } |
1561 | } |
1086 | 1562 | ||
1087 | /* Clear AF flag */ |
1563 | /* Clear AF flag */ |
1088 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
1564 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
1089 | 1565 | ||
1090 | /* Disable Address Acknowledge */ |
1566 | /* Disable Address Acknowledge */ |
1091 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
1567 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
1092 | 1568 | ||
1093 | hi2c->State = HAL_I2C_STATE_READY; |
1569 | hi2c->State = HAL_I2C_STATE_READY; |
1094 | hi2c->Mode = HAL_I2C_MODE_NONE; |
1570 | hi2c->Mode = HAL_I2C_MODE_NONE; |
1095 | 1571 | ||
1096 | /* Process Unlocked */ |
1572 | /* Process Unlocked */ |
Line 1103... | Line 1579... | ||
1103 | return HAL_BUSY; |
1579 | return HAL_BUSY; |
1104 | } |
1580 | } |
1105 | } |
1581 | } |
1106 | 1582 | ||
1107 | /** |
1583 | /** |
1108 | * @brief Receive in slave mode an amount of data in blocking mode |
1584 | * @brief Receive in slave mode an amount of data in blocking mode |
1109 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
1585 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
1110 | * the configuration information for the specified I2C. |
1586 | * the configuration information for the specified I2C. |
1111 | * @param pData Pointer to data buffer |
1587 | * @param pData Pointer to data buffer |
1112 | * @param Size Amount of data to be sent |
1588 | * @param Size Amount of data to be sent |
1113 | * @param Timeout Timeout duration |
1589 | * @param Timeout Timeout duration |
1114 | * @retval HAL status |
1590 | * @retval HAL status |
1115 | */ |
1591 | */ |
1116 | HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
1592 | HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
1117 | { |
1593 | { |
1118 | uint32_t tickstart = 0x00U; |
- | |
1119 | - | ||
1120 | /* Init tickstart for timeout management*/ |
1594 | /* Init tickstart for timeout management*/ |
1121 | tickstart = HAL_GetTick(); |
1595 | uint32_t tickstart = HAL_GetTick(); |
1122 | 1596 | ||
1123 | if(hi2c->State == HAL_I2C_STATE_READY) |
1597 | if (hi2c->State == HAL_I2C_STATE_READY) |
1124 | { |
1598 | { |
1125 | if((pData == NULL) || (Size == 0U)) |
1599 | if ((pData == NULL) || (Size == (uint16_t)0)) |
1126 | { |
1600 | { |
1127 | return HAL_ERROR; |
1601 | return HAL_ERROR; |
1128 | } |
1602 | } |
1129 | 1603 | ||
1130 | /* Process Locked */ |
1604 | /* Process Locked */ |
1131 | __HAL_LOCK(hi2c); |
1605 | __HAL_LOCK(hi2c); |
1132 | 1606 | ||
1133 | /* Check if the I2C is already enabled */ |
1607 | /* Check if the I2C is already enabled */ |
1134 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1608 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1135 | { |
1609 | { |
1136 | /* Enable I2C peripheral */ |
1610 | /* Enable I2C peripheral */ |
1137 | __HAL_I2C_ENABLE(hi2c); |
1611 | __HAL_I2C_ENABLE(hi2c); |
1138 | } |
1612 | } |
1139 | 1613 | ||
1140 | /* Disable Pos */ |
1614 | /* Disable Pos */ |
1141 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
1615 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
1142 | 1616 | ||
1143 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
1617 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
1144 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
1618 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
1145 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1619 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1146 | 1620 | ||
1147 | /* Prepare transfer parameters */ |
1621 | /* Prepare transfer parameters */ |
1148 | hi2c->pBuffPtr = pData; |
1622 | hi2c->pBuffPtr = pData; |
1149 | hi2c->XferCount = Size; |
1623 | hi2c->XferCount = Size; |
1150 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
1151 | hi2c->XferSize = hi2c->XferCount; |
1624 | hi2c->XferSize = hi2c->XferCount; |
- | 1625 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
1152 | 1626 | ||
1153 | /* Enable Address Acknowledge */ |
1627 | /* Enable Address Acknowledge */ |
1154 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
1628 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
1155 | 1629 | ||
1156 | /* Wait until ADDR flag is set */ |
1630 | /* Wait until ADDR flag is set */ |
1157 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK) |
1631 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK) |
1158 | { |
1632 | { |
1159 | return HAL_TIMEOUT; |
1633 | return HAL_ERROR; |
1160 | } |
1634 | } |
1161 | 1635 | ||
1162 | /* Clear ADDR flag */ |
1636 | /* Clear ADDR flag */ |
1163 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1637 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1164 | 1638 | ||
1165 | while(hi2c->XferSize > 0U) |
1639 | while (hi2c->XferSize > 0U) |
1166 | { |
1640 | { |
1167 | /* Wait until RXNE flag is set */ |
1641 | /* Wait until RXNE flag is set */ |
1168 | if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
1642 | if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
1169 | { |
1643 | { |
1170 | /* Disable Address Acknowledge */ |
1644 | /* Disable Address Acknowledge */ |
1171 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
1645 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
1172 | 1646 | ||
1173 | if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT) |
- | |
1174 | { |
- | |
1175 | return HAL_TIMEOUT; |
- | |
1176 | } |
- | |
1177 | else |
- | |
1178 | { |
- | |
1179 | return HAL_ERROR; |
1647 | return HAL_ERROR; |
1180 | } |
- | |
1181 | } |
1648 | } |
1182 | 1649 | ||
1183 | /* Read data from DR */ |
1650 | /* Read data from DR */ |
1184 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
1651 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 1652 | ||
- | 1653 | /* Increment Buffer pointer */ |
|
- | 1654 | hi2c->pBuffPtr++; |
|
- | 1655 | ||
- | 1656 | /* Update counter */ |
|
1185 | hi2c->XferSize--; |
1657 | hi2c->XferSize--; |
1186 | hi2c->XferCount--; |
1658 | hi2c->XferCount--; |
1187 | 1659 | ||
1188 | if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U)) |
1660 | if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U)) |
1189 | { |
1661 | { |
1190 | /* Read data from DR */ |
1662 | /* Read data from DR */ |
1191 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
1663 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 1664 | ||
- | 1665 | /* Increment Buffer pointer */ |
|
- | 1666 | hi2c->pBuffPtr++; |
|
- | 1667 | ||
- | 1668 | /* Update counter */ |
|
1192 | hi2c->XferSize--; |
1669 | hi2c->XferSize--; |
1193 | hi2c->XferCount--; |
1670 | hi2c->XferCount--; |
1194 | } |
1671 | } |
1195 | } |
1672 | } |
1196 | 1673 | ||
1197 | /* Wait until STOP flag is set */ |
1674 | /* Wait until STOP flag is set */ |
1198 | if(I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
1675 | if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
1199 | { |
1676 | { |
1200 | /* Disable Address Acknowledge */ |
1677 | /* Disable Address Acknowledge */ |
1201 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
1678 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
1202 | 1679 | ||
1203 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
1204 | { |
- | |
1205 | return HAL_ERROR; |
1680 | return HAL_ERROR; |
1206 | } |
- | |
1207 | else |
- | |
1208 | { |
- | |
1209 | return HAL_TIMEOUT; |
- | |
1210 | } |
- | |
1211 | } |
1681 | } |
1212 | 1682 | ||
1213 | /* Clear STOP flag */ |
1683 | /* Clear STOP flag */ |
1214 | __HAL_I2C_CLEAR_STOPFLAG(hi2c); |
1684 | __HAL_I2C_CLEAR_STOPFLAG(hi2c); |
1215 | 1685 | ||
1216 | /* Disable Address Acknowledge */ |
1686 | /* Disable Address Acknowledge */ |
1217 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
1687 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
1218 | 1688 | ||
1219 | hi2c->State = HAL_I2C_STATE_READY; |
1689 | hi2c->State = HAL_I2C_STATE_READY; |
1220 | hi2c->Mode = HAL_I2C_MODE_NONE; |
1690 | hi2c->Mode = HAL_I2C_MODE_NONE; |
1221 | 1691 | ||
1222 | /* Process Unlocked */ |
1692 | /* Process Unlocked */ |
Line 1242... | Line 1712... | ||
1242 | */ |
1712 | */ |
1243 | HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
1713 | HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
1244 | { |
1714 | { |
1245 | __IO uint32_t count = 0U; |
1715 | __IO uint32_t count = 0U; |
1246 | 1716 | ||
1247 | if(hi2c->State == HAL_I2C_STATE_READY) |
1717 | if (hi2c->State == HAL_I2C_STATE_READY) |
1248 | { |
1718 | { |
1249 | /* Wait until BUSY flag is reset */ |
1719 | /* Wait until BUSY flag is reset */ |
1250 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
1720 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
1251 | do |
1721 | do |
1252 | { |
1722 | { |
- | 1723 | count--; |
|
1253 | if(count-- == 0U) |
1724 | if (count == 0U) |
1254 | { |
1725 | { |
1255 | hi2c->PreviousState = I2C_STATE_NONE; |
1726 | hi2c->PreviousState = I2C_STATE_NONE; |
1256 | hi2c->State= HAL_I2C_STATE_READY; |
1727 | hi2c->State = HAL_I2C_STATE_READY; |
- | 1728 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 1729 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
1257 | 1730 | ||
1258 | /* Process Unlocked */ |
1731 | /* Process Unlocked */ |
1259 | __HAL_UNLOCK(hi2c); |
1732 | __HAL_UNLOCK(hi2c); |
1260 | 1733 | ||
1261 | return HAL_TIMEOUT; |
1734 | return HAL_ERROR; |
1262 | } |
1735 | } |
1263 | } |
1736 | } |
1264 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
1737 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
1265 | 1738 | ||
1266 | /* Process Locked */ |
1739 | /* Process Locked */ |
1267 | __HAL_LOCK(hi2c); |
1740 | __HAL_LOCK(hi2c); |
1268 | 1741 | ||
1269 | /* Check if the I2C is already enabled */ |
1742 | /* Check if the I2C is already enabled */ |
1270 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1743 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1271 | { |
1744 | { |
1272 | /* Enable I2C peripheral */ |
1745 | /* Enable I2C peripheral */ |
1273 | __HAL_I2C_ENABLE(hi2c); |
1746 | __HAL_I2C_ENABLE(hi2c); |
1274 | } |
1747 | } |
1275 | 1748 | ||
1276 | /* Disable Pos */ |
1749 | /* Disable Pos */ |
1277 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
1750 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
1278 | 1751 | ||
1279 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
1752 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
1280 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
1753 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
1281 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1754 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1282 | 1755 | ||
1283 | /* Prepare transfer parameters */ |
1756 | /* Prepare transfer parameters */ |
1284 | hi2c->pBuffPtr = pData; |
1757 | hi2c->pBuffPtr = pData; |
1285 | hi2c->XferCount = Size; |
1758 | hi2c->XferCount = Size; |
1286 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
1287 | hi2c->XferSize = hi2c->XferCount; |
1759 | hi2c->XferSize = hi2c->XferCount; |
- | 1760 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
1288 | hi2c->Devaddress = DevAddress; |
1761 | hi2c->Devaddress = DevAddress; |
1289 | 1762 | ||
1290 | /* Generate Start */ |
1763 | /* Generate Start */ |
1291 | hi2c->Instance->CR1 |= I2C_CR1_START; |
1764 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
1292 | 1765 | ||
1293 | /* Process Unlocked */ |
1766 | /* Process Unlocked */ |
1294 | __HAL_UNLOCK(hi2c); |
1767 | __HAL_UNLOCK(hi2c); |
1295 | 1768 | ||
1296 | /* Note : The I2C interrupts must be enabled after unlocking current process |
1769 | /* Note : The I2C interrupts must be enabled after unlocking current process |
Line 1319... | Line 1792... | ||
1319 | */ |
1792 | */ |
1320 | HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
1793 | HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
1321 | { |
1794 | { |
1322 | __IO uint32_t count = 0U; |
1795 | __IO uint32_t count = 0U; |
1323 | 1796 | ||
1324 | if(hi2c->State == HAL_I2C_STATE_READY) |
1797 | if (hi2c->State == HAL_I2C_STATE_READY) |
1325 | { |
1798 | { |
1326 | /* Wait until BUSY flag is reset */ |
1799 | /* Wait until BUSY flag is reset */ |
1327 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
1800 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
1328 | do |
1801 | do |
1329 | { |
1802 | { |
- | 1803 | count--; |
|
1330 | if(count-- == 0U) |
1804 | if (count == 0U) |
1331 | { |
1805 | { |
1332 | hi2c->PreviousState = I2C_STATE_NONE; |
1806 | hi2c->PreviousState = I2C_STATE_NONE; |
1333 | hi2c->State= HAL_I2C_STATE_READY; |
1807 | hi2c->State = HAL_I2C_STATE_READY; |
- | 1808 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 1809 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
1334 | 1810 | ||
1335 | /* Process Unlocked */ |
1811 | /* Process Unlocked */ |
1336 | __HAL_UNLOCK(hi2c); |
1812 | __HAL_UNLOCK(hi2c); |
1337 | 1813 | ||
1338 | return HAL_TIMEOUT; |
1814 | return HAL_ERROR; |
1339 | } |
1815 | } |
1340 | } |
1816 | } |
1341 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
1817 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
1342 | 1818 | ||
1343 | /* Process Locked */ |
1819 | /* Process Locked */ |
1344 | __HAL_LOCK(hi2c); |
1820 | __HAL_LOCK(hi2c); |
1345 | 1821 | ||
1346 | /* Check if the I2C is already enabled */ |
1822 | /* Check if the I2C is already enabled */ |
1347 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1823 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1348 | { |
1824 | { |
1349 | /* Enable I2C peripheral */ |
1825 | /* Enable I2C peripheral */ |
1350 | __HAL_I2C_ENABLE(hi2c); |
1826 | __HAL_I2C_ENABLE(hi2c); |
1351 | } |
1827 | } |
1352 | 1828 | ||
1353 | /* Disable Pos */ |
1829 | /* Disable Pos */ |
1354 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
1830 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
1355 | 1831 | ||
1356 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
1832 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
1357 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
1833 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
1358 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1834 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1359 | 1835 | ||
1360 | /* Prepare transfer parameters */ |
1836 | /* Prepare transfer parameters */ |
1361 | hi2c->pBuffPtr = pData; |
1837 | hi2c->pBuffPtr = pData; |
1362 | hi2c->XferCount = Size; |
1838 | hi2c->XferCount = Size; |
1363 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
1364 | hi2c->XferSize = hi2c->XferCount; |
1839 | hi2c->XferSize = hi2c->XferCount; |
- | 1840 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
1365 | hi2c->Devaddress = DevAddress; |
1841 | hi2c->Devaddress = DevAddress; |
1366 | 1842 | ||
1367 | /* Enable Acknowledge */ |
1843 | /* Enable Acknowledge */ |
1368 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
1844 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
1369 | 1845 | ||
1370 | /* Generate Start */ |
1846 | /* Generate Start */ |
1371 | hi2c->Instance->CR1 |= I2C_CR1_START; |
- | |
1372 | - | ||
1373 | /* Process Unlocked */ |
- | |
1374 | __HAL_UNLOCK(hi2c); |
- | |
1375 | - | ||
1376 | /* Note : The I2C interrupts must be enabled after unlocking current process |
- | |
1377 | to avoid the risk of I2C interrupt handle execution before current |
- | |
1378 | process unlock */ |
- | |
1379 | - | ||
1380 | /* Enable EVT, BUF and ERR interrupt */ |
- | |
1381 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
- | |
1382 | - | ||
1383 | return HAL_OK; |
- | |
1384 | } |
- | |
1385 | else |
- | |
1386 | { |
- | |
1387 | return HAL_BUSY; |
- | |
1388 | } |
- | |
1389 | } |
- | |
1390 | - | ||
1391 | /** |
- | |
1392 | * @brief Sequential transmit in master mode an amount of data in non-blocking mode with Interrupt |
- | |
1393 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
- | |
1394 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
- | |
1395 | * the configuration information for the specified I2C. |
- | |
1396 | * @param DevAddress Target device address: The device 7 bits address value |
- | |
1397 | * in datasheet must be shifted to the left before calling the interface |
- | |
1398 | * @param pData Pointer to data buffer |
- | |
1399 | * @param Size Amount of data to be sent |
- | |
1400 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
- | |
1401 | * @retval HAL status |
- | |
1402 | */ |
- | |
1403 | HAL_StatusTypeDef HAL_I2C_Master_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
- | |
1404 | { |
- | |
1405 | __IO uint32_t Prev_State = 0x00U; |
- | |
1406 | __IO uint32_t count = 0x00U; |
- | |
1407 | - | ||
1408 | /* Check the parameters */ |
- | |
1409 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
- | |
1410 | - | ||
1411 | if(hi2c->State == HAL_I2C_STATE_READY) |
- | |
1412 | { |
- | |
1413 | /* Check Busy Flag only if FIRST call of Master interface */ |
- | |
1414 | if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME)) |
- | |
1415 | { |
- | |
1416 | /* Wait until BUSY flag is reset */ |
- | |
1417 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
- | |
1418 | do |
- | |
1419 | { |
- | |
1420 | if(count-- == 0U) |
- | |
1421 | { |
- | |
1422 | hi2c->PreviousState = I2C_STATE_NONE; |
- | |
1423 | hi2c->State= HAL_I2C_STATE_READY; |
- | |
1424 | - | ||
1425 | /* Process Unlocked */ |
- | |
1426 | __HAL_UNLOCK(hi2c); |
- | |
1427 | - | ||
1428 | return HAL_TIMEOUT; |
- | |
1429 | } |
- | |
1430 | } |
- | |
1431 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
- | |
1432 | } |
- | |
1433 | - | ||
1434 | /* Process Locked */ |
- | |
1435 | __HAL_LOCK(hi2c); |
- | |
1436 | - | ||
1437 | /* Check if the I2C is already enabled */ |
- | |
1438 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
- | |
1439 | { |
- | |
1440 | /* Enable I2C peripheral */ |
- | |
1441 | __HAL_I2C_ENABLE(hi2c); |
- | |
1442 | } |
- | |
1443 | - | ||
1444 | /* Disable Pos */ |
- | |
1445 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
- | |
1446 | - | ||
1447 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
- | |
1448 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
- | |
1449 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
- | |
1450 | - | ||
1451 | /* Prepare transfer parameters */ |
- | |
1452 | hi2c->pBuffPtr = pData; |
- | |
1453 | hi2c->XferCount = Size; |
- | |
1454 | hi2c->XferOptions = XferOptions; |
- | |
1455 | hi2c->XferSize = hi2c->XferCount; |
- | |
1456 | hi2c->Devaddress = DevAddress; |
- | |
1457 | - | ||
1458 | Prev_State = hi2c->PreviousState; |
- | |
1459 | - | ||
1460 | /* Generate Start */ |
- | |
1461 | if((Prev_State == I2C_STATE_MASTER_BUSY_RX) || (Prev_State == I2C_STATE_NONE)) |
- | |
1462 | { |
- | |
1463 | /* Generate Start condition if first transfer */ |
- | |
1464 | if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME)) |
- | |
1465 | { |
- | |
1466 | /* Generate Start */ |
- | |
1467 | hi2c->Instance->CR1 |= I2C_CR1_START; |
- | |
1468 | } |
- | |
1469 | else |
- | |
1470 | { |
- | |
1471 | /* Generate ReStart */ |
- | |
1472 | hi2c->Instance->CR1 |= I2C_CR1_START; |
1847 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
1473 | } |
- | |
1474 | } |
- | |
1475 | - | ||
1476 | /* Process Unlocked */ |
- | |
1477 | __HAL_UNLOCK(hi2c); |
- | |
1478 | - | ||
1479 | /* Note : The I2C interrupts must be enabled after unlocking current process |
- | |
1480 | to avoid the risk of I2C interrupt handle execution before current |
- | |
1481 | process unlock */ |
- | |
1482 | - | ||
1483 | /* Enable EVT, BUF and ERR interrupt */ |
- | |
1484 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
- | |
1485 | - | ||
1486 | return HAL_OK; |
- | |
1487 | } |
- | |
1488 | else |
- | |
1489 | { |
- | |
1490 | return HAL_BUSY; |
- | |
1491 | } |
- | |
1492 | } |
- | |
1493 | - | ||
1494 | /** |
- | |
1495 | * @brief Sequential receive in master mode an amount of data in non-blocking mode with Interrupt |
- | |
1496 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
- | |
1497 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
- | |
1498 | * the configuration information for the specified I2C. |
- | |
1499 | * @param DevAddress Target device address: The device 7 bits address value |
- | |
1500 | * in datasheet must be shifted to the left before calling the interface |
- | |
1501 | * @param pData Pointer to data buffer |
- | |
1502 | * @param Size Amount of data to be sent |
- | |
1503 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
- | |
1504 | * @retval HAL status |
- | |
1505 | */ |
- | |
1506 | HAL_StatusTypeDef HAL_I2C_Master_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
- | |
1507 | { |
- | |
1508 | __IO uint32_t count = 0U; |
- | |
1509 | - | ||
1510 | /* Check the parameters */ |
- | |
1511 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
- | |
1512 | - | ||
1513 | if(hi2c->State == HAL_I2C_STATE_READY) |
- | |
1514 | { |
- | |
1515 | /* Check Busy Flag only if FIRST call of Master interface */ |
- | |
1516 | if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME)) |
- | |
1517 | { |
- | |
1518 | /* Wait until BUSY flag is reset */ |
- | |
1519 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
- | |
1520 | do |
- | |
1521 | { |
- | |
1522 | if(count-- == 0U) |
- | |
1523 | { |
- | |
1524 | hi2c->PreviousState = I2C_STATE_NONE; |
- | |
1525 | hi2c->State= HAL_I2C_STATE_READY; |
- | |
1526 | - | ||
1527 | /* Process Unlocked */ |
- | |
1528 | __HAL_UNLOCK(hi2c); |
- | |
1529 | - | ||
1530 | return HAL_TIMEOUT; |
- | |
1531 | } |
- | |
1532 | } |
- | |
1533 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
- | |
1534 | } |
- | |
1535 | - | ||
1536 | /* Process Locked */ |
- | |
1537 | __HAL_LOCK(hi2c); |
- | |
1538 | - | ||
1539 | /* Check if the I2C is already enabled */ |
- | |
1540 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
- | |
1541 | { |
- | |
1542 | /* Enable I2C peripheral */ |
- | |
1543 | __HAL_I2C_ENABLE(hi2c); |
- | |
1544 | } |
- | |
1545 | - | ||
1546 | /* Disable Pos */ |
- | |
1547 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
- | |
1548 | - | ||
1549 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
- | |
1550 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
- | |
1551 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
- | |
1552 | - | ||
1553 | /* Prepare transfer parameters */ |
- | |
1554 | hi2c->pBuffPtr = pData; |
- | |
1555 | hi2c->XferCount = Size; |
- | |
1556 | hi2c->XferOptions = XferOptions; |
- | |
1557 | hi2c->XferSize = hi2c->XferCount; |
- | |
1558 | hi2c->Devaddress = DevAddress; |
- | |
1559 | - | ||
1560 | if((hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) || (hi2c->PreviousState == I2C_STATE_NONE)) |
- | |
1561 | { |
- | |
1562 | /* Generate Start condition if first transfer */ |
- | |
1563 | if((XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME) || (XferOptions == I2C_NO_OPTION_FRAME)) |
- | |
1564 | { |
- | |
1565 | /* Enable Acknowledge */ |
- | |
1566 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
- | |
1567 | - | ||
1568 | /* Generate Start */ |
- | |
1569 | hi2c->Instance->CR1 |= I2C_CR1_START; |
- | |
1570 | } |
- | |
1571 | else |
- | |
1572 | { |
- | |
1573 | /* Enable Acknowledge */ |
- | |
1574 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
- | |
1575 | - | ||
1576 | /* Generate ReStart */ |
- | |
1577 | hi2c->Instance->CR1 |= I2C_CR1_START; |
- | |
1578 | } |
- | |
1579 | } |
- | |
1580 | 1848 | ||
1581 | /* Process Unlocked */ |
1849 | /* Process Unlocked */ |
1582 | __HAL_UNLOCK(hi2c); |
1850 | __HAL_UNLOCK(hi2c); |
1583 | 1851 | ||
1584 | /* Note : The I2C interrupts must be enabled after unlocking current process |
1852 | /* Note : The I2C interrupts must be enabled after unlocking current process |
Line 1604... | Line 1872... | ||
1604 | * @param Size Amount of data to be sent |
1872 | * @param Size Amount of data to be sent |
1605 | * @retval HAL status |
1873 | * @retval HAL status |
1606 | */ |
1874 | */ |
1607 | HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) |
1875 | HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) |
1608 | { |
1876 | { |
1609 | __IO uint32_t count = 0U; |
- | |
1610 | 1877 | ||
1611 | if(hi2c->State == HAL_I2C_STATE_READY) |
1878 | if (hi2c->State == HAL_I2C_STATE_READY) |
1612 | { |
1879 | { |
1613 | if((pData == NULL) || (Size == 0U)) |
1880 | if ((pData == NULL) || (Size == 0U)) |
1614 | { |
1881 | { |
1615 | return HAL_ERROR; |
1882 | return HAL_ERROR; |
1616 | } |
1883 | } |
1617 | 1884 | ||
1618 | /* Wait until BUSY flag is reset */ |
- | |
1619 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
- | |
1620 | do |
- | |
1621 | { |
- | |
1622 | if(count-- == 0U) |
- | |
1623 | { |
- | |
1624 | hi2c->PreviousState = I2C_STATE_NONE; |
- | |
1625 | hi2c->State= HAL_I2C_STATE_READY; |
- | |
1626 | - | ||
1627 | /* Process Unlocked */ |
- | |
1628 | __HAL_UNLOCK(hi2c); |
- | |
1629 | - | ||
1630 | return HAL_TIMEOUT; |
- | |
1631 | } |
- | |
1632 | } |
- | |
1633 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
- | |
1634 | - | ||
1635 | /* Process Locked */ |
1885 | /* Process Locked */ |
1636 | __HAL_LOCK(hi2c); |
1886 | __HAL_LOCK(hi2c); |
1637 | 1887 | ||
1638 | /* Check if the I2C is already enabled */ |
1888 | /* Check if the I2C is already enabled */ |
1639 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1889 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1640 | { |
1890 | { |
1641 | /* Enable I2C peripheral */ |
1891 | /* Enable I2C peripheral */ |
1642 | __HAL_I2C_ENABLE(hi2c); |
1892 | __HAL_I2C_ENABLE(hi2c); |
1643 | } |
1893 | } |
1644 | 1894 | ||
1645 | /* Disable Pos */ |
1895 | /* Disable Pos */ |
1646 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
1896 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
1647 | 1897 | ||
1648 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
1898 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
1649 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
1899 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
1650 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1900 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1651 | 1901 | ||
1652 | /* Prepare transfer parameters */ |
1902 | /* Prepare transfer parameters */ |
1653 | hi2c->pBuffPtr = pData; |
1903 | hi2c->pBuffPtr = pData; |
1654 | hi2c->XferCount = Size; |
1904 | hi2c->XferCount = Size; |
1655 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
1656 | hi2c->XferSize = hi2c->XferCount; |
1905 | hi2c->XferSize = hi2c->XferCount; |
- | 1906 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
1657 | 1907 | ||
1658 | /* Enable Address Acknowledge */ |
1908 | /* Enable Address Acknowledge */ |
1659 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
1909 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
1660 | 1910 | ||
1661 | /* Process Unlocked */ |
1911 | /* Process Unlocked */ |
1662 | __HAL_UNLOCK(hi2c); |
1912 | __HAL_UNLOCK(hi2c); |
1663 | 1913 | ||
1664 | /* Note : The I2C interrupts must be enabled after unlocking current process |
1914 | /* Note : The I2C interrupts must be enabled after unlocking current process |
Line 1675... | Line 1925... | ||
1675 | return HAL_BUSY; |
1925 | return HAL_BUSY; |
1676 | } |
1926 | } |
1677 | } |
1927 | } |
1678 | 1928 | ||
1679 | /** |
1929 | /** |
1680 | * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt |
1930 | * @brief Receive in slave mode an amount of data in non-blocking mode with Interrupt |
1681 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
1931 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
1682 | * the configuration information for the specified I2C. |
1932 | * the configuration information for the specified I2C. |
1683 | * @param pData Pointer to data buffer |
1933 | * @param pData Pointer to data buffer |
1684 | * @param Size Amount of data to be sent |
1934 | * @param Size Amount of data to be sent |
1685 | * @retval HAL status |
1935 | * @retval HAL status |
1686 | */ |
1936 | */ |
1687 | HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) |
1937 | HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) |
1688 | { |
1938 | { |
1689 | __IO uint32_t count = 0U; |
- | |
1690 | 1939 | ||
1691 | if(hi2c->State == HAL_I2C_STATE_READY) |
1940 | if (hi2c->State == HAL_I2C_STATE_READY) |
1692 | { |
1941 | { |
1693 | if((pData == NULL) || (Size == 0U)) |
1942 | if ((pData == NULL) || (Size == 0U)) |
1694 | { |
1943 | { |
1695 | return HAL_ERROR; |
1944 | return HAL_ERROR; |
1696 | } |
1945 | } |
1697 | 1946 | ||
1698 | /* Wait until BUSY flag is reset */ |
- | |
1699 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
- | |
1700 | do |
- | |
1701 | { |
- | |
1702 | if(count-- == 0U) |
- | |
1703 | { |
- | |
1704 | hi2c->PreviousState = I2C_STATE_NONE; |
- | |
1705 | hi2c->State= HAL_I2C_STATE_READY; |
- | |
1706 | - | ||
1707 | /* Process Unlocked */ |
- | |
1708 | __HAL_UNLOCK(hi2c); |
- | |
1709 | - | ||
1710 | return HAL_TIMEOUT; |
- | |
1711 | } |
- | |
1712 | } |
- | |
1713 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
- | |
1714 | - | ||
1715 | /* Process Locked */ |
1947 | /* Process Locked */ |
1716 | __HAL_LOCK(hi2c); |
1948 | __HAL_LOCK(hi2c); |
1717 | 1949 | ||
1718 | /* Check if the I2C is already enabled */ |
1950 | /* Check if the I2C is already enabled */ |
1719 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1951 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1720 | { |
1952 | { |
1721 | /* Enable I2C peripheral */ |
1953 | /* Enable I2C peripheral */ |
1722 | __HAL_I2C_ENABLE(hi2c); |
1954 | __HAL_I2C_ENABLE(hi2c); |
1723 | } |
1955 | } |
1724 | 1956 | ||
1725 | /* Disable Pos */ |
1957 | /* Disable Pos */ |
1726 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
1958 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
1727 | 1959 | ||
1728 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
1960 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
1729 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
1961 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
1730 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1962 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
1731 | 1963 | ||
1732 | /* Prepare transfer parameters */ |
1964 | /* Prepare transfer parameters */ |
1733 | hi2c->pBuffPtr = pData; |
- | |
1734 | hi2c->XferSize = Size; |
- | |
1735 | hi2c->XferCount = Size; |
- | |
1736 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
1737 | - | ||
1738 | /* Enable Address Acknowledge */ |
- | |
1739 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
- | |
1740 | - | ||
1741 | /* Process Unlocked */ |
- | |
1742 | __HAL_UNLOCK(hi2c); |
- | |
1743 | - | ||
1744 | /* Note : The I2C interrupts must be enabled after unlocking current process |
- | |
1745 | to avoid the risk of I2C interrupt handle execution before current |
- | |
1746 | process unlock */ |
- | |
1747 | - | ||
1748 | /* Enable EVT, BUF and ERR interrupt */ |
- | |
1749 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
- | |
1750 | - | ||
1751 | return HAL_OK; |
- | |
1752 | } |
- | |
1753 | else |
- | |
1754 | { |
- | |
1755 | return HAL_BUSY; |
- | |
1756 | } |
- | |
1757 | } |
- | |
1758 | - | ||
1759 | /** |
- | |
1760 | * @brief Sequential transmit in slave mode an amount of data in no-blocking mode with Interrupt |
- | |
1761 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
- | |
1762 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
- | |
1763 | * the configuration information for I2C module |
- | |
1764 | * @param pData Pointer to data buffer |
- | |
1765 | * @param Size Amount of data to be sent |
- | |
1766 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
- | |
1767 | * @retval HAL status |
- | |
1768 | */ |
- | |
1769 | HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
- | |
1770 | { |
- | |
1771 | /* Check the parameters */ |
- | |
1772 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
- | |
1773 | - | ||
1774 | if(hi2c->State == HAL_I2C_STATE_LISTEN) |
- | |
1775 | { |
- | |
1776 | if((pData == NULL) || (Size == 0U)) |
- | |
1777 | { |
- | |
1778 | return HAL_ERROR; |
- | |
1779 | } |
- | |
1780 | - | ||
1781 | /* Process Locked */ |
- | |
1782 | __HAL_LOCK(hi2c); |
- | |
1783 | - | ||
1784 | /* Check if the I2C is already enabled */ |
- | |
1785 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
- | |
1786 | { |
- | |
1787 | /* Enable I2C peripheral */ |
- | |
1788 | __HAL_I2C_ENABLE(hi2c); |
- | |
1789 | } |
- | |
1790 | - | ||
1791 | /* Disable Pos */ |
- | |
1792 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
- | |
1793 | - | ||
1794 | hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN; |
- | |
1795 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
- | |
1796 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
- | |
1797 | - | ||
1798 | /* Prepare transfer parameters */ |
- | |
1799 | hi2c->pBuffPtr = pData; |
1965 | hi2c->pBuffPtr = pData; |
1800 | hi2c->XferCount = Size; |
1966 | hi2c->XferCount = Size; |
1801 | hi2c->XferOptions = XferOptions; |
- | |
1802 | hi2c->XferSize = hi2c->XferCount; |
- | |
1803 | - | ||
1804 | /* Clear ADDR flag */ |
- | |
1805 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
- | |
1806 | - | ||
1807 | /* Process Unlocked */ |
- | |
1808 | __HAL_UNLOCK(hi2c); |
- | |
1809 | - | ||
1810 | /* Note : The I2C interrupts must be enabled after unlocking current process |
- | |
1811 | to avoid the risk of I2C interrupt handle execution before current |
- | |
1812 | process unlock */ |
- | |
1813 | - | ||
1814 | /* Enable EVT, BUF and ERR interrupt */ |
- | |
1815 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
- | |
1816 | - | ||
1817 | return HAL_OK; |
- | |
1818 | } |
- | |
1819 | else |
- | |
1820 | { |
- | |
1821 | return HAL_BUSY; |
- | |
1822 | } |
- | |
1823 | } |
- | |
1824 | - | ||
1825 | /** |
- | |
1826 | * @brief Sequential receive in slave mode an amount of data in non-blocking mode with Interrupt |
- | |
1827 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
- | |
1828 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
- | |
1829 | * the configuration information for the specified I2C. |
- | |
1830 | * @param pData Pointer to data buffer |
- | |
1831 | * @param Size Amount of data to be sent |
- | |
1832 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
- | |
1833 | * @retval HAL status |
- | |
1834 | */ |
- | |
1835 | HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
- | |
1836 | { |
- | |
1837 | /* Check the parameters */ |
- | |
1838 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
- | |
1839 | - | ||
1840 | if(hi2c->State == HAL_I2C_STATE_LISTEN) |
- | |
1841 | { |
- | |
1842 | if((pData == NULL) || (Size == 0U)) |
- | |
1843 | { |
- | |
1844 | return HAL_ERROR; |
- | |
1845 | } |
- | |
1846 | - | ||
1847 | /* Process Locked */ |
- | |
1848 | __HAL_LOCK(hi2c); |
- | |
1849 | - | ||
1850 | /* Check if the I2C is already enabled */ |
- | |
1851 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
- | |
1852 | { |
- | |
1853 | /* Enable I2C peripheral */ |
- | |
1854 | __HAL_I2C_ENABLE(hi2c); |
- | |
1855 | } |
- | |
1856 | - | ||
1857 | /* Disable Pos */ |
- | |
1858 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
- | |
1859 | - | ||
1860 | hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN; |
- | |
1861 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
- | |
1862 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
- | |
1863 | - | ||
1864 | /* Prepare transfer parameters */ |
- | |
1865 | hi2c->pBuffPtr = pData; |
- | |
1866 | hi2c->XferCount = Size; |
- | |
1867 | hi2c->XferOptions = XferOptions; |
- | |
1868 | hi2c->XferSize = hi2c->XferCount; |
1967 | hi2c->XferSize = hi2c->XferCount; |
- | 1968 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
1869 | 1969 | ||
1870 | /* Clear ADDR flag */ |
1970 | /* Enable Address Acknowledge */ |
1871 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
1971 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
1872 | 1972 | ||
1873 | /* Process Unlocked */ |
1973 | /* Process Unlocked */ |
1874 | __HAL_UNLOCK(hi2c); |
1974 | __HAL_UNLOCK(hi2c); |
1875 | 1975 | ||
1876 | /* Note : The I2C interrupts must be enabled after unlocking current process |
1976 | /* Note : The I2C interrupts must be enabled after unlocking current process |
Line 1887... | Line 1987... | ||
1887 | return HAL_BUSY; |
1987 | return HAL_BUSY; |
1888 | } |
1988 | } |
1889 | } |
1989 | } |
1890 | 1990 | ||
1891 | /** |
1991 | /** |
1892 | * @brief Enable the Address listen mode with Interrupt. |
- | |
1893 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
- | |
1894 | * the configuration information for the specified I2C. |
- | |
1895 | * @retval HAL status |
- | |
1896 | */ |
- | |
1897 | HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c) |
- | |
1898 | { |
- | |
1899 | if(hi2c->State == HAL_I2C_STATE_READY) |
- | |
1900 | { |
- | |
1901 | hi2c->State = HAL_I2C_STATE_LISTEN; |
- | |
1902 | - | ||
1903 | /* Check if the I2C is already enabled */ |
- | |
1904 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
- | |
1905 | { |
- | |
1906 | /* Enable I2C peripheral */ |
- | |
1907 | __HAL_I2C_ENABLE(hi2c); |
- | |
1908 | } |
- | |
1909 | - | ||
1910 | /* Enable Address Acknowledge */ |
- | |
1911 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
- | |
1912 | - | ||
1913 | /* Enable EVT and ERR interrupt */ |
- | |
1914 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
- | |
1915 | - | ||
1916 | return HAL_OK; |
- | |
1917 | } |
- | |
1918 | else |
- | |
1919 | { |
- | |
1920 | return HAL_BUSY; |
- | |
1921 | } |
- | |
1922 | } |
- | |
1923 | - | ||
1924 | /** |
- | |
1925 | * @brief Disable the Address listen mode with Interrupt. |
- | |
1926 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
- | |
1927 | * the configuration information for the specified I2C. |
- | |
1928 | * @retval HAL status |
- | |
1929 | */ |
- | |
1930 | HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c) |
- | |
1931 | { |
- | |
1932 | /* Declaration of tmp to prevent undefined behavior of volatile usage */ |
- | |
1933 | uint32_t tmp; |
- | |
1934 | - | ||
1935 | /* Disable Address listen mode only if a transfer is not ongoing */ |
- | |
1936 | if(hi2c->State == HAL_I2C_STATE_LISTEN) |
- | |
1937 | { |
- | |
1938 | tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK; |
- | |
1939 | hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode); |
- | |
1940 | hi2c->State = HAL_I2C_STATE_READY; |
- | |
1941 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | |
1942 | - | ||
1943 | /* Disable Address Acknowledge */ |
- | |
1944 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
- | |
1945 | - | ||
1946 | /* Disable EVT and ERR interrupt */ |
- | |
1947 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
- | |
1948 | - | ||
1949 | return HAL_OK; |
- | |
1950 | } |
- | |
1951 | else |
- | |
1952 | { |
- | |
1953 | return HAL_BUSY; |
- | |
1954 | } |
- | |
1955 | } |
- | |
1956 | - | ||
1957 | /** |
- | |
1958 | * @brief Transmit in master mode an amount of data in non-blocking mode with DMA |
1992 | * @brief Transmit in master mode an amount of data in non-blocking mode with DMA |
1959 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
1993 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
1960 | * the configuration information for the specified I2C. |
1994 | * the configuration information for the specified I2C. |
1961 | * @param DevAddress Target device address: The device 7 bits address value |
1995 | * @param DevAddress Target device address: The device 7 bits address value |
1962 | * in datasheet must be shifted to the left before calling the interface |
1996 | * in datasheet must be shifted to the left before calling the interface |
Line 1965... | Line 1999... | ||
1965 | * @retval HAL status |
1999 | * @retval HAL status |
1966 | */ |
2000 | */ |
1967 | HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
2001 | HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
1968 | { |
2002 | { |
1969 | __IO uint32_t count = 0U; |
2003 | __IO uint32_t count = 0U; |
- | 2004 | HAL_StatusTypeDef dmaxferstatus; |
|
1970 | 2005 | ||
1971 | if(hi2c->State == HAL_I2C_STATE_READY) |
2006 | if (hi2c->State == HAL_I2C_STATE_READY) |
1972 | { |
2007 | { |
1973 | /* Wait until BUSY flag is reset */ |
2008 | /* Wait until BUSY flag is reset */ |
1974 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
2009 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
1975 | do |
2010 | do |
1976 | { |
2011 | { |
- | 2012 | count--; |
|
1977 | if(count-- == 0U) |
2013 | if (count == 0U) |
1978 | { |
2014 | { |
1979 | hi2c->PreviousState = I2C_STATE_NONE; |
2015 | hi2c->PreviousState = I2C_STATE_NONE; |
1980 | hi2c->State= HAL_I2C_STATE_READY; |
2016 | hi2c->State = HAL_I2C_STATE_READY; |
- | 2017 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 2018 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
1981 | 2019 | ||
1982 | /* Process Unlocked */ |
2020 | /* Process Unlocked */ |
1983 | __HAL_UNLOCK(hi2c); |
2021 | __HAL_UNLOCK(hi2c); |
1984 | 2022 | ||
1985 | return HAL_TIMEOUT; |
2023 | return HAL_ERROR; |
1986 | } |
2024 | } |
1987 | } |
2025 | } |
1988 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
2026 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
1989 | 2027 | ||
1990 | /* Process Locked */ |
2028 | /* Process Locked */ |
1991 | __HAL_LOCK(hi2c); |
2029 | __HAL_LOCK(hi2c); |
1992 | 2030 | ||
1993 | /* Check if the I2C is already enabled */ |
2031 | /* Check if the I2C is already enabled */ |
1994 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2032 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
1995 | { |
2033 | { |
1996 | /* Enable I2C peripheral */ |
2034 | /* Enable I2C peripheral */ |
1997 | __HAL_I2C_ENABLE(hi2c); |
2035 | __HAL_I2C_ENABLE(hi2c); |
1998 | } |
2036 | } |
1999 | 2037 | ||
2000 | /* Disable Pos */ |
2038 | /* Disable Pos */ |
2001 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
2039 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
2002 | 2040 | ||
2003 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
2041 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
2004 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
2042 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
2005 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2043 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2006 | 2044 | ||
2007 | /* Prepare transfer parameters */ |
2045 | /* Prepare transfer parameters */ |
2008 | hi2c->pBuffPtr = pData; |
2046 | hi2c->pBuffPtr = pData; |
2009 | hi2c->XferCount = Size; |
2047 | hi2c->XferCount = Size; |
2010 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
2011 | hi2c->XferSize = hi2c->XferCount; |
2048 | hi2c->XferSize = hi2c->XferCount; |
- | 2049 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
2012 | hi2c->Devaddress = DevAddress; |
2050 | hi2c->Devaddress = DevAddress; |
2013 | 2051 | ||
2014 | if(hi2c->XferSize > 0U) |
2052 | if (hi2c->XferSize > 0U) |
2015 | { |
2053 | { |
2016 | /* Set the I2C DMA transfer complete callback */ |
2054 | /* Set the I2C DMA transfer complete callback */ |
2017 | hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt; |
2055 | hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt; |
2018 | 2056 | ||
2019 | /* Set the DMA error callback */ |
2057 | /* Set the DMA error callback */ |
2020 | hi2c->hdmatx->XferErrorCallback = I2C_DMAError; |
2058 | hi2c->hdmatx->XferErrorCallback = I2C_DMAError; |
2021 | 2059 | ||
2022 | /* Set the unused DMA callbacks to NULL */ |
2060 | /* Set the unused DMA callbacks to NULL */ |
2023 | hi2c->hdmatx->XferHalfCpltCallback = NULL; |
2061 | hi2c->hdmatx->XferHalfCpltCallback = NULL; |
2024 | hi2c->hdmatx->XferAbortCallback = NULL; |
2062 | hi2c->hdmatx->XferAbortCallback = NULL; |
2025 | 2063 | ||
2026 | /* Enable the DMA channel */ |
2064 | /* Enable the DMA channel */ |
2027 | HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize); |
2065 | dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize); |
2028 | 2066 | ||
- | 2067 | if (dmaxferstatus == HAL_OK) |
|
- | 2068 | { |
|
2029 | /* Enable Acknowledge */ |
2069 | /* Enable Acknowledge */ |
2030 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
2070 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2031 | 2071 | ||
2032 | /* Generate Start */ |
2072 | /* Generate Start */ |
2033 | hi2c->Instance->CR1 |= I2C_CR1_START; |
2073 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
2034 | 2074 | ||
2035 | /* Process Unlocked */ |
2075 | /* Process Unlocked */ |
2036 | __HAL_UNLOCK(hi2c); |
2076 | __HAL_UNLOCK(hi2c); |
2037 | 2077 | ||
2038 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2078 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2039 | to avoid the risk of I2C interrupt handle execution before current |
2079 | to avoid the risk of I2C interrupt handle execution before current |
2040 | process unlock */ |
2080 | process unlock */ |
2041 | 2081 | ||
2042 | /* Enable EVT and ERR interrupt */ |
2082 | /* Enable EVT and ERR interrupt */ |
2043 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
2083 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
2044 | 2084 | ||
2045 | /* Enable DMA Request */ |
2085 | /* Enable DMA Request */ |
2046 | hi2c->Instance->CR2 |= I2C_CR2_DMAEN; |
2086 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
- | 2087 | } |
|
- | 2088 | else |
|
- | 2089 | { |
|
- | 2090 | /* Update I2C state */ |
|
- | 2091 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 2092 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 2093 | ||
- | 2094 | /* Update I2C error code */ |
|
- | 2095 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
|
- | 2096 | ||
- | 2097 | /* Process Unlocked */ |
|
- | 2098 | __HAL_UNLOCK(hi2c); |
|
- | 2099 | ||
- | 2100 | return HAL_ERROR; |
|
- | 2101 | } |
|
2047 | } |
2102 | } |
2048 | else |
2103 | else |
2049 | { |
2104 | { |
2050 | /* Enable Acknowledge */ |
2105 | /* Enable Acknowledge */ |
2051 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
2106 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2052 | 2107 | ||
2053 | /* Generate Start */ |
2108 | /* Generate Start */ |
2054 | hi2c->Instance->CR1 |= I2C_CR1_START; |
2109 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
2055 | 2110 | ||
2056 | /* Process Unlocked */ |
2111 | /* Process Unlocked */ |
2057 | __HAL_UNLOCK(hi2c); |
2112 | __HAL_UNLOCK(hi2c); |
2058 | 2113 | ||
2059 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2114 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2060 | to avoid the risk of I2C interrupt handle execution before current |
2115 | to avoid the risk of I2C interrupt handle execution before current |
2061 | process unlock */ |
2116 | process unlock */ |
2062 | 2117 | ||
2063 | /* Enable EVT, BUF and ERR interrupt */ |
2118 | /* Enable EVT, BUF and ERR interrupt */ |
2064 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
2119 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
2065 | } |
2120 | } |
2066 | 2121 | ||
2067 | return HAL_OK; |
2122 | return HAL_OK; |
2068 | } |
2123 | } |
2069 | else |
2124 | else |
Line 2071... | Line 2126... | ||
2071 | return HAL_BUSY; |
2126 | return HAL_BUSY; |
2072 | } |
2127 | } |
2073 | } |
2128 | } |
2074 | 2129 | ||
2075 | /** |
2130 | /** |
2076 | * @brief Receive in master mode an amount of data in non-blocking mode with DMA |
2131 | * @brief Receive in master mode an amount of data in non-blocking mode with DMA |
2077 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
2132 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
2078 | * the configuration information for the specified I2C. |
2133 | * the configuration information for the specified I2C. |
2079 | * @param DevAddress Target device address: The device 7 bits address value |
2134 | * @param DevAddress Target device address: The device 7 bits address value |
2080 | * in datasheet must be shifted to the left before calling the interface |
2135 | * in datasheet must be shifted to the left before calling the interface |
2081 | * @param pData Pointer to data buffer |
2136 | * @param pData Pointer to data buffer |
Line 2083... | Line 2138... | ||
2083 | * @retval HAL status |
2138 | * @retval HAL status |
2084 | */ |
2139 | */ |
2085 | HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
2140 | HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
2086 | { |
2141 | { |
2087 | __IO uint32_t count = 0U; |
2142 | __IO uint32_t count = 0U; |
- | 2143 | HAL_StatusTypeDef dmaxferstatus; |
|
2088 | 2144 | ||
2089 | if(hi2c->State == HAL_I2C_STATE_READY) |
2145 | if (hi2c->State == HAL_I2C_STATE_READY) |
2090 | { |
2146 | { |
2091 | /* Wait until BUSY flag is reset */ |
2147 | /* Wait until BUSY flag is reset */ |
2092 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
2148 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
2093 | do |
2149 | do |
2094 | { |
2150 | { |
- | 2151 | count--; |
|
2095 | if(count-- == 0U) |
2152 | if (count == 0U) |
2096 | { |
2153 | { |
2097 | hi2c->PreviousState = I2C_STATE_NONE; |
2154 | hi2c->PreviousState = I2C_STATE_NONE; |
2098 | hi2c->State= HAL_I2C_STATE_READY; |
2155 | hi2c->State = HAL_I2C_STATE_READY; |
- | 2156 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 2157 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
2099 | 2158 | ||
2100 | /* Process Unlocked */ |
2159 | /* Process Unlocked */ |
2101 | __HAL_UNLOCK(hi2c); |
2160 | __HAL_UNLOCK(hi2c); |
2102 | 2161 | ||
2103 | return HAL_TIMEOUT; |
2162 | return HAL_ERROR; |
2104 | } |
2163 | } |
2105 | } |
2164 | } |
2106 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
2165 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
2107 | 2166 | ||
2108 | /* Process Locked */ |
2167 | /* Process Locked */ |
2109 | __HAL_LOCK(hi2c); |
2168 | __HAL_LOCK(hi2c); |
2110 | 2169 | ||
2111 | /* Check if the I2C is already enabled */ |
2170 | /* Check if the I2C is already enabled */ |
2112 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2171 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2113 | { |
2172 | { |
2114 | /* Enable I2C peripheral */ |
2173 | /* Enable I2C peripheral */ |
2115 | __HAL_I2C_ENABLE(hi2c); |
2174 | __HAL_I2C_ENABLE(hi2c); |
2116 | } |
2175 | } |
2117 | 2176 | ||
2118 | /* Disable Pos */ |
2177 | /* Disable Pos */ |
2119 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
2178 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
2120 | 2179 | ||
2121 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
2180 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
2122 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
2181 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
2123 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2182 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2124 | 2183 | ||
2125 | /* Prepare transfer parameters */ |
2184 | /* Prepare transfer parameters */ |
2126 | hi2c->pBuffPtr = pData; |
2185 | hi2c->pBuffPtr = pData; |
2127 | hi2c->XferCount = Size; |
2186 | hi2c->XferCount = Size; |
2128 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
2129 | hi2c->XferSize = hi2c->XferCount; |
2187 | hi2c->XferSize = hi2c->XferCount; |
- | 2188 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
2130 | hi2c->Devaddress = DevAddress; |
2189 | hi2c->Devaddress = DevAddress; |
2131 | 2190 | ||
2132 | if(hi2c->XferSize > 0U) |
2191 | if (hi2c->XferSize > 0U) |
2133 | { |
2192 | { |
2134 | /* Set the I2C DMA transfer complete callback */ |
2193 | /* Set the I2C DMA transfer complete callback */ |
2135 | hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt; |
2194 | hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt; |
2136 | 2195 | ||
2137 | /* Set the DMA error callback */ |
2196 | /* Set the DMA error callback */ |
Line 2140... | Line 2199... | ||
2140 | /* Set the unused DMA callbacks to NULL */ |
2199 | /* Set the unused DMA callbacks to NULL */ |
2141 | hi2c->hdmarx->XferHalfCpltCallback = NULL; |
2200 | hi2c->hdmarx->XferHalfCpltCallback = NULL; |
2142 | hi2c->hdmarx->XferAbortCallback = NULL; |
2201 | hi2c->hdmarx->XferAbortCallback = NULL; |
2143 | 2202 | ||
2144 | /* Enable the DMA channel */ |
2203 | /* Enable the DMA channel */ |
2145 | HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize); |
2204 | dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize); |
2146 | 2205 | ||
- | 2206 | if (dmaxferstatus == HAL_OK) |
|
- | 2207 | { |
|
2147 | /* Enable Acknowledge */ |
2208 | /* Enable Acknowledge */ |
2148 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
2209 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2149 | 2210 | ||
2150 | /* Generate Start */ |
2211 | /* Generate Start */ |
2151 | hi2c->Instance->CR1 |= I2C_CR1_START; |
2212 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
2152 | 2213 | ||
2153 | /* Process Unlocked */ |
2214 | /* Process Unlocked */ |
2154 | __HAL_UNLOCK(hi2c); |
2215 | __HAL_UNLOCK(hi2c); |
2155 | 2216 | ||
2156 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2217 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2157 | to avoid the risk of I2C interrupt handle execution before current |
2218 | to avoid the risk of I2C interrupt handle execution before current |
2158 | process unlock */ |
2219 | process unlock */ |
2159 | 2220 | ||
2160 | /* Enable EVT and ERR interrupt */ |
2221 | /* Enable EVT and ERR interrupt */ |
2161 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
2222 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
2162 | 2223 | ||
2163 | /* Enable DMA Request */ |
2224 | /* Enable DMA Request */ |
2164 | hi2c->Instance->CR2 |= I2C_CR2_DMAEN; |
2225 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
- | 2226 | } |
|
- | 2227 | else |
|
- | 2228 | { |
|
- | 2229 | /* Update I2C state */ |
|
- | 2230 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 2231 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 2232 | ||
- | 2233 | /* Update I2C error code */ |
|
- | 2234 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
|
- | 2235 | ||
- | 2236 | /* Process Unlocked */ |
|
- | 2237 | __HAL_UNLOCK(hi2c); |
|
- | 2238 | ||
- | 2239 | return HAL_ERROR; |
|
- | 2240 | } |
|
2165 | } |
2241 | } |
2166 | else |
2242 | else |
2167 | { |
2243 | { |
2168 | /* Enable Acknowledge */ |
2244 | /* Enable Acknowledge */ |
2169 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
2245 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2170 | 2246 | ||
2171 | /* Generate Start */ |
2247 | /* Generate Start */ |
2172 | hi2c->Instance->CR1 |= I2C_CR1_START; |
2248 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
2173 | 2249 | ||
2174 | /* Process Unlocked */ |
2250 | /* Process Unlocked */ |
2175 | __HAL_UNLOCK(hi2c); |
2251 | __HAL_UNLOCK(hi2c); |
2176 | 2252 | ||
2177 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2253 | /* Note : The I2C interrupts must be enabled after unlocking current process |
Line 2179... | Line 2255... | ||
2179 | process unlock */ |
2255 | process unlock */ |
2180 | 2256 | ||
2181 | /* Enable EVT, BUF and ERR interrupt */ |
2257 | /* Enable EVT, BUF and ERR interrupt */ |
2182 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
2258 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
2183 | } |
2259 | } |
2184 | - | ||
2185 | return HAL_OK; |
- | |
2186 | } |
- | |
2187 | else |
- | |
2188 | { |
- | |
2189 | return HAL_BUSY; |
- | |
2190 | } |
- | |
2191 | } |
- | |
2192 | - | ||
2193 | /** |
- | |
2194 | * @brief Abort a master I2C process communication with Interrupt. |
- | |
2195 | * @note This abort can be called only if state is ready |
- | |
2196 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
- | |
2197 | * the configuration information for the specified I2C. |
- | |
2198 | * @param DevAddress Target device address: The device 7 bits address value |
- | |
2199 | * in datasheet must be shifted to the left before calling the interface |
- | |
2200 | * @retval HAL status |
- | |
2201 | */ |
- | |
2202 | HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress) |
- | |
2203 | { |
- | |
2204 | /* Prevent unused argument(s) compilation warning */ |
- | |
2205 | UNUSED(DevAddress); |
- | |
2206 | - | ||
2207 | /* Abort Master transfer during Receive or Transmit process */ |
- | |
2208 | if(hi2c->Mode == HAL_I2C_MODE_MASTER) |
- | |
2209 | { |
- | |
2210 | /* Process Locked */ |
- | |
2211 | __HAL_LOCK(hi2c); |
- | |
2212 | - | ||
2213 | hi2c->PreviousState = I2C_STATE_NONE; |
- | |
2214 | hi2c->State = HAL_I2C_STATE_ABORT; |
- | |
2215 | - | ||
2216 | /* Disable Acknowledge */ |
- | |
2217 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
- | |
2218 | - | ||
2219 | /* Generate Stop */ |
- | |
2220 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
- | |
2221 | - | ||
2222 | hi2c->XferCount = 0U; |
- | |
2223 | - | ||
2224 | /* Disable EVT, BUF and ERR interrupt */ |
- | |
2225 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
- | |
2226 | - | ||
2227 | /* Process Unlocked */ |
- | |
2228 | __HAL_UNLOCK(hi2c); |
- | |
2229 | - | ||
2230 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
- | |
2231 | I2C_ITError(hi2c); |
- | |
2232 | 2260 | ||
2233 | return HAL_OK; |
2261 | return HAL_OK; |
2234 | } |
2262 | } |
2235 | else |
2263 | else |
2236 | { |
2264 | { |
2237 | /* Wrong usage of abort function */ |
- | |
2238 | /* This function should be used only in case of abort monitored by master device */ |
- | |
2239 | return HAL_ERROR; |
2265 | return HAL_BUSY; |
2240 | } |
2266 | } |
2241 | } |
2267 | } |
2242 | 2268 | ||
2243 | /** |
2269 | /** |
2244 | * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA |
2270 | * @brief Transmit in slave mode an amount of data in non-blocking mode with DMA |
2245 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
2271 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
2246 | * the configuration information for the specified I2C. |
2272 | * the configuration information for the specified I2C. |
2247 | * @param pData Pointer to data buffer |
2273 | * @param pData Pointer to data buffer |
2248 | * @param Size Amount of data to be sent |
2274 | * @param Size Amount of data to be sent |
2249 | * @retval HAL status |
2275 | * @retval HAL status |
2250 | */ |
2276 | */ |
2251 | HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) |
2277 | HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) |
2252 | { |
2278 | { |
2253 | __IO uint32_t count = 0U; |
2279 | HAL_StatusTypeDef dmaxferstatus; |
2254 | 2280 | ||
2255 | if(hi2c->State == HAL_I2C_STATE_READY) |
2281 | if (hi2c->State == HAL_I2C_STATE_READY) |
2256 | { |
2282 | { |
2257 | if((pData == NULL) || (Size == 0U)) |
2283 | if ((pData == NULL) || (Size == 0U)) |
2258 | { |
2284 | { |
2259 | return HAL_ERROR; |
2285 | return HAL_ERROR; |
2260 | } |
2286 | } |
2261 | 2287 | ||
2262 | /* Wait until BUSY flag is reset */ |
- | |
2263 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
- | |
2264 | do |
- | |
2265 | { |
- | |
2266 | if(count-- == 0U) |
- | |
2267 | { |
- | |
2268 | hi2c->PreviousState = I2C_STATE_NONE; |
- | |
2269 | hi2c->State= HAL_I2C_STATE_READY; |
- | |
2270 | - | ||
2271 | /* Process Unlocked */ |
- | |
2272 | __HAL_UNLOCK(hi2c); |
- | |
2273 | - | ||
2274 | return HAL_TIMEOUT; |
- | |
2275 | } |
- | |
2276 | } |
- | |
2277 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
- | |
2278 | - | ||
2279 | /* Process Locked */ |
2288 | /* Process Locked */ |
2280 | __HAL_LOCK(hi2c); |
2289 | __HAL_LOCK(hi2c); |
2281 | 2290 | ||
2282 | /* Check if the I2C is already enabled */ |
2291 | /* Check if the I2C is already enabled */ |
2283 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2292 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2284 | { |
2293 | { |
2285 | /* Enable I2C peripheral */ |
2294 | /* Enable I2C peripheral */ |
2286 | __HAL_I2C_ENABLE(hi2c); |
2295 | __HAL_I2C_ENABLE(hi2c); |
2287 | } |
2296 | } |
2288 | 2297 | ||
2289 | /* Disable Pos */ |
2298 | /* Disable Pos */ |
2290 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
2299 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
2291 | 2300 | ||
2292 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
2301 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
2293 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
2302 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
2294 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2303 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2295 | 2304 | ||
2296 | /* Prepare transfer parameters */ |
2305 | /* Prepare transfer parameters */ |
2297 | hi2c->pBuffPtr = pData; |
2306 | hi2c->pBuffPtr = pData; |
2298 | hi2c->XferCount = Size; |
2307 | hi2c->XferCount = Size; |
2299 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
2300 | hi2c->XferSize = hi2c->XferCount; |
2308 | hi2c->XferSize = hi2c->XferCount; |
- | 2309 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
2301 | 2310 | ||
2302 | /* Set the I2C DMA transfer complete callback */ |
2311 | /* Set the I2C DMA transfer complete callback */ |
2303 | hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt; |
2312 | hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt; |
2304 | 2313 | ||
2305 | /* Set the DMA error callback */ |
2314 | /* Set the DMA error callback */ |
2306 | hi2c->hdmatx->XferErrorCallback = I2C_DMAError; |
2315 | hi2c->hdmatx->XferErrorCallback = I2C_DMAError; |
2307 | 2316 | ||
2308 | /* Set the unused DMA callbacks to NULL */ |
2317 | /* Set the unused DMA callbacks to NULL */ |
2309 | hi2c->hdmatx->XferHalfCpltCallback = NULL; |
2318 | hi2c->hdmatx->XferHalfCpltCallback = NULL; |
2310 | hi2c->hdmatx->XferAbortCallback = NULL; |
2319 | hi2c->hdmatx->XferAbortCallback = NULL; |
2311 | 2320 | ||
2312 | /* Enable the DMA channel */ |
2321 | /* Enable the DMA channel */ |
2313 | HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize); |
2322 | dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize); |
2314 | 2323 | ||
- | 2324 | if (dmaxferstatus == HAL_OK) |
|
- | 2325 | { |
|
2315 | /* Enable Address Acknowledge */ |
2326 | /* Enable Address Acknowledge */ |
2316 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
2327 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2317 | 2328 | ||
2318 | /* Process Unlocked */ |
2329 | /* Process Unlocked */ |
2319 | __HAL_UNLOCK(hi2c); |
2330 | __HAL_UNLOCK(hi2c); |
2320 | 2331 | ||
2321 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2332 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2322 | to avoid the risk of I2C interrupt handle execution before current |
2333 | to avoid the risk of I2C interrupt handle execution before current |
2323 | process unlock */ |
2334 | process unlock */ |
2324 | /* Enable EVT and ERR interrupt */ |
2335 | /* Enable EVT and ERR interrupt */ |
2325 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
2336 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
2326 | 2337 | ||
2327 | /* Enable DMA Request */ |
2338 | /* Enable DMA Request */ |
2328 | hi2c->Instance->CR2 |= I2C_CR2_DMAEN; |
2339 | hi2c->Instance->CR2 |= I2C_CR2_DMAEN; |
2329 | 2340 | ||
2330 | return HAL_OK; |
2341 | return HAL_OK; |
- | 2342 | } |
|
- | 2343 | else |
|
- | 2344 | { |
|
- | 2345 | /* Update I2C state */ |
|
- | 2346 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 2347 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 2348 | ||
- | 2349 | /* Update I2C error code */ |
|
- | 2350 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
|
- | 2351 | ||
- | 2352 | /* Process Unlocked */ |
|
- | 2353 | __HAL_UNLOCK(hi2c); |
|
- | 2354 | ||
- | 2355 | return HAL_ERROR; |
|
- | 2356 | } |
|
2331 | } |
2357 | } |
2332 | else |
2358 | else |
2333 | { |
2359 | { |
2334 | return HAL_BUSY; |
2360 | return HAL_BUSY; |
2335 | } |
2361 | } |
2336 | } |
2362 | } |
2337 | 2363 | ||
2338 | /** |
2364 | /** |
2339 | * @brief Receive in slave mode an amount of data in non-blocking mode with DMA |
2365 | * @brief Receive in slave mode an amount of data in non-blocking mode with DMA |
2340 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
2366 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
2341 | * the configuration information for the specified I2C. |
2367 | * the configuration information for the specified I2C. |
2342 | * @param pData Pointer to data buffer |
2368 | * @param pData Pointer to data buffer |
2343 | * @param Size Amount of data to be sent |
2369 | * @param Size Amount of data to be sent |
2344 | * @retval HAL status |
2370 | * @retval HAL status |
2345 | */ |
2371 | */ |
2346 | HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) |
2372 | HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size) |
2347 | { |
2373 | { |
2348 | __IO uint32_t count = 0U; |
2374 | HAL_StatusTypeDef dmaxferstatus; |
2349 | 2375 | ||
2350 | if(hi2c->State == HAL_I2C_STATE_READY) |
2376 | if (hi2c->State == HAL_I2C_STATE_READY) |
2351 | { |
2377 | { |
2352 | if((pData == NULL) || (Size == 0U)) |
2378 | if ((pData == NULL) || (Size == 0U)) |
2353 | { |
2379 | { |
2354 | return HAL_ERROR; |
2380 | return HAL_ERROR; |
2355 | } |
2381 | } |
2356 | 2382 | ||
2357 | /* Wait until BUSY flag is reset */ |
- | |
2358 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
- | |
2359 | do |
- | |
2360 | { |
- | |
2361 | if(count-- == 0U) |
- | |
2362 | { |
- | |
2363 | hi2c->PreviousState = I2C_STATE_NONE; |
- | |
2364 | hi2c->State= HAL_I2C_STATE_READY; |
- | |
2365 | - | ||
2366 | /* Process Unlocked */ |
- | |
2367 | __HAL_UNLOCK(hi2c); |
- | |
2368 | - | ||
2369 | return HAL_TIMEOUT; |
- | |
2370 | } |
- | |
2371 | } |
- | |
2372 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
- | |
2373 | - | ||
2374 | /* Process Locked */ |
2383 | /* Process Locked */ |
2375 | __HAL_LOCK(hi2c); |
2384 | __HAL_LOCK(hi2c); |
2376 | 2385 | ||
2377 | /* Check if the I2C is already enabled */ |
2386 | /* Check if the I2C is already enabled */ |
2378 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2387 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2379 | { |
2388 | { |
2380 | /* Enable I2C peripheral */ |
2389 | /* Enable I2C peripheral */ |
2381 | __HAL_I2C_ENABLE(hi2c); |
2390 | __HAL_I2C_ENABLE(hi2c); |
2382 | } |
2391 | } |
2383 | 2392 | ||
2384 | /* Disable Pos */ |
2393 | /* Disable Pos */ |
2385 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
2394 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
2386 | 2395 | ||
2387 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
2396 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
2388 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
2397 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
2389 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2398 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2390 | 2399 | ||
2391 | /* Prepare transfer parameters */ |
2400 | /* Prepare transfer parameters */ |
2392 | hi2c->pBuffPtr = pData; |
2401 | hi2c->pBuffPtr = pData; |
2393 | hi2c->XferCount = Size; |
2402 | hi2c->XferCount = Size; |
2394 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
2395 | hi2c->XferSize = hi2c->XferCount; |
2403 | hi2c->XferSize = hi2c->XferCount; |
- | 2404 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
2396 | 2405 | ||
2397 | /* Set the I2C DMA transfer complete callback */ |
2406 | /* Set the I2C DMA transfer complete callback */ |
2398 | hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt; |
2407 | hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt; |
2399 | 2408 | ||
2400 | /* Set the DMA error callback */ |
2409 | /* Set the DMA error callback */ |
Line 2403... | Line 2412... | ||
2403 | /* Set the unused DMA callbacks to NULL */ |
2412 | /* Set the unused DMA callbacks to NULL */ |
2404 | hi2c->hdmarx->XferHalfCpltCallback = NULL; |
2413 | hi2c->hdmarx->XferHalfCpltCallback = NULL; |
2405 | hi2c->hdmarx->XferAbortCallback = NULL; |
2414 | hi2c->hdmarx->XferAbortCallback = NULL; |
2406 | 2415 | ||
2407 | /* Enable the DMA channel */ |
2416 | /* Enable the DMA channel */ |
2408 | HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize); |
2417 | dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize); |
2409 | 2418 | ||
- | 2419 | if (dmaxferstatus == HAL_OK) |
|
- | 2420 | { |
|
2410 | /* Enable Address Acknowledge */ |
2421 | /* Enable Address Acknowledge */ |
2411 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
2422 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2412 | 2423 | ||
2413 | /* Process Unlocked */ |
2424 | /* Process Unlocked */ |
2414 | __HAL_UNLOCK(hi2c); |
2425 | __HAL_UNLOCK(hi2c); |
2415 | 2426 | ||
2416 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2427 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2417 | to avoid the risk of I2C interrupt handle execution before current |
2428 | to avoid the risk of I2C interrupt handle execution before current |
2418 | process unlock */ |
2429 | process unlock */ |
2419 | /* Enable EVT and ERR interrupt */ |
2430 | /* Enable EVT and ERR interrupt */ |
2420 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
2431 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
2421 | 2432 | ||
2422 | /* Enable DMA Request */ |
2433 | /* Enable DMA Request */ |
2423 | hi2c->Instance->CR2 |= I2C_CR2_DMAEN; |
2434 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
2424 | 2435 | ||
2425 | return HAL_OK; |
2436 | return HAL_OK; |
- | 2437 | } |
|
- | 2438 | else |
|
- | 2439 | { |
|
- | 2440 | /* Update I2C state */ |
|
- | 2441 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 2442 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 2443 | ||
- | 2444 | /* Update I2C error code */ |
|
- | 2445 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
|
- | 2446 | ||
- | 2447 | /* Process Unlocked */ |
|
- | 2448 | __HAL_UNLOCK(hi2c); |
|
- | 2449 | ||
- | 2450 | return HAL_ERROR; |
|
- | 2451 | } |
|
2426 | } |
2452 | } |
2427 | else |
2453 | else |
2428 | { |
2454 | { |
2429 | return HAL_BUSY; |
2455 | return HAL_BUSY; |
2430 | } |
2456 | } |
2431 | } |
2457 | } |
- | 2458 | ||
2432 | /** |
2459 | /** |
2433 | * @brief Write an amount of data in blocking mode to a specific memory address |
2460 | * @brief Write an amount of data in blocking mode to a specific memory address |
2434 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
2461 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
2435 | * the configuration information for the specified I2C. |
2462 | * the configuration information for the specified I2C. |
2436 | * @param DevAddress Target device address: The device 7 bits address value |
2463 | * @param DevAddress Target device address: The device 7 bits address value |
Line 2442... | Line 2469... | ||
2442 | * @param Timeout Timeout duration |
2469 | * @param Timeout Timeout duration |
2443 | * @retval HAL status |
2470 | * @retval HAL status |
2444 | */ |
2471 | */ |
2445 | HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
2472 | HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
2446 | { |
2473 | { |
2447 | uint32_t tickstart = 0x00U; |
- | |
2448 | - | ||
2449 | /* Init tickstart for timeout management*/ |
2474 | /* Init tickstart for timeout management*/ |
2450 | tickstart = HAL_GetTick(); |
2475 | uint32_t tickstart = HAL_GetTick(); |
2451 | 2476 | ||
2452 | /* Check the parameters */ |
2477 | /* Check the parameters */ |
2453 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
2478 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
2454 | 2479 | ||
2455 | if(hi2c->State == HAL_I2C_STATE_READY) |
2480 | if (hi2c->State == HAL_I2C_STATE_READY) |
2456 | { |
2481 | { |
2457 | /* Wait until BUSY flag is reset */ |
2482 | /* Wait until BUSY flag is reset */ |
2458 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
2483 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
2459 | { |
2484 | { |
2460 | return HAL_BUSY; |
2485 | return HAL_BUSY; |
2461 | } |
2486 | } |
2462 | 2487 | ||
2463 | /* Process Locked */ |
2488 | /* Process Locked */ |
2464 | __HAL_LOCK(hi2c); |
2489 | __HAL_LOCK(hi2c); |
2465 | 2490 | ||
2466 | /* Check if the I2C is already enabled */ |
2491 | /* Check if the I2C is already enabled */ |
2467 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2492 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2468 | { |
2493 | { |
2469 | /* Enable I2C peripheral */ |
2494 | /* Enable I2C peripheral */ |
2470 | __HAL_I2C_ENABLE(hi2c); |
2495 | __HAL_I2C_ENABLE(hi2c); |
2471 | } |
2496 | } |
2472 | 2497 | ||
2473 | /* Disable Pos */ |
2498 | /* Disable Pos */ |
2474 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
2499 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
2475 | 2500 | ||
2476 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
2501 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
2477 | hi2c->Mode = HAL_I2C_MODE_MEM; |
2502 | hi2c->Mode = HAL_I2C_MODE_MEM; |
2478 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2503 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2479 | 2504 | ||
2480 | /* Prepare transfer parameters */ |
2505 | /* Prepare transfer parameters */ |
2481 | hi2c->pBuffPtr = pData; |
2506 | hi2c->pBuffPtr = pData; |
2482 | hi2c->XferCount = Size; |
2507 | hi2c->XferCount = Size; |
2483 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
2484 | hi2c->XferSize = hi2c->XferCount; |
2508 | hi2c->XferSize = hi2c->XferCount; |
- | 2509 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
2485 | 2510 | ||
2486 | /* Send Slave Address and Memory Address */ |
2511 | /* Send Slave Address and Memory Address */ |
2487 | if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK) |
2512 | if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK) |
2488 | { |
2513 | { |
2489 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
2490 | { |
- | |
2491 | /* Process Unlocked */ |
- | |
2492 | __HAL_UNLOCK(hi2c); |
- | |
2493 | return HAL_ERROR; |
2514 | return HAL_ERROR; |
2494 | } |
- | |
2495 | else |
- | |
2496 | { |
- | |
2497 | /* Process Unlocked */ |
- | |
2498 | __HAL_UNLOCK(hi2c); |
- | |
2499 | return HAL_TIMEOUT; |
- | |
2500 | } |
- | |
2501 | } |
2515 | } |
2502 | 2516 | ||
2503 | while(hi2c->XferSize > 0U) |
2517 | while (hi2c->XferSize > 0U) |
2504 | { |
2518 | { |
2505 | /* Wait until TXE flag is set */ |
2519 | /* Wait until TXE flag is set */ |
2506 | if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
2520 | if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
2507 | { |
2521 | { |
2508 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
2522 | if (hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
2509 | { |
2523 | { |
2510 | /* Generate Stop */ |
2524 | /* Generate Stop */ |
2511 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
2525 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
2512 | return HAL_ERROR; |
- | |
2513 | } |
- | |
2514 | else |
- | |
2515 | { |
- | |
2516 | return HAL_TIMEOUT; |
- | |
2517 | } |
2526 | } |
- | 2527 | return HAL_ERROR; |
|
2518 | } |
2528 | } |
2519 | 2529 | ||
2520 | /* Write data to DR */ |
2530 | /* Write data to DR */ |
2521 | hi2c->Instance->DR = (*hi2c->pBuffPtr++); |
2531 | hi2c->Instance->DR = *hi2c->pBuffPtr; |
- | 2532 | ||
- | 2533 | /* Increment Buffer pointer */ |
|
- | 2534 | hi2c->pBuffPtr++; |
|
- | 2535 | ||
- | 2536 | /* Update counter */ |
|
2522 | hi2c->XferSize--; |
2537 | hi2c->XferSize--; |
2523 | hi2c->XferCount--; |
2538 | hi2c->XferCount--; |
2524 | 2539 | ||
2525 | if((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U)) |
2540 | if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U)) |
2526 | { |
2541 | { |
2527 | /* Write data to DR */ |
2542 | /* Write data to DR */ |
2528 | hi2c->Instance->DR = (*hi2c->pBuffPtr++); |
2543 | hi2c->Instance->DR = *hi2c->pBuffPtr; |
- | 2544 | ||
- | 2545 | /* Increment Buffer pointer */ |
|
- | 2546 | hi2c->pBuffPtr++; |
|
- | 2547 | ||
- | 2548 | /* Update counter */ |
|
2529 | hi2c->XferSize--; |
2549 | hi2c->XferSize--; |
2530 | hi2c->XferCount--; |
2550 | hi2c->XferCount--; |
2531 | } |
2551 | } |
2532 | } |
2552 | } |
2533 | 2553 | ||
2534 | /* Wait until BTF flag is set */ |
2554 | /* Wait until BTF flag is set */ |
2535 | if(I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
2555 | if (I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
2536 | { |
2556 | { |
2537 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
2557 | if (hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
2538 | { |
2558 | { |
2539 | /* Generate Stop */ |
2559 | /* Generate Stop */ |
2540 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
2560 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
2541 | return HAL_ERROR; |
- | |
2542 | } |
- | |
2543 | else |
- | |
2544 | { |
- | |
2545 | return HAL_TIMEOUT; |
- | |
2546 | } |
2561 | } |
- | 2562 | return HAL_ERROR; |
|
2547 | } |
2563 | } |
2548 | 2564 | ||
2549 | /* Generate Stop */ |
2565 | /* Generate Stop */ |
2550 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
2566 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
2551 | 2567 | ||
2552 | hi2c->State = HAL_I2C_STATE_READY; |
2568 | hi2c->State = HAL_I2C_STATE_READY; |
2553 | hi2c->Mode = HAL_I2C_MODE_NONE; |
2569 | hi2c->Mode = HAL_I2C_MODE_NONE; |
2554 | 2570 | ||
2555 | /* Process Unlocked */ |
2571 | /* Process Unlocked */ |
2556 | __HAL_UNLOCK(hi2c); |
2572 | __HAL_UNLOCK(hi2c); |
2557 | 2573 | ||
2558 | return HAL_OK; |
2574 | return HAL_OK; |
2559 | } |
2575 | } |
Line 2576... | Line 2592... | ||
2576 | * @param Timeout Timeout duration |
2592 | * @param Timeout Timeout duration |
2577 | * @retval HAL status |
2593 | * @retval HAL status |
2578 | */ |
2594 | */ |
2579 | HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
2595 | HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout) |
2580 | { |
2596 | { |
2581 | uint32_t tickstart = 0x00U; |
2597 | __IO uint32_t count = 0U; |
2582 | 2598 | ||
2583 | /* Init tickstart for timeout management*/ |
2599 | /* Init tickstart for timeout management*/ |
2584 | tickstart = HAL_GetTick(); |
2600 | uint32_t tickstart = HAL_GetTick(); |
2585 | 2601 | ||
2586 | /* Check the parameters */ |
2602 | /* Check the parameters */ |
2587 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
2603 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
2588 | 2604 | ||
2589 | if(hi2c->State == HAL_I2C_STATE_READY) |
2605 | if (hi2c->State == HAL_I2C_STATE_READY) |
2590 | { |
2606 | { |
2591 | /* Wait until BUSY flag is reset */ |
2607 | /* Wait until BUSY flag is reset */ |
2592 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
2608 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
2593 | { |
2609 | { |
2594 | return HAL_BUSY; |
2610 | return HAL_BUSY; |
2595 | } |
2611 | } |
2596 | 2612 | ||
2597 | /* Process Locked */ |
2613 | /* Process Locked */ |
2598 | __HAL_LOCK(hi2c); |
2614 | __HAL_LOCK(hi2c); |
2599 | 2615 | ||
2600 | /* Check if the I2C is already enabled */ |
2616 | /* Check if the I2C is already enabled */ |
2601 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2617 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2602 | { |
2618 | { |
2603 | /* Enable I2C peripheral */ |
2619 | /* Enable I2C peripheral */ |
2604 | __HAL_I2C_ENABLE(hi2c); |
2620 | __HAL_I2C_ENABLE(hi2c); |
2605 | } |
2621 | } |
2606 | 2622 | ||
2607 | /* Disable Pos */ |
2623 | /* Disable Pos */ |
2608 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
2624 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
2609 | 2625 | ||
2610 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
2626 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
2611 | hi2c->Mode = HAL_I2C_MODE_MEM; |
2627 | hi2c->Mode = HAL_I2C_MODE_MEM; |
2612 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2628 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2613 | 2629 | ||
2614 | /* Prepare transfer parameters */ |
2630 | /* Prepare transfer parameters */ |
2615 | hi2c->pBuffPtr = pData; |
2631 | hi2c->pBuffPtr = pData; |
2616 | hi2c->XferCount = Size; |
2632 | hi2c->XferCount = Size; |
2617 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
2618 | hi2c->XferSize = hi2c->XferCount; |
2633 | hi2c->XferSize = hi2c->XferCount; |
- | 2634 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
2619 | 2635 | ||
2620 | /* Send Slave Address and Memory Address */ |
2636 | /* Send Slave Address and Memory Address */ |
2621 | if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK) |
2637 | if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK) |
2622 | { |
2638 | { |
2623 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
2624 | { |
- | |
2625 | /* Process Unlocked */ |
- | |
2626 | __HAL_UNLOCK(hi2c); |
- | |
2627 | return HAL_ERROR; |
2639 | return HAL_ERROR; |
2628 | } |
- | |
2629 | else |
- | |
2630 | { |
- | |
2631 | /* Process Unlocked */ |
- | |
2632 | __HAL_UNLOCK(hi2c); |
- | |
2633 | return HAL_TIMEOUT; |
- | |
2634 | } |
- | |
2635 | } |
2640 | } |
2636 | 2641 | ||
2637 | if(hi2c->XferSize == 0U) |
2642 | if (hi2c->XferSize == 0U) |
2638 | { |
2643 | { |
2639 | /* Clear ADDR flag */ |
2644 | /* Clear ADDR flag */ |
2640 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
2645 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
2641 | 2646 | ||
2642 | /* Generate Stop */ |
2647 | /* Generate Stop */ |
2643 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
2648 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
2644 | } |
2649 | } |
2645 | else if(hi2c->XferSize == 1U) |
2650 | else if (hi2c->XferSize == 1U) |
2646 | { |
2651 | { |
2647 | /* Disable Acknowledge */ |
2652 | /* Disable Acknowledge */ |
2648 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
2653 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2649 | 2654 | ||
2650 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
2655 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
2651 | software sequence must complete before the current byte end of transfer */ |
2656 | software sequence must complete before the current byte end of transfer */ |
2652 | __disable_irq(); |
2657 | __disable_irq(); |
2653 | 2658 | ||
2654 | /* Clear ADDR flag */ |
2659 | /* Clear ADDR flag */ |
2655 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
2660 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
2656 | 2661 | ||
2657 | /* Generate Stop */ |
2662 | /* Generate Stop */ |
2658 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
2663 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
2659 | 2664 | ||
2660 | /* Re-enable IRQs */ |
2665 | /* Re-enable IRQs */ |
2661 | __enable_irq(); |
2666 | __enable_irq(); |
2662 | } |
2667 | } |
2663 | else if(hi2c->XferSize == 2U) |
2668 | else if (hi2c->XferSize == 2U) |
2664 | { |
2669 | { |
2665 | /* Enable Pos */ |
2670 | /* Enable Pos */ |
2666 | hi2c->Instance->CR1 |= I2C_CR1_POS; |
2671 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
2667 | 2672 | ||
2668 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
2673 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
2669 | software sequence must complete before the current byte end of transfer */ |
2674 | software sequence must complete before the current byte end of transfer */ |
2670 | __disable_irq(); |
2675 | __disable_irq(); |
2671 | 2676 | ||
2672 | /* Clear ADDR flag */ |
2677 | /* Clear ADDR flag */ |
2673 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
2678 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
2674 | 2679 | ||
2675 | /* Disable Acknowledge */ |
2680 | /* Disable Acknowledge */ |
2676 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
2681 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2677 | 2682 | ||
2678 | /* Re-enable IRQs */ |
2683 | /* Re-enable IRQs */ |
2679 | __enable_irq(); |
2684 | __enable_irq(); |
2680 | } |
2685 | } |
2681 | else |
2686 | else |
2682 | { |
2687 | { |
2683 | /* Enable Acknowledge */ |
2688 | /* Enable Acknowledge */ |
2684 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2689 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2685 | - | ||
2686 | /* Clear ADDR flag */ |
2690 | /* Clear ADDR flag */ |
2687 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
2691 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
2688 | } |
2692 | } |
2689 | 2693 | ||
2690 | while(hi2c->XferSize > 0U) |
2694 | while (hi2c->XferSize > 0U) |
2691 | { |
2695 | { |
2692 | if(hi2c->XferSize <= 3U) |
2696 | if (hi2c->XferSize <= 3U) |
2693 | { |
2697 | { |
2694 | /* One byte */ |
2698 | /* One byte */ |
2695 | if(hi2c->XferSize== 1U) |
2699 | if (hi2c->XferSize == 1U) |
2696 | { |
2700 | { |
2697 | /* Wait until RXNE flag is set */ |
2701 | /* Wait until RXNE flag is set */ |
2698 | if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
2702 | if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
2699 | { |
2703 | { |
2700 | if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT) |
- | |
2701 | { |
- | |
2702 | return HAL_TIMEOUT; |
- | |
2703 | } |
- | |
2704 | else |
- | |
2705 | { |
- | |
2706 | return HAL_ERROR; |
2704 | return HAL_ERROR; |
2707 | } |
- | |
2708 | } |
2705 | } |
2709 | 2706 | ||
2710 | /* Read data from DR */ |
2707 | /* Read data from DR */ |
2711 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
2708 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 2709 | ||
- | 2710 | /* Increment Buffer pointer */ |
|
- | 2711 | hi2c->pBuffPtr++; |
|
- | 2712 | ||
- | 2713 | /* Update counter */ |
|
2712 | hi2c->XferSize--; |
2714 | hi2c->XferSize--; |
2713 | hi2c->XferCount--; |
2715 | hi2c->XferCount--; |
2714 | } |
2716 | } |
2715 | /* Two bytes */ |
2717 | /* Two bytes */ |
2716 | else if(hi2c->XferSize == 2U) |
2718 | else if (hi2c->XferSize == 2U) |
2717 | { |
2719 | { |
2718 | /* Wait until BTF flag is set */ |
2720 | /* Wait until BTF flag is set */ |
2719 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK) |
2721 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK) |
2720 | { |
2722 | { |
2721 | return HAL_TIMEOUT; |
2723 | return HAL_ERROR; |
2722 | } |
2724 | } |
2723 | 2725 | ||
2724 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
2726 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
2725 | software sequence must complete before the current byte end of transfer */ |
2727 | software sequence must complete before the current byte end of transfer */ |
2726 | __disable_irq(); |
2728 | __disable_irq(); |
2727 | 2729 | ||
2728 | /* Generate Stop */ |
2730 | /* Generate Stop */ |
2729 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
2731 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
2730 | 2732 | ||
2731 | /* Read data from DR */ |
2733 | /* Read data from DR */ |
2732 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
2734 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 2735 | ||
- | 2736 | /* Increment Buffer pointer */ |
|
- | 2737 | hi2c->pBuffPtr++; |
|
- | 2738 | ||
- | 2739 | /* Update counter */ |
|
2733 | hi2c->XferSize--; |
2740 | hi2c->XferSize--; |
2734 | hi2c->XferCount--; |
2741 | hi2c->XferCount--; |
2735 | 2742 | ||
2736 | /* Re-enable IRQs */ |
2743 | /* Re-enable IRQs */ |
2737 | __enable_irq(); |
2744 | __enable_irq(); |
2738 | 2745 | ||
2739 | /* Read data from DR */ |
2746 | /* Read data from DR */ |
2740 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
2747 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 2748 | ||
- | 2749 | /* Increment Buffer pointer */ |
|
- | 2750 | hi2c->pBuffPtr++; |
|
- | 2751 | ||
- | 2752 | /* Update counter */ |
|
2741 | hi2c->XferSize--; |
2753 | hi2c->XferSize--; |
2742 | hi2c->XferCount--; |
2754 | hi2c->XferCount--; |
2743 | } |
2755 | } |
2744 | /* 3 Last bytes */ |
2756 | /* 3 Last bytes */ |
2745 | else |
2757 | else |
2746 | { |
2758 | { |
2747 | /* Wait until BTF flag is set */ |
2759 | /* Wait until BTF flag is set */ |
2748 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK) |
2760 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK) |
2749 | { |
2761 | { |
2750 | return HAL_TIMEOUT; |
2762 | return HAL_ERROR; |
2751 | } |
2763 | } |
2752 | 2764 | ||
2753 | /* Disable Acknowledge */ |
2765 | /* Disable Acknowledge */ |
2754 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
2766 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2755 | 2767 | ||
2756 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
2768 | /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3 |
2757 | software sequence must complete before the current byte end of transfer */ |
2769 | software sequence must complete before the current byte end of transfer */ |
2758 | __disable_irq(); |
2770 | __disable_irq(); |
2759 | 2771 | ||
2760 | /* Read data from DR */ |
2772 | /* Read data from DR */ |
2761 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
2773 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 2774 | ||
- | 2775 | /* Increment Buffer pointer */ |
|
- | 2776 | hi2c->pBuffPtr++; |
|
- | 2777 | ||
- | 2778 | /* Update counter */ |
|
2762 | hi2c->XferSize--; |
2779 | hi2c->XferSize--; |
2763 | hi2c->XferCount--; |
2780 | hi2c->XferCount--; |
2764 | 2781 | ||
2765 | /* Wait until BTF flag is set */ |
2782 | /* Wait until BTF flag is set */ |
2766 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK) |
2783 | count = I2C_TIMEOUT_FLAG * (SystemCoreClock / 25U / 1000U); |
- | 2784 | do |
|
2767 | { |
2785 | { |
- | 2786 | count--; |
|
- | 2787 | if (count == 0U) |
|
- | 2788 | { |
|
- | 2789 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 2790 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 2791 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 2792 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
- | 2793 | ||
- | 2794 | /* Re-enable IRQs */ |
|
- | 2795 | __enable_irq(); |
|
- | 2796 | ||
- | 2797 | /* Process Unlocked */ |
|
- | 2798 | __HAL_UNLOCK(hi2c); |
|
- | 2799 | ||
2768 | return HAL_TIMEOUT; |
2800 | return HAL_ERROR; |
- | 2801 | } |
|
2769 | } |
2802 | } |
- | 2803 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET); |
|
2770 | 2804 | ||
2771 | /* Generate Stop */ |
2805 | /* Generate Stop */ |
2772 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
2806 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
2773 | 2807 | ||
2774 | /* Read data from DR */ |
2808 | /* Read data from DR */ |
2775 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
2809 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 2810 | ||
- | 2811 | /* Increment Buffer pointer */ |
|
- | 2812 | hi2c->pBuffPtr++; |
|
- | 2813 | ||
- | 2814 | /* Update counter */ |
|
2776 | hi2c->XferSize--; |
2815 | hi2c->XferSize--; |
2777 | hi2c->XferCount--; |
2816 | hi2c->XferCount--; |
2778 | 2817 | ||
2779 | /* Re-enable IRQs */ |
2818 | /* Re-enable IRQs */ |
2780 | __enable_irq(); |
2819 | __enable_irq(); |
2781 | 2820 | ||
2782 | /* Read data from DR */ |
2821 | /* Read data from DR */ |
2783 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
2822 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 2823 | ||
- | 2824 | /* Increment Buffer pointer */ |
|
- | 2825 | hi2c->pBuffPtr++; |
|
- | 2826 | ||
- | 2827 | /* Update counter */ |
|
2784 | hi2c->XferSize--; |
2828 | hi2c->XferSize--; |
2785 | hi2c->XferCount--; |
2829 | hi2c->XferCount--; |
2786 | } |
2830 | } |
2787 | } |
2831 | } |
2788 | else |
2832 | else |
2789 | { |
2833 | { |
2790 | /* Wait until RXNE flag is set */ |
2834 | /* Wait until RXNE flag is set */ |
2791 | if(I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
2835 | if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK) |
2792 | { |
2836 | { |
2793 | if(hi2c->ErrorCode == HAL_I2C_ERROR_TIMEOUT) |
- | |
2794 | { |
- | |
2795 | return HAL_TIMEOUT; |
- | |
2796 | } |
- | |
2797 | else |
- | |
2798 | { |
- | |
2799 | return HAL_ERROR; |
2837 | return HAL_ERROR; |
2800 | } |
- | |
2801 | } |
2838 | } |
2802 | 2839 | ||
2803 | /* Read data from DR */ |
2840 | /* Read data from DR */ |
2804 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
2841 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 2842 | ||
- | 2843 | /* Increment Buffer pointer */ |
|
- | 2844 | hi2c->pBuffPtr++; |
|
- | 2845 | ||
- | 2846 | /* Update counter */ |
|
2805 | hi2c->XferSize--; |
2847 | hi2c->XferSize--; |
2806 | hi2c->XferCount--; |
2848 | hi2c->XferCount--; |
2807 | 2849 | ||
2808 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) |
2850 | if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) |
2809 | { |
2851 | { |
2810 | /* Read data from DR */ |
2852 | /* Read data from DR */ |
2811 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
2853 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 2854 | ||
- | 2855 | /* Increment Buffer pointer */ |
|
- | 2856 | hi2c->pBuffPtr++; |
|
- | 2857 | ||
- | 2858 | /* Update counter */ |
|
2812 | hi2c->XferSize--; |
2859 | hi2c->XferSize--; |
2813 | hi2c->XferCount--; |
2860 | hi2c->XferCount--; |
2814 | } |
2861 | } |
2815 | } |
2862 | } |
2816 | } |
2863 | } |
2817 | 2864 | ||
2818 | hi2c->State = HAL_I2C_STATE_READY; |
2865 | hi2c->State = HAL_I2C_STATE_READY; |
2819 | hi2c->Mode = HAL_I2C_MODE_NONE; |
2866 | hi2c->Mode = HAL_I2C_MODE_NONE; |
2820 | 2867 | ||
2821 | /* Process Unlocked */ |
2868 | /* Process Unlocked */ |
2822 | __HAL_UNLOCK(hi2c); |
2869 | __HAL_UNLOCK(hi2c); |
2823 | 2870 | ||
2824 | return HAL_OK; |
2871 | return HAL_OK; |
2825 | } |
2872 | } |
Line 2846... | Line 2893... | ||
2846 | __IO uint32_t count = 0U; |
2893 | __IO uint32_t count = 0U; |
2847 | 2894 | ||
2848 | /* Check the parameters */ |
2895 | /* Check the parameters */ |
2849 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
2896 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
2850 | 2897 | ||
2851 | if(hi2c->State == HAL_I2C_STATE_READY) |
2898 | if (hi2c->State == HAL_I2C_STATE_READY) |
2852 | { |
2899 | { |
2853 | /* Wait until BUSY flag is reset */ |
2900 | /* Wait until BUSY flag is reset */ |
2854 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
2901 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
2855 | do |
2902 | do |
2856 | { |
2903 | { |
- | 2904 | count--; |
|
2857 | if(count-- == 0U) |
2905 | if (count == 0U) |
2858 | { |
2906 | { |
2859 | hi2c->PreviousState = I2C_STATE_NONE; |
2907 | hi2c->PreviousState = I2C_STATE_NONE; |
2860 | hi2c->State= HAL_I2C_STATE_READY; |
2908 | hi2c->State = HAL_I2C_STATE_READY; |
- | 2909 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 2910 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
2861 | 2911 | ||
2862 | /* Process Unlocked */ |
2912 | /* Process Unlocked */ |
2863 | __HAL_UNLOCK(hi2c); |
2913 | __HAL_UNLOCK(hi2c); |
2864 | 2914 | ||
2865 | return HAL_TIMEOUT; |
2915 | return HAL_ERROR; |
2866 | } |
2916 | } |
2867 | } |
2917 | } |
2868 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
2918 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
2869 | 2919 | ||
2870 | /* Process Locked */ |
2920 | /* Process Locked */ |
2871 | __HAL_LOCK(hi2c); |
2921 | __HAL_LOCK(hi2c); |
2872 | 2922 | ||
2873 | /* Check if the I2C is already enabled */ |
2923 | /* Check if the I2C is already enabled */ |
2874 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2924 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2875 | { |
2925 | { |
2876 | /* Enable I2C peripheral */ |
2926 | /* Enable I2C peripheral */ |
2877 | __HAL_I2C_ENABLE(hi2c); |
2927 | __HAL_I2C_ENABLE(hi2c); |
2878 | } |
2928 | } |
2879 | 2929 | ||
2880 | /* Disable Pos */ |
2930 | /* Disable Pos */ |
2881 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
2931 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
2882 | 2932 | ||
2883 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
2933 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
2884 | hi2c->Mode = HAL_I2C_MODE_MEM; |
2934 | hi2c->Mode = HAL_I2C_MODE_MEM; |
2885 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2935 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2886 | 2936 | ||
2887 | /* Prepare transfer parameters */ |
2937 | /* Prepare transfer parameters */ |
2888 | hi2c->pBuffPtr = pData; |
2938 | hi2c->pBuffPtr = pData; |
2889 | hi2c->XferSize = Size; |
2939 | hi2c->XferCount = Size; |
2890 | hi2c->XferCount = Size; |
2940 | hi2c->XferSize = hi2c->XferCount; |
2891 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
2941 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
2892 | hi2c->Devaddress = DevAddress; |
2942 | hi2c->Devaddress = DevAddress; |
2893 | hi2c->Memaddress = MemAddress; |
2943 | hi2c->Memaddress = MemAddress; |
2894 | hi2c->MemaddSize = MemAddSize; |
2944 | hi2c->MemaddSize = MemAddSize; |
2895 | hi2c->EventCount = 0U; |
2945 | hi2c->EventCount = 0U; |
2896 | 2946 | ||
2897 | /* Generate Start */ |
2947 | /* Generate Start */ |
2898 | hi2c->Instance->CR1 |= I2C_CR1_START; |
2948 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
2899 | 2949 | ||
2900 | /* Process Unlocked */ |
2950 | /* Process Unlocked */ |
2901 | __HAL_UNLOCK(hi2c); |
2951 | __HAL_UNLOCK(hi2c); |
2902 | 2952 | ||
2903 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2953 | /* Note : The I2C interrupts must be enabled after unlocking current process |
Line 2917... | Line 2967... | ||
2917 | 2967 | ||
2918 | /** |
2968 | /** |
2919 | * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address |
2969 | * @brief Read an amount of data in non-blocking mode with Interrupt from a specific memory address |
2920 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
2970 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
2921 | * the configuration information for the specified I2C. |
2971 | * the configuration information for the specified I2C. |
2922 | * @param DevAddress Target device address: The device 7 bits address value |
2972 | * @param DevAddress Target device address |
2923 | * in datasheet must be shifted to the left before calling the interface |
- | |
2924 | * @param MemAddress Internal memory address |
2973 | * @param MemAddress Internal memory address |
2925 | * @param MemAddSize Size of internal memory address |
2974 | * @param MemAddSize Size of internal memory address |
2926 | * @param pData Pointer to data buffer |
2975 | * @param pData Pointer to data buffer |
2927 | * @param Size Amount of data to be sent |
2976 | * @param Size Amount of data to be sent |
2928 | * @retval HAL status |
2977 | * @retval HAL status |
Line 2932... | Line 2981... | ||
2932 | __IO uint32_t count = 0U; |
2981 | __IO uint32_t count = 0U; |
2933 | 2982 | ||
2934 | /* Check the parameters */ |
2983 | /* Check the parameters */ |
2935 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
2984 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
2936 | 2985 | ||
2937 | if(hi2c->State == HAL_I2C_STATE_READY) |
2986 | if (hi2c->State == HAL_I2C_STATE_READY) |
2938 | { |
2987 | { |
2939 | /* Wait until BUSY flag is reset */ |
2988 | /* Wait until BUSY flag is reset */ |
2940 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
2989 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
2941 | do |
2990 | do |
2942 | { |
2991 | { |
- | 2992 | count--; |
|
2943 | if(count-- == 0U) |
2993 | if (count == 0U) |
2944 | { |
2994 | { |
2945 | hi2c->PreviousState = I2C_STATE_NONE; |
2995 | hi2c->PreviousState = I2C_STATE_NONE; |
2946 | hi2c->State= HAL_I2C_STATE_READY; |
2996 | hi2c->State = HAL_I2C_STATE_READY; |
- | 2997 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 2998 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
2947 | 2999 | ||
2948 | /* Process Unlocked */ |
3000 | /* Process Unlocked */ |
2949 | __HAL_UNLOCK(hi2c); |
3001 | __HAL_UNLOCK(hi2c); |
2950 | 3002 | ||
2951 | return HAL_TIMEOUT; |
3003 | return HAL_ERROR; |
2952 | } |
3004 | } |
2953 | } |
3005 | } |
2954 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
3006 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
2955 | 3007 | ||
2956 | /* Process Locked */ |
3008 | /* Process Locked */ |
2957 | __HAL_LOCK(hi2c); |
3009 | __HAL_LOCK(hi2c); |
2958 | 3010 | ||
2959 | /* Check if the I2C is already enabled */ |
3011 | /* Check if the I2C is already enabled */ |
2960 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
3012 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
2961 | { |
3013 | { |
2962 | /* Enable I2C peripheral */ |
3014 | /* Enable I2C peripheral */ |
2963 | __HAL_I2C_ENABLE(hi2c); |
3015 | __HAL_I2C_ENABLE(hi2c); |
2964 | } |
3016 | } |
2965 | 3017 | ||
2966 | /* Disable Pos */ |
3018 | /* Disable Pos */ |
2967 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
3019 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
2968 | 3020 | ||
2969 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
3021 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
2970 | hi2c->Mode = HAL_I2C_MODE_MEM; |
3022 | hi2c->Mode = HAL_I2C_MODE_MEM; |
2971 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
3023 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
2972 | 3024 | ||
2973 | /* Prepare transfer parameters */ |
3025 | /* Prepare transfer parameters */ |
2974 | hi2c->pBuffPtr = pData; |
3026 | hi2c->pBuffPtr = pData; |
2975 | hi2c->XferSize = Size; |
3027 | hi2c->XferCount = Size; |
2976 | hi2c->XferCount = Size; |
3028 | hi2c->XferSize = hi2c->XferCount; |
2977 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
3029 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
2978 | hi2c->Devaddress = DevAddress; |
3030 | hi2c->Devaddress = DevAddress; |
2979 | hi2c->Memaddress = MemAddress; |
3031 | hi2c->Memaddress = MemAddress; |
2980 | hi2c->MemaddSize = MemAddSize; |
3032 | hi2c->MemaddSize = MemAddSize; |
2981 | hi2c->EventCount = 0U; |
3033 | hi2c->EventCount = 0U; |
2982 | 3034 | ||
2983 | /* Enable Acknowledge */ |
3035 | /* Enable Acknowledge */ |
2984 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
3036 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
2985 | 3037 | ||
2986 | /* Generate Start */ |
3038 | /* Generate Start */ |
2987 | hi2c->Instance->CR1 |= I2C_CR1_START; |
3039 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
2988 | 3040 | ||
2989 | /* Process Unlocked */ |
3041 | /* Process Unlocked */ |
2990 | __HAL_UNLOCK(hi2c); |
3042 | __HAL_UNLOCK(hi2c); |
2991 | 3043 | ||
2992 | if(hi2c->XferSize > 0U) |
3044 | if (hi2c->XferSize > 0U) |
2993 | { |
3045 | { |
2994 | /* Note : The I2C interrupts must be enabled after unlocking current process |
3046 | /* Note : The I2C interrupts must be enabled after unlocking current process |
2995 | to avoid the risk of I2C interrupt handle execution before current |
3047 | to avoid the risk of I2C interrupt handle execution before current |
2996 | process unlock */ |
3048 | process unlock */ |
2997 | 3049 | ||
2998 | /* Enable EVT, BUF and ERR interrupt */ |
3050 | /* Enable EVT, BUF and ERR interrupt */ |
2999 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
3051 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
3000 | } |
3052 | } |
3001 | return HAL_OK; |
3053 | return HAL_OK; |
3002 | } |
3054 | } |
Line 3019... | Line 3071... | ||
3019 | * @retval HAL status |
3071 | * @retval HAL status |
3020 | */ |
3072 | */ |
3021 | HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size) |
3073 | HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size) |
3022 | { |
3074 | { |
3023 | __IO uint32_t count = 0U; |
3075 | __IO uint32_t count = 0U; |
3024 | - | ||
3025 | uint32_t tickstart = 0x00U; |
3076 | HAL_StatusTypeDef dmaxferstatus; |
3026 | 3077 | ||
3027 | /* Init tickstart for timeout management*/ |
3078 | /* Init tickstart for timeout management*/ |
3028 | tickstart = HAL_GetTick(); |
3079 | uint32_t tickstart = HAL_GetTick(); |
3029 | 3080 | ||
3030 | /* Check the parameters */ |
3081 | /* Check the parameters */ |
3031 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
3082 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
3032 | 3083 | ||
3033 | if(hi2c->State == HAL_I2C_STATE_READY) |
3084 | if (hi2c->State == HAL_I2C_STATE_READY) |
3034 | { |
3085 | { |
3035 | /* Wait until BUSY flag is reset */ |
3086 | /* Wait until BUSY flag is reset */ |
3036 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
3087 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
3037 | do |
3088 | do |
3038 | { |
3089 | { |
- | 3090 | count--; |
|
3039 | if(count-- == 0U) |
3091 | if (count == 0U) |
3040 | { |
3092 | { |
3041 | hi2c->PreviousState = I2C_STATE_NONE; |
3093 | hi2c->PreviousState = I2C_STATE_NONE; |
3042 | hi2c->State= HAL_I2C_STATE_READY; |
3094 | hi2c->State = HAL_I2C_STATE_READY; |
- | 3095 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 3096 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
3043 | 3097 | ||
3044 | /* Process Unlocked */ |
3098 | /* Process Unlocked */ |
3045 | __HAL_UNLOCK(hi2c); |
3099 | __HAL_UNLOCK(hi2c); |
3046 | 3100 | ||
3047 | return HAL_TIMEOUT; |
3101 | return HAL_ERROR; |
3048 | } |
3102 | } |
3049 | } |
3103 | } |
3050 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
3104 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
3051 | 3105 | ||
3052 | /* Process Locked */ |
3106 | /* Process Locked */ |
3053 | __HAL_LOCK(hi2c); |
3107 | __HAL_LOCK(hi2c); |
3054 | 3108 | ||
3055 | /* Check if the I2C is already enabled */ |
3109 | /* Check if the I2C is already enabled */ |
3056 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
3110 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
3057 | { |
3111 | { |
3058 | /* Enable I2C peripheral */ |
3112 | /* Enable I2C peripheral */ |
3059 | __HAL_I2C_ENABLE(hi2c); |
3113 | __HAL_I2C_ENABLE(hi2c); |
3060 | } |
3114 | } |
3061 | 3115 | ||
3062 | /* Disable Pos */ |
3116 | /* Disable Pos */ |
3063 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
3117 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
3064 | 3118 | ||
3065 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
3119 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
3066 | hi2c->Mode = HAL_I2C_MODE_MEM; |
3120 | hi2c->Mode = HAL_I2C_MODE_MEM; |
3067 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
3121 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
3068 | 3122 | ||
3069 | /* Prepare transfer parameters */ |
3123 | /* Prepare transfer parameters */ |
3070 | hi2c->pBuffPtr = pData; |
3124 | hi2c->pBuffPtr = pData; |
3071 | hi2c->XferSize = Size; |
3125 | hi2c->XferCount = Size; |
3072 | hi2c->XferCount = Size; |
3126 | hi2c->XferSize = hi2c->XferCount; |
3073 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
3127 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
3074 | 3128 | ||
3075 | if(hi2c->XferSize > 0U) |
3129 | if (hi2c->XferSize > 0U) |
3076 | { |
3130 | { |
3077 | /* Set the I2C DMA transfer complete callback */ |
3131 | /* Set the I2C DMA transfer complete callback */ |
3078 | hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt; |
3132 | hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt; |
3079 | 3133 | ||
3080 | /* Set the DMA error callback */ |
3134 | /* Set the DMA error callback */ |
3081 | hi2c->hdmatx->XferErrorCallback = I2C_DMAError; |
3135 | hi2c->hdmatx->XferErrorCallback = I2C_DMAError; |
Line 3083... | Line 3137... | ||
3083 | /* Set the unused DMA callbacks to NULL */ |
3137 | /* Set the unused DMA callbacks to NULL */ |
3084 | hi2c->hdmatx->XferHalfCpltCallback = NULL; |
3138 | hi2c->hdmatx->XferHalfCpltCallback = NULL; |
3085 | hi2c->hdmatx->XferAbortCallback = NULL; |
3139 | hi2c->hdmatx->XferAbortCallback = NULL; |
3086 | 3140 | ||
3087 | /* Enable the DMA channel */ |
3141 | /* Enable the DMA channel */ |
3088 | HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize); |
3142 | dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize); |
3089 | 3143 | ||
3090 | /* Send Slave Address and Memory Address */ |
3144 | if (dmaxferstatus == HAL_OK) |
3091 | if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK) |
- | |
3092 | { |
3145 | { |
3093 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
3146 | /* Send Slave Address and Memory Address */ |
- | 3147 | if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK) |
|
3094 | { |
3148 | { |
- | 3149 | /* Abort the ongoing DMA */ |
|
- | 3150 | dmaxferstatus = HAL_DMA_Abort_IT(hi2c->hdmatx); |
|
- | 3151 | ||
- | 3152 | /* Prevent unused argument(s) compilation and MISRA warning */ |
|
- | 3153 | UNUSED(dmaxferstatus); |
|
- | 3154 | ||
- | 3155 | /* Clear directly Complete callback as no XferAbortCallback is used to finalize Abort treatment */ |
|
- | 3156 | if (hi2c->hdmatx != NULL) |
|
- | 3157 | { |
|
- | 3158 | hi2c->hdmatx->XferCpltCallback = NULL; |
|
- | 3159 | } |
|
- | 3160 | ||
3095 | /* Process Unlocked */ |
3161 | /* Disable Acknowledge */ |
- | 3162 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 3163 | ||
- | 3164 | hi2c->XferSize = 0U; |
|
- | 3165 | hi2c->XferCount = 0U; |
|
- | 3166 | ||
- | 3167 | /* Disable I2C peripheral to prevent dummy data in buffer */ |
|
3096 | __HAL_UNLOCK(hi2c); |
3168 | __HAL_I2C_DISABLE(hi2c); |
- | 3169 | ||
3097 | return HAL_ERROR; |
3170 | return HAL_ERROR; |
3098 | } |
3171 | } |
- | 3172 | ||
3099 | else |
3173 | /* Clear ADDR flag */ |
3100 | { |
3174 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
- | 3175 | ||
3101 | /* Process Unlocked */ |
3176 | /* Process Unlocked */ |
3102 | __HAL_UNLOCK(hi2c); |
3177 | __HAL_UNLOCK(hi2c); |
- | 3178 | ||
- | 3179 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 3180 | to avoid the risk of I2C interrupt handle execution before current |
|
- | 3181 | process unlock */ |
|
- | 3182 | /* Enable ERR interrupt */ |
|
- | 3183 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR); |
|
- | 3184 | ||
3103 | return HAL_TIMEOUT; |
3185 | /* Enable DMA Request */ |
- | 3186 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 3187 | ||
3104 | } |
3188 | return HAL_OK; |
3105 | } |
3189 | } |
- | 3190 | else |
|
- | 3191 | { |
|
- | 3192 | /* Update I2C state */ |
|
- | 3193 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 3194 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 3195 | ||
- | 3196 | /* Update I2C error code */ |
|
- | 3197 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
|
- | 3198 | ||
- | 3199 | /* Process Unlocked */ |
|
- | 3200 | __HAL_UNLOCK(hi2c); |
|
- | 3201 | ||
- | 3202 | return HAL_ERROR; |
|
- | 3203 | } |
|
- | 3204 | } |
|
- | 3205 | else |
|
- | 3206 | { |
|
- | 3207 | /* Update I2C state */ |
|
- | 3208 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 3209 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 3210 | ||
- | 3211 | /* Update I2C error code */ |
|
- | 3212 | hi2c->ErrorCode |= HAL_I2C_ERROR_SIZE; |
|
3106 | 3213 | ||
3107 | /* Clear ADDR flag */ |
- | |
3108 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
- | |
3109 | - | ||
3110 | /* Process Unlocked */ |
3214 | /* Process Unlocked */ |
3111 | __HAL_UNLOCK(hi2c); |
3215 | __HAL_UNLOCK(hi2c); |
3112 | - | ||
3113 | /* Note : The I2C interrupts must be enabled after unlocking current process |
- | |
3114 | to avoid the risk of I2C interrupt handle execution before current |
- | |
3115 | process unlock */ |
- | |
3116 | /* Enable ERR interrupt */ |
- | |
3117 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR); |
- | |
3118 | 3216 | ||
3119 | /* Enable DMA Request */ |
3217 | return HAL_ERROR; |
3120 | hi2c->Instance->CR2 |= I2C_CR2_DMAEN; |
- | |
3121 | } |
3218 | } |
3122 | return HAL_OK; |
- | |
3123 | } |
3219 | } |
3124 | else |
3220 | else |
3125 | { |
3221 | { |
3126 | return HAL_BUSY; |
3222 | return HAL_BUSY; |
3127 | } |
3223 | } |
Line 3139... | Line 3235... | ||
3139 | * @param Size Amount of data to be read |
3235 | * @param Size Amount of data to be read |
3140 | * @retval HAL status |
3236 | * @retval HAL status |
3141 | */ |
3237 | */ |
3142 | HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size) |
3238 | HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size) |
3143 | { |
3239 | { |
3144 | uint32_t tickstart = 0x00U; |
- | |
3145 | __IO uint32_t count = 0U; |
- | |
3146 | - | ||
3147 | /* Init tickstart for timeout management*/ |
3240 | /* Init tickstart for timeout management*/ |
3148 | tickstart = HAL_GetTick(); |
3241 | uint32_t tickstart = HAL_GetTick(); |
- | 3242 | __IO uint32_t count = 0U; |
|
- | 3243 | HAL_StatusTypeDef dmaxferstatus; |
|
3149 | 3244 | ||
3150 | /* Check the parameters */ |
3245 | /* Check the parameters */ |
3151 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
3246 | assert_param(IS_I2C_MEMADD_SIZE(MemAddSize)); |
3152 | 3247 | ||
3153 | if(hi2c->State == HAL_I2C_STATE_READY) |
3248 | if (hi2c->State == HAL_I2C_STATE_READY) |
3154 | { |
3249 | { |
3155 | /* Wait until BUSY flag is reset */ |
3250 | /* Wait until BUSY flag is reset */ |
3156 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock /25U /1000U); |
3251 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
3157 | do |
3252 | do |
3158 | { |
3253 | { |
- | 3254 | count--; |
|
3159 | if(count-- == 0U) |
3255 | if (count == 0U) |
3160 | { |
3256 | { |
3161 | hi2c->PreviousState = I2C_STATE_NONE; |
3257 | hi2c->PreviousState = I2C_STATE_NONE; |
3162 | hi2c->State= HAL_I2C_STATE_READY; |
3258 | hi2c->State = HAL_I2C_STATE_READY; |
- | 3259 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 3260 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
3163 | 3261 | ||
3164 | /* Process Unlocked */ |
3262 | /* Process Unlocked */ |
3165 | __HAL_UNLOCK(hi2c); |
3263 | __HAL_UNLOCK(hi2c); |
3166 | 3264 | ||
3167 | return HAL_TIMEOUT; |
3265 | return HAL_ERROR; |
3168 | } |
3266 | } |
3169 | } |
3267 | } |
3170 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
3268 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
3171 | 3269 | ||
3172 | /* Process Locked */ |
3270 | /* Process Locked */ |
3173 | __HAL_LOCK(hi2c); |
3271 | __HAL_LOCK(hi2c); |
3174 | 3272 | ||
3175 | /* Check if the I2C is already enabled */ |
3273 | /* Check if the I2C is already enabled */ |
3176 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
3274 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
3177 | { |
3275 | { |
3178 | /* Enable I2C peripheral */ |
3276 | /* Enable I2C peripheral */ |
3179 | __HAL_I2C_ENABLE(hi2c); |
3277 | __HAL_I2C_ENABLE(hi2c); |
3180 | } |
3278 | } |
3181 | 3279 | ||
3182 | /* Disable Pos */ |
3280 | /* Disable Pos */ |
3183 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
3281 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
3184 | 3282 | ||
3185 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
3283 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
3186 | hi2c->Mode = HAL_I2C_MODE_MEM; |
3284 | hi2c->Mode = HAL_I2C_MODE_MEM; |
3187 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
3285 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
3188 | 3286 | ||
3189 | /* Prepare transfer parameters */ |
3287 | /* Prepare transfer parameters */ |
3190 | hi2c->pBuffPtr = pData; |
3288 | hi2c->pBuffPtr = pData; |
3191 | hi2c->XferCount = Size; |
3289 | hi2c->XferCount = Size; |
3192 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
- | |
3193 | hi2c->XferSize = hi2c->XferCount; |
3290 | hi2c->XferSize = hi2c->XferCount; |
- | 3291 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
|
3194 | 3292 | ||
3195 | if(hi2c->XferSize > 0U) |
3293 | if (hi2c->XferSize > 0U) |
3196 | { |
3294 | { |
3197 | /* Set the I2C DMA transfer complete callback */ |
3295 | /* Set the I2C DMA transfer complete callback */ |
3198 | hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt; |
3296 | hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt; |
3199 | 3297 | ||
3200 | /* Set the DMA error callback */ |
3298 | /* Set the DMA error callback */ |
3201 | hi2c->hdmarx->XferErrorCallback = I2C_DMAError; |
3299 | hi2c->hdmarx->XferErrorCallback = I2C_DMAError; |
3202 | 3300 | ||
3203 | /* Set the unused DMA callbacks to NULL */ |
3301 | /* Set the unused DMA callbacks to NULL */ |
- | 3302 | hi2c->hdmarx->XferHalfCpltCallback = NULL; |
|
3204 | hi2c->hdmarx->XferAbortCallback = NULL; |
3303 | hi2c->hdmarx->XferAbortCallback = NULL; |
3205 | 3304 | ||
3206 | /* Enable the DMA channel */ |
3305 | /* Enable the DMA channel */ |
3207 | HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize); |
3306 | dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize); |
3208 | 3307 | ||
3209 | /* Send Slave Address and Memory Address */ |
3308 | if (dmaxferstatus == HAL_OK) |
3210 | if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK) |
- | |
3211 | { |
3309 | { |
3212 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
3310 | /* Send Slave Address and Memory Address */ |
- | 3311 | if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK) |
|
3213 | { |
3312 | { |
- | 3313 | /* Abort the ongoing DMA */ |
|
- | 3314 | dmaxferstatus = HAL_DMA_Abort_IT(hi2c->hdmarx); |
|
- | 3315 | ||
- | 3316 | /* Prevent unused argument(s) compilation and MISRA warning */ |
|
- | 3317 | UNUSED(dmaxferstatus); |
|
- | 3318 | ||
- | 3319 | /* Clear directly Complete callback as no XferAbortCallback is used to finalize Abort treatment */ |
|
- | 3320 | if (hi2c->hdmarx != NULL) |
|
- | 3321 | { |
|
- | 3322 | hi2c->hdmarx->XferCpltCallback = NULL; |
|
- | 3323 | } |
|
- | 3324 | ||
3214 | /* Process Unlocked */ |
3325 | /* Disable Acknowledge */ |
- | 3326 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 3327 | ||
- | 3328 | hi2c->XferSize = 0U; |
|
- | 3329 | hi2c->XferCount = 0U; |
|
- | 3330 | ||
- | 3331 | /* Disable I2C peripheral to prevent dummy data in buffer */ |
|
3215 | __HAL_UNLOCK(hi2c); |
3332 | __HAL_I2C_DISABLE(hi2c); |
- | 3333 | ||
3216 | return HAL_ERROR; |
3334 | return HAL_ERROR; |
3217 | } |
3335 | } |
- | 3336 | ||
- | 3337 | if (hi2c->XferSize == 1U) |
|
- | 3338 | { |
|
- | 3339 | /* Disable Acknowledge */ |
|
- | 3340 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 3341 | } |
|
3218 | else |
3342 | else |
3219 | { |
3343 | { |
3220 | /* Process Unlocked */ |
3344 | /* Enable Last DMA bit */ |
3221 | __HAL_UNLOCK(hi2c); |
- | |
3222 | return HAL_TIMEOUT; |
3345 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST); |
3223 | } |
3346 | } |
3224 | } |
- | |
3225 | 3347 | ||
3226 | if(Size == 1U) |
3348 | /* Clear ADDR flag */ |
- | 3349 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
|
- | 3350 | ||
- | 3351 | /* Process Unlocked */ |
|
- | 3352 | __HAL_UNLOCK(hi2c); |
|
- | 3353 | ||
- | 3354 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 3355 | to avoid the risk of I2C interrupt handle execution before current |
|
3227 | { |
3356 | process unlock */ |
- | 3357 | /* Enable ERR interrupt */ |
|
- | 3358 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR); |
|
- | 3359 | ||
3228 | /* Disable Acknowledge */ |
3360 | /* Enable DMA Request */ |
3229 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
3361 | hi2c->Instance->CR2 |= I2C_CR2_DMAEN; |
3230 | } |
3362 | } |
3231 | else |
3363 | else |
3232 | { |
3364 | { |
3233 | /* Enable Last DMA bit */ |
3365 | /* Update I2C state */ |
3234 | hi2c->Instance->CR2 |= I2C_CR2_LAST; |
3366 | hi2c->State = HAL_I2C_STATE_READY; |
3235 | } |
3367 | hi2c->Mode = HAL_I2C_MODE_NONE; |
3236 | 3368 | ||
3237 | /* Clear ADDR flag */ |
3369 | /* Update I2C error code */ |
3238 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
3370 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
3239 | 3371 | ||
3240 | /* Process Unlocked */ |
3372 | /* Process Unlocked */ |
3241 | __HAL_UNLOCK(hi2c); |
3373 | __HAL_UNLOCK(hi2c); |
3242 | 3374 | ||
3243 | /* Note : The I2C interrupts must be enabled after unlocking current process |
- | |
3244 | to avoid the risk of I2C interrupt handle execution before current |
- | |
3245 | process unlock */ |
- | |
3246 | /* Enable ERR interrupt */ |
3375 | return HAL_ERROR; |
3247 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_ERR); |
- | |
3248 | 3376 | } |
|
3249 | /* Enable DMA Request */ |
- | |
3250 | hi2c->Instance->CR2 |= I2C_CR2_DMAEN; |
- | |
3251 | } |
3377 | } |
3252 | else |
3378 | else |
3253 | { |
3379 | { |
3254 | /* Send Slave Address and Memory Address */ |
3380 | /* Send Slave Address and Memory Address */ |
3255 | if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK) |
3381 | if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK) |
3256 | { |
3382 | { |
3257 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
3258 | { |
- | |
3259 | /* Process Unlocked */ |
- | |
3260 | __HAL_UNLOCK(hi2c); |
- | |
3261 | return HAL_ERROR; |
3383 | return HAL_ERROR; |
3262 | } |
- | |
3263 | else |
- | |
3264 | { |
- | |
3265 | /* Process Unlocked */ |
- | |
3266 | __HAL_UNLOCK(hi2c); |
- | |
3267 | return HAL_TIMEOUT; |
- | |
3268 | } |
- | |
3269 | } |
3384 | } |
3270 | 3385 | ||
3271 | /* Clear ADDR flag */ |
3386 | /* Clear ADDR flag */ |
3272 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
3387 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
3273 | 3388 | ||
3274 | /* Generate Stop */ |
3389 | /* Generate Stop */ |
3275 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
3390 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
3276 | 3391 | ||
3277 | hi2c->State = HAL_I2C_STATE_READY; |
3392 | hi2c->State = HAL_I2C_STATE_READY; |
3278 | 3393 | ||
3279 | /* Process Unlocked */ |
3394 | /* Process Unlocked */ |
3280 | __HAL_UNLOCK(hi2c); |
3395 | __HAL_UNLOCK(hi2c); |
Line 3299... | Line 3414... | ||
3299 | * @param Timeout Timeout duration |
3414 | * @param Timeout Timeout duration |
3300 | * @retval HAL status |
3415 | * @retval HAL status |
3301 | */ |
3416 | */ |
3302 | HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout) |
3417 | HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout) |
3303 | { |
3418 | { |
3304 | uint32_t tickstart = 0U, tmp1 = 0U, tmp2 = 0U, tmp3 = 0U, I2C_Trials = 1U; |
- | |
3305 | - | ||
3306 | /* Get tick */ |
3419 | /* Get tick */ |
3307 | tickstart = HAL_GetTick(); |
3420 | uint32_t tickstart = HAL_GetTick(); |
- | 3421 | uint32_t I2C_Trials = 1U; |
|
- | 3422 | FlagStatus tmp1; |
|
- | 3423 | FlagStatus tmp2; |
|
3308 | 3424 | ||
3309 | if(hi2c->State == HAL_I2C_STATE_READY) |
3425 | if (hi2c->State == HAL_I2C_STATE_READY) |
3310 | { |
3426 | { |
3311 | /* Wait until BUSY flag is reset */ |
3427 | /* Wait until BUSY flag is reset */ |
3312 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
3428 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
3313 | { |
3429 | { |
3314 | return HAL_BUSY; |
3430 | return HAL_BUSY; |
3315 | } |
3431 | } |
3316 | 3432 | ||
3317 | /* Process Locked */ |
3433 | /* Process Locked */ |
3318 | __HAL_LOCK(hi2c); |
3434 | __HAL_LOCK(hi2c); |
3319 | 3435 | ||
3320 | /* Check if the I2C is already enabled */ |
3436 | /* Check if the I2C is already enabled */ |
3321 | if((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
3437 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
3322 | { |
3438 | { |
3323 | /* Enable I2C peripheral */ |
3439 | /* Enable I2C peripheral */ |
3324 | __HAL_I2C_ENABLE(hi2c); |
3440 | __HAL_I2C_ENABLE(hi2c); |
3325 | } |
3441 | } |
3326 | 3442 | ||
3327 | /* Disable Pos */ |
3443 | /* Disable Pos */ |
3328 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
3444 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
3329 | 3445 | ||
3330 | hi2c->State = HAL_I2C_STATE_BUSY; |
3446 | hi2c->State = HAL_I2C_STATE_BUSY; |
3331 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
3447 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
3332 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
3448 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
3333 | 3449 | ||
3334 | do |
3450 | do |
3335 | { |
3451 | { |
3336 | /* Generate Start */ |
3452 | /* Generate Start */ |
3337 | hi2c->Instance->CR1 |= I2C_CR1_START; |
3453 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
3338 | 3454 | ||
3339 | /* Wait until SB flag is set */ |
3455 | /* Wait until SB flag is set */ |
3340 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, tickstart) != HAL_OK) |
3456 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, tickstart) != HAL_OK) |
3341 | { |
3457 | { |
- | 3458 | if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START) |
|
- | 3459 | { |
|
- | 3460 | hi2c->ErrorCode = HAL_I2C_WRONG_START; |
|
- | 3461 | } |
|
3342 | return HAL_TIMEOUT; |
3462 | return HAL_TIMEOUT; |
3343 | } |
3463 | } |
3344 | 3464 | ||
3345 | /* Send slave address */ |
3465 | /* Send slave address */ |
3346 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress); |
3466 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress); |
Line 3349... | Line 3469... | ||
3349 | /* Get tick */ |
3469 | /* Get tick */ |
3350 | tickstart = HAL_GetTick(); |
3470 | tickstart = HAL_GetTick(); |
3351 | 3471 | ||
3352 | tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR); |
3472 | tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR); |
3353 | tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF); |
3473 | tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF); |
3354 | tmp3 = hi2c->State; |
- | |
3355 | while((tmp1 == RESET) && (tmp2 == RESET) && (tmp3 != HAL_I2C_STATE_TIMEOUT)) |
3474 | while ((hi2c->State != HAL_I2C_STATE_TIMEOUT) && (tmp1 == RESET) && (tmp2 == RESET)) |
3356 | { |
3475 | { |
3357 | if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout)) |
3476 | if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) |
3358 | { |
3477 | { |
3359 | hi2c->State = HAL_I2C_STATE_TIMEOUT; |
3478 | hi2c->State = HAL_I2C_STATE_TIMEOUT; |
3360 | } |
3479 | } |
3361 | tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR); |
3480 | tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR); |
3362 | tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF); |
3481 | tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF); |
3363 | tmp3 = hi2c->State; |
- | |
3364 | } |
3482 | } |
3365 | 3483 | ||
3366 | hi2c->State = HAL_I2C_STATE_READY; |
3484 | hi2c->State = HAL_I2C_STATE_READY; |
3367 | 3485 | ||
3368 | /* Check if the ADDR flag has been set */ |
3486 | /* Check if the ADDR flag has been set */ |
3369 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET) |
3487 | if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET) |
3370 | { |
3488 | { |
3371 | /* Generate Stop */ |
3489 | /* Generate Stop */ |
3372 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
3490 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
3373 | 3491 | ||
3374 | /* Clear ADDR Flag */ |
3492 | /* Clear ADDR Flag */ |
3375 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
3493 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
3376 | 3494 | ||
3377 | /* Wait until BUSY flag is reset */ |
3495 | /* Wait until BUSY flag is reset */ |
3378 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
3496 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
3379 | { |
3497 | { |
3380 | return HAL_TIMEOUT; |
3498 | return HAL_ERROR; |
3381 | } |
3499 | } |
3382 | 3500 | ||
3383 | hi2c->State = HAL_I2C_STATE_READY; |
3501 | hi2c->State = HAL_I2C_STATE_READY; |
3384 | 3502 | ||
3385 | /* Process Unlocked */ |
3503 | /* Process Unlocked */ |
Line 3388... | Line 3506... | ||
3388 | return HAL_OK; |
3506 | return HAL_OK; |
3389 | } |
3507 | } |
3390 | else |
3508 | else |
3391 | { |
3509 | { |
3392 | /* Generate Stop */ |
3510 | /* Generate Stop */ |
3393 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
3511 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
3394 | 3512 | ||
3395 | /* Clear AF Flag */ |
3513 | /* Clear AF Flag */ |
3396 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
3514 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
3397 | 3515 | ||
3398 | /* Wait until BUSY flag is reset */ |
3516 | /* Wait until BUSY flag is reset */ |
3399 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
3517 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK) |
3400 | { |
3518 | { |
3401 | return HAL_TIMEOUT; |
3519 | return HAL_ERROR; |
3402 | } |
3520 | } |
3403 | } |
3521 | } |
- | 3522 | ||
- | 3523 | /* Increment Trials */ |
|
- | 3524 | I2C_Trials++; |
|
- | 3525 | } |
|
3404 | }while(I2C_Trials++ < Trials); |
3526 | while (I2C_Trials < Trials); |
3405 | 3527 | ||
3406 | hi2c->State = HAL_I2C_STATE_READY; |
3528 | hi2c->State = HAL_I2C_STATE_READY; |
3407 | 3529 | ||
3408 | /* Process Unlocked */ |
3530 | /* Process Unlocked */ |
3409 | __HAL_UNLOCK(hi2c); |
3531 | __HAL_UNLOCK(hi2c); |
Line 3415... | Line 3537... | ||
3415 | return HAL_BUSY; |
3537 | return HAL_BUSY; |
3416 | } |
3538 | } |
3417 | } |
3539 | } |
3418 | 3540 | ||
3419 | /** |
3541 | /** |
- | 3542 | * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with Interrupt. |
|
- | 3543 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
|
- | 3544 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 3545 | * the configuration information for the specified I2C. |
|
- | 3546 | * @param DevAddress Target device address: The device 7 bits address value |
|
- | 3547 | * in datasheet must be shifted to the left before calling the interface |
|
- | 3548 | * @param pData Pointer to data buffer |
|
- | 3549 | * @param Size Amount of data to be sent |
|
- | 3550 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
|
- | 3551 | * @retval HAL status |
|
- | 3552 | */ |
|
- | 3553 | HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
|
- | 3554 | { |
|
- | 3555 | __IO uint32_t Prev_State = 0x00U; |
|
- | 3556 | __IO uint32_t count = 0x00U; |
|
- | 3557 | ||
- | 3558 | /* Check the parameters */ |
|
- | 3559 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
|
- | 3560 | ||
- | 3561 | if (hi2c->State == HAL_I2C_STATE_READY) |
|
- | 3562 | { |
|
- | 3563 | /* Check Busy Flag only if FIRST call of Master interface */ |
|
- | 3564 | if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME)) |
|
- | 3565 | { |
|
- | 3566 | /* Wait until BUSY flag is reset */ |
|
- | 3567 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
|
- | 3568 | do |
|
- | 3569 | { |
|
- | 3570 | count--; |
|
- | 3571 | if (count == 0U) |
|
- | 3572 | { |
|
- | 3573 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 3574 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 3575 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 3576 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
- | 3577 | ||
- | 3578 | /* Process Unlocked */ |
|
- | 3579 | __HAL_UNLOCK(hi2c); |
|
- | 3580 | ||
- | 3581 | return HAL_ERROR; |
|
- | 3582 | } |
|
- | 3583 | } |
|
- | 3584 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
|
- | 3585 | } |
|
- | 3586 | ||
- | 3587 | /* Process Locked */ |
|
- | 3588 | __HAL_LOCK(hi2c); |
|
- | 3589 | ||
- | 3590 | /* Check if the I2C is already enabled */ |
|
- | 3591 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
|
- | 3592 | { |
|
- | 3593 | /* Enable I2C peripheral */ |
|
- | 3594 | __HAL_I2C_ENABLE(hi2c); |
|
- | 3595 | } |
|
- | 3596 | ||
- | 3597 | /* Disable Pos */ |
|
- | 3598 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
|
- | 3599 | ||
- | 3600 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
|
- | 3601 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
|
- | 3602 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
|
- | 3603 | ||
- | 3604 | /* Prepare transfer parameters */ |
|
- | 3605 | hi2c->pBuffPtr = pData; |
|
- | 3606 | hi2c->XferCount = Size; |
|
- | 3607 | hi2c->XferSize = hi2c->XferCount; |
|
- | 3608 | hi2c->XferOptions = XferOptions; |
|
- | 3609 | hi2c->Devaddress = DevAddress; |
|
- | 3610 | ||
- | 3611 | Prev_State = hi2c->PreviousState; |
|
- | 3612 | ||
- | 3613 | /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */ |
|
- | 3614 | /* Mean Previous state is same as current state */ |
|
- | 3615 | if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1)) |
|
- | 3616 | { |
|
- | 3617 | /* Generate Start */ |
|
- | 3618 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
|
- | 3619 | } |
|
- | 3620 | ||
- | 3621 | /* Process Unlocked */ |
|
- | 3622 | __HAL_UNLOCK(hi2c); |
|
- | 3623 | ||
- | 3624 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 3625 | to avoid the risk of I2C interrupt handle execution before current |
|
- | 3626 | process unlock */ |
|
- | 3627 | ||
- | 3628 | /* Enable EVT, BUF and ERR interrupt */ |
|
- | 3629 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
|
- | 3630 | ||
- | 3631 | return HAL_OK; |
|
- | 3632 | } |
|
- | 3633 | else |
|
- | 3634 | { |
|
- | 3635 | return HAL_BUSY; |
|
- | 3636 | } |
|
- | 3637 | } |
|
- | 3638 | ||
- | 3639 | /** |
|
- | 3640 | * @brief Sequential transmit in master I2C mode an amount of data in non-blocking mode with DMA. |
|
- | 3641 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
|
- | 3642 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 3643 | * the configuration information for the specified I2C. |
|
- | 3644 | * @param DevAddress Target device address: The device 7 bits address value |
|
- | 3645 | * in datasheet must be shifted to the left before calling the interface |
|
- | 3646 | * @param pData Pointer to data buffer |
|
- | 3647 | * @param Size Amount of data to be sent |
|
- | 3648 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
|
- | 3649 | * @retval HAL status |
|
- | 3650 | */ |
|
- | 3651 | HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
|
- | 3652 | { |
|
- | 3653 | __IO uint32_t Prev_State = 0x00U; |
|
- | 3654 | __IO uint32_t count = 0x00U; |
|
- | 3655 | HAL_StatusTypeDef dmaxferstatus; |
|
- | 3656 | ||
- | 3657 | /* Check the parameters */ |
|
- | 3658 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
|
- | 3659 | ||
- | 3660 | if (hi2c->State == HAL_I2C_STATE_READY) |
|
- | 3661 | { |
|
- | 3662 | /* Check Busy Flag only if FIRST call of Master interface */ |
|
- | 3663 | if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME)) |
|
- | 3664 | { |
|
- | 3665 | /* Wait until BUSY flag is reset */ |
|
- | 3666 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
|
- | 3667 | do |
|
- | 3668 | { |
|
- | 3669 | count--; |
|
- | 3670 | if (count == 0U) |
|
- | 3671 | { |
|
- | 3672 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 3673 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 3674 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 3675 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
- | 3676 | ||
- | 3677 | /* Process Unlocked */ |
|
- | 3678 | __HAL_UNLOCK(hi2c); |
|
- | 3679 | ||
- | 3680 | return HAL_ERROR; |
|
- | 3681 | } |
|
- | 3682 | } |
|
- | 3683 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
|
- | 3684 | } |
|
- | 3685 | ||
- | 3686 | /* Process Locked */ |
|
- | 3687 | __HAL_LOCK(hi2c); |
|
- | 3688 | ||
- | 3689 | /* Check if the I2C is already enabled */ |
|
- | 3690 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
|
- | 3691 | { |
|
- | 3692 | /* Enable I2C peripheral */ |
|
- | 3693 | __HAL_I2C_ENABLE(hi2c); |
|
- | 3694 | } |
|
- | 3695 | ||
- | 3696 | /* Disable Pos */ |
|
- | 3697 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
|
- | 3698 | ||
- | 3699 | hi2c->State = HAL_I2C_STATE_BUSY_TX; |
|
- | 3700 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
|
- | 3701 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
|
- | 3702 | ||
- | 3703 | /* Prepare transfer parameters */ |
|
- | 3704 | hi2c->pBuffPtr = pData; |
|
- | 3705 | hi2c->XferCount = Size; |
|
- | 3706 | hi2c->XferSize = hi2c->XferCount; |
|
- | 3707 | hi2c->XferOptions = XferOptions; |
|
- | 3708 | hi2c->Devaddress = DevAddress; |
|
- | 3709 | ||
- | 3710 | Prev_State = hi2c->PreviousState; |
|
- | 3711 | ||
- | 3712 | if (hi2c->XferSize > 0U) |
|
- | 3713 | { |
|
- | 3714 | /* Set the I2C DMA transfer complete callback */ |
|
- | 3715 | hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt; |
|
- | 3716 | ||
- | 3717 | /* Set the DMA error callback */ |
|
- | 3718 | hi2c->hdmatx->XferErrorCallback = I2C_DMAError; |
|
- | 3719 | ||
- | 3720 | /* Set the unused DMA callbacks to NULL */ |
|
- | 3721 | hi2c->hdmatx->XferHalfCpltCallback = NULL; |
|
- | 3722 | hi2c->hdmatx->XferAbortCallback = NULL; |
|
- | 3723 | ||
- | 3724 | /* Enable the DMA channel */ |
|
- | 3725 | dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize); |
|
- | 3726 | ||
- | 3727 | if (dmaxferstatus == HAL_OK) |
|
- | 3728 | { |
|
- | 3729 | /* Enable Acknowledge */ |
|
- | 3730 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 3731 | ||
- | 3732 | /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */ |
|
- | 3733 | /* Mean Previous state is same as current state */ |
|
- | 3734 | if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1)) |
|
- | 3735 | { |
|
- | 3736 | /* Generate Start */ |
|
- | 3737 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
|
- | 3738 | } |
|
- | 3739 | ||
- | 3740 | /* Process Unlocked */ |
|
- | 3741 | __HAL_UNLOCK(hi2c); |
|
- | 3742 | ||
- | 3743 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 3744 | to avoid the risk of I2C interrupt handle execution before current |
|
- | 3745 | process unlock */ |
|
- | 3746 | ||
- | 3747 | /* If XferOptions is not associated to a new frame, mean no start bit is request, enable directly the DMA request */ |
|
- | 3748 | /* In other cases, DMA request is enabled after Slave address treatment in IRQHandler */ |
|
- | 3749 | if ((XferOptions == I2C_NEXT_FRAME) || (XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP)) |
|
- | 3750 | { |
|
- | 3751 | /* Enable DMA Request */ |
|
- | 3752 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 3753 | } |
|
- | 3754 | ||
- | 3755 | /* Enable EVT and ERR interrupt */ |
|
- | 3756 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
|
- | 3757 | } |
|
- | 3758 | else |
|
- | 3759 | { |
|
- | 3760 | /* Update I2C state */ |
|
- | 3761 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 3762 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 3763 | ||
- | 3764 | /* Update I2C error code */ |
|
- | 3765 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
|
- | 3766 | ||
- | 3767 | /* Process Unlocked */ |
|
- | 3768 | __HAL_UNLOCK(hi2c); |
|
- | 3769 | ||
- | 3770 | return HAL_ERROR; |
|
- | 3771 | } |
|
- | 3772 | } |
|
- | 3773 | else |
|
- | 3774 | { |
|
- | 3775 | /* Enable Acknowledge */ |
|
- | 3776 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 3777 | ||
- | 3778 | /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */ |
|
- | 3779 | /* Mean Previous state is same as current state */ |
|
- | 3780 | if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1)) |
|
- | 3781 | { |
|
- | 3782 | /* Generate Start */ |
|
- | 3783 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
|
- | 3784 | } |
|
- | 3785 | ||
- | 3786 | /* Process Unlocked */ |
|
- | 3787 | __HAL_UNLOCK(hi2c); |
|
- | 3788 | ||
- | 3789 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 3790 | to avoid the risk of I2C interrupt handle execution before current |
|
- | 3791 | process unlock */ |
|
- | 3792 | ||
- | 3793 | /* Enable EVT, BUF and ERR interrupt */ |
|
- | 3794 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
|
- | 3795 | } |
|
- | 3796 | ||
- | 3797 | return HAL_OK; |
|
- | 3798 | } |
|
- | 3799 | else |
|
- | 3800 | { |
|
- | 3801 | return HAL_BUSY; |
|
- | 3802 | } |
|
- | 3803 | } |
|
- | 3804 | ||
- | 3805 | /** |
|
- | 3806 | * @brief Sequential receive in master I2C mode an amount of data in non-blocking mode with Interrupt |
|
- | 3807 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
|
- | 3808 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 3809 | * the configuration information for the specified I2C. |
|
- | 3810 | * @param DevAddress Target device address: The device 7 bits address value |
|
- | 3811 | * in datasheet must be shifted to the left before calling the interface |
|
- | 3812 | * @param pData Pointer to data buffer |
|
- | 3813 | * @param Size Amount of data to be sent |
|
- | 3814 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
|
- | 3815 | * @retval HAL status |
|
- | 3816 | */ |
|
- | 3817 | HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
|
- | 3818 | { |
|
- | 3819 | __IO uint32_t Prev_State = 0x00U; |
|
- | 3820 | __IO uint32_t count = 0U; |
|
- | 3821 | uint32_t enableIT = (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
|
- | 3822 | ||
- | 3823 | /* Check the parameters */ |
|
- | 3824 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
|
- | 3825 | ||
- | 3826 | if (hi2c->State == HAL_I2C_STATE_READY) |
|
- | 3827 | { |
|
- | 3828 | /* Check Busy Flag only if FIRST call of Master interface */ |
|
- | 3829 | if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME)) |
|
- | 3830 | { |
|
- | 3831 | /* Wait until BUSY flag is reset */ |
|
- | 3832 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
|
- | 3833 | do |
|
- | 3834 | { |
|
- | 3835 | count--; |
|
- | 3836 | if (count == 0U) |
|
- | 3837 | { |
|
- | 3838 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 3839 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 3840 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 3841 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
- | 3842 | ||
- | 3843 | /* Process Unlocked */ |
|
- | 3844 | __HAL_UNLOCK(hi2c); |
|
- | 3845 | ||
- | 3846 | return HAL_ERROR; |
|
- | 3847 | } |
|
- | 3848 | } |
|
- | 3849 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
|
- | 3850 | } |
|
- | 3851 | ||
- | 3852 | /* Process Locked */ |
|
- | 3853 | __HAL_LOCK(hi2c); |
|
- | 3854 | ||
- | 3855 | /* Check if the I2C is already enabled */ |
|
- | 3856 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
|
- | 3857 | { |
|
- | 3858 | /* Enable I2C peripheral */ |
|
- | 3859 | __HAL_I2C_ENABLE(hi2c); |
|
- | 3860 | } |
|
- | 3861 | ||
- | 3862 | /* Disable Pos */ |
|
- | 3863 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
|
- | 3864 | ||
- | 3865 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
|
- | 3866 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
|
- | 3867 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
|
- | 3868 | ||
- | 3869 | /* Prepare transfer parameters */ |
|
- | 3870 | hi2c->pBuffPtr = pData; |
|
- | 3871 | hi2c->XferCount = Size; |
|
- | 3872 | hi2c->XferSize = hi2c->XferCount; |
|
- | 3873 | hi2c->XferOptions = XferOptions; |
|
- | 3874 | hi2c->Devaddress = DevAddress; |
|
- | 3875 | ||
- | 3876 | Prev_State = hi2c->PreviousState; |
|
- | 3877 | ||
- | 3878 | if ((hi2c->XferCount == 2U) && ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))) |
|
- | 3879 | { |
|
- | 3880 | if (Prev_State == I2C_STATE_MASTER_BUSY_RX) |
|
- | 3881 | { |
|
- | 3882 | /* Disable Acknowledge */ |
|
- | 3883 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 3884 | ||
- | 3885 | /* Enable Pos */ |
|
- | 3886 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
|
- | 3887 | ||
- | 3888 | /* Remove Enabling of IT_BUF, mean RXNE treatment, treat the 2 bytes through BTF */ |
|
- | 3889 | enableIT &= ~I2C_IT_BUF; |
|
- | 3890 | } |
|
- | 3891 | else |
|
- | 3892 | { |
|
- | 3893 | /* Enable Acknowledge */ |
|
- | 3894 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 3895 | } |
|
- | 3896 | } |
|
- | 3897 | else |
|
- | 3898 | { |
|
- | 3899 | /* Enable Acknowledge */ |
|
- | 3900 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 3901 | } |
|
- | 3902 | ||
- | 3903 | /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */ |
|
- | 3904 | /* Mean Previous state is same as current state */ |
|
- | 3905 | if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1)) |
|
- | 3906 | { |
|
- | 3907 | /* Generate Start */ |
|
- | 3908 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
|
- | 3909 | } |
|
- | 3910 | ||
- | 3911 | /* Process Unlocked */ |
|
- | 3912 | __HAL_UNLOCK(hi2c); |
|
- | 3913 | ||
- | 3914 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 3915 | to avoid the risk of I2C interrupt handle execution before current |
|
- | 3916 | process unlock */ |
|
- | 3917 | ||
- | 3918 | /* Enable interrupts */ |
|
- | 3919 | __HAL_I2C_ENABLE_IT(hi2c, enableIT); |
|
- | 3920 | ||
- | 3921 | return HAL_OK; |
|
- | 3922 | } |
|
- | 3923 | else |
|
- | 3924 | { |
|
- | 3925 | return HAL_BUSY; |
|
- | 3926 | } |
|
- | 3927 | } |
|
- | 3928 | ||
- | 3929 | /** |
|
- | 3930 | * @brief Sequential receive in master mode an amount of data in non-blocking mode with DMA |
|
- | 3931 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
|
- | 3932 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 3933 | * the configuration information for the specified I2C. |
|
- | 3934 | * @param DevAddress Target device address: The device 7 bits address value |
|
- | 3935 | * in datasheet must be shifted to the left before calling the interface |
|
- | 3936 | * @param pData Pointer to data buffer |
|
- | 3937 | * @param Size Amount of data to be sent |
|
- | 3938 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
|
- | 3939 | * @retval HAL status |
|
- | 3940 | */ |
|
- | 3941 | HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
|
- | 3942 | { |
|
- | 3943 | __IO uint32_t Prev_State = 0x00U; |
|
- | 3944 | __IO uint32_t count = 0U; |
|
- | 3945 | uint32_t enableIT = (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
|
- | 3946 | HAL_StatusTypeDef dmaxferstatus; |
|
- | 3947 | ||
- | 3948 | /* Check the parameters */ |
|
- | 3949 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
|
- | 3950 | ||
- | 3951 | if (hi2c->State == HAL_I2C_STATE_READY) |
|
- | 3952 | { |
|
- | 3953 | /* Check Busy Flag only if FIRST call of Master interface */ |
|
- | 3954 | if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME)) |
|
- | 3955 | { |
|
- | 3956 | /* Wait until BUSY flag is reset */ |
|
- | 3957 | count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U); |
|
- | 3958 | do |
|
- | 3959 | { |
|
- | 3960 | count--; |
|
- | 3961 | if (count == 0U) |
|
- | 3962 | { |
|
- | 3963 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 3964 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 3965 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 3966 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
- | 3967 | ||
- | 3968 | /* Process Unlocked */ |
|
- | 3969 | __HAL_UNLOCK(hi2c); |
|
- | 3970 | ||
- | 3971 | return HAL_ERROR; |
|
- | 3972 | } |
|
- | 3973 | } |
|
- | 3974 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET); |
|
- | 3975 | } |
|
- | 3976 | ||
- | 3977 | /* Process Locked */ |
|
- | 3978 | __HAL_LOCK(hi2c); |
|
- | 3979 | ||
- | 3980 | /* Check if the I2C is already enabled */ |
|
- | 3981 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
|
- | 3982 | { |
|
- | 3983 | /* Enable I2C peripheral */ |
|
- | 3984 | __HAL_I2C_ENABLE(hi2c); |
|
- | 3985 | } |
|
- | 3986 | ||
- | 3987 | /* Disable Pos */ |
|
- | 3988 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
|
- | 3989 | ||
- | 3990 | /* Clear Last DMA bit */ |
|
- | 3991 | CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST); |
|
- | 3992 | ||
- | 3993 | hi2c->State = HAL_I2C_STATE_BUSY_RX; |
|
- | 3994 | hi2c->Mode = HAL_I2C_MODE_MASTER; |
|
- | 3995 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
|
- | 3996 | ||
- | 3997 | /* Prepare transfer parameters */ |
|
- | 3998 | hi2c->pBuffPtr = pData; |
|
- | 3999 | hi2c->XferCount = Size; |
|
- | 4000 | hi2c->XferSize = hi2c->XferCount; |
|
- | 4001 | hi2c->XferOptions = XferOptions; |
|
- | 4002 | hi2c->Devaddress = DevAddress; |
|
- | 4003 | ||
- | 4004 | Prev_State = hi2c->PreviousState; |
|
- | 4005 | ||
- | 4006 | if (hi2c->XferSize > 0U) |
|
- | 4007 | { |
|
- | 4008 | if ((hi2c->XferCount == 2U) && ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))) |
|
- | 4009 | { |
|
- | 4010 | if (Prev_State == I2C_STATE_MASTER_BUSY_RX) |
|
- | 4011 | { |
|
- | 4012 | /* Disable Acknowledge */ |
|
- | 4013 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 4014 | ||
- | 4015 | /* Enable Pos */ |
|
- | 4016 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
|
- | 4017 | ||
- | 4018 | /* Enable Last DMA bit */ |
|
- | 4019 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST); |
|
- | 4020 | } |
|
- | 4021 | else |
|
- | 4022 | { |
|
- | 4023 | /* Enable Acknowledge */ |
|
- | 4024 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 4025 | } |
|
- | 4026 | } |
|
- | 4027 | else |
|
- | 4028 | { |
|
- | 4029 | /* Enable Acknowledge */ |
|
- | 4030 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 4031 | ||
- | 4032 | if ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_OTHER_AND_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP)) |
|
- | 4033 | { |
|
- | 4034 | /* Enable Last DMA bit */ |
|
- | 4035 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST); |
|
- | 4036 | } |
|
- | 4037 | } |
|
- | 4038 | ||
- | 4039 | /* Set the I2C DMA transfer complete callback */ |
|
- | 4040 | hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt; |
|
- | 4041 | ||
- | 4042 | /* Set the DMA error callback */ |
|
- | 4043 | hi2c->hdmarx->XferErrorCallback = I2C_DMAError; |
|
- | 4044 | ||
- | 4045 | /* Set the unused DMA callbacks to NULL */ |
|
- | 4046 | hi2c->hdmarx->XferHalfCpltCallback = NULL; |
|
- | 4047 | hi2c->hdmarx->XferAbortCallback = NULL; |
|
- | 4048 | ||
- | 4049 | /* Enable the DMA channel */ |
|
- | 4050 | dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize); |
|
- | 4051 | ||
- | 4052 | if (dmaxferstatus == HAL_OK) |
|
- | 4053 | { |
|
- | 4054 | /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */ |
|
- | 4055 | /* Mean Previous state is same as current state */ |
|
- | 4056 | if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1)) |
|
- | 4057 | { |
|
- | 4058 | /* Generate Start */ |
|
- | 4059 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
|
- | 4060 | ||
- | 4061 | /* Update interrupt for only EVT and ERR */ |
|
- | 4062 | enableIT = (I2C_IT_EVT | I2C_IT_ERR); |
|
- | 4063 | } |
|
- | 4064 | else |
|
- | 4065 | { |
|
- | 4066 | /* Update interrupt for only ERR */ |
|
- | 4067 | enableIT = I2C_IT_ERR; |
|
- | 4068 | } |
|
- | 4069 | ||
- | 4070 | /* Process Unlocked */ |
|
- | 4071 | __HAL_UNLOCK(hi2c); |
|
- | 4072 | ||
- | 4073 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 4074 | to avoid the risk of I2C interrupt handle execution before current |
|
- | 4075 | process unlock */ |
|
- | 4076 | ||
- | 4077 | /* If XferOptions is not associated to a new frame, mean no start bit is request, enable directly the DMA request */ |
|
- | 4078 | /* In other cases, DMA request is enabled after Slave address treatment in IRQHandler */ |
|
- | 4079 | if ((XferOptions == I2C_NEXT_FRAME) || (XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP)) |
|
- | 4080 | { |
|
- | 4081 | /* Enable DMA Request */ |
|
- | 4082 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 4083 | } |
|
- | 4084 | ||
- | 4085 | /* Enable EVT and ERR interrupt */ |
|
- | 4086 | __HAL_I2C_ENABLE_IT(hi2c, enableIT); |
|
- | 4087 | } |
|
- | 4088 | else |
|
- | 4089 | { |
|
- | 4090 | /* Update I2C state */ |
|
- | 4091 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 4092 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 4093 | ||
- | 4094 | /* Update I2C error code */ |
|
- | 4095 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
|
- | 4096 | ||
- | 4097 | /* Process Unlocked */ |
|
- | 4098 | __HAL_UNLOCK(hi2c); |
|
- | 4099 | ||
- | 4100 | return HAL_ERROR; |
|
- | 4101 | } |
|
- | 4102 | } |
|
- | 4103 | else |
|
- | 4104 | { |
|
- | 4105 | /* Enable Acknowledge */ |
|
- | 4106 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 4107 | ||
- | 4108 | /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */ |
|
- | 4109 | /* Mean Previous state is same as current state */ |
|
- | 4110 | if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1)) |
|
- | 4111 | { |
|
- | 4112 | /* Generate Start */ |
|
- | 4113 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
|
- | 4114 | } |
|
- | 4115 | ||
- | 4116 | /* Process Unlocked */ |
|
- | 4117 | __HAL_UNLOCK(hi2c); |
|
- | 4118 | ||
- | 4119 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 4120 | to avoid the risk of I2C interrupt handle execution before current |
|
- | 4121 | process unlock */ |
|
- | 4122 | ||
- | 4123 | /* Enable interrupts */ |
|
- | 4124 | __HAL_I2C_ENABLE_IT(hi2c, enableIT); |
|
- | 4125 | } |
|
- | 4126 | return HAL_OK; |
|
- | 4127 | } |
|
- | 4128 | else |
|
- | 4129 | { |
|
- | 4130 | return HAL_BUSY; |
|
- | 4131 | } |
|
- | 4132 | } |
|
- | 4133 | ||
- | 4134 | /** |
|
- | 4135 | * @brief Sequential transmit in slave mode an amount of data in non-blocking mode with Interrupt |
|
- | 4136 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
|
- | 4137 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 4138 | * the configuration information for the specified I2C. |
|
- | 4139 | * @param pData Pointer to data buffer |
|
- | 4140 | * @param Size Amount of data to be sent |
|
- | 4141 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
|
- | 4142 | * @retval HAL status |
|
- | 4143 | */ |
|
- | 4144 | HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
|
- | 4145 | { |
|
- | 4146 | /* Check the parameters */ |
|
- | 4147 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
|
- | 4148 | ||
- | 4149 | if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) |
|
- | 4150 | { |
|
- | 4151 | if ((pData == NULL) || (Size == 0U)) |
|
- | 4152 | { |
|
- | 4153 | return HAL_ERROR; |
|
- | 4154 | } |
|
- | 4155 | ||
- | 4156 | /* Process Locked */ |
|
- | 4157 | __HAL_LOCK(hi2c); |
|
- | 4158 | ||
- | 4159 | /* Check if the I2C is already enabled */ |
|
- | 4160 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
|
- | 4161 | { |
|
- | 4162 | /* Enable I2C peripheral */ |
|
- | 4163 | __HAL_I2C_ENABLE(hi2c); |
|
- | 4164 | } |
|
- | 4165 | ||
- | 4166 | /* Disable Pos */ |
|
- | 4167 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
|
- | 4168 | ||
- | 4169 | hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN; |
|
- | 4170 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
|
- | 4171 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
|
- | 4172 | ||
- | 4173 | /* Prepare transfer parameters */ |
|
- | 4174 | hi2c->pBuffPtr = pData; |
|
- | 4175 | hi2c->XferCount = Size; |
|
- | 4176 | hi2c->XferSize = hi2c->XferCount; |
|
- | 4177 | hi2c->XferOptions = XferOptions; |
|
- | 4178 | ||
- | 4179 | /* Clear ADDR flag */ |
|
- | 4180 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
|
- | 4181 | ||
- | 4182 | /* Process Unlocked */ |
|
- | 4183 | __HAL_UNLOCK(hi2c); |
|
- | 4184 | ||
- | 4185 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 4186 | to avoid the risk of I2C interrupt handle execution before current |
|
- | 4187 | process unlock */ |
|
- | 4188 | ||
- | 4189 | /* Enable EVT, BUF and ERR interrupt */ |
|
- | 4190 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
|
- | 4191 | ||
- | 4192 | return HAL_OK; |
|
- | 4193 | } |
|
- | 4194 | else |
|
- | 4195 | { |
|
- | 4196 | return HAL_BUSY; |
|
- | 4197 | } |
|
- | 4198 | } |
|
- | 4199 | ||
- | 4200 | /** |
|
- | 4201 | * @brief Sequential transmit in slave mode an amount of data in non-blocking mode with DMA |
|
- | 4202 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
|
- | 4203 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 4204 | * the configuration information for the specified I2C. |
|
- | 4205 | * @param pData Pointer to data buffer |
|
- | 4206 | * @param Size Amount of data to be sent |
|
- | 4207 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
|
- | 4208 | * @retval HAL status |
|
- | 4209 | */ |
|
- | 4210 | HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
|
- | 4211 | { |
|
- | 4212 | HAL_StatusTypeDef dmaxferstatus; |
|
- | 4213 | ||
- | 4214 | /* Check the parameters */ |
|
- | 4215 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
|
- | 4216 | ||
- | 4217 | if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) |
|
- | 4218 | { |
|
- | 4219 | if ((pData == NULL) || (Size == 0U)) |
|
- | 4220 | { |
|
- | 4221 | return HAL_ERROR; |
|
- | 4222 | } |
|
- | 4223 | ||
- | 4224 | /* Process Locked */ |
|
- | 4225 | __HAL_LOCK(hi2c); |
|
- | 4226 | ||
- | 4227 | /* Disable Interrupts, to prevent preemption during treatment in case of multicall */ |
|
- | 4228 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
|
- | 4229 | ||
- | 4230 | /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */ |
|
- | 4231 | /* and then toggle the HAL slave RX state to TX state */ |
|
- | 4232 | if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN) |
|
- | 4233 | { |
|
- | 4234 | if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
|
- | 4235 | { |
|
- | 4236 | /* Abort DMA Xfer if any */ |
|
- | 4237 | if (hi2c->hdmarx != NULL) |
|
- | 4238 | { |
|
- | 4239 | CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 4240 | ||
- | 4241 | /* Set the I2C DMA Abort callback : |
|
- | 4242 | will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ |
|
- | 4243 | hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort; |
|
- | 4244 | ||
- | 4245 | /* Abort DMA RX */ |
|
- | 4246 | if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK) |
|
- | 4247 | { |
|
- | 4248 | /* Call Directly XferAbortCallback function in case of error */ |
|
- | 4249 | hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx); |
|
- | 4250 | } |
|
- | 4251 | } |
|
- | 4252 | } |
|
- | 4253 | } |
|
- | 4254 | else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) |
|
- | 4255 | { |
|
- | 4256 | if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
|
- | 4257 | { |
|
- | 4258 | CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 4259 | ||
- | 4260 | /* Abort DMA Xfer if any */ |
|
- | 4261 | if (hi2c->hdmatx != NULL) |
|
- | 4262 | { |
|
- | 4263 | /* Set the I2C DMA Abort callback : |
|
- | 4264 | will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ |
|
- | 4265 | hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort; |
|
- | 4266 | ||
- | 4267 | /* Abort DMA TX */ |
|
- | 4268 | if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK) |
|
- | 4269 | { |
|
- | 4270 | /* Call Directly XferAbortCallback function in case of error */ |
|
- | 4271 | hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx); |
|
- | 4272 | } |
|
- | 4273 | } |
|
- | 4274 | } |
|
- | 4275 | } |
|
- | 4276 | else |
|
- | 4277 | { |
|
- | 4278 | /* Nothing to do */ |
|
- | 4279 | } |
|
- | 4280 | ||
- | 4281 | /* Check if the I2C is already enabled */ |
|
- | 4282 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
|
- | 4283 | { |
|
- | 4284 | /* Enable I2C peripheral */ |
|
- | 4285 | __HAL_I2C_ENABLE(hi2c); |
|
- | 4286 | } |
|
- | 4287 | ||
- | 4288 | /* Disable Pos */ |
|
- | 4289 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
|
- | 4290 | ||
- | 4291 | hi2c->State = HAL_I2C_STATE_BUSY_TX_LISTEN; |
|
- | 4292 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
|
- | 4293 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
|
- | 4294 | ||
- | 4295 | /* Prepare transfer parameters */ |
|
- | 4296 | hi2c->pBuffPtr = pData; |
|
- | 4297 | hi2c->XferCount = Size; |
|
- | 4298 | hi2c->XferSize = hi2c->XferCount; |
|
- | 4299 | hi2c->XferOptions = XferOptions; |
|
- | 4300 | ||
- | 4301 | /* Set the I2C DMA transfer complete callback */ |
|
- | 4302 | hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt; |
|
- | 4303 | ||
- | 4304 | /* Set the DMA error callback */ |
|
- | 4305 | hi2c->hdmatx->XferErrorCallback = I2C_DMAError; |
|
- | 4306 | ||
- | 4307 | /* Set the unused DMA callbacks to NULL */ |
|
- | 4308 | hi2c->hdmatx->XferHalfCpltCallback = NULL; |
|
- | 4309 | hi2c->hdmatx->XferAbortCallback = NULL; |
|
- | 4310 | ||
- | 4311 | /* Enable the DMA channel */ |
|
- | 4312 | dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize); |
|
- | 4313 | ||
- | 4314 | if (dmaxferstatus == HAL_OK) |
|
- | 4315 | { |
|
- | 4316 | /* Enable Address Acknowledge */ |
|
- | 4317 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 4318 | ||
- | 4319 | /* Clear ADDR flag */ |
|
- | 4320 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
|
- | 4321 | ||
- | 4322 | /* Process Unlocked */ |
|
- | 4323 | __HAL_UNLOCK(hi2c); |
|
- | 4324 | ||
- | 4325 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 4326 | to avoid the risk of I2C interrupt handle execution before current |
|
- | 4327 | process unlock */ |
|
- | 4328 | /* Enable EVT and ERR interrupt */ |
|
- | 4329 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
|
- | 4330 | ||
- | 4331 | /* Enable DMA Request */ |
|
- | 4332 | hi2c->Instance->CR2 |= I2C_CR2_DMAEN; |
|
- | 4333 | ||
- | 4334 | return HAL_OK; |
|
- | 4335 | } |
|
- | 4336 | else |
|
- | 4337 | { |
|
- | 4338 | /* Update I2C state */ |
|
- | 4339 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 4340 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 4341 | ||
- | 4342 | /* Update I2C error code */ |
|
- | 4343 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
|
- | 4344 | ||
- | 4345 | /* Process Unlocked */ |
|
- | 4346 | __HAL_UNLOCK(hi2c); |
|
- | 4347 | ||
- | 4348 | return HAL_ERROR; |
|
- | 4349 | } |
|
- | 4350 | } |
|
- | 4351 | else |
|
- | 4352 | { |
|
- | 4353 | return HAL_BUSY; |
|
- | 4354 | } |
|
- | 4355 | } |
|
- | 4356 | ||
- | 4357 | /** |
|
- | 4358 | * @brief Sequential receive in slave mode an amount of data in non-blocking mode with Interrupt |
|
- | 4359 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
|
- | 4360 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 4361 | * the configuration information for the specified I2C. |
|
- | 4362 | * @param pData Pointer to data buffer |
|
- | 4363 | * @param Size Amount of data to be sent |
|
- | 4364 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
|
- | 4365 | * @retval HAL status |
|
- | 4366 | */ |
|
- | 4367 | HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
|
- | 4368 | { |
|
- | 4369 | /* Check the parameters */ |
|
- | 4370 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
|
- | 4371 | ||
- | 4372 | if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) |
|
- | 4373 | { |
|
- | 4374 | if ((pData == NULL) || (Size == 0U)) |
|
- | 4375 | { |
|
- | 4376 | return HAL_ERROR; |
|
- | 4377 | } |
|
- | 4378 | ||
- | 4379 | /* Process Locked */ |
|
- | 4380 | __HAL_LOCK(hi2c); |
|
- | 4381 | ||
- | 4382 | /* Check if the I2C is already enabled */ |
|
- | 4383 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
|
- | 4384 | { |
|
- | 4385 | /* Enable I2C peripheral */ |
|
- | 4386 | __HAL_I2C_ENABLE(hi2c); |
|
- | 4387 | } |
|
- | 4388 | ||
- | 4389 | /* Disable Pos */ |
|
- | 4390 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
|
- | 4391 | ||
- | 4392 | hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN; |
|
- | 4393 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
|
- | 4394 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
|
- | 4395 | ||
- | 4396 | /* Prepare transfer parameters */ |
|
- | 4397 | hi2c->pBuffPtr = pData; |
|
- | 4398 | hi2c->XferCount = Size; |
|
- | 4399 | hi2c->XferSize = hi2c->XferCount; |
|
- | 4400 | hi2c->XferOptions = XferOptions; |
|
- | 4401 | ||
- | 4402 | /* Clear ADDR flag */ |
|
- | 4403 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
|
- | 4404 | ||
- | 4405 | /* Process Unlocked */ |
|
- | 4406 | __HAL_UNLOCK(hi2c); |
|
- | 4407 | ||
- | 4408 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 4409 | to avoid the risk of I2C interrupt handle execution before current |
|
- | 4410 | process unlock */ |
|
- | 4411 | ||
- | 4412 | /* Enable EVT, BUF and ERR interrupt */ |
|
- | 4413 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
|
- | 4414 | ||
- | 4415 | return HAL_OK; |
|
- | 4416 | } |
|
- | 4417 | else |
|
- | 4418 | { |
|
- | 4419 | return HAL_BUSY; |
|
- | 4420 | } |
|
- | 4421 | } |
|
- | 4422 | ||
- | 4423 | /** |
|
- | 4424 | * @brief Sequential receive in slave mode an amount of data in non-blocking mode with DMA |
|
- | 4425 | * @note This interface allow to manage repeated start condition when a direction change during transfer |
|
- | 4426 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 4427 | * the configuration information for the specified I2C. |
|
- | 4428 | * @param pData Pointer to data buffer |
|
- | 4429 | * @param Size Amount of data to be sent |
|
- | 4430 | * @param XferOptions Options of Transfer, value of @ref I2C_XferOptions_definition |
|
- | 4431 | * @retval HAL status |
|
- | 4432 | */ |
|
- | 4433 | HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions) |
|
- | 4434 | { |
|
- | 4435 | HAL_StatusTypeDef dmaxferstatus; |
|
- | 4436 | ||
- | 4437 | /* Check the parameters */ |
|
- | 4438 | assert_param(IS_I2C_TRANSFER_OPTIONS_REQUEST(XferOptions)); |
|
- | 4439 | ||
- | 4440 | if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) |
|
- | 4441 | { |
|
- | 4442 | if ((pData == NULL) || (Size == 0U)) |
|
- | 4443 | { |
|
- | 4444 | return HAL_ERROR; |
|
- | 4445 | } |
|
- | 4446 | ||
- | 4447 | /* Process Locked */ |
|
- | 4448 | __HAL_LOCK(hi2c); |
|
- | 4449 | ||
- | 4450 | /* Disable Interrupts, to prevent preemption during treatment in case of multicall */ |
|
- | 4451 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
|
- | 4452 | ||
- | 4453 | /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */ |
|
- | 4454 | /* and then toggle the HAL slave RX state to TX state */ |
|
- | 4455 | if (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN) |
|
- | 4456 | { |
|
- | 4457 | if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
|
- | 4458 | { |
|
- | 4459 | /* Abort DMA Xfer if any */ |
|
- | 4460 | if (hi2c->hdmarx != NULL) |
|
- | 4461 | { |
|
- | 4462 | CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 4463 | ||
- | 4464 | /* Set the I2C DMA Abort callback : |
|
- | 4465 | will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ |
|
- | 4466 | hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort; |
|
- | 4467 | ||
- | 4468 | /* Abort DMA RX */ |
|
- | 4469 | if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK) |
|
- | 4470 | { |
|
- | 4471 | /* Call Directly XferAbortCallback function in case of error */ |
|
- | 4472 | hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx); |
|
- | 4473 | } |
|
- | 4474 | } |
|
- | 4475 | } |
|
- | 4476 | } |
|
- | 4477 | else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN) |
|
- | 4478 | { |
|
- | 4479 | if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
|
- | 4480 | { |
|
- | 4481 | CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 4482 | ||
- | 4483 | /* Abort DMA Xfer if any */ |
|
- | 4484 | if (hi2c->hdmatx != NULL) |
|
- | 4485 | { |
|
- | 4486 | /* Set the I2C DMA Abort callback : |
|
- | 4487 | will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ |
|
- | 4488 | hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort; |
|
- | 4489 | ||
- | 4490 | /* Abort DMA TX */ |
|
- | 4491 | if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK) |
|
- | 4492 | { |
|
- | 4493 | /* Call Directly XferAbortCallback function in case of error */ |
|
- | 4494 | hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx); |
|
- | 4495 | } |
|
- | 4496 | } |
|
- | 4497 | } |
|
- | 4498 | } |
|
- | 4499 | else |
|
- | 4500 | { |
|
- | 4501 | /* Nothing to do */ |
|
- | 4502 | } |
|
- | 4503 | ||
- | 4504 | /* Check if the I2C is already enabled */ |
|
- | 4505 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
|
- | 4506 | { |
|
- | 4507 | /* Enable I2C peripheral */ |
|
- | 4508 | __HAL_I2C_ENABLE(hi2c); |
|
- | 4509 | } |
|
- | 4510 | ||
- | 4511 | /* Disable Pos */ |
|
- | 4512 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
|
- | 4513 | ||
- | 4514 | hi2c->State = HAL_I2C_STATE_BUSY_RX_LISTEN; |
|
- | 4515 | hi2c->Mode = HAL_I2C_MODE_SLAVE; |
|
- | 4516 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
|
- | 4517 | ||
- | 4518 | /* Prepare transfer parameters */ |
|
- | 4519 | hi2c->pBuffPtr = pData; |
|
- | 4520 | hi2c->XferCount = Size; |
|
- | 4521 | hi2c->XferSize = hi2c->XferCount; |
|
- | 4522 | hi2c->XferOptions = XferOptions; |
|
- | 4523 | ||
- | 4524 | /* Set the I2C DMA transfer complete callback */ |
|
- | 4525 | hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt; |
|
- | 4526 | ||
- | 4527 | /* Set the DMA error callback */ |
|
- | 4528 | hi2c->hdmarx->XferErrorCallback = I2C_DMAError; |
|
- | 4529 | ||
- | 4530 | /* Set the unused DMA callbacks to NULL */ |
|
- | 4531 | hi2c->hdmarx->XferHalfCpltCallback = NULL; |
|
- | 4532 | hi2c->hdmarx->XferAbortCallback = NULL; |
|
- | 4533 | ||
- | 4534 | /* Enable the DMA channel */ |
|
- | 4535 | dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize); |
|
- | 4536 | ||
- | 4537 | if (dmaxferstatus == HAL_OK) |
|
- | 4538 | { |
|
- | 4539 | /* Enable Address Acknowledge */ |
|
- | 4540 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 4541 | ||
- | 4542 | /* Clear ADDR flag */ |
|
- | 4543 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
|
- | 4544 | ||
- | 4545 | /* Process Unlocked */ |
|
- | 4546 | __HAL_UNLOCK(hi2c); |
|
- | 4547 | ||
- | 4548 | /* Enable DMA Request */ |
|
- | 4549 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 4550 | ||
- | 4551 | /* Note : The I2C interrupts must be enabled after unlocking current process |
|
- | 4552 | to avoid the risk of I2C interrupt handle execution before current |
|
- | 4553 | process unlock */ |
|
- | 4554 | /* Enable EVT and ERR interrupt */ |
|
- | 4555 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
|
- | 4556 | ||
- | 4557 | return HAL_OK; |
|
- | 4558 | } |
|
- | 4559 | else |
|
- | 4560 | { |
|
- | 4561 | /* Update I2C state */ |
|
- | 4562 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 4563 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 4564 | ||
- | 4565 | /* Update I2C error code */ |
|
- | 4566 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
|
- | 4567 | ||
- | 4568 | /* Process Unlocked */ |
|
- | 4569 | __HAL_UNLOCK(hi2c); |
|
- | 4570 | ||
- | 4571 | return HAL_ERROR; |
|
- | 4572 | } |
|
- | 4573 | } |
|
- | 4574 | else |
|
- | 4575 | { |
|
- | 4576 | return HAL_BUSY; |
|
- | 4577 | } |
|
- | 4578 | } |
|
- | 4579 | ||
- | 4580 | /** |
|
- | 4581 | * @brief Enable the Address listen mode with Interrupt. |
|
- | 4582 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 4583 | * the configuration information for the specified I2C. |
|
- | 4584 | * @retval HAL status |
|
- | 4585 | */ |
|
- | 4586 | HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c) |
|
- | 4587 | { |
|
- | 4588 | if (hi2c->State == HAL_I2C_STATE_READY) |
|
- | 4589 | { |
|
- | 4590 | hi2c->State = HAL_I2C_STATE_LISTEN; |
|
- | 4591 | ||
- | 4592 | /* Check if the I2C is already enabled */ |
|
- | 4593 | if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE) |
|
- | 4594 | { |
|
- | 4595 | /* Enable I2C peripheral */ |
|
- | 4596 | __HAL_I2C_ENABLE(hi2c); |
|
- | 4597 | } |
|
- | 4598 | ||
- | 4599 | /* Enable Address Acknowledge */ |
|
- | 4600 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 4601 | ||
- | 4602 | /* Enable EVT and ERR interrupt */ |
|
- | 4603 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
|
- | 4604 | ||
- | 4605 | return HAL_OK; |
|
- | 4606 | } |
|
- | 4607 | else |
|
- | 4608 | { |
|
- | 4609 | return HAL_BUSY; |
|
- | 4610 | } |
|
- | 4611 | } |
|
- | 4612 | ||
- | 4613 | /** |
|
- | 4614 | * @brief Disable the Address listen mode with Interrupt. |
|
- | 4615 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 4616 | * the configuration information for the specified I2C. |
|
- | 4617 | * @retval HAL status |
|
- | 4618 | */ |
|
- | 4619 | HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c) |
|
- | 4620 | { |
|
- | 4621 | /* Declaration of tmp to prevent undefined behavior of volatile usage */ |
|
- | 4622 | uint32_t tmp; |
|
- | 4623 | ||
- | 4624 | /* Disable Address listen mode only if a transfer is not ongoing */ |
|
- | 4625 | if (hi2c->State == HAL_I2C_STATE_LISTEN) |
|
- | 4626 | { |
|
- | 4627 | tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK; |
|
- | 4628 | hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode); |
|
- | 4629 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 4630 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 4631 | ||
- | 4632 | /* Disable Address Acknowledge */ |
|
- | 4633 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 4634 | ||
- | 4635 | /* Disable EVT and ERR interrupt */ |
|
- | 4636 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
|
- | 4637 | ||
- | 4638 | return HAL_OK; |
|
- | 4639 | } |
|
- | 4640 | else |
|
- | 4641 | { |
|
- | 4642 | return HAL_BUSY; |
|
- | 4643 | } |
|
- | 4644 | } |
|
- | 4645 | ||
- | 4646 | /** |
|
- | 4647 | * @brief Abort a master I2C IT or DMA process communication with Interrupt. |
|
- | 4648 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 4649 | * the configuration information for the specified I2C. |
|
- | 4650 | * @param DevAddress Target device address: The device 7 bits address value |
|
- | 4651 | * in datasheet must be shifted to the left before calling the interface |
|
- | 4652 | * @retval HAL status |
|
- | 4653 | */ |
|
- | 4654 | HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress) |
|
- | 4655 | { |
|
- | 4656 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
|
- | 4657 | HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode; |
|
- | 4658 | ||
- | 4659 | /* Prevent unused argument(s) compilation warning */ |
|
- | 4660 | UNUSED(DevAddress); |
|
- | 4661 | ||
- | 4662 | /* Abort Master transfer during Receive or Transmit process */ |
|
- | 4663 | if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && (CurrentMode == HAL_I2C_MODE_MASTER)) |
|
- | 4664 | { |
|
- | 4665 | /* Process Locked */ |
|
- | 4666 | __HAL_LOCK(hi2c); |
|
- | 4667 | ||
- | 4668 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 4669 | hi2c->State = HAL_I2C_STATE_ABORT; |
|
- | 4670 | ||
- | 4671 | /* Disable Acknowledge */ |
|
- | 4672 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 4673 | ||
- | 4674 | /* Generate Stop */ |
|
- | 4675 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
|
- | 4676 | ||
- | 4677 | hi2c->XferCount = 0U; |
|
- | 4678 | ||
- | 4679 | /* Disable EVT, BUF and ERR interrupt */ |
|
- | 4680 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
|
- | 4681 | ||
- | 4682 | /* Process Unlocked */ |
|
- | 4683 | __HAL_UNLOCK(hi2c); |
|
- | 4684 | ||
- | 4685 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
|
- | 4686 | I2C_ITError(hi2c); |
|
- | 4687 | ||
- | 4688 | return HAL_OK; |
|
- | 4689 | } |
|
- | 4690 | else |
|
- | 4691 | { |
|
- | 4692 | /* Wrong usage of abort function */ |
|
- | 4693 | /* This function should be used only in case of abort monitored by master device */ |
|
- | 4694 | /* Or periphal is not in busy state, mean there is no active sequence to be abort */ |
|
- | 4695 | return HAL_ERROR; |
|
- | 4696 | } |
|
- | 4697 | } |
|
- | 4698 | ||
- | 4699 | /** |
|
- | 4700 | * @} |
|
- | 4701 | */ |
|
- | 4702 | ||
- | 4703 | /** @defgroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks |
|
- | 4704 | * @{ |
|
- | 4705 | */ |
|
- | 4706 | ||
- | 4707 | /** |
|
3420 | * @brief This function handles I2C event interrupt request. |
4708 | * @brief This function handles I2C event interrupt request. |
3421 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4709 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
3422 | * the configuration information for the specified I2C. |
4710 | * the configuration information for the specified I2C. |
3423 | * @retval None |
4711 | * @retval None |
3424 | */ |
4712 | */ |
3425 | void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c) |
4713 | void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c) |
3426 | { |
4714 | { |
- | 4715 | uint32_t sr1itflags; |
|
3427 | uint32_t sr2itflags = READ_REG(hi2c->Instance->SR2); |
4716 | uint32_t sr2itflags = 0U; |
3428 | uint32_t sr1itflags = READ_REG(hi2c->Instance->SR1); |
4717 | uint32_t itsources = READ_REG(hi2c->Instance->CR2); |
3429 | uint32_t itsources = READ_REG(hi2c->Instance->CR2); |
4718 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
3430 | - | ||
3431 | uint32_t CurrentMode = hi2c->Mode; |
4719 | HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode; |
- | 4720 | HAL_I2C_StateTypeDef CurrentState = hi2c->State; |
|
3432 | 4721 | ||
3433 | /* Master or Memory mode selected */ |
4722 | /* Master or Memory mode selected */ |
3434 | if((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM)) |
4723 | if ((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM)) |
3435 | { |
4724 | { |
- | 4725 | sr2itflags = READ_REG(hi2c->Instance->SR2); |
|
- | 4726 | sr1itflags = READ_REG(hi2c->Instance->SR1); |
|
- | 4727 | ||
- | 4728 | /* Exit IRQ event until Start Bit detected in case of Other frame requested */ |
|
- | 4729 | if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_SB) == RESET) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(CurrentXferOptions) == 1U)) |
|
- | 4730 | { |
|
- | 4731 | return; |
|
- | 4732 | } |
|
- | 4733 | ||
3436 | /* SB Set ----------------------------------------------------------------*/ |
4734 | /* SB Set ----------------------------------------------------------------*/ |
3437 | if(((sr1itflags & I2C_FLAG_SB) != RESET) && ((itsources & I2C_IT_EVT) != RESET)) |
4735 | if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_SB) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET)) |
3438 | { |
4736 | { |
- | 4737 | /* Convert OTHER_xxx XferOptions if any */ |
|
- | 4738 | I2C_ConvertOtherXferOptions(hi2c); |
|
- | 4739 | ||
3439 | I2C_Master_SB(hi2c); |
4740 | I2C_Master_SB(hi2c); |
3440 | } |
4741 | } |
3441 | /* ADD10 Set -------------------------------------------------------------*/ |
4742 | /* ADD10 Set -------------------------------------------------------------*/ |
3442 | else if(((sr1itflags & I2C_FLAG_ADD10) != RESET) && ((itsources & I2C_IT_EVT) != RESET)) |
4743 | else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADD10) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET)) |
3443 | { |
4744 | { |
3444 | I2C_Master_ADD10(hi2c); |
4745 | I2C_Master_ADD10(hi2c); |
3445 | } |
4746 | } |
3446 | /* ADDR Set --------------------------------------------------------------*/ |
4747 | /* ADDR Set --------------------------------------------------------------*/ |
3447 | else if(((sr1itflags & I2C_FLAG_ADDR) != RESET) && ((itsources & I2C_IT_EVT) != RESET)) |
4748 | else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET)) |
3448 | { |
4749 | { |
3449 | I2C_Master_ADDR(hi2c); |
4750 | I2C_Master_ADDR(hi2c); |
3450 | } |
4751 | } |
3451 | - | ||
3452 | /* I2C in mode Transmitter -----------------------------------------------*/ |
4752 | /* I2C in mode Transmitter -----------------------------------------------*/ |
3453 | if((sr2itflags & I2C_FLAG_TRA) != RESET) |
4753 | else if (I2C_CHECK_FLAG(sr2itflags, I2C_FLAG_TRA) != RESET) |
3454 | { |
4754 | { |
3455 | /* TXE set and BTF reset -----------------------------------------------*/ |
4755 | /* Do not check buffer and BTF flag if a Xfer DMA is on going */ |
3456 | if(((sr1itflags & I2C_FLAG_TXE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET)) |
4756 | if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN) |
3457 | { |
4757 | { |
- | 4758 | /* TXE set and BTF reset -----------------------------------------------*/ |
|
- | 4759 | if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_TXE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET)) |
|
- | 4760 | { |
|
3458 | I2C_MasterTransmit_TXE(hi2c); |
4761 | I2C_MasterTransmit_TXE(hi2c); |
3459 | } |
4762 | } |
3460 | /* BTF set -------------------------------------------------------------*/ |
4763 | /* BTF set -------------------------------------------------------------*/ |
3461 | else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET)) |
4764 | else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET)) |
3462 | { |
4765 | { |
- | 4766 | if (CurrentMode == HAL_I2C_MODE_MASTER) |
|
- | 4767 | { |
|
3463 | I2C_MasterTransmit_BTF(hi2c); |
4768 | I2C_MasterTransmit_BTF(hi2c); |
- | 4769 | } |
|
- | 4770 | else /* HAL_I2C_MODE_MEM */ |
|
- | 4771 | { |
|
- | 4772 | I2C_MemoryTransmit_TXE_BTF(hi2c); |
|
- | 4773 | } |
|
- | 4774 | } |
|
- | 4775 | else |
|
- | 4776 | { |
|
- | 4777 | /* Do nothing */ |
|
- | 4778 | } |
|
3464 | } |
4779 | } |
3465 | } |
4780 | } |
3466 | /* I2C in mode Receiver --------------------------------------------------*/ |
4781 | /* I2C in mode Receiver --------------------------------------------------*/ |
3467 | else |
4782 | else |
3468 | { |
4783 | { |
3469 | /* RXNE set and BTF reset -----------------------------------------------*/ |
4784 | /* Do not check buffer and BTF flag if a Xfer DMA is on going */ |
3470 | if(((sr1itflags & I2C_FLAG_RXNE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET)) |
- | |
3471 | { |
- | |
3472 | I2C_MasterReceive_RXNE(hi2c); |
- | |
3473 | } |
- | |
3474 | /* BTF set -------------------------------------------------------------*/ |
- | |
3475 | else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET)) |
4785 | if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN) |
3476 | { |
4786 | { |
- | 4787 | /* RXNE set and BTF reset -----------------------------------------------*/ |
|
- | 4788 | if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET)) |
|
- | 4789 | { |
|
- | 4790 | I2C_MasterReceive_RXNE(hi2c); |
|
- | 4791 | } |
|
- | 4792 | /* BTF set -------------------------------------------------------------*/ |
|
- | 4793 | else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET)) |
|
- | 4794 | { |
|
3477 | I2C_MasterReceive_BTF(hi2c); |
4795 | I2C_MasterReceive_BTF(hi2c); |
- | 4796 | } |
|
- | 4797 | else |
|
- | 4798 | { |
|
- | 4799 | /* Do nothing */ |
|
- | 4800 | } |
|
3478 | } |
4801 | } |
3479 | } |
4802 | } |
3480 | } |
4803 | } |
3481 | /* Slave mode selected */ |
4804 | /* Slave mode selected */ |
3482 | else |
4805 | else |
3483 | { |
4806 | { |
- | 4807 | /* If an error is detected, read only SR1 register to prevent */ |
|
- | 4808 | /* a clear of ADDR flags by reading SR2 after reading SR1 in Error treatment */ |
|
- | 4809 | if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE) |
|
- | 4810 | { |
|
- | 4811 | sr1itflags = READ_REG(hi2c->Instance->SR1); |
|
- | 4812 | } |
|
- | 4813 | else |
|
- | 4814 | { |
|
- | 4815 | sr2itflags = READ_REG(hi2c->Instance->SR2); |
|
- | 4816 | sr1itflags = READ_REG(hi2c->Instance->SR1); |
|
- | 4817 | } |
|
- | 4818 | ||
3484 | /* ADDR set --------------------------------------------------------------*/ |
4819 | /* ADDR set --------------------------------------------------------------*/ |
3485 | if(((sr1itflags & I2C_FLAG_ADDR) != RESET) && ((itsources & I2C_IT_EVT) != RESET)) |
4820 | if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET)) |
3486 | { |
4821 | { |
- | 4822 | /* Now time to read SR2, this will clear ADDR flag automatically */ |
|
- | 4823 | if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE) |
|
- | 4824 | { |
|
- | 4825 | sr2itflags = READ_REG(hi2c->Instance->SR2); |
|
- | 4826 | } |
|
3487 | I2C_Slave_ADDR(hi2c); |
4827 | I2C_Slave_ADDR(hi2c, sr2itflags); |
3488 | } |
4828 | } |
3489 | /* STOPF set --------------------------------------------------------------*/ |
4829 | /* STOPF set --------------------------------------------------------------*/ |
3490 | else if(((sr1itflags & I2C_FLAG_STOPF) != RESET) && ((itsources & I2C_IT_EVT) != RESET)) |
4830 | else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET)) |
3491 | { |
4831 | { |
3492 | I2C_Slave_STOPF(hi2c); |
4832 | I2C_Slave_STOPF(hi2c); |
3493 | } |
4833 | } |
3494 | /* I2C in mode Transmitter -----------------------------------------------*/ |
4834 | /* I2C in mode Transmitter -----------------------------------------------*/ |
3495 | else if((sr2itflags & I2C_FLAG_TRA) != RESET) |
4835 | else if ((CurrentState == HAL_I2C_STATE_BUSY_TX) || (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN)) |
3496 | { |
4836 | { |
3497 | /* TXE set and BTF reset -----------------------------------------------*/ |
4837 | /* TXE set and BTF reset -----------------------------------------------*/ |
3498 | if(((sr1itflags & I2C_FLAG_TXE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET)) |
4838 | if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_TXE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET)) |
3499 | { |
4839 | { |
3500 | I2C_SlaveTransmit_TXE(hi2c); |
4840 | I2C_SlaveTransmit_TXE(hi2c); |
3501 | } |
4841 | } |
3502 | /* BTF set -------------------------------------------------------------*/ |
4842 | /* BTF set -------------------------------------------------------------*/ |
3503 | else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET)) |
4843 | else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET)) |
3504 | { |
4844 | { |
3505 | I2C_SlaveTransmit_BTF(hi2c); |
4845 | I2C_SlaveTransmit_BTF(hi2c); |
3506 | } |
4846 | } |
- | 4847 | else |
|
- | 4848 | { |
|
- | 4849 | /* Do nothing */ |
|
- | 4850 | } |
|
3507 | } |
4851 | } |
3508 | /* I2C in mode Receiver --------------------------------------------------*/ |
4852 | /* I2C in mode Receiver --------------------------------------------------*/ |
3509 | else |
4853 | else |
3510 | { |
4854 | { |
3511 | /* RXNE set and BTF reset ----------------------------------------------*/ |
4855 | /* RXNE set and BTF reset ----------------------------------------------*/ |
3512 | if(((sr1itflags & I2C_FLAG_RXNE) != RESET) && ((itsources & I2C_IT_BUF) != RESET) && ((sr1itflags & I2C_FLAG_BTF) == RESET)) |
4856 | if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET)) |
3513 | { |
4857 | { |
3514 | I2C_SlaveReceive_RXNE(hi2c); |
4858 | I2C_SlaveReceive_RXNE(hi2c); |
3515 | } |
4859 | } |
3516 | /* BTF set -------------------------------------------------------------*/ |
4860 | /* BTF set -------------------------------------------------------------*/ |
3517 | else if(((sr1itflags & I2C_FLAG_BTF) != RESET) && ((itsources & I2C_IT_EVT) != RESET)) |
4861 | else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET)) |
3518 | { |
4862 | { |
3519 | I2C_SlaveReceive_BTF(hi2c); |
4863 | I2C_SlaveReceive_BTF(hi2c); |
3520 | } |
4864 | } |
- | 4865 | else |
|
- | 4866 | { |
|
- | 4867 | /* Do nothing */ |
|
- | 4868 | } |
|
3521 | } |
4869 | } |
3522 | } |
4870 | } |
3523 | } |
4871 | } |
3524 | 4872 | ||
3525 | /** |
4873 | /** |
Line 3528... | Line 4876... | ||
3528 | * the configuration information for the specified I2C. |
4876 | * the configuration information for the specified I2C. |
3529 | * @retval None |
4877 | * @retval None |
3530 | */ |
4878 | */ |
3531 | void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c) |
4879 | void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c) |
3532 | { |
4880 | { |
- | 4881 | HAL_I2C_ModeTypeDef tmp1; |
|
- | 4882 | uint32_t tmp2; |
|
3533 | uint32_t tmp1 = 0U, tmp2 = 0U, tmp3 = 0U, tmp4 = 0U; |
4883 | HAL_I2C_StateTypeDef tmp3; |
- | 4884 | uint32_t tmp4; |
|
3534 | uint32_t sr1itflags = READ_REG(hi2c->Instance->SR1); |
4885 | uint32_t sr1itflags = READ_REG(hi2c->Instance->SR1); |
3535 | uint32_t itsources = READ_REG(hi2c->Instance->CR2); |
4886 | uint32_t itsources = READ_REG(hi2c->Instance->CR2); |
- | 4887 | uint32_t error = HAL_I2C_ERROR_NONE; |
|
- | 4888 | HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode; |
|
3536 | 4889 | ||
3537 | /* I2C Bus error interrupt occurred ----------------------------------------*/ |
4890 | /* I2C Bus error interrupt occurred ----------------------------------------*/ |
3538 | if(((sr1itflags & I2C_FLAG_BERR) != RESET) && ((itsources & I2C_IT_ERR) != RESET)) |
4891 | if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BERR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET)) |
3539 | { |
4892 | { |
3540 | hi2c->ErrorCode |= HAL_I2C_ERROR_BERR; |
4893 | error |= HAL_I2C_ERROR_BERR; |
3541 | 4894 | ||
3542 | /* Clear BERR flag */ |
4895 | /* Clear BERR flag */ |
3543 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR); |
4896 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR); |
3544 | 4897 | ||
3545 | /* Workaround: Start cannot be generated after a misplaced Stop */ |
4898 | /* Workaround: Start cannot be generated after a misplaced Stop */ |
3546 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_SWRST); |
4899 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_SWRST); |
3547 | } |
4900 | } |
3548 | 4901 | ||
3549 | /* I2C Arbitration Loss error interrupt occurred ---------------------------*/ |
4902 | /* I2C Arbitration Lost error interrupt occurred ---------------------------*/ |
3550 | if(((sr1itflags & I2C_FLAG_ARLO) != RESET) && ((itsources & I2C_IT_ERR) != RESET)) |
4903 | if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ARLO) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET)) |
3551 | { |
4904 | { |
3552 | hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO; |
4905 | error |= HAL_I2C_ERROR_ARLO; |
3553 | 4906 | ||
3554 | /* Clear ARLO flag */ |
4907 | /* Clear ARLO flag */ |
3555 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO); |
4908 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO); |
3556 | } |
4909 | } |
3557 | 4910 | ||
3558 | /* I2C Acknowledge failure error interrupt occurred ------------------------*/ |
4911 | /* I2C Acknowledge failure error interrupt occurred ------------------------*/ |
3559 | if(((sr1itflags & I2C_FLAG_AF) != RESET) && ((itsources & I2C_IT_ERR) != RESET)) |
4912 | if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET)) |
3560 | { |
4913 | { |
3561 | tmp1 = hi2c->Mode; |
4914 | tmp1 = CurrentMode; |
3562 | tmp2 = hi2c->XferCount; |
4915 | tmp2 = hi2c->XferCount; |
3563 | tmp3 = hi2c->State; |
4916 | tmp3 = hi2c->State; |
3564 | tmp4 = hi2c->PreviousState; |
4917 | tmp4 = hi2c->PreviousState; |
3565 | if((tmp1 == HAL_I2C_MODE_SLAVE) && (tmp2 == 0U) && \ |
4918 | if ((tmp1 == HAL_I2C_MODE_SLAVE) && (tmp2 == 0U) && \ |
3566 | ((tmp3 == HAL_I2C_STATE_BUSY_TX) || (tmp3 == HAL_I2C_STATE_BUSY_TX_LISTEN) || \ |
4919 | ((tmp3 == HAL_I2C_STATE_BUSY_TX) || (tmp3 == HAL_I2C_STATE_BUSY_TX_LISTEN) || \ |
3567 | ((tmp3 == HAL_I2C_STATE_LISTEN) && (tmp4 == I2C_STATE_SLAVE_BUSY_TX)))) |
4920 | ((tmp3 == HAL_I2C_STATE_LISTEN) && (tmp4 == I2C_STATE_SLAVE_BUSY_TX)))) |
3568 | { |
4921 | { |
3569 | I2C_Slave_AF(hi2c); |
4922 | I2C_Slave_AF(hi2c); |
3570 | } |
4923 | } |
3571 | else |
4924 | else |
3572 | { |
4925 | { |
- | 4926 | /* Clear AF flag */ |
|
- | 4927 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
|
- | 4928 | ||
3573 | hi2c->ErrorCode |= HAL_I2C_ERROR_AF; |
4929 | error |= HAL_I2C_ERROR_AF; |
3574 | 4930 | ||
3575 | /* Do not generate a STOP in case of Slave receive non acknowledge during transfer (mean not at the end of transfer) */ |
4931 | /* Do not generate a STOP in case of Slave receive non acknowledge during transfer (mean not at the end of transfer) */ |
3576 | if(hi2c->Mode == HAL_I2C_MODE_MASTER) |
4932 | if ((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM)) |
3577 | { |
4933 | { |
3578 | /* Generate Stop */ |
4934 | /* Generate Stop */ |
3579 | SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP); |
4935 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
3580 | } |
4936 | } |
3581 | - | ||
3582 | /* Clear AF flag */ |
- | |
3583 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
- | |
3584 | } |
4937 | } |
3585 | } |
4938 | } |
3586 | 4939 | ||
3587 | /* I2C Over-Run/Under-Run interrupt occurred -------------------------------*/ |
4940 | /* I2C Over-Run/Under-Run interrupt occurred -------------------------------*/ |
3588 | if(((sr1itflags & I2C_FLAG_OVR) != RESET) && ((itsources & I2C_IT_ERR) != RESET)) |
4941 | if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_OVR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET)) |
3589 | { |
4942 | { |
3590 | hi2c->ErrorCode |= HAL_I2C_ERROR_OVR; |
4943 | error |= HAL_I2C_ERROR_OVR; |
3591 | /* Clear OVR flag */ |
4944 | /* Clear OVR flag */ |
3592 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR); |
4945 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR); |
3593 | } |
4946 | } |
3594 | 4947 | ||
3595 | /* Call the Error Callback in case of Error detected -----------------------*/ |
4948 | /* Call the Error Callback in case of Error detected -----------------------*/ |
3596 | if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE) |
4949 | if (error != HAL_I2C_ERROR_NONE) |
3597 | { |
4950 | { |
- | 4951 | hi2c->ErrorCode |= error; |
|
3598 | I2C_ITError(hi2c); |
4952 | I2C_ITError(hi2c); |
3599 | } |
4953 | } |
3600 | } |
4954 | } |
3601 | 4955 | ||
3602 | /** |
4956 | /** |
Line 3609... | Line 4963... | ||
3609 | { |
4963 | { |
3610 | /* Prevent unused argument(s) compilation warning */ |
4964 | /* Prevent unused argument(s) compilation warning */ |
3611 | UNUSED(hi2c); |
4965 | UNUSED(hi2c); |
3612 | 4966 | ||
3613 | /* NOTE : This function should not be modified, when the callback is needed, |
4967 | /* NOTE : This function should not be modified, when the callback is needed, |
3614 | the HAL_I2C_MasterTxCpltCallback can be implemented in the user file |
4968 | the HAL_I2C_MasterTxCpltCallback could be implemented in the user file |
3615 | */ |
4969 | */ |
3616 | } |
4970 | } |
3617 | 4971 | ||
3618 | /** |
4972 | /** |
3619 | * @brief Master Rx Transfer completed callback. |
4973 | * @brief Master Rx Transfer completed callback. |
Line 3625... | Line 4979... | ||
3625 | { |
4979 | { |
3626 | /* Prevent unused argument(s) compilation warning */ |
4980 | /* Prevent unused argument(s) compilation warning */ |
3627 | UNUSED(hi2c); |
4981 | UNUSED(hi2c); |
3628 | 4982 | ||
3629 | /* NOTE : This function should not be modified, when the callback is needed, |
4983 | /* NOTE : This function should not be modified, when the callback is needed, |
3630 | the HAL_I2C_MasterRxCpltCallback can be implemented in the user file |
4984 | the HAL_I2C_MasterRxCpltCallback could be implemented in the user file |
3631 | */ |
4985 | */ |
3632 | } |
4986 | } |
3633 | 4987 | ||
3634 | /** @brief Slave Tx Transfer completed callback. |
4988 | /** @brief Slave Tx Transfer completed callback. |
3635 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4989 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
Line 3640... | Line 4994... | ||
3640 | { |
4994 | { |
3641 | /* Prevent unused argument(s) compilation warning */ |
4995 | /* Prevent unused argument(s) compilation warning */ |
3642 | UNUSED(hi2c); |
4996 | UNUSED(hi2c); |
3643 | 4997 | ||
3644 | /* NOTE : This function should not be modified, when the callback is needed, |
4998 | /* NOTE : This function should not be modified, when the callback is needed, |
3645 | the HAL_I2C_SlaveTxCpltCallback can be implemented in the user file |
4999 | the HAL_I2C_SlaveTxCpltCallback could be implemented in the user file |
3646 | */ |
5000 | */ |
3647 | } |
5001 | } |
3648 | 5002 | ||
3649 | /** |
5003 | /** |
3650 | * @brief Slave Rx Transfer completed callback. |
5004 | * @brief Slave Rx Transfer completed callback. |
Line 3656... | Line 5010... | ||
3656 | { |
5010 | { |
3657 | /* Prevent unused argument(s) compilation warning */ |
5011 | /* Prevent unused argument(s) compilation warning */ |
3658 | UNUSED(hi2c); |
5012 | UNUSED(hi2c); |
3659 | 5013 | ||
3660 | /* NOTE : This function should not be modified, when the callback is needed, |
5014 | /* NOTE : This function should not be modified, when the callback is needed, |
3661 | the HAL_I2C_SlaveRxCpltCallback can be implemented in the user file |
5015 | the HAL_I2C_SlaveRxCpltCallback could be implemented in the user file |
3662 | */ |
5016 | */ |
3663 | } |
5017 | } |
3664 | 5018 | ||
3665 | /** |
5019 | /** |
3666 | * @brief Slave Address Match callback. |
5020 | * @brief Slave Address Match callback. |
3667 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5021 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
3668 | * the configuration information for the specified I2C. |
5022 | * the configuration information for the specified I2C. |
3669 | * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XferOptions_definition |
5023 | * @param TransferDirection Master request Transfer Direction (Write/Read), value of @ref I2C_XferDirection_definition |
3670 | * @param AddrMatchCode Address Match Code |
5024 | * @param AddrMatchCode Address Match Code |
3671 | * @retval None |
5025 | * @retval None |
3672 | */ |
5026 | */ |
3673 | __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode) |
5027 | __weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode) |
3674 | { |
5028 | { |
Line 3676... | Line 5030... | ||
3676 | UNUSED(hi2c); |
5030 | UNUSED(hi2c); |
3677 | UNUSED(TransferDirection); |
5031 | UNUSED(TransferDirection); |
3678 | UNUSED(AddrMatchCode); |
5032 | UNUSED(AddrMatchCode); |
3679 | 5033 | ||
3680 | /* NOTE : This function should not be modified, when the callback is needed, |
5034 | /* NOTE : This function should not be modified, when the callback is needed, |
3681 | the HAL_I2C_AddrCallback can be implemented in the user file |
5035 | the HAL_I2C_AddrCallback() could be implemented in the user file |
3682 | */ |
5036 | */ |
3683 | } |
5037 | } |
3684 | 5038 | ||
3685 | /** |
5039 | /** |
3686 | * @brief Listen Complete callback. |
5040 | * @brief Listen Complete callback. |
Line 3691... | Line 5045... | ||
3691 | __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c) |
5045 | __weak void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c) |
3692 | { |
5046 | { |
3693 | /* Prevent unused argument(s) compilation warning */ |
5047 | /* Prevent unused argument(s) compilation warning */ |
3694 | UNUSED(hi2c); |
5048 | UNUSED(hi2c); |
3695 | 5049 | ||
3696 | /* NOTE : This function should not be modified, when the callback is needed, |
5050 | /* NOTE : This function should not be modified, when the callback is needed, |
3697 | the HAL_I2C_ListenCpltCallback can be implemented in the user file |
5051 | the HAL_I2C_ListenCpltCallback() could be implemented in the user file |
3698 | */ |
5052 | */ |
3699 | } |
5053 | } |
3700 | 5054 | ||
3701 | /** |
5055 | /** |
3702 | * @brief Memory Tx Transfer completed callback. |
5056 | * @brief Memory Tx Transfer completed callback. |
3703 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5057 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
Line 3708... | Line 5062... | ||
3708 | { |
5062 | { |
3709 | /* Prevent unused argument(s) compilation warning */ |
5063 | /* Prevent unused argument(s) compilation warning */ |
3710 | UNUSED(hi2c); |
5064 | UNUSED(hi2c); |
3711 | 5065 | ||
3712 | /* NOTE : This function should not be modified, when the callback is needed, |
5066 | /* NOTE : This function should not be modified, when the callback is needed, |
3713 | the HAL_I2C_MemTxCpltCallback can be implemented in the user file |
5067 | the HAL_I2C_MemTxCpltCallback could be implemented in the user file |
3714 | */ |
5068 | */ |
3715 | } |
5069 | } |
3716 | 5070 | ||
3717 | /** |
5071 | /** |
3718 | * @brief Memory Rx Transfer completed callback. |
5072 | * @brief Memory Rx Transfer completed callback. |
Line 3724... | Line 5078... | ||
3724 | { |
5078 | { |
3725 | /* Prevent unused argument(s) compilation warning */ |
5079 | /* Prevent unused argument(s) compilation warning */ |
3726 | UNUSED(hi2c); |
5080 | UNUSED(hi2c); |
3727 | 5081 | ||
3728 | /* NOTE : This function should not be modified, when the callback is needed, |
5082 | /* NOTE : This function should not be modified, when the callback is needed, |
3729 | the HAL_I2C_MemRxCpltCallback can be implemented in the user file |
5083 | the HAL_I2C_MemRxCpltCallback could be implemented in the user file |
3730 | */ |
5084 | */ |
3731 | } |
5085 | } |
3732 | 5086 | ||
3733 | /** |
5087 | /** |
3734 | * @brief I2C error callback. |
5088 | * @brief I2C error callback. |
Line 3740... | Line 5094... | ||
3740 | { |
5094 | { |
3741 | /* Prevent unused argument(s) compilation warning */ |
5095 | /* Prevent unused argument(s) compilation warning */ |
3742 | UNUSED(hi2c); |
5096 | UNUSED(hi2c); |
3743 | 5097 | ||
3744 | /* NOTE : This function should not be modified, when the callback is needed, |
5098 | /* NOTE : This function should not be modified, when the callback is needed, |
3745 | the HAL_I2C_ErrorCallback can be implemented in the user file |
5099 | the HAL_I2C_ErrorCallback could be implemented in the user file |
3746 | */ |
5100 | */ |
3747 | } |
5101 | } |
3748 | 5102 | ||
3749 | /** |
5103 | /** |
3750 | * @brief I2C abort callback. |
5104 | * @brief I2C abort callback. |
Line 3765... | Line 5119... | ||
3765 | /** |
5119 | /** |
3766 | * @} |
5120 | * @} |
3767 | */ |
5121 | */ |
3768 | 5122 | ||
3769 | /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions |
5123 | /** @defgroup I2C_Exported_Functions_Group3 Peripheral State, Mode and Error functions |
3770 | * @brief Peripheral State and Errors functions |
5124 | * @brief Peripheral State, Mode and Error functions |
3771 | * |
5125 | * |
3772 | @verbatim |
5126 | @verbatim |
3773 | =============================================================================== |
5127 | =============================================================================== |
3774 | ##### Peripheral State, Mode and Error functions ##### |
5128 | ##### Peripheral State, Mode and Error functions ##### |
3775 | =============================================================================== |
5129 | =============================================================================== |
3776 | [..] |
5130 | [..] |
3777 | This subsection permits to get in run-time the status of the peripheral |
5131 | This subsection permit to get in run-time the status of the peripheral |
3778 | and the data flow. |
5132 | and the data flow. |
3779 | 5133 | ||
3780 | @endverbatim |
5134 | @endverbatim |
3781 | * @{ |
5135 | * @{ |
3782 | */ |
5136 | */ |
Line 3792... | Line 5146... | ||
3792 | /* Return I2C handle state */ |
5146 | /* Return I2C handle state */ |
3793 | return hi2c->State; |
5147 | return hi2c->State; |
3794 | } |
5148 | } |
3795 | 5149 | ||
3796 | /** |
5150 | /** |
3797 | * @brief Return the I2C Master, Slave, Memory or no mode. |
5151 | * @brief Returns the I2C Master, Slave, Memory or no mode. |
3798 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5152 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
3799 | * the configuration information for I2C module |
5153 | * the configuration information for I2C module |
3800 | * @retval HAL mode |
5154 | * @retval HAL mode |
3801 | */ |
5155 | */ |
3802 | HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c) |
5156 | HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c) |
3803 | { |
5157 | { |
3804 | return hi2c->Mode; |
5158 | return hi2c->Mode; |
3805 | } |
5159 | } |
3806 | 5160 | ||
3807 | /** |
5161 | /** |
3808 | * @brief Return the I2C error code |
5162 | * @brief Return the I2C error code. |
3809 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5163 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
3810 | * the configuration information for the specified I2C. |
5164 | * the configuration information for the specified I2C. |
3811 | * @retval I2C Error Code |
5165 | * @retval I2C Error Code |
3812 | */ |
5166 | */ |
3813 | uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c) |
5167 | uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c) |
Line 3818... | Line 5172... | ||
3818 | /** |
5172 | /** |
3819 | * @} |
5173 | * @} |
3820 | */ |
5174 | */ |
3821 | 5175 | ||
3822 | /** |
5176 | /** |
- | 5177 | * @} |
|
- | 5178 | */ |
|
- | 5179 | ||
- | 5180 | /** @addtogroup I2C_Private_Functions |
|
- | 5181 | * @{ |
|
- | 5182 | */ |
|
- | 5183 | ||
- | 5184 | /** |
|
3823 | * @brief Handle TXE flag for Master |
5185 | * @brief Handle TXE flag for Master |
3824 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5186 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
3825 | * the configuration information for I2C module |
5187 | * the configuration information for I2C module |
3826 | * @retval HAL status |
5188 | * @retval None |
3827 | */ |
5189 | */ |
3828 | static HAL_StatusTypeDef I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c) |
5190 | static void I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c) |
3829 | { |
5191 | { |
3830 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
5192 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
3831 | uint32_t CurrentState = hi2c->State; |
5193 | HAL_I2C_StateTypeDef CurrentState = hi2c->State; |
3832 | uint32_t CurrentMode = hi2c->Mode; |
5194 | HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode; |
3833 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
5195 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
3834 | 5196 | ||
3835 | if((hi2c->XferSize == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX)) |
5197 | if ((hi2c->XferSize == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX)) |
3836 | { |
5198 | { |
3837 | /* Call TxCpltCallback() directly if no stop mode is set */ |
5199 | /* Call TxCpltCallback() directly if no stop mode is set */ |
3838 | if((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME)) |
5200 | if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME)) |
3839 | { |
5201 | { |
3840 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
5202 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
3841 | 5203 | ||
3842 | hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX; |
5204 | hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX; |
3843 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5205 | hi2c->Mode = HAL_I2C_MODE_NONE; |
3844 | hi2c->State = HAL_I2C_STATE_READY; |
5206 | hi2c->State = HAL_I2C_STATE_READY; |
3845 | 5207 | ||
- | 5208 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5209 | hi2c->MasterTxCpltCallback(hi2c); |
|
- | 5210 | #else |
|
3846 | HAL_I2C_MasterTxCpltCallback(hi2c); |
5211 | HAL_I2C_MasterTxCpltCallback(hi2c); |
- | 5212 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
3847 | } |
5213 | } |
3848 | else /* Generate Stop condition then Call TxCpltCallback() */ |
5214 | else /* Generate Stop condition then Call TxCpltCallback() */ |
3849 | { |
5215 | { |
3850 | /* Disable EVT, BUF and ERR interrupt */ |
5216 | /* Disable EVT, BUF and ERR interrupt */ |
3851 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
5217 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
3852 | 5218 | ||
3853 | /* Generate Stop */ |
5219 | /* Generate Stop */ |
3854 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
5220 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
3855 | 5221 | ||
3856 | hi2c->PreviousState = I2C_STATE_NONE; |
5222 | hi2c->PreviousState = I2C_STATE_NONE; |
3857 | hi2c->State = HAL_I2C_STATE_READY; |
5223 | hi2c->State = HAL_I2C_STATE_READY; |
3858 | 5224 | ||
3859 | if(hi2c->Mode == HAL_I2C_MODE_MEM) |
5225 | if (hi2c->Mode == HAL_I2C_MODE_MEM) |
3860 | { |
5226 | { |
3861 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5227 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 5228 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5229 | hi2c->MemTxCpltCallback(hi2c); |
|
- | 5230 | #else |
|
3862 | HAL_I2C_MemTxCpltCallback(hi2c); |
5231 | HAL_I2C_MemTxCpltCallback(hi2c); |
- | 5232 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
3863 | } |
5233 | } |
3864 | else |
5234 | else |
3865 | { |
5235 | { |
3866 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5236 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 5237 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5238 | hi2c->MasterTxCpltCallback(hi2c); |
|
- | 5239 | #else |
|
3867 | HAL_I2C_MasterTxCpltCallback(hi2c); |
5240 | HAL_I2C_MasterTxCpltCallback(hi2c); |
- | 5241 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
3868 | } |
5242 | } |
3869 | } |
5243 | } |
3870 | } |
5244 | } |
3871 | else if((CurrentState == HAL_I2C_STATE_BUSY_TX) || \ |
5245 | else if ((CurrentState == HAL_I2C_STATE_BUSY_TX) || \ |
3872 | ((CurrentMode == HAL_I2C_MODE_MEM) && (CurrentState == HAL_I2C_STATE_BUSY_RX))) |
5246 | ((CurrentMode == HAL_I2C_MODE_MEM) && (CurrentState == HAL_I2C_STATE_BUSY_RX))) |
3873 | { |
5247 | { |
3874 | if(hi2c->XferCount == 0U) |
5248 | if (hi2c->XferCount == 0U) |
3875 | { |
5249 | { |
3876 | /* Disable BUF interrupt */ |
5250 | /* Disable BUF interrupt */ |
3877 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF); |
5251 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF); |
3878 | } |
5252 | } |
3879 | else |
5253 | else |
3880 | { |
5254 | { |
3881 | if(hi2c->Mode == HAL_I2C_MODE_MEM) |
5255 | if (hi2c->Mode == HAL_I2C_MODE_MEM) |
3882 | { |
5256 | { |
3883 | if(hi2c->EventCount == 0) |
- | |
3884 | { |
- | |
3885 | /* If Memory address size is 8Bit */ |
- | |
3886 | if(hi2c->MemaddSize == I2C_MEMADD_SIZE_8BIT) |
- | |
3887 | { |
- | |
3888 | /* Send Memory Address */ |
- | |
3889 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress); |
- | |
3890 | - | ||
3891 | hi2c->EventCount += 2; |
- | |
3892 | } |
- | |
3893 | /* If Memory address size is 16Bit */ |
5257 | I2C_MemoryTransmit_TXE_BTF(hi2c); |
3894 | else |
- | |
3895 | { |
- | |
3896 | /* Send MSB of Memory Address */ |
- | |
3897 | hi2c->Instance->DR = I2C_MEM_ADD_MSB(hi2c->Memaddress); |
- | |
3898 | - | ||
3899 | hi2c->EventCount++; |
- | |
3900 | } |
- | |
3901 | } |
- | |
3902 | else if(hi2c->EventCount == 1) |
- | |
3903 | { |
- | |
3904 | /* Send LSB of Memory Address */ |
- | |
3905 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress); |
- | |
3906 | - | ||
3907 | hi2c->EventCount++; |
- | |
3908 | } |
- | |
3909 | else if(hi2c->EventCount == 2) |
- | |
3910 | { |
- | |
3911 | if(hi2c->State == HAL_I2C_STATE_BUSY_RX) |
- | |
3912 | { |
- | |
3913 | /* Generate Restart */ |
- | |
3914 | hi2c->Instance->CR1 |= I2C_CR1_START; |
- | |
3915 | } |
- | |
3916 | else if(hi2c->State == HAL_I2C_STATE_BUSY_TX) |
- | |
3917 | { |
- | |
3918 | /* Write data to DR */ |
- | |
3919 | hi2c->Instance->DR = (*hi2c->pBuffPtr++); |
- | |
3920 | hi2c->XferCount--; |
- | |
3921 | } |
- | |
3922 | } |
- | |
3923 | } |
5258 | } |
3924 | else |
5259 | else |
3925 | { |
5260 | { |
3926 | /* Write data to DR */ |
5261 | /* Write data to DR */ |
3927 | hi2c->Instance->DR = (*hi2c->pBuffPtr++); |
5262 | hi2c->Instance->DR = *hi2c->pBuffPtr; |
- | 5263 | ||
- | 5264 | /* Increment Buffer pointer */ |
|
- | 5265 | hi2c->pBuffPtr++; |
|
- | 5266 | ||
- | 5267 | /* Update counter */ |
|
3928 | hi2c->XferCount--; |
5268 | hi2c->XferCount--; |
3929 | } |
5269 | } |
3930 | } |
5270 | } |
3931 | } |
5271 | } |
- | 5272 | else |
|
- | 5273 | { |
|
3932 | return HAL_OK; |
5274 | /* Do nothing */ |
- | 5275 | } |
|
3933 | } |
5276 | } |
3934 | 5277 | ||
3935 | /** |
5278 | /** |
3936 | * @brief Handle BTF flag for Master transmitter |
5279 | * @brief Handle BTF flag for Master transmitter |
3937 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5280 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
3938 | * the configuration information for I2C module |
5281 | * the configuration information for I2C module |
3939 | * @retval HAL status |
5282 | * @retval None |
3940 | */ |
5283 | */ |
3941 | static HAL_StatusTypeDef I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c) |
5284 | static void I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c) |
3942 | { |
5285 | { |
3943 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
5286 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
3944 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
5287 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
3945 | 5288 | ||
3946 | if(hi2c->State == HAL_I2C_STATE_BUSY_TX) |
5289 | if (hi2c->State == HAL_I2C_STATE_BUSY_TX) |
3947 | { |
5290 | { |
3948 | if(hi2c->XferCount != 0U) |
5291 | if (hi2c->XferCount != 0U) |
3949 | { |
5292 | { |
3950 | /* Write data to DR */ |
5293 | /* Write data to DR */ |
3951 | hi2c->Instance->DR = (*hi2c->pBuffPtr++); |
5294 | hi2c->Instance->DR = *hi2c->pBuffPtr; |
- | 5295 | ||
- | 5296 | /* Increment Buffer pointer */ |
|
- | 5297 | hi2c->pBuffPtr++; |
|
- | 5298 | ||
- | 5299 | /* Update counter */ |
|
3952 | hi2c->XferCount--; |
5300 | hi2c->XferCount--; |
3953 | } |
5301 | } |
3954 | else |
5302 | else |
3955 | { |
5303 | { |
3956 | /* Call TxCpltCallback() directly if no stop mode is set */ |
5304 | /* Call TxCpltCallback() directly if no stop mode is set */ |
3957 | if((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME)) |
5305 | if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME)) |
3958 | { |
5306 | { |
3959 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
5307 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
3960 | 5308 | ||
3961 | hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX; |
5309 | hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX; |
3962 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5310 | hi2c->Mode = HAL_I2C_MODE_NONE; |
3963 | hi2c->State = HAL_I2C_STATE_READY; |
5311 | hi2c->State = HAL_I2C_STATE_READY; |
- | 5312 | ||
- | 5313 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5314 | hi2c->MasterTxCpltCallback(hi2c); |
|
3964 | 5315 | #else |
|
3965 | HAL_I2C_MasterTxCpltCallback(hi2c); |
5316 | HAL_I2C_MasterTxCpltCallback(hi2c); |
- | 5317 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
3966 | } |
5318 | } |
3967 | else /* Generate Stop condition then Call TxCpltCallback() */ |
5319 | else /* Generate Stop condition then Call TxCpltCallback() */ |
3968 | { |
5320 | { |
3969 | /* Disable EVT, BUF and ERR interrupt */ |
5321 | /* Disable EVT, BUF and ERR interrupt */ |
3970 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
5322 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
3971 | 5323 | ||
3972 | /* Generate Stop */ |
5324 | /* Generate Stop */ |
3973 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
5325 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
3974 | 5326 | ||
3975 | hi2c->PreviousState = I2C_STATE_NONE; |
5327 | hi2c->PreviousState = I2C_STATE_NONE; |
3976 | hi2c->State = HAL_I2C_STATE_READY; |
5328 | hi2c->State = HAL_I2C_STATE_READY; |
3977 | - | ||
3978 | if(hi2c->Mode == HAL_I2C_MODE_MEM) |
5329 | hi2c->Mode = HAL_I2C_MODE_NONE; |
3979 | { |
5330 | |
3980 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5331 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
3981 | - | ||
3982 | HAL_I2C_MemTxCpltCallback(hi2c); |
5332 | hi2c->MasterTxCpltCallback(hi2c); |
3983 | } |
- | |
3984 | else |
5333 | #else |
3985 | { |
- | |
3986 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | |
3987 | - | ||
3988 | HAL_I2C_MasterTxCpltCallback(hi2c); |
5334 | HAL_I2C_MasterTxCpltCallback(hi2c); |
3989 | } |
5335 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
3990 | } |
5336 | } |
3991 | } |
5337 | } |
3992 | } |
5338 | } |
- | 5339 | else |
|
- | 5340 | { |
|
- | 5341 | /* Do nothing */ |
|
- | 5342 | } |
|
- | 5343 | } |
|
- | 5344 | ||
- | 5345 | /** |
|
- | 5346 | * @brief Handle TXE and BTF flag for Memory transmitter |
|
- | 5347 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 5348 | * the configuration information for I2C module |
|
3993 | return HAL_OK; |
5349 | * @retval None |
- | 5350 | */ |
|
- | 5351 | static void I2C_MemoryTransmit_TXE_BTF(I2C_HandleTypeDef *hi2c) |
|
- | 5352 | { |
|
- | 5353 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
|
- | 5354 | HAL_I2C_StateTypeDef CurrentState = hi2c->State; |
|
- | 5355 | ||
- | 5356 | if (hi2c->EventCount == 0U) |
|
- | 5357 | { |
|
- | 5358 | /* If Memory address size is 8Bit */ |
|
- | 5359 | if (hi2c->MemaddSize == I2C_MEMADD_SIZE_8BIT) |
|
- | 5360 | { |
|
- | 5361 | /* Send Memory Address */ |
|
- | 5362 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress); |
|
- | 5363 | ||
- | 5364 | hi2c->EventCount += 2U; |
|
- | 5365 | } |
|
- | 5366 | /* If Memory address size is 16Bit */ |
|
- | 5367 | else |
|
- | 5368 | { |
|
- | 5369 | /* Send MSB of Memory Address */ |
|
- | 5370 | hi2c->Instance->DR = I2C_MEM_ADD_MSB(hi2c->Memaddress); |
|
- | 5371 | ||
- | 5372 | hi2c->EventCount++; |
|
- | 5373 | } |
|
- | 5374 | } |
|
- | 5375 | else if (hi2c->EventCount == 1U) |
|
- | 5376 | { |
|
- | 5377 | /* Send LSB of Memory Address */ |
|
- | 5378 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress); |
|
- | 5379 | ||
- | 5380 | hi2c->EventCount++; |
|
- | 5381 | } |
|
- | 5382 | else if (hi2c->EventCount == 2U) |
|
- | 5383 | { |
|
- | 5384 | if (CurrentState == HAL_I2C_STATE_BUSY_RX) |
|
- | 5385 | { |
|
- | 5386 | /* Generate Restart */ |
|
- | 5387 | hi2c->Instance->CR1 |= I2C_CR1_START; |
|
- | 5388 | } |
|
- | 5389 | else if ((hi2c->XferCount > 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX)) |
|
- | 5390 | { |
|
- | 5391 | /* Write data to DR */ |
|
- | 5392 | hi2c->Instance->DR = *hi2c->pBuffPtr; |
|
- | 5393 | ||
- | 5394 | /* Increment Buffer pointer */ |
|
- | 5395 | hi2c->pBuffPtr++; |
|
- | 5396 | ||
- | 5397 | /* Update counter */ |
|
- | 5398 | hi2c->XferCount--; |
|
- | 5399 | } |
|
- | 5400 | else if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX)) |
|
- | 5401 | { |
|
- | 5402 | /* Generate Stop condition then Call TxCpltCallback() */ |
|
- | 5403 | /* Disable EVT, BUF and ERR interrupt */ |
|
- | 5404 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
|
- | 5405 | ||
- | 5406 | /* Generate Stop */ |
|
- | 5407 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
|
- | 5408 | ||
- | 5409 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 5410 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 5411 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 5412 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5413 | hi2c->MemTxCpltCallback(hi2c); |
|
- | 5414 | #else |
|
- | 5415 | HAL_I2C_MemTxCpltCallback(hi2c); |
|
- | 5416 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
- | 5417 | } |
|
- | 5418 | else |
|
- | 5419 | { |
|
- | 5420 | /* Do nothing */ |
|
- | 5421 | } |
|
- | 5422 | } |
|
- | 5423 | else |
|
- | 5424 | { |
|
- | 5425 | /* Do nothing */ |
|
- | 5426 | } |
|
3994 | } |
5427 | } |
3995 | 5428 | ||
3996 | /** |
5429 | /** |
3997 | * @brief Handle RXNE flag for Master |
5430 | * @brief Handle RXNE flag for Master |
3998 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5431 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
3999 | * the configuration information for I2C module |
5432 | * the configuration information for I2C module |
4000 | * @retval HAL status |
5433 | * @retval None |
4001 | */ |
5434 | */ |
4002 | static HAL_StatusTypeDef I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c) |
5435 | static void I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c) |
4003 | { |
5436 | { |
4004 | if(hi2c->State == HAL_I2C_STATE_BUSY_RX) |
5437 | if (hi2c->State == HAL_I2C_STATE_BUSY_RX) |
4005 | { |
5438 | { |
4006 | uint32_t tmp = 0U; |
5439 | uint32_t tmp; |
4007 | 5440 | ||
4008 | tmp = hi2c->XferCount; |
5441 | tmp = hi2c->XferCount; |
4009 | if(tmp > 3U) |
5442 | if (tmp > 3U) |
4010 | { |
5443 | { |
4011 | /* Read data from DR */ |
5444 | /* Read data from DR */ |
4012 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
5445 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 5446 | ||
- | 5447 | /* Increment Buffer pointer */ |
|
- | 5448 | hi2c->pBuffPtr++; |
|
- | 5449 | ||
- | 5450 | /* Update counter */ |
|
4013 | hi2c->XferCount--; |
5451 | hi2c->XferCount--; |
4014 | } |
- | |
4015 | else if((tmp == 2U) || (tmp == 3U)) |
- | |
4016 | { |
5452 | |
4017 | if(hi2c->XferOptions != I2C_NEXT_FRAME) |
5453 | if (hi2c->XferCount == (uint16_t)3) |
4018 | { |
- | |
4019 | /* Disable Acknowledge */ |
- | |
4020 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
- | |
4021 | - | ||
4022 | /* Enable Pos */ |
- | |
4023 | hi2c->Instance->CR1 |= I2C_CR1_POS; |
- | |
4024 | } |
- | |
4025 | else |
- | |
4026 | { |
5454 | { |
- | 5455 | /* Disable BUF interrupt, this help to treat correctly the last 4 bytes |
|
- | 5456 | on BTF subroutine */ |
|
4027 | /* Enable Acknowledge */ |
5457 | /* Disable BUF interrupt */ |
4028 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
5458 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF); |
4029 | } |
5459 | } |
4030 | - | ||
4031 | /* Disable BUF interrupt */ |
- | |
4032 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF); |
- | |
4033 | } |
5460 | } |
4034 | else |
- | |
- | 5461 | else if ((hi2c->XferOptions != I2C_FIRST_AND_NEXT_FRAME) && ((tmp == 1U) || (tmp == 0U))) |
|
4035 | { |
5462 | { |
4036 | if(hi2c->XferOptions != I2C_NEXT_FRAME) |
5463 | if (I2C_WaitOnSTOPRequestThroughIT(hi2c) == HAL_OK) |
4037 | { |
5464 | { |
4038 | /* Disable Acknowledge */ |
5465 | /* Disable Acknowledge */ |
4039 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
- | |
4040 | } |
- | |
4041 | else |
- | |
4042 | { |
- | |
4043 | /* Enable Acknowledge */ |
- | |
4044 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
5466 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4045 | } |
- | |
4046 | 5467 | ||
4047 | /* Disable EVT, BUF and ERR interrupt */ |
5468 | /* Disable EVT, BUF and ERR interrupt */ |
4048 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
5469 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
4049 | - | ||
4050 | /* Read data from DR */ |
- | |
4051 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
- | |
4052 | hi2c->XferCount--; |
- | |
4053 | 5470 | ||
4054 | hi2c->State = HAL_I2C_STATE_READY; |
5471 | /* Read data from DR */ |
4055 | hi2c->PreviousState = I2C_STATE_NONE; |
5472 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
4056 | 5473 | ||
- | 5474 | /* Increment Buffer pointer */ |
|
- | 5475 | hi2c->pBuffPtr++; |
|
- | 5476 | ||
- | 5477 | /* Update counter */ |
|
- | 5478 | hi2c->XferCount--; |
|
- | 5479 | ||
- | 5480 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 5481 | ||
4057 | if(hi2c->Mode == HAL_I2C_MODE_MEM) |
5482 | if (hi2c->Mode == HAL_I2C_MODE_MEM) |
4058 | { |
5483 | { |
4059 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5484 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 5485 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 5486 | ||
- | 5487 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5488 | hi2c->MemRxCpltCallback(hi2c); |
|
- | 5489 | #else |
|
4060 | HAL_I2C_MemRxCpltCallback(hi2c); |
5490 | HAL_I2C_MemRxCpltCallback(hi2c); |
- | 5491 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
- | 5492 | } |
|
- | 5493 | else |
|
- | 5494 | { |
|
- | 5495 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 5496 | hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX; |
|
- | 5497 | ||
- | 5498 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5499 | hi2c->MasterRxCpltCallback(hi2c); |
|
- | 5500 | #else |
|
- | 5501 | HAL_I2C_MasterRxCpltCallback(hi2c); |
|
- | 5502 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
- | 5503 | } |
|
4061 | } |
5504 | } |
4062 | else |
5505 | else |
4063 | { |
5506 | { |
- | 5507 | /* Disable EVT, BUF and ERR interrupt */ |
|
- | 5508 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
|
- | 5509 | ||
- | 5510 | /* Read data from DR */ |
|
- | 5511 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
|
- | 5512 | ||
- | 5513 | /* Increment Buffer pointer */ |
|
- | 5514 | hi2c->pBuffPtr++; |
|
- | 5515 | ||
- | 5516 | /* Update counter */ |
|
- | 5517 | hi2c->XferCount--; |
|
- | 5518 | ||
- | 5519 | hi2c->State = HAL_I2C_STATE_READY; |
|
4064 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5520 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 5521 | ||
- | 5522 | /* Call user error callback */ |
|
- | 5523 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5524 | hi2c->ErrorCallback(hi2c); |
|
- | 5525 | #else |
|
4065 | HAL_I2C_MasterRxCpltCallback(hi2c); |
5526 | HAL_I2C_ErrorCallback(hi2c); |
- | 5527 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4066 | } |
5528 | } |
4067 | } |
5529 | } |
- | 5530 | else |
|
- | 5531 | { |
|
- | 5532 | /* Do nothing */ |
|
- | 5533 | } |
|
4068 | } |
5534 | } |
4069 | return HAL_OK; |
- | |
4070 | } |
5535 | } |
4071 | 5536 | ||
4072 | /** |
5537 | /** |
4073 | * @brief Handle BTF flag for Master receiver |
5538 | * @brief Handle BTF flag for Master receiver |
4074 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5539 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4075 | * the configuration information for I2C module |
5540 | * the configuration information for I2C module |
4076 | * @retval HAL status |
5541 | * @retval None |
4077 | */ |
5542 | */ |
4078 | static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c) |
5543 | static void I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c) |
4079 | { |
5544 | { |
4080 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
5545 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
4081 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
5546 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
4082 | 5547 | ||
4083 | if(hi2c->XferCount == 3U) |
5548 | if (hi2c->XferCount == 4U) |
4084 | { |
5549 | { |
- | 5550 | /* Disable BUF interrupt, this help to treat correctly the last 2 bytes |
|
- | 5551 | on BTF subroutine if there is a reception delay between N-1 and N byte */ |
|
- | 5552 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF); |
|
- | 5553 | ||
- | 5554 | /* Read data from DR */ |
|
- | 5555 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
|
- | 5556 | ||
- | 5557 | /* Increment Buffer pointer */ |
|
- | 5558 | hi2c->pBuffPtr++; |
|
- | 5559 | ||
- | 5560 | /* Update counter */ |
|
- | 5561 | hi2c->XferCount--; |
|
- | 5562 | } |
|
- | 5563 | else if (hi2c->XferCount == 3U) |
|
- | 5564 | { |
|
- | 5565 | /* Disable BUF interrupt, this help to treat correctly the last 2 bytes |
|
- | 5566 | on BTF subroutine if there is a reception delay between N-1 and N byte */ |
|
- | 5567 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF); |
|
- | 5568 | ||
4085 | if((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME)) |
5569 | if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME)) |
4086 | { |
5570 | { |
4087 | /* Disable Acknowledge */ |
5571 | /* Disable Acknowledge */ |
4088 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
5572 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4089 | } |
5573 | } |
4090 | 5574 | ||
4091 | /* Read data from DR */ |
5575 | /* Read data from DR */ |
4092 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
5576 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 5577 | ||
- | 5578 | /* Increment Buffer pointer */ |
|
- | 5579 | hi2c->pBuffPtr++; |
|
- | 5580 | ||
- | 5581 | /* Update counter */ |
|
4093 | hi2c->XferCount--; |
5582 | hi2c->XferCount--; |
4094 | } |
5583 | } |
4095 | else if(hi2c->XferCount == 2U) |
5584 | else if (hi2c->XferCount == 2U) |
4096 | { |
5585 | { |
4097 | /* Prepare next transfer or stop current transfer */ |
5586 | /* Prepare next transfer or stop current transfer */ |
4098 | if((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME)) |
5587 | if ((CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP)) |
4099 | { |
5588 | { |
4100 | if(CurrentXferOptions != I2C_NEXT_FRAME) |
- | |
4101 | { |
- | |
4102 | /* Disable Acknowledge */ |
5589 | /* Disable Acknowledge */ |
4103 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
- | |
4104 | } |
- | |
4105 | else |
- | |
4106 | { |
- | |
4107 | /* Enable Acknowledge */ |
- | |
4108 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
5590 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4109 | } |
- | |
4110 | - | ||
4111 | /* Disable EVT and ERR interrupt */ |
- | |
4112 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
- | |
4113 | } |
5591 | } |
- | 5592 | else if ((CurrentXferOptions == I2C_NEXT_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_NEXT_FRAME)) |
|
4114 | else |
5593 | { |
- | 5594 | /* Enable Acknowledge */ |
|
- | 5595 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
|
- | 5596 | } |
|
- | 5597 | else if (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP) |
|
4115 | { |
5598 | { |
4116 | /* Disable EVT and ERR interrupt */ |
- | |
4117 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
- | |
4118 | - | ||
4119 | /* Generate Stop */ |
5599 | /* Generate Stop */ |
4120 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
5600 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
- | 5601 | } |
|
- | 5602 | else |
|
- | 5603 | { |
|
- | 5604 | /* Do nothing */ |
|
4121 | } |
5605 | } |
4122 | 5606 | ||
4123 | /* Read data from DR */ |
5607 | /* Read data from DR */ |
4124 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
5608 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 5609 | ||
- | 5610 | /* Increment Buffer pointer */ |
|
- | 5611 | hi2c->pBuffPtr++; |
|
- | 5612 | ||
- | 5613 | /* Update counter */ |
|
4125 | hi2c->XferCount--; |
5614 | hi2c->XferCount--; |
4126 | 5615 | ||
4127 | /* Read data from DR */ |
5616 | /* Read data from DR */ |
4128 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
5617 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 5618 | ||
- | 5619 | /* Increment Buffer pointer */ |
|
- | 5620 | hi2c->pBuffPtr++; |
|
- | 5621 | ||
- | 5622 | /* Update counter */ |
|
4129 | hi2c->XferCount--; |
5623 | hi2c->XferCount--; |
4130 | 5624 | ||
4131 | hi2c->State = HAL_I2C_STATE_READY; |
5625 | /* Disable EVT and ERR interrupt */ |
4132 | hi2c->PreviousState = I2C_STATE_NONE; |
5626 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
4133 | 5627 | ||
- | 5628 | hi2c->State = HAL_I2C_STATE_READY; |
|
4134 | if(hi2c->Mode == HAL_I2C_MODE_MEM) |
5629 | if (hi2c->Mode == HAL_I2C_MODE_MEM) |
4135 | { |
5630 | { |
4136 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5631 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 5632 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 5633 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5634 | hi2c->MemRxCpltCallback(hi2c); |
|
4137 | 5635 | #else |
|
4138 | HAL_I2C_MemRxCpltCallback(hi2c); |
5636 | HAL_I2C_MemRxCpltCallback(hi2c); |
- | 5637 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4139 | } |
5638 | } |
4140 | else |
5639 | else |
4141 | { |
5640 | { |
4142 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5641 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 5642 | hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX; |
|
- | 5643 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5644 | hi2c->MasterRxCpltCallback(hi2c); |
|
4143 | 5645 | #else |
|
4144 | HAL_I2C_MasterRxCpltCallback(hi2c); |
5646 | HAL_I2C_MasterRxCpltCallback(hi2c); |
- | 5647 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4145 | } |
5648 | } |
4146 | } |
5649 | } |
4147 | else |
5650 | else |
4148 | { |
5651 | { |
4149 | /* Read data from DR */ |
5652 | /* Read data from DR */ |
4150 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
5653 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 5654 | ||
- | 5655 | /* Increment Buffer pointer */ |
|
- | 5656 | hi2c->pBuffPtr++; |
|
- | 5657 | ||
- | 5658 | /* Update counter */ |
|
4151 | hi2c->XferCount--; |
5659 | hi2c->XferCount--; |
4152 | } |
5660 | } |
4153 | return HAL_OK; |
- | |
4154 | } |
5661 | } |
4155 | 5662 | ||
4156 | /** |
5663 | /** |
4157 | * @brief Handle SB flag for Master |
5664 | * @brief Handle SB flag for Master |
4158 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5665 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4159 | * the configuration information for I2C module |
5666 | * the configuration information for I2C module |
4160 | * @retval HAL status |
5667 | * @retval None |
4161 | */ |
5668 | */ |
4162 | static HAL_StatusTypeDef I2C_Master_SB(I2C_HandleTypeDef *hi2c) |
5669 | static void I2C_Master_SB(I2C_HandleTypeDef *hi2c) |
4163 | { |
5670 | { |
4164 | if(hi2c->Mode == HAL_I2C_MODE_MEM) |
5671 | if (hi2c->Mode == HAL_I2C_MODE_MEM) |
4165 | { |
5672 | { |
4166 | if(hi2c->EventCount == 0U) |
5673 | if (hi2c->EventCount == 0U) |
4167 | { |
5674 | { |
4168 | /* Send slave address */ |
5675 | /* Send slave address */ |
4169 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress); |
5676 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress); |
4170 | } |
5677 | } |
4171 | else |
5678 | else |
Line 4173... | Line 5680... | ||
4173 | hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress); |
5680 | hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress); |
4174 | } |
5681 | } |
4175 | } |
5682 | } |
4176 | else |
5683 | else |
4177 | { |
5684 | { |
4178 | if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT) |
5685 | if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT) |
4179 | { |
5686 | { |
4180 | /* Send slave 7 Bits address */ |
5687 | /* Send slave 7 Bits address */ |
4181 | if(hi2c->State == HAL_I2C_STATE_BUSY_TX) |
5688 | if (hi2c->State == HAL_I2C_STATE_BUSY_TX) |
4182 | { |
5689 | { |
4183 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress); |
5690 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress); |
4184 | } |
5691 | } |
4185 | else |
5692 | else |
4186 | { |
5693 | { |
4187 | hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress); |
5694 | hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress); |
4188 | } |
5695 | } |
- | 5696 | ||
- | 5697 | if (((hi2c->hdmatx != NULL) && (hi2c->hdmatx->XferCpltCallback != NULL)) |
|
- | 5698 | || ((hi2c->hdmarx != NULL) && (hi2c->hdmarx->XferCpltCallback != NULL))) |
|
- | 5699 | { |
|
- | 5700 | /* Enable DMA Request */ |
|
- | 5701 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 5702 | } |
|
4189 | } |
5703 | } |
4190 | else |
5704 | else |
4191 | { |
5705 | { |
4192 | if(hi2c->EventCount == 0U) |
5706 | if (hi2c->EventCount == 0U) |
4193 | { |
5707 | { |
4194 | /* Send header of slave address */ |
5708 | /* Send header of slave address */ |
4195 | hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(hi2c->Devaddress); |
5709 | hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(hi2c->Devaddress); |
4196 | } |
5710 | } |
4197 | else if(hi2c->EventCount == 1U) |
5711 | else if (hi2c->EventCount == 1U) |
4198 | { |
5712 | { |
4199 | /* Send header of slave address */ |
5713 | /* Send header of slave address */ |
4200 | hi2c->Instance->DR = I2C_10BIT_HEADER_READ(hi2c->Devaddress); |
5714 | hi2c->Instance->DR = I2C_10BIT_HEADER_READ(hi2c->Devaddress); |
4201 | } |
5715 | } |
- | 5716 | else |
|
- | 5717 | { |
|
- | 5718 | /* Do nothing */ |
|
- | 5719 | } |
|
4202 | } |
5720 | } |
4203 | } |
5721 | } |
4204 | - | ||
4205 | return HAL_OK; |
- | |
4206 | } |
5722 | } |
4207 | 5723 | ||
4208 | /** |
5724 | /** |
4209 | * @brief Handle ADD10 flag for Master |
5725 | * @brief Handle ADD10 flag for Master |
4210 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5726 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4211 | * the configuration information for I2C module |
5727 | * the configuration information for I2C module |
4212 | * @retval HAL status |
5728 | * @retval None |
4213 | */ |
5729 | */ |
4214 | static HAL_StatusTypeDef I2C_Master_ADD10(I2C_HandleTypeDef *hi2c) |
5730 | static void I2C_Master_ADD10(I2C_HandleTypeDef *hi2c) |
4215 | { |
5731 | { |
4216 | /* Send slave address */ |
5732 | /* Send slave address */ |
4217 | hi2c->Instance->DR = I2C_10BIT_ADDRESS(hi2c->Devaddress); |
5733 | hi2c->Instance->DR = I2C_10BIT_ADDRESS(hi2c->Devaddress); |
4218 | 5734 | ||
- | 5735 | if ((hi2c->hdmatx != NULL) || (hi2c->hdmarx != NULL)) |
|
- | 5736 | { |
|
- | 5737 | if ((hi2c->hdmatx->XferCpltCallback != NULL) || (hi2c->hdmarx->XferCpltCallback != NULL)) |
|
- | 5738 | { |
|
4219 | return HAL_OK; |
5739 | /* Enable DMA Request */ |
- | 5740 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 5741 | } |
|
- | 5742 | } |
|
4220 | } |
5743 | } |
4221 | 5744 | ||
4222 | /** |
5745 | /** |
4223 | * @brief Handle ADDR flag for Master |
5746 | * @brief Handle ADDR flag for Master |
4224 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5747 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4225 | * the configuration information for I2C module |
5748 | * the configuration information for I2C module |
4226 | * @retval HAL status |
5749 | * @retval None |
4227 | */ |
5750 | */ |
4228 | static HAL_StatusTypeDef I2C_Master_ADDR(I2C_HandleTypeDef *hi2c) |
5751 | static void I2C_Master_ADDR(I2C_HandleTypeDef *hi2c) |
4229 | { |
5752 | { |
4230 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
5753 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
4231 | uint32_t CurrentMode = hi2c->Mode; |
5754 | HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode; |
4232 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
5755 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
4233 | uint32_t Prev_State = hi2c->PreviousState; |
5756 | uint32_t Prev_State = hi2c->PreviousState; |
4234 | 5757 | ||
4235 | if(hi2c->State == HAL_I2C_STATE_BUSY_RX) |
5758 | if (hi2c->State == HAL_I2C_STATE_BUSY_RX) |
4236 | { |
5759 | { |
4237 | if((hi2c->EventCount == 0U) && (CurrentMode == HAL_I2C_MODE_MEM)) |
5760 | if ((hi2c->EventCount == 0U) && (CurrentMode == HAL_I2C_MODE_MEM)) |
4238 | { |
5761 | { |
4239 | /* Clear ADDR flag */ |
5762 | /* Clear ADDR flag */ |
4240 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5763 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4241 | } |
5764 | } |
4242 | else if((hi2c->EventCount == 0U) && (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)) |
5765 | else if ((hi2c->EventCount == 0U) && (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT)) |
4243 | { |
5766 | { |
4244 | /* Clear ADDR flag */ |
5767 | /* Clear ADDR flag */ |
4245 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5768 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4246 | 5769 | ||
4247 | /* Generate Restart */ |
5770 | /* Generate Restart */ |
4248 | hi2c->Instance->CR1 |= I2C_CR1_START; |
5771 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
4249 | 5772 | ||
4250 | hi2c->EventCount++; |
5773 | hi2c->EventCount++; |
4251 | } |
5774 | } |
4252 | else |
5775 | else |
4253 | { |
5776 | { |
4254 | if(hi2c->XferCount == 0U) |
5777 | if (hi2c->XferCount == 0U) |
4255 | { |
5778 | { |
4256 | /* Clear ADDR flag */ |
5779 | /* Clear ADDR flag */ |
4257 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5780 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4258 | 5781 | ||
4259 | /* Generate Stop */ |
5782 | /* Generate Stop */ |
4260 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
5783 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
4261 | } |
5784 | } |
4262 | else if(hi2c->XferCount == 1U) |
5785 | else if (hi2c->XferCount == 1U) |
4263 | { |
5786 | { |
4264 | if(CurrentXferOptions == I2C_NO_OPTION_FRAME) |
5787 | if (CurrentXferOptions == I2C_NO_OPTION_FRAME) |
4265 | { |
5788 | { |
4266 | /* Disable Acknowledge */ |
5789 | /* Disable Acknowledge */ |
4267 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
5790 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4268 | 5791 | ||
4269 | if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
5792 | if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
4270 | { |
5793 | { |
4271 | /* Disable Acknowledge */ |
5794 | /* Disable Acknowledge */ |
4272 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
5795 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4273 | 5796 | ||
4274 | /* Clear ADDR flag */ |
5797 | /* Clear ADDR flag */ |
4275 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5798 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4276 | } |
5799 | } |
4277 | else |
5800 | else |
4278 | { |
5801 | { |
4279 | /* Clear ADDR flag */ |
5802 | /* Clear ADDR flag */ |
4280 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5803 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4281 | 5804 | ||
4282 | /* Generate Stop */ |
5805 | /* Generate Stop */ |
4283 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
5806 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
4284 | } |
5807 | } |
4285 | } |
5808 | } |
4286 | /* Prepare next transfer or stop current transfer */ |
5809 | /* Prepare next transfer or stop current transfer */ |
4287 | else if((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) \ |
5810 | else if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) \ |
4288 | && (Prev_State != I2C_STATE_MASTER_BUSY_RX)) |
5811 | && ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (CurrentXferOptions == I2C_FIRST_FRAME))) |
4289 | { |
5812 | { |
4290 | if(hi2c->XferOptions != I2C_NEXT_FRAME) |
5813 | if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP)) |
4291 | { |
5814 | { |
4292 | /* Disable Acknowledge */ |
5815 | /* Disable Acknowledge */ |
4293 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
5816 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4294 | } |
5817 | } |
4295 | else |
5818 | else |
4296 | { |
5819 | { |
4297 | /* Enable Acknowledge */ |
5820 | /* Enable Acknowledge */ |
4298 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
5821 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4299 | } |
5822 | } |
4300 | 5823 | ||
4301 | /* Clear ADDR flag */ |
5824 | /* Clear ADDR flag */ |
4302 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5825 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4303 | } |
5826 | } |
4304 | else |
5827 | else |
4305 | { |
5828 | { |
4306 | /* Disable Acknowledge */ |
5829 | /* Disable Acknowledge */ |
4307 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
5830 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4308 | 5831 | ||
4309 | /* Clear ADDR flag */ |
5832 | /* Clear ADDR flag */ |
4310 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5833 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4311 | 5834 | ||
4312 | /* Generate Stop */ |
5835 | /* Generate Stop */ |
4313 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
5836 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
4314 | } |
5837 | } |
4315 | } |
5838 | } |
4316 | else if(hi2c->XferCount == 2U) |
5839 | else if (hi2c->XferCount == 2U) |
4317 | { |
5840 | { |
4318 | if(hi2c->XferOptions != I2C_NEXT_FRAME) |
5841 | if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP)) |
4319 | { |
5842 | { |
4320 | /* Enable Pos */ |
5843 | /* Enable Pos */ |
4321 | hi2c->Instance->CR1 |= I2C_CR1_POS; |
5844 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS); |
4322 | 5845 | ||
4323 | /* Clear ADDR flag */ |
5846 | /* Clear ADDR flag */ |
4324 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5847 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4325 | 5848 | ||
4326 | /* Disable Acknowledge */ |
5849 | /* Disable Acknowledge */ |
4327 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
5850 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4328 | } |
5851 | } |
4329 | else |
5852 | else |
4330 | { |
5853 | { |
4331 | /* Enable Acknowledge */ |
5854 | /* Enable Acknowledge */ |
4332 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
5855 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4333 | 5856 | ||
4334 | /* Clear ADDR flag */ |
5857 | /* Clear ADDR flag */ |
4335 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5858 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4336 | } |
5859 | } |
4337 | 5860 | ||
4338 | if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
5861 | if (((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) && ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP) || (CurrentXferOptions == I2C_LAST_FRAME))) |
4339 | { |
5862 | { |
4340 | /* Enable Last DMA bit */ |
5863 | /* Enable Last DMA bit */ |
4341 | hi2c->Instance->CR2 |= I2C_CR2_LAST; |
5864 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST); |
4342 | } |
5865 | } |
4343 | } |
5866 | } |
4344 | else |
5867 | else |
4345 | { |
5868 | { |
4346 | /* Enable Acknowledge */ |
5869 | /* Enable Acknowledge */ |
4347 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
5870 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4348 | 5871 | ||
4349 | if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
5872 | if (((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) && ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP) || (CurrentXferOptions == I2C_LAST_FRAME))) |
4350 | { |
5873 | { |
4351 | /* Enable Last DMA bit */ |
5874 | /* Enable Last DMA bit */ |
4352 | hi2c->Instance->CR2 |= I2C_CR2_LAST; |
5875 | SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST); |
4353 | } |
5876 | } |
4354 | 5877 | ||
4355 | /* Clear ADDR flag */ |
5878 | /* Clear ADDR flag */ |
4356 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5879 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4357 | } |
5880 | } |
4358 | 5881 | ||
4359 | /* Reset Event counter */ |
5882 | /* Reset Event counter */ |
4360 | hi2c->EventCount = 0U; |
5883 | hi2c->EventCount = 0U; |
4361 | } |
5884 | } |
4362 | } |
5885 | } |
4363 | else |
5886 | else |
4364 | { |
5887 | { |
4365 | /* Clear ADDR flag */ |
5888 | /* Clear ADDR flag */ |
4366 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5889 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4367 | } |
5890 | } |
4368 | - | ||
4369 | return HAL_OK; |
- | |
4370 | } |
5891 | } |
4371 | 5892 | ||
4372 | /** |
5893 | /** |
4373 | * @brief Handle TXE flag for Slave |
5894 | * @brief Handle TXE flag for Slave |
4374 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5895 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4375 | * the configuration information for I2C module |
5896 | * the configuration information for I2C module |
4376 | * @retval HAL status |
5897 | * @retval None |
4377 | */ |
5898 | */ |
4378 | static HAL_StatusTypeDef I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c) |
5899 | static void I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c) |
4379 | { |
5900 | { |
4380 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
5901 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
4381 | uint32_t CurrentState = hi2c->State; |
5902 | HAL_I2C_StateTypeDef CurrentState = hi2c->State; |
4382 | 5903 | ||
4383 | if(hi2c->XferCount != 0U) |
5904 | if (hi2c->XferCount != 0U) |
4384 | { |
5905 | { |
4385 | /* Write data to DR */ |
5906 | /* Write data to DR */ |
4386 | hi2c->Instance->DR = (*hi2c->pBuffPtr++); |
5907 | hi2c->Instance->DR = *hi2c->pBuffPtr; |
- | 5908 | ||
- | 5909 | /* Increment Buffer pointer */ |
|
- | 5910 | hi2c->pBuffPtr++; |
|
- | 5911 | ||
- | 5912 | /* Update counter */ |
|
4387 | hi2c->XferCount--; |
5913 | hi2c->XferCount--; |
4388 | 5914 | ||
4389 | if((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN)) |
5915 | if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN)) |
4390 | { |
5916 | { |
4391 | /* Last Byte is received, disable Interrupt */ |
5917 | /* Last Byte is received, disable Interrupt */ |
4392 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF); |
5918 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF); |
4393 | 5919 | ||
4394 | /* Set state at HAL_I2C_STATE_LISTEN */ |
5920 | /* Set state at HAL_I2C_STATE_LISTEN */ |
4395 | hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX; |
5921 | hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX; |
4396 | hi2c->State = HAL_I2C_STATE_LISTEN; |
5922 | hi2c->State = HAL_I2C_STATE_LISTEN; |
4397 | 5923 | ||
4398 | /* Call the Tx complete callback to inform upper layer of the end of receive process */ |
5924 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
- | 5925 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5926 | hi2c->SlaveTxCpltCallback(hi2c); |
|
- | 5927 | #else |
|
4399 | HAL_I2C_SlaveTxCpltCallback(hi2c); |
5928 | HAL_I2C_SlaveTxCpltCallback(hi2c); |
- | 5929 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4400 | } |
5930 | } |
4401 | } |
5931 | } |
4402 | return HAL_OK; |
- | |
4403 | } |
5932 | } |
4404 | 5933 | ||
4405 | /** |
5934 | /** |
4406 | * @brief Handle BTF flag for Slave transmitter |
5935 | * @brief Handle BTF flag for Slave transmitter |
4407 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5936 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4408 | * the configuration information for I2C module |
5937 | * the configuration information for I2C module |
4409 | * @retval HAL status |
5938 | * @retval None |
4410 | */ |
5939 | */ |
4411 | static HAL_StatusTypeDef I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c) |
5940 | static void I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c) |
4412 | { |
5941 | { |
4413 | if(hi2c->XferCount != 0U) |
5942 | if (hi2c->XferCount != 0U) |
4414 | { |
5943 | { |
4415 | /* Write data to DR */ |
5944 | /* Write data to DR */ |
4416 | hi2c->Instance->DR = (*hi2c->pBuffPtr++); |
5945 | hi2c->Instance->DR = *hi2c->pBuffPtr; |
- | 5946 | ||
- | 5947 | /* Increment Buffer pointer */ |
|
- | 5948 | hi2c->pBuffPtr++; |
|
- | 5949 | ||
- | 5950 | /* Update counter */ |
|
4417 | hi2c->XferCount--; |
5951 | hi2c->XferCount--; |
4418 | } |
5952 | } |
4419 | return HAL_OK; |
- | |
4420 | } |
5953 | } |
4421 | 5954 | ||
4422 | /** |
5955 | /** |
4423 | * @brief Handle RXNE flag for Slave |
5956 | * @brief Handle RXNE flag for Slave |
4424 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5957 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4425 | * the configuration information for I2C module |
5958 | * the configuration information for I2C module |
4426 | * @retval HAL status |
5959 | * @retval None |
4427 | */ |
5960 | */ |
4428 | static HAL_StatusTypeDef I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c) |
5961 | static void I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c) |
4429 | { |
5962 | { |
4430 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
5963 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
4431 | uint32_t CurrentState = hi2c->State; |
5964 | HAL_I2C_StateTypeDef CurrentState = hi2c->State; |
4432 | 5965 | ||
4433 | if(hi2c->XferCount != 0U) |
5966 | if (hi2c->XferCount != 0U) |
4434 | { |
5967 | { |
4435 | /* Read data from DR */ |
5968 | /* Read data from DR */ |
4436 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
5969 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 5970 | ||
- | 5971 | /* Increment Buffer pointer */ |
|
- | 5972 | hi2c->pBuffPtr++; |
|
- | 5973 | ||
- | 5974 | /* Update counter */ |
|
4437 | hi2c->XferCount--; |
5975 | hi2c->XferCount--; |
4438 | 5976 | ||
4439 | if((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)) |
5977 | if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)) |
4440 | { |
5978 | { |
4441 | /* Last Byte is received, disable Interrupt */ |
5979 | /* Last Byte is received, disable Interrupt */ |
4442 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF); |
5980 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_BUF); |
4443 | 5981 | ||
4444 | /* Set state at HAL_I2C_STATE_LISTEN */ |
5982 | /* Set state at HAL_I2C_STATE_LISTEN */ |
4445 | hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX; |
5983 | hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX; |
4446 | hi2c->State = HAL_I2C_STATE_LISTEN; |
5984 | hi2c->State = HAL_I2C_STATE_LISTEN; |
4447 | 5985 | ||
4448 | /* Call the Rx complete callback to inform upper layer of the end of receive process */ |
5986 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
- | 5987 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 5988 | hi2c->SlaveRxCpltCallback(hi2c); |
|
- | 5989 | #else |
|
4449 | HAL_I2C_SlaveRxCpltCallback(hi2c); |
5990 | HAL_I2C_SlaveRxCpltCallback(hi2c); |
- | 5991 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4450 | } |
5992 | } |
4451 | } |
5993 | } |
4452 | return HAL_OK; |
- | |
4453 | } |
5994 | } |
4454 | 5995 | ||
4455 | /** |
5996 | /** |
4456 | * @brief Handle BTF flag for Slave receiver |
5997 | * @brief Handle BTF flag for Slave receiver |
4457 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
5998 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4458 | * the configuration information for I2C module |
5999 | * the configuration information for I2C module |
4459 | * @retval HAL status |
6000 | * @retval None |
4460 | */ |
6001 | */ |
4461 | static HAL_StatusTypeDef I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c) |
6002 | static void I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c) |
4462 | { |
6003 | { |
4463 | if(hi2c->XferCount != 0U) |
6004 | if (hi2c->XferCount != 0U) |
4464 | { |
6005 | { |
4465 | /* Read data from DR */ |
6006 | /* Read data from DR */ |
4466 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
6007 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 6008 | ||
- | 6009 | /* Increment Buffer pointer */ |
|
- | 6010 | hi2c->pBuffPtr++; |
|
- | 6011 | ||
- | 6012 | /* Update counter */ |
|
4467 | hi2c->XferCount--; |
6013 | hi2c->XferCount--; |
4468 | } |
6014 | } |
4469 | return HAL_OK; |
- | |
4470 | } |
6015 | } |
4471 | 6016 | ||
4472 | /** |
6017 | /** |
4473 | * @brief Handle ADD flag for Slave |
6018 | * @brief Handle ADD flag for Slave |
4474 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
6019 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4475 | * the configuration information for I2C module |
6020 | * the configuration information for I2C module |
- | 6021 | * @param IT2Flags Interrupt2 flags to handle. |
|
4476 | * @retval HAL status |
6022 | * @retval None |
4477 | */ |
6023 | */ |
4478 | static HAL_StatusTypeDef I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c) |
6024 | static void I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c, uint32_t IT2Flags) |
4479 | { |
6025 | { |
4480 | uint8_t TransferDirection = I2C_DIRECTION_RECEIVE; |
6026 | uint8_t TransferDirection = I2C_DIRECTION_RECEIVE; |
4481 | uint16_t SlaveAddrCode = 0U; |
6027 | uint16_t SlaveAddrCode; |
4482 | 6028 | ||
4483 | /* Transfer Direction requested by Master */ |
- | |
4484 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TRA) == RESET) |
6029 | if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) |
4485 | { |
6030 | { |
- | 6031 | /* Disable BUF interrupt, BUF enabling is manage through slave specific interface */ |
|
- | 6032 | __HAL_I2C_DISABLE_IT(hi2c, (I2C_IT_BUF)); |
|
- | 6033 | ||
- | 6034 | /* Transfer Direction requested by Master */ |
|
- | 6035 | if (I2C_CHECK_FLAG(IT2Flags, I2C_FLAG_TRA) == RESET) |
|
- | 6036 | { |
|
4486 | TransferDirection = I2C_DIRECTION_TRANSMIT; |
6037 | TransferDirection = I2C_DIRECTION_TRANSMIT; |
4487 | } |
6038 | } |
4488 | 6039 | ||
4489 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_DUALF) == RESET) |
6040 | if (I2C_CHECK_FLAG(IT2Flags, I2C_FLAG_DUALF) == RESET) |
4490 | { |
6041 | { |
4491 | SlaveAddrCode = hi2c->Init.OwnAddress1; |
6042 | SlaveAddrCode = (uint16_t)hi2c->Init.OwnAddress1; |
- | 6043 | } |
|
- | 6044 | else |
|
- | 6045 | { |
|
- | 6046 | SlaveAddrCode = (uint16_t)hi2c->Init.OwnAddress2; |
|
- | 6047 | } |
|
- | 6048 | ||
- | 6049 | /* Process Unlocked */ |
|
- | 6050 | __HAL_UNLOCK(hi2c); |
|
- | 6051 | ||
- | 6052 | /* Call Slave Addr callback */ |
|
- | 6053 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6054 | hi2c->AddrCallback(hi2c, TransferDirection, SlaveAddrCode); |
|
- | 6055 | #else |
|
- | 6056 | HAL_I2C_AddrCallback(hi2c, TransferDirection, SlaveAddrCode); |
|
- | 6057 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4492 | } |
6058 | } |
4493 | else |
6059 | else |
4494 | { |
6060 | { |
4495 | SlaveAddrCode = hi2c->Init.OwnAddress2; |
- | |
4496 | } |
- | |
4497 | - | ||
4498 | /* Call Slave Addr callback */ |
6061 | /* Clear ADDR flag */ |
4499 | HAL_I2C_AddrCallback(hi2c, TransferDirection, SlaveAddrCode); |
6062 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ADDR); |
4500 | 6063 | ||
- | 6064 | /* Process Unlocked */ |
|
4501 | return HAL_OK; |
6065 | __HAL_UNLOCK(hi2c); |
- | 6066 | } |
|
4502 | } |
6067 | } |
4503 | 6068 | ||
4504 | /** |
6069 | /** |
4505 | * @brief Handle STOPF flag for Slave |
6070 | * @brief Handle STOPF flag for Slave |
4506 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
6071 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4507 | * the configuration information for I2C module |
6072 | * the configuration information for I2C module |
4508 | * @retval HAL status |
6073 | * @retval None |
4509 | */ |
6074 | */ |
4510 | static HAL_StatusTypeDef I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c) |
6075 | static void I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c) |
4511 | { |
6076 | { |
4512 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
6077 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
4513 | uint32_t CurrentState = hi2c->State; |
6078 | HAL_I2C_StateTypeDef CurrentState = hi2c->State; |
4514 | 6079 | ||
4515 | /* Disable EVT, BUF and ERR interrupt */ |
6080 | /* Disable EVT, BUF and ERR interrupt */ |
4516 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
6081 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
4517 | 6082 | ||
4518 | /* Clear STOPF flag */ |
6083 | /* Clear STOPF flag */ |
4519 | __HAL_I2C_CLEAR_STOPFLAG(hi2c); |
6084 | __HAL_I2C_CLEAR_STOPFLAG(hi2c); |
4520 | 6085 | ||
4521 | /* Disable Acknowledge */ |
6086 | /* Disable Acknowledge */ |
4522 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
6087 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4523 | 6088 | ||
4524 | /* If a DMA is ongoing, Update handle size context */ |
6089 | /* If a DMA is ongoing, Update handle size context */ |
4525 | if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
6090 | if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
4526 | { |
6091 | { |
4527 | if((hi2c->State == HAL_I2C_STATE_BUSY_RX) || (hi2c->State == HAL_I2C_STATE_BUSY_RX_LISTEN)) |
6092 | if ((CurrentState == HAL_I2C_STATE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)) |
4528 | { |
6093 | { |
4529 | hi2c->XferCount = __HAL_DMA_GET_COUNTER(hi2c->hdmarx); |
6094 | hi2c->XferCount = (uint16_t)(__HAL_DMA_GET_COUNTER(hi2c->hdmarx)); |
- | 6095 | ||
- | 6096 | if (hi2c->XferCount != 0U) |
|
- | 6097 | { |
|
- | 6098 | /* Set ErrorCode corresponding to a Non-Acknowledge */ |
|
- | 6099 | hi2c->ErrorCode |= HAL_I2C_ERROR_AF; |
|
- | 6100 | } |
|
- | 6101 | ||
- | 6102 | /* Disable, stop the current DMA */ |
|
- | 6103 | CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 6104 | ||
- | 6105 | /* Abort DMA Xfer if any */ |
|
- | 6106 | if (HAL_DMA_GetState(hi2c->hdmarx) != HAL_DMA_STATE_READY) |
|
- | 6107 | { |
|
- | 6108 | /* Set the I2C DMA Abort callback : |
|
- | 6109 | will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ |
|
- | 6110 | hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort; |
|
- | 6111 | ||
- | 6112 | /* Abort DMA RX */ |
|
- | 6113 | if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK) |
|
- | 6114 | { |
|
- | 6115 | /* Call Directly XferAbortCallback function in case of error */ |
|
- | 6116 | hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx); |
|
- | 6117 | } |
|
- | 6118 | } |
|
4530 | } |
6119 | } |
4531 | else |
6120 | else |
4532 | { |
6121 | { |
4533 | hi2c->XferCount = __HAL_DMA_GET_COUNTER(hi2c->hdmatx); |
6122 | hi2c->XferCount = (uint16_t)(__HAL_DMA_GET_COUNTER(hi2c->hdmatx)); |
- | 6123 | ||
- | 6124 | if (hi2c->XferCount != 0U) |
|
- | 6125 | { |
|
- | 6126 | /* Set ErrorCode corresponding to a Non-Acknowledge */ |
|
- | 6127 | hi2c->ErrorCode |= HAL_I2C_ERROR_AF; |
|
- | 6128 | } |
|
- | 6129 | ||
- | 6130 | /* Disable, stop the current DMA */ |
|
- | 6131 | CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
|
- | 6132 | ||
- | 6133 | /* Abort DMA Xfer if any */ |
|
- | 6134 | if (HAL_DMA_GetState(hi2c->hdmatx) != HAL_DMA_STATE_READY) |
|
- | 6135 | { |
|
- | 6136 | /* Set the I2C DMA Abort callback : |
|
- | 6137 | will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ |
|
- | 6138 | hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort; |
|
- | 6139 | ||
- | 6140 | /* Abort DMA TX */ |
|
- | 6141 | if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK) |
|
- | 6142 | { |
|
- | 6143 | /* Call Directly XferAbortCallback function in case of error */ |
|
- | 6144 | hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx); |
|
- | 6145 | } |
|
- | 6146 | } |
|
4534 | } |
6147 | } |
4535 | } |
6148 | } |
4536 | 6149 | ||
4537 | /* All data are not transferred, so set error code accordingly */ |
6150 | /* All data are not transferred, so set error code accordingly */ |
4538 | if(hi2c->XferCount != 0U) |
6151 | if (hi2c->XferCount != 0U) |
4539 | { |
6152 | { |
4540 | /* Store Last receive data if any */ |
6153 | /* Store Last receive data if any */ |
4541 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) |
6154 | if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) |
4542 | { |
6155 | { |
4543 | /* Read data from DR */ |
6156 | /* Read data from DR */ |
4544 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
6157 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 6158 | ||
- | 6159 | /* Increment Buffer pointer */ |
|
- | 6160 | hi2c->pBuffPtr++; |
|
- | 6161 | ||
- | 6162 | /* Update counter */ |
|
4545 | hi2c->XferCount--; |
6163 | hi2c->XferCount--; |
4546 | } |
6164 | } |
4547 | 6165 | ||
4548 | /* Store Last receive data if any */ |
6166 | /* Store Last receive data if any */ |
4549 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) |
6167 | if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) |
4550 | { |
6168 | { |
4551 | /* Read data from DR */ |
6169 | /* Read data from DR */ |
4552 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
6170 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 6171 | ||
- | 6172 | /* Increment Buffer pointer */ |
|
- | 6173 | hi2c->pBuffPtr++; |
|
- | 6174 | ||
- | 6175 | /* Update counter */ |
|
4553 | hi2c->XferCount--; |
6176 | hi2c->XferCount--; |
4554 | } |
6177 | } |
4555 | 6178 | ||
- | 6179 | if (hi2c->XferCount != 0U) |
|
- | 6180 | { |
|
4556 | /* Set ErrorCode corresponding to a Non-Acknowledge */ |
6181 | /* Set ErrorCode corresponding to a Non-Acknowledge */ |
4557 | hi2c->ErrorCode |= HAL_I2C_ERROR_AF; |
6182 | hi2c->ErrorCode |= HAL_I2C_ERROR_AF; |
- | 6183 | } |
|
4558 | } |
6184 | } |
4559 | 6185 | ||
4560 | if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE) |
6186 | if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE) |
4561 | { |
6187 | { |
4562 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
6188 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
4563 | I2C_ITError(hi2c); |
6189 | I2C_ITError(hi2c); |
4564 | } |
6190 | } |
4565 | else |
6191 | else |
4566 | { |
6192 | { |
4567 | if((CurrentState == HAL_I2C_STATE_LISTEN ) || (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN) || \ |
6193 | if (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN) |
- | 6194 | { |
|
- | 6195 | /* Set state at HAL_I2C_STATE_LISTEN */ |
|
- | 6196 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 6197 | hi2c->State = HAL_I2C_STATE_LISTEN; |
|
- | 6198 | ||
- | 6199 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
|
- | 6200 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6201 | hi2c->SlaveRxCpltCallback(hi2c); |
|
- | 6202 | #else |
|
- | 6203 | HAL_I2C_SlaveRxCpltCallback(hi2c); |
|
- | 6204 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
- | 6205 | } |
|
- | 6206 | ||
4568 | (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN)) |
6207 | if (hi2c->State == HAL_I2C_STATE_LISTEN) |
4569 | { |
6208 | { |
4570 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
6209 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
4571 | hi2c->PreviousState = I2C_STATE_NONE; |
6210 | hi2c->PreviousState = I2C_STATE_NONE; |
4572 | hi2c->State = HAL_I2C_STATE_READY; |
6211 | hi2c->State = HAL_I2C_STATE_READY; |
4573 | hi2c->Mode = HAL_I2C_MODE_NONE; |
6212 | hi2c->Mode = HAL_I2C_MODE_NONE; |
4574 | 6213 | ||
4575 | /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */ |
6214 | /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */ |
- | 6215 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6216 | hi2c->ListenCpltCallback(hi2c); |
|
- | 6217 | #else |
|
4576 | HAL_I2C_ListenCpltCallback(hi2c); |
6218 | HAL_I2C_ListenCpltCallback(hi2c); |
- | 6219 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4577 | } |
6220 | } |
4578 | else |
6221 | else |
4579 | { |
6222 | { |
4580 | if((hi2c->PreviousState == I2C_STATE_SLAVE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX)) |
6223 | if ((hi2c->PreviousState == I2C_STATE_SLAVE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX)) |
4581 | { |
6224 | { |
4582 | hi2c->PreviousState = I2C_STATE_NONE; |
6225 | hi2c->PreviousState = I2C_STATE_NONE; |
4583 | hi2c->State = HAL_I2C_STATE_READY; |
6226 | hi2c->State = HAL_I2C_STATE_READY; |
4584 | hi2c->Mode = HAL_I2C_MODE_NONE; |
6227 | hi2c->Mode = HAL_I2C_MODE_NONE; |
4585 | 6228 | ||
- | 6229 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6230 | hi2c->SlaveRxCpltCallback(hi2c); |
|
- | 6231 | #else |
|
4586 | HAL_I2C_SlaveRxCpltCallback(hi2c); |
6232 | HAL_I2C_SlaveRxCpltCallback(hi2c); |
- | 6233 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4587 | } |
6234 | } |
4588 | } |
6235 | } |
4589 | } |
6236 | } |
4590 | return HAL_OK; |
- | |
4591 | } |
6237 | } |
4592 | 6238 | ||
4593 | /** |
6239 | /** |
4594 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
6240 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
4595 | * the configuration information for I2C module |
6241 | * the configuration information for I2C module |
4596 | * @retval HAL status |
6242 | * @retval None |
4597 | */ |
6243 | */ |
4598 | static HAL_StatusTypeDef I2C_Slave_AF(I2C_HandleTypeDef *hi2c) |
6244 | static void I2C_Slave_AF(I2C_HandleTypeDef *hi2c) |
4599 | { |
6245 | { |
4600 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
6246 | /* Declaration of temporary variables to prevent undefined behavior of volatile usage */ |
4601 | uint32_t CurrentState = hi2c->State; |
6247 | HAL_I2C_StateTypeDef CurrentState = hi2c->State; |
4602 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
6248 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
4603 | 6249 | ||
4604 | if(((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME)) && \ |
6250 | if (((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME)) && \ |
4605 | (CurrentState == HAL_I2C_STATE_LISTEN)) |
6251 | (CurrentState == HAL_I2C_STATE_LISTEN)) |
4606 | { |
6252 | { |
4607 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
6253 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
4608 | 6254 | ||
4609 | /* Disable EVT, BUF and ERR interrupt */ |
6255 | /* Disable EVT, BUF and ERR interrupt */ |
4610 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
6256 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
4611 | 6257 | ||
4612 | /* Clear AF flag */ |
6258 | /* Clear AF flag */ |
4613 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
6259 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
4614 | 6260 | ||
4615 | /* Disable Acknowledge */ |
6261 | /* Disable Acknowledge */ |
4616 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
6262 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4617 | 6263 | ||
4618 | hi2c->PreviousState = I2C_STATE_NONE; |
6264 | hi2c->PreviousState = I2C_STATE_NONE; |
4619 | hi2c->State = HAL_I2C_STATE_READY; |
6265 | hi2c->State = HAL_I2C_STATE_READY; |
4620 | hi2c->Mode = HAL_I2C_MODE_NONE; |
6266 | hi2c->Mode = HAL_I2C_MODE_NONE; |
4621 | 6267 | ||
4622 | /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */ |
6268 | /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */ |
- | 6269 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6270 | hi2c->ListenCpltCallback(hi2c); |
|
- | 6271 | #else |
|
4623 | HAL_I2C_ListenCpltCallback(hi2c); |
6272 | HAL_I2C_ListenCpltCallback(hi2c); |
- | 6273 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4624 | } |
6274 | } |
4625 | else if(CurrentState == HAL_I2C_STATE_BUSY_TX) |
6275 | else if (CurrentState == HAL_I2C_STATE_BUSY_TX) |
4626 | { |
6276 | { |
4627 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
6277 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
4628 | hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX; |
6278 | hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX; |
4629 | hi2c->State = HAL_I2C_STATE_READY; |
6279 | hi2c->State = HAL_I2C_STATE_READY; |
4630 | hi2c->Mode = HAL_I2C_MODE_NONE; |
6280 | hi2c->Mode = HAL_I2C_MODE_NONE; |
4631 | 6281 | ||
4632 | /* Disable EVT, BUF and ERR interrupt */ |
6282 | /* Disable EVT, BUF and ERR interrupt */ |
4633 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
6283 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
4634 | 6284 | ||
4635 | /* Clear AF flag */ |
6285 | /* Clear AF flag */ |
4636 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
6286 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
4637 | 6287 | ||
4638 | /* Disable Acknowledge */ |
6288 | /* Disable Acknowledge */ |
4639 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
6289 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4640 | 6290 | ||
- | 6291 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6292 | hi2c->SlaveTxCpltCallback(hi2c); |
|
- | 6293 | #else |
|
4641 | HAL_I2C_SlaveTxCpltCallback(hi2c); |
6294 | HAL_I2C_SlaveTxCpltCallback(hi2c); |
- | 6295 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4642 | } |
6296 | } |
4643 | else |
6297 | else |
4644 | { |
6298 | { |
4645 | /* Clear AF flag only */ |
6299 | /* Clear AF flag only */ |
4646 | /* State Listen, but XferOptions == FIRST or NEXT */ |
6300 | /* State Listen, but XferOptions == FIRST or NEXT */ |
4647 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
6301 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
4648 | } |
6302 | } |
4649 | - | ||
4650 | return HAL_OK; |
- | |
4651 | } |
6303 | } |
4652 | 6304 | ||
4653 | /** |
6305 | /** |
4654 | * @brief I2C interrupts error process |
6306 | * @brief I2C interrupts error process |
4655 | * @param hi2c I2C handle. |
6307 | * @param hi2c I2C handle. |
4656 | * @retval None |
6308 | * @retval None |
4657 | */ |
6309 | */ |
4658 | static void I2C_ITError(I2C_HandleTypeDef *hi2c) |
6310 | static void I2C_ITError(I2C_HandleTypeDef *hi2c) |
4659 | { |
6311 | { |
4660 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
6312 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
4661 | uint32_t CurrentState = hi2c->State; |
6313 | HAL_I2C_StateTypeDef CurrentState = hi2c->State; |
- | 6314 | HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode; |
|
- | 6315 | uint32_t CurrentError; |
|
4662 | 6316 | ||
4663 | if((CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN) || (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)) |
6317 | if (((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM)) && (CurrentState == HAL_I2C_STATE_BUSY_RX)) |
- | 6318 | { |
|
- | 6319 | /* Disable Pos bit in I2C CR1 when error occurred in Master/Mem Receive IT Process */ |
|
- | 6320 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
|
- | 6321 | } |
|
- | 6322 | ||
- | 6323 | if (((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) |
|
4664 | { |
6324 | { |
4665 | /* keep HAL_I2C_STATE_LISTEN */ |
6325 | /* keep HAL_I2C_STATE_LISTEN */ |
4666 | hi2c->PreviousState = I2C_STATE_NONE; |
6326 | hi2c->PreviousState = I2C_STATE_NONE; |
4667 | hi2c->State = HAL_I2C_STATE_LISTEN; |
6327 | hi2c->State = HAL_I2C_STATE_LISTEN; |
4668 | } |
6328 | } |
4669 | else |
6329 | else |
4670 | { |
6330 | { |
4671 | /* If state is an abort treatment on going, don't change state */ |
6331 | /* If state is an abort treatment on going, don't change state */ |
4672 | /* This change will be do later */ |
6332 | /* This change will be do later */ |
4673 | if((hi2c->State != HAL_I2C_STATE_ABORT) && ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) != I2C_CR2_DMAEN)) |
6333 | if ((READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN) && (CurrentState != HAL_I2C_STATE_ABORT)) |
4674 | { |
6334 | { |
4675 | hi2c->State = HAL_I2C_STATE_READY; |
6335 | hi2c->State = HAL_I2C_STATE_READY; |
- | 6336 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
4676 | } |
6337 | } |
4677 | hi2c->PreviousState = I2C_STATE_NONE; |
6338 | hi2c->PreviousState = I2C_STATE_NONE; |
4678 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | |
4679 | } |
6339 | } |
4680 | 6340 | ||
4681 | /* Disable Pos bit in I2C CR1 when error occurred in Master/Mem Receive IT Process */ |
- | |
4682 | hi2c->Instance->CR1 &= ~I2C_CR1_POS; |
- | |
4683 | - | ||
4684 | /* Abort DMA transfer */ |
6341 | /* Abort DMA transfer */ |
4685 | if((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
6342 | if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) == I2C_CR2_DMAEN) |
4686 | { |
6343 | { |
4687 | hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN; |
6344 | hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN; |
4688 | 6345 | ||
4689 | if(hi2c->hdmatx->State != HAL_DMA_STATE_READY) |
6346 | if (hi2c->hdmatx->State != HAL_DMA_STATE_READY) |
4690 | { |
6347 | { |
4691 | /* Set the DMA Abort callback : |
6348 | /* Set the DMA Abort callback : |
4692 | will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ |
6349 | will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ |
4693 | hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort; |
6350 | hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort; |
4694 | 6351 | ||
4695 | if(HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK) |
6352 | if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK) |
4696 | { |
6353 | { |
4697 | /* Disable I2C peripheral to prevent dummy data in buffer */ |
6354 | /* Disable I2C peripheral to prevent dummy data in buffer */ |
4698 | __HAL_I2C_DISABLE(hi2c); |
6355 | __HAL_I2C_DISABLE(hi2c); |
4699 | 6356 | ||
4700 | hi2c->State = HAL_I2C_STATE_READY; |
6357 | hi2c->State = HAL_I2C_STATE_READY; |
Line 4703... | Line 6360... | ||
4703 | hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx); |
6360 | hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx); |
4704 | } |
6361 | } |
4705 | } |
6362 | } |
4706 | else |
6363 | else |
4707 | { |
6364 | { |
4708 | /* Set the DMA Abort callback : |
6365 | /* Set the DMA Abort callback : |
4709 | will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ |
6366 | will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */ |
4710 | hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort; |
6367 | hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort; |
4711 | 6368 | ||
4712 | if(HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK) |
6369 | if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK) |
4713 | { |
6370 | { |
4714 | /* Store Last receive data if any */ |
6371 | /* Store Last receive data if any */ |
4715 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) |
6372 | if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) |
4716 | { |
6373 | { |
4717 | /* Read data from DR */ |
6374 | /* Read data from DR */ |
4718 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
6375 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 6376 | ||
- | 6377 | /* Increment Buffer pointer */ |
|
- | 6378 | hi2c->pBuffPtr++; |
|
4719 | } |
6379 | } |
4720 | 6380 | ||
4721 | /* Disable I2C peripheral to prevent dummy data in buffer */ |
6381 | /* Disable I2C peripheral to prevent dummy data in buffer */ |
4722 | __HAL_I2C_DISABLE(hi2c); |
6382 | __HAL_I2C_DISABLE(hi2c); |
4723 | 6383 | ||
Line 4726... | Line 6386... | ||
4726 | /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */ |
6386 | /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */ |
4727 | hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx); |
6387 | hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx); |
4728 | } |
6388 | } |
4729 | } |
6389 | } |
4730 | } |
6390 | } |
4731 | else if(hi2c->State == HAL_I2C_STATE_ABORT) |
6391 | else if (hi2c->State == HAL_I2C_STATE_ABORT) |
4732 | { |
6392 | { |
4733 | hi2c->State = HAL_I2C_STATE_READY; |
6393 | hi2c->State = HAL_I2C_STATE_READY; |
4734 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
6394 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
4735 | 6395 | ||
4736 | /* Store Last receive data if any */ |
6396 | /* Store Last receive data if any */ |
4737 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) |
6397 | if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) |
4738 | { |
6398 | { |
4739 | /* Read data from DR */ |
6399 | /* Read data from DR */ |
4740 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
6400 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 6401 | ||
- | 6402 | /* Increment Buffer pointer */ |
|
- | 6403 | hi2c->pBuffPtr++; |
|
4741 | } |
6404 | } |
4742 | 6405 | ||
4743 | /* Disable I2C peripheral to prevent dummy data in buffer */ |
6406 | /* Disable I2C peripheral to prevent dummy data in buffer */ |
4744 | __HAL_I2C_DISABLE(hi2c); |
6407 | __HAL_I2C_DISABLE(hi2c); |
4745 | 6408 | ||
4746 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
6409 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
- | 6410 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6411 | hi2c->AbortCpltCallback(hi2c); |
|
- | 6412 | #else |
|
4747 | HAL_I2C_AbortCpltCallback(hi2c); |
6413 | HAL_I2C_AbortCpltCallback(hi2c); |
- | 6414 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4748 | } |
6415 | } |
4749 | else |
6416 | else |
4750 | { |
6417 | { |
4751 | /* Store Last receive data if any */ |
6418 | /* Store Last receive data if any */ |
4752 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) |
6419 | if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET) |
4753 | { |
6420 | { |
4754 | /* Read data from DR */ |
6421 | /* Read data from DR */ |
4755 | (*hi2c->pBuffPtr++) = hi2c->Instance->DR; |
6422 | *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR; |
- | 6423 | ||
- | 6424 | /* Increment Buffer pointer */ |
|
- | 6425 | hi2c->pBuffPtr++; |
|
4756 | } |
6426 | } |
4757 | 6427 | ||
4758 | /* Call user error callback */ |
6428 | /* Call user error callback */ |
- | 6429 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6430 | hi2c->ErrorCallback(hi2c); |
|
- | 6431 | #else |
|
4759 | HAL_I2C_ErrorCallback(hi2c); |
6432 | HAL_I2C_ErrorCallback(hi2c); |
- | 6433 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
- | 6434 | } |
|
- | 6435 | ||
- | 6436 | /* STOP Flag is not set after a NACK reception, BusError, ArbitrationLost, OverRun */ |
|
- | 6437 | CurrentError = hi2c->ErrorCode; |
|
- | 6438 | ||
- | 6439 | if (((CurrentError & HAL_I2C_ERROR_BERR) == HAL_I2C_ERROR_BERR) || \ |
|
- | 6440 | ((CurrentError & HAL_I2C_ERROR_ARLO) == HAL_I2C_ERROR_ARLO) || \ |
|
- | 6441 | ((CurrentError & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF) || \ |
|
- | 6442 | ((CurrentError & HAL_I2C_ERROR_OVR) == HAL_I2C_ERROR_OVR)) |
|
- | 6443 | { |
|
- | 6444 | /* Disable EVT, BUF and ERR interrupt */ |
|
- | 6445 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR); |
|
4760 | } |
6446 | } |
4761 | /* STOP Flag is not set after a NACK reception */ |
- | |
- | 6447 | ||
4762 | /* So may inform upper layer that listen phase is stopped */ |
6448 | /* So may inform upper layer that listen phase is stopped */ |
4763 | /* during NACK error treatment */ |
6449 | /* during NACK error treatment */ |
- | 6450 | CurrentState = hi2c->State; |
|
4764 | if((hi2c->State == HAL_I2C_STATE_LISTEN) && ((hi2c->ErrorCode & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF)) |
6451 | if (((hi2c->ErrorCode & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF) && (CurrentState == HAL_I2C_STATE_LISTEN)) |
4765 | { |
6452 | { |
4766 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
6453 | hi2c->XferOptions = I2C_NO_OPTION_FRAME; |
4767 | hi2c->PreviousState = I2C_STATE_NONE; |
6454 | hi2c->PreviousState = I2C_STATE_NONE; |
4768 | hi2c->State = HAL_I2C_STATE_READY; |
6455 | hi2c->State = HAL_I2C_STATE_READY; |
4769 | hi2c->Mode = HAL_I2C_MODE_NONE; |
6456 | hi2c->Mode = HAL_I2C_MODE_NONE; |
4770 | 6457 | ||
4771 | /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */ |
6458 | /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */ |
- | 6459 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6460 | hi2c->ListenCpltCallback(hi2c); |
|
- | 6461 | #else |
|
4772 | HAL_I2C_ListenCpltCallback(hi2c); |
6462 | HAL_I2C_ListenCpltCallback(hi2c); |
- | 6463 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
4773 | } |
6464 | } |
4774 | } |
6465 | } |
4775 | 6466 | ||
4776 | /** |
6467 | /** |
4777 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
6468 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
Line 4786... | Line 6477... | ||
4786 | { |
6477 | { |
4787 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
6478 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
4788 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
6479 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
4789 | 6480 | ||
4790 | /* Generate Start condition if first transfer */ |
6481 | /* Generate Start condition if first transfer */ |
4791 | if((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME)) |
6482 | if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME)) |
4792 | { |
6483 | { |
4793 | /* Generate Start */ |
6484 | /* Generate Start */ |
4794 | hi2c->Instance->CR1 |= I2C_CR1_START; |
6485 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
4795 | } |
6486 | } |
4796 | else if(hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) |
6487 | else if (hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX) |
4797 | { |
6488 | { |
4798 | /* Generate ReStart */ |
6489 | /* Generate ReStart */ |
4799 | hi2c->Instance->CR1 |= I2C_CR1_START; |
6490 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
- | 6491 | } |
|
- | 6492 | else |
|
- | 6493 | { |
|
- | 6494 | /* Do nothing */ |
|
4800 | } |
6495 | } |
4801 | 6496 | ||
4802 | /* Wait until SB flag is set */ |
6497 | /* Wait until SB flag is set */ |
4803 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
6498 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
4804 | { |
6499 | { |
- | 6500 | if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START) |
|
- | 6501 | { |
|
- | 6502 | hi2c->ErrorCode = HAL_I2C_WRONG_START; |
|
- | 6503 | } |
|
4805 | return HAL_TIMEOUT; |
6504 | return HAL_TIMEOUT; |
4806 | } |
6505 | } |
4807 | 6506 | ||
4808 | if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT) |
6507 | if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT) |
4809 | { |
6508 | { |
4810 | /* Send slave address */ |
6509 | /* Send slave address */ |
4811 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress); |
6510 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress); |
4812 | } |
6511 | } |
4813 | else |
6512 | else |
4814 | { |
6513 | { |
4815 | /* Send header of slave address */ |
6514 | /* Send header of slave address */ |
4816 | hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress); |
6515 | hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress); |
4817 | 6516 | ||
4818 | /* Wait until ADD10 flag is set */ |
6517 | /* Wait until ADD10 flag is set */ |
4819 | if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK) |
6518 | if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK) |
4820 | { |
6519 | { |
4821 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
4822 | { |
- | |
4823 | return HAL_ERROR; |
6520 | return HAL_ERROR; |
4824 | } |
- | |
4825 | else |
- | |
4826 | { |
- | |
4827 | return HAL_TIMEOUT; |
- | |
4828 | } |
- | |
4829 | } |
6521 | } |
4830 | 6522 | ||
4831 | /* Send slave address */ |
6523 | /* Send slave address */ |
4832 | hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress); |
6524 | hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress); |
4833 | } |
6525 | } |
4834 | 6526 | ||
4835 | /* Wait until ADDR flag is set */ |
6527 | /* Wait until ADDR flag is set */ |
4836 | if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
6528 | if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
4837 | { |
6529 | { |
4838 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
4839 | { |
- | |
4840 | return HAL_ERROR; |
6530 | return HAL_ERROR; |
4841 | } |
- | |
4842 | else |
- | |
4843 | { |
- | |
4844 | return HAL_TIMEOUT; |
- | |
4845 | } |
- | |
4846 | } |
6531 | } |
4847 | 6532 | ||
4848 | return HAL_OK; |
6533 | return HAL_OK; |
4849 | } |
6534 | } |
4850 | 6535 | ||
Line 4862... | Line 6547... | ||
4862 | { |
6547 | { |
4863 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
6548 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
4864 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
6549 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
4865 | 6550 | ||
4866 | /* Enable Acknowledge */ |
6551 | /* Enable Acknowledge */ |
4867 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
6552 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
4868 | 6553 | ||
4869 | /* Generate Start condition if first transfer */ |
6554 | /* Generate Start condition if first transfer */ |
4870 | if((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME)) |
6555 | if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME)) |
4871 | { |
6556 | { |
4872 | /* Generate Start */ |
6557 | /* Generate Start */ |
4873 | hi2c->Instance->CR1 |= I2C_CR1_START; |
6558 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
4874 | } |
6559 | } |
4875 | else if(hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) |
6560 | else if (hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX) |
4876 | { |
6561 | { |
4877 | /* Generate ReStart */ |
6562 | /* Generate ReStart */ |
4878 | hi2c->Instance->CR1 |= I2C_CR1_START; |
6563 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
- | 6564 | } |
|
- | 6565 | else |
|
- | 6566 | { |
|
- | 6567 | /* Do nothing */ |
|
4879 | } |
6568 | } |
4880 | 6569 | ||
4881 | /* Wait until SB flag is set */ |
6570 | /* Wait until SB flag is set */ |
4882 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
6571 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
4883 | { |
6572 | { |
- | 6573 | if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START) |
|
- | 6574 | { |
|
- | 6575 | hi2c->ErrorCode = HAL_I2C_WRONG_START; |
|
- | 6576 | } |
|
4884 | return HAL_TIMEOUT; |
6577 | return HAL_TIMEOUT; |
4885 | } |
6578 | } |
4886 | 6579 | ||
4887 | if(hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT) |
6580 | if (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_7BIT) |
4888 | { |
6581 | { |
4889 | /* Send slave address */ |
6582 | /* Send slave address */ |
4890 | hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress); |
6583 | hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress); |
4891 | } |
6584 | } |
4892 | else |
6585 | else |
4893 | { |
6586 | { |
4894 | /* Send header of slave address */ |
6587 | /* Send header of slave address */ |
4895 | hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress); |
6588 | hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress); |
4896 | 6589 | ||
4897 | /* Wait until ADD10 flag is set */ |
6590 | /* Wait until ADD10 flag is set */ |
4898 | if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK) |
6591 | if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK) |
4899 | { |
6592 | { |
4900 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
4901 | { |
- | |
4902 | return HAL_ERROR; |
6593 | return HAL_ERROR; |
4903 | } |
- | |
4904 | else |
- | |
4905 | { |
- | |
4906 | return HAL_TIMEOUT; |
- | |
4907 | } |
- | |
4908 | } |
6594 | } |
4909 | 6595 | ||
4910 | /* Send slave address */ |
6596 | /* Send slave address */ |
4911 | hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress); |
6597 | hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress); |
4912 | 6598 | ||
4913 | /* Wait until ADDR flag is set */ |
6599 | /* Wait until ADDR flag is set */ |
4914 | if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
6600 | if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
4915 | { |
6601 | { |
4916 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
4917 | { |
- | |
4918 | return HAL_ERROR; |
6602 | return HAL_ERROR; |
4919 | } |
- | |
4920 | else |
- | |
4921 | { |
- | |
4922 | return HAL_TIMEOUT; |
- | |
4923 | } |
- | |
4924 | } |
6603 | } |
4925 | 6604 | ||
4926 | /* Clear ADDR flag */ |
6605 | /* Clear ADDR flag */ |
4927 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
6606 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4928 | 6607 | ||
4929 | /* Generate Restart */ |
6608 | /* Generate Restart */ |
4930 | hi2c->Instance->CR1 |= I2C_CR1_START; |
6609 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
4931 | 6610 | ||
4932 | /* Wait until SB flag is set */ |
6611 | /* Wait until SB flag is set */ |
4933 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
6612 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
4934 | { |
6613 | { |
- | 6614 | if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START) |
|
- | 6615 | { |
|
- | 6616 | hi2c->ErrorCode = HAL_I2C_WRONG_START; |
|
- | 6617 | } |
|
4935 | return HAL_TIMEOUT; |
6618 | return HAL_TIMEOUT; |
4936 | } |
6619 | } |
4937 | 6620 | ||
4938 | /* Send header of slave address */ |
6621 | /* Send header of slave address */ |
4939 | hi2c->Instance->DR = I2C_10BIT_HEADER_READ(DevAddress); |
6622 | hi2c->Instance->DR = I2C_10BIT_HEADER_READ(DevAddress); |
4940 | } |
6623 | } |
4941 | 6624 | ||
4942 | /* Wait until ADDR flag is set */ |
6625 | /* Wait until ADDR flag is set */ |
4943 | if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
6626 | if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
4944 | { |
6627 | { |
4945 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
4946 | { |
- | |
4947 | return HAL_ERROR; |
6628 | return HAL_ERROR; |
4948 | } |
- | |
4949 | else |
- | |
4950 | { |
- | |
4951 | return HAL_TIMEOUT; |
- | |
4952 | } |
- | |
4953 | } |
6629 | } |
4954 | 6630 | ||
4955 | return HAL_OK; |
6631 | return HAL_OK; |
4956 | } |
6632 | } |
4957 | 6633 | ||
Line 4968... | Line 6644... | ||
4968 | * @retval HAL status |
6644 | * @retval HAL status |
4969 | */ |
6645 | */ |
4970 | static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart) |
6646 | static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart) |
4971 | { |
6647 | { |
4972 | /* Generate Start */ |
6648 | /* Generate Start */ |
4973 | hi2c->Instance->CR1 |= I2C_CR1_START; |
6649 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
4974 | 6650 | ||
4975 | /* Wait until SB flag is set */ |
6651 | /* Wait until SB flag is set */ |
4976 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
6652 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
4977 | { |
6653 | { |
- | 6654 | if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START) |
|
- | 6655 | { |
|
- | 6656 | hi2c->ErrorCode = HAL_I2C_WRONG_START; |
|
- | 6657 | } |
|
4978 | return HAL_TIMEOUT; |
6658 | return HAL_TIMEOUT; |
4979 | } |
6659 | } |
4980 | 6660 | ||
4981 | /* Send slave address */ |
6661 | /* Send slave address */ |
4982 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress); |
6662 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress); |
4983 | 6663 | ||
4984 | /* Wait until ADDR flag is set */ |
6664 | /* Wait until ADDR flag is set */ |
4985 | if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
6665 | if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
4986 | { |
6666 | { |
4987 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
4988 | { |
- | |
4989 | return HAL_ERROR; |
6667 | return HAL_ERROR; |
4990 | } |
- | |
4991 | else |
- | |
4992 | { |
- | |
4993 | return HAL_TIMEOUT; |
- | |
4994 | } |
- | |
4995 | } |
6668 | } |
4996 | 6669 | ||
4997 | /* Clear ADDR flag */ |
6670 | /* Clear ADDR flag */ |
4998 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
6671 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
4999 | 6672 | ||
5000 | /* Wait until TXE flag is set */ |
6673 | /* Wait until TXE flag is set */ |
5001 | if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) |
6674 | if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) |
5002 | { |
6675 | { |
5003 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
6676 | if (hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
5004 | { |
6677 | { |
5005 | /* Generate Stop */ |
6678 | /* Generate Stop */ |
5006 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
6679 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
5007 | return HAL_ERROR; |
- | |
5008 | } |
- | |
5009 | else |
- | |
5010 | { |
- | |
5011 | return HAL_TIMEOUT; |
- | |
5012 | } |
6680 | } |
- | 6681 | return HAL_ERROR; |
|
5013 | } |
6682 | } |
5014 | 6683 | ||
5015 | /* If Memory address size is 8Bit */ |
6684 | /* If Memory address size is 8Bit */ |
5016 | if(MemAddSize == I2C_MEMADD_SIZE_8BIT) |
6685 | if (MemAddSize == I2C_MEMADD_SIZE_8BIT) |
5017 | { |
6686 | { |
5018 | /* Send Memory Address */ |
6687 | /* Send Memory Address */ |
5019 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress); |
6688 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress); |
5020 | } |
6689 | } |
5021 | /* If Memory address size is 16Bit */ |
6690 | /* If Memory address size is 16Bit */ |
Line 5023... | Line 6692... | ||
5023 | { |
6692 | { |
5024 | /* Send MSB of Memory Address */ |
6693 | /* Send MSB of Memory Address */ |
5025 | hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress); |
6694 | hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress); |
5026 | 6695 | ||
5027 | /* Wait until TXE flag is set */ |
6696 | /* Wait until TXE flag is set */ |
5028 | if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) |
6697 | if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) |
5029 | { |
6698 | { |
5030 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
6699 | if (hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
5031 | { |
6700 | { |
5032 | /* Generate Stop */ |
6701 | /* Generate Stop */ |
5033 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
6702 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
5034 | return HAL_ERROR; |
- | |
5035 | } |
- | |
5036 | else |
- | |
5037 | { |
- | |
5038 | return HAL_TIMEOUT; |
- | |
5039 | } |
6703 | } |
- | 6704 | return HAL_ERROR; |
|
5040 | } |
6705 | } |
5041 | 6706 | ||
5042 | /* Send LSB of Memory Address */ |
6707 | /* Send LSB of Memory Address */ |
5043 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress); |
6708 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress); |
5044 | } |
6709 | } |
Line 5059... | Line 6724... | ||
5059 | * @retval HAL status |
6724 | * @retval HAL status |
5060 | */ |
6725 | */ |
5061 | static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart) |
6726 | static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart) |
5062 | { |
6727 | { |
5063 | /* Enable Acknowledge */ |
6728 | /* Enable Acknowledge */ |
5064 | hi2c->Instance->CR1 |= I2C_CR1_ACK; |
6729 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
5065 | 6730 | ||
5066 | /* Generate Start */ |
6731 | /* Generate Start */ |
5067 | hi2c->Instance->CR1 |= I2C_CR1_START; |
6732 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
5068 | 6733 | ||
5069 | /* Wait until SB flag is set */ |
6734 | /* Wait until SB flag is set */ |
5070 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
6735 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
5071 | { |
6736 | { |
- | 6737 | if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START) |
|
- | 6738 | { |
|
- | 6739 | hi2c->ErrorCode = HAL_I2C_WRONG_START; |
|
- | 6740 | } |
|
5072 | return HAL_TIMEOUT; |
6741 | return HAL_TIMEOUT; |
5073 | } |
6742 | } |
5074 | 6743 | ||
5075 | /* Send slave address */ |
6744 | /* Send slave address */ |
5076 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress); |
6745 | hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress); |
5077 | 6746 | ||
5078 | /* Wait until ADDR flag is set */ |
6747 | /* Wait until ADDR flag is set */ |
5079 | if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
6748 | if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
5080 | { |
6749 | { |
5081 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
5082 | { |
- | |
5083 | return HAL_ERROR; |
6750 | return HAL_ERROR; |
5084 | } |
- | |
5085 | else |
- | |
5086 | { |
- | |
5087 | return HAL_TIMEOUT; |
- | |
5088 | } |
- | |
5089 | } |
6751 | } |
5090 | 6752 | ||
5091 | /* Clear ADDR flag */ |
6753 | /* Clear ADDR flag */ |
5092 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
6754 | __HAL_I2C_CLEAR_ADDRFLAG(hi2c); |
5093 | 6755 | ||
5094 | /* Wait until TXE flag is set */ |
6756 | /* Wait until TXE flag is set */ |
5095 | if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) |
6757 | if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) |
5096 | { |
6758 | { |
5097 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
6759 | if (hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
5098 | { |
6760 | { |
5099 | /* Generate Stop */ |
6761 | /* Generate Stop */ |
5100 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
6762 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
5101 | return HAL_ERROR; |
- | |
5102 | } |
- | |
5103 | else |
- | |
5104 | { |
- | |
5105 | return HAL_TIMEOUT; |
- | |
5106 | } |
6763 | } |
- | 6764 | return HAL_ERROR; |
|
5107 | } |
6765 | } |
5108 | 6766 | ||
5109 | /* If Memory address size is 8Bit */ |
6767 | /* If Memory address size is 8Bit */ |
5110 | if(MemAddSize == I2C_MEMADD_SIZE_8BIT) |
6768 | if (MemAddSize == I2C_MEMADD_SIZE_8BIT) |
5111 | { |
6769 | { |
5112 | /* Send Memory Address */ |
6770 | /* Send Memory Address */ |
5113 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress); |
6771 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress); |
5114 | } |
6772 | } |
5115 | /* If Memory address size is 16Bit */ |
6773 | /* If Memory address size is 16Bit */ |
Line 5117... | Line 6775... | ||
5117 | { |
6775 | { |
5118 | /* Send MSB of Memory Address */ |
6776 | /* Send MSB of Memory Address */ |
5119 | hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress); |
6777 | hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress); |
5120 | 6778 | ||
5121 | /* Wait until TXE flag is set */ |
6779 | /* Wait until TXE flag is set */ |
5122 | if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) |
6780 | if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) |
5123 | { |
6781 | { |
5124 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
6782 | if (hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
5125 | { |
6783 | { |
5126 | /* Generate Stop */ |
6784 | /* Generate Stop */ |
5127 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
6785 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
5128 | return HAL_ERROR; |
- | |
5129 | } |
- | |
5130 | else |
- | |
5131 | { |
- | |
5132 | return HAL_TIMEOUT; |
- | |
5133 | } |
6786 | } |
- | 6787 | return HAL_ERROR; |
|
5134 | } |
6788 | } |
5135 | 6789 | ||
5136 | /* Send LSB of Memory Address */ |
6790 | /* Send LSB of Memory Address */ |
5137 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress); |
6791 | hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress); |
5138 | } |
6792 | } |
5139 | 6793 | ||
5140 | /* Wait until TXE flag is set */ |
6794 | /* Wait until TXE flag is set */ |
5141 | if(I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) |
6795 | if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK) |
5142 | { |
6796 | { |
5143 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
6797 | if (hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
5144 | { |
6798 | { |
5145 | /* Generate Stop */ |
6799 | /* Generate Stop */ |
5146 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
6800 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
5147 | return HAL_ERROR; |
- | |
5148 | } |
- | |
5149 | else |
- | |
5150 | { |
- | |
5151 | return HAL_TIMEOUT; |
- | |
5152 | } |
6801 | } |
- | 6802 | return HAL_ERROR; |
|
5153 | } |
6803 | } |
5154 | 6804 | ||
5155 | /* Generate Restart */ |
6805 | /* Generate Restart */ |
5156 | hi2c->Instance->CR1 |= I2C_CR1_START; |
6806 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_START); |
5157 | 6807 | ||
5158 | /* Wait until SB flag is set */ |
6808 | /* Wait until SB flag is set */ |
5159 | if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
6809 | if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK) |
5160 | { |
6810 | { |
- | 6811 | if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START) |
|
- | 6812 | { |
|
- | 6813 | hi2c->ErrorCode = HAL_I2C_WRONG_START; |
|
- | 6814 | } |
|
5161 | return HAL_TIMEOUT; |
6815 | return HAL_TIMEOUT; |
5162 | } |
6816 | } |
5163 | 6817 | ||
5164 | /* Send slave address */ |
6818 | /* Send slave address */ |
5165 | hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress); |
6819 | hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress); |
5166 | 6820 | ||
5167 | /* Wait until ADDR flag is set */ |
6821 | /* Wait until ADDR flag is set */ |
5168 | if(I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
6822 | if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK) |
5169 | { |
6823 | { |
5170 | if(hi2c->ErrorCode == HAL_I2C_ERROR_AF) |
- | |
5171 | { |
- | |
5172 | return HAL_ERROR; |
6824 | return HAL_ERROR; |
5173 | } |
- | |
5174 | else |
- | |
5175 | { |
- | |
5176 | return HAL_TIMEOUT; |
- | |
5177 | } |
- | |
5178 | } |
6825 | } |
5179 | 6826 | ||
5180 | return HAL_OK; |
6827 | return HAL_OK; |
5181 | } |
6828 | } |
5182 | 6829 | ||
Line 5185... | Line 6832... | ||
5185 | * @param hdma DMA handle |
6832 | * @param hdma DMA handle |
5186 | * @retval None |
6833 | * @retval None |
5187 | */ |
6834 | */ |
5188 | static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma) |
6835 | static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma) |
5189 | { |
6836 | { |
5190 | I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
6837 | I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ |
5191 | 6838 | ||
5192 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
6839 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
5193 | uint32_t CurrentState = hi2c->State; |
6840 | HAL_I2C_StateTypeDef CurrentState = hi2c->State; |
5194 | uint32_t CurrentMode = hi2c->Mode; |
6841 | HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode; |
- | 6842 | uint32_t CurrentXferOptions = hi2c->XferOptions; |
|
5195 | 6843 | ||
- | 6844 | /* Disable EVT and ERR interrupt */ |
|
- | 6845 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
|
- | 6846 | ||
- | 6847 | /* Clear Complete callback */ |
|
- | 6848 | if (hi2c->hdmatx != NULL) |
|
- | 6849 | { |
|
- | 6850 | hi2c->hdmatx->XferCpltCallback = NULL; |
|
- | 6851 | } |
|
- | 6852 | if (hi2c->hdmarx != NULL) |
|
- | 6853 | { |
|
- | 6854 | hi2c->hdmarx->XferCpltCallback = NULL; |
|
- | 6855 | } |
|
- | 6856 | ||
5196 | if((CurrentState == HAL_I2C_STATE_BUSY_TX) || ((CurrentState == HAL_I2C_STATE_BUSY_RX) && (CurrentMode == HAL_I2C_MODE_SLAVE))) |
6857 | if ((((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_BUSY_TX) == (uint32_t)HAL_I2C_STATE_BUSY_TX) || ((((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_BUSY_RX) == (uint32_t)HAL_I2C_STATE_BUSY_RX) && (CurrentMode == HAL_I2C_MODE_SLAVE))) |
5197 | { |
6858 | { |
5198 | /* Disable DMA Request */ |
6859 | /* Disable DMA Request */ |
5199 | hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN; |
6860 | CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
5200 | 6861 | ||
5201 | hi2c->XferCount = 0U; |
6862 | hi2c->XferCount = 0U; |
- | 6863 | ||
- | 6864 | if (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN) |
|
5202 | 6865 | { |
|
- | 6866 | /* Set state at HAL_I2C_STATE_LISTEN */ |
|
- | 6867 | hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX; |
|
- | 6868 | hi2c->State = HAL_I2C_STATE_LISTEN; |
|
- | 6869 | ||
- | 6870 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
|
- | 6871 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6872 | hi2c->SlaveTxCpltCallback(hi2c); |
|
- | 6873 | #else |
|
- | 6874 | HAL_I2C_SlaveTxCpltCallback(hi2c); |
|
- | 6875 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
- | 6876 | } |
|
- | 6877 | else if (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN) |
|
- | 6878 | { |
|
- | 6879 | /* Set state at HAL_I2C_STATE_LISTEN */ |
|
- | 6880 | hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX; |
|
- | 6881 | hi2c->State = HAL_I2C_STATE_LISTEN; |
|
- | 6882 | ||
- | 6883 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
|
- | 6884 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6885 | hi2c->SlaveRxCpltCallback(hi2c); |
|
- | 6886 | #else |
|
- | 6887 | HAL_I2C_SlaveRxCpltCallback(hi2c); |
|
- | 6888 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
- | 6889 | } |
|
- | 6890 | else |
|
- | 6891 | { |
|
- | 6892 | /* Do nothing */ |
|
- | 6893 | } |
|
- | 6894 | ||
5203 | /* Enable EVT and ERR interrupt */ |
6895 | /* Enable EVT and ERR interrupt to treat end of transfer in IRQ handler */ |
5204 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
6896 | __HAL_I2C_ENABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
5205 | } |
6897 | } |
- | 6898 | /* Check current Mode, in case of treatment DMA handler have been preempted by a prior interrupt */ |
|
5206 | else |
6899 | else if (hi2c->Mode != HAL_I2C_MODE_NONE) |
5207 | { |
6900 | { |
- | 6901 | if (hi2c->XferCount == (uint16_t)1) |
|
- | 6902 | { |
|
5208 | /* Disable Acknowledge */ |
6903 | /* Disable Acknowledge */ |
5209 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
6904 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
5210 | 6905 | } |
|
- | 6906 | ||
- | 6907 | /* Disable EVT and ERR interrupt */ |
|
- | 6908 | __HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR); |
|
- | 6909 | ||
- | 6910 | /* Prepare next transfer or stop current transfer */ |
|
- | 6911 | if ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_OTHER_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME)) |
|
- | 6912 | { |
|
5211 | /* Generate Stop */ |
6913 | /* Generate Stop */ |
5212 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
6914 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
5213 | 6915 | } |
|
- | 6916 | ||
5214 | /* Disable Last DMA */ |
6917 | /* Disable Last DMA */ |
5215 | hi2c->Instance->CR2 &= ~I2C_CR2_LAST; |
6918 | CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST); |
5216 | 6919 | ||
5217 | /* Disable DMA Request */ |
6920 | /* Disable DMA Request */ |
5218 | hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN; |
6921 | CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN); |
5219 | 6922 | ||
5220 | hi2c->XferCount = 0U; |
6923 | hi2c->XferCount = 0U; |
5221 | 6924 | ||
5222 | /* Check if Errors has been detected during transfer */ |
6925 | /* Check if Errors has been detected during transfer */ |
5223 | if(hi2c->ErrorCode != HAL_I2C_ERROR_NONE) |
6926 | if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE) |
5224 | { |
6927 | { |
- | 6928 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6929 | hi2c->ErrorCallback(hi2c); |
|
- | 6930 | #else |
|
5225 | HAL_I2C_ErrorCallback(hi2c); |
6931 | HAL_I2C_ErrorCallback(hi2c); |
- | 6932 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
5226 | } |
6933 | } |
5227 | else |
6934 | else |
5228 | { |
6935 | { |
5229 | hi2c->State = HAL_I2C_STATE_READY; |
6936 | hi2c->State = HAL_I2C_STATE_READY; |
5230 | 6937 | ||
5231 | if(hi2c->Mode == HAL_I2C_MODE_MEM) |
6938 | if (hi2c->Mode == HAL_I2C_MODE_MEM) |
5232 | { |
6939 | { |
5233 | hi2c->Mode = HAL_I2C_MODE_NONE; |
6940 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 6941 | hi2c->PreviousState = I2C_STATE_NONE; |
|
5234 | 6942 | ||
- | 6943 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6944 | hi2c->MemRxCpltCallback(hi2c); |
|
- | 6945 | #else |
|
5235 | HAL_I2C_MemRxCpltCallback(hi2c); |
6946 | HAL_I2C_MemRxCpltCallback(hi2c); |
- | 6947 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
5236 | } |
6948 | } |
5237 | else |
6949 | else |
5238 | { |
6950 | { |
5239 | hi2c->Mode = HAL_I2C_MODE_NONE; |
6951 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 6952 | hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX; |
|
5240 | 6953 | ||
- | 6954 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 6955 | hi2c->MasterRxCpltCallback(hi2c); |
|
- | 6956 | #else |
|
5241 | HAL_I2C_MasterRxCpltCallback(hi2c); |
6957 | HAL_I2C_MasterRxCpltCallback(hi2c); |
- | 6958 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
5242 | } |
6959 | } |
5243 | } |
6960 | } |
5244 | } |
6961 | } |
- | 6962 | else |
|
- | 6963 | { |
|
- | 6964 | /* Do nothing */ |
|
- | 6965 | } |
|
5245 | } |
6966 | } |
5246 | 6967 | ||
5247 | /** |
6968 | /** |
5248 | * @brief DMA I2C communication error callback. |
6969 | * @brief DMA I2C communication error callback. |
5249 | * @param hdma DMA handle |
6970 | * @param hdma DMA handle |
5250 | * @retval None |
6971 | * @retval None |
5251 | */ |
6972 | */ |
5252 | static void I2C_DMAError(DMA_HandleTypeDef *hdma) |
6973 | static void I2C_DMAError(DMA_HandleTypeDef *hdma) |
5253 | { |
6974 | { |
5254 | I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; |
6975 | I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ |
- | 6976 | ||
- | 6977 | /* Clear Complete callback */ |
|
- | 6978 | if (hi2c->hdmatx != NULL) |
|
- | 6979 | { |
|
- | 6980 | hi2c->hdmatx->XferCpltCallback = NULL; |
|
- | 6981 | } |
|
- | 6982 | if (hi2c->hdmarx != NULL) |
|
- | 6983 | { |
|
- | 6984 | hi2c->hdmarx->XferCpltCallback = NULL; |
|
5255 | 6985 | } |
|
- | 6986 | ||
5256 | /* Disable Acknowledge */ |
6987 | /* Disable Acknowledge */ |
5257 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
6988 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
5258 | 6989 | ||
5259 | hi2c->XferCount = 0U; |
6990 | hi2c->XferCount = 0U; |
5260 | - | ||
5261 | hi2c->State = HAL_I2C_STATE_READY; |
6991 | hi2c->State = HAL_I2C_STATE_READY; |
5262 | hi2c->Mode = HAL_I2C_MODE_NONE; |
6992 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 6993 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
|
5263 | 6994 | ||
- | 6995 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
5264 | hi2c->ErrorCode |= HAL_I2C_ERROR_DMA; |
6996 | hi2c->ErrorCallback(hi2c); |
5265 | 6997 | #else |
|
5266 | HAL_I2C_ErrorCallback(hi2c); |
6998 | HAL_I2C_ErrorCallback(hi2c); |
- | 6999 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
5267 | } |
7000 | } |
5268 | 7001 | ||
5269 | /** |
7002 | /** |
5270 | * @brief DMA I2C communication abort callback |
7003 | * @brief DMA I2C communication abort callback |
5271 | * (To be called at end of DMA Abort procedure). |
7004 | * (To be called at end of DMA Abort procedure). |
5272 | * @param hdma: DMA handle. |
7005 | * @param hdma DMA handle. |
5273 | * @retval None |
7006 | * @retval None |
5274 | */ |
7007 | */ |
5275 | static void I2C_DMAAbort(DMA_HandleTypeDef *hdma) |
7008 | static void I2C_DMAAbort(DMA_HandleTypeDef *hdma) |
5276 | { |
7009 | { |
- | 7010 | __IO uint32_t count = 0U; |
|
5277 | I2C_HandleTypeDef* hi2c = ( I2C_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
7011 | I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */ |
- | 7012 | ||
- | 7013 | /* Declaration of temporary variable to prevent undefined behavior of volatile usage */ |
|
- | 7014 | HAL_I2C_StateTypeDef CurrentState = hi2c->State; |
|
- | 7015 | ||
- | 7016 | /* During abort treatment, check that there is no pending STOP request */ |
|
- | 7017 | /* Wait until STOP flag is reset */ |
|
- | 7018 | count = I2C_TIMEOUT_FLAG * (SystemCoreClock / 25U / 1000U); |
|
- | 7019 | do |
|
- | 7020 | { |
|
- | 7021 | if (count == 0U) |
|
- | 7022 | { |
|
- | 7023 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
- | 7024 | break; |
|
- | 7025 | } |
|
- | 7026 | count--; |
|
- | 7027 | } |
|
- | 7028 | while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP); |
|
- | 7029 | ||
- | 7030 | /* Clear Complete callback */ |
|
- | 7031 | if (hi2c->hdmatx != NULL) |
|
- | 7032 | { |
|
- | 7033 | hi2c->hdmatx->XferCpltCallback = NULL; |
|
- | 7034 | } |
|
- | 7035 | if (hi2c->hdmarx != NULL) |
|
- | 7036 | { |
|
- | 7037 | hi2c->hdmarx->XferCpltCallback = NULL; |
|
5278 | 7038 | } |
|
- | 7039 | ||
5279 | /* Disable Acknowledge */ |
7040 | /* Disable Acknowledge */ |
5280 | hi2c->Instance->CR1 &= ~I2C_CR1_ACK; |
7041 | CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
5281 | 7042 | ||
5282 | hi2c->XferCount = 0U; |
7043 | hi2c->XferCount = 0U; |
5283 | 7044 | ||
5284 | /* Reset XferAbortCallback */ |
7045 | /* Reset XferAbortCallback */ |
- | 7046 | if (hi2c->hdmatx != NULL) |
|
- | 7047 | { |
|
5285 | hi2c->hdmatx->XferAbortCallback = NULL; |
7048 | hi2c->hdmatx->XferAbortCallback = NULL; |
- | 7049 | } |
|
- | 7050 | if (hi2c->hdmarx != NULL) |
|
- | 7051 | { |
|
5286 | hi2c->hdmarx->XferAbortCallback = NULL; |
7052 | hi2c->hdmarx->XferAbortCallback = NULL; |
- | 7053 | } |
|
- | 7054 | ||
- | 7055 | /* Disable I2C peripheral to prevent dummy data in buffer */ |
|
- | 7056 | __HAL_I2C_DISABLE(hi2c); |
|
5287 | 7057 | ||
5288 | /* Check if come from abort from user */ |
7058 | /* Check if come from abort from user */ |
5289 | if(hi2c->State == HAL_I2C_STATE_ABORT) |
7059 | if (hi2c->State == HAL_I2C_STATE_ABORT) |
5290 | { |
7060 | { |
5291 | hi2c->State = HAL_I2C_STATE_READY; |
7061 | hi2c->State = HAL_I2C_STATE_READY; |
5292 | hi2c->Mode = HAL_I2C_MODE_NONE; |
7062 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5293 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
7063 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
5294 | - | ||
5295 | /* Disable I2C peripheral to prevent dummy data in buffer */ |
- | |
5296 | __HAL_I2C_DISABLE(hi2c); |
- | |
5297 | 7064 | ||
5298 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
7065 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
- | 7066 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 7067 | hi2c->AbortCpltCallback(hi2c); |
|
- | 7068 | #else |
|
5299 | HAL_I2C_AbortCpltCallback(hi2c); |
7069 | HAL_I2C_AbortCpltCallback(hi2c); |
- | 7070 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
5300 | } |
7071 | } |
5301 | else |
7072 | else |
5302 | { |
7073 | { |
- | 7074 | if (((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN) |
|
- | 7075 | { |
|
5303 | hi2c->State = HAL_I2C_STATE_READY; |
7076 | /* Renable I2C peripheral */ |
5304 | hi2c->Mode = HAL_I2C_MODE_NONE; |
7077 | __HAL_I2C_ENABLE(hi2c); |
5305 | 7078 | ||
- | 7079 | /* Enable Acknowledge */ |
|
5306 | /* Disable I2C peripheral to prevent dummy data in buffer */ |
7080 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK); |
- | 7081 | ||
5307 | __HAL_I2C_DISABLE(hi2c); |
7082 | /* keep HAL_I2C_STATE_LISTEN */ |
- | 7083 | hi2c->PreviousState = I2C_STATE_NONE; |
|
- | 7084 | hi2c->State = HAL_I2C_STATE_LISTEN; |
|
- | 7085 | } |
|
- | 7086 | else |
|
- | 7087 | { |
|
- | 7088 | hi2c->State = HAL_I2C_STATE_READY; |
|
- | 7089 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 7090 | } |
|
5308 | 7091 | ||
5309 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
7092 | /* Call the corresponding callback to inform upper layer of End of Transfer */ |
- | 7093 | #if (USE_HAL_I2C_REGISTER_CALLBACKS == 1) |
|
- | 7094 | hi2c->ErrorCallback(hi2c); |
|
- | 7095 | #else |
|
5310 | HAL_I2C_ErrorCallback(hi2c); |
7096 | HAL_I2C_ErrorCallback(hi2c); |
- | 7097 | #endif /* USE_HAL_I2C_REGISTER_CALLBACKS */ |
|
5311 | } |
7098 | } |
5312 | } |
7099 | } |
5313 | 7100 | ||
5314 | /** |
7101 | /** |
5315 | * @brief This function handles I2C Communication Timeout. |
7102 | * @brief This function handles I2C Communication Timeout. |
Line 5322... | Line 7109... | ||
5322 | * @retval HAL status |
7109 | * @retval HAL status |
5323 | */ |
7110 | */ |
5324 | static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart) |
7111 | static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart) |
5325 | { |
7112 | { |
5326 | /* Wait until flag is set */ |
7113 | /* Wait until flag is set */ |
5327 | while((__HAL_I2C_GET_FLAG(hi2c, Flag) ? SET : RESET) == Status) |
7114 | while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status) |
5328 | { |
7115 | { |
5329 | /* Check for the Timeout */ |
7116 | /* Check for the Timeout */ |
5330 | if(Timeout != HAL_MAX_DELAY) |
7117 | if (Timeout != HAL_MAX_DELAY) |
5331 | { |
7118 | { |
5332 | if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout)) |
7119 | if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) |
5333 | { |
7120 | { |
5334 | hi2c->PreviousState = I2C_STATE_NONE; |
7121 | hi2c->PreviousState = I2C_STATE_NONE; |
5335 | hi2c->State= HAL_I2C_STATE_READY; |
7122 | hi2c->State = HAL_I2C_STATE_READY; |
5336 | hi2c->Mode = HAL_I2C_MODE_NONE; |
7123 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 7124 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
5337 | 7125 | ||
5338 | /* Process Unlocked */ |
7126 | /* Process Unlocked */ |
5339 | __HAL_UNLOCK(hi2c); |
7127 | __HAL_UNLOCK(hi2c); |
5340 | 7128 | ||
5341 | return HAL_TIMEOUT; |
7129 | return HAL_ERROR; |
5342 | } |
7130 | } |
5343 | } |
7131 | } |
5344 | } |
7132 | } |
5345 | - | ||
5346 | return HAL_OK; |
7133 | return HAL_OK; |
5347 | } |
7134 | } |
5348 | 7135 | ||
5349 | /** |
7136 | /** |
5350 | * @brief This function handles I2C Communication Timeout for Master addressing phase. |
7137 | * @brief This function handles I2C Communication Timeout for Master addressing phase. |
Line 5355... | Line 7142... | ||
5355 | * @param Tickstart Tick start value |
7142 | * @param Tickstart Tick start value |
5356 | * @retval HAL status |
7143 | * @retval HAL status |
5357 | */ |
7144 | */ |
5358 | static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart) |
7145 | static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart) |
5359 | { |
7146 | { |
5360 | while(__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET) |
7147 | while (__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET) |
5361 | { |
7148 | { |
5362 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) |
7149 | if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) |
5363 | { |
7150 | { |
5364 | /* Generate Stop */ |
7151 | /* Generate Stop */ |
5365 | hi2c->Instance->CR1 |= I2C_CR1_STOP; |
7152 | SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP); |
5366 | 7153 | ||
5367 | /* Clear AF Flag */ |
7154 | /* Clear AF Flag */ |
5368 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
7155 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
5369 | 7156 | ||
5370 | hi2c->ErrorCode = HAL_I2C_ERROR_AF; |
7157 | hi2c->PreviousState = I2C_STATE_NONE; |
- | 7158 | hi2c->State = HAL_I2C_STATE_READY; |
|
5371 | hi2c->PreviousState = I2C_STATE_NONE; |
7159 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5372 | hi2c->State= HAL_I2C_STATE_READY; |
7160 | hi2c->ErrorCode |= HAL_I2C_ERROR_AF; |
5373 | 7161 | ||
5374 | /* Process Unlocked */ |
7162 | /* Process Unlocked */ |
5375 | __HAL_UNLOCK(hi2c); |
7163 | __HAL_UNLOCK(hi2c); |
5376 | 7164 | ||
5377 | return HAL_ERROR; |
7165 | return HAL_ERROR; |
5378 | } |
7166 | } |
5379 | 7167 | ||
5380 | /* Check for the Timeout */ |
7168 | /* Check for the Timeout */ |
5381 | if(Timeout != HAL_MAX_DELAY) |
7169 | if (Timeout != HAL_MAX_DELAY) |
5382 | { |
7170 | { |
5383 | if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout)) |
7171 | if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) |
5384 | { |
7172 | { |
5385 | hi2c->PreviousState = I2C_STATE_NONE; |
7173 | hi2c->PreviousState = I2C_STATE_NONE; |
5386 | hi2c->State= HAL_I2C_STATE_READY; |
7174 | hi2c->State = HAL_I2C_STATE_READY; |
- | 7175 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 7176 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
5387 | 7177 | ||
5388 | /* Process Unlocked */ |
7178 | /* Process Unlocked */ |
5389 | __HAL_UNLOCK(hi2c); |
7179 | __HAL_UNLOCK(hi2c); |
5390 | 7180 | ||
5391 | return HAL_TIMEOUT; |
7181 | return HAL_ERROR; |
5392 | } |
7182 | } |
5393 | } |
7183 | } |
5394 | } |
7184 | } |
5395 | return HAL_OK; |
7185 | return HAL_OK; |
5396 | } |
7186 | } |
Line 5402... | Line 7192... | ||
5402 | * @param Timeout Timeout duration |
7192 | * @param Timeout Timeout duration |
5403 | * @param Tickstart Tick start value |
7193 | * @param Tickstart Tick start value |
5404 | * @retval HAL status |
7194 | * @retval HAL status |
5405 | */ |
7195 | */ |
5406 | static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart) |
7196 | static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart) |
5407 | { |
7197 | { |
5408 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET) |
7198 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET) |
5409 | { |
7199 | { |
5410 | /* Check if a NACK is detected */ |
7200 | /* Check if a NACK is detected */ |
5411 | if(I2C_IsAcknowledgeFailed(hi2c) != HAL_OK) |
7201 | if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK) |
5412 | { |
7202 | { |
5413 | return HAL_ERROR; |
7203 | return HAL_ERROR; |
5414 | } |
7204 | } |
5415 | 7205 | ||
5416 | /* Check for the Timeout */ |
7206 | /* Check for the Timeout */ |
5417 | if(Timeout != HAL_MAX_DELAY) |
7207 | if (Timeout != HAL_MAX_DELAY) |
5418 | { |
7208 | { |
5419 | if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout)) |
7209 | if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) |
5420 | { |
7210 | { |
5421 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
7211 | hi2c->PreviousState = I2C_STATE_NONE; |
5422 | hi2c->PreviousState = I2C_STATE_NONE; |
7212 | hi2c->State = HAL_I2C_STATE_READY; |
5423 | hi2c->State= HAL_I2C_STATE_READY; |
7213 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 7214 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
5424 | 7215 | ||
5425 | /* Process Unlocked */ |
7216 | /* Process Unlocked */ |
5426 | __HAL_UNLOCK(hi2c); |
7217 | __HAL_UNLOCK(hi2c); |
5427 | 7218 | ||
5428 | return HAL_TIMEOUT; |
7219 | return HAL_ERROR; |
5429 | } |
7220 | } |
5430 | } |
7221 | } |
5431 | } |
7222 | } |
5432 | return HAL_OK; |
7223 | return HAL_OK; |
5433 | } |
7224 | } |
5434 | 7225 | ||
5435 | /** |
7226 | /** |
5436 | * @brief This function handles I2C Communication Timeout for specific usage of BTF flag. |
7227 | * @brief This function handles I2C Communication Timeout for specific usage of BTF flag. |
5437 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
7228 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
Line 5439... | Line 7230... | ||
5439 | * @param Timeout Timeout duration |
7230 | * @param Timeout Timeout duration |
5440 | * @param Tickstart Tick start value |
7231 | * @param Tickstart Tick start value |
5441 | * @retval HAL status |
7232 | * @retval HAL status |
5442 | */ |
7233 | */ |
5443 | static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart) |
7234 | static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart) |
5444 | { |
7235 | { |
5445 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET) |
7236 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET) |
5446 | { |
7237 | { |
5447 | /* Check if a NACK is detected */ |
7238 | /* Check if a NACK is detected */ |
5448 | if(I2C_IsAcknowledgeFailed(hi2c) != HAL_OK) |
7239 | if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK) |
5449 | { |
7240 | { |
5450 | return HAL_ERROR; |
7241 | return HAL_ERROR; |
5451 | } |
7242 | } |
5452 | 7243 | ||
5453 | /* Check for the Timeout */ |
7244 | /* Check for the Timeout */ |
5454 | if(Timeout != HAL_MAX_DELAY) |
7245 | if (Timeout != HAL_MAX_DELAY) |
5455 | { |
7246 | { |
5456 | if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout)) |
7247 | if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) |
5457 | { |
7248 | { |
5458 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
7249 | hi2c->PreviousState = I2C_STATE_NONE; |
5459 | hi2c->PreviousState = I2C_STATE_NONE; |
7250 | hi2c->State = HAL_I2C_STATE_READY; |
5460 | hi2c->State= HAL_I2C_STATE_READY; |
7251 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 7252 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
5461 | 7253 | ||
5462 | /* Process Unlocked */ |
7254 | /* Process Unlocked */ |
5463 | __HAL_UNLOCK(hi2c); |
7255 | __HAL_UNLOCK(hi2c); |
5464 | 7256 | ||
5465 | return HAL_TIMEOUT; |
7257 | return HAL_ERROR; |
5466 | } |
7258 | } |
5467 | } |
7259 | } |
5468 | } |
7260 | } |
5469 | return HAL_OK; |
7261 | return HAL_OK; |
5470 | } |
7262 | } |
Line 5476... | Line 7268... | ||
5476 | * @param Timeout Timeout duration |
7268 | * @param Timeout Timeout duration |
5477 | * @param Tickstart Tick start value |
7269 | * @param Tickstart Tick start value |
5478 | * @retval HAL status |
7270 | * @retval HAL status |
5479 | */ |
7271 | */ |
5480 | static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart) |
7272 | static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart) |
5481 | { |
7273 | { |
5482 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) |
7274 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET) |
5483 | { |
7275 | { |
5484 | /* Check if a NACK is detected */ |
7276 | /* Check if a NACK is detected */ |
5485 | if(I2C_IsAcknowledgeFailed(hi2c) != HAL_OK) |
7277 | if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK) |
5486 | { |
7278 | { |
5487 | return HAL_ERROR; |
7279 | return HAL_ERROR; |
5488 | } |
7280 | } |
5489 | 7281 | ||
5490 | /* Check for the Timeout */ |
7282 | /* Check for the Timeout */ |
5491 | if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout)) |
7283 | if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) |
5492 | { |
7284 | { |
5493 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
7285 | hi2c->PreviousState = I2C_STATE_NONE; |
5494 | hi2c->PreviousState = I2C_STATE_NONE; |
7286 | hi2c->State = HAL_I2C_STATE_READY; |
5495 | hi2c->State= HAL_I2C_STATE_READY; |
7287 | hi2c->Mode = HAL_I2C_MODE_NONE; |
- | 7288 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
5496 | 7289 | ||
5497 | /* Process Unlocked */ |
7290 | /* Process Unlocked */ |
5498 | __HAL_UNLOCK(hi2c); |
7291 | __HAL_UNLOCK(hi2c); |
5499 | 7292 | ||
5500 | return HAL_TIMEOUT; |
7293 | return HAL_ERROR; |
- | 7294 | } |
|
- | 7295 | } |
|
- | 7296 | return HAL_OK; |
|
- | 7297 | } |
|
- | 7298 | ||
- | 7299 | /** |
|
- | 7300 | * @brief This function handles I2C Communication Timeout for specific usage of STOP request through Interrupt. |
|
- | 7301 | * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains |
|
- | 7302 | * the configuration information for the specified I2C. |
|
- | 7303 | * @retval HAL status |
|
- | 7304 | */ |
|
- | 7305 | static HAL_StatusTypeDef I2C_WaitOnSTOPRequestThroughIT(I2C_HandleTypeDef *hi2c) |
|
- | 7306 | { |
|
- | 7307 | __IO uint32_t count = 0U; |
|
- | 7308 | ||
- | 7309 | /* Wait until STOP flag is reset */ |
|
- | 7310 | count = I2C_TIMEOUT_STOP_FLAG * (SystemCoreClock / 25U / 1000U); |
|
- | 7311 | do |
|
- | 7312 | { |
|
- | 7313 | count--; |
|
- | 7314 | if (count == 0U) |
|
- | 7315 | { |
|
- | 7316 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
- | 7317 | ||
- | 7318 | return HAL_ERROR; |
|
5501 | } |
7319 | } |
5502 | } |
7320 | } |
- | 7321 | while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP); |
|
- | 7322 | ||
5503 | return HAL_OK; |
7323 | return HAL_OK; |
5504 | } |
7324 | } |
5505 | 7325 | ||
5506 | /** |
7326 | /** |
5507 | * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag. |
7327 | * @brief This function handles I2C Communication Timeout for specific usage of RXNE flag. |
Line 5510... | Line 7330... | ||
5510 | * @param Timeout Timeout duration |
7330 | * @param Timeout Timeout duration |
5511 | * @param Tickstart Tick start value |
7331 | * @param Tickstart Tick start value |
5512 | * @retval HAL status |
7332 | * @retval HAL status |
5513 | */ |
7333 | */ |
5514 | static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart) |
7334 | static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart) |
5515 | { |
7335 | { |
5516 | 7336 | ||
5517 | while(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET) |
7337 | while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET) |
5518 | { |
7338 | { |
5519 | /* Check if a STOPF is detected */ |
7339 | /* Check if a STOPF is detected */ |
5520 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) |
7340 | if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET) |
5521 | { |
7341 | { |
5522 | /* Clear STOP Flag */ |
7342 | /* Clear STOP Flag */ |
5523 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); |
7343 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_STOPF); |
5524 | 7344 | ||
5525 | hi2c->ErrorCode = HAL_I2C_ERROR_NONE; |
7345 | hi2c->PreviousState = I2C_STATE_NONE; |
- | 7346 | hi2c->State = HAL_I2C_STATE_READY; |
|
5526 | hi2c->PreviousState = I2C_STATE_NONE; |
7347 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5527 | hi2c->State= HAL_I2C_STATE_READY; |
7348 | hi2c->ErrorCode |= HAL_I2C_ERROR_NONE; |
5528 | 7349 | ||
5529 | /* Process Unlocked */ |
7350 | /* Process Unlocked */ |
5530 | __HAL_UNLOCK(hi2c); |
7351 | __HAL_UNLOCK(hi2c); |
5531 | 7352 | ||
5532 | return HAL_ERROR; |
7353 | return HAL_ERROR; |
5533 | } |
7354 | } |
5534 | 7355 | ||
5535 | /* Check for the Timeout */ |
7356 | /* Check for the Timeout */ |
5536 | if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout)) |
7357 | if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) |
5537 | { |
7358 | { |
5538 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
7359 | hi2c->PreviousState = I2C_STATE_NONE; |
5539 | hi2c->State= HAL_I2C_STATE_READY; |
7360 | hi2c->State = HAL_I2C_STATE_READY; |
- | 7361 | hi2c->Mode = HAL_I2C_MODE_NONE; |
|
- | 7362 | hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT; |
|
5540 | 7363 | ||
5541 | /* Process Unlocked */ |
7364 | /* Process Unlocked */ |
5542 | __HAL_UNLOCK(hi2c); |
7365 | __HAL_UNLOCK(hi2c); |
5543 | 7366 | ||
5544 | return HAL_TIMEOUT; |
7367 | return HAL_ERROR; |
5545 | } |
7368 | } |
5546 | } |
7369 | } |
5547 | return HAL_OK; |
7370 | return HAL_OK; |
5548 | } |
7371 | } |
5549 | 7372 | ||
Line 5553... | Line 7376... | ||
5553 | * the configuration information for the specified I2C. |
7376 | * the configuration information for the specified I2C. |
5554 | * @retval HAL status |
7377 | * @retval HAL status |
5555 | */ |
7378 | */ |
5556 | static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c) |
7379 | static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c) |
5557 | { |
7380 | { |
5558 | if(__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) |
7381 | if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET) |
5559 | { |
7382 | { |
5560 | /* Clear NACKF Flag */ |
7383 | /* Clear NACKF Flag */ |
5561 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
7384 | __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF); |
5562 | 7385 | ||
5563 | hi2c->ErrorCode = HAL_I2C_ERROR_AF; |
7386 | hi2c->PreviousState = I2C_STATE_NONE; |
- | 7387 | hi2c->State = HAL_I2C_STATE_READY; |
|
5564 | hi2c->PreviousState = I2C_STATE_NONE; |
7388 | hi2c->Mode = HAL_I2C_MODE_NONE; |
5565 | hi2c->State= HAL_I2C_STATE_READY; |
7389 | hi2c->ErrorCode |= HAL_I2C_ERROR_AF; |
5566 | 7390 | ||
5567 | /* Process Unlocked */ |
7391 | /* Process Unlocked */ |
5568 | __HAL_UNLOCK(hi2c); |
7392 | __HAL_UNLOCK(hi2c); |
5569 | 7393 | ||
5570 | return HAL_ERROR; |
7394 | return HAL_ERROR; |
5571 | } |
7395 | } |
5572 | return HAL_OK; |
7396 | return HAL_OK; |
5573 | } |
7397 | } |
- | 7398 | ||
- | 7399 | /** |
|
- | 7400 | * @brief Convert I2Cx OTHER_xxx XferOptions to functionnal XferOptions. |
|
- | 7401 | * @param hi2c I2C handle. |
|
- | 7402 | * @retval None |
|
- | 7403 | */ |
|
- | 7404 | static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c) |
|
- | 7405 | { |
|
- | 7406 | /* if user set XferOptions to I2C_OTHER_FRAME */ |
|
- | 7407 | /* it request implicitly to generate a restart condition */ |
|
- | 7408 | /* set XferOptions to I2C_FIRST_FRAME */ |
|
- | 7409 | if (hi2c->XferOptions == I2C_OTHER_FRAME) |
|
- | 7410 | { |
|
- | 7411 | hi2c->XferOptions = I2C_FIRST_FRAME; |
|
- | 7412 | } |
|
- | 7413 | /* else if user set XferOptions to I2C_OTHER_AND_LAST_FRAME */ |
|
- | 7414 | /* it request implicitly to generate a restart condition */ |
|
- | 7415 | /* then generate a stop condition at the end of transfer */ |
|
- | 7416 | /* set XferOptions to I2C_FIRST_AND_LAST_FRAME */ |
|
- | 7417 | else if (hi2c->XferOptions == I2C_OTHER_AND_LAST_FRAME) |
|
- | 7418 | { |
|
- | 7419 | hi2c->XferOptions = I2C_FIRST_AND_LAST_FRAME; |
|
- | 7420 | } |
|
- | 7421 | else |
|
- | 7422 | { |
|
- | 7423 | /* Nothing to do */ |
|
- | 7424 | } |
|
- | 7425 | } |
|
- | 7426 | ||
5574 | /** |
7427 | /** |
5575 | * @} |
7428 | * @} |
5576 | */ |
7429 | */ |
5577 | 7430 | ||
5578 | #endif /* HAL_I2C_MODULE_ENABLED */ |
7431 | #endif /* HAL_I2C_MODULE_ENABLED */ |
5579 | - | ||
5580 | /** |
7432 | /** |
5581 | * @} |
7433 | * @} |
5582 | */ |
7434 | */ |
5583 | 7435 | ||
5584 | /** |
7436 | /** |