forked from Massdrop/mdloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdloader_common.h
186 lines (159 loc) · 5.46 KB
/
mdloader_common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (C) 2018 Massdrop Inc.
*
* This file is part of Massdrop Loader.
*
* Massdrop Loader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Massdrop Loader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Massdrop Loader. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef _MDLOADER_COMMON_H
#define _MDLOADER_COMMON_H
#define PROGRAM_NAME "Massdrop Loader"
#define VERSION_MAJOR 1
#define VERSION_MINOR 3 //0-99
#ifdef _WIN32
#define INITGUID
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include "tchar.h"
#include <conio.h>
#else
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <dirent.h>
#endif //_WIN32
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <getopt.h>
//Atmel files
#include "./atmel/applet.h"
#include "./atmel/status_codes.h"
#define FALSE 0
#define TRUE 1
#ifdef _WIN32
#define slp(ms) Sleep(ms)
extern HANDLE gport;
#else
#define slp(ms) usleep(ms * 1000)
extern int gport;
#endif
extern char verbose;
extern char testmode;
extern int restart_after_program;
extern int hex_cols;
extern int hex_colw;
//These parameters must match the bootloader's
#define SERIAL_MAX_LENGTH 20
//SAM-BA Applet commands
#define CMD_END '#'
#define CMD_SET_NORMAL_MODE 'N' //N# N# done
#define CMD_SET_TERMINAL_MODE 'T' //T# T# NOT SUPPORTING
#define CMD_WRITE_ADDR_8 'O' //Addr,Value# O00004000,AA# done
#define CMD_READ_ADDR_8 'o' //Addr,# o00004000,# done
#define CMD_WRITE_ADDR_16 'H' //Addr,Value# H00004000,AABB# done
#define CMD_READ_ADDR_16 'h' //Addr,# h00004000,# done
#define CMD_WRITE_ADDR_32 'W' //Addr,Value# W00004000,AABBCCDD# done
#define CMD_READ_ADDR_32 'w' //Addr,# w00004000,# done
#define CMD_SEND_FILE 'S' //Addr,Bytes# S00004000,1000# done
#define CMD_RECV_FILE 'R' //Addr,Bytes# R00004000,1000# done
#define CMD_GOTO_ADDR 'G' //Addr# G00004000# done
#define CMD_READ_VERSION 'V' //V# V# done
#define CMD_LOAD_APP 'X' //Load program (Note: Custom to Massdrop Bootloader) done
//Applet info consists of 8 32-bit words found at the end of applet binary
#pragma pack(push, 1)
typedef struct appinfo_s {
uint32_t magic;
uint32_t load_addr;
uint32_t mail_addr;
uint32_t unused[5];
} appinfo_t;
#pragma pack(pop)
//SAM-BA Settings
extern mailbox_t initparams;
extern mailbox_t appletinfo;
extern appinfo_t appinfo;
typedef struct mcu_s {
char name[20]; //MCU Name
int cidr_addr; //Chip ID Address
int cidr; //Chip ID
int flash_size; //Program Memory (FLASH_SIZE)
int ram_size; //Data Memory (HSRAM_SIZE)
int flash_addr; //Program Addr (FLASH_ADDR)
int ram_addr; //Data Addr (HSRAM_ADDR)
} mcu_t;
extern mcu_t mcus[];
extern mcu_t *mcu;
extern uint32_t bootloader_length;
int check_bootloader_write_attempt(int addr);
extern int read_error;
int print_bootloader_serial(void);
int send_mail(mailbox_t *mail);
int read_mail(mailbox_t *mail);
int print_mail(mailbox_t *mail);
#define APPLET_WAIT_MS 10 //Time between retries
#define APPLET_RETRY_NORMAL 10 //Normal operation retries
#define APPLET_RETRY_ERASE 25 //Erase operation retries
int run_applet(mailbox_t *mail);
void display_version(void);
void display_copyright(void);
void display_help(void);
enum command {
CMD_NONE = 0,
CMD_HELP,
CMD_VERSION,
CMD_LISTDEV,
CMD_DOWNLOAD,
CMD_UPLOAD,
CMD_TEST,
CMD_ABORT
};
extern struct option long_options[];
#define COLW 4
#define COLS 8
#define TOKCHAR ' '
#define COLCHAR ' '
void print_hex_listing(char *data, int binfilesize, int markbyte, int base_addr);
int write_word(int addr, int data);
int write_half_word(int addr, int data);
int write_byte(int addr, int data);
int read_byte(int addr);
int read_half_word(int addr);
int read_word(int addr);
int set_terminal_mode(void);
//OS specific commands
void print_com_example(void);
int goto_address(int addr);
char *recv_file(int addr, int bytes);
int send_file(int addr, int bytes, char *data);
int print_bootloader_version(void);
int set_normal_mode(void);
int jump_application(void);
int open_port(char *portname, char silent);
int close_port(char silent);
int config_port(void);
int test_port(char *portname, char silent);
int test_mcu(char silent);
int filesize(char *fname);
int read_data(int addr, int readsize);
int write_data(int addr, int writesize, int data);
void list_devices(char *first);
void strupper(char *str);
void strlower(char *str);
#endif //_MDLOADER_COMMON_H