-
Notifications
You must be signed in to change notification settings - Fork 916
pio_ppm - RC PPM protocol example works on GPIO 3 #414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
elmot
wants to merge
7
commits into
raspberrypi:develop
Choose a base branch
from
elmot:master
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5bc1c3c
pio_ppm - RC PPM protocol example works on GPIO 3
elmot da095f2
pio_ppm - More precise timing
elmot 7a0b42e
pio_ppm - Code polishing
elmot a6e1eac
pio_ppm - Comments polishing
elmot 38d1130
pio_ppm - Fixes after testing on real hardware
elmot 439f5f4
pio_ppm - Comments polishing
elmot 1a70e4a
pio_ppm - Proper synchro gap length
elmot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
add_executable(pio_ppm) | ||
|
||
pico_generate_pio_header(pio_ppm ${CMAKE_CURRENT_LIST_DIR}/ppm.pio) | ||
|
||
target_sources(pio_ppm PRIVATE ppm.c) | ||
|
||
target_link_libraries(pio_ppm PRIVATE pico_stdlib hardware_pio hardware_clocks) | ||
pico_add_extra_outputs(pio_ppm) | ||
|
||
# add url via pico_set_program_url | ||
example_auto_set_url(pio_ppm) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
/* | ||
* 8-channel Radio Control PPM example | ||
* Output on GPIO3 | ||
* | ||
|
||
* synchro | ch0 value 1-2ms | ch1 value 1-2ms |...| ch8 value 1-2ms | | ||
* >=5ms _______ _______ _______ _______ | ||
* _______...__| 0.5ms |_____________| 0.5ms |___________|...| 0.5ms |____________| 0.5ms |_... | ||
* | ||
*/ | ||
#include <stdio.h> | ||
|
||
#include "pico/stdlib.h" | ||
#include "hardware/clocks.h" | ||
#include "hardware/pio.h" | ||
#include "ppm.pio.h" | ||
|
||
void set_value_and_log(uint channel, uint value_usec) { | ||
printf("Channel %d is set to value %5.3f ms\r\n", channel, (float)value_usec / 1000.0f); | ||
ppm_set_value(channel, value_usec); | ||
|
||
} | ||
int main() { | ||
setup_default_uart(); | ||
uint pin = 3; | ||
ppm_program_init(pio0, pin); | ||
while (1) { | ||
set_value_and_log(1, 1100); | ||
set_value_and_log(8, 1800); | ||
sleep_ms(1000); | ||
set_value_and_log(1, 1700); | ||
set_value_and_log(8, 1200); | ||
sleep_ms(1000); | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
; | ||
; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. | ||
; | ||
; SPDX-License-Identifier: BSD-3-Clause | ||
; | ||
|
||
.program ppm | ||
.side_set 1 | ||
|
||
set pindirs, 1 side 0; Set pin to output | ||
.wrap_target | ||
out x, 16 side 0 | ||
delay0: | ||
jmp x-- delay0 side 0 | ||
out x, 16 side 1 | ||
delay1: | ||
jmp x-- delay1 side 1 | ||
.wrap | ||
|
||
% c-sdk { | ||
#include "hardware/pio.h" | ||
#include "hardware/clocks.h" | ||
#include "ppm.pio.h" | ||
|
||
|
||
#define PPM_CHANNELS (8) | ||
struct { | ||
volatile PIO pio; | ||
// 16 MSB - strobe length in microsec | ||
// 16 LSB - gap length in microsec | ||
volatile uint32_t ch_values[PPM_CHANNELS + 1]; | ||
volatile uint value_idx; | ||
} ppm_data; | ||
|
||
void ppm_handler() { | ||
while (!pio_sm_is_tx_fifo_full(ppm_data.pio, 0)) { | ||
pio_sm_put(ppm_data.pio, 0, ppm_data.ch_values[ppm_data.value_idx]); | ||
ppm_data.value_idx = (ppm_data.value_idx + 1) % (PPM_CHANNELS + 1); | ||
} | ||
} | ||
|
||
// 500 microseconds strobe + gap = value (microseconds) | ||
#define BUILD_CH_VALUE(value_us) (((500 - 1) << 16) + (value_us - 500 - 1)) | ||
|
||
static inline void ppm_program_init(PIO pio, uint pin) { | ||
ppm_data.pio = pio == NULL ? pio0 : pio; | ||
ppm_data.ch_values[0] = BUILD_CH_VALUE(5000); | ||
// Neutral position (1500us) by default | ||
for (int i = 1; i <= PPM_CHANNELS; ++i) | ||
ppm_data.ch_values[i] = BUILD_CH_VALUE(1500); | ||
ppm_data.value_idx = 0; | ||
uint offset = pio_add_program(ppm_data.pio, &ppm_program); | ||
pio_gpio_init(ppm_data.pio, pin); | ||
pio_sm_set_consecutive_pindirs(pio, 0, pin, 1, true); | ||
pio_sm_config pio_conf = ppm_program_get_default_config(offset); | ||
sm_config_set_sideset_pins(&pio_conf, pin); | ||
sm_config_set_set_pins(&pio_conf, 0, 1); | ||
sm_config_set_out_shift(&pio_conf, true, true, 0); | ||
sm_config_set_fifo_join(&pio_conf, PIO_FIFO_JOIN_TX); | ||
pio_sm_init(ppm_data.pio, 0, offset, &pio_conf); | ||
pio_sm_set_clkdiv(ppm_data.pio, 0, (float) clock_get_hz(clk_sys) / 1000000); //1MHz | ||
pio_sm_clear_fifos(ppm_data.pio, 0); | ||
pio_sm_restart(ppm_data.pio, 0); | ||
|
||
pio_set_irq0_source_enabled(ppm_data.pio, PIO_INTR_SM0_TXNFULL_LSB, true); | ||
irq_set_exclusive_handler(PIO0_IRQ_0, ppm_handler); | ||
irq_set_enabled(PIO0_IRQ_0, true); | ||
pio_sm_set_enabled(ppm_data.pio, 0, true); | ||
} | ||
|
||
/** Sets channel value | ||
* | ||
* @channel_number RC channel number [1..PPM_CHANNELS] | ||
* @value_usec channel vaue [1000, 2000] | ||
*/ | ||
static inline void ppm_set_value(uint channel_number, uint value_usec) { | ||
valid_params_if(PIO, channel_number <= PPM_CHANNELS); | ||
valid_params_if(PIO, channel_number >= 1); | ||
valid_params_if(PIO, value_usec <= 2000); | ||
valid_params_if(PIO, value_usec >= 1000); | ||
ppm_data.ch_values[channel_number] = BUILD_CH_VALUE(value_usec); | ||
} | ||
%} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.