Skip to content

Commit fad5ff0

Browse files
committed
Documentation and API cleanup
1 parent 4096fb0 commit fad5ff0

File tree

4 files changed

+50
-132
lines changed

4 files changed

+50
-132
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
2+
name = "st7920"
3+
description = "SPI driver for the ST7920 LCD display controller"
24
authors = ["Wojciech Jakóbczyk <[email protected]>"]
35
categories = ["embedded", "no-std"]
4-
description = "SPI driver for the ST7920 LCD display controller"
56
documentation = "https://docs.rs/st7920"
67
exclude = [".gitignore"]
78
keywords = ["no-std", "st7920", "lcd", "embedded", "embedded-hal-driver"]
89
license = "MIT"
9-
name = "st7920"
1010
readme = "README.md"
1111
repository = "https://github.com/wjakobczyk/st7920"
1212
version = "0.1.0"

README.md

Lines changed: 16 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,30 @@
1-
# `cortex-m-quickstart`
1+
# `ST7920`
22

3-
> A template for building applications for ARM Cortex-M microcontrollers
3+
This is a Rust driver library for LCD displays using the [ST7920] controller. It supports graphics mode of the controller, 128x64 in 1bpp. SPI connection to MCU is supported.
44

5-
This project is developed and maintained by the [Cortex-M team][team].
5+
It implements [embedded-graphics] driver API.
66

7-
## Dependencies
7+
It is platform independent as it uses [embedded-hal] APIs to access hardware.
88

9-
To build embedded programs using this template you'll need:
9+
The examples are based on the [stm32f4xx_hal] implementation of embedded-hal.
1010

11-
- Rust 1.31, 1.30-beta, nightly-2018-09-13 or a newer toolchain. e.g. `rustup
12-
default beta`
1311

