IoT-MBBR (masterthesis)
Masterthesis upgrading a Moving Bed BioReactor with extra sensors and cloud connectivity.
i2c.c
Go to the documentation of this file.
1 /***************************************************************************//**
2  * @file i2c.c
3  * @brief I2C functionality.
4  * @version 1.0
5  * @author Brecht Van Eeckhoudt
6  *
7  * ******************************************************************************
8  *
9  * @section Versions
10  *
11  * @li v1.0: Initial version.
12  *
13  * ******************************************************************************
14  *
15  * @section License
16  *
17  * **Copyright (C) 2020 - Brecht Van Eeckhoudt**
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the **GNU General Public License** as published by
21  * the Free Software Foundation, either **version 3** of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * *A copy of the GNU General Public License can be found in the `LICENSE`
30  * file along with this source code.*
31  *
32  * @n
33  *
34  * Some methods use code obtained from examples from [Silicon Labs' GitHub](https://github.com/SiliconLabs/peripheral_examples).
35  * These sections are licensed under the Silabs License Agreement. See the file
36  * "Silabs_License_Agreement.txt" for details. Before using this software for
37  * any purpose, you must agree to the terms of that agreement.
38  *
39  ******************************************************************************/
40 
41 
42 #include <stdint.h> /* (u)intXX_t */
43 #include <stdbool.h> /* "bool", "true", "false" */
44 #include "em_device.h" /* Include necessary MCU-specific header file */
45 #include "em_cmu.h" /* Clock management unit */
46 #include "em_gpio.h" /* General Purpose IO */
47 #include "em_i2c.h"
48 
49 #include "i2c.h" /* Corresponding header file */
50 #include "debug_dbprint.h" /* Enable or disable printing to UART */
51 #include "pin_mapping.h" /* PORT and PIN definitions */
52 
53 
54 /* Local variables */
55 bool I2C_initialized = false;
56 
57 
58 /**************************************************************************//**
59  * @brief
60  * I2C initialization (if not already initialized).
61  *
62  * @return
63  * @li `0` - Initialized correctly.
64  * @li `-1` - Initialization failed.
65  *****************************************************************************/
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
int8_t initI2C(void)
I2C initialization (if not already initialized).
Definition: i2c.c:66
signed char int8_t
Typedef defining 8 bit char type. The developer should modify this to suit the platform being deploye...
Definition: vl53l1_types.h:103
#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
The pin definitions for the custom board.
#define BUS_SCL_PIN
Definition: pin_mapping.h:58
Enable or disable printing to UART with dbprint.
#define BUS_SDA_PORT
Definition: pin_mapping.h:55
I2C functionality.