(改正)关于不精确定时的延时时间计算方法(不利用TIM,用有限空循环延时)

软件教程xddl 回复了问题 • 2 人关注 • 2 个回复 • 1730 次浏览 • 2015-03-03 09:52 • 来自相关话题

新手学习一点心得

新手交流整个夏天 发表了文章 • 6 个评论 • 1407 次浏览 • 2015-03-01 17:19 • 来自相关话题

分享一点心得。学习stm32的时候就被一堆寄存器搞晕了,就在想为什么不开发一个软件来自动配置寄存器呢,这完全是可以是实现的,直到一天知道了这个软件。
但慢慢的发现资料太少了,hal库还是全英文的,也没有几个函数,外设配置好了就不知道该怎么办了。痛苦了几天之后找到了一个办法——操作寄存器。
像51一样,st也对所有的寄存器进行了封装,所以可以方便的访问每一个寄存器。因为cube已经帮我们配置好了,我们就只管我们需要的寄存器就行,工作量很小了。比如说串口,配置好之后,一般就是收发数据,所以只要找到它






就是USART1->DR,读它就是接收到数据,写值就是发送,跟51用法一样。
比如GPIO






GPIOA->IDR就是输入状态,ODR就是控制输出,写0xff就全高电平,写0x00就全低电平,跟51也一样了。
其他的定时器什么的也大致如此。如果你是51转战过来的,你在51上的所有试验都可以毫无障碍的移植到stm32上,学习进度可以加快不少。
附stm32中文寄存器手册http://pan.baidu.com/s/1hqKPHXE 查看全部
分享一点心得。学习stm32的时候就被一堆寄存器搞晕了,就在想为什么不开发一个软件来自动配置寄存器呢,这完全是可以是实现的,直到一天知道了这个软件。
但慢慢的发现资料太少了,hal库还是全英文的,也没有几个函数,外设配置好了就不知道该怎么办了。痛苦了几天之后找到了一个办法——操作寄存器。
像51一样,st也对所有的寄存器进行了封装,所以可以方便的访问每一个寄存器。因为cube已经帮我们配置好了,我们就只管我们需要的寄存器就行,工作量很小了。比如说串口,配置好之后,一般就是收发数据,所以只要找到它

360桌面截图20150301153938.jpg


就是USART1->DR,读它就是接收到数据,写值就是发送,跟51用法一样。
比如GPIO

360桌面截图20150301170457.jpg


GPIOA->IDR就是输入状态,ODR就是控制输出,写0xff就全高电平,写0x00就全低电平,跟51也一样了。
其他的定时器什么的也大致如此。如果你是51转战过来的,你在51上的所有试验都可以毫无障碍的移植到stm32上,学习进度可以加快不少。
附stm32中文寄存器手册http://pan.baidu.com/s/1hqKPHXE

刚装好了f1的库。生成了adc的配置。然后没头绪了

问题困惑中子星 回复了问题 • 3 人关注 • 3 个回复 • 1167 次浏览 • 2015-03-01 08:24 • 来自相关话题

f103输入捕获模式红外解码

经验分享整个夏天 发表了文章 • 12 个评论 • 1668 次浏览 • 2015-02-28 21:10 • 来自相关话题

用定时器输入捕获模式做了一个红外解码
用定时器输入捕获模式做了一个红外解码

你们确定你的stm32cubemx的4.6.0版本能用?F4的时钟树外部晶振始终修改?

回复

问题困惑popdes 发起了问题 • 1 人关注 • 0 个回复 • 1716 次浏览 • 2015-02-28 20:20 • 来自相关话题

cube i2c的一个bug

经验分享日日夜夜 发表了文章 • 7 个评论 • 3332 次浏览 • 2015-02-26 00:18 • 来自相关话题

这两天用stm32f103 调i2c 发现cube忘记给i2c引脚加输出模式了 各位一定要小心啊
/ USER CODE BEGIN I2C1_MspInit 0 /

// __HAL_RCC_AFIO_CLK_ENABLE();
/ USER CODE END I2C1_MspInit 0 /
/ Peripheral clock enable /
__I2C1_CLK_ENABLE();

/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
上面是源代码

在那个 usercode里面加上
GPIO_InitStruct.Mode =GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull=GPIO_PULLUP;
保证跑到400K无压力
各位调i2c的要小心啊 查看全部
这两天用stm32f103 调i2c 发现cube忘记给i2c引脚加输出模式了 各位一定要小心啊
/ USER CODE BEGIN I2C1_MspInit 0 /

