IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
i2c.c File Reference

I2C functionality. More...

#include <stdint.h>
#include <stdbool.h>
#include "em_device.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_i2c.h"
#include "i2c.h"
#include "debug_dbprint.h"
#include "pin_mapping.h"

Go to the source code of this file.

Functions

int8_t initI2C (void)
 I2C initialization (if not already initialized). More...
 

Variables

bool I2C_initialized = false
 

Detailed Description

I2C functionality.

Version
1.0
Author
Brecht Van Eeckhoudt

Versions

  • v1.0: Initial version.

License

Copyright (C) 2020 - Brecht Van Eeckhoudt

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

A copy of the GNU General Public License can be found in the LICENSE file along with this source code.


Some methods use code obtained from examples from Silicon Labs' GitHub. These sections are licensed under the Silabs License Agreement. See the file "Silabs_License_Agreement.txt" for details. Before using this software for any purpose, you must agree to the terms of that agreement.

Definition in file i2c.c.

Function Documentation

◆ initI2C()

int8_t initI2C ( void  )

I2C initialization (if not already initialized).

Returns
  • 0 - Initialized correctly.
  • -1 - Initialization failed.

Definition at line 66 of file i2c.c.

67 {
68  /* Initialize I2C if not already the case */
69  if (!I2C_initialized)
70  {
71  /* Enabling clock to the I2C, GPIO, LE */
72  CMU_ClockEnable(cmuClock_I2C0, true);
73  CMU_ClockEnable(cmuClock_GPIO, true);
74  CMU_ClockEnable(cmuClock_HFLE, true);
75 
76  /* Starting LFXO and waiting until it is stable */
77  CMU_OscillatorEnable(cmuOsc_LFXO, true, true);
78 
79  /* Setup I2C using default settings */
80  I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;
81  /* Use ~400khz SCK */
82  i2cInit.freq = I2C_FREQ_FAST_MAX;
83 
84  /* Configure GPIO */
85  GPIO_PinModeSet(BUS_SDA_PORT, BUS_SDA_PIN, gpioModeWiredAndPullUpFilter, 1);
86  GPIO_PinModeSet(BUS_SCL_PORT, BUS_SCL_PIN, gpioModeWiredAndPullUpFilter, 1);
87 
88  /* Enable pins at location as specified in datasheet */
89  I2C0->ROUTE = I2C_ROUTE_SDAPEN | I2C_ROUTE_SCLPEN;
90  I2C0->ROUTE = (I2C0->ROUTE & (~_I2C_ROUTE_LOCATION_MASK)) | BUS_I2C_LOC;
91 
92  /* Initializing the I2C */
93  I2C_Init(BUS_I2C, &i2cInit);
94 
95  /* Indicate that I2C is initialized */
96  I2C_initialized = true;
97 
98 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
99  dbinfo("I2C initialized.");
100 #endif /* DEBUG_DBPRINT */
101 
102  }
103 
104  return (0);
105 }
#define BUS_I2C_LOC
Definition: pin_mapping.h:54
bool I2C_initialized
Definition: i2c.c:55
#define BUS_I2C
Definition: pin_mapping.h:53
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
Definition: dbprint.c:505
#define BUS_SDA_PIN
Definition: pin_mapping.h:56
#define BUS_SCL_PORT
Definition: pin_mapping.h:57
#define BUS_SCL_PIN
Definition: pin_mapping.h:58
#define BUS_SDA_PORT
Definition: pin_mapping.h:55

Variable Documentation

◆ I2C_initialized

bool I2C_initialized = false

Definition at line 55 of file i2c.c.