/* * 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/. * * ---------------------------------------------------------------------------- * Utilities - reimplementation of jshardware bits in minimal flash * ---------------------------------------------------------------------------- */ #include "platform_config.h" #ifdef USB #ifdef LEGACY_USB #include "legacy_usb.h" #else #include "usb_device.h" #include "usbd_cdc_hid.h" #endif #endif #include "jshardware.h" #define BUFFERMASK 8191 char rxBuffer[BUFFERMASK+1]; int rxHead=0, rxTail=0; char txBuffer[BUFFERMASK+1]; int txHead=0, txTail=0; static ALWAYS_INLINE uint16_t stmPin(Pin ipin) { JsvPinInfoPin pin = pinInfo[ipin].pin; return (uint16_t)(1 << (pin-JSH_PIN0)); } static ALWAYS_INLINE GPIO_TypeDef *stmPort(Pin pin) { JsvPinInfoPort port = pinInfo[pin].port&JSH_PORT_MASK; return (GPIO_TypeDef *)((char*)GPIOA + (port-JSH_PORTA)*0x0400); } bool jshPinGetValue(Pin pin) { return GPIO_ReadInputDataBit(stmPort(pin), stmPin(pin)) != 0; } void jshPinOutput(Pin pin, bool value) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = stmPin(pin); #ifdef STM32API2 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; #else GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; #endif GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(stmPort(pin), &GPIO_InitStructure); #ifdef STM32API2 if (value) GPIO_SetBits(stmPort(pin), stmPin(pin)); else GPIO_ResetBits(stmPort(pin), stmPin(pin)); #else if (value) stmPort(pin)->BSRR = stmPin(pin); else stmPort(pin)->BRR = stmPin(pin); #endif } #ifdef STM32F4 void WWDG_IRQHandler() { // why do we need this on the F401? } #endif void SysTick_Handler(void) { } bool jshIsUSBSERIALConnected() { return USB_IsConnected(); } 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; //if (rxHead == rxTail) weHaveOverFlowed(); } void jshPushIOCharEvents(IOEventFlags channel, char *data, unsigned int count) { unsigned int i; for (i=0;i13 Mhz for USB to work (see STM32F103 C/D/E errata) RCC_PCLK2Config(RCC_HCLK_Div4); /* System Clock */ SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); SysTick_Config(SYSTICK_RANGE-1); // 24 bit NVIC_SetPriority(SysTick_IRQn, 0); // Super high priority MX_USB_DEVICE_Init(); }