Merge CDC-ECM, RNG, DFU bootloader, watchdog, settings manager, energy scan routines of Robert Quattlebaum

This commit is contained in:
dak664 2010-09-17 21:59:09 +00:00
parent 7b529cac03
commit 110bc0242e
42 changed files with 4739 additions and 2669 deletions

View file

@ -50,10 +50,12 @@
#include "usb_descriptors.h"
#include "usb_specific_request.h"
#include "rndis/rndis_protocol.h"
#include "rndis/cdc_ecm.h"
#include "serial/uart_usb_lib.h"
#include "storage/ctrl_access.h"
#include "uip.h"
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <string.h>
//_____ M A C R O S ________________________________________________________
@ -85,30 +87,44 @@ Bool usb_user_read_request(U8 type, U8 request)
{
U16 wLength;
//Both protocols have two bytes we throw away
Usb_read_byte();
Usb_read_byte();
switch(request)
{
case SEND_ENCAPSULATED_COMMAND:
Usb_read_byte();
Usb_read_byte();
Usb_read_byte();//wIndex LSB
Usb_read_byte();//wIndex MSB
LSB(wLength) = Usb_read_byte();
MSB(wLength) = Usb_read_byte();
return send_encapsulated_command(wLength);
if((usb_configuration_nb==USB_CONFIG_RNDIS)||(usb_configuration_nb==USB_CONFIG_RNDIS_DEBUG))
return rndis_send_encapsulated_command(wLength);
else
return FALSE;
break;
case GET_ENCAPSULATED_COMMAND:
Usb_read_byte();
Usb_read_byte();
Usb_read_byte();//wIndex LSB
Usb_read_byte();//wIndex MSB
LSB(wLength) = Usb_read_byte();
MSB(wLength) = Usb_read_byte();
return get_encapsulated_command();
if((usb_configuration_nb==USB_CONFIG_RNDIS)||(usb_configuration_nb==USB_CONFIG_RNDIS_DEBUG))
return rndis_get_encapsulated_command();
else
return FALSE;
break;
case SET_ETHERNET_PACKET_FILTER:
if((usb_configuration_nb==USB_CONFIG_ECM) || (usb_configuration_nb==USB_CONFIG_ECM_DEBUG)) {
cdc_ecm_set_ethernet_packet_filter();
return TRUE;
} else
return FALSE;
break;
#if USB_CONF_STORAGE
case MASS_STORAGE_RESET:
Usb_ack_receive_setup();
@ -130,18 +146,34 @@ Bool usb_user_read_request(U8 type, U8 request)
/* We don't have a real serial port - so these aren't applicable. We
advertise that we support nothing, so shouldn't get them anyway */
case GET_LINE_CODING:
cdc_get_line_coding();
return TRUE;
Usb_read_byte();
Usb_read_byte();
if(USB_CONFIG_HAS_DEBUG_PORT(usb_configuration_nb)) {
cdc_get_line_coding();
return TRUE;
} else {
return FALSE;
}
break;
case SET_LINE_CODING:
cdc_set_line_coding();
return TRUE;
Usb_read_byte();
Usb_read_byte();
if(USB_CONFIG_HAS_DEBUG_PORT(usb_configuration_nb)) {
cdc_set_line_coding();
return TRUE;
} else {
return FALSE;
}
break;
case SET_CONTROL_LINE_STATE:
cdc_set_control_line_state();
return TRUE;
if(USB_CONFIG_HAS_DEBUG_PORT(usb_configuration_nb)) {
cdc_set_control_line_state();
return TRUE;
} else {
return FALSE;
}
break;
#endif /* USB_CONF_CDC */
default:
@ -174,21 +206,6 @@ Bool usb_user_get_descriptor(U8 type, U8 string)
pbuffer = &(usb_user_language_id.bLength);
return TRUE;
break;
case MAN_INDEX:
data_to_transfer = sizeof (usb_user_manufacturer_string_descriptor);
pbuffer = &(usb_user_manufacturer_string_descriptor.bLength);
return TRUE;
break;
case PROD_INDEX:
data_to_transfer = sizeof (usb_user_product_string_descriptor);
pbuffer = &(usb_user_product_string_descriptor.bLength);
return TRUE;
break;
case SN_INDEX:
data_to_transfer = sizeof (usb_user_serial_number);
pbuffer = &(usb_user_serial_number.bLength);
return TRUE;
break;
default:
return FALSE;
}
@ -198,6 +215,94 @@ Bool usb_user_get_descriptor(U8 type, U8 string)
return FALSE;
}
static char itoh(unsigned char i) {
char ret;
ret = pgm_read_byte_near(PSTR("0123456789abcdef")+(i&0xF));
return ret;
}
const char* usb_user_get_string_sram(U8 string_type) {
static char serial[13];
uint8_t i;
switch (string_type)
{
case USB_STRING_SERIAL:
case USB_STRING_MAC_ADDRESS:
{
uint8_t mac_address[6];
usb_eth_get_mac_address(mac_address);
for(i=0;i<6;i++) {
serial[i*2] = itoh(mac_address[i]>>4);
serial[i*2+1] = itoh(mac_address[i]);
}
}
break;
default:
serial[0] = 0;
break;
}
return serial;
}
PGM_P usb_user_get_string(U8 string_type) {
switch (string_type)
{
case USB_STRING_MAN:
return PSTR("Atmel");
case USB_STRING_PRODUCT:
return PSTR("Jackdaw 6LoWPAN Adaptor");
case USB_STRING_CONFIG_COMPOSITE:
return PSTR("RNDIS+Debug");
case USB_STRING_CONFIG_RNDIS:
case USB_STRING_INTERFACE_RNDIS:
return PSTR("RNDIS");
case USB_STRING_CONFIG_EEM:
case USB_STRING_INTERFACE_EEM:
return PSTR("CDC-EEM");
case USB_STRING_CONFIG_ECM:
case USB_STRING_INTERFACE_ECM:
return PSTR("CDC-ECM");
case USB_STRING_CONFIG_ECM_DEBUG:
return PSTR("CDC-ECM+DEBUG");
case USB_STRING_INTERFACE_SERIAL:
return PSTR("Debug Port");
/*
case USB_STRING_INTERFACE_ECM_ATTACHED:
return PSTR("Attached");
case USB_STRING_INTERFACE_ECM_DETACHED:
return PSTR("Detached");
*/
#if USB_CONF_STORAGE
case USB_STRING_CONFIG_MS:
case USB_STRING_INTERFACE_MS:
return PSTR("Mass Storage");
#endif
/*
// This is now handled by usb_user_get_string_sram.
case USB_STRING_SERIAL:
return PSTR("JD01");
case USB_STRING_MAC_ADDRESS:
return PSTR("021213141516");
*/
default:
return NULL;
}
}
//! usb_user_endpoint_init.
//!
@ -206,115 +311,54 @@ Bool usb_user_get_descriptor(U8 type, U8 string)
//! @param conf_nb Not used
void usb_user_endpoint_init(U8 conf_nb)
{
if(USB_CONFIG_HAS_DEBUG_PORT(conf_nb)) {
uart_usb_configure_endpoints();
}
/* If we use RNDIS endpoints */
if ((usb_mode == rndis_only) || (usb_mode == rndis_debug)) {
switch(conf_nb) {
case USB_CONFIG_ECM:
case USB_CONFIG_ECM_DEBUG:
cdc_ecm_configure_endpoints();
break;
usb_configure_endpoint(INT_EP, \
TYPE_INTERRUPT, \
DIRECTION_IN, \
SIZE_64, \
ONE_BANK, \
NYET_ENABLED);
case USB_CONFIG_RNDIS_DEBUG:
case USB_CONFIG_RNDIS:
rndis_configure_endpoints();
break;
case USB_CONFIG_EEM:
cdc_ecm_configure_endpoints();
usb_configure_endpoint(TX_EP, \
TYPE_BULK, \
DIRECTION_IN, \
SIZE_64, \
TWO_BANKS, \
NYET_ENABLED);
break;
#if USB_CONF_STORAGE
case USB_CONFIG_MS:
usb_configure_endpoint(VCP_INT_EP, \
TYPE_INTERRUPT, \
DIRECTION_IN, \
SIZE_32, \
ONE_BANK, \
NYET_ENABLED);
usb_configure_endpoint(RX_EP, \
TYPE_BULK, \
DIRECTION_OUT, \
SIZE_64, \
TWO_BANKS, \
NYET_ENABLED);
usb_configure_endpoint(VCP_TX_EP, \
TYPE_BULK, \
DIRECTION_IN, \
SIZE_32, \
TWO_BANKS, \
NYET_ENABLED);
}
/* If we use virtual comm port (VCP) endpoints */
if (usb_mode == rndis_debug) {
usb_configure_endpoint(VCP_INT_EP, \
TYPE_INTERRUPT, \
DIRECTION_IN, \
SIZE_32, \
ONE_BANK, \
NYET_ENABLED);
usb_configure_endpoint(VCP_TX_EP, \
TYPE_BULK, \
DIRECTION_IN, \
SIZE_32, \
TWO_BANKS, \
NYET_ENABLED);
usb_configure_endpoint(VCP_RX_EP, \
TYPE_BULK, \
DIRECTION_OUT, \
SIZE_32, \
TWO_BANKS, \
NYET_ENABLED);
}
/* If we use mass storage endpoints */
if (usb_mode == mass_storage) {
usb_configure_endpoint(MS_IN_EP, \
TYPE_BULK, \
DIRECTION_IN, \
SIZE_64, \
ONE_BANK, \
NYET_ENABLED);
usb_configure_endpoint(MS_OUT_EP, \
TYPE_BULK, \
DIRECTION_OUT, \
SIZE_64, \
ONE_BANK, \
NYET_ENABLED);
}
if (usb_mode == eem ) {
usb_configure_endpoint(TX_EP, \
TYPE_BULK, \
DIRECTION_IN, \
SIZE_64, \
TWO_BANKS, \
NYET_ENABLED);
usb_configure_endpoint(RX_EP, \
TYPE_BULK, \
DIRECTION_OUT, \
SIZE_64, \
TWO_BANKS, \
NYET_ENABLED);
}
if ((usb_mode == rndis_only) || (usb_mode == rndis_debug)) {
Usb_reset_endpoint(INT_EP);
Usb_reset_endpoint(TX_EP);
Usb_reset_endpoint(RX_EP);
}
if (usb_mode == rndis_debug){
Usb_reset_endpoint(VCP_INT_EP);
Usb_reset_endpoint(VCP_TX_EP);
Usb_reset_endpoint(VCP_RX_EP);
}
if (usb_mode == mass_storage) {
Usb_reset_endpoint(VCP_TX_EP);
Usb_reset_endpoint(VCP_RX_EP);
}
if (usb_mode == eem){
Usb_reset_endpoint(TX_EP);
Usb_reset_endpoint(RX_EP);
}
usb_configure_endpoint(VCP_RX_EP, \
TYPE_BULK, \
DIRECTION_OUT, \
SIZE_32, \
TWO_BANKS, \
NYET_ENABLED);
Usb_reset_endpoint(VCP_INT_EP);
Usb_reset_endpoint(VCP_TX_EP);
Usb_reset_endpoint(VCP_RX_EP);
break;
#endif
}
Led0_on();
}
#if USB_CONF_CDC
@ -337,12 +381,7 @@ void cdc_get_line_coding(void)
Usb_write_byte(line_coding.bParityType);
Usb_write_byte(line_coding.bDataBits);
Usb_send_control_in();
while(!(Is_usb_read_control_enabled()));
//Usb_clear_tx_complete();
while(!Is_usb_receive_out());
Usb_ack_receive_out();
Usb_send_control_in();
}
@ -352,19 +391,20 @@ void cdc_get_line_coding(void)
//!
void cdc_set_line_coding (void)
{
Usb_ack_receive_setup();
while (!(Is_usb_receive_out()));
LSB0(line_coding.dwDTERate) = Usb_read_byte();
LSB1(line_coding.dwDTERate) = Usb_read_byte();
LSB2(line_coding.dwDTERate) = Usb_read_byte();
LSB3(line_coding.dwDTERate) = Usb_read_byte();
line_coding.bCharFormat = Usb_read_byte();
line_coding.bParityType = Usb_read_byte();
line_coding.bDataBits = Usb_read_byte();
Usb_ack_receive_out();
Usb_ack_receive_setup();
if(usb_endpoint_wait_for_receive_out()==0) {
LSB0(line_coding.dwDTERate) = Usb_read_byte();
LSB1(line_coding.dwDTERate) = Usb_read_byte();
LSB2(line_coding.dwDTERate) = Usb_read_byte();
LSB3(line_coding.dwDTERate) = Usb_read_byte();
line_coding.bCharFormat = Usb_read_byte();
line_coding.bParityType = Usb_read_byte();
line_coding.bDataBits = Usb_read_byte();
Usb_ack_receive_out();
Usb_send_control_in(); // send a ZLP for STATUS phase
while(!(Is_usb_read_control_enabled()));
Usb_send_control_in(); // send a ZLP for STATUS phase
usb_endpoint_wait_for_read_control_enabled();
}
}
//! cdc_set_control_line_state.
@ -375,8 +415,27 @@ void cdc_set_line_coding (void)
//!
void cdc_set_control_line_state (void)
{
U8 controlLineState = Usb_read_byte();
U8 dummy = Usb_read_byte();
U8 interface = Usb_read_byte();
Usb_ack_receive_setup();
Usb_send_control_in();
while(!(Is_usb_read_control_enabled()));
usb_endpoint_wait_for_read_control_enabled();
if(interface == INTERFACE2_NB) {
uart_usb_set_control_line_state(controlLineState);
}
}
#endif /* USB_CONF_CDC */
Bool usb_user_set_alt_interface(U8 interface, U8 alt_setting) {
return FALSE;
if((interface==ECM_INTERFACE0_NB) && ((usb_configuration_nb==USB_CONFIG_ECM) || (usb_configuration_nb==USB_CONFIG_ECM_DEBUG))) {
// The alt_setting in this case corresponds to
// if the interface is enabled or not.
usb_eth_set_active(alt_setting);
}
return TRUE;
}