| BlueNRG-1 Peripherals drivers Examples

- Main Page
- Modules
- Classes
- Files

- File List
- File Members

I2C_Slave_Polling_main.c
Go to the documentation of this file.
1
2 /******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
3 * File Name : I2C_Slave_Polling_main.c
4 * Author : VMA DIVISION
5 * Version : 1.0.0
6 * Date : 21-September-2015
7 * Description : Code demostrating the I2C slave functionality
8 ********************************************************************************
9 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
10 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
11 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
12 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
13 * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
14 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
15 *******************************************************************************/
16
187 /* Includes ------------------------------------------------------------------*/
188 #include "BlueNRG1.h"
189 #include "BlueNRG1_conf.h"
190 #include "SDK_EVAL_Config.h"
191
204 /* Private typedef -----------------------------------------------------------*/
205 /* Private define ------------------------------------------------------------*/
206 #define TOGGLE_DL1 '1'
207 #define TOGGLE_DL2 '2'
208 #define TOGGLE_DL3 '3'
209
210 /* I2C device address */
211 #define DEV_ADDRESS 0xBE
212
213 /* I2C clock frequency */
214 #define SDK_EVAL_I2C_CLK_SPEED (10000)
215
216 /* Private macro -------------------------------------------------------------*/
217 /* Private variables ---------------------------------------------------------*/
218 /* Private function prototypes -----------------------------------------------*/
219 void processCommand(void);
220 void I2C_ConfigurationSlave(void);
221
222 /* Private functions ---------------------------------------------------------*/
223
224
230 int main(void)
231 {
232 /* System initialization function */
233 SystemInit();
234
235 /* Identify BlueNRG1 platform */
236 SdkEvalIdentification();
237
238 /* LEDS initialization */
239 SdkEvalLedInit(LED1);
240 SdkEvalLedInit(LED2);
241 SdkEvalLedInit(LED3);
242
243 /* I2C initialization */
244 I2C_ConfigurationSlave();
245
246 /* Infinite loop for process command */
247 processCommand();
248
249 }
250
251
252
258 void processCommand(void)
259 {
260 uint8_t leds_state;
261 uint8_t command;
262
263 /* Get the LEDS actual status */
264 leds_state = ((uint8_t)SdkEvalLedGetState(LED3))<<2 | ((uint8_t)SdkEvalLedGetState(LED2))<<1 | ((uint8_t)SdkEvalLedGetState(LED1));
265
266 /* Infinite loop */
267 while(1) {
268
269 /* Write-to-slave request received */
270 if (I2C_GetITStatus((I2C_Type*)SDK_EVAL_I2C, I2C_IT_WTSR) == SET)
271 {
272 /* Clear write-to-slave flag */
273 I2C_ClearITPendingBit((I2C_Type*)SDK_EVAL_I2C, I2C_IT_WTSR);
274
275 /* Wait till I2C transaction is finished */
276 while (I2C_GetITStatus((I2C_Type*)SDK_EVAL_I2C, I2C_IT_STD) == RESET);
277
278 /* Length of the received data */
279 command = ((I2C_Type*)SDK_EVAL_I2C)->SR_b.LENGTH;
280
281 /* Get the register address to be used */
282 command = I2C_ReceiveData((I2C_Type*)SDK_EVAL_I2C);
283
284 /* Parse the command */
285 if(command == TOGGLE_DL1) {
286 SdkEvalLedToggle(LED1);
287 }
288 else if(command == TOGGLE_DL2) {
289 SdkEvalLedToggle(LED2);
290 }
291 else if(command == TOGGLE_DL3) {
292 SdkEvalLedToggle(LED3);
293 }
294
296 I2C_ClearITPendingBit((I2C_Type*)SDK_EVAL_I2C, I2C_IT_STD);
297
298 }
299
300 /* Read-from-slave request received */
301 else if (I2C_GetITStatus((I2C_Type*)SDK_EVAL_I2C, I2C_IT_RFSR) == SET)
302 {
303 /* Clear read-from-slave */
304 I2C_ClearITPendingBit((I2C_Type*)SDK_EVAL_I2C, I2C_IT_RFSR);
305
306 /* Fill the tx fifo for the response */
307 I2C_FillTxFIFO((I2C_Type*)SDK_EVAL_I2C, leds_state);
308
309
310 /* Wait till the operation is finished */
311 while (I2C_GetITStatus((I2C_Type*)SDK_EVAL_I2C, I2C_IT_STD) == RESET)
312 {
313 if(I2C_GetITStatus((I2C_Type*)SDK_EVAL_I2C, I2C_IT_RFSE ) == SET)
314 {
315 /* Fill the tx fifo for the response */
316 I2C_FillTxFIFO((I2C_Type*)SDK_EVAL_I2C, leds_state);
317 }
318 }
319
320 /* Get the LEDS actual status */
321 leds_state = ((uint8_t)SdkEvalLedGetState(LED3))<<2 | ((uint8_t)SdkEvalLedGetState(LED2))<<1 | ((uint8_t)SdkEvalLedGetState(LED1));
322
323 /* Slave transaction done - clear flag*/
324 I2C_ClearITPendingBit((I2C_Type*)SDK_EVAL_I2C, I2C_IT_STD);
325 }
326
327 }
328 }
329
330
331
337 void I2C_ConfigurationSlave(void)
338 {
339 GPIO_InitType GPIO_InitStructure;
340 I2C_InitType I2C_InitStruct;
341
342 /* Enable I2C and GPIO clocks */
343 if((I2C_Type*)SDK_EVAL_I2C == I2C2) {
344 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_I2C2 | CLOCK_PERIPH_GPIO, ENABLE);
345 }
346 else {
347 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_I2C1 | CLOCK_PERIPH_GPIO, ENABLE);
348 }
349
350 /* Configure I2C pins */
351 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_I2C_CLK_PIN ;
352 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_I2C_DATA_MODE;
353 GPIO_InitStructure.GPIO_Pull = ENABLE;
354 GPIO_InitStructure.GPIO_HighPwr = DISABLE;
355 GPIO_Init(&GPIO_InitStructure);
356
357 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_I2C_DATA_PIN;
358 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_I2C_DATA_MODE;
359 GPIO_Init(&GPIO_InitStructure);
360
361 /* Configure I2C in slave mode */
362 I2C_StructInit(&I2C_InitStruct);
363 I2C_InitStruct.I2C_OperatingMode = I2C_OperatingMode_Slave;
364 I2C_InitStruct.I2C_ClockSpeed = SDK_EVAL_I2C_CLK_SPEED;
365 I2C_InitStruct.I2C_OwnAddress1 = DEV_ADDRESS;
366 I2C_Init((I2C_Type*)(I2C_Type*)SDK_EVAL_I2C, &I2C_InitStruct);
367
368 I2C_SetRxThreshold((I2C_Type*)SDK_EVAL_I2C, 1);
369 I2C_SetTxThreshold((I2C_Type*)SDK_EVAL_I2C, 1);
370
372 I2C_ClearITPendingBit((I2C_Type*)(I2C_Type*)SDK_EVAL_I2C, I2C_IT_MSK);
373
374 }
375
376
377 #ifdef USE_FULL_ASSERT
378
385 void assert_failed(uint8_t* file, uint32_t line)
386 {
387 /* User can add his own implementation to report the file name and line number,
388 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
389
390 /* Infinite loop */
391 while (1)
392 {
393 }
394 }
395
396 #endif
397
410 /******************* (C) COPYRIGHT 2014 STMicroelectronics *****END OF FILE****/
TOGGLE_DL2
#define TOGGLE_DL2
Definition: I2C_Slave_Polling_main.c:207
main
int main(void)
Main program code.
Definition: I2C_Slave_Polling_main.c:230
processCommand
void processCommand(void)
Process command code.
Definition: I2C_Slave_Polling_main.c:258
SDK_EVAL_I2C_CLK_SPEED
#define SDK_EVAL_I2C_CLK_SPEED
Definition: I2C_Slave_Polling_main.c:214
DEV_ADDRESS
#define DEV_ADDRESS
Definition: I2C_Slave_Polling_main.c:211
TOGGLE_DL1
#define TOGGLE_DL1
Definition: I2C_Slave_Polling_main.c:206
I2C_ConfigurationSlave
void I2C_ConfigurationSlave(void)
I2C Slave initialization.
Definition: I2C_Slave_Polling_main.c:337
TOGGLE_DL3
#define TOGGLE_DL3
Definition: I2C_Slave_Polling_main.c:208