// Copyright lowRISC contributors.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#include "sw/device/examples/demos.h"

#include <stdbool.h>

#include "sw/device/lib/arch/device.h"
#include "sw/device/lib/dif/dif_gpio.h"
#include "sw/device/lib/dif/dif_spi_device.h"
#include "sw/device/lib/dif/dif_uart.h"
#include "sw/device/lib/runtime/hart.h"
#include "sw/device/lib/runtime/log.h"
#include "sw/device/lib/testing/test_framework/check.h"

void demo_gpio_startup(dif_gpio_t *gpio) {
  LOG_INFO("Watch the LEDs!");

  // Give a LED pattern as startup indicator for 5 seconds.
  CHECK_DIF_OK(dif_gpio_write_all(gpio, 0x00ff));
  for (int i = 0; i < 32; ++i) {
    busy_spin_micros(5 * 1000);  // 5 ms
    CHECK_DIF_OK(dif_gpio_write(gpio, 8 + (i % 8), (i / 8) % 2));
  }
  CHECK_DIF_OK(dif_gpio_write_all(gpio, 0x0000));  // All LEDs off.
}

/**
 * Mask for "valid" GPIO bits. The nibble at [11:8] represents switch inputs,
 * while the bit at [22] represents the FTDI bit (SW strap 0).
 */
static const uint32_t kGpioMask = 0x400f00;

/**
 * Mask for the FTDI bit (SW strap 0) among the GPIO bits.
 */
static const uint32_t kFtdiMask = 0x400000;

uint32_t demo_gpio_to_log_echo(dif_gpio_t *gpio, uint32_t prev_gpio_state) {
  uint32_t gpio_state;
  CHECK_DIF_OK(dif_gpio_read_all(gpio, &gpio_state));
  gpio_state &= kGpioMask;

  uint32_t state_delta = prev_gpio_state ^ gpio_state;
  for (int bit_idx = 8; bit_idx <= 11; ++bit_idx) {
    bool changed = ((state_delta >> bit_idx) & 0x1) != 0;
    bool is_currently_set = ((gpio_state >> bit_idx) & 0x1) != 0;
    if (changed) {
      LOG_INFO("GPIO switch #%d changed to %d", bit_idx, is_currently_set);
    }
  }

  if ((state_delta & kFtdiMask) != 0) {
    if ((gpio_state & kFtdiMask) != 0) {
      LOG_INFO("FTDI control changed. Enable JTAG.");
    } else {
      LOG_INFO("FTDI control changed. Enable JTAG.");
    }
  }

  return gpio_state;
}

void demo_spi_to_log_echo(dif_spi_device_handle_t *spi) {
  uint32_t spi_buf[8];
  size_t spi_len;
  CHECK_DIF_OK(dif_spi_device_recv(spi, spi_buf, sizeof(spi_buf), &spi_len));
  if (spi_len > 0) {
    uint32_t echo_word = spi_buf[0] ^ 0x01010101;
    CHECK_DIF_OK(dif_spi_device_send(spi, &echo_word, sizeof(uint32_t),
                                     /*bytes_sent=*/NULL));
    LOG_INFO("SPI: %!s", spi_len, spi_buf);
  }
}

void demo_uart_to_uart_and_gpio_echo(dif_uart_t *uart, dif_gpio_t *gpio) {
  while (true) {
    size_t chars_available;
    if (dif_uart_rx_bytes_available(uart, &chars_available) != kDifOk ||
        chars_available == 0) {
      break;
    }

    uint8_t rcv_char;
    CHECK_DIF_OK(dif_uart_bytes_receive(uart, 1, &rcv_char, NULL));
    CHECK_DIF_OK(dif_uart_byte_send_polled(uart, rcv_char));
    CHECK_DIF_OK(dif_gpio_write_all(gpio, rcv_char));
  }
}
