first working prototye of the frequency counter; can count up to 2.69MHy

This commit is contained in:
Miguel Iglesias 2024-10-13 18:06:15 +02:00
parent 7b8585012a
commit c6e7cf9645
9 changed files with 85 additions and 204 deletions

7
.vscode/launch.json vendored
View File

@ -7,6 +7,13 @@
"name": "Launch build_1", "name": "Launch build_1",
"config": "${workspaceFolder}/build_1", "config": "${workspaceFolder}/build_1",
"runToEntryPoint": "main" "runToEntryPoint": "main"
},
{
"type": "nrf-connect",
"request": "launch",
"name": "Launch SensorSoftwareTest/build",
"config": "${workspaceFolder}/build",
"runToEntryPoint": "main"
} }
] ]
} }

View File

@ -21,3 +21,4 @@ target_sources(app PRIVATE main.c)
# Set the build type to Debug # Set the build type to Debug
set(CMAKE_BUILD_TYPE Debug) set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=output.map")

View File

@ -1,6 +0,0 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
config BOARD_E73_TMB
bool "e73-tmb"
depends on SOC_NRF52810_QFAA

View File

@ -1,13 +0,0 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
if BOARD_E73_TMB
config BOARD
default "e73_tmb"
config BT_CTLR
default BT
endif

View File

@ -1,9 +0,0 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
board_runner_args(jlink "--device=nrf52810_xxaa" "--speed=4000")
board_runner_args(pyocd "--target=nrf52810" "--frequency=4000000")
include(${ZEPHYR_BASE}/boards/common/nrfjprog.board.cmake)
include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
include(${ZEPHYR_BASE}/boards/common/pyocd.board.cmake)

View File

@ -1,88 +0,0 @@
// Copyright (c) 2024 Nordic Semiconductor ASA
// SPDX-License-Identifier: Apache-2.0
/dts-v1/;
#include <nordic/nrf52810_qfaa.dtsi>
/ {
model = "e73-tmb";
compatible = "mi,e73-tmb";
chosen {
zephyr,console = &uart0;
zephyr,shell-uart = &uart0;
zephyr,uart-mcumgr = &uart0;
zephyr,sram = &sram0;
zephyr,flash = &flash0;
zephyr,code-partition = &slot0_partition;
};
aliases {
led0 = &myled0;
led1 = &myled1;
};
leds {
compatible = "gpio-leds";
myled0: led_0 {
gpios = <&gpio0 17 GPIO_ACTIVE_LOW>;
};
myled1: led_1 {
gpios = <&gpio0 18 GPIO_ACTIVE_HIGH>;
label = "LED";
};
};
};
&gpio0 {
status = "okay";
};
&gpiote {
status = "okay";
};
&flash0 {
partitions {
compatible = "fixed-partitions";
#address-cells = <1>;
#size-cells = <1>;
boot_partition: partition@0 {
label = "mcuboot";
reg = <0x0 0xc000>;
};
slot0_partition: partition@c000 {
label = "image-0";
reg = <0xc000 0xa000>;
};
slot1_partition: partition@16000 {
label = "image-1";
reg = <0x16000 0xa000>;
};
scratch_partition: partition@20000 {
label = "image-scratch";
reg = <0x20000 0xa000>;
};
storage_partition: partition@2a000 {
label = "storage";
reg = <0x2a000 0x6000>;
};
};
};
&uart0 {
status = "okay";
compatible="nordic,nrf-uart";
pinctrl-0 = <&uart0_default>;
pinctrl-names = "default";
current-speed = <115200>;
};
&pinctrl {
uart0_default: uart0_default {
group1 {
psels = <NRF_PSEL(UART_TX, 0, 6)>, <NRF_PSEL(UART_RX, 0, 8)>;
};
};
};

View File

@ -1,16 +0,0 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
CONFIG_SOC_SERIES_NRF52X=y
CONFIG_SOC_NRF52810_QFAA=y
CONFIG_BOARD_E73_TMB=y
# Enable MPU
CONFIG_ARM_MPU=y
# Enable hardware stack protection
CONFIG_HW_STACK_PROTECTION=y
# Enable debug
CONFIG_CORTEX_M_DEBUG_MONITOR_HOOK=y
CONFIG_SEGGER_DEBUGMON=y

133
main.c
View File

