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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_spi.c] - Blame information for rev 74

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
// #################################################################################################
2 10 zero_gravi
// # << NEORV32: neorv32_spi.c - Serial Peripheral Interface Controller (SPI) HW Driver >>         #
3 2 zero_gravi
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6 70 zero_gravi
// # Copyright (c) 2022, 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_spi.c
38 10 zero_gravi
 * @brief Serial peripheral interface controller (SPI) HW driver source file.
39 2 zero_gravi
 *
40 44 zero_gravi
 * @note These functions should only be used if the SPI unit was synthesized (IO_SPI_EN = true).
41 2 zero_gravi
 **************************************************************************/
42
 
43
#include "neorv32.h"
44
#include "neorv32_spi.h"
45
 
46
 
47
/**********************************************************************//**
48
 * Check if SPI unit was synthesized.
49
 *
50
 * @return 0 if SPI was not synthesized, 1 if SPI is available.
51
 **************************************************************************/
52
int neorv32_spi_available(void) {
53
 
54 64 zero_gravi
  if (NEORV32_SYSINFO.SOC & (1 << SYSINFO_SOC_IO_SPI)) {
55 2 zero_gravi
    return 1;
56
  }
57
  else {
58
    return 0;
59
  }
60
}
61
 
62
 
63
/**********************************************************************//**
64 64 zero_gravi
 * Enable and configure SPI controller. The SPI control register bits are listed in #NEORV32_SPI_CTRL_enum.
65 2 zero_gravi
 *
66
 * @param[in] prsc Clock prescaler select (0..7).  See #NEORV32_CLOCK_PRSC_enum.
67 65 zero_gravi
 * @param[in] clk_phase Clock phase (0=sample on rising edge, 1=sample on falling edge).
68
 * @param[in] clk_polarity Clock polarity (when idle).
69 2 zero_gravi
 * @param[in] data_size Data transfer size (0: 8-bit, 1: 16-bit, 2: 24-bit, 3: 32-bit).
70
 **************************************************************************/
71 65 zero_gravi
void neorv32_spi_setup(uint8_t prsc, uint8_t clk_phase, uint8_t clk_polarity, uint8_t data_size) {
72 2 zero_gravi
 
73 64 zero_gravi
  NEORV32_SPI.CTRL = 0; // reset
74 2 zero_gravi
 
75
  uint32_t ct_enable = 1;
76 64 zero_gravi
  ct_enable = ct_enable << SPI_CTRL_EN;
77 2 zero_gravi
 
78
  uint32_t ct_prsc = (uint32_t)(prsc & 0x07);
79 64 zero_gravi
  ct_prsc = ct_prsc << SPI_CTRL_PRSC0;
80 2 zero_gravi
 
81 65 zero_gravi
  uint32_t ct_phase = (uint32_t)(clk_phase & 0x01);
82
  ct_phase = ct_phase << SPI_CTRL_CPHA;
83
 
84 2 zero_gravi
  uint32_t ct_polarity = (uint32_t)(clk_polarity & 0x01);
85 65 zero_gravi
  ct_polarity = ct_polarity << SPI_CTRL_CPOL;
86 2 zero_gravi
 
87
  uint32_t ct_size = (uint32_t)(data_size & 0x03);
88 64 zero_gravi
  ct_size = ct_size << SPI_CTRL_SIZE0;
89 2 zero_gravi
 
90 65 zero_gravi
  NEORV32_SPI.CTRL = ct_enable | ct_prsc | ct_phase | ct_polarity | ct_size;
91 2 zero_gravi
}
92
 
93
 
94
/**********************************************************************//**
95 52 zero_gravi
 * Disable SPI controller.
96 2 zero_gravi
 **************************************************************************/
97
void neorv32_spi_disable(void) {
98
 
99 64 zero_gravi
  NEORV32_SPI.CTRL &= ~((uint32_t)(1 << SPI_CTRL_EN));
100 2 zero_gravi
}
101
 
102
 
103
/**********************************************************************//**
104 61 zero_gravi
 * Enable SPI controller.
105
 **************************************************************************/
106
void neorv32_spi_enable(void) {
107
 
108 64 zero_gravi
  NEORV32_SPI.CTRL |= ((uint32_t)(1 << SPI_CTRL_EN));
109 61 zero_gravi
}
110
 
111
 
