Skip to content

Commit e60106c

Browse files
[sw] SPI host loopback smoketest
1 parent 7ab686e commit e60106c

7 files changed

Lines changed: 96 additions & 1 deletion

File tree

sw/device/lib/hal/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
# SPDX-License-Identifier: Apache-2.0
44

5-
set(SRCS clkmgr.c gpio.c i2c.c mailbox.c mocha.c plic.c rstmgr.c spi_device.c timer.c uart.c)
5+
set(SRCS clkmgr.c gpio.c i2c.c mailbox.c mocha.c plic.c rstmgr.c spi_device.c spi_host.c timer.c uart.c)
66

77
mocha_add_library(NAME hal LIBRARIES SOURCES ${SRCS})

sw/device/lib/hal/mocha.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static const uintptr_t uart_base = 0x41000000ul;
2020
static const uintptr_t i2c_base = 0x42000000ul;
2121
static const uintptr_t spi_device_base = 0x43000000ul;
2222
static const uintptr_t timer_base = 0x44000000ul;
23+
static const uintptr_t spi_host_base = 0x45000000ul;
2324
static const uintptr_t plic_base = 0x48000000ul;
2425

2526
#if defined(__riscv_zcherihybrid)
@@ -114,6 +115,15 @@ timer_t mocha_system_timer(void)
114115
#endif /* defined(__riscv_zcherihybrid) */
115116
}
116117

