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

Subversion Repositories neorv32

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

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_spi.c - Serial Peripheral Interface Controller (SPI) 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_spi.c
38
 * @author Stephan Nolting
39 10 zero_gravi
 * @brief Serial peripheral interface controller (SPI) HW driver source file.
40 2 zero_gravi
 *
41 44 zero_gravi
 * @note These functions should only be used if the SPI unit was synthesized (IO_SPI_EN = true).
42 2 zero_gravi
 **************************************************************************/
43
 
44
#include "neorv32.h"
45
#include "neorv32_spi.h"
46
 
47
 
48
/**********************************************************************//**
49
 * Check if SPI unit was synthesized.
50
 *
51
 * @return 0 if SPI was not synthesized, 1 if SPI is available.
52
 **************************************************************************/
53
int neorv32_spi_available(void) {
54
 
55 12 zero_gravi
  if (SYSINFO_FEATURES & (1 << SYSINFO_FEATURES_IO_SPI)) {
56 2 zero_gravi
    return 1;
57
  }
58
  else {
59
    return 0;
60
  }
61
}
62
 
63
 
64
/**********************************************************************//**
65
 * Enable and configure SPI controller. The SPI control register bits are listed in #NEORV32_SPI_CT_enum.
66
 *
67
 * @param[in] prsc Clock prescaler select (0..7).  See #NEORV32_CLOCK_PRSC_enum.
68
 * @param[in] clk_polarity Idle clock polarity (0, 1).
69
 * @param[in] data_size Data transfer size (0: 8-bit, 1: 16-bit, 2: 24-bit, 3: 32-bit).
70
 **************************************************************************/
71 48 zero_gravi
void neorv32_spi_setup(uint8_t prsc, uint8_t clk_polarity, uint8_t data_size) {
72 2 zero_gravi
 
73
  SPI_CT = 0; // reset
74
 
75
  uint32_t ct_enable = 1;
76
  ct_enable = ct_enable << SPI_CT_EN;
77
 
78
  uint32_t ct_prsc = (uint32_t)(prsc & 0x07);
79
  ct_prsc = ct_prsc << SPI_CT_PRSC0;
80
 
81
  uint32_t ct_polarity = (uint32_t)(clk_polarity & 0x01);
82
  ct_polarity = ct_polarity << SPI_CT_CPHA;
83
 
84
  uint32_t ct_size = (uint32_t)(data_size & 0x03);
85
  ct_size = ct_size << SPI_CT_SIZE0;
86
 
87 48 zero_gravi
  SPI_CT = ct_enable | ct_prsc | ct_polarity | ct_size;
88 2 zero_gravi
}
89
 
90
 
91
/**********************************************************************//**
92
 * Disable and SPI controller.
93
 **************************************************************************/
94
void neorv32_spi_disable(void) {
95
 
96
  SPI_CT &= ~((uint32_t)(1 << SPI_CT_EN));
97
}
98
 
99
 
100
/**********************************************************************//**
101
 * Activate SPI chip select signal.
102
 *
103
 * @note The chip select output lines are LOW when activated.
104
 *
105
 * @param cs Chip select line to activate (0..7).
106
 **************************************************************************/
107
void neorv32_spi_cs_en(uint8_t cs) {
108
 
109
  uint32_t cs_mask = (uint32_t)(1 << (cs & 0x07));
110
  cs_mask = cs_mask << SPI_CT_CS0;
111
  SPI_CT |= cs_mask;
112
}
113
 
114
 
115
/**********************************************************************//**
116
 * Deactivate SPI chip select signal.
117
 *
118
 * @note The chip select output lines are HIGH when deactivated.
119
 *
120
 * @param cs Chip select line to deactivate (0..7).
121
 **************************************************************************/
122
void neorv32_spi_cs_dis(uint8_t cs) {
123
 
124
  uint32_t cs_mask = (uint32_t)(1 << (cs & 0x07));
125
  cs_mask = cs_mask << SPI_CT_CS0;
126
  SPI_CT &= ~cs_mask;
127
}
128
 
129
 
130
/**********************************************************************//**
131
 * Initiate SPI transfer.
132
 *
133 36 zero_gravi
 * @warning The SPI always sends MSB first.
134
 *
135 2 zero_gravi
 * @note This function is blocking.
136
 *
137
 * @param tx_data Transmit data (8/16/24/32-bit, LSB-aligned).
138
 * @return Receive data (8/16/24/32-bit, LSB-aligned).
139
 **************************************************************************/
140
uint32_t neorv32_spi_trans(uint32_t tx_data) {
141
 
142
  SPI_DATA = tx_data; // trigger transfer
143
  while((SPI_CT & (1<<SPI_CT_BUSY)) != 0); // wait for current transfer to finish
144
 
145
  return SPI_DATA;
146
}
147 23 zero_gravi
 
148
 
149
/**********************************************************************//**
150
 * Check if SPI transceiver is busy.
151
 *
152
 * @note This function is blocking.
153
 *
154
 * @return 0 if idle, 1 if busy
155
 **************************************************************************/
156
int neorv32_spi_busy(void) {
157
 
158
  if ((SPI_CT & (1<<SPI_CT_BUSY)) != 0) {
159
    return 1;
160
  }
161
  return 0;
162
}

powered by: WebSVN 2.1.0

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