~repos /rp2350

#zig#raspberry-pi

git clone https://pyrossh.dev/repos/rp2350.git

code to drive rp2350


7bbd2d1f pyrossh

7 months ago
get screen working
Files changed (2) hide show
  1. main.js +102 -9
  2. src/main.zig +72 -56
main.js CHANGED
@@ -1,13 +1,106 @@
1
- pinMode(0, OUTPUT);
1
+ const { GPIO } = require('gpio');
2
- pinMode(1, INPUT_PULLUP);
2
+ const { SPI } = require('spi');
3
- pinMode(26, INPUT)
4
3
 
4
+ const ssr = new GPIO(0, OUTPUT);
5
+ const button = new GPIO(1, INPUT_PULLUP);
6
+ // pinMode(26, INPUT) // analogRead(26)
7
+ const csn = new GPIO(17, OUTPUT);
8
+ const dc = new GPIO(20, OUTPUT);
9
+ const rst = new GPIO(21, OUTPUT);
5
- attachInterrupt(1, (pin, mode) => {
10
+ const spi0 = new SPI(0, {
11
+ mode: SPI.MODE_0,
12
+ baudrate: 800000,
13
+ bitorder: SPI.MSB,
14
+ });
15
+
16
+ // command, data, delay
17
+ const initCommands = [
18
+ [0x01, [], 150], // Software reset
19
+ // .[ 0xEF, [ 0x03, 0x80, 0x02 ] },
20
+ [0xCF, [0x00, 0xC1, 0x30]], // Pwr ctrl B
21
+ [0xED, [0x64, 0x03, 0x12, 0x81]], // # Pwr on seq. ctrl
22
+ [0xE8, [0x85, 0x00, 0x78]], // Driver timing ctrl A
23
+ [0xCB, [0x39, 0x2C, 0x00, 0x34, 0x02]], // Pwr ctrl A
24
+ [0xF7, [0x20]], // Pump ratio control
25
+ [0xEA, [0x00, 0x00]], // Driver timing ctrl B
26
+ [0xC0, [0x23]], // Pwr ctrl 1
27
+ [0xC1, [0x10]], // Pwr ctrl 2
28
+ [0xC5, [0x3E, 0x28]], // VCM control1
29
+ [0xC7, [0x86]], // VCM control2
30
+ [0x36, [0x80]], // Memory Access Control
31
+ [0x37, [0x00]], // Vertical scroll zero
32
+ [0x3A, [0x55]], // COLMOD: Pixel Format Set
33
+ [0xB1, [0x00, 0x18]], // Frame Rate Control (In Normal Mode/Full Colors)
34
+ [0xB6, [0x08, 0x82, 0x27]], // Display Function Control
35
+ [0xF2, [0x00]], // 3Gamma Function Disable
36
+ [0x26, [0x01]], // Gamma curve selected
37
+ // .[ 0xE0, [ 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00 ] ], // Set Gamma Positive Gamma Correction
38
+ // .[ 0xE1, [ 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F ] ], // Set Gamma Negative Gamma Correction
39
+ [0x11, [], 150], // Exit Sleep/Sleep Out
40
+ [0x29, [], 150], // Display
41
+ ]
42
+
6
- console.log(analogRead(26));
43
+ function sendCommand(cmd, data) {
44
+ dc.write(LOW);
45
+ csn.write(LOW);
46
+ spi0.send(new Uint8Array([cmd]));
47
+ csn.write(HIGH);
7
- if (digitalRead(1) === LOW) {
48
+ if (data.length > 0) {
8
- digitalWrite(0, HIGH)
49
+ dc.write(HIGH);
50
+ csn.write(LOW);
51
+ spi0.send(data);
52
+ csn.write(HIGH);
53
+ }
54
+ }
55
+
56
+ function block(x0, y0, x1, y1, data) {
57
+ sendCommand(0x2A, [x0 >> 8, x0 & 0xFF, x1 >> 8, x1 & 0xFF]); // Column address set
58
+ sendCommand(0x2B, [y0 >> 8, y0 & 0xFF, y1 >> 8, y1 & 0xFF]); // Page address set
59
+ sendCommand(0x2C, data); // Memory write
60
+ }
61
+
62
+ function hardware_reset() {
63
+ rst.write(0);
64
+ delay(50);
65
+ rst.write(1);
66
+ delay(50);
67
+ }
68
+
69
+ function cleanup() {
70
+ clear()
71
+ // self.display_off()
72
+ // self.spi.deinit()
73
+ }
74
+
75
+
76
+ const width = 240;
77
+ const height = 320;
78
+ function clear(color) {
79
+ for (var y = 0; y < height; y += 8) {
80
+ block(0, y, width, y + 8 - 1, new Uint8Array(width * 2 * 8).fill(color));
81
+ }
82
+ }
83
+
84
+ delay(100);
85
+ csn.write(1);
86
+ dc.write(0);
87
+ rst.write(1);
88
+ delay(50);
89
+ hardware_reset();
90
+
91
+ for (const init of initCommands) {
92
+ sendCommand(init[0], init[1]);
93
+ if (init[2]) {
94
+ delay(init[2]);
9
95
  }
96
+ }
97
+
98
+ button.irq((pin, mode) => {
10
- if (digitalRead(1) === HIGH) {
99
+ if (button.read() === LOW) {
100
+ ssr.write(HIGH);
101
+ clear(0x00);
102
+ } else {
11
- digitalWrite(0, LOW)
103
+ ssr.write(LOW);
104
+ clear(0xA0);
12
105
  }
13
106
  }, CHANGE);
src/main.zig CHANGED
@@ -23,42 +23,31 @@ const Command = struct {
23
23
  data: []const u8,
24
24
  delay_ms: ?u32 = null,
25
25
  };
26
- // MIRROR_ROTATE = { # MADCTL configurations for rotation and mirroring
27
- // (False, 0): 0x80, # 1000 0000
28
- // (False, 90): 0xE0, # 1110 0000
29
- // (False, 180): 0x40, # 0100 0000
30
- // (False, 270): 0x20, # 0010 0000
31
- // (True, 0): 0xC0, # 1100 0000
32
- // (True, 90): 0x60, # 0110 0000
33
- // (True, 180): 0x00, # 0000 0000
34
- // (True, 270): 0xA0, # 1010 0000
35
- // }
36
- // self.write_cmd(self.MADCTL, self.rotation) # Memory access ctrl
37
26
 
38
27
  const initDisplayCommands: []const Command = &.{
39
- Command{ .code = 0x01, .data = &.{}, .delay_ms = 150 }, // Software reset
28
+ .{ .code = 0x01, .data = &.{}, .delay_ms = 150 }, // Software reset
40
- Command{ .code = 0xEF, .data = &.{ 0x03, 0x80, 0x02 } },
29
+ // .{ .code = 0xEF, .data = &.{ 0x03, 0x80, 0x02 } },
41
- Command{ .code = 0xCF, .data = &.{ 0x00, 0xC1, 0x30 } }, // Pwr ctrl B
30
+ .{ .code = 0xCF, .data = &.{ 0x00, 0xC1, 0x30 } }, // Pwr ctrl B
42
- Command{ .code = 0xED, .data = &.{ 0x64, 0x03, 0x12, 0x81 } }, // # Pwr on seq. ctrl
31
+ .{ .code = 0xED, .data = &.{ 0x64, 0x03, 0x12, 0x81 } }, // # Pwr on seq. ctrl
43
- Command{ .code = 0xE8, .data = &.{ 0x85, 0x00, 0x78 } }, // Driver timing ctrl A
32
+ .{ .code = 0xE8, .data = &.{ 0x85, 0x00, 0x78 } }, // Driver timing ctrl A
44
- Command{ .code = 0xCB, .data = &.{ 0x39, 0x2C, 0x00, 0x34, 0x02 } }, // Pwr ctrl A
33
+ .{ .code = 0xCB, .data = &.{ 0x39, 0x2C, 0x00, 0x34, 0x02 } }, // Pwr ctrl A
45
- Command{ .code = 0xF7, .data = &.{0x20} }, // Pump ratio control
34
+ .{ .code = 0xF7, .data = &.{0x20} }, // Pump ratio control
46
- Command{ .code = 0xEA, .data = &.{ 0x00, 0x00 } }, // Driver timing ctrl B
35
+ .{ .code = 0xEA, .data = &.{ 0x00, 0x00 } }, // Driver timing ctrl B
47
- Command{ .code = 0xC0, .data = &.{0x23} }, // Pwr ctrl 1
36
+ .{ .code = 0xC0, .data = &.{0x23} }, // Pwr ctrl 1
48
- Command{ .code = 0xC1, .data = &.{0x10} }, // Pwr ctrl 2
37
+ .{ .code = 0xC1, .data = &.{0x10} }, // Pwr ctrl 2
49
- Command{ .code = 0xC5, .data = &.{ 0x3E, 0x28 } }, // VCM control1
38
+ .{ .code = 0xC5, .data = &.{ 0x3E, 0x28 } }, // VCM control1
50
- Command{ .code = 0xC7, .data = &.{0x86} }, // VCM control2
39
+ .{ .code = 0xC7, .data = &.{0x86} }, // VCM control2
51
- Command{ .code = 0x36, .data = &.{0x48} }, // Memory Access Control
40
+ .{ .code = 0x36, .data = &.{0x80} }, // Memory Access Control
52
- Command{ .code = 0x37, .data = &.{0x00} }, // Vertical scroll zero
41
+ .{ .code = 0x37, .data = &.{0x00} }, // Vertical scroll zero
53
- Command{ .code = 0x3A, .data = &.{0x55} }, // COLMOD: Pixel Format Set
42
+ .{ .code = 0x3A, .data = &.{0x55} }, // COLMOD: Pixel Format Set
54
- Command{ .code = 0xB1, .data = &.{ 0x00, 0x18 } }, // Frame Rate Control (In Normal Mode/Full Colors)
43
+ .{ .code = 0xB1, .data = &.{ 0x00, 0x18 } }, // Frame Rate Control (In Normal Mode/Full Colors)
55
- Command{ .code = 0xB6, .data = &.{ 0x08, 0x82, 0x27 } }, // Display Function Control
44
+ .{ .code = 0xB6, .data = &.{ 0x08, 0x82, 0x27 } }, // Display Function Control
56
- Command{ .code = 0xF2, .data = &.{0x00} }, // 3Gamma Function Disable
45
+ .{ .code = 0xF2, .data = &.{0x00} }, // 3Gamma Function Disable
57
- Command{ .code = 0x26, .data = &.{0x01} }, // Gamma curve selected
46
+ .{ .code = 0x26, .data = &.{0x01} }, // Gamma curve selected
58
- // Command{ .code = 0xE0, .data = &.{ 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00 } }, // Set Gamma Positive Gamma Correction
47
+ // .{ .code = 0xE0, .data = &.{ 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00 } }, // Set Gamma Positive Gamma Correction
59
- // Command{ .code = 0xE1, .data = &.{ 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F } }, // Set Gamma Negative Gamma Correction
48
+ // .{ .code = 0xE1, .data = &.{ 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F } }, // Set Gamma Negative Gamma Correction
60
- Command{ .code = 0x11, .data = &.{}, .delay_ms = 150 }, // Exit Sleep/Sleep Out
49
+ .{ .code = 0x11, .data = &.{}, .delay_ms = 150 }, // Exit Sleep/Sleep Out
61
- Command{ .code = 0x29, .data = &.{}, .delay_ms = 150 }, // Display on
50
+ .{ .code = 0x29, .data = &.{}, .delay_ms = 150 }, // Display on
62
51
  };
63
52
 
64
53
  fn send(self: Command) void {
@@ -86,25 +75,29 @@ fn sendData(data: []const u8) void {
86
75
  // x1 (int): Ending X position.
87
76
  // y1 (int): Ending Y position.
88
77
  // data (bytes): Data buffer to write.
89
- fn block(x0: u8, y0: u8, x1: u8, y1: u8) void {
78
+ fn block(x0: u16, y0: u16, x1: u16, y1: u16, data: []const u8) void {
90
- send(Command{ .code = 0x2A, .data = &.{ x0 >> 8, x0 & 0xFF, x1 >> 8, x1 & 0xFF } }); // Column address set
79
+ send(Command{ .code = 0x2A, .data = &.{ @intCast(x0 >> 8), @truncate(x0), @intCast(x1 >> 8), @truncate(x1) } }); // Column address set
91
- send(Command{ .code = 0x2B, .data = &.{ y0 >> 8, y0 & 0xFF, y1 >> 8, y1 & 0xFF } }); // Page address set
80
+ send(Command{ .code = 0x2B, .data = &.{ @intCast(y0 >> 8), @truncate(y0), @intCast(y1 >> 8), @truncate(y1) } }); // Page address set
92
- send(Command{ .code = 0x2C, .data = &{} }); // Memory write
81
+ send(Command{ .code = 0x2C, .data = data });
93
82
  }
94
83
 
95
- fn clear() void {
96
- var buffer: [][]u16 = &.{};
97
- for (0..320 - 1) |i| {
84
+ fn color565(r: u32, g: u32, b: u32) u16 {
98
- buffer[i] = &.{};
99
- for (0..240 - 1) |j| {
85
+ return (r & 0xF8) << 8 | (g & 0xFC) << 3 | b >> 3;
100
- buffer[i][j] = 0xFFFF;
101
- }
86
+ }
87
+
88
+ const width = 480;
89
+ const height = 320;
90
+ fn clear(color: u8) void {
91
+ // mem.zeroes([4096]u8);
92
+ var buffer: [width]u8 = undefined;
93
+ var i: usize = 0;
94
+ while (i < buffer.len) : (i += 1) {
95
+ buffer[i] = color;
96
+ }
97
+ var y: u16 = 0;
98
+ while (y < height) : (y += 1) {
99
+ block(0, y, width - 1, y + 8 - 1, &buffer);
102
100
  }
103
- block(0, 0, 320 - 1, 240 - 1, &.{});
104
- dc.put(1);
105
- csn.put(0);
106
- SPI0.writev_blocking(u16, buffer);
107
- csn.put(1);
108
101
  }
109
102
 
110
103
  fn hardware_reset() void {
@@ -115,26 +108,25 @@ fn hardware_reset() void {
115
108
  }
116
109
 
117
110
  pub fn main() !void {
118
- inline for (&.{ csn, mosi, sck }) |pin| {
111
+ inline for (&.{ miso, mosi, sck }) |pin| {
119
112
  pin.set_function(.spi);
120
113
  }
121
114
  ssr.set_function(.sio);
122
115
  ssr.set_direction(.out);
123
116
  csn.set_function(.sio);
124
117
  csn.set_direction(.out);
125
- csn.put(1);
126
118
  dc.set_function(.sio);
127
119
  dc.set_direction(.out);
128
- dc.put(0);
129
120
  rst.set_function(.sio);
130
121
  rst.set_direction(.out);
131
- rst.put(1);
132
122
  button.set_function(.sio);
133
123
  button.set_direction(.in);
134
124
  button.set_pull(.up);
135
125
 
136
- try SPI0.apply(.{ .clock_config = rp2xxx.clock_config });
126
+ try SPI0.apply(.{ .clock_config = rp2xxx.clock_config, .baud_rate = 40_000_000 });
137
-
127
+ csn.put(1);
128
+ dc.put(0);
129
+ rst.put(1);
138
130
  hardware_reset();
139
131
  for (initDisplayCommands) |cmd| {
140
132
  send(cmd);
@@ -142,14 +134,38 @@ pub fn main() !void {
142
134
  time.sleep_ms(delay_ms);
143
135
  }
144
136
  }
137
+ clear(0x00);
138
+ var ascon = rp2xxx.rand.Ascon.init();
139
+ var rng = ascon.random();
140
+ var set: u1 = 0;
145
141
 
146
142
  while (true) {
147
143
  const a = button.read();
148
144
  if (a == 0) {
149
145
  ssr.put(1);
146
+ if (set != 1) {
147
+ clear(rng.int(u8));
148
+ }
149
+ set = 1;
150
150
  } else {
151
151
  ssr.put(0);
152
+ set = 0;
152
153
  }
153
154
  time.sleep_ms(250);
154
155
  }
155
156
  }
157
+
158
+ // const expect = std.testing.expect;
159
+
160
+ // fn data2(v: u8) u8 {
161
+ // return v;
162
+ // }
163
+
164
+ // test "truncate u16 to u6" {
165
+ // const a: u16 = 320;
166
+ // try expect(data2(@truncate(a)) == 64);
167
+ // try expect(320 & 0xFF == 64);
168
+ // try expect(320 >> 8 == 1);
169
+
170
+ // try expect(data2(@intCast(a >> 8)) == 1);
171
+ // }