main.c 7.85 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
61
#include "crc16-ccitt.h"

62

schneider's avatar
schneider committed
63
64
#define GPIO_PORT_IN                PORT_1
#define GPIO_PIN_IN                 PIN_6
65
66


schneider's avatar
schneider committed
67
68
69
#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)
70

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

schneider's avatar
schneider committed
75
bool mount(void)
76
{
schneider's avatar
schneider committed
77
78
79
80
81
    FRESULT res;
    res=f_mount(&FatFs,"/",0);
    if(res != FR_OK) {
        printf("f_mount error %d\n", res);
        return false;
82
83
    }

schneider's avatar
schneider committed
84
85
86
87
    res = f_opendir(&dir, "0:");
    if(res != FR_OK) {
        printf("f_opendir error %d\n", res);
        return false;
88
89
    }

schneider's avatar
schneider committed
90
91
    return true;
}
92

schneider's avatar
schneider committed
93
94
95
96
97
98
99
100
101
102
103
104
105
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;
    }
106

107
    uint16_t crcval = 0;
schneider's avatar
schneider committed
108
109
110
111
112
113
    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;
114
        }
115
        crcval = crc16_ccitt(crcval, data, readbytes);
schneider's avatar
schneider committed
116
    } while (readbytes == sizeof(data));
117

schneider's avatar
schneider committed
118
119
120
121
122
123
124
    f_close(&file);

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

schneider's avatar
schneider committed
128
bool is_update_needed(void)
129
{
schneider's avatar
schneider committed
130
131
132
133
134
135
136
137
138
139
    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;
140
141
    }

schneider's avatar
schneider committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    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));
156

schneider's avatar
schneider committed
157
    f_close(&file);
158

schneider's avatar
schneider committed
159
160
    //different = true;
    return different;
161
162
}

schneider's avatar
schneider committed
163
void erase_partition(void)
164
{
schneider's avatar
schneider committed
165
166
167
168
    int ret = FLC_MultiPageErase(PARTITION_START, PARTITION_END);
    if(ret != E_NO_ERROR) {
        printf("FLC_MultiPageErase failed with %d\n", ret);
        while(1);
169
170
171
    }
}

schneider's avatar
schneider committed
172
void flash_partition(void)
173
{
schneider's avatar
schneider committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    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);
205
206
}

schneider's avatar
schneider committed
207
208
#if 0
void test(void)
209
{
schneider's avatar
schneider committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    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);
228
}
schneider's avatar
schneider committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#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");
};

244
245

/******************************************************************************/
schneider's avatar
schneider committed
246
int main(void)
247
{
schneider's avatar
schneider committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

    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);
266
267
268
    }


schneider's avatar
schneider committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
    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.
    }
291
292
293
294
295
296
297
}

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