// __HAL_RCC_AFIO_CLK_ENABLE();
/ USER CODE END I2C1_MspInit 0 /
/ Peripheral clock enable /
__I2C1_CLK_ENABLE();

/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
上面是源代码

在那个 usercode里面加上
GPIO_InitStruct.Mode =GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull=GPIO_PULLUP;
保证跑到400K无压力
各位调i2c的要小心啊

谁能给我解释下STM32CubeMx生成的代码只能使用2个任务?

问题困惑日日夜夜 回复了问题 • 3 人关注 • 1 个回复 • 1328 次浏览 • 2015-02-26 00:08 • 来自相关话题

这两天要用AVR单片机所以研究了下ATxmega64,各种不习惯啊!

其他分享mf1111 回复了问题 • 2 人关注 • 3 个回复 • 1502 次浏览 • 2015-02-17 20:15 • 来自相关话题

STM32CubeFX能否进行版本管理,卸载掉老的版本?

问题困惑admin 回复了问题 • 2 人关注 • 3 个回复 • 1124 次浏览 • 2015-02-15 16:06 • 来自相关话题

分享国外文章 : 建立一个基于USB_CDC的工程为STM32F4Discovery(建立usb虚拟串口),非常详细,值得借鉴!

其他分享admin 发表了文章 • 3 个评论 • 3447 次浏览 • 2015-02-13 15:15 • 来自相关话题

文章名:Making a basic USB CDC project for STM32F4Discovery
摘录:
This tutorial shows how to create a basic USB communication device using the STM32F4Discovery board. The USB protocol stack implementation is contained in the STM32CubeF4 firmware package, however to get a complete project you need to either use the Stm32Cube MX tool to generate it, or assemble it from different components in the firmware package. In this tutorial we will use the second way.
这篇教程是是基于他们官方的软件来写的,名字是VisualGDB 4.2,这个就是一个viral studio的外挂插件,安装后类似于keil的编译器,很强大,我试了一下,就是用着不习惯,呵呵

关于他的插件的用法咱们不去追究,只看CDC怎么从stm32cubemx的软件包提供的模板,去完成一个实用的usb_CDC功能.

不过,对这个软件插件有兴趣的可以安装使用一下, 大小只有5M
但是安装后会提示安装一些stm32的驱动,大概也有10M,
和谐版安装包已经发布在本站网盘,名字是: VisualGDB-4.2r4.rar

这篇文章的PDF版本同时也放在了VisualGDB-4.2r4.rar文件中!

插件安装完成后,将文件夹内的Crack里面的文件,复制到安装目录就行了.
具体自己百度......

文章地址: usb cdc工程完整演示

如果以上地址打不开,可以到本论坛网盘里下载PDF文档,
文档名称:Making a basic USB CDC project for STM32F4Discovery _ VisualGDB Tutorials.pdf
下面是我摘抄的部分代码, 人家国外写代码真心漂亮!

1.Now that the basic project is working, we will add the USB protocol implementation from ST. The USB implementation consists of 3 parts:
USB core protocol implementation
USB CDC device class (virtual COM port)
USB configuration (descriptors and hardware mappings)




[]Select the STM32_USB_Device_Library\Core directory from the STM32CubeF4 package. Select 'copy folder to project with subpath' and enter 'USB' as the subpath:[/]
3.Press OK. The core files will be copied and imported to Solution Explorer. Import the Class\CDC directory from the STM32CubeF4 package in a similar way:


4.Finally we need to copy USB configuration files. A sample CDC project can be found under Projects\STM324x9I_EVAL\Applications\USB_Device\CDC_Standalone in the STM32CubeF4 package. Copy usb_conf and usb_desc sources from the src directory and their counterparts from the inc directory. Add them to the project and delete the usbd_conf_template.h and usbd_conf_template.c files from the USB\core folder as they are now replaced by actual configuration files. When you're done, the Solution Explorer should look the following way:

