├── .cproject ├── .gitattributes ├── .gitignore ├── .project ├── Makefile ├── README.md ├── bradwii-V202.uvproj ├── bradwii-X4.uvproj ├── bradwii-jd385.uvproj ├── bradwii.uvproj ├── lib-Mini51 ├── CMSIS │ ├── CMSIS END USER LICENCE AGREEMENT.pdf │ ├── Include │ │ ├── arm_common_tables.h │ │ ├── arm_math.h │ │ ├── core_cm0.h │ │ ├── core_cm0plus.h │ │ ├── core_cm3.h │ │ ├── core_cm4.h │ │ ├── core_cm4_simd.h │ │ ├── core_cmFunc.h │ │ ├── core_cmInstr.h │ │ ├── core_sc000.h │ │ └── core_sc300.h │ ├── README.txt │ └── index.html ├── Device │ └── Nuvoton │ │ └── Mini51Series │ │ ├── Include │ │ ├── Mini51Series.h │ │ └── system_Mini51Series.h │ │ └── Source │ │ ├── ARM │ │ └── startup_Mini51Series.s │ │ ├── IAR │ │ └── startup_Mini51Series.s │ │ └── system_Mini51Series.c ├── StdDriver │ ├── inc │ │ ├── adc.h │ │ ├── clk.h │ │ ├── fmc.h │ │ ├── gpio.h │ │ ├── i2c.h │ │ ├── pwm.h │ │ ├── spi.h │ │ ├── sys.h │ │ ├── timer.h │ │ ├── uart.h │ │ └── wdt.h │ └── src │ │ ├── adc.c │ │ ├── clk.c │ │ ├── fmc.c │ │ ├── gpio.c │ │ ├── i2c.c │ │ ├── pwm.c │ │ ├── retarget.c │ │ ├── spi.c │ │ ├── sys.c │ │ ├── timer.c │ │ ├── uart.c │ │ └── wdt.c ├── hal │ ├── drv_gpio.c │ ├── drv_gpio.h │ ├── drv_hal.c │ ├── drv_pwm.c │ ├── drv_pwm.h │ ├── drv_serial.c │ ├── drv_serial.h │ ├── hal.h │ ├── lib_adc.c │ ├── lib_adc.h │ ├── lib_digitalio.c │ ├── lib_digitalio.h │ ├── lib_fp.c │ ├── lib_fp.h │ ├── lib_i2c.c │ ├── lib_i2c.h │ ├── lib_serial.c │ ├── lib_serial.h │ ├── lib_soft_3_wire_spi.c │ ├── lib_soft_3_wire_spi.h │ ├── lib_spi.c │ ├── lib_spi.h │ ├── lib_timers.c │ └── lib_timers.h ├── linker │ ├── link.ld │ └── memory.ld ├── startup_Mini51Series.c └── startup_Mini51Series.s ├── lib ├── CMSIS │ └── CM3 │ │ ├── CoreSupport │ │ ├── core_cm3.c │ │ └── core_cm3.h │ │ └── DeviceSupport │ │ └── ST │ │ └── STM32F10x │ │ ├── Release_Notes.html │ │ ├── startup │ │ ├── TrueSTUDIO │ │ │ ├── startup_stm32f10x_cl.s │ │ │ ├── startup_stm32f10x_hd.s │ │ │ ├── startup_stm32f10x_hd_vl.s │ │ │ ├── startup_stm32f10x_ld.s │ │ │ ├── startup_stm32f10x_ld_vl.s │ │ │ ├── startup_stm32f10x_md.s │ │ │ ├── startup_stm32f10x_md_vl.s │ │ │ └── startup_stm32f10x_xl.s │ │ ├── arm │ │ │ ├── startup_stm32f10x_cl.s │ │ │ ├── startup_stm32f10x_hd.s │ │ │ ├── startup_stm32f10x_hd_vl.s │ │ │ ├── startup_stm32f10x_ld.s │ │ │ ├── startup_stm32f10x_ld_vl.s │ │ │ ├── startup_stm32f10x_md.s │ │ │ ├── startup_stm32f10x_md_vl.s │ │ │ └── startup_stm32f10x_xl.s │ │ ├── gcc_ride7 │ │ │ ├── startup_stm32f10x_cl.s │ │ │ ├── startup_stm32f10x_hd.s │ │ │ ├── startup_stm32f10x_hd_vl.s │ │ │ ├── startup_stm32f10x_ld.s │ │ │ ├── startup_stm32f10x_ld_vl.s │ │ │ ├── startup_stm32f10x_md.s │ │ │ ├── startup_stm32f10x_md_vl.s │ │ │ └── startup_stm32f10x_xl.s │ │ └── iar │ │ │ ├── startup_stm32f10x_cl.s │ │ │ ├── startup_stm32f10x_hd.s │ │ │ ├── startup_stm32f10x_hd_vl.s │ │ │ ├── startup_stm32f10x_ld.s │ │ │ ├── startup_stm32f10x_ld_vl.s │ │ │ ├── startup_stm32f10x_md.s │ │ │ ├── startup_stm32f10x_md_vl.s │ │ │ └── startup_stm32f10x_xl.s │ │ ├── stm32f10x.h │ │ ├── stm32f10x_conf.h │ │ ├── system_stm32f10x.c │ │ └── system_stm32f10x.h ├── STM32F10x_StdPeriph_Driver │ ├── inc │ │ ├── misc.h │ │ ├── stm32f10x_adc.h │ │ ├── stm32f10x_bkp.h │ │ ├── stm32f10x_can.h │ │ ├── stm32f10x_cec.h │ │ ├── stm32f10x_crc.h │ │ ├── stm32f10x_dac.h │ │ ├── stm32f10x_dbgmcu.h │ │ ├── stm32f10x_dma.h │ │ ├── stm32f10x_exti.h │ │ ├── stm32f10x_flash.h │ │ ├── stm32f10x_fsmc.h │ │ ├── stm32f10x_gpio.h │ │ ├── stm32f10x_i2c.h │ │ ├── stm32f10x_iwdg.h │ │ ├── stm32f10x_pwr.h │ │ ├── stm32f10x_rcc.h │ │ ├── stm32f10x_rtc.h │ │ ├── stm32f10x_sdio.h │ │ ├── stm32f10x_spi.h │ │ ├── stm32f10x_tim.h │ │ ├── stm32f10x_usart.h │ │ └── stm32f10x_wwdg.h │ └── src │ │ ├── misc.c │ │ ├── stm32f10x_adc.c │ │ ├── stm32f10x_bkp.c │ │ ├── stm32f10x_can.c │ │ ├── stm32f10x_cec.c │ │ ├── stm32f10x_crc.c │ │ ├── stm32f10x_dac.c │ │ ├── stm32f10x_dbgmcu.c │ │ ├── stm32f10x_dma.c │ │ ├── stm32f10x_exti.c │ │ ├── stm32f10x_flash.c │ │ ├── stm32f10x_fsmc.c │ │ ├── stm32f10x_gpio.c │ │ ├── stm32f10x_i2c.c │ │ ├── stm32f10x_iwdg.c │ │ ├── stm32f10x_pwr.c │ │ ├── stm32f10x_rcc.c │ │ ├── stm32f10x_rtc.c │ │ ├── stm32f10x_sdio.c │ │ ├── stm32f10x_spi.c │ │ ├── stm32f10x_tim.c │ │ ├── stm32f10x_usart.c │ │ └── stm32f10x_wwdg.c ├── hal │ ├── drv_gpio.c │ ├── drv_gpio.h │ ├── drv_hal.c │ ├── drv_pwm.c │ ├── drv_pwm.h │ ├── drv_serial.c │ ├── drv_serial.h │ ├── hal.h │ ├── lib_digitalio.c │ ├── lib_digitalio.h │ ├── lib_fp.c │ ├── lib_fp.h │ ├── lib_i2c.c │ ├── lib_i2c.h │ ├── lib_serial.c │ ├── lib_serial.h │ ├── lib_timers.c │ ├── lib_timers.h │ ├── lib_usb.c │ └── lib_usb.h └── startup_stm32f10x_md.s ├── pinout.txt ├── pinout_q4.txt └── src ├── a7105.c ├── a7105.h ├── accelerometer.c ├── accelerometer.h ├── autotune.c ├── autotune.h ├── baro.c ├── baro.h ├── bradwii.c ├── bradwii.h ├── checkboxes.c ├── checkboxes.h ├── compass.c ├── compass.h ├── config.h ├── config_JD385.h ├── config_STM32.h ├── config_V202.h ├── config_X4.h ├── defs.h ├── eeprom.c ├── eeprom.h ├── gps.c ├── gps.h ├── gyro.c ├── gyro.h ├── imu.c ├── imu.h ├── leds.c ├── leds.h ├── navigation.c ├── navigation.h ├── nrf24l01.c ├── nrf24l01.h ├── options.h ├── output.c ├── output.h ├── pilotcontrol.c ├── pilotcontrol.h ├── projectsettings.h ├── rx.c ├── rx.h ├── rx_v202.c ├── rx_x4.c ├── serial.c ├── serial.h ├── vectors.c └── vectors.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | #* text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Custom for Keil MDK 13 | *.uvproj merge=union 14 | 15 | # Declare files that will always have CRLF line endings on checkout. 16 | *.uvproj text eol=crlf 17 | 18 | # Standard to msysgit 19 | *.doc diff=astextplain 20 | *.DOC diff=astextplain 21 | *.docx diff=astextplain 22 | *.DOCX diff=astextplain 23 | *.dot diff=astextplain 24 | *.DOT diff=astextplain 25 | *.pdf diff=astextplain 26 | *.PDF diff=astextplain 27 | *.rtf diff=astextplain 28 | *.RTF diff=astextplain 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | obj/ 2 | obj-Mini51/ 3 | *.dep 4 | *.uvopt 5 | *.uvgui.* 6 | *.bak 7 | *.map 8 | *.lst 9 | /Debug 10 | .settings/ 11 | 12 | us.stackdump 13 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | bradwii-x4-gcc 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 24 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 25 | 26 | 27 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #GCC-arm Makefile for bradwii 2 | #Copyright 2015 pokey9000 3 | # 4 | #This program is free software: you can redistribute it and/or modify 5 | #it under the terms of the GNU General Public License as published by 6 | #the Free Software Foundation, either version 3 of the License, or 7 | #any later version. 8 | # 9 | #This program is distributed in the hope that it will be useful, 10 | #but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | #GNU General Public License for more details. 13 | # 14 | #You should have received a copy of the GNU General Public License 15 | #along with this program. If not, see . 16 | # 17 | 18 | # Note: lifted from Eclipse generated makefiles 19 | 20 | 21 | COPTER="X4" 22 | 23 | ifeq ($(COPTER), "X4") 24 | MICRO="MINI51" 25 | endif 26 | 27 | OBJS += src/a7105.o src/accelerometer.o src/autotune.o src/bradwii.o src/checkboxes.o src/eeprom.o src/gyro.o src/imu.o src/leds.o src/output.o src/pilotcontrol.o src/serial.o src/rx_x4.o src/vectors.o 28 | 29 | MINI51_OBJS += lib-Mini51/hal/drv_gpio.o lib-Mini51/hal/drv_hal.o lib-Mini51/hal/drv_pwm.o lib-Mini51/hal/drv_serial.o lib-Mini51/hal/lib_adc.o lib-Mini51/hal/lib_digitalio.o lib-Mini51/hal/lib_fp.o lib-Mini51/hal/lib_i2c.o lib-Mini51/hal/lib_serial.o lib-Mini51/hal/lib_soft_3_wire_spi.o lib-Mini51/hal/lib_timers.o 30 | 31 | MINI51_OBJS += lib-Mini51/StdDriver/src/adc.o lib-Mini51/StdDriver/src/clk.o lib-Mini51/StdDriver/src/fmc.o lib-Mini51/StdDriver/src/gpio.o lib-Mini51/StdDriver/src/i2c.o lib-Mini51/StdDriver/src/pwm.o lib-Mini51/StdDriver/src/sys.o lib-Mini51/StdDriver/src/timer.o lib-Mini51/StdDriver/src/wdt.o lib-Mini51/StdDriver/src/uart.o 32 | 33 | MINI51_OBJS += lib-Mini51/Device/Nuvoton/Mini51Series/Source/system_Mini51Series.o 34 | 35 | MINI51_OBJS += lib-Mini51/startup_Mini51Series.o 36 | 37 | ifeq ($(MICRO), "MINI51") 38 | OBJS += $(MINI51_OBJS) 39 | endif 40 | 41 | # Add inputs and outputs from these tool invocations to the build variables 42 | 43 | SECONDARY_FLASH += bradwii-x4-gcc.hex 44 | SECONDARY_SIZE += bradwii-x4-gcc.siz 45 | 46 | # All Target 47 | all: bradwii-x4-gcc.siz 48 | 49 | # Tool invocations 50 | 51 | %.o: %.c 52 | @echo 'Building file: $<' 53 | @echo 'Invoking: Cross ARM C Compiler' 54 | arm-none-eabi-gcc -mcpu=cortex-m0 -mthumb -Os -ffunction-sections -fno-common -fno-builtin -Wall -g -DX4_BUILD -DDEBUG_ENABLE_SEMIHOST -I"lib-Mini51/hal" -I"src" -I"lib-Mini51/Device/Nuvoton/Mini51Series/Include" -I"lib-Mini51/CMSIS/Include" -I"lib-Mini51/StdDriver/inc" -std=gnu99 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -c -o "$@" "$<" 55 | 56 | bradwii-x4-gcc: $(OBJS) $(USER_OBJS) 57 | @echo 'Building target: $@' 58 | @echo 'Invoking: Cross ARM C Linker' 59 | arm-none-eabi-gcc -mcpu=cortex-m0 -mthumb -Os -ffunction-sections -fno-common -fno-builtin -Wall -g -T "lib-Mini51/linker/memory.ld" -T "lib-Mini51/linker/link.ld" -nostartfiles -Xlinker --gc-sections -Wl,-Map,"bradwii-x4-gcc.map" -o "bradwii-x4-gcc" $(OBJS) $(USER_OBJS) $(LIBS) 60 | @echo 'Finished building target: $@' 61 | @echo ' ' 62 | 63 | bradwii-x4-gcc.hex: bradwii-x4-gcc 64 | @echo 'Invoking: Cross ARM GNU Create Flash Image' 65 | arm-none-eabi-objcopy -O ihex --strip-unneeded "bradwii-x4-gcc" "bradwii-x4-gcc.hex" 66 | @echo 'Finished building: $@' 67 | @echo ' ' 68 | 69 | bradwii-x4-gcc.siz: bradwii-x4-gcc 70 | @echo 'Invoking: Cross ARM GNU Print Size' 71 | arm-none-eabi-size --format=berkeley "bradwii-x4-gcc" 72 | @echo 'Finished building: $@' 73 | @echo ' ' 74 | 75 | # Other Targets 76 | clean: 77 | -rm -f $(SECONDARY_SIZE) $(OBJS) $(OBJS:.o=.d) $(SECONDARY_FLASH) bradwii-x4-gcc bradwii-x4-gcc.map 78 | -@echo ' ' 79 | 80 | # TODO: package up openocd changes 81 | flash: bradwii-x4-gcc 82 | -openocd -f target/mini51_stlinkv2.cfg -c "init; halt; flash write_image erase bradwii-x4-gcc 0; reset; shutdown" 83 | 84 | debug: 85 | -openocd -f target/mini51_stlinkv2.cfg -c "init; halt; arm semihosting enable; reset run" 86 | 87 | .PHONY: all clean dependents 88 | .SECONDARY: 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Bradwii for JXD JD385, WLToys V202, Hubsan H107L and clones 2 | ======= 3 | 4 | This is a port of bradwii to another ARM architecture, Nuvoton Mini51 which is represented 5 | by commercial microhelis WL-Toys V202, V252, and JXD JD-385. For hardware description 6 | details see https://github.com/hackocopter/JD385_Documentation . 7 | 8 | Briefly, it uses MINI54ZAN ARM MCU with 16KB ROM and 2KB RAM, combined gyro/accelerometer 9 | InvenSense MPU-6050, and nRF24L01+ clone Beken BK2423. 10 | 11 | Hubsan X4 H107 serie uses Invensense MPU-3050 gyro, mCube MC3210 accelerometer and A7105 RF. 12 | 13 | Hopefully it also will allow us to port it to similar ARM platform, Nuvoton M051, 14 | which is represented by HiSky HMX120 (former FF120) and its rebadge HobbyKing Q-BOT micro. 15 | 16 | It uses M058ZBN ARM MCU with 32KB ROM and 4KB RAM, gyro InvenSense MPU-3050, supposedly 17 | accelerometer Freescale MMA8452Q (maybe ST LIS3DH), and nRF24L01 clone Si24R1. 18 | 19 | Datasheets for HMX120 are at http://www.rcgroups.com/forums/showthread.php?t=1826018&page=51#post24710038 20 | 21 | #### The H107L uses the following hardware 22 | * Nuvoton MINI54ZAN ARM Cortex-M0 23 | * AMICCOM A7105 2.5GHz transceiver 24 | * mCube MC3210 3-Axis Accelerometer 25 | * InvenSense MPU-3050 3-Axis MEMS Gyroscope 26 | 27 | TheLastMutt has made lots of improvements to the H107L port, they're merged into this repo and still builds with Keil. 28 | 29 | extra notes from TheLastMutt: 30 | ====== 31 | 32 | Current status 33 | #### 34 | The control parameters habe been optimized in a way that flying feels similar to the original firmware. 35 | It is possible to lift off very slowly and fly close to the ground. 36 | The control parameters have not been tested in acro or semi-acro mode. 37 | 38 | Accelerometer calibration can be separated from gyro calibration, like the original firmware does. After executing the manual accelerometer calibration, 39 | the parameters are stored in data flash. At subsequent power-on only the gyro calibration is executed. This way there is no need to find a level surface 40 | for every power-on. 41 | 42 | Testing a new flight mode: "yaw hold". In this mode the yaw controller controls the yaw angle instead of the yaw rate. 43 | If the quadcopter gets rotated away from its desired heading (due to wind or high throttle or whatever), it will try to rotate back to the desired heading. 44 | This is similar to compass mode, but a compass is not needed. This mode can be activated using the checkboxes like all the other flight modes. 45 | It is currently activated using AUX2 ("flip"/"no flip" channel of X4). 46 | 47 | The X4 board does not have a serial port, so a PC configuration software cannot be connected. 48 | 49 | How to use: 50 | * Switch quadcopter on 51 | * (If all LEDs slowly blink 4 times, no accelerometer calibration was found in data flash. Accelerometer will be calibrated after binding with transmitter, 52 | so a level surface is needed. Use manual accelerometer calibration to remedy this) 53 | * All LEDs blink alternating 54 | * Put quadcopter on steady surface 55 | * Switch transmitter on 56 | * LEDs blink in circular pattern. Don't move the quadcopter during this time because the calibration is ongoing. 57 | * When calibration is done, all LEDs blink short pulses. Quadcopter is not armed yet and will not respond to throttle. 58 | * Press the lower throttle trim button for one second ("LEDs off" for Hubsan firmware) 59 | * All LEDs are on and you are ready to fly. 60 | * When the LEDs start to blink during flight it's time to land because the battery is nearly empty. 61 | With this firmware the LEDs only blink *while* the battery voltage is low, so blinking might be temporary at high throttle. 62 | 63 | Disarm by pressing the lower throttle trim button again for one second. 64 | 65 | Manual accelerometer calibration: 66 | * Quadcopter must be on level surface 67 | * Quadcopter must be in "not armed" state 68 | * Throttle stick at minimum 69 | * Move roll stick 3 times left and right 70 | * LEDs blink in circular pattern to indicate calibration process. When finished, results are stored in data flash. 71 | 72 | #### Development issues: 73 | 74 | When burning a firmware with new PID control parameters, checkboxconfig or anything else from the usersettings struct make sure to erase the data flash. 75 | Otherwise the firmware will continue to use the old data. 76 | 77 | 78 | Credits 79 | ====== 80 | 81 | * Bradwii was originally coded by Brad Quick for AVR: https://github.com/bradquick/bradwii 82 | * Trollcop forked and ported to ARM STM32, untested: https://github.com/trollcop/bradwii 83 | * The Mini54ZAN ARM port to V202/JD385 was done by Victor: https://github.com/victzh/bradwii 84 | * The Hubsan X4 H107L port was done by Goebish: https://github.com/goebish/bradwii-X4 85 | * Extra work on the H107L port by TheLastMutt: https://github.com/TheLastMutt/bradwii-x4-gcc 86 | -------------------------------------------------------------------------------- /lib-Mini51/CMSIS/CMSIS END USER LICENCE AGREEMENT.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victzh/bradwii/51436da66ef105e0824a854fe07ca0ad2dbc368f/lib-Mini51/CMSIS/CMSIS END USER LICENCE AGREEMENT.pdf -------------------------------------------------------------------------------- /lib-Mini51/CMSIS/Include/arm_common_tables.h: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------- 2 | * Copyright (C) 2010 ARM Limited. All rights reserved. 3 | * 4 | * $Date: 13/09/14 1:29p $Revision: V1.0.2 5 | * 6 | * Project: CMSIS DSP Library 7 | * Title: arm_common_tables.h 8 | * 9 | * Description: This file has extern declaration for common tables like Bitreverse, reciprocal etc which are used across different functions 10 | * 11 | * Target Processor: Cortex-M4/Cortex-M3 12 | * 13 | * Version 1.0.2 2010/11/11 14 | * Documentation updated. 15 | * 16 | * Version 1.0.1 2010/10/05 17 | * Production release and review comments incorporated. 18 | * 19 | * Version 1.0.0 2010/09/20 20 | * Production release and review comments incorporated. 21 | * -------------------------------------------------------------------- */ 22 | 23 | #ifndef _ARM_COMMON_TABLES_H 24 | #define _ARM_COMMON_TABLES_H 25 | 26 | #include "arm_math.h" 27 | 28 | extern const uint16_t armBitRevTable[1024]; 29 | extern const q15_t armRecipTableQ15[64]; 30 | extern const q31_t armRecipTableQ31[64]; 31 | extern const q31_t realCoefAQ31[1024]; 32 | extern const q31_t realCoefBQ31[1024]; 33 | extern const float32_t twiddleCoef[6144]; 34 | extern const q31_t twiddleCoefQ31[6144]; 35 | extern const q15_t twiddleCoefQ15[6144]; 36 | 37 | #endif /* ARM_COMMON_TABLES_H */ 38 | -------------------------------------------------------------------------------- /lib-Mini51/CMSIS/README.txt: -------------------------------------------------------------------------------- 1 | * ------------------------------------------------------------------- 2 | * Copyright (C) 2011-2012 ARM Limited. All rights reserved. 3 | * 4 | * Date: 07 March 2012 5 | * Revision: V3.01 6 | * 7 | * Project: Cortex Microcontroller Software Interface Standard (CMSIS) 8 | * Title: Release Note for CMSIS 9 | * 10 | * ------------------------------------------------------------------- 11 | 12 | 13 | NOTE - Open the index.html file to access CMSIS documentation 14 | 15 | 16 | The Cortex Microcontroller Software Interface Standard (CMSIS) provides a single standard across all 17 | Cortex-Mx processor series vendors. It enables code re-use and code sharing across software projects 18 | and reduces time-to-market for new embedded applications. 19 | 20 | CMSIS is released under the terms of the end user license agreement ("CMSIS END USER LICENCE AGREEMENT.pdf"). 21 | Any user of the software package is bound to the terms and conditions of the end user license agreement. 22 | 23 | 24 | You will find the following sub-directories: 25 | 26 | Documentation - Contains CMSIS documentation. 27 | 28 | DSP_Lib - MDK project files, Examples and source files etc.. to build the 29 | CMSIS DSP Software Library for Cortex-M0, Cortex-M3, Cortex-M4 processors. 30 | 31 | Include - CMSIS Core Support and CMSIS DSP Include Files. 32 | 33 | Lib - CMSIS DSP Libraries. 34 | 35 | RTOS - CMSIS RTOS API template header file. 36 | 37 | SVD - CMSIS SVD Schema files and Conversion Utility. 38 | -------------------------------------------------------------------------------- /lib-Mini51/CMSIS/index.html: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Redirect to the CMSIS main page after 0 seconds 5 | 6 | 7 | 8 | 9 | 10 | 11 | If the automatic redirection is failing, click open CMSIS Documentation. 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /lib-Mini51/Device/Nuvoton/Mini51Series/Include/Mini51Series.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victzh/bradwii/51436da66ef105e0824a854fe07ca0ad2dbc368f/lib-Mini51/Device/Nuvoton/Mini51Series/Include/Mini51Series.h -------------------------------------------------------------------------------- /lib-Mini51/Device/Nuvoton/Mini51Series/Include/system_Mini51Series.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file system_Mini51Series.h 3 | * @version V1.00 4 | * $Revision: 3 $ 5 | * $Date: 13/10/01 9:05a $ 6 | * @brief Mini51 series system clock definition file 7 | * 8 | * @note 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. 10 | *****************************************************************************/ 11 | 12 | 13 | #ifndef __SYSTEM_MINI51SERIES_H__ 14 | #define __SYSTEM_MINI51SERIES_H__ 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | /*---------------------------------------------------------------------------------------------------------*/ 21 | /* Macro Definition */ 22 | /*---------------------------------------------------------------------------------------------------------*/ 23 | 24 | /*---------------------------------------------------------------------------- 25 | Define SYSCLK 26 | *----------------------------------------------------------------------------*/ 27 | 28 | #define __XTAL12M (12000000UL) 29 | #define __XTAL32K (32768UL) 30 | #define __IRC22M (22118400UL) 31 | #define __IRC10K (10000UL) 32 | #define __XTAL __XTAL12M 33 | #define __HSI (__IRC22M) /* Factory Default is internal 22MHz */ 34 | 35 | 36 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ 37 | extern uint32_t CyclesPerUs; /*!< Cycles per micro second */ 38 | 39 | /** 40 | * Update SystemCoreClock variable 41 | * 42 | * @param None 43 | * @return None 44 | * 45 | * @brief Updates the SystemCoreClock with current core Clock 46 | * retrieved from cpu registers. 47 | */ 48 | 49 | extern void SystemCoreClockUpdate (void); 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | 55 | #endif //__SYSTEM_MINI51SERIES_H__ 56 | 57 | 58 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ 59 | -------------------------------------------------------------------------------- /lib-Mini51/Device/Nuvoton/Mini51Series/Source/system_Mini51Series.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file system_Mini51Series.c 3 | * @version V1.00 4 | * $Revision: 4 $ 5 | * $Date: 13/10/01 9:05a $ 6 | * @brief Mini51 series system clock init code and assert handler 7 | * 8 | * @note 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. 10 | *****************************************************************************/ 11 | 12 | #include 13 | #include "Mini51Series.h" 14 | 15 | 16 | /*---------------------------------------------------------------------------- 17 | Clock Variable definitions 18 | *----------------------------------------------------------------------------*/ 19 | uint32_t SystemCoreClock = __HSI; /*!< System Clock Frequency (Core Clock) */ 20 | uint32_t CyclesPerUs = (__HSI / 1000000); /*!< Cycles per micro second */ 21 | 22 | 23 | /** 24 | * @brief This function is used to update the variable SystemCoreClock 25 | * and must be called whenever the core clock is changed. 26 | * @param None. 27 | * @retval None. 28 | */ 29 | 30 | void SystemCoreClockUpdate (void) 31 | { 32 | uint32_t u32CoreFreq, u32ClkSrc; 33 | 34 | u32ClkSrc = CLK->CLKSEL0 & CLK_CLKSEL0_HCLK_S_Msk; 35 | 36 | if (u32ClkSrc == 0) 37 | u32CoreFreq = __XTAL; /* External crystal clock */ 38 | else if (u32ClkSrc == 3) 39 | u32CoreFreq = __IRC10K; /* Interanl 10K crystyl clock */ 40 | else if (u32ClkSrc == 7) 41 | u32CoreFreq = __IRC22M; /* Internal 22M */ 42 | else 43 | u32CoreFreq = __IRC22M; /* unknown value, use default Internal 22M */ 44 | 45 | SystemCoreClock = (u32CoreFreq/((CLK->CLKDIV & CLK_CLKDIV_HCLK_N_Msk) + 1)); 46 | CyclesPerUs = (SystemCoreClock + 500000) / 1000000; 47 | } 48 | 49 | #if USE_ASSERT 50 | 51 | /** 52 | * @brief Assert Error Message 53 | * 54 | * @param[in] file the source file name 55 | * @param[in] line line number 56 | * 57 | * @return None 58 | * 59 | * @details The function prints the source file name and line number where 60 | * the ASSERT_PARAM() error occurs, and then stops in an infinite loop. 61 | */ 62 | void AssertError(uint8_t * file, uint32_t line) 63 | { 64 | 65 | printf("[%s] line %d : wrong parameters.\r\n", file, line); 66 | 67 | /* Infinite loop */ 68 | while(1) ; 69 | } 70 | #endif 71 | 72 | void SystemInit (void) 73 | { 74 | 75 | } 76 | 77 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ 78 | -------------------------------------------------------------------------------- /lib-Mini51/StdDriver/inc/fmc.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file FMC.h 3 | * @version V1.00 4 | * $Revision: 8 $ 5 | * $Date: 13/10/01 9:25a $ 6 | * @brief MINI51 series FMC driver header file 7 | * 8 | * @note 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. 10 | ******************************************************************************/ 11 | #ifndef __FMC_H__ 12 | #define __FMC_H__ 13 | 14 | #ifdef __cplusplus 15 | extern "C" 16 | { 17 | #endif 18 | 19 | /** @addtogroup MINI51_Device_Driver MINI51 Device Driver 20 | @{ 21 | */ 22 | 23 | /** @addtogroup MINI51_FMC_Driver FMC Driver 24 | @{ 25 | */ 26 | 27 | /** @addtogroup MINI51_FMC_EXPORTED_CONSTANTS FMC Exported Constants 28 | @{ 29 | */ 30 | /*---------------------------------------------------------------------------------------------------------*/ 31 | /* Define Base Address */ 32 | /*---------------------------------------------------------------------------------------------------------*/ 33 | #define FMC_APROM_BASE 0x00000000UL /*!< APROM Base Address */ 34 | #define FMC_LDROM_BASE 0x00100000UL /*!< LDROM Base Address */ 35 | #define FMC_LDROM_END 0x00100800UL /*!< LDROM End Address */ 36 | #define FMC_CONFIG_BASE 0x00300000UL /*!< CONFIG Base Address */ 37 | 38 | #define FMC_FLASH_PAGE_SIZE 0x200 /*!< Flash Page Size (512 Bytes) */ 39 | #define FMC_LDROM_SIZE 0x800 /*!< LDROM Size (2 kBytes) */ 40 | 41 | 42 | /*---------------------------------------------------------------------------------------------------------*/ 43 | /* ISPCMD constant definitions */ 44 | /*---------------------------------------------------------------------------------------------------------*/ 45 | #define FMC_ISPCMD_READ 0x00 /*!< ISP Command: Read Flash */ 46 | #define FMC_ISPCMD_PROGRAM 0x21 /*!< ISP Command: Program Flash */ 47 | #define FMC_ISPCMD_PAGE_ERASE 0x22 /*!< ISP Command: Page Erase Flash */ 48 | #define FMC_ISPCMD_READ_CID 0x0B /*!< ISP Command: Read Company ID */ 49 | #define FMC_ISPCMD_READ_DID 0x0C /*!< ISP Command: Read Device ID */ 50 | #define FMC_ISPCMD_READ_UID 0x04 /*!< ISP Command: Read Unique ID */ 51 | #define FMC_ISPCMD_VECMAP 0x2E /*!< ISP Command: Vector Page Remap */ 52 | 53 | 54 | /*@}*/ /* end of group MINI51_FMC_EXPORTED_CONSTANTS */ 55 | 56 | /** @addtogroup MINI51_FMC_EXPORTED_FUNCTIONS FMC Exported Functions 57 | @{ 58 | */ 59 | 60 | #define FMC_SET_APROM_BOOT() (FMC->ISPCON &= ~FMC_ISPCON_BS_Msk) /*!< Select booting from APROM */ 61 | #define FMC_SET_LDROM_BOOT() (FMC->ISPCON |= FMC_ISPCON_BS_Msk) /*!< Select booting from LDROM */ 62 | #define FMC_DISABLE_AP_UPDATE() (FMC->ISPCON &= ~FMC_ISPCON_APUEN_Msk) /*!< Disable APROM update */ 63 | #define FMC_DISABLE_CFG_UPDATE() (FMC->ISPCON &= ~FMC_ISPCON_CFGUEN_Msk) /*!< Disable User Config update */ 64 | #define FMC_DISABLE_LD_UPDATE() (FMC->ISPCON &= ~FMC_ISPCON_LDUEN_Msk) /*!< Disable LDROM update */ 65 | #define FMC_DISABLE_ISP() (FMC->ISPCON &= ~FMC_ISPCON_ISPEN_Msk) /*!< Disable ISP function */ 66 | #define FMC_ENABLE_AP_UPDATE() (FMC->ISPCON |= FMC_ISPCON_APUEN_Msk) /*!< Enable APROM update */ 67 | #define FMC_ENABLE_LD_UPDATE() (FMC->ISPCON |= FMC_ISPCON_LDUEN_Msk) /*!< Enable LDROM update */ 68 | #define FMC_ENABLE_CFG_UPDATE() (FMC->ISPCON |= FMC_ISPCON_CFGUEN_Msk) /*!< Enable User Config update */ 69 | #define FMC_ENABLE_ISP() (FMC->ISPCON |= FMC_ISPCON_ISPEN_Msk) /*!< Enable ISP function */ 70 | #define FMC_GET_FAIL_FLAG() ((FMC->ISPCON & FMC_ISPCON_ISPFF_Msk) ? 1 : 0) /*!< Get ISP fail flag */ 71 | 72 | 73 | extern void FMC_SetBootSource(int32_t i32BootSrc); 74 | extern void FMC_Close(void); 75 | extern void FMC_DisableAPUpdate(void); 76 | extern void FMC_DisableConfigUpdate(void); 77 | extern void FMC_DisableLDUpdate(void); 78 | extern void FMC_EnableAPUpdate(void); 79 | extern void FMC_EnableConfigUpdate(void); 80 | extern void FMC_EnableLDUpdate(void); 81 | extern int32_t FMC_Erase(uint32_t u32PageAddr); 82 | extern int32_t FMC_GetBootSource(void); 83 | extern void FMC_Open(void); 84 | extern uint32_t FMC_Read (uint32_t u32Addr); 85 | extern uint32_t FMC_ReadCID(void); 86 | extern uint32_t FMC_ReadDID(void); 87 | extern uint32_t FMC_ReadPID(void); 88 | extern uint32_t FMC_ReadUCID(uint32_t u32Index); 89 | extern uint32_t FMC_ReadUID(uint32_t u32Index); 90 | extern uint32_t FMC_ReadDataFlashBaseAddr(void); 91 | extern void FMC_SetVectorPageAddr(uint32_t u32PageAddr); 92 | extern int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data); 93 | extern int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count); 94 | extern int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count); 95 | 96 | 97 | /*@}*/ /* end of group MINI51_FMC_EXPORTED_FUNCTIONS */ 98 | 99 | /*@}*/ /* end of group MINI51_FMC_Driver */ 100 | 101 | /*@}*/ /* end of group MINI51_Device_Driver */ 102 | 103 | #ifdef __cplusplus 104 | } 105 | #endif 106 | 107 | 108 | #endif 109 | 110 | -------------------------------------------------------------------------------- /lib-Mini51/StdDriver/inc/wdt.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file wdt.h 3 | * @version V1.00 4 | * $Revision: 5 $ 5 | * $Date: 13/10/05 1:30p $ 6 | * @brief Mini51 series WDT driver header file 7 | * 8 | * @note 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. 10 | *****************************************************************************/ 11 | #ifndef __WDT_H__ 12 | #define __WDT_H__ 13 | 14 | #ifdef __cplusplus 15 | extern "C" 16 | { 17 | #endif 18 | 19 | 20 | /** @addtogroup MINI51_Device_Driver MINI51 Device Driver 21 | @{ 22 | */ 23 | 24 | /** @addtogroup MINI51_WDT_Driver WDT Driver 25 | @{ 26 | */ 27 | 28 | /** @addtogroup MINI51_WDT_EXPORTED_CONSTANTS WDT Exported Constants 29 | @{ 30 | */ 31 | #define WDT_TIMEOUT_2POW4 (0UL << WDT_WTCR_WTIS_Pos) /*!< WDT setting for timeout interval = 2^4 * WDT clocks */ 32 | #define WDT_TIMEOUT_2POW6 (1UL << WDT_WTCR_WTIS_Pos) /*!< WDT setting for timeout interval = 2^6 * WDT clocks */ 33 | #define WDT_TIMEOUT_2POW8 (2UL << WDT_WTCR_WTIS_Pos) /*!< WDT setting for timeout interval = 2^8 * WDT clocks */ 34 | #define WDT_TIMEOUT_2POW10 (3UL << WDT_WTCR_WTIS_Pos) /*!< WDT setting for timeout interval = 2^10 * WDT clocks */ 35 | #define WDT_TIMEOUT_2POW12 (4UL << WDT_WTCR_WTIS_Pos) /*!< WDT setting for timeout interval = 2^12 * WDT clocks */ 36 | #define WDT_TIMEOUT_2POW14 (5UL << WDT_WTCR_WTIS_Pos) /*!< WDT setting for timeout interval = 2^14 * WDT clocks */ 37 | #define WDT_TIMEOUT_2POW16 (6UL << WDT_WTCR_WTIS_Pos) /*!< WDT setting for timeout interval = 2^16 * WDT clocks */ 38 | #define WDT_TIMEOUT_2POW18 (7UL << WDT_WTCR_WTIS_Pos) /*!< WDT setting for timeout interval = 2^18 * WDT clocks */ 39 | 40 | /*@}*/ /* end of group MINI51_WDT_EXPORTED_CONSTANTS */ 41 | 42 | 43 | /** @addtogroup MINI51_WDT_EXPORTED_FUNCTIONS WDT Exported Functions 44 | @{ 45 | */ 46 | 47 | /** 48 | * @brief This macro clear WDT time-out reset system flag. 49 | */ 50 | #define WDT_CLEAR_RESET_FLAG() (WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTIF_Msk | WDT_WTCR_WTWKF_Msk)) | WDT_WTCR_WTRF_Msk) 51 | 52 | /** 53 | * @brief This macro clear WDT time-out interupt flag. 54 | */ 55 | #define WDT_CLEAR_TIMEOUT_INT_FLAG() (WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTRF_Msk | WDT_WTCR_WTWKF_Msk)) | WDT_WTCR_WTIF_Msk) 56 | 57 | /** @brief This macro clear WDT time-out wake-up system flag. */ 58 | #define WDT_CLEAR_TIMEOUT_WAKEUP_FLAG() (WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTRF_Msk | WDT_WTCR_WTIF_Msk)) | WDT_WTCR_WTWKF_Msk) 59 | 60 | /** 61 | * @brief This macro indicate WDT time-out to reset system or not. 62 | * @return WDT reset system or not 63 | * @retval 0 WDT did not cause system reset 64 | * @retval 1 WDT caused system reset 65 | */ 66 | #define WDT_GET_RESET_FLAG() (WDT->WTCR & WDT_WTCR_WTRF_Msk ? 1 : 0) 67 | 68 | /** 69 | * @brief This macro indicate WDT time-out interrupt occurred or not. 70 | * @return WDT time-out interrupt occurred or not 71 | * @retval 0 WDT time-out interrupt did not occurr 72 | * @retval 1 WDT time-out interrupt occurred 73 | */ 74 | #define WDT_GET_TIMEOUT_INT_FLAG() (WDT->WTCR & WDT_WTCR_WTIF_Msk ? 1 : 0) 75 | 76 | /** 77 | * @brief This macro indicate WDT time-out waked system up or not 78 | * @return WDT time-out waked system up or not 79 | * @retval 0 WDT did not wake up system 80 | * @retval 1 WDT waked up system 81 | */ 82 | #define WDT_GET_TIMEOUT_WAKEUP_FLAG() (WDT->WTCR & WDT_WTCR_WTWKF_Msk ? 1 : 0) 83 | 84 | /** 85 | * @brief This macro is used to reset 18-bit WDT counter. 86 | * @details If WDT is activated and enabled to reset system, software must reset WDT counter 87 | * before WDT time-out plus reset delay reached. Or WDT generate a reset signal. 88 | */ 89 | #define WDT_RESET_COUNTER() (WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTIF_Msk | WDT_WTCR_WTWKF_Msk | WDT_WTCR_WTRF_Msk)) | WDT_WTCR_WTR_Msk) 90 | 91 | 92 | void WDT_Open(uint32_t u32TimeoutInterval, 93 | uint32_t u32ResetDelay, 94 | uint32_t u32EnableReset, 95 | uint32_t u32EnableWakeup); 96 | void WDT_Close(void); 97 | 98 | void WDT_EnableInt(void); 99 | void WDT_DisableInt(void); 100 | 101 | /*@}*/ /* end of group MINI51_WDT_EXPORTED_FUNCTIONS */ 102 | 103 | /*@}*/ /* end of group MINI51_WDT_Driver */ 104 | 105 | /*@}*/ /* end of group MINI51_Device_Driver */ 106 | 107 | #ifdef __cplusplus 108 | } 109 | #endif 110 | 111 | #endif //__WDT_H__ 112 | 113 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ 114 | -------------------------------------------------------------------------------- /lib-Mini51/StdDriver/src/gpio.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file gpio.c 3 | * @version V1.00 4 | * $Revision: 5 $ 5 | * $Date: 13/10/01 9:05a $ 6 | * @brief MINI51 series GPIO driver source file 7 | * 8 | * @note 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. 10 | *****************************************************************************/ 11 | #include "Mini51Series.h" 12 | 13 | 14 | 15 | /** @addtogroup MINI51_Device_Driver MINI51 Device Driver 16 | @{ 17 | */ 18 | 19 | /** @addtogroup MINI51_GPIO_Driver GPIO Driver 20 | @{ 21 | */ 22 | 23 | 24 | /** @addtogroup MINI51_GPIO_EXPORTED_FUNCTIONS GPIO Exported Functions 25 | @{ 26 | */ 27 | 28 | /** 29 | * @brief Set GPIO operation mode 30 | * 31 | * @param[in] PORT GPIO port. It could be P0, P1, P2, P3, P4 or P5. 32 | * @param[in] pin_mask The single or multiple pins of specified GPIO port. 33 | * @param[in] mode Operation mode. GPIO_PMD_INPUT, GPIO_PMD_OUTPUT, GPIO_PMD_OPEN_DRAIN, GPIO_PMD_QUASI 34 | * 35 | * @return None 36 | * 37 | * @details This function is used to set specified GPIO operation mode. 38 | */ 39 | void GPIO_SetMode(GPIO_T *PORT, uint32_t pin_mask, uint32_t mode) 40 | { 41 | uint32_t i; 42 | 43 | for (i=0; iPMD = (PORT->PMD & ~(0x3 << (i << 1))) | (mode << (i << 1)); 48 | } 49 | } 50 | } 51 | 52 | /** 53 | * @brief Enable GPIO interrupt 54 | * 55 | * @param[in] PORT GPIO port. It could be P0, P1, P2, P3, P4 or P5. 56 | * @param[in] u32Pin The pin of specified GPIO port. It could be 0 ~ 7. 57 | * @param[in] u32IntAttribs The interrupt attribute of specified GPIO pin. It could be \n 58 | * GPIO_INT_RISING, GPIO_INT_FALLING, GPIO_INT_BOTH_EDGE, GPIO_INT_HIGH, GPIO_INT_LOW. 59 | * 60 | * @return None 61 | * 62 | * @details This function is used to enable specified GPIO pin interrupt. 63 | */ 64 | void GPIO_EnableInt(GPIO_T *PORT, uint32_t u32Pin, uint32_t u32IntAttribs) 65 | { 66 | PORT->IMD |= (((u32IntAttribs >> 24) & 0xFFUL) << u32Pin); 67 | PORT->IEN |= ((u32IntAttribs & 0xFFFFFFUL) << u32Pin); 68 | } 69 | 70 | 71 | /** 72 | * @brief Disable GPIO interrupt 73 | * 74 | * @param[in] PORT GPIO port. It could be P0, P1, P2, P3, P4 or P5. 75 | * @param[in] u32Pin The pin of specified GPIO port. It could be 0 ~ 7. 76 | * 77 | * @return None 78 | * 79 | * @details This function is used to enable specified GPIO pin interrupt. 80 | */ 81 | void GPIO_DisableInt(GPIO_T *PORT, uint32_t u32Pin) 82 | { 83 | PORT->IMD &= ~(1UL << u32Pin); 84 | PORT->IEN &= ~((0x00010001UL) << u32Pin); 85 | } 86 | 87 | 88 | 89 | /*@}*/ /* end of group MINI51_GPIO_EXPORTED_FUNCTIONS */ 90 | 91 | /*@}*/ /* end of group MINI51_GPIO_Driver */ 92 | 93 | /*@}*/ /* end of group MINI51_Device_Driver */ 94 | 95 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ 96 | -------------------------------------------------------------------------------- /lib-Mini51/StdDriver/src/sys.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file sys.c 3 | * @version V1.00 4 | * $Revision: 12 $ 5 | * $Date: 13/10/01 9:25a $ 6 | * @brief MINI51 series SYS driver source file 7 | * 8 | * @note 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. 10 | *****************************************************************************/ 11 | #include "Mini51Series.h" 12 | /** @addtogroup MINI51_Device_Driver MINI51 Device Driver 13 | @{ 14 | */ 15 | 16 | /** @addtogroup MINI51_SYS_Driver SYS Driver 17 | @{ 18 | */ 19 | 20 | 21 | /** @addtogroup MINI51_SYS_EXPORTED_FUNCTIONS SYS Exported Functions 22 | @{ 23 | */ 24 | 25 | /** 26 | * @brief This function clear the selected system reset source 27 | * @param u32Src is system reset source 28 | * @return None 29 | */ 30 | void SYS_ClearResetSrc(uint32_t u32Src) 31 | { 32 | SYS->RSTSRC |= u32Src; 33 | } 34 | 35 | /** 36 | * @brief This function get Brown-out detector output status 37 | * @return 0: System voltage is higher than BOD_VL setting or BOD_EN is 0. 38 | * 1: System voltage is lower than BOD_VL setting. 39 | * Note : If the BOD_EN is 0, this function always return 0. 40 | */ 41 | uint32_t SYS_GetBODStatus(void) 42 | { 43 | return (SYS->BODCTL & SYS_BODCR_BOD_OUT_Msk)?1:0; 44 | } 45 | 46 | /** 47 | * @brief This function get the system reset source register value 48 | * @return Reset source 49 | */ 50 | uint32_t SYS_GetResetSrc(void) 51 | { 52 | return (SYS->RSTSRC); 53 | } 54 | 55 | /** 56 | * @brief This function check register write-protection bit setting 57 | * @return 0: Write-protection function is disabled. 58 | * 1: Write-protection function is enabled. 59 | */ 60 | uint32_t SYS_IsRegLocked(void) 61 | { 62 | return (SYS->RegLockAddr & SYS_RegLockAddr_RegUnLock_Msk); 63 | } 64 | 65 | /** 66 | * @brief This function enable register write-protection function 67 | * @return None 68 | * @details To lock the protected register to forbid write access 69 | */ 70 | void SYS_LockReg(void) 71 | { 72 | SYS->RegLockAddr = 0; 73 | } 74 | 75 | /** 76 | * @brief This function disable register write-protection function 77 | * @return None 78 | * @details To unlock the protected register to allow write access 79 | */ 80 | void SYS_UnlockReg(void) 81 | { 82 | while(SYS->RegLockAddr != SYS_RegLockAddr_RegUnLock_Msk) { 83 | SYS->RegLockAddr = 0x59; 84 | SYS->RegLockAddr = 0x16; 85 | SYS->RegLockAddr = 0x88; 86 | } 87 | } 88 | 89 | /** 90 | * @brief This function get product ID. 91 | * @return Product ID 92 | */ 93 | uint32_t SYS_ReadPDID(void) 94 | { 95 | return SYS->PDID; 96 | } 97 | 98 | /** 99 | * @brief This function reset chip. 100 | * @return None 101 | */ 102 | void SYS_ResetChip(void) 103 | { 104 | SYS->IPRSTC1 |= SYS_IPRSTC1_CHIP_RST_Msk; 105 | } 106 | 107 | /** 108 | * @brief This function reset CPU. 109 | * @return None 110 | */ 111 | void SYS_ResetCPU(void) 112 | { 113 | SYS->IPRSTC1 |= SYS_IPRSTC1_CPU_RST_Msk; 114 | } 115 | 116 | /** 117 | * @brief This function reset selected modules. 118 | * @param u32ModuleIndex is module index. Including : 119 | * - \ref CHIP_RST 120 | * - \ref CPU_RST 121 | * - \ref ADC_RST 122 | * - \ref ACMP_RST 123 | * - \ref PWM_RST 124 | * - \ref UART_RST 125 | * - \ref SPI_RST 126 | * - \ref I2C_RST 127 | * - \ref TMR1_RST 128 | * - \ref TMR0_RST 129 | * - \ref GPIO_RST 130 | * @return None 131 | */ 132 | void SYS_ResetModule(uint32_t u32ModuleIndex) 133 | { 134 | *(volatile uint32_t *)(&(SYS->IPRSTC1) + (u32ModuleIndex>>24)) |= 1<<(u32ModuleIndex & 0x00ffffff); 135 | *(volatile uint32_t *)(&(SYS->IPRSTC1) + (u32ModuleIndex>>24)) &= ~(1<<(u32ModuleIndex & 0x00ffffff)); 136 | } 137 | 138 | /** 139 | * @brief This function configure BOD function. 140 | * Configure BOD reset or interrupt mode and set Brown-out voltage level. 141 | * Enable Brown-out function 142 | * @param i32Mode is reset or interrupt mode. Including : 143 | * - \ref SYS_BODCR_BOD_RST_EN 144 | * - \ref SYS_BODCR_BOD_INTERRUPT_EN 145 | * @param u32BODLevel is Brown-out voltage level. Including : 146 | * - \ref SYS_BODCR_BOD_VL_4_4V 147 | * - \ref SYS_BODCR_BOD_VL_3_7V 148 | * - \ref SYS_BODCR_BOD_VL_2_7V 149 | * - \ref SYS_BODCR_BOD_VL_2_2V 150 | * @return None 151 | */ 152 | void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel) 153 | { 154 | SYS->BODCTL |= SYS_BODCR_BOD_VL_EXT_Msk; 155 | SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCR_BOD_RSTEN_Msk) | i32Mode; 156 | SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCR_BOD_VL_Msk) | u32BODLevel; 157 | } 158 | 159 | /** 160 | * @brief This function disable BOD function. 161 | * @return None 162 | */ 163 | void SYS_DisableBOD(void) 164 | { 165 | SYS->BODCTL &= ~SYS_BODCR_BOD_VL_EXT_Msk; 166 | SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCR_BOD_VL_Msk) | SYS_BODCR_BOD_DISABLE ; 167 | } 168 | 169 | 170 | 171 | /*@}*/ /* end of group MINI51_SYS_EXPORTED_FUNCTIONS */ 172 | 173 | /*@}*/ /* end of group MINI51_SYS_Driver */ 174 | 175 | /*@}*/ /* end of group MINI51_Device_Driver */ 176 | 177 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ 178 | -------------------------------------------------------------------------------- /lib-Mini51/StdDriver/src/wdt.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file wdt.c 3 | * @version V1.00 4 | * $Revision: 3 $ 5 | * $Date: 13/10/05 1:24p $ 6 | * @brief MINI51 series WDT driver source file 7 | * 8 | * @note 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. 10 | *****************************************************************************/ 11 | #include "Mini51Series.h" 12 | 13 | /** @addtogroup MINI51_Device_Driver MINI51 Device Driver 14 | @{ 15 | */ 16 | 17 | /** @addtogroup MINI51_WDT_Driver WDT Driver 18 | @{ 19 | */ 20 | 21 | 22 | /** @addtogroup MINI51_WDT_EXPORTED_FUNCTIONS WDT Exported Functions 23 | @{ 24 | */ 25 | 26 | /** 27 | * @brief This function make WDT module start counting with different time-out interval 28 | * @param[in] u32TimeoutInterval Time-out interval period of WDT module. Valid values are: 29 | * - \ref WDT_TIMEOUT_2POW4 30 | * - \ref WDT_TIMEOUT_2POW6 31 | * - \ref WDT_TIMEOUT_2POW8 32 | * - \ref WDT_TIMEOUT_2POW10 33 | * - \ref WDT_TIMEOUT_2POW12 34 | * - \ref WDT_TIMEOUT_2POW14 35 | * - \ref WDT_TIMEOUT_2POW16 36 | * - \ref WDT_TIMEOUT_2POW18 37 | * @param[in] u32ResetDelay This parameter is current not used 38 | * @param[in] u32EnableReset Enable WDT rest system function. Valid values are TRUE and FALSE 39 | * @param[in] u32EnableWakeup Enable WDT wake-up system function. Valid values are TRUE and FALSE 40 | * @return None 41 | */ 42 | void WDT_Open(uint32_t u32TimeoutInterval, 43 | uint32_t u32ResetDelay, 44 | uint32_t u32EnableReset, 45 | uint32_t u32EnableWakeup) 46 | { 47 | 48 | WDT->WTCR = u32TimeoutInterval | WDT_WTCR_WTE_Msk | 49 | (u32EnableReset << WDT_WTCR_WTRE_Pos) | 50 | (u32EnableWakeup << WDT_WTCR_WTWKE_Pos); 51 | return; 52 | } 53 | 54 | /** 55 | * @brief This function stops WDT counting and disable WDT module 56 | * @param None 57 | * @return None 58 | */ 59 | void WDT_Close(void) 60 | { 61 | WDT->WTCR = 0; 62 | return; 63 | } 64 | 65 | /** 66 | * @brief This function enables the WDT time-out interrupt 67 | * @param None 68 | * @return None 69 | */ 70 | void WDT_EnableInt(void) 71 | { 72 | WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTIF_Msk | WDT_WTCR_WTWKF_Msk | WDT_WTCR_WTRF_Msk)) | WDT_WTCR_WTIE_Msk; 73 | return; 74 | } 75 | 76 | /** 77 | * @brief This function disables the WDT time-out interrupt 78 | * @param None 79 | * @return None 80 | */ 81 | void WDT_DisableInt(void) 82 | { 83 | WDT->WTCR &= ~(WDT_WTCR_WTIF_Msk | WDT_WTCR_WTWKF_Msk | WDT_WTCR_WTRF_Msk | WDT_WTCR_WTIE_Msk); 84 | return; 85 | } 86 | 87 | 88 | 89 | /*@}*/ /* end of group MINI51_WDT_EXPORTED_FUNCTIONS */ 90 | 91 | /*@}*/ /* end of group MINI51_WDT_Driver */ 92 | 93 | /*@}*/ /* end of group MINI51_Device_Driver */ 94 | 95 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ 96 | -------------------------------------------------------------------------------- /lib-Mini51/hal/drv_gpio.c: -------------------------------------------------------------------------------- 1 | #include "hal.h" 2 | #include "drv_gpio.h" 3 | 4 | #if CONTROL_BOARD_TYPE == CONTROL_BOARD_WLT_V202 5 | #elif CONTROL_BOARD_TYPE == CONTROL_BOARD_JXD_JD385 6 | #else 7 | void gpioInit(GPIO_TypeDef *gpio, gpio_config_t *config) 8 | { 9 | uint32_t pinpos; 10 | for (pinpos = 0; pinpos < 16; pinpos++) { 11 | // are we doing this pin? 12 | if (config->pin & (0x1 << pinpos)) { 13 | // reference CRL or CRH, depending whether pin number is 0..7 or 8..15 14 | __IO uint32_t *cr = &gpio->CRL + (pinpos / 8); 15 | // mask out extra bits from pinmode, leaving just CNF+MODE 16 | uint32_t currentmode = config->mode & 0x0F; 17 | // offset to CNF and MODE portions of CRx register 18 | uint32_t shift = (pinpos % 8) * 4; 19 | // Read out current CRx value 20 | uint32_t tmp = *cr; 21 | // if we're in output mode, add speed too. 22 | if (config->mode & 0x10) 23 | currentmode |= config->speed; 24 | // Mask out 4 bits 25 | tmp &= ~(0xF << shift); 26 | // apply current pinmode 27 | tmp |= currentmode << shift; 28 | *cr = tmp; 29 | // Special handling for IPD/IPU 30 | if (config->mode == Mode_IPD) { 31 | gpio->ODR &= ~(1U << pinpos); 32 | } else if (config->mode == Mode_IPU) { 33 | gpio->ODR |= (1U << pinpos); 34 | } 35 | } 36 | } 37 | } 38 | #endif 39 | -------------------------------------------------------------------------------- /lib-Mini51/hal/drv_gpio.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Mini51 ports have only 8 pin each 4 | typedef enum 5 | { 6 | Pin_0 = 0x0001, 7 | Pin_1 = 0x0002, 8 | Pin_2 = 0x0004, 9 | Pin_3 = 0x0008, 10 | Pin_4 = 0x0010, 11 | Pin_5 = 0x0020, 12 | Pin_6 = 0x0040, 13 | Pin_7 = 0x0080, 14 | Pin_All = 0x00FF 15 | } GPIO_Pin; 16 | 17 | #if 0 18 | 19 | typedef enum 20 | { 21 | Mode_AIN = 0x0, 22 | Mode_IN_FLOATING = 0x04, 23 | Mode_IPD = 0x28, 24 | Mode_IPU = 0x48, 25 | Mode_Out_OD = 0x14, 26 | Mode_Out_PP = 0x10, 27 | Mode_AF_OD = 0x1C, 28 | Mode_AF_PP = 0x18 29 | } GPIO_Mode; 30 | 31 | typedef enum 32 | { 33 | Speed_10MHz = 1, 34 | Speed_2MHz, 35 | Speed_50MHz 36 | } GPIO_Speed; 37 | 38 | typedef enum 39 | { 40 | Pin_0 = 0x0001, 41 | Pin_1 = 0x0002, 42 | Pin_2 = 0x0004, 43 | Pin_3 = 0x0008, 44 | Pin_4 = 0x0010, 45 | Pin_5 = 0x0020, 46 | Pin_6 = 0x0040, 47 | Pin_7 = 0x0080, 48 | Pin_8 = 0x0100, 49 | Pin_9 = 0x0200, 50 | Pin_10 = 0x0400, 51 | Pin_11 = 0x0800, 52 | Pin_12 = 0x1000, 53 | Pin_13 = 0x2000, 54 | Pin_14 = 0x4000, 55 | Pin_15 = 0x8000, 56 | Pin_All = 0xFFFF 57 | } GPIO_Pin; 58 | 59 | typedef struct 60 | { 61 | uint16_t pin; 62 | GPIO_Mode mode; 63 | GPIO_Speed speed; 64 | } gpio_config_t; 65 | 66 | #define digitalHi(p, i) { p->BSRR = i; } 67 | #define digitalLo(p, i) { p->BRR = i; } 68 | #define digitalToggle(p, i) { p->ODR ^= i; } 69 | #define digitalIn(p, i) (p->IDR & i) 70 | 71 | void gpioInit(GPIO_TypeDef *gpio, gpio_config_t *config); 72 | #endif 73 | -------------------------------------------------------------------------------- /lib-Mini51/hal/drv_pwm.c: -------------------------------------------------------------------------------- 1 | #include "hal.h" 2 | #include "drv_pwm.h" 3 | #include "config.h" 4 | 5 | #define PULSE_1MS (1000) // 1ms pulse width 6 | 7 | #define MWII_PWM_MAX 1000 8 | #define MWII_PWM_PRE 1 9 | 10 | // returns whether driver is asking to calibrate throttle or not 11 | bool pwmInit(drv_pwm_config_t *init) 12 | { 13 | #if (CONTROL_BOARD_TYPE == CONTROL_BOARD_HUBSAN_H107L) || (CONTROL_BOARD_TYPE == CONTROL_BOARD_HUBSAN_Q4) 14 | 15 | CLK_EnableModuleClock(PWM01_MODULE); 16 | CLK_EnableModuleClock(PWM23_MODULE); 17 | 18 | // PWM clock source 19 | CLK->CLKSEL1 &= ~(CLK_CLKSEL1_PWM23_S_Msk | CLK_CLKSEL1_PWM01_S_Msk); 20 | CLK->CLKSEL1 |= CLK_CLKSEL1_PWM23_S_HCLK | CLK_CLKSEL1_PWM01_S_HCLK; 21 | 22 | // Multifuncional pin set up PWM0-4 23 | SYS->P2_MFP &= ~(SYS_MFP_P22_Msk | SYS_MFP_P23_Msk | SYS_MFP_P24_Msk | SYS_MFP_P25_Msk); 24 | SYS->P2_MFP |= SYS_MFP_P22_PWM0 | SYS_MFP_P23_PWM1 | SYS_MFP_P24_PWM2 | SYS_MFP_P25_PWM3; 25 | 26 | #define MWII_PWM_MASK ((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)) 27 | 28 | // Even channel N and N+1 share prescaler 29 | PWM_SET_PRESCALER(PWM, 0, MWII_PWM_PRE); 30 | PWM_SET_PRESCALER(PWM, 2, MWII_PWM_PRE); 31 | PWM_SET_DIVIDER(PWM, 0, PWM_CLK_DIV_1); 32 | PWM_SET_DIVIDER(PWM, 1, PWM_CLK_DIV_1); 33 | PWM_SET_DIVIDER(PWM, 2, PWM_CLK_DIV_1); 34 | PWM_SET_DIVIDER(PWM, 3, PWM_CLK_DIV_1); 35 | 36 | PWM_Start(PWM, MWII_PWM_MASK); 37 | // No analog of PWM_Start for enabling auto-reload mode 38 | for (int i = 0; i <= 3; ++i) { 39 | PWM->PCR |= PWM_PCR_CH0MOD_Msk << (4 * i); 40 | } 41 | 42 | // Duty 43 | PWM_SET_CMR(PWM, 0, 0); 44 | PWM_SET_CMR(PWM, 1, 0); 45 | PWM_SET_CMR(PWM, 2, 0); 46 | PWM_SET_CMR(PWM, 3, 0); 47 | // Period, actually sets it to safe value 1000+1 48 | PWM_SET_CNR(PWM, 0, MWII_PWM_MAX); 49 | PWM_SET_CNR(PWM, 1, MWII_PWM_MAX); 50 | PWM_SET_CNR(PWM, 2, MWII_PWM_MAX); 51 | PWM_SET_CNR(PWM, 3, MWII_PWM_MAX); 52 | 53 | // end of CONTROL_BOARD_TYPE == CONTROL_BOARD_HUBSAN_H107L 54 | #elif (CONTROL_BOARD_TYPE == CONTROL_BOARD_JXD_JD385) || (CONTROL_BOARD_TYPE == CONTROL_BOARD_WLT_V202) 55 | CLK_EnableModuleClock(PWM23_MODULE); 56 | CLK_EnableModuleClock(PWM45_MODULE); 57 | 58 | // PWM clock source 59 | CLK->CLKSEL1 &= ~CLK_CLKSEL1_PWM23_S_Msk; 60 | CLK->CLKSEL1 |= CLK_CLKSEL1_PWM23_S_HCLK; 61 | CLK->CLKSEL2 &= ~CLK_CLKSEL2_PWM45_S_Msk; 62 | CLK->CLKSEL2 |= CLK_CLKSEL2_PWM45_S_HCLK; 63 | 64 | // Multifuncional pin set up PWM2-5 65 | SYS->P2_MFP &= ~(SYS_MFP_P24_Msk | SYS_MFP_P25_Msk | SYS_MFP_P26_Msk); 66 | SYS->P2_MFP |= SYS_MFP_P24_PWM2 | SYS_MFP_P25_PWM3 | SYS_MFP_P26_PWM4; 67 | SYS->P0_MFP &= ~SYS_MFP_P04_Msk; 68 | SYS->P0_MFP |= SYS_MFP_P04_PWM5; 69 | 70 | // 71 | 72 | #define MWII_PWM_MASK ((1 << 2) | (1 << 3) | (1 << 4) | (1 << 5)) 73 | 74 | // SYS_ResetModule(PWM_RST); 75 | 76 | // Even channel N and N+1 share prescaler 77 | PWM_SET_PRESCALER(PWM, 2, MWII_PWM_PRE); 78 | PWM_SET_PRESCALER(PWM, 4, MWII_PWM_PRE); 79 | PWM_SET_DIVIDER(PWM, 2, PWM_CLK_DIV_1); 80 | PWM_SET_DIVIDER(PWM, 3, PWM_CLK_DIV_1); 81 | PWM_SET_DIVIDER(PWM, 4, PWM_CLK_DIV_1); 82 | PWM_SET_DIVIDER(PWM, 5, PWM_CLK_DIV_1); 83 | 84 | PWM_Start(PWM, MWII_PWM_MASK); 85 | // No analog of PWM_Start for enabling auto-reload mode 86 | for (int i = 2; i <= 5; ++i) { 87 | PWM->PCR |= PWM_PCR_CH0MOD_Msk << (4 * i); 88 | } 89 | // PWM->PCR = PWM_PCR_CH3EN_Msk | PWM_PCR_CH3MOD_Msk; 90 | 91 | 92 | // Duty 93 | PWM_SET_CMR(PWM, 2, 0); 94 | PWM_SET_CMR(PWM, 3, 0); 95 | PWM_SET_CMR(PWM, 4, 0); 96 | PWM_SET_CMR(PWM, 5, 0); 97 | // Period, actually sets it to safe value 1000+1 98 | PWM_SET_CNR(PWM, 2, MWII_PWM_MAX); 99 | PWM_SET_CNR(PWM, 3, MWII_PWM_MAX); 100 | PWM_SET_CNR(PWM, 4, MWII_PWM_MAX); 101 | PWM_SET_CNR(PWM, 5, MWII_PWM_MAX); 102 | #endif // 103 | PWM_EnableOutput(PWM, MWII_PWM_MASK); 104 | 105 | return false; 106 | } 107 | void pwmWriteMotor(uint8_t index, uint16_t value) 108 | { 109 | #if CONTROL_BOARD_TYPE == CONTROL_BOARD_WLT_V202 110 | // Motor 0 BACK_R - PWM4 111 | // Motor 1 FRONT_R - PWM5 112 | // Motor 2 BACK_L - PWM3 113 | // Motor 3 FRONT_L - PWM2 114 | static uint8_t motor_to_pwm[] = { 4, 5, 3, 2 }; 115 | #elif CONTROL_BOARD_TYPE == CONTROL_BOARD_JXD_JD385 116 | // Motor 0 BACK_R - PWM2 117 | // Motor 1 FRONT_R - PWM3 118 | // Motor 2 BACK_L - PWM5 119 | // Motor 3 FRONT_L - PWM4 120 | static uint8_t motor_to_pwm[] = { 2, 3, 5, 4 }; 121 | #elif (CONTROL_BOARD_TYPE == CONTROL_BOARD_HUBSAN_H107L) || (CONTROL_BOARD_TYPE == CONTROL_BOARD_HUBSAN_Q4) 122 | // Motor 1 BACK_R - PWM2 123 | // Motor 3 FRONT_R - PWM3 124 | // Motor 2 BACK_L - PWM1 125 | // Motor 4 FRONT_L - PWM0 126 | static uint8_t motor_to_pwm[] = { 2, 3, 1, 0}; 127 | #endif 128 | if (index > 3) return; 129 | PWM_SET_CMR(PWM, motor_to_pwm[index], value-1000); 130 | } 131 | 132 | // Not implmented 133 | //void pwmWriteServo(uint8_t index, uint16_t value) 134 | //{ 135 | //} 136 | //uint16_t pwmRead(uint8_t channel) 137 | //{ 138 | // return 0; 139 | //} 140 | -------------------------------------------------------------------------------- /lib-Mini51/hal/drv_pwm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #define MAX_MOTORS 12 7 | #define MAX_SERVOS 8 8 | #define MAX_INPUTS 8 9 | 10 | typedef struct drv_pwm_config_t { 11 | bool usePPM; 12 | bool useUART; 13 | bool useServos; 14 | bool extraServos; // configure additional 4 channels in PPM mode as servos, not motors 15 | bool airplane; // fixed wing hardware config, lots of servos etc 16 | uint16_t motorPwmRate; 17 | uint16_t servoPwmRate; 18 | } drv_pwm_config_t; 19 | 20 | // This indexes into the read-only hardware definition structure in drv_pwm.c, as well as into pwmPorts[] structure with dynamic data. 21 | enum { 22 | PWM1 = 0, 23 | PWM2, 24 | PWM3, 25 | PWM4, 26 | PWM5, 27 | PWM6, 28 | PWM7, 29 | PWM8, 30 | PWM9, 31 | PWM10, 32 | PWM11, 33 | PWM12, 34 | PWM13, 35 | PWM14, 36 | MAX_PORTS 37 | }; 38 | 39 | bool pwmInit(drv_pwm_config_t *init); // returns whether driver is asking to calibrate throttle or not 40 | void pwmWriteMotor(uint8_t index, uint16_t value); 41 | void pwmWriteServo(uint8_t index, uint16_t value); 42 | uint16_t pwmRead(uint8_t channel); 43 | -------------------------------------------------------------------------------- /lib-Mini51/hal/drv_serial.c: -------------------------------------------------------------------------------- 1 | #include "hal.h" 2 | #include "drv_serial.h" 3 | 4 | //#define USE_PERIPH_BUFFERS 5 | 6 | serialPort_t serialPort1; 7 | 8 | // UART1 - Configuration 9 | serialPort_t *serialUART1(uint32_t baudRate, portMode_t mode) 10 | { 11 | serialPort_t *s; 12 | #if !defined(USE_PERIPH_BUFFERS) 13 | static volatile uint8_t rx1Buffer[UART1_RX_BUFFER_SIZE]; 14 | static volatile uint8_t tx1Buffer[UART1_TX_BUFFER_SIZE]; 15 | #endif 16 | s = &serialPort1; 17 | #if !defined(USE_PERIPH_BUFFERS) 18 | s->rxBufferSize = UART1_RX_BUFFER_SIZE; 19 | s->txBufferSize = UART1_TX_BUFFER_SIZE; 20 | s->rxBuffer = rx1Buffer; 21 | s->txBuffer = tx1Buffer; 22 | #endif 23 | s->UARTx = UART; // extraneous 24 | 25 | // UART RXD P12 and TXD P13 26 | SYS->P1_MFP &= ~(SYS_MFP_P12_Msk | SYS_MFP_P13_Msk); 27 | SYS->P1_MFP |= (SYS_MFP_P12_RXD | SYS_MFP_P13_TXD); 28 | 29 | return s; 30 | } 31 | 32 | serialPort_t *serialOpen(UART_T *UARTx, serialReceiveCallbackPtr callback, uint32_t baudRate, portMode_t mode) 33 | { 34 | serialPort_t *s = NULL; 35 | 36 | if (UARTx == UART) 37 | s = serialUART1(baudRate, mode); 38 | 39 | s->UARTx = UARTx; 40 | 41 | // Clear the FIFOs 42 | UARTx->FSR |= (UART_FCR_TFR_Msk | UART_FCR_RFR_Msk); 43 | s->rxBufferHead = s->rxBufferTail = 0; 44 | s->txBufferHead = s->txBufferTail = 0; 45 | // callback for IRQ-based RX ONLY 46 | s->callback = callback; 47 | s->mode = mode; 48 | s->baudRate = baudRate; 49 | 50 | // For simplicity, ignore word length, stop bits, parity, mode 51 | // If we really need it, use UART_SetLine_Config, and for mode, 52 | // use MFP to disable either TX or RX 53 | UART_Open(UARTx, baudRate); 54 | // If we use UART_SetLine_Config, we need this: 55 | // UARTx->FUN_SEL = UART_FUNC_SEL_UART; 56 | 57 | 58 | if ((mode & MODE_RX) 59 | #if defined(USE_PERIPH_BUFFERS) 60 | && callback 61 | #endif 62 | ) { 63 | // Rx ready interrupt and buffer error interrupt 64 | // Buffer error handles hardware RX buffer overflow, 65 | // otherwise RX stops receiving 66 | UART_EnableInt(UARTx, UART_IER_RDA_IEN_Msk | UART_IER_BUF_ERR_IEN_Msk); 67 | } 68 | 69 | return s; 70 | } 71 | 72 | uint8_t uartAvailable(serialPort_t *s) 73 | { 74 | // return (s->UARTx->FSR & UART_FSR_RX_POINTER_Msk) >> UART_FSR_RX_POINTER_Pos; 75 | #if defined(USE_PERIPH_BUFFERS) 76 | return !(UART->FSR & UART_FSR_RX_EMPTY_Msk); 77 | #else 78 | return (s->rxBufferHead - s->rxBufferTail) % s->rxBufferSize; 79 | #endif 80 | } 81 | 82 | uint8_t uartRead(serialPort_t *s) 83 | { 84 | #if defined(USE_PERIPH_BUFFERS) 85 | return UART_READ(s->UARTx); 86 | #else 87 | uint8_t ch; 88 | 89 | ch = s->rxBuffer[s->rxBufferTail]; 90 | s->rxBufferTail = (s->rxBufferTail + 1) % s->rxBufferSize; 91 | 92 | return ch; 93 | #endif 94 | } 95 | 96 | void uartWrite(serialPort_t *s, uint8_t ch) 97 | { 98 | #if defined(USE_PERIPH_BUFFERS) 99 | UART_WRITE(s->UARTx, ch); 100 | #else 101 | // Wait here if buffer is full 102 | uint32_t nextHead = (s->txBufferHead + 1) % s->txBufferSize; 103 | while (nextHead == s->txBufferTail) ; 104 | s->txBuffer[s->txBufferHead] = ch; 105 | s->txBufferHead = nextHead; 106 | 107 | // Enable transmit by enabling TX empty interrupt 108 | UART_EnableInt(s->UARTx, UART_IER_THRE_IEN_Msk); 109 | #endif 110 | } 111 | 112 | 113 | void uartInit() 114 | { 115 | CLK_EnableModuleClock(UART_MODULE); 116 | 117 | // UART clock source 118 | // CLK_SetModuleClock(UART_MODULE,CLK_CLKSEL1_UART_S_IRC22M,CLK_CLKDIV_UART(1)); 119 | CLK->CLKSEL1 &= ~CLK_CLKSEL1_UART_S_Msk; 120 | CLK->CLKSEL1 |= CLK_CLKSEL1_UART_S_IRC22M;// Clock source from internal 22.1184MHz RC clock 121 | CLK->CLKDIV &= ~CLK_CLKDIV_UART_N_Msk; 122 | CLK->CLKDIV |= CLK_CLKDIV_UART(1); 123 | // SYS_ResetModule(SYS_IPRSTC2_UART_RST_Msk); 124 | } 125 | 126 | 127 | 128 | // Handlers 129 | 130 | // USART1 Rx/Tx IRQ Handler 131 | void UART_IRQHandler(void) 132 | { 133 | serialPort_t *s = &serialPort1; 134 | UART_T *uart = s->UARTx; 135 | uint16_t isr = uart->ISR; 136 | 137 | if (isr & UART_ISR_RDA_INT_Msk) { 138 | /* Get all the input characters */ 139 | while(uart->ISR & UART_ISR_RDA_IF_Msk) { 140 | /* Get the character from UART Buffer */ 141 | uint8_t u8InChar = uart->RBR; 142 | // If we registered a callback, pass crap there 143 | if (s->callback) { 144 | s->callback(u8InChar); 145 | } else { 146 | s->rxBuffer[s->rxBufferHead] = u8InChar; 147 | s->rxBufferHead = (s->rxBufferHead + 1) % s->rxBufferSize; 148 | } 149 | } 150 | } 151 | if (isr & UART_ISR_THRE_INT_Msk) { 152 | if (s->txBufferTail != s->txBufferHead) { 153 | UART_WRITE(uart, s->txBuffer[s->txBufferTail]); 154 | s->txBufferTail = (s->txBufferTail + 1) % s->txBufferSize; 155 | } else { 156 | // Don't use UART_DisableInt here, it will disable 157 | // ALL interrupts from UART 158 | uart->IER &= ~UART_IER_THRE_IEN_Msk; 159 | } 160 | } 161 | if (isr & UART_ISR_BUF_ERR_INT_Msk) { 162 | if (uart->FSR | UART_FSR_RX_OVER_IF_Msk) { 163 | // Clear receive buffer - can't rely on it's content 164 | // after overflow 165 | // s->rxBufferHead = s->rxBufferTail = 0; 166 | } 167 | uart->FSR |= UART_FSR_BIF_Msk | UART_FSR_FEF_Msk | UART_FSR_PEF_Msk | UART_FSR_RX_OVER_IF_Msk; 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /lib-Mini51/hal/drv_serial.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #define UART_BUFFER_SIZE 64 5 | 6 | #define UART1_RX_BUFFER_SIZE 256 7 | #define UART1_TX_BUFFER_SIZE 16 8 | #define UART2_RX_BUFFER_SIZE 64 9 | #define UART2_TX_BUFFER_SIZE 64 10 | #define UART3_RX_BUFFER_SIZE 64 11 | #define UART3_TX_BUFFER_SIZE 64 12 | 13 | typedef enum portMode_t { 14 | MODE_RX = 1, 15 | MODE_TX = 2, 16 | MODE_RXTX = MODE_RX | MODE_TX 17 | } portMode_t; 18 | 19 | typedef void (* serialReceiveCallbackPtr)(uint8_t data); // used by serial drivers to return frames to app 20 | 21 | typedef struct { 22 | portMode_t mode; 23 | uint32_t baudRate; 24 | uint32_t rxBufferSize; 25 | uint32_t txBufferSize; 26 | volatile uint8_t *rxBuffer; 27 | volatile uint8_t *txBuffer; 28 | uint32_t rxBufferHead; 29 | uint32_t rxBufferTail; 30 | volatile uint32_t txBufferHead; 31 | volatile uint32_t txBufferTail; 32 | 33 | UART_T *UARTx; 34 | 35 | serialReceiveCallbackPtr callback; 36 | } serialPort_t; 37 | 38 | extern serialPort_t serialPort1; 39 | //extern serialPort_t serialPort2; 40 | //extern serialPort_t serialPort3; 41 | 42 | serialPort_t *serialOpen(UART_T *UARTx, serialReceiveCallbackPtr callback, uint32_t baudRate, portMode_t mode); 43 | // Available chars in RX queue 44 | uint8_t uartAvailable(serialPort_t *s); 45 | uint8_t uartRead(serialPort_t *s); 46 | void uartWrite(serialPort_t *s, uint8_t ch); 47 | void uartInit(void); 48 | -------------------------------------------------------------------------------- /lib-Mini51/hal/hal.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define __USE_C99_MATH 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "Mini51Series.h" 14 | // These includes are totally not needed here, they only bring 15 | // dependency on actual processor details into main source 16 | //#include "drv_gpio.h" 17 | //#include "drv_serial.h" 18 | //#include "drv_pwm.h" 19 | 20 | void lib_hal_init(void); 21 | // eeprom_read and eeprom_write return number of read/written bytes 22 | size_t eeprom_read_block (void *dst, uint16_t index, size_t size); 23 | size_t eeprom_write_block (const void *src, uint16_t index, size_t size); 24 | void eeprom_commit(void); 25 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_adc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 TheLastMutt 3 | * This file is free. It comes without any warranty, to the extent permitted by applicable law. 4 | * You can redistribute it and/or modify it under the terms of the 5 | * Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. 6 | * See http://www.wtfpl.net/ for more details. 7 | */ 8 | 9 | #include "hal.h" 10 | #include "lib_fp.h" 11 | #include "lib_adc.h" 12 | #include "config.h" 13 | 14 | // ADC reference voltage defined in config_*.h as fixedpointnum 15 | #define FP_ADC_REF_VOLTAGE FIXEDPOINTCONSTANT(ADC_REF_VOLTAGE) 16 | 17 | void lib_adc_init(void) { 18 | CLK_EnableModuleClock(ADC_MODULE); 19 | 20 | // At the moment just for testing! 21 | // Disabel SetModuleClock for ADC when using serial because the ClockModul is already setup to meet the BAUD settings 22 | #if MULTIWII_CONFIG_SERIAL_PORTS == NOSERIALPORT 23 | // 22MHz / 75 = 293kHz clock for ADC module 24 | // -> approx 137µs conversion time. 25 | CLK_SetModuleClock(ADC_MODULE, 26 | CLK_CLKSEL1_ADC_S_IRC22M, 27 | CLK_CLKDIV_ADC(50)); 28 | #else 29 | #warning "ADC conversion time is depenend from serial clock speed!" 30 | #endif 31 | 32 | ADC_POWER_ON(ADC); 33 | } // lib_adc_init() 34 | 35 | void lib_adc_select_channel(lib_adc_channel_t channel) { 36 | // While changing channel, ADST bit must be cleared 37 | ADC_STOP_CONV(ADC); 38 | 39 | if (channel == LIB_ADC_CHANREF) { 40 | // Needs special treatment, shared with input 7 41 | ADC_CONFIG_CH7(ADC, ADC_CH7_BGP); 42 | channel = LIB_ADC_CHAN7; 43 | } else if (channel == LIB_ADC_CHAN7) { 44 | ADC_CONFIG_CH7(ADC, ADC_CH7_EXT); 45 | } 46 | 47 | ADC_SET_INPUT_CHANNEL(ADC,channel); 48 | } // lib_adc_select_channel() 49 | 50 | bool lib_adc_is_busy(void) { 51 | return ADC_IS_BUSY(ADC); 52 | } 53 | 54 | void lib_adc_startconv(void) { 55 | ADC_START_CONV(ADC); 56 | } 57 | 58 | // Returns measured absolute voltage as fixedpointnum 59 | fixedpointnum lib_adc_read_volt(void) { 60 | fixedpointnum voltage; 61 | // ADC_GET_CONVERSION_DATA() returns uint32, only lower 10 bits used 62 | voltage = (fixedpointnum) ADC_GET_CONVERSION_DATA(ADC, 0); 63 | // Now shift left to get a fixedpointnum, which then is a fraction 64 | // relative to ADC reference voltage 65 | voltage = voltage << (FIXEDPOINTSHIFT - 10); 66 | // Now multiply by supply voltage 67 | voltage = lib_fp_multiply(voltage, FP_ADC_REF_VOLTAGE); 68 | return voltage; 69 | } // lib_adc_read_volt() 70 | 71 | // Returns ADC result as fixedpointnum between 0..1 72 | fixedpointnum lib_adc_read_raw(void) { 73 | fixedpointnum voltage; 74 | // ADC_GET_CONVERSION_DATA() returns uint32, only lower 10 bits used 75 | voltage = (fixedpointnum) ADC_GET_CONVERSION_DATA(ADC, 0); 76 | // Now shift left to get a fixedpointnum, which then is a fraction 77 | // relative to ADC reference voltage 78 | voltage = voltage << (FIXEDPOINTSHIFT - 10); 79 | return voltage; 80 | } // lib_adc_read_volt() 81 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_adc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 TheLastMutt 3 | * This file is free. It comes without any warranty, to the extent permitted by applicable law. 4 | * You can redistribute it and/or modify it under the terms of the 5 | * Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. 6 | * See http://www.wtfpl.net/ for more details. 7 | */ 8 | 9 | #ifndef LIB_ADC_H_ 10 | #define LIB_ADC_H_ 11 | 12 | // Parameter type for lib_adc_select_channel 13 | typedef enum lib_adc_channel_tag 14 | { 15 | LIB_ADC_CHAN0 = (1 << 0), // Pin AIN0 16 | LIB_ADC_CHAN1 = (1 << 1), // .. 17 | LIB_ADC_CHAN2 = (1 << 2), 18 | LIB_ADC_CHAN3 = (1 << 3), 19 | LIB_ADC_CHAN4 = (1 << 4), 20 | LIB_ADC_CHAN5 = (1 << 5), 21 | LIB_ADC_CHAN6 = (1 << 6), 22 | LIB_ADC_CHAN7 = (1 << 7), // Pin AIN7 23 | LIB_ADC_CHANREF = 0xFF // Internal bandgap reference, needs ADC Clock < 300kHz 24 | } lib_adc_channel_t; 25 | 26 | void lib_adc_init(void); 27 | void lib_adc_select_channel(lib_adc_channel_t channel); 28 | bool lib_adc_is_busy(void); 29 | void lib_adc_startconv(void); 30 | // Returns absolute voltage 31 | fixedpointnum lib_adc_read_volt(void); 32 | // Returns 0..1 33 | fixedpointnum lib_adc_read_raw(void); 34 | 35 | 36 | #endif /* LIB_ADC_H_ */ 37 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_digitalio.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "hal.h" 19 | #include "lib_digitalio.h" 20 | #include "drv_gpio.h" 21 | 22 | // This code controls the simple digital IO 23 | 24 | // Usage: 25 | 26 | // #define LEDOUTPUT (DIGITALPORTB | 7) // defines the reference for pin 7 on PORTB 27 | // lib_digitalio_initpin(LEDOUTPUT,DIGITALOUTPUT | PULLUPRESISTOR); // set the pin as an output (also turns on the pull up resistor) 28 | // lib_digitalio_setoutput(LEDOUTPUT, DIGITALLOW); // turn the output on by pulling it low 29 | 30 | // #define PUSHBUTTON (DIGITALPORTB | 4) // defines the reference for pin 4 on PORTB 31 | // #define INTERRUPT6PORTANDPIN PUSHBUTTON 32 | // lib_digitalio_initpin(PUSHBUTTON,DIGITALINPUT); // set the pin as an input (also turns on the pull up resistor) 33 | // if (lib_digitalio_getinput(PUSHBUTTON)) {} // Check the input 34 | // unimplemented below: 35 | // lib_digitalio_setinterruptcallback(PUSHBUTTON,mypushbuttoncallback); // tell the interrupt 36 | // void mypushbuttoncallback(char interruptnumber,char newstate) // call back will get called any time the pin changes 37 | // { 38 | // if (newstate==DIGITALON) {} 39 | // } 40 | 41 | void lib_digitalio_initpin(unsigned char portandpinnumber, unsigned char output) 42 | { 43 | uint8_t port = (portandpinnumber & 0xF0) >> 4; 44 | uint8_t pin = portandpinnumber & 0x0F; 45 | // Call drv_gpio here 46 | uint32_t mode = output == DIGITALINPUT ? GPIO_PMD_INPUT : GPIO_PMD_OUTPUT; 47 | GPIO_SetMode(((GPIO_T *) (P0_BASE + 0x40*port)), (1 << pin), mode); 48 | } 49 | 50 | unsigned char lib_digitalio_getinput(unsigned char portandpinnumber) 51 | { 52 | uint8_t port = (portandpinnumber & 0xF0) >> 4; 53 | uint8_t pin = portandpinnumber & 0x0F; 54 | return GPIO_PIN_ADDR(port, pin); 55 | } 56 | 57 | void lib_digitalio_setoutput(unsigned char portandpinnumber, unsigned char value) 58 | { 59 | uint8_t port = (portandpinnumber & 0xF0) >> 4; 60 | uint8_t pin = portandpinnumber & 0x0F; 61 | GPIO_PIN_ADDR(port, pin) = value ? 1 : 0; 62 | } 63 | 64 | void lib_digitalio_setinterruptcallback(unsigned char pinnumber, digitalcallbackfunctptr callback) 65 | { 66 | // Not implemented, no need on real hardware... 67 | } 68 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_digitalio.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #pragma once 19 | 20 | #include "projectsettings.h" 21 | 22 | #define DIGITALINPUT 0 23 | #define DIGITALOUTPUT 1 24 | 25 | #define DIGITALPORTA 0x10 26 | #define DIGITALPORTB 0x20 27 | #define DIGITALPORTC 0x30 28 | 29 | #define DIGITALPORT0 0x00 30 | #define DIGITALPORT1 0x10 31 | #define DIGITALPORT2 0x20 32 | #define DIGITALPORT3 0x30 33 | #define DIGITALPORT4 0x40 34 | #define DIGITALPORT5 0x50 35 | 36 | #define DIGITALHIGH 0 37 | #define DIGITALLOW 1 38 | 39 | #define DIGITALON 1 40 | #define DIGITALOFF 0 41 | 42 | void lib_digitalio_initpin(unsigned char portandpinnumber, unsigned char output); 43 | unsigned char lib_digitalio_getinput(unsigned char portandpinnumber); 44 | void lib_digitalio_setoutput(unsigned char portandpinnumber,unsigned char value); 45 | 46 | typedef void (* digitalcallbackfunctptr)(unsigned char interruptnumber, unsigned char newpinstate); 47 | void lib_digitalio_setinterruptcallback(unsigned char portandpinnumber, digitalcallbackfunctptr callback); 48 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_fp.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | 19 | // a fixedpointnum is a real number that's stored in a long that's shifted left FIXEDPOINTSHIFT bits 20 | // FIXEDPOINTSHIFT is currently 16, so 16 bits are used for the integer part of the number and 21 | // the other 16 bits are used for the fractional part of the number. I've tried using different 22 | // values, but the compliler likes shifting things by 16 bits (words at a time) so things run 23 | // faster with a FIXEDPOINTSHIFT of 16. 24 | 25 | // Therefore, the range of a fixedpointnum is -32768.0 to 32767.0 with an accuracy of 0.000015 26 | // There is no overflow or underflow protection, so it's up to the programmer to watch out. 27 | 28 | #include "hal.h" 29 | 30 | #define fixedpointnum int32_t 31 | 32 | #define FIXEDPOINTSHIFT 16 33 | 34 | #define FIXEDPOINTONE (1L<>1) 49 | #define FIXEDPOINTONEOVERFOUR (FIXEDPOINTONE>>2) 50 | #define FIXEDPOINTONEOVERONEFOURTH (FIXEDPOINTONE<<2) 51 | #define FIXEDPOINTONEOVERONESIXTEENTH (FIXEDPOINTONE<<4) 52 | #define FIXEDPOINTONEOVERONESIXTYITH (60L<. 16 | */ 17 | 18 | #pragma once 19 | 20 | #define I2C_READ 1 21 | #define I2C_WRITE 0 22 | 23 | #define I2C_100_KHZ 0 24 | #define I2C_400_KHZ 1 25 | 26 | void lib_i2c_init(void); 27 | void lib_i2c_setclockspeed(unsigned char speed); 28 | unsigned char lib_i2c_start(unsigned char address); 29 | char lib_i2c_start_wait(unsigned char address); 30 | unsigned char lib_i2c_rep_start(unsigned char address); 31 | void lib_i2c_stop(void); 32 | unsigned char lib_i2c_write( unsigned char data ); 33 | unsigned char lib_i2c_readack(void); 34 | unsigned char lib_i2c_readnak(void); 35 | void lib_i2c_writereg(unsigned char address,unsigned char reg, unsigned char value); 36 | unsigned char lib_i2c_readreg(unsigned char address,unsigned char reg); 37 | void lib_i2c_readdata(unsigned char address,unsigned char reg,unsigned char *data,unsigned char length); 38 | 39 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | 19 | #pragma once 20 | 21 | void lib_serial_initport(unsigned char serialportnumber,long baud); 22 | void lib_serial_sendchar(unsigned char serialportnumber,unsigned char c); 23 | void lib_serial_sendstring(unsigned char serialportnumber,char *string); 24 | void lib_serial_senddata(unsigned char serialportnumber,unsigned char *data,int datalength); 25 | int lib_serial_numcharsavailable(unsigned char serialportnumber); 26 | unsigned char lib_serial_getchar(unsigned char serialportnumber); 27 | void lib_serial_getdata(unsigned char serialportnumber,unsigned char *data,int datalength); 28 | int lib_serial_availableoutputbuffersize(unsigned char serialportnumber); 29 | 30 | typedef void (* serialcallbackfunctptr)(unsigned char c); 31 | void lib_serial_setrxcallback(unsigned char serialportnumber,serialcallbackfunctptr callback); 32 | 33 | 34 | #define USBPORTNUMBER 5 35 | #define SEMIHOSTPORTNUMBER 6 36 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_soft_3_wire_spi.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Goebish 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "hal.h" 19 | #include "lib_soft_3_wire_spi.h" 20 | 21 | static uint8_t pin_SDIO, pin_SCK, pin_SCS; 22 | void lib_soft_3_wire_spi_setSCK(uint8_t state); 23 | void lib_soft_3_wire_spi_setSDIO(uint8_t state); 24 | 25 | void lib_soft_3_wire_spi_setCS(uint8_t state) 26 | { 27 | lib_digitalio_setoutput( pin_SCS, state); 28 | } 29 | 30 | void lib_soft_3_wire_spi_setSCK(uint8_t state) 31 | { 32 | lib_digitalio_setoutput( pin_SCK, state); 33 | } 34 | 35 | void lib_soft_3_wire_spi_setSDIO(uint8_t state) 36 | { 37 | lib_digitalio_setoutput( pin_SDIO, state); 38 | } 39 | 40 | void lib_soft_3_wire_spi_init(uint8_t SDIO_portandpinnumber, uint8_t SCK_portandpinnumber, uint8_t SCS_portandpinnumber ) 41 | { 42 | pin_SDIO = SDIO_portandpinnumber; 43 | pin_SCK = SCK_portandpinnumber; 44 | pin_SCS = SCS_portandpinnumber; 45 | lib_digitalio_initpin(pin_SDIO, DIGITALOUTPUT); 46 | lib_digitalio_initpin(pin_SCK, DIGITALOUTPUT); 47 | lib_digitalio_initpin(pin_SCS, DIGITALOUTPUT); 48 | lib_soft_3_wire_spi_setCS(DIGITALOFF); 49 | lib_soft_3_wire_spi_setSDIO(DIGITALOFF); 50 | lib_soft_3_wire_spi_setSCK(DIGITALOFF); 51 | } 52 | 53 | void lib_soft_3_wire_spi_write(uint8_t data) 54 | { 55 | uint8_t n=8; 56 | lib_soft_3_wire_spi_setSCK(DIGITALOFF); 57 | lib_soft_3_wire_spi_setSDIO(DIGITALOFF); 58 | while(n--) { 59 | if(data&0x80) // MSB first 60 | lib_soft_3_wire_spi_setSDIO(DIGITALON); 61 | else 62 | lib_soft_3_wire_spi_setSDIO(DIGITALOFF); 63 | lib_soft_3_wire_spi_setSCK(DIGITALON); 64 | lib_soft_3_wire_spi_setSCK(DIGITALOFF); 65 | data = data << 1; 66 | } 67 | lib_soft_3_wire_spi_setSDIO(DIGITALON); 68 | } 69 | 70 | uint8_t lib_soft_3_wire_spi_read(void) 71 | { 72 | uint8_t result=0; 73 | uint8_t i; 74 | lib_digitalio_initpin( pin_SDIO, DIGITALINPUT); 75 | for(i=0;i<8;i++) { 76 | if( lib_digitalio_getinput(pin_SDIO)) 77 | result=(result<<1)|0x01; 78 | else 79 | result=result<<1; 80 | lib_soft_3_wire_spi_setSCK(DIGITALON); 81 | lib_soft_3_wire_spi_setSCK(DIGITALOFF); 82 | } 83 | lib_digitalio_initpin( pin_SDIO, DIGITALOUTPUT); 84 | return result; 85 | } 86 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_soft_3_wire_spi.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "lib_digitalio.h" 3 | 4 | void lib_soft_3_wire_spi_init(uint8_t SDIO_portandpinnumber, uint8_t SCK_portandpinnumber, uint8_t SCS_portandpinnumber ); 5 | void lib_soft_3_wire_spi_setCS(uint8_t state); 6 | void lib_soft_3_wire_spi_write(uint8_t data); 7 | uint8_t lib_soft_3_wire_spi_read(void); 8 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_spi.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Victor Joukov 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #pragma once 19 | 20 | #include "hal.h" 21 | #include "lib_spi.h" 22 | 23 | void lib_spi_init(void) 24 | { 25 | CLK_EnableModuleClock(SPI_MODULE); 26 | 27 | CLK->CLKSEL1 |= CLK_CLKSEL1_SPI_S_HCLK; 28 | 29 | // MFP pin configuration 30 | SYS->P0_MFP |= SYS_MFP_P01_SPISS | SYS_MFP_P05_MOSI | SYS_MFP_P06_MISO | SYS_MFP_P07_SPICLK; 31 | 32 | //SYS_ResetModule(SPI_RST); 33 | SYS->IPRSTC2 |= SYS_IPRSTC2_SPI_RST_Msk; 34 | SYS->IPRSTC2 &= ~SYS_IPRSTC2_SPI_RST_Msk; 35 | 36 | // Configure as a master, clock idle low, 37 | // falling clock edge Tx, rising edge Rx and 32-bit transaction 38 | CLK->APBCLK |= CLK_APBCLK_SPI_EN_Msk; 39 | SPI->CNTRL = SPI_MASTER | SPI_MODE_0; 40 | 41 | // SPI clock rate = 1843200Hz (PCLK/12) 42 | //SPI->DIVIDER = 5; 43 | SPI->DIVIDER = 10; // PCLK/22 ~1MHz 44 | 45 | // 8-bit per transfer 46 | SPI_SET_DATA_WIDTH(SPI, 8); 47 | 48 | // Select the SS pin and configure as low-active. 49 | SPI->SSR |= SPI_SS_ACTIVE_LOW; 50 | } 51 | 52 | void lib_spi_ss_on(void) 53 | { 54 | SPI->SSR |= SPI_SS; 55 | } 56 | 57 | void lib_spi_ss_off(void) 58 | { 59 | SPI->SSR &= ~SPI_SS; 60 | } 61 | 62 | uint8_t lib_spi_xfer(uint8_t data) 63 | { 64 | SPI->TX = data; 65 | 66 | /* SPI Go */ 67 | SPI->CNTRL |= SPI_CNTRL_GO_BUSY_Msk; 68 | 69 | /* Wait SPI is free */ 70 | while(SPI->CNTRL & SPI_CNTRL_GO_BUSY_Msk); 71 | 72 | /* Read Data */ 73 | return SPI->RX; 74 | } 75 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_spi.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Victor Joukov 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | void lib_spi_init(void); 19 | void lib_spi_ss_on(void); 20 | void lib_spi_ss_off(void); 21 | uint8_t lib_spi_xfer(uint8_t data); 22 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_timers.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "hal.h" 19 | #include "lib_timers.h" 20 | 21 | // This code creates a timer to count microseconds and includes support for using this timer to run multiple 22 | // software timers. The timer can run on either TIMER0 or TIMER1. When run on TIMER1, a 16 bit timer is used 23 | // which gives us microsecond resolution and less interrupt overhead. When run on TIMER0, we get 4 microsecond 24 | // resolution and more overhead. Unsigned longs are used to store microseconds, so the longest intervals that 25 | // can be measured without extra code is about 70 minutes. 26 | 27 | // Usage: 28 | 29 | // half second delay done multiple ways 30 | 31 | // inittimers(); 32 | // unsigned long mytimer=lib_timers_starttimer(); 33 | // while (lib_timers_gettimermicroseconds(mytimer)<500000L) {} 34 | 35 | // inittimers(); 36 | // lib_timers_delaymilliseconds(500) {} 37 | 38 | // current uptime for 1kHz systick timer. will rollover after 49 days. hopefully we won't care. 39 | static volatile uint32_t sysTickUptime = 0; 40 | static uint32_t sysTickLimit; 41 | 42 | // SysTick 43 | void SysTick_Handler(void) 44 | { 45 | sysTickUptime++; 46 | } 47 | 48 | // needs to be called once in the program before timers can be used 49 | void lib_timers_init(void) 50 | { 51 | // SysTick 52 | sysTickLimit = SystemCoreClock / 1000; 53 | SysTick_Config(sysTickLimit); 54 | } 55 | 56 | uint32_t lib_timers_getcurrentmicroseconds(void) 57 | { 58 | // returns microseconds since startup. This mainly used internally because it wraps around. 59 | register uint32_t ms, cycle_cnt; 60 | do { 61 | ms = sysTickUptime; 62 | cycle_cnt = SysTick->VAL; 63 | } while (ms != sysTickUptime); 64 | // goebish timer patch, modified by victzh for performance and clarity 65 | return (ms * 1000) + (sysTickLimit - cycle_cnt) / CyclesPerUs; 66 | } 67 | 68 | unsigned long lib_timers_gettimermicroseconds(unsigned long starttime) 69 | { 70 | // returns microseconds since this timer was started 71 | unsigned long currenttime = lib_timers_getcurrentmicroseconds(); 72 | if (starttime > currenttime) // we have wrapped around 73 | { 74 | return (0xFFFFFFFF - starttime + currenttime); 75 | } else { 76 | return (currenttime - starttime); 77 | } 78 | } 79 | 80 | unsigned long lib_timers_gettimermicrosecondsandreset(unsigned long *starttime) 81 | { 82 | // returns microseconds since this timer was started and then reset the start time 83 | // this allows us to keep checking the time without losing any time 84 | unsigned long currenttime = lib_timers_getcurrentmicroseconds(); 85 | unsigned long returnvalue; 86 | 87 | if (*starttime > currenttime) // we have wrapped around 88 | { 89 | returnvalue = (0xFFFFFFFF - *starttime + currenttime); 90 | } else { 91 | returnvalue = (currenttime - *starttime); 92 | } 93 | *starttime = currenttime; 94 | return (returnvalue); 95 | } 96 | 97 | unsigned long lib_timers_starttimer() 98 | { // start a timer 99 | return (lib_timers_getcurrentmicroseconds()); 100 | } 101 | 102 | void lib_timers_delaymilliseconds(unsigned long delaymilliseconds) 103 | { 104 | unsigned long timercounts = lib_timers_starttimer(); 105 | while (lib_timers_gettimermicroseconds(timercounts) < delaymilliseconds * 1000L) { 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /lib-Mini51/hal/lib_timers.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #pragma once 19 | 20 | void lib_timers_init(void); 21 | unsigned long lib_timers_starttimer(void); 22 | unsigned long lib_timers_gettimermicroseconds(unsigned long starttime); 23 | unsigned long lib_timers_gettimermicrosecondsandreset(unsigned long *starttime); 24 | void lib_timers_delaymilliseconds(unsigned long delaymilliseconds); 25 | -------------------------------------------------------------------------------- /lib-Mini51/linker/link.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT ("elf32-littlearm", "elf32-bigarm", "elf32-littlearm") 2 | 3 | /* Section Definitions */ 4 | SECTIONS 5 | { 6 | .text : 7 | { 8 | KEEP(*(.isr_vector .isr_vector.*)) 9 | *(.text .text.* .gnu.linkonce.t.*) 10 | *(.glue_7t) *(.glue_7) 11 | *(.rodata .rodata* .gnu.linkonce.r.*) 12 | } > rom 13 | 14 | .ARM.extab : 15 | { 16 | *(.ARM.extab* .gnu.linkonce.armextab.*) 17 | } > rom 18 | 19 | .ARM.exidx : 20 | { 21 | *(.ARM.exidx* .gnu.linkonce.armexidx.*) 22 | } > rom 23 | 24 | . = ALIGN(4); 25 | _etext = .; 26 | _sidata = .; 27 | 28 | .data : AT (_etext) 29 | { 30 | _sdata = .; 31 | *(.data .data.*) 32 | . = ALIGN(4); 33 | _edata = . ; 34 | } > ram 35 | 36 | /* .bss section which is used for uninitialized data */ 37 | .bss (NOLOAD) : 38 | { 39 | _sbss = . ; 40 | *(.bss .bss.*) 41 | *(COMMON) 42 | . = ALIGN(4); 43 | _ebss = . ; 44 | } > ram 45 | 46 | /* stack section */ 47 | .co_stack (NOLOAD): 48 | { 49 | . = ALIGN(8); 50 | *(.co_stack .co_stack.*) 51 | } > ram 52 | 53 | . = ALIGN(4); 54 | _end = . ; 55 | } -------------------------------------------------------------------------------- /lib-Mini51/linker/memory.ld: -------------------------------------------------------------------------------- 1 | 2 | MEMORY 3 | { 4 | rom (rx) : ORIGIN = 0x00000000, LENGTH = 0x00008000 5 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00002000 6 | } 7 | 8 | _eram = 0x20000000 + 0x00002000; -------------------------------------------------------------------------------- /lib/CMSIS/CM3/DeviceSupport/ST/STM32F10x/stm32f10x.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victzh/bradwii/51436da66ef105e0824a854fe07ca0ad2dbc368f/lib/CMSIS/CM3/DeviceSupport/ST/STM32F10x/stm32f10x.h -------------------------------------------------------------------------------- /lib/CMSIS/CM3/DeviceSupport/ST/STM32F10x/stm32f10x_conf.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file Project/STM32F10x_StdPeriph_Template/stm32f10x_conf.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 08-April-2011 7 | * @brief Library configuration file. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __STM32F10x_CONF_H 24 | #define __STM32F10x_CONF_H 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | /* Uncomment/Comment the line below to enable/disable peripheral header file inclusion */ 28 | #include "stm32f10x_adc.h" 29 | #include "stm32f10x_bkp.h" 30 | #include "stm32f10x_can.h" 31 | #include "stm32f10x_cec.h" 32 | #include "stm32f10x_crc.h" 33 | #include "stm32f10x_dac.h" 34 | #include "stm32f10x_dbgmcu.h" 35 | #include "stm32f10x_dma.h" 36 | #include "stm32f10x_exti.h" 37 | #include "stm32f10x_flash.h" 38 | #include "stm32f10x_fsmc.h" 39 | #include "stm32f10x_gpio.h" 40 | #include "stm32f10x_i2c.h" 41 | #include "stm32f10x_iwdg.h" 42 | #include "stm32f10x_pwr.h" 43 | #include "stm32f10x_rcc.h" 44 | #include "stm32f10x_rtc.h" 45 | #include "stm32f10x_sdio.h" 46 | #include "stm32f10x_spi.h" 47 | #include "stm32f10x_tim.h" 48 | #include "stm32f10x_usart.h" 49 | #include "stm32f10x_wwdg.h" 50 | #include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */ 51 | 52 | extern void assert_param(int val); 53 | 54 | /* Exported types ------------------------------------------------------------*/ 55 | /* Exported constants --------------------------------------------------------*/ 56 | /* Uncomment the line below to expanse the "assert_param" macro in the 57 | Standard Peripheral Library drivers code */ 58 | // #define USE_FULL_ASSERT 1 59 | 60 | /* Exported macro ------------------------------------------------------------*/ 61 | #ifdef USE_FULL_ASSERT 62 | 63 | /** 64 | * @brief The assert_param macro is used for function's parameters check. 65 | * @param expr: If expr is false, it calls assert_failed function which reports 66 | * the name of the source file and the source line number of the call 67 | * that failed. If expr is true, it returns no value. 68 | * @retval None 69 | */ 70 | #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) 71 | /* Exported functions ------------------------------------------------------- */ 72 | void assert_failed(uint8_t* file, uint32_t line); 73 | #else 74 | #define assert_param(expr) ((void)0) 75 | #endif /* USE_FULL_ASSERT */ 76 | 77 | #endif /* __STM32F10x_CONF_H */ 78 | 79 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 80 | -------------------------------------------------------------------------------- /lib/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file system_stm32f10x.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Header File. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /** @addtogroup CMSIS 23 | * @{ 24 | */ 25 | 26 | /** @addtogroup stm32f10x_system 27 | * @{ 28 | */ 29 | 30 | /** 31 | * @brief Define to prevent recursive inclusion 32 | */ 33 | #ifndef __SYSTEM_STM32F10X_H 34 | #define __SYSTEM_STM32F10X_H 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | /** @addtogroup STM32F10x_System_Includes 41 | * @{ 42 | */ 43 | 44 | /** 45 | * @} 46 | */ 47 | 48 | 49 | /** @addtogroup STM32F10x_System_Exported_types 50 | * @{ 51 | */ 52 | 53 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ 54 | 55 | /** 56 | * @} 57 | */ 58 | 59 | /** @addtogroup STM32F10x_System_Exported_Constants 60 | * @{ 61 | */ 62 | 63 | /** 64 | * @} 65 | */ 66 | 67 | /** @addtogroup STM32F10x_System_Exported_Macros 68 | * @{ 69 | */ 70 | 71 | /** 72 | * @} 73 | */ 74 | 75 | /** @addtogroup STM32F10x_System_Exported_Functions 76 | * @{ 77 | */ 78 | 79 | extern void SystemInit(void); 80 | extern void SystemCoreClockUpdate(void); 81 | /** 82 | * @} 83 | */ 84 | 85 | #ifdef __cplusplus 86 | } 87 | #endif 88 | 89 | #endif /*__SYSTEM_STM32F10X_H */ 90 | 91 | /** 92 | * @} 93 | */ 94 | 95 | /** 96 | * @} 97 | */ 98 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 99 | -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/inc/stm32f10x_crc.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_crc.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file contains all the functions prototypes for the CRC firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

