Esempio n. 1
0
int main(void) {
	/* Initialize system */
	SystemInit();
	
	/* Initialize delay */
	TM_DELAY_Init();
	
	/* Initialize leds on board */
	TM_DISCO_LedInit();
	
	/* Initialize button */
	TM_DISCO_ButtonInit();
	
	/* Disable watchdog when we are in debug mode */
	DBGMCU->APB1FZ |= DBGMCU_IWDG_STOP;
	
	/* Initialize watchdog timer */
	/* Set timeout to 1s */
	/* If we are in debug mode, watchdog won't start even if we enable it */
	if (TM_WATCHDOG_Init(TM_WATCHDOG_Timeout_1s)) {
		/* System was reset by watchdog */
		TM_DISCO_LedOn(LED_RED);
	} else {
		/* System was not reset by watchdog */
		TM_DISCO_LedOn(LED_GREEN);
	}
	
	while (1) {
		/* If button is pressed, do nothing and system will be reset after 1 second */
		while (TM_DISCO_ButtonPressed());
		
		/* Reset watchdog */
		TM_WATCHDOG_Reset();
	}
}
Esempio n. 2
0
int main(void) {
	/* Initialize system */
	SystemInit();
	
	/* Initialize delay */
	TM_DELAY_Init();
	
	/* Initialize leds on board */
	TM_DISCO_LedInit();
	
	/* Initialize button */
	TM_DISCO_ButtonInit();
	
	/* Initialize watchdog timer */
	/* Set timeout to 1s */
	if (TM_WATCHDOG_Init(TM_WATCHDOG_Timeout_1s)) {
		/* System was reset by watchdog */
		TM_DISCO_LedOn(LED_RED);
	} else {
		/* System was not reset by watchdog */
		TM_DISCO_LedOn(LED_GREEN);
	}
	
	while (1) {
		/* If button is pressed, do nothing and system will be reset after 1 second */
		while (TM_DISCO_ButtonPressed());
		
		/* Reset watchdog */
		TM_WATCHDOG_Reset();
	}
}
Esempio n. 3
0
int main(void) {
	//Initialize system
	SystemInit();
	//Initialize delay
	TM_DELAY_Init();
	//Initialize leds on board
	TM_DISCO_LedInit();
	//Initialize button
	TM_DISCO_ButtonInit();
	//Initialize watchdog timer
	//Set timeout to 1s
	if (TM_WATCHDOG_Init(TM_WATCHDOG_Timeout_1s)) {
		//System was reset by watchdog
		TM_DISCO_LedOn(LED_RED);
	} else {
		//System was not reset by watchdog
		TM_DISCO_LedOn(LED_GREEN);
	}
	
	while (1) {
		//if button is pressed, do nothing and system will be reset after 1 second
		while (TM_DISCO_ButtonPressed());
		
		//Reset watchdog
		TM_WATCHDOG_Reset();
	}
}
Esempio n. 4
0
/* Handle all EXTI lines */
void TM_EXTI_Handler(uint16_t GPIO_Pin) {
	/* Check proper line */
	if (GPIO_Pin == TM_DISCO_BUTTON_PIN) {
		/* Toggle pin only if button is pressed */
		if (TM_DISCO_ButtonPressed()) {
			/* Toggle LEDs if interrupt on button line happens */
			TM_DISCO_LedToggle(LED_ALL);
		}
	}
}
uint8_t TM_DISCO_ButtonOnReleased(void) {
	/* If button is now released, but was not already released */
	if (!TM_DISCO_ButtonPressed() && TM_INT_DISCO_ButtonPressed) {
		/* Clear flag */
		TM_INT_DISCO_ButtonPressed = 0;
		
		/* Return button onreleased */
		return 1;
	}
	
	/* Button is not released or it was already released before */
	return 0;
}
uint8_t TM_DISCO_ButtonOnPressed(void) {
	/* If button is now pressed, but was not already pressed */
	if (TM_DISCO_ButtonPressed() && !TM_INT_DISCO_ButtonPressed) {
		/* Set flag */
		TM_INT_DISCO_ButtonPressed = 1;
		
		/* Return button onpressed */
		return 1;
	}
	
	/* Button is not pressed or it was already pressed before */
	return 0;
}
Esempio n. 7
0
int main(void) {
	/* Init system clock for maximum system speed */
	TM_RCC_InitSystem();
	
	/* Init HAL layer */
	HAL_Init();
	
	/* Init leds */
	TM_DISCO_LedInit();
	
	/* Init button */
	TM_DISCO_ButtonInit();
	
	/* Check if system reset because of IWDG */
	if (TM_IWDG_Init(TM_IWDG_Timeout_4s)) {
		/* System reset was done because of IWDG timer */
		TM_DISCO_LedOn(LED_RED);
	} else {
		/* No IWDG */
		TM_DISCO_LedOn(LED_GREEN);
	}
	
	/* If there is no LED active (F7-Discovery, Nucleo boards), */
	/* then system reset occurred because of IWDG */
	
	while (1) {
		/* Check for button */
		if (TM_DISCO_ButtonPressed()) {
			/* Wait till pressed */
			/* If pressed more than 4 seconds in a row, system will reset because of IWDG timer */
			while (TM_DISCO_ButtonPressed());
		}
		
		/* Reset watchdog */
		TM_IWDG_Reset();
	}
}
Esempio n. 8
0
int main(void) {	
	/* Initialize system */
	SystemInit();
	
	/* Initialize delay */
	TM_DELAY_Init();
	
	/* Initiaize button */
	TM_DISCO_ButtonInit();
	
	/* Initialize Leds */
	TM_DISCO_LedInit();
	
	/* Initialize USART, TX: PB10, RX: PB11 */
	TM_USART_Init(USART3, TM_USART_PinsPack_1, 115200);
	
	/* Initialize RTC with internal 32768Hz clock */
	/* It's not very accurate */
	if (!TM_RTC_Init(TM_RTC_ClockSource_Internal)) {
		/* RTC was first time initialized */
		/* Do your stuff here */
		/* eg. set default time */
	}
	
	/* Set wakeup interrupt every 125 ms */
	TM_RTC_Interrupts(TM_RTC_Int_125ms);
	
	while (1) {
		/* If button pressed */
		if (TM_DISCO_ButtonPressed()) {
			
			/* Subseconds are ignored when writing new time */
			
			datatime.hours = 0;
			datatime.minutes = 59;
			datatime.seconds = 55;
			datatime.year = 14;
			datatime.month = 6;
			datatime.date = 30;
			datatime.day = 6;
			
			/* Set new time */
			TM_RTC_SetDateTime(&datatime, TM_RTC_Format_BIN);
		}
	}
}
Esempio n. 9
0
int main(void) {
	__IO uint32_t i;
	
	/* CPU load structure */
	TM_CPULOAD_t CPU_LOAD;
	
	/* Init CPU load monitor */
	TM_CPULOAD_Init(&CPU_LOAD);
	
	/* Initialize system */
	SystemInit();
	
	/* Initialize delay */
	TM_DELAY_Init();
	
	/* Initialize leds on board */
	TM_DISCO_LedInit();
	
	/* Init button */
	TM_DISCO_ButtonInit();
	
	/* Init USART2, TX: PA2, RX: PA3, 921600 bauds */
	TM_USART_Init(USART2, TM_USART_PinsPack_1, 921600);
	
	while (1) {
		/* Check if CPU LOAD variable is updated */
		if (CPU_LOAD.Updated) {
			/* Print to user */
			printf("W: %u; S: %u; Load: %5.2f\n", CPU_LOAD.WCNT, CPU_LOAD.SCNT, CPU_LOAD.Load);
		}
		
		/* Toggle leds */
		TM_DISCO_LedToggle(LED_ALL);
		
		/* If button pressed, do some useless counting */
		if (TM_DISCO_ButtonPressed()) {
			/* Count something to waste some time before entering to sleep mode */
			i = 0;
			while (i++ < 0x1FFF);
		}
		
		/* Go low power mode, sleep mode until interrupt, measure CPU load */
		TM_CPULOAD_GoToSleepMode(&CPU_LOAD, TM_LOWPOWERMODE_SleepUntilInterrupt);
	}
}
Esempio n. 10
0
int main(void) {	
	/* Initialize system */
	SystemInit();
	
	/* Initialize delay */
	TM_DELAY_Init();
	
	/* Initiaize button */
	TM_DISCO_ButtonInit();
	
	/* Initialize Leds */
	TM_DISCO_LedInit();
	
	/* Initialize RTC with internal 32768Hz clock */
	/* It's not very accurate */
	if (!TM_RTC_Init(TM_RTC_ClockSource_Internal)) {
		/* RTC was first time initialized */
		/* Do your stuff here */
		/* eg. set default time */
		
		/* Write data to backup register 4 */
		TM_RTC_WriteBackupRegister(4, 0x1244);
		
		/* Turn on RED led = Write operation */
		TM_DISCO_LedOn(LED_RED);
	} else {
		/* Try to read data back and check if it is OK */
		if (TM_RTC_ReadBackupRegister(4) == 0x1244) {
			/* Read OK after reset, turn on GREEN led */
			TM_DISCO_LedOn(LED_GREEN);
		}
	}
	
	/* If not leds ON, try to remove power from device first so RTC will need to initialize again */
	/* If still no leds after that, then you have problems with your RTC clock */
	
	while (1) {
		/* If button pressed */
		if (TM_DISCO_ButtonPressed()) {
			/* After button press, system will reset */
			NVIC_SystemReset();
		}
	}
}
Esempio n. 11
0
int main(void) {
	/* Initialize System */
	SystemInit();
	
	/* Initialize leds on board */
	TM_DISCO_LedInit();
	/* Initialize button on board */
	TM_DISCO_ButtonInit();
	
    while(1) {
		/* If button pressed */
    	if (TM_DISCO_ButtonPressed()) {
			/* Turn on leds */
    		TM_DISCO_LedOn(LED_RED | LED_GREEN);
    	} else {
			/* Turn off leds */
    		TM_DISCO_LedOff(LED_RED | LED_GREEN);
    	}
    }
}
Esempio n. 12
0
int main(void) {
	/* Initialize System */
	SystemInit();
	
	/* Initialize leds on board */
	TM_DISCO_LedInit();
	
	/* Initialize button on board */
	TM_DISCO_ButtonInit();
	
    while(1) {
		/* If button pressed, do stuff all the time button is pressed */
    	if (TM_DISCO_ButtonPressed()) {
			/* Turn on leds */
    		TM_DISCO_LedOn(LED_RED | LED_GREEN);
    	} else {
			/* Turn off leds */
    		TM_DISCO_LedOff(LED_RED | LED_GREEN);
    	}
		
		/* Do the stuff only once when button is pressed */
		if (TM_DISCO_ButtonOnPressed()) {
			/* Do here stuff only once */
			/* This function will return 0 until you release button and press it again */
			/* For example, you can send data here to USART, but only once when button is pressed */
			GPIOD->BSRRL = LED_BLUE;
		}
		
		/* Do the stuff only once when button is released */
		if (TM_DISCO_ButtonOnReleased()) {
			/* DO here stuff only once */
			/* This function will return 0 until you press button and release it again */
			GPIOD->BSRRH = LED_BLUE;
		}
    }
}
Esempio n. 13
0
int main(void) {
	uint8_t already = 0;
	
	/* Set structs for all examples */
	TM_USB_HIDDEVICE_Keyboard_t Keyboard;
	TM_USB_HIDDEVICE_Gamepad_t Gamepad1, Gamepad2;
	TM_USB_HIDDEVICE_Mouse_t Mouse;
	
	/* Initialize system */
	SystemInit();
	
	/* Initialize leds */
	TM_DISCO_LedInit();
	
	/* Initialize button */
	TM_DISCO_ButtonInit();
	
	/* Initialize delay */
	TM_DELAY_Init();
	
	/* Initialize USB HID Device */
	TM_USB_HIDDEVICE_Init();
	
	/* Set default values for mouse struct */
	TM_USB_HIDDEVICE_MouseStructInit(&Mouse);
	/* Set default values for keyboard struct */
	TM_USB_HIDDEVICE_KeyboardStructInit(&Keyboard);
	/* Set default values for gamepad structs */
	TM_USB_HIDDEVICE_GamepadStructInit(&Gamepad1);
	TM_USB_HIDDEVICE_GamepadStructInit(&Gamepad2);

	while (1) {		  
		/* If we are connected and drivers are OK */
		if (TM_USB_HIDDEVICE_GetStatus() == TM_USB_HIDDEVICE_Status_Connected) {
			/* Turn on green LED */
			TM_DISCO_LedOn(LED_GREEN);
			
/* Simple sketch start */	
			
			/* If you pressed button right now and was not already pressed */
			if (TM_DISCO_ButtonPressed() && already == 0) { /* Button on press */
				already = 1;
				
				/* Set pressed keys = WIN + R */
				Keyboard.L_GUI = TM_USB_HIDDEVICE_Button_Pressed;	/* Win button */
				Keyboard.Key1 = 0x15; 								/* R */
				/* Result = "Run" command */
				
				/* Send keyboard report */
				TM_USB_HIDDEVICE_KeyboardSend(&Keyboard);
			} else if (!TM_DISCO_ButtonPressed() && already == 1) { /* Button on release */
				already = 0;
				
				/* Release all buttons */
				Keyboard.L_GUI = TM_USB_HIDDEVICE_Button_Released;	/* No button */
				Keyboard.Key1 = 0x00; 								/* No key */
				/* Result = Released everything */
				
				/* Send keyboard report */
				TM_USB_HIDDEVICE_KeyboardSend(&Keyboard);
			}
			
/* Simple sketch end */
			
		} else {
			/* Turn off green LED */
			TM_DISCO_LedOff(LED_GREEN);
		}
	}
}
Esempio n. 14
0
int main(void) {	
	/* Initialize system */
	SystemInit();
	
	/* Initialize delay */
	TM_DELAY_Init();
	
	/* Initiaize button */
	TM_DISCO_ButtonInit();
	
	/* Initialize Leds */
	TM_DISCO_LedInit();
	
	/* Initialize USART, TX: PB10, RX: PB11 */
	TM_USART_Init(USART3, TM_USART_PinsPack_1, 115200);
	
	/* Initialize RTC with internal 32768Hz clock */
	/* It's not very accurate */
	if (!TM_RTC_Init(TM_RTC_ClockSource_Internal)) {
		/* RTC was first time initialized */
		/* Do your stuff here */
		/* eg. set default time */
	}
	
	/* Set wakeup interrupt every 1 second */
	TM_RTC_Interrupts(TM_RTC_Int_1s);
	
	while (1) {
		/* If button pressed */
		if (TM_DISCO_ButtonPressed()) {
			
			/* Set new time */
			Time.hours = 21;
			Time.minutes = 11;
			Time.seconds = 00;
			Time.year = 14;
			Time.month = 10;
			Time.date = 20;
			Time.day = 1;
			
			/* Set new RTC time */
			TM_RTC_SetDateTime(&Time, TM_RTC_Format_BIN);
			
			/* Set alarm A each day 1 (Monday) in a week */
			/* Alarm will be first triggered 5 seconds later as time is configured for RTC */
			AlarmTime.hours = Time.hours;
			AlarmTime.minutes = Time.minutes;
			AlarmTime.seconds = Time.seconds + 5;
			AlarmTime.alarmtype = TM_RTC_AlarmType_DayInWeek;
			AlarmTime.day = 1;
			
			/* Set RTC alarm A, time in binary format */
			TM_RTC_SetAlarm(TM_RTC_Alarm_A, &AlarmTime, TM_RTC_Format_BIN);
			
			/* Set alarm B each 20th day in a month */
			/* Alarm will be first triggered 10 seconds later as time is configured for RTC */
			AlarmTime.hours = Time.hours;
			AlarmTime.minutes = Time.minutes;
			AlarmTime.seconds = Time.seconds + 10;
			AlarmTime.day = 20;
			AlarmTime.alarmtype = TM_RTC_AlarmType_DayInMonth;
			
			/* Set RTC alarm B, time in binary format */
			TM_RTC_SetAlarm(TM_RTC_Alarm_B, &AlarmTime, TM_RTC_Format_BIN);
		}
	}
}
Esempio n. 15
0
int main(void) {
	uint8_t already = 0;

	/* Set structs for all examples */
	TM_USB_HIDDEVICE_Keyboard_t Keyboard;
	TM_USB_HIDDEVICE_Gamepad_t Gamepad1, Gamepad2;
	TM_USB_HIDDEVICE_Mouse_t Mouse;

	/* Initialize system */
	SystemInit();

	//Initialize LIS302DL - acceleration sensor;...
	//... the board position determines the movement of joystick

	LIS302DL_Init1();

	/* Initialize leds */
	TM_DISCO_LedInit();

	/* Initialize button */
	TM_DISCO_ButtonInit();

	/* Initialize delay */
	TM_DELAY_Init();

	/* Initialize USB HID Device */
	TM_USB_HIDDEVICE_Init();

	/* Set default values for mouse struct */
	TM_USB_HIDDEVICE_MouseStructInit(&Mouse);
	/* Set default values for keyboard struct */
	TM_USB_HIDDEVICE_KeyboardStructInit(&Keyboard);
	/* Set default values for gamepad structs */
	TM_USB_HIDDEVICE_GamepadStructInit(&Gamepad1);
	TM_USB_HIDDEVICE_GamepadStructInit(&Gamepad2);

	//Variables
	const int8_t axis_x_min = 0; //The number represents maximum deflection of left-stick on left; Default: -128 (!The value recommended for Unity game: 0 - Unity can read only not minus numbers of axis!)
	const int8_t axis_x_max = 127; //The number represents maximum deflection of left-stick on right; Default: 127
	const int8_t axis_x_avg = (axis_x_min + axis_x_max) / 2; //The value presented center position of left stick
	const int8_t acc_x_min = -60; //Minimum (approximately) value reading from accelerometer for X-axis
	const int8_t acc_x_max = 60; //Maximum (approximately) value reading from accelerometer for X-axis
	const int8_t kx = (-axis_x_min + axis_x_max) / (-acc_x_min + acc_x_max); //Thanks to this ratio, can create universal formula on value of gamepad left-axis depends on STM-board position

		const int8_t axis_y_min = 0; //The number represents maximum deflection of left-stick on top; Default: -128 (!The value recommended for Unity game: 0 - Unity can read only not minus numbers of axis!)
		const int8_t axis_y_max = 127; //The number represents maximum deflection of left-stick on bottom; Default: 127
		const int8_t axis_y_avg = (axis_y_min + axis_y_max) / 2; //The value presented center position of left stick
		const int8_t acc_y_min = -60; //Minimum (approximately) value reading from accelerometer for Y-axis
		const int8_t acc_y_max = 60; //Maximum (approximately) value reading from accelerometer for Y-axis
		const int8_t ky = (-axis_y_min + axis_y_max) / (-acc_y_min + acc_y_max); //Thanks to this ratio, can create universal formula on value of gamepad left-axis depends on STM-board position

	while (1) {
		/* If we are connected and drivers are OK */
		if (TM_USB_HIDDEVICE_GetStatus() == TM_USB_HIDDEVICE_Status_Connected) {
			/* Turn on green LED */
			TM_DISCO_LedOn(LED_GREEN);

/* Simple sketch start */

			/* If you pressed button right now and was not already pressed */
			if (TM_DISCO_ButtonPressed() && already == 0) { /* Button on press */
				already = 1;

				/* Gamepad 1 */
				/* Simulate button 1 on gamepad 1 */
				Gamepad1.Button1 = TM_USB_HIDDEVICE_Button_Pressed;

				/* Send report for gamepad 1 */
				TM_USB_HIDDEVICE_GamepadSend(TM_USB_HIDDEVICE_Gamepad_Number_1, &Gamepad1);


			} else if (!TM_DISCO_ButtonPressed() && already == 1) { /* Button on release */
				already = 0;

				//I thought the command that is 5 lines below was sufficient but no...
				//I released blue button but computer "thought" I still pressed it.
				Gamepad1.Button1 = TM_USB_HIDDEVICE_Button_Released; //release button 1

				/* Send command to release all buttons on both gamepads */
				TM_USB_HIDDEVICE_GamepadReleaseAll(TM_USB_HIDDEVICE_Gamepad_Number_1);
			}

			// Read the board position on axes
			LIS302DL_Read(&acc_x,LIS302DL_OUT_X_ADDR,1);
			LIS302DL_Read(&acc_y,LIS302DL_OUT_Y_ADDR,1);
			LIS302DL_Read(&acc_z,LIS302DL_OUT_Z_ADDR,1);

			//The value on axis depends on board lean
			//HERE: Changing orientation of left joystick under the influence of X-axis

			/* Simulate left stick rotation */
			/* X axis */
			if (acc_x > -15 && acc_x < 15) //horizontal position of STM-board
			{
				Gamepad1.LeftXAxis = axis_x_avg;
			}
			else
			{
				if (acc_x >= acc_x_max - 10) //maximum right position --> vertical position of STM-board (USB mini-B on top of board)
				{
					Gamepad1.LeftXAxis = axis_x_max;
				}
				else
				{
					if (acc_x <= acc_x_min + 10) //maximum left position --> vertical position of STM-board (mini-jack out and USB micro-B on top )
					{
						Gamepad1.LeftXAxis = axis_x_min;
					}
					else
					{
						Gamepad1.LeftXAxis = acc_x * kx + axis_x_avg; //universal formula on left-stick position of gamepad depending on STM position
						//it seems to be sufficient (without if-conditionals); but using of "if", it ensures more stability
					}
				}
			}

			/* Y axis */
			if (acc_y > -15 && acc_y < 15) //horizontal position of STM-board
			{
				Gamepad1.LeftYAxis = axis_y_avg;
			}
			else
			{
				if (acc_y >= acc_x_max - 10) //maximum "up" position --> blue button of STM is higher than black reset-button
				{
					Gamepad1.LeftYAxis = axis_y_min;
				}
				else
				{
					if (acc_y <= acc_x_min + 10) //maximum "down" position --> black button of STM is higher than blue button
					{
						Gamepad1.LeftYAxis = axis_y_max;
					}
					else
					{
						Gamepad1.LeftYAxis = acc_y * (-ky) + axis_y_avg; //universal formula on left-stick position of gamepad depending on STM position
						//it seems to be sufficient (without if-conditionals); but using of "if", it ensures more stability
					}
				}
			}

			TM_USB_HIDDEVICE_GamepadSend(TM_USB_HIDDEVICE_Gamepad_Number_1, &Gamepad1);

/* Simple sketch end */

		} else {
			/* Turn off green LED */
			TM_DISCO_LedOff(LED_GREEN);
		}
	}
}