2017-02-27 19:31:09 +00:00
|
|
|
#include "board.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "usb/device/dfu/dfu.h"
|
|
|
|
#include "usb/common/dfu/usb_dfu.h"
|
|
|
|
#include "manifest.h"
|
|
|
|
|
2017-02-27 23:21:45 +00:00
|
|
|
#define ALTIF_RAM 0
|
|
|
|
#define ALTIF_FLASH 1
|
|
|
|
|
2017-02-27 19:31:09 +00:00
|
|
|
unsigned int g_unique_id[4];
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------
|
|
|
|
* Callbacks
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
2017-02-27 23:21:45 +00:00
|
|
|
#define RAM_ADDR(offset) (IRAM_ADDR + BOARD_DFU_RAM_SIZE + offset)
|
|
|
|
#define FLASH_ADDR(offset) (IFLASH_ADDR + BOARD_DFU_BOOT_SIZE + offset)
|
|
|
|
|
|
|
|
#define IFLASH_END ((uint8_t *)IFLASH_ADDR + IFLASH_SIZE)
|
|
|
|
#define IRAM_END ((uint8_t *)IRAM_ADDR + IRAM_SIZE)
|
|
|
|
|
|
|
|
/* incoming call-back: Host has transfered 'len' bytes (stored at
|
|
|
|
* 'data'), which we shall write to 'offset' into the partition
|
|
|
|
* associated with 'altif'. Guaranted to be les than
|
|
|
|
* BOARD_DFU_PAGE_SIZE */
|
|
|
|
int USBDFU_handle_dnload(uint8_t altif, unsigned int offset,
|
|
|
|
uint8_t *data, unsigned int len)
|
2017-02-27 19:31:09 +00:00
|
|
|
{
|
2017-02-27 23:21:45 +00:00
|
|
|
uint32_t addr;
|
|
|
|
int rc;
|
|
|
|
|
2017-03-02 23:35:51 +00:00
|
|
|
printf("dnload(altif=%u, offset=%u, len=%u)\n\r", altif, offset, len);
|
2017-02-27 23:21:45 +00:00
|
|
|
|
|
|
|
switch (altif) {
|
|
|
|
case ALTIF_RAM:
|
|
|
|
addr = RAM_ADDR(offset);
|
|
|
|
if (addr > IRAM_ADDR + IRAM_SIZE) {
|
2017-02-28 00:02:24 +00:00
|
|
|
g_dfu->state = DFU_STATE_dfuERROR;
|
|
|
|
g_dfu->status = DFU_STATUS_errADDRESS;
|
2017-02-27 23:21:45 +00:00
|
|
|
return DFU_RET_STALL;
|
|
|
|
}
|
|
|
|
memcpy((void *)addr, data, len);
|
|
|
|
return DFU_RET_ZLP;
|
|
|
|
case ALTIF_FLASH:
|
|
|
|
addr = FLASH_ADDR(offset);
|
|
|
|
if (addr > IFLASH_ADDR + IFLASH_SIZE) {
|
2017-02-28 00:02:24 +00:00
|
|
|
g_dfu->state = DFU_STATE_dfuERROR;
|
|
|
|
g_dfu->status = DFU_STATUS_errADDRESS;
|
2017-02-27 23:21:45 +00:00
|
|
|
return DFU_RET_STALL;
|
|
|
|
}
|
|
|
|
rc = FLASHD_Write(addr, data, len);
|
|
|
|
if (rc != 0) {
|
|
|
|
/* FIXME: set error codes */
|
|
|
|
return DFU_RET_STALL;
|
|
|
|
}
|
|
|
|
return DFU_RET_ZLP;
|
|
|
|
default:
|
|
|
|
/* FIXME: set error codes */
|
|
|
|
TRACE_ERROR("DFU download for unknown AltIf %d\n\r", altif);
|
|
|
|
return DFU_RET_STALL;
|
|
|
|
}
|
2017-02-27 19:31:09 +00:00
|
|
|
}
|
2017-02-27 23:21:45 +00:00
|
|
|
|
|
|
|
/* incoming call-back: Host has requested to read back 'req_len' bytes
|
|
|
|
* starting from 'offset' of the firmware * associated with partition
|
|
|
|
* 'altif' */
|
|
|
|
int USBDFU_handle_upload(uint8_t altif, unsigned int offset,
|
|
|
|
uint8_t *data, unsigned int req_len)
|
|
|
|
{
|
|
|
|
uint32_t addr;
|
|
|
|
|
|
|
|
printf("upload(altif=%u, offset=%u, len=%u)", altif, offset, req_len);
|
|
|
|
|
|
|
|
switch (altif) {
|
|
|
|
case ALTIF_RAM:
|
|
|
|
addr = RAM_ADDR(offset);
|
|
|
|
if (addr > IRAM_ADDR + IRAM_SIZE) {
|
2017-02-28 00:02:24 +00:00
|
|
|
g_dfu->state = DFU_STATE_dfuERROR;
|
|
|
|
g_dfu->status = DFU_STATUS_errADDRESS;
|
2017-02-27 23:21:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((uint8_t *)addr + req_len > IRAM_END)
|
|
|
|
req_len = IRAM_END - (uint8_t *)addr;
|
|
|
|
memcpy(data, (void *)addr, req_len);
|
|
|
|
break;
|
|
|
|
case ALTIF_FLASH:
|
|
|
|
addr = FLASH_ADDR(offset);
|
|
|
|
if (addr > IFLASH_ADDR + IFLASH_SIZE) {
|
2017-02-28 00:02:24 +00:00
|
|
|
g_dfu->state = DFU_STATE_dfuERROR;
|
|
|
|
g_dfu->status = DFU_STATUS_errADDRESS;
|
2017-02-27 23:21:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((uint8_t *)addr + req_len > IFLASH_END)
|
|
|
|
req_len = IFLASH_END - (uint8_t *)addr;
|
|
|
|
memcpy(data, (void *)addr, req_len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
TRACE_ERROR("DFU upload for unknown AltIf %d\n\r", altif);
|
|
|
|
/* FIXME: set error codes */
|
|
|
|
return -1;
|
|
|
|
}
|
2017-03-03 00:33:24 +00:00
|
|
|
printf("=%u\n\r", req_len);
|
2017-02-27 23:21:45 +00:00
|
|
|
return req_len;
|
|
|
|
}
|
|
|
|
|
2017-02-27 19:31:09 +00:00
|
|
|
|
|
|
|
/* returns '1' in case we should break any endless loop */
|
|
|
|
static void check_exec_dbg_cmd(void)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
if (!UART_IsRxReady())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ch = UART_GetChar();
|
|
|
|
|
|
|
|
//board_exec_dbg_cmd(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
|
|
* Main
|
|
|
|
*------------------------------------------------------------------------------*/
|
|
|
|
#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
|
|
|
|
extern int main(void)
|
|
|
|
{
|
|
|
|
uint8_t isUsbConnected = 0;
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
|
|
|
LED_Configure(LED_NUM_RED);
|
|
|
|
LED_Configure(LED_NUM_GREEN);
|
|
|
|
LED_Set(LED_NUM_RED);
|
|
|
|
|
|
|
|
/* Disable watchdog */
|
|
|
|
WDT_Disable(WDT);
|
|
|
|
|
|
|
|
//req_ctx_init();
|
|
|
|
|
|
|
|
PIO_InitializeInterrupts(0);
|
|
|
|
|
|
|
|
EEFC_ReadUniqueID(g_unique_id);
|
|
|
|
|
2017-03-03 00:33:24 +00:00
|
|
|
printf("\n\r\n\r"
|
|
|
|
"=============================================================================\n\r"
|
|
|
|
"DFU bootloader %s for board %s (C) 2010-2017 by Harald Welte\n\r"
|
|
|
|
"=============================================================================\n\r",
|
2017-02-27 19:31:09 +00:00
|
|
|
manifest_revision, manifest_board);
|
|
|
|
|
2017-03-03 00:33:24 +00:00
|
|
|
TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);
|
|
|
|
TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
|
2017-02-27 19:31:09 +00:00
|
|
|
g_unique_id[0], g_unique_id[1],
|
|
|
|
g_unique_id[2], g_unique_id[3]);
|
|
|
|
|
|
|
|
//board_main_top();
|
|
|
|
|
2017-03-03 00:33:24 +00:00
|
|
|
TRACE_INFO("USB init...\n\r");
|
2017-02-27 19:31:09 +00:00
|
|
|
USBDFU_Initialize(&dfu_descriptors);
|
|
|
|
|
|
|
|
while (USBD_GetState() < USBD_STATE_CONFIGURED) {
|
|
|
|
check_exec_dbg_cmd();
|
2017-02-27 23:21:45 +00:00
|
|
|
#if 1
|
2017-02-27 19:31:09 +00:00
|
|
|
if (i >= MAX_USB_ITER * 3) {
|
|
|
|
TRACE_ERROR("Resetting board (USB could "
|
2017-03-03 00:33:24 +00:00
|
|
|
"not be configured)\n\r");
|
2017-02-27 19:31:09 +00:00
|
|
|
NVIC_SystemReset();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2017-03-02 22:18:40 +00:00
|
|
|
FLASHD_Initialize(BOARD_MCK, 1);
|
2017-03-03 00:33:24 +00:00
|
|
|
TRACE_INFO("entering main loop...\n\r");
|
2017-02-27 19:31:09 +00:00
|
|
|
while (1) {
|
|
|
|
#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
|
|
|
|
const char rotor[] = { '-', '\\', '|', '/' };
|
|
|
|
putchar('\b');
|
|
|
|
putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
|
|
|
|
#endif
|
|
|
|
check_exec_dbg_cmd();
|
|
|
|
//osmo_timers_prepare();
|
|
|
|
//osmo_timers_update();
|
|
|
|
|
|
|
|
if (USBD_GetState() < USBD_STATE_CONFIGURED) {
|
|
|
|
|
|
|
|
if (isUsbConnected) {
|
|
|
|
isUsbConnected = 0;
|
|
|
|
}
|
|
|
|
} else if (isUsbConnected == 0) {
|
2017-03-03 00:33:24 +00:00
|
|
|
TRACE_INFO("USB is now configured\n\r");
|
2017-02-27 19:31:09 +00:00
|
|
|
LED_Set(LED_NUM_GREEN);
|
|
|
|
LED_Clear(LED_NUM_RED);
|
|
|
|
|
|
|
|
isUsbConnected = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|