aboutsummaryrefslogtreecommitdiff
path: root/embassy-rp/src/pio/instr.rs
blob: 304ddb20a500f99cbb6bda87fceeb8d4f28a9d54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! Instructions controlling the PIO.
use pio::{InSource, InstructionOperands, JmpCondition, OutDestination, SetDestination};

use crate::pio::{Instance, StateMachine};

impl<'d, PIO: Instance, const SM: usize> StateMachine<'d, PIO, SM> {
    /// Set value of scratch register X.
    ///
    /// SAFETY: autopull enabled else it will write undefined data.
    /// Make sure to have setup the according configuration see
    /// [shift_out](crate::pio::Config::shift_out) and [auto_fill](crate::pio::ShiftConfig::auto_fill).
    pub unsafe fn set_x(&mut self, value: u32) {
        const OUT: u16 = InstructionOperands::OUT {
            destination: OutDestination::X,
            bit_count: 32,
        }
        .encode();
        self.tx().push(value);
        self.exec_instr(OUT);
    }

    /// Get value of scratch register X.
    ///
    /// SAFETY: autopush enabled else it will read undefined data.
    /// Make sure to have setup the according configurations see
    /// [shift_in](crate::pio::Config::shift_in) and [auto_fill](crate::pio::ShiftConfig::auto_fill).
    pub unsafe fn get_x(&mut self) -> u32 {
        const IN: u16 = InstructionOperands::IN {
            source: InSource::X,
            bit_count: 32,
        }
        .encode();
        self.exec_instr(IN);
        self.rx().pull()
    }

    /// Set value of scratch register Y.
    ///
    /// SAFETY: autopull enabled else it will write undefined data.
    /// Make sure to have setup the according configuration see
    /// [shift_out](crate::pio::Config::shift_out) and [auto_fill](crate::pio::ShiftConfig::auto_fill).
    pub unsafe fn set_y(&mut self, value: u32) {
        const OUT: u16 = InstructionOperands::OUT {
            destination: OutDestination::Y,
            bit_count: 32,
        }
        .encode();
        self.tx().push(value);
        self.exec_instr(OUT);
    }

    /// Get value of scratch register Y.
    ///
    /// SAFETY: autopush enabled else it will read undefined data.
    /// Make sure to have setup the according configurations see
    /// [shift_in](crate::pio::Config::shift_in) and [auto_fill](crate::pio::ShiftConfig::auto_fill).
    pub unsafe fn get_y(&mut self) -> u32 {
        const IN: u16 = InstructionOperands::IN {
            source: InSource::Y,
            bit_count: 32,
        }
        .encode();
        self.exec_instr(IN);

        self.rx().pull()
    }

    /// Set instruction for pindir destination.
    pub unsafe fn set_pindir(&mut self, data: u8) {
        let set: u16 = InstructionOperands::SET {
            destination: SetDestination::PINDIRS,
            data,
        }
        .encode();
        self.exec_instr(set);
    }

    /// Set instruction for pin destination.
    pub unsafe fn set_pin(&mut self, data: u8) {
        let set: u16 = InstructionOperands::SET {
            destination: SetDestination::PINS,
            data,
        }
        .encode();
        self.exec_instr(set);
    }

    /// Out instruction for pin destination.
    pub unsafe fn set_out_pin(&mut self, data: u32) {
        const OUT: u16 = InstructionOperands::OUT {
            destination: OutDestination::PINS,
            bit_count: 32,
        }
        .encode();
        self.tx().push(data);
        self.exec_instr(OUT);
    }

    /// Out instruction for pindir destination.
    pub unsafe fn set_out_pindir(&mut self, data: u32) {
        const OUT: u16 = InstructionOperands::OUT {
            destination: OutDestination::PINDIRS,
            bit_count: 32,
        }
        .encode();
        self.tx().push(data);
        self.exec_instr(OUT);
    }

    /// Jump instruction to address.
    pub unsafe fn exec_jmp(&mut self, to_addr: u8) {
        let jmp: u16 = InstructionOperands::JMP {
            address: to_addr,
            condition: JmpCondition::Always,
        }
        .encode();
        self.exec_instr(jmp);
    }
}