/*
 * 03_flash_information.c
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 *
 * Copyright (c) 2026 Georgy Moshkin <https://georgymoshkin.com>
 * All rights reserved.
 *
 */

#include "examples.h"

#ifdef EXAMPLE_03_FLASH_INFORMATION

#ifndef SRC_03_FLASH_INFORMATION_C_
#define SRC_03_FLASH_INFORMATION_C_

#define UART_BUFFER_SIZE 64
__IO uint8_t uartBuffer[UART_BUFFER_SIZE];	// DMA buffer for serial port RX
uint8_t cmdByte; 							// Temporary TX/RX byte variable

void app_boot()
{
	// implement boot logic here
}

void app_init(UART_HandleTypeDef *huartPtr)
{
	// Initialize universal serial port library (uart.h, uart.c)
	COM_Init(0,					// Port number
			huartPtr,			// HAL port handle
			uartBuffer,			// RX buffer array
			UART_BUFFER_SIZE);	// RX buffer size
}


void app_loop()
{
	// PC13 LED Blink
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, (HAL_GetTick() & 512) ? GPIO_PIN_SET : GPIO_PIN_RESET);

	// Check if at least one byte is present in RX buffer
	if (COM_ReadFast(&cmdByte, 1)) switch (cmdByte)
	{

	case 0x01: // Command 0x01 - Get flash parameters
	{

		flash_calculate_sizes();

		// Send response to serial port
		cmdByte=0xA1;
		COM_Write(&cmdByte, 1);

		COM_Write(&flash_size_total, 4);

		COM_Write(&flash_address_bootloader,4);
		COM_Write(&flash_address_settings, 4);
		COM_Write(&flash_address_application, 4);

		COM_Write(&flash_size_bootloader, 4);
		COM_Write(&flash_size_settings, 4);
		COM_Write(&flash_size_application, 4);
		break;
	}

	}
}


#endif /* SRC_03_INFORMATION_C_ */
#endif /* EXAMPLE_03_INFORMATION */
