1 /*!
2     \file    usb_iap_core.h
3     \brief   the header file of IAP driver
4 
5     \version 2020-08-04, V1.1.0, firmware for GD32VF103
6 */
7 
8 /*
9     Copyright (c) 2020, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #ifndef __USB_IAP_CORE_H
36 #define __USB_IAP_CORE_H
37 
38 #include "usbd_enum.h"
39 #include "usb_hid.h"
40 
41 #define USB_SERIAL_STRING_SIZE              0x06U
42 #define USB_DESC_LEN_IAP_REPORT             35U
43 #define USB_DESC_LEN_IAP_CONFIG_SET         41U
44 
45 /* special commands with download request */
46 #define IAP_OPTION_BYTE1                    0x01U
47 #define IAP_ERASE                           0x02U
48 #define IAP_DNLOAD                          0x03U
49 #define IAP_LEAVE                           0x04U
50 #define IAP_GETBIN_ADDRESS                  0x05U
51 #define IAP_OPTION_BYTE2                    0x06U
52 
53 typedef struct
54 {
55     uint8_t report_buf[IAP_OUT_PACKET + 1U];
56     uint8_t option_byte[IAP_IN_PACKET];
57 
58     /* state machine variables */
59     uint8_t dev_status[IAP_IN_PACKET];
60     uint8_t bin_addr[IAP_IN_PACKET];
61 
62     uint8_t reportID;
63     uint8_t flag;
64 
65     uint32_t protocol;
66     uint32_t idlestate;
67 
68     uint16_t transfer_times;
69     uint16_t page_count;
70     uint16_t lps;           /* last packet size */
71     uint32_t file_length;
72     uint32_t base_address;
73 } usbd_iap_handler;
74 
75 typedef  void  (*app_func) (void);
76 
77 extern usb_desc iap_desc;
78 extern usb_class_core iap_class;
79 
80 /* function declarations */
81 /* send iap report */
82 uint8_t iap_report_send (usb_dev *udev, uint8_t *report, uint32_t len);
83 
84 #endif /* __USB_IAP_CORE_H */
85