RP2040-Decoder
Loading...
Searching...
No Matches
shared.h
Go to the documentation of this file.
1
8#pragma once
9#include "string.h"
10#include "pico/float.h"
11#include "pico/stdlib.h"
12#include "pico/multicore.h"
13#include "pico/printf.h"
14#include "hardware/pwm.h"
15#include "hardware/adc.h"
16#include "hardware/flash.h"
17#include "hardware/gpio.h"
18
19
20// here you can enable printf debugging (needs extra soldering and you have to enable uart in the cmake configuration)
21// please note
22// - if it's disabled it will be compiled completely out
23// - be careful that printf debugging can change your timing behavior, for example in the pid controller limit the outputs
24// like already done (only print every 100th iteration for example!)
25// - also note that with logging enabled GPIO0/1 are DISABLED from decoder functions because they are used for uart to the
26// outside world (this is configure automatically for you below!)
27// - IMPORTANT: you have to enable uart in the cmakelists.txt to enable logging!
28#define LOGLEVEL 0
29#define LOG(level, ...) { if(level <= LOGLEVEL) {printf(__VA_ARGS__);}}
30
31// Constant Value of 125 x 10^6
32#define _125M 125000000
33
34// GPIO used for DCC-Signal, Motor PWM or ADC
35#define DCC_INPUT_PIN 21u
36#define MOTOR_FWD_PIN 23u
37#define MOTOR_REV_PIN 22u
38#define FWD_V_EMF_ADC_PIN 28u
39#define REV_V_EMF_ADC_PIN 29u
40
41// if logging is enabled do NOT use gpio0/1 for functions in the decoder
42#if LOGLEVEL != 0
43 // GPIO used directly (GPIO 2-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.)
44 #define GPIO_OUTPUT_PIN_MASK (1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) | (1u<<5) | (1u<<4) | (1u<<3) | (1u<<2)
45#else
46 // GPIO used directly (GPIO 0-5 incl.) as outputs or to switch auxiliary output transistors (GPIO 24-27 incl.)
47 #define GPIO_OUTPUT_PIN_MASK (1u<<24) | (1u<<25) | (1u<<26) | (1u<<27) | (1u<<5) | (1u<<4) | (1u<<3) | (1u<<2) | (1u<<1) | (1u<<0)
48#endif
49
50// GPIO pin mask to prevent setting illegal GPIOs (ADC, Motor, DCC Input Pin)
51#define GPIO_ILLEGAL_MASK (1u<<DCC_INPUT_PIN) | (1u<<MOTOR_FWD_PIN) | (1u<<MOTOR_REV_PIN) | (1u<<FWD_V_EMF_ADC_PIN) | (1u<<REV_V_EMF_ADC_PIN)
52
53// GPIO pin mask with allowed outputs (AUX & GPIO (configured as outputs))
54#define GPIO_ALLOWED_OUTPUTS (GPIO_OUTPUT_PIN_MASK) & ~(GPIO_ILLEGAL_MASK)
55
56// Offset from base address used for saving CV_ARRAY_FLASH
57#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES-FLASH_SECTOR_SIZE)
58
59extern repeating_timer_t pid_control_timer;
60extern repeating_timer_t speed_helper_timer;
61extern const uint8_t *CV_ARRAY_FLASH;
62extern uint8_t speed_step_target;
63extern uint8_t speed_step_target_prev;
64
65
66void core1_entry();
67void write_cv_handler(uint16_t cv_index, uint8_t cv_data);
68float measure(uint8_t total_iterations,
69 uint8_t measurement_delay_us,
70 uint8_t l_side_arr_cutoff,
71 uint8_t r_side_arr_cutoff,
72 bool direction);
73uint32_t get_32bit_CV (uint16_t CV_start_index);
74uint16_t get_16bit_CV (uint16_t CV_start_index);
106bool get_direction_of_speed_step(uint8_t speed_step);
bool get_direction_of_speed_step(uint8_t speed_step)
Calculates the direction based on a speed step byte.
Definition shared.c:67