@ -1,54 +1,41 @@
#include <string.h>
#include <stdio.h>
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <zephyr/device.h> #include <zephyr/device.h>
#include <zephyr/drivers/uart.h> #include <zephyr/sys/printk.h>
#include <zephyr/drivers/gpio.h>
#include <nrf_gpio.h> #include <nrf_gpio.h>
#include <nrf_timer.h> #include <nrf_timer.h>
#define TST_CLK_PIN 17 //this is led 0 pin on nrf52-dk /* The devicetree node identifier for the "led0" alias. */
#define FREQ_MEASURE_PIN 11
const struct device *uart0 = DEVICE_DT_GET(DT_NODELABEL(uart0)); #define TST_CLK_PIN 18 //this is led 0 pin on nrf52-dk
#define FREQ_MEASURE_PIN 18
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios);
struct uart_config uart_cfg = { int count = 0;
.baudrate = 9600,
.parity = UART_CFG_PARITY_NONE,
.stop_bits = UART_CFG_STOP_BITS_1,
.flow_ctrl = UART_CFG_FLOW_CTRL_NONE,
.data_bits = UART_CFG_DATA_BITS_8,
};
void send_str(const struct device *uart, char *str) void configure_clock_pin(uint32_t pin_number){
{ nrf_gpio_cfg(
int msg_len = strlen(str); pin_number,
NRF_GPIO_PIN_DIR_OUTPUT,
for (int i = 0; i < msg_len; i++) { NRF_GPIO_PIN_INPUT_CONNECT,
uart_poll_out(uart, str[i]); NRF_GPIO_PIN_NOPULL,
} NRF_GPIO_PIN_S0S1,
NRF_GPIO_PIN_NOSENSE);
printk("Device %s sent: \"%s\"\n", uart->name, str); *(volatile uint32_t *)(NRF_GPIOTE_BASE + 0x600 + (4 * pin_number)) = 1;
} }
void recv_str(const struct device *uart, char *str)
{
char *head = str;
char c;
while (!uart_poll_in(uart, &c)) { void gpio_clock_8m(uint32_t pin_number) {
*head++ = c;
}
*head = '\0';
printk("Device %s received: \"%s\"\n", uart->name, str);
}
void gpio_clock_8m(uint32_t pin_number)
{
NRF_TIMER0->PRESCALER = 0; // 16MHz NRF_TIMER0->PRESCALER = 0; // 16MHz
NRF_TIMER0->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk; NRF_TIMER0->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk;
NRF_TIMER0->CC[0] = 3; //1 for 16/2=8MHz for, 2 for 8/2=4MHz, 4 for 4/2=2MHz etc.. NRF_TIMER0->CC[0] = 0x3; //~1KHz //1 for 16/2=8MHz for, 2 for 8/2=4MHz, 4 for 4/2=2MHz etc..
NRF_TIMER0->BITMODE = (TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos); NRF_TIMER0->BITMODE = (TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos);
//using config [1] since [0] //using config [1] since [0]
NRF_GPIOTE->CONFIG[1] = GPIOTE_CONFIG_MODE_Task | (pin_number << GPIOTE_CONFIG_PSEL_Pos) | NRF_GPIOTE->CONFIG[1] = GPIOTE_CONFIG_MODE_Task | (pin_number << GPIOTE_CONFIG_PSEL_Pos) |
@ -68,12 +55,12 @@ static void timer_init() //gate timer
{ {
NRF_TIMER1->TASKS_STOP = 1; //trigger stop NRF_TIMER1->TASKS_STOP = 1; //trigger stop
NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer; //mode timer NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer; //mode timer
NRF_TIMER1->PRESCALER = 8; // Fhck / 2^8 NRF_TIMER1->PRESCALER = 4; // Fhck / 2^8 -1MHz
//total gate time of timer 62500 - 1s //total gate time of timer 10000 - 100mS
NRF_TIMER1->CC[0] = 62501; //end gate count NRF_TIMER1->CC[0] = 50000; //end gate count
NRF_TIMER1->CC[1] = 1; //start gate count. don't start at 0 to no start on clear. NRF_TIMER1->CC[1] = 1; //start gate count. don't start at 0 to no start on clear.
NRF_TIMER1->BITMODE = (TIMER_BITMODE_BITMODE_16Bit << TIMER_BITMODE_BITMODE_Pos); NRF_TIMER1->BITMODE = (TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos);
NRF_TIMER1->TASKS_CLEAR = 1; //trigger zero timer NRF_TIMER1->TASKS_CLEAR = 1; //trigger zero timer
NRF_TIMER1->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos); //interrupt on end gate count NRF_TIMER1->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos); //interrupt on end gate count
@ -132,57 +119,73 @@ void TIMER1_IRQHandler(void)
{ {
if (NRF_TIMER1->EVENTS_COMPARE[0] != 0) //end gate event if (NRF_TIMER1->EVENTS_COMPARE[0] != 0) //end gate event
{ {
gpio_pin_toggle_dt(&led);
NRF_TIMER1->EVENTS_COMPARE[0] = 0; //reset end gate flag NRF_TIMER1->EVENTS_COMPARE[0] = 0; //reset end gate flag
NRF_TIMER1->EVENTS_COMPARE[1] = 0; //reset start gate flag NRF_TIMER1->EVENTS_COMPARE[1] = 0; //reset start gate flag
NRF_TIMER2->TASKS_CAPTURE[0] = 1; //trigger get counter value NRF_TIMER2->TASKS_CAPTURE[0] = 1; //trigger get counter value
count = NRF_TIMER2->CC[0];
printk("cc: %dHz\n", NRF_TIMER2->CC[0]); //report to serial console
NRF_TIMER1->TASKS_CLEAR = 1; //reset timer NRF_TIMER1->TASKS_CLEAR = 1; //reset timer
NRF_TIMER2->TASKS_CLEAR = 1; //reset counter NRF_TIMER2->TASKS_CLEAR = 1; //reset counter
//NRF_TIMER1->TASKS_STOP = 1; //stop
NRF_TIMER1->TASKS_START = 1; //start next count gate NRF_TIMER1->TASKS_START = 1; //start next count gate
} }
} }
int main(void) int main(void)
{ {
char send_buf[64]; printf("Helloworld! \n");
printk("Hellowold 1!");
/*rc = uart_configure(uart0, &uart_cfg);
if (rc) {
printk("Could not configure device %s", uart0->name);
}*/
snprintf(send_buf, 64, "Helloworld 2");
send_str(uart0, send_buf);
NVIC_EnableIRQ(TIMER1_IRQn); //enable timer1 interrupt
IRQ_DIRECT_CONNECT(TIMER1_IRQn, 3, TIMER1_IRQHandler, 0); //link interrupt flag to handler
NVIC_SetPriority(TIMER1_IRQn, 3); //set interrupt execution priority
struct device *dev;//port container for gpio management
dev = device_get_binding("GPIO_14"); //button SW1
/**/
//nrf_gpio_cfg(dev, FREQ_MEASURE_PIN, NRF_GPIO_DIR_INPUT|GPIO_ACTIVE_HIGH|GPIO_PULL_DOWN); //p0.11 freq in
nrf_gpio_cfg_input(14, NRF_GPIO_PIN_PULLUP); nrf_gpio_cfg_input(14, NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_input(13, NRF_GPIO_PIN_PULLUP);
int ret;
if (!gpio_is_ready_dt(&led)) {
return 0;
}
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
ret = gpio_pin_toggle_dt(&led);
k_msleep(100);
ret = gpio_pin_toggle_dt(&led);
k_msleep(100);
ret = gpio_pin_toggle_dt(&led);
NVIC_ClearPendingIRQ(TIMER1_IRQn);
NVIC_EnableIRQ(TIMER1_IRQn); //enable timer1 interrupt
IRQ_CONNECT(TIMER1_IRQn, 5, TIMER1_IRQHandler, 0, 0); //link interrupt flag to handler
configure_clock_pin(FREQ_MEASURE_PIN);
//init freq count hardware
counter_init(); counter_init();
timer_init(); timer_init();
gpiote_init(FREQ_MEASURE_PIN); gpiote_init(FREQ_MEASURE_PIN); // buttom in SW2
ppi_gpiote_counter_init(); ppi_gpiote_counter_init();
ppi_timer_stop_counter_init(); ppi_timer_stop_counter_init();
ppi_timer_start_counter_init(); ppi_timer_start_counter_init();
//test clock hardware //test clock hardware
gpio_clock_8m(TST_CLK_PIN);
gpio_clock_8m(FREQ_MEASURE_PIN);
//start freq count task //start freq count task
NRF_TIMER1->TASKS_START = 1; NRF_TIMER1->TASKS_START = 1;
gpio_pin_set_dt(&led, 1);
int old_count = 0;
while (1) { while (1) {
//manage loop k_msleep(100);
if(old_count != count){
printf("cc: %dHz\n", count); //report to serial console
old_count = count;
}
} }
} }

View File

@ -1,6 +1,8 @@
CONFIG_NRFX_TIMER0=y #CONFIG_UART_USE_RUNTIME_CONFIGURE=n
CONFIG_NRFX_TIMER1=y #CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_SERIAL=y CONFIG_GPIO=y
CONFIG_UART_USE_RUNTIME_CONFIGURE=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_REBOOT=n
CONFIG_RESET_ON_FATAL_ERROR=n