Skip to content

Commit

Permalink
Реализовано обновление прошивки с USB Flash.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreili committed Mar 20, 2018
1 parent 984ccf4 commit 9257fd6
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 15 deletions.
8 changes: 8 additions & 0 deletions inc/fw_updater.h
@@ -0,0 +1,8 @@
#ifndef _FW_UPDATER_H_
#define _FW_UPDATER_H_

#include <inttypes.h>

void update_fw(uint8_t *buf, uint32_t fw_size);

#endif
47 changes: 47 additions & 0 deletions src/fw_updater.c
@@ -0,0 +1,47 @@
#include "fw_updater.h"
#include "cmsis_os.h"
#include "hid_proc.h"

void update_fw(uint8_t *buf, uint32_t fw_size)
{
LED_4_ON();
osDelay(100);

__disable_irq();
__disable_fault_irq();
LED_4_ON();
LED_3_ON();

FLASH_Unlock();
FLASH_EraseAllSectors(VoltageRange_3);
FLASH_Lock();

uint32_t page_idx = 0;
uint32_t wr_addr_start;
uint32_t fw_offs = 0;

while (fw_offs < fw_size)
{
uint32_t fw_dw = *((uint32_t*)&buf[fw_offs]);
wr_addr_start = FLASH_BASE + fw_offs;
FLASH_Unlock();
FLASH_ProgramWord(fw_offs, fw_dw);
FLASH_Lock();
fw_offs += 4;
}

FLASH_Lock();

int idx = 0;
while (1)
{
++idx;
if (idx == 5000000)
LED_4_ON();
else if (idx == 10000000)
{
idx = 0;
LED_4_OFF();
}
}
}
10 changes: 8 additions & 2 deletions src/gui_all.c
Expand Up @@ -26,6 +26,10 @@ void draw_cross(int x, int y, uint16_t color)

extern int dev_mode;
extern int boot_OK;
extern int boot_mounted;
extern int fw_size;
extern uint32_t fw_crc;
extern uint32_t fw_crc_base;

void main_GUI(void)
{
Expand All @@ -36,9 +40,11 @@ void main_GUI(void)
while (dev_mode == 0)
{
if (boot_OK == 0)
GUI_Text(10, 20, "FW load state: 0", White, Black);
GUI_Text(10, 20, "Waiting firmware", White, Black);
else
GUI_Text(10, 20, "USB boot mode", White, Black);
GUI_Text(10, 20, "USB boot mode ", White, Black);
sprintf(buf, "Drive: %i %i %X %X", boot_mounted, fw_size, fw_crc, fw_crc_base);
GUI_Text(10, 40, buf, White, Black);
}

LCD_clear();
Expand Down
19 changes: 12 additions & 7 deletions src/main.c
Expand Up @@ -7,7 +7,7 @@
#include "cmsis_os.h"
#include "gui_all.h"

//#define ENABLE_USB_BOOT
#define ENABLE_USB_BOOT
//#define ENABLE_PS2

#define USE_GUI
Expand Down Expand Up @@ -70,7 +70,7 @@ void sw_init(void)
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
Expand Down Expand Up @@ -113,8 +113,6 @@ void task_GUI(void const * argument);
void task_matrix(void const * argument);
void task_ps2(void const * argument);

int dev_mode = 0;

int main(void)
{
led_init();
Expand All @@ -123,7 +121,7 @@ int main(void)

// get configuration
sw_cfg = (GPIOC->IDR & 0x0f);
sw_mode = ((GPIOA->IDR & 0xff) < 4);
sw_mode = (GPIOA->IDR & 0xff);

osThreadDef(USBTask, task_USB, osPriorityNormal, 0, 128);
USBTaskHandle = osThreadCreate(osThread(USBTask), NULL);
Expand All @@ -150,8 +148,13 @@ int main(void)
while (1);
}

int dev_mode = 0;
extern int boot_mounted;

void task_USB(void const * argument)
{
int mount_counter = 0;

dev_mode = 0;

#ifdef ENABLE_USB_BOOT
Expand All @@ -166,6 +169,8 @@ void task_USB(void const * argument)
&USR_MS_cb);
while (1)
{
if ((!boot_mounted) && (++mount_counter > 3000))
break;
USBH_Process(&USB_OTG_Core , &USB_Host);
osDelay(1);
}
Expand All @@ -188,8 +193,8 @@ void task_USB(void const * argument)
{
USBH_Process(&USB_OTG_Core , &USB_Host);
// fill keyboard matrix
fill_matrix(sw_mode);
osDelay(5);
fill_matrix(sw_mode);
osDelay(5);
}
}

