main.c 8.17 KB
Newer Older
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
/*******************************************************************************
 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated
 * Products, Inc. shall not be used except as stated in the Maxim Integrated
 * Products, Inc. Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products, Inc. retains all
 * ownership rights.
 *
 * $Id: main.c 31505 2017-10-20 21:16:29Z zach.metzinger $
 *
 *******************************************************************************
 */

/**
 * @file    main.c
 * @brief   USB Mass Storage Class example
 * @details This project creates a mass storage device using either on-board RAM or 
 *          external SPI flash memory.  Load the project, connect a cable from the PC
 *          to the USB connector.  A new external drive should appear than can be read
 *          and written.
 */

#include <stdio.h>
#include <stddef.h>
schneider's avatar
schneider committed
48
49
50
#include <stdbool.h>
#include <string.h>

51
52
53
#include "mxc_config.h"
#include "mxc_sys.h"
#include "mxc_delay.h"
schneider's avatar
schneider committed
54
55
56
#include "flc.h"
#include "icc.h"
#include "crc.h"
57
58
#include "board.h"
#include "led.h"
schneider's avatar
schneider committed
59
#include "ff.h"
60

schneider's avatar
schneider committed
61
62
#define GPIO_PORT_IN                PORT_1
#define GPIO_PIN_IN                 PIN_6
63
64


schneider's avatar
schneider committed
65
66
67
#define PARTITION_START (0x10000000 + 64 * 1024)
#define PARTITION_END (0x10000000 + 512 * 1024 - 1) /* TODO: check if 1 MB also works. Might have to enable the second bank */
//#define PARTITION_END (0x10000000 + 1024 * 1024 - 1)
68

schneider's avatar
schneider committed
69
70
71
extern void run_usbmsc(void);
DIR dir;                /* Directory object */
FATFS FatFs;
72

schneider's avatar
schneider committed
73
static void crc_sw(uint32_t *crcval, uint8_t *data, uint8_t len, uint32_t polynomial)
74
{
schneider's avatar
schneider committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	int i, j;
	uint32_t temp;

	for(i = 0; i < len; ++i) {
		temp = (data[i] & 0xFF);
		for(j = 0; j < 8; ++j) {
			if((temp ^ *crcval) & 1) {
				*crcval >>= 1;
				*crcval ^= polynomial;
			} else {
				*crcval >>= 1;
			}
			temp >>= 1;
		}
	}
90
91
92
}


schneider's avatar
schneider committed
93
bool mount(void)
94
{
schneider's avatar
schneider committed
95
96
97
98
99
    FRESULT res;
    res=f_mount(&FatFs,"/",0);
    if(res != FR_OK) {
        printf("f_mount error %d\n", res);
        return false;
100
101
    }

schneider's avatar
schneider committed
102
103
104
105
    res = f_opendir(&dir, "0:");
    if(res != FR_OK) {
        printf("f_opendir error %d\n", res);
        return false;
106
107
    }

schneider's avatar
schneider committed
108
109
    return true;
}
110

schneider's avatar
schneider committed
111
112
113
114
115
116
117
118
119
120
121
122
123
bool check_integrity(void)
{
    FIL file;
    UINT readbytes;
    char *filename = "card10.bin";
    uint8_t data[512];
    FRESULT res;

    res=f_open(&file, filename, FA_OPEN_EXISTING|FA_READ);
    if(res != FR_OK) {
        printf("f_open error %d\n", res);
        return false;
    }
124

schneider's avatar
schneider committed
125
126
127
128
129
130
131
    uint32_t crcval = 0;
    do {
        res = f_read(&file, data, sizeof(data), &readbytes);
        if(res != FR_OK) {
            printf("f_read error %d\n", res);
            crcval = 1; // Make sure to fail the test
            break;
132
        }
schneider's avatar
schneider committed
133
134
        crc_sw(&crcval, data, readbytes, TPU_CRC_CCITT);
    } while (readbytes == sizeof(data));
135

schneider's avatar
schneider committed
136
137
138
139
140
141
142
143
    f_close(&file);

    if(crcval == 0) {
        return true;
    } else {
        printf("CRC check failed. Final CRC: %d\n", crcval);
        return true;
        return false;
144
145
146
    }
}

