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

Subversion Repositories neorv32

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

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
  if (neorv32_cpu_csr_read(CSR_MFEATURES) & (1 << CPU_MFEATURES_IO_TWI)) {
56
    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
 **************************************************************************/
70
void neorv32_twi_setup(uint8_t prsc, uint8_t irq_en) {
71
 
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
  uint32_t ct_irq = (uint32_t)(irq_en & 0x01);
81
  ct_irq = ct_irq << TWI_CT_IRQ_EN;
82
 
83
  TWI_CT = ct_enable | ct_prsc | ct_irq;
84
}
85
 
86
 
87
/**********************************************************************//**
88
 * Disable TWI controller.
89
 **************************************************************************/
90
void neorv32_twi_disable(void) {
91
 
92
  TWI_CT &= ~((uint32_t)(1 << TWI_CT_IRQ_EN));
93
}
94
 
95
 
96
/**********************************************************************//**
97 10 zero_gravi
 * Activate sending ACKs by controller (MACK).
98 2 zero_gravi
 **************************************************************************/
99
void neorv32_twi_mack_enable(void) {
100
 
101
  TWI_CT |= ((uint32_t)(1 << TWI_CT_MACK));
102
}
103
 
104
 
105
/**********************************************************************//**
106 10 zero_gravi
 * Deacivate sending ACKs by controller (MACK).
107 2 zero_gravi
 **************************************************************************/
108
void neorv32_twi_mack_disable(void) {
109
 
110
  TWI_CT &= ~((uint32_t)(1 << TWI_CT_MACK));
111
}
112
 
113
 
114
 /**********************************************************************//**
115
 * Generate START condition and send first byte (address including R/W bit).
116
 *
117
 * @note Blocking function.
118
 *
119
 * @param[in] a Data byte including 7-bit address and R/W-bit (lsb).
120
 * @return 0: ACK received, 1: NACK received.
121
 **************************************************************************/
122
int neorv32_twi_start_trans(uint8_t a) {
123
 
124
  neorv32_twi_generate_start(); // generate START condition
125
 
126
  TWI_DATA = (uint32_t)a; // send address
127
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
128
 
129
  // check for ACK/NACK
130
  if (TWI_CT & (1 << TWI_CT_ACK))
131
    return 0; // ACK received
132
  else
133
    return 1; // NACK received
134
}
135
 
136
 
137
 /**********************************************************************//**
138
 * Send data byte and also receive data byte (can be read via neorv32_twi_get_data()).
139
 *
140
 * @note Blocking function.
141
 *
142
 * @param[in] d Data byte to be send.
143
 * @return 0: ACK received, 1: NACK received.
144
 **************************************************************************/
145
int neorv32_twi_trans(uint8_t d) {
146
 
147
  TWI_DATA = (uint32_t)d; // send data
148
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
149
 
150
  // check for ACK/NACK
151
  if (TWI_CT & (1 << TWI_CT_ACK))
152
    return 0; // ACK received
153
  else
154
    return 1; // NACK received
155
}
156
 
157
 
158
 /**********************************************************************//**
159
 * Get received data from last transmission.
160
 *
161
 * @return 0: Last received data byte.
162
 **************************************************************************/
163
uint8_t neorv32_twi_get_data(void) {
164
 
165
  return (uint8_t)TWI_DATA; // get RX data from previous transmission
166
}
167
 
168
 
169
 /**********************************************************************//**
170
 * Generate STOP condition.
171
 *
172
 * @note Blocking function.
173
 **************************************************************************/
174
void neorv32_twi_generate_stop(void) {
175
 
176
  TWI_CT |= (uint32_t)(1 << TWI_CT_STOP); // generate STOP condition
177
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
178
}
179
 
180
 
181
 /**********************************************************************//**
182
 * Generate START condition.
183
 *
184
 * @note Blocking function.
185
 **************************************************************************/
186
void neorv32_twi_generate_start(void) {
187
 
188
  TWI_CT |= (1 << TWI_CT_START); // generate START condition
189
  while(TWI_CT & (1 << TWI_CT_BUSY)); // wait until idle again
190
}

powered by: WebSVN 2.1.0

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