5.Finally we need to add the directories with the copied include files to the include directory list and ensure the usb_conf.c file uses the USB peripheral in FS mode (HS is not supported on STM32F407). Open VisualGDB Project Properties, add the 'USE_USB_FS' macro to Preprocessor Macros and the '.', 'USB/Core/Inc' and 'USB/CDED/Inc' directories to Include Directories (don't use the backward slash ('\')):


6.Now it's the time to start using the imported USB code from main(). Replace the main() function with the following code (do not remove SysTick_Handler):


extern "C"
{
#include <usbd_core.h>
#include <usbd_cdc.h>
#include <usbd_cdc_if_template.h>
#include <usbd_desc.h>

USBD_HandleTypeDef USBD_Device;
}

static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;

__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

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 = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
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_5);
}

int main(void)
{
HAL_Init();
SystemClock_Config();
USBD_Init(&USBD_Device, &VCP_Desc, 0);

USBD_RegisterClass(&USBD_Device, &USBD_CDC);
USBD_CDC_RegisterInterface(&USBD_Device,
&USBD_CDC_Template_fops);
USBD_Start(&USBD_Device);

for (;;) ;
}

++++++++++++++++++++++++++++++++
Note that the PLL values above are valid for the 8 MHz crystal on the STM32F4Discovery board. If you are using a different crystal, you will need to modify the values.

7.Two last things to do before we can test our device is to implement the _sbrk() function that tells the C library the location of the heap and provide the USB OTG interrupt handler that will call the corresponding handler of the ST USB library:


extern "C"
{
caddr_t _sbrk(int increment)
{
extern char end asm("end");
register char *pStack asm("sp");

static char *s_pHeapEnd;

if (!s_pHeapEnd)
s_pHeapEnd = &end;

if (s_pHeapEnd + increment > pStack)
return (caddr_t)-1;

char *pOldHeapEnd = s_pHeapEnd;
s_pHeapEnd += increment;
return (caddr_t)pOldHeapEnd;
}

extern PCD_HandleTypeDef hpcd;

void OTG_FS_IRQHandler(void)
{
HAL_PCD_IRQHandler(&hpcd);
}
}

8.Build the project again. Note how the FLASH footprint increased from 2.6KB to 23KB as the USB code is now included in the binary:


9.Connect your board using both USB sockets. Note that the mini-USB socket is needed for JTAG debugging and the micro-USB socket is connected to the STM32F407 microcontroller itself:



10.Press Ctrl-F5 to program the device without debugging it. The Device Manager should now show 2 instances of the port. Each instance corresponds to one of 2 interfaces reported by the ST library (see the definition of USBD_CDC_CfgFSDesc):



11.Although the ST USB library implements the standard USB CDC class and Windows has drivers for it, it will not recognize the device without an inf file specifying which driver to use. Download and install the STM32 VCP drivers to get Windows to recognize your device. Take a note of the COM port number assigned to the device:
这个VCP驱动本站已经上传到网盘, 分为X86系统盒X64系统,自己看着下载啊,别下错!


12.Now we will add some basic functionality to our project. We will make a variation of a simple 'echo' application. First of all modify the TEMPLATE_Init() function in usbd_cdc_if_template.c to set a receive buffer:


extern USBD_HandleTypeDef USBD_Device;

static struct
{
uint8_t Buffer[CDC_DATA_HS_OUT_PACKET_SIZE];
int Position, Size;
char ReadDone;
} s_RxBuffer;

char g_VCPInitialized;

static int8_t TEMPLATE_Init(void)
{
USBD_CDC_SetRxBuffer(&USBD_Device, s_RxBuffer.Buffer);
g_VCPInitialized = 1;
return (0);
}


13.Then modify TEMPLATE_Receive() that is called from an interrupt handler to store received data in our buffer:


static int8_t TEMPLATE_Receive (uint8_t[i] Buf, uint32_t [/i]Len)
{
s_RxBuffer.Position = 0;
s_RxBuffer.Size = *Len;
s_RxBuffer.ReadDone = 1;
return (0);
}


14.Then add basic read and write functions:


int VCP_read(void *pBuffer, int size)
{
if (!s_RxBuffer.ReadDone)
return 0;

int remaining = s_RxBuffer.Size - s_RxBuffer.Position;
int todo = MIN(remaining, size);
if (todo <= 0)
return 0;

memcpy(pBuffer, s_RxBuffer.Buffer + s_RxBuffer.Position, todo);
s_RxBuffer.Position += todo;
if (s_RxBuffer.Position >= s_RxBuffer.Size)
{
s_RxBuffer.ReadDone = 0;
USBD_CDC_ReceivePacket(&USBD_Device);
}

return todo;
}

