<p>roh has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/simtrace2/+/14969">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">add new board and app for gpio testing on octsimtest board<br><br>Change-Id: I01243044002f51b34e8dc12c1b1f565bbf1740a2<br>---<br>A firmware/apps/gpio_test/Makefile<br>A firmware/apps/gpio_test/gpio_test.c<br>A firmware/apps/gpio_test/main.c<br>A firmware/apps/gpio_test/usb_strings.txt<br>A firmware/libboard/octsimtest/include/board.h<br>A firmware/libboard/octsimtest/include/i2c.h<br>A firmware/libboard/octsimtest/include/mcp23017.h<br>A firmware/libboard/octsimtest/source/board_octsimtest.c<br>A firmware/libboard/octsimtest/source/i2c.c<br>A firmware/libboard/octsimtest/source/mcp23017.c<br>10 files changed, 686 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/simtrace2 refs/changes/69/14969/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/firmware/apps/gpio_test/Makefile b/firmware/apps/gpio_test/Makefile</span><br><span>new file mode 100644</span><br><span>index 0000000..b2b4707</span><br><span>--- /dev/null</span><br><span>+++ b/firmware/apps/gpio_test/Makefile</span><br><span>@@ -0,0 +1,3 @@</span><br><span style="color: hsl(120, 100%, 40%);">+C_FILES += $(C_LIBUSB_RT)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+C_FILES += gpio_test.c</span><br><span>diff --git a/firmware/apps/gpio_test/gpio_test.c b/firmware/apps/gpio_test/gpio_test.c</span><br><span>new file mode 100644</span><br><span>index 0000000..2ab23e7</span><br><span>--- /dev/null</span><br><span>+++ b/firmware/apps/gpio_test/gpio_test.c</span><br><span>@@ -0,0 +1,10 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#include <stdint.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include "utils.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "chip.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+void gpio_test_init(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  printf("FIXME run tests here\n\n");</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span>diff --git a/firmware/apps/gpio_test/main.c b/firmware/apps/gpio_test/main.c</span><br><span>new file mode 100644</span><br><span>index 0000000..8fe68b3</span><br><span>--- /dev/null</span><br><span>+++ b/firmware/apps/gpio_test/main.c</span><br><span>@@ -0,0 +1,54 @@</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include "board.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "utils.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "osmocom/core/timer.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+extern void gpio_test_init(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* returns '1' in case we should break any endless loop */</span><br><span style="color: hsl(120, 100%, 40%);">+static void check_exec_dbg_cmd(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        int ch;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     if (!UART_IsRxReady())</span><br><span style="color: hsl(120, 100%, 40%);">+                return;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     ch = UART_GetChar();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        board_exec_dbg_cmd(ch);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+extern int main(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      led_init();</span><br><span style="color: hsl(120, 100%, 40%);">+   led_blink(LED_RED, BLINK_ALWAYS_ON);</span><br><span style="color: hsl(120, 100%, 40%);">+  led_blink(LED_GREEN, BLINK_ALWAYS_ON);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* Enable watchdog for 2000 ms, with no window */</span><br><span style="color: hsl(120, 100%, 40%);">+     WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |</span><br><span style="color: hsl(120, 100%, 40%);">+            (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        PIO_InitializeInterrupts(0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      printf("\n\r\n\r"</span><br><span style="color: hsl(120, 100%, 40%);">+           "=============================================================================\n\r"</span><br><span style="color: hsl(120, 100%, 40%);">+         "GPIO Test firmware " GIT_VERSION " (C) 2019 Sysmocom GmbH\n\r"</span><br><span style="color: hsl(120, 100%, 40%);">+           "=============================================================================\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     board_main_top();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   TRACE_INFO("starting gpio test...\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+    gpio_test_init();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   TRACE_INFO("entering main loop...\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+    while (1) {</span><br><span style="color: hsl(120, 100%, 40%);">+           WDT_Restart(WDT);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+           check_exec_dbg_cmd();</span><br><span style="color: hsl(120, 100%, 40%);">+         osmo_timers_prepare();</span><br><span style="color: hsl(120, 100%, 40%);">+                osmo_timers_update();</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span>diff --git a/firmware/apps/gpio_test/usb_strings.txt b/firmware/apps/gpio_test/usb_strings.txt</span><br><span>new file mode 100644</span><br><span>index 0000000..0e797ac</span><br><span>--- /dev/null</span><br><span>+++ b/firmware/apps/gpio_test/usb_strings.txt</span><br><span>@@ -0,0 +1,10 @@</span><br><span style="color: hsl(120, 100%, 40%);">+sysmocom - s.f.m.c. GmbH</span><br><span style="color: hsl(120, 100%, 40%);">+SIMtrace 2 compatible device</span><br><span style="color: hsl(120, 100%, 40%);">+SIMtrace Sniffer</span><br><span style="color: hsl(120, 100%, 40%);">+SIMtrace CCID</span><br><span style="color: hsl(120, 100%, 40%);">+SIMtrace Phone</span><br><span style="color: hsl(120, 100%, 40%);">+SIMtrace MITM</span><br><span style="color: hsl(120, 100%, 40%);">+CardEmulator Modem 1</span><br><span style="color: hsl(120, 100%, 40%);">+CardEmulator Modem 2</span><br><span style="color: hsl(120, 100%, 40%);">+CardEmulator Modem 3</span><br><span style="color: hsl(120, 100%, 40%);">+CardEmulator Modem 4</span><br><span>diff --git a/firmware/libboard/octsimtest/include/board.h b/firmware/libboard/octsimtest/include/board.h</span><br><span>new file mode 100644</span><br><span>index 0000000..018cfdd</span><br><span>--- /dev/null</span><br><span>+++ b/firmware/libboard/octsimtest/include/board.h</span><br><span>@@ -0,0 +1,155 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/* SIMtrace with SAM3S board definition</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * (C) 2016-2017 by Harald Welte <hwelte@hmw-consulting.de></span><br><span style="color: hsl(120, 100%, 40%);">+ * (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or modify</span><br><span style="color: hsl(120, 100%, 40%);">+ * it under the terms of the GNU General Public License as published by</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Free Software Foundation; either version 2 of the License, or</span><br><span style="color: hsl(120, 100%, 40%);">+ * (at your option) any later version.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You should have received a copy of the GNU General Public License</span><br><span style="color: hsl(120, 100%, 40%);">+ * along with this program; if not, write to the Free Software</span><br><span style="color: hsl(120, 100%, 40%);">+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#pragma once</span><br><span style="color: hsl(120, 100%, 40%);">+#include "board_common.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "simtrace_usb.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* Name of the board */</span><br><span style="color: hsl(120, 100%, 40%);">+#define BOARD_NAME "OCTSIMTEST"</span><br><span style="color: hsl(120, 100%, 40%);">+/* Board definition */</span><br><span style="color: hsl(120, 100%, 40%);">+#define octsimtest</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/** oscillator used as main clock source (in Hz) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define BOARD_MAINOSC 18432000</span><br><span style="color: hsl(120, 100%, 40%);">+/** desired main clock frequency (in Hz, based on BOARD_MAINOSC) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define BOARD_MCK 58982400 // 18.432 * 16 / 5</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/** Pin configuration **/</span><br><span style="color: hsl(120, 100%, 40%);">+/* Button to force bootloader start (shorted to ground when pressed */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_BOOTLOADER_SW      {PIO_PA5, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+//FIXME SIM_PWEN_PIN collides with PA5/bootloader_sw on octsimtest</span><br><span style="color: hsl(120, 100%, 40%);">+/* Enable powering the card using the second 3.3 V output of the LDO (active high) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define SIM_PWEN_PIN           {PIO_PA12, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Enable powering the SIM card */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PWR_PINS                SIM_PWEN_PIN</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+// FIXME PA8 is 32khz xtal on octsimtest</span><br><span style="color: hsl(120, 100%, 40%);">+/* Card presence pin */</span><br><span style="color: hsl(120, 100%, 40%);">+#define SW_SIM                  PIO_PA11</span><br><span style="color: hsl(120, 100%, 40%);">+/* Pull card presence pin high (shorted to ground in card slot when card is present) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define SMARTCARD_CONNECT_PIN  {SW_SIM, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP | PIO_DEBOUNCE | PIO_DEGLITCH | PIO_IT_EDGE }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/** Smart card connection **/</span><br><span style="color: hsl(120, 100%, 40%);">+//FIXME</span><br><span style="color: hsl(120, 100%, 40%);">+/* Card RST reset signal input (active low; RST_SIM in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SIM_RST            {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Card I/O data signal input/output (I/O_SIM in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SIM_IO             {PIO_PA6A_TXD0, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Card CLK clock input (CLK_SIM in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SIM_CLK            {PIO_PA2B_SCK0, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Pin to measure card I/O timing (to start measuring the ETU on I/O activity; connected I/O_SIM in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SIM_IO_INPUT       {PIO_PA1B_TIOB0, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+//FIXME PIO_PA4B_TCLK0 PA4 is LED on octsimtest</span><br><span style="color: hsl(120, 100%, 40%);">+/* Pin used as clock input (to measure the ETU duration; connected to CLK_SIM in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SIM_CLK_INPUT      {PIO_PA14, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Pins used to measure ETU timing (using timer counter) */ </span><br><span style="color: hsl(120, 100%, 40%);">+#define PINS_TC                 PIN_SIM_IO_INPUT, PIN_SIM_CLK_INPUT</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/** Phone connection **/</span><br><span style="color: hsl(120, 100%, 40%);">+/* Phone USIM slot 1 VCC pin (VCC_PHONE in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_USIM1_VCC          {PIO_PA25, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Phone USIM slot 1 RST pin (active low; RST_PHONE in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_USIM1_nRST         {PIO_PA24, PIOA, ID_PIOA, PIO_INPUT, PIO_IT_RISE_EDGE | PIO_DEGLITCH }</span><br><span style="color: hsl(120, 100%, 40%);">+/* Phone I/O data signal input/output (I/O_PHONE in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_PHONE_IO           {PIO_PA22A_TXD1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Phone CLK clock input (CLK_PHONE in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_PHONE_CLK          {PIO_PA23A_SCK1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Pin used for phone USIM slot 1 communication */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PINS_USIM1              PIN_PHONE_IO, PIN_PHONE_CLK, PIN_PHONE_CLK_INPUT, PIN_USIM1_VCC, PIN_PHONE_IO_INPUT, PIN_USIM1_nRST</span><br><span style="color: hsl(120, 100%, 40%);">+/* Phone I/O data signal input/output (unused USART RX input; connected to I/O_PHONE in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_PHONE_IO_INPUT     {PIO_PA21A_RXD1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Pin used as clock input (to measure the ETU duration; connected to CLK_PHONE in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_PHONE_CLK_INPUT    {PIO_PA29B_TCLK2, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/** Default pin configuration **/</span><br><span style="color: hsl(120, 100%, 40%);">+/* Disconnect VPP, CLK, and RST lines between card and phone using bus switch (high sets bus switch to high-impedance) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SC_SW_DEFAULT      {PIO_PA20, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Disconnect I/O line between card and phone using bus switch (high sets bus switch to high-impedance) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_IO_SW_DEFAULT      {PIO_PA19, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Disconnect all lines (VPP, CLK, RST, and I/O) between card and phone */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PINS_BUS_DEFAULT        PIN_SC_SW_DEFAULT, PIN_IO_SW_DEFAULT</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/** Sniffer configuration **/</span><br><span style="color: hsl(120, 100%, 40%);">+/* Connect VPP, CLK, and RST lines between card and phone using bus switch (low connects signals on bus switch) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SC_SW_SNIFF        {PIO_PA20, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Connect I/O line between card and phone using bus switch (low connects signals on bus switch) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_IO_SW_SNIFF        {PIO_PA19, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Connect all lines (VPP, CLK, RST, and I/O) between card and phone */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PINS_BUS_SNIFF          PIN_SC_SW_SNIFF, PIN_IO_SW_SNIFF</span><br><span style="color: hsl(120, 100%, 40%);">+/* Card RST reset signal input (use as input since the phone will drive it) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SIM_RST_SNIFF      {PIO_PA7, PIOA, ID_PIOA, PIO_INPUT, PIO_DEGLITCH | PIO_IT_EDGE}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Pins used to sniff phone-card communication */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PINS_SIM_SNIFF          PIN_SIM_IO, PIN_SIM_CLK, PIN_SIM_RST_SNIFF</span><br><span style="color: hsl(120, 100%, 40%);">+/* Disable power converter 4.5-6V to 3.3V (active high) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SIM_PWEN_SNIFF     {SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Enable power switch to forward VCC_PHONE to VCC_SIM (active high) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_VCC_FWD_SNIFF      {VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* Use phone VCC to power card */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PINS_PWR_SNIFF          PIN_SIM_PWEN_SNIFF, PIN_VCC_FWD_SNIFF</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/** CCID configuration */</span><br><span style="color: hsl(120, 100%, 40%);">+/* Card RST reset signal input (active low; RST_SIM in schematic) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_ISO7816_RSTMC      {PIO_PA7, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* ISO7816-communication related pins */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PINS_ISO7816            PIN_SIM_IO,  PIN_SIM_CLK,  PIN_ISO7816_RSTMC // SIM_PWEN_PIN, PIN_SIM_IO2, PIN_SIM_CLK2</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/** External SPI flash interface   **/</span><br><span style="color: hsl(120, 100%, 40%);">+/* SPI MISO pin definition */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SPI_MISO  {PIO_PA12A_MISO, PIOA, PIOA, PIO_PERIPH_A, PIO_PULLUP}</span><br><span style="color: hsl(120, 100%, 40%);">+/* SPI MOSI pin definition */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SPI_MOSI  {PIO_PA13A_MOSI, PIOA, PIOA, PIO_PERIPH_A, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* SPI SCK pin definition */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SPI_SCK   {PIO_PA14A_SPCK, PIOA, PIOA, PIO_PERIPH_A, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* SPI pins definition. Contains MISO, MOSI & SCK */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PINS_SPI       PIN_SPI_MISO, PIN_SPI_MOSI, PIN_SPI_SCK</span><br><span style="color: hsl(120, 100%, 40%);">+/* SPI chip select 0 pin definition */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SPI_NPCS0 {PIO_PA11A_NPCS0, PIOA, PIOA, PIO_PERIPH_A, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+/* SPI flash write protect pin (active low, pulled low) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_SPI_WP    {PA15, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/** Pin configuration to control USB pull-up on D+</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @details the USB pull-up on D+ is enable by default on the board but can be disabled by setting PA16 high</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#define PIN_USB_PULLUP  {PIO_PA16, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/** USB definitions */</span><br><span style="color: hsl(120, 100%, 40%);">+/* OpenMoko SIMtrace 2 USB vendor ID */</span><br><span style="color: hsl(120, 100%, 40%);">+#define BOARD_USB_VENDOR_ID USB_VENDOR_OPENMOKO</span><br><span style="color: hsl(120, 100%, 40%);">+/* OpenMoko SIMtrace 2 USB product ID (main application/runtime mode) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define BOARD_USB_PRODUCT_ID     USB_PRODUCT_SIMTRACE2</span><br><span style="color: hsl(120, 100%, 40%);">+/* OpenMoko SIMtrace 2 DFU USB product ID (DFU bootloader/DFU mode) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define BOARD_DFU_USB_PRODUCT_ID USB_PRODUCT_SIMTRACE2_DFU</span><br><span style="color: hsl(120, 100%, 40%);">+/* USB release number (bcdDevice, shown as 0.00) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define BOARD_USB_RELEASE       0x000</span><br><span style="color: hsl(120, 100%, 40%);">+/* Indicate SIMtrace is bus power in USB attributes */</span><br><span style="color: hsl(120, 100%, 40%);">+#define BOARD_USB_BMATTRIBUTES   USBConfigurationDescriptor_BUSPOWERED_NORWAKEUP</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/** Supported modes */</span><br><span style="color: hsl(120, 100%, 40%);">+/* SIMtrace board supports sniffer mode */</span><br><span style="color: hsl(120, 100%, 40%);">+//#define HAVE_SNIFFER</span><br><span style="color: hsl(120, 100%, 40%);">+/* SIMtrace board supports CCID mode */</span><br><span style="color: hsl(120, 100%, 40%);">+//#define HAVE_CCID</span><br><span style="color: hsl(120, 100%, 40%);">+/* SIMtrace board supports card emulation mode */</span><br><span style="color: hsl(120, 100%, 40%);">+//#define HAVE_CARDEM</span><br><span style="color: hsl(120, 100%, 40%);">+/* SIMtrace board supports man-in-the-middle mode */</span><br><span style="color: hsl(120, 100%, 40%);">+//#define HAVE_MITM</span><br><span style="color: hsl(120, 100%, 40%);">+/* octsimtest board supports gpio_test mode */</span><br><span style="color: hsl(120, 100%, 40%);">+#define HAVE_GPIO_TEST</span><br><span>diff --git a/firmware/libboard/octsimtest/include/i2c.h b/firmware/libboard/octsimtest/include/i2c.h</span><br><span>new file mode 100644</span><br><span>index 0000000..5a8c908</span><br><span>--- /dev/null</span><br><span>+++ b/firmware/libboard/octsimtest/include/i2c.h</span><br><span>@@ -0,0 +1,28 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/* I2C EEPROM memory read and write utilities</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or modify</span><br><span style="color: hsl(120, 100%, 40%);">+ * it under the terms of the GNU General Public License as published by</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Free Software Foundation; either version 2 of the License, or</span><br><span style="color: hsl(120, 100%, 40%);">+ * (at your option) any later version.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You should have received a copy of the GNU General Public License</span><br><span style="color: hsl(120, 100%, 40%);">+ * along with this program; if not, write to the Free Software</span><br><span style="color: hsl(120, 100%, 40%);">+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#pragma once</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <stdbool.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+void i2c_pin_init(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+bool i2c_write_byte(bool send_start, bool send_stop, uint8_t byte);</span><br><span style="color: hsl(120, 100%, 40%);">+uint8_t i2c_read_byte(bool nack, bool send_stop);</span><br><span style="color: hsl(120, 100%, 40%);">+static void i2c_stop_cond(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int eeprom_write_byte(uint8_t slave, uint8_t addr, uint8_t byte);</span><br><span style="color: hsl(120, 100%, 40%);">+int eeprom_read_byte(uint8_t slave, uint8_t addr);</span><br><span>diff --git a/firmware/libboard/octsimtest/include/mcp23017.h b/firmware/libboard/octsimtest/include/mcp23017.h</span><br><span>new file mode 100644</span><br><span>index 0000000..dea6285</span><br><span>--- /dev/null</span><br><span>+++ b/firmware/libboard/octsimtest/include/mcp23017.h</span><br><span>@@ -0,0 +1,23 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/* mcp23017 i2c gpio expander read and write utilities</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or modify</span><br><span style="color: hsl(120, 100%, 40%);">+ * it under the terms of the GNU General Public License as published by</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Free Software Foundation; either version 2 of the License, or</span><br><span style="color: hsl(120, 100%, 40%);">+ * (at your option) any later version.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You should have received a copy of the GNU General Public License</span><br><span style="color: hsl(120, 100%, 40%);">+ * along with this program; if not, write to the Free Software</span><br><span style="color: hsl(120, 100%, 40%);">+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#pragma once</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_ADDRESS 0x20</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int mcp23017_init(uint8_t slave);</span><br><span style="color: hsl(120, 100%, 40%);">+//int mcp23017_write_byte(uint8_t slave, uint8_t addr, uint8_t byte);</span><br><span style="color: hsl(120, 100%, 40%);">+//int mcp23017_read_byte(uint8_t slave, uint8_t addr);</span><br><span>diff --git a/firmware/libboard/octsimtest/source/board_octsimtest.c b/firmware/libboard/octsimtest/source/board_octsimtest.c</span><br><span>new file mode 100644</span><br><span>index 0000000..2772015</span><br><span>--- /dev/null</span><br><span>+++ b/firmware/libboard/octsimtest/source/board_octsimtest.c</span><br><span>@@ -0,0 +1,72 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/* SIMtrace with SAM3S specific application code</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * (C) 2017 by Harald Welte <laforge@gnumonks.org></span><br><span style="color: hsl(120, 100%, 40%);">+ * (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or modify</span><br><span style="color: hsl(120, 100%, 40%);">+ * it under the terms of the GNU General Public License as published by</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Free Software Foundation; either version 2 of the License, or</span><br><span style="color: hsl(120, 100%, 40%);">+ * (at your option) any later version.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You should have received a copy of the GNU General Public License</span><br><span style="color: hsl(120, 100%, 40%);">+ * along with this program; if not, write to the Free Software</span><br><span style="color: hsl(120, 100%, 40%);">+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#include "board.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "simtrace.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "utils.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "sim_switch.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/timer.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include "usb_buf.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "i2c.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "mcp23017.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+void board_exec_dbg_cmd(int ch)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      switch (ch) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case '?':</span><br><span style="color: hsl(120, 100%, 40%);">+             printf("\t?\thelp\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+            printf("\tR\treset SAM3\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+              break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case 'R':</span><br><span style="color: hsl(120, 100%, 40%);">+             printf("Asking NVIC to reset us\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+              USBD_Disconnect();</span><br><span style="color: hsl(120, 100%, 40%);">+            NVIC_SystemReset();</span><br><span style="color: hsl(120, 100%, 40%);">+           break;</span><br><span style="color: hsl(120, 100%, 40%);">+        default:</span><br><span style="color: hsl(120, 100%, 40%);">+              printf("Unknown command '%c'\n\r", ch);</span><br><span style="color: hsl(120, 100%, 40%);">+             break;</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+void board_main_top(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef APPLICATION_dfu</span><br><span style="color: hsl(120, 100%, 40%);">+ usb_buf_init();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     i2c_pin_init();</span><br><span style="color: hsl(120, 100%, 40%);">+       mcp23017_init(MCP23017_ADDRESS);</span><br><span style="color: hsl(120, 100%, 40%);">+      /* Initialize checking for card insert/remove events */</span><br><span style="color: hsl(120, 100%, 40%);">+       //card_present_init();</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int board_override_enter_dfu(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    const Pin bl_sw_pin = PIN_BOOTLOADER_SW;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    PIO_Configure(&bl_sw_pin, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Enter DFU bootloader in case the respective button is pressed */</span><br><span style="color: hsl(120, 100%, 40%);">+   if (PIO_Get(&bl_sw_pin) == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+           /* do not print to early since the console is not initialized yet */</span><br><span style="color: hsl(120, 100%, 40%);">+          //printf("BOOTLOADER switch pressed -> Force DFU\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+          return 1;</span><br><span style="color: hsl(120, 100%, 40%);">+     } else</span><br><span style="color: hsl(120, 100%, 40%);">+                return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span>diff --git a/firmware/libboard/octsimtest/source/i2c.c b/firmware/libboard/octsimtest/source/i2c.c</span><br><span>new file mode 100644</span><br><span>index 0000000..a708704</span><br><span>--- /dev/null</span><br><span>+++ b/firmware/libboard/octsimtest/source/i2c.c</span><br><span>@@ -0,0 +1,225 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/* I2C EEPROM memory read and write utilities</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or modify</span><br><span style="color: hsl(120, 100%, 40%);">+ * it under the terms of the GNU General Public License as published by</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Free Software Foundation; either version 2 of the License, or</span><br><span style="color: hsl(120, 100%, 40%);">+ * (at your option) any later version.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You should have received a copy of the GNU General Public License</span><br><span style="color: hsl(120, 100%, 40%);">+ * along with this program; if not, write to the Free Software</span><br><span style="color: hsl(120, 100%, 40%);">+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#include "board.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include <stdbool.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* Low-Level I2C Routines */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static const Pin pin_sda = {PIO_PA30, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_OPENDRAIN };</span><br><span style="color: hsl(120, 100%, 40%);">+static const Pin pin_sda_in = {PIO_PA30, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT };</span><br><span style="color: hsl(120, 100%, 40%);">+static const Pin pin_scl = {PIO_PA31, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_OPENDRAIN };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void i2c_delay()</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  volatile int v;</span><br><span style="color: hsl(120, 100%, 40%);">+       int i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* 100 cycles results in SCL peak length of 44us, so it's about</span><br><span style="color: hsl(120, 100%, 40%);">+    * 440ns per cycle here */</span><br><span style="color: hsl(120, 100%, 40%);">+    for (i = 0; i < 14; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+         v = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+void i2c_pin_init(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    PIO_Configure(&pin_scl, PIO_LISTSIZE(pin_scl));</span><br><span style="color: hsl(120, 100%, 40%);">+   PIO_Configure(&pin_sda, PIO_LISTSIZE(pin_sda));</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void set_scl(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        PIO_Set(&pin_scl);</span><br><span style="color: hsl(120, 100%, 40%);">+        i2c_delay();</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void set_sda(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       PIO_Set(&pin_sda);</span><br><span style="color: hsl(120, 100%, 40%);">+        i2c_delay();</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void clear_scl(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     PIO_Clear(&pin_scl);</span><br><span style="color: hsl(120, 100%, 40%);">+      i2c_delay();</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void clear_sda(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     PIO_Clear(&pin_sda);</span><br><span style="color: hsl(120, 100%, 40%);">+      i2c_delay();</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static bool read_sda(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      bool ret;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   PIO_Configure(&pin_sda_in, PIO_LISTSIZE(pin_sda_in));</span><br><span style="color: hsl(120, 100%, 40%);">+     if (PIO_Get(&pin_sda_in))</span><br><span style="color: hsl(120, 100%, 40%);">+         ret = true;</span><br><span style="color: hsl(120, 100%, 40%);">+   else</span><br><span style="color: hsl(120, 100%, 40%);">+          ret = false;</span><br><span style="color: hsl(120, 100%, 40%);">+  PIO_Configure(&pin_sda, PIO_LISTSIZE(pin_sda));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return ret;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* Core I2C Routines */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static bool i2c_started = false;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void i2c_start_cond(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  if (i2c_started) {</span><br><span style="color: hsl(120, 100%, 40%);">+            set_sda();</span><br><span style="color: hsl(120, 100%, 40%);">+            set_scl();</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   clear_sda();</span><br><span style="color: hsl(120, 100%, 40%);">+  i2c_delay();</span><br><span style="color: hsl(120, 100%, 40%);">+  clear_scl();</span><br><span style="color: hsl(120, 100%, 40%);">+  i2c_started = true;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void i2c_stop_cond(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  clear_sda();</span><br><span style="color: hsl(120, 100%, 40%);">+  set_scl();</span><br><span style="color: hsl(120, 100%, 40%);">+    set_sda();</span><br><span style="color: hsl(120, 100%, 40%);">+    i2c_delay();</span><br><span style="color: hsl(120, 100%, 40%);">+  i2c_started = false;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void i2c_write_bit(bool bit)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     if (bit)</span><br><span style="color: hsl(120, 100%, 40%);">+              set_sda();</span><br><span style="color: hsl(120, 100%, 40%);">+    else</span><br><span style="color: hsl(120, 100%, 40%);">+          clear_sda();</span><br><span style="color: hsl(120, 100%, 40%);">+  i2c_delay(); // ?</span><br><span style="color: hsl(120, 100%, 40%);">+     set_scl();</span><br><span style="color: hsl(120, 100%, 40%);">+    clear_scl();</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static bool i2c_read_bit(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  bool bit;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   set_sda();</span><br><span style="color: hsl(120, 100%, 40%);">+    set_scl();</span><br><span style="color: hsl(120, 100%, 40%);">+    bit = read_sda();</span><br><span style="color: hsl(120, 100%, 40%);">+     clear_scl();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        return bit;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+bool i2c_write_byte(bool send_start, bool send_stop, uint8_t byte)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       uint8_t bit;</span><br><span style="color: hsl(120, 100%, 40%);">+  bool nack;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  if (send_start)</span><br><span style="color: hsl(120, 100%, 40%);">+               i2c_start_cond();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   for (bit = 0; bit < 8; bit++) {</span><br><span style="color: hsl(120, 100%, 40%);">+            i2c_write_bit((byte & 0x80) != 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                byte <<= 1;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   nack = i2c_read_bit();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      if (send_stop)</span><br><span style="color: hsl(120, 100%, 40%);">+                i2c_stop_cond();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    return nack;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+uint8_t i2c_read_byte(bool nack, bool send_stop)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        uint8_t byte = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     uint8_t bit;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        for (bit = 0; bit < 8; bit++) {</span><br><span style="color: hsl(120, 100%, 40%);">+            byte = (byte << 1) | i2c_read_bit();</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   i2c_write_bit(nack);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        if (send_stop)</span><br><span style="color: hsl(120, 100%, 40%);">+                i2c_stop_cond();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    return byte;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* EEPROM related code */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int eeprom_write_byte(uint8_t slave, uint8_t addr, uint8_t byte)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ bool nack;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  WDT_Restart(WDT);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Write slave address */</span><br><span style="color: hsl(120, 100%, 40%);">+     nack = i2c_write_byte(true, false, slave << 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+        nack = i2c_write_byte(false, false, addr);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+        nack = i2c_write_byte(false, true, byte);</span><br><span style="color: hsl(120, 100%, 40%);">+     if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+        /* Wait tWR time to ensure EEPROM is writing correctly (tWR = 5 ms for AT24C02) */</span><br><span style="color: hsl(120, 100%, 40%);">+    mdelay(5);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+out_stop:</span><br><span style="color: hsl(120, 100%, 40%);">+       i2c_stop_cond();</span><br><span style="color: hsl(120, 100%, 40%);">+      if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    else</span><br><span style="color: hsl(120, 100%, 40%);">+          return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int eeprom_read_byte(uint8_t slave, uint8_t addr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  bool nack;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  WDT_Restart(WDT);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* dummy write cycle */</span><br><span style="color: hsl(120, 100%, 40%);">+       nack = i2c_write_byte(true, false, slave << 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+        nack = i2c_write_byte(false, false, addr);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+        /* Re-start with read */</span><br><span style="color: hsl(120, 100%, 40%);">+      nack = i2c_write_byte(true, false, (slave << 1) | 1);</span><br><span style="color: hsl(120, 100%, 40%);">+   if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      return i2c_read_byte(true, true);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+out_stop:</span><br><span style="color: hsl(120, 100%, 40%);">+        i2c_stop_cond();</span><br><span style="color: hsl(120, 100%, 40%);">+      if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    else</span><br><span style="color: hsl(120, 100%, 40%);">+          return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span>diff --git a/firmware/libboard/octsimtest/source/mcp23017.c b/firmware/libboard/octsimtest/source/mcp23017.c</span><br><span>new file mode 100644</span><br><span>index 0000000..63390b4</span><br><span>--- /dev/null</span><br><span>+++ b/firmware/libboard/octsimtest/source/mcp23017.c</span><br><span>@@ -0,0 +1,106 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#include "board.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include <stdbool.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include "i2c.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "mcp23017.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+//defines from https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library/blob/master/Adafruit_MCP23017.h under BSD license</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+// registers</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_IODIRA 0x00</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_IPOLA 0x02</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_GPINTENA 0x04</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_DEFVALA 0x06</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_INTCONA 0x08</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_IOCONA 0x0A</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_GPPUA 0x0C</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_INTFA 0x0E</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_INTCAPA 0x10</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_GPIOA 0x12</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_OLATA 0x14</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_IODIRB 0x01</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_IPOLB 0x03</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_GPINTENB 0x05</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_DEFVALB 0x07</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_INTCONB 0x09</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_IOCONB 0x0B</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_GPPUB 0x0D</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_INTFB 0x0F</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_INTCAPB 0x11</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_GPIOB 0x13</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_OLATB 0x15</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define MCP23017_INT_ERR 255</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+//bool i2c_write_byte(bool send_start, bool send_stop, uint8_t byte)</span><br><span style="color: hsl(120, 100%, 40%);">+//uint8_t i2c_read_byte(bool nack, bool send_stop)</span><br><span style="color: hsl(120, 100%, 40%);">+//static void i2c_stop_cond(void)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int mcp23017_write_byte(uint8_t slave, uint8_t addr, uint8_t byte)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   bool nack;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  WDT_Restart(WDT);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+//  Write slave address </span><br><span style="color: hsl(120, 100%, 40%);">+ nack = i2c_write_byte(true, false, slave << 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+        nack = i2c_write_byte(false, false, addr);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+        nack = i2c_write_byte(false, true, byte);</span><br><span style="color: hsl(120, 100%, 40%);">+     if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+out_stop:</span><br><span style="color: hsl(120, 100%, 40%);">+   i2c_stop_cond();</span><br><span style="color: hsl(120, 100%, 40%);">+      if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    else</span><br><span style="color: hsl(120, 100%, 40%);">+          return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int mcp23017_read_byte(uint8_t slave, uint8_t addr)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        bool nack;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  WDT_Restart(WDT);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   // dummy write cycle</span><br><span style="color: hsl(120, 100%, 40%);">+  nack = i2c_write_byte(true, false, slave << 1);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+        nack = i2c_write_byte(false, false, addr);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+        // Re-start with read</span><br><span style="color: hsl(120, 100%, 40%);">+ nack = i2c_write_byte(true, false, (slave << 1) | 1);</span><br><span style="color: hsl(120, 100%, 40%);">+   if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             goto out_stop;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      return i2c_read_byte(true, true);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+out_stop:</span><br><span style="color: hsl(120, 100%, 40%);">+        i2c_stop_cond();</span><br><span style="color: hsl(120, 100%, 40%);">+      if (nack)</span><br><span style="color: hsl(120, 100%, 40%);">+             return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    else</span><br><span style="color: hsl(120, 100%, 40%);">+          return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int mcp23017_init(uint8_t slave)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   printf("mcp23017_init\n");</span><br><span style="color: hsl(120, 100%, 40%);">+  // all gpio input</span><br><span style="color: hsl(120, 100%, 40%);">+     if (mcp23017_write_byte(slave, MCP23017_IODIRA, 0xff))</span><br><span style="color: hsl(120, 100%, 40%);">+                return false;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (mcp23017_write_byte(slave, MCP23017_IODIRB, 0xff))</span><br><span style="color: hsl(120, 100%, 40%);">+                return false;</span><br><span style="color: hsl(120, 100%, 40%);">+ printf("mcp23017 found\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return true;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/simtrace2/+/14969">change 14969</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/simtrace2/+/14969"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: simtrace2 </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I01243044002f51b34e8dc12c1b1f565bbf1740a2 </div>
<div style="display:none"> Gerrit-Change-Number: 14969 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: roh <jsteiger@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>