blob: 0300213b3ba03337f52c7f40fb598df4637e3ec0 [file] [log] [blame]
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hw/top_matcha/sw/autogen/top_matcha.h"
#include "sw/device/examples/demo_hps_live/hps_demo_smc_fpga_nexus_bin_c.h" // Generated.
#include "sw/device/lib/arch/device.h"
#include "sw/device/lib/dif/dif_gpio.h"
#include "sw/device/lib/dif/dif_pinmux.h"
#include "sw/device/lib/dif/dif_rv_plic.h"
#include "sw/device/lib/dif/dif_smc_ctrl.h"
#include "sw/device/lib/dif/dif_spi_device.h"
#include "sw/device/lib/dif/dif_tlul_mailbox.h"
#include "sw/device/lib/dif/dif_uart.h"
#include "sw/device/lib/runtime/irq.h"
#include "sw/device/lib/runtime/print.h"
#include "sw/device/lib/spi_to_host/spi_to_host.h"
#include "sw/device/lib/testing/test_framework/check.h"
#include "sw/device/lib/testing/test_framework/ottf_test_config.h"
/*
* Sample program to run some code on all cores in the system.
* SC - Orchestrate the other cores, print status to UART.
* SMC - Start ISP. Receive interrupts from ISP and start Kelvin
* ISP - Continuously send frame-end interrupts into SMC.
* Kelvin - Run ML model to compute scores.
*/
OTTF_DEFINE_TEST_CONFIG();
static dif_smc_ctrl_t smc_ctrl;
static dif_uart_t uart;
static dif_rv_plic_t plic_sec;
static dif_gpio_t gpio;
static dif_tlul_mailbox_t tlul_mailbox;
static dif_pinmux_t pinmux;
static dif_spi_device_handle_t spi_device;
static spi_to_host_t spi_to_host;
#define kSwStrap0Pin (22)
void ottf_external_isr(void) {
uint32_t rx;
dif_rv_plic_irq_id_t plic_irq_id;
CHECK_DIF_OK(dif_rv_plic_irq_claim(&plic_sec, kTopMatchaPlicTargetIbex0,
&plic_irq_id));
top_matcha_plic_peripheral_t peripheral_id =
top_matcha_plic_interrupt_for_peripheral[plic_irq_id];
switch (peripheral_id) {
case kTopMatchaPlicPeripheralTlulMailboxSec: {
CHECK_DIF_OK(dif_tlul_mailbox_irq_acknowledge(&tlul_mailbox,
kDifTlulMailboxIrqRtirq));
CHECK_DIF_OK(dif_tlul_mailbox_read_message(&tlul_mailbox, &rx));
uint32_t pin = rx >> 16;
uint32_t value = rx & 0xFFFF;
CHECK_DIF_OK(dif_gpio_write(&gpio, pin, value));
CHECK_DIF_OK(dif_tlul_mailbox_send_message(&tlul_mailbox, &rx));
break;
}
case kTopMatchaPlicPeripheralSpiDevice: {
CHECK_DIF_OK(spi_to_host_irq_handler(&spi_to_host, plic_irq_id));
break;
}
default:
LOG_FATAL("Unhandled interrupt");
break;
}
CHECK_DIF_OK(dif_rv_plic_irq_complete(&plic_sec, kTopMatchaPlicTargetIbex0,
plic_irq_id));
}
void _ottf_main(void) {
CHECK_DIF_OK(
dif_uart_init(mmio_region_from_addr(TOP_MATCHA_UART0_BASE_ADDR), &uart));
CHECK_DIF_OK(
dif_uart_configure(&uart, (dif_uart_config_t){
.baudrate = kUartBaudrate,
.clk_freq_hz = kClockFreqPeripheralHz,
.parity_enable = kDifToggleDisabled,
.parity = kDifUartParityEven,
.tx_enable = kDifToggleEnabled,
.rx_enable = kDifToggleDisabled,
}));
base_uart_stdout(&uart);
LOG_INFO("[SC] Demo app start from SC!");
// Add GPIO mux / init
CHECK_DIF_OK(dif_pinmux_init(
mmio_region_from_addr(TOP_MATCHA_PINMUX_AON_BASE_ADDR), &pinmux));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIod0,
kTopMatchaPinmuxOutselGpioGpio16));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIod1,
kTopMatchaPinmuxOutselGpioGpio17));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIoc0,
kTopMatchaPinmuxOutselGpioGpio22));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux,
kTopMatchaPinmuxPeripheralInGpioGpio22,
kTopMatchaPinmuxInselConstantZero));
// Mux I2C
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux,
kTopMatchaPinmuxPeripheralInCamI2cSda,
kTopMatchaPinmuxInselIoa5));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux,
kTopMatchaPinmuxMioOutIoa5, kTopMatchaPinmuxOutselCamI2cSda));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIoa4, kTopMatchaPinmuxOutselCamI2cScl));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInCamI2cScl, kTopMatchaPinmuxInselIoa4));
// Mux ISP wrapper
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInIspWrapperSData0, kTopMatchaPinmuxInselIob0));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInIspWrapperSData1, kTopMatchaPinmuxInselIob1));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInIspWrapperSData2, kTopMatchaPinmuxInselIob2));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInIspWrapperSData3, kTopMatchaPinmuxInselIob3));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInIspWrapperSData4, kTopMatchaPinmuxInselIob4));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInIspWrapperSData5, kTopMatchaPinmuxInselIob5));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInIspWrapperSData6, kTopMatchaPinmuxInselIob6));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInIspWrapperSData7, kTopMatchaPinmuxInselIob7));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInIspWrapperSVsync, kTopMatchaPinmuxInselIoa2));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInIspWrapperSHsync, kTopMatchaPinmuxInselIoa3));
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInIspWrapperSPclk, kTopMatchaPinmuxInselIob8));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIoa2, kTopMatchaPinmuxOutselConstantHighZ));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIoa3, kTopMatchaPinmuxOutselConstantHighZ));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIob0, kTopMatchaPinmuxOutselConstantHighZ));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIob1, kTopMatchaPinmuxOutselConstantHighZ));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIob2, kTopMatchaPinmuxOutselConstantHighZ));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIob3, kTopMatchaPinmuxOutselConstantHighZ));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIob4, kTopMatchaPinmuxOutselConstantHighZ));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIob5, kTopMatchaPinmuxOutselConstantHighZ));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIob6, kTopMatchaPinmuxOutselConstantHighZ));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIob7, kTopMatchaPinmuxOutselConstantHighZ));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIob8, kTopMatchaPinmuxOutselConstantHighZ));
// Mux cam CTRL / TRIG
CHECK_DIF_OK(dif_pinmux_input_select(&pinmux, kTopMatchaPinmuxPeripheralInCamCtrlCamInt, kTopMatchaPinmuxInselIoa6));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIoa7, kTopMatchaPinmuxOutselCamCtrlCamTrig));
// Mux SPI_HOST2 (clk, mosi, cs)
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIor12, kTopMatchaPinmuxOutselSpiHost2Sck));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIob10, kTopMatchaPinmuxOutselSpiHost2Sd0));
CHECK_DIF_OK(dif_pinmux_output_select(&pinmux, kTopMatchaPinmuxMioOutIor13, kTopMatchaPinmuxOutselSpiHost2Csb));
CHECK_DIF_OK(
dif_gpio_init(mmio_region_from_addr(TOP_MATCHA_GPIO_BASE_ADDR), &gpio));
CHECK_DIF_OK(dif_gpio_output_set_enabled(&gpio, 16, kDifToggleEnabled));
CHECK_DIF_OK(dif_gpio_output_set_enabled(&gpio, 17, kDifToggleEnabled));
CHECK_DIF_OK(dif_spi_device_init_handle(
mmio_region_from_addr(TOP_MATCHA_SPI_DEVICE_BASE_ADDR), &spi_device));
CHECK_DIF_OK(
spi_to_host_init(&spi_to_host, &spi_device, &gpio, kSwStrap0Pin));
CHECK_DIF_OK(dif_tlul_mailbox_init(
mmio_region_from_addr(TOP_MATCHA_TLUL_MAILBOX_SEC_BASE_ADDR),
&tlul_mailbox));
CHECK_DIF_OK(dif_tlul_mailbox_irq_set_enabled(
&tlul_mailbox, kDifTlulMailboxIrqRtirq, kDifToggleEnabled));
CHECK_DIF_OK(dif_tlul_mailbox_irq_set_enabled(
&tlul_mailbox, kDifTlulMailboxIrqWtirq, kDifToggleEnabled));
CHECK_DIF_OK(dif_tlul_mailbox_irq_set_enabled(
&tlul_mailbox, kDifTlulMailboxIrqEirq, kDifToggleEnabled));
CHECK_DIF_OK(dif_rv_plic_init(
mmio_region_from_addr(TOP_MATCHA_RV_PLIC_BASE_ADDR), &plic_sec));
CHECK_DIF_OK(
spi_to_host_irq_init(&spi_to_host, &plic_sec, kTopMatchaPlicTargetIbex0));
CHECK_DIF_OK(dif_rv_plic_irq_set_enabled(
&plic_sec, kTopMatchaPlicIrqIdTlulMailboxSecRtirq,
kTopMatchaPlicTargetIbex0, kDifToggleEnabled));
CHECK_DIF_OK(dif_rv_plic_irq_set_priority(
&plic_sec, kTopMatchaPlicIrqIdTlulMailboxSecRtirq, 1));
irq_global_ctrl(true);
irq_external_ctrl(true);
// Copy embedded binary to SMC RAM.
memcpy((void *)TOP_MATCHA_RAM_SMC_BASE_ADDR, smc_bin, smc_bin_len);
// Enable SMC.
CHECK_DIF_OK(dif_smc_ctrl_init(
mmio_region_from_addr(TOP_MATCHA_SMC_CTRL_BASE_ADDR), &smc_ctrl));
CHECK_DIF_OK(dif_smc_ctrl_set_en(&smc_ctrl));
LOG_INFO("[SC] Finished executing.");
LOG_INFO("[SC] Check SMC messages!");
while (true) {
asm volatile("wfi");
}
}