Expand Down
95 changes: 89 additions & 6 deletions src/ms_proc.c
@@ -1,5 +1,6 @@
#include "ms_proc.h"
#include "hid_proc.h"
#include "fw_updater.h"

#define USH_USR_FS_INIT 0
#define USH_USR_FS_READLIST 1
Expand Down Expand Up @@ -77,7 +78,86 @@ void USBH_USR_MS_OverCurrentDetected (void)
{
}

int fw_size = 0;
uint32_t fw_crc = 0;
uint32_t fw_crc_base = 0;
__ALIGN_BEGIN uint8_t buf_fw[60*1024] __ALIGN_END;
#define FW_READ_SIZE 512

static uint32_t crc32r_table[256];

#define CRC32_POLY_R 0xEDB88320

static void crc32_init(void)
{
int i, j;
uint32_t cr;
for (i = 0; i < 256; ++i)
{
cr = i;
for (j = 8; j > 0; --j)
cr = cr & 0x00000001 ? (cr >> 1) ^ CRC32_POLY_R : (cr >> 1);
crc32r_table[i] = cr;
}
}

uint32_t crc32_byte(uint32_t init_crc, uint8_t *buf, int len)
{
uint32_t v;
uint32_t crc;
crc = ~init_crc;
while(len > 0)
{
v = *buf++;
crc = ( crc >> 8 ) ^ crc32r_table[( crc ^ (v ) ) & 0xff];
len --;
}
return ~crc;
}


int read_fw(void)
{
UINT readed;

if(f_open(&file, "usb_keyb.crc", FA_READ) == FR_OK)
{
f_read(&file, &fw_crc_base, sizeof(uint32_t), &readed);
fw_crc_base = __builtin_bswap32(fw_crc_base);
f_close(&file);
}
else
return 0;

if(f_open(&file, "usb_keyb.bin", FA_READ) == FR_OK)
{
int pos = 0;
fw_size = file.fsize;
while (pos < fw_size)
{
f_read(&file, &buf_fw[pos], FW_READ_SIZE, &readed);
pos += readed;
}

crc32_init();
fw_crc = crc32_byte(0, &buf_fw[0], fw_size);

f_close(&file);
}
else
return 0;

if (fw_crc == fw_crc_base)
{
update_fw(buf_fw, fw_size);
return 1;
}
else
return 0;
}

extern USB_OTG_CORE_HANDLE USB_OTG_Core;
int boot_mounted = 0;
int boot_OK = 0;

int USBH_USR_MS_Application(void)
Expand All @@ -86,10 +166,9 @@ int USBH_USR_MS_Application(void)
switch(USBH_USR_ApplicationState)
{
case USH_USR_FS_INIT:
/* Initialises the File System*/
if ( f_mount( 0, &fatfs ) != FR_OK )
if (f_mount( 0, &fatfs ) != FR_OK)
{
/* efs initialisation fails*/
USBH_USR_ApplicationState = USH_USR_FS_DRAW;
return(-1);
}
USBH_USR_ApplicationState = USH_USR_FS_READFW;
Expand All @@ -104,16 +183,20 @@ int USBH_USR_MS_Application(void)
USBH_USR_ApplicationState = USH_USR_FS_DRAW;
break;
}

boot_mounted = 1;

f_mount(0, &fatfs);
//f_mount(0, &fatfs);

f_mount(0, NULL);
boot_OK = 1;
boot_OK = read_fw();

boot_mounted = 0;

USBH_USR_ApplicationState = USH_USR_FS_DRAW;
break;

case USH_USR_FS_DRAW:
f_mount(0, NULL);
LED_3_OFF();
break;
default: break;
Expand Down

0 comments on commit 9257fd6

Please sign in to comment.