schneider's avatar
schneider committed
147
bool is_update_needed(void)
148
{
schneider's avatar
schneider committed
149
150
151
152
153
154
155
156
157
158
    FIL file;
    UINT readbytes;
    char *filename = "card10.bin";
    uint8_t data[512];
    FRESULT res;

    res=f_open(&file, filename, FA_OPEN_EXISTING|FA_READ);
    if(res != FR_OK) {
        printf("f_open error %d\n", res);
        return false;
159
160
    }

schneider's avatar
schneider committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    uint8_t *partition = (uint8_t *)(intptr_t)PARTITION_START;
    bool different = false;
    do {
        res = f_read(&file, data, sizeof(data), &readbytes);
        if(res != FR_OK) {
            printf("f_read error %d\n", res);
            break; /* Going to return false, don't want to use this file */
        }
        if(memcmp(partition, data, readbytes)) {
            different = true;
            break;
        }
        partition += readbytes;
    } while (readbytes == sizeof(data));
175

schneider's avatar
schneider committed
176
    f_close(&file);
177

schneider's avatar
schneider committed
178
179
    //different = true;
    return different;
180
181
}

schneider's avatar
schneider committed
182
void erase_partition(void)
183
{
schneider's avatar
schneider committed
184
185
186
187
    int ret = FLC_MultiPageErase(PARTITION_START, PARTITION_END);
    if(ret != E_NO_ERROR) {
        printf("FLC_MultiPageErase failed with %d\n", ret);
        while(1);
188
189
190
    }
}

schneider's avatar
schneider committed
191
void flash_partition(void)
192
{
schneider's avatar
schneider committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    FIL file;
    UINT readbytes;
    char *filename = "card10.bin";
    uint8_t data[512];
    FRESULT res;

    res=f_open(&file, filename, FA_OPEN_EXISTING|FA_READ);
    if(res != FR_OK) {
        printf("f_open error %d\n", res);
        while(1);
    }

    uint32_t partition = PARTITION_START;

    ICC_Disable();
    do {
        res = f_read(&file, data, sizeof(data), &readbytes);
        if(res != FR_OK) {
            printf("f_read error %d\n", res);
            break; /* Going to return false, don't want to use this file */
        }
        int ret = FLC_Write(partition, readbytes, (uint32_t *)data); /* wild cast. not sure if this works */
        if(ret != E_NO_ERROR) {
            printf("FLC_Write failed with %d\n", ret);
            while(1);
        }
        partition += readbytes;
    } while (readbytes == sizeof(data));
    ICC_Enable();

    f_close(&file);
224
225
}

schneider's avatar
schneider committed
226
227
#if 0
void test(void)
228
{
schneider's avatar
schneider committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    FRESULT res;
    FIL file;
    UINT readbytes;
    char *filename = "card10.bin";
    int len = 512;
    char data[512];

    res=f_open(&file, filename, FA_OPEN_EXISTING|FA_READ);
    if(res != FR_OK) printf("f_open error %d\n", res);

    res = f_read(&file, data, len, &readbytes);
    if(res != FR_OK) printf("f_read error %d\n", res);

    f_close(&file);

    data[readbytes] = 0;

    printf("data: %s\n", data);
247
}
schneider's avatar
schneider committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#endif

static inline void boot(const void * vtable){
    SCB->VTOR = (uintptr_t) vtable;

	// Reset stack pointer & branch to the new reset vector.
	__asm(  "mov r0, %0\n"
			"ldr sp, [r0]\n"
			"ldr r0, [r0, #4]\n"
			"bx r0\n"
			:
			: "r"(vtable)
			: "%sp", "r0");
};

263
264

/******************************************************************************/
schneider's avatar
schneider committed
265
int main(void)
266
{
schneider's avatar
schneider committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

    printf("\n\nBootloader\n");

    gpio_cfg_t gpio_in;
    gpio_in.port = GPIO_PORT_IN;
    gpio_in.mask = GPIO_PIN_IN;
    gpio_in.pad = GPIO_PAD_PULL_UP;
    gpio_in.func = GPIO_FUNC_IN;
    GPIO_Config(&gpio_in);

    // If the button is pressed, we go into MSC mode.
    if (!GPIO_InGet(&gpio_in)) {
        run_usbmsc();

        // If we return, don't try to boot. Maybe rather trigger a software reset.
        // Reason: Not sure in which state the USB peripheral is and what kind
        // of interrupts are active.
        while(1);
285
286
287
    }


schneider's avatar
schneider committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    if(mount()) {
        if(check_integrity()) {
            if(is_update_needed()) {
                MXC_FLC0->clkdiv = 54;
                erase_partition();
                flash_partition();
            } else {
                printf("No update needed\n");
            }
        } else {
            printf("Integrity check failed\n");
        }
    }


    printf("Trying to boot\n");
    // boot partition
    boot((uintptr_t *) PARTITION_START);

    while (1) {
        // Should never be reached.
    }
310
311
312
313
314
315
316
}

/******************************************************************************/
void SysTick_Handler(void)
{
    mxc_delay_handler();
}