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