Add command to reset into bootloader
This commit is contained in:
parent
8e1c27aa83
commit
b18657e1d1
8 changed files with 74 additions and 1 deletions
|
|
@ -1 +1 @@
|
||||||
352
|
353
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#include "usb_cdc.h"
|
#include "usb_cdc.h"
|
||||||
#include "pwm_output.h"
|
#include "pwm_output.h"
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
|
#include "system.h"
|
||||||
|
|
||||||
unsigned int HPGL_ParseErrorCounter = 0;
|
unsigned int HPGL_ParseErrorCounter = 0;
|
||||||
|
|
||||||
|
|
@ -12,6 +13,7 @@ typedef enum
|
||||||
|
|
||||||
// First byte of instruction has been received, waiting for second one
|
// First byte of instruction has been received, waiting for second one
|
||||||
HPGL_Parser_ReceivingInstruction,
|
HPGL_Parser_ReceivingInstruction,
|
||||||
|
HPGL_Parser_ReceivingDeviceControl,
|
||||||
|
|
||||||
// Receiving the pen number (currently ignored)
|
// Receiving the pen number (currently ignored)
|
||||||
HPGL_Parser_ReceivingPenNumber,
|
HPGL_Parser_ReceivingPenNumber,
|
||||||
|
|
@ -77,6 +79,12 @@ void HPGL_Poll(void)
|
||||||
// First character of all supported instructions
|
// First character of all supported instructions
|
||||||
state = HPGL_Parser_ReceivingInstruction;
|
state = HPGL_Parser_ReceivingInstruction;
|
||||||
}
|
}
|
||||||
|
else if(character == 27)
|
||||||
|
{
|
||||||
|
// Found a device control instruction, which always starts with
|
||||||
|
// ESC
|
||||||
|
state = HPGL_Parser_ReceivingDeviceControl;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Unsupported command
|
// Unsupported command
|
||||||
|
|
@ -118,6 +126,25 @@ void HPGL_Poll(void)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Unsupported instruction
|
// Unsupported instruction
|
||||||
|
HPGL_ParseErrorCounter++;
|
||||||
|
state = HPGL_Parser_Error;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case HPGL_Parser_ReceivingDeviceControl:
|
||||||
|
switch(character)
|
||||||
|
{
|
||||||
|
case 'B':
|
||||||
|
// Reset into bootloader
|
||||||
|
LED_On(LED_Orange);
|
||||||
|
LED_On(LED_Pink);
|
||||||
|
System_EnterBootloader();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Unsupported instruction
|
||||||
|
HPGL_ParseErrorCounter++;
|
||||||
state = HPGL_Parser_Error;
|
state = HPGL_Parser_Error;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
17
stm32f103t8u6/src/system.c
Normal file
17
stm32f103t8u6/src/system.c
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#include "system.h"
|
||||||
|
#include "usb.h"
|
||||||
|
#include "usb_util.h"
|
||||||
|
|
||||||
|
void System_EnterBootloader(void)
|
||||||
|
{
|
||||||
|
USB_Disable();
|
||||||
|
USB_Delay(100000);
|
||||||
|
|
||||||
|
// Set bootloader magic value in RTC backup register
|
||||||
|
RCC->APB1ENR |= RCC_APB1ENR_BKPEN | RCC_APB1ENR_PWREN;
|
||||||
|
PWR->CR = PWR_CR_DBP;
|
||||||
|
BKP->DR1 = 0xb007;
|
||||||
|
|
||||||
|
// Initiate software reset
|
||||||
|
NVIC_SystemReset();
|
||||||
|
}
|
||||||
6
stm32f103t8u6/src/system.h
Normal file
6
stm32f103t8u6/src/system.h
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "stm32f1xx.h"
|
||||||
|
|
||||||
|
// Immediately reset MCU and enter bootloader
|
||||||
|
void System_EnterBootloader(void);
|
||||||
|
|
@ -62,6 +62,21 @@ void USB_Init(void)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void USB_Disable(void)
|
||||||
|
{
|
||||||
|
// Disable interrupts, force reset
|
||||||
|
USB->CNTR = USB_CNTR_FRES;
|
||||||
|
|
||||||
|
// Clear interrupt flags
|
||||||
|
USB->ISTR = 0x00;
|
||||||
|
|
||||||
|
// Power down
|
||||||
|
USB->CNTR = USB_CNTR_FRES | USB_CNTR_PDWN;
|
||||||
|
|
||||||
|
// Disable D- pull-up
|
||||||
|
GPIOA->BRR = (1 << PIN_USB_PULLUP);
|
||||||
|
}
|
||||||
|
|
||||||
static inline void USB_HandleReset(void)
|
static inline void USB_HandleReset(void)
|
||||||
{
|
{
|
||||||
// Remove reset flag
|
// Remove reset flag
|
||||||
|
|
|
||||||
|
|
@ -134,3 +134,4 @@ typedef void (*USB_PacketHandler_t)(int length);
|
||||||
extern USB_PacketHandler_t USB_EP0OutPacketHandler;
|
extern USB_PacketHandler_t USB_EP0OutPacketHandler;
|
||||||
|
|
||||||
void USB_Init(void);
|
void USB_Init(void);
|
||||||
|
void USB_Disable(void);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include "usb_vendor.h"
|
#include "usb_vendor.h"
|
||||||
|
#include "system.h"
|
||||||
|
|
||||||
void USBVendor_HandleDeviceSetup(USB_SetupPacket_t *sp,
|
void USBVendor_HandleDeviceSetup(USB_SetupPacket_t *sp,
|
||||||
const void **reply_data, int *reply_length, uint8_t *reply_response)
|
const void **reply_data, int *reply_length, uint8_t *reply_response)
|
||||||
|
|
@ -14,6 +15,11 @@ void USBVendor_HandleDeviceSetup(USB_SetupPacket_t *sp,
|
||||||
*reply_response = USB_EP_TX_VALID;
|
*reply_response = USB_EP_TX_VALID;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case USB_VENDOR_COMMAND_ENTER_BOOTLOADER:
|
||||||
|
*reply_response = USB_EP_TX_VALID;
|
||||||
|
System_EnterBootloader();
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
*reply_response = USB_EP_TX_STALL;
|
*reply_response = USB_EP_TX_STALL;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
USB_VENDOR_COMMAND_NOP = 0x00,
|
USB_VENDOR_COMMAND_NOP = 0x00,
|
||||||
|
USB_VENDOR_COMMAND_ENTER_BOOTLOADER = 0xb0,
|
||||||
} USBVendor_Command_t;
|
} USBVendor_Command_t;
|
||||||
|
|
||||||
void USBVendor_HandleDeviceSetup(USB_SetupPacket_t *sp,
|
void USBVendor_HandleDeviceSetup(USB_SetupPacket_t *sp,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue