blob: e1d9bef47ce844acbe89442dfde1e80c62b5faad [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 <Arduino.h>
#include <cstdint>
#include <SPI.h>
#include "pw_log/log.h"
#include "pw_string/format.h"
// STM32 Port & pin // PCB NET Name
const int ICE40Reset = PC13; // ICE_RST_N
const int ICE40Done = PC8; // ICE_CDONE
const int FlashCS = PD2; // ICE_SPI_SS
const int FlashHold = PC11; // FLASH_HOLD
const int FlashWP = PC12; // FLASH_WP
const int FlashMOSI = PB5; // ICE_SPI_MOSI
const int FlashMISO = PB4; // ICE_SPI_MOSI
const int FlashCLK = PB3; // ICE_SPI_SCK
const int StatusLed = PB13; // STATUS
int main() {
Serial.begin(115200);
// Put FPGA in reset.
PW_LOG_INFO("Hold FPGA in reset");
pinMode(ICE40Reset, OUTPUT);
digitalWrite(ICE40Reset, LOW);
delay(10);
// Debug status LED
pinMode(StatusLed, OUTPUT);
digitalWrite(StatusLed, LOW);
// SPI pin definitions
pinMode(FlashCS, OUTPUT);
digitalWrite(FlashCS, HIGH);
SPI.setMISO(FlashMISO);
SPI.setMOSI(FlashMOSI);
SPI.setSCLK(FlashCLK);
SPI.begin();
char buffer[32];
uint16_t update_count = 0;
uint8_t manufacturer_id[4];
while (true) {
// Read the SPI Flash JEDEC ID.
manufacturer_id[0] = 0x9f;
manufacturer_id[1] = 0;
manufacturer_id[2] = 0;
manufacturer_id[3] = 0;
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(FlashCS, LOW);
SPI.transfer(manufacturer_id, 4);
digitalWrite(FlashCS, HIGH);
SPI.endTransaction();
pw::string::Format(buffer, "SPI Flash JEDEC ID: %x %x %x",
manufacturer_id[1], manufacturer_id[2],
manufacturer_id[3]);
PW_LOG_INFO("%s", buffer);
delay(1000);
update_count = (update_count + 1) % UINT16_MAX;
// Toggle status LED each loop.
if (update_count % 2 == 0) {
digitalWrite(StatusLed, HIGH);
} else {
digitalWrite(StatusLed, LOW);
}
}
return 0;
}