`#include "debug.h"
/* CH1CVR register Definition */
/* Private variables */
/*********************************************************************
- @fn TIM1_PWMOut_Init
*
- @brief Initializes TIM1 PWM output.
*
- @param arr - the period value.
- psc - the prescaler value.
- ccp - the pulse value.
*
- @return none
*/
void TIM1_Init()
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure); //CH3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure); //CH4
}
void TIM3_Init()
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //CH3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //CH4
}
void TIM1_PWMOut_IniTCH3(u16 arr, u16 duty)
{
TIM_OCInitTypeDef TIM_OCInitStructure = {0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE );
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = duty;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1, ENABLE );
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
}
void TIM1_PWMOut_IniTCH4(u16 arr, u16 duty)
{
TIM_OCInitTypeDef TIM_OCInitStructure = {0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE );
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = duty;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1, ENABLE );
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
}
void TIM3_PWMOut_IniTCH3(u16 arr, u16 duty)
{
TIM_OCInitTypeDef TIM_OCInitStructure = {0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE );
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = duty;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM3, ENABLE );
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
void TIM3_PWMOut_IniTCH4(u16 arr, u16 duty)
{
TIM_OCInitTypeDef TIM_OCInitStructure = {0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE );
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = 0;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = duty;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM3, ENABLE );
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
/*********************************************************************
- @fn TIM1_DMA_Init
*
- @brief Initializes the TIM DMAy Channelx configuration.
*
- @param DMA_CHx -
- x can be 1 to 7.
- ppadr - Peripheral base address.
- memadr - Memory base address.
- bufsize - DMA channel buffer size.
*
- @return none
*/
/*********************************************************************
- @fn main
*
- @brief Main program.
*
- @return none
/
#define RXBUF_SIZE 1024 // DMA buffer size
#define size(a) (sizeof(a) / sizeof((a)))
/* Global Variable */
u8 TxBuffer[] = " ";
u8 RxBuffer[RXBUF_SIZE]={0};
/*******************************************************************************
Function Name : USARTx_CFG
Description : Initializes the USART peripheral.
描述 : 串口初始化
Input : None
Return : None
*******************************************************************************/
void USARTx_CFG(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//开启时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART7, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/* USART7 TX-->C2 RX-->C3 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //RX,输入上拉
GPIO_Init(GPIOC, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200; // 波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 数据位 8
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位 1
USART_InitStructure.USART_Parity = USART_Parity_No; // 无校验
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件流控
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //使能 RX 和 TX
USART_Init(UART7, &USART_InitStructure);
DMA_Cmd(DMA2_Channel9, ENABLE); //开启接收 DMA
USART_Cmd(UART7, ENABLE); //开启UART
}
/*******************************************************************************
Function Name : DMA_INIT
Description : Configures the DMA.
描述 : DMA 初始化
Input : None
Return : None
*******************************************************************************/
void DMA_INIT(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
// TX DMA 初始化
DMA_DeInit(DMA2_Channel8);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&UART7->DATAR); // DMA 外设基址,需指向对应的外设
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)TxBuffer; // DMA 内存基址,指向发送缓冲区的首地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // 方向 : 外设 作为 终点,即 内存 -> 外设
DMA_InitStructure.DMA_BufferSize = 0; // 缓冲区大小,即要DMA发送的数据长度,目前没有数据可发
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外设地址自增,禁用
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // 内存地址自增,启用
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; // 外设数据位宽,8位(Byte)
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; // 内存数据位宽,8位(Byte)
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // 普通模式,发完结束,不循环发送
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; // 优先级最高
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; // M2P,禁用M2M
DMA_Init(DMA2_Channel8, &DMA_InitStructure);
// RX DMA 初始化,环形缓冲区自动接收
DMA_DeInit(DMA2_Channel9);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&UART7->DATAR);
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)RxBuffer; // 接收缓冲区
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // 方向 : 外设 作为 源,即 内存 <- 外设
DMA_InitStructure.DMA_BufferSize = RXBUF_SIZE; // 缓冲区长度为 RXBUF_SIZE
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // 循环模式,构成环形缓冲区
DMA_Init(DMA2_Channel9, &DMA_InitStructure);
}
/*******************************************************************************
Function Name : GPIO_CFG
Description : Initializes GPIOs.
描述 : GPIO 初始化
Input : None
Return : None
****************************************************************************/
void GPIO_CFG(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// CH9141 配置引脚初始化
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/ BLE_sleep --> C13 BLE_AT-->A7 /
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/****************************************************************************
Function Name : uartWriteBLE
Description : send data to BLE via UART7 向蓝牙模组发送数据
Input : char * data data to send 要发送的数据的首地址
uint16_t num number of data 数据长度
Return : RESET UART7 busy,failed to send 发送失败
SET send success 发送成功
*******************************************************************************/
FlagStatus uartWriteBLE(char * data , uint16_t num)
{
//如上次发送未完成,返回
if(DMA_GetCurrDataCounter(DMA2_Channel8) != 0){
return RESET;
}
DMA_ClearFlag(DMA2_FLAG_TC8);
DMA_Cmd(DMA2_Channel8, DISABLE ); // 关 DMA 后操作
DMA2_Channel8->MADDR = (uint32_t)data; // 发送缓冲区为 data
DMA_SetCurrDataCounter(DMA2_Channel8,num); // 设置缓冲区长度
DMA_Cmd(DMA2_Channel8, ENABLE); // 开 DMA
return SET;
}
/*******************************************************************************
- Function Name : uartWriteBLEstr
- Description : send string to BLE via UART7 向蓝牙模组发送字符串
- Input : char * str string to send
- Return : RESET UART7 busy,failed to send 发送失败
- SET send success 发送成功
*******************************************************************************/
FlagStatus uartWriteBLEstr(char * str)
{
uint16_t num = 0;
while(str[num])num++; // 计算字符串长度
return uartWriteBLE(str,num);
}
/*******************************************************************************
Function Name : uartReadBLE
Description : read some bytes from receive buffer 从接收缓冲区读出一组数据
Input : char * buffer buffer to storage the data 用来存放读出数据的地址
uint16_t num number of data to read 要读的字节数
Return : int number of bytes read 返回实际读出的字节数
*******************************************************************************/
uint16_t rxBufferReadPos = 0; //接收缓冲区读指针
uint32_t uartReadBLE(char * buffer , uint16_t num)
{
uint16_t rxBufferEnd = RXBUF_SIZE - DMA_GetCurrDataCounter(DMA2_Channel9); //计算 DMA 数据尾的位置
uint16_t i = 0;
if (rxBufferReadPos == rxBufferEnd){
// 无数据,返回
return 0;
}
while (rxBufferReadPos!=rxBufferEnd && i < num){
buffer = RxBuffer[rxBufferReadPos];
i++;
rxBufferReadPos++;
if(rxBufferReadPos >= RXBUF_SIZE){
// 超出缓冲区,回零
rxBufferReadPos = 0;
}
}
return i;
}
/*******************************************************************************
- Function Name : uartReadByteBLE
- Description : read one byte from UART buffer 从接收缓冲区读出 1 字节数据
- Input : None
- Return : char read data 返回读出的数据(无数据也返回0)
****************************************************************************/
char uartReadByteBLE()
{
char ret;
uint16_t rxBufferEnd = RXBUF_SIZE - DMA_GetCurrDataCounter(DMA2_Channel9);//计算 DMA 数据尾的位置
if (rxBufferReadPos == rxBufferEnd){
// 无数据,返回
return 0;
}
ret = RxBuffer[rxBufferReadPos];
rxBufferReadPos++;
if(rxBufferReadPos >= RXBUF_SIZE){
// 超出缓冲区,回零
rxBufferReadPos = 0;
}
return ret;
}
/****************************************************************************
- Function Name : uartAvailableBLE
- Description : get number of bytes Available to read from the UART buffer 获取缓冲区中可读数据的数量
- Input : None
- Return : uint16_t number of bytes Available to readd 返回可读数据数量
*******************************************************************************/
uint16_t uartAvailableBLE()
{
uint16_t rxBufferEnd = RXBUF_SIZE - DMA_GetCurrDataCounter(DMA2_Channel9);//计算 DMA 数据尾的位置
// 计算可读字节
if (rxBufferReadPos <= rxBufferEnd){
return rxBufferEnd - rxBufferReadPos;
}else{
return rxBufferEnd +RXBUF_SIZE -rxBufferReadPos;
}
}
int main(void)
{
u8 e = 250;
SystemCoreClockUpdate();
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n",SystemCoreClock);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Delay_Init();
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n",SystemCoreClock);
DMA_INIT();
USARTx_CFG(); /* USART INIT */
USART_DMACmd(UART7,USART_DMAReq_Tx|USART_DMAReq_Rx,ENABLE);
GPIO_CFG();
// GPIO_WriteBit(GPIOA, GPIO_Pin_7,RESET); //进入 AT
GPIO_WriteBit(GPIOC, GPIO_Pin_13,SET); //enable CH9141
Delay_Ms(1000);
//uartReadByteBLE();
//uartAvailableBLE();
TIM1_Init();
TIM3_Init();
while(1)
{
char buffer[1024]={"\0"};
int num = uartAvailableBLE();
uartReadBLE(buffer , num);
//TIM1_PWMOut_IniTCH3(5000,e-50);TIM1_PWMOut_IniTCH4(5000,0);TIM3_PWMOut_IniTCH3(5000,e+100);TIM3_PWMOut_IniTCH4(5000,0);
switch (num)
{
case 0x01:TIM1_PWMOut_IniTCH3(5000,e-50);TIM1_PWMOut_IniTCH4(5000,e-50);TIM3_PWMOut_IniTCH3(5000,e+100);TIM3_PWMOut_IniTCH4(5000,e+100);break;
case 0x02:TIM1_PWMOut_IniTCH3(5000,e-50);TIM1_PWMOut_IniTCH4(5000,e-50);TIM3_PWMOut_IniTCH3(5000,0);TIM3_PWMOut_IniTCH4(5000,0);break;
case 0x03:TIM1_PWMOut_IniTCH3(5000,0);TIM1_PWMOut_IniTCH4(5000,0);TIM3_PWMOut_IniTCH3(5000,e+100);TIM3_PWMOut_IniTCH4(5000,e+100);break;
case 0x04:TIM1_PWMOut_IniTCH3(5000,e-50);TIM1_PWMOut_IniTCH4(5000,0);TIM3_PWMOut_IniTCH3(5000,e+100);TIM3_PWMOut_IniTCH4(5000,0);break;
case 0x05:TIM1_PWMOut_IniTCH3(5000,0);TIM1_PWMOut_IniTCH4(5000,0);TIM3_PWMOut_IniTCH3(5000,0);TIM3_PWMOut_IniTCH4(5000,0);break;
case 0x06:e+= 25;break;
case 0x07:e-= 25;break;
default:break;
}
GPIO_WriteBit(GPIOA, GPIO_Pin_7,SET);
}
}
`