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

Subversion Repositories neorv32

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

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
// # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
7
// #                                                                                               #
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
 * @note These functions should only be used if the TWI unit was synthesized (IO_TWI_USE = true).
42
 **************************************************************************/
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
 * @param[in] irq_en Enable transfer-done interrupt when 1.
69 35 zero_gravi
 * @param[in] ckst_en Enable clock-stretching by peripherals when 1.
70 2 zero_gravi
 **************************************************************************/
71 35 zero_gravi
void neorv32_twi_setup(uint8_t prsc, uint8_t irq_en, uint8_t ckst_en) {
72 2 zero_gravi
 
73
  TWI_CT = 0; // reset
74
 
75
  uint32_t ct_enable = 1;
76
  ct_enable = ct_enable << TWI_CT_EN;
77
 
78
  uint32_t ct_prsc = (uint32_t)(prsc & 0x07);
79
  ct_prsc = ct_prsc << TWI_CT_PRSC0;
80
 
81
  uint32_t ct_irq = (uint32_t)(irq_en & 0x01);
82
  ct_irq = ct_irq << TWI_CT_IRQ_EN;
83
 
84 35 zero_gravi
  uint32_t ct_cksten = (uint32_t)(ckst_en & 0x01);
85
  ct_cksten = ct_cksten << TWI_CT_CKSTEN;
86
 
87
  TWI_CT = ct_enable | ct_prsc | ct_irq | ct_cksten;
88 2 zero_gravi
}
89
 
90
 
91
/**********************************************************************//**
92
 * Disable TWI controller.
93
 **************************************************************************/
94
void neorv32_twi_disable(void) {
95
 
96
  TWI_CT &= ~((uint32_t)(1 << TWI_CT_IRQ_EN));
97
}
98
 
99
 
100
/**********************************************************************//**
101 10 zero_gravi
 * Activate sending ACKs by controller (MACK).
102 2 zero_gravi
 **************************************************************************/
103
void neorv32_twi_mack_enable(void) {
104
 
105
  TWI_CT |= ((uint32_t)(1 << TWI_CT_MACK));
106
}
107
 
108
 
109
/**********************************************************************//**
110 10 zero_gravi
 * Deacivate sending ACKs by controller (MACK).
111 2 zero_gravi
 **************************************************************************/
112
void neorv32_twi_mack_disable(void) {
113
 
114
  TWI_CT &= ~((uint32_t)(1 << TWI_CT_MACK));
115
}
116
 
117
 
118 23 zero_gravi
/**********************************************************************//**
119
 * Check if TWI is busy.
120
 *
121
 * @note This function is blocking.
122
 *
123
 * @return 0 if idle, 1 if busy
124
 **************************************************************************/
125
int neorv32_twi_busy(void) {
126
 
127
  if (TWI_CT & (1 << TWI_CT_BUSY)) {
128
    return 1;
129
  }
130
  return 0;
131
}
132
 
133
 
134 2 zero_gravi
 /**********************************************************************//**
135
 * Generate START condition and send first byte (address including R/W bit).
136
 *
137
 * @note Blocking function.
138
 *
139
 * @param[in] a Data byte including 7-bit address and R/W-bit (lsb).
140
 * @return 0: ACK received, 1: NACK received.
141
 **************************************************************************/
142
int neorv32_twi_start_trans(uint8_t a) {
143
 
144
  neorv32_twi_generate_start(); // generate START condition
145
 
146
  TWI_DATA = (uint32_t)a; // send address
147
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
148
 
149
  // check for ACK/NACK
150
  if (TWI_CT & (1 << TWI_CT_ACK))
151
    return 0; // ACK received
152
  else
153
    return 1; // NACK received
154
}
155
 
156
 
157
 /**********************************************************************//**
158
 * Send data byte and also receive data byte (can be read via neorv32_twi_get_data()).
159
 *
160
 * @note Blocking function.
161
 *
162
 * @param[in] d Data byte to be send.
163
 * @return 0: ACK received, 1: NACK received.
164
 **************************************************************************/
165
int neorv32_twi_trans(uint8_t d) {
166
 
167
  TWI_DATA = (uint32_t)d; // send data
168
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
169
 
170
  // check for ACK/NACK
171
  if (TWI_CT & (1 << TWI_CT_ACK))
172
    return 0; // ACK received
173
  else
174
    return 1; // NACK received
175
}
176
 
177
 
178
 /**********************************************************************//**
179
 * Get received data from last transmission.
180
 *
181
 * @return 0: Last received data byte.
182
 **************************************************************************/
183
uint8_t neorv32_twi_get_data(void) {
184
 
185
  return (uint8_t)TWI_DATA; // get RX data from previous transmission
186
}
187
 
188
 
189
 /**********************************************************************//**
190
 * Generate STOP condition.
191
 *
192
 * @note Blocking function.
193
 **************************************************************************/
194
void neorv32_twi_generate_stop(void) {
195
 
196
  TWI_CT |= (uint32_t)(1 << TWI_CT_STOP); // generate STOP condition
197
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
198
}
199
 
200
 
201
 /**********************************************************************//**
202
 * Generate START condition.
203
 *
204
 * @note Blocking function.
205
 **************************************************************************/
206
void neorv32_twi_generate_start(void) {
207
 
208
  TWI_CT |= (1 << TWI_CT_START); // generate START condition
209
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
210
}

powered by: WebSVN 2.1.0

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