📄 cdc_task.c
字号:
//!
//! @file cdc_task.c,v
//!
//! Copyright (c) 2004 Atmel.
//!
//! Please read file license.txt for copyright notice.
//!
//! @brief This file manages the CDC task.
//!
//!
//! @version 1.6 at90usb128-demo-cdc-1_0_3 $Id: cdc_task.c,v 1.6 2006/03/17 13:06:22 rletendu Exp $
//!
//! @todo
//! @bug
//!/
//_____ I N C L U D E S ___________________________________________________
#include "config.h"
#include "conf_usb.h"
#include "cdc_task.h"
#include "lib_mcu\usb\usb_drv.h"
#include "usb_descriptors.h"
#include "modules\usb\device_chap9\usb_standard_request.h"
#include "usb_specific_request.h"
#include "lib_mcu/uart/uart_lib.h"
#include "uart_usb_lib.h"
#include <stdio.h>
//_____ M A C R O S ________________________________________________________
//_____ D E F I N I T I O N S ______________________________________________
//_____ D E C L A R A T I O N S ____________________________________________
volatile U8 cpt_sof;
extern U8 rx_counter;
extern U8 tx_counter;
S_line_coding line_coding;
unsigned char flag=0,init=0,dataa=0,datab=0,datac=0,chp=0;
unsigned char Io_con_on(unsigned char a, unsigned char b)
{
switch(a)
{
case 0:
DDRA|=(1<<b);
PORTA|=(1<<b);
if(PINA&(1<<b))
return 1;
else return 0;
break;
case 1:
DDRB|=(1<<b);
PORTB|=(1<<b);
if(PINB&(1<<b))
return 1;
else return 0;
break;
case 2:
DDRC|=(1<<b);
PORTC|=(1<<b);
if(PINC&(1<<b))
return 1;
else return 0;
break;
case 3:
DDRD|=(1<<b);
PORTD|=(1<<b);
if(PIND&(1<<b))
return 1;
else return 0;
break;
case 4:
DDRE|=(1<<b);
PORTE|=(1<<b);
if(PINE&(1<<b))
return 1;
else return 0;
break;
case 5:
DDRF|=(1<<b);
PORTF|=(1<<b);
if(PINF&(1<<b))
return 1;
else return 0;
break;
default:
break;
}
return 0;
}
unsigned char Io_con_off(unsigned char a, unsigned char b)
{
switch(a)
{
case 0:
DDRA&=~(1<<b);
PORTA&=~(1<<b);
if(PINA&(1<<b))
return 0;
else return 1;
break;
case 1:
DDRB&=~(1<<b);
PORTB&=~(1<<b);
if(PINB&(1<<b))
return 0;
else return 1;
break;
case 2:
DDRC&=~(1<<b);
PORTC&=~(1<<b);
if(PINC&(1<<b))
return 0;
else return 1;
break;
case 3:
DDRD&=~(1<<b);
PORTD&=~(1<<b);
if(PIND&(1<<b))
return 0;
else return 1;
break;
case 4:
DDRE&=~(1<<b);
PORTE&=~(1<<b);
if(PINE&(1<<b))
return 0;
else return 1;
break;
case 5:
DDRF&=~(1<<b);
PORTF&=~(1<<b);
if(PINF&(1<<b))
return 0;
else return 1;
break;
default:
break;
}
return 0;
}
#pragma vector=(0X50)/*TIMER1_OVF_vect*/
__interrupt void ms10_count(void)
{
datac--;
//printf("%d/r/n",datac);
TCNT1H+=0XD8;
TCNT1L+=0XEF;
}
void ms10_init(void)
{
CLKPR|=(1<<7);
CLKPR&=0X80;
TCCR1B=0X01;
TCNT1H=0XD8;
TCNT1L=0XEF;
TIMSK1|=(1<<0);
SREG|=(1<<7);
}
/*void delay(unsigned char ms)
{
unsigned int i,j,k,l;
for(i=0;i<ms;i++)
for(l=0;l<1000;l++)
for(k=0;k<10000;k++)
for(j=0;j<11410;j++);
}*/
//! @brief This function initializes the hardware ressources required for CDC demo.
//!
//!
//! @param none
//!
//! @return none
//!
//!/
void cdc_task_init(void)
{
uart_init();
Leds_init();
Joy_init();
Hwb_button_init();
Usb_enable_sof_interrupt();
#ifdef AVRGCC
fdevopen(uart_usb_putchar,uart_usb_getchar); //for printf redirection
#endif
}
//! @brief Entry point of the uart cdc management
//!
//! This function links the uart and the USB bus.
//!
//! @param none
//!
//! @return none
void cdc_task(void)
{
if(Is_device_enumerated()) //Enumeration processs OK ?
{
if(cpt_sof>=NB_MS_BEFORE_FLUSH && tx_counter!=0 ) //Flush buffer in Timeout
{
cpt_sof=0;
uart_usb_flush();
}
if (uart_test_hit()) //Something on USART ?
{
uart_usb_putchar(uart_getchar()); // Loop back, USART to USB
Led0_toggle();
}
if (Uart_tx_ready()) //USART free ?
{
if (uart_usb_test_hit()) // Something received from the USB ?
{
/* if((!init)&&(!flag)){
while(rx_counter)
{
while(!(init=uart_usb_getchar()));
flag=1;
//printf("IO port is open.\r\n");
//printf("%c\r\n",init);
// printf("which PORT ?\r\n");
break;
}}*/
if((!flag)&&(!dataa)){
while(rx_counter)
{
while(!(dataa=uart_usb_getchar()));
flag=1;
//printf("%c\r\n",dataa);
//printf("which OUT ?\r\n");
break;
}}
if((!flag)&&(!datab)){
while(rx_counter)
{
while(!(datab=uart_usb_getchar()));
flag=1;
//printf("%c\r\n",datab);
//printf("TIME ?\r\n");
break;
}}
if((!flag)&&(!datac)){
while(rx_counter)
{
while(!(datac=uart_usb_getchar()));
flag=1;
//printf("%d ms\r\n",(datac*10));
break;
}}
}
}
if(dataa&&datab&&datac)
{
//printf("PORT %c out %c is open.\r\n",(dataa+17),(datab));
Io_con_on((dataa-48),(datab-48));
ms10_init();
while(datac)
{
printf("");
}
SREG&=~(1<<7);
Io_con_off((dataa-48),(datab-48));
//printf("PORT %c out %c is closed.\r\n",(dataa+17),(datab));
dataa=0;
datab=0;
datac=0;
init=0;
}
/*if (Uart_tx_ready()) //USART free ?
{
if (uart_usb_test_hit()) // Something received from the USB ?
{
while (rx_counter)
{
uart_putchar(uart_usb_getchar()); // loop back USB to USART
Led3_toggle();
}
}
}*/
flag=0;
if ( cpt_sof>=REPEAT_KEY_PRESSED) //Debounce joystick events
{
if (Is_joy_select())
printf ("Select Pressed !\r\n");
if (Is_joy_right())
printf ("Right Pressed !\r\n");
if (Is_joy_left())
printf ("Left Pressed !\r\n");
if (Is_joy_down())
printf ("Down Pressed !\r\n");
if (Is_joy_up())
printf ("Up Pressed !\r\n");
if(Is_hwb())
printf("Hello from AT90USBXXX !\r\n");
}
}
}
//! @brief sof_action
//!
//! This function increments the cpt_sof counter each times
//! the USB Start Of Frame interrupt subroutine is executed (1ms)
//! Usefull to manage time delays
//!
//! @warning Code:?? bytes (function code length)
//!
//! @param none
//!
//! @return none
void sof_action()
{
cpt_sof++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -