From 0ccac69c37ed9e4cd72fba5ed9602ea03898347e Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Tue, 1 Oct 2013 18:54:55 +0100 Subject: [PATCH] initial bootloader - small overhead, not working yet --- Makefile | 10 +++ targets/stm32_boot/main.c | 184 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 targets/stm32_boot/main.c diff --git a/Makefile b/Makefile index b8d7ce0e9..f94ee61ef 100755 --- a/Makefile +++ b/Makefile @@ -31,6 +31,7 @@ # DEBUG=1 # add debug symbols (-g) # RELEASE=1 # Force release-style compile (no asserts, etc) # SINGLETHREAD=1 # Compile single-threaded to make compilation errors easier to find +# BOOTLOADER=1 # make the bootloader (not Espruino) ifndef SINGLETHREAD MAKEFLAGS=-j5 # multicore @@ -280,6 +281,13 @@ src/jsdevices.c \ $(WRAPPERFILE) CPPSOURCES = +ifdef BOOTLOADER +WRAPPERSOURCES = +SOURCES = \ +targets/stm32_boot/main.c +OPTIMIZEFLAGS=-Os +endif + ifdef SAVE_ON_FLASH DEFINES+=-DSAVE_ON_FLASH endif @@ -701,11 +709,13 @@ DEFINES += -DFAKE_STDLIB # FAKE_STDLIB is for Espruino - it uses its own standard library so we don't have to link in the normal one + get bloated DEFINES += -DSTM32 -DUSE_STDPERIPH_DRIVER=1 -D$(CHIP) -D$(BOARD) -D$(STLIB) INCLUDE += -I$(ROOT)/targets/stm32 +ifndef BOOTLOADER SOURCES += \ targets/stm32/main.c \ targets/stm32/jshardware.c \ targets/stm32/stm32_it.c endif +endif ifdef LINUX DEFINES += -DLINUX diff --git a/targets/stm32_boot/main.c b/targets/stm32_boot/main.c new file mode 100644 index 000000000..3db8a6786 --- /dev/null +++ b/targets/stm32_boot/main.c @@ -0,0 +1,184 @@ +/* + * This file is part of Espruino, a JavaScript interpreter for Microcontrollers + * + * Copyright (C) 2013 Gordon Williams + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * ---------------------------------------------------------------------------- + * Bootloader entry point + * ---------------------------------------------------------------------------- + */ +#include "platform_config.h" +#include "usb_utils.h" +#include "usb_lib.h" +#include "usb_desc.h" +#include "usb_pwr.h" +#include "usb_istr.h" +#include "jshardware.h" + +#define ACK (0x79) +#define NACK (0x1F) + +#define BUFFERMASK 1023 +char rxBuffer[BUFFERMASK+1]; +int rxHead=0, rxTail=0; +char txBuffer[BUFFERMASK+1]; +int txHead=0, txTail=0; + +uint16_t stmPin(Pin ipin) { + JsvPinInfoPin pin = pinInfo[ipin].pin; + if (pin==JSH_PIN0 ) return GPIO_Pin_0; + if (pin==JSH_PIN1 ) return GPIO_Pin_1; + if (pin==JSH_PIN2 ) return GPIO_Pin_2; + if (pin==JSH_PIN3 ) return GPIO_Pin_3; + if (pin==JSH_PIN4 ) return GPIO_Pin_4; + if (pin==JSH_PIN5 ) return GPIO_Pin_5; + if (pin==JSH_PIN6 ) return GPIO_Pin_6; + if (pin==JSH_PIN7 ) return GPIO_Pin_7; + if (pin==JSH_PIN8 ) return GPIO_Pin_8; + if (pin==JSH_PIN9 ) return GPIO_Pin_9; + if (pin==JSH_PIN10) return GPIO_Pin_10; + if (pin==JSH_PIN11) return GPIO_Pin_11; + if (pin==JSH_PIN12) return GPIO_Pin_12; + if (pin==JSH_PIN13) return GPIO_Pin_13; + if (pin==JSH_PIN14) return GPIO_Pin_14; + if (pin==JSH_PIN15) return GPIO_Pin_15; + return GPIO_Pin_0; +} + +GPIO_TypeDef *stmPort(Pin pin) { + JsvPinInfoPort port = pinInfo[pin].port; + if (port == JSH_PORTA) return GPIOA; + if (port == JSH_PORTB) return GPIOB; + if (port == JSH_PORTC) return GPIOC; + if (port == JSH_PORTD) return GPIOD; + if (port == JSH_PORTE) return GPIOE; + if (port == JSH_PORTF) return GPIOF; +#if defined(STM32F4) + if (port == JSH_PORTG) return GPIOG; + if (port == JSH_PORTH) return GPIOH; +#endif + return GPIOA; +} + + +void jshPinOutput(Pin pin, bool value) { + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_Pin = stmPin(pin); + GPIO_Init(stmPort(pin), &GPIO_InitStructure); + if (value) + stmPort(pin)->BSRR = stmPin(pin); + else + stmPort(pin)->BRR = stmPin(pin); +} + +void USB_LP_CAN1_RX0_IRQHandler(void) +{ + USB_Istr(); +} + +unsigned int SysTickUSBWatchdog = 0; + +void jshKickUSBWatchdog() { + SysTickUSBWatchdog = 0; +} + +void SysTick_Handler(void) { + if (SysTickUSBWatchdog < SYSTICKS_BEFORE_USB_DISCONNECT) { + SysTickUSBWatchdog++; + } +} + +bool jshIsUSBSERIALConnected() { + return SysTickUSBWatchdog < SYSTICKS_BEFORE_USB_DISCONNECT; +} + +int jshGetCharToTransmit(IOEventFlags device) { + if (txHead == txTail) return -1; + char d = txBuffer[txTail]; + txTail = (txTail+1) & BUFFERMASK; + return d; +} + +void jshPushIOCharEvent(IOEventFlags channel, char charData) { + rxBuffer[rxHead] = charData; + rxHead = (rxHead+1) & BUFFERMASK; +} + +bool jshHasEventSpaceForChars(int n) { + return true; +} + +int getc() { + if (rxHead == rxTail) return -1; + char d = rxBuffer[rxTail]; + rxTail = (rxTail+1) & BUFFERMASK; + return d; +} + +void putc(char charData) { + txBuffer[txHead] = charData; + txHead = (txHead+1) & BUFFERMASK; +} + + + +int main(void){ + + RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); + RCC_APB2PeriphClockCmd( + RCC_APB2Periph_ADC1 | + RCC_APB2Periph_GPIOA | + RCC_APB2Periph_GPIOB | + RCC_APB2Periph_GPIOC | + RCC_APB2Periph_GPIOD | + RCC_APB2Periph_GPIOE | + RCC_APB2Periph_GPIOF | + RCC_APB2Periph_GPIOG | + RCC_APB2Periph_AFIO, ENABLE); + RCC_PCLK1Config(RCC_HCLK_Div8); // PCLK1 must be >8 Mhz for USB to work + RCC_PCLK2Config(RCC_HCLK_Div16); + /* System Clock */ + SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); + SysTick_Config(SYSTICK_RANGE-1); // 24 bit + NVIC_SetPriority(SysTick_IRQn, 0); // Super high priority + + +#ifdef USB +#if defined(STM32F1) || defined(STM32F3) + USB_Init_Hardware(); + USB_Init(); +#endif +#ifdef STM32F4 + USBD_Init(&USB_OTG_dev, +#ifdef USE_USB_OTG_HS + USB_OTG_HS_CORE_ID, +#else + USB_OTG_FS_CORE_ID, +#endif + &USR_desc, + &USBD_CDC_cb, + &USR_cb); +#endif +#endif + + int flashy = 0; + + while (1) { + if (!jshIsUSBSERIALConnected()) { + jshPinOutput(LED2_PININDEX, 0); + // reset, led off + } else { + jshPinOutput(LED2_PININDEX, ((flashy++)&0x1000)!=0); + // flash led + int d = getc(); + if (d>=0) putc(d); + } + } +} +