20 | ****************************************************************************** 21 | */ 22 | 23 | /* Define to prevent recursive inclusion -------------------------------------*/ 24 | #ifndef __STM32F10x_CRC_H 25 | #define __STM32F10x_CRC_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /* Includes ------------------------------------------------------------------*/ 32 | #include "stm32f10x.h" 33 | 34 | /** @addtogroup STM32F10x_StdPeriph_Driver 35 | * @{ 36 | */ 37 | 38 | /** @addtogroup CRC 39 | * @{ 40 | */ 41 | 42 | /** @defgroup CRC_Exported_Types 43 | * @{ 44 | */ 45 | 46 | /** 47 | * @} 48 | */ 49 | 50 | /** @defgroup CRC_Exported_Constants 51 | * @{ 52 | */ 53 | 54 | /** 55 | * @} 56 | */ 57 | 58 | /** @defgroup CRC_Exported_Macros 59 | * @{ 60 | */ 61 | 62 | /** 63 | * @} 64 | */ 65 | 66 | /** @defgroup CRC_Exported_Functions 67 | * @{ 68 | */ 69 | 70 | void CRC_ResetDR(void); 71 | uint32_t CRC_CalcCRC(uint32_t Data); 72 | uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t BufferLength); 73 | uint32_t CRC_GetCRC(void); 74 | void CRC_SetIDRegister(uint8_t IDValue); 75 | uint8_t CRC_GetIDRegister(void); 76 | 77 | #ifdef __cplusplus 78 | } 79 | #endif 80 | 81 | #endif /* __STM32F10x_CRC_H */ 82 | /** 83 | * @} 84 | */ 85 | 86 | /** 87 | * @} 88 | */ 89 | 90 | /** 91 | * @} 92 | */ 93 | 94 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 95 | -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/inc/stm32f10x_dbgmcu.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_dbgmcu.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file contains all the functions prototypes for the DBGMCU 8 | * firmware library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

20 | ****************************************************************************** 21 | */ 22 | 23 | /* Define to prevent recursive inclusion -------------------------------------*/ 24 | #ifndef __STM32F10x_DBGMCU_H 25 | #define __STM32F10x_DBGMCU_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /* Includes ------------------------------------------------------------------*/ 32 | #include "stm32f10x.h" 33 | 34 | /** @addtogroup STM32F10x_StdPeriph_Driver 35 | * @{ 36 | */ 37 | 38 | /** @addtogroup DBGMCU 39 | * @{ 40 | */ 41 | 42 | /** @defgroup DBGMCU_Exported_Types 43 | * @{ 44 | */ 45 | 46 | /** 47 | * @} 48 | */ 49 | 50 | /** @defgroup DBGMCU_Exported_Constants 51 | * @{ 52 | */ 53 | 54 | #define DBGMCU_SLEEP ((uint32_t)0x00000001) 55 | #define DBGMCU_STOP ((uint32_t)0x00000002) 56 | #define DBGMCU_STANDBY ((uint32_t)0x00000004) 57 | #define DBGMCU_IWDG_STOP ((uint32_t)0x00000100) 58 | #define DBGMCU_WWDG_STOP ((uint32_t)0x00000200) 59 | #define DBGMCU_TIM1_STOP ((uint32_t)0x00000400) 60 | #define DBGMCU_TIM2_STOP ((uint32_t)0x00000800) 61 | #define DBGMCU_TIM3_STOP ((uint32_t)0x00001000) 62 | #define DBGMCU_TIM4_STOP ((uint32_t)0x00002000) 63 | #define DBGMCU_CAN1_STOP ((uint32_t)0x00004000) 64 | #define DBGMCU_I2C1_SMBUS_TIMEOUT ((uint32_t)0x00008000) 65 | #define DBGMCU_I2C2_SMBUS_TIMEOUT ((uint32_t)0x00010000) 66 | #define DBGMCU_TIM8_STOP ((uint32_t)0x00020000) 67 | #define DBGMCU_TIM5_STOP ((uint32_t)0x00040000) 68 | #define DBGMCU_TIM6_STOP ((uint32_t)0x00080000) 69 | #define DBGMCU_TIM7_STOP ((uint32_t)0x00100000) 70 | #define DBGMCU_CAN2_STOP ((uint32_t)0x00200000) 71 | #define DBGMCU_TIM15_STOP ((uint32_t)0x00400000) 72 | #define DBGMCU_TIM16_STOP ((uint32_t)0x00800000) 73 | #define DBGMCU_TIM17_STOP ((uint32_t)0x01000000) 74 | #define DBGMCU_TIM12_STOP ((uint32_t)0x02000000) 75 | #define DBGMCU_TIM13_STOP ((uint32_t)0x04000000) 76 | #define DBGMCU_TIM14_STOP ((uint32_t)0x08000000) 77 | #define DBGMCU_TIM9_STOP ((uint32_t)0x10000000) 78 | #define DBGMCU_TIM10_STOP ((uint32_t)0x20000000) 79 | #define DBGMCU_TIM11_STOP ((uint32_t)0x40000000) 80 | 81 | #define IS_DBGMCU_PERIPH(PERIPH) ((((PERIPH) & 0x800000F8) == 0x00) && ((PERIPH) != 0x00)) 82 | /** 83 | * @} 84 | */ 85 | 86 | /** @defgroup DBGMCU_Exported_Macros 87 | * @{ 88 | */ 89 | 90 | /** 91 | * @} 92 | */ 93 | 94 | /** @defgroup DBGMCU_Exported_Functions 95 | * @{ 96 | */ 97 | 98 | uint32_t DBGMCU_GetREVID(void); 99 | uint32_t DBGMCU_GetDEVID(void); 100 | void DBGMCU_Config(uint32_t DBGMCU_Periph, FunctionalState NewState); 101 | 102 | #ifdef __cplusplus 103 | } 104 | #endif 105 | 106 | #endif /* __STM32F10x_DBGMCU_H */ 107 | /** 108 | * @} 109 | */ 110 | 111 | /** 112 | * @} 113 | */ 114 | 115 | /** 116 | * @} 117 | */ 118 | 119 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 120 | -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/inc/stm32f10x_iwdg.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_iwdg.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file contains all the functions prototypes for the IWDG 8 | * firmware library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

