blob: 1699362638f55f9d3baa59bcc7f335044bcc708b [file] [log] [blame]
// Copyright 2023 The Pigweed Authors
//
// 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
//
// https://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 <bitset>
#include <cstdint>
#include <Arduino.h>
#include <SPI.h>
#define PW_LOG_LEVEL PW_LOG_LEVEL_INFO
#define PW_LOG_MODULE_NAME "main"
#include "pw_log/log.h"
#include "pw_result/result.h"
#include "pw_string/format.h"
#include "gonk/adc.h"
#include "gonk/fpga_control.h"
#include "gonk/pin_config.h"
#include "gonk/spi_flash.h"
using gonk::adc::Adc;
using gonk::fpga_control::FpgaControl;
using gonk::pin_config::FlashCS;
using gonk::pin_config::FpgaDspiCS;
using gonk::pin_config::FpgaIoMode;
using gonk::pin_config::FpgaIoReset;
using gonk::pin_config::FpgaIoValid;
using gonk::pin_config::ICE40Done;
using gonk::pin_config::PinConfig;
using gonk::pin_config::StatusLed;
using gonk::spi_flash::SpiFlash;
namespace {
PinConfig pin_config = PinConfig();
FpgaControl fpga_control = FpgaControl(&pin_config);
SpiFlash spi_flash =
SpiFlash(FlashCS, /*baudrate=*/1000000, pin_config.flash_spi);
Adc fpga_adc = Adc(Serial, pin_config.fpga_spi, /*fpga_spi_baudrate=*/7500000,
FpgaDspiCS, FpgaIoMode, FpgaIoReset, FpgaIoValid);
void (*current_task)();
} // namespace
void IdleTask();
void FpgaConfigTask();
void ADCIdleTask();
void ADCReadTask();
void IdleTask() {
static uint32_t last_update = millis();
static uint32_t this_update = millis();
static uint16_t update_count = 0;
this_update = millis();
// Check for serial input.
if (Serial.available()) {
int data_in = Serial.read();
// Press enter to restart the FPGA config.
if (data_in == '\n') {
PW_LOG_INFO("Restarting FPGA Config.");
current_task = &FpgaConfigTask;
}
}
// Output an idle state heartbeat message each second.
if (this_update > last_update + 1000) {
PW_LOG_INFO("Idle update: %d", update_count);
last_update = this_update;
update_count = (update_count + 1) % UINT16_MAX;
// Toggle status LED each loop.
if (update_count % 2 == 0) {
digitalWrite(StatusLed, HIGH);
} else {
digitalWrite(StatusLed, LOW);
}
}
}
void ADCIdleTask() {
static uint32_t last_update = millis();
static uint32_t this_update = millis();
static uint16_t update_count = 0;
this_update = millis();
if (Serial.available()) {
int data_in = Serial.read();
if (data_in == '\n') {
PW_LOG_INFO("Starting ADC Continuous Reads.");
fpga_adc.SetContinuousReadMode();
current_task = &ADCReadTask;
}
}
// Output an idle state heartbeat message each second.
if (this_update > last_update + 1000) {
PW_LOG_INFO("ADC Idle");
last_update = this_update;
update_count = (update_count + 1) % UINT16_MAX;
// Toggle status LED each loop.
if (update_count % 2 == 0) {
digitalWrite(StatusLed, HIGH);
} else {
digitalWrite(StatusLed, LOW);
}
}
}
void ADCReadTask() {
// Check for serial input.
if (Serial.available()) {
int data_in = Serial.read();
// Press enter to stop.
if (data_in == '\n') {
PW_LOG_INFO("Stopping ADC Continuous Reads.");
fpga_adc.SetReadWriteMode();
current_task = &ADCIdleTask;
return;
}
}
pw::Status update_result = fpga_adc.UpdateContinuousMeasurements();
if (!update_result.ok()) {
PW_LOG_ERROR("UpdateContinuousMeasurements() failed");
} else {
pw::Status write_result = fpga_adc.WriteMeasurementPacket();
if (!write_result.ok()) {
PW_LOG_ERROR("WriteMeasurementPacket failed");
}
}
}
void CheckSpiFlash() {
PW_LOG_INFO("Checking SPI Flash");
spi_flash.StartFlashSpi();
pw::Result<SpiFlash::DeviceId> result = spi_flash.GetDeviceIds();
if (result.ok()) {
PW_LOG_INFO("SPI Flash JEDEC ID: %x %x %x", result.value().manufacturer_id,
result.value().family_code, result.value().product_version);
}
}
void FpgaConfigTask() {
auto result = fpga_control.StartConfig();
if (result.ok()) {
// FPGA has been configure successfully.
current_task = &ADCReadTask;
// Check if SPI flash is readable.
CheckSpiFlash();
// Init ADCs
fpga_adc.InitAdcs();
fpga_adc.CheckAllAdcs();
// Select the first five ADC channels.
fpga_adc.SelectContinuousReadAdcs(0b11111);
fpga_adc.SetContinuousReadMode();
} else {
PW_LOG_INFO("Restarting FPGA Config.");
current_task = &FpgaConfigTask;
}
}
int main() {
pin_config.Init();
pin_config.InitFpgaPins();
delay(500);
current_task = &FpgaConfigTask;
// Start the task loop.
while (true) {
current_task();
}
PW_UNREACHABLE;
return 0;
}