14-
- The `cargo generate` subcommand. [Installation
15-
instructions](https://github.com/ashleygwilliams/cargo-generate#installation).
1612

17-
- `rust-std` components (pre-compiled `core` crate) for the ARM Cortex-M
18-
targets. Run:
13+
# Documentation
1914

20-
``` console
21-
$ rustup target add thumbv6m-none-eabi thumbv7m-none-eabi thumbv7em-none-eabi thumbv7em-none-eabihf
22-
```
15+
See [examples].
2316

24-
## Using this template
17+
The controller supports 1 bit-per-pixel displays, so an off-screen buffer has to be used to provide random access to pixels.
18+
Size of the buffer is 1024 bytes.
2519

26-
**NOTE**: This is the very short version that only covers building programs. For
27-
the long version, which additionally covers flashing, running and debugging
28-
programs, check [the embedded Rust book][book].
29-
30-
[book]: https://rust-embedded.github.io/book
31-
32-
0. Before we begin you need to identify some characteristics of the target
33-
device as these will be used to configure the project:
34-
35-
- The ARM core. e.g. Cortex-M3.
36-
37-
- Does the ARM core include an FPU? Cortex-M4**F** and Cortex-M7**F** cores do.
38-
39-
- How much Flash memory and RAM does the target device has? e.g. 256 KiB of
40-
Flash and 32 KiB of RAM.
41-
42-
- Where are Flash memory and RAM mapped in the address space? e.g. RAM is
43-
commonly located at address `0x2000_0000`.
44-
45-
You can find this information in the data sheet or the reference manual of your
46-
device.
47-
48-
In this example we'll be using the STM32F3DISCOVERY. This board contains an
49-
STM32F303VCT6 microcontroller. This microcontroller has:
50-
51-
- A Cortex-M4F core that includes a single precision FPU
52-
53-
- 256 KiB of Flash located at address 0x0800_0000.
54-
55-
- 40 KiB of RAM located at address 0x2000_0000. (There's another RAM region but
56-
for simplicity we'll ignore it).
57-
58-
1. Instantiate the template.
59-
60-
``` console
61-
$ cargo generate --git https://github.com/rust-embedded/cortex-m-quickstart
62-
Project Name: app
63-
Creating project called `app`...
64-
Done! New project created /tmp/app
65-
66-
$ cd app
67-
```
68-
69-
2. Set a default compilation target. There are four options as mentioned at the
70-
bottom of `.cargo/config`. For the STM32F303VCT6, which has a Cortex-M4F
71-
core, we'll pick the `thumbv7em-none-eabihf` target.
72-
73-
``` console
74-
$ tail -n6 .cargo/config
75-
```
76-
77-
``` toml
78-
[build]
79-
# Pick ONE of these compilation targets
80-
# target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
81-
# target = "thumbv7m-none-eabi" # Cortex-M3
82-
# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU)
83-
target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
84-
```
85-
86-
3. Enter the memory region information into the `memory.x` file.
87-
88-
``` console
89-
$ cat memory.x
90-
/* Linker script for the STM32F303VCT6 */
91-
MEMORY
92-
{
93-
/* NOTE 1 K = 1 KiBi = 1024 bytes */
94-
FLASH : ORIGIN = 0x08000000, LENGTH = 256K
95-
RAM : ORIGIN = 0x20000000, LENGTH = 40K
96-
}
97-
```
98-
99-
4. Build the template application or one of the examples.
100-
101-
``` console
102-
$ cargo build
103-
```
20+
The buffer has to be flushed to update the display after a group of draw calls has been completed. The flush is not part of embedded-graphics API.
10421

10522
# License
10623

107-
This template is licensed under either of
108-
109-
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
110-
http://www.apache.org/licenses/LICENSE-2.0)
111-
112-
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
113-
114-
at your option.
115-
116-
## Contribution
117-
118-
Unless you explicitly state otherwise, any contribution intentionally submitted
119-
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
120-
dual licensed as above, without any additional terms or conditions.
121-
122-
## Code of Conduct
123-
124-
Contribution to this crate is organized under the terms of the [Rust Code of
125-
Conduct][CoC], the maintainer of this crate, the [Cortex-M team][team], promises
126-
to intervene to uphold that code of conduct.
24+
This library is licensed under MIT license ([LICENSE](LICENSE) or http://opensource.org/licenses/MIT)
12725

128-
[CoC]: https://www.rust-lang.org/policies/code-of-conduct
129-
[team]: https://github.com/rust-embedded/wg#the-cortex-m-team
26+
[embedded-graphics]: https://docs.rs/embedded-graphics/0.6.0-alpha.2/embedded_graphics/
27+
[embedded-hal]: https://docs.rs/embedded-hal/0.2.3/embedded_hal/
28+
[stm32f4xx_hal]: https://docs.rs/stm32f4xx-hal/0.5.0/stm32f4xx_hal/
29+
[examples]: https://github.com/wjakobczyk/st7920/tree/master/examples
30+
[ST7920]: https://www.lcd-module.de/eng/pdf/zubehoer/st7920_chinese.pdf

examples/graphics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn main() -> ! {
6060
.into_iter(),
6161
);
6262

63-
disp.flush_range(15, 15, 14, 31)
63+
disp.flush_region(15, 15, 14, 31)
6464
.expect("could not flush display");
6565
}
6666

src/lib.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
//! # ST7920
2+
//!
3+
//! This is a Rust driver library for LCD displays using the [ST7920] controller.
4+
//!
5+
//! It supports graphics mode of the controller, 128x64 in 1bpp. SPI connection to MCU is supported.
6+
//!
7+
//! The controller supports 1 bit-per-pixel displays, so an off-screen buffer has to be used to provide random access to pixels.
8+
//!
9+
//! Size of the buffer is 1024 bytes.
10+
//!
11+
//! The buffer has to be flushed to update the display after a group of draw calls has been completed.
12+
//! The flush is not part of embedded-graphics API.
13+
114
#![no_std]
215

316
use num_derive::ToPrimitive;
@@ -14,7 +27,7 @@ pub enum Error<CommError, PinError> {
1427
Pin(PinError),
1528
}
1629

17-
/// ST7735 instructions.
30+
/// ST7920 instructions.
1831
#[derive(ToPrimitive)]
1932
enum Instruction {
2033
BasicFunction = 0x30,
@@ -32,19 +45,19 @@ const ROW_SIZE: usize = (WIDTH / 8) as usize;
3245
const BUFFER_SIZE: usize = ROW_SIZE * HEIGHT as usize;
3346
const X_ADDR_DIV: u8 = 16;
3447

35-
/// ST7735 driver to connect to TFT displays.
3648
pub struct ST7920<SPI, RST, CS, DELAY>
3749
where
3850
SPI: spi::Write<u8>,
3951
RST: OutputPin,
4052
CS: OutputPin,
4153
{
42-
/// SPI
54+
/// SPI pin
4355
spi: SPI,
4456

4557
/// Reset pin.
4658
rst: RST,
4759

60+
/// CS pin
4861
cs: Option<CS>,
4962

5063
delay: DELAY,
@@ -59,10 +72,10 @@ where
5972
CS: OutputPin<Error = PinError>,
6073
DELAY: DelayMs<u8> + DelayUs<u8>,
6174
{
62-
/// Creates a new driver instance that uses hardware SPI.
75+
/// Create a new driver instance that uses SPI connection.
6376
pub fn new(
6477
spi: SPI,
65-
rst: RST, //TODO option
78+
rst: RST,
6679
cs: Option<CS>,
6780
delay: DELAY,
6881
) -> Self {
@@ -93,7 +106,7 @@ where
93106
Ok(())
94107
}
95108

96-
/// Runs commands to initialize the display.
109+
/// Initialize the display controller
97110
pub fn init(&mut self) -> Result<(), Error<SPIError, PinError>> {
98111
self.enable_cs()?;
99112
self.hard_reset()?;
@@ -167,6 +180,7 @@ where
167180
Ok(())
168181
}
169182

183+
/// Clear whole display area
170184
pub fn clear(&mut self) -> Result<(), Error<SPIError, PinError>> {
171185
self.enable_cs()?;
172186

@@ -183,6 +197,7 @@ where
183197
Ok(())
184198
}
185199

200+
/// Draw pixel
186201
pub fn set_pixel(&mut self, x: u8, y: u8, val: u8) {
187202
let x_mask = 0x80 >> (x % 8);
188203
if val != 0 {
@@ -192,6 +207,7 @@ where
192207
}
193208
}
194209

210+
/// Flush buffer to update entire display
195211
pub fn flush(&mut self) -> Result<(), Error<SPIError, PinError>> {
196212
self.enable_cs()?;
197213

@@ -212,10 +228,11 @@ where
212228
Ok(())
213229
}
214230

215-
pub fn flush_range(
231+
/// Flush buffer to update region of the display
232+
pub fn flush_region(
216233
&mut self,
217-
x1: u8,
218-
y1: u8,
234+
x: u8,
235+
y: u8,
219236
mut w: u8,
220237
h: u8,
221238
) -> Result<(), Error<SPIError, PinError>> {
@@ -229,13 +246,13 @@ where
229246
w += 8; //need to send even number of bytes
230247
}
231248

232-
let mut row_start = y1 as usize * ROW_SIZE;
233-
for y in y1..y1 + h {
234-
self.set_address(x1 / 8, y)?;
249+
let mut row_start = y as usize * ROW_SIZE;
250+
for y in y..y + h {
251+
self.set_address(x / 8, y)?;
235252

236-
for x in x1 / 8..(x1 + w) / 8 {
253+
for x in x / 8..(x + w) / 8 {
237254
self.write_data(self.buffer[row_start + x as usize])?;
238-
//TODO send in one SPI transaction
255+
//TODO send in a single SPI transaction
239256
}
240257

241258
row_start += ROW_SIZE;

0 commit comments

Comments
 (0)