主程序
#include "drv_usb_hw.h"
#include "cdc_acm_core.h"
#include "gpio.h"
#include "IIC.h"
#include "MyADC.h"
#include "stdbool.h"
#include "systick.h"
#include "Flash.h"
void Cmd_ADC_Ctrl(void);
void Cmd_Enbale_Ctrl(void);
void Cmd_TTL_Ctrl_6(void);
void Cmd_Flash_Ctrl(void);
void ADCStandard_FlashRead(void);
bool OverTEMP = false;
bool DataCollectMultiple = false;
bool ReceiveCOMCtrlOpen = false;
bool ReceiveCOMCtrlClose = false;
extern uint8_t I2CReceBuffer[16];
extern uint8_t I2CSendBuffer[2];
//extern uint8_t GPIOStateBuff_PORT[9];
extern uint8_t BUFF_74HC595[17];
extern uint8_t adc_value_8bit[32];
extern uint8_t usb_data_bufferSend[64] ;
extern uint8_t usb_data_bufferReceive[64] ;
extern uint32_t Myreceive_length ;
extern uint32_t FlashBuff[1];
uint32_t Flash_addr = 0x080E0000; //���� 11 ��ַ������ģ���Ҫ��flash.c Flash_WriteWord����������������ַ
uint32_t Flash_ReadData = 0;
usb_core_driver cdc_acm;
uint16_t ADC_Standard = 0; //ADCУֵ
/*!
\brief main routine will construct a USB CDC device
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
usb_gpio_config();
usb_rcu_config();
usb_timer_init();
usbd_init (&cdc_acm,
#ifdef USE_USB_FS
USB_CORE_ENUM_FS,
#elif defined(USE_USB_HS)
USB_CORE_ENUM_HS,
#endif /* USE_USB_FS */
&cdc_desc,
&cdc_class);
usb_intr_config();
systick_config();
GPIO_Init();
GpioAllLow();
MyADC_Init();
IICgpio_Init();
IIC_Init();
ADCStandard_FlashRead();
int count = 0;
usbd_ep_recev (&cdc_acm, CDC_DATA_OUT_EP, usb_data_bufferReceive, 64);
while (1)
{
count++;
//Ϊ��һ�ν�������
//usbd_ep_recev (&cdc_acm, CDC_DATA_OUT_EP, usb_data_bufferReceive, 64);
//��λ��ָ���
if( Myreceive_length == 2) //ADC����
{
Cmd_ADC_Ctrl();
}
//2023/12/6����Flash��дָ��
if( Myreceive_length == 3) //ADC����
{
Cmd_Flash_Ctrl();
}
if( Myreceive_length == 4) //ʹ�ܿ���
{
Cmd_Enbale_Ctrl();
}
//2023/9/27
if( Myreceive_length == 5) //TTL�������
{
Cmd_TTL_Ctrl_6();
}
//
if(DataCollectMultiple) //��ȡADC����
{
ADC_ValueGet(ADC_Standard);
for(int i = 0;i<32;i++)
{
usb_data_bufferSend[i+1] = adc_value_8bit[i];
}
usb_data_bufferSend[0] = 0xAA;
usbd_ep_send (&cdc_acm, CDC_DATA_IN_EP,usb_data_bufferSend,33);
// DataCollectMultiple = false;
}
//��ȡIIC������ ��ѯ�¶�ָ�꣬�¶ȵ�������ʱ�������ȣ������¶ȸ���10���϶� �Զ��Ͽ�
if(count > 400000)//300000
{
IIC_Send(0x4D<<1,I2CSendBuffer,1);
IIC_Receive(0x4D<<1,I2CReceBuffer,2);
if((Data_Coculeat(I2CReceBuffer))>(-16)) //�¶ȸ���-18���϶� 2023/9/20�� -18
{
OverTEMP = false;
}
if((Data_Coculeat(I2CReceBuffer))<(-18)) //�¶ȵ���-19���϶� -19
{
OverTEMP = true;
}
if((OverTEMP == true)&&(ReceiveCOMCtrlClose == false))//����-19�ȣ�����λ���ر�ָ��Ϊ��
{
gpio_bit_set(GPIOA,GPIO_PIN_5);
}
if((OverTEMP == false)&&(ReceiveCOMCtrlOpen == false))
{
gpio_bit_reset(GPIOA,GPIO_PIN_5);
}
count = 0;
I2CReceBuffer[0] = 0;
I2CReceBuffer[1] = 0;
}
}
}
void Cmd_ADC_Ctrl(void)
{
Myreceive_length = 0;
if(usb_data_bufferReceive[0]==0xAA)
{
switch(usb_data_bufferReceive[1])
{
case 0x01://��������
gpio_bit_set(GPIOA,GPIO_PIN_5);
ReceiveCOMCtrlOpen = true;
ReceiveCOMCtrlClose = false;
break;
case 0x10://�رռ���
gpio_bit_reset(GPIOA,GPIO_PIN_5);
ReceiveCOMCtrlClose = true;
ReceiveCOMCtrlOpen = false;
break;
case 0x02://����ADC�ɼ�
DataCollectMultiple = true;
break;
case 0x03://ֹͣADC�ɼ�
DataCollectMultiple = false;
break;
case 0x04://ͨѶУ��
usb_data_bufferSend[0] = 0xAA;
usb_data_bufferSend[1] = 0x04;
usb_data_bufferSend[2] = 0x02;
usbd_ep_send (&cdc_acm, CDC_DATA_IN_EP,usb_data_bufferSend,3);
break;
}
}
}
/*
ʹ��λ���� AC 10 XX XX (Group Data) ���Dataֻ��Ϊ 0 / 1
��ȡ����TTLֵ AC 00 00 00
*/
void Cmd_Enbale_Ctrl(void)
{
Myreceive_length = 0;
if(usb_data_bufferReceive[0]==0xAC)
{
if(usb_data_bufferReceive[1]== 0x00 && usb_data_bufferReceive[3]== 0x00 &&usb_data_bufferReceive[2]== 0x00)
{
for(int i=0;i<17;i++)
{
usb_data_bufferSend[i+1] = BUFF_74HC595[i];
}
usb_data_bufferSend[0] = 0xAC;
usbd_ep_send (&cdc_acm, CDC_DATA_IN_EP,usb_data_bufferSend,18);
}
if(usb_data_bufferReceive[1]== 0x10)
{
int group = usb_data_bufferReceive[2];
uint8_t data = usb_data_bufferReceive[3];
FuncOfEnable(group,data);
}
}
}
/*
���TTLֵ AC 01 XX XX XX (Group ProssMode Data)
ע�� AGC����Ҳʹ�� ���TTLֵ��ָ�� AC 01 XX XX XX ����
*/
void Cmd_TTL_Ctrl_6(void)
{
Myreceive_length = 0;
if(usb_data_bufferReceive[0]==0xAC)
{
if(usb_data_bufferReceive[1] != 0x01) return;
int group = usb_data_bufferReceive[2];
int prossmode = usb_data_bufferReceive[3];
uint8_t data = usb_data_bufferReceive[4];
FuncOfInterface(group,prossmode,data);
}
}
/*AB XX XX �ȸߺ��*/
void Cmd_Flash_Ctrl(void)
{
Myreceive_length = 0;
uint16_t ReceiveFlashData=0;
uint32_t ReceiveFlashData_U32=0;
uint16_t DATA = Flash_ReadWord(Flash_addr);
if(usb_data_bufferReceive[0]==0xAB)
{
ReceiveFlashData = ((uint16_t)usb_data_bufferReceive[1]<<8) | usb_data_bufferReceive[2];
ReceiveFlashData_U32 = (uint32_t)ReceiveFlashData;
Flash_WriteWord(Flash_addr,ReceiveFlashData_U32);
ADCStandard_FlashRead();
usb_data_bufferSend[0] = 0xAE;
usb_data_bufferSend[1] = DATA>>8;
usb_data_bufferSend[2] = DATA;
usb_data_bufferSend[3] = ReceiveFlashData>>8;
usb_data_bufferSend[4] = ReceiveFlashData;
usbd_ep_send (&cdc_acm, CDC_DATA_IN_EP,usb_data_bufferSend,5);
}
if(usb_data_bufferReceive[0]==0xAC)
{
usb_data_bufferSend[0] = 0xAE;
usb_data_bufferSend[1] = DATA>>8;
usb_data_bufferSend[2] = DATA;
usbd_ep_send (&cdc_acm, CDC_DATA_IN_EP,usb_data_bufferSend,3);
}
}
void ADCStandard_FlashRead(void)
{
Flash_ReadData = Flash_ReadWord(Flash_addr);
if(Flash_ReadData==0 || Flash_ReadData==0xFFFFFFFF)
{
ADC_Standard = 0x0CE4;
}
if(Flash_ReadData<0x0E10 && Flash_ReadData>0X0BB8)//3000-3600
{
ADC_Standard = Flash_ReadData;
}
}
IIC代码:
#include "IIC.h"
#include "systick.h"
/*һ�β�����ֻ����ɶ�����д �����ֶ���д*/
//#define SDL_H gpio_bit_set(GPIOF,GPIO_PIN_0)
//#define SDL_L gpio_bit_reset(GPIOF,GPIO_PIN_0)
//#define SCL_H gpio_bit_set(GPIOF,GPIO_PIN_1)
//#define SCL_L gpio_bit_reset(GPIOF,GPIO_PIN_1)
#define I2C1_SLAVE_ADDRESS7 0x9A
uint8_t I2CReceBuffer[16];
uint8_t I2CSendBuffer[2] = {0x9A,0x00};
void IICgpio_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOF);
gpio_af_set(GPIOF, GPIO_AF_4, GPIO_PIN_0);
gpio_af_set(GPIOF, GPIO_AF_4, GPIO_PIN_1);
gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE,GPIO_PIN_0);
gpio_output_options_set(GPIOF, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_0);
gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE,GPIO_PIN_1);
gpio_output_options_set(GPIOF, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
}
void IIC_Init(void)
{
rcu_periph_clock_enable(RCU_I2C1);
i2c_clock_config(I2C1,100000,I2C_DTCY_2);
i2c_mode_addr_config(I2C1,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,0xA0);//�ӻ�ģʽ�µĵ�ַ
i2c_enable(I2C1);
i2c_ack_config(I2C1,I2C_ACK_ENABLE);
}
void IIC_Send(uint8_t addr,uint8_t *buff,uint8_t len)
{
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY)); //æ
i2c_start_on_bus(I2C1);
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));//��ʼλ����
i2c_master_addressing(I2C1, addr, I2C_TRANSMITTER);
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));//��ַ����
i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));//�����ڼ� I2C_dataΪ��
//for(int i=0;i<len;i++)
// {
i2c_data_transmit(I2C1,buff[1]);
while(!i2c_flag_get(I2C1,I2C_FLAG_TBE));//�����ڼ�I2C_DATAΪ��
// }
i2c_stop_on_bus(I2C1);
while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
}
void IIC_Receive(uint8_t addr,uint8_t *buff,uint8_t len)
{
while(i2c_flag_get(I2C1,I2C_FLAG_I2CBSY)); //æ��־
i2c_start_on_bus(I2C1);
while(!i2c_flag_get(I2C1,I2C_FLAG_SBSEND)); //�ȴ� ��ʼ�źŷ���
i2c_master_addressing(I2C1,addr,I2C_RECEIVER);
while(!i2c_flag_get(I2C1,I2C_FLAG_ADDSEND)); //�ȴ� ���͵�ַ
i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
for(int i=0; i<len; i++){
if((len-2) == i)
{
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));//�ֽڴ������
i2c_ack_config(I2C1, I2C_ACK_DISABLE);
}
while(!i2c_flag_get(I2C1, I2C_FLAG_RBNE));//�����ڼ� I2C_data�ǿ�
buff[i] = i2c_data_receive(I2C1);
}
i2c_stop_on_bus(I2C1);
while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
i2c_ack_config(I2C1, I2C_ACK_ENABLE);
}
int16_t Data_Coculeat(uint8_t *buff) //�����¶�ֵ ��12bit ��8bitΪ�������֣���4bitΪС��λ �ֱ���0.0625
{
int16_t Sener = 0;
Sener = (int16_t)(buff[0]<<8)+buff[1];
Sener = Sener>>4;
if(Sener&0x800)//���¶�
{
Sener = (Sener-4096)/16;
}
if(!(Sener&0x800))//���¶�
{
Sener = Sener/16;//*0.0625
}
return Sener;
}
void SD5075Read(void)
{
}
ADC + DMA代码:
#include "MyADC.h"
#include "systick.h"
uint16_t adc_value[16];
uint8_t adc_value_8bit[32];
//uint16_t ADC_Standard = 0x0CDC; //3292
int count = 0;
void MyADC_Init(void)
{
rcu_periph_clock_enable(RCU_DMA1);
rcu_periph_clock_enable(RCU_GPIOA); //��������ʱ��
rcu_periph_clock_enable(RCU_GPIOC);
rcu_periph_clock_enable(RCU_GPIOF);
rcu_periph_clock_enable(RCU_ADC2);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_0);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_1);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_2);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_3);
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_0);
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_1);
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_2);
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_3);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_3);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_4);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_5);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_6);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_7);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_8);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_9);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_10);
adc_deinit(); //ȱʡ
adc_sync_mode_config(ADC_SYNC_MODE_INDEPENDENT); //��ADC��������
adc_resolution_config(ADC2,ADC_RESOLUTION_12B); //�ֱ���12bit
adc_special_function_config(ADC2, ADC_CONTINUOUS_MODE, ENABLE); //����ģʽ
adc_special_function_config(ADC2, ADC_SCAN_MODE, ENABLE); //ɨ��ģʽ
adc_data_alignment_config(ADC2, ADC_DATAALIGN_RIGHT); //LSB��λ��ǰ
adc_channel_length_config(ADC2, ADC_ROUTINE_CHANNEL,16); //����ͨ���� 16ͨ��
adc_routine_channel_config(ADC2,0,ADC_CHANNEL_0,ADC_SAMPLETIME_144); //ͨ������ 8usһ��ͨ��
adc_routine_channel_config(ADC2,1,ADC_CHANNEL_1,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,2,ADC_CHANNEL_2,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,3,ADC_CHANNEL_3,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,4,ADC_CHANNEL_4,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,5,ADC_CHANNEL_5,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,6,ADC_CHANNEL_6,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,7,ADC_CHANNEL_7,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,8,ADC_CHANNEL_8,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,9,ADC_CHANNEL_9,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,10,ADC_CHANNEL_10,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,11,ADC_CHANNEL_11,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,12,ADC_CHANNEL_12,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,13,ADC_CHANNEL_13,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,14,ADC_CHANNEL_14,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,15,ADC_CHANNEL_15,ADC_SAMPLETIME_144); //ͨ������
//adc_external_trigger_config(ADC2,ADC_ROUTINE_CHANNEL,EXTERNAL_TRIGGER_DISABLE);
adc_dma_request_after_last_enable(ADC2);
adc_dma_mode_enable(ADC2);
adc_enable(ADC2); //ʹ��
delay_1ms(1);
adc_calibration_enable(ADC2); //У
adc_clock_config(ADC_ADCCK_PCLK2_DIV8); //8��Ƶ 10.5MHZ /156 67.3KHZ
dma_single_data_parameter_struct dma_single_data_parameter;
dma_deinit(DMA1, DMA_CH0);
dma_single_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC2));
dma_single_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_single_data_parameter.memory0_addr = (uint32_t)(adc_value);
dma_single_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_single_data_parameter.periph_memory_width = DMA_PERIPH_WIDTH_16BIT;
dma_single_data_parameter.direction = DMA_PERIPH_TO_MEMORY;
dma_single_data_parameter.number = 16;
dma_single_data_parameter.priority = DMA_PRIORITY_HIGH;
dma_single_data_mode_init(DMA1, DMA_CH0,&dma_single_data_parameter);
dma_channel_subperipheral_select(DMA1, DMA_CH0, DMA_SUBPERI2);
dma_circulation_enable(DMA1, DMA_CH0);
dma_channel_enable(DMA1, DMA_CH0);
adc_software_trigger_enable(ADC2,ADC_ROUTINE_CHANNEL); //��������
}
uint8_t* ADC_ValueGet(uint16_t Standard)
{
uint16_t buff0[16],buff1[16],buff2[16],buff3[16],buff4[16],buff5[16],buff6[16],buff7[16],buff8[16],buff9[16],
buff10[16],buff11[16],buff12[16],buff13[16],buff14[16],buff15[16],buff16[16],buff17[16],buff18[16],buff19[16],everBuff[16];
uint16_t *IOBUFF[]={buff0,buff1,buff2,buff3,buff4,buff5,buff6,buff7,buff8,buff9,buff10,buff11,buff12,
buff13,buff14,buff15,buff16,buff17,buff18,buff19};
for(int i = 0;i<20;i++)
{
for(int j=0;j<16;j++)
{
IOBUFF[i][j] = adc_value[j];
}
delay_1ms(1);
}
// double s = (double)(Standard/1000);
// double sm = ((double)(Standard))/1000;
for(int i=0;i<16;i++)
{
everBuff[i] =(buff0[i]+buff1[i]+buff2[i]+buff3[i]+buff4[i]+buff5[i]+buff6[i]+buff7[i]+buff8[i]+buff9[i]+
buff10[i]+buff11[i]+buff12[i]+buff13[i]+buff14[i]+buff15[i]+buff16[i]+buff17[i]+buff18[i]+buff19[i])/20;
everBuff[i] = everBuff[i]*1000/4095*(((double)(Standard))/1000)*3/2;
// everBuff[i] = everBuff[i]*1000/4095*3.292f*3/2;
// if(i==2)
// {
// if(everBuff[i]>0x07D0)
// {
// everBuff[i] = everBuff[i]-5;
// }
// }
}
for(int i=0;i<16;i++)
{
adc_value_8bit[2*i] = everBuff[i];
adc_value_8bit[2*i+1] = everBuff[i]>>8;
}
return adc_value_8bit;
}
Flash代码:
#include "Flash.h"
uint32_t FlashBuff[1];
/*
>>>>3072KBƬ�����棬��Ϊ��Ƭ��ǰ1024kbΪbank0 ��2048kbΪbank1��
>>>>֧��32λ���֣�16λ���֣�8λ�ֽڱ�̡�
>>>>֧��������������Ƭ��������ҳ����֧��ҳ�������������������ݳ�ʼ��Ϊ0xFF��
>>>>��������ռ��Ϊ8��16KB������2��64KB������14��128KB������4��256KB������
>>>>�ڽ���д�롢��������ʱ����Ҫ�Ƚ�����������ɺ�������
api��fmc_unlock(void)
fmc_lock(void)
*/
uint32_t Flash_ReadWord(uint32_t addr)
{
return *(uint32_t*)addr;
}
void Flash_ReadBuff(uint32_t addr,uint32_t *buff,int Num)
{
for(uint32_t i=0;i<Num;i++)
{
buff[i] = Flash_ReadWord(addr);
addr+=4;
}
}
bool Flash_WriteWord(uint32_t addr,uint32_t data)
{
bool flag;
fmc_unlock();
if(FMC_BUSY != fmc_state_get())
{
fmc_sector_erase(CTL_SECTOR_NUMBER_11);
flag = fmc_word_program(addr,data);
}
fmc_lock();
return flag;
}
bool Flash_WriteBuff(uint32_t addr,uint32_t *buff,int Num)
{
for(int i=0;i<Num;i++)
{
bool flag = Flash_WriteWord(addr,buff[i]);
if(!flag)
{
return false;
}
addr+=4;
buff++;
}
return true;
}
CDC_ACM 虚拟串口:
/*!
\file cdc_acm_core.c
\brief CDC ACM driver
\version 2020-08-01, V3.0.0, firmware for GD32F4xx
\version 2022-03-09, V3.1.0, firmware for GD32F4xx
\version 2022-06-30, V3.2.0, firmware for GD32F4xx
*/
/*
Copyright (c) 2022, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#include "cdc_acm_core.h"
uint8_t usb_data_bufferSend[64] ;
uint8_t usb_data_bufferReceive[64] ;
uint32_t Myreceive_length = 0 ;
#define USBD_VID 0x28E9U
#define USBD_PID 0x018AU
/* note:it should use the C99 standard when compiling the below codes */
/* USB standard device descriptor */
__ALIGN_BEGIN const usb_desc_dev cdc_dev_desc __ALIGN_END =
{
.header =
{
.bLength = USB_DEV_DESC_LEN,
.bDescriptorType = USB_DESCTYPE_DEV,
},
.bcdUSB = 0x0200U,
.bDeviceClass = USB_CLASS_CDC,
.bDeviceSubClass = 0x00U,
.bDeviceProtocol = 0x00U,
.bMaxPacketSize0 = USB_FS_EP0_MAX_LEN,
.idVendor = USBD_VID,
.idProduct = USBD_PID,
.bcdDevice = 0x0100U,
.iManufacturer = STR_IDX_MFC,
.iProduct = STR_IDX_PRODUCT,
.iSerialNumber = STR_IDX_SERIAL,
.bNumberConfigurations = USBD_CFG_MAX_NUM,
};
/* USB device configuration descriptor */
__ALIGN_BEGIN const usb_cdc_desc_config_set cdc_config_desc __ALIGN_END =
{
.config =
{
.header =
{
.bLength = sizeof(usb_desc_config),
.bDescriptorType = USB_DESCTYPE_CONFIG,
},
.wTotalLength = USB_CDC_ACM_CONFIG_DESC_SIZE,
.bNumInterfaces = 0x02U,
.bConfigurationValue = 0x01U,
.iConfiguration = 0x00U,
.bmAttributes = 0x80U,
.bMaxPower = 0x32U
},
.cmd_itf =
{
.header =
{
.bLength = sizeof(usb_desc_itf),
.bDescriptorType = USB_DESCTYPE_ITF
},
.bInterfaceNumber = 0x00U,
.bAlternateSetting = 0x00U,
.bNumEndpoints = 0x01U,
.bInterfaceClass = USB_CLASS_CDC,
.bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
.bInterfaceProtocol = USB_CDC_PROTOCOL_AT,
.iInterface = 0x00U
},
.cdc_header =
{
.header =
{
.bLength = sizeof(usb_desc_header_func),
.bDescriptorType = USB_DESCTYPE_CS_INTERFACE
},
.bDescriptorSubtype = 0x00U,
.bcdCDC = 0x0110U
},
.cdc_call_managment =
{
.header =
{
.bLength = sizeof(usb_desc_call_managment_func),
.bDescriptorType = USB_DESCTYPE_CS_INTERFACE
},
.bDescriptorSubtype = 0x01U,
.bmCapabilities = 0x00U,
.bDataInterface = 0x01U
},
.cdc_acm =
{
.header =
{
.bLength = sizeof(usb_desc_acm_func),
.bDescriptorType = USB_DESCTYPE_CS_INTERFACE
},
.bDescriptorSubtype = 0x02U,
.bmCapabilities = 0x02U,
},
.cdc_union =
{
.header =
{
.bLength = sizeof(usb_desc_union_func),
.bDescriptorType = USB_DESCTYPE_CS_INTERFACE
},
.bDescriptorSubtype = 0x06U,
.bMasterInterface = 0x00U,
.bSlaveInterface0 = 0x01U,
},
.cdc_cmd_endpoint =
{
.header =
{
.bLength = sizeof(usb_desc_ep),
.bDescriptorType = USB_DESCTYPE_EP,
},
.bEndpointAddress = CDC_CMD_EP,
.bmAttributes = USB_EP_ATTR_INT,
.wMaxPacketSize = USB_CDC_CMD_PACKET_SIZE,
.bInterval = 0x0AU
},
.cdc_data_interface =
{
.header =
{
.bLength = sizeof(usb_desc_itf),
.bDescriptorType = USB_DESCTYPE_ITF,
},
.bInterfaceNumber = 0x01U,
.bAlternateSetting = 0x00U,
.bNumEndpoints = 0x02U,
.bInterfaceClass = USB_CLASS_DATA,
.bInterfaceSubClass = 0x00U,
.bInterfaceProtocol = USB_CDC_PROTOCOL_NONE,
.iInterface = 0x00U
},
.cdc_out_endpoint =
{
.header =
{
.bLength = sizeof(usb_desc_ep),
.bDescriptorType = USB_DESCTYPE_EP,
},
.bEndpointAddress = CDC_DATA_OUT_EP,
.bmAttributes = USB_EP_ATTR_BULK,
.wMaxPacketSize = USB_CDC_DATA_PACKET_SIZE,
.bInterval = 0x00U
},
.cdc_in_endpoint =
{
.header =
{
.bLength = sizeof(usb_desc_ep),
.bDescriptorType = USB_DESCTYPE_EP
},
.bEndpointAddress = CDC_DATA_IN_EP,
.bmAttributes = USB_EP_ATTR_BULK,
.wMaxPacketSize = USB_CDC_DATA_PACKET_SIZE,
.bInterval = 0x00U
}
};
/* USB language ID Descriptor */
static __ALIGN_BEGIN const usb_desc_LANGID usbd_language_id_desc __ALIGN_END =
{
.header =
{
.bLength = sizeof(usb_desc_LANGID),
.bDescriptorType = USB_DESCTYPE_STR,
},
.wLANGID = ENG_LANGID
};
/* USB manufacture string */
static __ALIGN_BEGIN const usb_desc_str manufacturer_string __ALIGN_END =
{
.header =
{
.bLength = USB_STRING_LEN(10),
.bDescriptorType = USB_DESCTYPE_STR,
},
.unicode_string = {'G', 'i', 'g', 'a', 'D', 'e', 'v', 'i', 'c', 'e'}
};
/* USB product string */
static __ALIGN_BEGIN const usb_desc_str product_string __ALIGN_END =
{
.header =
{
.bLength = USB_STRING_LEN(12),
.bDescriptorType = USB_DESCTYPE_STR,
},
.unicode_string = {'G', 'D', '3', '2', '-', 'C', 'D', 'C', '_', 'A', 'C', 'M'}
};
/* USBD serial string */
static __ALIGN_BEGIN usb_desc_str serial_string __ALIGN_END =
{
.header =
{
.bLength = USB_STRING_LEN(12),
.bDescriptorType = USB_DESCTYPE_STR,
}
};
/* USB string descriptor set */
void *const usbd_cdc_strings[] =
{
[STR_IDX_LANGID] = (uint8_t *) &usbd_language_id_desc,
[STR_IDX_MFC] = (uint8_t *) &manufacturer_string,
[STR_IDX_PRODUCT] = (uint8_t *) &product_string,
[STR_IDX_SERIAL] = (uint8_t *) &serial_string
};
usb_desc cdc_desc =
{
.dev_desc = (uint8_t *) &cdc_dev_desc,
.config_desc = (uint8_t *) &cdc_config_desc,
.strings = usbd_cdc_strings
};
/* local function prototypes ('static') */
static uint8_t cdc_acm_init (usb_dev *udev, uint8_t config_index);
static uint8_t cdc_acm_deinit (usb_dev *udev, uint8_t config_index);
static uint8_t cdc_acm_req (usb_dev *udev, usb_req *req);
static uint8_t cdc_acm_ctlx_out (usb_dev *udev);
static uint8_t cdc_acm_in (usb_dev *udev, uint8_t ep_num);
static uint8_t cdc_acm_out (usb_dev *udev, uint8_t ep_num);
/* USB CDC device class callbacks structure */
usb_class_core cdc_class =
{
.command = NO_CMD,
.alter_set = 0U,
.init = cdc_acm_init,
.deinit = cdc_acm_deinit,
.req_proc = cdc_acm_req,
.ctlx_out = cdc_acm_ctlx_out,
.data_in = cdc_acm_in,
.data_out = cdc_acm_out
};
/*!
\brief check CDC ACM is ready for data transfer
\param[in] udev: pointer to USB device instance
\param[out] none
\retval 0 if CDC is ready, 5 else
*/
uint8_t cdc_acm_check_ready(usb_dev *udev)
{
if (udev->dev.class_data[CDC_COM_INTERFACE] != NULL) {
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
if ((1U == cdc->packet_receive) && (1U == cdc->packet_sent)) {
return 0U;
}
}
return 1U;
}
/*!
\brief send CDC ACM data
\param[in] udev: pointer to USB device instance
\param[out] none
\retval USB device operation status
*/
void cdc_acm_data_send (usb_dev *udev)
{
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
if (0U != cdc->receive_length) {
cdc->packet_sent = 0U;
usbd_ep_send (udev, CDC_DATA_IN_EP, (uint8_t*)(cdc->data), cdc->receive_length);
cdc->receive_length = 0U;
}
}
/*!
\brief receive CDC ACM data
\param[in] udev: pointer to USB device instance
\param[out] none
\retval USB device operation status
*/
void cdc_acm_data_receive (usb_dev *udev)
{
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
cdc->packet_receive = 0U;
cdc->packet_sent = 0U;
usbd_ep_recev (udev, CDC_DATA_OUT_EP, (uint8_t*)(cdc->data), USB_CDC_DATA_PACKET_SIZE);
}
/*!
\brief initialize the CDC ACM device
\param[in] udev: pointer to USB device instance
\param[in] config_index: configuration index
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_init (usb_dev *udev, uint8_t config_index)
{
static __ALIGN_BEGIN usb_cdc_handler cdc_handler __ALIGN_END;
/* initialize the data Tx endpoint */
usbd_ep_setup (udev, &(cdc_config_desc.cdc_in_endpoint));
/* initialize the data Rx endpoint */
usbd_ep_setup (udev, &(cdc_config_desc.cdc_out_endpoint));
/* initialize the command Tx endpoint */
usbd_ep_setup (udev, &(cdc_config_desc.cdc_cmd_endpoint));
/* initialize CDC handler structure */
cdc_handler.packet_receive = 1U;
cdc_handler.packet_sent = 1U;
cdc_handler.receive_length = 0U;
cdc_handler.line_coding = (acm_line){
.dwDTERate = 115200U,
.bCharFormat = 0U,
.bParityType = 0U,
.bDataBits = 0x08U
};
udev->dev.class_data[CDC_COM_INTERFACE] = (void *)&cdc_handler;
return USBD_OK;
}
/*!
\brief deinitialize the CDC ACM device
\param[in] udev: pointer to USB device instance
\param[in] config_index: configuration index
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_deinit (usb_dev *udev, uint8_t config_index)
{
/* deinitialize the data Tx/Rx endpoint */
usbd_ep_clear (udev, CDC_DATA_IN_EP);
usbd_ep_clear (udev, CDC_DATA_OUT_EP);
/* deinitialize the command Tx endpoint */
usbd_ep_clear (udev, CDC_CMD_EP);
return USBD_OK;
}
/*!
\brief handle the CDC ACM class-specific requests
\param[in] udev: pointer to USB device instance
\param[in] req: device class-specific request
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_req (usb_dev *udev, usb_req *req)
{
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
usb_transc *transc = NULL;
switch (req->bRequest) {
case SEND_ENCAPSULATED_COMMAND:
/* no operation for this driver */
break;
case GET_ENCAPSULATED_RESPONSE:
/* no operation for this driver */
break;
case SET_COMM_FEATURE:
/* no operation for this driver */
break;
case GET_COMM_FEATURE:
/* no operation for this driver */
break;
case CLEAR_COMM_FEATURE:
/* no operation for this driver */
break;
case SET_LINE_CODING:
transc = &udev->dev.transc_out[0];
/* set the value of the current command to be processed */
udev->dev.class_core->alter_set = req->bRequest;
/* enable EP0 prepare to receive command data packet */
transc->remain_len = req->wLength;
transc->xfer_buf = cdc->cmd;
break;
case GET_LINE_CODING:
transc = &udev->dev.transc_in[0];
cdc->cmd[0] = (uint8_t)(cdc->line_coding.dwDTERate);
cdc->cmd[1] = (uint8_t)(cdc->line_coding.dwDTERate >> 8);
cdc->cmd[2] = (uint8_t)(cdc->line_coding.dwDTERate >> 16);
cdc->cmd[3] = (uint8_t)(cdc->line_coding.dwDTERate >> 24);
cdc->cmd[4] = cdc->line_coding.bCharFormat;
cdc->cmd[5] = cdc->line_coding.bParityType;
cdc->cmd[6] = cdc->line_coding.bDataBits;
transc->xfer_buf = cdc->cmd;
transc->remain_len = 7U;
break;
case SET_CONTROL_LINE_STATE:
/* no operation for this driver */
break;
case SEND_BREAK:
/* no operation for this driver */
break;
default:
break;
}
return USBD_OK;
}
/*!
\brief command data received on control endpoint
\param[in] udev: pointer to USB device instance
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_ctlx_out (usb_dev *udev)
{
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
if (udev->dev.class_core->alter_set != NO_CMD) {
/* process the command data */
cdc->line_coding.dwDTERate = (uint32_t)((uint32_t)cdc->cmd[0] |
((uint32_t)cdc->cmd[1] << 8U) |
((uint32_t)cdc->cmd[2] << 16U) |
((uint32_t)cdc->cmd[3] << 24U));
cdc->line_coding.bCharFormat = cdc->cmd[4];
cdc->line_coding.bParityType = cdc->cmd[5];
cdc->line_coding.bDataBits = cdc->cmd[6];
udev->dev.class_core->alter_set = NO_CMD;
}
usbd_ep_recev (udev, CDC_DATA_OUT_EP, usb_data_bufferReceive, 64);
return USBD_OK;
}
/*!
\brief handle CDC ACM data in
\param[in] udev: pointer to USB device instance
\param[in] ep_num: endpoint identifier
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_in (usb_dev *udev, uint8_t ep_num)
{
usb_transc *transc = &udev->dev.transc_in[EP_ID(ep_num)];
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
if ((0U == transc->xfer_len % transc->max_len) && (0U != transc->xfer_len)) {
usbd_ep_send (udev, ep_num, NULL, 0U);
} else {
cdc->packet_sent = 1U;
}
return USBD_OK;
}
/*!
\brief handle CDC ACM data out
\param[in] udev: pointer to USB device instance
\param[in] ep_num: endpoint identifier
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_out (usb_dev *udev, uint8_t ep_num)
{
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
cdc->packet_receive = 1U;
cdc->receive_length = ((usb_core_driver *)udev)->dev.transc_out[ep_num].xfer_count;
// ��������
//Ϊ��һ�ν�������
usbd_ep_recev (udev, CDC_DATA_OUT_EP, usb_data_bufferReceive, 64);
Myreceive_length = cdc->receive_length;
return USBD_OK;
}
USART + DMA 串口代码不是这个工程的 但是也提供 以供参考
#include "stdio.h"
#include "stdbool.h"
#include "usart.h"
#include "systick.h"
#define USART0_DATA_ADDRESS ((uint32_t)&USART_DATA(USART0))
bool DMA_Send = false;
uint8_t tx_buffer[64];
uint8_t rx_buffer[64];
/*!
\brief configure COM port
\param[in] COM: COM on the board
\arg COM0: COM on the board
\param[out] none
\retval none
*/
void gd_com_init(uint32_t com,uint32_t baud)
{
/* enable GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_USART0);
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9);
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_10);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
/* configure USART Rx as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* USART configure */
usart_deinit(com);
usart_baudrate_set(com,baud);
usart_parity_config(com,USART_PM_NONE);
usart_stop_bit_set(com,USART_STB_1BIT);
usart_receive_config(com, USART_RECEIVE_ENABLE);
usart_transmit_config(com, USART_TRANSMIT_ENABLE);
usart_dma_receive_config(com, USART_RECEIVE_DMA_ENABLE);
usart_dma_transmit_config(com,USART_TRANSMIT_DMA_ENABLE);
usart_enable(com);
nvic_irq_enable(USART0_IRQn, 0, 0);
usart_interrupt_enable(com, USART_INT_IDLE); //�����ж�
usart_flag_clear(com,USART_FLAG_IDLE);
}
void usart_dma_config(void)
{
dma_single_data_parameter_struct dma_init_struct;
/* enable DMA1 */
rcu_periph_clock_enable(RCU_DMA1);
/* deinitialize DMA channel7(USART0 tx) */
dma_deinit(DMA1, DMA_CH7);
dma_init_struct.direction = DMA_MEMORY_TO_PERIPH;
dma_init_struct.memory0_addr = (uint32_t)tx_buffer;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
dma_init_struct.number = 64;
dma_init_struct.periph_addr = USART0_DATA_ADDRESS;
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_single_data_mode_init(DMA1, DMA_CH7, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA1, DMA_CH7);
dma_channel_subperipheral_select(DMA1, DMA_CH7, DMA_SUBPERI4);
//dma_channel_enable(DMA1, DMA_CH7);
nvic_irq_enable(DMA1_Channel7_IRQn,2,0);
dma_interrupt_enable(DMA1,DMA_CH7,DMA_CHXCTL_FTFIE);
dma_deinit(DMA1, DMA_CH2);
dma_init_struct.direction = DMA_PERIPH_TO_MEMORY;
dma_init_struct.memory0_addr = (uint32_t)rx_buffer;
dma_single_data_mode_init(DMA1, DMA_CH2, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA1, DMA_CH2);
dma_channel_subperipheral_select(DMA1, DMA_CH2, DMA_SUBPERI4);
dma_channel_enable(DMA1, DMA_CH2);
//nvic_irq_enable(DMA1_Channel2_IRQn,2,0);
//dma_interrupt_enable(DMA1,DMA_CH2,DMA_CHXCTL_FTFIE);
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(COM0, (uint8_t)ch);
while(RESET == usart_flag_get(COM0, USART_FLAG_TBE));
return ch;
}
//���ڵ��ַ�����
void com_put_char(uint8_t dat)
{
usart_data_transmit(COM0, dat);
while(RESET == usart_flag_get(COM0, USART_FLAG_TBE));
}
//�����ַ�������
void uart_printf(char *str)
{
unsigned int k=0;
do
{
com_put_char( *(str + k) );
k++;
} while(*(str + k)!='\0');
}
//�����ַ������߽ṹ�巢��
void uart_send_data(uint8_t* data, int len)
{
uint8_t i;
for(i = 0; i < len; i++)
{
com_put_char(data[i]);
}
}
void usart_dma_send(uint8_t len)
{
dma_channel_disable(DMA1,DMA_CH7);
dma_transfer_number_config(DMA1,DMA_CH7,len);
dma_channel_enable(DMA1,DMA_CH7);
// while(usart_flag_get(USART1,USART_FLAG_TC)!=RESET);
}
void usart_dma_en(void)
{
// while(DMA_Send);
// DMA_Send = false;
dma_flag_clear(DMA1, DMA_CH2, DMA_FLAG_FTF);
dma_transfer_number_config(DMA1,DMA_CH2,64);
dma_channel_enable(DMA1,DMA_CH2);
}
void usart_send(uint8_t *buff,uint8_t len)
{
for(int i = 0;i<len;i++)
{
usart_data_transmit(USART0, buff[i]);
delay_1ms(5);
}
}
USART中断处理函数:
void USART0_IRQHandler(void)
{
if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE))
{
dma_channel_disable(DMA1,DMA_CH2);
//usart_interrupt_flag_clear(USART0,USART_INT_FLAG_IDLE);
usart_data_receive(USART0); //CLEAR FLAG
DMA_ReceLen = (64-(dma_transfer_number_get(DMA1,DMA_CH2)));
if((DMA_ReceLen==2)&&(rx_buffer[0]==0xAA))
{
CMD_handle(DMA_ReceLen);
}
if((DMA_ReceLen==3)&&(rx_buffer[0]==0xAC))
{
CMD_handle(DMA_ReceLen);
}
usart_dma_en();
}
}
void DMA1_Channel7_IRQHandler(void) //tx
{
if(dma_interrupt_flag_get(DMA1, DMA_CH7, DMA_INTF_FTFIF)) {
dma_interrupt_flag_clear(DMA1, DMA_CH7, DMA_INTF_FTFIF);
dma_channel_disable(DMA1,DMA_CH7);
// DMA_Send = true;
}
}
void DMA1_Channel2_IRQHandler(void) //rx
{
if(dma_interrupt_flag_get(DMA1, DMA_CH2, DMA_INTF_FTFIF)) {
dma_interrupt_flag_clear(DMA1, DMA_CH2, DMA_INTF_FTFIF);
}
}