├── .gitattributes
├── .gitignore
├── 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_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
└── 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
└── 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.c
├── config_X4.h
├── defs.h
├── eeprom.c
├── eeprom.h
├── gps.c
├── gps.h
├── gyro.c
├── gyro.h
├── imu.c
├── imu.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 |
--------------------------------------------------------------------------------
/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 | Hopefully it also will allow us to port it to similar ARM platform, Nuvoton M051,
12 | which is represented by HiSky HMX120 (former FF120) and its rebadge HobbyKing Q-BOT micro.
13 |
14 | It uses M058ZBN ARM MCU with 32KB ROM and 4KB RAM, gyro InvenSense MPU-3050, supposedly
15 | accelerometer Freescale MMA8452Q (maybe ST LIS3DH), and nRF24L01 clone Si24R1.
16 |
17 | Datasheets for HMX120 are at http://www.rcgroups.com/forums/showthread.php?t=1826018&page=51#post24710038
18 |
19 | Credits
20 | ======
21 |
22 | * Bradwii was originally coded by Brad Quick for AVR: https://github.com/bradquick/bradwii
23 | * Trollcop forked and ported to ARM STM32, untested: https://github.com/trollcop/bradwii
24 | * The Mini54ZAN ARM port to V202/JD385 was done by Victor: https://github.com/victzh/bradwii
25 | * The Hubsan X4 H107L port was done by Goebish: https://github.com/goebish/bradwii-X4
26 |
--------------------------------------------------------------------------------
/lib-Mini51/CMSIS/CMSIS END USER LICENCE AGREEMENT.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackocopter/bradwii-jd385/8c285ef936c33b2c9d8099615d9c1b23867e0ead/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/hackocopter/bradwii-jd385/8c285ef936c33b2c9d8099615d9c1b23867e0ead/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 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
73 |
--------------------------------------------------------------------------------
/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 void 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_hal.c:
--------------------------------------------------------------------------------
1 | #include "hal.h"
2 | #include "drv_serial.h"
3 | #include "drv_pwm.h"
4 | #include "drv_gpio.h"
5 |
6 | #include "lib_timers.h"
7 | #include "lib_i2c.h"
8 | #include "lib_spi.h"
9 |
10 | #include "config.h"
11 | #include "defs.h"
12 |
13 | #define FLASH_PAGE_SIZE ((uint16_t)0x200)
14 | #define FLASH_WRITE_ADDR (16*1024 - FLASH_PAGE_SIZE) // use the last 0.5 KB for storage
15 | #define EEP_SIZE (FLASH_PAGE_SIZE)
16 |
17 | static bool flash_available = false;
18 | static uint32_t last_write_index = FLASH_PAGE_SIZE;
19 |
20 | static int set_data_flash_base(uint32_t u32DFBA);
21 |
22 | void lib_hal_init(void)
23 | {
24 | SYS_UnlockReg();
25 |
26 | // Init System Clock
27 |
28 | // Enable internal 22.1184MHz
29 | CLK->PWRCON |= CLK_PWRCON_IRC22M_EN_Msk;
30 |
31 | // Waiting for clock ready
32 | CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk);
33 |
34 | /* Switch HCLK clock source to XTL */
35 | // CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_XTAL,CLK_CLKDIV_HCLK(1));
36 | // Stay with reset value = internal oscillator
37 |
38 | /* STCLK to XTL STCLK to XTL */
39 | // CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLK_S_XTAL);
40 | // Stay with reset value = internal oscillator
41 |
42 | // Update SystemCoreClock and CyclesPerUs
43 | SystemCoreClockUpdate();
44 |
45 | lib_timers_init();
46 |
47 | FMC_Open();
48 | if (set_data_flash_base(FLASH_WRITE_ADDR) == 0)
49 | {
50 | flash_available = true;
51 | }
52 | FMC_Close();
53 |
54 | // Init UART clock
55 | #if (MULTIWII_CONFIG_SERIAL_PORTS != NOSERIALPORT)
56 | uartInit();
57 | #endif
58 |
59 | // Init PWM clock and hardware
60 | drv_pwm_config_t pwm;
61 | pwmInit(&pwm);
62 |
63 | lib_i2c_init();
64 | #if (RX_TYPE==RX_SPI_PROTOCOL)
65 | lib_spi_init();
66 | #endif
67 |
68 | SYS_LockReg();
69 | }
70 |
71 | ///////////////////////////////////////////////////////////////////////
72 | // EEPROM
73 |
74 | // Mask for alignment by word boundary
75 | #define ALIGN_SIZE sizeof(int)
76 | #define ALIGN_MASK (ALIGN_SIZE - 1)
77 |
78 |
79 | size_t eeprom_write_block(const void *src, uint16_t index, size_t size)
80 | {
81 | if (!flash_available) return 0;
82 | SYS_UnlockReg();
83 | FMC_Open();
84 |
85 | if (index < last_write_index) {
86 | FMC_Erase(FLASH_WRITE_ADDR);
87 | }
88 | uint8_t *read_src = (uint8_t *) src;
89 | uint32_t addr = (FLASH_WRITE_ADDR + index) & ~ALIGN_MASK;
90 | int i = index & ALIGN_MASK;
91 | uint32_t data;
92 | if (i) {
93 | data = FMC_Read(addr);
94 | }
95 | uint32_t endAddr = (FLASH_WRITE_ADDR + index + size + (ALIGN_SIZE-1)) & ~ALIGN_MASK;
96 | for (; addr < endAddr; addr += ALIGN_SIZE)
97 | {
98 | int l = FLASH_WRITE_ADDR + index + size - addr;
99 | if (l > ALIGN_SIZE) l = ALIGN_SIZE;
100 | if (l != ALIGN_SIZE) {
101 | data = FMC_Read(addr);
102 | }
103 | for (; i < l;) {
104 | ((uint8_t *) &data)[i++] = *read_src++;
105 | }
106 | FMC_Write(addr, data);
107 | i = 0;
108 | }
109 |
110 | FMC_Close();
111 | SYS_LockReg();
112 | last_write_index = index + size;
113 |
114 | return size;
115 | }
116 |
117 | void eeprom_commit(void)
118 | {
119 | }
120 |
121 | size_t eeprom_read_block(void *dst, uint16_t index, size_t size)
122 | {
123 | if (!flash_available) return 0;
124 | SYS_UnlockReg();
125 | FMC_Open();
126 |
127 | uint8_t *write_dst = (uint8_t *) dst;
128 | uint32_t addr = (FLASH_WRITE_ADDR + index) & ~ALIGN_MASK;
129 | int i = index & ALIGN_MASK;
130 | uint32_t endAddr = (FLASH_WRITE_ADDR + index + size + (ALIGN_SIZE-1)) & ~ALIGN_MASK;
131 | for (; addr < endAddr; addr += ALIGN_SIZE)
132 | {
133 | uint32_t data = FMC_Read(addr);
134 | int l = FLASH_WRITE_ADDR + index + size - addr;
135 | if (l > ALIGN_SIZE) l = ALIGN_SIZE;
136 | for (; i < l;) {
137 | *write_dst++ = ((uint8_t *) &data)[i++];
138 | }
139 | i = 0;
140 | }
141 |
142 | FMC_Close();
143 | SYS_LockReg();
144 |
145 | return size;
146 | }
147 |
148 |
149 | // Copied from Nuvoton's sample code
150 | static int set_data_flash_base(uint32_t u32DFBA)
151 | {
152 | uint32_t au32Config[2];
153 |
154 | if (FMC_ReadConfig(au32Config, 2) < 0)
155 | {
156 | return -1;
157 | }
158 |
159 | if ((!(au32Config[0] & 0x1)) && (au32Config[1] == u32DFBA))
160 | return 0;
161 |
162 | FMC_EnableConfigUpdate();
163 |
164 | if (u32DFBA !=0)
165 | au32Config[0] &= ~0x1;
166 | else
167 | au32Config[0] |= 0x1;
168 | au32Config[1] = u32DFBA;
169 |
170 | if (FMC_WriteConfig(au32Config, 2) < 0)
171 | return -1;
172 |
173 | // Perform chip reset to make new User Config take effect
174 | SYS->IPRSTC1 = SYS_IPRSTC1_CHIP_RST_Msk;
175 | // This is to pacify compiler - it never reaches this point
176 | return 0;
177 | }
178 |
--------------------------------------------------------------------------------
/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)
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
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 | // USART2 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_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 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 reg,unsigned char *data,unsigned char length);
38 |
39 |
--------------------------------------------------------------------------------
/lib-Mini51/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 | #include "drv_serial.h"
36 |
37 | static serialPort_t *lib_serial_getport(unsigned char serialportnumber)
38 | {
39 | switch (serialportnumber) {
40 | case 0:
41 | return &serialPort1;
42 | // case 2:
43 | // return &serialPort2;
44 | default:
45 | return NULL;
46 | }
47 | }
48 |
49 | int lib_serial_availableoutputbuffersize(unsigned char serialportnumber)
50 | {
51 | // returns how many more bytes can fit in the outputbuffer
52 | return 256; // TODO who cares
53 | }
54 |
55 | // TODO: not implemented
56 | // used only in rx.c for decoding of some receivers
57 | //void lib_serial_setrxcallback(unsigned char serialportnumber, serialcallbackfunctptr callback)
58 | //{
59 | // serialPort_t *port = lib_serial_getport(serialportnumber);
60 | // port->callback = callback;
61 | //}
62 |
63 | void lib_serial_initport(unsigned char serialportnumber, long baud)
64 | {
65 | // initialize the serial port and set up a read buffer and interrupts so that we don't lose any data from reading too slowly
66 | switch (serialportnumber) {
67 | // SERIALPORT0
68 | case 0:
69 | serialOpen(UART, NULL, baud, MODE_RXTX);
70 | break;
71 | // SERIALPORT1 - no such thing on Mini51
72 | // case 2:
73 | // serialOpen(USART2, NULL, baud, MODE_RX);
74 | // break;
75 | }
76 | }
77 |
78 | void lib_serial_sendchar(unsigned char serialportnumber, unsigned char c)
79 | {
80 | // add a character to the send buffer
81 | serialPort_t *port = lib_serial_getport(serialportnumber);
82 | uartWrite(port, c);
83 | }
84 |
85 | void lib_serial_sendstring(unsigned char serialportnumber, char *string)
86 | {
87 | // adds the string to the output buffer.
88 | while (*string)
89 | lib_serial_sendchar(serialportnumber, *string++);
90 | }
91 |
92 | void lib_serial_senddata(unsigned char serialportnumber, unsigned char *data, int datalength)
93 | {
94 | // send datalength bytes of data to the serial port
95 | while (datalength-- >0)
96 | lib_serial_sendchar(serialportnumber, *data++);
97 | }
98 |
99 | int lib_serial_numcharsavailable(unsigned char serialportnumber)
100 | {
101 | // returns number of characters available in the rx buffer
102 | serialPort_t *port = lib_serial_getport(serialportnumber);
103 | return uartAvailable(port);
104 | }
105 |
106 | unsigned char lib_serial_getchar(unsigned char serialportnumber)
107 | {
108 | // get the next character from the serial port
109 | serialPort_t *port = lib_serial_getport(serialportnumber);
110 | return uartRead(port);
111 | }
112 |
113 | void lib_serial_getdata(unsigned char serialportnumber, unsigned char *data, int numchars)
114 | {
115 | int x;
116 | for (x = 0; x < numchars; ++x)
117 | *data++ = lib_serial_getchar(serialportnumber);
118 | }
119 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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/CMSIS/CM3/DeviceSupport/ST/STM32F10x/stm32f10x.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackocopter/bradwii-jd385/8c285ef936c33b2c9d8099615d9c1b23867e0ead/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.c:
--------------------------------------------------------------------------------
1 | #include "stm32f10x.h"
2 |
3 | #define SYSCLK_FREQ_72MHz 72000000
4 |
5 | uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */
6 |
7 | __I uint8_t AHBPrescTable[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9 };
8 |
9 | uint32_t hse_value = 8000000;
10 |
11 | void SystemInit(void)
12 | {
13 | /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
14 | /* Set HSION bit */
15 | RCC->CR |= (uint32_t) 0x00000001;
16 |
17 | /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
18 | RCC->CFGR &= (uint32_t) 0xF8FF0000;
19 |
20 | /* Reset HSEON, CSSON and PLLON bits */
21 | RCC->CR &= (uint32_t) 0xFEF6FFFF;
22 |
23 | /* Reset HSEBYP bit */
24 | RCC->CR &= (uint32_t) 0xFFFBFFFF;
25 |
26 | /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
27 | RCC->CFGR &= (uint32_t) 0xFF80FFFF;
28 |
29 | /* Disable all interrupts and clear pending bits */
30 | RCC->CIR = 0x009F0000;
31 |
32 | SCB->VTOR = FLASH_BASE; /* Vector Table Relocation in Internal FLASH. */
33 | }
34 |
35 | void SystemCoreClockUpdate(void)
36 | {
37 | uint32_t tmp = 0, pllmull = 0, pllsource = 0;
38 |
39 | /* Get SYSCLK source ------------------------------------------------------- */
40 | tmp = RCC->CFGR & RCC_CFGR_SWS;
41 |
42 | switch (tmp) {
43 | case 0x00: /* HSI used as system clock */
44 | SystemCoreClock = HSI_VALUE;
45 | break;
46 | case 0x04: /* HSE used as system clock */
47 | SystemCoreClock = hse_value;
48 | break;
49 | case 0x08: /* PLL used as system clock */
50 |
51 | /* Get PLL clock source and multiplication factor ---------------------- */
52 | pllmull = RCC->CFGR & RCC_CFGR_PLLMULL;
53 | pllsource = RCC->CFGR & RCC_CFGR_PLLSRC;
54 |
55 | pllmull = (pllmull >> 18) + 2;
56 |
57 | if (pllsource == 0x00) {
58 | /* HSI oscillator clock divided by 2 selected as PLL clock entry */
59 | SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
60 | } else {
61 | /* HSE selected as PLL clock entry */
62 | if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t) RESET) { /* HSE oscillator clock divided by 2 */
63 | SystemCoreClock = (hse_value >> 1) * pllmull;
64 | } else {
65 | SystemCoreClock = hse_value * pllmull;
66 | }
67 | }
68 | break;
69 |
70 | default:
71 | SystemCoreClock = HSI_VALUE;
72 | break;
73 | }
74 |
75 | /* Compute HCLK clock frequency ---------------- */
76 | /* Get HCLK prescaler */
77 | tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
78 | /* HCLK clock frequency */
79 | SystemCoreClock >>= tmp;
80 | }
81 |
82 | enum {
83 | SRC_NONE = 0,
84 | SRC_HSI,
85 | SRC_HSE
86 | };
87 |
88 | // Set system clock to 72 (HSE) or 64 (HSI) MHz
89 | void SetSysClock(void)
90 | {
91 | __IO uint32_t StartUpCounter = 0, status = 0, clocksrc = SRC_NONE;
92 | __IO uint32_t *RCC_CRH = &GPIOC->CRH;
93 | __IO uint32_t RCC_CFGR_PLLMUL = RCC_CFGR_PLLMULL9;
94 |
95 | // First, try running off HSE
96 | RCC->CR |= ((uint32_t)RCC_CR_HSEON);
97 | RCC->APB2ENR |= RCC_CFGR_HPRE_0;
98 |
99 | // Wait till HSE is ready
100 | do {
101 | status = RCC->CR & RCC_CR_HSERDY;
102 | StartUpCounter++;
103 | } while ((status == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
104 |
105 | if ((RCC->CR & RCC_CR_HSERDY) != RESET) {
106 | // external xtal started up, we're good to go
107 | clocksrc = SRC_HSE;
108 | } else {
109 | // If HSE fails to start-up, try to enable HSI and configure for 64MHz operation
110 | RCC->CR |= ((uint32_t)RCC_CR_HSION);
111 | StartUpCounter = 0;
112 | do {
113 | status = RCC->CR & RCC_CR_HSIRDY;
114 | StartUpCounter++;
115 | } while ((status == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
116 | if ((RCC->CR & RCC_CR_HSIRDY) != RESET) {
117 | // we're on internal RC
118 | clocksrc = SRC_HSI;
119 | } else {
120 | // We're fucked
121 | while(1);
122 | }
123 | }
124 |
125 | // Enable Prefetch Buffer
126 | FLASH->ACR |= FLASH_ACR_PRFTBE;
127 | // Flash 2 wait state
128 | FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
129 | FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
130 | // HCLK = SYSCLK
131 | RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
132 | // PCLK2 = HCLK
133 | RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
134 | // PCLK1 = HCLK
135 | RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
136 | *RCC_CRH &= (uint32_t)~((uint32_t)0xF << (RCC_CFGR_PLLMULL9 >> 16));
137 |
138 | // Configure PLL
139 | hse_value = 8000000;
140 | RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
141 | *RCC_CRH |= (uint32_t)0x8 << (RCC_CFGR_PLLMULL9 >> 16);
142 | GPIOC->ODR &= (uint32_t)~(CAN_MCR_RESET);
143 | RCC_CFGR_PLLMUL = GPIOC->IDR & CAN_MCR_RESET ? hse_value = 12000000, RCC_CFGR_PLLMULL6 : RCC_CFGR_PLLMULL9;
144 | switch (clocksrc) {
145 | case SRC_HSE:
146 | // PLL configuration: PLLCLK = HSE * 9 = 72 MHz
147 | RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMUL);
148 | break;
149 | case SRC_HSI:
150 | // PLL configuration: PLLCLK = HSI / 2 * 16 = 64 MHz
151 | RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLMULL16);
152 | break;
153 | }
154 |
155 | // Enable PLL
156 | RCC->CR |= RCC_CR_PLLON;
157 | // Wait till PLL is ready
158 | while ((RCC->CR & RCC_CR_PLLRDY) == 0);
159 | // Select PLL as system clock source
160 | RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
161 | RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
162 | // Wait till PLL is used as system clock source
163 | while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08);
164 |
165 | SystemCoreClockUpdate();
166 | }
167 |
--------------------------------------------------------------------------------
/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/hackocopter/bradwii-jd385/8c285ef936c33b2c9d8099615d9c1b23867e0ead/lib/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c
--------------------------------------------------------------------------------
/lib/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hackocopter/bradwii-jd385/8c285ef936c33b2c9d8099615d9c1b23867e0ead/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/hackocopter/bradwii-jd385/8c285ef936c33b2c9d8099615d9c1b23867e0ead/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/hackocopter/bradwii-jd385/8c285ef936c33b2c9d8099615d9c1b23867e0ead/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 |
--------------------------------------------------------------------------------
/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 |
22 | extern globalstruct global;
23 | extern usersettingsstruct usersettings;
24 |
25 | char checkboxnames[] /* PROGMEM */ = // names for dynamic generation of config GUI
26 | // this could be moved to program memory if we wanted to save a few bytes of space.
27 | "Arm;" "Thr. Helper;" "Alt. Hold;" "Mag. Hold;" "Pos. Hold;" "Ret. Home;" "Semi Acro;" "Full Acro;" "High Rates;" "High Angle;" "Auto Tune;" "Uncrashable;" "Headfree;";
28 |
29 | // each checkbox item has a checkboxvalue. The bits in this value represent low, medium, and high checkboxes
30 | // for each of the aux switches, just as they show up in most config programs.
31 | void checkcheckboxitems(void)
32 | {
33 | global.previousactivecheckboxitems = global.activecheckboxitems;
34 | global.activecheckboxitems = 0;
35 |
36 | uint16_t mask = 0; // a mask of what aux states are true
37 | #if (RXNUMCHANNELS>4)
38 | if (global.rxvalues[AUX1INDEX] < FPAUXMIDRANGELOW) // low
39 | mask |= (1 << 0);
40 | else if (global.rxvalues[AUX1INDEX] > FPAUXMIDRANGEHIGH) // high
41 | mask |= (1 << 2);
42 | else
43 | mask |= (1 << 1); // mid
44 | #endif
45 |
46 | #if (RXNUMCHANNELS>5)
47 | if (global.rxvalues[AUX2INDEX] < FPAUXMIDRANGELOW) // low
48 | mask |= (1 << 3);
49 | else if (global.rxvalues[AUX2INDEX] > FPAUXMIDRANGEHIGH) // high
50 | mask |= (1 << 5);
51 | else
52 | mask |= (1 << 4); //mid
53 | #endif
54 |
55 | #if (RXNUMCHANNELS>6)
56 | if (global.rxvalues[AUX3INDEX] < FPAUXMIDRANGELOW) // low
57 | mask |= (1 << 6);
58 | else if (global.rxvalues[AUX3INDEX] > FPAUXMIDRANGEHIGH) // high
59 | mask |= (1 << 8);
60 | else
61 | mask |= (1 << 7); //mid
62 | #endif
63 |
64 | #if (RXNUMCHANNELS>7)
65 | if (global.rxvalues[AUX4INDEX] < FPAUXMIDRANGELOW) // low
66 | mask |= (1 << 9);
67 | else if (global.rxvalues[AUX4INDEX] > FPAUXMIDRANGEHIGH) // high
68 | mask |= (1 << 11);
69 | else
70 | mask |= (1 << 10); //mid
71 | #endif
72 |
73 | for (int x = 0; x < NUMCHECKBOXES; ++x) {
74 | if (usersettings.checkboxconfiguration[x] & mask)
75 | global.activecheckboxitems |= (1 << x);
76 | }
77 |
78 | #if (defined(STICK_ARM) | defined (STICK_DISARM))
79 | // figure out where the sticks are
80 | unsigned int stickmask = 0;
81 | if (global.rxvalues[ROLLINDEX] < FPSTICKLOW)
82 | stickmask |= STICK_COMMAND_ROLL_LOW;
83 | else if (global.rxvalues[ROLLINDEX] > FPSTICKHIGH)
84 | stickmask |= STICK_COMMAND_ROLL_HIGH;
85 |
86 | if (global.rxvalues[PITCHINDEX] < FPSTICKLOW)
87 | stickmask |= STICK_COMMAND_PITCH_LOW;
88 | else if (global.rxvalues[PITCHINDEX] > FPSTICKHIGH)
89 | stickmask |= STICK_COMMAND_PITCH_HIGH;
90 |
91 | if (global.rxvalues[YAWINDEX] < FPSTICKLOW)
92 | stickmask |= STICK_COMMAND_YAW_LOW;
93 | else if (global.rxvalues[YAWINDEX] > FPSTICKHIGH)
94 | stickmask |= STICK_COMMAND_YAW_HIGH;
95 |
96 |
97 | // If the sticks are in the right positions, set the arm or disarm checkbox value
98 | // Start with the previous value in case the sticks aren't doing anything special
99 | global.activecheckboxitems = (global.activecheckboxitems & ~CHECKBOXMASKARM) |(global.previousactivecheckboxitems & CHECKBOXMASKARM);
100 |
101 | if ((stickmask & (STICK_ARM)) == STICK_ARM)
102 | global.activecheckboxitems |= CHECKBOXMASKARM;
103 |
104 | else if ((stickmask & (STICK_DISARM)) == STICK_DISARM)
105 | global.activecheckboxitems &= ~CHECKBOXMASKARM;
106 |
107 | #endif
108 | }
109 |
--------------------------------------------------------------------------------
/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 NUMCHECKBOXES 13
34 |
35 | #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/config_X4.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 "bradwii.h"
19 | #include "config_X4.h"
20 |
21 | extern usersettingsstruct usersettings;
22 |
23 | /* The Hubsan X4 does not have a serial port to connect it
24 | to a GUI such as MultiwiiConfig GUI, settings are configured
25 | from here until a better solution is found */
26 | void x4_set_usersettings()
27 | {
28 | // set acro mode rotation rates
29 | usersettings.maxyawrate = 600L << FIXEDPOINTSHIFT; // degrees per second
30 | usersettings.maxpitchandrollrate = 400L << FIXEDPOINTSHIFT; // degrees per second
31 |
32 | // pitch PIDs
33 | usersettings.pid_pgain[PITCHINDEX] = 15L << 3; // 1.5 on configurator
34 | usersettings.pid_igain[PITCHINDEX] = 8L; // .008 on configurator
35 | usersettings.pid_dgain[PITCHINDEX] = 8L << 2; // 8 on configurator
36 |
37 | // roll PIDs
38 | usersettings.pid_pgain[ROLLINDEX] = 15L << 3; // 1.5 on configurator
39 | usersettings.pid_igain[ROLLINDEX] = 8L; // .008 on configurator
40 | usersettings.pid_dgain[ROLLINDEX] = 8L << 2; // 8 on configurator
41 |
42 | // yaw PIDs
43 | usersettings.pid_pgain[YAWINDEX] = 30L << 3; // 3.0 on configurator
44 | usersettings.pid_igain[YAWINDEX] = 8L; // .008 on configurator
45 | usersettings.pid_dgain[YAWINDEX] = 8L << 2; // 8 on configurator
46 |
47 | for (int x = 0; x < NUMPOSSIBLECHECKBOXES; ++x) {
48 | usersettings.checkboxconfiguration[x] = 0;
49 | }
50 |
51 | // flight modes, see checkboxes.h for a complete list
52 |
53 | // set fullacro flight mode (gyro only) for AUX1 high (LEDs on/off channel on stock TX)
54 | // default for H107L, H107C & H107D stock TXs
55 | usersettings.checkboxconfiguration[CHECKBOXFULLACRO] = CHECKBOXMASKAUX1HIGH; // rate mode (gyro only)
56 | //usersettings.checkboxconfiguration[CHECKBOXHIGHRATES] = CHECKBOXMASKAUX1HIGH; // uncommentr for high rates
57 |
58 | // set semiacro flight mode for AUX1 low
59 | // default for H107 stock TX
60 | usersettings.checkboxconfiguration[CHECKBOXSEMIACRO] = CHECKBOXMASKAUX1LOW;
61 | //usersettings.checkboxconfiguration[CHECKBOXHIGHANGLE] = CHECKBOXMASKAUX1LOW; // uncomment for high angle
62 | }
63 |
64 | void x4_init_leds()
65 | {
66 | lib_digitalio_initpin(LED1_OUTPUT, DIGITALOUTPUT);
67 | lib_digitalio_initpin(LED2_OUTPUT, DIGITALOUTPUT);
68 | lib_digitalio_initpin(LED5_OUTPUT, DIGITALOUTPUT);
69 | lib_digitalio_initpin(LED6_OUTPUT, DIGITALOUTPUT);
70 | }
71 |
72 | void x4_set_leds(unsigned char state)
73 | {
74 | lib_digitalio_setoutput( LED1_OUTPUT , state ? LED1_ON : !LED1_ON);
75 | lib_digitalio_setoutput( LED2_OUTPUT , state ? LED2_ON : !LED2_ON);
76 | lib_digitalio_setoutput( LED5_OUTPUT , state ? LED5_ON : !LED5_ON);
77 | lib_digitalio_setoutput( LED6_OUTPUT , state ? LED6_ON : !LED6_ON);
78 | }
79 |
80 |
--------------------------------------------------------------------------------
/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 | /*
29 | uint16_t magicnumber = MAGICNUMBER;
30 | int16_t size = sizeof(usersettingsstruct);
31 | uint16_t index = 0;
32 |
33 |
34 | index += eeprom_write_block((const void *)&magicnumber, index, sizeof(magicnumber));
35 | index += eeprom_write_block((const void *)&size, index, sizeof(size));
36 | index += eeprom_write_block((const void *)&usersettings, index, size);
37 | eeprom_commit();
38 | */
39 | }
40 |
41 | void readusersettingsfromeeprom(void)
42 | {
43 | /*
44 | uint16_t magicnumber = 0;
45 | int16_t size = 0;
46 | uint16_t index = 0;
47 | index += eeprom_read_block((void *) &magicnumber, index, sizeof(magicnumber));
48 |
49 | if (magicnumber != MAGICNUMBER)
50 | return;
51 |
52 | index += eeprom_read_block((void *) &size, index, sizeof(size));
53 | if (size > sizeof(usersettingsstruct))
54 | size = sizeof(usersettingsstruct);
55 |
56 | eeprom_read_block((void *) &usersettings, index, size);
57 |
58 | global.usersettingsfromeeprom = 1; // set a flag so the rest of the program know it's working with calibtated settings
59 | */
60 | }
61 |
--------------------------------------------------------------------------------
/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 | void initimu(void);
22 | void imucalculateestimatedattitude(void);
23 | void calibrategyroandaccelerometer(void);
24 |
--------------------------------------------------------------------------------
/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 |
37 | // MICROCONTROLLER_TYPE's
38 | #define MEGA2560 0
39 | #define MEGA328P 1
40 | #define MEGA32U4 2
41 |
42 | // AIRCRAFT_TYPE's (need to stay in this order for multi-wii config)
43 | #define NOTYPE 0
44 | #define TRI 1
45 | #define QUADP 2
46 | #define QUADX 3
47 | #define BI 4
48 | #define GIMBAL 5
49 | #define Y6 6
50 | #define HEX6 7
51 | #define FLYING_WING 8
52 | #define Y4 9
53 | #define HEX6X 10
54 | #define OCTOX8 11
55 | #define OCTOFLATP 12
56 | #define OCTOFLATX 13
57 | #define AIRPLANE 14
58 | #define HELI_120_CCPM 15
59 | #define HELI_90_DEG 16
60 | #define VTAIL4 17
61 |
62 | // GYRO_TYPE's
63 | #define ITG3200 1
64 | #define MPU6050 2
65 | #define MPU3050 3
66 |
67 | // ACCELEROMETER_TYPE's
68 | #define BMA180 1
69 | #define MPU6050 2
70 | #define MC3210 3
71 |
72 | // GPS_TYPE's
73 | #define NO_GPS 0
74 | #define SERIAL_GPS 1
75 | #define I2C_GPS 2
76 |
77 | // COMPASS_TYPE's
78 | #define NO_COMPASS 0
79 | #define HMC5883 1
80 | #define HMC5883_VIA_MPU6050 2
81 | #define HMC5843 3
82 | #define MAG3110 4
83 |
84 | // BAROMETER_TYPE's
85 | #define NO_BAROMETER 0
86 | #define BMP085 1
87 | #define MS5611 2
88 |
89 | // MULTIWII_CONFIG_SERIAL_PORTS
90 | // These can be added (or or'ed together) to choose muliple ports
91 | #define NOSERIALPORT 0
92 | #define SERIALPORT0 1
93 | #define SERIALPORT1 2
94 | #define SERIALPORT2 4
95 | #define SERIALPORT3 8
96 | #define SERIALPORTUSB 16
97 |
98 | // RX receiver types
99 | #define RX_NORMAL 0
100 | #define RX_DSM2_1024 1
101 | #define RX_DSM2_2048 2
102 | #define RX_CPPM 3
103 | #define RX_SPI_PROTOCOL 100
104 | #define RX_SOFT_3_WIRE_PROTOCOL 101
105 |
106 | // Stick Command stick positions
107 | #define STICK_COMMAND_ROLL_LOW (1<<0)
108 | #define STICK_COMMAND_ROLL_HIGH (1<<1)
109 | #define STICK_COMMAND_PITCH_LOW (1<<2)
110 | #define STICK_COMMAND_PITCH_HIGH (1<<3)
111 | #define STICK_COMMAND_YAW_LOW (1<<4)
112 | #define STICK_COMMAND_YAW_HIGH (1<<5)
113 |
114 | // general yes/no (used for MOTOR_STOP)
115 | #define YES 1
116 | #define NO 0
117 |
--------------------------------------------------------------------------------
/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.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 "vectors.h"
19 | #include "bradwii.h"
20 |
21 | extern globalstruct global;
22 |
23 | void vectorcrossproduct(fixedpointnum * v1, fixedpointnum * v2, fixedpointnum * v3)
24 | {
25 | v3[XINDEX] = lib_fp_multiply(v1[YINDEX], v2[ZINDEX]) - lib_fp_multiply(v1[ZINDEX], v2[YINDEX]);
26 | v3[YINDEX] = lib_fp_multiply(v1[ZINDEX], v2[XINDEX]) - lib_fp_multiply(v1[XINDEX], v2[ZINDEX]);
27 | v3[ZINDEX] = lib_fp_multiply(v1[XINDEX], v2[YINDEX]) - lib_fp_multiply(v1[YINDEX], v2[XINDEX]);
28 | }
29 |
30 | fixedpointnum normalizevector(fixedpointnum * v)
31 | {
32 | fixedpointnum vectorlengthsquared = lib_fp_multiply(v[0], v[0])
33 | + lib_fp_multiply(v[1], v[1])
34 | + lib_fp_multiply(v[2], v[2]);
35 |
36 | // if we are near zero lenght, choose any unit length vector
37 | if (vectorlengthsquared < 10) {
38 | v[0] = FIXEDPOINTONE;
39 | v[1] = v[2] = 0;
40 | } else {
41 | fixedpointnum multiplier = lib_fp_invsqrt(vectorlengthsquared);
42 |
43 | v[0] = lib_fp_multiply(v[0], multiplier);
44 | v[1] = lib_fp_multiply(v[1], multiplier);
45 | v[2] = lib_fp_multiply(v[2], multiplier);
46 | }
47 | return (vectorlengthsquared);
48 | }
49 |
50 | fixedpointnum vectordotproduct(fixedpointnum * v1, fixedpointnum * v2)
51 | {
52 | return (lib_fp_multiply(v1[0], v2[0]) + lib_fp_multiply(v1[1], v2[1]) + lib_fp_multiply(v1[2], v2[2]));
53 | }
54 |
55 | void rotatevectorwithsmallangles(fixedpointnum * v, fixedpointnum rolldeltaangle, fixedpointnum pitchdeltaangle, fixedpointnum yawdeltaangle)
56 | {
57 | // rotate theattitude by the delta angles.
58 | // assumes that the delta angles are small angles in degrees and that they are shifted left by TIMESLIVEREXTRASHIFT
59 | fixedpointnum v_tmp_x = v[XINDEX];
60 | fixedpointnum v_tmp_y = v[YINDEX];
61 | fixedpointnum v_tmp_z = v[ZINDEX];
62 |
63 | // remember that our delta angles are shifted left by TIMESLIVEREXTRASHIFT for resolution. Take it out here
64 | v[XINDEX] += (lib_fp_multiply(rolldeltaangle, v_tmp_z) - lib_fp_multiply(yawdeltaangle, v_tmp_y)) >> (TIMESLIVEREXTRASHIFT);
65 | v[YINDEX] += (lib_fp_multiply(pitchdeltaangle, v_tmp_z) + lib_fp_multiply(yawdeltaangle, v_tmp_x)) >> (TIMESLIVEREXTRASHIFT);
66 | v[ZINDEX] -= (lib_fp_multiply(rolldeltaangle, v_tmp_x) + lib_fp_multiply(pitchdeltaangle, v_tmp_y)) >> (TIMESLIVEREXTRASHIFT);
67 | }
68 |
69 | // some extra vector functions that aren't currently used.
70 | #ifdef EXTENDEDVECTORFUNCTIONS
71 | void vectordifferencetoeulerangles(fixedpointnum * v1, fixedpointnum * v2, fixedpointnum * euler)
72 | { // take the difference between the two attitudes and return the euler angles between them
73 | // find the axis of rotation and angle between the two downvectors
74 | // the cross products of the two vectors will give us the axis of rotation from one to the other
75 | fixedpointnum axisofrotation[3];
76 | vectorcrossproduct(v1, v2, axisofrotation);
77 |
78 | fixedpointnum axislength = lib_fp_sqrt(normalizevector(axisofrotation));
79 |
80 | // get the angle of rotation between the two vectors
81 | fixedpointnum angle = lib_fp_atan2(axislength, vectordotproduct(v1, v2));
82 |
83 | fixedpointnum unitvector[3];
84 | unitvector[0] = 0;
85 | unitvector[1] = FIXEDPOINTONE;
86 | unitvector[2] = 0;
87 |
88 | euler[0] = lib_fp_multiply(vectordotproduct(axisofrotation, unitvector), angle);
89 |
90 | unitvector[0] = FIXEDPOINTONE;
91 | unitvector[1] = 0;
92 | unitvector[2] = 0;
93 |
94 | euler[1] = lib_fp_multiply(vectordotproduct(axisofrotation, unitvector), angle);
95 | }
96 |
97 |
98 | void rotatevectorbyaxisangle(fixedpointnum * v1, fixedpointnum * axisvector, fixedpointnum angle, fixedpointnum * v2)
99 | {
100 | fixedpointnum cosineofangle = lib_fp_cosine(angle);
101 | fixedpointnum sineofangle = lib_fp_sine(angle);
102 |
103 | fixedpointnum crossproductvector[3];
104 | vectorcrossproduct(axisvector, v1, crossproductvector);
105 |
106 | fixedpointnum dotproducttimesoneminuscosineofangle = lib_fp_multiply(vectordotproduct(axisvector, v1), FIXEDPOINTONE - cosineofangle);
107 |
108 | v2[0] = lib_fp_multiply(v1[0], cosineofangle) + lib_fp_multiply(crossproductvector[0], sineofangle) + lib_fp_multiply(axisvector[0], dotproducttimesoneminuscosineofangle);
109 | v2[1] = lib_fp_multiply(v1[1], cosineofangle) + lib_fp_multiply(crossproductvector[1], sineofangle) + lib_fp_multiply(axisvector[1], dotproducttimesoneminuscosineofangle);
110 | v2[2] = lib_fp_multiply(v1[2], cosineofangle) + lib_fp_multiply(crossproductvector[2], sineofangle) + lib_fp_multiply(axisvector[2], dotproducttimesoneminuscosineofangle);
111 | }
112 |
113 | void rotatevectorbyaxissmallangle(fixedpointnum * v1, fixedpointnum * axisvector, fixedpointnum angle)
114 | { // rotates vector by small angle angle around axis vector.
115 | fixedpointnum angleinradians = lib_fp_multiply(angle, FIXEDPOINTPIOVER180);
116 |
117 | fixedpointnum crossproductvector[3];
118 | vectorcrossproduct(axisvector, v1, crossproductvector);
119 |
120 | v1[0] += lib_fp_multiply(crossproductvector[0], angleinradians);
121 | v1[1] += lib_fp_multiply(crossproductvector[1], angleinradians);
122 | v1[2] += lib_fp_multiply(crossproductvector[2], angleinradians);
123 | }
124 |
125 | #endif
126 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------