aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2019-02-28 18:24:36 +0100
committerHarald Welte <laforge@gnumonks.org>2019-02-28 20:05:59 +0100
commitbb9b0dc8e8cec8988a179a6a6105b6f66a759801 (patch)
tree66a4d72dd2384fb74cbb53e691703e1164c2d53d
parentb7e326cad3638f39fb127f5c4dbc357c03a8a6e6 (diff)
Add freq_ctr app
The freq_ctr app is a small application that is implementing a simplistic direct-mode frequency counter using the internal 32.768kHz oscillator and two TC blocks. One of them is used to generate a 1Hz signal, which is then subsequently used by the other TC to trigger a counter read after exactly 1s. This is in itself not something useful on a simtrace2 device. However, it is a separate 'app' and I prefer to have the code here in master over some obscure branch that's easy to forget about. Change-Id: I2249bfb8dd6a88d85d406f3b33537377133d0939
-rw-r--r--firmware/apps/freq_ctr/Makefile3
-rw-r--r--firmware/apps/freq_ctr/freq_ctr.c55
-rw-r--r--firmware/apps/freq_ctr/main.c54
-rw-r--r--firmware/apps/freq_ctr/usb_strings.txt10
4 files changed, 122 insertions, 0 deletions
diff --git a/firmware/apps/freq_ctr/Makefile b/firmware/apps/freq_ctr/Makefile
new file mode 100644
index 0000000..1097e9c
--- /dev/null
+++ b/firmware/apps/freq_ctr/Makefile
@@ -0,0 +1,3 @@
+C_FILES += $(C_LIBUSB_RT)
+
+C_FILES += freq_ctr.c
diff --git a/firmware/apps/freq_ctr/freq_ctr.c b/firmware/apps/freq_ctr/freq_ctr.c
new file mode 100644
index 0000000..ad6497c
--- /dev/null
+++ b/firmware/apps/freq_ctr/freq_ctr.c
@@ -0,0 +1,55 @@
+#include <stdint.h>
+#include "utils.h"
+#include "tc_etu.h"
+#include "chip.h"
+
+
+/* pins for Channel 0 of TC-block 0 */
+#define PIN_TIOA0 {PIO_PA0, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT}
+
+/* pins for Channel 1 of TC-block 0 */
+#define PIN_TIOA1 {PIO_PA15, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT}
+#define PIN_TCLK1 {PIO_PA28, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT}
+
+static const Pin pins_tc[] = { PIN_TIOA0, PIN_TIOA1, PIN_TCLK1 };
+
+static TcChannel *tc1 = &TC0->TC_CHANNEL[1];
+
+void TC1_IrqHandler(void)
+{
+ uint32_t sr = tc1->TC_SR;
+ printf("TC1=%lu; SR=0x%08lx\r\n", tc1->TC_RA, sr);
+}
+
+void freq_ctr_init(void)
+{
+ TcChannel *tc0 = &TC0->TC_CHANNEL[0];
+
+ PIO_Configure(pins_tc, ARRAY_SIZE(pins_tc));
+
+ PMC_EnablePeripheral(ID_TC0);
+ PMC_EnablePeripheral(ID_TC1);
+
+ /* route TCLK1 to XC1 */
+ TC0->TC_BMR &= ~TC_BMR_TC1XC1S_Msk;
+ TC0->TC_BMR |= TC_BMR_TC1XC1S_TCLK1;
+
+ /* TC0 in wveform mode: Run from SCLK. Raise TIOA on RA; lower TIOA on RC + trigger */
+ tc0->TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK5 | TC_CMR_BURST_NONE |
+ TC_CMR_EEVTEDG_NONE | TC_CMR_WAVSEL_UP_RC | TC_CMR_WAVE |
+ TC_CMR_ACPA_SET | TC_CMR_ACPC_CLEAR;
+ tc0->TC_RA = 16384; /* set high at 16384 */
+ tc0->TC_RC = 32786; /* set low at 32786 */
+
+ /* TC1 in capture mode: Run from XC1. Trigger on TIOA rising. Load RA on rising */
+ tc1->TC_CMR = TC_CMR_TCCLKS_XC1 | TC_CMR_BURST_NONE |
+ TC_CMR_ETRGEDG_RISING | TC_CMR_ABETRG | TC_CMR_LDRA_RISING;
+ /* Interrupt us if the external trigger happens */
+ tc1->TC_IER = TC_IER_ETRGS;
+ NVIC_EnableIRQ(TC1_IRQn);
+
+ TC0->TC_BCR = TC_BCR_SYNC;
+
+ tc0->TC_CCR = TC_CCR_CLKEN|TC_CCR_SWTRG;
+ tc1->TC_CCR = TC_CCR_CLKEN|TC_CCR_SWTRG;
+}
diff --git a/firmware/apps/freq_ctr/main.c b/firmware/apps/freq_ctr/main.c
new file mode 100644
index 0000000..761bc17
--- /dev/null
+++ b/firmware/apps/freq_ctr/main.c
@@ -0,0 +1,54 @@
+
+#include "board.h"
+#include "utils.h"
+#include "osmocom/core/timer.h"
+
+extern void freq_ctr_init(void);
+
+/* returns '1' in case we should break any endless loop */
+static void check_exec_dbg_cmd(void)
+{
+ int ch;
+
+ if (!UART_IsRxReady())
+ return;
+
+ ch = UART_GetChar();
+
+ board_exec_dbg_cmd(ch);
+}
+
+
+extern int main(void)
+{
+ led_init();
+ led_blink(LED_RED, BLINK_ALWAYS_ON);
+ led_blink(LED_GREEN, BLINK_ALWAYS_ON);
+
+ /* Enable watchdog for 2000 ms, with no window */
+ WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
+ (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000));
+
+ PIO_InitializeInterrupts(0);
+
+
+ printf("\n\r\n\r"
+ "=============================================================================\n\r"
+ "Freq Ctr firmware " GIT_VERSION " (C) 2019 by Harald Welte\n\r"
+ "=============================================================================\n\r");
+
+ board_main_top();
+
+ TRACE_INFO("starting frequency counter...\n\r");
+ freq_ctr_init();
+
+ TRACE_INFO("entering main loop...\n\r");
+ while (1) {
+ WDT_Restart(WDT);
+
+ check_exec_dbg_cmd();
+ osmo_timers_prepare();
+ osmo_timers_update();
+ }
+
+}
diff --git a/firmware/apps/freq_ctr/usb_strings.txt b/firmware/apps/freq_ctr/usb_strings.txt
new file mode 100644
index 0000000..0e797ac
--- /dev/null
+++ b/firmware/apps/freq_ctr/usb_strings.txt
@@ -0,0 +1,10 @@
+sysmocom - s.f.m.c. GmbH
+SIMtrace 2 compatible device
+SIMtrace Sniffer
+SIMtrace CCID
+SIMtrace Phone
+SIMtrace MITM
+CardEmulator Modem 1
+CardEmulator Modem 2
+CardEmulator Modem 3
+CardEmulator Modem 4