20 | ****************************************************************************** 21 | */ 22 | 23 | /* Define to prevent recursive inclusion -------------------------------------*/ 24 | #ifndef __STM32F10x_IWDG_H 25 | #define __STM32F10x_IWDG_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /* Includes ------------------------------------------------------------------*/ 32 | #include "stm32f10x.h" 33 | 34 | /** @addtogroup STM32F10x_StdPeriph_Driver 35 | * @{ 36 | */ 37 | 38 | /** @addtogroup IWDG 39 | * @{ 40 | */ 41 | 42 | /** @defgroup IWDG_Exported_Types 43 | * @{ 44 | */ 45 | 46 | /** 47 | * @} 48 | */ 49 | 50 | /** @defgroup IWDG_Exported_Constants 51 | * @{ 52 | */ 53 | 54 | /** @defgroup IWDG_WriteAccess 55 | * @{ 56 | */ 57 | 58 | #define IWDG_WriteAccess_Enable ((uint16_t)0x5555) 59 | #define IWDG_WriteAccess_Disable ((uint16_t)0x0000) 60 | #define IS_IWDG_WRITE_ACCESS(ACCESS) (((ACCESS) == IWDG_WriteAccess_Enable) || \ 61 | ((ACCESS) == IWDG_WriteAccess_Disable)) 62 | /** 63 | * @} 64 | */ 65 | 66 | /** @defgroup IWDG_prescaler 67 | * @{ 68 | */ 69 | 70 | #define IWDG_Prescaler_4 ((uint8_t)0x00) 71 | #define IWDG_Prescaler_8 ((uint8_t)0x01) 72 | #define IWDG_Prescaler_16 ((uint8_t)0x02) 73 | #define IWDG_Prescaler_32 ((uint8_t)0x03) 74 | #define IWDG_Prescaler_64 ((uint8_t)0x04) 75 | #define IWDG_Prescaler_128 ((uint8_t)0x05) 76 | #define IWDG_Prescaler_256 ((uint8_t)0x06) 77 | #define IS_IWDG_PRESCALER(PRESCALER) (((PRESCALER) == IWDG_Prescaler_4) || \ 78 | ((PRESCALER) == IWDG_Prescaler_8) || \ 79 | ((PRESCALER) == IWDG_Prescaler_16) || \ 80 | ((PRESCALER) == IWDG_Prescaler_32) || \ 81 | ((PRESCALER) == IWDG_Prescaler_64) || \ 82 | ((PRESCALER) == IWDG_Prescaler_128)|| \ 83 | ((PRESCALER) == IWDG_Prescaler_256)) 84 | /** 85 | * @} 86 | */ 87 | 88 | /** @defgroup IWDG_Flag 89 | * @{ 90 | */ 91 | 92 | #define IWDG_FLAG_PVU ((uint16_t)0x0001) 93 | #define IWDG_FLAG_RVU ((uint16_t)0x0002) 94 | #define IS_IWDG_FLAG(FLAG) (((FLAG) == IWDG_FLAG_PVU) || ((FLAG) == IWDG_FLAG_RVU)) 95 | #define IS_IWDG_RELOAD(RELOAD) ((RELOAD) <= 0xFFF) 96 | /** 97 | * @} 98 | */ 99 | 100 | /** 101 | * @} 102 | */ 103 | 104 | /** @defgroup IWDG_Exported_Macros 105 | * @{ 106 | */ 107 | 108 | /** 109 | * @} 110 | */ 111 | 112 | /** @defgroup IWDG_Exported_Functions 113 | * @{ 114 | */ 115 | 116 | void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess); 117 | void IWDG_SetPrescaler(uint8_t IWDG_Prescaler); 118 | void IWDG_SetReload(uint16_t Reload); 119 | void IWDG_ReloadCounter(void); 120 | void IWDG_Enable(void); 121 | FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG); 122 | 123 | #ifdef __cplusplus 124 | } 125 | #endif 126 | 127 | #endif /* __STM32F10x_IWDG_H */ 128 | /** 129 | * @} 130 | */ 131 | 132 | /** 133 | * @} 134 | */ 135 | 136 | /** 137 | * @} 138 | */ 139 | 140 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 141 | -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/inc/stm32f10x_pwr.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_pwr.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file contains all the functions prototypes for the PWR firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

