blob: 91efff67ec91be7845f8102f1efc4efe68bfdd6f [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/lib/arch/device.h"
#include "sw/device/lib/camera_hm01b0.h"
#include "sw/device/lib/dif/dif_cam_ctrl.h"
#include "sw/device/lib/dif/dif_isp_wrapper.h"
#include "sw/device/lib/dif/dif_rv_plic.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/testing/test_framework/check.h"
#include "sw/device/lib/testing/test_framework/ottf_test_config.h"
#include "sw/device/lib/testing/test_framework/test_util.h"
OTTF_DEFINE_TEST_CONFIG();
static dif_cam_ctrl_t cam_ctrl;
static dif_uart_t smc_uart;
static dif_isp_wrapper_t isp_wrapper;
static dif_rv_plic_t plic_smc;
void ottf_external_isr(void) {
dif_rv_plic_irq_id_t plic_irq_id;
CHECK_DIF_OK(dif_rv_plic_irq_claim(&plic_smc, kTopMatchaPlicTargetIbex0Smc,
&plic_irq_id));
top_matcha_plic_peripheral_smc_t peripheral_id =
top_matcha_plic_interrupt_for_peripheral_smc[plic_irq_id];
switch (peripheral_id) {
case kTopMatchaPlicPeripheralCamI2c: {
CHECK_DIF_OK(camera_hm01b0_irq_handler(plic_irq_id));
break;
}
case kTopMatchaPlicPeripheralIspWrapper: {
uint32_t mi_mis, isp_mis;
CHECK_DIF_OK(dif_isp_wrapper_read_mi_mis(&isp_wrapper, &mi_mis));
CHECK_DIF_OK(dif_isp_wrapper_read_isp_mis(&isp_wrapper, &isp_mis));
CHECK_DIF_OK(dif_isp_wrapper_write_isp_icr(&isp_wrapper, isp_mis));
CHECK_DIF_OK(dif_isp_wrapper_write_mi_icr(&isp_wrapper, mi_mis));
CHECK_DIF_OK(dif_isp_wrapper_irq_acknowledge_all(&isp_wrapper));
break;
}
default:
LOG_FATAL("Unexpected peripheral_id!");
}
CHECK_DIF_OK(dif_rv_plic_irq_complete(&plic_smc, kTopMatchaPlicTargetIbex0Smc,
plic_irq_id));
}
void _ottf_main(void) {
// Config_Init
init_uart(TOP_MATCHA_SMC_UART_BASE_ADDR, &smc_uart);
LOG_INFO("spi_to_host_smc");
// Initialize cam_ctrl and ISP.
CHECK_DIF_OK(dif_cam_ctrl_init(
mmio_region_from_addr(TOP_MATCHA_CAM_CTRL_BASE_ADDR), &cam_ctrl));
CHECK_DIF_OK(dif_isp_wrapper_init(
mmio_region_from_addr(TOP_MATCHA_ISP_WRAPPER_BASE_ADDR), &isp_wrapper));
CHECK_DIF_OK(dif_rv_plic_init(
mmio_region_from_addr(TOP_MATCHA_RV_PLIC_SMC_BASE_ADDR), &plic_smc));
// Configure interrupts for Camera, ISP and mailbox.
CHECK_DIF_OK(camera_hm01b0_irq_init(&plic_smc, kTopMatchaPlicTargetIbex0Smc));
CHECK_DIF_OK(dif_rv_plic_irq_set_enabled(
&plic_smc, kTopMatchaPlicIrqIdIspWrapperIsp, kTopMatchaPlicTargetIbex0Smc,
kDifToggleEnabled));
CHECK_DIF_OK(dif_rv_plic_irq_set_enabled(
&plic_smc, kTopMatchaPlicIrqIdIspWrapperMi, kTopMatchaPlicTargetIbex0Smc,
kDifToggleEnabled));
CHECK_DIF_OK(dif_rv_plic_irq_set_priority(
&plic_smc, kTopMatchaPlicIrqIdIspWrapperIsp, kDifRvPlicMaxPriority));
CHECK_DIF_OK(dif_rv_plic_irq_set_priority(
&plic_smc, kTopMatchaPlicIrqIdIspWrapperMi, kDifRvPlicMaxPriority));
irq_global_ctrl(true);
irq_external_ctrl(true);
// Configure and enable camera.
CHECK_DIF_OK(camera_hm01b0_init());
CHECK_DIF_OK(camera_hm01b0_set_default_registers(
kCameraHm01b0Resolution320x240, kCameraHm01b0FrameRate1Fps));
CHECK_DIF_OK(dif_isp_wrapper_set_raw_320x240_byp_320x240_en(&isp_wrapper));
CHECK_DIF_OK(dif_cam_ctrl_set_en(&cam_ctrl));
while (true) {
asm volatile("wfi");
}
}