int VCP_write(const void *pBuffer, int size)
{
if (size > CDC_DATA_HS_OUT_PACKET_SIZE)
{
int offset;
for (offset = 0; offset < size; offset++)
{
int todo = MIN(CDC_DATA_HS_OUT_PACKET_SIZE,
size - offset);
int done = VCP_write(((char *)pBuffer) + offset, todo);
if (done != todo)
return offset + done;
}

return size;
}

USBD_CDC_HandleTypeDef *pCDC =
(USBD_CDC_HandleTypeDef *)USBD_Device.pClassData;
while(pCDC->TxState) { } //Wait for previous transfer

USBD_CDC_SetTxBuffer(&USBD_Device, (uint8_t *)pBuffer, size);
if (USBD_CDC_TransmitPacket(&USBD_Device) != USBD_OK)
return 0;

while(pCDC->TxState) { } //Wait until transfer is done
return size;
}


15.Add the prototypes of the new functions and the initialization flag to usb_cdc_if_template.h:


int VCP_read(void *pBuffer, int size);
int VCP_write(const void *pBuffer, int size);
extern char g_VCPInitialized;

+++++++++++++++++++++++++++++++++
Note that the current code will use the receive buffer for one packet only and wait for the entire transmission to complete before returning. This is slow and inefficient compared to a pair of ring buffers and is only shown here do to extreme simplicity.

16.The ST's USBD_CDC_TransmitPacket() function contains a bug: it sets the 'transmission in progress' flag AFTER starting the actual transmission, so if the 'transmission done' interrupt happens too early, the flag will be set AFTER the interrupt and hence will never be cleared. To fix this, move "hcdc->TxState = 1" line before the call to USBD_LL_Transmit():


if(hcdc->TxState == 0)
{
hcdc->TxState = 1;

USBD_LL_Transmit(pdev, CDC_IN_EP, hcdc->TxBuffer,
hcdc->TxLength);
return USBD_OK;
}


17.Finally replace the empty loop in main() with the following code:


char byte;
for (;;)
{
if (VCP_read(&byte, 1) != 1)
continue;
VCP_write("\r\nYou typed ", 12);
VCP_write(&byte, 1);
VCP_write("\r\n", 2);
}

后面还有一点,不过,不用他的插件的话,就用不上! 查看全部
文章名:Making a basic USB CDC project for STM32F4Discovery
摘录:
This tutorial shows how to create a basic USB communication device using the STM32F4Discovery board. The USB protocol stack implementation is contained in the STM32CubeF4 firmware package, however to get a complete project you need to either use the Stm32Cube MX tool to generate it, or assemble it from different components in the firmware package. In this tutorial we will use the second way.
这篇教程是是基于他们官方的软件来写的,名字是VisualGDB 4.2,这个就是一个viral studio的外挂插件,安装后类似于keil的编译器,很强大,我试了一下,就是用着不习惯,呵呵

关于他的插件的用法咱们不去追究,只看CDC怎么从stm32cubemx的软件包提供的模板,去完成一个实用的usb_CDC功能.

不过,对这个软件插件有兴趣的可以安装使用一下, 大小只有5M
但是安装后会提示安装一些stm32的驱动,大概也有10M,
和谐版安装包已经发布在本站网盘,名字是: VisualGDB-4.2r4.rar

这篇文章的PDF版本同时也放在了VisualGDB-4.2r4.rar文件中!

插件安装完成后,将文件夹内的Crack里面的文件,复制到安装目录就行了.
具体自己百度......

文章地址: usb cdc工程完整演示

如果以上地址打不开,可以到本论坛网盘里下载PDF文档,
文档名称:Making a basic USB CDC project for STM32F4Discovery _ VisualGDB Tutorials.pdf
下面是我摘抄的部分代码, 人家国外写代码真心漂亮!

1.Now that the basic project is working, we will add the USB protocol implementation from ST. The USB implementation consists of 3 parts:
USB core protocol implementation
USB CDC device class (virtual COM port)
USB configuration (descriptors and hardware mappings)




    []Select the STM32_USB_Device_Library\Core directory from the STM32CubeF4 package. Select 'copy folder to project with subpath' and enter 'USB' as the subpath:[/]