20 | ****************************************************************************** 21 | */ 22 | 23 | /* Define to prevent recursive inclusion -------------------------------------*/ 24 | #ifndef __STM32F10x_PWR_H 25 | #define __STM32F10x_PWR_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /* Includes ------------------------------------------------------------------*/ 32 | #include "stm32f10x.h" 33 | 34 | /** @addtogroup STM32F10x_StdPeriph_Driver 35 | * @{ 36 | */ 37 | 38 | /** @addtogroup PWR 39 | * @{ 40 | */ 41 | 42 | /** @defgroup PWR_Exported_Types 43 | * @{ 44 | */ 45 | 46 | /** 47 | * @} 48 | */ 49 | 50 | /** @defgroup PWR_Exported_Constants 51 | * @{ 52 | */ 53 | 54 | /** @defgroup PVD_detection_level 55 | * @{ 56 | */ 57 | 58 | #define PWR_PVDLevel_2V2 ((uint32_t)0x00000000) 59 | #define PWR_PVDLevel_2V3 ((uint32_t)0x00000020) 60 | #define PWR_PVDLevel_2V4 ((uint32_t)0x00000040) 61 | #define PWR_PVDLevel_2V5 ((uint32_t)0x00000060) 62 | #define PWR_PVDLevel_2V6 ((uint32_t)0x00000080) 63 | #define PWR_PVDLevel_2V7 ((uint32_t)0x000000A0) 64 | #define PWR_PVDLevel_2V8 ((uint32_t)0x000000C0) 65 | #define PWR_PVDLevel_2V9 ((uint32_t)0x000000E0) 66 | #define IS_PWR_PVD_LEVEL(LEVEL) (((LEVEL) == PWR_PVDLevel_2V2) || ((LEVEL) == PWR_PVDLevel_2V3)|| \ 67 | ((LEVEL) == PWR_PVDLevel_2V4) || ((LEVEL) == PWR_PVDLevel_2V5)|| \ 68 | ((LEVEL) == PWR_PVDLevel_2V6) || ((LEVEL) == PWR_PVDLevel_2V7)|| \ 69 | ((LEVEL) == PWR_PVDLevel_2V8) || ((LEVEL) == PWR_PVDLevel_2V9)) 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @defgroup Regulator_state_is_STOP_mode 75 | * @{ 76 | */ 77 | 78 | #define PWR_Regulator_ON ((uint32_t)0x00000000) 79 | #define PWR_Regulator_LowPower ((uint32_t)0x00000001) 80 | #define IS_PWR_REGULATOR(REGULATOR) (((REGULATOR) == PWR_Regulator_ON) || \ 81 | ((REGULATOR) == PWR_Regulator_LowPower)) 82 | /** 83 | * @} 84 | */ 85 | 86 | /** @defgroup STOP_mode_entry 87 | * @{ 88 | */ 89 | 90 | #define PWR_STOPEntry_WFI ((uint8_t)0x01) 91 | #define PWR_STOPEntry_WFE ((uint8_t)0x02) 92 | #define IS_PWR_STOP_ENTRY(ENTRY) (((ENTRY) == PWR_STOPEntry_WFI) || ((ENTRY) == PWR_STOPEntry_WFE)) 93 | 94 | /** 95 | * @} 96 | */ 97 | 98 | /** @defgroup PWR_Flag 99 | * @{ 100 | */ 101 | 102 | #define PWR_FLAG_WU ((uint32_t)0x00000001) 103 | #define PWR_FLAG_SB ((uint32_t)0x00000002) 104 | #define PWR_FLAG_PVDO ((uint32_t)0x00000004) 105 | #define IS_PWR_GET_FLAG(FLAG) (((FLAG) == PWR_FLAG_WU) || ((FLAG) == PWR_FLAG_SB) || \ 106 | ((FLAG) == PWR_FLAG_PVDO)) 107 | 108 | #define IS_PWR_CLEAR_FLAG(FLAG) (((FLAG) == PWR_FLAG_WU) || ((FLAG) == PWR_FLAG_SB)) 109 | /** 110 | * @} 111 | */ 112 | 113 | /** 114 | * @} 115 | */ 116 | 117 | /** @defgroup PWR_Exported_Macros 118 | * @{ 119 | */ 120 | 121 | /** 122 | * @} 123 | */ 124 | 125 | /** @defgroup PWR_Exported_Functions 126 | * @{ 127 | */ 128 | 129 | void PWR_DeInit(void); 130 | void PWR_BackupAccessCmd(FunctionalState NewState); 131 | void PWR_PVDCmd(FunctionalState NewState); 132 | void PWR_PVDLevelConfig(uint32_t PWR_PVDLevel); 133 | void PWR_WakeUpPinCmd(FunctionalState NewState); 134 | void PWR_EnterSTOPMode(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry); 135 | void PWR_EnterSTANDBYMode(void); 136 | FlagStatus PWR_GetFlagStatus(uint32_t PWR_FLAG); 137 | void PWR_ClearFlag(uint32_t PWR_FLAG); 138 | 139 | #ifdef __cplusplus 140 | } 141 | #endif 142 | 143 | #endif /* __STM32F10x_PWR_H */ 144 | /** 145 | * @} 146 | */ 147 | 148 | /** 149 | * @} 150 | */ 151 | 152 | /** 153 | * @} 154 | */ 155 | 156 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 157 | -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/inc/stm32f10x_rtc.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_rtc.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file contains all the functions prototypes for the RTC firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

20 | ****************************************************************************** 21 | */ 22 | 23 | /* Define to prevent recursive inclusion -------------------------------------*/ 24 | #ifndef __STM32F10x_RTC_H 25 | #define __STM32F10x_RTC_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /* Includes ------------------------------------------------------------------*/ 32 | #include "stm32f10x.h" 33 | 34 | /** @addtogroup STM32F10x_StdPeriph_Driver 35 | * @{ 36 | */ 37 | 38 | /** @addtogroup RTC 39 | * @{ 40 | */ 41 | 42 | /** @defgroup RTC_Exported_Types 43 | * @{ 44 | */ 45 | 46 | /** 47 | * @} 48 | */ 49 | 50 | /** @defgroup RTC_Exported_Constants 51 | * @{ 52 | */ 53 | 54 | /** @defgroup RTC_interrupts_define 55 | * @{ 56 | */ 57 | 58 | #define RTC_IT_OW ((uint16_t)0x0004) /*!< Overflow interrupt */ 59 | #define RTC_IT_ALR ((uint16_t)0x0002) /*!< Alarm interrupt */ 60 | #define RTC_IT_SEC ((uint16_t)0x0001) /*!< Second interrupt */ 61 | #define IS_RTC_IT(IT) ((((IT) & (uint16_t)0xFFF8) == 0x00) && ((IT) != 0x00)) 62 | #define IS_RTC_GET_IT(IT) (((IT) == RTC_IT_OW) || ((IT) == RTC_IT_ALR) || \ 63 | ((IT) == RTC_IT_SEC)) 64 | /** 65 | * @} 66 | */ 67 | 68 | /** @defgroup RTC_interrupts_flags 69 | * @{ 70 | */ 71 | 72 | #define RTC_FLAG_RTOFF ((uint16_t)0x0020) /*!< RTC Operation OFF flag */ 73 | #define RTC_FLAG_RSF ((uint16_t)0x0008) /*!< Registers Synchronized flag */ 74 | #define RTC_FLAG_OW ((uint16_t)0x0004) /*!< Overflow flag */ 75 | #define RTC_FLAG_ALR ((uint16_t)0x0002) /*!< Alarm flag */ 76 | #define RTC_FLAG_SEC ((uint16_t)0x0001) /*!< Second flag */ 77 | #define IS_RTC_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xFFF0) == 0x00) && ((FLAG) != 0x00)) 78 | #define IS_RTC_GET_FLAG(FLAG) (((FLAG) == RTC_FLAG_RTOFF) || ((FLAG) == RTC_FLAG_RSF) || \ 79 | ((FLAG) == RTC_FLAG_OW) || ((FLAG) == RTC_FLAG_ALR) || \ 80 | ((FLAG) == RTC_FLAG_SEC)) 81 | #define IS_RTC_PRESCALER(PRESCALER) ((PRESCALER) <= 0xFFFFF) 82 | 83 | /** 84 | * @} 85 | */ 86 | 87 | /** 88 | * @} 89 | */ 90 | 91 | /** @defgroup RTC_Exported_Macros 92 | * @{ 93 | */ 94 | 95 | /** 96 | * @} 97 | */ 98 | 99 | /** @defgroup RTC_Exported_Functions 100 | * @{ 101 | */ 102 | 103 | void RTC_ITConfig(uint16_t RTC_IT, FunctionalState NewState); 104 | void RTC_EnterConfigMode(void); 105 | void RTC_ExitConfigMode(void); 106 | uint32_t RTC_GetCounter(void); 107 | void RTC_SetCounter(uint32_t CounterValue); 108 | void RTC_SetPrescaler(uint32_t PrescalerValue); 109 | void RTC_SetAlarm(uint32_t AlarmValue); 110 | uint32_t RTC_GetDivider(void); 111 | void RTC_WaitForLastTask(void); 112 | void RTC_WaitForSynchro(void); 113 | FlagStatus RTC_GetFlagStatus(uint16_t RTC_FLAG); 114 | void RTC_ClearFlag(uint16_t RTC_FLAG); 115 | ITStatus RTC_GetITStatus(uint16_t RTC_IT); 116 | void RTC_ClearITPendingBit(uint16_t RTC_IT); 117 | 118 | #ifdef __cplusplus 119 | } 120 | #endif 121 | 122 | #endif /* __STM32F10x_RTC_H */ 123 | /** 124 | * @} 125 | */ 126 | 127 | /** 128 | * @} 129 | */ 130 | 131 | /** 132 | * @} 133 | */ 134 | 135 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 136 | -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/inc/stm32f10x_wwdg.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_wwdg.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file contains all the functions prototypes for the WWDG firmware 8 | * library. 9 | ****************************************************************************** 10 | * @attention 11 | * 12 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 | * 19 | *

© COPYRIGHT 2011 STMicroelectronics

