Set RTC


Provides utility to set RTC.

Toolkit:STM Development System

Location:/bipom/devtools/STM32/examples/set_rtc

Code Example


UCB uart1;
DEVICE_INTERFACE spi1_int_arm;

//********************************************************************************

static void vRTCTEST( void *pvParameters )
{
	(void) pvParameters;
	//

	while(1) 
	{
		for(;;)	switch (Menu())
		{
			case '0': 
				PrintTime();	
			break;
			case '1': 
				tprintf ("\n\rHours[0..23]:");
				Set_RTC_Register (HOUR_REG);
			break;
			case '2': 
				tprintf ("\n\rMinutes[0..59]:");
				Set_RTC_Register (MINUTE_REG);
			break;
			case '3': 
				tprintf ("\n\rSeconds[0..59]:");
				Set_RTC_Register (SECOND_REG);
			break;
			case '4': 
				tprintf ("\n\rDate[1..31]:");
				Set_RTC_Register (DATE_REG);
			break;
			case '5': 
				tprintf ("\n\rMonth[1..12]:");
				Set_RTC_Register (MONTH_REG);
			break;
			case '6': 
				tprintf ("\n\rYear[0..99]:20");
				Set_RTC_Register (YEAR_REG);
			break;
			default:
				tprintf ("\n\rUNKNOWN COMMAND");
			break; 
		}	
	}
}

//********************************************************************************

void Set_RTC_Register (UBYTE reg)
{
	BIPOM_TIME t;
	BIPOM_TIME_GetTime(&t);
	
	register UBYTE dataByte;
	dataByte = GetNumber(2);
	
	switch(reg)
	{
		case HOUR_REG:		t.hour	= dataByte;	break;
		case MINUTE_REG:	t.minute= dataByte;	break;
		case SECOND_REG:	t.second= dataByte;	break;
		case DATE_REG:		t.day	= dataByte;	break;
		case MONTH_REG:		t.month	= dataByte;	break;
		case YEAR_REG: 		t.year	= dataByte + 2000;	break;
		default:
			tprintf ("\n\rERROR: unknown register, %d",reg);
		break;					
	}
	
	BIPOM_TIME_SetTime(&t);
	
	PrintTime();
} 

//********************************************************************************

void PrintTime()
{
	char buf[128];
	
	BIPOM_TIME t;
	BIPOM_TIME_GetTime(&t);
	BIPOM_TIME_Format(buf, &t, BIPOM_TIME_FULL_FORMAT);
	
	tprintf("\n\r%s", buf);
}

//********************************************************************************

UBYTE Menu()
{
	tprintf ("\n\r* SET RTC UTILITY *");
	tprintf ("\n\r0 - Print time");
	tprintf ("\n\r1 - Set hours");
	tprintf ("\n\r2 - Set minutes");
	tprintf ("\n\r3 - Set seconds");
	tprintf ("\n\r4 - Set date");
	tprintf ("\n\r5 - Set month");
	tprintf ("\n\r6 - Set year");
	tprintf ("\n\r>");	

	UBYTE key = WaitChar();
	tprintf ("%c",key);
	
	return key;
	
}

//********************************************************************************

UINT GetNumber (UBYTE digits)
{
	UBYTE ndx,symbol = 0;
	UINT value;
	UBYTE buffer[6];
	UINT cnt;
	if (digits > 5) return 0;
	ndx =0;
	value =0;
	for (;;)
	{
		if(ndx < digits)	
			symbol = toupper(WaitChar());
		if (
			(symbol == 0x0d)
			||
			(ndx == digits)
			)
		{
			cnt =1;
			while(ndx)
			{
						
				symbol = buffer[ndx-1] -'0';
				value += symbol * cnt;
				cnt *= 10;
				ndx--;
			} 	
				
			return value;
		}
		else
		{	
			if(
				((symbol >= '0') && (symbol <='9'))
			)
				{
					tprintf ("%c",symbol);
					buffer[ndx++] = symbol;
				}
		}
	}
}

//********************************************************************************

UBYTE WaitChar()
{

	while(1)
	{
		int dataInt = uart1.getData();
		if (dataInt == -1) 
			continue;
			
		return (UBYTE)dataInt;
	}
	
	return 0;
}

//********************************************************************************

int main(void) 
{
	tprintf("\n\rMini-Max/STM32F1");
	tprintf("\n\rSET RTC UTILITY REV 1.01");

	DM_Init();
	SPI1_Init(SPI1_INTERFACE_TYPE,DEVICE_INTERFACE_IN_PROGRESS);

	uart1.iface			= SERIALAPI_UART1_INTERFACE;
	uart1.baudRate		= 115200;									// uart baudrate

	uart1.parity		= SERIALAPI_PARITY_NO;						// uart parity

	uart1.stop			= SERIALAPI_STOP_1;							// uart stop bit

	uart1.word_length	= SERIALAPI_WORLD_LENGTH_8;					// uart word length

	//

	int ec = serialAPI_init(&uart1);
	if(ec) 
	{
		tprintf("\r\nERROR: can't initialize USART1, ec = %d",ec);
		for(;;);	
	}
	tprintf("\r\nuart1 init is OK");
	//

	INT_ARM_Init();
	INT_ARM_RegisterInterface(&spi1_int_arm);
	spi1_int_arm.iFaceType 	= SPI1_INTERFACE_TYPE;
	spi1_int_arm.iFaceState	= DEVICE_INTERFACE_PROCESS_REQUEST;
	spi1_int_arm.process	= DM_Process;

	
	// Initialize RTC 

	// if RTC_Init returns SUCCESS the RTC is still running with correct time

	// Otherwise RTC is reset to 00:00:00 

	if(SUCCESS != RTC_Init()) RTC_Set(0);
	
	BIPOM_TIME_Init(BIPOM_TIME_HDW_STM_RTC, 0);

	if(pdPASS != 
		xTaskCreate (vRTCTEST, ( const signed portCHAR * const )"RTCTEST", 
			configMINIMAL_STACK_SIZE<<4, NULL, tskIDLE_PRIORITY, NULL ))
	{
		tprintf("\n\rERROR: can't create RTCCTEST task");
	}
	/* Now all the tasks have been started - start the scheduler. */
	vTaskStartScheduler();
	for(;;);
	return 0;
}