{"id":181,"date":"2020-12-03T13:35:00","date_gmt":"2020-12-03T11:35:00","guid":{"rendered":"http:\/\/kivijakola.fi\/projektit\/?p=181"},"modified":"2020-12-08T10:27:43","modified_gmt":"2020-12-08T08:27:43","slug":"arduino-renault-ecu-decoder","status":"publish","type":"post","link":"https:\/\/kivijakola.fi\/projektit\/2020\/12\/03\/arduino-renault-ecu-decoder\/","title":{"rendered":"Arduino Renault ECU Decoder &#038; IMMO emulator"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"534\" src=\"https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/arduino_decoder-1024x534.png\" alt=\"\" class=\"wp-image-182\" srcset=\"https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/arduino_decoder-1024x534.png 1024w, https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/arduino_decoder-300x156.png 300w, https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/arduino_decoder-768x400.png 768w, https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/arduino_decoder.png 1061w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">1: standard\n2: advanced1\n3: advanced2\n4: semiauto\n5: clear by code\n6: set by code\n&gt;&gt;<span class=\"has-inline-color has-vivid-red-color\">5<\/span>\n5: clearing by code\nGive code\n&gt;&gt;<span class=\"has-inline-color has-vivid-red-color\">1234<\/span>\nCycle 1234\n...<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-cpp\" data-lang=\"C++\"><code>\/\/(c) 2020 Janne Kivijakola\n\n#define IMMO_PIN 2\n#define IMMO_PIN_ACTIVE_STATE HIGH\n#define PWR_PIN 3\n#define PWR_PIN_ACTIVE_STATE HIGH\n\nunsigned long DELAY_TIME = 12500; \/\/ 12.5 msec\n\nvoid setup()\n{\n    Serial.begin(115200);\n    Serial.print(&quot;\\n\\nStart\\n&quot;);\n    pinMode(IMMO_PIN, OUTPUT);\n    digitalWrite(IMMO_PIN, !IMMO_PIN_ACTIVE_STATE);\n    pinMode(PWR_PIN, OUTPUT);\n    digitalWrite(PWR_PIN, !PWR_PIN_ACTIVE_STATE);\n}\n\nvoid loop()\n{\n    char serialByte = 0;\n    Serial.println(&quot;\\n1: standard&quot;);\n    Serial.println(&quot;2: advanced1&quot;);\n    Serial.println(&quot;3: advanced2&quot;);\n    Serial.println(&quot;4: semiauto&quot;);\n    Serial.println(&quot;5: clear by code&quot;);\n    Serial.println(&quot;6: set by code (IMMO emulator)&quot;);\n    Serial.print(&quot;&gt;&gt;&quot;);\n    while (serialByte &lt; &#39;0&#39; || serialByte &gt; &#39;6&#39;)\n    {\n        if (Serial.available() &gt; 0)\n        {\n            serialByte = Serial.read();\n        }\n    }\n    Serial.println(serialByte);\n    switch (serialByte)\n    {\n    case &#39;1&#39;:\n        standard();\n        break;\n    case &#39;2&#39;:\n        advanced1();\n        break;\n    case &#39;3&#39;:\n        advanced2();\n        break;\n    case &#39;4&#39;:\n        semiauto();\n        break;\n    case &#39;5&#39;:\n        clearbycode();\n        break;\n    case &#39;6&#39;:\n        setcode();\n        break;\n    }\n\n}\n\nvoid standard()\n{\n  \n    Serial.println(&quot;\\n1: standard&quot;);\n        while (Serial.available() &gt; 0)\n        Serial.read();\n    process(16, 2, 0, 0, 0);\n}\n\nvoid advanced1()\n{\n    Serial.println(&quot;2: advanced1&quot;);\n        while (Serial.available() &gt; 0)\n        Serial.read();\n    process(16, 5, 0, 0, 0);\n}\n\nvoid advanced2()\n{\n    Serial.println(&quot;3: advanced2&quot;);\n        while (Serial.available() &gt; 0)\n        Serial.read();\n    process(48, 6, 0, 0, 0);\n}\n\nvoid semiauto()\n{\n    Serial.println(&quot;4: semiauto&quot;);\n        while (Serial.available() &gt; 0)\n        Serial.read();\n    process(16, 7, 1, 0, 0);\n}\n\nvoid clearbycode()\n{\n    int code = 472;\n    Serial.println(&quot;5: clearing by code&quot;);\n    code = readInteger();\n\n    process(48, 2, 0, code, 0);\n}\n\nvoid setcode()\n{\n    int code = 0;\n    Serial.println(&quot;5: setting code&quot;);\n    while(code&lt;=0)\n      code = readInteger();\n    \n    process(48, 2, 0, code, 1);\n}\nint readInteger()\n{\n    char serialByte = 0;\n    int receivedBytes = 0;\n    char serbuffer[10] ={};\n    Serial.print(&quot;Give code\\n&gt;&gt;&quot;);\n    while (Serial.available() &gt; 0)\n        Serial.read();\n    while (1)\n    {\n        if (Serial.available() &gt; 0)\n        {\n            serialByte = Serial.read();\n            if((serialByte &gt;= &#39;0&#39; && serialByte &lt;= &#39;9&#39;)||receivedBytes &gt; 4)\n            {\n                serbuffer[receivedBytes++] = serialByte;\n            }\n            else\n                break;\n        }\n       \n    }\n    Serial.println(serbuffer);\n    return atoi(serbuffer);\n}\n\nvoid process(int resetStateBits, int rounds, int mode, int myOwnCode, int setCode)\n{\n    unsigned long delayStart = 0;\n    int counter = 1;\n    int bitsRemaining = 0;\n    int currentRound = 0;\n    unsigned long pipeline = 0;\n    int resetStateRemaining = 0;\n    int dataWidth = 22;\n    if (mode == 1)\n    {\n        dataWidth = 9;\n    }\n\n    if(myOwnCode!=0)\n    {\n      counter = myOwnCode;\n      resetStateRemaining = resetStateBits;\n    }\n\n    delayStart = micros();\n\n    while (1)\n    {\n        int immoPinState = 0;\n        if (((micros() - delayStart) &gt;= DELAY_TIME))\n        {\n            delayStart += DELAY_TIME;\n\n            if (!bitsRemaining)\n            {\n                if (Serial.available() &gt; 0)\n                    return;\n\n                if (!mode)\n                    bitsRemaining = 32;\n                else\n                    bitsRemaining = 18;\n\n                if (currentRound &gt;= rounds && myOwnCode == 0)\n                {\n\n                    counter++;\n                    currentRound = 0;\n                    resetStateRemaining = resetStateBits;\n                }\n                unsigned long A=1;\n                if(setCode==1)\n                {\n                    A=0;\n                }\n\n                if (!mode)\n\n                \/*\n                 * IGN __-------------------------------------------------___-------------------------------------------------__\n                 * IMMO__SSS_AIIIBJJJJCKKKKDLLLLE_SSS_AIIIBJJJJCKKKKDLLLLE___SSS_AIIIBJJJJCKKKKDLLLLE_SSS_AIIIBJJJJCKKKKDLLLLE\n                 *            round1                    round2                       round1                    round2\n                 *                         counter1                                             counter2\n                 * A: Clear code bit                        \n                 * B,C,D,E: Sync bits\n                 * I,J,K,L: Data bits\n                 * S: \n                 *\/\n                                              \n                    pipeline = A &lt;&lt; 19 \/\/A \n                            | ((counter & 0x7000UL) &lt;&lt; 4) \/\/III\n                            | (1UL & ~(0x1UL & (counter &gt;&gt; 12))) &lt;&lt; 15 \/\/B\n                            | ((counter & 0x0f00) &lt;&lt; 3) \/\/JJJJ\n                            | (1 & ~(0x1 & (counter &gt;&gt; 8))) &lt;&lt; 10 \/\/C\n                            | ((counter & 0x00f0) &lt;&lt; 2) \/\/KKKK\n                            | (1 & ~(0x1 & (counter &gt;&gt; 4))) &lt;&lt; 5 \/\/D\n                            | ((counter & 0x000f) &lt;&lt; 1) \/\/LLLL\n                            | (1 & ~(0x1 & (counter &gt;&gt; 0))) &lt;&lt; 0; \/\/E\n                else\n\n                 \/*\n                 * IGN __---------------------------------------...___---------------------------------------...\n                 * IMMO__SSS_IIIIIIII_SSS_IIIIIIII_SSS_IIIIIIII_...___SSS_IIIIIIII_SSS_IIIIIIII_SSS_IIIIIIII_...\n                 *            round1      round2        round3             round1       round2        round3\n                 *                         counter1                                     counter2\n                 *\/\n                    pipeline = ((counter + 1) & 0x00ffUL) &lt;&lt; 13;\/\/IIIIIIII\n\n                if (currentRound == 0)\n                {\n                    Serial.print(&quot;Cycle &quot;);\n                    Serial.print(counter);\n                    Serial.print(&quot; \/ &quot;);\n\n                    if (mode == 0)\n                    {\n                        Serial.print(&quot;6562&quot;);\n                    }\n                    else\n                    {\n                        int code = (counter & 3) + 1\n                                + 10 * (((counter &gt;&gt; 2) & 3) + 1)\n                                + 100 * (((counter &gt;&gt; 4) & 3) + 1)\n                                + 1000 * (((counter &gt;&gt; 6) & 3) + 1);\n                        Serial.print(&quot;255&quot;);\n                        Serial.print(&quot; code: &quot;);\n                        Serial.print(code);\n                    }\n\n                    Serial.print(&quot;\\n&quot;);\n\n                    if ((counter == 6562 && mode == 0)\n                            || (counter == 255 && mode == 1))\n                    {\n                        Serial.print(&quot;DONE\\n&quot;);\n                        return;\n                    }\n                }\n                currentRound++;\n            }\n\n            if (resetStateRemaining)\n            {\n                digitalWrite(PWR_PIN, !PWR_PIN_ACTIVE_STATE);\n                resetStateRemaining--;\n            }\n            else\n            {\n                \/\/start bits\n                if (bitsRemaining &gt; dataWidth)\n                    immoPinState = 1;\n                \/\/data streaming\n                else\n                {\n                    immoPinState = (pipeline & 0x100000UL) &gt;&gt; 20;\n                    pipeline &lt;&lt;= 1;\n                }\n                if (immoPinState)\n                {\n                    digitalWrite(IMMO_PIN, IMMO_PIN_ACTIVE_STATE);\n                }\n                else\n                {\n                    digitalWrite(IMMO_PIN, !IMMO_PIN_ACTIVE_STATE);\n                }\n                digitalWrite(PWR_PIN, PWR_PIN_ACTIVE_STATE);\n                bitsRemaining--;\n            }\n\n        }\n    }\n}\n<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"321\" height=\"230\" src=\"https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/pin_found.png\" alt=\"\" class=\"wp-image-195\" srcset=\"https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/pin_found.png 321w, https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/pin_found-300x215.png 300w\" sizes=\"auto, (max-width: 321px) 100vw, 321px\" \/><figcaption>Koodi tallennettuna ECU:n flashiin<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"849\" height=\"158\" src=\"https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/decod_seq.png\" alt=\"\" class=\"wp-image-204\" srcset=\"https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/decod_seq.png 849w, https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/decod_seq-300x56.png 300w, https:\/\/kivijakola.fi\/projektit\/wp-content\/uploads\/2020\/12\/decod_seq-768x143.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><figcaption>Dekoodaussekvenssi<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1: standard 2: advanced1 3: advanced2 4: semiauto 5: clear by code 6: set by code &gt;&gt;5 5: clearing by code Give code &gt;&gt;1234 Cycle 1234 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,6,1],"tags":[],"class_list":["post-181","post","type-post","status-publish","format-standard","hentry","category-auto","category-elektroniikka","category-yleinen"],"_links":{"self":[{"href":"https:\/\/kivijakola.fi\/projektit\/wp-json\/wp\/v2\/posts\/181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kivijakola.fi\/projektit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kivijakola.fi\/projektit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kivijakola.fi\/projektit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kivijakola.fi\/projektit\/wp-json\/wp\/v2\/comments?post=181"}],"version-history":[{"count":13,"href":"https:\/\/kivijakola.fi\/projektit\/wp-json\/wp\/v2\/posts\/181\/revisions"}],"predecessor-version":[{"id":206,"href":"https:\/\/kivijakola.fi\/projektit\/wp-json\/wp\/v2\/posts\/181\/revisions\/206"}],"wp:attachment":[{"href":"https:\/\/kivijakola.fi\/projektit\/wp-json\/wp\/v2\/media?parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kivijakola.fi\/projektit\/wp-json\/wp\/v2\/categories?post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kivijakola.fi\/projektit\/wp-json\/wp\/v2\/tags?post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}