Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions sw/device/examples/hello_world/hello_world.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "hal/mocha.h"
#include "hal/spi_device.h"
#include "hal/timer.h"
#include "hal/uart.h"
#include <stdint.h>
Expand All @@ -11,8 +12,11 @@ int main(void)
{
uart_t uart = mocha_system_uart();
timer_t timer = mocha_system_timer();
spi_device_t spi_device = mocha_system_spi_device();
uart_init(uart);
timer_init(timer);
spi_device_init(spi_device);

timer_set_prescale_step(timer, (SYSCLK_FREQ / 1000000) - 1, 1); // 1 tick/us
timer_enable(timer);

Expand All @@ -29,11 +33,64 @@ int main(void)
uart_puts(uart, "Safe to exit simulator.\xd8\xaf\xfb\xa0\xc7\xe1\xa9\xd7");
uart_puts(uart, "This should not be printed in simulation.\r\n");

// Print every 1s
// Poll and process SPI command
Comment thread
engdoreis marked this conversation as resolved.
Resolved
spi_device_cmd_t cmd;
while (1) {
timer_busy_sleep(timer, 1000000);
cmd = spi_device_cmd_get(spi_device);
if (cmd.status != 0) {
uart_puts(uart, "SPI payload overflow\n");
spi_device_flash_status_set(spi_device, 0);
continue;
}

switch (cmd.opcode) {
case SPI_DEVICE_OPCODE_CHIP_ERASE:
uart_puts(uart, "SPI CHIP ERASE");
break;
case SPI_DEVICE_OPCODE_SECTOR_ERASE:
uart_puts(uart, "SPI SECTOR ERASE");
break;
case SPI_DEVICE_OPCODE_PAGE_PROGRAM:
uart_puts(uart, "SPI PAGE PROGRAM");
break;
case SPI_DEVICE_OPCODE_RESET:
uart_puts(uart, "SPI RESET");
break;
default:
uart_puts(uart, "SPI ??");
break;
}

if (cmd.address != UINT32_MAX) {
uart_puts(uart, " addr: 0x");
uart_put_uint32_hex(uart, cmd.address);
}

if (cmd.payload_byte_count > 0) {
uart_puts(uart, " payload_bytes: 0x");
uart_put_uint32_hex(uart, (uint32_t)cmd.payload_byte_count);

uint32_t payload_word_count = ((uint32_t)cmd.payload_byte_count) / sizeof(uint32_t);
if ((cmd.payload_byte_count % sizeof(uint32_t)) != 0) {
++payload_word_count;
}

uart_puts(uart, " payload:");

uint32_t word;
for (uint32_t i = 0; i < payload_word_count; ++i) {
word = spi_device_flash_payload_buffer_read(spi_device, i * sizeof(uint32_t));
spi_device_flash_read_buffer_write(spi_device, cmd.address + i * sizeof(uint32_t),
word);

uart_puts(uart, " 0x");
uart_put_uint32_hex(uart, word);
}
}

uart_puts(uart, "\n");

uart_puts(uart, "timer 1s\n");
spi_device_flash_status_set(spi_device, 0);
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion sw/device/lib/hal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

set(SRCS mocha.c plic.c timer.c uart.c)
set(SRCS mocha.c plic.c spi_device.c timer.c uart.c)

mocha_add_library(NAME hal LIBARIES SOURCES ${SRCS})
10 changes: 10 additions & 0 deletions sw/device/lib/hal/mocha.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#endif /* defined(__riscv_zcherihybrid) */

static const uintptr_t uart_base = 0x41000000ul;
static const uintptr_t spi_device_base = 0x43000000ul;
static const uintptr_t timer_base = 0x44000000ul;
static const uintptr_t plic_base = 0x48000000ul;

Expand Down Expand Up @@ -61,3 +62,12 @@ plic_t mocha_system_plic(void)
return (plic_t)plic_base;
#endif /* defined(__riscv_zcherihybrid) */
}

spi_device_t mocha_system_spi_device(void)
{
#if defined(__riscv_zcherihybrid)
return (spi_device_t)create_mmio_capability(spi_device_base, 0x1FC0u);
#else /* !defined(__riscv_zcherihybrid) */
return (spi_device_t)spi_device_base;
#endif /* defined(__riscv_zcherihybrid) */
}
2 changes: 2 additions & 0 deletions sw/device/lib/hal/mocha.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include "hal/plic.h"
#include "hal/spi_device.h"
#include "hal/timer.h"
#include "hal/uart.h"

Expand All @@ -16,3 +17,4 @@
uart_t mocha_system_uart(void);
timer_t mocha_system_timer(void);
plic_t mocha_system_plic(void);
spi_device_t mocha_system_spi_device(void);
18 changes: 18 additions & 0 deletions sw/device/lib/hal/reg_field.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright lowRISC contributors (COSMIC project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

// Register field macros.

#pragma once

#include <stdint.h>

static inline uint32_t reg32_field(uint8_t upper, uint8_t lower, uint32_t value)
{
if (32 <= upper || upper < lower) {
return 0;
}
const uint32_t mask = (1 << (upper - lower + 1)) - 1;
return (mask & value) << lower;
}
Loading