| 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 |
|
|
// # 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_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 |
|
|
* @note These functions should only be used if the SPI unit was synthesized (IO_SPI_USE = true).
|
| 42 |
|
|
**************************************************************************/
|
| 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 |
|
|
* @param[in] irq_en Enable transfer-done interrupt when 1.
|
| 71 |
|
|
**************************************************************************/
|
| 72 |
36 |
zero_gravi |
void neorv32_spi_setup(uint8_t prsc, uint8_t clk_polarity, uint8_t data_size, uint8_t irq_en) {
|
| 73 |
2 |
zero_gravi |
|
| 74 |
|
|
SPI_CT = 0; // reset
|
| 75 |
|
|
|
| 76 |
|
|
uint32_t ct_enable = 1;
|
| 77 |
|
|
ct_enable = ct_enable << SPI_CT_EN;
|
| 78 |
|
|
|
| 79 |
|
|
uint32_t ct_prsc = (uint32_t)(prsc & 0x07);
|
| 80 |
|
|
ct_prsc = ct_prsc << SPI_CT_PRSC0;
|
| 81 |
|
|
|
| 82 |
|
|
uint32_t ct_polarity = (uint32_t)(clk_polarity & 0x01);
|
| 83 |
|
|
ct_polarity = ct_polarity << SPI_CT_CPHA;
|
| 84 |
|
|
|
| 85 |
|
|
uint32_t ct_size = (uint32_t)(data_size & 0x03);
|
| 86 |
|
|
ct_size = ct_size << SPI_CT_SIZE0;
|
| 87 |
|
|
|
| 88 |
|
|
uint32_t ct_irq = (uint32_t)(irq_en & 0x01);
|
| 89 |
|
|
ct_irq = ct_irq << SPI_CT_IRQ_EN;
|
| 90 |
|
|
|
| 91 |
36 |
zero_gravi |
SPI_CT = ct_enable | ct_prsc | ct_polarity | ct_size | ct_irq;
|
| 92 |
2 |
zero_gravi |
}
|
| 93 |
|
|
|
| 94 |
|
|
|
| 95 |
|
|
/**********************************************************************//**
|
| 96 |
|
|
* Disable and SPI controller.
|
| 97 |
|
|
**************************************************************************/
|
| 98 |
|
|
void neorv32_spi_disable(void) {
|
| 99 |
|
|
|
| 100 |
|
|
SPI_CT &= ~((uint32_t)(1 << SPI_CT_EN));
|
| 101 |
|
|
}
|
| 102 |
|
|
|
| 103 |
|
|
|
| 104 |
|
|
/**********************************************************************//**
|
| 105 |
|
|
* Activate SPI chip select signal.
|
| 106 |
|
|
*
|
| 107 |
|
|
* @note The chip select output lines are LOW when activated.
|
| 108 |
|
|
*
|
| 109 |
|
|
* @param cs Chip select line to activate (0..7).
|
| 110 |
|
|
**************************************************************************/
|
| 111 |
|
|
void neorv32_spi_cs_en(uint8_t cs) {
|
| 112 |
|
|
|
| 113 |
|
|
uint32_t cs_mask = (uint32_t)(1 << (cs & 0x07));
|
| 114 |
|
|
cs_mask = cs_mask << SPI_CT_CS0;
|
| 115 |
|
|
SPI_CT |= cs_mask;
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
|
| 119 |
|
|
/**********************************************************************//**
|
| 120 |
|
|
* Deactivate SPI chip select signal.
|
| 121 |
|
|
*
|
| 122 |
|
|
* @note The chip select output lines are HIGH when deactivated.
|
| 123 |
|
|
*
|
| 124 |
|
|
* @param cs Chip select line to deactivate (0..7).
|
| 125 |
|
|
**************************************************************************/
|
| 126 |
|
|
void neorv32_spi_cs_dis(uint8_t cs) {
|
| 127 |
|
|
|
| 128 |
|
|
uint32_t cs_mask = (uint32_t)(1 << (cs & 0x07));
|
| 129 |
|
|
cs_mask = cs_mask << SPI_CT_CS0;
|
| 130 |
|
|
SPI_CT &= ~cs_mask;
|
| 131 |
|
|
}
|
| 132 |
|
|
|
| 133 |
|
|
|
| 134 |
|
|
/**********************************************************************//**
|
| 135 |
|
|
* Initiate SPI transfer.
|
| 136 |
|
|
*
|
| 137 |
36 |
zero_gravi |
* @warning The SPI always sends MSB first.
|
| 138 |
|
|
*
|
| 139 |
2 |
zero_gravi |
* @note This function is blocking.
|
| 140 |
|
|
*
|
| 141 |
|
|
* @param tx_data Transmit data (8/16/24/32-bit, LSB-aligned).
|
| 142 |
|
|
* @return Receive data (8/16/24/32-bit, LSB-aligned).
|
| 143 |
|
|
**************************************************************************/
|
| 144 |
|
|
uint32_t neorv32_spi_trans(uint32_t tx_data) {
|
| 145 |
|
|
|
| 146 |
|
|
SPI_DATA = tx_data; // trigger transfer
|
| 147 |
|
|
while((SPI_CT & (1<<SPI_CT_BUSY)) != 0); // wait for current transfer to finish
|
| 148 |
|
|
|
| 149 |
|
|
return SPI_DATA;
|
| 150 |
|
|
}
|
| 151 |
23 |
zero_gravi |
|
| 152 |
|
|
|
| 153 |
|
|
/**********************************************************************//**
|
| 154 |
|
|
* Check if SPI transceiver is busy.
|
| 155 |
|
|
*
|
| 156 |
|
|
* @note This function is blocking.
|
| 157 |
|
|
*
|
| 158 |
|
|
* @return 0 if idle, 1 if busy
|
| 159 |
|
|
**************************************************************************/
|
| 160 |
|
|
int neorv32_spi_busy(void) {
|
| 161 |
|
|
|
| 162 |
|
|
if ((SPI_CT & (1<<SPI_CT_BUSY)) != 0) {
|
| 163 |
|
|
return 1;
|
| 164 |
|
|
}
|
| 165 |
|
|
return 0;
|
| 166 |
|
|
}
|