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

@ -1,383 +0,0 @@
/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file cdc_task.c **********************************************************
*
* \brief
* Manages the CDC-ACM Virtual Serial Port Dataclass for the USB Device
*
* \addtogroup usbstick
*
* \author
* Colin O'Flynn <coflynn@newae.com>
*
******************************************************************************/
/* Copyright (c) 2008 ATMEL Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of the copyright holders nor the names of
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 OWNER 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.
*/
/**
\ingroup usbstick
\defgroup cdctask CDC Task
@{
*/
//_____ I N C L U D E S ___________________________________________________
#include "contiki.h"
#include "usb_drv.h"
#include "usb_descriptors.h"
#include "usb_specific_request.h"
#include "serial/cdc_task.h"
#include "serial/uart_usb_lib.h"
#include "rndis/rndis_protocol.h"
#include "sicslow_ethernet.h"
#if RF230BB
extern void rf230_set_channel(int channel);
extern int rf230_get_channel(void);
#else
#include "radio.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#define BUF ((struct uip_eth_hdr *)&uip_buf[0])
#define PRINTF printf
#define PRINTF_P printf_P
//_____ M A C R O S ________________________________________________________
//_____ D E F I N I T I O N S ______________________________________________
#define IAD_TIMEOUT_DETACH 300
#define IAD_TIMEOUT_ATTACH 600
#define PBUF ((rndis_data_packet_t *) data_buffer)
//_____ D E C L A R A T I O N S ____________________________________________
void menu_print(void);
void menu_process(char c);
extern char usb_busy;
//! Counter for USB Serial port
extern U8 tx_counter;
//! Timers for LEDs
uint8_t led3_timer;
//! Was USB device *just* enumerated?
uint8_t justenumerated = 1;
static uint8_t timer = 0;
static struct etimer et;
PROCESS(cdc_process, "CDC process");
/**
* \brief Communication Data Class (CDC) Process
*
* This is the link between USB and the "good stuff". In this routine data
* is received and processed by CDC-ACM Class
*/
PROCESS_THREAD(cdc_process, ev, data_proc)
{
PROCESS_BEGIN();
uart_usb_init();
while(1) {
// turn off LED's if necessary
if (led3_timer) led3_timer--;
else Led3_off();
if(Is_device_enumerated() && (usb_mode == rndis_debug) && rndis_state && (!usb_busy)) {
if (justenumerated) {
//If we have serial port, set it as output
if (usb_mode == rndis_debug) {
uart_usb_set_stdout();
menu_print();
}
justenumerated = 0;
}
//Flush buffer if timeout
if(timer >= 4 && tx_counter!=0 ){
timer = 0;
uart_usb_flush();
} else {
timer++;
}
while (uart_usb_test_hit()){
menu_process(uart_usb_getchar()); // See what they want
}
}//if (Is_device_enumerated())
if (usb_mode == rndis_debug) {
etimer_set(&et, CLOCK_SECOND/80);
} else {
etimer_set(&et, CLOCK_SECOND);
}
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
} // while(1)
PROCESS_END();
}
/**
\brief Print debug menu
*/
void menu_print(void)
{
PRINTF_P(PSTR("\n\n\r********** Jackdaw Menu ******************\n\r"));
PRINTF_P(PSTR("* *\n\r"));
PRINTF_P(PSTR("* Main Menu: *\n\r"));
PRINTF_P(PSTR("* h,? Print this menu *\n\r"));
PRINTF_P(PSTR("* m Print current mode *\n\r"));
PRINTF_P(PSTR("* s Set to sniffer mode *\n\r"));
PRINTF_P(PSTR("* n Set to network mode *\n\r"));
PRINTF_P(PSTR("* c Set RF channel *\n\r"));
PRINTF_P(PSTR("* 6 Toggle 6lowpan *\n\r"));
PRINTF_P(PSTR("* r Toggle raw mode *\n\r"));
PRINTF_P(PSTR("* u Switch to mass-storage*\n\r"));
PRINTF_P(PSTR("* *\n\r"));
PRINTF_P(PSTR("* Make selection at any time by pressing *\n\r"));
PRINTF_P(PSTR("* your choice on keyboard. *\n\r"));
PRINTF_P(PSTR("******************************************\n\r"));
}
/**
\brief Process incomming char on debug port
*/
void menu_process(char c)
{
static enum menustate_enum /* Defines an enumeration type */
{
normal,
channel
} menustate = normal;
static char channel_string[3];
static uint8_t channel_string_i = 0;
int tempchannel;
if (menustate == channel) {
switch(c) {
case '\r':
case '\n':
channel_string[channel_string_i] = 0;
//Will return zero in event of error...
tempchannel = atoi(channel_string);
//Bounds check only if user had real input
if ( ((channel_string_i) && (tempchannel < 11)) || (tempchannel > 26)) {
PRINTF_P(PSTR("\n\rInvalid input\n\r"));
}
//If valid input, change it
if (tempchannel) {
#if RF230BB
rf230_set_channel(tempchannel);
#else
radio_set_operating_channel(tempchannel);
#endif
// eeprom_write_byte((uint8_t *) 9, tempchannel); //Write channel
// eeprom_write_byte((uint8_t *)10, ~tempchannel); //Bit inverse as check
}
menustate = normal;
break;
case '\b':
if (channel_string_i)
channel_string_i--;
break;
default:
if (channel_string_i > 1) {
menustate = normal;
PRINTF_P(PSTR("\n\rInput too long!\n\r"));
break;
}
channel_string[channel_string_i] = c;
channel_string_i++;
}
} else {
uint8_t i;
switch(c) {
case '\r':
case '\n':
break;
case 'h':
case '?':
menu_print();
break;
case 's':
PRINTF_P(PSTR("Jackdaw now in sniffer mode\n\r"));
usbstick_mode.sendToRf = 0;
usbstick_mode.translate = 0;
break;
case 'n':
PRINTF_P(PSTR("Jackdaw now in network mode\n\r"));
usbstick_mode.sendToRf = 1;
usbstick_mode.translate = 1;
break;
case '6':
if (usbstick_mode.sicslowpan) {
PRINTF_P(PSTR("Jackdaw does not perform 6lowpan translation\n\r"));
usbstick_mode.sicslowpan = 0;
} else {
PRINTF_P(PSTR("Jackdaw now performs 6lowpan translations\n\r"));
usbstick_mode.sicslowpan = 1;
}
break;
case 'r':
if (usbstick_mode.raw) {
PRINTF_P(PSTR("Jackdaw does not capture raw frames\n\r"));
usbstick_mode.raw = 0;
} else {
PRINTF_P(PSTR("Jackdaw now captures raw frames\n\r"));
usbstick_mode.raw = 1;
}
break;
case 'c':
#if RF230BB
PRINTF_P(PSTR("Select 802.15.4 Channel in range 11-26 [%d]: "), rf230_get_channel());
#else
PRINTF_P(PSTR("Select 802.15.4 Channel in range 11-26 [%d]: "), radio_get_operating_channel());
#endif
menustate = channel;
channel_string_i = 0;
break;
case 'm':
PRINTF_P(PSTR("Currently Jackdaw:\n\r * Will "));
if (usbstick_mode.sendToRf == 0) { PRINTF_P(PSTR("not "));}
PRINTF_P(PSTR("send data over RF\n\r * Will "));
if (usbstick_mode.translate == 0) { PRINTF_P(PSTR("not "));}
PRINTF_P(PSTR("change link-local addresses inside IP messages\n\r * Will "));
if (usbstick_mode.sicslowpan == 0) { PRINTF_P(PSTR("not "));}
PRINTF_P(PSTR("decompress 6lowpan headers\n\r * Will "));
if (usbstick_mode.raw == 0) { PRINTF_P(PSTR("not "));}
PRINTF_P(PSTR("Output raw 802.15.4 frames\n\r "));
#if RF230BB
PRINTF_P(PSTR(" * Operates on channel %d\n\r "), rf230_get_channel());
#else
PRINTF_P(PSTR(" * Operates on channel %d\n\r "), radio_get_operating_channel());
#endif
break;
case 'u':
//Mass storage mode
usb_mode = mass_storage;
//No more serial port
stdout = NULL;
//RNDIS is over
rndis_state = rndis_uninitialized;
Leds_off();
//Deatch USB
Usb_detach();
//Wait a few seconds
for(i = 0; i < 50; i++)
_delay_ms(100);
//Attach USB
Usb_attach();
break;
default:
PRINTF_P(PSTR("%c is not a valid option! h for menu\n\r"), c);
break;
}
}
return;
}
/**
@brief This will enable the VCP_TRX_END LED for a period
*/
void vcptx_end_led(void)
{
Led3_on();
led3_timer = 10;
}
/** @} */