20 | ****************************************************************************** 21 | */ 22 | 23 | /* Define to prevent recursive inclusion -------------------------------------*/ 24 | #ifndef __STM32F10x_WWDG_H 25 | #define __STM32F10x_WWDG_H 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | /* Includes ------------------------------------------------------------------*/ 32 | #include "stm32f10x.h" 33 | 34 | /** @addtogroup STM32F10x_StdPeriph_Driver 35 | * @{ 36 | */ 37 | 38 | /** @addtogroup WWDG 39 | * @{ 40 | */ 41 | 42 | /** @defgroup WWDG_Exported_Types 43 | * @{ 44 | */ 45 | 46 | /** 47 | * @} 48 | */ 49 | 50 | /** @defgroup WWDG_Exported_Constants 51 | * @{ 52 | */ 53 | 54 | /** @defgroup WWDG_Prescaler 55 | * @{ 56 | */ 57 | 58 | #define WWDG_Prescaler_1 ((uint32_t)0x00000000) 59 | #define WWDG_Prescaler_2 ((uint32_t)0x00000080) 60 | #define WWDG_Prescaler_4 ((uint32_t)0x00000100) 61 | #define WWDG_Prescaler_8 ((uint32_t)0x00000180) 62 | #define IS_WWDG_PRESCALER(PRESCALER) (((PRESCALER) == WWDG_Prescaler_1) || \ 63 | ((PRESCALER) == WWDG_Prescaler_2) || \ 64 | ((PRESCALER) == WWDG_Prescaler_4) || \ 65 | ((PRESCALER) == WWDG_Prescaler_8)) 66 | #define IS_WWDG_WINDOW_VALUE(VALUE) ((VALUE) <= 0x7F) 67 | #define IS_WWDG_COUNTER(COUNTER) (((COUNTER) >= 0x40) && ((COUNTER) <= 0x7F)) 68 | 69 | /** 70 | * @} 71 | */ 72 | 73 | /** 74 | * @} 75 | */ 76 | 77 | /** @defgroup WWDG_Exported_Macros 78 | * @{ 79 | */ 80 | /** 81 | * @} 82 | */ 83 | 84 | /** @defgroup WWDG_Exported_Functions 85 | * @{ 86 | */ 87 | 88 | void WWDG_DeInit(void); 89 | void WWDG_SetPrescaler(uint32_t WWDG_Prescaler); 90 | void WWDG_SetWindowValue(uint8_t WindowValue); 91 | void WWDG_EnableIT(void); 92 | void WWDG_SetCounter(uint8_t Counter); 93 | void WWDG_Enable(uint8_t Counter); 94 | FlagStatus WWDG_GetFlagStatus(void); 95 | void WWDG_ClearFlag(void); 96 | 97 | #ifdef __cplusplus 98 | } 99 | #endif 100 | 101 | #endif /* __STM32F10x_WWDG_H */ 102 | 103 | /** 104 | * @} 105 | */ 106 | 107 | /** 108 | * @} 109 | */ 110 | 111 | /** 112 | * @} 113 | */ 114 | 115 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 116 | -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/src/stm32f10x_crc.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_crc.c 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file provides all the CRC firmware functions. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Includes ------------------------------------------------------------------*/ 23 | #include "stm32f10x_crc.h" 24 | 25 | /** @addtogroup STM32F10x_StdPeriph_Driver 26 | * @{ 27 | */ 28 | 29 | /** @defgroup CRC 30 | * @brief CRC driver modules 31 | * @{ 32 | */ 33 | 34 | /** @defgroup CRC_Private_TypesDefinitions 35 | * @{ 36 | */ 37 | 38 | /** 39 | * @} 40 | */ 41 | 42 | /** @defgroup CRC_Private_Defines 43 | * @{ 44 | */ 45 | 46 | /** 47 | * @} 48 | */ 49 | 50 | /** @defgroup CRC_Private_Macros 51 | * @{ 52 | */ 53 | 54 | /** 55 | * @} 56 | */ 57 | 58 | /** @defgroup CRC_Private_Variables 59 | * @{ 60 | */ 61 | 62 | /** 63 | * @} 64 | */ 65 | 66 | /** @defgroup CRC_Private_FunctionPrototypes 67 | * @{ 68 | */ 69 | 70 | /** 71 | * @} 72 | */ 73 | 74 | /** @defgroup CRC_Private_Functions 75 | * @{ 76 | */ 77 | 78 | /** 79 | * @brief Resets the CRC Data register (DR). 80 | * @param None 81 | * @retval None 82 | */ 83 | void CRC_ResetDR(void) 84 | { 85 | /* Reset CRC generator */ 86 | CRC->CR = CRC_CR_RESET; 87 | } 88 | 89 | /** 90 | * @brief Computes the 32-bit CRC of a given data word(32-bit). 91 | * @param Data: data word(32-bit) to compute its CRC 92 | * @retval 32-bit CRC 93 | */ 94 | uint32_t CRC_CalcCRC(uint32_t Data) 95 | { 96 | CRC->DR = Data; 97 | 98 | return (CRC->DR); 99 | } 100 | 101 | /** 102 | * @brief Computes the 32-bit CRC of a given buffer of data word(32-bit). 103 | * @param pBuffer: pointer to the buffer containing the data to be computed 104 | * @param BufferLength: length of the buffer to be computed 105 | * @retval 32-bit CRC 106 | */ 107 | uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t BufferLength) 108 | { 109 | uint32_t index = 0; 110 | 111 | for(index = 0; index < BufferLength; index++) 112 | { 113 | CRC->DR = pBuffer[index]; 114 | } 115 | return (CRC->DR); 116 | } 117 | 118 | /** 119 | * @brief Returns the current CRC value. 120 | * @param None 121 | * @retval 32-bit CRC 122 | */ 123 | uint32_t CRC_GetCRC(void) 124 | { 125 | return (CRC->DR); 126 | } 127 | 128 | /** 129 | * @brief Stores a 8-bit data in the Independent Data(ID) register. 130 | * @param IDValue: 8-bit value to be stored in the ID register 131 | * @retval None 132 | */ 133 | void CRC_SetIDRegister(uint8_t IDValue) 134 | { 135 | CRC->IDR = IDValue; 136 | } 137 | 138 | /** 139 | * @brief Returns the 8-bit data stored in the Independent Data(ID) register 140 | * @param None 141 | * @retval 8-bit value of the ID register 142 | */ 143 | uint8_t CRC_GetIDRegister(void) 144 | { 145 | return (CRC->IDR); 146 | } 147 | 148 | /** 149 | * @} 150 | */ 151 | 152 | /** 153 | * @} 154 | */ 155 | 156 | /** 157 | * @} 158 | */ 159 | 160 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 161 | -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/src/stm32f10x_dbgmcu.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_dbgmcu.c 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file provides all the DBGMCU firmware functions. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Includes ------------------------------------------------------------------*/ 23 | #include "stm32f10x_dbgmcu.h" 24 | 25 | /** @addtogroup STM32F10x_StdPeriph_Driver 26 | * @{ 27 | */ 28 | 29 | /** @defgroup DBGMCU 30 | * @brief DBGMCU driver modules 31 | * @{ 32 | */ 33 | 34 | /** @defgroup DBGMCU_Private_TypesDefinitions 35 | * @{ 36 | */ 37 | 38 | /** 39 | * @} 40 | */ 41 | 42 | /** @defgroup DBGMCU_Private_Defines 43 | * @{ 44 | */ 45 | 46 | #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFF) 47 | /** 48 | * @} 49 | */ 50 | 51 | /** @defgroup DBGMCU_Private_Macros 52 | * @{ 53 | */ 54 | 55 | /** 56 | * @} 57 | */ 58 | 59 | /** @defgroup DBGMCU_Private_Variables 60 | * @{ 61 | */ 62 | 63 | /** 64 | * @} 65 | */ 66 | 67 | /** @defgroup DBGMCU_Private_FunctionPrototypes 68 | * @{ 69 | */ 70 | 71 | /** 72 | * @} 73 | */ 74 | 75 | /** @defgroup DBGMCU_Private_Functions 76 | * @{ 77 | */ 78 | 79 | /** 80 | * @brief Returns the device revision identifier. 81 | * @param None 82 | * @retval Device revision identifier 83 | */ 84 | uint32_t DBGMCU_GetREVID(void) 85 | { 86 | return(DBGMCU->IDCODE >> 16); 87 | } 88 | 89 | /** 90 | * @brief Returns the device identifier. 91 | * @param None 92 | * @retval Device identifier 93 | */ 94 | uint32_t DBGMCU_GetDEVID(void) 95 | { 96 | return(DBGMCU->IDCODE & IDCODE_DEVID_MASK); 97 | } 98 | 99 | /** 100 | * @brief Configures the specified peripheral and low power mode behavior 101 | * when the MCU under Debug mode. 102 | * @param DBGMCU_Periph: specifies the peripheral and low power mode. 103 | * This parameter can be any combination of the following values: 104 | * @arg DBGMCU_SLEEP: Keep debugger connection during SLEEP mode 105 | * @arg DBGMCU_STOP: Keep debugger connection during STOP mode 106 | * @arg DBGMCU_STANDBY: Keep debugger connection during STANDBY mode 107 | * @arg DBGMCU_IWDG_STOP: Debug IWDG stopped when Core is halted 108 | * @arg DBGMCU_WWDG_STOP: Debug WWDG stopped when Core is halted 109 | * @arg DBGMCU_TIM1_STOP: TIM1 counter stopped when Core is halted 110 | * @arg DBGMCU_TIM2_STOP: TIM2 counter stopped when Core is halted 111 | * @arg DBGMCU_TIM3_STOP: TIM3 counter stopped when Core is halted 112 | * @arg DBGMCU_TIM4_STOP: TIM4 counter stopped when Core is halted 113 | * @arg DBGMCU_CAN1_STOP: Debug CAN2 stopped when Core is halted 114 | * @arg DBGMCU_I2C1_SMBUS_TIMEOUT: I2C1 SMBUS timeout mode stopped when Core is halted 115 | * @arg DBGMCU_I2C2_SMBUS_TIMEOUT: I2C2 SMBUS timeout mode stopped when Core is halted 116 | * @arg DBGMCU_TIM5_STOP: TIM5 counter stopped when Core is halted 117 | * @arg DBGMCU_TIM6_STOP: TIM6 counter stopped when Core is halted 118 | * @arg DBGMCU_TIM7_STOP: TIM7 counter stopped when Core is halted 119 | * @arg DBGMCU_TIM8_STOP: TIM8 counter stopped when Core is halted 120 | * @arg DBGMCU_CAN2_STOP: Debug CAN2 stopped when Core is halted 121 | * @arg DBGMCU_TIM15_STOP: TIM15 counter stopped when Core is halted 122 | * @arg DBGMCU_TIM16_STOP: TIM16 counter stopped when Core is halted 123 | * @arg DBGMCU_TIM17_STOP: TIM17 counter stopped when Core is halted 124 | * @arg DBGMCU_TIM9_STOP: TIM9 counter stopped when Core is halted 125 | * @arg DBGMCU_TIM10_STOP: TIM10 counter stopped when Core is halted 126 | * @arg DBGMCU_TIM11_STOP: TIM11 counter stopped when Core is halted 127 | * @arg DBGMCU_TIM12_STOP: TIM12 counter stopped when Core is halted 128 | * @arg DBGMCU_TIM13_STOP: TIM13 counter stopped when Core is halted 129 | * @arg DBGMCU_TIM14_STOP: TIM14 counter stopped when Core is halted 130 | * @param NewState: new state of the specified peripheral in Debug mode. 131 | * This parameter can be: ENABLE or DISABLE. 132 | * @retval None 133 | */ 134 | void DBGMCU_Config(uint32_t DBGMCU_Periph, FunctionalState NewState) 135 | { 136 | /* Check the parameters */ 137 | assert_param(IS_DBGMCU_PERIPH(DBGMCU_Periph)); 138 | assert_param(IS_FUNCTIONAL_STATE(NewState)); 139 | 140 | if (NewState != DISABLE) 141 | { 142 | DBGMCU->CR |= DBGMCU_Periph; 143 | } 144 | else 145 | { 146 | DBGMCU->CR &= ~DBGMCU_Periph; 147 | } 148 | } 149 | 150 | /** 151 | * @} 152 | */ 153 | 154 | /** 155 | * @} 156 | */ 157 | 158 | /** 159 | * @} 160 | */ 161 | 162 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 163 | -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victzh/bradwii/51436da66ef105e0824a854fe07ca0ad2dbc368f/lib/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victzh/bradwii/51436da66ef105e0824a854fe07ca0ad2dbc368f/lib/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/src/stm32f10x_iwdg.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f10x_iwdg.c 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 11-March-2011 7 | * @brief This file provides all the IWDG firmware functions. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Includes ------------------------------------------------------------------*/ 23 | #include "stm32f10x_iwdg.h" 24 | 25 | /** @addtogroup STM32F10x_StdPeriph_Driver 26 | * @{ 27 | */ 28 | 29 | /** @defgroup IWDG 30 | * @brief IWDG driver modules 31 | * @{ 32 | */ 33 | 34 | /** @defgroup IWDG_Private_TypesDefinitions 35 | * @{ 36 | */ 37 | 38 | /** 39 | * @} 40 | */ 41 | 42 | /** @defgroup IWDG_Private_Defines 43 | * @{ 44 | */ 45 | 46 | /* ---------------------- IWDG registers bit mask ----------------------------*/ 47 | 48 | /* KR register bit mask */ 49 | #define KR_KEY_Reload ((uint16_t)0xAAAA) 50 | #define KR_KEY_Enable ((uint16_t)0xCCCC) 51 | 52 | /** 53 | * @} 54 | */ 55 | 56 | /** @defgroup IWDG_Private_Macros 57 | * @{ 58 | */ 59 | 60 | /** 61 | * @} 62 | */ 63 | 64 | /** @defgroup IWDG_Private_Variables 65 | * @{ 66 | */ 67 | 68 | /** 69 | * @} 70 | */ 71 | 72 | /** @defgroup IWDG_Private_FunctionPrototypes 73 | * @{ 74 | */ 75 | 76 | /** 77 | * @} 78 | */ 79 | 80 | /** @defgroup IWDG_Private_Functions 81 | * @{ 82 | */ 83 | 84 | /** 85 | * @brief Enables or disables write access to IWDG_PR and IWDG_RLR registers. 86 | * @param IWDG_WriteAccess: new state of write access to IWDG_PR and IWDG_RLR registers. 87 | * This parameter can be one of the following values: 88 | * @arg IWDG_WriteAccess_Enable: Enable write access to IWDG_PR and IWDG_RLR registers 89 | * @arg IWDG_WriteAccess_Disable: Disable write access to IWDG_PR and IWDG_RLR registers 90 | * @retval None 91 | */ 92 | void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess) 93 | { 94 | /* Check the parameters */ 95 | assert_param(IS_IWDG_WRITE_ACCESS(IWDG_WriteAccess)); 96 | IWDG->KR = IWDG_WriteAccess; 97 | } 98 | 99 | /** 100 | * @brief Sets IWDG Prescaler value. 101 | * @param IWDG_Prescaler: specifies the IWDG Prescaler value. 102 | * This parameter can be one of the following values: 103 | * @arg IWDG_Prescaler_4: IWDG prescaler set to 4 104 | * @arg IWDG_Prescaler_8: IWDG prescaler set to 8 105 | * @arg IWDG_Prescaler_16: IWDG prescaler set to 16 106 | * @arg IWDG_Prescaler_32: IWDG prescaler set to 32 107 | * @arg IWDG_Prescaler_64: IWDG prescaler set to 64 108 | * @arg IWDG_Prescaler_128: IWDG prescaler set to 128 109 | * @arg IWDG_Prescaler_256: IWDG prescaler set to 256 110 | * @retval None 111 | */ 112 | void IWDG_SetPrescaler(uint8_t IWDG_Prescaler) 113 | { 114 | /* Check the parameters */ 115 | assert_param(IS_IWDG_PRESCALER(IWDG_Prescaler)); 116 | IWDG->PR = IWDG_Prescaler; 117 | } 118 | 119 | /** 120 | * @brief Sets IWDG Reload value. 121 | * @param Reload: specifies the IWDG Reload value. 122 | * This parameter must be a number between 0 and 0x0FFF. 123 | * @retval None 124 | */ 125 | void IWDG_SetReload(uint16_t Reload) 126 | { 127 | /* Check the parameters */ 128 | assert_param(IS_IWDG_RELOAD(Reload)); 129 | IWDG->RLR = Reload; 130 | } 131 | 132 | /** 133 | * @brief Reloads IWDG counter with value defined in the reload register 134 | * (write access to IWDG_PR and IWDG_RLR registers disabled). 135 | * @param None 136 | * @retval None 137 | */ 138 | void IWDG_ReloadCounter(void) 139 | { 140 | IWDG->KR = KR_KEY_Reload; 141 | } 142 | 143 | /** 144 | * @brief Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled). 145 | * @param None 146 | * @retval None 147 | */ 148 | void IWDG_Enable(void) 149 | { 150 | IWDG->KR = KR_KEY_Enable; 151 | } 152 | 153 | /** 154 | * @brief Checks whether the specified IWDG flag is set or not. 155 | * @param IWDG_FLAG: specifies the flag to check. 156 | * This parameter can be one of the following values: 157 | * @arg IWDG_FLAG_PVU: Prescaler Value Update on going 158 | * @arg IWDG_FLAG_RVU: Reload Value Update on going 159 | * @retval The new state of IWDG_FLAG (SET or RESET). 160 | */ 161 | FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG) 162 | { 163 | FlagStatus bitstatus = RESET; 164 | /* Check the parameters */ 165 | assert_param(IS_IWDG_FLAG(IWDG_FLAG)); 166 | if ((IWDG->SR & IWDG_FLAG) != (uint32_t)RESET) 167 | { 168 | bitstatus = SET; 169 | } 170 | else 171 | { 172 | bitstatus = RESET; 173 | } 174 | /* Return the flag status */ 175 | return bitstatus; 176 | } 177 | 178 | /** 179 | * @} 180 | */ 181 | 182 | /** 183 | * @} 184 | */ 185 | 186 | /** 187 | * @} 188 | */ 189 | 190 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 191 | -------------------------------------------------------------------------------- /lib/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victzh/bradwii/51436da66ef105e0824a854fe07ca0ad2dbc368f/lib/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c -------------------------------------------------------------------------------- /lib/hal/drv_gpio.c: -------------------------------------------------------------------------------- 1 | #include "hal.h" 2 | 3 | void gpioInit(GPIO_TypeDef *gpio, gpio_config_t *config) 4 | { 5 | uint32_t pinpos; 6 | for (pinpos = 0; pinpos < 16; pinpos++) { 7 | // are we doing this pin? 8 | if (config->pin & (0x1 << pinpos)) { 9 | // reference CRL or CRH, depending whether pin number is 0..7 or 8..15 10 | __IO uint32_t *cr = &gpio->CRL + (pinpos / 8); 11 | // mask out extra bits from pinmode, leaving just CNF+MODE 12 | uint32_t currentmode = config->mode & 0x0F; 13 | // offset to CNF and MODE portions of CRx register 14 | uint32_t shift = (pinpos % 8) * 4; 15 | // Read out current CRx value 16 | uint32_t tmp = *cr; 17 | // if we're in output mode, add speed too. 18 | if (config->mode & 0x10) 19 | currentmode |= config->speed; 20 | // Mask out 4 bits 21 | tmp &= ~(0xF << shift); 22 | // apply current pinmode 23 | tmp |= currentmode << shift; 24 | *cr = tmp; 25 | // Special handling for IPD/IPU 26 | if (config->mode == Mode_IPD) { 27 | gpio->ODR &= ~(1U << pinpos); 28 | } else if (config->mode == Mode_IPU) { 29 | gpio->ODR |= (1U << pinpos); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /lib/hal/drv_gpio.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | typedef enum 4 | { 5 | Mode_AIN = 0x0, 6 | Mode_IN_FLOATING = 0x04, 7 | Mode_IPD = 0x28, 8 | Mode_IPU = 0x48, 9 | Mode_Out_OD = 0x14, 10 | Mode_Out_PP = 0x10, 11 | Mode_AF_OD = 0x1C, 12 | Mode_AF_PP = 0x18 13 | } GPIO_Mode; 14 | 15 | typedef enum 16 | { 17 | Speed_10MHz = 1, 18 | Speed_2MHz, 19 | Speed_50MHz 20 | } GPIO_Speed; 21 | 22 | typedef enum 23 | { 24 | Pin_0 = 0x0001, 25 | Pin_1 = 0x0002, 26 | Pin_2 = 0x0004, 27 | Pin_3 = 0x0008, 28 | Pin_4 = 0x0010, 29 | Pin_5 = 0x0020, 30 | Pin_6 = 0x0040, 31 | Pin_7 = 0x0080, 32 | Pin_8 = 0x0100, 33 | Pin_9 = 0x0200, 34 | Pin_10 = 0x0400, 35 | Pin_11 = 0x0800, 36 | Pin_12 = 0x1000, 37 | Pin_13 = 0x2000, 38 | Pin_14 = 0x4000, 39 | Pin_15 = 0x8000, 40 | Pin_All = 0xFFFF 41 | } GPIO_Pin; 42 | 43 | typedef struct 44 | { 45 | uint16_t pin; 46 | GPIO_Mode mode; 47 | GPIO_Speed speed; 48 | } gpio_config_t; 49 | 50 | #define digitalHi(p, i) { p->BSRR = i; } 51 | #define digitalLo(p, i) { p->BRR = i; } 52 | #define digitalToggle(p, i) { p->ODR ^= i; } 53 | #define digitalIn(p, i) (p->IDR & i) 54 | 55 | void gpioInit(GPIO_TypeDef *gpio, gpio_config_t *config); 56 | -------------------------------------------------------------------------------- /lib/hal/drv_hal.c: -------------------------------------------------------------------------------- 1 | #include "hal.h" 2 | 3 | extern void SetSysClock(void); 4 | #define AFIO_MAPR_SWJ_CFG_NO_JTAG_SW (0x2 << 24) 5 | 6 | void lib_hal_init(void) 7 | { 8 | gpio_config_t gpio; 9 | drv_pwm_config_t pwm; 10 | 11 | // Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers 12 | // Configure the Flash Latency cycles and enable prefetch buffer 13 | SetSysClock(); 14 | 15 | // Turn on clocks for stuff we use 16 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 | RCC_APB1Periph_I2C2, ENABLE); 17 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_TIM1 | RCC_APB2Periph_ADC1 | RCC_APB2Periph_USART1, ENABLE); 18 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); 19 | RCC_ClearFlag(); 20 | 21 | // Make all GPIO in by default to save power and reduce noise 22 | gpio.pin = Pin_All; 23 | gpio.mode = Mode_AIN; 24 | gpioInit(GPIOA, &gpio); 25 | gpioInit(GPIOB, &gpio); 26 | gpioInit(GPIOC, &gpio); 27 | 28 | // Turn off JTAG port 'cause we're using the GPIO for leds 29 | AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_NO_JTAG_SW; 30 | 31 | pwm.airplane = false; 32 | pwm.useUART = false; 33 | pwm.usePPM = true; 34 | pwm.enableInput = true; 35 | pwm.useServos = false; 36 | pwm.extraServos = false; 37 | pwm.motorPwmRate = 498; 38 | pwm.servoPwmRate = 50; 39 | 40 | pwmInit(&pwm); 41 | } 42 | 43 | #ifndef FLASH_PAGE_COUNT 44 | #define FLASH_PAGE_COUNT 128 45 | #endif 46 | 47 | #define FLASH_PAGE_SIZE ((uint16_t)0x400) 48 | #define FLASH_WRITE_ADDR (0x08000000 + (uint32_t)FLASH_PAGE_SIZE * (FLASH_PAGE_COUNT - 1)) // use the last KB for storage 49 | #define EEP_SIZE (FLASH_PAGE_SIZE) 50 | 51 | static uint8_t eep[EEP_SIZE]; 52 | 53 | size_t eeprom_write_block (const void *src, uint16_t index, size_t size) 54 | { 55 | memcpy(eep + index, src, size); 56 | return size; 57 | } 58 | 59 | void eeprom_commit(void) 60 | { 61 | int tries = 0; 62 | int i; 63 | FLASH_Status status; 64 | 65 | retry: 66 | FLASH_Unlock(); 67 | FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); 68 | 69 | if (FLASH_ErasePage(FLASH_WRITE_ADDR) == FLASH_COMPLETE) { 70 | for (i = 0; i < EEP_SIZE; i += 4) { 71 | status = FLASH_ProgramWord(FLASH_WRITE_ADDR + i, *(uint32_t *) ((char *)eep + i)); 72 | if (status != FLASH_COMPLETE) { 73 | FLASH_Lock(); 74 | tries++; 75 | if (tries < 3) 76 | goto retry; 77 | else 78 | break; 79 | } 80 | } 81 | } 82 | FLASH_Lock(); 83 | } 84 | 85 | size_t eeprom_read_block (void *dst, uint16_t index, size_t size) 86 | { 87 | memcpy(dst, (char *)FLASH_WRITE_ADDR + index, size); 88 | return size; 89 | } 90 | -------------------------------------------------------------------------------- /lib/hal/drv_pwm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define MAX_MOTORS 12 4 | #define MAX_SERVOS 8 5 | #define MAX_INPUTS 8 6 | 7 | typedef struct drv_pwm_config_t { 8 | bool enableInput; 9 | bool usePPM; 10 | bool useUART; 11 | bool useServos; 12 | bool extraServos; // configure additional 4 channels in PPM mode as servos, not motors 13 | bool airplane; // fixed wing hardware config, lots of servos etc 14 | uint16_t motorPwmRate; 15 | uint16_t servoPwmRate; 16 | } drv_pwm_config_t; 17 | 18 | // This indexes into the read-only hardware definition structure in drv_pwm.c, as well as into pwmPorts[] structure with dynamic data. 19 | enum { 20 | PWM1 = 0, 21 | PWM2, 22 | PWM3, 23 | PWM4, 24 | PWM5, 25 | PWM6, 26 | PWM7, 27 | PWM8, 28 | PWM9, 29 | PWM10, 30 | PWM11, 31 | PWM12, 32 | PWM13, 33 | PWM14, 34 | MAX_PORTS 35 | }; 36 | 37 | typedef struct { 38 | TIM_TypeDef *tim; 39 | GPIO_TypeDef *gpio; 40 | uint32_t pin; 41 | uint8_t channel; 42 | uint8_t irq; 43 | uint8_t outputEnable; 44 | } pwmHardware_t; 45 | 46 | bool pwmInit(drv_pwm_config_t *init); // returns whether driver is asking to calibrate throttle or not 47 | void pwmWriteMotor(uint8_t index, uint16_t value); 48 | void pwmWriteServo(uint8_t index, uint16_t value); 49 | uint16_t pwmRead(uint8_t channel); 50 | -------------------------------------------------------------------------------- /lib/hal/drv_serial.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victzh/bradwii/51436da66ef105e0824a854fe07ca0ad2dbc368f/lib/hal/drv_serial.c -------------------------------------------------------------------------------- /lib/hal/drv_serial.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define UART_BUFFER_SIZE 64 4 | 5 | #define UART1_RX_BUFFER_SIZE 256 6 | #define UART1_TX_BUFFER_SIZE 16 7 | #define UART2_RX_BUFFER_SIZE 64 8 | #define UART2_TX_BUFFER_SIZE 64 9 | #define UART3_RX_BUFFER_SIZE 64 10 | #define UART3_TX_BUFFER_SIZE 64 11 | 12 | typedef enum portMode_t { 13 | MODE_RX = 1, 14 | MODE_TX = 2, 15 | MODE_RXTX = MODE_RX | MODE_TX 16 | } portMode_t; 17 | 18 | typedef void (* serialReceiveCallbackPtr)(uint8_t data); // used by serial drivers to return frames to app 19 | 20 | typedef struct { 21 | portMode_t mode; 22 | uint32_t baudRate; 23 | uint32_t rxBufferSize; 24 | uint32_t txBufferSize; 25 | volatile uint8_t *rxBuffer; 26 | volatile uint8_t *txBuffer; 27 | uint32_t rxDMAPos; 28 | bool txDMAEmpty; 29 | uint32_t rxBufferHead; 30 | uint32_t rxBufferTail; 31 | uint32_t txBufferHead; 32 | uint32_t txBufferTail; 33 | 34 | DMA_Channel_TypeDef *rxDMAChannel; 35 | DMA_Channel_TypeDef *txDMAChannel; 36 | uint32_t rxDMAIrq; 37 | uint32_t txDMAIrq; 38 | USART_TypeDef *USARTx; 39 | 40 | serialReceiveCallbackPtr callback; 41 | } serialPort_t; 42 | 43 | extern serialPort_t serialPort1; 44 | extern serialPort_t serialPort2; 45 | extern serialPort_t serialPort3; 46 | 47 | serialPort_t *serialOpen(USART_TypeDef *USARTx, serialReceiveCallbackPtr callback, uint32_t baudRate, portMode_t mode); 48 | uint8_t uartAvailable(serialPort_t *s); 49 | uint8_t uartRead(serialPort_t *s); 50 | void uartWrite(serialPort_t *s, uint8_t ch); 51 | -------------------------------------------------------------------------------- /lib/hal/hal.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define __USE_C99_MATH 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "stm32f10x_conf.h" 14 | #include "core_cm3.h" 15 | #include "drv_gpio.h" 16 | #include "drv_serial.h" 17 | #include "drv_pwm.h" 18 | 19 | void lib_hal_init(void); 20 | // eeprom_read and eeprom_write return number of read/written bytes 21 | size_t eeprom_read_block (void *dst, uint16_t index, size_t size); 22 | size_t eeprom_write_block (const void *src, uint16_t index, size_t size); 23 | void eeprom_commit(void); 24 | -------------------------------------------------------------------------------- /lib/hal/lib_digitalio.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "hal.h" 19 | #include "lib_digitalio.h" 20 | 21 | // This code controls the simple digital IO 22 | 23 | // Usage: 24 | 25 | // #define LEDOUTPUT (DIGITALPORTB | 7) // defines the reference for pin 7 on PORTB 26 | // lib_digitalio_initpin(LEDOUTPUT,DIGITALOUTPUT | PULLUPRESISTOR); // set the pin as an output (also turns on the pull up resistor) 27 | // lib_digitalio_setoutput(LEDOUTPUT, DIGITALLOW); // turn the output on by pulling it low 28 | 29 | // #define PUSHBUTTON (DIGITALPORTB | 4) // defines the reference for pin 4 on PORTB 30 | // #define INTERRUPT6PORTANDPIN PUSHBUTTON 31 | // lib_digitalio_initpin(PUSHBUTTON,DIGITALINPUT); // set the pin as an input (also turns on the pull up resistor) 32 | // if (lib_digitalio_getinput(PUSHBUTTON)) {} // Check the input 33 | // unimplemented below: 34 | // lib_digitalio_setinterruptcallback(PUSHBUTTON,mypushbuttoncallback); // tell the interrupt 35 | // void mypushbuttoncallback(char interruptnumber,char newstate) // call back will get called any time the pin changes 36 | // { 37 | // if (newstate==DIGITALON) {} 38 | // } 39 | 40 | 41 | static GPIO_TypeDef *lib_digitalio_getport(unsigned char pinnumber) 42 | { 43 | unsigned char port = pinnumber & 0xf0; 44 | 45 | switch (port) { 46 | case DIGITALPORTA: 47 | return GPIOA; 48 | case DIGITALPORTB: 49 | return GPIOB; 50 | case DIGITALPORTC: 51 | return GPIOC; 52 | default: 53 | return NULL; 54 | } 55 | } 56 | 57 | void lib_digitalio_initpin(unsigned char pinnumber, unsigned char output) 58 | { 59 | // set pin pinnumber to be an output if output | DIGITALOUTPUT, othewise set it to be an input 60 | GPIO_TypeDef *gpio = lib_digitalio_getport(pinnumber); 61 | gpio_config_t cfg; 62 | 63 | pinnumber &= 0x0f; 64 | 65 | cfg.pin = 1 << pinnumber; 66 | cfg.speed = Speed_2MHz; 67 | if (output & DIGITALOUTPUT) 68 | cfg.mode = Mode_Out_PP; 69 | else 70 | cfg.mode = Mode_IN_FLOATING; 71 | gpioInit(gpio, &cfg); 72 | } 73 | 74 | unsigned char lib_digitalio_getinput(unsigned char pinnumber) 75 | { 76 | GPIO_TypeDef *gpio = lib_digitalio_getport(pinnumber); 77 | pinnumber &= 0x0f; 78 | 79 | return (gpio->IDR & (1 << pinnumber)) != 0; 80 | } 81 | 82 | void lib_digitalio_setoutput(unsigned char pinnumber, unsigned char value) 83 | { 84 | GPIO_TypeDef *gpio = lib_digitalio_getport(pinnumber); 85 | pinnumber &= 0x0f; 86 | 87 | if (value) 88 | gpio->BRR = (1 << pinnumber); 89 | else 90 | gpio->BSRR = (1 << pinnumber); 91 | } 92 | 93 | void lib_digitalio_setinterruptcallback(unsigned char pinnumber, digitalcallbackfunctptr callback) 94 | { 95 | // Not implemented, no need on real hardware... 96 | } 97 | -------------------------------------------------------------------------------- /lib/hal/lib_digitalio.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #pragma once 19 | 20 | #include "projectsettings.h" 21 | 22 | #define DIGITALINPUT 0 23 | #define DIGITALOUTPUT 1 24 | 25 | #define DIGITALPORTA 0x10 26 | #define DIGITALPORTB 0x20 27 | #define DIGITALPORTC 0x30 28 | 29 | #define DIGITALHIGH 0 30 | #define DIGITALLOW 1 31 | 32 | #define DIGITALON 1 33 | #define DIGITALOFF 0 34 | 35 | void lib_digitalio_initpin(unsigned char portandpinnumber, unsigned char output); 36 | unsigned char lib_digitalio_getinput(unsigned char portandpinnumber); 37 | void lib_digitalio_setoutput(unsigned char portandpinnumber,unsigned char value); 38 | 39 | typedef void (* digitalcallbackfunctptr)(unsigned char interruptnumber, unsigned char newpinstate); 40 | void lib_digitalio_setinterruptcallback(unsigned char portandpinnumber, digitalcallbackfunctptr callback); 41 | -------------------------------------------------------------------------------- /lib/hal/lib_fp.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | 19 | // a fixedpointnum is a real number that's stored in a long that's shifted left FIXEDPOINTSHIFT bits 20 | // FIXEDPOINTSHIFT is currently 16, so 16 bits are used for the integer part of the number and 21 | // the other 16 bits are used for the fractional part of the number. I've tried using different 22 | // values, but the compliler likes shifting things by 16 bits (words at a time) so things run 23 | // faster with a FIXEDPOINTSHIFT of 16. 24 | 25 | // Therefore, the range of a fixedpointnum is -32768.0 to 32786.0 with an accuracy of 0.000015 26 | // There is no overflow or underflow protection, so it's up to the programmer to watch out. 27 | 28 | #include "hal.h" 29 | 30 | #define fixedpointnum int32_t 31 | 32 | #define FIXEDPOINTSHIFT 16 33 | 34 | #define FIXEDPOINTONE (1L<>1) 49 | #define FIXEDPOINTONEOVERFOUR (FIXEDPOINTONE>>2) 50 | #define FIXEDPOINTONEOVERONEFOURTH (FIXEDPOINTONE<<2) 51 | #define FIXEDPOINTONEOVERONESIXTEENTH (FIXEDPOINTONE<<4) 52 | #define FIXEDPOINTONEOVERONESIXTYITH (60L<. 16 | */ 17 | 18 | #pragma once 19 | 20 | #define I2C_READ 1 21 | #define I2C_WRITE 0 22 | 23 | #define I2C_100_KHZ 0 24 | #define I2C_400_KHZ 1 25 | 26 | void lib_i2c_init(void); 27 | void lib_i2c_setclockspeed(unsigned char speed); 28 | unsigned char lib_i2c_start(unsigned char address); 29 | char lib_i2c_start_wait(unsigned char address); 30 | unsigned char lib_i2c_rep_start(unsigned char address); 31 | void lib_i2c_stop(void); 32 | unsigned char lib_i2c_write( unsigned char data ); 33 | unsigned char lib_i2c_readack(void); 34 | unsigned char lib_i2c_readnak(void); 35 | void lib_i2c_writereg(unsigned char address,unsigned char reg, unsigned char value); 36 | unsigned char lib_i2c_readreg(unsigned char address,unsigned char reg); 37 | void lib_i2c_readdata(unsigned char address,unsigned char register,unsigned char *data,unsigned char length); 38 | 39 | -------------------------------------------------------------------------------- /lib/hal/lib_serial.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | // This library manages serial ports. It sets up input and output buffers and sends and receieves using interrupts. 19 | // To use this code, define which ports to include and the size of their buffers in projectsettings.h 20 | // Then use the library like this: 21 | // 22 | // lib_serial_initport(2,9600); // init serial port 2 to 9600 baud 23 | // lib_serial_sendstring(2,"Send this string"); 24 | // lib_serial_sendchar(2,'.'); 25 | // lib_serial_senddata(2,"This is Data",4); // sends the first 4 characters of the string 26 | // int count=lib_serial_numcharsavailable(2); 27 | // if (count>0) 28 | // { 29 | // lib_serial_getdata(2,data,count); 30 | // } 31 | 32 | #include "hal.h" 33 | #include "lib_serial.h" 34 | #include "projectsettings.h" 35 | 36 | static serialPort_t *lib_serial_getport(unsigned char serialportnumber) 37 | { 38 | switch (serialportnumber) { 39 | case 1: 40 | return &serialPort1; 41 | case 2: 42 | return &serialPort2; 43 | default: 44 | return NULL; 45 | } 46 | } 47 | 48 | int lib_serial_availableoutputbuffersize(unsigned char serialportnumber) 49 | { 50 | // returns how many more bytes can fit in the outputbuffer 51 | return 128; // TODO who cares 52 | } 53 | 54 | void lib_serial_setrxcallback(unsigned char serialportnumber, serialcallbackfunctptr callback) 55 | { 56 | serialPort_t *port = lib_serial_getport(serialportnumber); 57 | 58 | port->callback = callback; 59 | } 60 | 61 | void lib_serial_initport(unsigned char serialportnumber, long baud) 62 | { 63 | // initialize the serial port and set up a read buffer and interrupts so that we don't lose any data from reading too slowly 64 | switch (serialportnumber) { 65 | case 1: 66 | serialOpen(USART1, NULL, baud, MODE_RXTX); 67 | break; 68 | case 2: 69 | serialOpen(USART2, NULL, baud, MODE_RX); 70 | break; 71 | } 72 | } 73 | 74 | void lib_serial_sendchar(unsigned char serialportnumber, unsigned char c) 75 | { 76 | // add a character to the send buffer 77 | serialPort_t *port = lib_serial_getport(serialportnumber); 78 | uartWrite(port, c); 79 | } 80 | 81 | void lib_serial_sendstring(unsigned char serialportnumber, char *string) 82 | { 83 | // adds the string to the output buffer. 84 | while (*string) 85 | lib_serial_sendchar(serialportnumber, *string++); 86 | } 87 | 88 | void lib_serial_senddata(unsigned char serialportnumber, unsigned char *data, int datalength) 89 | { 90 | // send datalength bytes of data to the serial port 91 | while (datalength-- >0) 92 | lib_serial_sendchar(serialportnumber, *data++); 93 | } 94 | 95 | int lib_serial_numcharsavailable(unsigned char serialportnumber) 96 | { 97 | // returns number of characters available in the rx buffer 98 | serialPort_t *port = lib_serial_getport(serialportnumber); 99 | return uartAvailable(port); 100 | } 101 | 102 | unsigned char lib_serial_getchar(unsigned char serialportnumber) 103 | { 104 | // get the next character from the serial port 105 | serialPort_t *port = lib_serial_getport(serialportnumber); 106 | return uartRead(port); 107 | } 108 | 109 | void lib_serial_getdata(unsigned char serialportnumber, unsigned char *data, int numchars) 110 | { 111 | int x; 112 | for (x = 0; x < numchars; ++x) 113 | *data++ = lib_serial_getchar(serialportnumber); 114 | } 115 | -------------------------------------------------------------------------------- /lib/hal/lib_serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | 19 | #pragma once 20 | 21 | void lib_serial_initport(unsigned char serialportnumber,long baud); 22 | void lib_serial_sendchar(unsigned char serialportnumber,unsigned char c); 23 | void lib_serial_sendstring(unsigned char serialportnumber,char *string); 24 | void lib_serial_senddata(unsigned char serialportnumber,unsigned char *data,int datalength); 25 | int lib_serial_numcharsavailable(unsigned char serialportnumber); 26 | unsigned char lib_serial_getchar(unsigned char serialportnumber); 27 | void lib_serial_getdata(unsigned char serialportnumber,unsigned char *data,int datalength); 28 | int lib_serial_availableoutputbuffersize(unsigned char serialportnumber); 29 | 30 | typedef void (* serialcallbackfunctptr)(unsigned char c); 31 | void lib_serial_setrxcallback(unsigned char serialportnumber,serialcallbackfunctptr callback); 32 | 33 | 34 | #define USBPORTNUMBER 5 35 | -------------------------------------------------------------------------------- /lib/hal/lib_timers.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "hal.h" 19 | #include "lib_timers.h" 20 | 21 | // This code creates a timer to count microseconds and includes support for using this timer to run multiple 22 | // software timers. The timer can run on either TIMER0 or TIMER1. When run on TIMER1, a 16 bit timer is used 23 | // which gives us microsecond resolution and less interrupt overhead. When run on TIMER0, we get 4 microsecond 24 | // resolution and more overhead. Unsigned longs are used to store microseconds, so the longest intervals that 25 | // can be measured without extra code is about 70 minutes. 26 | 27 | // Usage: 28 | 29 | // half second delay done multiple ways 30 | 31 | // inittimers(); 32 | // unsigned long mytimer=lib_timers_starttimer(); 33 | // while (lib_timers_gettimermicroseconds(mytimer)<500000L) {} 34 | 35 | // inittimers(); 36 | // lib_timers_delaymilliseconds(500) {} 37 | 38 | 39 | // cycles per microsecond 40 | static volatile uint32_t usTicks = 0; 41 | // current uptime for 1kHz systick timer. will rollover after 49 days. hopefully we won't care. 42 | static volatile uint32_t sysTickUptime = 0; 43 | 44 | // SysTick 45 | void SysTick_Handler(void) 46 | { 47 | sysTickUptime++; 48 | } 49 | 50 | void lib_timers_init(void) 51 | { 52 | // needs to be called once in the program before timers can be used 53 | 54 | RCC_ClocksTypeDef clocks; 55 | RCC_GetClocksFreq(&clocks); 56 | usTicks = clocks.SYSCLK_Frequency / 1000000; 57 | 58 | // SysTick 59 | SysTick_Config(SystemCoreClock / 1000); 60 | } 61 | 62 | uint32_t lib_timers_getcurrentmicroseconds(void) 63 | { 64 | // returns microseconds since startup. This mainly used internally because it wraps around. 65 | register uint32_t ms, cycle_cnt; 66 | do { 67 | ms = sysTickUptime; 68 | cycle_cnt = SysTick->VAL; 69 | } while (ms != sysTickUptime); 70 | return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks; 71 | } 72 | 73 | 74 | unsigned long lib_timers_gettimermicroseconds(unsigned long starttime) 75 | { 76 | // returns microseconds since this timer was started 77 | unsigned long currenttime = lib_timers_getcurrentmicroseconds(); 78 | if (starttime > currenttime) // we have wrapped around 79 | { 80 | return (0xFFFFFFFF - starttime + currenttime); 81 | } else { 82 | return (currenttime - starttime); 83 | } 84 | } 85 | 86 | unsigned long lib_timers_gettimermicrosecondsandreset(unsigned long *starttime) 87 | { \ 88 | // returns microseconds since this timer was started and then reset the start time 89 | // this allows us to keep checking the time without losing any time 90 | unsigned long currenttime = lib_timers_getcurrentmicroseconds(); 91 | unsigned long returnvalue; 92 | 93 | if (*starttime > currenttime) // we have wrapped around 94 | { 95 | returnvalue = (0xFFFFFFFF - *starttime + currenttime); 96 | } else { 97 | returnvalue = (currenttime - *starttime); 98 | } 99 | *starttime = currenttime; 100 | return (returnvalue); 101 | } 102 | 103 | unsigned long lib_timers_starttimer() 104 | { // start a timer 105 | return (lib_timers_getcurrentmicroseconds()); 106 | } 107 | 108 | void lib_timers_delaymilliseconds(unsigned long delaymilliseconds) 109 | { 110 | unsigned long timercounts = lib_timers_starttimer(); 111 | while (lib_timers_gettimermicroseconds(timercounts) < delaymilliseconds * 1000L) { 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /lib/hal/lib_timers.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #pragma once 19 | 20 | void lib_timers_init(void); 21 | unsigned long lib_timers_starttimer(void); 22 | unsigned long lib_timers_gettimermicroseconds(unsigned long starttime); 23 | unsigned long lib_timers_gettimermicrosecondsandreset(unsigned long *starttime); 24 | void lib_timers_delaymilliseconds(unsigned long delaymilliseconds); 25 | -------------------------------------------------------------------------------- /pinout.txt: -------------------------------------------------------------------------------- 1 | 1 = MOSFET SENSE NC 2 | 2 = RESET 3 | 3 = NC? 4 | 4 = NC? 5 | 5 = MOSFET SENSE NC 6 | 6 = GYRO/ACC 7 | 7 = GYRO/ACC SDA 8 | 8 = GYRO/ACC SCL 9 | 9 = TESTPOINT NC 10 | 10 = NC? 11 | 11 = NC? 12 | 12 = NC? 13 | 13 = to R and then to sot23 but all NC 14 | 14 = NC? 15 | 15 = NC? 16 | 16 = MOSFET PWM M1 17 | 17 = MOSFET PWM M2 18 | 18 = MOSFET PWM M3 19 | 19 = ICE CLK 20 | 20 = ICE DATA 21 | 21 = NRF24 22 | 22 = NRF24 23 | 23 = NRF24 24 | 24 = MOSFET PWM M4 25 | 25 = NRF24 26 | 26 = LEDS (both!) 27 | 27 = >>>AIN<<< NC ? 28 | 28 = VDD 29 | 29 = RESISTOR ---> V+ Battery? = P1.0 AIN1 30 | 30 = RX UART 31 | 31 = TX UART 32 | 32 = MOSEFET SENSE NC 33 | -------------------------------------------------------------------------------- /pinout_q4.txt: -------------------------------------------------------------------------------- 1 | Nuvoton Mini54ZAN / HBS002 in Hubsan Q4 / Estes Proto-X 2 | 1 - ACMP0_P, AIN5, P1.5 - 3 | 2 - /RESET - "R" pad (?) 4 | 3 - ACMP1_N, AIN6, P3.0 - 5 | 4 - P5.4 - 6 | 5 - ACMP1_P, AIN7, P3.1 - 7 | 6 - ACMP1_P, T0EX, STADC, INT0, P3.2 - 8 | 7 - ACMP1_P, SDA, T0, P3.4 - sensors SDA 9 | 8 - ACMP1_P, SCL, T1, P3.5 - sensors SCL 10 | 9 - CKO, T1EX, ACMP0_O, P3.6 - 11 | 10 - XTAL2, P5.1 - LED FL 12 | 11 - XTAL1, P5.0 - LED RL 13 | 12 - VSS - ground 14 | 13 - INT1, P5.2 - 15 | 14 - PWM0, P2.2 - MOT FL 16 | 15 - PWM1, P2.3 - MOT RL 17 | 16 - PWM2, P2.4 - MOT RR 18 | 17 - PWM3, P2.5 - MOT FR 19 | 18 - PWM4, ACMP1_O, P2.6 - LED RR 20 | 19 - ICE_CLK, P4.6 - SWD "C" pad 21 | 20 - ICE_DAT, P4.7 - SWD "D" pad 22 | 21 - SPICLK, P0.7 - 23 | 22 - MISO, P0.6 - 24 | 23 - MOSI, P0.5 - 25 | 24 - SPISS, PWM5, P0.4 - LED FR 26 | 25 - SPISS, RX, RTSn, P0.1 - MPU3050 gyro p12 INT 27 | 26 - TX, CTSn, P0.0 - A7105 P16 GIO1 28 | 27 - AIN0, P5.3 - A7105 p14 SDIO 29 | 28 - VDD - 3.3V from regulator 30 | 29 - ACMP0_P, AIN1, P1.0 - A7105 p12 SCK 31 | 30 - ACMP0_P, RX, AIN2, P1.2 - A7105 p11 SCS 32 | 31 - ACMP0_P, TX, AIN3, P1.3 - 3210AK accel p5 INTN 33 | 32 - ACMP0_N, AIN4, P1.4 - VBAT divider gnd-3.8K-AIN4-4.7-VBAT 34 | 33 - VSS (center pad) - ground 35 | -------------------------------------------------------------------------------- /src/a7105.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Goebish 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "a7105.h" 19 | #include "lib_soft_3_wire_spi.h" 20 | 21 | void A7105_WriteID(uint32_t ida) 22 | { 23 | lib_soft_3_wire_spi_setCS(DIGITALOFF); 24 | lib_soft_3_wire_spi_write(A7105_06_ID_DATA);//ex id=0x5475c52a ;txid3txid2txid1txid0 25 | lib_soft_3_wire_spi_write((ida>>24)&0xff);//53 26 | lib_soft_3_wire_spi_write((ida>>16)&0xff);//75 27 | lib_soft_3_wire_spi_write((ida>>8)&0xff);//c5 28 | lib_soft_3_wire_spi_write((ida>>0)&0xff);//2a 29 | lib_soft_3_wire_spi_setCS(DIGITALON); 30 | } 31 | 32 | // read 4 bytes ID 33 | void A7105_ReadID(uint8_t *_aid) 34 | { 35 | uint8_t i; 36 | lib_soft_3_wire_spi_setCS(DIGITALOFF); 37 | lib_soft_3_wire_spi_write(0x46); 38 | for(i=0;i<4;i++){ 39 | _aid[i]=lib_soft_3_wire_spi_read(); 40 | } 41 | lib_soft_3_wire_spi_setCS(DIGITALON); 42 | } 43 | 44 | void A7105_WritePayload(uint8_t *_packet, uint8_t len) 45 | { 46 | uint8_t i; 47 | lib_soft_3_wire_spi_setCS(DIGITALOFF); 48 | lib_soft_3_wire_spi_write(A7105_RST_WRPTR); 49 | lib_soft_3_wire_spi_write(0x05); 50 | for (i=0;i 2 | 3 | // strobe commands 4 | #define A7105_SLEEP 0x80 5 | #define A7105_IDLE 0x90 6 | #define A7105_STANDBY 0xA0 7 | #define A7105_PLL 0xB0 8 | #define A7105_RX 0xC0 9 | #define A7105_TX 0xD0 10 | #define A7105_RST_WRPTR 0xE0 11 | #define A7105_RST_RDPTR 0xF0 12 | 13 | // registers 14 | #define A7105_00_MODE 0x00 15 | #define A7105_01_MODE_CONTROL 0x01 16 | #define A7105_02_CALC 0x02 17 | #define A7105_03_FIFOI 0x03 18 | #define A7105_04_FIFOII 0x04 19 | #define A7105_05_FIFO_DATA 0x05 20 | #define A7105_06_ID_DATA 0x06 21 | #define A7105_07_RC_OSC_I 0x07 22 | #define A7105_08_RC_OSC_II 0x08 23 | #define A7105_09_RC_OSC_III 0x09 24 | #define A7105_0A_CK0_PIN 0x0A 25 | #define A7105_0B_GPIO1_PIN1 0x0B 26 | #define A7105_0C_GPIO2_PIN_II 0x0C 27 | #define A7105_0D_CLOCK 0x0D 28 | #define A7105_0E_DATA_RATE 0x0E 29 | #define A7105_0F_PLL_I 0x0F 30 | #define A7105_10_PLL_II 0x10 31 | #define A7105_11_PLL_III 0x11 32 | #define A7105_12_PLL_IV 0x12 33 | #define A7105_13_PLL_V 0x13 34 | #define A7105_14_TX_I 0x14 35 | #define A7105_15_TX_II 0x15 36 | #define A7105_16_DELAY_I 0x16 37 | #define A7105_17_DELAY_II 0x17 38 | #define A7105_18_RX 0x18 39 | #define A7105_19_RX_GAIN_I 0x19 40 | #define A7105_1A_RX_GAIN_II 0x1A 41 | #define A7105_1B_RX_GAIN_III 0x1B 42 | #define A7105_1C_RX_GAIN_IV 0x1C 43 | #define A7105_1D_RSSI_THOLD 0x1D 44 | #define A7105_1E_ADC 0x1E 45 | #define A7105_1F_CODE_I 0x1F 46 | #define A7105_20_CODE_II 0x20 47 | #define A7105_21_CODE_III 0x21 48 | #define A7105_22_IF_CALIB_I 0x22 49 | #define A7105_23_IF_CALIB_II 0x23 50 | #define A7105_24_VCO_CURCAL 0x24 51 | #define A7105_25_VCO_SBCAL_I 0x25 52 | #define A7105_26_VCO_SBCAL_II 0x26 53 | #define A7105_27_BATTERY_DET 0x27 54 | #define A7105_28_TX_TEST 0x28 55 | #define A7105_29_RX_DEM_TEST_I 0x29 56 | #define A7105_2A_RX_DEM_TEST_II 0x2A 57 | #define A7105_2B_CPC 0x2B 58 | #define A7105_2C_XTAL_TEST 0x2C 59 | #define A7105_2D_PLL_TEST 0x2D 60 | #define A7105_2E_VCO_TEST_I 0x2E 61 | #define A7105_2F_VCO_TEST_II 0x2F 62 | #define A7105_30_IFAT 0x30 63 | #define A7105_31_RSCALE 0x31 64 | #define A7105_32_FILTER_TEST 0x32 65 | 66 | #define A7105_MODE_TRER_MASK (uint8_t)(1 << 0) // TRX is enabled 67 | 68 | void A7105_WriteID(uint32_t ida); 69 | void A7105_ReadID(uint8_t *_aid); 70 | void A7105_WritePayload(uint8_t *_packet, uint8_t len); 71 | void A7105_ReadPayload(uint8_t *_packet, uint8_t len); 72 | void A7105_Reset(void); 73 | uint8_t A7105_ReadRegister(uint8_t address); 74 | void A7105_WriteRegister(uint8_t address, uint8_t data); 75 | void A7105_Strobe(uint8_t command); 76 | -------------------------------------------------------------------------------- /src/accelerometer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | void initacc(void); 19 | void readacc(void); 20 | -------------------------------------------------------------------------------- /src/autotune.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | 19 | #include "lib_fp.h" 20 | 21 | 22 | #define AUTOTUNESTARTING 1 23 | #define AUTOTUNESTOPPING 2 24 | #define AUTOTUNETUNING 0 25 | 26 | #define AUTOTUNEGOINGTOWARDTARGET 0 27 | #define AUTOTUNEGOINGAWAYFROMTARGET 1 28 | 29 | #define AUTOTUNESETTLINGTIME (FIXEDPOINTCONSTANT(.25)<. 16 | */ 17 | 18 | void initbaro(void); 19 | char readbaro(void); 20 | -------------------------------------------------------------------------------- /src/checkboxes.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "checkboxes.h" 19 | #include "bradwii.h" 20 | #include "rx.h" 21 | #include "lib_timers.h" 22 | #include "config.h" 23 | 24 | extern globalstruct global; 25 | extern usersettingsstruct usersettings; 26 | 27 | #ifndef X4_BUILD 28 | char checkboxnames[] /* PROGMEM */ = // names for dynamic generation of config GUI 29 | // this could be moved to program memory if we wanted to save a few bytes of space. 30 | "Arm;" "Thr. Helper;" "Alt. Hold;" "Mag. Hold;" "Pos. Hold;" "Ret. Home;" "Semi Acro;" "Full Acro;" "High Rates;" "High Angle;" "Auto Tune;" "Uncrashable;" "Headfree;" "Yaw Hold;" "LED1;" "LED2;"; 31 | #endif 32 | 33 | // each checkbox item has a checkboxvalue. The bits in this value represent low, medium, and high checkboxes 34 | // for each of the aux switches, just as they show up in most config programs. 35 | void checkcheckboxitems(void) 36 | { 37 | global.previousactivecheckboxitems = global.activecheckboxitems; 38 | global.activecheckboxitems = 0; 39 | 40 | uint16_t mask = 0; // a mask of what aux states are true 41 | #if (RXNUMCHANNELS>4) 42 | if (global.rxvalues[AUX1INDEX] < FPAUXMIDRANGELOW) // low 43 | mask |= (1 << 0); 44 | else if (global.rxvalues[AUX1INDEX] > FPAUXMIDRANGEHIGH) // high 45 | mask |= (1 << 2); 46 | else 47 | mask |= (1 << 1); // mid 48 | #endif 49 | 50 | #if (RXNUMCHANNELS>5) 51 | if (global.rxvalues[AUX2INDEX] < FPAUXMIDRANGELOW) // low 52 | mask |= (1 << 3); 53 | else if (global.rxvalues[AUX2INDEX] > FPAUXMIDRANGEHIGH) // high 54 | mask |= (1 << 5); 55 | else 56 | mask |= (1 << 4); //mid 57 | #endif 58 | 59 | #if (RXNUMCHANNELS>6) 60 | if (global.rxvalues[AUX3INDEX] < FPAUXMIDRANGELOW) // low 61 | mask |= (1 << 6); 62 | else if (global.rxvalues[AUX3INDEX] > FPAUXMIDRANGEHIGH) // high 63 | mask |= (1 << 8); 64 | else 65 | mask |= (1 << 7); //mid 66 | #endif 67 | 68 | #if (RXNUMCHANNELS>7) 69 | if (global.rxvalues[AUX4INDEX] < FPAUXMIDRANGELOW) // low 70 | mask |= (1 << 9); 71 | else if (global.rxvalues[AUX4INDEX] > FPAUXMIDRANGEHIGH) // high 72 | mask |= (1 << 11); 73 | else 74 | mask |= (1 << 10); //mid 75 | #endif 76 | 77 | for (int x = 0; x < NUMCHECKBOXES; ++x) { 78 | if (usersettings.checkboxconfiguration[x] & mask) 79 | global.activecheckboxitems |= (1 << x); 80 | } 81 | 82 | #if (defined(STICK_ARM) | defined (STICK_DISARM)) 83 | // figure out where the sticks are 84 | unsigned int stickmask = 0; 85 | static uint32_t stickCounterArm = 0; 86 | static uint32_t stickCounterDisArm = 0; 87 | 88 | if (global.rxvalues[ROLLINDEX] < FPSTICKLOW) 89 | stickmask |= STICK_COMMAND_ROLL_LOW; 90 | else if (global.rxvalues[ROLLINDEX] > FPSTICKHIGH) 91 | stickmask |= STICK_COMMAND_ROLL_HIGH; 92 | 93 | if (global.rxvalues[PITCHINDEX] < FPSTICKLOW) 94 | stickmask |= STICK_COMMAND_PITCH_LOW; 95 | else if (global.rxvalues[PITCHINDEX] > FPSTICKHIGH) 96 | stickmask |= STICK_COMMAND_PITCH_HIGH; 97 | 98 | if (global.rxvalues[YAWINDEX] < FPSTICKLOW) 99 | stickmask |= STICK_COMMAND_YAW_LOW; 100 | else if (global.rxvalues[YAWINDEX] > FPSTICKHIGH) 101 | stickmask |= STICK_COMMAND_YAW_HIGH; 102 | 103 | 104 | // If the sticks are in the right positions, set the arm or disarm checkbox value 105 | // Start with the previous value in case the sticks aren't doing anything special 106 | global.activecheckboxitems = (global.activecheckboxitems & ~CHECKBOXMASKARM) |(global.previousactivecheckboxitems & CHECKBOXMASKARM); 107 | 108 | if ((stickmask & (STICK_ARM)) == STICK_ARM) { 109 | if (stickCounterArm == 0) 110 | stickCounterArm = lib_timers_starttimer(); 111 | 112 | if (lib_timers_gettimermicroseconds(stickCounterArm) > STICK_ARM_TIME * 1000) 113 | global.activecheckboxitems |= CHECKBOXMASKARM; 114 | } 115 | else if ((stickmask & (STICK_DISARM)) == STICK_DISARM) { 116 | if (stickCounterDisArm == 0) 117 | stickCounterDisArm = lib_timers_starttimer(); 118 | 119 | if (lib_timers_gettimermicroseconds(stickCounterDisArm) > STICK_ARM_TIME * 1000) 120 | global.activecheckboxitems &= ~CHECKBOXMASKARM; 121 | } 122 | else 123 | { 124 | stickCounterArm = 0; 125 | stickCounterDisArm = 0; 126 | } 127 | 128 | #endif 129 | } 130 | -------------------------------------------------------------------------------- /src/checkboxes.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #define NUMPOSSIBLECHECKBOXES 20 // allocate room for more checkboxes in eeprom memory in case we add them later 19 | 20 | #define CHECKBOXARM 0 21 | #define CHECKBOXAUTOTHROTTLE 1 22 | #define CHECKBOXALTHOLD 2 23 | #define CHECKBOXCOMPASS 3 24 | #define CHECKBOXPOSITIONHOLD 4 25 | #define CHECKBOXRETURNTOHOME 5 26 | #define CHECKBOXSEMIACRO 6 27 | #define CHECKBOXFULLACRO 7 28 | #define CHECKBOXHIGHRATES 8 29 | #define CHECKBOXHIGHANGLE 9 30 | #define CHECKBOXAUTOTUNE 10 31 | #define CHECKBOXUNCRASHABLE 11 32 | #define CHECKBOXHEADFREE 12 33 | #define CHECKBOXYAWHOLD 13 34 | #define CHECKBOXLED1 14 35 | #define CHECKBOXLED2 15 36 | #define NUMCHECKBOXES 16 37 | 38 | #define CHECKBOXMASKARM (1<. 16 | */ 17 | 18 | void initcompass(void); 19 | void calibratecompass(void); 20 | char readcompass(void); 21 | -------------------------------------------------------------------------------- /src/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | // this is the file that is used to configure the software. Uncomment the appropriate lines by removing the // in front of them. 19 | // Configuration works with a lot of defaults. The only thing you really need to choose is the control board. After that, 20 | // all defaults will be chosen for that board. If you want to use other than defaults, then uncomment the things you want 21 | // to change. To see what the defaults are, look in defs.h 22 | // The options are defined in options.h 23 | 24 | #if defined(V202_BUILD) 25 | #include "config_V202.h" 26 | #elif defined(JD385_BUILD) 27 | #include "config_JD385.h" 28 | #elif defined(X4_BUILD) 29 | #include "config_X4.h" 30 | #else 31 | #include "config_STM32.h" 32 | #endif 33 | -------------------------------------------------------------------------------- /src/eeprom.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "eeprom.h" 19 | #include "bradwii.h" 20 | 21 | extern usersettingsstruct usersettings; 22 | extern globalstruct global; 23 | 24 | #define MAGICNUMBER 12345 25 | 26 | void writeusersettingstoeeprom(void) 27 | { 28 | #if (EEPROM_SIZE != NO_EEPROM) 29 | 30 | uint16_t magicnumber = MAGICNUMBER; 31 | int16_t size = sizeof(usersettingsstruct); 32 | uint16_t index = 0; 33 | 34 | // if usersettings larger den defined eeprom size, leave funktion. 35 | if(size > EEPROM_SIZE) 36 | return; 37 | 38 | index += eeprom_write_block((const void *)&magicnumber, index, sizeof(magicnumber)); 39 | index += eeprom_write_block((const void *)&size, index, sizeof(size)); 40 | index += eeprom_write_block((const void *)&usersettings, index, size); 41 | eeprom_commit(); //Why? It's a emtpy function in drv_hal.c! 42 | #endif 43 | } 44 | 45 | void readusersettingsfromeeprom(void) 46 | { 47 | #if (EEPROM_SIZE != NO_EEPROM) 48 | 49 | uint16_t magicnumber = 0; 50 | int16_t size = 0; 51 | uint16_t index = 0; 52 | index += eeprom_read_block((void *) &magicnumber, index, sizeof(magicnumber)); 53 | 54 | if (magicnumber != MAGICNUMBER) 55 | return; 56 | 57 | index += eeprom_read_block((void *) &size, index, sizeof(size)); 58 | if (size > sizeof(usersettingsstruct)) 59 | size = sizeof(usersettingsstruct); 60 | 61 | if(size > EEPROM_SIZE) 62 | return; 63 | 64 | eeprom_read_block((void *) &usersettings, index, size); 65 | global.usersettingsfromeeprom = 1; // set a flag so the rest of the program know it's working with calibtated settings 66 | #endif 67 | } 68 | -------------------------------------------------------------------------------- /src/eeprom.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | void writeusersettingstoeeprom(void); 19 | void readusersettingsfromeeprom(void); 20 | -------------------------------------------------------------------------------- /src/gps.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "lib_fp.h" 19 | 20 | #define LATLONGEXTRASHIFT 6 // keep latitudes and longitudes shifted extra for accuracy 21 | 22 | void initgps(void); 23 | char readgps(void); 24 | 25 | fixedpointnum gpsgetdistanceandbearing(fixedpointnum lat1, fixedpointnum lon1, fixedpointnum lat2, fixedpointnum lon2, fixedpointnum * bearing); 26 | -------------------------------------------------------------------------------- /src/gyro.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | void initgyro(void); 19 | void readgyro(void); 20 | -------------------------------------------------------------------------------- /src/imu.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "lib_fp.h" 19 | 20 | 21 | 22 | void initimu(void); 23 | void imucalculateestimatedattitude(void); 24 | void calibrategyroandaccelerometer(bool both); 25 | -------------------------------------------------------------------------------- /src/leds.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013-2014 TheLastMutt, TheSmartGerman 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "bradwii.h" 19 | #include "leds.h" 20 | #include "defs.h" 21 | #include "lib_timers.h" 22 | 23 | 24 | // if LED is defined set the according output from defs.s to according IO Pin 25 | 26 | void leds_init() 27 | { 28 | #ifdef LED1 29 | lib_digitalio_initpin(LED1_OUTPUT, DIGITALOUTPUT); 30 | #endif 31 | 32 | #ifdef LED2 33 | lib_digitalio_initpin(LED2_OUTPUT, DIGITALOUTPUT); 34 | #endif 35 | 36 | #ifdef LED3 37 | lib_digitalio_initpin(LED3_OUTPUT, DIGITALOUTPUT); 38 | #endif 39 | 40 | #ifdef LED4 41 | lib_digitalio_initpin(LED4_OUTPUT, DIGITALOUTPUT); 42 | #endif 43 | 44 | #ifdef LED5 45 | lib_digitalio_initpin(LED5_OUTPUT, DIGITALOUTPUT); 46 | #endif 47 | 48 | #ifdef LED6 49 | lib_digitalio_initpin(LED6_OUTPUT, DIGITALOUTPUT); 50 | #endif 51 | 52 | #ifdef LED7 53 | lib_digitalio_initpin(LED7_OUTPUT, DIGITALOUTPUT); 54 | #endif 55 | 56 | #ifdef LED8 57 | lib_digitalio_initpin(LED8_OUTPUT, DIGITALOUTPUT); 58 | #endif 59 | 60 | } 61 | 62 | // according to the state, switch the led on or off 63 | 64 | void leds_set(unsigned char state) 65 | { 66 | #ifdef LED1 67 | lib_digitalio_setoutput( LED1_OUTPUT , (state & LED1) ? LED1_ON : !LED1_ON); 68 | #endif 69 | 70 | #ifdef LED2 71 | lib_digitalio_setoutput( LED2_OUTPUT , (state & LED2) ? LED2_ON : !LED2_ON); 72 | #endif 73 | 74 | #ifdef LED3 75 | lib_digitalio_setoutput( LED3_OUTPUT , (state & LED3) ? LED3_ON : !LED3_ON); 76 | #endif 77 | 78 | #ifdef LED4 79 | lib_digitalio_setoutput( LED4_OUTPUT , (state & LED4) ? LED4_ON : !LED4_ON); 80 | #endif 81 | 82 | #ifdef LED5 83 | lib_digitalio_setoutput( LED5_OUTPUT , (state & LED5) ? LED5_ON : !LED5_ON); 84 | #endif 85 | 86 | #ifdef LED6 87 | lib_digitalio_setoutput( LED6_OUTPUT , (state & LED6) ? LED6_ON : !LED6_ON); 88 | #endif 89 | 90 | #ifdef LED7 91 | lib_digitalio_setoutput( LED7_OUTPUT , (state & LED7) ? LED7_ON : !LED7_ON); 92 | #endif 93 | 94 | #ifdef LED8 95 | lib_digitalio_setoutput( LED8_OUTPUT , (state & LED8) ? LED8_ON : !LED8_ON); 96 | #endif 97 | 98 | } 99 | 100 | 101 | // blink the LEDs a specified amount of cycles 102 | 103 | void leds_blink_cycles(unsigned char state, unsigned int on_time, unsigned int off_time, unsigned char cycles) 104 | { 105 | for(uint8_t i=0;i (off_time * 1000) ) 117 | leds_set(state); 118 | else 119 | leds_set(~state); 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/leds.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013-2014 TheSmartGerman 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #define LED_BLINK_FAST 100 19 | #define LED_BLINK_MEDIUM 250 20 | #define LED_BLINK_SLOW 500 21 | 22 | #define LED1_STATE 0x01 23 | #define LED2_STATE 0x02 24 | #define LED3_STATE 0x04 25 | #define LED4_STATE 0x08 26 | #define LED5_STATE 0x10 27 | #define LED6_STATE 0x20 28 | #define LED7_STATE 0x40 29 | #define LED8_STATE 0x80 30 | 31 | #define LED_ALL 0xFF 32 | #define LED_NONE 0x00 33 | 34 | 35 | void leds_init(void); 36 | void leds_set(unsigned char state); 37 | void leds_blink_cycles(unsigned char state, unsigned int on_time, unsigned int off_time, unsigned char cycles); 38 | void leds_blink_continuous(unsigned char state, unsigned int on_time, unsigned int off_time); 39 | -------------------------------------------------------------------------------- /src/navigation.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "lib_fp.h" 19 | 20 | void navigation_sethometocurrentlocation(void); 21 | void navigation_set_destination(fixedpointnum latitude, fixedpointnum longitude); 22 | void navigation_setangleerror(unsigned char gotnewgpsreading, fixedpointnum * angleerror); 23 | fixedpointnum navigation_getdistanceandbearing(fixedpointnum lat1, fixedpointnum lon1, fixedpointnum lat2, fixedpointnum lon2, fixedpointnum * bearing); 24 | -------------------------------------------------------------------------------- /src/nrf24l01.h: -------------------------------------------------------------------------------- 1 | // This file is copied with modifications from project Deviation, 2 | // see http://deviationtx.com, file iface_nrf24l01.h 3 | 4 | #ifndef _IFACE_NRF24L01_H_ 5 | #define _IFACE_NRF24L01_H_ 6 | 7 | // Register map 8 | enum { 9 | NRF24L01_00_CONFIG = 0x00, 10 | NRF24L01_01_EN_AA = 0x01, 11 | NRF24L01_02_EN_RXADDR = 0x02, 12 | NRF24L01_03_SETUP_AW = 0x03, 13 | NRF24L01_04_SETUP_RETR = 0x04, 14 | NRF24L01_05_RF_CH = 0x05, 15 | NRF24L01_06_RF_SETUP = 0x06, 16 | NRF24L01_07_STATUS = 0x07, 17 | NRF24L01_08_OBSERVE_TX = 0x08, 18 | NRF24L01_09_CD = 0x09, 19 | NRF24L01_0A_RX_ADDR_P0 = 0x0A, 20 | NRF24L01_0B_RX_ADDR_P1 = 0x0B, 21 | NRF24L01_0C_RX_ADDR_P2 = 0x0C, 22 | NRF24L01_0D_RX_ADDR_P3 = 0x0D, 23 | NRF24L01_0E_RX_ADDR_P4 = 0x0E, 24 | NRF24L01_0F_RX_ADDR_P5 = 0x0F, 25 | NRF24L01_10_TX_ADDR = 0x10, 26 | NRF24L01_11_RX_PW_P0 = 0x11, 27 | NRF24L01_12_RX_PW_P1 = 0x12, 28 | NRF24L01_13_RX_PW_P2 = 0x13, 29 | NRF24L01_14_RX_PW_P3 = 0x14, 30 | NRF24L01_15_RX_PW_P4 = 0x15, 31 | NRF24L01_16_RX_PW_P5 = 0x16, 32 | NRF24L01_17_FIFO_STATUS = 0x17, 33 | NRF24L01_1C_DYNPD = 0x1C, 34 | NRF24L01_1D_FEATURE = 0x1D 35 | }; 36 | 37 | // Bit mnemonics 38 | enum { 39 | NRF24L01_00_MASK_RX_DR = 6, 40 | NRF24L01_00_MASK_TX_DS = 5, 41 | NRF24L01_00_MASK_MAX_RT = 4, 42 | NRF24L01_00_EN_CRC = 3, 43 | NRF24L01_00_CRCO = 2, 44 | NRF24L01_00_PWR_UP = 1, 45 | NRF24L01_00_PRIM_RX = 0, 46 | NRF24L01_07_RX_DR = 6, 47 | NRF24L01_07_TX_DS = 5, 48 | NRF24L01_07_MAX_RT = 4, 49 | }; 50 | 51 | // Bitrates 52 | enum { 53 | NRF24L01_BR_1M = 0, 54 | NRF24L01_BR_2M, 55 | NRF24L01_BR_250K, 56 | NRF24L01_BR_RSVD 57 | }; 58 | 59 | enum TXRX_State { 60 | TXRX_OFF, 61 | TX_EN, 62 | RX_EN, 63 | }; 64 | 65 | enum TxPower { 66 | TXPOWER_100uW, 67 | TXPOWER_300uW, 68 | TXPOWER_1mW, 69 | TXPOWER_3mW, 70 | TXPOWER_10mW, 71 | TXPOWER_30mW, 72 | TXPOWER_100mW, 73 | TXPOWER_150mW, 74 | TXPOWER_LAST, 75 | }; 76 | 77 | 78 | void NRF24L01_Initialize(void); 79 | int NRF24L01_Reset(void); 80 | uint8_t NRF24L01_WriteReg(uint8_t reg, uint8_t data); 81 | uint8_t NRF24L01_WriteRegisterMulti(uint8_t reg, const uint8_t data[], uint8_t length); 82 | uint8_t NRF24L01_WritePayload(uint8_t *data, uint8_t len); 83 | uint8_t NRF24L01_ReadReg(uint8_t reg); 84 | uint8_t NRF24L01_ReadRegisterMulti(uint8_t reg, uint8_t data[], uint8_t length); 85 | uint8_t NRF24L01_ReadPayload(uint8_t *data, uint8_t len); 86 | 87 | uint8_t NRF24L01_FlushTx(void); 88 | uint8_t NRF24L01_FlushRx(void); 89 | uint8_t NRF24L01_Activate(uint8_t code); 90 | 91 | 92 | // Bitrate 0 - 1Mbps, 1 - 2Mbps, 3 - 250K (for nRF24L01+) 93 | uint8_t NRF24L01_SetBitrate(uint8_t bitrate); 94 | 95 | uint8_t NRF24L01_SetPower(uint8_t power); 96 | void NRF24L01_SetTxRxMode(enum TXRX_State); 97 | int NRF24L01_Reset(void); 98 | 99 | // To enable radio transmit after WritePayload you need to turn the radio 100 | //void NRF24L01_PulseCE(); 101 | 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /src/options.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | // This file contains options that can be set in the config.h file. 19 | 20 | #pragma once 21 | 22 | // CONTROL_BOARD_TYPE's 23 | #define CONTROL_BOARD_HK_MULTIWII_PRO_2 1 24 | #define CONTROL_BOARD_HK_MULTIWII_328P 2 25 | #define CONTROL_BOARD_HK_NANOWII 3 26 | #define CONTROL_BOARD_HK_POCKET_QUAD 4 27 | #define CONTROL_BOARD_SIRIUS_AIR 5 28 | #define CONTROL_BOARD_SIRIUS_AIR_GPS 6 29 | #define CONTROL_BOARD_SIRIUS_PARIS_V4 7 30 | #define CONTROL_BOARD_WITESPY_FLIP 8 31 | #define CONTROL_BOARD_WITESPY_MULTIWII_PRO_2 9 32 | #define CONTROL_BOARD_WITESPY_MULTIWII_PRO_2_GPS 10 33 | #define CONTROL_BOARD_WLT_V202 100 34 | #define CONTROL_BOARD_JXD_JD385 101 35 | #define CONTROL_BOARD_HUBSAN_H107L 102 36 | #define CONTROL_BOARD_HUBSAN_Q4 103 37 | 38 | // MICROCONTROLLER_TYPE's 39 | #define MEGA2560 0 40 | #define MEGA328P 1 41 | #define MEGA32U4 2 42 | 43 | // AIRCRAFT_TYPE's (need to stay in this order for multi-wii config) 44 | #define NOTYPE 0 45 | #define TRI 1 46 | #define QUADP 2 47 | #define QUADX 3 48 | #define BI 4 49 | #define GIMBAL 5 50 | #define Y6 6 51 | #define HEX6 7 52 | #define FLYING_WING 8 53 | #define Y4 9 54 | #define HEX6X 10 55 | #define OCTOX8 11 56 | #define OCTOFLATP 12 57 | #define OCTOFLATX 13 58 | #define AIRPLANE 14 59 | #define HELI_120_CCPM 15 60 | #define HELI_90_DEG 16 61 | #define VTAIL4 17 62 | 63 | // GYRO_TYPE's 64 | #define ITG3200 1 65 | #define MPU6050 2 66 | #define MPU3050 3 67 | 68 | // ACCELEROMETER_TYPE's 69 | #define BMA180 1 70 | #define MPU6050 2 71 | #define MC3210 3 72 | 73 | //DEFINE ADCs: 74 | #define NO_ADC 0 75 | 76 | // GPS_TYPE's 77 | #define NO_GPS 0 78 | #define SERIAL_GPS 1 79 | #define I2C_GPS 2 80 | 81 | // COMPASS_TYPE's 82 | #define NO_COMPASS 0 83 | #define HMC5883 1 84 | #define HMC5883_VIA_MPU6050 2 85 | #define HMC5843 3 86 | #define MAG3110 4 87 | 88 | // BAROMETER_TYPE's 89 | #define NO_BAROMETER 0 90 | #define BMP085 1 91 | #define MS5611 2 92 | 93 | // MULTIWII_CONFIG_SERIAL_PORTS 94 | // These can be added (or or'ed together) to choose muliple ports 95 | #define NOSERIALPORT 0 96 | #define SERIALPORT0 1 97 | #define SERIALPORT1 2 98 | #define SERIALPORT2 4 99 | #define SERIALPORT3 8 100 | #define SERIALPORTUSB 16 101 | 102 | // RX receiver types 103 | #define RX_NORMAL 0 104 | #define RX_DSM2_1024 1 105 | #define RX_DSM2_2048 2 106 | #define RX_CPPM 3 107 | #define RX_SPI_PROTOCOL 100 108 | #define RX_SOFT_3_WIRE_PROTOCOL 101 109 | 110 | // Stick Command stick positions 111 | #define STICK_COMMAND_ROLL_LOW (1<<0) 112 | #define STICK_COMMAND_ROLL_HIGH (1<<1) 113 | #define STICK_COMMAND_PITCH_LOW (1<<2) 114 | #define STICK_COMMAND_PITCH_HIGH (1<<3) 115 | #define STICK_COMMAND_YAW_LOW (1<<4) 116 | #define STICK_COMMAND_YAW_HIGH (1<<5) 117 | 118 | // general yes/no (used for MOTOR_STOP) 119 | #define YES 1 120 | #define NO 0 121 | 122 | // EEPROM 123 | #define NO_EEPROM 0 124 | -------------------------------------------------------------------------------- /src/output.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "output.h" 19 | #include "projectsettings.h" 20 | #include "bradwii.h" 21 | #include "lib_timers.h" 22 | #include "drv_pwm.h" 23 | 24 | extern globalstruct global; 25 | 26 | #ifdef DC_MOTORS 27 | // for dc motors, we reduce the top so that we can switch at 8khz 28 | #define TOPMOTORCOUNT16BIT 0x3FF 29 | #define TOPMOTORCOUNT11BIT 0x3FF 30 | #define PRESCALER11BIT PWM411BITPRESCALER1 31 | #else 32 | // for speed controllers, we switch at about 490Hz 33 | #define TOPMOTORCOUNT16BIT 0x3FFF 34 | #define TOPMOTORCOUNT11BIT 0x7FF // top is smaller, but we use a bigger prescaller so the cycle time is correct 35 | #define PRESCALER11BIT PWM411BITPRESCALER16 36 | #endif 37 | 38 | void initoutputs(void) 39 | { 40 | setallmotoroutputs(MIN_MOTOR_OUTPUT); 41 | } 42 | 43 | void setmotoroutput(unsigned char motornum, unsigned char motorchannel, fixedpointnum fpvalue) 44 | { 45 | // set the output of a motor 46 | // convert from fixedpoint 0 to 1 into int 1000 to 2000 47 | int value = 1000 + ((fpvalue * 1000L) >> FIXEDPOINTSHIFT); 48 | 49 | if (value < ARMED_MIN_MOTOR_OUTPUT) 50 | value = ARMED_MIN_MOTOR_OUTPUT; 51 | if (value > MAX_MOTOR_OUTPUT) 52 | value = MAX_MOTOR_OUTPUT; 53 | setoutput(motorchannel, value); 54 | 55 | global.motoroutputvalue[motornum] = value; 56 | } 57 | 58 | void setallmotoroutputs(int value) 59 | { 60 | int x; 61 | for (x = 0; x < NUMMOTORS; ++x) { 62 | global.motoroutputvalue[x] = value; 63 | setoutput(x, value); 64 | } 65 | } 66 | 67 | void setoutput(unsigned char outputchannel, unsigned int value) 68 | { 69 | // value is from 1000 to 2000 70 | pwmWriteMotor(outputchannel, value); 71 | } 72 | -------------------------------------------------------------------------------- /src/output.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "lib_fp.h" 19 | 20 | #define OUTPUT_TIMER1 0x10 21 | #define OUTPUT_TIMER2 0x20 22 | #define OUTPUT_TIMER3 0x30 23 | #define OUTPUT_TIMER4 0x40 24 | #define OUTPUT_TIMER5 0x50 25 | #define OUTPUT_CHANNELA 0x01 26 | #define OUTPUT_CHANNELB 0x02 27 | #define OUTPUT_CHANNELC 0X03 28 | #define OUTPUT_CHANNELD 0X04 29 | 30 | void initoutputs(void); 31 | //void setoutputs(unsigned int *values,char numvalues); 32 | void setoutput(unsigned char outputchannel, unsigned int value); 33 | void setmotoroutput(unsigned char motornum, unsigned char motorchannel, fixedpointnum fpvalue); 34 | void setallmotoroutputs(int value); 35 | -------------------------------------------------------------------------------- /src/pilotcontrol.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "lib_fp.h" 19 | 20 | void resetpilotcontrol(void); 21 | void getangleerrorfrompilotinput(fixedpointnum * angleerror); 22 | -------------------------------------------------------------------------------- /src/projectsettings.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "defs.h" 19 | 20 | // this file defines the values needed by my libraries 21 | 22 | 23 | // Serial Ports 24 | 25 | #if (MULTIWII_CONFIG_SERIAL_PORTS & SERIALPORT0) 26 | #define USESERIALPORT0 27 | #define SERIAL0OUTPUTBUFFERSIZE 150 28 | #define SERIAL0INPUTBUFFERSIZE 64 29 | #endif 30 | 31 | #if (MULTIWII_CONFIG_SERIAL_PORTS & SERIALPORT1) 32 | #define USESERIALPORT1 33 | #define SERIAL1OUTPUTBUFFERSIZE 150 34 | #define SERIAL1INPUTBUFFERSIZE 64 35 | #endif 36 | 37 | #if (MULTIWII_CONFIG_SERIAL_PORTS & SERIALPORT2) 38 | #define USESERIALPORT2 39 | #define SERIAL2OUTPUTBUFFERSIZE 150 40 | #define SERIAL2INPUTBUFFERSIZE 64 41 | #endif 42 | 43 | #if (MULTIWII_CONFIG_SERIAL_PORTS & SERIALPORT3) 44 | #define USESERIALPORT3 45 | #define SERIAL3OUTPUTBUFFERSIZE 150 46 | #define SERIAL3INPUTBUFFERSIZE 64 47 | #endif 48 | 49 | #if (RX_TYPE==RX_DSM2_1024 || RX_TYPE==RX_DSM2_2048) 50 | 51 | #if (RX_DSM2_SERIAL_PORT==0) 52 | #define USESERIALPORT0 53 | #define SERIAL0OUTPUTBUFFERSIZE 1 54 | #define SERIAL0INPUTBUFFERSIZE 1 55 | #endif 56 | 57 | #if (RX_DSM2_SERIAL_PORT==1) 58 | #define USESERIALPORT1 59 | #define SERIAL1OUTPUTBUFFERSIZE 1 60 | #define SERIAL1INPUTBUFFERSIZE 1 61 | #endif 62 | 63 | #if (RX_DSM2_SERIAL_PORT==2) 64 | #define USESERIALPORT2 65 | #define SERIAL2OUTPUTBUFFERSIZE 1 66 | #define SERIAL2INPUTBUFFERSIZE 1 67 | #endif 68 | 69 | #if (RX_DSM2_SERIAL_PORT==3) 70 | #define USESERIALPORT3 71 | #define SERIAL3OUTPUTBUFFERSIZE 1 72 | #define SERIAL3INPUTBUFFERSIZE 1 73 | #endif 74 | 75 | #endif 76 | 77 | 78 | #if (MULTIWII_CONFIG_SERIAL_PORTS & SERIALPORTUSB) 79 | #define USESERIALPORTUSB 80 | #endif 81 | 82 | #if (GPS_TYPE==SERIAL_GPS) 83 | 84 | #if (GPS_SERIAL_PORT==0) 85 | #define USESERIALPORT0 86 | #define SERIAL0OUTPUTBUFFERSIZE 1 87 | #define SERIAL0INPUTBUFFERSIZE 100 88 | #endif 89 | 90 | #if (GPS_SERIAL_PORT==1) 91 | #define USESERIALPORT1 92 | #define SERIAL1OUTPUTBUFFERSIZE 1 93 | #define SERIAL1INPUTBUFFERSIZE 100 94 | #endif 95 | 96 | #if (GPS_SERIAL_PORT==2) 97 | #define USESERIALPORT2 98 | #define SERIAL2OUTPUTBUFFERSIZE 1 99 | #define SERIAL2INPUTBUFFERSIZE 100 100 | #endif 101 | 102 | #if (GPS_SERIAL_PORT==3) 103 | #define USESERIALPORT3 104 | #define SERIAL3OUTPUTBUFFERSIZE 1 105 | #define SERIAL3INPUTBUFFERSIZE 100 106 | #endif 107 | 108 | #endif 109 | 110 | #if (MICROCONTROLLER_TYPE==MEGA2560) 111 | #define USEPWM3 112 | #define USEPWM4 113 | #define USEDIGITALPORTB 114 | #define USEDIGITALPORTC 115 | #define USEDIGITALPORTK 116 | 117 | #define USEDIGITALPORTE // for PWM 118 | #define USEDIGITALPORTH // for PWM 119 | 120 | // timer 1 is free, use it for better resolution and less overhead 121 | #define USETIMER1FORGENERALTIMER 122 | 123 | #elif (MICROCONTROLLER_TYPE==MEGA328P) 124 | #define USEPWM1 125 | #define USEPWM2 126 | #define USEDIGITALPORTB 127 | #define USEDIGITALPORTD 128 | #if (RXNUMCHANNELS>6) 129 | #define USEDIGITALPORTC 130 | #endif 131 | 132 | #elif (MICROCONTROLLER_TYPE==MEGA32U4) 133 | #define USEPWM1 134 | #define USEPWM3 135 | #define USEPWM411BIT 136 | #define USEDIGITALPORTB 137 | #define USEDIGITALPORTC 138 | #define USEDIGITALPORTD 139 | #define USEDIGITALPORTE 140 | #endif 141 | -------------------------------------------------------------------------------- /src/rx.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | Some of this code is based on Multiwii code by Alexandre Dubus (www.multiwii.com) 5 | 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | any later version. 10 | 11 | This program is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #include "lib_fp.h" 21 | 22 | void initrx(void); 23 | void readrx(void); 24 | 25 | #define THROTTLE_RX_TIMER FIRSTRXTIMER 26 | #define ROLL_RX_TIMER FIRSTRXTIMER+1 27 | #define PITCH_RX_TIMER FIRSTRXTIMER+2 28 | #define YAW_RX_TIMER FIRSTRXTIMER+3 29 | #define AUX1_RX_TIMER FIRSTRXTIMER+4 30 | #define AUX2_RX_TIMER FIRSTRXTIMER+5 31 | #if (RXNUMCHANNELS>6) 32 | #define AUX3_RX_TIMER FIRSTRXTIMER+6 33 | #define AUX4_RX_TIMER FIRSTRXTIMER+7 34 | #endif 35 | 36 | // convert from 1000-2000 range to fixedpointnum -1 to 1 37 | #define FPMINMOTOROUTPUT (((fixedpointnum)MIN_MOTOR_OUTPUT-1500)<<7) 38 | #define FPMAXMOTOROUTPUT (((fixedpointnum)MAX_MOTOR_OUTPUT-1500)<<7) 39 | #define FPFAILSAFEMOTOROUTPUT (((fixedpointnum)FAILSAFE_MOTOR_OUTPUT-1500)<<7) 40 | #define FPAUXMIDRANGELOW (((fixedpointnum)AUX_MID_RANGE_LOW-1500)<<7) 41 | #define FPAUXMIDRANGEHIGH (((fixedpointnum)AUX_MID_RANGE_HIGH-1500)<<7) 42 | #define FPTHROTTLELOW (((fixedpointnum)900-1500)<<7) 43 | #define FPTHROTTLETOMOTOROFFSET (((fixedpointnum) THROTTLE_TO_MOTOR_OFFSET)<<7) 44 | 45 | #define FPSTICKLOW (((fixedpointnum)STICK_RANGE_LOW-1500)<<7) 46 | #define FPSTICKHIGH (((fixedpointnum)STICK_RANGE_HIGH-1500)<<7) 47 | -------------------------------------------------------------------------------- /src/serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | Some of this code is based on Multiwii code by Alexandre Dubus (www.multiwii.com) 5 | 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | any later version. 10 | 11 | This program is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | void serialinit(void); 21 | void serialcheckforaction(void); 22 | 23 | // Multiwii Serial Protocol 0 24 | #define MSP_VERSION 0 25 | 26 | #define MSP_IDENT 100 //out message multitype + multiwii version + protocol version + capability variable 27 | #define MSP_STATUS 101 //out message cycletime & errors_count & sensor present & box activation 28 | #define MSP_RAW_IMU 102 //out message 9 DOF 29 | #define MSP_SERVO 103 //out message 8 servos 30 | #define MSP_MOTOR 104 //out message 8 motors 31 | #define MSP_RC 105 //out message 8 rc chan 32 | #define MSP_RAW_GPS 106 //out message fix, numsat, lat, lon, alt, speed 33 | #define MSP_COMP_GPS 107 //out message distance home, direction home 34 | #define MSP_ATTITUDE 108 //out message 2 angles 1 heading 35 | #define MSP_ALTITUDE 109 //out message 1 altitude 36 | #define MSP_BAT 110 //out message vbat, powermetersum 37 | #define MSP_RC_TUNING 111 //out message rc rate, rc expo, rollpitch rate, yaw rate, dyn throttle PID 38 | #define MSP_PID 112 //out message up to 16 P I D (8 are used) 39 | #define MSP_BOX 113 //out message up to 16 checkbox (11 are used) 40 | #define MSP_MISC 114 //out message powermeter trig + 8 free for future use 41 | #define MSP_MOTOR_PINS 115 //out message which pins are in use for motors & servos, for GUI 42 | #define MSP_BOXNAMES 116 //out message the aux switch names 43 | #define MSP_PIDNAMES 117 //out message the PID names 44 | #define MSP_WP 118 //out message get a WP, WP# is in the payload, returns (WP#, lat, lon, alt, flags) WP#0-home, WP#16-poshold 45 | 46 | #define MSP_SET_RAW_RC 200 //in message 8 rc chan 47 | #define MSP_SET_RAW_GPS 201 //in message fix, numsat, lat, lon, alt, speed 48 | #define MSP_SET_PID 202 //in message up to 16 P I D (8 are used) 49 | #define MSP_SET_BOX 203 //in message up to 16 checkbox (11 are used) 50 | #define MSP_SET_RC_TUNING 204 //in message rc rate, rc expo, rollpitch rate, yaw rate, dyn throttle PID 51 | #define MSP_ACC_CALIBRATION 205 //in message no param 52 | #define MSP_MAG_CALIBRATION 206 //in message no param 53 | #define MSP_SET_MISC 207 //in message powermeter trig + 8 free for future use 54 | #define MSP_RESET_CONF 208 //in message no param 55 | #define MSP_WP_SET 209 //in message sets a given WP (WP#,lat, lon, alt, flags) 56 | 57 | #define MSP_EEPROM_WRITE 250 //in message no param 58 | 59 | #define MSP_DEBUG 254 //out message debug1,debug2,debug3,debug4 60 | -------------------------------------------------------------------------------- /src/vectors.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Brad Quick 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #pragma once 19 | 20 | #include "lib_fp.h" 21 | 22 | // an attitude is defined as first a rotation from north, then a 3d vector that points down (gravity) from the rotated reference. 23 | typedef struct { 24 | fixedpointnum westvector[3]; 25 | fixedpointnum downvector[3]; 26 | } attitudestruct; 27 | 28 | void vectorcrossproduct(fixedpointnum * v1, fixedpointnum * v2, fixedpointnum * v3); 29 | fixedpointnum normalizevector(fixedpointnum * v); 30 | fixedpointnum vectordotproduct(fixedpointnum * v1, fixedpointnum * v2); 31 | void vectordifferencetoeulerangles(fixedpointnum * v1, fixedpointnum * v2, fixedpointnum * euler); 32 | void attitudetoeulerangles(attitudestruct * theattitude, fixedpointnum * eulerangles); 33 | void rotatevectorwithsmallangles(fixedpointnum * v, fixedpointnum rolldeltaangle, fixedpointnum pitchdeltaangle, fixedpointnum yawdeltaangle); 34 | void rotatevectorbyaxisangle(fixedpointnum * v1, fixedpointnum * axisvector, fixedpointnum angle, fixedpointnum * v2); 35 | void rotatevectorbyaxissmallangle(fixedpointnum * v1, fixedpointnum * axisvector, fixedpointnum angle); 36 | --------------------------------------------------------------------------------