3.Press OK. The core files will be copied and imported to Solution Explorer. Import the Class\CDC directory from the STM32CubeF4 package in a similar way:


4.Finally we need to copy USB configuration files. A sample CDC project can be found under Projects\STM324x9I_EVAL\Applications\USB_Device\CDC_Standalone in the STM32CubeF4 package. Copy usb_conf and usb_desc sources from the src directory and their counterparts from the inc directory. Add them to the project and delete the usbd_conf_template.h and usbd_conf_template.c files from the USB\core folder as they are now replaced by actual configuration files. When you're done, the Solution Explorer should look the following way:

5.Finally we need to add the directories with the copied include files to the include directory list and ensure the usb_conf.c file uses the USB peripheral in FS mode (HS is not supported on STM32F407). Open VisualGDB Project Properties, add the 'USE_USB_FS' macro to Preprocessor Macros and the '.', 'USB/Core/Inc' and 'USB/CDED/Inc' directories to Include Directories (don't use the backward slash ('\')):


6.Now it's the time to start using the imported USB code from main(). Replace the main() function with the following code (do not remove SysTick_Handler):


extern "C"
{
#include <usbd_core.h>
#include <usbd_cdc.h>
#include <usbd_cdc_if_template.h>
#include <usbd_desc.h>

USBD_HandleTypeDef USBD_Device;
}

static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;

__PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

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 = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
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_5);
}

int main(void)
{
HAL_Init();
SystemClock_Config();
USBD_Init(&USBD_Device, &VCP_Desc, 0);

USBD_RegisterClass(&USBD_Device, &USBD_CDC);
USBD_CDC_RegisterInterface(&USBD_Device,
&USBD_CDC_Template_fops);
USBD_Start(&USBD_Device);

for (;;) ;
}

++++++++++++++++++++++++++++++++
Note that the PLL values above are valid for the 8 MHz crystal on the STM32F4Discovery board. If you are using a different crystal, you will need to modify the values.

7.Two last things to do before we can test our device is to implement the _sbrk() function that tells the C library the location of the heap and provide the USB OTG interrupt handler that will call the corresponding handler of the ST USB library:


extern "C"
{
caddr_t _sbrk(int increment)
{
extern char end asm("end");
register char *pStack asm("sp");

static char *s_pHeapEnd;

if (!s_pHeapEnd)
s_pHeapEnd = &end;

if (s_pHeapEnd + increment > pStack)
return (caddr_t)-1;

char *pOldHeapEnd = s_pHeapEnd;
s_pHeapEnd += increment;
return (caddr_t)pOldHeapEnd;
}

extern PCD_HandleTypeDef hpcd;

void OTG_FS_IRQHandler(void)
{
HAL_PCD_IRQHandler(&hpcd);
}
}

8.Build the project again. Note how the FLASH footprint increased from 2.6KB to 23KB as the USB code is now included in the binary:


9.Connect your board using both USB sockets. Note that the mini-USB socket is needed for JTAG debugging and the micro-USB socket is connected to the STM32F407 microcontroller itself:



10.Press Ctrl-F5 to program the device without debugging it. The Device Manager should now show 2 instances of the port. Each instance corresponds to one of 2 interfaces reported by the ST library (see the definition of USBD_CDC_CfgFSDesc):



11.Although the ST USB library implements the standard USB CDC class and Windows has drivers for it, it will not recognize the device without an inf file specifying which driver to use. Download and install the STM32 VCP drivers to get Windows to recognize your device. Take a note of the COM port number assigned to the device:
这个VCP驱动本站已经上传到网盘, 分为X86系统盒X64系统,自己看着下载啊,别下错!


12.Now we will add some basic functionality to our project. We will make a variation of a simple 'echo' application. First of all modify the TEMPLATE_Init() function in usbd_cdc_if_template.c to set a receive buffer:


extern USBD_HandleTypeDef USBD_Device;

static struct
{
uint8_t Buffer[CDC_DATA_HS_OUT_PACKET_SIZE];
int Position, Size;
char ReadDone;
} s_RxBuffer;

char g_VCPInitialized;

static int8_t TEMPLATE_Init(void)
{
USBD_CDC_SetRxBuffer(&USBD_Device, s_RxBuffer.Buffer);
g_VCPInitialized = 1;
return (0);
}


13.Then modify TEMPLATE_Receive() that is called from an interrupt handler to store received data in our buffer:


