OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_twi.c] - Blame information for rev 61

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
// #################################################################################################
2 10 zero_gravi
// # << NEORV32: neorv32_twi.c - Two-Wire Interface Controller (TWI) HW Driver >>                  #
3 2 zero_gravi
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6 44 zero_gravi
// # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
7 2 zero_gravi
// #                                                                                               #
8
// # Redistribution and use in source and binary forms, with or without modification, are          #
9
// # permitted provided that the following conditions are met:                                     #
10
// #                                                                                               #
11
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
// #    conditions and the following disclaimer.                                                   #
13
// #                                                                                               #
14
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
// #    conditions and the following disclaimer in the documentation and/or other materials        #
16
// #    provided with the distribution.                                                            #
17
// #                                                                                               #
18
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
// #    endorse or promote products derived from this software without specific prior written      #
20
// #    permission.                                                                                #
21
// #                                                                                               #
22
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
// # ********************************************************************************************* #
32
// # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
// #################################################################################################
34
 
35
 
36
/**********************************************************************//**
37
 * @file neorv32_twi.c
38
 * @author Stephan Nolting
39 10 zero_gravi
 * @brief Two-Wire Interface Controller (TWI) HW driver source file.
40 2 zero_gravi
 *
41 44 zero_gravi
 * @note These functions should only be used if the TWI unit was synthesized (IO_TWI_EN = true).
42 2 zero_gravi
 **************************************************************************/
43
 
44
#include "neorv32.h"
45
#include "neorv32_twi.h"
46
 
47
 
48
/**********************************************************************//**
49
 * Check if TWI unit was synthesized.
50
 *
51
 * @return 0 if TWI was not synthesized, 1 if TWI is available.
52
 **************************************************************************/
53
int neorv32_twi_available(void) {
54
 
55 12 zero_gravi
  if (SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_IO_TWI)) {
56 2 zero_gravi
    return 1;
57
  }
58
  else {
59
    return 0;
60
  }
61
}
62
 
63
 
64
/**********************************************************************//**
65
 * Enable and configure TWI controller. The TWI control register bits are listed in #NEORV32_TWI_CT_enum.
66
 *
67
 * @param[in] prsc Clock prescaler select (0..7). See #NEORV32_CLOCK_PRSC_enum.
68 35 zero_gravi
 * @param[in] ckst_en Enable clock-stretching by peripherals when 1.
69 2 zero_gravi
 **************************************************************************/
70 48 zero_gravi
void neorv32_twi_setup(uint8_t prsc, uint8_t ckst_en) {
71 2 zero_gravi
 
72
  TWI_CT = 0; // reset
73
 
74
  uint32_t ct_enable = 1;
75
  ct_enable = ct_enable << TWI_CT_EN;
76
 
77
  uint32_t ct_prsc = (uint32_t)(prsc & 0x07);
78
  ct_prsc = ct_prsc << TWI_CT_PRSC0;
79
 
80 35 zero_gravi
  uint32_t ct_cksten = (uint32_t)(ckst_en & 0x01);
81
  ct_cksten = ct_cksten << TWI_CT_CKSTEN;
82
 
83 48 zero_gravi
  TWI_CT = ct_enable | ct_prsc | ct_cksten;
84 2 zero_gravi
}
85
 
86
 
87
/**********************************************************************//**
88
 * Disable TWI controller.
89
 **************************************************************************/
90
void neorv32_twi_disable(void) {
91
 
92 48 zero_gravi
  TWI_CT &= ~((uint32_t)(1 << TWI_CT_EN));
93 2 zero_gravi
}
94
 
95
 
96
/**********************************************************************//**
97 61 zero_gravi
 * Enable TWI controller.
98
 **************************************************************************/
99
void neorv32_twi_enable(void) {
100
 
101
  TWI_CT |= (uint32_t)(1 << TWI_CT_EN);
102
}
103
 
104
 
105
/**********************************************************************//**
106 10 zero_gravi
 * Activate sending ACKs by controller (MACK).
107 2 zero_gravi
 **************************************************************************/
108
void neorv32_twi_mack_enable(void) {
109
 
110
  TWI_CT |= ((uint32_t)(1 << TWI_CT_MACK));
111
}
112
 
113
 
114
/**********************************************************************//**
115 10 zero_gravi
 * Deacivate sending ACKs by controller (MACK).
116 2 zero_gravi
 **************************************************************************/
117
void neorv32_twi_mack_disable(void) {
118
 
119
  TWI_CT &= ~((uint32_t)(1 << TWI_CT_MACK));
120
}
121
 
122
 
123 23 zero_gravi
/**********************************************************************//**
124
 * Check if TWI is busy.
125
 *
126
 * @note This function is blocking.
127
 *
128
 * @return 0 if idle, 1 if busy
129
 **************************************************************************/
130
int neorv32_twi_busy(void) {
131
 
132
  if (TWI_CT & (1 << TWI_CT_BUSY)) {
133
    return 1;
134
  }
135
  return 0;
136
}
137
 
138
 
139 2 zero_gravi
 /**********************************************************************//**
140
 * Generate START condition and send first byte (address including R/W bit).
141
 *
142
 * @note Blocking function.
143
 *
144
 * @param[in] a Data byte including 7-bit address and R/W-bit (lsb).
145
 * @return 0: ACK received, 1: NACK received.
146
 **************************************************************************/
147
int neorv32_twi_start_trans(uint8_t a) {
148
 
149
  neorv32_twi_generate_start(); // generate START condition
150
 
151
  TWI_DATA = (uint32_t)a; // send address
152
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
153
 
154
  // check for ACK/NACK
155
  if (TWI_CT & (1 << TWI_CT_ACK))
156
    return 0; // ACK received
157
  else
158
    return 1; // NACK received
159
}
160
 
161
 
162
 /**********************************************************************//**
163
 * Send data byte and also receive data byte (can be read via neorv32_twi_get_data()).
164
 *
165
 * @note Blocking function.
166
 *
167
 * @param[in] d Data byte to be send.
168
 * @return 0: ACK received, 1: NACK received.
169
 **************************************************************************/
170
int neorv32_twi_trans(uint8_t d) {
171
 
172
  TWI_DATA = (uint32_t)d; // send data
173
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
174
 
175
  // check for ACK/NACK
176
  if (TWI_CT & (1 << TWI_CT_ACK))
177
    return 0; // ACK received
178
  else
179
    return 1; // NACK received
180
}
181
 
182
 
183
 /**********************************************************************//**
184
 * Get received data from last transmission.
185
 *
186
 * @return 0: Last received data byte.
187
 **************************************************************************/
188
uint8_t neorv32_twi_get_data(void) {
189
 
190
  return (uint8_t)TWI_DATA; // get RX data from previous transmission
191
}
192
 
193
 
194
 /**********************************************************************//**
195
 * Generate STOP condition.
196
 *
197
 * @note Blocking function.
198
 **************************************************************************/
199
void neorv32_twi_generate_stop(void) {
200
 
201
  TWI_CT |= (uint32_t)(1 << TWI_CT_STOP); // generate STOP condition
202
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
203
}
204
 
205
 
206
 /**********************************************************************//**
207
 * Generate START condition.
208
 *
209
 * @note Blocking function.
210
 **************************************************************************/
211
void neorv32_twi_generate_start(void) {
212
 
213
  TWI_CT |= (1 << TWI_CT_START); // generate START condition
214
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
215
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.