diff --git a/MakeConfig.inc b/MakeConfig.inc index 3e5249b..fc02383 100755 --- a/MakeConfig.inc +++ b/MakeConfig.inc @@ -1,33 +1,39 @@ ifeq ($(SCCC_CONFIG),INNO) BIN_SUFFIX:=$(BIN_SUFFIX)-inno -CPPFLAGS+=-DK60_INNO +CPPFLAGS+=-DK60_INNO=1 SCCC_MCU=MK60DZ10 $(info Config = INNO) else ifeq ($(SCCC_CONFIG),2014_CAMERA) BIN_SUFFIX:=$(BIN_SUFFIX)-2014-camera -CPPFLAGS+=-DK60_2014_CAMERA +CPPFLAGS+=-DK60_2014_CAMERA=1 SCCC_MCU=MK60DZ10 $(info Config = 2014_CAMERA) else ifeq ($(SCCC_CONFIG),2014_CCD) BIN_SUFFIX:=$(BIN_SUFFIX)-2014-ccd -CPPFLAGS+=-DK60_2014_CCD +CPPFLAGS+=-DK60_2014_CCD=1 SCCC_MCU=MK60DZ10 $(info Config = 2014_CCD) else ifeq ($(SCCC_CONFIG),2014_INNO) BIN_SUFFIX:=$(BIN_SUFFIX)-2014-inno -CPPFLAGS+=-DK60_2014_INNO +CPPFLAGS+=-DK60_2014_INNO=1 SCCC_MCU=MK60DZ10 $(info Config = 2014_INNO) else ifeq ($(SCCC_CONFIG),2014_MAGNETIC) BIN_SUFFIX:=$(BIN_SUFFIX)-2014-magnetic -CPPFLAGS+=-DK60_2014_MAGNETIC +CPPFLAGS+=-DK60_2014_MAGNETIC=1 SCCC_MCU=MK60DZ10 $(info Config = 2014_MAGNETIC) +else ifeq ($(SCCC_CONFIG),VCAN_FX15DEV) +BIN_SUFFIX:=$(BIN_SUFFIX)-vcan-fx15dev +CPPFLAGS+=-DK60_VCAN_FX15DEV=1 +SCCC_MCU=MK60F15 +$(info Config = VCAN_FX15DEV) + else $(error Missing/Unknown config '$(SCCC_CONFIG)' (set SCCC_CONFIG)) diff --git a/Makefile b/Makefile index c6ff19b..d060ab7 100755 --- a/Makefile +++ b/Makefile @@ -56,20 +56,20 @@ CCFLAGS+=-Wall -Wextra ifeq ($(SCCC_BUILD),DEBUG) BIN_SUFFIX:=$(BIN_SUFFIX)-d -CPPFLAGS+=-DDEBUG +CPPFLAGS+=-DDEBUG=1 CCFLAGS+=-O0 -g3 $(info Build = DEBUG) else ifeq ($(SCCC_BUILD),RELEASE) BIN_SUFFIX:=$(BIN_SUFFIX)-r -CPPFLAGS+=-DRELEASE -DNDEBUG +CPPFLAGS+=-DRELEASE=1 -DNDEBUG CCFLAGS+=-O2 -g0 $(info Build = RELEASE) else $(warning Unknown build type, defaulting to DEBUG (set SCCC_BUILD)) BIN_SUFFIX:=$(BIN_SUFFIX)-d -CPPFLAGS+=-DDEBUG +CPPFLAGS+=-DDEBUG=1 CCFLAGS+=-O0 -g3 $(info Build = DEBUG) @@ -78,17 +78,23 @@ endif include MakeConfig.inc ifeq ($(SCCC_MCU),MK60DZ10) -CPPFLAGS+=-DMK60DZ10 +CPPFLAGS+=-DMK60DZ10=1 CCFLAGS+=-mthumb -mthumb-interwork -mcpu=cortex-m4 CCFLAGS+=-msoft-float -mfloat-abi=soft $(info MCU sub-family = MK60DZ10) else ifeq ($(SCCC_MCU),MK60D10) -CPPFLAGS+=-DMK60D10 +CPPFLAGS+=-DMK60D10=1 CCFLAGS+=-mthumb -mthumb-interwork -mcpu=cortex-m4 CCFLAGS+=-msoft-float -mfloat-abi=soft $(info MCU sub-family = MK60D10) +else ifeq ($(SCCC_MCU),MK60F15) +CPPFLAGS+=-DMK60F15=1 +CCFLAGS+=-mthumb -mthumb-interwork -mcpu=cortex-m4 +CCFLAGS+=-mfpu=fpv4-sp-d16 -mfloat-abi=hard +$(info MCU sub-family = MK60F15) + else $(error Missing/Unknown MCU identifier '$(SCCC_MCU)' (set SCCC_MCU)) @@ -125,6 +131,20 @@ SRC_FILES:=$(shell find $(SRC_PATH) -type f -name *.c -o -name *.S -o -name *.cp endif +not_contain=$(foreach v,$2,$(if $(findstring $1,$v),,$v)) +SRC_FILES:=$(call not_contain,/pinout/,$(SRC_FILES)) + +ifeq ($(SCCC_MCU),MK60DZ10) +SRC_FILES+=$(SRC_PATH)/libbase/k60/pinout/mk60d10_lqfp144.cpp + +else ifeq ($(SCCC_MCU),MK60D10) +SRC_FILES+=$(SRC_PATH)/libbase/k60/pinout/mk60d10_lqfp144.cpp + +else ifeq ($(SCCC_MCU),MK60F15) +SRC_FILES+=$(SRC_PATH)/libbase/k60/pinout/mk60f15_lqfp144.cpp + +endif + OBJ_FILES:=$(SRC_FILES:$(SRC_PATH)/%.c=$(OUT_OBJ_PATH)/%.o) OBJ_FILES:=$(OBJ_FILES:$(SRC_PATH)/%.S=$(OUT_OBJ_PATH)/%.o) OBJ_FILES:=$(OBJ_FILES:$(SRC_PATH)/%.cpp=$(OUT_OBJ_PATH)/%.o) diff --git a/README.md b/README.md index 91e00c3..e3d7f2c 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,15 @@ libsccc ============== -libsccc (previously libsmartcarcpp) is the library being used among the HKUST SmartCar Team. -Master is the stable branch. Current developments are being done mainly on [fauxstess-cupcake](https://github.com/hkust-smartcar/libsccc/tree/fauxstess-cupcake), but also various other branches. +libsccc (previously libsmartcarcpp) is the library being used among the HKUST SmartCar Team ## What's Inside -libbase: K60 peripheral library +libbase: Peripheral library for MK60D(Z)10, MK60F15(/12) libsc: High-level component library based on libbase -libutil: Useful utilities +libutil: Useful utilities ## Dependency None ## Build -GNU Make, [GNU Tools for ARM Embedded Processor](https://launchpad.net/gcc-arm-embedded) +GNU Make 3.81+, [GNU Tools for ARM Embedded Processor 4.8+](https://launchpad.net/gcc-arm-embedded) diff --git a/inc/libbase/cmsis/core_cm4.h b/inc/libbase/cmsis/core_cm4.h index 1a3b861..6b2c599 100755 --- a/inc/libbase/cmsis/core_cm4.h +++ b/inc/libbase/cmsis/core_cm4.h @@ -37,6 +37,8 @@ #if defined ( __ICCARM__ ) #pragma system_include /* treat file as system include file for MISRA check */ +#elif defined ( __GNUC__ ) + #pragma GCC system_header #endif #ifndef __CORE_CM4_H_GENERIC diff --git a/inc/libbase/cmsis/core_cm4_simd.h b/inc/libbase/cmsis/core_cm4_simd.h index f9bceff..88c06cd 100755 --- a/inc/libbase/cmsis/core_cm4_simd.h +++ b/inc/libbase/cmsis/core_cm4_simd.h @@ -37,6 +37,8 @@ #if defined ( __ICCARM__ ) #pragma system_include /* treat file as system include file for MISRA check */ +#elif defined ( __GNUC__ ) + #pragma GCC system_header #endif #ifndef __CORE_CM4_SIMD_H diff --git a/inc/libbase/cmsis/core_cmFunc.h b/inc/libbase/cmsis/core_cmFunc.h index 2c2af69..87eacf1 100755 --- a/inc/libbase/cmsis/core_cmFunc.h +++ b/inc/libbase/cmsis/core_cmFunc.h @@ -35,9 +35,19 @@ ---------------------------------------------------------------------------*/ +#if defined ( __ICCARM__ ) + #pragma system_include /* treat file as system include file for MISRA check */ +#elif defined ( __GNUC__ ) + #pragma GCC system_header +#endif + #ifndef __CORE_CMFUNC_H #define __CORE_CMFUNC_H +#ifdef __cplusplus + extern "C" { +#endif + /* ########################### Core Function Access ########################### */ /** \ingroup CMSIS_Core_FunctionInterface @@ -634,4 +644,8 @@ __attribute__( ( always_inline ) ) __STATIC_INLINE void __set_FPSCR(uint32_t fps /*@} end of CMSIS_Core_RegAccFunctions */ +#ifdef __cplusplus +} +#endif + #endif /* __CORE_CMFUNC_H */ diff --git a/inc/libbase/cmsis/core_cmInstr.h b/inc/libbase/cmsis/core_cmInstr.h index d2ec262..af026f3 100755 --- a/inc/libbase/cmsis/core_cmInstr.h +++ b/inc/libbase/cmsis/core_cmInstr.h @@ -35,9 +35,19 @@ ---------------------------------------------------------------------------*/ +#if defined ( __ICCARM__ ) + #pragma system_include /* treat file as system include file for MISRA check */ +#elif defined ( __GNUC__ ) + #pragma GCC system_header +#endif + #ifndef __CORE_CMINSTR_H #define __CORE_CMINSTR_H +#ifdef __cplusplus + extern "C" { +#endif + /* ########################## Core Instruction Access ######################### */ /** \defgroup CMSIS_Core_InstructionInterface CMSIS Core Instruction Interface @@ -447,7 +457,7 @@ __attribute__( ( always_inline ) ) __STATIC_INLINE int32_t __REVSH(int32_t value */ __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __ROR(uint32_t op1, uint32_t op2) { - return (op1 >> op2) | (op1 << (32 - op2)); + return (op1 >> op2) | (op1 << (32 - op2)); } @@ -684,4 +694,8 @@ __attribute__( ( always_inline ) ) __STATIC_INLINE uint8_t __CLZ(uint32_t value) /*@}*/ /* end of group CMSIS_Core_InstructionInterface */ +#ifdef __cplusplus +} +#endif + #endif /* __CORE_CMINSTR_H */ diff --git a/inc/libbase/k60/adc.h b/inc/libbase/k60/adc.h index ddbb40f..a4f0b30 100755 --- a/inc/libbase/k60/adc.h +++ b/inc/libbase/k60/adc.h @@ -2,7 +2,8 @@ * adc.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -14,6 +15,7 @@ #include "libbase/k60/misc_utils.h" #include "libbase/k60/pin.h" +#include "libbase/k60/pinout_macros.h" namespace libbase { @@ -46,6 +48,28 @@ class Adc kAdc1Ad23, kAdc1Ad24, kAdc1Ad25, kAdc1Ad26, kAdc1Ad27, kAdc1Ad28, kAdc1Ad29, kAdc1Ad30, kAdc1Ad31, +#if PINOUT_ADC_COUNT > 2 + kAdc2DAd0 = 72, kAdc2DAd1, kAdc2DAd2, kAdc2DAd3, + kAdc2Ad4A, kAdc2Ad5A, kAdc2Ad6A, kAdc2Ad7A, + kAdc2Ad4B, kAdc2Ad5B, kAdc2Ad6B, kAdc2Ad7B, + kAdc2Ad8, kAdc2Ad9, kAdc2Ad10, kAdc2Ad11, kAdc2Ad12, + kAdc2Ad13, kAdc2Ad14, kAdc2Ad15, kAdc2Ad16, kAdc2Ad17, + kAdc2Ad18, kAdc2Ad19, kAdc2Ad20, kAdc2Ad21, kAdc2Ad22, + kAdc2Ad23, kAdc2Ad24, kAdc2Ad25, kAdc2Ad26, kAdc2Ad27, + kAdc2Ad28, kAdc2Ad29, kAdc2Ad30, kAdc2Ad31, +#endif + +#if PINOUT_ADC_COUNT > 3 + kAdc3DAd0 = 108, kAdc3DAd1, kAdc3DAd2, kAdc3DAd3, + kAdc3Ad4A, kAdc3Ad5A, kAdc3Ad6A, kAdc3Ad7A, + kAdc3Ad4B, kAdc3Ad5B, kAdc3Ad6B, kAdc3Ad7B, + kAdc3Ad8, kAdc3Ad9, kAdc3Ad10, kAdc3Ad11, kAdc3Ad12, + kAdc3Ad13, kAdc3Ad14, kAdc3Ad15, kAdc3Ad16, kAdc3Ad17, + kAdc3Ad18, kAdc3Ad19, kAdc3Ad20, kAdc3Ad21, kAdc3Ad22, + kAdc3Ad23, kAdc3Ad24, kAdc3Ad25, kAdc3Ad26, kAdc3Ad27, + kAdc3Ad28, kAdc3Ad29, kAdc3Ad30, kAdc3Ad31, +#endif + kDisable }; @@ -114,12 +138,19 @@ class Adc void StopConvert(); /** - * Return the result of a single conversion. If a conversion has not been - * started yet, it will be started automatically (through StartConvert()). + * Return the raw result of a single conversion. If a conversion has not + * been started yet, it will be started automatically (through StartConvert()) * - * @return The result + * @return */ uint16_t GetResult(); + /** + * Return the divided result of a single conversion. If a conversion has not + * been started yet, it will be started automatically (through StartConvert()) + * + * @return The result, [0.0, 3.3] + */ + float GetResultF(); bool PeekResult(uint16_t *out_val); bool IsActive() const; diff --git a/inc/libbase/k60/adc_utils.h b/inc/libbase/k60/adc_utils.h index 3ecc5ab..5075682 100755 --- a/inc/libbase/k60/adc_utils.h +++ b/inc/libbase/k60/adc_utils.h @@ -2,7 +2,8 @@ * adc_utils.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/clock_utils.h b/inc/libbase/k60/clock_utils.h index e1fec82..d42eafc 100755 --- a/inc/libbase/k60/clock_utils.h +++ b/inc/libbase/k60/clock_utils.h @@ -2,7 +2,8 @@ * clock_utils.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/cmsis/mk60f15.h b/inc/libbase/k60/cmsis/mk60f15.h index 1a979f9..0041996 100755 --- a/inc/libbase/k60/cmsis/mk60f15.h +++ b/inc/libbase/k60/cmsis/mk60f15.h @@ -11138,6 +11138,10 @@ typedef struct { */ /* end of group Backward_Compatibility_Symbols */ +#ifdef __cplusplus +} +#endif + #endif /* #if !defined(MK60F15_H_) */ /* MK60F15.h, eof. */ diff --git a/inc/libbase/k60/cmsis/system.h b/inc/libbase/k60/cmsis/system.h new file mode 100755 index 0000000..8b4a426 --- /dev/null +++ b/inc/libbase/k60/cmsis/system.h @@ -0,0 +1,23 @@ +/* + * system.h + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Setup the microcontroller system. This function should be used only after + * reset + */ +void SystemInit(void); + +#ifdef __cplusplus +} +#endif diff --git a/inc/libbase/k60/cmsis/system_mk60dz10.h b/inc/libbase/k60/cmsis/system_mk60dz10.h deleted file mode 100755 index 6e28b32..0000000 --- a/inc/libbase/k60/cmsis/system_mk60dz10.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * system_mk60dz10.h - * - * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team - */ - -#ifndef __SYSTEM_MK60DZ10_H -#define __SYSTEM_MK60DZ10_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -/** - * System Clock Frequency (Core Clock & System Clock) - */ -extern uint32_t g_system_core_clock; - -/** - * Setup the microcontroller system. This function should be used only after - * reset - */ -void SystemInit(void); - -#ifdef __cplusplus -} -#endif - -#endif /*__SYSTEM_MK60DZ10_H */ diff --git a/inc/libbase/k60/crc.h b/inc/libbase/k60/crc.h index 352d031..f2497f6 100755 --- a/inc/libbase/k60/crc.h +++ b/inc/libbase/k60/crc.h @@ -2,7 +2,8 @@ * crc.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/crc_utils.h b/inc/libbase/k60/crc_utils.h index 8054cc2..605fd93 100755 --- a/inc/libbase/k60/crc_utils.h +++ b/inc/libbase/k60/crc_utils.h @@ -2,7 +2,8 @@ * crc_utils.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/dac.h b/inc/libbase/k60/dac.h index ad3d874..ee3e270 100755 --- a/inc/libbase/k60/dac.h +++ b/inc/libbase/k60/dac.h @@ -2,7 +2,8 @@ * dac.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/dac_utils.h b/inc/libbase/k60/dac_utils.h index d8af894..7b138bb 100755 --- a/inc/libbase/k60/dac_utils.h +++ b/inc/libbase/k60/dac_utils.h @@ -2,7 +2,8 @@ * dac_utils.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/dwt.h b/inc/libbase/k60/dwt.h index 5667455..1776e12 100755 --- a/inc/libbase/k60/dwt.h +++ b/inc/libbase/k60/dwt.h @@ -3,7 +3,8 @@ * Data Watchpoint and Trace, currently only supports utilizing CYCCNT to delay * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/fpu.h b/inc/libbase/k60/fpu.h new file mode 100755 index 0000000..2275fb9 --- /dev/null +++ b/inc/libbase/k60/fpu.h @@ -0,0 +1,58 @@ +/* + * fpu.h + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +namespace libbase +{ +namespace k60 +{ + +class Fpu +{ +public: + struct Config + { + enum struct RoundingMode + { + kNearest = 0, + kPosInf, + kNegInf, + kZero, + }; + + // Enable the default NaN mode, see p.63 of DDI0403D + bool is_default_nan = true; + // Enable the flush-to-zero mode, which effectively discard subnormal + // numbers, see p.62 of DDI0403D + bool is_flush_to_zero = true; + // Set the floating point rounding mode, has nothing to do with the + // float->int conversion + RoundingMode rounding_mode = RoundingMode::kNearest; + }; + + static Fpu& Get() + { + static Fpu inst; + return inst; + } + + void Init(); + +private: + Fpu(); + + __attribute__((__weak__)) + static Config GetFpuConfig(); + + void InitFpccr(const Config &config); + void InitFpdscr(const Config &config); +}; + +} +} diff --git a/inc/libbase/k60/fpu_c.h b/inc/libbase/k60/fpu_c.h new file mode 100755 index 0000000..1db248f --- /dev/null +++ b/inc/libbase/k60/fpu_c.h @@ -0,0 +1,19 @@ +/* + * fpu_c.h + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +void LibbaseK60FpuInit(void); + +#ifdef __cplusplus +} +#endif diff --git a/inc/libbase/k60/ftm.h b/inc/libbase/k60/ftm.h index 11a9885..b3116c8 100755 --- a/inc/libbase/k60/ftm.h +++ b/inc/libbase/k60/ftm.h @@ -2,12 +2,14 @@ * ftm.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once #include "libbase/k60/pin.h" +#include "libbase/k60/pinout_macros.h" namespace libbase { @@ -34,7 +36,34 @@ class Ftm kFtm2Ch0 = 16, kFtm2Ch1, - kDisable = 24 +#if PINOUT_FTM_COUNT > 3 + kFtm3Ch0 = 24, + kFtm3Ch1, + kFtm3Ch2, + kFtm3Ch3, + kFtm3Ch4, + kFtm3Ch5, + kFtm3Ch6, + kFtm3Ch7, +#endif + + kDisable = 32 + }; + + enum struct QdName + { + kFtm0QdPha = 0, + kFtm0QdPhb, + kFtm1QdPha, + kFtm1QdPhb, + kFtm2QdPha, + kFtm2QdPhb, +#if PINOUT_FTM_COUNT > 3 + kFtm3QdPha, + kFtm3QdPhb, +#endif + + kDisable }; }; diff --git a/inc/libbase/k60/ftm_pwm.h b/inc/libbase/k60/ftm_pwm.h index 6560b06..72394ff 100755 --- a/inc/libbase/k60/ftm_pwm.h +++ b/inc/libbase/k60/ftm_pwm.h @@ -2,7 +2,8 @@ * ftm_pwm.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/ftm_quad_decoder.h b/inc/libbase/k60/ftm_quad_decoder.h index 841058b..755560a 100755 --- a/inc/libbase/k60/ftm_quad_decoder.h +++ b/inc/libbase/k60/ftm_quad_decoder.h @@ -2,7 +2,8 @@ * ftm_quad_decoder.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/ftm_utils.h b/inc/libbase/k60/ftm_utils.h index eaa9ad1..f2cbdbd 100755 --- a/inc/libbase/k60/ftm_utils.h +++ b/inc/libbase/k60/ftm_utils.h @@ -2,7 +2,8 @@ * ftm_utils.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -20,8 +21,6 @@ namespace k60 class FtmUtils { public: - static Ftm::Name GetFtmName(const Pin::Name pin); - static Uint GetFtmModule(const Ftm::Name pin) { return static_cast(pin) / PINOUT::GetFtmChannelCount(); @@ -31,6 +30,16 @@ class FtmUtils { return static_cast(pin) % PINOUT::GetFtmChannelCount(); } + + static Uint GetFtmModule(const Ftm::QdName pin) + { + return static_cast(pin) >> 1; + } + + static Uint GetFtmPhase(const Ftm::QdName pin) + { + return static_cast(pin) % 2; + } }; } diff --git a/inc/libbase/k60/gpio.h b/inc/libbase/k60/gpio.h index d548a94..6dffd5b 100755 --- a/inc/libbase/k60/gpio.h +++ b/inc/libbase/k60/gpio.h @@ -2,7 +2,8 @@ * gpio.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/hardware.h b/inc/libbase/k60/hardware.h index f88d79e..bdc1e98 100755 --- a/inc/libbase/k60/hardware.h +++ b/inc/libbase/k60/hardware.h @@ -2,18 +2,20 @@ * hardware.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once #if defined(MK60DZ10) #include "libbase/k60/cmsis/mk60dz10.h" -#include "libbase/k60/cmsis/system_mk60dz10.h" #elif defined(MK60D10) #include "libbase/k60/cmsis/mk60d10.h" -#include "libbase/k60/cmsis/system_mk60dz10.h" + +#elif defined(MK60F15) +#include "libbase/k60/cmsis/mk60f15.h" #else #error Unknown MCU diff --git a/inc/libbase/k60/i2c_master_interface.h b/inc/libbase/k60/i2c_master_interface.h index 150e108..22d747b 100755 --- a/inc/libbase/k60/i2c_master_interface.h +++ b/inc/libbase/k60/i2c_master_interface.h @@ -2,7 +2,8 @@ * i2c_master_interface.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -27,6 +28,7 @@ class I2cMasterInterface { Pin::Name scl_pin; Pin::Name sda_pin; + uint16_t scl_low_timeout = UINT16_MAX; }; virtual ~I2cMasterInterface() @@ -34,7 +36,8 @@ class I2cMasterInterface virtual operator bool() const = 0; - virtual bool GetByte(const Byte slave_addr, const Byte reg_addr, Byte *out_byte) = 0; + virtual bool GetByte(const Byte slave_addr, const Byte reg_addr, + Byte *out_byte) = 0; virtual std::vector GetBytes(const Byte slave_addr, const Byte reg_addr, const uint8_t size) = 0; virtual bool SendByte(const Byte slave_addr, const Byte reg_addr, diff --git a/inc/libbase/k60/mcg.h b/inc/libbase/k60/mcg.h index e0f3b38..26a8c61 100755 --- a/inc/libbase/k60/mcg.h +++ b/inc/libbase/k60/mcg.h @@ -2,7 +2,8 @@ * mcg.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -21,6 +22,8 @@ class Mcg public: struct Config { + Config(); + uint32_t external_oscillator_khz; uint32_t core_clock_khz; uint32_t bus_clock_khz; @@ -28,23 +31,36 @@ class Mcg uint32_t flash_clock_khz; }; - static void Init(); + static Mcg& Get() + { + static Mcg inst; + return inst; + } - static uint32_t GetCoreClock() + void Init(); + + uint32_t GetCoreClock() { return m_core_clock; } private: - __attribute__((__weak__)) + Mcg(); + + /** + * Get the Config object used during initialization, to be implemented by + * user + * + * @return + */ static Config GetMcgConfig(); - static void InitFbe(const Config &config); - static void InitPbe(const Config &config, const uint8_t vdiv); - static void InitPee(const Config &config); - static void InitClocks(const Config &config, const uint32_t core_clock); + void InitFbe(const Config &config); + void InitPbe(const Config &config, const uint8_t vdiv); + void InitPee(const Config &config); + void InitClocks(const Config &config, const uint32_t core_clock); - static uint32_t m_core_clock; + uint32_t m_core_clock; }; } diff --git a/inc/libbase/k60/mcg_c.h b/inc/libbase/k60/mcg_c.h index 370e56e..43499f6 100755 --- a/inc/libbase/k60/mcg_c.h +++ b/inc/libbase/k60/mcg_c.h @@ -2,7 +2,8 @@ * mcg_c.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -13,8 +14,8 @@ extern "C" { #endif -void LibbaseK60McgInit(); -uint32_t LibbaseK60McgGetCoreClock(); +void LibbaseK60McgInit(void); +uint32_t LibbaseK60McgGetCoreClock(void); #ifdef __cplusplus } diff --git a/inc/libbase/k60/misc_utils.h b/inc/libbase/k60/misc_utils.h index a060503..d255749 100755 --- a/inc/libbase/k60/misc_utils.h +++ b/inc/libbase/k60/misc_utils.h @@ -2,7 +2,8 @@ * misc_utils.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/misc_utils_c.h b/inc/libbase/k60/misc_utils_c.h index 47a3f60..594b307 100755 --- a/inc/libbase/k60/misc_utils_c.h +++ b/inc/libbase/k60/misc_utils_c.h @@ -3,7 +3,8 @@ * Subset of misc utils that could be used with plain C * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/pin.h b/inc/libbase/k60/pin.h index 40f1772..c081b52 100755 --- a/inc/libbase/k60/pin.h +++ b/inc/libbase/k60/pin.h @@ -2,7 +2,8 @@ * pin.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/pin_isr_manager.h b/inc/libbase/k60/pin_isr_manager.h index 5a98110..13daeee 100755 --- a/inc/libbase/k60/pin_isr_manager.h +++ b/inc/libbase/k60/pin_isr_manager.h @@ -3,7 +3,8 @@ * Manage ISR for pins * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -34,7 +35,7 @@ class PinIsrManager private: struct PinData { - libbase::k60::Pin *pin = nullptr; + Pin *pin = nullptr; OnPinIrqListener isr = nullptr; }; diff --git a/inc/libbase/k60/pin_utils.h b/inc/libbase/k60/pin_utils.h index 0f40c46..2d78c60 100755 --- a/inc/libbase/k60/pin_utils.h +++ b/inc/libbase/k60/pin_utils.h @@ -2,7 +2,8 @@ * pin_utils.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/pinout.h b/inc/libbase/k60/pinout.h index 8bcd517..799bc67 100755 --- a/inc/libbase/k60/pinout.h +++ b/inc/libbase/k60/pinout.h @@ -2,7 +2,8 @@ * pinout.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -11,6 +12,10 @@ #include "libbase/k60/pinout/mk60d10_lqfp144.h" #define PINOUT libbase::k60::Mk60d10Lqfp144 +#elif defined(MK60F15) +#include "libbase/k60/pinout/mk60f15_lqfp144.h" +#define PINOUT libbase::k60::Mk60f15Lqfp144 + #else #error Unknown MCU diff --git a/inc/libbase/k60/pinout/mk60d10_lqfp144.h b/inc/libbase/k60/pinout/mk60d10_lqfp144.h old mode 100755 new mode 100644 index 0439df1..d56a386 --- a/inc/libbase/k60/pinout/mk60d10_lqfp144.h +++ b/inc/libbase/k60/pinout/mk60d10_lqfp144.h @@ -9,7 +9,8 @@ * MK60DN512VMD10 * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -18,6 +19,7 @@ #include "libbase/k60/adc.h" #include "libbase/k60/dac.h" +#include "libbase/k60/ftm.h" #include "libbase/k60/misc_utils.h" #include "libbase/k60/pin.h" @@ -31,52 +33,57 @@ class Mk60d10Lqfp144 public: static bool RegPin(const Uint pin) { - return GetInstance()->RegPin_(pin); + return Get().RegPin_(pin); } static bool RegPin(const Pin::Name pin) { - return GetInstance()->RegPin_(pin); + return Get().RegPin_(pin); } static bool RegPin(const Adc::Name pin) { - return GetInstance()->RegPin_(pin); + return Get().RegPin_(pin); } static bool RegPin(const Dac::Name pin) { - return GetInstance()->RegPin_(pin); + return Get().RegPin_(pin); } static void UnregPin(const Uint pin) { - GetInstance()->UnregPin_(pin); + Get().UnregPin_(pin); } static void UnregPin(const Pin::Name pin) { - GetInstance()->UnregPin_(pin); + Get().UnregPin_(pin); } static void UnregPin(const Adc::Name pin) { - GetInstance()->UnregPin_(pin); + Get().UnregPin_(pin); } static void UnregPin(const Dac::Name pin) { - GetInstance()->UnregPin_(pin); + Get().UnregPin_(pin); + } + + static constexpr Uint GetAdcCount() + { + return PINOUT_ADC_COUNT; } static constexpr Uint GetFtmCount() { - return 3; + return PINOUT_FTM_COUNT; } static constexpr Uint GetFtmChannelCount() { - return 8; + return PINOUT_FTM_CHANNEL_COUNT; } static constexpr Uint GetPinCount() @@ -86,36 +93,39 @@ class Mk60d10Lqfp144 static constexpr Uint GetPortCount() { - return 5; + return PINOUT_PORT_COUNT; } static constexpr Uint GetPortPinCount() { - return 32; + return PINOUT_PORT_PIN_COUNT; } static constexpr Uint GetSpiCount() { - return 3; + return PINOUT_SPI_COUNT; } static constexpr Uint GetUartCount() { - return 6; + return PINOUT_UART_COUNT; } + static Adc::Name GetAdc(const Pin::Name pin); + static Ftm::Name GetFtm(const Pin::Name pin); + static Pin::Config::MuxControl GetFtmMux(const Pin::Name pin); + static Ftm::QdName GetFtmQd(const Pin::Name pin); + static Pin::Config::MuxControl GetFtmQdMux(const Pin::Name pin); + private: - static constexpr Uint kPinCount = 144; + static constexpr Uint kPinCount = PINOUT_PIN_COUNT; Mk60d10Lqfp144(); - static Mk60d10Lqfp144* GetInstance() + static Mk60d10Lqfp144& Get() { - if (!m_instance) - { - m_instance = new Mk60d10Lqfp144; - } - return m_instance; + static Mk60d10Lqfp144 inst; + return inst; } bool RegPin_(const Uint pin); @@ -128,8 +138,6 @@ class Mk60d10Lqfp144 void UnregPin_(const Dac::Name pin); std::bitset m_is_pins_active; - - static Mk60d10Lqfp144 *m_instance; }; } diff --git a/inc/libbase/k60/pinout/mk60d10_lqfp144_macros.h b/inc/libbase/k60/pinout/mk60d10_lqfp144_macros.h new file mode 100644 index 0000000..9d3cc9e --- /dev/null +++ b/inc/libbase/k60/pinout/mk60d10_lqfp144_macros.h @@ -0,0 +1,20 @@ +/* + * mk60d10_lqfp144_macros.h + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +// These macros are here to support conditional inclution only, DO NOT use +// directly +#define PINOUT_PIN_COUNT 144 +#define PINOUT_ADC_COUNT 2 +#define PINOUT_FTM_COUNT 3 +#define PINOUT_FTM_CHANNEL_COUNT 8 +#define PINOUT_PORT_COUNT 5 +#define PINOUT_PORT_PIN_COUNT 32 +#define PINOUT_SPI_COUNT 3 +#define PINOUT_UART_COUNT 6 diff --git a/inc/libbase/k60/pinout/mk60f15_lqfp144.h b/inc/libbase/k60/pinout/mk60f15_lqfp144.h new file mode 100644 index 0000000..836db71 --- /dev/null +++ b/inc/libbase/k60/pinout/mk60f15_lqfp144.h @@ -0,0 +1,139 @@ +/* + * mk60f15_lqfp144.h + * Pinout for: + * MK60FX512VLQ15 + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#include + +#include "libbase/k60/adc.h" +#include "libbase/k60/dac.h" +#include "libbase/k60/ftm.h" +#include "libbase/k60/misc_utils.h" +#include "libbase/k60/pin.h" + +namespace libbase +{ +namespace k60 +{ + +class Mk60f15Lqfp144 +{ +public: + static bool RegPin(const Uint pin) + { + return Get().RegPin_(pin); + } + + static bool RegPin(const Pin::Name pin) + { + return Get().RegPin_(pin); + } + + static bool RegPin(const Adc::Name pin) + { + return Get().RegPin_(pin); + } + + static bool RegPin(const Dac::Name pin) + { + return Get().RegPin_(pin); + } + + static void UnregPin(const Uint pin) + { + Get().UnregPin_(pin); + } + + static void UnregPin(const Pin::Name pin) + { + Get().UnregPin_(pin); + } + + static void UnregPin(const Adc::Name pin) + { + Get().UnregPin_(pin); + } + + static void UnregPin(const Dac::Name pin) + { + Get().UnregPin_(pin); + } + + static constexpr Uint GetAdcCount() + { + return PINOUT_ADC_COUNT; + } + + static constexpr Uint GetFtmCount() + { + return PINOUT_FTM_COUNT; + } + + static constexpr Uint GetFtmChannelCount() + { + return PINOUT_FTM_CHANNEL_COUNT; + } + + static constexpr Uint GetPinCount() + { + return kPinCount; + } + + static constexpr Uint GetPortCount() + { + return PINOUT_PORT_COUNT; + } + + static constexpr Uint GetPortPinCount() + { + return PINOUT_PORT_PIN_COUNT; + } + + static constexpr Uint GetSpiCount() + { + return PINOUT_SPI_COUNT; + } + + static constexpr Uint GetUartCount() + { + return PINOUT_UART_COUNT; + } + + static Adc::Name GetAdc(const Pin::Name pin); + static Ftm::Name GetFtm(const Pin::Name pin); + static Pin::Config::MuxControl GetFtmMux(const Pin::Name pin); + static Ftm::QdName GetFtmQd(const Pin::Name pin); + static Pin::Config::MuxControl GetFtmQdMux(const Pin::Name pin); + +private: + static constexpr Uint kPinCount = PINOUT_PIN_COUNT; + + Mk60f15Lqfp144(); + + static Mk60f15Lqfp144& Get() + { + static Mk60f15Lqfp144 inst; + return inst; + } + + bool RegPin_(const Uint pin); + bool RegPin_(const Pin::Name pin); + bool RegPin_(const Adc::Name pin); + bool RegPin_(const Dac::Name pin); + void UnregPin_(const Uint pin); + void UnregPin_(const Pin::Name pin); + void UnregPin_(const Adc::Name pin); + void UnregPin_(const Dac::Name pin); + + std::bitset m_is_pins_active; +}; + +} +} diff --git a/inc/libbase/k60/pinout/mk60f15_lqfp144_macros.h b/inc/libbase/k60/pinout/mk60f15_lqfp144_macros.h new file mode 100644 index 0000000..1c3f1ea --- /dev/null +++ b/inc/libbase/k60/pinout/mk60f15_lqfp144_macros.h @@ -0,0 +1,20 @@ +/* + * mk60f15_lqfp144_macros.h + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +// These macros are here to support conditional inclution only, DO NOT use +// directly +#define PINOUT_PIN_COUNT 144 +#define PINOUT_ADC_COUNT 4 +#define PINOUT_FTM_COUNT 4 +#define PINOUT_FTM_CHANNEL_COUNT 8 +#define PINOUT_PORT_COUNT 5 +#define PINOUT_PORT_PIN_COUNT 32 +#define PINOUT_SPI_COUNT 3 +#define PINOUT_UART_COUNT 6 diff --git a/inc/libbase/k60/pinout_macros.h b/inc/libbase/k60/pinout_macros.h new file mode 100755 index 0000000..18b1dd4 --- /dev/null +++ b/inc/libbase/k60/pinout_macros.h @@ -0,0 +1,20 @@ +/* + * pinout_constants.h + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#if defined(MK60DZ10) || defined(MK60D10) +#include "libbase/k60/pinout/mk60d10_lqfp144_macros.h" + +#elif defined(MK60F15) +#include "libbase/k60/pinout/mk60f15_lqfp144_macros.h" + +#else +#error Unknown MCU + +#endif diff --git a/inc/libbase/k60/pit.h b/inc/libbase/k60/pit.h index 3e03283..de86c40 100755 --- a/inc/libbase/k60/pit.h +++ b/inc/libbase/k60/pit.h @@ -2,7 +2,8 @@ * pit.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/pwm.h b/inc/libbase/k60/pwm.h index 5206fa1..b51bc3d 100755 --- a/inc/libbase/k60/pwm.h +++ b/inc/libbase/k60/pwm.h @@ -2,7 +2,8 @@ * pwm.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -24,7 +25,7 @@ class Pwm enum struct Precision { kUs, - KNs, + kNs, }; Pin::Name pin; diff --git a/inc/libbase/k60/pwm_utils.h b/inc/libbase/k60/pwm_utils.h index 16b363e..54f7a0e 100755 --- a/inc/libbase/k60/pwm_utils.h +++ b/inc/libbase/k60/pwm_utils.h @@ -2,7 +2,8 @@ * pwm_utils.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/quad_decoder_interface.h b/inc/libbase/k60/quad_decoder_interface.h index b1cc062..8220ff0 100755 --- a/inc/libbase/k60/quad_decoder_interface.h +++ b/inc/libbase/k60/quad_decoder_interface.h @@ -3,7 +3,8 @@ * Generic quadrature decoder interface * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/rand_generator.h b/inc/libbase/k60/rand_generator.h index 64dc34e..ea5fb00 100755 --- a/inc/libbase/k60/rand_generator.h +++ b/inc/libbase/k60/rand_generator.h @@ -3,7 +3,8 @@ * Random Number Generator Accelerator * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/sim.h b/inc/libbase/k60/sim.h old mode 100755 new mode 100644 index 9e7b39e..1eefa7f --- a/inc/libbase/k60/sim.h +++ b/inc/libbase/k60/sim.h @@ -3,7 +3,8 @@ * System integration module * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -34,6 +35,7 @@ class Sim kK60 }; +#if MK60DZ10 || MK60D10 enum struct ClockGate { kAdc0, @@ -67,6 +69,7 @@ class Sim kPortC, kPortD, kPortE, + kRegFile, kRng, kRtc, kSdhc, @@ -85,6 +88,68 @@ class Sim kVref, }; +#elif MK60F15 + enum struct ClockGate + { + kAdc0, + kAdc1, + kAdc2, + kAdc3, + kCmp, + kCmt, + kCrc, + kDac0, + kDac1, + kDma, + kDmaMux0, + kDmaMux1, + kDspi0, + kDspi1, + kDspi2, + kEsdhc, + kEthernet, + kExtWatchdog, + kFlexBus, + kFlexCan0, + kFlexCan1, + kFtm0, + kFtm1, + kFtm2, + kFtm3, + kIic0, + kIic1, + kLlwu, + kLptimer, + kMpu, + kNfc, + kOsc1, + kPdb, + kPit, + kPortA, + kPortB, + kPortC, + kPortD, + kPortE, + kPortF, + kRegFile, + kRng, + kRtc, + kSai0, + kSai1, + kTsi, + kUart0, + kUart1, + kUart2, + kUart3, + kUart4, + kUart5, + kUsbDcd, + kUsbFs, + kUsbHs, + kVref, + }; +#endif + static uint32_t GetRamBytes(); static Uint GetRamKBytes() { diff --git a/inc/libbase/k60/soft_i2c_master.h b/inc/libbase/k60/soft_i2c_master.h index 0addde7..f33d40c 100755 --- a/inc/libbase/k60/soft_i2c_master.h +++ b/inc/libbase/k60/soft_i2c_master.h @@ -2,7 +2,8 @@ * soft_i2c_master.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -57,10 +58,11 @@ class SoftI2cMaster : public I2cMasterInterface void Start(); void Stop(); bool SendByte_(const Byte byte); - Byte ReadByte_(const bool is_ack); + bool ReadByte_(const bool is_ack, Byte *out_byte); inline void Delay(); + uint16_t m_scl_low_timeout; uint32_t m_delay_us; Gpio m_scl; diff --git a/inc/libbase/k60/soft_pwm.h b/inc/libbase/k60/soft_pwm.h index bbacb02..8d2fedb 100755 --- a/inc/libbase/k60/soft_pwm.h +++ b/inc/libbase/k60/soft_pwm.h @@ -3,7 +3,8 @@ * Software emulated PWM using PIT and GPIO * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/soft_quad_decoder.h b/inc/libbase/k60/soft_quad_decoder.h index f7ccb85..21a1f48 100755 --- a/inc/libbase/k60/soft_quad_decoder.h +++ b/inc/libbase/k60/soft_quad_decoder.h @@ -6,7 +6,8 @@ * current count by 4 in software mode, so there will be a maximum of +-3 error * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/soft_spi_master.h b/inc/libbase/k60/soft_spi_master.h index cbbc395..abfeab2 100755 --- a/inc/libbase/k60/soft_spi_master.h +++ b/inc/libbase/k60/soft_spi_master.h @@ -3,7 +3,8 @@ * Emulated SPI through GPIO with a compatible interface with SpiMaster * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/spi_master.h b/inc/libbase/k60/spi_master.h index ee56418..ed64f43 100755 --- a/inc/libbase/k60/spi_master.h +++ b/inc/libbase/k60/spi_master.h @@ -2,7 +2,8 @@ * spi_master.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/spi_utils.h b/inc/libbase/k60/spi_utils.h index b959296..ea8bc0f 100755 --- a/inc/libbase/k60/spi_utils.h +++ b/inc/libbase/k60/spi_utils.h @@ -2,7 +2,8 @@ * spi_utils.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/sys_tick.h b/inc/libbase/k60/sys_tick.h index 7850369..3460cce 100755 --- a/inc/libbase/k60/sys_tick.h +++ b/inc/libbase/k60/sys_tick.h @@ -2,7 +2,8 @@ * sys_tick.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/uart.h b/inc/libbase/k60/uart.h index 2623514..f67177a 100755 --- a/inc/libbase/k60/uart.h +++ b/inc/libbase/k60/uart.h @@ -2,7 +2,8 @@ * uart.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -71,7 +72,7 @@ class Uart bool is_tx_irq_threshold_percentage = false; OnRxFullListener rx_isr; - /// The # bytes in the Tx buffer needed to fire the interrupt + /// The # bytes in the Rx buffer needed to fire the interrupt uint8_t rx_irq_threshold = 1; /// To treat rx_irq_threshold as a percentage of Rx buffer size bool is_rx_irq_threshold_percentage = false; diff --git a/inc/libbase/k60/vectors.h b/inc/libbase/k60/vectors.h index c07e060..1704076 100755 --- a/inc/libbase/k60/vectors.h +++ b/inc/libbase/k60/vectors.h @@ -2,7 +2,8 @@ * vectors.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ /* ################################################################### ** THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT. @@ -37,15 +38,10 @@ extern "C" { #endif #include "libbase/k60/hardware.h" - #include "libbase/k60/misc_utils_c.h" -/* Interrupt vector table type definition */ +/* Interrupt handler type definition */ typedef void (*tIsrFunc)(void); -typedef struct { - void * __ptr; - tIsrFunc __fun[0x77]; -} tVectorTable; typedef void (*HardFaultHandler)(void); extern HardFaultHandler g_hard_fault_handler; diff --git a/inc/libbase/k60/watchdog.h b/inc/libbase/k60/watchdog.h index 888b616..83af2d8 100755 --- a/inc/libbase/k60/watchdog.h +++ b/inc/libbase/k60/watchdog.h @@ -2,7 +2,8 @@ * watchdog.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/k60/watchdog_c.h b/inc/libbase/k60/watchdog_c.h index a31deae..69df545 100755 --- a/inc/libbase/k60/watchdog_c.h +++ b/inc/libbase/k60/watchdog_c.h @@ -2,7 +2,8 @@ * watchdog_c.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/log.h b/inc/libbase/log.h index 268e454..c86a590 100755 --- a/inc/libbase/log.h +++ b/inc/libbase/log.h @@ -2,7 +2,8 @@ * log.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/misc_types.h b/inc/libbase/misc_types.h index 8ec884a..c29632d 100755 --- a/inc/libbase/misc_types.h +++ b/inc/libbase/misc_types.h @@ -2,7 +2,8 @@ * misc_types.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/misc_utils.h b/inc/libbase/misc_utils.h index 4a3b2b2..141f343 100755 --- a/inc/libbase/misc_utils.h +++ b/inc/libbase/misc_utils.h @@ -2,7 +2,8 @@ * misc_utils.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/misc_utils_c.h b/inc/libbase/misc_utils_c.h index 0c4bd1d..11f6b00 100755 --- a/inc/libbase/misc_utils_c.h +++ b/inc/libbase/misc_utils_c.h @@ -3,7 +3,8 @@ * Subset of misc utils that could be used with plain C * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libbase/syscall.h b/inc/libbase/syscall.h index e17f1bc..049a3d4 100755 --- a/inc/libbase/syscall.h +++ b/inc/libbase/syscall.h @@ -2,7 +2,8 @@ * syscall.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/config.h b/inc/libsc/config.h index 0a7abaf..53d8e8d 100644 --- a/inc/libsc/config.h +++ b/inc/libsc/config.h @@ -3,7 +3,8 @@ * To include the corresponding config file * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -35,6 +36,9 @@ #elif defined(K60_2014_MAGNETIC) #include "libsc/k60/config/2014_magnetic.h" +#elif defined(K60_VCAN_FX15DEV) +#include "libsc/k60/config/vcan_fx15dev.h" + #else #error Unknown board diff --git a/inc/libsc/device_h/ds18b20.h b/inc/libsc/device_h/ds18b20.h new file mode 100755 index 0000000..ed7e9ca --- /dev/null +++ b/inc/libsc/device_h/ds18b20.h @@ -0,0 +1,13 @@ +/* + * ds18b20.h + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#define DS18B20_CONVERT_T 0x44 // Convert T +#define DS18B20_READ_SP 0xBE // Read Scratchpad +#define DS18B20_SKIP 0xCC // Skip ROM diff --git a/inc/libsc/device_h/mpu6050.h b/inc/libsc/device_h/mpu6050.h index ea8ac0d..08a5874 100755 --- a/inc/libsc/device_h/mpu6050.h +++ b/inc/libsc/device_h/mpu6050.h @@ -2,7 +2,8 @@ * mpu6050.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ /* ============================================ I2Cdev device library code is placed under the MIT license diff --git a/inc/libsc/device_h/st7735r.h b/inc/libsc/device_h/st7735r.h new file mode 100755 index 0000000..3a491d3 --- /dev/null +++ b/inc/libsc/device_h/st7735r.h @@ -0,0 +1,33 @@ +/* + * st7735r.h + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#define ST7735R_SWRESET 0x01 // Software Reset +#define ST7735R_RDDID 0x04 // Read Display ID +#define ST7735R_SLPIN 0x10 // Sleep In +#define ST7735R_SLPOUT 0x11 // Sleep Out +#define ST7735R_INVOFF 0x20 // Display Inversion Off +#define ST7735R_INVON 0x21 // Display Inversion On +#define ST7735R_DISPOFF 0x28 // Display Off +#define ST7735R_DISPON 0x29 // Display On +#define ST7735R_CASET 0x2A // Column Address Set +#define ST7735R_RASET 0x2B // Row Address Set +#define ST7735R_RAMWR 0x2C // Memory Write +#define ST7735R_MADCTL 0x36 // Memory Data Access Control +#define ST7735R_COLMOD 0x3A // Interface Pixel Format +#define ST7735R_FRMCTRL1 0xB1 // Frame Rate Control (in normal mode) +#define ST7735R_INVCTR 0xB4 // Display Inversion Control +#define ST7735R_PWCTR1 0xC0 // Power Control 1 +#define ST7735R_PWCTR2 0xC1 // Power Control 2 +#define ST7735R_PWCTR3 0xC2 // Power Control 3 (in normal mode) +#define ST7735R_PWCTR4 0xC3 // Power Control 4 (in idle/8-bit mode) +#define ST7735R_PWCTR5 0xC4 // Power Control 5 (in partial mode) +#define ST7735R_VMCTR1 0xC5 // VCOM Control 1 +#define ST7735R_GMCTRP1 0xE0 // Gamma (+ polarity) Correction Characteristics Setting +#define ST7735R_GMCTRN1 0xE1 // Gamma (- polarity) Correction Characteristics Setting diff --git a/inc/libsc/k60/ab_encoder.h b/inc/libsc/k60/ab_encoder.h new file mode 100755 index 0000000..628b537 --- /dev/null +++ b/inc/libsc/k60/ab_encoder.h @@ -0,0 +1,40 @@ +/* + * ab_encoder.h + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#include + +#include "libsc/config.h" +#include "libsc/k60/encoder.h" + +namespace libsc +{ +namespace k60 +{ + +class AbEncoder : public Encoder +{ +public: + typedef Encoder::Config Config; + + explicit AbEncoder(const Config &config); + +protected: + struct Initializer : public Encoder::Initializer + { + explicit Initializer(const Config &config) + : Encoder::Initializer(config) + {} + + QuadDecoder::Config GetQuadDecoderConfig() const override; + }; +}; + +} +} diff --git a/inc/libsc/k60/alternate_motor.h b/inc/libsc/k60/alternate_motor.h index ca8dd7f..d2ae7c6 100755 --- a/inc/libsc/k60/alternate_motor.h +++ b/inc/libsc/k60/alternate_motor.h @@ -2,7 +2,7 @@ * alternate_motor.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ @@ -27,7 +27,7 @@ class AlternateMotor : public Motor uint8_t id; }; - AlternateMotor(const Config &config); + explicit AlternateMotor(const Config &config); private: void OnSetPower(const uint16_t power) override; diff --git a/inc/libsc/k60/ambient_light_sensor.h b/inc/libsc/k60/ambient_light_sensor.h index 2271520..3d9b51a 100755 --- a/inc/libsc/k60/ambient_light_sensor.h +++ b/inc/libsc/k60/ambient_light_sensor.h @@ -3,7 +3,8 @@ * LS1970 * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -20,7 +21,12 @@ namespace k60 class AmbientLightSensor { public: - explicit AmbientLightSensor(const uint8_t id); + struct Config + { + uint8_t id; + }; + + explicit AmbientLightSensor(const Config &config); uint16_t GetLux(); diff --git a/inc/libsc/k60/battery_meter.h b/inc/libsc/k60/battery_meter.h new file mode 100755 index 0000000..545642e --- /dev/null +++ b/inc/libsc/k60/battery_meter.h @@ -0,0 +1,36 @@ +/* + * battery_meter.h + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#include "libbase/k60/adc.h" + +namespace libsc +{ +namespace k60 +{ + +class BatteryMeter +{ +public: + struct Config + { + float voltage_ratio; + }; + + explicit BatteryMeter(const Config &config); + + float GetVoltage(); + +private: + libbase::k60::Adc m_adc; + float m_voltage_ratio; +}; + +} +} diff --git a/inc/libsc/k60/button.h b/inc/libsc/k60/button.h index 790d6a7..11ff8c2 100755 --- a/inc/libsc/k60/button.h +++ b/inc/libsc/k60/button.h @@ -2,7 +2,8 @@ * button.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -19,7 +20,13 @@ namespace k60 class Button { public: - explicit Button(const uint8_t id); + struct Config + { + uint8_t id; + bool is_active_low; + }; + + explicit Button(const Config &config); bool IsDown() const; bool IsUp() const @@ -29,6 +36,7 @@ class Button private: libbase::k60::Gpi m_pin; + bool m_is_active_low; }; } diff --git a/inc/libsc/k60/config/2013_ccd.h b/inc/libsc/k60/config/2013_ccd.h index fa0d570..5c3ae2d 100644 --- a/inc/libsc/k60/config/2013_ccd.h +++ b/inc/libsc/k60/config/2013_ccd.h @@ -3,7 +3,8 @@ * Config file for 2013 Gen 2/3 board * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #ifndef LIBSC_2013_CCD_H_ diff --git a/inc/libsc/k60/config/2013_gen2.h b/inc/libsc/k60/config/2013_gen2.h index e63213e..d134e38 100644 --- a/inc/libsc/k60/config/2013_gen2.h +++ b/inc/libsc/k60/config/2013_gen2.h @@ -3,7 +3,8 @@ * Config file for 2013 Gen 2/3 board * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #ifndef LIBSC_2013_GEN2_H_ diff --git a/inc/libsc/k60/config/2013_magnetic.h b/inc/libsc/k60/config/2013_magnetic.h index 850fdf6..fa73800 100755 --- a/inc/libsc/k60/config/2013_magnetic.h +++ b/inc/libsc/k60/config/2013_magnetic.h @@ -3,7 +3,8 @@ * Config file for 2013 Gen 2/3 board * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #ifndef LIBSC_2013_MAGNETIC_H_ diff --git a/inc/libsc/k60/config/2014.h b/inc/libsc/k60/config/2014.h index 13f21ad..adba2d6 100755 --- a/inc/libsc/k60/config/2014.h +++ b/inc/libsc/k60/config/2014.h @@ -3,7 +3,8 @@ * Config file for 2014 Gen 1 board * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #ifndef LIBSC_2014_H_ diff --git a/inc/libsc/k60/config/2014_camera.h b/inc/libsc/k60/config/2014_camera.h index 134e092..449fe14 100644 --- a/inc/libsc/k60/config/2014_camera.h +++ b/inc/libsc/k60/config/2014_camera.h @@ -3,7 +3,8 @@ * Config file for 2014 Gen 1 board (camera car) * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #ifndef LIBSC_2014_CAMERA_H_ @@ -14,79 +15,60 @@ * define LIBSC_USE_UART to 2 when there are two UART devices connected. Comment * out the specific defines instead of defining them to 0 for unused devices */ -#define LIBSC_USE_UART 1 -// Support 2 encoders only when using FTM -//#define LIBSC_USE_ENCODER 2 -//#define LIBSC_USE_ENCODER_FTM +#define LIBSC_USE_BUTTON 1 +//#define LIBSC_USE_CAMERA 1 +#define LIBSC_USE_ENCODER 2 +#define LIBSC_USE_JOYSTICK 1 #define LIBSC_USE_LCD 1 -#define LIBSC_USE_LCD_HW_SPI #define LIBSC_USE_LED 4 #define LIBSC_USE_MOTOR 2 -#define LIBSC_USE_CAMERA 1 -#define LIBSC_USE_JOYSTICK 1 -#define LIBSC_USE_BUTTON 1 #define LIBSC_USE_MPU6050 1 -#define LIBSC_USE_K60_ENCODERS 1 - #define LIBSC_USE_TEMPERATURE_SENSOR 0 -#define LIBSC_TEMPERATURE0 PTC2 +#define LIBSC_USE_UART 1 -#ifdef LIBSC_USE_ENCODER_FTM -#define LIBSC_ENCODER0_OUT PTB0 -#define LIBSC_ENCODER0_VCC PTB1 -#define LIBSC_ENCODER1_OUT PTB18 -#define LIBSC_ENCODER1_VCC PTB19 +#define LIBSC_ACCELEROMETER_RX libbase::k60::Adc::Name::kAdc0Ad17 -#else -#define LIBSC_ENCODER0 PTB18 +#define LIBSC_BATTERY_METER libbase::k60::Adc::Name::kAdc1Ad4B -#endif +#define LIBSC_BUTTON0 libbase::k60::Pin::Name::kPtc10 -#define LIBSC_LED0 PTE9 -#define LIBSC_LED1 PTE10 -#define LIBSC_LED2 PTE11 -#define LIBSC_LED3 PTE12 -#define LIBSC_MOTOR0_PWM PTD7 -#define LIBSC_MOTOR0_DIR PTE7 -#define LIBSC_MOTOR1_PWM PTD6 -#define LIBSC_MOTOR1_DIR PTE6 +//#define LIBSC_CAMERA_SDA PTB17 +//#define LIBSC_CAMERA_SCL PTB16 +//#define LIBSC_CAMERA_PCLK PTB2 +//#define LIBSC_CAMERA_HREF PTB7 +//#define LIBSC_CAMERA_VSYNC PTB4 +//#define LIBSC_CAMERA_DMA_SRC_ADDR PTD8 +//#define LIBSC_CAMERA_DMA_CH 0 -#define LIBSC_CAMERA_SDA PTB17 -#define LIBSC_CAMERA_SCL PTB16 -#define LIBSC_CAMERA_PCLK PTB2 -#define LIBSC_CAMERA_HREF PTB7 -#define LIBSC_CAMERA_VSYNC PTB4 -#define LIBSC_CAMERA_DMA_SRC_ADDR PTD8 -#define LIBSC_CAMERA_DMA_CH 0 +#define LIBSC_ENCODER0_QDA libbase::k60::Pin::Name::kPtb0 +#define LIBSC_ENCODER0_QDB libbase::k60::Pin::Name::kPtb1 +#define LIBSC_ENCODER1_QDA libbase::k60::Pin::Name::kPtb18 +#define LIBSC_ENCODER1_QDB libbase::k60::Pin::Name::kPtb19 -#ifdef LIBSC_USE_LCD_HW_SPI -#define LIBSC_LCD_RST PTA13 - #define LIBSC_LCD_DC PTA17 - #define LIBSC_LCD_CS PTA14 - // Connect SOUT pin with SDAT - #define LIBSC_LCD_MOSI PTA16 - // MISO pin is not used (one way transfer) but is needed to get the SPI info - #define LIBSC_LCD_MISO PTA17 - #define LIBSC_LCD_SCLK PTA15 -#else - #define LIBSC_LCD_RST PTA13 - #define LIBSC_LCD_DC PTA17 - #define LIBSC_LCD_CS PTA14 - #define LIBSC_LCD_SDAT PTA16 - #define LIBSC_LCD_SCLK PTA15 -#endif +#define LIBSC_JOYSTICK0_UP libbase::k60::Pin::Name::kPtc6 +#define LIBSC_JOYSTICK0_DOWN libbase::k60::Pin::Name::kPtc5 +#define LIBSC_JOYSTICK0_LEFT libbase::k60::Pin::Name::kPtc8 +#define LIBSC_JOYSTICK0_RIGHT libbase::k60::Pin::Name::kPtc7 +#define LIBSC_JOYSTICK0_SELECT libbase::k60::Pin::Name::kPtc9 -#define LIBSC_VR ADC1_SE4b +#define LIBSC_LCD_RST libbase::k60::Pin::Name::kPta13 +#define LIBSC_LCD_DC libbase::k60::Pin::Name::kPta17 +#define LIBSC_LCD_CS libbase::k60::Pin::Name::kPta14 +#define LIBSC_LCD_SDAT libbase::k60::Pin::Name::kPta16 +#define LIBSC_LCD_SCLK libbase::k60::Pin::Name::kPta15 -#define RXADC ADC0_SE17 +#define LIBSC_LED0 libbase::k60::Pin::Name::kPte9 +#define LIBSC_LED1 libbase::k60::Pin::Name::kPte10 +#define LIBSC_LED2 libbase::k60::Pin::Name::kPte11 +#define LIBSC_LED3 libbase::k60::Pin::Name::kPte12 +#define LIBSC_MOTOR0_PWM libbase::k60::Pin::Name::kPtd7 +#define LIBSC_MOTOR0_DIR libbase::k60::Pin::Name::kPte7 +#define LIBSC_MOTOR1_PWM libbase::k60::Pin::Name::kPtd6 +#define LIBSC_MOTOR1_DIR libbase::k60::Pin::Name::kPte6 -#define LIBSC_JOYSTICK0_UP PTC6 -#define LIBSC_JOYSTICK0_DOWN PTC5 -#define LIBSC_JOYSTICK0_LEFT PTC8 -#define LIBSC_JOYSTICK0_RIGHT PTC7 -#define LIBSC_JOYSTICK0_SELECT PTC9 +#define LIBSC_DIR_MOTOR_CW_LEVEL 0 -#define LIBSC_BUTTON0 PTC10 +#define LIBSC_TEMPERATURE0 libbase::k60::Pin::Name::kPtc2 #endif /* LIBSC_2014_CAMERA_H_ */ diff --git a/inc/libsc/k60/config/2014_ccd.h b/inc/libsc/k60/config/2014_ccd.h index 533b8dc..40183ea 100755 --- a/inc/libsc/k60/config/2014_ccd.h +++ b/inc/libsc/k60/config/2014_ccd.h @@ -3,7 +3,8 @@ * Config file for 2014 CCD board * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -16,18 +17,17 @@ #define LIBSC_USE_BUTTON 2 #define LIBSC_USE_BUZZER 1 #define LIBSC_USE_ENCODER 1 -//#define LIBSC_USE_SOFT_ENCODER +//#define LIBSC_USE_SOFT_ENCODER 1 #define LIBSC_USE_GYROSCOPE 1 #define LIBSC_USE_JOYSTICK 1 #define LIBSC_USE_LCD 1 -//#define LIBSC_USE_LCD_HW_SPI #define LIBSC_USE_LED 4 #define LIBSC_USE_LIGHT_SENSOR 2 #define LIBSC_USE_LINEAR_CCD 2 #define LIBSC_NEGATE_LINEAR_CCD #define LIBSC_USE_MOTOR 1 #define LIBSC_USE_SERVO 1 -//#define LIBSC_USE_SOFT_SERVO_PWM +//#define LIBSC_USE_SOFT_SERVO_PWM 1 #define LIBSC_USE_SWITCH 5 #define LIBSC_USE_UART 1 @@ -50,19 +50,12 @@ #define LIBSC_JOYSTICK0_RIGHT libbase::k60::Pin::Name::kPtc7 #define LIBSC_JOYSTICK0_SELECT libbase::k60::Pin::Name::kPtc9 -#ifdef LIBSC_USE_LCD_HW_SPI - #define LIBSC_LCD_RST libbase::k60::Pin::Name::kPta13 - #define LIBSC_LCD_DC libbase::k60::Pin::Name::kPta17 - #define LIBSC_LCD_CS libbase::k60::Pin::Name::kPta14 - #define LIBSC_LCD_SDAT libbase::k60::Pin::Name::kPta16 - #define LIBSC_LCD_SCLK libbase::k60::Pin::Name::kPta15 -#else - #define LIBSC_LCD_RST libbase::k60::Pin::Name::kPta13 - #define LIBSC_LCD_DC libbase::k60::Pin::Name::kPta17 - #define LIBSC_LCD_CS libbase::k60::Pin::Name::kPta14 - #define LIBSC_LCD_SDAT libbase::k60::Pin::Name::kPta16 - #define LIBSC_LCD_SCLK libbase::k60::Pin::Name::kPta15 -#endif +#define LIBSC_LCD_RST libbase::k60::Pin::Name::kPta13 +#define LIBSC_LCD_DC libbase::k60::Pin::Name::kPta17 +#define LIBSC_LCD_CS libbase::k60::Pin::Name::kPta14 +#define LIBSC_LCD_SDAT libbase::k60::Pin::Name::kPta16 +#define LIBSC_LCD_SCLK libbase::k60::Pin::Name::kPta15 +#define LIBSC_ST7735R_BGR_PANEL 1 #define LIBSC_LED0 libbase::k60::Pin::Name::kPtd0 #define LIBSC_LED1 libbase::k60::Pin::Name::kPtd1 diff --git a/inc/libsc/k60/config/2014_inno.h b/inc/libsc/k60/config/2014_inno.h index bb1c666..6f48ea4 100755 --- a/inc/libsc/k60/config/2014_inno.h +++ b/inc/libsc/k60/config/2014_inno.h @@ -2,12 +2,13 @@ * 2014_inno.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ #pragma once +#define LIBSC_USE_BATTERY_METER 1 #define LIBSC_USE_BUTTON 2 //#define LIBSC_USE_BUZZER 1 //#define LIBSC_USE_JOYSTICK 1 @@ -16,6 +17,8 @@ #define LIBSC_USE_SERVO 1 #define LIBSC_USE_UART 1 +#define LIBSC_BATTERY_METER libbase::k60::Adc::Name::kAdc1Ad17 + #define LIBSC_BUTTON0 libbase::k60::Pin::Name::kPtc0 #define LIBSC_BUTTON1 libbase::k60::Pin::Name::kPtb23 @@ -42,3 +45,6 @@ #define LIBSC_UART0_TX libbase::k60::Pin::Name::kPtd7 #define LIBSC_UART0_RX libbase::k60::Pin::Name::kPtd6 + +#define LIBSC_MPU6050_SCL libbase::k60::Pin::Name::kPtb0 +#define LIBSC_MPU6050_SDA libbase::k60::Pin::Name::kPtb1 diff --git a/inc/libsc/k60/config/2014_magnetic.h b/inc/libsc/k60/config/2014_magnetic.h index 0afb318..29e8e41 100644 --- a/inc/libsc/k60/config/2014_magnetic.h +++ b/inc/libsc/k60/config/2014_magnetic.h @@ -3,7 +3,8 @@ * Config file for 2014 Magnetic board * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #ifndef LIBSC_2014_MAGNETIC_H_ diff --git a/inc/libsc/k60/config/inno.h b/inc/libsc/k60/config/inno.h index 529fc05..9ce391b 100644 --- a/inc/libsc/k60/config/inno.h +++ b/inc/libsc/k60/config/inno.h @@ -3,7 +3,8 @@ * Config file for Innovation Carnival board * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/config/vcan_fx15dev.h b/inc/libsc/k60/config/vcan_fx15dev.h new file mode 100755 index 0000000..1290e68 --- /dev/null +++ b/inc/libsc/k60/config/vcan_fx15dev.h @@ -0,0 +1,41 @@ +/* + * vcan_fx15dev.h + * Config file for the VCAN K60FX15 development board + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#define LIBSC_USE_BUTTON 1 +#define LIBSC_USE_LCD 1 +#define LIBSC_USE_LCD_HW_SPI +#define LIBSC_USE_LED 4 +#define LIBSC_USE_MOTOR 1 +#define LIBSC_USE_SERVO 1 +#define LIBSC_USE_UART 1 + +#define LIBSC_BUTTON0 libbase::k60::Pin::Name::kPtd7 + +#define LIBSC_LCD_RST libbase::k60::Pin::Name::kPta13 +#define LIBSC_LCD_DC libbase::k60::Pin::Name::kPta17 +#define LIBSC_LCD_CS libbase::k60::Pin::Name::kPta14 +#define LIBSC_LCD_SDAT libbase::k60::Pin::Name::kPta16 +#define LIBSC_LCD_SCLK libbase::k60::Pin::Name::kPta15 + +#define LIBSC_LED0 libbase::k60::Pin::Name::kPtb20 +#define LIBSC_LED1 libbase::k60::Pin::Name::kPtb21 +#define LIBSC_LED2 libbase::k60::Pin::Name::kPtb22 +#define LIBSC_LED3 libbase::k60::Pin::Name::kPtb23 + +#define LIBSC_MOTOR0_PWMA libbase::k60::Pin::Name::kPtc1 +#define LIBSC_MOTOR0_PWMB libbase::k60::Pin::Name::kPtc2 +#define LIBSC_MOTOR0_DEADTIME 1000 +#define LIBSC_ALTERNATE_MOTOR_CW_PWM 1 + +#define LIBSC_SERVO0 libbase::k60::Pin::Name::kPtd0 + +#define LIBSC_UART0_TX libbase::k60::Pin::Name::kPtd7 +#define LIBSC_UART0_RX libbase::k60::Pin::Name::kPtd6 diff --git a/inc/libsc/k60/delay.h b/inc/libsc/k60/delay.h index e4a7e97..917ea7b 100755 --- a/inc/libsc/k60/delay.h +++ b/inc/libsc/k60/delay.h @@ -2,7 +2,8 @@ * delay.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/dir_encoder.h b/inc/libsc/k60/dir_encoder.h new file mode 100755 index 0000000..45a6304 --- /dev/null +++ b/inc/libsc/k60/dir_encoder.h @@ -0,0 +1,38 @@ +/* + * dir_encoder.h + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#include "libsc/config.h" +#include "libsc/k60/encoder.h" + +namespace libsc +{ +namespace k60 +{ + +class DirEncoder : public Encoder +{ +public: + typedef Encoder::Config Config; + + explicit DirEncoder(const Config &config); + +protected: + struct Initializer : public Encoder::Initializer + { + explicit Initializer(const Config &config) + : Encoder::Initializer(config) + {} + + QuadDecoder::Config GetQuadDecoderConfig() const override; + }; +}; + +} +} diff --git a/inc/libsc/k60/dir_motor.h b/inc/libsc/k60/dir_motor.h index 3399cff..e5050c8 100755 --- a/inc/libsc/k60/dir_motor.h +++ b/inc/libsc/k60/dir_motor.h @@ -2,7 +2,7 @@ * dir_motor.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ @@ -28,7 +28,7 @@ class DirMotor : public Motor uint8_t id; }; - DirMotor(const Config &config); + explicit DirMotor(const Config &config); private: void OnSetPower(const uint16_t power) override; diff --git a/inc/libsc/k60/temperature_sensor.h b/inc/libsc/k60/ds18b20.h similarity index 63% rename from inc/libsc/k60/temperature_sensor.h rename to inc/libsc/k60/ds18b20.h index 9a39c6b..fe009f7 100755 --- a/inc/libsc/k60/temperature_sensor.h +++ b/inc/libsc/k60/ds18b20.h @@ -1,9 +1,10 @@ /* - * temperature_sensor.h - * DS18B20 temperature sensor + * ds18b20.h + * DS18B20 digital thermometer * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -17,10 +18,15 @@ namespace libsc namespace k60 { -class TemperatureSensor +class Ds18b20 { public: - explicit TemperatureSensor(const uint8_t id); + struct Config + { + uint8_t id; + }; + + explicit Ds18b20(const Config &config); void UpdateTemperature(); diff --git a/inc/libsc/k60/dwt_delay.h b/inc/libsc/k60/dwt_delay.h index abb62ac..b08a965 100755 --- a/inc/libsc/k60/dwt_delay.h +++ b/inc/libsc/k60/dwt_delay.h @@ -2,7 +2,8 @@ * dwt_delay.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/encoder.h b/inc/libsc/k60/encoder.h index c73e579..f0d684b 100644 --- a/inc/libsc/k60/encoder.h +++ b/inc/libsc/k60/encoder.h @@ -5,7 +5,8 @@ * emulation will pose a large overhead to the system * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -26,7 +27,18 @@ namespace k60 class Encoder { public: - explicit Encoder(const uint8_t id); +#if LIBSC_USE_SOFT_ENCODER + typedef libbase::k60::SoftQuadDecoder QuadDecoder; + +#else + typedef libbase::k60::FtmQuadDecoder QuadDecoder; + +#endif // LIBSC_USE_SOFT_ENCODER + + struct Config + { + uint8_t id; + }; void Update(); @@ -40,20 +52,28 @@ class Encoder return m_count; } -private: - const uint8_t m_id; +protected: + /** + * Use to initialize the encoder in possibly a polymorphic way, notice that + * Initializer::config is stored as a reference only + */ + struct Initializer + { + explicit Initializer(const Config &config) + : config(config) + {} - int32_t m_count; + virtual QuadDecoder::Config GetQuadDecoderConfig() const; -#ifdef LIBSC_USE_ENCODER -#ifdef LIBSC_USE_SOFT_ENCODER - libbase::k60::SoftQuadDecoder m_quad_decoder; + const Config &config; + }; -#else - libbase::k60::FtmQuadDecoder m_quad_decoder; + explicit Encoder(const Initializer &initializer); -#endif // LIBSC_USE_SOFT_ENCODER -#endif // LIBSC_USE_ENCODER +private: + int32_t m_count; + + QuadDecoder m_quad_decoder; }; } diff --git a/src/libsc/k60/ftdi_ft232r.h b/inc/libsc/k60/ftdi_ft232r.h similarity index 84% rename from src/libsc/k60/ftdi_ft232r.h rename to inc/libsc/k60/ftdi_ft232r.h index 8ad0852..71aa548 100755 --- a/src/libsc/k60/ftdi_ft232r.h +++ b/inc/libsc/k60/ftdi_ft232r.h @@ -3,7 +3,7 @@ * Generic class for the FTDI FT232R USB UART IC * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ diff --git a/inc/libsc/k60/futaba_s3010.h b/inc/libsc/k60/futaba_s3010.h index 2670771..c548fe4 100755 --- a/inc/libsc/k60/futaba_s3010.h +++ b/inc/libsc/k60/futaba_s3010.h @@ -3,7 +3,8 @@ * Futaba S3010 RC servo * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -20,7 +21,12 @@ namespace k60 class FutabaS3010 : public Servo { public: - explicit FutabaS3010(const uint8_t id); + struct Config + { + uint8_t id; + }; + + explicit FutabaS3010(const Config &config); }; } diff --git a/inc/libsc/k60/gyroscope.h b/inc/libsc/k60/gyroscope.h deleted file mode 100755 index c610e13..0000000 --- a/inc/libsc/k60/gyroscope.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * gyroscope.h - * Gyroscope - * - * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team - */ - -#pragma once - -#include - -#include "libbase/k60/adc.h" - -namespace libsc -{ -namespace k60 -{ - -class Gyroscope -{ -public: - Gyroscope(); - explicit Gyroscope(const Uint avg_size); - ~Gyroscope(); - - void Update(); - - uint16_t GetAngle() const - { - return m_current; - } - - float GetAverageAngle() const - { - return m_avg; - } - -private: - libbase::k60::Adc m_pin; - - uint16_t m_current; - float m_avg; - uint16_t *m_values; - Uint m_size; - Uint m_index; -}; - -} -} diff --git a/inc/libsc/k60/infra_red_sensor.h b/inc/libsc/k60/infra_red_sensor.h index 0973d04..15076a3 100755 --- a/inc/libsc/k60/infra_red_sensor.h +++ b/inc/libsc/k60/infra_red_sensor.h @@ -3,7 +3,8 @@ * Infra-red sensor * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -24,14 +25,19 @@ class InfraRedSensor public: typedef std::function OnDetectListener; - InfraRedSensor(const uint8_t id, const OnDetectListener &listener); - explicit InfraRedSensor(const uint8_t id); + struct Config + { + uint8_t id; + OnDetectListener listener; + }; + + explicit InfraRedSensor(const Config &config); bool IsDetected() const; private: - libbase::k60::Gpi m_pin; OnDetectListener m_isr; + libbase::k60::Gpi m_pin; }; } diff --git a/inc/libsc/k60/joystick.h b/inc/libsc/k60/joystick.h index 7fd4a86..f280901 100755 --- a/inc/libsc/k60/joystick.h +++ b/inc/libsc/k60/joystick.h @@ -1,9 +1,9 @@ /* * joystick.h - * Joystick * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -30,12 +30,19 @@ class Joystick SELECT, }; - explicit Joystick(const uint8_t id); + struct Config + { + uint8_t id; + bool is_active_low; + }; + + explicit Joystick(const Config &config); State GetState() const; private: libbase::k60::Gpi m_pins[5]; + bool m_is_active_low; }; } diff --git a/inc/libsc/k60/jy_mcu_bt_106.h b/inc/libsc/k60/jy_mcu_bt_106.h index 3510a51..c65f8c1 100755 --- a/inc/libsc/k60/jy_mcu_bt_106.h +++ b/inc/libsc/k60/jy_mcu_bt_106.h @@ -3,7 +3,7 @@ * JY-MCU BT Board v1.06 * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ @@ -23,17 +23,13 @@ namespace k60 class JyMcuBt106 : public UartDevice { public: - JyMcuBt106(const uint8_t id, - const libbase::k60::Uart::Config::BaudRate baud_rate); - -protected: - class UartConfigBuilder : public UartDevice::UartConfigBuilder + struct Config { - public: - using UartDevice::UartConfigBuilder::UartConfigBuilder; - - virtual libbase::k60::Uart::Config Build() const override; + uint8_t id; + libbase::k60::Uart::Config::BaudRate baud_rate; }; + + explicit JyMcuBt106(const Config &config); }; } diff --git a/inc/libsc/k60/lcd_console.h b/inc/libsc/k60/lcd_console.h index 92413a4..ba91c10 100644 --- a/inc/libsc/k60/lcd_console.h +++ b/inc/libsc/k60/lcd_console.h @@ -3,7 +3,8 @@ * Use the LCD screen as console * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/led.h b/inc/libsc/k60/led.h index f219518..bc45a16 100644 --- a/inc/libsc/k60/led.h +++ b/inc/libsc/k60/led.h @@ -2,7 +2,8 @@ * led.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -19,7 +20,12 @@ namespace k60 class Led { public: - explicit Led(const uint8_t id); + struct Config + { + uint8_t id; + }; + + explicit Led(const Config &config); void SetEnable(const bool flag); void Switch(); diff --git a/inc/libsc/k60/light_sensor.h b/inc/libsc/k60/light_sensor.h old mode 100755 new mode 100644 index e2ce074..60312f5 --- a/inc/libsc/k60/light_sensor.h +++ b/inc/libsc/k60/light_sensor.h @@ -1,9 +1,9 @@ /* * light_sensor.h - * Light sensor * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -22,16 +22,36 @@ namespace k60 class LightSensor { public: - typedef std::function OnDetectListener; - - LightSensor(const uint8_t id, const OnDetectListener &listener); - explicit LightSensor(const uint8_t id); - - bool IsDetected() const; + typedef std::function Listener; + + struct Config + { + enum struct Trigger + { + kBright, + kDark, + kBoth, + }; + + uint8_t id; + bool is_active_low; + Listener listener; + /// When to trigger the listener, ignored is @a listener is not set + Trigger listener_trigger; + }; + + explicit LightSensor(const Config &config); + + bool IsBright() const; + bool IsDark() const + { + return !IsBright(); + } private: libbase::k60::Gpi m_pin; - OnDetectListener m_isr; + bool m_is_active_low; + Listener m_isr; }; } diff --git a/inc/libsc/k60/linear_ccd.h b/inc/libsc/k60/linear_ccd.h index 37c7fb7..30d40ac 100644 --- a/inc/libsc/k60/linear_ccd.h +++ b/inc/libsc/k60/linear_ccd.h @@ -3,7 +3,8 @@ * Linear CCD (for TSL1401CL) * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/motor.h b/inc/libsc/k60/motor.h index b0827cc..9e2b689 100644 --- a/inc/libsc/k60/motor.h +++ b/inc/libsc/k60/motor.h @@ -2,7 +2,7 @@ * motor.h * * Author: Harrison Ng, Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ diff --git a/inc/libsc/k60/mpu6050.h b/inc/libsc/k60/mpu6050.h new file mode 100644 index 0000000..65ef105 --- /dev/null +++ b/inc/libsc/k60/mpu6050.h @@ -0,0 +1,76 @@ +/* + * mpu6050.h + * + * Author: Harrison Ng, Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#include +#include + +#include "libbase/k60/soft_i2c_master.h" +#include "libbase/misc_types.h" + +using namespace libbase::k60; + +namespace libsc +{ +namespace k60 +{ + +class Mpu6050 +{ +public: + struct Config + { + enum struct Range + { + kSmall = 0, + kMid, + kLarge, + kExtreme, + }; + + // kSmall -> kExtreme = ±250°/s, ±500°/s, ±1000°/s, ±2000°/s + Range gyro_range; + // kSmall -> kExtreme = ±2g, ±4g, ±8g, ±16g + Range accel_range; + }; + + explicit Mpu6050(const Config &config); + + void Update(); + + const std::array& GetAccel() + { + return m_accel; + } + + const std::array& GetOmega() + { + return m_omega; + } + + float GetCelsius() + { + return m_temp; + } + +private: + float GetGyroScaleFactor(); + float GetAccelScaleFactor(); + + libbase::k60::SoftI2cMaster m_i2c; + std::array m_accel; + std::array m_omega; + float m_temp; + + Config::Range m_gyro_range; + Config::Range m_accel_range; +}; + +} /* namespace k60 */ +} /* namespace libsc */ diff --git a/inc/libsc/k60/pit_delay.h b/inc/libsc/k60/pit_delay.h index 5e8510d..6fdf687 100755 --- a/inc/libsc/k60/pit_delay.h +++ b/inc/libsc/k60/pit_delay.h @@ -2,7 +2,8 @@ * pit_delay.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/pit_timer.h b/inc/libsc/k60/pit_timer.h index 424c891..8823d48 100755 --- a/inc/libsc/k60/pit_timer.h +++ b/inc/libsc/k60/pit_timer.h @@ -2,7 +2,8 @@ * pit_timer.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/servo.h b/inc/libsc/k60/servo.h index 48eaa2e..b37be67 100644 --- a/inc/libsc/k60/servo.h +++ b/inc/libsc/k60/servo.h @@ -3,7 +3,8 @@ * RC servo * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -23,6 +24,19 @@ namespace k60 class Servo { public: + struct Config + { + uint8_t id; + // The period of the PWM signal, in us + uint16_t period; + // The min pos width of the PWM signal, in us + uint16_t min_pos_width; + // The max pos width of the PWM signal, in us + uint16_t max_pos_width; + }; + + explicit Servo(const Config &config); + /** * Set the degree(the servo turn in CCW), [0, 180] * 10 (i.e., 84 => 8.4°) * @@ -34,18 +48,6 @@ class Servo return m_degree; } -protected: - /** - * Construct a RC Servo - * - * @param id - * @param period The period of the PWM signal, in us - * @param pos_width_min The min pos width of the PWM signal, in us - * @param pos_width_max The max pos width of the PWM signal, in us - */ - Servo(const uint8_t id, const uint16_t period, const uint16_t pos_width_min, - const uint16_t pos_width_max); - private: const uint16_t m_pos_width_min; const uint16_t m_pos_width_diff; diff --git a/inc/libsc/k60/simple_buzzer.h b/inc/libsc/k60/simple_buzzer.h index 5fd3a61..b3e8114 100755 --- a/inc/libsc/k60/simple_buzzer.h +++ b/inc/libsc/k60/simple_buzzer.h @@ -3,7 +3,8 @@ * Simple Buzzer * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/spi_device.h b/inc/libsc/k60/spi_device.h deleted file mode 100755 index 36411b1..0000000 --- a/inc/libsc/k60/spi_device.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * spi_device.h - * - * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team - */ - -#pragma once - -#include -#include - -#include -#include -#include -#include - -#include "libbase/k60/misc_utils.h" -#include "libbase/k60/spi_master.h" - -#include "libutil/dynamic_block_buffer.h" - -namespace libsc -{ -namespace k60 -{ - -class SpiDevice -{ -public: - explicit SpiDevice(const libbase::k60::SpiMaster::Config &config); - explicit SpiDevice(nullptr_t); - SpiDevice(const SpiDevice&) = delete; - SpiDevice(SpiDevice &&rhs); - ~SpiDevice(); - - /** - * Send a buffer through SPI. A copy will be queued - * - * @param buf - * @param len - */ - void SendBuffer(const Byte *buf, const size_t len); - void SendBuffer(const uint16_t *buf, const size_t len) - { - SendBuffer(reinterpret_cast(buf), len * 2); - } - /** - * Send a buffer through SPI. A moved copy will be queued - * - * @param buf - * @param len - */ - void SendBuffer(std::unique_ptr &&buf, const size_t len); - void SendBuffer(std::unique_ptr &&buf, const size_t len) - { - SendBuffer(std::unique_ptr(reinterpret_cast(buf.release())), - len * 2); - } - /** - * Send a buffer through SPI. A moved copy will be queued - * - * @param buf - */ - void SendBuffer(std::vector &&buf); - -private: - struct Buffer; - - void OnTxFill(libbase::k60::SpiMaster *spi); - void OnRxDrain(libbase::k60::SpiMaster *spi); - - //std::list m_rx_buf; - libutil::DynamicBlockBuffer m_tx_buf; - - libbase::k60::SpiMaster m_spi; -}; - -} -} diff --git a/inc/libsc/k60/st7735r.h b/inc/libsc/k60/st7735r.h index a67927e..879b57b 100644 --- a/inc/libsc/k60/st7735r.h +++ b/inc/libsc/k60/st7735r.h @@ -2,7 +2,8 @@ * st7735r.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -21,13 +22,17 @@ namespace k60 class St7735r { public: + struct Config + { + bool is_revert = false; + }; + static constexpr uint8_t kW = 128; static constexpr uint8_t kH = 160; static constexpr uint8_t kFontW = 8; static constexpr uint8_t kFontH = 16; - St7735r(); - explicit St7735r(const bool is_revert); + explicit St7735r(const Config &config); void Clear(); void Clear(const uint16_t color) diff --git a/inc/libsc/k60/switch.h b/inc/libsc/k60/switch.h index a510667..62d6437 100755 --- a/inc/libsc/k60/switch.h +++ b/inc/libsc/k60/switch.h @@ -1,9 +1,9 @@ /* * switch.h - * Switch * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -20,7 +20,14 @@ namespace k60 class Switch { public: - explicit Switch(const uint8_t id); + struct Config + { + uint8_t id; + // on == active + bool is_active_low; + }; + + explicit Switch(const Config &config); bool IsOn() const; bool IsOff() const @@ -30,6 +37,7 @@ class Switch private: libbase::k60::Gpi m_pin; + bool m_is_active_low; }; } diff --git a/inc/libsc/k60/sys_tick_delay.h b/inc/libsc/k60/sys_tick_delay.h index 6447c57..9d635f6 100755 --- a/inc/libsc/k60/sys_tick_delay.h +++ b/inc/libsc/k60/sys_tick_delay.h @@ -2,7 +2,8 @@ * sys_tick_delay.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/sys_tick_timer.h b/inc/libsc/k60/sys_tick_timer.h index 8b855d3..33fc4ba 100755 --- a/inc/libsc/k60/sys_tick_timer.h +++ b/inc/libsc/k60/sys_tick_timer.h @@ -2,7 +2,8 @@ * sys_tick_timer.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/system.h b/inc/libsc/k60/system.h index e8651d7..93f7ef0 100755 --- a/inc/libsc/k60/system.h +++ b/inc/libsc/k60/system.h @@ -2,7 +2,8 @@ * system.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/timer.h b/inc/libsc/k60/timer.h index c511b93..bdef09d 100644 --- a/inc/libsc/k60/timer.h +++ b/inc/libsc/k60/timer.h @@ -2,7 +2,8 @@ * timer.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libsc/k60/tower_pro_mg995.h b/inc/libsc/k60/tower_pro_mg995.h index 851cbb4..e7f38b4 100755 --- a/inc/libsc/k60/tower_pro_mg995.h +++ b/inc/libsc/k60/tower_pro_mg995.h @@ -3,7 +3,7 @@ * TowerPro MG995 RC servo * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ @@ -21,7 +21,12 @@ namespace k60 class TowerProMg995 : public Servo { public: - explicit TowerProMg995(const uint8_t id); + struct Config + { + uint8_t id; + }; + + explicit TowerProMg995(const Config &config); }; } diff --git a/inc/libsc/k60/trs_d05.h b/inc/libsc/k60/trs_d05.h index 4eb9c44..fba5904 100755 --- a/inc/libsc/k60/trs_d05.h +++ b/inc/libsc/k60/trs_d05.h @@ -3,7 +3,8 @@ * TRS-D05 RC servo * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once @@ -20,7 +21,12 @@ namespace k60 class TrsD05 : public Servo { public: - explicit TrsD05(const uint8_t id); + struct Config + { + uint8_t id; + }; + + explicit TrsD05(const Config &config); }; } diff --git a/inc/libsc/k60/uart_device.h b/inc/libsc/k60/uart_device.h index 22d7503..85265e7 100644 --- a/inc/libsc/k60/uart_device.h +++ b/inc/libsc/k60/uart_device.h @@ -3,7 +3,7 @@ * Generic class for UART devices, also handles Tx and Rx buffering * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ @@ -12,12 +12,14 @@ #include #include +#include #include #include #include #include #include "libbase/k60/misc_utils.h" +#include "libbase/k60/pin.h" #include "libbase/k60/uart.h" #include "libutil/dynamic_block_buffer.h" @@ -33,8 +35,23 @@ class UartDevice typedef std::function OnReceiveListener; - UartDevice(const uint8_t id, - const libbase::k60::Uart::Config::BaudRate baud_rate); + struct Config + { + uint8_t id; + libbase::k60::Uart::Config::BaudRate baud_rate; + /** + * The # bytes in the Rx buffer needed to fire the interrupt. This will + * affect how often new bytes are pushed to the internal buffer, or your + * listener being triggered, depending on the config + */ + uint8_t rx_irq_threshold; + /// To treat rx_irq_threshold as a percentage of Rx buffer size + bool is_rx_irq_threshold_percentage = false; + std::bitset tx_config; + std::bitset rx_config; + }; + + explicit UartDevice(const Config &config); virtual ~UartDevice(); /** @@ -107,22 +124,6 @@ class UartDevice m_uart.SetLoopMode(flag); } -protected: - class UartConfigBuilder - { - public: - UartConfigBuilder(const uint8_t id, - const libbase::k60::Uart::Config::BaudRate baud_rate, - UartDevice *uart); - - virtual libbase::k60::Uart::Config Build() const; - - private: - libbase::k60::Uart::Config m_config; - }; - - explicit UartDevice(const UartConfigBuilder &builder); - private: struct RxBuffer; diff --git a/inc/libsc/lib_guard.h b/inc/libsc/lib_guard.h index c665299..d70d311 100755 --- a/inc/libsc/lib_guard.h +++ b/inc/libsc/lib_guard.h @@ -3,7 +3,8 @@ * Protect you from unmatched symbol and linked library * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libutil/dynamic_block_buffer.h b/inc/libutil/dynamic_block_buffer.h index 0c44fcf..4f167a0 100755 --- a/inc/libutil/dynamic_block_buffer.h +++ b/inc/libutil/dynamic_block_buffer.h @@ -2,7 +2,8 @@ * dynamic_block_buffer.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libutil/incremental_pid_controller.h b/inc/libutil/incremental_pid_controller.h index 1bcba71..9177190 100755 --- a/inc/libutil/incremental_pid_controller.h +++ b/inc/libutil/incremental_pid_controller.h @@ -3,7 +3,7 @@ * Generic incremental PID controller * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ diff --git a/inc/libutil/incremental_pid_controller.tcc b/inc/libutil/incremental_pid_controller.tcc index e9c1cab..4263260 100755 --- a/inc/libutil/incremental_pid_controller.tcc +++ b/inc/libutil/incremental_pid_controller.tcc @@ -3,7 +3,7 @@ * Generic incremental PID controller * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ diff --git a/inc/libutil/kalman_filter.h b/inc/libutil/kalman_filter.h index e40119d..3fa3302 100755 --- a/inc/libutil/kalman_filter.h +++ b/inc/libutil/kalman_filter.h @@ -2,7 +2,8 @@ * kalman_filter.h * * Author: Tommy Wong - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #pragma once diff --git a/inc/libutil/looper.h b/inc/libutil/looper.h new file mode 100755 index 0000000..70ac623 --- /dev/null +++ b/inc/libutil/looper.h @@ -0,0 +1,43 @@ +/* + * looper.h + * Handle main loop and periodically invoking callbacks + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#pragma once + +#include +#include + +#include "libsc/k60/timer.h" + +namespace libutil +{ + +class Looper +{ +public: + typedef std::function Callback; + + Looper(); + ~Looper(); + + void Loop(); + void Break(); + + void RunAfter(const libsc::k60::Timer::TimerInt ms, const Callback &c); + +private: + struct RunnerState; + + void Invoke(); + + std::list m_states; + bool m_is_run; +}; + +} diff --git a/inc/libutil/misc.h b/inc/libutil/misc.h index dfde51c..c043cbd 100644 --- a/inc/libutil/misc.h +++ b/inc/libutil/misc.h @@ -3,7 +3,8 @@ * Misc util * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #ifndef LIBUTIL_MISC_H_ diff --git a/inc/libutil/positional_pid_controller.h b/inc/libutil/positional_pid_controller.h index bed9a5c..bb843a4 100755 --- a/inc/libutil/positional_pid_controller.h +++ b/inc/libutil/positional_pid_controller.h @@ -3,7 +3,7 @@ * Generic poitional PID controller * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ diff --git a/inc/libutil/positional_pid_controller.tcc b/inc/libutil/positional_pid_controller.tcc index 30ddb90..ffc8cf9 100755 --- a/inc/libutil/positional_pid_controller.tcc +++ b/inc/libutil/positional_pid_controller.tcc @@ -3,7 +3,7 @@ * Generic poitional PID controller * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ diff --git a/inc/libutil/remote_var_manager.h b/inc/libutil/remote_var_manager.h index 43072e5..9e4f974 100755 --- a/inc/libutil/remote_var_manager.h +++ b/inc/libutil/remote_var_manager.h @@ -2,7 +2,7 @@ * remote_var_manager.h * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ diff --git a/inc/libutil/string.h b/inc/libutil/string.h index f2fcd32..ffc32fb 100755 --- a/inc/libutil/string.h +++ b/inc/libutil/string.h @@ -3,7 +3,8 @@ * String utilities * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #ifndef LIBUTIL_STRING_H_ diff --git a/src/libbase/assert.c b/src/libbase/assert.c index 9e0678f..c38f30f 100755 --- a/src/libbase/assert.c +++ b/src/libbase/assert.c @@ -2,7 +2,8 @@ * assert.c * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libbase/k60/adc.cpp b/src/libbase/k60/adc.cpp index 9938c4f..3efd072 100755 --- a/src/libbase/k60/adc.cpp +++ b/src/libbase/k60/adc.cpp @@ -2,7 +2,8 @@ * adc.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" @@ -30,8 +31,6 @@ using namespace libutil; using namespace std; -#define ADC_COUNT 2 - namespace libbase { namespace k60 @@ -40,9 +39,19 @@ namespace k60 namespace { -constexpr ADC_Type* MEM_MAPS[ADC_COUNT] = {ADC0, ADC1}; - -Adc* g_instances[ADC_COUNT] = {}; +constexpr ADC_Type* MEM_MAPS[PINOUT::GetAdcCount()] = +{ + ADC0, + ADC1, +#if PINOUT_ADC_COUNT > 2 + ADC2, +#endif +#if PINOUT_ADC_COUNT > 3 + ADC3, +#endif +}; + +Adc* g_instances[PINOUT::GetAdcCount()] = {}; Uint GetClockDivider(const Uint target_clock_khz) { @@ -154,127 +163,8 @@ Adc& Adc::operator=(Adc &&rhs) bool Adc::InitModule(const Pin::Name pin) { - switch (pin) - { - case Pin::Name::kPta7: - m_name = Name::kAdc0Ad10; - return true; - - case Pin::Name::kPta8: - m_name = Name::kAdc0Ad11; - return true; - - case Pin::Name::kPta17: - m_name = Name::kAdc1Ad17; - return true; - - case Pin::Name::kPtb0: - m_name = Name::kAdc0Ad8; - return true; - - case Pin::Name::kPtb1: - m_name = Name::kAdc0Ad9; - return true; - - case Pin::Name::kPtb2: - m_name = Name::kAdc0Ad12; - return true; - - case Pin::Name::kPtb3: - m_name = Name::kAdc0Ad13; - return true; - - case Pin::Name::kPtb4: - m_name = Name::kAdc1Ad10; - return true; - - case Pin::Name::kPtb5: - m_name = Name::kAdc1Ad11; - return true; - - case Pin::Name::kPtb6: - m_name = Name::kAdc1Ad12; - return true; - - case Pin::Name::kPtb7: - m_name = Name::kAdc1Ad13; - return true; - - case Pin::Name::kPtb10: - m_name = Name::kAdc1Ad14; - return true; - - case Pin::Name::kPtb11: - m_name = Name::kAdc1Ad15; - return true; - - case Pin::Name::kPtc0: - m_name = Name::kAdc0Ad14; - return true; - - case Pin::Name::kPtc1: - m_name = Name::kAdc0Ad15; - return true; - - case Pin::Name::kPtc2: - m_name = Name::kAdc0Ad4B; - return true; - - case Pin::Name::kPtc8: - m_name = Name::kAdc1Ad4B; - return true; - - case Pin::Name::kPtc9: - m_name = Name::kAdc1Ad5B; - return true; - - case Pin::Name::kPtc10: - m_name = Name::kAdc1Ad6B; - return true; - - case Pin::Name::kPtc11: - m_name = Name::kAdc1Ad7B; - return true; - - case Pin::Name::kPtd1: - m_name = Name::kAdc0Ad5B; - return true; - - case Pin::Name::kPtd5: - m_name = Name::kAdc0Ad6B; - return true; - - case Pin::Name::kPtd6: - m_name = Name::kAdc0Ad7B; - return true; - - case Pin::Name::kPte0: - m_name = Name::kAdc1Ad4A; - return true; - - case Pin::Name::kPte1: - m_name = Name::kAdc1Ad5A; - return true; - - case Pin::Name::kPte2: - m_name = Name::kAdc1Ad6A; - return true; - - case Pin::Name::kPte3: - m_name = Name::kAdc1Ad7A; - return true; - - case Pin::Name::kPte24: - m_name = Name::kAdc0Ad17; - return true; - - case Pin::Name::kPte25: - m_name = Name::kAdc0Ad18; - return true; - - default: - return false; - } + m_name = PINOUT::GetAdc(pin); + return (m_name != Adc::Name::kDisable); } bool Adc::InitModule(const Name adc) @@ -487,6 +377,28 @@ uint16_t Adc::GetResult() return result; } +float Adc::GetResultF() +{ + // we can't predivide 3.3 and the resolution because the number is too small + // i.e., precision problem + const float multiplied = GetResult() * 3.3f; + switch (m_config.resolution) + { + case Config::Resolution::k8Bit: + return multiplied / 0x00FF; + + case Config::Resolution::k10Bit: + return multiplied / 0x03FF; + + case Config::Resolution::k12Bit: + return multiplied / 0x0FFF; + + default: + case Config::Resolution::k16Bit: + return multiplied / 0xFFFF; + } +} + bool Adc::PeekResult(uint16_t *out_val) { STATE_GUARD(Adc, false); @@ -553,22 +465,53 @@ void Adc::EnableInterrupt() const Uint module = AdcUtils::GetModule(m_name); g_instances[module] = this; - SetIsr(EnumAdvance(ADC0_IRQn, module), IrqHandler); - EnableIrq(EnumAdvance(ADC0_IRQn, module)); + if (module < 2) + { + SetIsr(EnumAdvance(ADC0_IRQn, module), IrqHandler); + EnableIrq(EnumAdvance(ADC0_IRQn, module)); + } +#if PINOUT_ADC_COUNT > 2 + else if (module < 4) + { + SetIsr(EnumAdvance(ADC2_IRQn, module - 2), IrqHandler); + EnableIrq(EnumAdvance(ADC2_IRQn, module - 2)); + } +#endif SET_BIT(MEM_MAPS[module]->SC1[0], ADC_SC1_AIEN_SHIFT); } void Adc::DisableInterrupt(const Uint module) { CLEAR_BIT(MEM_MAPS[module]->SC1[0], ADC_SC1_AIEN_SHIFT); - DisableIrq(EnumAdvance(ADC0_IRQn, module)); - SetIsr(EnumAdvance(ADC0_IRQn, module), nullptr); + if (module < 2) + { + DisableIrq(EnumAdvance(ADC0_IRQn, module)); + SetIsr(EnumAdvance(ADC0_IRQn, module), nullptr); + } +#if PINOUT_ADC_COUNT > 2 + else if (module < 4) + { + DisableIrq(EnumAdvance(ADC2_IRQn, module - 2)); + SetIsr(EnumAdvance(ADC2_IRQn, module - 2), nullptr); + } +#endif g_instances[module] = nullptr; } __ISR void Adc::IrqHandler() { - const Uint module = GetActiveIrq() - ADC0_IRQn; + Uint module = 0; + if (module < 2) + { + module = GetActiveIrq() - ADC0_IRQn; + } +#if PINOUT_ADC_COUNT > 2 + else if (module < 4) + { + module = GetActiveIrq() - ADC2_IRQn; + } +#endif + Adc *const that = g_instances[module]; if (!that || !(*that)) { diff --git a/src/libbase/k60/clock_utils.cpp b/src/libbase/k60/clock_utils.cpp index 51053c2..a91e5e8 100755 --- a/src/libbase/k60/clock_utils.cpp +++ b/src/libbase/k60/clock_utils.cpp @@ -2,7 +2,8 @@ * clock_utils.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" @@ -27,14 +28,14 @@ uint32_t GetBusClock_() >> SIM_CLKDIV1_OUTDIV1_SHIFT) + 1; const Uint bus_div = ((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV2_MASK) >> SIM_CLKDIV1_OUTDIV2_SHIFT) + 1; - return Mcg::GetCoreClock() * core_div / bus_div; + return Mcg::Get().GetCoreClock() * core_div / bus_div; } } uint32_t ClockUtils::GetCoreClock() { - return Mcg::GetCoreClock(); + return Mcg::Get().GetCoreClock(); } uint32_t ClockUtils::GetCoreClockKhz() diff --git a/src/libbase/k60/cmsis/startup_mk60dz10.c b/src/libbase/k60/cmsis/startup.c similarity index 96% rename from src/libbase/k60/cmsis/startup_mk60dz10.c rename to src/libbase/k60/cmsis/startup.c index 5272faa..0e013ff 100755 --- a/src/libbase/k60/cmsis/startup_mk60dz10.c +++ b/src/libbase/k60/cmsis/startup.c @@ -10,7 +10,7 @@ #include #include -#include "libbase/k60/cmsis/system_mk60dz10.h" +#include "libbase/k60/cmsis/system.h" #include "libbase/k60/watchdog_c.h" #include "libbase/syscall.h" @@ -181,20 +181,16 @@ void __thumb_startup(void) "skip_sp:\n\t" ::"r"(addr)); - // zero-fill the .bss section + // zero-fill the .bss section zero_fill_bss(); // SUPPORT_ROM_TO_RAM __copy_rom_sections_to_ram(); SystemInit(); + KeepSyscallSymbols(); __libc_init_array(); main(); - while (1); -} - -// Called in __libc_init_array() -void _init(void) -{ - KeepSyscallSymbols(); + while (1) + {} } diff --git a/src/libbase/k60/cmsis/system_mk60dz10.c b/src/libbase/k60/cmsis/system.c similarity index 80% rename from src/libbase/k60/cmsis/system_mk60dz10.c rename to src/libbase/k60/cmsis/system.c index 3abf83b..53b4bbe 100755 --- a/src/libbase/k60/cmsis/system_mk60dz10.c +++ b/src/libbase/k60/cmsis/system.c @@ -1,9 +1,3 @@ -/* - * system_mk60dz10.c - * - * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team - */ /* ################################################################### ** THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT. ** Filename : Cpu.c @@ -32,15 +26,11 @@ #include -#include "libbase/k60/cmsis/system_mk60dz10.h" +#include "libbase/k60/cmsis/system.h" +#include "libbase/k60/fpu_c.h" #include "libbase/k60/mcg_c.h" #include "libbase/k60/vectors.h" -/** - * XXX deprecated - */ -int bus_clk_khz; - /* Flash configuration field */ __attribute__((__section__(".cfmconfig"))) const uint8_t _cfm[0x10] = @@ -82,12 +72,9 @@ const uint8_t _cfm[0x10] = void SystemInit(void) { LibbaseK60McgInit(); - - const int core_div = ((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV1_MASK) - >> SIM_CLKDIV1_OUTDIV1_SHIFT) + 1; - const int bus_div = ((SIM->CLKDIV1 & SIM_CLKDIV1_OUTDIV2_MASK) - >> SIM_CLKDIV1_OUTDIV2_SHIFT) + 1; - bus_clk_khz = LibbaseK60McgGetCoreClock() * core_div / bus_div / 1000; +#if __FPU_USED + LibbaseK60FpuInit(); +#endif InitVectorTable(); } diff --git a/src/libbase/k60/crc.cpp b/src/libbase/k60/crc.cpp index 8e324dc..e65b32e 100755 --- a/src/libbase/k60/crc.cpp +++ b/src/libbase/k60/crc.cpp @@ -2,7 +2,8 @@ * crc.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" diff --git a/src/libbase/k60/crc_utils.cpp b/src/libbase/k60/crc_utils.cpp index f694c3f..ee58691 100755 --- a/src/libbase/k60/crc_utils.cpp +++ b/src/libbase/k60/crc_utils.cpp @@ -2,7 +2,8 @@ * crc_utils.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libbase/k60/dac.cpp b/src/libbase/k60/dac.cpp index 78ae750..3661876 100755 --- a/src/libbase/k60/dac.cpp +++ b/src/libbase/k60/dac.cpp @@ -2,7 +2,8 @@ * dac.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" diff --git a/src/libbase/k60/dwt.cpp b/src/libbase/k60/dwt.cpp index fe8c959..28a2caf 100755 --- a/src/libbase/k60/dwt.cpp +++ b/src/libbase/k60/dwt.cpp @@ -2,7 +2,8 @@ * dwt.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" diff --git a/src/libbase/k60/fpu.cpp b/src/libbase/k60/fpu.cpp new file mode 100755 index 0000000..d72cd51 --- /dev/null +++ b/src/libbase/k60/fpu.cpp @@ -0,0 +1,91 @@ +/* + * fpu.cpp + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#include "libbase/k60/hardware.h" + +#include + +#include "libbase/k60/fpu.h" +#include "libbase/k60/fpu_c.h" +#include "libbase/k60/misc_utils.h" + +#define FPU_FPDSCR_RMode(x) (((x) << FPU_FPDSCR_RMode_Pos) & FPU_FPDSCR_RMode_Msk) + +namespace libbase +{ +namespace k60 +{ + +#if __FPU_PRESENT + +Fpu::Fpu() +{} + +void Fpu::Init() +{ + const Config &config = GetFpuConfig(); + + // Enable full access to the FPU coprocessor + SCB->CPACR |= 0x00F00000; + InitFpccr(config); + InitFpdscr(config); +} + +void Fpu::InitFpccr(const Config&) +{ + // ASPEN and LSPEN must not be modified at this stage + uint32_t reg = FPU->FPCCR & (FPU_FPCCR_ASPEN_Msk | FPU_FPCCR_LSPEN_Msk); + SET_BIT(reg, FPU_FPCCR_MONRDY_Pos); + SET_BIT(reg, FPU_FPCCR_BFRDY_Pos); + SET_BIT(reg, FPU_FPCCR_MMRDY_Pos); + SET_BIT(reg, FPU_FPCCR_HFRDY_Pos); + SET_BIT(reg, FPU_FPCCR_THREAD_Pos); + SET_BIT(reg, FPU_FPCCR_USER_Pos); + // enable lazy stacking + SET_BIT(reg, FPU_FPCCR_LSPACT_Pos); + + FPU->FPCCR = reg; +} + +void Fpu::InitFpdscr(const Config &config) +{ + uint32_t reg = 0; + if (config.is_default_nan) + { + SET_BIT(reg, FPU_FPDSCR_DN_Pos); + } + if (config.is_flush_to_zero) + { + SET_BIT(reg, FPU_FPDSCR_FZ_Pos); + } + reg |= FPU_FPDSCR_RMode((int)config.rounding_mode); + + FPU->FPDSCR = reg; +} + +__attribute__((__weak__)) +Fpu::Config Fpu::GetFpuConfig() +{ + // Just use the default config + return {}; +} + +#else +void Fpu::Init() {} + +#endif + +} +} + +void LibbaseK60FpuInit() +{ +#if __FPU_PRESENT + libbase::k60::Fpu::Get().Init(); +#endif +} diff --git a/src/libbase/k60/ftm_pwm.cpp b/src/libbase/k60/ftm_pwm.cpp index 553e287..daef8e1 100755 --- a/src/libbase/k60/ftm_pwm.cpp +++ b/src/libbase/k60/ftm_pwm.cpp @@ -2,7 +2,8 @@ * ftm_pwm.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" @@ -34,7 +35,15 @@ namespace k60 namespace { -constexpr FTM_Type* MEM_MAPS[PINOUT::GetFtmCount()] = {FTM0, FTM1, FTM2}; +constexpr FTM_Type* MEM_MAPS[PINOUT::GetFtmCount()] = +{ + FTM0, + FTM1, + FTM2, +#if PINOUT_FTM_COUNT > 3 + FTM3, +#endif +}; FtmPwm* g_instances[PINOUT::GetFtmCount()][PINOUT::GetFtmChannelCount()] = {}; @@ -71,7 +80,7 @@ void ModCalc::Calc(const uint32_t period, const Pwm::Config::Precision precision ticks = ClockUtils::GetBusTickPerUs(period); break; - case Pwm::Config::Precision::KNs: + case Pwm::Config::Precision::kNs: ticks = ClockUtils::GetBusTickPerNs(period); break; } @@ -116,7 +125,7 @@ void CvCalc::Calc(const uint32_t pos_width, ticks = ClockUtils::GetBusTickPerUs(pos_width); break; - case Pwm::Config::Precision::KNs: + case Pwm::Config::Precision::kNs: ticks = ClockUtils::GetBusTickPerNs(pos_width); break; } @@ -291,7 +300,7 @@ FtmPwm& FtmPwm::operator=(FtmPwm &&rhs) bool FtmPwm::InitModule(const Pin::Name pin) { - const Ftm::Name ftm = FtmUtils::GetFtmName(pin); + const Ftm::Name ftm = PINOUT::GetFtm(pin); if (ftm == Ftm::Name::kDisable) { return false; @@ -306,21 +315,7 @@ void FtmPwm::InitPin(const Pin::Name pin) { Pin::Config config; config.pin = pin; - if (((int)pin >= (int)Pin::Name::kPta0 && (int)pin <= (int)Pin::Name::kPta13) - || (int)pin == (int)Pin::Name::kPtb0 - || (int)pin == (int)Pin::Name::kPtb1 - || (int)pin == (int)Pin::Name::kPtb18 - || (int)pin == (int)Pin::Name::kPtb19) - { - config.mux = Pin::Config::MuxControl::kAlt3; - } - else if (((int)pin >= (int)Pin::Name::kPtc1 - && (int)pin <= (int)Pin::Name::kPtc4) - || ((int)pin >= (int)Pin::Name::kPtd4 - && (int)pin <= (int)Pin::Name::kPtd7)) - { - config.mux = Pin::Config::MuxControl::kAlt4; - } + config.mux = PINOUT::GetFtmMux(pin); m_pin = Pin(config); } diff --git a/src/libbase/k60/ftm_quad_decoder.cpp b/src/libbase/k60/ftm_quad_decoder.cpp index 315b4e4..48399f5 100755 --- a/src/libbase/k60/ftm_quad_decoder.cpp +++ b/src/libbase/k60/ftm_quad_decoder.cpp @@ -2,7 +2,8 @@ * ftm_quad_decoder.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" @@ -123,13 +124,13 @@ FtmQuadDecoder& FtmQuadDecoder::operator=(FtmQuadDecoder &&rhs) bool FtmQuadDecoder::InitModule(const Pin::Name a_pin, const Pin::Name b_pin) { - const Ftm::Name a_ftm = FtmUtils::GetFtmName(a_pin); + const Ftm::QdName a_ftm = PINOUT::GetFtmQd(a_pin); const int a_module = FtmUtils::GetFtmModule(a_ftm); - const Ftm::Name b_ftm = FtmUtils::GetFtmName(b_pin); + const Ftm::QdName b_ftm = PINOUT::GetFtmQd(b_pin); const int b_module = FtmUtils::GetFtmModule(b_ftm); - if (a_module == b_module && a_ftm != Ftm::Name::kDisable) + if (a_module == b_module && a_ftm != Ftm::QdName::kDisable) { m_module = a_module; return true; @@ -144,12 +145,9 @@ void FtmQuadDecoder::InitPin(const Pin::Name a_pin, const Pin::Name b_pin) { Pin::Config a_config, b_config; a_config.pin = a_pin; + a_config.mux = PINOUT::GetFtmQdMux(a_pin); b_config.pin = b_pin; - - a_config.mux = (a_pin == Pin::Name::kPta12) ? Pin::Config::MuxControl::kAlt7 - : Pin::Config::MuxControl::kAlt6; - b_config.mux = (b_pin == Pin::Name::kPta13) ? Pin::Config::MuxControl::kAlt7 - : Pin::Config::MuxControl::kAlt6; + b_config.mux = PINOUT::GetFtmQdMux(b_pin); m_a = Pin(a_config); m_b = Pin(b_config); diff --git a/src/libbase/k60/ftm_utils.cpp b/src/libbase/k60/ftm_utils.cpp deleted file mode 100755 index d869e5b..0000000 --- a/src/libbase/k60/ftm_utils.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/* - * ftm_utils.cpp - * - * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team - */ - -#include "libbase/k60/ftm.h" -#include "libbase/k60/ftm_utils.h" -#include "libbase/k60/pin.h" - -namespace libbase -{ -namespace k60 -{ - -Ftm::Name FtmUtils::GetFtmName(const Pin::Name pin) -{ - switch (pin) - { - case Pin::Name::kPta3: - case Pin::Name::kPtc1: - return Ftm::Name::kFtm0Ch0; - - case Pin::Name::kPta4: - case Pin::Name::kPtc2: - return Ftm::Name::kFtm0Ch1; - - case Pin::Name::kPta5: - case Pin::Name::kPtc3: - return Ftm::Name::kFtm0Ch2; - - case Pin::Name::kPta6: - case Pin::Name::kPtc4: - return Ftm::Name::kFtm0Ch3; - - case Pin::Name::kPta7: - case Pin::Name::kPtd4: - return Ftm::Name::kFtm0Ch4; - - case Pin::Name::kPta0: - case Pin::Name::kPtd5: - return Ftm::Name::kFtm0Ch5; - - case Pin::Name::kPta1: - case Pin::Name::kPtd6: - return Ftm::Name::kFtm0Ch6; - - case Pin::Name::kPta2: - case Pin::Name::kPtd7: - return Ftm::Name::kFtm0Ch7; - - case Pin::Name::kPta8: - case Pin::Name::kPta12: - case Pin::Name::kPtb0: - return Ftm::Name::kFtm1Ch0; - - case Pin::Name::kPta9: - case Pin::Name::kPta13: - case Pin::Name::kPtb1: - return Ftm::Name::kFtm1Ch1; - - case Pin::Name::kPta10: - case Pin::Name::kPtb18: - return Ftm::Name::kFtm2Ch0; - - case Pin::Name::kPta11: - case Pin::Name::kPtb19: - return Ftm::Name::kFtm2Ch1; - - default: - return Ftm::Name::kDisable; - } -} - -} -} diff --git a/src/libbase/k60/gpio.cpp b/src/libbase/k60/gpio.cpp index 099611d..047d07a 100755 --- a/src/libbase/k60/gpio.cpp +++ b/src/libbase/k60/gpio.cpp @@ -2,7 +2,8 @@ * gpio.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" diff --git a/src/libbase/k60/mcg.cpp b/src/libbase/k60/mcg.cpp index b5b711e..184abdf 100755 --- a/src/libbase/k60/mcg.cpp +++ b/src/libbase/k60/mcg.cpp @@ -2,7 +2,8 @@ * mcg.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ /* * The FreeRTOS source code is licensed by a *modified* GNU General Public @@ -67,9 +68,41 @@ #define FLL_MAX_FREQ 39062 #define FLL_MIN_FREQ 31250 -#define MAX_BUS_CLOCK 50000000 -#define MAX_FLEXBUS_CLOCK 50000000 -#define MAX_FLASH_CLOCK 25000000 +#if MK60DZ10 + #define _MCG_C2_RANGE0 MCG_C2_RANGE + #define _MCG_C5_PRDIV0 MCG_C5_PRDIV + #define _MCG_C6_VDIV0 MCG_C6_VDIV + #define _MCG_S_LOCK0_SHIFT MCG_S_LOCK_SHIFT +#else + #define _MCG_C2_RANGE0 MCG_C2_RANGE0 + #define _MCG_C5_PRDIV0 MCG_C5_PRDIV0 + #define _MCG_C6_VDIV0 MCG_C6_VDIV0 + #define _MCG_S_LOCK0_SHIFT MCG_S_LOCK0_SHIFT +#endif + +#if MK60DZ10 || MK60D10 + #define PRDIV_MAX 0x18 + #define VDIV_BASE 24 + #define MIN_PLL_REF_KHZ 2000 + #define MAX_PLL_REF_KHZ 4000 +#elif MK60F15 + #define PRDIV_MAX 0x7 + #define VDIV_BASE 16 + #define MIN_PLL_REF_KHZ 8000 + #define MAX_PLL_REF_KHZ 16000 +#endif + +#if MK60DZ10 || MK60D10 + #define MAX_CORE_CLOCK 100000000 + #define MAX_BUS_CLOCK 50000000 + #define MAX_FLEXBUS_CLOCK 50000000 + #define MAX_FLASH_CLOCK 25000000 +#elif MK60F15 + #define MAX_CORE_CLOCK 150000000 + #define MAX_BUS_CLOCK 75000000 + #define MAX_FLEXBUS_CLOCK 50000000 + #define MAX_FLASH_CLOCK 25000000 +#endif namespace libbase { @@ -143,11 +176,22 @@ void PllDividerCalc::Calc(const uint32_t external_osc_khz, { Uint best_prdiv = 0, best_vdiv = 0; Uint min_diff = static_cast(-1); - for (Uint i = 0; i <= 0x18; ++i) + for (Uint i = 0; i <= PRDIV_MAX; ++i) { + const uint32_t pll_ref_khz = external_osc_khz / (i + 1); + if (pll_ref_khz < MIN_PLL_REF_KHZ || pll_ref_khz > MAX_PLL_REF_KHZ) + { + // PLL reference freq not valid + continue; + } + for (Uint j = 0; j <= 0x1F; ++j) { - const uint32_t this_clock = external_osc_khz * (j + 24) / (i + 1); + uint32_t this_clock = external_osc_khz * (j + VDIV_BASE) / (i + 1); +#if MK60F15 + // K60 120/150 parts have an additional /2 at the output of VCO + this_clock >>= 1; +#endif const Uint this_diff = abs((int32_t)(this_clock - core_clock_khz)); if (this_diff < min_diff) { @@ -166,13 +210,26 @@ void PllDividerCalc::Calc(const uint32_t external_osc_khz, m_prdiv = best_prdiv; m_vdiv = best_vdiv; - m_core_clock = (uint64_t)(external_osc_khz * 1000) * (m_vdiv + 24) + m_core_clock = (uint64_t)(external_osc_khz * 1000) * (m_vdiv + VDIV_BASE) / (m_prdiv + 1); +#if MK60F15 + m_core_clock >>= 1; +#endif } } -uint32_t Mcg::m_core_clock = 0; +Mcg::Config::Config() + : external_oscillator_khz(0), + core_clock_khz(MAX_CORE_CLOCK / 1000), + bus_clock_khz(MAX_BUS_CLOCK / 1000), + flexbus_clock_khz(MAX_FLEXBUS_CLOCK / 1000), + flash_clock_khz(MAX_FLEXBUS_CLOCK / 1000) +{} + +Mcg::Mcg() + : m_core_clock(0) +{} void Mcg::Init() { @@ -186,7 +243,14 @@ void Mcg::Init() InitClocks(config, calc.GetCoreClock()); // Then configure C5[PRDIV] to generate correct PLL reference frequency - MCG->C5 |= MCG_C5_PRDIV(calc.GetPrdiv()); + MCG->C5 |= _MCG_C5_PRDIV0(calc.GetPrdiv()); +#if MK60F15 + // Select PLL0 clock source + CLEAR_BIT(MCG->C11, MCG_C11_PLLCS_SHIFT); + // Select the external reference clock from OSC0 as the reference clock to + // the PLL + CLEAR_BIT(MCG->C5, MCG_C5_PLLREFSEL0_SHIFT); +#endif // Then, FBE must transition either directly to PBE mode or first through // BLPE mode and then to PBE mode @@ -201,7 +265,11 @@ void Mcg::Init() void Mcg::InitFbe(const Config &config) { uint8_t c2_reg = 0; - c2_reg |= MCG_C2_RANGE(2); +#if MK60F15 + // Reset with a loss of OSC clock + SET_BIT(c2_reg, MCG_C2_LOCRE0_SHIFT); +#endif + c2_reg |= _MCG_C2_RANGE0(2); MCG->C2 = c2_reg; uint8_t c1_reg = 0; @@ -256,7 +324,7 @@ void Mcg::InitPbe(const Config&, const uint8_t vdiv) uint8_t c6_reg = 0; SET_BIT(c6_reg, MCG_C6_PLLS_SHIFT); - c6_reg |= MCG_C6_VDIV(vdiv); + c6_reg |= _MCG_C6_VDIV0(vdiv); MCG->C6 = c6_reg; // Clear C2[LP] to 0 here to switch to PBE mode @@ -268,7 +336,7 @@ void Mcg::InitPbe(const Config&, const uint8_t vdiv) {} // Then loop until S[LOCK] is set, indicating that the PLL has acquired lock - while (!GET_BIT(MCG->S, MCG_S_LOCK_SHIFT)) + while (!GET_BIT(MCG->S, _MCG_S_LOCK0_SHIFT)) {} } @@ -322,22 +390,14 @@ void Mcg::InitClocks(const Config &config, const uint32_t core_clock) } assert(flash_div > 0); - uint32_t reg = 0; - reg |= SIM_CLKDIV1_OUTDIV2(std::min(bus_div - 1, 0xF)); - reg |= SIM_CLKDIV1_OUTDIV3(std::min(flexbus_div - 1, 0xF)); - reg |= SIM_CLKDIV1_OUTDIV4(std::min(flash_div - 1, 0xF)); - SIM->CLKDIV1 = reg; -} - -__attribute__((__weak__)) Mcg::Config Mcg::GetMcgConfig() -{ - Mcg::Config config; - config.external_oscillator_khz = 50000; - config.core_clock_khz = 100000; - config.bus_clock_khz = 50000; - config.flexbus_clock_khz = 50000; - config.flash_clock_khz = 25000; - return config; +// uint32_t reg = 0; +// reg |= SIM_CLKDIV1_OUTDIV2(std::min(bus_div - 1, 0xF)); +// reg |= SIM_CLKDIV1_OUTDIV3(std::min(flexbus_div - 1, 0xF)); +// reg |= SIM_CLKDIV1_OUTDIV4(std::min(flash_div - 1, 0xF)); +// SIM->CLKDIV1 = reg; + SetSysDividers(0, std::min(bus_div - 1, 0xF), + std::min(flexbus_div - 1, 0xF), + std::min(flash_div - 1, 0xF)); } } @@ -345,10 +405,10 @@ __attribute__((__weak__)) Mcg::Config Mcg::GetMcgConfig() void LibbaseK60McgInit() { - libbase::k60::Mcg::Init(); + libbase::k60::Mcg::Get().Init(); } uint32_t LibbaseK60McgGetCoreClock() { - return libbase::k60::Mcg::GetCoreClock(); + return libbase::k60::Mcg::Get().GetCoreClock(); } diff --git a/src/libbase/k60/misc_utils.cpp b/src/libbase/k60/misc_utils.cpp index e43e796..53622cb 100755 --- a/src/libbase/k60/misc_utils.cpp +++ b/src/libbase/k60/misc_utils.cpp @@ -2,7 +2,8 @@ * misc_utils.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libbase/k60/pin.cpp b/src/libbase/k60/pin.cpp index b3b60bf..5aaeaec 100755 --- a/src/libbase/k60/pin.cpp +++ b/src/libbase/k60/pin.cpp @@ -2,7 +2,8 @@ * pin.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" diff --git a/src/libbase/k60/pin_isr_manager.cpp b/src/libbase/k60/pin_isr_manager.cpp index 57c4e13..ce1e894 100755 --- a/src/libbase/k60/pin_isr_manager.cpp +++ b/src/libbase/k60/pin_isr_manager.cpp @@ -3,7 +3,8 @@ * Manage ISR for ports * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -137,7 +138,7 @@ template __ISR void PinIsrManager::PortIrqHandler() { PinData *pin_data = PinIsrManager::GetInstance()->m_pin_data[port]; - for (int i = 0; i < PINOUT::GetPortPinCount(); ++i) + for (Uint i = 0; i < PINOUT::GetPortPinCount(); ++i) { const Pin::Name pin = PinUtils::GetPin(port, i); if (Pin::IsInterruptRequested(pin)) diff --git a/src/libbase/k60/pinout/mk60d10_lqfp144.cpp b/src/libbase/k60/pinout/mk60d10_lqfp144.cpp index 2b42444..a8af293 100755 --- a/src/libbase/k60/pinout/mk60d10_lqfp144.cpp +++ b/src/libbase/k60/pinout/mk60d10_lqfp144.cpp @@ -2,16 +2,21 @@ * mk60d10_lqfp144.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include #include +#include "libbase/k60/pinout/mk60d10_lqfp144.h" + +#include "libbase/k60/adc.h" +#include "libbase/k60/ftm.h" #include "libbase/k60/misc_utils.h" #include "libbase/k60/pin.h" -#include "libbase/k60/pinout/mk60d10_lqfp144.h" +#include "libbase/k60/pin_utils.h" namespace libbase { @@ -250,8 +255,6 @@ int GetPinId(const Dac::Name pin) } -Mk60d10Lqfp144 *Mk60d10Lqfp144::m_instance = nullptr; - Mk60d10Lqfp144::Mk60d10Lqfp144() {} @@ -315,5 +318,217 @@ void Mk60d10Lqfp144::UnregPin_(const Dac::Name pin) UnregPin_((Uint)GetPinId(pin)); } +Adc::Name Mk60d10Lqfp144::GetAdc(const Pin::Name pin) +{ + switch (pin) + { + case Pin::Name::kPta7: + return Adc::Name::kAdc0Ad10; + + case Pin::Name::kPta8: + return Adc::Name::kAdc0Ad11; + + case Pin::Name::kPta17: + return Adc::Name::kAdc1Ad17; + + case Pin::Name::kPtb0: + return Adc::Name::kAdc0Ad8; + + case Pin::Name::kPtb1: + return Adc::Name::kAdc0Ad9; + + case Pin::Name::kPtb2: + return Adc::Name::kAdc0Ad12; + + case Pin::Name::kPtb3: + return Adc::Name::kAdc0Ad13; + + case Pin::Name::kPtb4: + return Adc::Name::kAdc1Ad10; + + case Pin::Name::kPtb5: + return Adc::Name::kAdc1Ad11; + + case Pin::Name::kPtb6: + return Adc::Name::kAdc1Ad12; + + case Pin::Name::kPtb7: + return Adc::Name::kAdc1Ad13; + + case Pin::Name::kPtb10: + return Adc::Name::kAdc1Ad14; + + case Pin::Name::kPtb11: + return Adc::Name::kAdc1Ad15; + + case Pin::Name::kPtc0: + return Adc::Name::kAdc0Ad14; + + case Pin::Name::kPtc1: + return Adc::Name::kAdc0Ad15; + + case Pin::Name::kPtc2: + return Adc::Name::kAdc0Ad4B; + + case Pin::Name::kPtc8: + return Adc::Name::kAdc1Ad4B; + + case Pin::Name::kPtc9: + return Adc::Name::kAdc1Ad5B; + + case Pin::Name::kPtc10: + return Adc::Name::kAdc1Ad6B; + + case Pin::Name::kPtc11: + return Adc::Name::kAdc1Ad7B; + + case Pin::Name::kPtd1: + return Adc::Name::kAdc0Ad5B; + + case Pin::Name::kPtd5: + return Adc::Name::kAdc0Ad6B; + + case Pin::Name::kPtd6: + return Adc::Name::kAdc0Ad7B; + + case Pin::Name::kPte0: + return Adc::Name::kAdc1Ad4A; + + case Pin::Name::kPte1: + return Adc::Name::kAdc1Ad5A; + + case Pin::Name::kPte2: + return Adc::Name::kAdc1Ad6A; + + case Pin::Name::kPte3: + return Adc::Name::kAdc1Ad7A; + + case Pin::Name::kPte24: + return Adc::Name::kAdc0Ad17; + + case Pin::Name::kPte25: + return Adc::Name::kAdc0Ad18; + + default: + return Adc::Name::kDisable; + } +} + +Ftm::Name Mk60d10Lqfp144::GetFtm(const Pin::Name pin) +{ + switch (pin) + { + case Pin::Name::kPta3: + case Pin::Name::kPtc1: + return Ftm::Name::kFtm0Ch0; + + case Pin::Name::kPta4: + case Pin::Name::kPtc2: + return Ftm::Name::kFtm0Ch1; + + case Pin::Name::kPta5: + case Pin::Name::kPtc3: + return Ftm::Name::kFtm0Ch2; + + case Pin::Name::kPta6: + case Pin::Name::kPtc4: + return Ftm::Name::kFtm0Ch3; + + case Pin::Name::kPta7: + case Pin::Name::kPtd4: + return Ftm::Name::kFtm0Ch4; + + case Pin::Name::kPta0: + case Pin::Name::kPtd5: + return Ftm::Name::kFtm0Ch5; + + case Pin::Name::kPta1: + case Pin::Name::kPtd6: + return Ftm::Name::kFtm0Ch6; + + case Pin::Name::kPta2: + case Pin::Name::kPtd7: + return Ftm::Name::kFtm0Ch7; + + case Pin::Name::kPta8: + case Pin::Name::kPta12: + case Pin::Name::kPtb0: + return Ftm::Name::kFtm1Ch0; + + case Pin::Name::kPta9: + case Pin::Name::kPta13: + case Pin::Name::kPtb1: + return Ftm::Name::kFtm1Ch1; + + case Pin::Name::kPta10: + case Pin::Name::kPtb18: + return Ftm::Name::kFtm2Ch0; + + case Pin::Name::kPta11: + case Pin::Name::kPtb19: + return Ftm::Name::kFtm2Ch1; + + default: + return Ftm::Name::kDisable; + } +} + +Pin::Config::MuxControl Mk60d10Lqfp144::GetFtmMux(const Pin::Name pin) +{ + const Uint port = PinUtils::GetPort(pin); + switch (port) + { + case 0: + case 1: + return Pin::Config::MuxControl::kAlt3; + + case 2: + case 3: + return Pin::Config::MuxControl::kAlt4; + + default: + return Pin::Config::MuxControl::kGpio; + } +} + +Ftm::QdName Mk60d10Lqfp144::GetFtmQd(const Pin::Name pin) +{ + switch (pin) + { + case Pin::Name::kPta8: + case Pin::Name::kPta12: + case Pin::Name::kPtb0: + return Ftm::QdName::kFtm1QdPha; + + case Pin::Name::kPta9: + case Pin::Name::kPta13: + case Pin::Name::kPtb1: + return Ftm::QdName::kFtm1QdPhb; + + case Pin::Name::kPta10: + case Pin::Name::kPtb18: + return Ftm::QdName::kFtm2QdPha; + + case Pin::Name::kPta11: + case Pin::Name::kPtb19: + return Ftm::QdName::kFtm2QdPhb; + + default: + return Ftm::QdName::kDisable; + } +} + +Pin::Config::MuxControl Mk60d10Lqfp144::GetFtmQdMux(const Pin::Name pin) +{ + if (pin == Pin::Name::kPta12 || pin == Pin::Name::kPta13) + { + return Pin::Config::MuxControl::kAlt7; + } + else + { + return Pin::Config::MuxControl::kAlt6; + } +} + } } diff --git a/src/libbase/k60/pinout/mk60f15_lqfp144.cpp b/src/libbase/k60/pinout/mk60f15_lqfp144.cpp new file mode 100755 index 0000000..4783706 --- /dev/null +++ b/src/libbase/k60/pinout/mk60f15_lqfp144.cpp @@ -0,0 +1,702 @@ +/* + * mk60f15_lqfp144.cpp + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#include + +#include + +#include "libbase/k60/pinout/mk60f15_lqfp144.h" + +#include "libbase/k60/adc.h" +#include "libbase/k60/ftm.h" +#include "libbase/k60/misc_utils.h" +#include "libbase/k60/pin.h" +#include "libbase/k60/pin_utils.h" + +namespace libbase +{ +namespace k60 +{ + +namespace +{ + +int GetPinId(const Pin::Name pin) +{ + if ((Uint)pin <= (Uint)Pin::Name::kPta5) + { + return (Uint)pin - (Uint)Pin::Name::kPta0 + 49; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPta6 + && (Uint)pin <= (Uint)Pin::Name::kPta17) + { + return (Uint)pin - (Uint)Pin::Name::kPta6 + 57; + } + else if (pin == Pin::Name::kPta18) + { + return 71; + } + else if (pin == Pin::Name::kPta19) + { + return 72; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPta24 + && (Uint)pin <= (Uint)Pin::Name::kPta29) + { + return (Uint)pin - (Uint)Pin::Name::kPta24 + 74; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPtb0 + && (Uint)pin <= (Uint)Pin::Name::kPtb11) + { + return (Uint)pin - (Uint)Pin::Name::kPtb0 + 80; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPtb16 + && (Uint)pin <= (Uint)Pin::Name::kPtb23) + { + return (Uint)pin - (Uint)Pin::Name::kPtb16 + 94; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPtc0 + && (Uint)pin <= (Uint)Pin::Name::kPtc3) + { + return (Uint)pin - (Uint)Pin::Name::kPtc0 + 102; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPtc4 + && (Uint)pin <= (Uint)Pin::Name::kPtc15) + { + return (Uint)pin - (Uint)Pin::Name::kPtc4 + 108; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPtc16 + && (Uint)pin <= (Uint)Pin::Name::kPtc19) + { + return (Uint)pin - (Uint)Pin::Name::kPtc16 + 122; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPtd0 + && (Uint)pin <= (Uint)Pin::Name::kPtd6) + { + return (Uint)pin - (Uint)Pin::Name::kPtd0 + 126; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPtd7 + && (Uint)pin <= (Uint)Pin::Name::kPtd15) + { + return (Uint)pin - (Uint)Pin::Name::kPtd7 + 135; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPte0 + && (Uint)pin <= (Uint)Pin::Name::kPte3) + { + return (Uint)pin - (Uint)Pin::Name::kPte0 + 0; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPte4 + && (Uint)pin <= (Uint)Pin::Name::kPte12) + { + return (Uint)pin - (Uint)Pin::Name::kPte4 + 6; + } + else if ((Uint)pin >= (Uint)Pin::Name::kPte24 + && (Uint)pin <= (Uint)Pin::Name::kPte28) + { + return (Uint)pin - (Uint)Pin::Name::kPte24 + 44; + } + else + { + assert(false); + return -1; + } +} + +int GetPinId(const Adc::Name pin) +{ + if ((Uint)pin >= (Uint)Adc::Name::kAdc1Ad4A + && (Uint)pin <= (Uint)Adc::Name::kAdc1Ad7A) + { + return (Uint)pin - (Uint)Adc::Name::kAdc1Ad4A; + } + else if (pin == Adc::Name::kAdc2Ad16) + { + return 10; + } + else if (pin == Adc::Name::kAdc2Ad17) + { + return 11; + } + else if (pin == Adc::Name::kAdc3Ad16) + { + return 13; + } + else if (pin == Adc::Name::kAdc3Ad17) + { + return 14; + } + else if (pin == Adc::Name::kAdc2DAd2|| pin == Adc::Name::kAdc2DAd0 + || pin == Adc::Name::kAdc3DAd3 || pin == Adc::Name::kAdc0DAd1) + { + return 22; + } + else if (pin == Adc::Name::kAdc3DAd2 || pin == Adc::Name::kAdc3DAd0 + || pin == Adc::Name::kAdc2DAd3 || pin == Adc::Name::kAdc1DAd1) + { + return 24; + } + else if (pin == Adc::Name::kAdc0DAd2 || pin == Adc::Name::kAdc0DAd0 + || pin == Adc::Name::kAdc1DAd3) + { + return 26; + } + else if (pin == Adc::Name::kAdc1DAd2 || pin == Adc::Name::kAdc1DAd0 + || pin == Adc::Name::kAdc0DAd3) + { + return 28; + } + else if (pin == Adc::Name::kAdc1Ad16 || pin == Adc::Name::kAdc0Ad22) + { + return 34; + } + else if (pin == Adc::Name::kAdc0Ad16 || pin == Adc::Name::kAdc0Ad21) + { + return 35; + } + else if (pin == Adc::Name::kAdc1Ad18) + { + return 36; + } + else if (pin == Adc::Name::kAdc0Ad23) + { + return 37; + } + else if (pin == Adc::Name::kAdc1Ad23) + { + return 38; + } + else if (pin == Adc::Name::kAdc0Ad17) + { + return 44; + } + else if (pin == Adc::Name::kAdc0Ad18) + { + return 45; + } + else if (pin == Adc::Name::kAdc3Ad5B) + { + return 46; + } + else if (pin == Adc::Name::kAdc3Ad4B) + { + return 47; + } + else if (pin == Adc::Name::kAdc3Ad7A) + { + return 48; + } + else if (pin == Adc::Name::kAdc3Ad6A) + { + return 57; + } + else if (pin == Adc::Name::kAdc0Ad10) + { + return 58; + } + else if (pin == Adc::Name::kAdc0Ad11) + { + return 59; + } + else if (pin == Adc::Name::kAdc3Ad5A) + { + return 60; + } + else if (pin == Adc::Name::kAdc3Ad4A) + { + return 61; + } + else if (pin == Adc::Name::kAdc3Ad15) + { + return 62; + } + else if (pin == Adc::Name::kAdc1Ad17) + { + return 68; + } + else if (pin == Adc::Name::kAdc2Ad15) + { + return 76; + } + else if (pin == Adc::Name::kAdc2Ad14) + { + return 77; + } + else if (pin == Adc::Name::kAdc2Ad13) + { + return 78; + } + else if (pin == Adc::Name::kAdc2Ad12) + { + return 79; + } + else if (pin == Adc::Name::kAdc0Ad8 || pin == Adc::Name::kAdc1Ad8 + || pin == Adc::Name::kAdc2Ad8 || pin == Adc::Name::kAdc3Ad8) + { + return 80; + } + else if (pin == Adc::Name::kAdc0Ad9 || pin == Adc::Name::kAdc1Ad9 + || pin == Adc::Name::kAdc2Ad9 || pin == Adc::Name::kAdc3Ad9) + { + return 81; + } + else if (pin == Adc::Name::kAdc0Ad12) + { + return 82; + } + else if (pin == Adc::Name::kAdc0Ad13) + { + return 83; + } + else if ((Uint)pin >= (Uint)Adc::Name::kAdc1Ad10 + && (Uint)pin <= (Uint)Adc::Name::kAdc1Ad13) + { + return (Uint)pin - (Uint)Adc::Name::kAdc1Ad10 + 84; + } + else if (pin == Adc::Name::kAdc1Ad14) + { + return 90; + } + else if (pin == Adc::Name::kAdc1Ad15) + { + return 91; + } + else if (pin == Adc::Name::kAdc2Ad4A) + { + return 98; + } + else if (pin == Adc::Name::kAdc2Ad5A) + { + return 99; + } + else if (pin == Adc::Name::kAdc0Ad14) + { + return 102; + } + else if (pin == Adc::Name::kAdc0Ad15) + { + return 103; + } + else if (pin == Adc::Name::kAdc0Ad4B) + { + return 104; + } + else if ((Uint)pin >= (Uint)Adc::Name::kAdc1Ad4B + && (Uint)pin <= (Uint)Adc::Name::kAdc1Ad7B) + { + return (Uint)pin - (Uint)Adc::Name::kAdc1Ad4B + 112; + } + else if (pin == Adc::Name::kAdc0Ad5B) + { + return 127; + } + else if (pin == Adc::Name::kAdc0Ad6B) + { + return 131; + } + else if (pin == Adc::Name::kAdc0Ad7B) + { + return 132; + } + else + { + assert(false); + return -1; + } +} + +int GetPinId(const Dac::Name pin) +{ + switch (pin) + { + case Dac::Name::kDac0: + return 37; + + case Dac::Name::kDac1: + return 38; + + default: + assert(false); + return -1; + } +} + +} + +Mk60f15Lqfp144::Mk60f15Lqfp144() +{} + +bool Mk60f15Lqfp144::RegPin_(const Uint pin) +{ + if (pin >= GetPinCount()) + { + assert(false); + return false; + } + + if (m_is_pins_active[pin]) + { + return false; + } + else + { + m_is_pins_active[pin] = true; + return true; + } +} + +bool Mk60f15Lqfp144::RegPin_(const Pin::Name pin) +{ + return RegPin_((Uint)GetPinId(pin)); +} + +bool Mk60f15Lqfp144::RegPin_(const Adc::Name pin) +{ + return RegPin_((Uint)GetPinId(pin)); +} + +bool Mk60f15Lqfp144::RegPin_(const Dac::Name pin) +{ + return RegPin_((Uint)GetPinId(pin)); +} + +void Mk60f15Lqfp144::UnregPin_(const Uint pin) +{ + if (pin >= GetPinCount()) + { + assert(false); + return; + } + + m_is_pins_active[pin] = false; +} + +void Mk60f15Lqfp144::UnregPin_(const Pin::Name pin) +{ + UnregPin_((Uint)GetPinId(pin)); +} + +void Mk60f15Lqfp144::UnregPin_(const Adc::Name pin) +{ + UnregPin_((Uint)GetPinId(pin)); +} + +void Mk60f15Lqfp144::UnregPin_(const Dac::Name pin) +{ + UnregPin_((Uint)GetPinId(pin)); +} + +Adc::Name Mk60f15Lqfp144::GetAdc(const Pin::Name pin) +{ + switch (pin) + { + case Pin::Name::kPta6: + return Adc::Name::kAdc3Ad6A; + + case Pin::Name::kPta7: + return Adc::Name::kAdc0Ad10; + + case Pin::Name::kPta8: + return Adc::Name::kAdc0Ad11; + + case Pin::Name::kPta9: + return Adc::Name::kAdc3Ad5A; + + case Pin::Name::kPta10: + return Adc::Name::kAdc3Ad4A; + + case Pin::Name::kPta11: + return Adc::Name::kAdc3Ad15; + + case Pin::Name::kPta17: + return Adc::Name::kAdc1Ad17; + + case Pin::Name::kPta26: + return Adc::Name::kAdc2Ad15; + + case Pin::Name::kPta27: + return Adc::Name::kAdc2Ad14; + + case Pin::Name::kPta28: + return Adc::Name::kAdc2Ad13; + + case Pin::Name::kPta29: + return Adc::Name::kAdc2Ad12; + + case Pin::Name::kPtb0: + return Adc::Name::kAdc0Ad8; + + case Pin::Name::kPtb1: + return Adc::Name::kAdc0Ad9; + + case Pin::Name::kPtb2: + return Adc::Name::kAdc0Ad12; + + case Pin::Name::kPtb3: + return Adc::Name::kAdc0Ad13; + + case Pin::Name::kPtb4: + return Adc::Name::kAdc1Ad10; + + case Pin::Name::kPtb5: + return Adc::Name::kAdc1Ad11; + + case Pin::Name::kPtb6: + return Adc::Name::kAdc1Ad12; + + case Pin::Name::kPtb7: + return Adc::Name::kAdc1Ad13; + + case Pin::Name::kPtb10: + return Adc::Name::kAdc1Ad14; + + case Pin::Name::kPtb11: + return Adc::Name::kAdc1Ad15; + + case Pin::Name::kPtb20: + return Adc::Name::kAdc2Ad4A; + + case Pin::Name::kPtb21: + return Adc::Name::kAdc2Ad5A; + + case Pin::Name::kPtc0: + return Adc::Name::kAdc0Ad14; + + case Pin::Name::kPtc1: + return Adc::Name::kAdc0Ad15; + + case Pin::Name::kPtc2: + return Adc::Name::kAdc0Ad4B; + + case Pin::Name::kPtc8: + return Adc::Name::kAdc1Ad4B; + + case Pin::Name::kPtc9: + return Adc::Name::kAdc1Ad5B; + + case Pin::Name::kPtc10: + return Adc::Name::kAdc1Ad6B; + + case Pin::Name::kPtc11: + return Adc::Name::kAdc1Ad7B; + + case Pin::Name::kPtd1: + return Adc::Name::kAdc0Ad5B; + + case Pin::Name::kPtd5: + return Adc::Name::kAdc0Ad6B; + + case Pin::Name::kPtd6: + return Adc::Name::kAdc0Ad7B; + + case Pin::Name::kPte0: + return Adc::Name::kAdc1Ad4A; + + case Pin::Name::kPte1: + return Adc::Name::kAdc1Ad5A; + + case Pin::Name::kPte2: + return Adc::Name::kAdc1Ad6A; + + case Pin::Name::kPte3: + return Adc::Name::kAdc1Ad7A; + + case Pin::Name::kPte8: + return Adc::Name::kAdc2Ad16; + + case Pin::Name::kPte9: + return Adc::Name::kAdc2Ad17; + + case Pin::Name::kPte11: + return Adc::Name::kAdc3Ad16; + + case Pin::Name::kPte12: + return Adc::Name::kAdc3Ad17; + + case Pin::Name::kPte24: + return Adc::Name::kAdc0Ad17; + + case Pin::Name::kPte25: + return Adc::Name::kAdc0Ad18; + + case Pin::Name::kPte26: + return Adc::Name::kAdc3Ad5B; + + case Pin::Name::kPte27: + return Adc::Name::kAdc3Ad4B; + + case Pin::Name::kPte28: + return Adc::Name::kAdc3Ad7A; + + default: + return Adc::Name::kDisable; + } +} + +Ftm::Name Mk60f15Lqfp144::GetFtm(const Pin::Name pin) +{ + switch (pin) + { + case Pin::Name::kPta3: + case Pin::Name::kPtc1: + return Ftm::Name::kFtm0Ch0; + + case Pin::Name::kPta4: + case Pin::Name::kPtc2: + return Ftm::Name::kFtm0Ch1; + + case Pin::Name::kPta5: + case Pin::Name::kPtc3: + return Ftm::Name::kFtm0Ch2; + + case Pin::Name::kPta6: + case Pin::Name::kPtc4: + return Ftm::Name::kFtm0Ch3; + + case Pin::Name::kPta7: + case Pin::Name::kPtd4: + return Ftm::Name::kFtm0Ch4; + + case Pin::Name::kPta0: + case Pin::Name::kPtd5: + return Ftm::Name::kFtm0Ch5; + + case Pin::Name::kPta1: + case Pin::Name::kPtd6: + return Ftm::Name::kFtm0Ch6; + + case Pin::Name::kPta2: + case Pin::Name::kPtd7: + return Ftm::Name::kFtm0Ch7; + + case Pin::Name::kPta8: + case Pin::Name::kPta12: + case Pin::Name::kPtb0: + return Ftm::Name::kFtm1Ch0; + + case Pin::Name::kPta9: + case Pin::Name::kPta13: + case Pin::Name::kPtb1: + return Ftm::Name::kFtm1Ch1; + + case Pin::Name::kPta10: + case Pin::Name::kPtb18: + return Ftm::Name::kFtm2Ch0; + + case Pin::Name::kPta11: + case Pin::Name::kPtb19: + return Ftm::Name::kFtm2Ch1; + + case Pin::Name::kPte5: + case Pin::Name::kPtd0: + return Ftm::Name::kFtm3Ch0; + + case Pin::Name::kPte6: + case Pin::Name::kPtd1: + return Ftm::Name::kFtm3Ch1; + + case Pin::Name::kPte7: + case Pin::Name::kPtd2: + return Ftm::Name::kFtm3Ch2; + + case Pin::Name::kPte8: + case Pin::Name::kPtd3: + return Ftm::Name::kFtm3Ch3; + + case Pin::Name::kPte9: + case Pin::Name::kPtc8: + return Ftm::Name::kFtm3Ch4; + + case Pin::Name::kPte10: + case Pin::Name::kPtc9: + return Ftm::Name::kFtm3Ch5; + + case Pin::Name::kPte11: + case Pin::Name::kPtc10: + return Ftm::Name::kFtm3Ch6; + + case Pin::Name::kPte12: + case Pin::Name::kPtc11: + return Ftm::Name::kFtm3Ch7; + + default: + return Ftm::Name::kDisable; + } +} + +Pin::Config::MuxControl Mk60f15Lqfp144::GetFtmMux(const Pin::Name pin) +{ + const Uint port = PinUtils::GetPort(pin); + const Uint pin_num = PinUtils::GetPinNumber(pin); + switch (port) + { + case 0: + case 1: + return Pin::Config::MuxControl::kAlt3; + + case 2: + if (pin_num <= 4) + { + return Pin::Config::MuxControl::kAlt4; + } + else + { + return Pin::Config::MuxControl::kAlt3; + } + + case 3: + return Pin::Config::MuxControl::kAlt4; + + case 4: + return Pin::Config::MuxControl::kAlt6; + + default: + return Pin::Config::MuxControl::kGpio; + } +} + +Ftm::QdName Mk60f15Lqfp144::GetFtmQd(const Pin::Name pin) +{ + switch (pin) + { + case Pin::Name::kPta8: + case Pin::Name::kPta12: + case Pin::Name::kPtb0: + return Ftm::QdName::kFtm1QdPha; + + case Pin::Name::kPta9: + case Pin::Name::kPta13: + case Pin::Name::kPtb1: + return Ftm::QdName::kFtm1QdPhb; + + case Pin::Name::kPta10: + case Pin::Name::kPtb18: + return Ftm::QdName::kFtm2QdPha; + + case Pin::Name::kPta11: + case Pin::Name::kPtb19: + return Ftm::QdName::kFtm2QdPhb; + + default: + return Ftm::QdName::kDisable; + } +} + +Pin::Config::MuxControl Mk60f15Lqfp144::GetFtmQdMux(const Pin::Name pin) +{ + if (pin == Pin::Name::kPta12 || pin == Pin::Name::kPta13) + { + return Pin::Config::MuxControl::kAlt7; + } + else + { + return Pin::Config::MuxControl::kAlt6; + } +} + +} +} diff --git a/src/libbase/k60/pit.cpp b/src/libbase/k60/pit.cpp index f602c77..db9d0ae 100755 --- a/src/libbase/k60/pit.cpp +++ b/src/libbase/k60/pit.cpp @@ -2,7 +2,8 @@ * pit.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" diff --git a/src/libbase/k60/rand_generator_a.cpp b/src/libbase/k60/rand_generator_a.cpp index b03c34c..63f99df 100755 --- a/src/libbase/k60/rand_generator_a.cpp +++ b/src/libbase/k60/rand_generator_a.cpp @@ -3,7 +3,8 @@ * Random Number Generator Accelerator (for RNGA) * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #ifdef MK60D10 diff --git a/src/libbase/k60/sim.cpp b/src/libbase/k60/sim.cpp index d449f07..a559ce6 100755 --- a/src/libbase/k60/sim.cpp +++ b/src/libbase/k60/sim.cpp @@ -3,7 +3,8 @@ * System integration module * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" @@ -22,6 +23,22 @@ using libutil::String; using namespace std; +#if MK60DZ10 || MK60D10 + #define _SIM_SCGC6_DMAMUX0_SHIFT SIM_SCGC6_DMAMUX_SHIFT + #define _SIM_SCGC6_SPI0_SHIFT SIM_SCGC6_SPI0_SHIFT + #define _SIM_SCGC6_SPI1_SHIFT SIM_SCGC6_SPI1_SHIFT + #define _SIM_SCGC3_SPI2_SHIFT SIM_SCGC3_SPI2_SHIFT + #define _SIM_SCGC4_I2C0_SHIFT SIM_SCGC4_I2C0_SHIFT + #define _SIM_SCGC4_I2C1_SHIFT SIM_SCGC4_I2C1_SHIFT +#elif MK60F15 + #define _SIM_SCGC6_DMAMUX0_SHIFT SIM_SCGC6_DMAMUX0_SHIFT + #define _SIM_SCGC6_SPI0_SHIFT SIM_SCGC6_DSPI0_SHIFT + #define _SIM_SCGC6_SPI1_SHIFT SIM_SCGC6_DSPI1_SHIFT + #define _SIM_SCGC3_SPI2_SHIFT SIM_SCGC3_DSPI2_SHIFT + #define _SIM_SCGC4_I2C0_SHIFT SIM_SCGC4_IIC0_SHIFT + #define _SIM_SCGC4_I2C1_SHIFT SIM_SCGC4_IIC1_SHIFT +#endif + namespace libbase { namespace k60 @@ -133,6 +150,16 @@ void Sim::SetEnableClockGate(const ClockGate cg, const bool flag) SetClockGateBit(SIM->SCGC3, SIM_SCGC3_ADC1_SHIFT, flag); break; +#if MK60F15 + case ClockGate::kAdc2: + SetClockGateBit(SIM->SCGC6, SIM_SCGC6_ADC2_SHIFT, flag); + break; + + case ClockGate::kAdc3: + SetClockGateBit(SIM->SCGC3, SIM_SCGC3_ADC3_SHIFT, flag); + break; +#endif + case ClockGate::kCmp: SetClockGateBit(SIM->SCGC4, SIM_SCGC4_CMP_SHIFT, flag); break; @@ -157,10 +184,24 @@ void Sim::SetEnableClockGate(const ClockGate cg, const bool flag) SetClockGateBit(SIM->SCGC7, SIM_SCGC7_DMA_SHIFT, flag); break; +#if MK60DZ10 || MK60D10 case ClockGate::kDmaMux: - SetClockGateBit(SIM->SCGC6, SIM_SCGC6_DMAMUX_SHIFT, flag); +#elif MK60F15 + case ClockGate::kDmaMux0: +#endif + SetClockGateBit(SIM->SCGC6, _SIM_SCGC6_DMAMUX0_SHIFT, flag); + break; + +#if MK60F15 + case ClockGate::kDmaMux1: + SetClockGateBit(SIM->SCGC6, SIM_SCGC6_DMAMUX1_SHIFT, flag); break; + case ClockGate::kEsdhc: + SetClockGateBit(SIM->SCGC3, SIM_SCGC3_ESDHC_SHIFT, flag); + break; +#endif + case ClockGate::kEthernet: SetClockGateBit(SIM->SCGC2, SIM_SCGC2_ENET_SHIFT, flag); break; @@ -181,9 +222,11 @@ void Sim::SetEnableClockGate(const ClockGate cg, const bool flag) SetClockGateBit(SIM->SCGC3, SIM_SCGC3_FLEXCAN1_SHIFT, flag); break; +#if MK60DZ10 || MK60D10 case ClockGate::kFlash: SetClockGateBit(SIM->SCGC6, SIM_SCGC6_FTFL_SHIFT, flag); break; +#endif case ClockGate::kFtm0: SetClockGateBit(SIM->SCGC6, SIM_SCGC6_FTM0_SHIFT, flag); @@ -197,17 +240,33 @@ void Sim::SetEnableClockGate(const ClockGate cg, const bool flag) SetClockGateBit(SIM->SCGC3, SIM_SCGC3_FTM2_SHIFT, flag); break; +#if MK60F15 + case ClockGate::kFtm3: + SetClockGateBit(SIM->SCGC3, SIM_SCGC3_FTM3_SHIFT, flag); + break; +#endif + +#if MK60DZ10 || MK60D10 case ClockGate::kI2c0: - SetClockGateBit(SIM->SCGC4, SIM_SCGC4_I2C0_SHIFT, flag); +#elif MK60F15 + case ClockGate::kIic0: +#endif + SetClockGateBit(SIM->SCGC4, _SIM_SCGC4_I2C0_SHIFT, flag); break; +#if MK60DZ10 || MK60D10 case ClockGate::kI2c1: - SetClockGateBit(SIM->SCGC4, SIM_SCGC4_I2C1_SHIFT, flag); +#elif MK60F15 + case ClockGate::kIic1: +#endif + SetClockGateBit(SIM->SCGC4, _SIM_SCGC4_I2C1_SHIFT, flag); break; +#if MK60DZ10 || MK60D10 case ClockGate::kI2s: SetClockGateBit(SIM->SCGC6, SIM_SCGC6_I2S_SHIFT, flag); break; +#endif case ClockGate::kLlwu: SetClockGateBit(SIM->SCGC4, SIM_SCGC4_LLWU_SHIFT, flag); @@ -221,6 +280,16 @@ void Sim::SetEnableClockGate(const ClockGate cg, const bool flag) SetClockGateBit(SIM->SCGC7, SIM_SCGC7_MPU_SHIFT, flag); break; +#if MK60F15 + case ClockGate::kNfc: + SetClockGateBit(SIM->SCGC3, SIM_SCGC3_NFC_SHIFT, flag); + break; + + case ClockGate::kOsc1: + SetClockGateBit(SIM->SCGC1, SIM_SCGC1_OSC1_SHIFT, flag); + break; +#endif + case ClockGate::kPdb: SetClockGateBit(SIM->SCGC6, SIM_SCGC6_PDB_SHIFT, flag); break; @@ -249,6 +318,16 @@ void Sim::SetEnableClockGate(const ClockGate cg, const bool flag) SetClockGateBit(SIM->SCGC5, SIM_SCGC5_PORTE_SHIFT, flag); break; +#if MK60F15 + case ClockGate::kPortF: + SetClockGateBit(SIM->SCGC5, SIM_SCGC5_PORTF_SHIFT, flag); + break; +#endif + + case ClockGate::kRegFile: + SetClockGateBit(SIM->SCGC5, SIM_SCGC5_REGFILE_SHIFT, flag); + break; + case ClockGate::kRng: #ifdef MK60DZ10 SetClockGateBit(SIM->SCGC3, SIM_SCGC3_RNGB_SHIFT, flag); @@ -261,20 +340,44 @@ void Sim::SetEnableClockGate(const ClockGate cg, const bool flag) SetClockGateBit(SIM->SCGC6, SIM_SCGC6_RTC_SHIFT, flag); break; +#if MK60F15 + case ClockGate::kSai0: + SetClockGateBit(SIM->SCGC6, SIM_SCGC6_SAI0_SHIFT, flag); + break; + + case ClockGate::kSai1: + SetClockGateBit(SIM->SCGC3, SIM_SCGC3_SAI1_SHIFT, flag); + break; +#endif + +#ifdef MK60DZ10 case ClockGate::kSdhc: SetClockGateBit(SIM->SCGC3, SIM_SCGC3_SDHC_SHIFT, flag); break; +#endif +#if MK60DZ10 || MK60D10 case ClockGate::kSpi0: - SetClockGateBit(SIM->SCGC6, SIM_SCGC6_SPI0_SHIFT, flag); +#elif MK60F15 + case ClockGate::kDspi0: +#endif + SetClockGateBit(SIM->SCGC6, _SIM_SCGC6_SPI0_SHIFT, flag); break; +#if MK60DZ10 || MK60D10 case ClockGate::kSpi1: - SetClockGateBit(SIM->SCGC6, SIM_SCGC6_SPI1_SHIFT, flag); +#elif MK60F15 + case ClockGate::kDspi1: +#endif + SetClockGateBit(SIM->SCGC6, _SIM_SCGC6_SPI1_SHIFT, flag); break; +#if MK60DZ10 || MK60D10 case ClockGate::kSpi2: - SetClockGateBit(SIM->SCGC3, SIM_SCGC3_SPI2_SHIFT, flag); +#elif MK60F15 + case ClockGate::kDspi2: +#endif + SetClockGateBit(SIM->SCGC3, _SIM_SCGC3_SPI2_SHIFT, flag); break; case ClockGate::kTsi: @@ -309,9 +412,21 @@ void Sim::SetEnableClockGate(const ClockGate cg, const bool flag) SetClockGateBit(SIM->SCGC6, SIM_SCGC6_USBDCD_SHIFT, flag); break; +#if MK60F15 + case ClockGate::kUsbFs: + SetClockGateBit(SIM->SCGC4, SIM_SCGC4_USBFS_SHIFT, flag); + break; + + case ClockGate::kUsbHs: + SetClockGateBit(SIM->SCGC6, SIM_SCGC6_USBHS_SHIFT, flag); + break; +#endif + +#if MK60DZ10 || MK60D10 case ClockGate::kUsbOtg: SetClockGateBit(SIM->SCGC4, SIM_SCGC4_USBOTG_SHIFT, flag); break; +#endif case ClockGate::kVref: SetClockGateBit(SIM->SCGC4, SIM_SCGC4_VREF_SHIFT, flag); diff --git a/src/libbase/k60/soft_i2c_master.cpp b/src/libbase/k60/soft_i2c_master.cpp index 3e64ec3..cc94379 100755 --- a/src/libbase/k60/soft_i2c_master.cpp +++ b/src/libbase/k60/soft_i2c_master.cpp @@ -2,7 +2,8 @@ * soft_i2c_master.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" @@ -20,7 +21,10 @@ #include "libbase/k60/misc_utils.h" #include "libbase/k60/soft_i2c_master.h" +#include "libsc/k60/system.h" + using namespace std; +using namespace libsc::k60; #define SEND_BYTE_GUARDED(x, ret) do { if (!SendByte_(x)) { Stop(); return ret; } } while (false) @@ -58,7 +62,8 @@ inline void SoftI2cMaster::Delay() } SoftI2cMaster::SoftI2cMaster(const Config &config) - : m_delay_us(config.delay_us), + : m_scl_low_timeout(config.scl_low_timeout), + m_delay_us(config.delay_us), m_scl(GetSclConfig(config)), m_sda(GetSdaConfig(config)), m_is_init(true) @@ -71,7 +76,8 @@ SoftI2cMaster::SoftI2cMaster(SoftI2cMaster &&rhs) } SoftI2cMaster::SoftI2cMaster(nullptr_t) - : m_delay_us(0), + : m_scl_low_timeout(0), + m_delay_us(0), m_scl(nullptr), m_sda(nullptr), m_is_init(false) @@ -206,7 +212,7 @@ bool SoftI2cMaster::SendByte_(const Byte byte) } // Assume SCL is low when being called -Byte SoftI2cMaster::ReadByte_(const bool is_ack) +bool SoftI2cMaster::ReadByte_(const bool is_ack, Byte *out_byte) { m_sda.EnsureGpi(); @@ -215,9 +221,20 @@ Byte SoftI2cMaster::ReadByte_(const bool is_ack) { m_scl.EnsureGpi(); Delay(); + + // TODO libbase should not depends on libsc + const Timer::TimerInt start = System::Time(); + const Timer::TimerInt max = (m_scl_low_timeout * m_delay_us + 999) + / 1000; // Clock stretching while (!m_scl.Get()) - {} + { + if (Timer::TimeDiff(System::Time(), start) >= max) + { + LOG_DL("i2c scl timeout"); + return false; + } + } byte |= m_sda.Get() << (7 - i); m_scl.Clear(); @@ -233,7 +250,8 @@ Byte SoftI2cMaster::ReadByte_(const bool is_ack) Delay(); m_scl.Clear(); Delay(); - return byte; + *out_byte = byte; + return true; } bool SoftI2cMaster::GetByte(const Byte slave_addr, const Byte reg_addr, @@ -246,9 +264,16 @@ bool SoftI2cMaster::GetByte(const Byte slave_addr, const Byte reg_addr, SEND_BYTE_GUARDED(reg_addr, false); Start(); SEND_BYTE_GUARDED((slave_addr << 1) | 0x1, false); - *out_byte = ReadByte_(false); - Stop(); - return true; + if (!ReadByte_(false, out_byte)) + { + Stop(); + return false; + } + else + { + Stop(); + return true; + } } vector SoftI2cMaster::GetBytes(const Byte slave_addr, const Byte reg_addr, @@ -266,9 +291,19 @@ vector SoftI2cMaster::GetBytes(const Byte slave_addr, const Byte reg_addr, SEND_BYTE_GUARDED(((slave_addr << 1) & 0xFE) | 0x1, {}); for (uint8_t i = 0; i < size - 1; ++i) { - bytes.push_back(ReadByte_(true)); + Byte byte; + if (!ReadByte_(true, &byte)) + { + Stop(); + return bytes; + } + bytes.push_back(byte); + } + Byte byte; + if (ReadByte_(true, &byte)) + { + bytes.push_back(byte); } - bytes.push_back(ReadByte_(false)); Stop(); return bytes; } diff --git a/src/libbase/k60/soft_pwm.cpp b/src/libbase/k60/soft_pwm.cpp index 2f14ddc..948f986 100755 --- a/src/libbase/k60/soft_pwm.cpp +++ b/src/libbase/k60/soft_pwm.cpp @@ -2,7 +2,8 @@ * soft_pwm.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -83,7 +84,7 @@ void SoftPwm::Setup(const uint32_t period, const uint32_t pos_width) m_neg_count = ClockUtils::GetBusTickPerUs(m_neg_width); break; - case Pwm::Config::Precision::KNs: + case Pwm::Config::Precision::kNs: m_pos_count = ClockUtils::GetBusTickPerNs(m_pos_width); m_neg_count = ClockUtils::GetBusTickPerNs(m_neg_width); break; diff --git a/src/libbase/k60/soft_quad_decoder.cpp b/src/libbase/k60/soft_quad_decoder.cpp index 66af9fd..6365228 100755 --- a/src/libbase/k60/soft_quad_decoder.cpp +++ b/src/libbase/k60/soft_quad_decoder.cpp @@ -2,7 +2,8 @@ * soft_quad_decoder.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libbase/k60/soft_spi_master.cpp b/src/libbase/k60/soft_spi_master.cpp index 9c47328..ad678a4 100755 --- a/src/libbase/k60/soft_spi_master.cpp +++ b/src/libbase/k60/soft_spi_master.cpp @@ -2,7 +2,8 @@ * soft_spi_master.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" diff --git a/src/libbase/k60/spi_master.cpp b/src/libbase/k60/spi_master.cpp index c40708e..a3d4f37 100755 --- a/src/libbase/k60/spi_master.cpp +++ b/src/libbase/k60/spi_master.cpp @@ -2,7 +2,8 @@ * spi_master.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" @@ -120,7 +121,11 @@ SpiMaster::SpiMaster(const Config &config) m_is_init = true; g_instances[m_module] = this; +#if MK60DZ10 || MK60D10 Sim::SetEnableClockGate(EnumAdvance(Sim::ClockGate::kSpi0, m_module), true); +#elif MK60F15 + Sim::SetEnableClockGate(EnumAdvance(Sim::ClockGate::kDspi0, m_module), true); +#endif InitPin(config); InitMcrReg(config); InitCtarReg(config); @@ -484,8 +489,13 @@ void SpiMaster::Uninit() m_is_init = false; SetHalt(true); +#if MK60DZ10 || MK60D10 Sim::SetEnableClockGate(EnumAdvance(Sim::ClockGate::kSpi0, m_module), false); +#elif MK60F15 + Sim::SetEnableClockGate(EnumAdvance(Sim::ClockGate::kDspi0, m_module), + false); +#endif g_instances[m_module] = nullptr; } } diff --git a/src/libbase/k60/spi_utils.cpp b/src/libbase/k60/spi_utils.cpp index 4f4f1af..9c5c097 100755 --- a/src/libbase/k60/spi_utils.cpp +++ b/src/libbase/k60/spi_utils.cpp @@ -2,7 +2,8 @@ * spi_utils.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libbase/k60/sys_tick.cpp b/src/libbase/k60/sys_tick.cpp index bfc5537..72af48f 100755 --- a/src/libbase/k60/sys_tick.cpp +++ b/src/libbase/k60/sys_tick.cpp @@ -2,7 +2,8 @@ * sys_tick.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" diff --git a/src/libbase/k60/uart.cpp b/src/libbase/k60/uart.cpp index d93474c..edeeca6 100644 --- a/src/libbase/k60/uart.cpp +++ b/src/libbase/k60/uart.cpp @@ -2,7 +2,8 @@ * uart.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" @@ -292,8 +293,8 @@ void Uart::InitBaudRate(const Config::BaudRate br) assert(sbr <= 0x1FFF); UART_Type* uart_ptr = MEM_MAPS[m_module]; - uart_ptr->BDH = (sbr >> 8) & 0x1F; - uart_ptr->BDL = sbr & 0xFF; + uart_ptr->BDH = UART_BDH_SBR(sbr >> 8); + uart_ptr->BDL = UART_BDL_SBR(sbr); float null; const int brfa = roundf(modf(target, &null) * 32); diff --git a/src/libbase/k60/vectors.c b/src/libbase/k60/vectors.c index 9d1de46..b9cc16b 100755 --- a/src/libbase/k60/vectors.c +++ b/src/libbase/k60/vectors.c @@ -2,7 +2,8 @@ * vectors.c * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ /** ################################################################### ** THIS COMPONENT MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT. @@ -39,14 +40,19 @@ __attribute__((__naked__)) void HardFaultHandlerAsm(void); HardFaultHandler g_hard_fault_handler = NULL; - /* ISR prototype */ extern uint32_t __SP_INIT; extern void __thumb_startup(void); +#if MK60DZ10 || MK60D10 +/* Interrupt vector table type definition */ +typedef struct { + void * __ptr; + tIsrFunc __fun[0x77]; +} tVectorTable; -__attribute__ ((section (".vectortable"))) const tVectorTable __vect_table = { /* Interrupt vector table */ - +__attribute__ ((section (".vectortable"))) +const tVectorTable __vect_table = { /* Interrupt vector table */ /* ISR name No. Address Pri Name Description */ &__SP_INIT, /* 0x00 0x00000000 - ivINT_Initial_Stack_Pointer used by PE */ { @@ -172,6 +178,144 @@ __attribute__ ((section (".vectortable"))) const tVectorTable __vect_table = { / } }; +#elif MK60F15 +/* Interrupt vector table type definition */ +typedef struct { + void * __ptr; + tIsrFunc __fun[0x79]; +} tVectorTable; + +__attribute__ ((section (".vectortable"))) +const tVectorTable __vect_table = { /* Interrupt vector table */ + /* ISR name No. Address Pri Name Description */ + &__SP_INIT, /* 0x00 0x00000000 - ivINT_Initial_Stack_Pointer used by PE */ + { + (tIsrFunc)&__thumb_startup, /* 0x01 0x00000004 - ivINT_Initial_Program_Counter used by PE */ + (tIsrFunc)&DefaultIsr, /* 0x02 0x00000008 -2 ivINT_NMI used by PE */ + (tIsrFunc)&HardFaultHandlerAsm, /* 0x03 0x0000000C -1 ivINT_Hard_Fault unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x04 0x00000010 - ivINT_Mem_Manage_Fault unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x05 0x00000014 - ivINT_Bus_Fault unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x06 0x00000018 - ivINT_Usage_Fault unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x07 0x0000001C - ivINT_Reserved7 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x08 0x00000020 - ivINT_Reserved8 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x09 0x00000024 - ivINT_Reserved9 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x0A 0x00000028 - ivINT_Reserved10 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x0B 0x0000002C - ivINT_SVCall unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x0C 0x00000030 - ivINT_DebugMonitor unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x0D 0x00000034 - ivINT_Reserved13 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x0E 0x00000038 - ivINT_PendableSrvReq unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x0F 0x0000003C - ivINT_SysTick unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x10 0x00000040 - ivINT_DMA0_DMA16 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x11 0x00000044 - ivINT_DMA1_DMA17 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x12 0x00000048 - ivINT_DMA2_DMA18 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x13 0x0000004C - ivINT_DMA3_DMA19 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x14 0x00000050 - ivINT_DMA4_DMA20 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x15 0x00000054 - ivINT_DMA5_DMA21 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x16 0x00000058 - ivINT_DMA6_DMA22 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x17 0x0000005C - ivINT_DMA7_DMA23 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x18 0x00000060 - ivINT_DMA8_DMA24 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x19 0x00000064 - ivINT_DMA9_DMA25 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x1A 0x00000068 - ivINT_DMA10_DMA26 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x1B 0x0000006C - ivINT_DMA11_DMA27 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x1C 0x00000070 - ivINT_DMA12_DMA28 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x1D 0x00000074 - ivINT_DMA13_DMA29 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x1E 0x00000078 - ivINT_DMA14_DMA30 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x1F 0x0000007C - ivINT_DMA15_DMA31 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x20 0x00000080 - ivINT_DMA_Error unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x21 0x00000084 - ivINT_MCM unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x22 0x00000088 - ivINT_FTFE unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x23 0x0000008C - ivINT_Read_Collision unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x24 0x00000090 - ivINT_LVD_LVW unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x25 0x00000094 - ivINT_LLW unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x26 0x00000098 - ivINT_Watchdog unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x27 0x0000009C - ivINT_RNG unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x28 0x000000A0 - ivINT_I2C0 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x29 0x000000A4 - ivINT_I2C1 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x2A 0x000000A8 - ivINT_SPI0 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x2B 0x000000AC - ivINT_SPI1 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x2C 0x000000B0 - ivINT_SPI2 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x2D 0x000000B4 - ivINT_CAN0_ORed_Message_buffer unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x2E 0x000000B8 - ivINT_CAN0_Bus_Off unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x2F 0x000000BC - ivINT_CAN0_Error unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x30 0x000000C0 - ivINT_CAN0_Tx_Warning unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x31 0x000000C4 - ivINT_CAN0_Rx_Warning unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x32 0x000000C8 - ivINT_CAN0_Wake_Up unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x33 0x000000CC - ivINT_I2S0_Tx unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x34 0x000000D0 - ivINT_I2S0_Rx unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x35 0x000000D4 - ivINT_CAN1_ORed_Message_buffer unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x36 0x000000D8 - ivINT_CAN1_Bus_Off unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x37 0x000000DC - ivINT_CAN1_Error unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x38 0x000000E0 - ivINT_CAN1_Tx_Warning unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x39 0x000000E4 - ivINT_CAN1_Rx_Warning unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x3A 0x000000E8 - ivINT_CAN1_Wake_Up unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x3B 0x000000EC - ivINT_Reserved59 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x3C 0x000000F0 - ivINT_UART0_LON unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x3D 0x000000F4 - ivINT_UART0_RX_TX unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x3E 0x000000F8 - ivINT_UART0_ERR unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x3F 0x000000FC - ivINT_UART1_RX_TX unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x40 0x00000100 - ivINT_UART1_ERR unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x41 0x00000104 - ivINT_UART2_RX_TX unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x42 0x00000108 - ivINT_UART2_ERR unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x43 0x0000010C - ivINT_UART3_RX_TX unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x44 0x00000110 - ivINT_UART3_ERR unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x45 0x00000114 - ivINT_UART4_RX_TX unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x46 0x00000118 - ivINT_UART4_ERR unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x47 0x0000011C - ivINT_UART5_RX_TX unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x48 0x00000120 - ivINT_UART5_ERR unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x49 0x00000124 - ivINT_ADC0 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x4A 0x00000128 - ivINT_ADC1 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x4B 0x0000012C - ivINT_CMP0 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x4C 0x00000130 - ivINT_CMP1 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x4D 0x00000134 - ivINT_CMP2 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x4E 0x00000138 - ivINT_FTM0 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x4F 0x0000013C - ivINT_FTM1 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x50 0x00000140 - ivINT_FTM2 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x51 0x00000144 - ivINT_CMT unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x52 0x00000148 - ivINT_RTC unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x53 0x0000014C - ivINT_RTC_Seconds unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x54 0x00000150 - ivINT_PIT0 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x55 0x00000154 - ivINT_PIT1 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x56 0x00000158 - ivINT_PIT2 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x57 0x0000015C - ivINT_PIT3 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x58 0x00000160 - ivINT_PDB0 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x59 0x00000164 - ivINT_USB0 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x5A 0x00000168 - ivINT_USBDCD unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x5B 0x0000016C - ivINT_ENET_1588_Timer unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x5C 0x00000170 - ivINT_ENET_Transmit unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x5D 0x00000174 - ivINT_ENET_Receive unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x5E 0x00000178 - ivINT_ENET_Error unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x5F 0x0000017C - ivINT_Reserved95 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x60 0x00000180 - ivINT_SDHC unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x61 0x00000184 - ivINT_DAC0 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x62 0x00000188 - ivINT_DAC1 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x63 0x0000018C - ivINT_TSI0 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x64 0x00000190 - ivINT_MCG unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x65 0x00000194 - ivINT_LPTimer unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x66 0x00000198 - ivINT_Reserved102 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x67 0x0000019C - ivINT_PORTA unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x68 0x000001A0 - ivINT_PORTB unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x69 0x000001A4 - ivINT_PORTC unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x6A 0x000001A8 - ivINT_PORTD unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x6B 0x000001AC - ivINT_PORTE unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x6C 0x000001B0 - ivINT_PORTF unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x6D 0x000001B4 - ivINT_Reserved109 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x6E 0x000001B8 - ivINT_SWI unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x6F 0x000001BC - ivINT_NFC unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x70 0x000001C0 - ivINT_USBHS unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x71 0x000001C4 - ivINT_Reserved113 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x72 0x000001C8 - ivINT_CMP3 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x73 0x000001CC - ivINT_Reserved115 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x74 0x000001D0 - ivINT_Reserved116 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x75 0x000001D4 - ivINT_FTM3 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x76 0x000001D8 - ivINT_ADC2 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x77 0x000001DC - ivINT_ADC3 unused by PE */ + (tIsrFunc)&DefaultIsr, /* 0x78 0x000001E0 - ivINT_I2S1_Tx unused by PE */ + (tIsrFunc)&DefaultIsr /* 0x79 0x000001E4 - ivINT_I2S1_Rx unused by PE */ + } +}; + +#endif + __attribute__ ((section (".vectortableram"))) tVectorTable __vect_ram; /* Interrupt vector table in RAM */ diff --git a/src/libbase/k60/watchdog.cpp b/src/libbase/k60/watchdog.cpp index 8c656bf..12bfafe 100755 --- a/src/libbase/k60/watchdog.cpp +++ b/src/libbase/k60/watchdog.cpp @@ -2,7 +2,8 @@ * watchdog.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/hardware.h" @@ -57,7 +58,9 @@ void Watchdog::InitStctrlReg(const Config &config) uint16_t reg_h = 0; +#if MK60DZ10 || MK60D10 SET_BIT(reg_h, WDOG_STCTRLH_STNDBYEN_SHIFT); +#endif SET_BIT(reg_h, WDOG_STCTRLH_WAITEN_SHIFT); SET_BIT(reg_h, WDOG_STCTRLH_STOPEN_SHIFT); if (config.is_allow_update) @@ -115,7 +118,9 @@ void Watchdog::StartupInitialize() // Disable but allow reconfig on startup uint32_t reg_h = 0; SET_BIT(reg_h, WDOG_STCTRLH_DISTESTWDOG_SHIFT); +#if MK60DZ10 || MK60D10 SET_BIT(reg_h, WDOG_STCTRLH_STNDBYEN_SHIFT); +#endif SET_BIT(reg_h, WDOG_STCTRLH_WAITEN_SHIFT); SET_BIT(reg_h, WDOG_STCTRLH_STOPEN_SHIFT); SET_BIT(reg_h, WDOG_STCTRLH_ALLOWUPDATE_SHIFT); diff --git a/src/libsc/k60/ab_encoder.cpp b/src/libsc/k60/ab_encoder.cpp new file mode 100755 index 0000000..14186ca --- /dev/null +++ b/src/libsc/k60/ab_encoder.cpp @@ -0,0 +1,44 @@ +/* + * ab_encoder.cpp + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#include + +#include "libbase/k60/ftm_quad_decoder.h" + +#include "libsc/config.h" +#include "libsc/k60/ab_encoder.h" + +using namespace libbase::k60; + +namespace libsc +{ +namespace k60 +{ + +#if LIBSC_USE_SOFT_ENCODER +SoftQuadDecoder::Config AbEncoder::Initializer::GetQuadDecoderConfig() const +{ + return Initializer::GetQuadDecoderConfig(); +} + +#else +FtmQuadDecoder::Config AbEncoder::Initializer::GetQuadDecoderConfig() const +{ + FtmQuadDecoder::Config product = Initializer::GetQuadDecoderConfig(); + product.encoding_mode = FtmQuadDecoder::Config::EncodingMode::kPhaseAB; + return product; +} + +#endif + +AbEncoder::AbEncoder(const Config &config) + : Encoder(Initializer(config)) +{} + +} +} diff --git a/src/libsc/k60/alternate_motor.cpp b/src/libsc/k60/alternate_motor.cpp index cd918d3..0cef9f1 100755 --- a/src/libsc/k60/alternate_motor.cpp +++ b/src/libsc/k60/alternate_motor.cpp @@ -2,7 +2,7 @@ * alternate_motor.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ @@ -121,7 +121,7 @@ FtmPwm::Config GetFtmPwmAConfig(const uint8_t id) config.pin = GetPwmAPin(id); config.period = PERIOD; config.pos_width = 0; - config.precision = Pwm::Config::Precision::KNs; + config.precision = Pwm::Config::Precision::kNs; config.alignment = FtmPwm::Config::Alignment::kCenter; config.is_insert_deadtime = true; config.deadtime_ns = GetDeadtime(id); @@ -134,7 +134,7 @@ FtmPwm::Config GetFtmPwmBConfig(const uint8_t id) config.pin = GetPwmBPin(id); config.period = PERIOD; config.pos_width = 0; - config.precision = Pwm::Config::Precision::KNs; + config.precision = Pwm::Config::Precision::kNs; config.alignment = FtmPwm::Config::Alignment::kCenter; config.is_insert_deadtime = true; config.deadtime_ns = GetDeadtime(id); diff --git a/src/libsc/k60/ambient_light_sensor.cpp b/src/libsc/k60/ambient_light_sensor.cpp index 9495a7d..4b73d52 100755 --- a/src/libsc/k60/ambient_light_sensor.cpp +++ b/src/libsc/k60/ambient_light_sensor.cpp @@ -3,7 +3,8 @@ * LS1970 * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -65,8 +66,8 @@ Adc::Config GetAdcConfig(const uint8_t id) } -AmbientLightSensor::AmbientLightSensor(const uint8_t id) - : m_pin(GetAdcConfig(id)) +AmbientLightSensor::AmbientLightSensor(const Config &config) + : m_pin(GetAdcConfig(config.id)) {} uint16_t AmbientLightSensor::GetLux() @@ -78,7 +79,7 @@ uint16_t AmbientLightSensor::GetLux() } #else -AmbientLightSensor::AmbientLightSensor(const uint8_t) +AmbientLightSensor::AmbientLightSensor(const Config&) : m_pin(nullptr) { LOG_DL("Configured not to use AmbientLightSensor"); diff --git a/src/libsc/k60/battery_meter.cpp b/src/libsc/k60/battery_meter.cpp new file mode 100755 index 0000000..b0ecd2c --- /dev/null +++ b/src/libsc/k60/battery_meter.cpp @@ -0,0 +1,62 @@ +/* + * battery_meter.cpp + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#include "libbase/log.h" +#include "libbase/k60/adc.h" + +#include "libsc/config.h" +#include "libsc/k60/battery_meter.h" + +using namespace libbase::k60; + +namespace libsc +{ +namespace k60 +{ + +#ifdef LIBSC_USE_BATTERY_METER + +namespace +{ + +Adc::Config GetAdcConfig() +{ + Adc::Config config; + config.adc = LIBSC_BATTERY_METER; + config.speed = Adc::Config::SpeedMode::kExFast; + config.is_continuous_mode = true; + config.avg_pass = Adc::Config::AveragePass::k4; + return config; +} + +} + +BatteryMeter::BatteryMeter(const Config &config) + : m_adc(GetAdcConfig()), + m_voltage_ratio(1.0f / config.voltage_ratio) +{ + m_adc.StartConvert(); +} + +float BatteryMeter::GetVoltage() +{ + return m_adc.GetResultF() * m_voltage_ratio; +} + +#else +BatteryMeter::BatteryMeter(const Config&) + : m_adc(nullptr) +{ + LOG_DL("Configured not to use BatteryMeter"); +} +float BatteryMeter::GetVoltage() { return 0.0f; } + +#endif /* LIBSC_USE_BATTERY_METER */ + +} +} diff --git a/src/libsc/k60/button.cpp b/src/libsc/k60/button.cpp index fe1491d..129bda5 100755 --- a/src/libsc/k60/button.cpp +++ b/src/libsc/k60/button.cpp @@ -2,7 +2,8 @@ * button.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -87,18 +88,19 @@ Gpi::Config GetGpiConfig(const uint8_t id) } -Button::Button(const uint8_t id) - : m_pin(GetGpiConfig(id)) +Button::Button(const Config &config) + : m_pin(GetGpiConfig(config.id)), + m_is_active_low(config.is_active_low) {} bool Button::IsDown() const { - return !m_pin.Get(); + return (m_pin.Get() ^ m_is_active_low); } #else -Button::Button(const uint8_t) - : m_pin(nullptr) +Button::Button(const Config&) + : m_pin(nullptr), m_is_active_low(false) { LOG_DL("Configured not to use Button"); } diff --git a/src/libsc/k60/dir_encoder.cpp b/src/libsc/k60/dir_encoder.cpp new file mode 100755 index 0000000..311cee1 --- /dev/null +++ b/src/libsc/k60/dir_encoder.cpp @@ -0,0 +1,43 @@ +/* + * dir_encoder.cpp + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#include "libbase/k60/ftm_quad_decoder.h" + +#include "libsc/config.h" +#include "libsc/k60/dir_encoder.h" + +using namespace libbase::k60; + +namespace libsc +{ +namespace k60 +{ + +#if LIBSC_USE_SOFT_ENCODER +SoftQuadDecoder::Config AbEncoder::Initializer::GetQuadDecoderConfig() const +{ + return Initializer::GetQuadDecoderConfig(); +} + +#else +FtmQuadDecoder::Config DirEncoder::Initializer::GetQuadDecoderConfig() const +{ + FtmQuadDecoder::Config product = Initializer::GetQuadDecoderConfig(); + product.encoding_mode = + FtmQuadDecoder::Config::EncodingMode::kCountDirection; + return product; +} + +#endif + +DirEncoder::DirEncoder(const Config &config) + : Encoder(Initializer(config)) +{} + +} +} diff --git a/src/libsc/k60/dir_motor.cpp b/src/libsc/k60/dir_motor.cpp index c282788..860e091 100644 --- a/src/libsc/k60/dir_motor.cpp +++ b/src/libsc/k60/dir_motor.cpp @@ -3,7 +3,7 @@ * DC Motor * * Author: Harrison Ng, Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ @@ -97,7 +97,7 @@ FtmPwm::Config GetFtmPwmConfig(const uint8_t id) config.pin = GetPwmPin(id); config.period = PERIOD; config.pos_width = 0; - config.precision = Pwm::Config::Precision::KNs; + config.precision = Pwm::Config::Precision::kNs; config.alignment = FtmPwm::Config::Alignment::kCenter; return config; } diff --git a/src/libsc/k60/temperature_sensor.cpp b/src/libsc/k60/ds18b20.cpp similarity index 70% rename from src/libsc/k60/temperature_sensor.cpp rename to src/libsc/k60/ds18b20.cpp index ac1b9b5..acf54b6 100755 --- a/src/libsc/k60/temperature_sensor.cpp +++ b/src/libsc/k60/ds18b20.cpp @@ -1,9 +1,10 @@ /* - * temperature_sensor.cpp - * DS18B20 temperature sensor + * ds18b20.h + * DS18B20 digital thermometer * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -13,15 +14,12 @@ #include "libbase/k60/gpio.h" #include "libsc/config.h" +#include "libsc/device_h/ds18b20.h" +#include "libsc/k60/ds18b20.h" #include "libsc/k60/system.h" -#include "libsc/k60/temperature_sensor.h" using namespace libbase::k60; -#define CMD_SKIP 0xCC -#define CMD_CONVERT_T 0x44 -#define CMD_READ_SP 0xBE - namespace libsc { namespace k60 @@ -53,20 +51,21 @@ GpoConfig GetGpoConfig(const uint8_t id) } -TemperatureSensor::TemperatureSensor(const uint8_t id) - : m_pin(GetGpoConfig(id)), m_temperature(0.0f) +Ds18b20::Ds18b20(const Config &config) + : m_pin(GetGpoConfig(config.id)), + m_temperature(0.0f) {} -void TemperatureSensor::UpdateTemperature() +void Ds18b20::UpdateTemperature() { if (Init()) { - SendByte(CMD_SKIP); - SendByte(CMD_CONVERT_T); + SendByte(DS18B20_SKIP); + SendByte(DS18B20_CONVERT_T); if (Init()) { - SendByte(CMD_SKIP); - SendByte(CMD_READ_SP); + SendByte(DS18B20_SKIP); + SendByte(DS18B20_READ_SP); uint16_t data = ReceiveByte(); data |= ReceiveByte() << 8; @@ -94,7 +93,7 @@ void TemperatureSensor::UpdateTemperature() } } -bool TemperatureSensor::Init() +bool Ds18b20::Init() { m_pin.Set(false); System::DelayUs(480); @@ -110,7 +109,7 @@ bool TemperatureSensor::Init() return true; } -void TemperatureSensor::SendByte(const uint8_t byte) +void Ds18b20::SendByte(const uint8_t byte) { for (int i = 0; i < 8; ++i) { @@ -124,13 +123,13 @@ void TemperatureSensor::SendByte(const uint8_t byte) else { System::DelayUs(60); - m_pin.Set(true); + m_pin.EnsureGpi(); } System::DelayUs(1); } } -uint8_t TemperatureSensor::ReceiveByte() +uint8_t Ds18b20::ReceiveByte() { uint8_t product = 0; for (int i = 0; i < 8; ++i) @@ -145,12 +144,12 @@ uint8_t TemperatureSensor::ReceiveByte() #else -TemperatureSensor::TemperatureSensor(const uint8_t) +Ds18b20::Ds18b20(const Config&) : m_pin(nullptr), m_temperature(0.0f) { - LOG_DL("Configured not to use TemperatureSensor"); + LOG_DL("Configured not to use Ds18b20"); } -void TemperatureSensor::UpdateTemperature() {} +void Ds18b20::UpdateTemperature() {} #endif /* LIBSC_USE_TEMPERATURE_SENSOR */ diff --git a/src/libsc/k60/encoder.cpp b/src/libsc/k60/encoder.cpp index 8406138..8705e23 100755 --- a/src/libsc/k60/encoder.cpp +++ b/src/libsc/k60/encoder.cpp @@ -3,7 +3,8 @@ * Phase A/B encoder * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -26,7 +27,7 @@ namespace libsc namespace k60 { -#ifdef LIBSC_USE_ENCODER +#if LIBSC_USE_ENCODER namespace { @@ -85,8 +86,8 @@ inline Pin::Name GetQdb(const uint8_t id) #endif // LIBSC_USE_ENCODER -#ifdef LIBSC_USE_SOFT_ENCODER -SoftQuadDecoder::Config GetQuadDecoderConfig(const uint8_t id) +#if LIBSC_USE_SOFT_ENCODER +inline SoftQuadDecoder::Config GetQuadDecoderConfig_(const uint8_t id) { SoftQuadDecoder::Config config; config.a_pin = GetQda(id); @@ -95,12 +96,11 @@ SoftQuadDecoder::Config GetQuadDecoderConfig(const uint8_t id) } #else -FtmQuadDecoder::Config GetQuadDecoderConfig(const uint8_t id) +inline FtmQuadDecoder::Config GetQuadDecoderConfig_(const uint8_t id) { FtmQuadDecoder::Config config; config.a_pin = GetQda(id); config.b_pin = GetQdb(id); - config.encoding_mode = FtmQuadDecoder::Config::EncodingMode::kPhaseAB; config.a_filter_length = 1; config.b_filter_length = 1; return config; @@ -110,22 +110,25 @@ FtmQuadDecoder::Config GetQuadDecoderConfig(const uint8_t id) } -Encoder::Encoder(const uint8_t id) - : m_id(id), - m_count(0), - m_quad_decoder(GetQuadDecoderConfig(m_id)) +Encoder::QuadDecoder::Config Encoder::Initializer::GetQuadDecoderConfig() const +{ + return GetQuadDecoderConfig_(config.id); +} + +Encoder::Encoder(const Initializer &initializer) + : m_count(0), + m_quad_decoder(initializer.GetQuadDecoderConfig()) {} void Encoder::Update() { m_count = m_quad_decoder.GetCount(); m_quad_decoder.ResetCount(); - return; } #else -Encoder::Encoder(const uint8_t) - : m_id(0), m_count(0) +Encoder::Encoder(const Initializer&) + : m_count(0), m_quad_decoder(nullptr) { LOG_DL("Configured not to use Encoder"); } diff --git a/src/libsc/k60/futaba_s3010.cpp b/src/libsc/k60/futaba_s3010.cpp index b6ffdc5..2fd73a8 100755 --- a/src/libsc/k60/futaba_s3010.cpp +++ b/src/libsc/k60/futaba_s3010.cpp @@ -3,7 +3,8 @@ * Futaba S3010 RC servo * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -19,8 +20,23 @@ namespace libsc namespace k60 { -FutabaS3010::FutabaS3010(const uint8_t id) - : Servo(id, PERIOD, POS_WIDTH_MIN, POS_WIDTH_MAX) +namespace +{ + +Servo::Config GetServoConfig(const uint8_t id) +{ + Servo::Config config; + config.id = id; + config.period = PERIOD; + config.min_pos_width = POS_WIDTH_MIN; + config.max_pos_width = POS_WIDTH_MAX; + return config; +} + +} + +FutabaS3010::FutabaS3010(const Config &config) + : Servo(GetServoConfig(config.id)) {} } diff --git a/src/libsc/k60/gyroscope.cpp b/src/libsc/k60/gyroscope.cpp deleted file mode 100755 index d880ba6..0000000 --- a/src/libsc/k60/gyroscope.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - * gyroscope.cpp - * Gyroscope - * - * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team - */ - -#include - -#include - -#include "libbase/log.h" -#include "libbase/k60/adc.h" -#include "libbase/k60/pin.h" - -#include "libsc/config.h" -#include "libsc/k60/gyroscope.h" - -using namespace libbase::k60; - -namespace libsc -{ -namespace k60 -{ - -#ifdef LIBSC_USE_GYRO - -namespace -{ - -#if LIBSC_USE_GYRO == 1 -inline Pin::Name GetPin(const uint8_t id) -{ - if (id != 0) - { - assert(false); - } - return LIBSC_GYRO0_ANGULAR; -} - -#endif - -Adc::Config GetAdcConfig(const uint8_t id) -{ - Adc::Config config; - config.pin = GetPin(id); - config.resolution = Adc::Config::Resolution::k16Bit; - return config; -} - -} - -Gyroscope::Gyroscope() - : Gyroscope(3) -{} - -Gyroscope::Gyroscope(const Uint avg_size) - : m_pin(GetAdcConfig(0)), - m_current(0), - m_avg(0), - m_values(nullptr), - m_size(avg_size), - m_index(0) -{ - if (m_size > 0) - { - m_values = new uint16_t[m_size]; - std::fill(m_values, m_values + m_size, 0); - } -} - -Gyroscope::~Gyroscope() -{ - delete[] m_values; -} - -void Gyroscope::Update() -{ - m_current = m_pin.GetResult(); - if (!m_values) - { - m_avg = m_current; - } - else - { - m_avg -= m_values[m_index] / (float)m_size; - m_avg += m_current / (float)m_size; - m_values[m_index] = m_current; - - if (++m_index >= m_size) - { - m_index = 0; - } - } -} - -#else -Gyroscope::Gyroscope() - : m_pin(nullptr), m_current(0), m_avg(0), m_values(nullptr), m_size(0), - m_index(0) -{ - LOG_DL("Configured not to use Gyroscope"); -} -Gyroscope::Gyroscope(const Uint) : Gyroscope() {} -Gyroscope::~Gyroscope() {} -void Gyroscope::Update() {} - -#endif - -} -} diff --git a/src/libsc/k60/infra_red_sensor.cpp b/src/libsc/k60/infra_red_sensor.cpp index efd7535..1a1f934 100755 --- a/src/libsc/k60/infra_red_sensor.cpp +++ b/src/libsc/k60/infra_red_sensor.cpp @@ -3,7 +3,8 @@ * Infra-red sensor * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -59,7 +60,7 @@ inline Pin::Name GetPin(const uint8_t id) #endif Gpi::Config GetGpiConfig(const uint8_t id, - const InfraRedSensor::OnDetectListener &listener) + const Gpi::OnGpiEventListener &listener) { Gpi::Config config; config.pin = GetPin(id); @@ -67,37 +68,36 @@ Gpi::Config GetGpiConfig(const uint8_t id, if (listener) { config.interrupt = Pin::Config::Interrupt::kRising; - config.isr = [&listener](Gpi*) - { - listener(id); - }; + config.isr = listener; } return config; } } -InfraRedSensor::InfraRedSensor(const uint8_t id, - const OnDetectListener &listener) - : m_pin(nullptr), m_isr(listener) +InfraRedSensor::InfraRedSensor(const Config &config) + : m_isr(config.listener), + m_pin(nullptr) { - m_pin = Gpi(GetGpiConfig(id, m_isr)); + Gpi::OnGpiEventListener listener; + if (config.listener) + { + const uint8_t id = config.id; + listener = [this, id](Gpi*) + { + m_isr(id); + }; + } + m_pin = Gpi(GetGpiConfig(config.id, listener)); } -InfraRedSensor::InfraRedSensor(const uint8_t id) - : InfraRedSensor(id, nullptr) -{} - bool InfraRedSensor::IsDetected() const { return m_pin.Get(); } #else -InfraRedSensor::InfraRedSensor(const uint8_t, const OnDetectListener&) - : InfraRedSensor(0) -{} -InfraRedSensor::InfraRedSensor(const uint8_t) +InfraRedSensor::InfraRedSensor(const Config&) : m_pin(nullptr) { LOG_DL("Configured not to use InfraRedSensor"); diff --git a/src/libsc/k60/joystick.cpp b/src/libsc/k60/joystick.cpp index 0b88e21..01b38c1 100755 --- a/src/libsc/k60/joystick.cpp +++ b/src/libsc/k60/joystick.cpp @@ -1,9 +1,9 @@ /* * joystick.cpp - * Joystick * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -28,21 +28,54 @@ namespace { #if LIBSC_USE_JOYSTICK == 1 -#define GetUpPin(x) LIBSC_JOYSTICK0_UP -#define GetDownPin(x) LIBSC_JOYSTICK0_DOWN -#define GetLeftPin(x) LIBSC_JOYSTICK0_LEFT -#define GetRightPin(x) LIBSC_JOYSTICK0_RIGHT -#define GetSelectPin(x) LIBSC_JOYSTICK0_SELECT +inline Pin::Name GetUpPin(const uint8_t id) +{ + if (id != 0) + { + assert(false); + } + return LIBSC_JOYSTICK0_UP; +} -#endif +inline Pin::Name GetDownPin(const uint8_t id) +{ + if (id != 0) + { + assert(false); + } + return LIBSC_JOYSTICK0_DOWN; +} + +inline Pin::Name GetLeftPin(const uint8_t id) +{ + if (id != 0) + { + assert(false); + } + return LIBSC_JOYSTICK0_LEFT; +} + +inline Pin::Name GetRightPin(const uint8_t id) +{ + if (id != 0) + { + assert(false); + } + return LIBSC_JOYSTICK0_RIGHT; +} + +inline Pin::Name GetSelectPin(const uint8_t id) +{ + if (id != 0) + { + assert(false); + } + return LIBSC_JOYSTICK0_SELECT; +} -#if LIBSC_USE_JOYSTICK == 1 && defined(__GNUC__) -#define UNUSED __attribute__((__unused__)) -#else -#define UNUSED #endif -Gpi::Config GetUpGpiConfig(const uint8_t id UNUSED) +Gpi::Config GetUpGpiConfig(const uint8_t id) { Gpi::Config config; config.pin = GetUpPin(id); @@ -50,7 +83,7 @@ Gpi::Config GetUpGpiConfig(const uint8_t id UNUSED) return config; } -Gpi::Config GetDownGpiConfig(const uint8_t id UNUSED) +Gpi::Config GetDownGpiConfig(const uint8_t id) { Gpi::Config config; config.pin = GetDownPin(id); @@ -58,7 +91,7 @@ Gpi::Config GetDownGpiConfig(const uint8_t id UNUSED) return config; } -Gpi::Config GetLeftGpiConfig(const uint8_t id UNUSED) +Gpi::Config GetLeftGpiConfig(const uint8_t id) { Gpi::Config config; config.pin = GetLeftPin(id); @@ -66,7 +99,7 @@ Gpi::Config GetLeftGpiConfig(const uint8_t id UNUSED) return config; } -Gpi::Config GetRightGpiConfig(const uint8_t id UNUSED) +Gpi::Config GetRightGpiConfig(const uint8_t id) { Gpi::Config config; config.pin = GetRightPin(id); @@ -74,7 +107,7 @@ Gpi::Config GetRightGpiConfig(const uint8_t id UNUSED) return config; } -Gpi::Config GetSelectGpiConfig(const uint8_t id UNUSED) +Gpi::Config GetSelectGpiConfig(const uint8_t id) { Gpi::Config config; config.pin = GetSelectPin(id); @@ -82,37 +115,36 @@ Gpi::Config GetSelectGpiConfig(const uint8_t id UNUSED) return config; } -#if LIBSC_USE_JOYSTICK == 1 -#pragma GCC diagnostic pop -#endif - } -Joystick::Joystick(const uint8_t id) - : m_pins{Gpi(GetUpGpiConfig(id)), Gpi(GetDownGpiConfig(id)), - Gpi(GetLeftGpiConfig(id)), Gpi(GetRightGpiConfig(id)), - Gpi(GetSelectGpiConfig(id))} +Joystick::Joystick(const Config &config) + : m_pins{Gpi(GetUpGpiConfig(config.id)), + Gpi(GetDownGpiConfig(config.id)), + Gpi(GetLeftGpiConfig(config.id)), + Gpi(GetRightGpiConfig(config.id)), + Gpi(GetSelectGpiConfig(config.id))}, + m_is_active_low(config.is_active_low) {} Joystick::State Joystick::GetState() const { - if (!m_pins[0].Get()) + if (m_pins[0].Get() ^ m_is_active_low) { return State::UP; } - else if (!m_pins[1].Get()) + else if (m_pins[1].Get() ^ m_is_active_low) { return State::DOWN; } - else if (!m_pins[2].Get()) + else if (m_pins[2].Get() ^ m_is_active_low) { return State::LEFT; } - else if (!m_pins[3].Get()) + else if (m_pins[3].Get() ^ m_is_active_low) { return State::RIGHT; } - else if (!m_pins[4].Get()) + else if (m_pins[4].Get() ^ m_is_active_low) { return State::SELECT; } @@ -123,7 +155,7 @@ Joystick::State Joystick::GetState() const } #else -Joystick::Joystick(const uint8_t) +Joystick::Joystick(const Config&) : m_pins{Gpi(nullptr), Gpi(nullptr), Gpi(nullptr), Gpi(nullptr), Gpi(nullptr)} { diff --git a/src/libsc/k60/jy_mcu_bt_106.cpp b/src/libsc/k60/jy_mcu_bt_106.cpp index 65f1343..c3c65c7 100755 --- a/src/libsc/k60/jy_mcu_bt_106.cpp +++ b/src/libsc/k60/jy_mcu_bt_106.cpp @@ -1,13 +1,18 @@ /* * jy_mcu_bt_106.cpp + * JY-MCU BT Board v1.06 * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ +#include + +#include "libbase/k60/pin.h" #include "libbase/k60/uart.h" +#include "libsc/config.h" #include "libsc/k60/jy_mcu_bt_106.h" using namespace libbase::k60; @@ -17,20 +22,26 @@ namespace libsc namespace k60 { -JyMcuBt106::JyMcuBt106(const uint8_t id, - const Uart::Config::BaudRate baud_rate) - : UartDevice(UartConfigBuilder(id, baud_rate, this)) -{} +namespace +{ -Uart::Config JyMcuBt106::UartConfigBuilder::Build() const +UartDevice::Config GetUartDeviceConfig(const JyMcuBt106::Config &config) { - Uart::Config config = UartDevice::UartConfigBuilder::Build(); + UartDevice::Config product; + product.id = config.id; + product.baud_rate = config.baud_rate; // On this board, there's a diode connected to the Tx pin, preventing the // module to correctly send data to the MCU - config.rx_config[Pin::Config::kPullEnable] = true; - config.rx_config[Pin::Config::kPullUp] = true; - return config; + product.rx_config[Pin::Config::kPullEnable] = true; + product.rx_config[Pin::Config::kPullUp] = true; + return product; } +} + +JyMcuBt106::JyMcuBt106(const Config &config) + : UartDevice(GetUartDeviceConfig(config)) +{} + } } diff --git a/src/libsc/k60/lcd_console.cpp b/src/libsc/k60/lcd_console.cpp index a036d05..f31f856 100644 --- a/src/libsc/k60/lcd_console.cpp +++ b/src/libsc/k60/lcd_console.cpp @@ -3,7 +3,8 @@ * Use the LCD screen as console * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libsc/k60/led.cpp b/src/libsc/k60/led.cpp index 54f96dd..cc884e7 100644 --- a/src/libsc/k60/led.cpp +++ b/src/libsc/k60/led.cpp @@ -2,7 +2,8 @@ * led.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -77,8 +78,8 @@ Gpo::Config GetGpoConfig(const uint8_t id) } -Led::Led(const uint8_t id) - : m_pin(GetGpoConfig(id)) +Led::Led(const Config &config) + : m_pin(GetGpoConfig(config.id)) {} void Led::SetEnable(const bool flag) @@ -92,7 +93,7 @@ void Led::Switch() } #else -Led::Led(const uint8_t) +Led::Led(const Config&) : m_pin(nullptr) { LOG_DL("Configured not to use Led"); diff --git a/src/libsc/k60/light_sensor.cpp b/src/libsc/k60/light_sensor.cpp old mode 100755 new mode 100644 index 89938e9..ae62ba5 --- a/src/libsc/k60/light_sensor.cpp +++ b/src/libsc/k60/light_sensor.cpp @@ -1,9 +1,9 @@ /* * light_sensor.cpp - * Light sensor * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -58,50 +58,68 @@ inline Pin::Name GetPin(const uint8_t id) #endif -Gpi::Config GetGpiConfig(const uint8_t id, - const LightSensor::OnDetectListener &listener) +Gpi::Config GetGpiConfig(const LightSensor::Config &config, + const Gpi::OnGpiEventListener &listener) { - Gpi::Config config; - config.pin = GetPin(id); - config.config.set(Pin::Config::ConfigBit::kPassiveFilter); + Gpi::Config product; + product.pin = GetPin(config.id); + product.config.set(Pin::Config::ConfigBit::kPassiveFilter); if (listener) { - config.interrupt = Pin::Config::Interrupt::kRising; - config.isr = [&listener, id](Gpi*) - { - listener(id); - }; + switch (config.listener_trigger) + { + default: + case LightSensor::Config::Trigger::kBright: + product.interrupt = (config.is_active_low + ? Pin::Config::Interrupt::kFalling + : Pin::Config::Interrupt::kRising); + break; + + case LightSensor::Config::Trigger::kDark: + product.interrupt = (config.is_active_low + ? Pin::Config::Interrupt::kRising + : Pin::Config::Interrupt::kFalling); + break; + + case LightSensor::Config::Trigger::kBoth: + product.interrupt = Pin::Config::Interrupt::kBoth; + break; + } + product.isr = listener; } - return config; + return product; } } -LightSensor::LightSensor(const uint8_t id, const OnDetectListener &listener) - : m_pin(nullptr), m_isr(listener) +LightSensor::LightSensor(const Config &config) + : m_pin(nullptr), + m_is_active_low(config.is_active_low) { - m_pin = Gpi(GetGpiConfig(id, m_isr)); + Gpi::OnGpiEventListener listener; + if (config.listener) + { + const uint8_t id = config.id; + listener = [this, id](Gpi*) + { + m_isr(id); + }; + } + m_pin = Gpi(GetGpiConfig(config, listener)); } -LightSensor::LightSensor(const uint8_t id) - : LightSensor(id, nullptr) -{} - -bool LightSensor::IsDetected() const +bool LightSensor::IsBright() const { - return m_pin.Get(); + return (m_pin.Get() ^ m_is_active_low); } #else -LightSensor::LightSensor(const uint8_t, const OnDetectListener&) - : LightSensor(0) -{} -LightSensor::LightSensor(const uint8_t) - : m_pin(nullptr) +LightSensor::LightSensor(const Config&) + : m_pin(nullptr), m_is_active_low(false) { LOG_DL("Configured not to use LightSensor"); } -bool LightSensor::IsDetected() const { return false; } +bool LightSensor::IsBright() const { return false; } #endif diff --git a/src/libsc/k60/linear_ccd.cpp b/src/libsc/k60/linear_ccd.cpp index 9f2606b..a5e2954 100755 --- a/src/libsc/k60/linear_ccd.cpp +++ b/src/libsc/k60/linear_ccd.cpp @@ -3,7 +3,8 @@ * Linear CCD (for TSL1401CL) * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libsc/k60/motor.cpp b/src/libsc/k60/motor.cpp index e2e6397..aded40e 100755 --- a/src/libsc/k60/motor.cpp +++ b/src/libsc/k60/motor.cpp @@ -2,7 +2,7 @@ * motor.cpp * * Author: Harrison Ng, Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ diff --git a/src/libsc/k60/mpu6050.cpp b/src/libsc/k60/mpu6050.cpp new file mode 100644 index 0000000..78ccced --- /dev/null +++ b/src/libsc/k60/mpu6050.cpp @@ -0,0 +1,155 @@ +/* + * mpu6050.cpp + * + * Author: Harrison Ng + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#include "libbase/k60/hardware.h" + +#include +#include +#include + +#include "libbase/log.h" +#include "libbase/k60/soft_i2c_master.h" + +#include "libsc/config.h" +#include "libsc/device_h/mpu6050.h" +#include "libsc/k60/mpu6050.h" +#include "libutil/misc.h" + +using namespace libbase::k60; +using namespace std; + +namespace libsc +{ +namespace k60 +{ + +#ifdef LIBSC_USE_MPU6050 + +namespace +{ + +SoftI2cMaster::Config GetI2CConfig() +{ + SoftI2cMaster::Config config; + config.scl_pin = LIBSC_MPU6050_SCL; + config.sda_pin = LIBSC_MPU6050_SDA; + return config; +} + +} + +Mpu6050::Mpu6050(const Config &config) + : m_i2c(GetI2CConfig()), + m_gyro_range(config.gyro_range), + m_accel_range(config.accel_range) +{ + m_i2c.SendByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_1, 0x00); + + //Register 25 – Sample Rate Divider: Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV) + //Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or 7) + m_i2c.SendByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_SMPLRT_DIV, 0x00); + + //Register 26 - CONFIG: EXT_SYNC_SET[2:0]<<3 | DLPF_CFG[2:0]; + //EXT_SYNC_SET=0, Input disabled; + //DLPF_CFG=0, Accel = 260Hz, Gyroscope = 256Hz; + m_i2c.SendByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_CONFIG, 0x00); + + //Register 27 - GYRO_CONFIG: FS_SEL[1:0] << 3; + //FS_SEL=0, ± 250 °/s; FS_SEL=1, ± 500 °/s; FS_SEL=2, ± 1000 °/s; FS_SEL=3, ± 2000 °/s; + uint8_t gyro_config = static_cast(m_gyro_range) << 3; + m_i2c.SendByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_GYRO_CONFIG, gyro_config); + + //Register 28 - ACCEL_CONFIG: AFS_SEL[1:0] << 3; + //AFS_SEL=0, ±2g; AFS_SEL=1, ±4g; AFS_SEL=2, ±8g; AFS_SEL=3, ±16g; + uint8_t accel_config = static_cast(m_accel_range) << 3; + m_i2c.SendByte(MPU6050_DEFAULT_ADDRESS, MPU6050_RA_ACCEL_CONFIG, + accel_config); +} + +float Mpu6050::GetGyroScaleFactor() +{ + switch (m_gyro_range) + { + default: + LOG_EL("Mpu6050 Gyro in illegal state"); + case Config::Range::kSmall: + return 131.0f; + + case Config::Range::kMid: + return 65.5f; + + case Config::Range::kLarge: + return 32.8f; + + case Config::Range::kExtreme: + return 16.4f; + } +} + +float Mpu6050::GetAccelScaleFactor() +{ + switch (m_accel_range) + { + default: + LOG_EL("Mpu6050 Accel in illegal state"); + case Config::Range::kSmall: + return 16384.0f; + + case Config::Range::kMid: + return 8192.0f; + + case Config::Range::kLarge: + return 4096.0f; + + case Config::Range::kExtreme: + return 2048.0f; + } +} + +void Mpu6050::Update() +{ + const vector &data = m_i2c.GetBytes(MPU6050_DEFAULT_ADDRESS, + MPU6050_RA_ACCEL_XOUT_H, 14); + int16_t raw_accel[3]; + int16_t raw_gyro[3]; + for (size_t i = 0; i < data.size(); i += 2) + { + if (i <= 5) + { + const int j = i / 2; + raw_accel[j] = data[i] << 8 | data[i + 1]; + m_accel[j] = (float)raw_accel[j] / GetAccelScaleFactor(); + } + else if (i == 6) + { + const int16_t raw_temp = data[i] << 8 | data[i + 1]; + m_temp = (float)raw_temp / 340 + 36.53; + } + else + { + const int j = (i - 8) / 2; + raw_gyro[j] = data[i] << 8 | data[i + 1]; + m_omega[j] = (float)raw_gyro[j] / GetGyroScaleFactor(); + } + } +} + +#else +Mpu6050::Mpu6050(const Config&) + : m_i2c(nullptr), m_temp(0), + m_gyro_range(Config::Range::kSmall), + m_accel_range(Config::Range::kSmall) +{ + LOG_DL("Configured not to use Mpu6050"); +} +void Mpu6050::Update() {} + +#endif /* LIBSC_USE_MPU6050 */ + +} /* namespace k60 */ +} /* namespace libsc */ diff --git a/src/libsc/k60/pit_delay.cpp b/src/libsc/k60/pit_delay.cpp index 85a1a12..50e0f6e 100755 --- a/src/libsc/k60/pit_delay.cpp +++ b/src/libsc/k60/pit_delay.cpp @@ -2,7 +2,8 @@ * pit_delay.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libsc/k60/pit_timer.cpp b/src/libsc/k60/pit_timer.cpp index 18d4c02..6e0ab11 100644 --- a/src/libsc/k60/pit_timer.cpp +++ b/src/libsc/k60/pit_timer.cpp @@ -2,7 +2,8 @@ * timer.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libsc/k60/servo.cpp b/src/libsc/k60/servo.cpp index becafad..be79682 100644 --- a/src/libsc/k60/servo.cpp +++ b/src/libsc/k60/servo.cpp @@ -3,7 +3,8 @@ * RC servo * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -113,12 +114,11 @@ FtmPwm::Config GetPwmConfig(const uint8_t id, const uint16_t period, } -Servo::Servo(const uint8_t id, const uint16_t period, - const uint16_t pos_width_min, const uint16_t pos_width_max) - : m_pos_width_min(pos_width_min), - m_pos_width_diff(pos_width_max - pos_width_min), - m_pwm(GetPwmConfig(id, period, - (pos_width_max - pos_width_min) / 2 + pos_width_min)), +Servo::Servo(const Config &config) + : m_pos_width_min(config.min_pos_width), + m_pos_width_diff(config.max_pos_width - config.min_pos_width), + m_pwm(GetPwmConfig(config.id, config.period, + m_pos_width_diff / 2 + m_pos_width_min)), m_degree(900) {} @@ -137,7 +137,7 @@ void Servo::SetDegree(const uint16_t degree) } #else -Servo::Servo(const uint8_t, const uint16_t, const uint16_t, const uint16_t) +Servo::Servo(const Config&) : m_pos_width_min(0), m_pos_width_diff(0), m_degree(0) { LOG_DL("Configured not to use Servo"); diff --git a/src/libsc/k60/simple_buzzer.cpp b/src/libsc/k60/simple_buzzer.cpp index df6ac19..42857d5 100755 --- a/src/libsc/k60/simple_buzzer.cpp +++ b/src/libsc/k60/simple_buzzer.cpp @@ -3,7 +3,8 @@ * Simple Buzzer * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libsc/k60/spi_device.cpp b/src/libsc/k60/spi_device.cpp deleted file mode 100755 index fd8d7f0..0000000 --- a/src/libsc/k60/spi_device.cpp +++ /dev/null @@ -1,146 +0,0 @@ -/* - * spi_device.cpp - * - * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team - */ - -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "libbase/k60/misc_utils.h" -#include "libbase/k60/spi_master.h" - -#include "libsc/k60/spi_device.h" -#include "libutil/dynamic_block_buffer.h" - -using namespace libbase::k60; -using namespace libutil; -using namespace std; - -#define BLOCK_SIZE 64 - -namespace libsc -{ -namespace k60 -{ - -namespace -{ - -SpiMaster::Config GetSpiConfig(const SpiMaster::Config &config, - const SpiMaster::OnTxFillListener &tx_isr, - const SpiMaster::OnRxDrainListener &rx_isr) -{ - SpiMaster::Config product = config; - product.tx_isr = tx_isr; - product.rx_isr = rx_isr; - return product; -} - -} - -SpiDevice::SpiDevice(const SpiMaster::Config &config) - : m_tx_buf(28), - m_spi(GetSpiConfig(config, - std::bind(&SpiDevice::OnTxFill, this, placeholders::_1), - std::bind(&SpiDevice::OnRxDrain, this, placeholders::_1))) -{} - -SpiDevice::SpiDevice(SpiDevice&&) - : SpiDevice(nullptr) -{ - // TODO - //*this = std::move(rhs); -} - -SpiDevice::SpiDevice(nullptr_t) - : m_tx_buf(0), - m_spi(nullptr) -{} - -SpiDevice::~SpiDevice() -{} - -void SpiDevice::SendBuffer(const Byte *buf, const size_t len) -{ - if (len == 0) - { - return; - } - Byte *data = new Byte[len]; - memcpy(data, buf, len); - - m_tx_buf.PushBuffer(DynamicBlockBuffer::Block(data, len)); - m_spi.KickStart(); -} - -void SpiDevice::SendBuffer(unique_ptr &&buf, const size_t len) -{ - if (len == 0) - { - return; - } - - m_tx_buf.PushBuffer(DynamicBlockBuffer::Block(buf.release(), len)); - m_spi.KickStart(); -} - -void SpiDevice::SendBuffer(vector &&buf) -{ - if (buf.size() == 0) - { - return; - } - - m_tx_buf.PushBuffer(DynamicBlockBuffer::Block(new vector(std::move(buf)))); - m_spi.KickStart(); -} - -void SpiDevice::OnTxFill(SpiMaster *spi) -{ - volatile DynamicBlockBuffer::Block *block = m_tx_buf.GetActiveBlock(); - if (m_tx_buf.GetSize() == 0 && (!block || block->it == block->size)) - { - return; - } - - while (!block || block->it == block->size) - { - m_tx_buf.Acquire(); - block = m_tx_buf.GetActiveBlock(); - } - - const size_t size = block->size - block->it; - switch (block->type) - { - case DynamicBlockBuffer::Block::kByteAry: - block->it += spi->PushData(0, block->data.byte_ + block->it, size); - break; - - case DynamicBlockBuffer::Block::kString: - block->it += spi->PushData(0, (const Byte*)block->data.string_->data() - + block->it, size); - break; - - case DynamicBlockBuffer::Block::kVector: - block->it += spi->PushData(0, block->data.vector_->data() + block->it, - size); - break; - } -} - -void SpiDevice::OnRxDrain(SpiMaster*) -{ - // TODO -} - -} -} diff --git a/src/libsc/k60/st7735r.cpp b/src/libsc/k60/st7735r.cpp index 12a997e..41db1a9 100644 --- a/src/libsc/k60/st7735r.cpp +++ b/src/libsc/k60/st7735r.cpp @@ -2,7 +2,8 @@ * st7735r.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ /* * Author: Henry @@ -17,6 +18,7 @@ #include "libbase/k60/soft_spi_master.h" #include "libsc/config.h" +#include "libsc/device_h/st7735r.h" #include "libsc/k60/st7735r.h" #include "libsc/k60/system.h" #include "libsc/lcd_font.h" @@ -25,24 +27,6 @@ #define SEND_COMMAND(dat) Send(true, dat) #define SEND_DATA(dat) Send(false, dat) -#define CMD_SW_RESET 0x01 -#define CMD_SLEEP_OUT 0x11 -#define CMD_DISPLAY_ON 0x29 -#define CMD_COLUMN_ADDRESS_SET 0x2A -#define CMD_ROW_ADDRESS_SET 0x2B -#define CMD_MEMORY_WRITE 0x2C -#define CMD_MEM_DATA_ACCESS_CTRL 0x36 -#define CMD_PIXEL_FORMAT 0x3A -#define CMD_FRAME_RATE_CTRL_NORMAL 0xB1 -#define CMD_POWER_CTRL1 0xC0 -#define CMD_POWER_CTRL2 0xC1 -#define CMD_POWER_CTRL3 0xC2 -#define CMD_POWER_CTRL4 0xC3 -#define CMD_POWER_CTRL5 0xC4 -#define CMD_VCOM_CTRL 0xC5 -#define CMD_GAMMA_CORRECTION_POS_POLARITY 0xE0 -#define CMD_GAMMA_CORRECTION_NEG_POLARITY 0xE1 - using namespace libbase::k60; using namespace std; @@ -90,63 +74,61 @@ Gpo::Config GetDcConfig() } -St7735r::St7735r() - : St7735r(false) -{} - -St7735r::St7735r(const bool is_revert) +St7735r::St7735r(const Config &config) : m_spi(GetSpiConfig()), m_rst(GetRstConfig()), m_dc(GetDcConfig()), m_bg_color(0) { -#ifdef LIBSC_USE_LCD_HW_SPI - spi_init(SpiUtils::GetSpiModule(), - SpiUtils::GetSpiPcs(), - MASTER, SPI_MOSI_FLAG, 12000000u); -#endif - Clear(); - SEND_COMMAND(CMD_SW_RESET); + SEND_COMMAND(ST7735R_SWRESET); System::DelayMs(10); - SEND_COMMAND(CMD_SLEEP_OUT); + SEND_COMMAND(ST7735R_SLPOUT); System::DelayMs(120); - SEND_COMMAND(CMD_MEM_DATA_ACCESS_CTRL); - SEND_DATA(is_revert ? 0x08 : 0xC8); + uint8_t madctl_reg = 0; + if (!config.is_revert) + { + madctl_reg |= 0xC0; + } +#ifdef LIBSC_ST7735R_BGR_PANEL + madctl_reg |= 0x08; +#endif + SEND_COMMAND(ST7735R_MADCTL); + SEND_DATA(madctl_reg); // 16-bit - SEND_COMMAND(CMD_PIXEL_FORMAT); + SEND_COMMAND(ST7735R_COLMOD); SEND_DATA(0x55); // ~60Hz - SEND_COMMAND(CMD_FRAME_RATE_CTRL_NORMAL); + SEND_COMMAND(ST7735R_FRMCTRL1); SEND_DATA(0x05); SEND_DATA(0x3C); SEND_DATA(0x3C); - SEND_COMMAND(CMD_POWER_CTRL1); + SEND_COMMAND(ST7735R_PWCTR1); SEND_DATA(0xA2); SEND_DATA(0x02); SEND_DATA(0x84); - SEND_COMMAND(CMD_POWER_CTRL2); + SEND_COMMAND(ST7735R_PWCTR2); SEND_DATA(0xC5); - SEND_COMMAND(CMD_POWER_CTRL3); + SEND_COMMAND(ST7735R_PWCTR3); SEND_DATA(0x0A); SEND_DATA(0x00); - SEND_COMMAND(CMD_POWER_CTRL4); + SEND_COMMAND(ST7735R_PWCTR4); SEND_DATA(0x8A); SEND_DATA(0x2A); - SEND_COMMAND(CMD_POWER_CTRL5); + SEND_COMMAND(ST7735R_PWCTR5); SEND_DATA(0x8A); SEND_DATA(0xEE); - SEND_COMMAND(CMD_VCOM_CTRL); + SEND_COMMAND(ST7735R_VMCTR1); SEND_DATA(0x0E); - SEND_COMMAND(CMD_GAMMA_CORRECTION_POS_POLARITY); + SEND_COMMAND(ST7735R_GMCTRP1); SEND_DATA(0x02); SEND_DATA(0x1C); SEND_DATA(0x07); @@ -164,7 +146,7 @@ St7735r::St7735r(const bool is_revert) SEND_DATA(0x03); SEND_DATA(0x10); - SEND_COMMAND(CMD_GAMMA_CORRECTION_NEG_POLARITY); + SEND_COMMAND(ST7735R_GMCTRN1); SEND_DATA(0x03); SEND_DATA(0x1D); SEND_DATA(0x07); @@ -184,7 +166,7 @@ St7735r::St7735r(const bool is_revert) SetActiveRect(0, 0, kW, kH); - SEND_COMMAND(CMD_DISPLAY_ON); + SEND_COMMAND(ST7735R_DISPON); System::DelayMs(10); } @@ -202,7 +184,7 @@ void St7735r::DrawGrayscalePixelBuffer(const uint8_t x, const uint8_t y, const uint8_t w, const uint8_t h, const uint8_t *pixel) { SetActiveRect(x, y, w, h); - SEND_COMMAND(CMD_MEMORY_WRITE); + SEND_COMMAND(ST7735R_RAMWR); const Uint area = w * h; for (Uint i = 0; i < area; ++i) { @@ -216,7 +198,7 @@ void St7735r::DrawPixelBuffer(const uint8_t x, const uint8_t y, const uint8_t w, const uint8_t h, const uint16_t *pixel) { SetActiveRect(x, y, w, h); - SEND_COMMAND(CMD_MEMORY_WRITE); + SEND_COMMAND(ST7735R_RAMWR); const Uint area = w * h; for (Uint i = 0; i < area; ++i) { @@ -230,7 +212,7 @@ void St7735r::DrawPixelBuffer(const uint8_t x, const uint8_t y, const uint8_t w, const bool *data) { SetActiveRect(x, y, w, h); - SEND_COMMAND(CMD_MEMORY_WRITE); + SEND_COMMAND(ST7735R_RAMWR); const Uint area = w * h; for (Uint i = 0; i < area; ++i) { @@ -251,7 +233,7 @@ void St7735r::DrawPixel(const uint8_t x, const uint8_t y, const uint8_t w, const uint8_t h, const uint16_t color) { SetActiveRect(x, y, w, h); - SEND_COMMAND(CMD_MEMORY_WRITE); + SEND_COMMAND(ST7735R_RAMWR); const Uint area = w * h; for (Uint i = 0; i < area; ++i) { @@ -294,7 +276,7 @@ void St7735r::DrawChar(const uint8_t x, const uint8_t y, const char ch, void St7735r::SetActiveRect(const uint8_t x, const uint8_t y, const uint8_t w, const uint8_t h) { - SEND_COMMAND(CMD_COLUMN_ADDRESS_SET); + SEND_COMMAND(ST7735R_CASET); // start SEND_DATA(0x00); SEND_DATA(x); @@ -302,7 +284,7 @@ void St7735r::SetActiveRect(const uint8_t x, const uint8_t y, const uint8_t w, SEND_DATA(0x00); SEND_DATA(x + w - 1); - SEND_COMMAND(CMD_ROW_ADDRESS_SET); + SEND_COMMAND(ST7735R_RASET); SEND_DATA(0x00); SEND_DATA(y); SEND_DATA(0x00); @@ -316,12 +298,11 @@ void St7735r::Send(const bool is_cmd, const uint8_t data) } #else -St7735r::St7735r() +St7735r::St7735r(const Config&) : m_spi(nullptr), m_rst(nullptr), m_dc(nullptr), m_bg_color(0) { LOG_DL("Configured not to use St7735r(LCD)"); } -St7735r::St7735r(const bool) : St7735r() {} void St7735r::Clear() {} void St7735r::DrawPixel(const uint8_t, const uint8_t, const uint8_t, const uint8_t, const uint16_t) {} diff --git a/src/libsc/k60/switch.cpp b/src/libsc/k60/switch.cpp index a41d4fb..cd5f75a 100755 --- a/src/libsc/k60/switch.cpp +++ b/src/libsc/k60/switch.cpp @@ -1,9 +1,9 @@ /* * switch.cpp - * Switch * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -86,18 +86,19 @@ Gpi::Config GetGpiConfig(const uint8_t id) } -Switch::Switch(const uint8_t id) - : m_pin(GetGpiConfig(id)) +Switch::Switch(const Config &config) + : m_pin(GetGpiConfig(config.id)), + m_is_active_low(config.is_active_low) {} bool Switch::IsOn() const { - return m_pin.Get(); + return (m_pin.Get() ^ m_is_active_low); } #else -Switch::Switch(const uint8_t) - : m_pin(nullptr) +Switch::Switch(const Config&) + : m_pin(nullptr), m_is_active_low(false) { LOG_DL("Configured not to use Switch"); } diff --git a/src/libsc/k60/sys_tick_delay.cpp b/src/libsc/k60/sys_tick_delay.cpp index ff36afe..307b635 100755 --- a/src/libsc/k60/sys_tick_delay.cpp +++ b/src/libsc/k60/sys_tick_delay.cpp @@ -2,7 +2,8 @@ * sys_tick_delay.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libsc/k60/sys_tick_timer.cpp b/src/libsc/k60/sys_tick_timer.cpp index fd2ce8f..b932a33 100755 --- a/src/libsc/k60/sys_tick_timer.cpp +++ b/src/libsc/k60/sys_tick_timer.cpp @@ -2,7 +2,8 @@ * sys_tick_timer.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libsc/k60/system.cpp b/src/libsc/k60/system.cpp index a0069da..92b6691 100755 --- a/src/libsc/k60/system.cpp +++ b/src/libsc/k60/system.cpp @@ -2,7 +2,8 @@ * system.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libbase/k60/watchdog.h" @@ -21,6 +22,7 @@ System *System::m_instance = nullptr; System::System() : //m_watchdog(GetWatchdogConfig()), + m_delay(), m_timer() {} diff --git a/src/libsc/k60/tower_pro_mg995.cpp b/src/libsc/k60/tower_pro_mg995.cpp index b84d050..a17bafd 100755 --- a/src/libsc/k60/tower_pro_mg995.cpp +++ b/src/libsc/k60/tower_pro_mg995.cpp @@ -3,7 +3,7 @@ * TowerPro MG995 RC servo * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ @@ -20,8 +20,23 @@ namespace libsc namespace k60 { -TowerProMg995::TowerProMg995(const uint8_t id) - : Servo(id, PERIOD, POS_WIDTH_MIN, POS_WIDTH_MAX) +namespace +{ + +Servo::Config GetServoConfig(const uint8_t id) +{ + Servo::Config config; + config.id = id; + config.period = PERIOD; + config.min_pos_width = POS_WIDTH_MIN; + config.max_pos_width = POS_WIDTH_MAX; + return config; +} + +} + +TowerProMg995::TowerProMg995(const Config &config) + : Servo(GetServoConfig(config.id)) {} } diff --git a/src/libsc/k60/trs_d05.cpp b/src/libsc/k60/trs_d05.cpp index 3baf106..c6a216a 100755 --- a/src/libsc/k60/trs_d05.cpp +++ b/src/libsc/k60/trs_d05.cpp @@ -3,7 +3,8 @@ * TRS-D05 RC servo * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include @@ -20,8 +21,23 @@ namespace libsc namespace k60 { -TrsD05::TrsD05(const uint8_t id) - : Servo(id, PERIOD, POS_WIDTH_MIN, POS_WIDTH_MAX) +namespace +{ + +Servo::Config GetServoConfig(const uint8_t id) +{ + Servo::Config config; + config.id = id; + config.period = PERIOD; + config.min_pos_width = POS_WIDTH_MIN; + config.max_pos_width = POS_WIDTH_MAX; + return config; +} + +} + +TrsD05::TrsD05(const Config &config) + : Servo(GetServoConfig(config.id)) {} } diff --git a/src/libsc/k60/uart_device.cpp b/src/libsc/k60/uart_device.cpp index ff1e6b7..4dd562e 100644 --- a/src/libsc/k60/uart_device.cpp +++ b/src/libsc/k60/uart_device.cpp @@ -2,7 +2,7 @@ * uart_device.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ @@ -108,43 +108,33 @@ inline Pin::Name GetRxPin(const uint8_t id) #endif -Uart::Config GetUartConfig(const uint8_t id, - const Uart::Config::BaudRate baud_rate, +Uart::Config GetUartConfig(const UartDevice::Config &config, const Uart::OnTxEmptyListener &tx_isr, const Uart::OnRxFullListener &rx_isr) { - Uart::Config config; - config.tx_pin = GetTxPin(id); - config.rx_pin = GetRxPin(id); - config.baud_rate = baud_rate; - config.config.set(Uart::Config::ConfigBit::kFifo); - config.tx_isr = tx_isr; - config.rx_isr = rx_isr; - return config; + Uart::Config product; + product.tx_pin = GetTxPin(config.id); + product.tx_config = config.tx_config; + product.rx_pin = GetRxPin(config.id); + product.rx_config = config.rx_config; + product.baud_rate = config.baud_rate; + product.config.set(Uart::Config::ConfigBit::kFifo); + product.tx_isr = tx_isr; + product.rx_isr = rx_isr; + product.rx_irq_threshold = config.rx_irq_threshold; + product.is_rx_irq_threshold_percentage = + config.is_rx_irq_threshold_percentage; + return product; } } -UartDevice::UartConfigBuilder::UartConfigBuilder(const uint8_t id, - const Uart::Config::BaudRate baud_rate, UartDevice *uart) - : m_config(GetUartConfig(id, baud_rate, - std::bind(&UartDevice::OnTxEmpty, uart, placeholders::_1), - std::bind(&UartDevice::OnRxFull, uart, placeholders::_1))) -{} - -Uart::Config UartDevice::UartConfigBuilder::Build() const -{ - return m_config; -} - -UartDevice::UartDevice(const uint8_t id, const Uart::Config::BaudRate baud_rate) - : UartDevice(UartConfigBuilder(id, baud_rate, this)) -{} - -UartDevice::UartDevice(const UartConfigBuilder &builder) +UartDevice::UartDevice(const Config &config) : m_rx_buf{new RxBuffer}, m_listener(nullptr), m_tx_buf(14), m_is_tx_idle(true), - m_uart(builder.Build()) + m_uart(GetUartConfig(config, + std::bind(&UartDevice::OnTxEmpty, this, placeholders::_1), + std::bind(&UartDevice::OnRxFull, this, placeholders::_1))) { m_uart.SetEnableRxIrq(true); m_uart.SetEnableTxIrq(true); @@ -341,7 +331,7 @@ void UartDevice::OnRxFull(Uart *uart) struct UartDevice::RxBuffer {}; -UartDevice::UartDevice(const uint8_t, const Uart::Config::BaudRate) +UartDevice::UartDevice(const Config&) : m_tx_buf(0), m_is_tx_idle(true), m_uart(nullptr) { LOG_DL("Configured not to use UartDevice"); diff --git a/src/libsc/lib_guard.cpp b/src/libsc/lib_guard.cpp index fa7ea4a..4400fa4 100755 --- a/src/libsc/lib_guard.cpp +++ b/src/libsc/lib_guard.cpp @@ -2,7 +2,8 @@ * libsc_guard.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libsc/lib_guard.h" diff --git a/src/libutil/dynamic_block_buffer.cpp b/src/libutil/dynamic_block_buffer.cpp index 99dad57..904a9c3 100755 --- a/src/libutil/dynamic_block_buffer.cpp +++ b/src/libutil/dynamic_block_buffer.cpp @@ -2,7 +2,8 @@ * dynamic_block_buffer.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libutil/kalman_filter.cpp b/src/libutil/kalman_filter.cpp index e0ade32..2e9c46e 100755 --- a/src/libutil/kalman_filter.cpp +++ b/src/libutil/kalman_filter.cpp @@ -2,7 +2,8 @@ * kalman_filter.h * * Author: Tommy Wong - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include "libutil/kalman_filter.h" diff --git a/src/libutil/looper.cpp b/src/libutil/looper.cpp new file mode 100755 index 0000000..d5c8224 --- /dev/null +++ b/src/libutil/looper.cpp @@ -0,0 +1,83 @@ +/* + * looper.cpp + * + * Author: Ming Tsang + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details + */ + +#include +#include + +#include "libsc/k60/system.h" +#include "libsc/k60/timer.h" +#include "libutil/looper.h" + +using namespace libsc::k60; +using namespace std; + +namespace libutil +{ + +struct Looper::RunnerState +{ + Timer::TimerInt request; + Timer::TimerInt start; + Callback callback; +}; + +Looper::Looper() + : m_is_run(true) +{} + +Looper::~Looper() +{} + +void Looper::Loop() +{ + Timer::TimerInt prev = System::Time(); + while (m_is_run) + { + const Timer::TimerInt now = System::Time(); + if (now != prev) + { + Invoke(); + prev = now; + } + } +} + +void Looper::Invoke() +{ + list::iterator it = m_states.begin(); + while (it != m_states.end()) + { + const Timer::TimerInt duration = Timer::TimeDiff(System::Time(), + it->start); + if (duration >= it->request) + { + it->callback(it->request, duration); + it = m_states.erase(it); + } + else + { + ++it; + } + } +} + +void Looper::Break() +{ + m_is_run = false; +} + +void Looper::RunAfter(const Timer::TimerInt ms, const Callback &c) +{ + RunnerState rs; + rs.request = ms; + rs.start = System::Time(); + rs.callback = c; + m_states.push_back(std::move(rs)); +} + +} diff --git a/src/libutil/misc.cpp b/src/libutil/misc.cpp index e68b17b..3b0cf40 100644 --- a/src/libutil/misc.cpp +++ b/src/libutil/misc.cpp @@ -3,7 +3,8 @@ * Misc util * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include diff --git a/src/libutil/remote_var_manager.cpp b/src/libutil/remote_var_manager.cpp index faf4017..6ab966a 100755 --- a/src/libutil/remote_var_manager.cpp +++ b/src/libutil/remote_var_manager.cpp @@ -2,7 +2,7 @@ * remote_var_manager.cpp * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team * Refer to LICENSE for details */ diff --git a/src/libutil/string.cpp b/src/libutil/string.cpp index 69f77ac..7d70cd0 100755 --- a/src/libutil/string.cpp +++ b/src/libutil/string.cpp @@ -3,7 +3,8 @@ * String util * * Author: Ming Tsang - * Copyright (c) 2014 HKUST SmartCar Team + * Copyright (c) 2014-2015 HKUST SmartCar Team + * Refer to LICENSE for details */ #include