changeset 4:ca4f5b55b391

working on pwm
author cin
date Wed, 18 Jan 2017 03:27:00 +0300
parents 3d9705e842f8
children 0d3eea2dd7ea
files l476rg-hal-test/.settings/language.settings.xml l476rg-hal-test/src/main.cpp l476rg/.settings/language.settings.xml l476rg/Src/main.c l476rg/Src/stm32l4xx_hal_timebase_TIM.c l476rg/l476rg.ioc
diffstat 6 files changed, 86 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/l476rg-hal-test/.settings/language.settings.xml	Wed Jan 18 01:07:59 2017 +0300
+++ b/l476rg-hal-test/.settings/language.settings.xml	Wed Jan 18 03:27:00 2017 +0300
@@ -4,7 +4,7 @@
 		<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
 			<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
 			<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
-			<provider class="com.atollic.truestudio.mbs.GCCSpecsDetectorAtollicArm" console="false" env-hash="1885092277368706637" id="com.atollic.truestudio.mbs.provider" keep-relative-paths="false" name="Atollic ARM Tools Language Settings" parameter="${COMMAND} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
+			<provider class="com.atollic.truestudio.mbs.GCCSpecsDetectorAtollicArm" console="false" env-hash="940512069781223905" id="com.atollic.truestudio.mbs.provider" keep-relative-paths="false" name="Atollic ARM Tools Language Settings" parameter="${COMMAND} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 				<language-scope id="org.eclipse.cdt.core.gcc"/>
 				<language-scope id="org.eclipse.cdt.core.g++"/>
 			</provider>
@@ -14,7 +14,7 @@
 		<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
 			<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
 			<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
-			<provider class="com.atollic.truestudio.mbs.GCCSpecsDetectorAtollicArm" console="false" env-hash="1885092277368706637" id="com.atollic.truestudio.mbs.provider" keep-relative-paths="false" name="Atollic ARM Tools Language Settings" parameter="${COMMAND} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
+			<provider class="com.atollic.truestudio.mbs.GCCSpecsDetectorAtollicArm" console="false" env-hash="940512069781223905" id="com.atollic.truestudio.mbs.provider" keep-relative-paths="false" name="Atollic ARM Tools Language Settings" parameter="${COMMAND} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 				<language-scope id="org.eclipse.cdt.core.gcc"/>
 				<language-scope id="org.eclipse.cdt.core.g++"/>
 			</provider>
--- a/l476rg-hal-test/src/main.cpp	Wed Jan 18 01:07:59 2017 +0300
+++ b/l476rg-hal-test/src/main.cpp	Wed Jan 18 03:27:00 2017 +0300
@@ -45,6 +45,51 @@
 
 using namespace halpp;
 
+template <typename TDev> class TTimerTraits {
+public:
+	static void SetPeriod(uint32_t period) {
+	}
+
+	static void SetPrescaler(uint32_t prescaler) {
+	}
+};
+
+template <typename TDev> class TGenTimerTraits : public TTimerTraits<TDev> {
+public:
+	static void SetDuty(uint32_t duty) {
+		TDev::instance().CCR1 = duty;
+	}
+};
+
+template <
+	typename TGpioDev,
+	unsigned short Pin,
+	typename TTimerDev,
+	unsigned short Ch,
+	typename TGpioTraits = TGpioTraits<TGpioDev>,
+	typename TTimerTraits = TGenTimerTraits<TTimerDev>
+> class TPwmOut {
+	static uint32_t pwmPeriod;
+public:
+	static void Init(uint32_t freq) {
+		pwmPeriod = TTimerDev::GetFreq()/freq - 1;
+		TTimerTraits::SetPeriod(pwmPeriod);
+	}
+
+	static void SetDutyFactor(float f) {
+		TTimerTraits::SetDuty(pwmPeriod*f);
+	}
+};
+
+template <
+	typename TGpioDev,
+	unsigned short Pin,
+	typename TTimerDev,
+	unsigned short Ch,
+	typename TGpioTraits,
+	typename TTimerTraits
+> uint32_t TPwmOut<TGpioDev, Pin, TTimerDev, Ch, TGpioTraits, TTimerTraits>::pwmPeriod = 0;
+
 template<typename TDev, unsigned short PIN, typename TTraits = TGpioTraits<TDev> > class TLed {
 	TLed();
 	explicit TLed(const TLed&);
@@ -68,32 +113,6 @@
 	}
 };
 
-template<typename TDev, typename TTraits = TGpioTraits<TDev> > class TLedInst {
-	const unsigned short m_ledNo;
-
-	explicit TLedInst(const TLedInst&);
-public:
-	TLedInst(unsigned short led) : m_ledNo(led) {
-	}
-	void Init() const {
-		TTraits::SetPinMode(m_ledNo, GpioModeOutput);
-		TTraits::SetPinOutputType(m_ledNo, GpioPushPull);
-		TTraits::SetPinPullMode(m_ledNo, GpioNoPull);
-	}
-
-	void Set() const {
-		TTraits::WritePin(m_ledNo, 1);
-	}
-
-	void Reset() const {
-		TTraits::WritePin(m_ledNo, 0);
-	}
-
-	void Toogle() const {
-		TTraits::TooglePin(m_ledNo);
-	}
-};
-
 class AHB2Bus {
 public:
 	static void Enable(unsigned int flag) {
@@ -119,6 +138,10 @@
 	static void Enable() {
 		TBus::Enable(ENF);
 	}
+
+	static uint32_t GetFreq() {
+		return 80000000;
+	}
 };
 
 class Hardware {
@@ -130,21 +153,18 @@
 		}
 	};
 
