diff --git a/UAV-ControlSystem/inc/drivers/system_clock.h b/UAV-ControlSystem/inc/drivers/system_clock.h new file mode 100644 index 0000000..9680a0d --- /dev/null +++ b/UAV-ControlSystem/inc/drivers/system_clock.h @@ -0,0 +1,51 @@ +/************************************************************************** +* NAME: system_clock.h * +* AUTHOR: Lennart Eriksson * +* PURPOSE: Enable and handle system clock functionality * +* INFORMATION: * +* This file initilizes the system clock and handles delays, get time * +* in both micro- and milliseconds. * +* * +* GLOBAL VARIABLES: * +* Variable Type Description * +* -------- ---- ----------- * +**************************************************************************/ + + +#ifndef DRIVERS_SYSTEM_CLOCK_H_ +#define DRIVERS_SYSTEM_CLOCK_H_ + +#include "stm32f4xx.h" + +/*********************************************************************** +* BRIEF: Starts the system clock at 100MHz * +* INFORMATION: In the current version it works with ADC and DMA * +***********************************************************************/ +void system_clock_config(void); + +/*********************************************************************** +* BRIEF: Get the time since the system started in milliseconds +* INFORMATION: return the time in milliseconds +***********************************************************************/ +uint32_t clock_get_ms(); + +/*********************************************************************** +* BRIEF: Get the time since the system started in microseconds +* INFORMATION: return the time in microseconds +***********************************************************************/ +uint32_t clock_get_us(); + +/*********************************************************************** +* BRIEF: stall the system for number of milliseconds +* INFORMATION: +***********************************************************************/ +void clock_delay_ms(uint32_t ms); + +/*********************************************************************** +* BRIEF: Stall the system for a number of microseconds +* INFORMATION: +***********************************************************************/ +void clock_delay_us(uint32_t us); + + +#endif /* DRIVERS_SYSTEM_CLOCK_H_ */ diff --git a/UAV-ControlSystem/src/drivers/system_clock.c b/UAV-ControlSystem/src/drivers/system_clock.c new file mode 100644 index 0000000..44510b9 --- /dev/null +++ b/UAV-ControlSystem/src/drivers/system_clock.c @@ -0,0 +1,110 @@ +/************************************************************************** +* NAME: system_clock.c * +* PURPOSE: Enable and handle system clock functionality * +* INFORMATION: * +* * +* GLOBAL VARIABLES: * +* Variable Type Description * +* -------- ---- ----------- * +**************************************************************************/ + +#include "drivers/system_clock.h" + +//the number of clock cycles per microsecond +static uint32_t usTicks = 0; + +/*********************************************************************** +* BRIEF: Starts the system clock at 100MHz * +* INFORMATION: In the current version it works with ADC and DMA * +***********************************************************************/ +void system_clock_config(void) +{ + RCC_ClkInitTypeDef RCC_ClkInitStruct; + RCC_OscInitTypeDef RCC_OscInitStruct; + + //Enable GPIO clocks + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + + //Enable power clock + __HAL_RCC_PWR_CLK_ENABLE(); + + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); + + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLM = 8; + RCC_OscInitStruct.PLL.PLLN = 288; + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; + RCC_OscInitStruct.PLL.PLLQ = 6; + HAL_RCC_OscConfig(&RCC_OscInitStruct); + + RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; + HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4); + + + if (HAL_GetREVID() == 0x1001) + __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); + + //Set the number of sycles per microsecond + usTicks = SystemCoreClock / 1000000; + +} + +/*********************************************************************** +* BRIEF: Get the time since the system started in microseconds +* INFORMATION: return the time in microseconds +***********************************************************************/ +uint32_t clock_get_us() +{ + register uint32_t ms, cycle_cnt; + + //make sure that the system tick interrupt does not happen during the time we fetch microsecond from the register + do { + ms = clock_get_ms(); + cycle_cnt = SysTick->VAL; + + //If the SysTick timer expired during the previous instruction, we need to give it a little time for that + //interrupt to be delivered before we can recheck sysTickUptime: + asm volatile("\tnop\n"); + } while (ms != clock_get_ms()); + + assert_param(us >= 1000); + + return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks; +} + +/*********************************************************************** +* BRIEF: Get the time since the system started in milliseconds +* INFORMATION: return the time in milliseconds +***********************************************************************/ +uint32_t clock_get_ms() +{ + return HAL_GetTick(); +} + +/*********************************************************************** +* BRIEF: stall the system for number of milliseconds +* INFORMATION: +***********************************************************************/ +void clock_delay_ms(uint32_t ms) +{ + HAL_Delay(ms); +} + +/*********************************************************************** +* BRIEF: Stall the system for a number of microseconds +* INFORMATION: +***********************************************************************/ +void clock_delay_us(uint32_t us) +{ + volatile uint32_t time = clock_get_us() + us; + while(clock_get_us() < time); +} diff --git a/UAV-ControlSystem/src/main.c b/UAV-ControlSystem/src/main.c index e9b440d..3368f78 100644 --- a/UAV-ControlSystem/src/main.c +++ b/UAV-ControlSystem/src/main.c @@ -11,121 +11,79 @@ #include "drivers/adc.h" +#include "drivers/system_clock.h" #include "stm32f4xx.h" #include "system_variables.h" #include "utilities.h" #include - - - -static void SystemClock_Config(void); - - - int main(void) { - - // Comment? + // Initialize the Hardware Abstraction Layer HAL_Init(); + // Configure the system clock to 100 MHz + system_clock_config(); + + int i = 1; - /* Configure the system clock to 100 MHz */ - SystemClock_Config(); + //Add ADC Channels + adc_pin_add(ADC_CHANNEL_0); + adc_pin_add(ADC_CHANNEL_1); + adc_pin_add(ADC_CHANNEL_12); - adc_pin_add(ADC_CHANNEL_0); - adc_pin_add(ADC_CHANNEL_1); - adc_pin_add(ADC_CHANNEL_12); + //Configure the ADCs + adc_configure(); + /* This is done in system_clock_config for all GPIO clocks */ + //__GPIOB_CLK_ENABLE(); - adc_configure(); + GPIO_InitTypeDef gpinit; + gpinit.Pin = GPIO_PIN_5; + gpinit.Mode = GPIO_MODE_OUTPUT_PP; + gpinit.Pull = GPIO_PULLUP; + gpinit.Speed = GPIO_SPEED_HIGH; + HAL_GPIO_Init(GPIOB, &gpinit); + adc_start(); + int num = 2000; + int j = 0; + volatile uint32_t time_us[num]; - __GPIOB_CLK_ENABLE(); + while (1) + { + i++; - GPIO_InitTypeDef gpinit; - gpinit.Pin = GPIO_PIN_5; - gpinit.Mode = GPIO_MODE_OUTPUT_PP; - gpinit.Pull = GPIO_PULLUP; - gpinit.Speed = GPIO_SPEED_HIGH; - HAL_GPIO_Init(GPIOB, &gpinit); + //g_ADCValue = accumulate(g_ADCBuffer,ADC_BUFFER_LENGTH) / ADC_BUFFER_LENGTH; + //HAL_Delay(100); + int g_ADCValue = adc_read(ADC_CHANNEL_0); + int g_ADCValue1 = adc_read(ADC_CHANNEL_1); + int g_ADCValue12 = adc_read(ADC_CHANNEL_12); - adc_start(); + int offTime = g_ADCValue; + int onTime = 4096 - offTime; + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5,GPIO_PIN_SET); + for (int i = 0; i < onTime; i++) + { + asm("nop"); + } + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5,GPIO_PIN_RESET); + for (int i = 0; i < offTime; i++) + { + asm("nop"); + } - /*##-3- Toggle PA05 IO in an infinite loop #################################*/ - while (1) - { - i++; - - //g_ADCValue = accumulate(g_ADCBuffer,ADC_BUFFER_LENGTH) / ADC_BUFFER_LENGTH; - //HAL_Delay(100); - int g_ADCValue = adc_read(ADC_CHANNEL_0); - int g_ADCValue1 = adc_read(ADC_CHANNEL_1); - int g_ADCValue12 = adc_read(ADC_CHANNEL_12); - - int offTime = g_ADCValue; - int onTime = 4096 - offTime; - HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5,GPIO_PIN_SET); - for (int i = 0; i < onTime; i++) - { - asm("nop"); - } - HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5,GPIO_PIN_RESET); - for (int i = 0; i < offTime; i++) - { - asm("nop"); - } - - } + //Get time in microseconds + if(j < num) + time_us[j++] = clock_get_us(); + } for(;;); } -/*********************************************************************** -* BRIEF: Starts the system clock * -* INFORMATION: In the current version it works with ADC and DMA * -***********************************************************************/ -static void SystemClock_Config(void) -{ - RCC_ClkInitTypeDef RCC_ClkInitStruct; - RCC_OscInitTypeDef RCC_OscInitStruct; - - - //__PWR_CLK_ENABLE(); // samma skit - - __HAL_RCC_PWR_CLK_ENABLE(); - __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); - - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; - RCC_OscInitStruct.HSEState = RCC_HSE_ON; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; - RCC_OscInitStruct.PLL.PLLM = 8; - RCC_OscInitStruct.PLL.PLLN = 288; - RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; - RCC_OscInitStruct.PLL.PLLQ = 6; - HAL_RCC_OscConfig(&RCC_OscInitStruct); - - RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; - HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4); - - - if (HAL_GetREVID() == 0x1001) - __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); - - //SystemCoreClockUpdate(); - - -} - -