112
/**********************************************************************//**
113 70 zero_gravi
 * Enable high-speed SPI mode (running at half of the processor clock).
114
 *
115
 * @note High-speed SPI mode ignores the programmed clock prescaler configuration.
116
 **************************************************************************/
117
void neorv32_spi_highspeed_enable(void) {
118
 
119
  NEORV32_SPI.CTRL |= 1 << SPI_CTRL_HIGHSPEED;
120
}
121
 
122
 
123
/**********************************************************************//**
124
 * Disable high-speed SPI mode.
125
 *
126
 * @note High-speed SPI mode ignores the programmed clock prescaler configuration.
127
 **************************************************************************/
128
void neorv32_spi_highspeed_disable(void) {
129
 
130
  NEORV32_SPI.CTRL &= ~(1 << SPI_CTRL_HIGHSPEED);
131
}
132
 
133
 
134
/**********************************************************************//**
135 2 zero_gravi
 * Activate SPI chip select signal.
136
 *
137
 * @note The chip select output lines are LOW when activated.
138
 *
139
 * @param cs Chip select line to activate (0..7).
140
 **************************************************************************/
141
void neorv32_spi_cs_en(uint8_t cs) {
142
 
143
  uint32_t cs_mask = (uint32_t)(1 << (cs & 0x07));
144 64 zero_gravi
  cs_mask = cs_mask << SPI_CTRL_CS0;
145
  NEORV32_SPI.CTRL |= cs_mask;
146 2 zero_gravi
}
147
 
148
 
149
/**********************************************************************//**
150
 * Deactivate SPI chip select signal.
151
 *
152
 * @note The chip select output lines are HIGH when deactivated.
153
 *
154
 * @param cs Chip select line to deactivate (0..7).
155
 **************************************************************************/
156
void neorv32_spi_cs_dis(uint8_t cs) {
157
 
158
  uint32_t cs_mask = (uint32_t)(1 << (cs & 0x07));
159 64 zero_gravi
  cs_mask = cs_mask << SPI_CTRL_CS0;
160
  NEORV32_SPI.CTRL &= ~cs_mask;
161 2 zero_gravi
}
162
 
163
 
164
/**********************************************************************//**
165
 * Initiate SPI transfer.
166
 *
167
 * @note This function is blocking.
168
 *
169
 * @param tx_data Transmit data (8/16/24/32-bit, LSB-aligned).
170
 * @return Receive data (8/16/24/32-bit, LSB-aligned).
171
 **************************************************************************/
172
uint32_t neorv32_spi_trans(uint32_t tx_data) {
173
 
174 64 zero_gravi
  NEORV32_SPI.DATA = tx_data; // trigger transfer
175
  while((NEORV32_SPI.CTRL & (1<<SPI_CTRL_BUSY)) != 0); // wait for current transfer to finish
176 2 zero_gravi
 
177 64 zero_gravi
  return NEORV32_SPI.DATA;
178 2 zero_gravi
}
179 23 zero_gravi
 
180
 
181
/**********************************************************************//**
182 66 zero_gravi
 * Initiate SPI TX transfer (non-blocking).
183
 *
184
 * @param tx_data Transmit data (8/16/24/32-bit, LSB-aligned).
185
 **************************************************************************/
186
void neorv32_spi_put_nonblocking(uint32_t tx_data) {
187
 
188
  NEORV32_SPI.DATA = tx_data; // trigger transfer
189
}
190
 
191
 
192
/**********************************************************************//**
193
 * Get SPI RX data (non-blocking).
194
 *
195
 * @return Receive data (8/16/24/32-bit, LSB-aligned).
196
 **************************************************************************/
197
uint32_t neorv32_spi_get_nonblocking(void) {
198
 
199
  return NEORV32_SPI.DATA;
200
}
201
 
202
 
203
/**********************************************************************//**
204 23 zero_gravi
 * Check if SPI transceiver is busy.
205
 *
206
 * @return 0 if idle, 1 if busy
207
 **************************************************************************/
208
int neorv32_spi_busy(void) {
209
 
210 64 zero_gravi
  if ((NEORV32_SPI.CTRL & (1<<SPI_CTRL_BUSY)) != 0) {
211 23 zero_gravi
    return 1;
212
  }
213 65 zero_gravi
  else {
214
    return 0;
215
  }
216 23 zero_gravi
}

powered by: WebSVN 2.1.0

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