View file

@ -1,74 +0,0 @@
/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file cdc_task.h ************************************************************
*
* \brief
* This file manages the CDC task for the virtual COM port.
*
* \addtogroup usbstick
*
* \author
* Colin O'Flynn <coflynn@newae.com>
*
******************************************************************************/
/* Copyright (c) 2008 ATMEL Corporation
Copyright (c) 2008 Colin O'Flynn
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of the copyright holders nor the names of
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 OWNER 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.
*/
#ifndef _CDC_TASK_H_
#define _CDC_TASK_H_
/**
\addtogroup cdctask
@{
*/
//_____ I N C L U D E S ____________________________________________________
#include "config.h"
//_____ M A C R O S ________________________________________________________
//_____ D E C L A R A T I O N S ____________________________________________
void sof_action(void);
void vcptx_end_led(void);
void rawmode_enable(void);
void rawmode_disable(void);
PROCESS_NAME(cdc_process);
/** @} */
#endif /* _CDC_TASK_H_ */

View file

@ -45,7 +45,6 @@
#include "config.h"
#include "usb_drv.h"
#include "usb_descriptors.h"
#include "serial/cdc_task.h"
#include "serial/uart_usb_lib.h"
#include <stdio.h>
@ -56,6 +55,26 @@
/*_____ M A C R O S ________________________________________________________*/
#ifndef USB_CDC_ACM_HOOK_TX_START
#define USB_CDC_ACM_HOOK_TX_START(char)
#endif
#ifndef USB_CDC_ACM_HOOK_TX_END
#define USB_CDC_ACM_HOOK_TX_END(char)
#endif
#ifndef USB_CDC_ACM_HOOK_CLS_CHANGED
#define USB_CDC_ACM_HOOK_CLS_CHANGED(state)
#endif
#ifndef USB_CDC_ACM_HOOK_CONFIGURED
#define USB_CDC_ACM_HOOK_CONFIGURED()
#endif
#ifndef USB_CDC_ACM_CONF_LOCAL_ECHO
#define USB_CDC_ACM_CONF_LOCAL_ECHO 0
#endif
/*_____ D E F I N I T I O N ________________________________________________*/
Uchar tx_counter;
@ -64,22 +83,65 @@ S_line_coding line_coding;
/*_____ D E C L A R A T I O N ______________________________________________*/
void uart_usb_configure_endpoints() {
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
);
Usb_reset_endpoint(VCP_INT_EP);
Usb_reset_endpoint(VCP_TX_EP);
Usb_reset_endpoint(VCP_RX_EP);
USB_CDC_ACM_HOOK_CONFIGURED();
}
int usb_stdout_putchar(char c, FILE *stream)
{
// send to USB port
// don't send anything if USB can't accept chars
Usb_select_endpoint(TX_EP);
if (!uart_usb_tx_ready())
return 0;
// turn on LED
vcptx_end_led();
{
// Preserve the currently selected endpoint
uint8_t uenum = UENUM;
// send to USB port
// don't send anything if USB can't accept chars
Usb_select_endpoint(VCP_TX_EP);
if(usb_endpoint_wait_for_write_enabled()!=0)
return 0;
if(c=='\n')
uart_usb_putchar('\r');
if(c!='\r')
uart_usb_putchar(c);
// Restore previously selected endpoint
UENUM = uenum;
uart_usb_putchar(c);
return 0;
}
}
static FILE usb_stdout = FDEV_SETUP_STREAM(usb_stdout_putchar,
NULL,
@ -100,6 +162,20 @@ void uart_usb_set_stdout(void)
}
static uint8_t uart_usb_control_line_state = 0;
uint8_t uart_usb_get_control_line_state(void) {
return uart_usb_control_line_state;
}
void uart_usb_set_control_line_state(uint8_t control_line_state)
{
uart_usb_control_line_state = control_line_state;
USB_CDC_ACM_HOOK_CLS_CHANGED(control_line_state);
}
/**
* @brief This function checks if the USB emission buffer is ready to accept at
* at least 1 byte
@ -109,6 +185,7 @@ void uart_usb_set_stdout(void)
*/
bit uart_usb_tx_ready(void)
{
Usb_select_endpoint(VCP_TX_EP);
if (!Is_usb_write_enabled())
{
return FALSE;
@ -127,16 +204,31 @@ bit uart_usb_tx_ready(void)
*/
int uart_usb_putchar(int data_to_send)
{
Usb_select_endpoint(VCP_TX_EP);
if(!uart_usb_tx_ready()) return -1;
// Preserve the currently selected endpoint
uint8_t uenum = UENUM;
USB_CDC_ACM_HOOK_TX_START(data_to_send);
Usb_select_endpoint(VCP_TX_EP);
if(!uart_usb_tx_ready()) {
data_to_send=-1;
goto bail;
}
Usb_write_byte(data_to_send);
tx_counter++;
if(!Is_usb_write_enabled()) //If Endpoint full -> flush
{
uart_usb_flush();
}
//If Endpoint full -> flush
if(!Is_usb_write_enabled())
uart_usb_flush();
USB_CDC_ACM_HOOK_TX_END(data_to_send);
bail:
// Restore previously selected endpoint
UENUM = uenum;
return data_to_send;
}
@ -149,6 +241,8 @@ bit uart_usb_test_hit(void)
{
if (!rx_counter)
{
// Preserve the currently selected endpoint
uint8_t uenum = UENUM;
Usb_select_endpoint(VCP_RX_EP);
if (Is_usb_receive_out())
{
@ -158,6 +252,8 @@ bit uart_usb_test_hit(void)
Usb_ack_receive_out();
}
}
// Restore previously selected endpoint
UENUM = uenum;
}
return (rx_counter!=0);
}
@ -174,15 +270,23 @@ char uart_usb_getchar(void)
{
register Uchar data_rx;
// Preserve the currently selected endpoint
uint8_t uenum = UENUM;
Usb_select_endpoint(VCP_RX_EP);
if (!rx_counter) while (!uart_usb_test_hit());
data_rx=Usb_read_byte();
rx_counter--;
if (!rx_counter) Usb_ack_receive_out();
#if USB_CDC_ACM_CONF_LOCAL_ECHO
//Local echo
uart_usb_putchar(data_rx);
#endif
// Restore previously selected endpoint
UENUM = uenum;
return data_rx;
}
@ -193,9 +297,16 @@ char uart_usb_getchar(void)
*/
void uart_usb_flush (void)
{
Usb_select_endpoint(VCP_TX_EP);
Usb_send_in();
// Preserve the currently selected endpoint
uint8_t uenum = UENUM;
Usb_select_endpoint(VCP_TX_EP);
Usb_send_in();
tx_counter = 0;
usb_endpoint_wait_for_write_enabled();
// Restore previously selected endpoint
UENUM = uenum;
}
/** @} */

View file

@ -78,6 +78,10 @@ void uart_usb_flush(void);
bit uart_usb_test_hit(void);
char uart_usb_getchar(void);
void uart_usb_set_stdout(void);
uint8_t uart_usb_get_control_line_state(void);
void uart_usb_set_control_line_state(uint8_t control_line_state);
extern void uart_usb_configure_endpoints();
/** @} **/