206 lines
7.8 KiB
Plaintext
206 lines
7.8 KiB
Plaintext
/****************************************************************************************
|
|
| Description: demo program application source file
|
|
| File Name: main.c
|
|
|
|
|
|----------------------------------------------------------------------------------------
|
|
| C O P Y R I G H T
|
|
|----------------------------------------------------------------------------------------
|
|
| Copyright (c) 2011 by Feaser http://www.feaser.com All rights reserved
|
|
|
|
|
|----------------------------------------------------------------------------------------
|
|
| L I C E N S E
|
|
|----------------------------------------------------------------------------------------
|
|
| This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
|
| modify it under the terms of the GNU General Public License as published by the Free
|
|
| Software Foundation, either version 3 of the License, or (at your option) any later
|
|
| version.
|
|
|
|
|
| OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
| without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
| PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
| You should have received a copy of the GNU General Public License along with OpenBLT.
|
|
| If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
| A special exception to the GPL is included to allow you to distribute a combined work
|
|
| that includes OpenBLT without being obliged to provide the source code for any
|
|
| proprietary components. The exception text is included at the bottom of the license
|
|
| file <license.html>.
|
|
|
|
|
****************************************************************************************/
|
|
|
|
/****************************************************************************************
|
|
* Include files
|
|
****************************************************************************************/
|
|
#include "header.h" /* generic header */
|
|
#include <stdio.h> /* standard IO library */
|
|
|
|
|
|
/****************************************************************************************
|
|
* Function prototypes
|
|
****************************************************************************************/
|
|
static void DisplayUptime(void);
|
|
static void Init(void);
|
|
|
|
|
|
/****************************************************************************************
|
|
** NAME: main
|
|
** PARAMETER: none
|
|
** RETURN VALUE: program return code
|
|
** DESCRIPTION: This is the entry point for the bootloader application and is called
|
|
** by the reset interrupt vector after the C-startup routines executed.
|
|
**
|
|
****************************************************************************************/
|
|
int main(void)
|
|
{
|
|
/* initialize the microcontroller */
|
|
Init();
|
|
/* initialize the bootloader interface */
|
|
BootComInit();
|
|
|
|
/* start the infinite program loop */
|
|
while (1)
|
|
{
|
|
/* toggle LED with a fixed frequency */
|
|
LedToggle();
|
|
/* Output uptime on terminal */
|
|
DisplayUptime();
|
|
/* check for bootloader activation request */
|
|
BootComCheckActivationRequest();
|
|
}
|
|
|
|
/* program should never get here */
|
|
return 0;
|
|
} /*** end of main ***/
|
|
|
|
|
|
/****************************************************************************************
|
|
** NAME: DisplayUptime
|
|
** PARAMETER: none
|
|
** RETURN VALUE: none
|
|
** DESCRIPTION: Outputs the program's runtime on the terminal every second.
|
|
**
|
|
****************************************************************************************/
|
|
static void DisplayUptime(void)
|
|
{
|
|
static unsigned long uptime_ms_last = 0;
|
|
unsigned long uptime_ms_now;
|
|
unsigned char hr, min, sec;
|
|
|
|
uptime_ms_now = TimerGet();
|
|
|
|
/* output info on terminal just once a second */
|
|
if ( (uptime_ms_now - uptime_ms_last) < 1000)
|
|
{
|
|
/* not yet time to toggle */
|
|
return;
|
|
}
|
|
/* calculate uptime */
|
|
sec = uptime_ms_now/1000;
|
|
min = sec/60;
|
|
hr = min/60;
|
|
|
|
/* output time on terminal */
|
|
printf("Program uptime: %d:%02d:%02d\r", hr, min, sec);
|
|
|
|
/* store for next comparison */
|
|
uptime_ms_last = uptime_ms_now;
|
|
} /*** end of DisplayUptime ***/
|
|
|
|
|
|
/****************************************************************************************
|
|
** NAME: Init
|
|
** PARAMETER: none
|
|
** RETURN VALUE: none
|
|
** DESCRIPTION: Initializes the microcontroller.
|
|
**
|
|
****************************************************************************************/
|
|
static void Init(void)
|
|
{
|
|
volatile unsigned long StartUpCounter = 0, HSEStatus = 0;
|
|
unsigned long pll_multiplier;
|
|
|
|
/* reset the RCC clock configuration to the default reset state (for debug purpose) */
|
|
/* set HSION bit */
|
|
RCC->CR |= (unsigned long)0x00000001;
|
|
/* reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
|
|
RCC->CFGR &= (unsigned long)0xF8FF0000;
|
|
/* reset HSEON, CSSON and PLLON bits */
|
|
RCC->CR &= (unsigned long)0xFEF6FFFF;
|
|
/* reset HSEBYP bit */
|
|
RCC->CR &= (unsigned long)0xFFFBFFFF;
|
|
/* reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
|
|
RCC->CFGR &= (unsigned long)0xFF80FFFF;
|
|
/* disable all interrupts and clear pending bits */
|
|
RCC->CIR = 0x009F0000;
|
|
/* enable HSE */
|
|
RCC->CR |= ((unsigned long)RCC_CR_HSEON);
|
|
/* wait till HSE is ready and if Time out is reached exit */
|
|
do
|
|
{
|
|
HSEStatus = RCC->CR & RCC_CR_HSERDY;
|
|
StartUpCounter++;
|
|
}
|
|
while((HSEStatus == 0) && (StartUpCounter != 1500));
|
|
/* check if time out was reached */
|
|
if ((RCC->CR & RCC_CR_HSERDY) == RESET)
|
|
{
|
|
/* cannot continue when HSE is not ready */
|
|
while (1) { ; }
|
|
}
|
|
/* enable flash prefetch buffer */
|
|
FLASH->ACR |= FLASH_ACR_PRFTBE;
|
|
/* reset flash wait state configuration to default 0 wait states */
|
|
FLASH->ACR &= (unsigned long)((unsigned long)~FLASH_ACR_LATENCY);
|
|
#if (BOOT_CPU_SYSTEM_SPEED_KHZ > 48000)
|
|
/* configure 2 flash wait states */
|
|
FLASH->ACR |= (unsigned long)FLASH_ACR_LATENCY_2;
|
|
#elif (BOOT_CPU_SYSTEM_SPEED_KHZ > 24000)
|
|
/* configure 1 flash wait states */
|
|
FLASH->ACR |= (unsigned long)FLASH_ACR_LATENCY_1;
|
|
#endif
|
|
/* HCLK = SYSCLK */
|
|
RCC->CFGR |= (unsigned long)RCC_CFGR_HPRE_DIV1;
|
|
/* PCLK2 = HCLK/2 */
|
|
RCC->CFGR |= (unsigned long)RCC_CFGR_PPRE2_DIV2;
|
|
/* PCLK1 = HCLK/2 */
|
|
RCC->CFGR |= (unsigned long)RCC_CFGR_PPRE1_DIV2;
|
|
/* reset PLL configuration */
|
|
RCC->CFGR &= (unsigned long)((unsigned long)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | \
|
|
RCC_CFGR_PLLMULL));
|
|
/* calculate multiplier value */
|
|
pll_multiplier = BOOT_CPU_SYSTEM_SPEED_KHZ/BOOT_CPU_XTAL_SPEED_KHZ;
|
|
/* convert to register value */
|
|
pll_multiplier = (unsigned long)((pll_multiplier - 2) << 18);
|
|
/* set the PLL multiplier and clock source */
|
|
RCC->CFGR |= (unsigned long)(RCC_CFGR_PLLSRC_HSE | pll_multiplier);
|
|
/* enable PLL */
|
|
RCC->CR |= RCC_CR_PLLON;
|
|
/* wait till PLL is ready */
|
|
while((RCC->CR & RCC_CR_PLLRDY) == 0)
|
|
{
|
|
}
|
|
/* select PLL as system clock source */
|
|
RCC->CFGR &= (unsigned long)((unsigned long)~(RCC_CFGR_SW));
|
|
RCC->CFGR |= (unsigned long)RCC_CFGR_SW_PLL;
|
|
/* wait till PLL is used as system clock source */
|
|
while ((RCC->CFGR & (unsigned long)RCC_CFGR_SWS) != (unsigned long)0x08)
|
|
{
|
|
}
|
|
#if (BOOT_COM_UART_ENABLE == 0)
|
|
/* initialize the UART interface if this is not the interface used to
|
|
* reactivate OpenBLT
|
|
*/
|
|
UartInit(BOOT_COM_UART_BAUDRATE);
|
|
#endif
|
|
/* init the led driver */
|
|
LedInit();
|
|
/* init the timer driver */
|
|
TimerInit();
|
|
/* enable IRQ's, because they were initially disabled by the bootloader */
|
|
IrqInterruptEnable();
|
|
} /*** end of Init ***/
|
|
|
|
|
|
/*********************************** end of main.c *************************************/
|