-	class Tim3: public TDevice<AHB1Bus, RCC_APB1ENR1_TIM3EN> {
+	class Tim3: public TDevice<APB1Bus, RCC_APB1ENR1_TIM3EN> {
 	public:
 		static TIM_TypeDef& instance() {
 			return *TIM3;
 		}
 	};
 
-	static const TLedInst<GpioA> greenLed;
-	static const TLedInst<GpioA> led2;
-
+	typedef TLed<GpioA, 5> GreenLed;
+	typedef TLed<GpioA, 6> Led2;
+	typedef TPwmOut<GpioA, 7, Tim3, 2> PwmLed3;
 };
 
-const TLedInst<Hardware::GpioA> Hardware::greenLed(5);
-const TLedInst<Hardware::GpioA> Hardware::led2(5);
-
 int main(void) {
 
 	/**
@@ -160,15 +180,23 @@
 	/* TODO - Add your application code here */
 
 	Hardware::GpioA::Enable();
-	Hardware::greenLed.Init();
-	Hardware::led2.Init();
+	Hardware::Tim3::Enable();
+	Hardware::GreenLed::Init();
+	Hardware::Led2::Init();
+	Hardware::PwmLed3::Init(1000);
 
+	int ii = 0;
 	/* Infinite loop */
 	while (1) {
-		Hardware::greenLed.Toogle();
-		Hardware::led2.Toogle();
+		Hardware::GreenLed::Toogle();
+		Hardware::Led2::Toogle();
+
+		Hardware::PwmLed3::SetDutyFactor(ii / 10.f);
+
+		ii = (ii+1) % 11; // 0 .. 10
 
 		for (int i = 0; i < 1000000; i++) {
+
 		}
 	}
 }
--- a/l476rg/.settings/language.settings.xml	Wed Jan 18 01:07:59 2017 +0300
+++ b/l476rg/.settings/language.settings.xml	Wed Jan 18 03:27:00 2017 +0300
@@ -4,7 +4,7 @@
 		<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
 			<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
 			<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
-			<provider class="com.atollic.truestudio.mbs.GCCSpecsDetectorAtollicArm" console="false" env-hash="1793193066533405323" id="com.atollic.truestudio.mbs.provider" keep-relative-paths="false" name="Atollic ARM Tools Language Settings" parameter="${COMMAND} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
+			<provider class="com.atollic.truestudio.mbs.GCCSpecsDetectorAtollicArm" console="false" env-hash="922121010244122911" id="com.atollic.truestudio.mbs.provider" keep-relative-paths="false" name="Atollic ARM Tools Language Settings" parameter="${COMMAND} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
 				<language-scope id="org.eclipse.cdt.core.gcc"/>
 				<language-scope id="org.eclipse.cdt.core.g++"/>
 			</provider>
--- a/l476rg/Src/main.c	Wed Jan 18 01:07:59 2017 +0300
+++ b/l476rg/Src/main.c	Wed Jan 18 03:27:00 2017 +0300
@@ -128,7 +128,7 @@
                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
-  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
+  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
--- a/l476rg/Src/stm32l4xx_hal_timebase_TIM.c	Wed Jan 18 01:07:59 2017 +0300
+++ b/l476rg/Src/stm32l4xx_hal_timebase_TIM.c	Wed Jan 18 03:27:00 2017 +0300
@@ -80,7 +80,7 @@
   HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
   
   /* Compute TIM6 clock */
-  uwTimclock = HAL_RCC_GetPCLK1Freq();
+  uwTimclock = 2*HAL_RCC_GetPCLK1Freq();
    
   /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
   uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1);
--- a/l476rg/l476rg.ioc	Wed Jan 18 01:07:59 2017 +0300
+++ b/l476rg/l476rg.ioc	Wed Jan 18 03:27:00 2017 +0300
@@ -121,24 +121,25 @@
 ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL,2-SystemClock_Config-RCC-false-HAL,3-MX_USART2_UART_Init-USART2-false-HAL
 RCC.ADCFreq_Value=64000000
 RCC.AHBFreq_Value=80000000
-RCC.APB1Freq_Value=80000000
+RCC.APB1CLKDivider=RCC_HCLK_DIV2
+RCC.APB1Freq_Value=40000000
 RCC.APB1TimFreq_Value=80000000
 RCC.APB2Freq_Value=80000000
 RCC.APB2TimFreq_Value=80000000
 RCC.CortexFreq_Value=80000000
