C言語でBBC micro:bit V2およびDFRobot micro:Maqueenを操作する
Revisión | 9b44ba3268d7f08b8c1a63b21aebde5d24842a1e (tree) |
---|---|
Tiempo | 2023-09-16 07:18:58 |
Autor | xm6_original <tanaka_yasushi2008@yaho...> |
Commiter | xm6_original |
超音波センサによる距離測定を追加
@@ -64,7 +64,6 @@ | ||
64 | 64 | <listOptionValue builtIn="false" value=""../include""/> |
65 | 65 | <listOptionValue builtIn="false" value=""../system/include""/> |
66 | 66 | <listOptionValue builtIn="false" value=""../system/include/cmsis""/> |
67 | - <listOptionValue builtIn="false" value=""../system/include/DEVICE""/> | |
68 | 67 | </option> |
69 | 68 | <option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnuarmeclipse.managedbuild.cross.option.assembler.defs.1274092509" name="定義済みシンボル (-D)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.assembler.defs" valueType="definedSymbols"> |
70 | 69 | <listOptionValue builtIn="false" value="DEBUG"/> |
@@ -95,7 +94,6 @@ | ||
95 | 94 | <listOptionValue builtIn="false" value=""../include""/> |
96 | 95 | <listOptionValue builtIn="false" value=""../system/include""/> |
97 | 96 | <listOptionValue builtIn="false" value=""../system/include/cmsis""/> |
98 | - <listOptionValue builtIn="false" value=""../system/include/DEVICE""/> | |
99 | 97 | </option> |
100 | 98 | <option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.defs.1680054453" name="定義済みシンボル (-D)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.defs" useByScannerDiscovery="true" valueType="definedSymbols"> |
101 | 99 | <listOptionValue builtIn="false" value="DEBUG"/> |
@@ -103,7 +101,7 @@ | ||
103 | 101 | <listOptionValue builtIn="false" value="OS_USE_TRACE_SEMIHOSTING_STDOUT"/> |
104 | 102 | <listOptionValue builtIn="false" value="NRF52833_XXAA"/> |
105 | 103 | </option> |
106 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.warnabi.1516595037" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.warnabi" value="false" valueType="boolean"/> | |
104 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.warnabi.1516595037" name="Warn on ABI violations (-Wabi)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.warnabi" useByScannerDiscovery="true" value="false" valueType="boolean"/> | |
107 | 105 | <inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.input.161133683" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.input"/> |
108 | 106 | </tool> |
109 | 107 | <tool id="ilg.gnuarmeclipse.managedbuild.cross.tool.c.linker.1340459132" name="GNU Arm Cross C Linker" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.c.linker"> |
@@ -222,16 +220,16 @@ | ||
222 | 220 | <option id="ilg.gnuarmeclipse.managedbuild.cross.option.command.size.698871301" name="Size command" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.command.size" value="size" valueType="string"/> |
223 | 221 | <option id="ilg.gnuarmeclipse.managedbuild.cross.option.command.make.543432284" name="ビルド・コマンド" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.command.make" value="make" valueType="string"/> |
224 | 222 | <option id="ilg.gnuarmeclipse.managedbuild.cross.option.command.rm.1313472073" name="Remove command" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.command.rm" value="rm" valueType="string"/> |
225 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.abi.276165612" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.abi" value="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.abi.hard" valueType="enumerated"/> | |
226 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.unit.43487436" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.unit" value="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.unit.fpv4spd16" valueType="enumerated"/> | |
227 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.uninitialized.1838683092" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.uninitialized" value="true" valueType="boolean"/> | |
228 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.missingdeclaration.1471617354" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.missingdeclaration" value="true" valueType="boolean"/> | |
229 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.conversion.1000939115" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.conversion" value="true" valueType="boolean"/> | |
230 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.pointerarith.1755960847" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.pointerarith" value="true" valueType="boolean"/> | |
231 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.shadow.1404970943" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.shadow" value="true" valueType="boolean"/> | |
232 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.logicalop.1416267589" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.logicalop" value="true" valueType="boolean"/> | |
233 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.floatequal.2015684798" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.floatequal" value="true" valueType="boolean"/> | |
234 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.padded.1699569031" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.padded" value="false" valueType="boolean"/> | |
223 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.abi.276165612" name="Float ABI" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.abi" value="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.abi.hard" valueType="enumerated"/> | |
224 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.unit.43487436" name="FPU Type" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.unit" value="ilg.gnuarmeclipse.managedbuild.cross.option.arm.target.fpu.unit.fpv4spd16" valueType="enumerated"/> | |
225 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.uninitialized.1838683092" name="Warn on uninitialized variables (-Wuninitialised)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.uninitialized" value="true" valueType="boolean"/> | |
226 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.missingdeclaration.1471617354" name="Warn on undeclared global function (-Wmissing-declaration)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.missingdeclaration" value="true" valueType="boolean"/> | |
227 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.conversion.1000939115" name="Warn on implicit conversions (-Wconversion)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.conversion" value="true" valueType="boolean"/> | |
228 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.pointerarith.1755960847" name="Warn if pointer arithmetic (-Wpointer-arith)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.pointerarith" value="true" valueType="boolean"/> | |
229 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.shadow.1404970943" name="Warn if shadowed variable (-Wshadow)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.shadow" value="true" valueType="boolean"/> | |
230 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.logicalop.1416267589" name="Warn if suspicious logical ops (-Wlogical-op)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.logicalop" value="true" valueType="boolean"/> | |
231 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.floatequal.2015684798" name="Warn if floats are compared as equal (-Wfloat-equal)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.floatequal" value="true" valueType="boolean"/> | |
232 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.padded.1699569031" name="Warn if padding is included (-Wpadded)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.warnings.padded" value="false" valueType="boolean"/> | |
235 | 233 | <targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="ilg.gnuarmeclipse.managedbuild.cross.targetPlatform.773906421" isAbstract="false" osList="all" superClass="ilg.gnuarmeclipse.managedbuild.cross.targetPlatform"/> |
236 | 234 | <builder buildPath="${workspace_loc:/nRFHello}/Release" id="ilg.gnuarmeclipse.managedbuild.cross.builder.36652053" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make ビルダー" superClass="ilg.gnuarmeclipse.managedbuild.cross.builder"/> |
237 | 235 | <tool id="ilg.gnuarmeclipse.managedbuild.cross.tool.assembler.342206795" name="GNU Arm Cross Assembler" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.assembler"> |
@@ -240,7 +238,6 @@ | ||
240 | 238 | <listOptionValue builtIn="false" value=""../include""/> |
241 | 239 | <listOptionValue builtIn="false" value=""../system/include""/> |
242 | 240 | <listOptionValue builtIn="false" value=""../system/include/cmsis""/> |
243 | - <listOptionValue builtIn="false" value=""../system/include/DEVICE""/> | |
244 | 241 | </option> |
245 | 242 | <option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnuarmeclipse.managedbuild.cross.option.assembler.defs.2040931945" name="定義済みシンボル (-D)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.assembler.defs" valueType="definedSymbols"> |
246 | 243 | <listOptionValue builtIn="false" value="NDEBUG"/> |
@@ -258,10 +255,10 @@ | ||
258 | 255 | <listOptionValue builtIn="false" value="NDEBUG"/> |
259 | 256 | <listOptionValue builtIn="false" value="NRF52833_XXAA"/> |
260 | 257 | </option> |
261 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.otheroptimizations.1155659942" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.otheroptimizations" value="-fsingle-precision-constant" valueType="string"/> | |
262 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.missingprototypes.603782387" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.missingprototypes" value="true" valueType="boolean"/> | |
263 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.strictprototypes.1934558844" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.strictprototypes" value="true" valueType="boolean"/> | |
264 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.badfunctioncast.1880518477" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.badfunctioncast" value="true" valueType="boolean"/> | |
258 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.otheroptimizations.1155659942" name="その他の最適化フラグ" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.otheroptimizations" useByScannerDiscovery="true" value="-fsingle-precision-constant" valueType="string"/> | |
259 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.missingprototypes.603782387" name="Warn if a global function has no prototype (-Wmissing-prototypes)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.missingprototypes" useByScannerDiscovery="true" value="true" valueType="boolean"/> | |
260 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.strictprototypes.1934558844" name="Warn if a function has no arg type (-Wstrict-prototypes)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.strictprototypes" useByScannerDiscovery="true" value="true" valueType="boolean"/> | |
261 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.badfunctioncast.1880518477" name="Warn if wrong cast (-Wbad-function-cast)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.warning.badfunctioncast" useByScannerDiscovery="true" value="true" valueType="boolean"/> | |
265 | 262 | <inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler.input.1426178694" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler.input"/> |
266 | 263 | </tool> |
267 | 264 | <tool id="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.3867943" name="GNU Arm Cross C++ Compiler" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler"> |
@@ -269,13 +266,12 @@ | ||
269 | 266 | <listOptionValue builtIn="false" value=""../include""/> |
270 | 267 | <listOptionValue builtIn="false" value=""../system/include""/> |
271 | 268 | <listOptionValue builtIn="false" value=""../system/include/cmsis""/> |
272 | - <listOptionValue builtIn="false" value=""../system/include/DEVICE""/> | |
273 | 269 | </option> |
274 | 270 | <option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.defs.1367930683" name="定義済みシンボル (-D)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.defs" useByScannerDiscovery="true" valueType="definedSymbols"> |
275 | 271 | <listOptionValue builtIn="false" value="NDEBUG"/> |
276 | 272 | <listOptionValue builtIn="false" value="NRF52833_XXAA"/> |
277 | 273 | </option> |
278 | - <option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.std.1900154255" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.std" value="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.std.gnucpp14" valueType="enumerated"/> | |
274 | + <option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.std.1900154255" name="言語標準" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.std" useByScannerDiscovery="true" value="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.std.gnucpp14" valueType="enumerated"/> | |
279 | 275 | <inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.input.706143826" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.input"/> |
280 | 276 | </tool> |
281 | 277 | <tool id="ilg.gnuarmeclipse.managedbuild.cross.tool.c.linker.609428063" name="GNU Arm Cross C Linker" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.c.linker"> |
@@ -306,7 +302,7 @@ | ||
306 | 302 | </option> |
307 | 303 | <option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.nostart.955404113" name="標準開始ファイルを使用しない (-nostartfiles)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.nostart" value="true" valueType="boolean"/> |
308 | 304 | <option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.usenewlibnano.628835414" name="Use newlib-nano (--specs=nano.specs)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.usenewlibnano" value="true" valueType="boolean"/> |
309 | - <option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.flags.240737852" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.flags" valueType="stringList"> | |
305 | + <option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.flags.240737852" name="Linker flags (-Xlinker [option])" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.flags" valueType="stringList"> | |
310 | 306 | <listOptionValue builtIn="false" value="--no-warn-rwx-segments"/> |
311 | 307 | </option> |
312 | 308 | <inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.linker.input.1847511563" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.linker.input"> |
@@ -53,7 +53,9 @@ typedef enum PF_GPIO_ID_Tag | ||
53 | 53 | PF_GPIO_ID_MAQUEEN_LED_L, //!< MAQUEEN LED L |
54 | 54 | PF_GPIO_ID_MAQUEEN_LED_R, //!< MAQUEEN LED R |
55 | 55 | PF_GPIO_ID_MAQUEEN_PATROL_L, //!< MAQUEEN PATROL L |
56 | - PF_GPIO_ID_MAQUEEN_PATROL_R, //!< MAUQEEN PATROL R | |
56 | + PF_GPIO_ID_MAQUEEN_PATROL_R, //!< MAQUEEN PATROL R | |
57 | + PF_GPIO_ID_MAQUEEN_TRIG, //!< MAQUEEN 超音波 TRIG | |
58 | + PF_GPIO_ID_MAQUEEN_ECHO, //!< MAQUEEN 超音波 ECHO | |
57 | 59 | PF_GPIO_ID_MAX, //!< (IDの個数を表す) |
58 | 60 | } PF_GPIO_ID; |
59 | 61 |
@@ -73,7 +75,7 @@ u4 pf_gpio_get_pin(PF_GPIO_ID id); | ||
73 | 75 | |
74 | 76 | //! @brief GPIOから入力 |
75 | 77 | //! @param [in] id GPIOピンのID |
76 | -//! @result 入力レベル(TRUE='H'レベル/FALSE='L'レベル) | |
78 | +//! @return 入力レベル(TRUE='H'レベル/FALSE='L'レベル) | |
77 | 79 | BOOL pf_gpio_input(PF_GPIO_ID id); |
78 | 80 | |
79 | 81 | //! @brief GPIOへ出力 |
@@ -37,7 +37,7 @@ typedef enum PF_I2C_ID_Tag | ||
37 | 37 | PF_I2C_ID_ACCELEROMETER, //!< 加速度センサ |
38 | 38 | PF_I2C_ID_MAGNETOMETER, //!< 磁気センサ |
39 | 39 | PF_I2C_ID_MAQUEEN_MOTOR, //!< MAQUEEN モータ |
40 | - PF_I2C_ID_MAX //!< (IDの個数を表す) | |
40 | + PF_I2C_ID_MAX, //!< (IDの個数を表す) | |
41 | 41 | } PF_I2C_ID; |
42 | 42 | |
43 | 43 | //! @brief I2Cエラー情報構造体 |
@@ -32,10 +32,11 @@ typedef enum PF_INTERRUPT_PRI_Tag | ||
32 | 32 | { |
33 | 33 | PF_INTERRUPT_PRI_SYSTICK = 0, //!< SysTick |
34 | 34 | PF_INTERRUPT_PRI_UART, //!< UART |
35 | - PF_INTERRUPT_PRI_DISPLAY_TIMER, //!< Display Timer | |
35 | + PF_INTERRUPT_PRI_DISPLAY, //!< Display | |
36 | 36 | PF_INTERRUPT_PRI_I2C_INT, //!< I2C(内部) |
37 | 37 | PF_INTERRUPT_PRI_I2C_EXT, //!< I2C(外部) |
38 | 38 | PF_INTERRUPT_PRI_MUSIC, //!< 音楽演奏 |
39 | + PF_INTERRUPT_PRI_ULTRA, //!< 超音波 | |
39 | 40 | } PF_INTERRUPT_PRI; |
40 | 41 | |
41 | 42 | //! @brief グローバル割り込み禁止 |
@@ -28,7 +28,7 @@ | ||
28 | 28 | #include "pf_types.h" |
29 | 29 | |
30 | 30 | //! @brief 制御周期[ms] |
31 | -#define PF_SYSTICK_SYNC_MS ((u4)5U) | |
31 | +#define PF_SYSTICK_SYNC_MS (5) | |
32 | 32 | |
33 | 33 | //! @brief SysTick時刻情報構造体 |
34 | 34 | typedef struct PF_SYSTICK_TIME_Tag |
@@ -37,9 +37,10 @@ typedef void (*PF_TIMER_CALLBACK)(u4 eventbit); | ||
37 | 37 | //! @brief TimerのID |
38 | 38 | typedef enum PF_TIMER_ID_Tag |
39 | 39 | { |
40 | - PF_TIMER_ID_DISPLAY = 0, //!< ディスプレイタイマ | |
40 | + PF_TIMER_ID_DISPLAY = 0, //!< Display | |
41 | 41 | PF_TIMER_ID_MUSIC, //!< 音楽 |
42 | - PF_TIMER_ID_MAX //!< (IDの個数を表す) | |
42 | + PF_TIMER_ID_ULTRA, //!< 超音波 | |
43 | + PF_TIMER_ID_MAX, //!< (IDの個数を表す) | |
43 | 44 | } PF_TIMER_ID; |
44 | 45 | |
45 | 46 | //! @brief Timer初期化 |
@@ -59,6 +60,21 @@ void pf_timer_stop(PF_TIMER_ID id); | ||
59 | 60 | //! @param [in] cc コンペアカウンタ配列 |
60 | 61 | void pf_timer_cc(PF_TIMER_ID id, u4 *cc); |
61 | 62 | |
63 | +//! @brief Timerキャプチャカウンタを取得 | |
64 | +//! @param [in] id TimerのID | |
65 | +//! @return キャプチャカウンタ | |
66 | +u4 pf_timer_get_capture(PF_TIMER_ID id); | |
67 | + | |
68 | +//! @brief Timerスタートアドレス取得 | |
69 | +//! @param [in] id TimerのID | |
70 | +//! @return TASKS_STARTレジスタのアドレス | |
71 | +u4 pf_timer_get_tasks_start(PF_TIMER_ID id); | |
72 | + | |
73 | +//! @brief Timerキャプチャイベントアドレス取得 | |
74 | +//! @param [in] id TimerのID | |
75 | +//! @return TASKS_CAPTURE[0]レジスタのアドレス | |
76 | +u4 pf_timer_get_tasks_capture(PF_TIMER_ID id); | |
77 | + | |
62 | 78 | //! @brief Timerコールバック関数設定 |
63 | 79 | //! @param [in] id TimerのID |
64 | 80 | //! @param [in] func Timerコールバック関数へのポインタ |
@@ -0,0 +1,46 @@ | ||
1 | +//! @file pf_ultra.h | |
2 | +//! @brief プラットフォーム(超音波)ヘッダファイル | |
3 | + | |
4 | +// The MIT License (MIT) | |
5 | +// Copyright (c) 2023 @xm6_original | |
6 | +// | |
7 | +// Permission is hereby granted, free of charge, to any person obtaining a | |
8 | +// copy of this software and associated documentation files (the "Software"), | |
9 | +// to deal in the Software without restriction, including without limitation | |
10 | +// the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | +// and/or sell copies of the Software, and to permit persons to whom the | |
12 | +// Software is furnished to do so, subject to the following conditions: | |
13 | +// | |
14 | +// The above copyright notice and this permission notice shall be included in | |
15 | +// all copies or substantial portions of the Software. | |
16 | +// | |
17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
23 | +// DEALINGS IN THE SOFTWARE. | |
24 | + | |
25 | +#ifndef PF_ULTRA_H | |
26 | +#define PF_ULTRA_H | |
27 | + | |
28 | +#include "pf_types.h" | |
29 | + | |
30 | +//! @brief 超音波初期化 | |
31 | +//! @remarks プラットフォーム初期化処理から呼び出すこと | |
32 | +void pf_ultra_init(void); | |
33 | + | |
34 | +//! @brief 超音波定期タスク | |
35 | +//! @remarks プラットフォーム定期タスク(入力系)処理から呼び出すこと | |
36 | +void pf_ultra_task(void); | |
37 | + | |
38 | +//! @brief 距離取得 | |
39 | +//! @return 前方の距離(0=測定エラー) | |
40 | +u4 pf_ultra_get(void); | |
41 | + | |
42 | +//! @brief GPIOTE割り込みハンドラ | |
43 | +//! @attention データ競合(割り込み干渉)に注意する | |
44 | +void GPIOTE_IRQHandler(void); | |
45 | + | |
46 | +#endif // PF_ULTRA_H |
@@ -35,6 +35,7 @@ | ||
35 | 35 | #include "pf_led.h" |
36 | 36 | #include "pf_patrol.h" |
37 | 37 | #include "pf_motor.h" |
38 | +#include "pf_ultra.h" | |
38 | 39 | #include "pf_music.h" |
39 | 40 | |
40 | 41 | //! @brief プラットフォーム初期化 |
@@ -60,6 +61,7 @@ static void pf_init(void) | ||
60 | 61 | pf_led_init(); |
61 | 62 | pf_patrol_init(); |
62 | 63 | pf_motor_init(); |
64 | + pf_ultra_init(); | |
63 | 65 | pf_music_init(); |
64 | 66 | } |
65 | 67 |
@@ -70,6 +72,7 @@ static void pf_input_task(void) | ||
70 | 72 | pf_uart_task(); |
71 | 73 | pf_button_task(); |
72 | 74 | pf_patrol_task(); |
75 | + pf_ultra_task(); | |
73 | 76 | } |
74 | 77 | |
75 | 78 | //! @brief 定期タスク(出力系) |
@@ -221,7 +224,7 @@ static const APP_SQUARE_UNIT app_square_table[9] = | ||
221 | 224 | // 左回転 |
222 | 225 | { |
223 | 226 | PF_MOTOR_DIR_LEFT, |
224 | - 72, | |
227 | + 74, | |
225 | 228 | }, |
226 | 229 | |
227 | 230 | // 前進 |
@@ -366,8 +369,13 @@ static void app_led_task(void) | ||
366 | 369 | left = pf_patrol_line(PF_PATROL_ID_L); |
367 | 370 | right = pf_patrol_line(PF_PATROL_ID_R); |
368 | 371 | |
372 | +#if PF_SYSTICK_SYNC_MS == 5 | |
369 | 373 | // stepが32の倍数(160ms)で点滅 |
370 | 374 | if (app_step & 0x0020) |
375 | +#else | |
376 | + // stepが16の倍数(160ms)で点滅 | |
377 | + if (app_step & 0x0010) | |
378 | +#endif | |
371 | 379 | { |
372 | 380 | // 常に消灯 |
373 | 381 | pf_led_ctrl(PF_LED_ID_L, FALSE); |
@@ -491,6 +499,36 @@ static void app_run_line(void) | ||
491 | 499 | app_step++; |
492 | 500 | } |
493 | 501 | |
502 | +//! @brief 走行継続判定 | |
503 | +//! @return 走行継続フラグ(TRUE=走行継続/FALSE=走行中断) | |
504 | +static BOOL app_run_ultra(void) | |
505 | +{ | |
506 | + u4 distance; | |
507 | + BOOL run; | |
508 | + | |
509 | + // オート変数初期化 | |
510 | + distance = 0; | |
511 | + run = TRUE; | |
512 | + | |
513 | + // 前方までの距離を取得 | |
514 | + distance = pf_ultra_get(); | |
515 | + | |
516 | + // 0は取得エラー | |
517 | + if (distance > 0) | |
518 | + { | |
519 | + // 距離しきい値チェック | |
520 | + if (distance < 10) | |
521 | + { | |
522 | + // 走行停止 | |
523 | + run = FALSE; | |
524 | + app_run = FALSE; | |
525 | + app_step = 0; | |
526 | + } | |
527 | + } | |
528 | + | |
529 | + return run; | |
530 | +} | |
531 | + | |
494 | 532 | //! @brief アプリケーションタスク |
495 | 533 | static void app_task(void) |
496 | 534 | { |
@@ -515,14 +553,14 @@ static void app_task(void) | ||
515 | 553 | if ((APP_MODE_ID_SQUARE == app_mode) && (TRUE == app_run)) |
516 | 554 | { |
517 | 555 | app_run_square(); |
518 | - run = TRUE; | |
556 | + run = app_run_ultra(); | |
519 | 557 | } |
520 | 558 | |
521 | 559 | // 走行(ライントレース) |
522 | 560 | if ((APP_MODE_ID_LINE == app_mode) && (TRUE == app_run)) |
523 | 561 | { |
524 | 562 | app_run_line(); |
525 | - run = TRUE; | |
563 | + run = app_run_ultra(); | |
526 | 564 | } |
527 | 565 | |
528 | 566 | // 走行状態以外なら停止 |
@@ -24,10 +24,15 @@ | ||
24 | 24 | |
25 | 25 | #include "pf_types.h" |
26 | 26 | #include "pf_gpio.h" |
27 | +#include "pf_systick.h" | |
27 | 28 | #include "pf_button.h" |
28 | 29 | |
29 | 30 | //! @brief チャタリング防止のためのフィルタ段数(最低2段以上にすること) |
31 | +#if PF_SYSTICK_SYNC_MS == 5 | |
30 | 32 | #define PF_BUTTON_FILTER_MAX ((u4)5U) |
33 | +#else | |
34 | +#define PF_BUTTON_FILTER_MAX ((u4)3U) | |
35 | +#endif | |
31 | 36 | |
32 | 37 | //! @brief ボタン→GPIO IDテーブル |
33 | 38 | static const PF_GPIO_ID pf_button_to_gpio[PF_BUTTON_ID_MAX] = |
@@ -1299,7 +1299,7 @@ void pf_display_image(const u1 *image) | ||
1299 | 1299 | if (FALSE == pf_display_info.force) |
1300 | 1300 | { |
1301 | 1301 | // 割り込み禁止 |
1302 | - enable = pf_interrupt_local_disable(PF_INTERRUPT_PRI_DISPLAY_TIMER); | |
1302 | + enable = pf_interrupt_local_disable(PF_INTERRUPT_PRI_DISPLAY); | |
1303 | 1303 | |
1304 | 1304 | // ワークを設定 |
1305 | 1305 | pf_display_info.on = TRUE; |
@@ -1324,7 +1324,7 @@ void pf_display_image(const u1 *image) | ||
1324 | 1324 | } |
1325 | 1325 | |
1326 | 1326 | // 割り込み復元 |
1327 | - pf_interrupt_local_restore(PF_INTERRUPT_PRI_DISPLAY_TIMER, enable); | |
1327 | + pf_interrupt_local_restore(PF_INTERRUPT_PRI_DISPLAY, enable); | |
1328 | 1328 | } |
1329 | 1329 | } |
1330 | 1330 | } |
@@ -1341,13 +1341,13 @@ void pf_display_off(void) | ||
1341 | 1341 | if (FALSE == pf_display_info.force) |
1342 | 1342 | { |
1343 | 1343 | // 割り込み禁止 |
1344 | - enable = pf_interrupt_local_disable(PF_INTERRUPT_PRI_DISPLAY_TIMER); | |
1344 | + enable = pf_interrupt_local_disable(PF_INTERRUPT_PRI_DISPLAY); | |
1345 | 1345 | |
1346 | 1346 | // ワークを設定 |
1347 | 1347 | pf_display_info.on = FALSE; |
1348 | 1348 | |
1349 | 1349 | // 割り込み復元 |
1350 | - pf_interrupt_local_restore(PF_INTERRUPT_PRI_DISPLAY_TIMER, enable); | |
1350 | + pf_interrupt_local_restore(PF_INTERRUPT_PRI_DISPLAY, enable); | |
1351 | 1351 | } |
1352 | 1352 | } |
1353 | 1353 |
@@ -340,6 +340,28 @@ const PF_GPIO_INIT pf_gpio_init_table[PF_GPIO_ID_MAX] = | ||
340 | 340 | PF_GPIO_PULL_UP, // プルアップ・プルダウン |
341 | 341 | PF_GPIO_DRIVE_S0S1, // 駆動特性 |
342 | 342 | }, |
343 | + | |
344 | + // PF_GPIO_ID_MAQUEEN_TRIG | |
345 | + { | |
346 | + 0, // ポート | |
347 | + 3, // ピン | |
348 | + FALSE, // 初期出力レベル | |
349 | + PF_GPIO_DIR_OUTPUT, // 入出力方向 | |
350 | + PF_GPIO_INBUF_DISCONNECT, // 入力バッファ | |
351 | + PF_GPIO_PULL_NONE, // プルアップ・プルダウン | |
352 | + PF_GPIO_DRIVE_S0S1, // 駆動特性 | |
353 | + }, | |
354 | + | |
355 | + // PF_GPIO_ID_MAQUEEN_ECHO | |
356 | + { | |
357 | + 0, // ポート | |
358 | + 4, // ピン | |
359 | + FALSE, // 初期出力レベル | |
360 | + PF_GPIO_DIR_INPUT, // 入出力方向 | |
361 | + PF_GPIO_INBUF_CONNECT, // 入力バッファ | |
362 | + PF_GPIO_PULL_NONE, // プルアップ・プルダウン | |
363 | + PF_GPIO_DRIVE_S0S1, // 駆動特性 | |
364 | + }, | |
343 | 365 | }; |
344 | 366 | |
345 | 367 | //! @brief GPIOデバイス取得 |
@@ -447,7 +469,7 @@ u4 pf_gpio_get_pin(PF_GPIO_ID id) | ||
447 | 469 | |
448 | 470 | //! @brief GPIOから入力 |
449 | 471 | //! @param [in] id GPIOピンのID |
450 | -//! @result 入力レベル(TRUE='H'レベル/FALSE='L'レベル) | |
472 | +//! @return 入力レベル(TRUE='H'レベル/FALSE='L'レベル) | |
451 | 473 | BOOL pf_gpio_input(PF_GPIO_ID id) |
452 | 474 | { |
453 | 475 | NRF_GPIO_Type *dev; |
@@ -30,7 +30,7 @@ | ||
30 | 30 | #include "pf_i2c.h" |
31 | 31 | |
32 | 32 | //! @brief I2Cチャネルの最大数 |
33 | -#define PF_I2C_CHANNEL_MAX (2) | |
33 | +#define PF_I2C_CHANNEL_MAX ((u4)2U) | |
34 | 34 | |
35 | 35 | //! @brief I2Cアドレス情報構造体 |
36 | 36 | typedef struct PF_I2C_ADDR_Tag |
@@ -492,7 +492,6 @@ BOOL pf_i2c_recv(PF_I2C_ID id, u1 reg, u1 *buf, u4 bytes) | ||
492 | 492 | void pf_i2c_error(PF_I2C_ID id, PF_I2C_ERROR *error) |
493 | 493 | { |
494 | 494 | u4 channel; |
495 | - channel = 0; | |
496 | 495 | u4 enable; |
497 | 496 | |
498 | 497 | // オート変数初期化 |
@@ -597,7 +596,7 @@ static void pf_i2c_isr_error(u4 channel) | ||
597 | 596 | // チャネルから通信先IDを逆引きする |
598 | 597 | id = pf_i2c_get_id(channel); |
599 | 598 | |
600 | - // 通信エラーあり | |
599 | + // 通信エラーあり(送信失敗 or 受信失敗) | |
601 | 600 | pf_i2c_info[id].status = FALSE; |
602 | 601 | |
603 | 602 | // エラー種別判定(ANACK) |
@@ -78,8 +78,8 @@ static IRQn_Type pf_interrupt_get_irq(PF_INTERRUPT_PRI pri) | ||
78 | 78 | irq = UARTE0_UART0_IRQn; |
79 | 79 | break; |
80 | 80 | |
81 | - // Display Timer | |
82 | - case PF_INTERRUPT_PRI_DISPLAY_TIMER: | |
81 | + // Display | |
82 | + case PF_INTERRUPT_PRI_DISPLAY: | |
83 | 83 | irq = TIMER0_IRQn; |
84 | 84 | break; |
85 | 85 |
@@ -98,6 +98,11 @@ static IRQn_Type pf_interrupt_get_irq(PF_INTERRUPT_PRI pri) | ||
98 | 98 | irq = TIMER1_IRQn; |
99 | 99 | break; |
100 | 100 | |
101 | + // 超音波 | |
102 | + case PF_INTERRUPT_PRI_ULTRA: | |
103 | + irq = GPIOTE_IRQn; | |
104 | + break; | |
105 | + | |
101 | 106 | default: |
102 | 107 | break; |
103 | 108 | } |
@@ -28,7 +28,7 @@ | ||
28 | 28 | #include "pf_motor.h" |
29 | 29 | |
30 | 30 | //! @brief I2C常時送信動作(TRUE=常時送信/FALSE=キャッシュ動作) |
31 | -#define PF_MOTOR_I2C_ANY (TRUE) | |
31 | +#define PF_MOTOR_I2C_ALWAYS (TRUE) | |
32 | 32 | |
33 | 33 | //! @brief モータ速度(デフォルト) |
34 | 34 | #define PF_MOTOR_DEFAULT_SPEED ((u1)100U) |
@@ -259,7 +259,7 @@ void pf_motor_init(void) | ||
259 | 259 | //! @brief I2C送信 |
260 | 260 | static void pf_motor_send(void) |
261 | 261 | { |
262 | -#if PF_MOTOR_I2C_ANY == TRUE | |
262 | +#if PF_MOTOR_I2C_ALWAYS == TRUE | |
263 | 263 | // 送信中チャネルをクリア |
264 | 264 | pf_motor_info.channel = 0; |
265 | 265 |
@@ -309,7 +309,7 @@ static void pf_motor_send(void) | ||
309 | 309 | (void)pf_i2c_send(PF_I2C_ID_MAQUEEN_MOTOR, &pf_motor_info.buf[pf_motor_info.channel][0], |
310 | 310 | PF_MOTOR_I2C_MAX); |
311 | 311 | } |
312 | -#endif // PF_MOTOR_I2C_ANY | |
312 | +#endif // PF_MOTOR_I2C_ALWAYS | |
313 | 313 | } |
314 | 314 | |
315 | 315 | //! @brief I2Cバッファ構成(駆動方向) |
@@ -790,7 +790,6 @@ static void pf_music_clock(u4 device) | ||
790 | 790 | } |
791 | 791 | |
792 | 792 | //! @brief Timerコールバック |
793 | -//! @param [in] eventbit イベントビットパターン(bit0:クロックタイミング) | |
794 | 793 | //! @attention Timer割り込みコンテキストで実行される |
795 | 794 | static void pf_music_callback(u4 /*eventbit*/) |
796 | 795 | { |
@@ -39,6 +39,7 @@ typedef enum PF_TIMER_ACTION_Tag | ||
39 | 39 | { |
40 | 40 | PF_TIMER_ACTION_ONESHOT, //!< ワンショット動作 |
41 | 41 | PF_TIMER_ACTION_INTERVAL, //!< インターバル動作 |
42 | + PF_TIMER_ACTION_CAPTURE, //!< キャプチャ動作 | |
42 | 43 | } PF_TIMER_ACTION; |
43 | 44 | |
44 | 45 | //! @brief Timer初期化情報構造体 |
@@ -62,7 +63,7 @@ const PF_TIMER_INIT pf_timer_table[PF_TIMER_ID_MAX] = | ||
62 | 63 | { |
63 | 64 | 0, // チャネル |
64 | 65 | NRF_TIMER0, // デバイス |
65 | - PF_INTERRUPT_PRI_DISPLAY_TIMER, // 割り込み優先度 | |
66 | + PF_INTERRUPT_PRI_DISPLAY, // 割り込み優先度 | |
66 | 67 | PF_TIMER_ACTION_INTERVAL, // Timer動作 |
67 | 68 | 2, // コンペア数 |
68 | 69 | MICROSEC_TO_TIMERCC(800), // コンペア値(0) |
@@ -83,6 +84,19 @@ const PF_TIMER_INIT pf_timer_table[PF_TIMER_ID_MAX] = | ||
83 | 84 | 0, // コンペア値(2) |
84 | 85 | 0, // コンペア値(3) |
85 | 86 | }, |
87 | + | |
88 | + // PF_TIMER_ID_ULTRASONIC | |
89 | + { | |
90 | + 2, // チャネル | |
91 | + NRF_TIMER2, // デバイス | |
92 | + 0, // 割り込み優先度 | |
93 | + PF_TIMER_ACTION_CAPTURE, // Timer動作 | |
94 | + 0, // コンペア数 | |
95 | + 0, // コンペア値(0) | |
96 | + 0, // コンペア値(1) | |
97 | + 0, // コンペア値(2) | |
98 | + 0, // コンペア値(3) | |
99 | + }, | |
86 | 100 | }; |
87 | 101 | |
88 | 102 | //! @brief compares→CLEARビットテーブル |
@@ -177,15 +191,24 @@ static void pf_timer_init_id(PF_TIMER_ID id) | ||
177 | 191 | << TIMER_TASKS_STOP_TASKS_STOP_Pos; |
178 | 192 | |
179 | 193 | // イベントのショートカット |
180 | - if (PF_TIMER_ACTION_INTERVAL == pf_timer_table[id].action) | |
181 | - { | |
182 | - // COMPARE[i]に達したらCLEAR | |
183 | - pf_timer_table[id].dev->SHORTS = pf_timer_clear_table[pf_timer_table[id].compares]; | |
184 | - } | |
185 | - else | |
194 | + switch (pf_timer_table[id].action) | |
186 | 195 | { |
196 | + // ワンショット動作 | |
197 | + case PF_TIMER_ACTION_ONESHOT: | |
187 | 198 | // COMPARE[i]に達したらSTOP |
188 | 199 | pf_timer_table[id].dev->SHORTS = pf_timer_stop_table[pf_timer_table[id].compares]; |
200 | + break; | |
201 | + | |
202 | + // インターバル動作 | |
203 | + case PF_TIMER_ACTION_INTERVAL: | |
204 | + // COMPARE[i]に達したらCLEAR | |
205 | + pf_timer_table[id].dev->SHORTS = pf_timer_clear_table[pf_timer_table[id].compares]; | |
206 | + break; | |
207 | + | |
208 | + // その他(キャプチャ動作) | |
209 | + default: | |
210 | + pf_timer_table[id].dev->SHORTS = 0; | |
211 | + break; | |
189 | 212 | } |
190 | 213 | |
191 | 214 | // 割り込み有効 |
@@ -232,11 +255,15 @@ static void pf_timer_init_id(PF_TIMER_ID id) | ||
232 | 255 | pf_timer_info[id].cc[3] = pf_timer_table[id].cc3; |
233 | 256 | pf_timer_info[id].running = FALSE; |
234 | 257 | |
235 | - // 割り込み設定 | |
236 | - irq = pf_timer_to_irq[id]; | |
237 | - NVIC_SetPriority(irq, pf_timer_table[id].priority); | |
238 | - NVIC_ClearPendingIRQ(irq); | |
239 | - NVIC_EnableIRQ(irq); | |
258 | + // キャプチャモード以外は割り込みを使用する | |
259 | + if (PF_TIMER_ACTION_CAPTURE != pf_timer_table[id].action) | |
260 | + { | |
261 | + // 割り込み設定 | |
262 | + irq = pf_timer_to_irq[id]; | |
263 | + NVIC_SetPriority(irq, pf_timer_table[id].priority); | |
264 | + NVIC_ClearPendingIRQ(irq); | |
265 | + NVIC_EnableIRQ(irq); | |
266 | + } | |
240 | 267 | } |
241 | 268 | |
242 | 269 | //! @brief Timer初期化 |
@@ -267,6 +294,12 @@ void pf_timer_start(PF_TIMER_ID id) | ||
267 | 294 | pf_timer_table[id].dev->TASKS_CLEAR = |
268 | 295 | TIMER_TASKS_CLEAR_TASKS_CLEAR_Trigger << TIMER_TASKS_CLEAR_TASKS_CLEAR_Pos; |
269 | 296 | |
297 | +// // キャプチャモード時はCC[0]をクリア | |
298 | +// if (PF_TIMER_ACTION_CAPTURE == pf_timer_table[id].action) | |
299 | +// { | |
300 | +// pf_timer_table[id].dev->CC[0] = 0; | |
301 | +// } | |
302 | + | |
270 | 303 | // 開始 |
271 | 304 | pf_timer_table[id].dev->TASKS_START = |
272 | 305 | TIMER_TASKS_START_TASKS_START_Trigger << TIMER_TASKS_START_TASKS_START_Pos; |
@@ -367,6 +400,68 @@ void pf_timer_cc(PF_TIMER_ID id, u4 *cc) | ||
367 | 400 | } |
368 | 401 | } |
369 | 402 | |
403 | +//! @brief Timerキャプチャカウンタを取得 | |
404 | +//! @param [in] id TimerのID | |
405 | +//! @return キャプチャカウンタ | |
406 | +u4 pf_timer_get_capture(PF_TIMER_ID id) | |
407 | +{ | |
408 | + u4 capture; | |
409 | + | |
410 | + // オート変数初期化 | |
411 | + capture = 0; | |
412 | + | |
413 | + // パラメータチェック | |
414 | + if (id < PF_TIMER_ID_MAX) | |
415 | + { | |
416 | + capture = pf_timer_table[id].dev->CC[0]; | |
417 | + | |
418 | + // 次回に備え、停止・クリア・CC[0]クリアを行う | |
419 | + pf_timer_table[id].dev->TASKS_STOP = TIMER_TASKS_STOP_TASKS_STOP_Trigger << TIMER_TASKS_STOP_TASKS_STOP_Pos; | |
420 | + pf_timer_table[id].dev->TASKS_CLEAR = TIMER_TASKS_CLEAR_TASKS_CLEAR_Trigger << TIMER_TASKS_CLEAR_TASKS_CLEAR_Pos; | |
421 | + pf_timer_table[id].dev->CC[0] = 0; | |
422 | + } | |
423 | + | |
424 | + return capture; | |
425 | +} | |
426 | + | |
427 | +//! @brief Timerスタートアドレス取得 | |
428 | +//! @param [in] id TimerのID | |
429 | +//! @return TASKS_STARTレジスタのアドレス | |
430 | +u4 pf_timer_get_tasks_start(PF_TIMER_ID id) | |
431 | +{ | |
432 | + u4 addr; | |
433 | + | |
434 | + // オート変数初期化 | |
435 | + addr = 0; | |
436 | + | |
437 | + // パラメータチェック | |
438 | + if (id < PF_TIMER_ID_MAX) | |
439 | + { | |
440 | + addr = (u4)&(pf_timer_table[id].dev->TASKS_START); | |
441 | + } | |
442 | + | |
443 | + return addr; | |
444 | +} | |
445 | + | |
446 | +//! @brief Timerキャプチャイベントアドレス取得 | |
447 | +//! @param [in] id TimerのID | |
448 | +//! @return TASKS_CAPTURE[0]レジスタのアドレス | |
449 | +u4 pf_timer_get_tasks_capture(PF_TIMER_ID id) | |
450 | +{ | |
451 | + u4 addr; | |
452 | + | |
453 | + // オート変数初期化 | |
454 | + addr = 0; | |
455 | + | |
456 | + // パラメータチェック | |
457 | + if (id < PF_TIMER_ID_MAX) | |
458 | + { | |
459 | + addr = (u4)&(pf_timer_table[id].dev->TASKS_CAPTURE[0]); | |
460 | + } | |
461 | + | |
462 | + return addr; | |
463 | +} | |
464 | + | |
370 | 465 | //! @brief 共通割り込みハンドラ |
371 | 466 | //! @param [in] channel Timerチャネル(0~4) |
372 | 467 | static void pf_timer_isr(u4 channel) |
@@ -629,7 +629,7 @@ void UARTE0_UART0_IRQHandler(void) | ||
629 | 629 | pf_uart_isr_error(); |
630 | 630 | } |
631 | 631 | |
632 | - // RXSTATRED | |
632 | + // RXSTARTED | |
633 | 633 | if ((UARTE_EVENTS_RXSTARTED_EVENTS_RXSTARTED_Generated |
634 | 634 | << UARTE_EVENTS_RXSTARTED_EVENTS_RXSTARTED_Pos) == NRF_UARTE0->EVENTS_RXSTARTED) |
635 | 635 | { |
@@ -0,0 +1,291 @@ | ||
1 | +//! @file pf_ultra.c | |
2 | +//! @brief プラットフォーム(超音波)実装ファイル | |
3 | + | |
4 | +// The MIT License (MIT) | |
5 | +// Copyright (c) 2023 @xm6_original | |
6 | +// | |
7 | +// Permission is hereby granted, free of charge, to any person obtaining a | |
8 | +// copy of this software and associated documentation files (the "Software"), | |
9 | +// to deal in the Software without restriction, including without limitation | |
10 | +// the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
11 | +// and/or sell copies of the Software, and to permit persons to whom the | |
12 | +// Software is furnished to do so, subject to the following conditions: | |
13 | +// | |
14 | +// The above copyright notice and this permission notice shall be included in | |
15 | +// all copies or substantial portions of the Software. | |
16 | +// | |
17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
22 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
23 | +// DEALINGS IN THE SOFTWARE. | |
24 | + | |
25 | +#include "pf_types.h" | |
26 | +#include "nrf52833.h" | |
27 | +#include "nrf52833_bitfields.h" | |
28 | +#include "pf_gpio.h" | |
29 | +#include "pf_systick.h" | |
30 | +#include "pf_interrupt.h" | |
31 | +#include "pf_timer.h" | |
32 | +#include "pf_ultra.h" | |
33 | + | |
34 | +//! @brief 測定間隔[ms] | |
35 | +//! @details データシートより200ms以上が必要 | |
36 | +#define PF_ULTRA_MEASURE_CYCLE_MS ((u4)200U) | |
37 | + | |
38 | +//! @brief TRIGホールド時間[us] | |
39 | +//! @details データシートより10us以上が必要(少しマージンを取る) | |
40 | +#define PF_ULTRA_TRIG_HOLD_US ((u4)14U) | |
41 | + | |
42 | +//! @brief 測定温度 | |
43 | +//! @details 温度センサと組み合わせるのが理想だが、一律25℃で固定する | |
44 | +#define PF_ULTRA_TEMPERATURE ((f4)25.0f) | |
45 | + | |
46 | +//! @brief 測定上限[cm] | |
47 | +//! @details 上限値を超えた場合はエラーとする | |
48 | +#define PF_ULTRA_UPPER_LIMIT_CM ((f4)200.0f) | |
49 | + | |
50 | +//! @brief 超音波動作情報構造体 | |
51 | +typedef struct PF_ULTRA_INFO_Tag | |
52 | +{ | |
53 | + u4 cm; //!< 前方までの距離(0=エラー) | |
54 | + u4 count; //!< 周期カウンタ | |
55 | + BOOL measure; //!< 測定中フラグ | |
56 | +} PF_ULTRA_INFO; | |
57 | + | |
58 | +//! @brief 超音波動作情報 | |
59 | +static PF_ULTRA_INFO pf_ultra_info; | |
60 | + | |
61 | +//! @brief GPIOTE初期化 | |
62 | +static void pf_ultra_init_gpiote(void) | |
63 | +{ | |
64 | + u4 port; | |
65 | + u4 pin; | |
66 | + | |
67 | + // オート変数初期化 | |
68 | + port = 0; | |
69 | + pin = 0; | |
70 | + | |
71 | + // GPIOより、ECHOのポートとピンを取得 | |
72 | + port = pf_gpio_get_port(PF_GPIO_ID_MAQUEEN_ECHO); | |
73 | + pin = pf_gpio_get_pin(PF_GPIO_ID_MAQUEEN_ECHO); | |
74 | + | |
75 | + // チャネル0コンフィグ(Eventモード、Rising Edgeで使用) | |
76 | + NRF_GPIOTE->CONFIG[0] = (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos) | |
77 | + | (pin << GPIOTE_CONFIG_PSEL_Pos) | (port << GPIOTE_CONFIG_PORT_Pos) | |
78 | + | (GPIOTE_CONFIG_POLARITY_LoToHi << GPIOTE_CONFIG_POLARITY_Pos); | |
79 | + | |
80 | + // チャネル1コンフィグ(Eventモード、Falling Edgeで使用) | |
81 | + NRF_GPIOTE->CONFIG[1] = (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos) | |
82 | + | (pin << GPIOTE_CONFIG_PSEL_Pos) | (port << GPIOTE_CONFIG_PORT_Pos) | |
83 | + | (GPIOTE_CONFIG_POLARITY_HiToLo << GPIOTE_CONFIG_POLARITY_Pos); | |
84 | + | |
85 | + // 割り込み設定(ノイズを考慮し、この時点では禁止しておく) | |
86 | + NVIC_SetPriority(GPIOTE_IRQn, PF_INTERRUPT_PRI_ULTRA); | |
87 | + NVIC_DisableIRQ(GPIOTE_IRQn); | |
88 | + | |
89 | + // 割り込み有効化(チャネル1のタイミングで発生させる) | |
90 | + NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN1_Set << GPIOTE_INTENSET_IN1_Pos; | |
91 | +} | |
92 | + | |
93 | +//! @brief PPI初期化 | |
94 | +static void pf_ultra_init_ppi(void) | |
95 | +{ | |
96 | + // チャネル0(GPIOTEのEVENTS_IN[0]をTIMERのTASK_STARTに接続) | |
97 | + NRF_PPI->CH[0].EEP = (u4)&(NRF_GPIOTE->EVENTS_IN[0]); | |
98 | + NRF_PPI->CH[0].TEP = pf_timer_get_tasks_start(PF_TIMER_ID_ULTRA); | |
99 | + NRF_PPI->FORK[0].TEP = 0; | |
100 | + NRF_PPI->CHENSET = PPI_CHENSET_CH0_Set << PPI_CHENSET_CH0_Pos; | |
101 | + | |
102 | + // チャネル1(GPIOTEのEVENTS_IN[1]をTIMERのTASK_CAPTUREに接続) | |
103 | + NRF_PPI->CH[1].EEP = (u4)&(NRF_GPIOTE->EVENTS_IN[1]); | |
104 | + NRF_PPI->CH[1].TEP = pf_timer_get_tasks_capture(PF_TIMER_ID_ULTRA); | |
105 | + NRF_PPI->FORK[1].TEP = 0; | |
106 | + NRF_PPI->CHENSET = PPI_CHENSET_CH1_Set << PPI_CHENSET_CH1_Pos; | |
107 | +} | |
108 | + | |
109 | +//! @brief 超音波初期化 | |
110 | +//! @remarks プラットフォーム初期化処理から呼び出すこと | |
111 | +void pf_ultra_init(void) | |
112 | +{ | |
113 | + // 周辺機能を初期化 | |
114 | + pf_ultra_init_gpiote(); | |
115 | + pf_ultra_init_ppi(); | |
116 | + | |
117 | + // 動作情報を初期化 | |
118 | + pf_ultra_info.cm = 0; | |
119 | + pf_ultra_info.count = (PF_ULTRA_MEASURE_CYCLE_MS) / (PF_SYSTICK_SYNC_MS) - 1; | |
120 | + pf_ultra_info.measure = FALSE; | |
121 | +} | |
122 | + | |
123 | +//! @brief usウェイト | |
124 | +//! @param [in] us 待ち時間[us] | |
125 | +static void pf_ultra_wait_us(u4 us) | |
126 | +{ | |
127 | + u4 diff; | |
128 | + PF_SYSTICK_TIME time[2]; | |
129 | + | |
130 | + // オート変数初期化 | |
131 | + diff = 0; | |
132 | + time[0].sec = 0; | |
133 | + time[0].ms = 0; | |
134 | + time[0].us = 0; | |
135 | + time[1].sec = 0; | |
136 | + time[1].ms = 0; | |
137 | + time[1].us = 0; | |
138 | + | |
139 | + // 現在のSysTick時刻を取得 | |
140 | + pf_systick_time(&time[0]); | |
141 | + | |
142 | + // 差分が引数を超えるまでループ | |
143 | + while (diff < us) | |
144 | + { | |
145 | + // time[1]を取得 | |
146 | + pf_systick_time(&time[1]); | |
147 | + | |
148 | + // msが変化なしか? | |
149 | + if (time[0].ms == time[1].ms) | |
150 | + { | |
151 | + // msが同じであれば、usの差異を得る | |
152 | + diff = time[1].us - time[0].us; | |
153 | + } | |
154 | + else | |
155 | + { | |
156 | + // msが異なっているなら、us差異に1000加算する | |
157 | + diff = 1000U + time[1].us - time[0].us; | |
158 | + } | |
159 | + } | |
160 | +} | |
161 | + | |
162 | +//! @brief 超音波測定開始 | |
163 | +static void pf_ultra_start(void) | |
164 | +{ | |
165 | + u4 enable; | |
166 | + | |
167 | + // オート変数初期化 | |
168 | + enable = 0; | |
169 | + | |
170 | + // 測定開始 | |
171 | + pf_ultra_info.measure = TRUE; | |
172 | + | |
173 | + // TRIGピンをHighレベルにする | |
174 | + pf_gpio_output(PF_GPIO_ID_MAQUEEN_TRIG, TRUE); | |
175 | + | |
176 | + // TRIGピンをHighのままホールドする | |
177 | + pf_ultra_wait_us(PF_ULTRA_TRIG_HOLD_US); | |
178 | + | |
179 | + // グローバル割り込み禁止 | |
180 | + enable = pf_interrupt_global_disable(); | |
181 | + | |
182 | + // GPIOTEイベントをクリア | |
183 | + NRF_GPIOTE->EVENTS_IN[0] = GPIOTE_EVENTS_PORT_EVENTS_PORT_NotGenerated | |
184 | + << GPIOTE_EVENTS_PORT_EVENTS_PORT_Pos; | |
185 | + NRF_GPIOTE->EVENTS_IN[1] = GPIOTE_EVENTS_PORT_EVENTS_PORT_NotGenerated | |
186 | + << GPIOTE_EVENTS_PORT_EVENTS_PORT_Pos; | |
187 | + | |
188 | + // GPIOTE割り込み有効 | |
189 | + NVIC_ClearPendingIRQ(GPIOTE_IRQn); | |
190 | + NVIC_EnableIRQ(GPIOTE_IRQn); | |
191 | + | |
192 | + // TRIGピンをLowレベルにする | |
193 | + pf_gpio_output(PF_GPIO_ID_MAQUEEN_TRIG, FALSE); | |
194 | + | |
195 | + // グローバル割り込み復元 | |
196 | + pf_interrupt_global_restore(enable); | |
197 | +} | |
198 | + | |
199 | +//! @brief 超音波定期タスク | |
200 | +//! @remarks プラットフォーム定期タスク(入力系)処理から呼び出すこと | |
201 | +void pf_ultra_task(void) | |
202 | +{ | |
203 | + // カウントアップ | |
204 | + pf_ultra_info.count++; | |
205 | + | |
206 | + // 測定周期チェック | |
207 | + if (((PF_ULTRA_MEASURE_CYCLE_MS) / (PF_SYSTICK_SYNC_MS)) >= pf_ultra_info.count) | |
208 | + { | |
209 | + // カウンタリセット | |
210 | + pf_ultra_info.count = 0; | |
211 | + | |
212 | + // この時点で測定中であれば、ダミー読み出し後、エラーにする | |
213 | + if (TRUE == pf_ultra_info.measure) | |
214 | + { | |
215 | + (void)pf_timer_get_capture(PF_TIMER_ID_ULTRA); | |
216 | + pf_ultra_info.cm = 0; | |
217 | + pf_ultra_info.measure = FALSE; | |
218 | + } | |
219 | + | |
220 | + // 測定開始 | |
221 | + pf_ultra_start(); | |
222 | + } | |
223 | +} | |
224 | + | |
225 | +//! @brief 距離取得 | |
226 | +//! @return 前方の距離(0=測定エラー) | |
227 | +u4 pf_ultra_get(void) | |
228 | +{ | |
229 | + u4 enable; | |
230 | + u4 cm; | |
231 | + | |
232 | + // オート変数初期化 | |
233 | + enable = 0; | |
234 | + cm = 0; | |
235 | + | |
236 | + // 割り込み禁止 | |
237 | + enable = pf_interrupt_local_disable(PF_INTERRUPT_PRI_ULTRA); | |
238 | + | |
239 | + // 測定結果を取得 | |
240 | + cm = pf_ultra_info.cm; | |
241 | + | |
242 | + // 割り込み復元 | |
243 | + pf_interrupt_local_restore(PF_INTERRUPT_PRI_ULTRA, enable); | |
244 | + | |
245 | + return cm; | |
246 | +} | |
247 | + | |
248 | +//! @brief GPIOTE割り込みハンドラ | |
249 | +//! @attention データ競合(割り込み干渉)に注意する | |
250 | +void GPIOTE_IRQHandler(void) | |
251 | +{ | |
252 | + u4 capture; | |
253 | + f4 speed_of_sound; | |
254 | + f4 duration; | |
255 | + f4 distance; | |
256 | + | |
257 | + // オート変数初期化 | |
258 | + capture = 0; | |
259 | + speed_of_sound = 331.5f + 0.6f * PF_ULTRA_TEMPERATURE; | |
260 | + duration = 0.0f; | |
261 | + distance = 0.0f; | |
262 | + | |
263 | + // GPIOTEイベントをクリア | |
264 | + NRF_GPIOTE->EVENTS_IN[0] = GPIOTE_EVENTS_PORT_EVENTS_PORT_NotGenerated | |
265 | + << GPIOTE_EVENTS_PORT_EVENTS_PORT_Pos; | |
266 | + NRF_GPIOTE->EVENTS_IN[1] = GPIOTE_EVENTS_PORT_EVENTS_PORT_NotGenerated | |
267 | + << GPIOTE_EVENTS_PORT_EVENTS_PORT_Pos; | |
268 | + | |
269 | + // タイマのキャプチャカウンタ(単位:us)を得る | |
270 | + capture = pf_timer_get_capture(PF_TIMER_ID_ULTRA); | |
271 | + | |
272 | + // キャプチャカウンタから変換 | |
273 | + duration = (f4)capture; | |
274 | + duration /= 2.0f; | |
275 | + distance = (duration * speed_of_sound) / 10000U; | |
276 | + | |
277 | + // 上限チェック | |
278 | + if (distance < PF_ULTRA_UPPER_LIMIT_CM) | |
279 | + { | |
280 | + // 測定成功 | |
281 | + pf_ultra_info.cm = (u4)distance; | |
282 | + } | |
283 | + else | |
284 | + { | |
285 | + // エラー | |
286 | + pf_ultra_info.cm = 0; | |
287 | + } | |
288 | + | |
289 | + // 測定完了 | |
290 | + pf_ultra_info.measure = FALSE; | |
291 | +} |