| BlueNRG-1 Peripherals drivers Examples

- Main Page
- Modules
- Classes
- Files

- File List
- File Members

I2C_Master_Polling_main.c
Go to the documentation of this file.
1
2 /******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
3 * File Name : I2C_Master_Polling_main.c
4 * Author : VMA DIVISION
5 * Version : 1.0.0
6 * Date : 21-September-2015
7 * Description : Code demostrating the I2C master 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
199 /* Includes ------------------------------------------------------------------*/
200 #include "BlueNRG1.h"
201 #include <stdio.h>
202 #include "BlueNRG1_conf.h"
203 #include "SDK_EVAL_Config.h"
204
217 /* Private typedef -----------------------------------------------------------*/
218 typedef enum {
219 I2C_OP_FAILED = 0,
220 I2C_OP_SUCCEED
221 }
222 I2C_AppOpStatus;
223
224 /* Private define ------------------------------------------------------------*/
225 #define TOGGLE_DL1 '1'
226 #define TOGGLE_DL2 '2'
227 #define TOGGLE_DL3 '3'
228
229 /* I2C Device address */
230 #define DEV_ADDRESS 0xBE
231
232 /* I2C clock frequency */
233 #define SDK_EVAL_I2C_CLK_SPEED (10000)
234
235 /* Private macro -------------------------------------------------------------*/
236 /* Private variables ---------------------------------------------------------*/
237 /* Private function prototypes -----------------------------------------------*/
238 void I2C_ConfigurationMaster(void);
239 I2C_AppOpStatus I2C_Read_Reg(uint8_t dev_address, uint8_t send_val, uint8_t* get_val);
240 void processCommand(void);
241 void helpMessage(void);
242
243 /* Private functions ---------------------------------------------------------*/
244
250 int main(void)
251 {
252 /* System initialization function */
253 SystemInit();
254
255 /* Identify BlueNRG1 platform */
256 SdkEvalIdentification();
257
258 /* UART initialization */
259 SdkEvalComUartInit(UART_BAUDRATE);
260
261 /* I2C initialization */
262 I2C_ConfigurationMaster();
263
264 /* Infinite loop for process command */
265 processCommand();
266
267 }
268
274 void helpMessage(void)
275 {
276 printf("** I2C Master test program **\r\n(requires another board with I2C Slave test program connected by I2C\r\n\n");
277 printf("%c: Toggle LED DL1\r\n", TOGGLE_DL1);
278 printf("%c: Toggle LED DL2\r\n", TOGGLE_DL2);
279 printf("%c: Toggle LED DL3\r\n", TOGGLE_DL3);
280
281 }
282
283
289 void processCommand(void)
290 {
291 uint8_t command;
292 uint8_t led_status;
293
294 /* Infinite loop */
295 while(1) {
296
297 /* Loop until the UART Receive Data Register is not empty */
298 while (UART_GetFlagStatus(UART_FLAG_RXFE) == SET);
299
300 /* Store the received byte in RxBuffer */
301 command = UART_ReceiveData();
302
303 /* Parse the command */
304 switch (command) {
305
306 case TOGGLE_DL1:
307 /* I2C Master communication */
308 I2C_Read_Reg(DEV_ADDRESS, TOGGLE_DL1, &led_status);
309
310 if((led_status&1) == 1) {
311 printf("LED DL1 OFF\r\n");
312 }
313 else {
314 printf("LED DL1 ON\r\n");
315 }
316 break;
317
318 case TOGGLE_DL2:
319 /* I2C Master communication */
320 I2C_Read_Reg(DEV_ADDRESS, TOGGLE_DL2, &led_status);
321
322 if((led_status&2) == 2) {
323 printf("LED DL2 OFF\r\n");
324 }
325 else {
326 printf("LED DL2 ON\r\n");
327 }
328 break;
329
330 case TOGGLE_DL3:
331 /* I2C Master communication */
332 I2C_Read_Reg(DEV_ADDRESS, TOGGLE_DL3, &led_status);
333
334 if((led_status&4) == 4) {
335 printf("LED DL3 OFF\r\n");
336 }
337 else {
338 printf("LED DL3 ON\r\n");
339 }
340 break;
341
342 default:
343 helpMessage();
344 break;
345 }
346 led_status = 0;
347 }
348 }
349
355 void I2C_ConfigurationMaster(void)
356 {
357 GPIO_InitType GPIO_InitStructure;
358 I2C_InitType I2C_InitStruct;
359
360 /* Enable I2C and GPIO clocks */
361 if((I2C_Type*)SDK_EVAL_I2C == I2C2) {
362 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_I2C2 | CLOCK_PERIPH_GPIO, ENABLE);
363 }
364 else {
365 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_I2C1 | CLOCK_PERIPH_GPIO, ENABLE);
366 }
367
368 /* Configure I2C pins */
369 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_I2C_CLK_PIN ;
370 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_I2C_DATA_MODE;
371 GPIO_InitStructure.GPIO_Pull = ENABLE;
372 GPIO_InitStructure.GPIO_HighPwr = DISABLE;
373 GPIO_Init(&GPIO_InitStructure);
374
375 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_I2C_DATA_PIN;
376 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_I2C_DATA_MODE;
377 GPIO_Init(&GPIO_InitStructure);
378
379 /* Configure I2C in master mode */
380 I2C_StructInit(&I2C_InitStruct);
381 I2C_InitStruct.I2C_OperatingMode = I2C_OperatingMode_Master;
382 I2C_InitStruct.I2C_ClockSpeed = SDK_EVAL_I2C_CLK_SPEED;
383 I2C_Init((I2C_Type*)SDK_EVAL_I2C, &I2C_InitStruct);
384
385 /* Clear all I2C pending interrupts */
386 I2C_ClearITPendingBit((I2C_Type*)SDK_EVAL_I2C, I2C_IT_MSK);
387
388 }
389
397 I2C_AppOpStatus I2C_Read_Reg(uint8_t dev_address, uint8_t send_val, uint8_t* get_val)
398 {
399 I2C_TransactionType t;
400
401 /* Write I2C device address address */
402 t.Operation = I2C_Operation_Write;
403 t.Address = dev_address;
404 t.StartByte = I2C_StartByte_Disable;
405 t.AddressType = I2C_AddressType_7Bit;
406 t.StopCondition = I2C_StopCondition_Disable;
407 t.Length = 1;
408
409 I2C_FlushTx((I2C_Type*)SDK_EVAL_I2C);
410
411 /* wait until Flush finished */
412 while (I2C_WaitFlushTx((I2C_Type*)SDK_EVAL_I2C) == I2C_OP_ONGOING);
413
414 /* Write I2C device address address and put the send_val in TX FIFO */
415 I2C_BeginTransaction((I2C_Type*)SDK_EVAL_I2C, &t);
416 I2C_FillTxFIFO((I2C_Type*)SDK_EVAL_I2C, send_val);
417
418 /* Check write */
419 do
420 {
421 if(I2C_OP_ABORTED == I2C_GetStatus((I2C_Type*)SDK_EVAL_I2C))
422 return I2C_OP_FAILED;
423
424 } while (I2C_GetITStatus((I2C_Type*)SDK_EVAL_I2C, I2C_IT_MTDWS) == RESET);
425
426 I2C_ClearITPendingBit((I2C_Type*)SDK_EVAL_I2C, I2C_IT_MTDWS);
427
428 /* Prepare to read data */
429 t.Operation = I2C_Operation_Read;
430 t.Address = dev_address;
431 t.StartByte = I2C_StartByte_Disable;
432 t.AddressType = I2C_AddressType_7Bit;
433 t.StopCondition = I2C_StopCondition_Enable;
434 t.Length = 1;
435
436 I2C_BeginTransaction((I2C_Type*)SDK_EVAL_I2C, &t);
437
438 /* Check read */
439 do
440 {
441 if(I2C_OP_ABORTED == I2C_GetStatus((I2C_Type*)SDK_EVAL_I2C))
442 return I2C_OP_FAILED;
443
444 } while (RESET == I2C_GetITStatus((I2C_Type*)SDK_EVAL_I2C, I2C_IT_MTD));
445
446 I2C_ClearITPendingBit((I2C_Type*)SDK_EVAL_I2C, I2C_IT_MTD | I2C_IT_MTDWS);
447
448 /* Get data */
449 *get_val = I2C_ReceiveData((I2C_Type*)SDK_EVAL_I2C);
450
451 return I2C_OP_SUCCEED;
452 }
453
454
455
456 #ifdef USE_FULL_ASSERT
457
464 void assert_failed(uint8_t* file, uint32_t line)
465 {
466 /* User can add his own implementation to report the file name and line number,
467 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
468
469 /* Infinite loop */
470 while (1)
471 {
472 }
473 }
474
475 #endif
476
489 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/
SDK_EVAL_I2C_CLK_SPEED
#define SDK_EVAL_I2C_CLK_SPEED
Definition: I2C_Master_Polling_main.c:233
main
int main(void)
Main program code.
Definition: I2C_Master_Polling_main.c:250
TOGGLE_DL3
#define TOGGLE_DL3
Definition: I2C_Master_Polling_main.c:227
TOGGLE_DL1
#define TOGGLE_DL1
Definition: I2C_Master_Polling_main.c:225
I2C_OP_FAILED
Definition: I2C_Master_Polling_main.c:219
I2C_OP_SUCCEED
Definition: I2C_Master_Polling_main.c:220
I2C_AppOpStatus
I2C_AppOpStatus
Definition: I2C_Master_Polling_main.c:218
DEV_ADDRESS
#define DEV_ADDRESS
Definition: I2C_Master_Polling_main.c:230
I2C_ConfigurationMaster
void I2C_ConfigurationMaster(void)
I2C Master initialization.
Definition: I2C_Master_Polling_main.c:355
processCommand
void processCommand(void)
Process command code.
Definition: I2C_Master_Polling_main.c:289
I2C_Read_Reg
I2C_AppOpStatus I2C_Read_Reg(uint8_t dev_address, uint8_t send_val, uint8_t *get_val)
I2C Master communication.
Definition: I2C_Master_Polling_main.c:397
TOGGLE_DL2
#define TOGGLE_DL2
Definition: I2C_Master_Polling_main.c:226
helpMessage
void helpMessage(void)
Help message code.
Definition: I2C_Master_Polling_main.c:274
UART_BAUDRATE
#define UART_BAUDRATE
Definition: ADC_PDM_main.c:225