#include // Standard Arduino header #include #include // Define the serial port using PF0 (TX) and PF1 (RX) HardwareSerial Serial2(PF0, PF1); //"High Power Output" #define HPWR0 PA5 #define HPWR1 PA6 //Presure Sensor #define PSENSE PA15 //Touch Sensor #define TSENSE PA2 //ADC B+ #define BATV PA0 //ADC 5V #define VIN PA1 //Pin definitions for the Flash #define FLASH_PIN_CS PA3 #define FLASH_PIN_MISO PB6 #define FLASH_PIN_MOSI PB7 #define FLASH_PIN_SCK PB8 // Pin definitions for the LCD #define LCD_BL_PWR PA9 #define LCD_VDD_PWR PF_3 #define LCD_PIN_RESET PB4 // PB4: LCD Reset #define LCD_PIN_CLOCK PB3 // PB3: LCD Clock #define LCD_PIN_DATA PB5 // PB5: LCD Data #define LCD_PIN_SELECT PA8 // PA8: LCD Select #define LCD_PIN_CMD_DATA PA12 // PA12: LCD Command or Data #define LCD_PIN_BACKLIGHT PA11 // PA11: LCD Backlight #define LCD_WIDTH 128 #define LCD_HEIGHT 160 // Some RGB565 colors #define LCD_RGB565_BLACK 0x0000 #define LCD_RGB565_WHITE 0xFFFF #define LCD_RGB565_RED 0xF800 #define LCD_RGB565_GREEN 0x07E0 #define LCD_RGB565_BLUE 0x001F void setup() { // Enable the LSI (Low-Speed Internal) oscillator used by the IWDG RCC->CSR |= RCC_CSR_LSION; // Enable LSI oscillator // Wait for the LSI to become ready while (!(RCC->CSR & RCC_CSR_LSIRDY)) { // Wait here until LSI is stable } // Unlock the IWDG registers IWDG->KR = 0x5555; // Writing 0x5555 unlocks the control registers of IWDG // Set the prescaler (divides the LSI clock to determine the watchdog timeout) IWDG->PR = 0x03; // Set prescaler to divide by 64 (prescaler values range from 0-7) // Set the reload value (this sets the timeout period) IWDG->RLR = 0x0FFF; // Maximum reload value for the longest timeout // Start the watchdog timer IWDG->KR = 0xCCCC; // Writing 0xCCCC starts the IWDG // Initialize serial communication for debugging purposes Serial2.begin(115200); Serial2.println("IWDG Initialized."); //Set pin Modes pinMode(HPWR0, OUTPUT); pinMode(HPWR1, OUTPUT); pinMode(LCD_PIN_RESET, OUTPUT); pinMode(LCD_PIN_CLOCK, OUTPUT); pinMode(LCD_PIN_DATA, OUTPUT); pinMode(LCD_PIN_SELECT, OUTPUT); pinMode(LCD_PIN_CMD_DATA, OUTPUT); pinMode(LCD_PIN_BACKLIGHT, OUTPUT); pinMode(LCD_VDD_PWR, OUTPUT); pinMode(LCD_BL_PWR, OUTPUT); digitalWrite(LCD_VDD_PWR, LOW); digitalWrite(LCD_BL_PWR, HIGH); digitalWrite(LCD_PIN_BACKLIGHT, HIGH); // Configure Flash CS pin pinMode(FLASH_PIN_CS, OUTPUT); digitalWrite(FLASH_PIN_CS, HIGH); // Deselect flash // Configure LCD CS and Control Pins pinMode(LCD_PIN_SELECT, OUTPUT); pinMode(LCD_PIN_CMD_DATA, OUTPUT); digitalWrite(LCD_PIN_SELECT, HIGH); // Deselect LCD // Configure SPI2 (for Flash) SPI.setSCLK(FLASH_PIN_SCK); SPI.setMISO(FLASH_PIN_MISO); SPI.setMOSI(FLASH_PIN_MOSI); SPI.begin(); SPI.setBitOrder(MSBFIRST); SPI.setDataMode(SPI_MODE0); SPI.setClockDivider(SPI_CLOCK_DIV4); // Adjust as needed // Read Flash ID digitalWrite(FLASH_PIN_CS, LOW); SPI.transfer(0x9F); // Send RDID command uint8_t manufacturerID = SPI.transfer(0x00); uint8_t memoryType = SPI.transfer(0x00); uint8_t capacity = SPI.transfer(0x00); digitalWrite(FLASH_PIN_CS, HIGH); // Print the IDs Serial2.print("Manufacturer ID: 0x"); Serial2.println(manufacturerID, HEX); Serial2.print("Memory Type: 0x"); Serial2.println(memoryType, HEX); Serial2.print("Capacity: 0x"); Serial2.println(capacity, HEX); float voltage_5v = analogRead(PA1)*(33000+33000)/33000*2/1023; Serial2.println(voltage_5v); float voltage_battery = analogRead(PA0)*(10000+9900)/9900*2/1023; Serial2.println(voltage_battery); } void loop() { // The main loop where we periodically refresh the watchdog refreshIWDG(); // Do some work here and simulate task delays // Add some delay to allow tasks to run and avoid over-refreshing the watchdog delay(500); // Example delay (this can be adjusted based on your requirements) } // Function to refresh the Independent Watchdog (IWDG) void refreshIWDG() { // Write 0xAAAA to the KR (Key Register) to refresh the IWDG counter IWDG->KR = 0xAAAA; // Writing 0xAAAA resets the watchdog timer counter Serial2.println("IWDG Refreshed."); }