/*
 * 05_fast_blink.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://hexflashlab.com>
 * All rights reserved.
 *
 */

#include "examples.h"

#ifdef EXAMPLE_05_FAST_BLINK

#ifndef SRC_05_FAST_BLINK_C_
#define SRC_05_FAST_BLINK_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()
{

}

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 fast
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, (HAL_GetTick() & 0b1000000) ? GPIO_PIN_SET : GPIO_PIN_RESET);

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

	// Command 0xF0: Run bootloader
	case 0xF0:
	{
		flash_region_run(REGION_BOOTLOADER);
		break;
	}

	// Command 0xF1: Run application
	case 0xF1:
	{
		flash_region_run(REGION_APPLICATION);
		break;
	}

	}
}


#endif /* SRC_05_FAST_BLINK_C_ */
#endif /* EXAMPLE_05_FAST_BLINK */
