OpenCores
URL https://opencores.org/ocsvn/gecko3/gecko3/trunk

Subversion Repositories gecko3

[/] [gecko3/] [trunk/] [GECKO3COM/] [gecko3com-fw/] [firmware/] [lib/] [usb_dfu.c] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 nussgipfel
/* GECKO3COM
2
 *
3
 * Copyright (C) 2008 by
4
 *   ___    ____  _   _
5
 *  (  _`\ (  __)( ) ( )
6
 *  | (_) )| (_  | |_| |   Berne University of Applied Sciences
7
 *  |  _ <'|  _) |  _  |   School of Engineering and
8
 *  | (_) )| |   | | | |   Information Technology
9
 *  (____/'(_)   (_) (_)
10
 *
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation, either version 3 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
 
25
//#define USB_DFU_SUPPORT
26
 
27
#include <stdint.h>
28
#include "fx2regs.h"
29
#include "isr.h"
30
#include "usb_requests.h"
31
#include "usb_common.h"
32
#include "usb_dfu.h"
33
#include "debugprint.h"
34
 
35
volatile uint8_t  usb_dfu_status = DFU_STATUS_OK;
36
volatile enum dfu_state usb_dfu_state = DFU_STATE_appIDLE;
37
volatile uint8_t usb_dfu_timeout = DFU_TIMEOUT;
38
 
39
uint8_t usb_handle_dfu_packet (void)
40
{
41
  if ((bRequestType & bmRT_DIR_MASK) == bmRT_DIR_IN){
42
    /*********************************
43
     *    handle the DFU IN requests
44
     ********************************/
45
 
46
    switch (bRequest){
47
 
48
    case DFU_UPLOAD:
49
      usb_dfu_status == DFU_STATUS_errSTALLEDPKT;
50
      return 0;
51
      break;
52
 
53
    case DFU_GETSTATUS:
54
      EP0BUF[0] = usb_dfu_status;
55
      EP0BUF[1] = 0;
56
      EP0BUF[2] = 0;
57
      EP0BUF[3] = 0xff;
58
      EP0BUF[4] = usb_dfu_state;
59
      EP0BUF[5] = 0;
60
      EP0BCH = 0;
61
      EP0BCL = 6;
62
      break;
63
 
64
    case DFU_GETSTATE:
65
      EP0BUF[0] = usb_dfu_state;
66
      EP0BCH = 0;
67
      EP0BCL = 1;
68
      break;
69
 
70
    default:
71
      return 0;
72
    }
73
  }
74
 
75
  else if ((bRequestType & bmRT_DIR_MASK) == bmRT_DIR_OUT){
76
 
77
    /***********************************
78
     *    handle the DFU OUT requests
79
     **********************************/
80
 
81
    switch (bRequest){
82
    case DFU_DETACH:
83
      if(usb_dfu_state == DFU_STATE_appIDLE){
84
        usb_toggle_dfu_handlers();
85
        usb_dfu_state = DFU_STATE_appDETACH;
86
        usb_dfu_timeout = DFU_TIMEOUT;
87
 
88
        /* FIXME start watchdog for usb reset */
89
      }
90
      else {
91
        usb_dfu_status = DFU_STATUS_errSTALLEDPKT;
92
        return 0;
93
      }
94
      break;
95
 
96
    case DFU_DNLOAD:
97
      if((usb_dfu_state == DFU_STATE_dfuIDLE ||
98
          usb_dfu_state == DFU_STATE_dfuDNLOAD_IDLE) &&
99
         wLengthL > 0){
100
        if(!app_firmware_write()){
101
          usb_dfu_status = DFU_STATUS_errWRITE;
102
          usb_dfu_state = DFU_STATE_dfuERROR;
103
          return 0;
104
        }
105
        usb_dfu_state = DFU_STATE_dfuDNLOAD_IDLE;
106
      }
107
      else if(usb_dfu_state == DFU_STATE_dfuDNLOAD_IDLE &&
108
              wLengthL == 0){
109
        usb_toggle_dfu_handlers();
110
        usb_dfu_state = DFU_STATE_dfuMANIFEST_WAIT_RST;
111
      }
112
      else {
113
        usb_dfu_status = DFU_STATUS_errSTALLEDPKT;
114
        return 0;
115
      }
116
      break;
117
 
118
    case DFU_CLRSTATUS:
119
      if(usb_dfu_state == DFU_STATE_dfuERROR){
120
        usb_dfu_status = DFU_STATUS_OK;
121
        usb_dfu_state = DFU_STATE_dfuIDLE;
122
      }
123
      else {
124
        usb_dfu_status = DFU_STATUS_errSTALLEDPKT;
125
        return 0;
126
      }
127
      break;
128
 
129
    case DFU_ABORT:
130
      if(usb_dfu_state != DFU_STATE_appIDLE && \
131
         usb_dfu_state != DFU_STATE_appDETACH){
132
        /*FIXME stop all pending operations */
133
        usb_dfu_status = DFU_STATUS_OK;
134
        usb_dfu_state = DFU_STATE_dfuIDLE;
135
      }
136
      else {
137
        usb_dfu_status = DFU_STATUS_errSTALLEDPKT;
138
        return 0;
139
      }
140
      break;
141
 
142
    default:
143
      usb_dfu_status = DFU_STATUS_errSTALLEDPKT;
144
      return 0;
145
    }
146
  }
147
  else
148
    return 0;    /* invalid bRequestType */
149
 
150
  return 1;
151
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.