-RCC.DFSDMFreq_Value=80000000
+RCC.DFSDMFreq_Value=40000000
 RCC.FCLKCortexFreq_Value=80000000
 RCC.FamilyName=M
 RCC.HCLKFreq_Value=80000000
 RCC.HSE_VALUE=8000000
 RCC.HSI_VALUE=16000000
-RCC.I2C1Freq_Value=80000000
-RCC.I2C2Freq_Value=80000000
-RCC.I2C3Freq_Value=80000000
-RCC.IPParameters=ADCFreq_Value,AHBFreq_Value,APB1Freq_Value,APB1TimFreq_Value,APB2Freq_Value,APB2TimFreq_Value,CortexFreq_Value,DFSDMFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,HSE_VALUE,HSI_VALUE,I2C1Freq_Value,I2C2Freq_Value,I2C3Freq_Value,LPTIM1Freq_Value,LPTIM2Freq_Value,LPUART1Freq_Value,LSCOPinFreq_Value,LSI_VALUE,MCO1PinFreq_Value,MSI_VALUE,PLLN,PLLPoutputFreq_Value,PLLQoutputFreq_Value,PLLRCLKFreq_Value,PLLSAI1PoutputFreq_Value,PLLSAI1QoutputFreq_Value,PLLSAI1RoutputFreq_Value,PLLSAI2PoutputFreq_Value,PLLSAI2RoutputFreq_Value,PLLSourceVirtual,PREFETCH_ENABLE,PWRFreq_Value,RNGFreq_Value,SAI1Freq_Value,SAI2Freq_Value,SDMMCFreq_Value,SWPMI1Freq_Value,SYSCLKFreq_VALUE,SYSCLKSource,UART4Freq_Value,UART5Freq_Value,USART1Freq_Value,USART2Freq_Value,USART3Freq_Value,USBFreq_Value,VCOInputFreq_Value,VCOOutputFreq_Value,VCOSAI1OutputFreq_Value,VCOSAI2OutputFreq_Value
-RCC.LPTIM1Freq_Value=80000000
-RCC.LPTIM2Freq_Value=80000000
-RCC.LPUART1Freq_Value=80000000
+RCC.I2C1Freq_Value=40000000
+RCC.I2C2Freq_Value=40000000
+RCC.I2C3Freq_Value=40000000
+RCC.IPParameters=ADCFreq_Value,AHBFreq_Value,APB1CLKDivider,APB1Freq_Value,APB1TimFreq_Value,APB2Freq_Value,APB2TimFreq_Value,CortexFreq_Value,DFSDMFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,HSE_VALUE,HSI_VALUE,I2C1Freq_Value,I2C2Freq_Value,I2C3Freq_Value,LPTIM1Freq_Value,LPTIM2Freq_Value,LPUART1Freq_Value,LSCOPinFreq_Value,LSI_VALUE,MCO1PinFreq_Value,MSI_VALUE,PLLN,PLLPoutputFreq_Value,PLLQoutputFreq_Value,PLLRCLKFreq_Value,PLLSAI1PoutputFreq_Value,PLLSAI1QoutputFreq_Value,PLLSAI1RoutputFreq_Value,PLLSAI2PoutputFreq_Value,PLLSAI2RoutputFreq_Value,PLLSourceVirtual,PREFETCH_ENABLE,PWRFreq_Value,RNGFreq_Value,SAI1Freq_Value,SAI2Freq_Value,SDMMCFreq_Value,SWPMI1Freq_Value,SYSCLKFreq_VALUE,SYSCLKSource,UART4Freq_Value,UART5Freq_Value,USART1Freq_Value,USART2CLockSelection,USART2Freq_Value,USART3Freq_Value,USBFreq_Value,VCOInputFreq_Value,VCOOutputFreq_Value,VCOSAI1OutputFreq_Value,VCOSAI2OutputFreq_Value
+RCC.LPTIM1Freq_Value=40000000
+RCC.LPTIM2Freq_Value=40000000
+RCC.LPUART1Freq_Value=40000000
 RCC.LSCOPinFreq_Value=32000
 RCC.LSI_VALUE=32000
 RCC.MCO1PinFreq_Value=80000000
@@ -159,14 +160,15 @@
 RCC.SAI1Freq_Value=18285714.285714287
 RCC.SAI2Freq_Value=18285714.285714287
 RCC.SDMMCFreq_Value=64000000
-RCC.SWPMI1Freq_Value=80000000
+RCC.SWPMI1Freq_Value=40000000
 RCC.SYSCLKFreq_VALUE=80000000
 RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK
-RCC.UART4Freq_Value=80000000
-RCC.UART5Freq_Value=80000000
+RCC.UART4Freq_Value=40000000
+RCC.UART5Freq_Value=40000000
 RCC.USART1Freq_Value=80000000
-RCC.USART2Freq_Value=80000000
-RCC.USART3Freq_Value=80000000
+RCC.USART2CLockSelection=RCC_USART2CLKSOURCE_HSI
+RCC.USART2Freq_Value=16000000
+RCC.USART3Freq_Value=40000000
 RCC.USBFreq_Value=64000000
 RCC.VCOInputFreq_Value=16000000
 RCC.VCOOutputFreq_Value=160000000