-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.h
155 lines (119 loc) · 4.52 KB
/
i2c.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
\file i2c.h
\brief
This is the header file for the i2c device driver for Kinetis K64.
\authors: César Villarreal Hernández, ie707560
José Luis Rodríguez Gutierrez,ie705694
\date 10/05/2019
*/
#ifndef I2C_H_
#define I2C_H_
#include "NVIC.h"
#include "Bits.h"
#include "GPIO.h"
#include "MK64F12.h"
#include "stdint.h"
/*\brief Constant that represent the freq_div for I2C_F*/
#define FREQUENCY_DIVIDER 0x40
/**\brief Constant that represent the clock enable for GPIO A */
#define I2C0_CLOCK_GATING 0x40
#define MULT 4
/**\brief This enum define the I2C channel to be used. **/
typedef enum {I2C_0, I2C_1, I2C_2} i2c_channel_t;
/**\brief This enum defines the master/slave config **/
typedef enum{I2C_SLV = FALSE,I2C_MST = TRUE}i2c_mst_t;
/**\brief This enum defines the transmission/reception **/
typedef enum{I2C_RX = FALSE,I2C_TX = TRUE}i2c_mode_t;
/*!
\brief
Configures the I2C port based on the input parameters.
Also, internally this function configures the GPIO, pin control register and clock gating, to be used as I2C.
It is recommended to use pin 2 and 3 of GPIOB.
\param[in] channel It is the channel to be used.
\param[in] systemClock Frequency of the system.
\param[in] baudRate baud rate between MCU and I2C device.
\return void
*/
void I2C_init(i2c_channel_t channel, uint32_t system_clock, uint16_t baud_rate);
/*!
\brief
Indicates the status of the bus regardless of slave or master mode. Internally checks the busy bit int the
I2Cx_S register. This bit is set when a START signal is detected and cleared when a STOP signal is detected.
\return This function returns a 0 logic if the bus is idle and returns 1 logic if the bus is busy.
*/
uint8_t I2C_busy(void);
/*!
\brief
It selects between master or slave mode.
\param[in] masterOrSlave If == 1 master mode, if == 0 slave mode.
\return void
*/
void I2C_mst_or_slv_mode(uint8_t mst_or_slv);
/*!
\brief
It selects between transmitter mode or receiver mode.
\param[in] txOrRx If == 1 transmitter mode, if == 0 slave mode.
\return void
*/
void I2C_tx_rx_mode(uint8_t tx_or_rx);
/*!
\brief
It generates the Not ACKnowledge that is needed when the master reads data.
\return void
*/
void I2C_nack(void);
/*!
\brief
It generates a repeated start that is needed when master reads data.
\return void
*/
void I2C_repeated_start(void);
/*!
\brief
It writes the data to be transmitted into the transmission buffer. When you want to
write a value into the buffer you need to use this sentence I2C0_D = data. Avoid to use
masks like this I2C0_D |= data.
\return void
*/
void I2C_write_byte(uint8_t data);
/*!
\brief
It reads data from the receiving buffer.
\return void
*/
uint8_t I2C_read_byte(void);
/*!
\brief
Indicates the status of the bus regardless of slave or master mode. Internally checks the interrupt flag of the
I2Cx_S register. This bit is set on the following events:
• One byte transfer, including ACK/NACK bit, completes if FACK is 0. An ACK or NACK is sent on the
bus by writing 0 or 1 to TXAK after this bit is set in receive mode.
• One byte transfer, excluding ACK/NACK bit, completes if FACK is 1.
This function should be implemented as a blocking function by using while((I2C0->S & 0x02)== 0);, the bit number 2 of this register must be set.
The blocking implementation of this function only to reduce the complexity of the lab. However, blocking implementation must be avoided.
\return Void.
*/
void I2C_wait(void);
/*!
\brief
Indicates if the acknowledge was received.
\return This function returns a 0 logic if the acknowledge was received and returns 1 logic if the acknowledge was not received.
*/
void I2C_get_ack(void);
/*!
\brief
Generates the start signal. When MST bit is changed from 0 to 1, a START signal is generated
on the bus and master mode is selected. Also, inside the function the I2C is
change to transmitter mode.
\return void
*/
void I2C_start(void);
/*!
\brief
Generates the stop signal. When this bit changes from 1 to 0, a STOP signal is generated
and the mode of operation changes from master to slave. Also, inside the function the I2C is
change to receiver mode.
\return void
*/
void I2C_stop(void);
#endif /* I2C_H_ */