-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathscript.c
More file actions
151 lines (127 loc) · 4.57 KB
/
script.c
File metadata and controls
151 lines (127 loc) · 4.57 KB
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
/**
* @file script.c
*
* @brief This file contains script processing functions
*
* @author Thomas Fischl
* @copyright (c) 2013-2014 Thomas Fischl
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <inttypes.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "clock.h"
#include "hal.h"
#include "isp.h"
#include "counter.h"
#include "script.h"
/**
* @brief Pointer to script data in flash memory
*/
unsigned char scriptdata[] SCRIPT_SECTION = {SCRIPT_CMD_END}; // dummy (is overwritten by hex creator)
/**
* @brief Execute script stored in flash memory
* @retval 1 Everything okay
* @retval 0 Error occured
*/
uint8_t script_run() {
DEFINE_DATAPOINTER;
uint8_t cmd;
while (1) {
cmd = flash_readbyte(scriptdata_p++);
uint8_t success = 0;
switch (cmd) {
case SCRIPT_CMD_CONNECT:
success = isp_connect(flash_readbyte(scriptdata_p++));
break;
case SCRIPT_CMD_DISCONNECT:
success = isp_disconnect();
break;
case SCRIPT_CMD_WAIT:
{
uint8_t loops = flash_readbyte(scriptdata_p++);
while (loops--) {
clock_delayFast(CLOCK_TICKER_FAST_10MS);
}
success = 1;
}
break;
case SCRIPT_CMD_SPI_SEND:
{
uint8_t data[4];
uint8_t i;
for (i = 0; i < 4; i++)
data[i] = flash_readbyte(scriptdata_p++);
isp_transmit(data, sizeof (data));
success = 1;
}
break;
case SCRIPT_CMD_SPI_VERIFY:
{
uint8_t data[4];
uint8_t i;
uint8_t verifybyte;
for (i = 0; i < 4; i++)
data[i] = flash_readbyte(scriptdata_p++);
verifybyte = flash_readbyte(scriptdata_p++);
isp_transmit(data, sizeof (data));
if (data[3] == verifybyte) success = 1;
else success = 0;
}
break;
case SCRIPT_CMD_FLASH:
case SCRIPT_CMD_EEPROM:
{
uint32_t address = (uint32_t) flash_readbyte(scriptdata_p++) << 24;
address |= (uint32_t) flash_readbyte(scriptdata_p++) << 16;
address |= (uint32_t) flash_readbyte(scriptdata_p++) << 8;
address |= (uint32_t) flash_readbyte(scriptdata_p++);
uint32_t length = (uint32_t) flash_readbyte(scriptdata_p++) << 24;
length |= (uint32_t) flash_readbyte(scriptdata_p++) << 16;
length |= (uint32_t) flash_readbyte(scriptdata_p++) << 8;
length |= (uint32_t) flash_readbyte(scriptdata_p++);
uint16_t pagesize = (uint16_t) flash_readbyte(scriptdata_p++) << 8;
pagesize |= (uint16_t) flash_readbyte(scriptdata_p++);
if (cmd == SCRIPT_CMD_FLASH) {
isp_writeFlash(scriptdata_p, address, length, pagesize);
success = isp_verifyFlash(scriptdata_p, address, length);
} else {
isp_writeEEPROM(scriptdata_p, address, length, pagesize);
success = isp_verifyEEPROM(scriptdata_p, address, length);
}
scriptdata_p += length;
}
break;
case SCRIPT_CMD_DECCOUNTER:
{
uint16_t startvalue = (uint16_t) flash_readbyte(scriptdata_p++) << 8;
startvalue |= (uint16_t) flash_readbyte(scriptdata_p++);
counter_decrement(startvalue);
success = 1;
}
break;
case SCRIPT_CMD_END:
return 1;
break;
}
if (!success) {
isp_disconnect();
return 0;
}
}
}