From b925d5817b8b349750754518da2d6aa054db8b18 Mon Sep 17 00:00:00 2001 From: Stone Date: Thu, 23 Jun 2016 03:34:35 +0000 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4Time.h=E7=9A=84=E5=BC=95?= =?UTF-8?q?=E7=94=A8=20STM32F1=E6=8B=86=E5=88=86Pwm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App/Button.cpp | 1 - Device/SerialPort.cpp | 1 - Drivers/74HC165Monitor.h | 1 - Drivers/Esp8266/WaitExpect.cpp | 3 +- Drivers/HX711.cpp | 2 - Drivers/NRF24L01.cpp | 1 - Drivers/SHT30.cpp | 1 - Kernel/WaitHandle.cpp | 1 - Platform/CortexM/Sys.cpp | 1 - Platform/CortexM/Time.cpp | 2 +- Platform/GD32F1x0/RTC.cpp | 1 - Platform/GD32F1x0/SerialPort.cpp | 1 - Platform/STM32F1/Pwm.cpp | 257 +++++++++++++++++++++++++++++++ Platform/STM32F1/RTC.cpp | 2 +- Platform/STM32F1/SerialPort.cpp | 1 - Platform/STM32F1/Timer.cpp | 252 ------------------------------ TinyIP/Arp.cpp | 3 +- TinyIP/Tcp.cpp | 3 +- TokenNet/Device.cpp | 3 +- TokenNet/HelloMessage.cpp | 3 +- TokenNet/TokenController.cpp | 3 +- TokenNet/TokenDevice.cpp | 4 +- TokenNet/TokenMessage.cpp | 3 +- 23 files changed, 267 insertions(+), 283 deletions(-) create mode 100644 Platform/STM32F1/Pwm.cpp diff --git a/App/Button.cpp b/App/Button.cpp index a9a234cd..5d23898e 100644 --- a/App/Button.cpp +++ b/App/Button.cpp @@ -1,5 +1,4 @@ #include "Button.h" -#include "Time.h" InputPort Button::ACZero; int Button::ACZeroAdjTime = 2300; diff --git a/Device/SerialPort.cpp b/Device/SerialPort.cpp index c9a563f6..086d81ec 100644 --- a/Device/SerialPort.cpp +++ b/Device/SerialPort.cpp @@ -2,7 +2,6 @@ #include "Task.h" #include "SerialPort.h" -#include "Time.h" #define COM_DEBUG 0 diff --git a/Drivers/74HC165Monitor.h b/Drivers/74HC165Monitor.h index 3e0073a5..2a940144 100644 --- a/Drivers/74HC165Monitor.h +++ b/Drivers/74HC165Monitor.h @@ -3,7 +3,6 @@ #include "Port.h" #include "Power.h" -#include "Time.h" /* 备注 调用构造函数后 需要Open()一下 diff --git a/Drivers/Esp8266/WaitExpect.cpp b/Drivers/Esp8266/WaitExpect.cpp index f64ad334..e0127f28 100644 --- a/Drivers/Esp8266/WaitExpect.cpp +++ b/Drivers/Esp8266/WaitExpect.cpp @@ -1,5 +1,4 @@ -#include "Time.h" -#include "Sys.h" +#include "Sys.h" #include "WaitExpect.h" diff --git a/Drivers/HX711.cpp b/Drivers/HX711.cpp index c4d9911b..bfebfe9e 100644 --- a/Drivers/HX711.cpp +++ b/Drivers/HX711.cpp @@ -1,7 +1,5 @@ #include "HX711.h" -#include "Time.h" - /* XH711 有两个通道 A B A通道增益可以设置 64/128 diff --git a/Drivers/NRF24L01.cpp b/Drivers/NRF24L01.cpp index 1a624a23..ca22dcf8 100644 --- a/Drivers/NRF24L01.cpp +++ b/Drivers/NRF24L01.cpp @@ -1,5 +1,4 @@ #include "Sys.h" -#include "Time.h" #include "Port.h" #include "NRF24L01.h" diff --git a/Drivers/SHT30.cpp b/Drivers/SHT30.cpp index 9d4ad2e7..80afeb80 100644 --- a/Drivers/SHT30.cpp +++ b/Drivers/SHT30.cpp @@ -1,5 +1,4 @@ #include "SHT30.h" -#include "Time.h" #define LITTLE_ENDIAN diff --git a/Kernel/WaitHandle.cpp b/Kernel/WaitHandle.cpp index 2e97992d..1354434c 100644 --- a/Kernel/WaitHandle.cpp +++ b/Kernel/WaitHandle.cpp @@ -1,5 +1,4 @@ #include "Task.h" -#include "Time.h" #include "WaitHandle.h" diff --git a/Platform/CortexM/Sys.cpp b/Platform/CortexM/Sys.cpp index ff0de574..ccb38d67 100644 --- a/Platform/CortexM/Sys.cpp +++ b/Platform/CortexM/Sys.cpp @@ -1,6 +1,5 @@ #include "Sys.h" -#include "Time.h" #include "WatchDog.h" #include "Platform\stm32.h" diff --git a/Platform/CortexM/Time.cpp b/Platform/CortexM/Time.cpp index c187a3a0..b5c25181 100644 --- a/Platform/CortexM/Time.cpp +++ b/Platform/CortexM/Time.cpp @@ -1,4 +1,4 @@ -#include "Time.h" +#include "TTime.h" #include "Timer.h" #include "Platform\stm32.h" diff --git a/Platform/GD32F1x0/RTC.cpp b/Platform/GD32F1x0/RTC.cpp index 77e18644..0c5c1279 100644 --- a/Platform/GD32F1x0/RTC.cpp +++ b/Platform/GD32F1x0/RTC.cpp @@ -1,5 +1,4 @@ #include "RTC.h" -#include "Time.h" #include "Platform\stm32.h" diff --git a/Platform/GD32F1x0/SerialPort.cpp b/Platform/GD32F1x0/SerialPort.cpp index e8ce7b80..5ecf22a6 100644 --- a/Platform/GD32F1x0/SerialPort.cpp +++ b/Platform/GD32F1x0/SerialPort.cpp @@ -2,7 +2,6 @@ #include "Task.h" #include "SerialPort.h" -#include "Time.h" #include "Platform\stm32.h" diff --git a/Platform/STM32F1/Pwm.cpp b/Platform/STM32F1/Pwm.cpp new file mode 100644 index 00000000..fb54a1a6 --- /dev/null +++ b/Platform/STM32F1/Pwm.cpp @@ -0,0 +1,257 @@ +#include "Sys.h" + +#include "Pwm.h" + +#include "Platform\stm32.h" + +/*================ Pwm ================*/ + +// STM32F030 的 先这么写着 后面再对 103 做调整 +typedef void (*TIM_OCInit)(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct); +const static TIM_OCInit OCInits[4]={TIM_OC1Init, TIM_OC2Init, TIM_OC3Init, TIM_OC4Init}; +// 外部初始化引脚 ?? AFIO很头疼 +/* @arg GPIO_AF_0:TIM15, TIM17, TIM14 + * @arg GPIO_AF_1:Tim3, TIM15 + * @arg GPIO_AF_2:TIM2, TIM1, TIM16, TIM17. + * @arg GPIO_AF_3:TIM15, + * @arg GPIO_AF_4:TIM14. + * @arg GPIO_AF_5:TIM16, TIM17. + * @arg GPIO_AF_6: + * @arg GPIO_AF_7:*/ +//const static uint8_t TIM_CH_AFa[8]= +//{ +//}; + +void Pwm::Config() +{ + TS("Pwm::Config"); + + Timer::Config(); // 主要是配置时钟基础部分 TIM_TimeBaseInit + + TIM_OCInitTypeDef oc; + + TIM_OCStructInit(&oc); + oc.TIM_OCMode = TIM_OCMode_PWM1; + oc.TIM_OutputState = TIM_OutputState_Enable; + oc.TIM_OCPolarity = Polarity ? TIM_OCPolarity_High : TIM_OCPolarity_Low; + oc.TIM_OCIdleState = IdleState ? TIM_OCIdleState_Reset : TIM_OCIdleState_Set; + + auto ti = (TIM_TypeDef*)_Timer; + for(int i=0; i<4; i++) + { + if(Pulse[i] != 0xFFFF) + { + oc.TIM_Pulse = Pulse[i]; + OCInits[i](ti, &oc); + Configed |= 0x1 << i; + } + } + + if(!Pulses) + { + // PWM模式用不上中断 直接就丢了吧 给中断管理减减麻烦 + TIM_ITConfig(ti, TIM_IT_Update, DISABLE); + TIM_ClearFlag(ti, TIM_FLAG_Update); + } + + TIM_SetCounter(ti, 0x00000000); // 清零定时器CNT计数寄存器 + TIM_ARRPreloadConfig(ti, ENABLE); // 使能预装载寄存器ARR + + // 如果需要连续调整宽度,那么需要中断 + if(Pulses) SetHandler(true); +} + +void Pwm::FlushOut() +{ + TIM_OCInitTypeDef oc; + + TIM_OCStructInit(&oc); + oc.TIM_OCMode = TIM_OCMode_PWM1; + oc.TIM_OutputState = TIM_OutputState_Enable; + oc.TIM_OCPolarity = Polarity ? TIM_OCPolarity_High : TIM_OCPolarity_Low; + oc.TIM_OCIdleState = IdleState ? TIM_OCIdleState_Reset : TIM_OCIdleState_Set; + + for(int i=0; i<4; i++) + { + if(Pulse[i] != 0xFFFF) + { + oc.TIM_Pulse = Pulse[i]; + OCInits[i]((TIM_TypeDef*)_Timer, &oc); + Configed |= 0x1 << i; + } + } +} + +void Pwm::Open() +{ + Timer::Open(); + +#if defined(STM32F0) || defined(GD32F150) + if(_index == 0 ||_index == 7||_index == 14 ||_index == 15|| _index == 16) + TIM_CtrlPWMOutputs((TIM_TypeDef*)_Timer, ENABLE); +#endif + +#if defined(STM32F1) + if(_index == 0 ||_index == 14 ||_index == 15|| _index == 16) + TIM_CtrlPWMOutputs((TIM_TypeDef*)_Timer, ENABLE); +#endif + +#if defined(STM32F4) +#endif +} + +void Pwm::Close() +{ +#if defined(STM32F1) || defined(GD32F150) + if(_index == 0 ||_index == 7||_index == 14 ||_index == 15|| _index == 16) + TIM_CtrlPWMOutputs((TIM_TypeDef*)_Timer, DISABLE); +#elif defined(STM32F1) + if(_index == 0 ||_index == 14 ||_index == 15|| _index == 16) + TIM_CtrlPWMOutputs((TIM_TypeDef*)_Timer, DISABLE); +#else //defined(STM32F4) +#endif + + Timer::Close(); +} + +void Pwm::OnInterrupt() +{ + if(!Pulses || !PulseCount || PulseIndex > PulseCount) return; + + auto ti = (TIM_TypeDef*)_Timer; + // 动态计算4个寄存器中的某一个,并设置宽度 +#ifdef STM32F1 + volatile ushort* reg = &(ti->CCR1); +#else + volatile uint* reg = &(ti->CCR1); +#endif + reg += Channel; + + // 发送完成以后,最后一次中断,把占空比调整为一半 + if(PulseIndex >= PulseCount) + { + PulseIndex++; + *reg = Period >> 1; + return; + } + + // 在中断里面切换宽度 + ushort p = Pulses[PulseIndex++]; + + // 设置宽度 + *reg = p; + + // 重复 + if(Repeated && PulseIndex >= PulseCount) PulseIndex = 0; +} + +/* +#ifdef STM32F0 +typedef uint32_t (*GetCap)(TIM_TypeDef* TIMx); +const static GetCap GetCapturex[4]={ +TIM_GetCapture1, +TIM_GetCapture2, +TIM_GetCapture3, +TIM_GetCapture4 +}; +typedef void (*SetICxPres)(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC); +const static SetICxPres SetICPrescaler[]={ +TIM_SetIC1Prescaler , +TIM_SetIC2Prescaler , +TIM_SetIC3Prescaler , +TIM_SetIC4Prescaler , +}; + +Capture::Capture(Timer * timer) +{ + if(timer == nullptr)return ; + tr = timer; +// HaveCap = 0x00; + for(int i =0;i<4;i++) + { + _Handler [i]=nullptr; // 其实可以不赋值 + _Param [i]=nullptr; + } +} + + + +void Capture::Start(int channel) +{ + +} + + +void Capture::Stop(int channel) +{ + +} + + +uint Capture :: GetCapture (int channel) +{ + if(channel >4 || channel <1)return 0; + return (GetCapturex[channel-1])(tr->_Timer ); +} +#endif + +void Capture::Register(int channel,EventHandler handler, void* param ) +{ + if(channel<1||channel>4) return ; + _Handler[channel-1] = handler; + _Param[channel-1]=param; +// if(handler != nullptr) +// tr->Register (OnHandler ,this); + int irq; + if(handler) + { + if(tr ->_index == 0) + irq = TIM1_CC_IRQn; +// else// stm32f103有个TIM8 这里留空 + Interrupt.SetPriority(irq, 1); + Interrupt.Activate(irq, OnHandler, this); + } + else + { + _Handler[channel-1] = nullptr ; + _Param [channel-1]=nullptr; + for(int i =0 ;i<4;i++) + if(_Handler [i] != nullptr )return ; + Interrupt.Deactivate(irq); + } +} + +// 直接用指针访问私有成员 不好 +//void Capture :: OnHandler(void* sender, void* param) +//{ +// Capture * cap= (Capture*)param; +// if(cap->_Handler != nullptr) +// cap->_Handler(sender,cap->_Param ); +//} + +void Capture :: OnHandler(ushort num, void* param) +{ + Capture * cap= (Capture*)param; + if(cap != nullptr) + cap->OnInterrupt(); +} + +void Capture::OnInterrupt() +{ + // 找出中断源 + ushort ccx = TIM_FLAG_CC1; + for(int i =0;i<4;i++) + { + if(TIM_GetFlagStatus(tr->_Timer , ccx<OnInterrupt(); } } - -/*================ Pwm ================*/ - -// STM32F030 的 先这么写着 后面再对 103 做调整 -typedef void (*TIM_OCInit)(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct); -const static TIM_OCInit OCInits[4]={TIM_OC1Init, TIM_OC2Init, TIM_OC3Init, TIM_OC4Init}; -// 外部初始化引脚 ?? AFIO很头疼 -/* @arg GPIO_AF_0:TIM15, TIM17, TIM14 - * @arg GPIO_AF_1:Tim3, TIM15 - * @arg GPIO_AF_2:TIM2, TIM1, TIM16, TIM17. - * @arg GPIO_AF_3:TIM15, - * @arg GPIO_AF_4:TIM14. - * @arg GPIO_AF_5:TIM16, TIM17. - * @arg GPIO_AF_6: - * @arg GPIO_AF_7:*/ -//const static uint8_t TIM_CH_AFa[8]= -//{ -//}; - -void Pwm::Config() -{ - TS("Pwm::Config"); - - Timer::Config(); // 主要是配置时钟基础部分 TIM_TimeBaseInit - - TIM_OCInitTypeDef oc; - - TIM_OCStructInit(&oc); - oc.TIM_OCMode = TIM_OCMode_PWM1; - oc.TIM_OutputState = TIM_OutputState_Enable; - oc.TIM_OCPolarity = Polarity ? TIM_OCPolarity_High : TIM_OCPolarity_Low; - oc.TIM_OCIdleState = IdleState ? TIM_OCIdleState_Reset : TIM_OCIdleState_Set; - - auto ti = (TIM_TypeDef*)_Timer; - for(int i=0; i<4; i++) - { - if(Pulse[i] != 0xFFFF) - { - oc.TIM_Pulse = Pulse[i]; - OCInits[i](ti, &oc); - Configed |= 0x1 << i; - } - } - - if(!Pulses) - { - // PWM模式用不上中断 直接就丢了吧 给中断管理减减麻烦 - TIM_ITConfig(ti, TIM_IT_Update, DISABLE); - TIM_ClearFlag(ti, TIM_FLAG_Update); - } - - TIM_SetCounter(ti, 0x00000000); // 清零定时器CNT计数寄存器 - TIM_ARRPreloadConfig(ti, ENABLE); // 使能预装载寄存器ARR - - // 如果需要连续调整宽度,那么需要中断 - if(Pulses) SetHandler(true); -} - -void Pwm::FlushOut() -{ - TIM_OCInitTypeDef oc; - - TIM_OCStructInit(&oc); - oc.TIM_OCMode = TIM_OCMode_PWM1; - oc.TIM_OutputState = TIM_OutputState_Enable; - oc.TIM_OCPolarity = Polarity ? TIM_OCPolarity_High : TIM_OCPolarity_Low; - oc.TIM_OCIdleState = IdleState ? TIM_OCIdleState_Reset : TIM_OCIdleState_Set; - - for(int i=0; i<4; i++) - { - if(Pulse[i] != 0xFFFF) - { - oc.TIM_Pulse = Pulse[i]; - OCInits[i]((TIM_TypeDef*)_Timer, &oc); - Configed |= 0x1 << i; - } - } -} - -void Pwm::Open() -{ - Timer::Open(); - -#if defined(STM32F0) || defined(GD32F150) - if(_index == 0 ||_index == 7||_index == 14 ||_index == 15|| _index == 16) - TIM_CtrlPWMOutputs((TIM_TypeDef*)_Timer, ENABLE); -#endif - -#if defined(STM32F1) - if(_index == 0 ||_index == 14 ||_index == 15|| _index == 16) - TIM_CtrlPWMOutputs((TIM_TypeDef*)_Timer, ENABLE); -#endif - -#if defined(STM32F4) -#endif -} - -void Pwm::Close() -{ -#if defined(STM32F1) || defined(GD32F150) - if(_index == 0 ||_index == 7||_index == 14 ||_index == 15|| _index == 16) - TIM_CtrlPWMOutputs((TIM_TypeDef*)_Timer, DISABLE); -#elif defined(STM32F1) - if(_index == 0 ||_index == 14 ||_index == 15|| _index == 16) - TIM_CtrlPWMOutputs((TIM_TypeDef*)_Timer, DISABLE); -#else //defined(STM32F4) -#endif - - Timer::Close(); -} - -void Pwm::OnInterrupt() -{ - if(!Pulses || !PulseCount || PulseIndex > PulseCount) return; - - auto ti = (TIM_TypeDef*)_Timer; - // 动态计算4个寄存器中的某一个,并设置宽度 -#ifdef STM32F1 - volatile ushort* reg = &(ti->CCR1); -#else - volatile uint* reg = &(ti->CCR1); -#endif - reg += Channel; - - // 发送完成以后,最后一次中断,把占空比调整为一半 - if(PulseIndex >= PulseCount) - { - PulseIndex++; - *reg = Period >> 1; - return; - } - - // 在中断里面切换宽度 - ushort p = Pulses[PulseIndex++]; - - // 设置宽度 - *reg = p; - - // 重复 - if(Repeated && PulseIndex >= PulseCount) PulseIndex = 0; -} - -/* -#ifdef STM32F0 -typedef uint32_t (*GetCap)(TIM_TypeDef* TIMx); -const static GetCap GetCapturex[4]={ -TIM_GetCapture1, -TIM_GetCapture2, -TIM_GetCapture3, -TIM_GetCapture4 -}; -typedef void (*SetICxPres)(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC); -const static SetICxPres SetICPrescaler[]={ -TIM_SetIC1Prescaler , -TIM_SetIC2Prescaler , -TIM_SetIC3Prescaler , -TIM_SetIC4Prescaler , -}; - -Capture::Capture(Timer * timer) -{ - if(timer == nullptr)return ; - tr = timer; -// HaveCap = 0x00; - for(int i =0;i<4;i++) - { - _Handler [i]=nullptr; // 其实可以不赋值 - _Param [i]=nullptr; - } -} - - - -void Capture::Start(int channel) -{ - -} - - -void Capture::Stop(int channel) -{ - -} - - -uint Capture :: GetCapture (int channel) -{ - if(channel >4 || channel <1)return 0; - return (GetCapturex[channel-1])(tr->_Timer ); -} -#endif - -void Capture::Register(int channel,EventHandler handler, void* param ) -{ - if(channel<1||channel>4) return ; - _Handler[channel-1] = handler; - _Param[channel-1]=param; -// if(handler != nullptr) -// tr->Register (OnHandler ,this); - int irq; - if(handler) - { - if(tr ->_index == 0) - irq = TIM1_CC_IRQn; -// else// stm32f103有个TIM8 这里留空 - Interrupt.SetPriority(irq, 1); - Interrupt.Activate(irq, OnHandler, this); - } - else - { - _Handler[channel-1] = nullptr ; - _Param [channel-1]=nullptr; - for(int i =0 ;i<4;i++) - if(_Handler [i] != nullptr )return ; - Interrupt.Deactivate(irq); - } -} - -// 直接用指针访问私有成员 不好 -//void Capture :: OnHandler(void* sender, void* param) -//{ -// Capture * cap= (Capture*)param; -// if(cap->_Handler != nullptr) -// cap->_Handler(sender,cap->_Param ); -//} - -void Capture :: OnHandler(ushort num, void* param) -{ - Capture * cap= (Capture*)param; - if(cap != nullptr) - cap->OnInterrupt(); -} - -void Capture::OnInterrupt() -{ - // 找出中断源 - ushort ccx = TIM_FLAG_CC1; - for(int i =0;i<4;i++) - { - if(TIM_GetFlagStatus(tr->_Timer , ccx<