Skip to content

Commit 67ea926

Browse files
YuguoWHabrodkin
authored andcommitted
example: add spi slave example
use IoTDK board as SPI master and send data to EMSK as SPI Slave. The example shows how to use SPI driver with polling mode for both master and slave. Signed-off-by: Yuguo Zou <[email protected]>
1 parent 4e1afd2 commit 67ea926

File tree

3 files changed

+298
-0
lines changed

3 files changed

+298
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
.. _example_spi_slave:
2+
3+
SPI_SLAVE
4+
#########
5+
6+
Overview
7+
********
8+
9+
This example shows SPI driver polling mode usage for both master and slave.
10+
11+
Detailed Description
12+
====================
13+
* Extra Required Tools
14+
jumper wire
15+
16+
* Extra Required Peripherals
17+
NO
18+
19+
* Design Concept
20+
The master will transfer data to slave. Upon recieving, the slave will loopback the data.
21+
22+
* Usage Manual
23+
This example requires one IoTDK board (as master) and one EMSK board (as slave)
24+
25+
* Extra Comments
26+
NO
27+
28+
Buidling and Running
29+
********************
30+
31+
Use jumper wire to connect EMSK P1 lower row with IoTDK Pmod_B upper row. For pin map please refer to documents:
32+
`EMSK <https://foss-for-synopsys-dwc-arc-processors.github.io/embarc_osp/doc/build/html/board/emsk.html#pmod-pins-definition>`_
33+
`IoTDK <https://foss-for-synopsys-dwc-arc-processors.github.io/embarc_osp/doc/build/html/board/iotdk.html#extension-interfaces>`_
34+
35+
36+
.. note:: Make sure you have also connect GND of two boards with jumper wire
37+
38+
39+
With two different types of board connected to your computer, an extra command parameter ''DIG_NAME'' is needed to tell which board is which.
40+
Launch ''Digilent Adept'' which should have been installed along with board driver to get the ''DIG_NAME''. In our developing secnario, the ''DIG_NAME'' for EMSK is TE0604-03
41+
And the ''DIG_NAME'' for IoTDK is IoTDK. Please noted that the ''DIG_NAME'' may varies from board to board.
42+
43+
*please run EMSK (SPI slave) program first*, tThe commands to run this example are as follows:
44+
45+
.. code-block:: console
46+
47+
$ cd <embarc_root>/example/baremetal/spi_slave
48+
$ make BOARD=emsk BD_VER=22 CUR_CORE=arcem9d DIG_NAME=TE0604-03 run
49+
50+
If you do not have an EMSK development board, you can use the nSIM simulator
51+
which have been installed in MetaWare IDE.
52+
53+
.. code-block:: console
54+
55+
$ cd <embarc_root>/example/baremetal/spi_slave
56+
$ make BOARD=iotdk DIG_NAME=IoTDK run
57+
58+
59+
Sample Output
60+
=============
61+
62+
The output from IoTDK (SPI master) are shown below
63+
64+
.. code-block:: console
65+
66+
-----------------------------------------------------------
67+
____ _ ____
68+
| _ \ _____ _____ _ __ ___ __| | __ ) _ _
69+
| |_) / _ \ \ /\ / / _ \ '__/ _ \/ _` | _ \| | | |
70+
| __/ (_) \ V V / __/ | | __/ (_| | |_) | |_| |
71+
|_| \___/ \_/\_/ \___|_| \___|\__,_|____/ \__, |
72+
|___/
73+
_ _ ____ ____
74+
___ _ __ ___ | |__ / \ | _ \ / ___|
75+
/ _ \ '_ ` _ \| '_ \ / _ \ | |_) | |
76+
| __/ | | | | | |_) / ___ \| _ <| |___
77+
\___|_| |_| |_|_.__/_/ \_\_| \_\\____|
78+
------------------------------------------------------------
79+
80+
embARC Build Time: Oct 13 2021, 10:35:16
81+
Compiler Version: Metaware, Clang 11.1.0
82+
PI Master & Slave test Oct 13 2021 / 14:42:48
83+
IoTDK Pmod B upper row is working as SPI Master
84+
SPI Master is successfully opened
85+
SPI Master Buffer
86+
TX: 0x0 0x0 0x0 0x0
87+
RX: 0x0 0x0 0x0 0x0
88+
SPI Master Buffer
89+
TX: 0x1 0x1 0x1 0x1
90+
RX: 0x0 0x0 0x0 0x0
91+
SPI Master Buffer
92+
TX: 0x2 0x2 0x2 0x2
93+
RX: 0x1 0x1 0x1 0x1
94+
SPI Master Buffer
95+
TX: 0x3 0x3 0x3 0x3
96+
RX: 0x2 0x2 0x2 0x2
97+
SPI Master Buffer
98+
TX: 0x4 0x4 0x4 0x4
99+
RX: 0x3 0x3 0x3 0x3
100+
SPI Master Buffer
101+
TX: 0x5 0x5 0x5 0x5
102+
RX: 0x4 0x4 0x4 0x4
103+
SPI Master Buffer
104+
TX: 0x6 0x6 0x6 0x6
105+
RX: 0x5 0x5 0x5 0x5
106+
.................
107+
108+
The output from EMSK (SPI slave) are shown below
109+
110+
.. code-block:: console
111+
112+
-----------------------------------------------------------
113+
____ _ ____
114+
| _ \ _____ _____ _ __ ___ __| | __ ) _ _
115+
| |_) / _ \ \ /\ / / _ \ '__/ _ \/ _` | _ \| | | |
116+
| __/ (_) \ V V / __/ | | __/ (_| | |_) | |_| |
117+
|_| \___/ \_/\_/ \___|_| \___|\__,_|____/ \__, |
118+
|___/
119+
_ _ ____ ____
120+
___ _ __ ___ | |__ / \ | _ \ / ___|
121+
/ _ \ '_ ` _ \| '_ \ / _ \ | |_) | |
122+
| __/ | | | | | |_) / ___ \| _ <| |___
123+
\___|_| |_| |_|_.__/_/ \_\_| \_\\____|
124+
------------------------------------------------------------
125+
126+
embARC Build Time: Oct 11 2021, 10:12:43
127+
Compiler Version: Metaware, Clang 11.1.0
128+
SPI Master & Slave test Oct 13 2021 / 14:42:47
129+
EMSK Pmod 1 (J1) lower row is working as SPI Slave
130+
SPI Slave is successfully opened
131+
SPI Slave Buffer
132+
TX: 0x0 0x0 0x0 0x0
133+
RX: 0x0 0x0 0x0 0x0
134+
SPI Slave Buffer
135+
TX: 0x0 0x0 0x0 0x0
136+
RX: 0x1 0x1 0x1 0x1
137+
SPI Slave Buffer
138+
TX: 0x1 0x1 0x1 0x1
139+
RX: 0x2 0x2 0x2 0x2
140+
SPI Slave Buffer
141+
TX: 0x2 0x2 0x2 0x2
142+
RX: 0x3 0x3 0x3 0x3
143+
SPI Slave Buffer
144+
TX: 0x3 0x3 0x3 0x3
145+
RX: 0x4 0x4 0x4 0x4
146+
SPI Slave Buffer
147+
TX: 0x4 0x4 0x4 0x4
148+
RX: 0x5 0x5 0x5 0x5
149+
SPI Slave Buffer
150+
TX: 0x5 0x5 0x5 0x5
151+
RX: 0x6 0x6 0x6 0x6
152+
.................

example/baremetal/spi_slave/main.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Application name
2+
APPL ?= spi_slave
3+
4+
#
5+
# root dir of embARC
6+
#
7+
EMBARC_ROOT = ../../..
8+
9+
MID_SEL = common
10+
11+
# application source dirs
12+
APPL_CSRC_DIR = .
13+
APPL_ASMSRC_DIR = .
14+
15+
# application include dirs
16+
APPL_INC_DIR = .
17+
18+
# include current project makefile
19+
COMMON_COMPILE_PREREQUISITES += makefile
20+
21+
### Options above must be added before include options.mk ###
22+
# include key embARC build system makefile
23+
include $(EMBARC_ROOT)/options/options.mk

0 commit comments

Comments
 (0)