|
| 1 | +/* ------------------------------------------ |
| 2 | + * Copyright (c) 2021, Synopsys, Inc. All rights reserved. |
| 3 | +
|
| 4 | + * Redistribution and use in source and binary forms, with or without modification, |
| 5 | + * are permitted provided that the following conditions are met: |
| 6 | +
|
| 7 | + * 1) Redistributions of source code must retain the above copyright notice, this |
| 8 | + * list of conditions and the following disclaimer. |
| 9 | +
|
| 10 | + * 2) Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation and/or |
| 12 | + * other materials provided with the distribution. |
| 13 | +
|
| 14 | + * 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may |
| 15 | + * be used to endorse or promote products derived from this software without |
| 16 | + * specific prior written permission. |
| 17 | +
|
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 22 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 25 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | + --------------------------------------------- */ |
| 30 | +#include "embARC.h" |
| 31 | +#include "embARC_debug.h" |
| 32 | + |
| 33 | +#define BUFFER_SIZE 4 |
| 34 | + |
| 35 | +void printbuffer(uint8_t *tx, uint8_t *rx) |
| 36 | +{ |
| 37 | +#ifdef BOARD_EMSK |
| 38 | + EMBARC_PRINTF("SPI Slave Buffer\n"); |
| 39 | +#elif defined BOARD_IOTDK |
| 40 | + EMBARC_PRINTF("SPI Master Buffer\n"); |
| 41 | +#endif |
| 42 | + EMBARC_PRINTF("TX:"); |
| 43 | + for(uint8_t i = 0; i < BUFFER_SIZE; i++){ |
| 44 | + EMBARC_PRINTF(" 0x%x", tx[i]); |
| 45 | + } |
| 46 | + EMBARC_PRINTF("\nRX:"); |
| 47 | + for(uint8_t i = 0; i < BUFFER_SIZE; i++){ |
| 48 | + EMBARC_PRINTF(" 0x%x", rx[i]); |
| 49 | + } |
| 50 | + EMBARC_PRINTF("\n"); |
| 51 | + return; |
| 52 | +} |
| 53 | + |
| 54 | +/* Please run EMSK (SPI Slave) first */ |
| 55 | +int main(void) |
| 56 | +{ |
| 57 | + unsigned int ercd = 0; |
| 58 | + EMBARC_PRINTF("SPI Master & Slave test " __DATE__ " / " __TIME__ " \n"); |
| 59 | + |
| 60 | +#ifdef BOARD_EMSK |
| 61 | + EMBARC_PRINTF("EMSK Pmod 1 (J1) lower row is working as SPI Slave\n"); |
| 62 | + |
| 63 | + DEV_SPI_PTR pSpiSlave = spi_get_dev(DW_SPI_1_ID); |
| 64 | + |
| 65 | + ercd = pSpiSlave->spi_open(DEV_SLAVE_MODE, SPI_CLK_MODE_0); |
| 66 | + if ((ercd == E_OPNED) || (ercd == E_OK)) { |
| 67 | + EMBARC_PRINTF("SPI Slave is successfully opened\n"); |
| 68 | + } else { |
| 69 | + EMBARC_PRINTF("SPI Slave is unable to open: error=%d\nEnd of Test\n", ercd); |
| 70 | + return E_SYS; |
| 71 | + } |
| 72 | + DEV_SPI_TRANSFER spi_s_xfer; |
| 73 | + uint8_t spi_s_buffer_tx[BUFFER_SIZE]={0}; |
| 74 | + uint8_t spi_s_buffer_rx[BUFFER_SIZE]={0}; |
| 75 | + DEV_SPI_XFER_SET_TXBUF(&spi_s_xfer, spi_s_buffer_tx, 0, BUFFER_SIZE); |
| 76 | + DEV_SPI_XFER_SET_RXBUF(&spi_s_xfer, spi_s_buffer_rx, 0, BUFFER_SIZE); |
| 77 | + DEV_SPI_XFER_SET_NEXT(&spi_s_xfer, NULL); |
| 78 | + |
| 79 | + while (1) { |
| 80 | + ercd = pSpiSlave->spi_control(SPI_CMD_TRANSFER_POLLING, CONV2VOID(&spi_s_xfer)); |
| 81 | + printbuffer(spi_s_buffer_tx, spi_s_buffer_rx); |
| 82 | + // loop back the data |
| 83 | + for(uint8_t i = 0; i < BUFFER_SIZE; i++){ |
| 84 | + spi_s_buffer_tx[i] = spi_s_buffer_rx[i]; |
| 85 | + } |
| 86 | + board_delay_ms(500, 1); |
| 87 | + } |
| 88 | +#elif defined BOARD_IOTDK |
| 89 | + EMBARC_PRINTF("IoTDK Pmod B upper row is working as SPI Master\n"); |
| 90 | + DEV_SPI_PTR pSpiMaster = spi_get_dev(DFSS_SPI_1_ID); |
| 91 | + ercd = pSpiMaster->spi_open(DEV_MASTER_MODE, 1000000); |
| 92 | + if ((ercd == E_OPNED) || (ercd == E_OK)) { |
| 93 | + EMBARC_PRINTF("SPI Master is successfully opened\n"); |
| 94 | + ercd = pSpiMaster->spi_control(SPI_CMD_SET_CLK_MODE, CONV2VOID(SPI_CLK_MODE_0)); |
| 95 | + } else { |
| 96 | + EMBARC_PRINTF("SPI Master is unable to open: error=%d\nEnd of Test\n", ercd); |
| 97 | + return E_SYS; |
| 98 | + } |
| 99 | + DEV_SPI_TRANSFER spi_m_xfer; |
| 100 | + uint8_t spi_m_buffer_tx[BUFFER_SIZE]={0}; |
| 101 | + uint8_t spi_m_buffer_rx[BUFFER_SIZE]={0}; |
| 102 | + DEV_SPI_XFER_SET_TXBUF(&spi_m_xfer, spi_m_buffer_tx, 0, BUFFER_SIZE); |
| 103 | + DEV_SPI_XFER_SET_RXBUF(&spi_m_xfer, spi_m_buffer_rx, 0, BUFFER_SIZE); |
| 104 | + DEV_SPI_XFER_SET_NEXT(&spi_m_xfer, NULL); |
| 105 | + |
| 106 | + while (1) { |
| 107 | + ercd = pSpiMaster->spi_control(SPI_CMD_MST_SEL_DEV, CONV2VOID(0)); |
| 108 | + ercd = pSpiMaster->spi_control(SPI_CMD_TRANSFER_POLLING, CONV2VOID(&spi_m_xfer)); |
| 109 | + ercd = pSpiMaster->spi_control(SPI_CMD_MST_DSEL_DEV, CONV2VOID(0)); |
| 110 | + printbuffer(spi_m_buffer_tx, spi_m_buffer_rx); |
| 111 | + for(uint8_t i = 0; i < BUFFER_SIZE; i++){ |
| 112 | + spi_m_buffer_tx[i]++; |
| 113 | + } |
| 114 | + board_delay_ms(3000, 1); |
| 115 | + } |
| 116 | + |
| 117 | +#else |
| 118 | + EMBARC_PRINTF("This example is not supported under current configurations \r\n"); |
| 119 | +#endif |
| 120 | + |
| 121 | + |
| 122 | + return E_SYS; |
| 123 | +} |
0 commit comments