Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

組み込みソフトウェア基礎_【連続講座 #6】TDD #2 LEDドライバ(ホストPC編)

k-abe
October 31, 2022

組み込みソフトウェア基礎_【連続講座 #6】TDD #2 LEDドライバ(ホストPC編)

2022/10/31 社内勉強会向け資料です。
TDDでLEDドライバをホストPCでつくります。

※リンクが有効なスライドはこちらを参照してください。
https://www.docswell.com/s/juraruming/5G31LZ-2022-10-31-220730

k-abe

October 31, 2022
Tweet

More Decks by k-abe

Other Decks in Programming

Transcript

  1. ʲ࿈ଓߨ࠲ #6ʳTDD #2 LED ド ϥΠ バ (ϗετ PCฤ) 2022/10/31

    ύʔιϧςΫϊϩδʔελοϑגࣜձࣾ Ѩ෦ߞೋ [email protected] ૊ΈࠐΈιϑτ΢ΣΞجૅ
  2. • ࣗݾ঺հ • ࢀՃऀҰݴίϝϯτ • ߨ࠲։࠵ͷഎܠɾ໨త • ຊ೔ͷςʔϚʲLED ド ϥΠ

    バ (ϗετPCฤ)ʳ • TDDֶशͷࢀߟ • ࢀՃऀײ૝ • ࣍ճ༧ࠂ ໨࣍ 2
  3. • LED ド ϥΠ バ (ϗετPCฤ)ͱ͸Կ͔? • TDD͢Δલʹ • TDD΍ͬͯΈΔ

    ຊ೔ͷςʔϚ ʲLED ド ϥΠ バ (ϗετPCฤ)ʳ 8
  4. • LED ド ϥΠ バ (ϗετPCฤ)ͱ͸Կ͔? • TDD͢Δલʹ • TDD΍ͬͯΈΔ

    ຊ೔ͷςʔϚ ʲLED ド ϥΠ バ (ϗετPCฤ)ʳ 9
  5. • LED ド ϥΠ バ (ϗετPCฤ)ͱ͸Կ͔? • TDD͢Δલʹ • TDD΍ͬͯΈΔ

    ຊ೔ͷςʔϚ ʲLED ド ϥΠ バ (ϗετPCฤ)ʳ 12
  6. ɾࢲͷTDD։ൃ؀ڥ ▪ϗετPC: macOS Monterey όʔδϣϯ 12.6 ▪ςετϑϨʔϜϫʔΫ: CppUTest 4.0 ▪ίϯύΠϥ:

    Apple clang version 11.0.0 (clang-1100.0.33.8) ▪ςετର৅ϓϩάϥϛϯάݴޠ: C ▪ϏϧυγεςϜ: cmake 3.21.1 TDD͢Δલʹ 13
  7. ɾࢲͷTDD։ൃ؀ڥ ิ଍ ʼ▪ϗετPC: macOS Monterey όʔδϣϯ 12.6 ɾWindows, LinuxͰ΋Մೳ͔ͱࢥ͍·͢ɻ ʼ▪ςετϑϨʔϜϫʔΫ:

    CppUTest 4.0 ɾϚΠίϯ؀ڥͰ΋TDD͔ͨͬͨ͠ͷͰCppUTestʹܾΊ·ͨ͠ɻ >▪ςετର৅ϓϩάϥϛϯάݴޠ: C ɾςετ͸C++, γεςϜ͸C >▪ϏϧυγεςϜ: cmake 3.21.1 ɾmake͸Ͱ͖ͳ͔ͬͨɾཧղͰ͖ͳ͔ͬͨͷͰcmakeʹ͠·ͨ͠ɻ TDD͢Δલʹ 14
  8. • LED ド ϥΠ バ (ϗετPCฤ)ͱ͸Կ͔? • TDD͢Δલʹ • TDD΍ͬͯΈΔ

    ຊ೔ͷςʔϚ ʲLED ド ϥΠ バ (ϗετPCฤ)ʳ 17
  9. ɾςετΛॻ͘ ʙTDD؀ڥͷ֬ೝ 2ʙ ɹbuildσΟϨΫτϦͰcmakeΛ࣮ߦ͠ɺMakefileΛ࡞੒͢Δ $ cmake .. -- The C

    compiler identification is AppleClang 11.0.0.11000033 -- The CXX compiler identification is AppleClang 11.0.0.11000033 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done Using CppUTest found in /usr/local/Cellar/cpputest/4.0 -- Configuring done -- Generating done -- Build files have been written to: /Users/k-abe/evkart/evkart_tdd/build $ ls CMakeCache.txt Makefile cmake_install.cmake src CMakeFiles bin lib tests TDD΍ͬͯΈΔ 24 Makefile͕ग़དྷͨ
  10. ɾςετΛॻ͘ ʙTDD؀ڥͷ֬ೝ 3ʙ ɹ࡞੒͞ΕͨMakefileΛmake͢Δ $ make [ 20%] Building C

    object src/hello/CMakeFiles/hello.dir/Hello.c.o [ 40%] Linking C static library ../../lib/libhello.a [ 40%] Built target hello [ 60%] Building CXX object tests/CMakeFiles/tdd_test.dir/AllTests.cpp.o [ 80%] Building CXX object tests/CMakeFiles/tdd_test.dir/hello/HelloTest.cpp.o [100%] Linking CXX executable ../bin/tdd_test [100%] Built target tdd_test KojinoMacBook-2:build k-abe$ ls bin tdd_test TDD΍ͬͯΈΔ 25 ࣮ߦϑΝΠϧ(tdd_test)͕Ͱ͖Δ
  11. ɾςετΛॻ͘ ʙTDD؀ڥͷ֬ೝ 4ʙ ɹ࣮ߦϑΝΠϧΛ࣮ߦ͢Δ $ ./bin/tdd_test /Users/k-abe/evkart/evkart_tdd/tests/hello/HelloTest.cpp:23: error: Failure in

    TEST(Hello, Create) Start here!!! . Errors (1 failures, 1 tests, 1 ran, 1 checks, 0 ignored, 0 filtered out, 1 ms) TDD΍ͬͯΈΔ 26 ςετΛ࣮ߦ͠ɺςετ͕ࣦഊ͠ ͨ͜ͱ͕Θ͔Δɻ
  12. ɾςετΛॻ͘ ʙTDD؀ڥͷ֬ೝ 5ʙ ɹςετϑΝΠϧΛ֬ೝ͢Δɻ ɹevkart_tdd/tests/hello/HelloTest.cpp TEST(Hello, Create) { FAIL("Start here!!!");

    } ※CppUTestͰ࢖͑ΔΞαʔγϣϯ͕ͪ͜ΒΛࢀর TDD΍ͬͯΈΔ 27 FAILͰςετࣦഊ͍ͤͯ͞Δɻ
  13. ɾςετΛॻ͘ ʙςετͷ਽ܗ࡞੒ʙ ɹ͔͜͜ΒࠓճͷςετΛॻ͍͍ͯ͘ɻ·ͣ͸ςετͷ਽ܗΛͭ͘Δɻ ɹ ɹࠓճͷιʔείʔυͷϧʔτͷ֊૚(evkart_tdd)Ͱ࣍ͷίϚϯυΛ࣮ߦ͢Δɻ $ NewPackageDirs HallSensorLedDriver creating src/HallSensorLedDriver

    creating include/HallSensorLedDriver creating tests/HallSensorLedDriver $ NewCModule.sh HallSensorLedDriver HallSensorLedDriver creating include/HallSensorLedDriver/HallSensorLedDriver.h creating src/HallSensorLedDriver/HallSensorLedDriver.c creating tests/HallSensorLedDriver/HallSensorLedDriverTest.cpp TDD΍ͬͯΈΔ 28 σΟϨΫτϦΛͭ͘Γɺ ͦͷதʹCίʔυͷεέϧτϯΛ࡞੒ͯ͘͠ΕΔ CppUTestͷεΫϦϓτΛ࣮ߦ͢Δɻ
  14. ɾςετΛॻ͘ ʙcmakeઃఆมߋ2ʙ ɹcmakeઃఆมߋޙʹMakefile࡞੒ɾmake͠ੜ੒͞Ε࣮ͨߦϑΝΠϧΛ࣮ߦ͢Δͱࠓճ௥Ճͨ͠ HallSensorLedDriverͷςετ͕ࣦഊ͍ͯ͠Δ͜ͱ͕֬ೝͰ͖ͨɻ $ ./bin/tdd_test /Users/k-abe/evkart/evkart_tdd/tests/hello/HelloTest.cpp:23: error: Failure in

    TEST(Hello, Create) Start here!!! . /Users/k-abe/evkart/evkart_tdd/tests/HallSensorLedDriver/HallSensorLedDriverTest.cpp:23: error: Failure in TEST(HallSensorLedDriver, Create) Start here . Errors (2 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms)ɹ ɹ TDD΍ͬͯΈΔ 30
  15. ɾςετΛॻ͘ ʙυϥΠόΛॳظԽͨ͠Βɺ͢΂ͯͷLED͕ফ౮͍ͯ͠Δʙ makeɺ࣮ߦ͢Δͱ૝ఆͱ͓Γςετ͸ࣦഊ͍ͯ͠Δɻ $ ./bin/tdd_test ! /Users/k-abe/evkart/evkart_tdd/tests/HallSensorLedDriver/ HallSensorLedDriverTest.cpp:24: error: Failure

    in TEST(HallSensorLedDriver, LedsOffAfterCreate) Start here . Errors (1 failures, 2 tests, 1 ran, 1 checks, 1 ignored, 0 filtered out, 1 ms) TDD΍ͬͯΈΔ 33 ςετ໊͕Create͔Βมߋޙ ͷLedsOffAfterCreateʹͳ͍ͬͯΔɻ
  16. ɾςετΛॻ͘ ʙυϥΠόΛॳظԽͨ͠Βɺ͢΂ͯͷLED͕ফ౮͍ͯ͠Δʙ make͢ΔͱίϯύΠϧΤϥʔͱͳΔɻ $ make [ 25%] Built target hello

    [ 50%] Built target HallSensorLedDriver Consolidate compiler generated dependencies of target tdd_test [ 62%] Building CXX object tests/CMakeFiles/tdd_test.dir/HallSensorLedDriver/HallSensorLedDriverTest.cpp.o /Users/k-abe/evkart/evkart_tdd/tests/HallSensorLedDriver/HallSensorLedDriverTest.cpp:25:3: error: no matching function for call to 'HallSensorLedDriver_Create' HallSensorLedDriver_Create(&virtualLeds); ^~~~~~~~~~~~~~~~~~~~~~~~~~ /Users/k-abe/evkart/evkart_tdd/include/HallSensorLedDriver/HallSensorLedDriver.h:10:6: note: candidate function not viable: requires 0 arguments, but 1 was provided void HallSensorLedDriver_Create(void); ^ 1 error generated. make[2]: *** [tests/CMakeFiles/tdd_test.dir/HallSensorLedDriver/HallSensorLedDriverTest.cpp.o] Error 1 make[1]: *** [tests/CMakeFiles/tdd_test.dir/all] Error 2 make: *** [all] Error 2 TDD΍ͬͯΈΔ 36
  17. ɾςετΛॻ͘ ʙυϥΠόΛॳظԽͨ͠Βɺ͢΂ͯͷLED͕ফ౮͍ͯ͠Δʙ ίϯύΠϧΤϥʔΛղফ͠makeɺςετ࣮ߦ͢Δͱͭ͗ʹͳΔɻ LEDυϥΠόͷॳظԽΛ͍ͯ͠ͳ͍ͨΊςετ͸ࣦഊ͢Δɻ KojinoMacBook-2:build k-abeKojinoMacBook-2:build k-abe$ ./bin/tdd_test ! /Users/k-abe/evkart/evkart_tdd/tests/HallSensorLedDriver/HallSensorLedDriverTest.cpp:26:

    error: Failure in TEST(HallSensorLedDriver, LedsOffAfterCreate) LONGS_EQUAL((0) & 0xff, (virtualLeds) & 0xff) failed expected < 0 (0x0)> but was <255 (0xff)> . Errors (1 failures, 2 tests, 1 ran, 1 checks, 1 ignored, 0 filtered out, 1 ms) TDD΍ͬͯΈΔ 37
  18. ɾςετΛॻ͘ ʙυϥΠόΛॳظԽͨ͠Βɺ͢΂ͯͷLED͕ফ౮͍ͯ͠Δʙ LEDυϥΠόͷॳظԽΛͯ͠makeɺ࣮ߦ͢Δɻςετ͸੒ޭ͢Δɻ void HallSensorLedDriver_Create(unsigned char* address) { *address =

    0; // ͜ΕΛ௥Ճͨ͠ } $ ./bin/tdd_test !. OK (2 tests, 1 ran, 1 checks, 1 ignored, 0 filtered out, 1 ms) $ ./bin/tdd_test -v IGNORE_TEST(Hello, Create) - 0 ms TEST(HallSensorLedDriver, LedsOffAfterCreate) - 0 ms OK (2 tests, 1 ran, 1 checks, 1 ignored, 0 filtered out, 1 ms) TDD΍ͬͯΈΔ 38
  19. ɾςετΛॻ͘ ʙϗʔϧηϯαʔU͕1ͷ৔߹ɺϗʔϧηϯαʔLEDΛΦϯʹ͢Δʙ ςετ໊শʲTurnOnHallULedʳͱͯ͠ॻ͘ɻ 
 ςετΛ௥Ճ͠ɺࣦഊ͢Δ͜ͱΛ֬ೝ͢Δɻ // ϗʔϧηϯαʔU͕1ͷ৔߹ɺϗʔϧηϯαʔLEDΛΦϯʹ͢Δ TEST(HallSensorLedDriver, TurnOnHallULed) {

    FAIL("Fail: TurnOnHallULed!!!"); } make͠ςετΛ࣮ߦ͢Δɻ௥Ճͨ͠ςετ͕૝ఆͱ͓Γࣦഊ͍ͯ͠Δ͜ͱΛ֬ೝͰ͖Δɻ $ ./bin/tdd_test ! /Users/k-abe/evkart/evkart_tdd/tests/HallSensorLedDriver/HallSensorLedDriverTest.cpp:32: error: Failure in TEST(HallSensorLedDriver, TurnOnHallULed) Fail: TurnOnHallULed!!! .. Errors (1 failures, 3 tests, 2 ran, 2 checks, 1 ignored, 0 filtered out, 4 ms) TDD΍ͬͯΈΔ 41
  20. ɾςετΛॻ͘ ʙϗʔϧηϯαʔU͕1ͷ৔߹ɺϗʔϧηϯαʔLEDΛΦϯʹ͢Δʙ Τϥʔ͕ൃੜ͢ΔɻHallSensorLedDriver_TurnOn͸ؔ਺ఆ͍ٛͯ͠ͳ͍ͷͰ૝ఆ௨Γɻ $ make [ 25%] Built target hello

    [ 50%] Built target HallSensorLedDriver Consolidate compiler generated dependencies of target tdd_test [ 62%] Building CXX object tests/CMakeFiles/tdd_test.dir/HallSensorLedDriver/HallSensorLedDriverTest.cpp.o /Users/k-abe/evkart/evkart_tdd/tests/HallSensorLedDriver/HallSensorLedDriverTest.cpp:34:3: error: use of undeclared identifier 'HallSensorLedDriver_TurnOn'; did you mean 'HallSensorLedDriver_Create'? HallSensorLedDriver_TurnOn(0x04); ^~~~~~~~~~~~~~~~~~~~~~~~~~ HallSensorLedDriver_Create /Users/k-abe/evkart/evkart_tdd/include/HallSensorLedDriver/HallSensorLedDriver.h:10:6: note: 'HallSensorLedDriver_Create' declared here void HallSensorLedDriver_Create(unsigned char* address); ^ /Users/k-abe/evkart/evkart_tdd/tests/HallSensorLedDriver/HallSensorLedDriverTest.cpp:34:30: error: cannot initialize a parameter of type 'unsigned char *' with an rvalue of type 'int' HallSensorLedDriver_TurnOn(0x04); ^~~~ /Users/k-abe/evkart/evkart_tdd/include/HallSensorLedDriver/HallSensorLedDriver.h:10:48: note: passing argument to parameter 'address' here void HallSensorLedDriver_Create(unsigned char* address); ^ 2 errors generated. make[2]: *** [tests/CMakeFiles/tdd_test.dir/HallSensorLedDriver/HallSensorLedDriverTest.cpp.o] Error 1 make[1]: *** [tests/CMakeFiles/tdd_test.dir/all] Error 2 make: *** [all] Error 2 TDD΍ͬͯΈΔ 44
  21. ɾςετΛॻ͘ ʙϗʔϧηϯαʔU͕1ͷ৔߹ɺϗʔϧηϯαʔLEDΛΦϯʹ͢Δʙ ϔομʔϑΝΠϧʹHallSensorLedDriver_TurnOnΛϓϩλΠϓએݴ͢Δɻ ίϯύΠϧΤϥʔ͸ղফ͞ΕΔ͕ɺؔ਺ͷ࣮ମ͕ͳ͍͔ΒϦϯΫΤϥʔʹͳΔɻ $ make [ 25%] Built target

    hello [ 37%] Building C object src/HallSensorLedDriver/CMakeFiles/HallSensorLedDriver.dir/HallSensorLedDriver.c.o [ 50%] Linking C static library ../../lib/libHallSensorLedDriver.a [ 50%] Built target HallSensorLedDriver Consolidate compiler generated dependencies of target tdd_test [ 62%] Building CXX object tests/CMakeFiles/tdd_test.dir/HallSensorLedDriver/HallSensorLedDriverTest.cpp.o [ 75%] Linking CXX executable ../bin/tdd_test Undefined symbols for architecture x86_64: "_HallSensorLedDriver_TurnOn", referenced from: TEST_HallSensorLedDriver_TurnOnHallULed_Test::testBody() in HallSensorLedDriverTest.cpp.o ɹ(লུ) ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[2]: *** [bin/tdd_test] Error 1 make[1]: *** [tests/CMakeFiles/tdd_test.dir/all] Error 2 make: *** [all] Error 2 TDD΍ͬͯΈΔ 45
  22. ɾςετΛॻ͘ ʙϗʔϧηϯαʔU͕1ͷ৔߹ɺϗʔϧηϯαʔLEDΛΦϯʹ͢Δʙ ؔ਺ͷ࣮ମΛॻ͘ɻॲཧ͸ະ࣮૷ʹ͓ͯ͘͠ɻϦϯΫ͕੒ޭ͢ΔͷͰ ςετΛ࣮ߦ͢Δɻ $ ./bin/tdd_test ! /Users/k-abe/evkart/evkart_tdd/tests/HallSensorLedDriver/HallSensorLedDriverTest.cpp:35: error: Failure

    in TEST(HallSensorLedDriver, TurnOnHallULed) LONGS_EQUAL((0x04) & 0xff, (virtualLeds) & 0xff) failed expected <4 (0x4)> but was <0 (0x0)> Errors (1 failures, 3 tests, 2 ran, 2 checks, 1 ignored, 0 filtered out, 1 ms) HallSensorLedDriver_TurnOn͸ະ࣮૷ͷͨΊςετ͕૝ఆͱ͓Γࣦഊ͢Δɻ TDD΍ͬͯΈΔ 46
  23. ɾςετΛॻ͘ ʙϗʔϧηϯαʔU͕1ͷ৔߹ɺϗʔϧηϯαʔLEDΛΦϯʹ͢Δʙ ςετΛ੒ޭͤ͞ΔͨΊͷͭ͗ͷ࣮૷Λߦ͏ɻ HallSensorLedDriver_TurnOnͰ͸HallSensorLedDriver_CreateͷҾ਺ʹࢦఆͨ͠ΞυϨε ͷLEDΛΦϯ͢Ε͹ςετ͸੒ޭ͢Δ͸ͣɻ static unsigned char* hallSensorLedsAddress; void

    HallSensorLedDriver_Create(unsigned char* address) { // *address = 0; hallSensorLedsAddress = address; *hallSensorLedsAddress = 0; } void HallSensorLedDriver_TurnOn(unsigned char ledBitNumber) { *hallSensorLedsAddress = 0x04; } TDD΍ͬͯΈΔ 47
  24. ɾςετΛॻ͘ ʙϗʔϧηϯαʔU͕1ͷ৔߹ɺϗʔϧηϯαʔLEDΛΦϯ ʹ͢Δʙ make͠ςετΛ࣮ߦ͢Δɻςετ੒ޭ͕֬ೝͰ͖ͨɻ $ ./bin/tdd_test -v IGNORE_TEST(Hello, Create) -

    0 ms TEST(HallSensorLedDriver, TurnOnHallULed) - 0 ms TEST(HallSensorLedDriver, LedsOffAfterCreate) - 0 ms OK (3 tests, 2 ran, 2 checks, 1 ignored, 0 filtered out, 2 ms) TDD΍ͬͯΈΔ 48
  25. ɾςετΛॻ͘ ʙϗʔϧηϯαʔU͕0ͷ৔߹ɺϗʔϧηϯαʔLEDΛΦϑʹ͢Δʙ LEDΦϯͷͱ͖ͱಉ༷ͷखॱͰςετ௥Ճɺ࣮૷Λߦ͏ɻ // ௥Ճͨ͠ςετ // ϗʔϧηϯαʔU͕0ͷ৔߹ɺϗʔϧηϯαʔLEDΛΦϑʹ͢Δ TEST(HallSensorLedDriver, TurnOffHallULed) {

    unsigned char virtualLeds; HallSensorLedDriver_Create(&virtualLeds); HallSensorLedDriver_TurnOn(0x04); HallSensorLedDriver_TurnOff(0x04); BYTES_EQUAL(0x00, virtualLeds); } // ςετΛ੒ޭͤ͞Δ࣮૷ void HallSensorLedDriver_TurnOff(unsigned char ledBitNumber){ *hallSensorLedsAddress = 0x00; } TDD΍ͬͯΈΔ 50
  26. ɾςετΛॻ͘ ʙϗʔϧηϯαʔU͕0ͷ৔߹ɺϗʔϧηϯαʔLEDΛΦϑʹ ͢Δʙ make͠ςετΛ࣮ߦ͢Δɻ $ ./bin/tdd_test -v IGNORE_TEST(Hello, Create) -

    0 ms TEST(HallSensorLedDriver, TurnOffHallULed) - 0 ms TEST(HallSensorLedDriver, TurnOnHallULed) - 0 ms TEST(HallSensorLedDriver, LedsOffAfterCreate) - 0 ms OK (4 tests, 3 ran, 3 checks, 1 ignored, 0 filtered out, 4 ms) TDD΍ͬͯΈΔ 51
  27. • ςʔϚͷલఏઆ໌ ʲCQ EVΧʔτʳͱ͸??? ɾಈը1: ૸ߦத ɾಈը2: ج൘Λ֦େ աڈʹॻ͍ٕͨज़ॻ(EV Χʔτ

    で ࢝ΊΔϞ デ ϧ ベ ʔε։ൃ)Λϕʔεʹઆ໌͠·͢ɻ هࡌՕॴɿʲୈ1ষ EVΧʔτͱ͸Կ͔ʳ ࢀߟʲςʔϚͷલఏઆ໌ʳ 58