static int8_t TEMPLATE_Receive (uint8_t[i] Buf, uint32_t [/i]Len)
{
s_RxBuffer.Position = 0;
s_RxBuffer.Size = *Len;
s_RxBuffer.ReadDone = 1;
return (0);
}


14.Then add basic read and write functions:


int VCP_read(void *pBuffer, int size)
{
if (!s_RxBuffer.ReadDone)
return 0;

int remaining = s_RxBuffer.Size - s_RxBuffer.Position;
int todo = MIN(remaining, size);
if (todo <= 0)
return 0;

memcpy(pBuffer, s_RxBuffer.Buffer + s_RxBuffer.Position, todo);
s_RxBuffer.Position += todo;
if (s_RxBuffer.Position >= s_RxBuffer.Size)
{
s_RxBuffer.ReadDone = 0;
USBD_CDC_ReceivePacket(&USBD_Device);
}

return todo;
}

int VCP_write(const void *pBuffer, int size)
{
if (size > CDC_DATA_HS_OUT_PACKET_SIZE)
{
int offset;
for (offset = 0; offset < size; offset++)
{
int todo = MIN(CDC_DATA_HS_OUT_PACKET_SIZE,
size - offset);
int done = VCP_write(((char *)pBuffer) + offset, todo);
if (done != todo)
return offset + done;
}

return size;
}

USBD_CDC_HandleTypeDef *pCDC =
(USBD_CDC_HandleTypeDef *)USBD_Device.pClassData;
while(pCDC->TxState) { } //Wait for previous transfer

USBD_CDC_SetTxBuffer(&USBD_Device, (uint8_t *)pBuffer, size);
if (USBD_CDC_TransmitPacket(&USBD_Device) != USBD_OK)
return 0;

while(pCDC->TxState) { } //Wait until transfer is done
return size;
}


15.Add the prototypes of the new functions and the initialization flag to usb_cdc_if_template.h:


int VCP_read(void *pBuffer, int size);
int VCP_write(const void *pBuffer, int size);
extern char g_VCPInitialized;

+++++++++++++++++++++++++++++++++
Note that the current code will use the receive buffer for one packet only and wait for the entire transmission to complete before returning. This is slow and inefficient compared to a pair of ring buffers and is only shown here do to extreme simplicity.

16.The ST's USBD_CDC_TransmitPacket() function contains a bug: it sets the 'transmission in progress' flag AFTER starting the actual transmission, so if the 'transmission done' interrupt happens too early, the flag will be set AFTER the interrupt and hence will never be cleared. To fix this, move "hcdc->TxState = 1" line before the call to USBD_LL_Transmit():


if(hcdc->TxState == 0)
{
hcdc->TxState = 1;

USBD_LL_Transmit(pdev, CDC_IN_EP, hcdc->TxBuffer,
hcdc->TxLength);
return USBD_OK;
}


17.Finally replace the empty loop in main() with the following code:


char byte;
for (;;)
{
if (VCP_read(&byte, 1) != 1)
continue;
VCP_write("\r\nYou typed ", 12);
VCP_write(&byte, 1);
VCP_write("\r\n", 2);
}

后面还有一点,不过,不用他的插件的话,就用不上!

STM32F107的APB1频率设置为72MHz,实际只有8MHz

问题困惑冰镇兔子 回复了问题 • 4 人关注 • 4 个回复 • 1463 次浏览 • 2015-02-13 09:07 • 来自相关话题

怎么根据需要使用工程模板啊??求助

新手交流admin 回复了问题 • 2 人关注 • 1 个回复 • 1042 次浏览 • 2015-02-11 09:00 • 来自相关话题

stm32F407通过dp83848与PC端通信成功,在ubuntu下仅可以ping通,但通信不成功

回复

问题困惑lx09110718 发起了问题 • 2 人关注 • 0 个回复 • 2241 次浏览 • 2015-02-09 17:02 • 来自相关话题

调用串口发送函数后,总是不能将值写入数据寄存器,求指点迷津!上图!

问题困惑L 回复了问题 • 2 人关注 • 3 个回复 • 1835 次浏览 • 2015-02-09 16:48 • 来自相关话题

UART 里面怎么没有sendDate的函数呀

问题困惑sadou 回复了问题 • 3 人关注 • 3 个回复 • 1834 次浏览 • 2015-02-09 06:00 • 来自相关话题