118+
spi_host_t mocha_system_spi_host(void)
119+
{
120+
#if defined(__riscv_zcherihybrid)
121+
return (spi_host_t)create_mmio_capability(spi_host_base, 0x38u);
122+
#else /* !defined(__riscv_zcherihybrid) */
123+
return (spi_host_t)spi_host_base;
124+
#endif /* defined(__riscv_zcherihybrid) */
125+
}
126+
117127
plic_t mocha_system_plic(void)
118128
{
119129
#if defined(__riscv_zcherihybrid)

sw/device/lib/hal/mocha.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "hal/plic.h"
1414
#include "hal/rstmgr.h"
1515
#include "hal/spi_device.h"
16+
#include "hal/spi_host.h"
1617
#include "hal/timer.h"
1718
#include "hal/uart.h"
1819

@@ -37,6 +38,7 @@ uart_t mocha_system_uart(void);
3738
i2c_t mocha_system_i2c(void);
3839
spi_device_t mocha_system_spi_device(void);
3940
timer_t mocha_system_timer(void);
41+
spi_host_t mocha_system_spi_host(void);
4042
plic_t mocha_system_plic(void);
4143
void *mocha_system_dram(void);
4244

sw/device/lib/hal/spi_host.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright lowRISC contributors (COSMIC project).
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#include "hal/spi_host.h"
6+
#include "hal/mmio.h"
7+
#include <stdbool.h>
8+
#include <stdint.h>
9+
10+
void spi_host_init(spi_host_t spi_host)
11+
{
12+
DEV_WRITE(spi_host + SPI_HOST_CONTROL_REG,
13+
SPI_HOST_CONTROL_SPIEN_MASK | SPI_HOST_CONTROL_OUTPUTEN_MASK);
14+
}
15+
16+
void spi_host_write(spi_host_t spi_host, uint32_t data)
17+
{
18+
DEV_WRITE(spi_host + SPI_HOST_TXDATA_REG, data);
19+
DEV_WRITE(spi_host + SPI_HOST_COMMAND_REG,
20+
SPI_HOST_COMMAND_DIRECTION_BIDIRECTIONAL |
21+
((3 << SPI_HOST_COMMAND_LEN_OFF) & SPI_HOST_COMMAND_LEN_MASK));
22+
}
23+
24+
uint32_t spi_host_read(spi_host_t spi_host)
25+
{
26+
return DEV_READ(spi_host + SPI_HOST_RXDATA_REG);
27+
}

sw/device/lib/hal/spi_host.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright lowRISC contributors (COSMIC project).
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#pragma once
6+
7+
#include "hal/mmio.h"
8+
#include <stdbool.h>
9+
#include <stdint.h>
10+
11+
#define SPI_HOST_INTR_STATE_REG (0x0)
12+
#define SPI_HOST_INTR_ENABLE_REG (0x4)
13+
#define SPI_HOST_INTR_TEST_REG (0x8)
14+
#define SPI_HOST_CONTROL_REG (0x10)
15+
#define SPI_HOST_CONTROL_SPIEN_MASK (1u << 31)
16+
#define SPI_HOST_CONTROL_OUTPUTEN_MASK (1u << 29)
17+
#define SPI_HOST_STATUS_REG (0x14)
18+
#define SPI_HOST_CONFIGOPTS_REG (0x18)
19+
#define SPI_HOST_CSID_REG (0x1C)
20+
#define SPI_HOST_COMMAND_REG (0x20)
21+
#define SPI_HOST_COMMAND_DIRECTION_OFFSET (3)
22+
#define SPI_HOST_COMMAND_DIRECTION_RECEIVE (1 << SPI_HOST_COMMAND_DIRECTION_OFFSET)
23+
#define SPI_HOST_COMMAND_DIRECTION_TRANSMIT (2 << SPI_HOST_COMMAND_DIRECTION_OFFSET)
24+
#define SPI_HOST_COMMAND_DIRECTION_BIDIRECTIONAL (3 << SPI_HOST_COMMAND_DIRECTION_OFFSET)
25+
#define SPI_HOST_COMMAND_LEN_OFF (5)
26+
#define SPI_HOST_COMMAND_LEN_MASK (0xFFFFF << SPI_HOST_COMMAND_LEN_OFF)
27+
#define SPI_HOST_RXDATA_REG (0x24)
28+
#define SPI_HOST_TXDATA_REG (0x28)
29+
30+
typedef void *spi_host_t;
31+
32+
void spi_host_init(spi_host_t spi_host);
33+
void spi_host_write(spi_host_t spi_host, uint32_t data);
34+
uint32_t spi_host_read(spi_host_t spi_host);

sw/device/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mocha_add_test(NAME plic_smoketest SOURCES plic/smoketest.c LIBRARIES ${LIBS} FP
1313
# Cannot currently run this on FPGA because the boot ROM expects the test to be provided on the SPI again.
1414
mocha_add_test(NAME rstmgr_software_reset SOURCES rstmgr/software_reset.c LIBRARIES ${LIBS})
1515
mocha_add_test(NAME spi_device_smoketest SOURCES spi_device/smoketest.c LIBRARIES ${LIBS} FPGA)
16+
mocha_add_test(NAME spi_host_smoketest SOURCES spi_host/smoketest.c LIBRARIES ${LIBS} FPGA)
1617
mocha_add_test(NAME tag_controller_smoketest SOURCES tag_controller/smoketest.c LIBRARIES ${LIBS} FPGA)
1718
mocha_add_test(NAME tag_controller_tag_test SOURCES tag_controller/tag_test.c LIBRARIES ${LIBS} FPGA)
1819
mocha_add_test(NAME timer_smoketest SOURCES timer/smoketest.c LIBRARIES ${LIBS} FPGA)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright lowRISC contributors (COSMIC project).
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#include "hal/mocha.h"
6+
#include "hal/spi_host.h"
7+
#include <stdbool.h>
8+
#include <stdint.h>
9+
10+
bool test_main()
11+
{
12+
uint32_t tx_data = 0xDEADC0DE;
13+
uint32_t rx_data;
14+
spi_host_t spi_host;
15+
16+
spi_host = mocha_system_spi_host();
17+
spi_host_init(spi_host);
18+
spi_host_write(spi_host, tx_data);
19+
rx_data = spi_host_read(spi_host);
20+
return tx_data == rx_data;
21+
}

0 commit comments

Comments
 (0)