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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.
//! SyscallDriver for the HD44780 LCD screen.
//!
//! The LCD must be connected as shown here, because the pins of the LCD are
//! already defined in the kernel, and modifying them means re-compiling the
//! kernel with the modifications.
//!
//! This capsule takes an alarm, an array of pins and one buffer initialized
//! to 0.
//!
//! This capsule uses the TextScreen capsule and implements the TextScreen trait,
//! through which it can receive commands (specific driver commands or write
//! commands) and call specific callbacks (write_complete() or command_complete()).
//!
//! According to the HD44780 datasheet, there must be a delay between certain
//! operations on the device. Since there cannot be a delay while running on
//! kernel mode, the alarm is the best way to implement those delays. To
//! remember the state before and after each delay, the program will be a big
//! state-machine that goes through the possible states defined in the
//! LCDStatus enum. Also, after every command completed, a callback will be called
//! to the text_screen capsule, in order for this capsule to be able to receive new
//! commands. If a command is sent while this capsule is busy, it will return a
//! "BUSY" code.
//! Usage
//! -----
//! ```rust,ignore
//! let lcd = components::hd44780::HD44780Component::new(mux_alarm).finalize(
//! components::hd44780_component_helper!(
//! stm32f429zi::tim2::Tim2,
//! // rs pin
//! gpio_ports.pins[5][13].as_ref().unwrap(),
//! // en pin
//! gpio_ports.pins[4][11].as_ref().unwrap(),
//! // data 4 pin
//! gpio_ports.pins[5][14].as_ref().unwrap(),
//! // data 5 pin
//! gpio_ports.pins[4][13].as_ref().unwrap(),
//! // data 6 pin
//! gpio_ports.pins[5][15].as_ref().unwrap(),
//! // data 7 pin
//! gpio_ports.pins[6][14].as_ref().unwrap()
//! )
//! );
//!
//! let text_screen = components::text_screen::TextScreenComponent::new(board_kernel, lcd)
//! .finalize(components::screen_buffer_size!(64));
//! ```
//!
//! Author: Teona Severin <teona.severin9@gmail.com>
use core::cell::Cell;
use kernel::hil::gpio;
use kernel::hil::text_screen::{TextScreen, TextScreenClient};
use kernel::hil::time::{self, Alarm, Frequency};
use kernel::utilities::cells::{OptionalCell, TakeCell};
use kernel::ErrorCode;
/// commands
static LCD_CLEARDISPLAY: u8 = 0x01;
static LCD_ENTRYMODESET: u8 = 0x04;
static LCD_DISPLAYCONTROL: u8 = 0x08;
static LCD_FUNCTIONSET: u8 = 0x20;
static LCD_SETDDRAMADDR: u8 = 0x80;
/// flags for display entry mode
static LCD_ENTRYLEFT: u8 = 0x02;
static LCD_ENTRYSHIFTDECREMENT: u8 = 0x00;
/// flags for display on/off control
static LCD_DISPLAYON: u8 = 0x04;
static LCD_CURSORON: u8 = 0x02;
static LCD_BLINKON: u8 = 0x01;
static LCD_BLINKOFF: u8 = 0x00;
/// flags for function set
static LCD_8BITMODE: u8 = 0x10;
static LCD_4BITMODE: u8 = 0x00;
static LCD_2LINE: u8 = 0x08;
static LCD_1LINE: u8 = 0x00;
static LCD_5X8DOTS: u8 = 0x00;
pub const BUF_LEN: usize = 4;
/// The states the program can be in.
#[derive(Copy, Clone, PartialEq)]
enum LCDStatus {
Idle,
Begin0,
Begin0_1,
Begin1,
Begin1_2,
Begin2,
Begin2_3,
Begin3,
Begin4,
Begin5,
Begin6,
Begin7,
Begin8,
Begin9,
Begin10,
Begin11,
Begin12,
Printing,
PulseLow,
PulseHigh,
Command,
Clear,
}
pub struct HD44780<'a, A: Alarm<'a>> {
rs_pin: &'a dyn gpio::Pin,
en_pin: &'a dyn gpio::Pin,
data_4_pin: &'a dyn gpio::Pin,
data_5_pin: &'a dyn gpio::Pin,
data_6_pin: &'a dyn gpio::Pin,
data_7_pin: &'a dyn gpio::Pin,
width: Cell<u8>,
height: Cell<u8>,
display_function: Cell<u8>,
display_control: Cell<u8>,
display_mode: Cell<u8>,
num_lines: Cell<u8>,
row_offsets: TakeCell<'static, [u8]>,
alarm: &'a A,
lcd_status: Cell<LCDStatus>,
lcd_after_pulse_status: Cell<LCDStatus>,
lcd_after_command_status: Cell<LCDStatus>,
lcd_after_delay_status: Cell<LCDStatus>,
command_to_finish: Cell<u8>,
begin_done: Cell<bool>,
initialized: Cell<bool>,
text_screen_client: OptionalCell<&'a dyn TextScreenClient>,
done_printing: Cell<bool>,
write_buffer: TakeCell<'static, [u8]>,
write_len: Cell<u8>,
write_buffer_len: Cell<u8>,
write_offset: Cell<u8>,
}
impl<'a, A: Alarm<'a>> HD44780<'a, A> {
pub fn new(
rs_pin: &'a dyn gpio::Pin,
en_pin: &'a dyn gpio::Pin,
data_4_pin: &'a dyn gpio::Pin,
data_5_pin: &'a dyn gpio::Pin,
data_6_pin: &'a dyn gpio::Pin,
data_7_pin: &'a dyn gpio::Pin,
row_offsets: &'static mut [u8],
alarm: &'a A,
width: u8,
height: u8,
) -> HD44780<'a, A> {
rs_pin.make_output();
en_pin.make_output();
data_4_pin.make_output();
data_5_pin.make_output();
data_6_pin.make_output();
data_7_pin.make_output();
let hd44780 = HD44780 {
rs_pin,
en_pin,
data_4_pin,
data_5_pin,
data_6_pin,
data_7_pin,
width: Cell::new(width),
height: Cell::new(height),
display_function: Cell::new(LCD_4BITMODE | LCD_1LINE | LCD_5X8DOTS),
display_control: Cell::new(0),
display_mode: Cell::new(0),
num_lines: Cell::new(0),
row_offsets: TakeCell::new(row_offsets),
alarm,
lcd_status: Cell::new(LCDStatus::Idle),
lcd_after_pulse_status: Cell::new(LCDStatus::Idle),
lcd_after_command_status: Cell::new(LCDStatus::Idle),
lcd_after_delay_status: Cell::new(LCDStatus::Idle),
command_to_finish: Cell::new(0),
begin_done: Cell::new(false),
initialized: Cell::new(false),
text_screen_client: OptionalCell::empty(),
done_printing: Cell::new(false),
write_buffer: TakeCell::empty(),
write_len: Cell::new(0),
write_buffer_len: Cell::new(0),
write_offset: Cell::new(0),
};
hd44780.init(width, height);
hd44780
}
/// `init()` initializes the functioning parameters and communication
/// parameters of the LCD, according to its datasheet (HD44780).
///
/// When the init is done, the screen capsule will receive a "screen_is_ready()"
/// callback, in order to be able to receive other commands.
///
/// `init()` is called after the capsule is instantiated:
/// - hd44780.init(16,2);
///
fn init(&self, col: u8, row: u8) {
self.begin_done.set(false);
self.width.set(col);
self.height.set(row);
if row > 1 {
self.display_function
.replace(self.display_function.get() | LCD_2LINE);
}
self.num_lines.replace(row);
let _ = self.set_rows(0x00, 0x40, 0x00 + col, 0x40 + col);
}
pub fn screen_command(&self, command: usize, op: usize, value: u8) -> Result<(), ErrorCode> {
if self.lcd_status.get() == LCDStatus::Idle {
match command {
1 => {
if op == 0 {
self.display_control.set(self.display_control.get() | value);
} else {
self.display_control
.set(self.display_control.get() & !value);
}
self.command_to_finish
.replace(LCD_DISPLAYCONTROL | self.display_control.get());
self.lcd_command(self.command_to_finish.get(), LCDStatus::Idle);
Ok(())
}
2 => {
self.lcd_clear(LCDStatus::Idle);
Ok(())
}
_ => Err(ErrorCode::INVAL),
}
} else {
Err(ErrorCode::BUSY)
}
}
/// `set_rows()` sets initializing parameters for the communication.
///
/// Example:
/// self.set_rows(0x00, 0x40, 0x00+col, 0x40+col);
///
fn set_rows(&self, row0: u8, row1: u8, row2: u8, row3: u8) -> Result<(), ErrorCode> {
self.row_offsets.map(|buffer| {
buffer[0] = row0;
buffer[1] = row1;
buffer[2] = row2;
buffer[3] = row3;
});
Ok(())
}
/// `pulse()` function starts executing the toggle needed by the device after
/// each write operation, according to the HD44780 datasheet, figure 26,
/// toggle that will be continued in the fired() function.
///
/// As argument, there is:
/// - the status of the program after the process of pulse is done
///
/// Example:
/// self.pulse(LCDStatus::Idle);
///
fn pulse(&self, after_pulse_status: LCDStatus) {
self.lcd_after_pulse_status.set(after_pulse_status);
self.en_pin.clear();
self.set_delay(500, LCDStatus::PulseLow);
}
/// `write_4_bits()` will either set or clear each data_pin according to the
/// value to be written on the device.
///
/// As arguments, there are:
/// - the value to be written
/// - the next status of the program after writing the value
///
/// Example:
/// self.write_4_bits(27, LCDStatus::Idle);
///
fn write_4_bits(&self, value: u8, next_status: LCDStatus) {
if (value >> 0) & 0x01 != 0 {
self.data_4_pin.set();
} else {
self.data_4_pin.clear();
}
if (value >> 1) & 0x01 != 0 {
self.data_5_pin.set();
} else {
self.data_5_pin.clear();
}
if (value >> 2) & 0x01 != 0 {
self.data_6_pin.set();
} else {
self.data_6_pin.clear();
}
if (value >> 3) & 0x01 != 0 {
self.data_7_pin.set();
} else {
self.data_7_pin.clear();
}
self.pulse(next_status);
}
/// `continue_ops()` is called after an alarm is fired and continues to
/// execute the command from the state it was left in before the alarm
fn continue_ops(&self) {
let state = self.lcd_status.get();
match state {
// the execution of a command was just finished and a callback to the
// screen capsule will be sent (according to the command type)
LCDStatus::Idle => {
self.text_screen_client.map(|client| {
if self.begin_done.get() {
self.begin_done.set(false);
self.initialized.set(true);
client.command_complete(Ok(()));
} else if self.write_len.get() > 0 {
self.write_character();
} else if self.done_printing.get() {
self.done_printing.set(false);
if self.write_buffer.is_some() {
self.write_buffer.take().map(|buffer| {
client.write_complete(
buffer,
self.write_buffer_len.get() as usize,
Ok(()),
)
});
}
} else {
client.command_complete(Ok(()));
}
});
}
LCDStatus::Begin0 => {
self.rs_pin.clear();
self.en_pin.clear();
if (self.display_function.get() & LCD_8BITMODE) == 0 {
self.write_4_bits(0x03, LCDStatus::Begin0_1);
} else {
self.rs_pin.clear();
self.lcd_command(
(LCD_FUNCTIONSET | self.display_function.get()) >> 4,
LCDStatus::Begin4,
);
}
}
LCDStatus::Begin0_1 => {
self.set_delay(200, LCDStatus::Begin1);
}
LCDStatus::Begin1 => {
self.write_4_bits(0x03, LCDStatus::Begin1_2);
}
LCDStatus::Begin1_2 => {
self.set_delay(200, LCDStatus::Begin2);
}
LCDStatus::Begin2 => {
self.write_4_bits(0x03, LCDStatus::Begin2_3);
}
LCDStatus::Begin2_3 => {
self.set_delay(500, LCDStatus::Begin3);
}
LCDStatus::Begin3 => {
self.write_4_bits(0x02, LCDStatus::Begin9);
}
LCDStatus::Begin4 => {
self.command_to_finish
.set(LCD_FUNCTIONSET | self.display_function.get());
self.lcd_command(
LCD_FUNCTIONSET | self.display_function.get(),
LCDStatus::Begin5,
);
}
LCDStatus::Begin5 => self.set_delay(200, LCDStatus::Begin6),
LCDStatus::Begin6 => {
self.lcd_command(
LCD_FUNCTIONSET | self.display_function.get(),
LCDStatus::Begin7,
);
}
LCDStatus::Begin7 => {
self.set_delay(500, LCDStatus::Begin8);
}
LCDStatus::Begin8 => {
self.lcd_command(
LCD_FUNCTIONSET | self.display_function.get(),
LCDStatus::Begin9,
);
}
LCDStatus::Begin9 => {
self.command_to_finish
.set(LCD_FUNCTIONSET | self.display_function.get());
self.lcd_command(
LCD_FUNCTIONSET | self.display_function.get(),
LCDStatus::Begin10,
);
}
LCDStatus::Begin10 => {
self.display_control
.set(LCD_DISPLAYON | LCD_CURSORON | LCD_BLINKOFF);
self.lcd_display(LCDStatus::Begin11);
}
LCDStatus::Begin11 => {
self.lcd_clear(LCDStatus::Begin12);
}
LCDStatus::Begin12 => {
self.display_mode
.set(LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT);
self.command_to_finish
.set(LCD_ENTRYMODESET | self.display_mode.get());
self.begin_done.set(true);
self.lcd_command(self.command_to_finish.get(), LCDStatus::Idle);
}
LCDStatus::Clear => {
self.set_delay(500, self.lcd_after_delay_status.get());
}
LCDStatus::Printing => {
self.write_4_bits(self.command_to_finish.get(), LCDStatus::Idle);
}
LCDStatus::PulseLow => {
self.en_pin.set();
self.set_delay(500, LCDStatus::PulseHigh);
}
LCDStatus::Command => {
self.write_4_bits(
self.command_to_finish.get(),
self.lcd_after_command_status.get(),
);
}
LCDStatus::PulseHigh => {
self.en_pin.clear();
self.set_delay(500, self.lcd_after_pulse_status.get());
}
}
}
/// `lcd_display()` will call lcd_command with certain arguments for the display
/// initialization.
///
/// As argument, there is:
/// - the status of the program after setting the display
///
/// Example:
/// self.lcd_display(LCDStatus::Idle);
///
fn lcd_display(&self, next_state: LCDStatus) {
self.command_to_finish
.set(LCD_DISPLAYCONTROL | self.display_control.get());
self.lcd_command(LCD_DISPLAYCONTROL | self.display_control.get(), next_state);
}
/// `lcd_command()` is the main function that communicates with the device, and
/// sends certain values received as arguments to the device (through
/// write_4_bits function). Due to the delays, the function is continued in
/// the fired() function.
///
/// As arguments, there are:
/// - the value to be sent to the device
/// - the next status of the program after sending the value
///
/// Example:
/// self.lcd_command(LCD_CLEARDISPLAY, LCDStatus::Clear);
///
fn lcd_command(&self, value: u8, next_state: LCDStatus) {
self.lcd_after_command_status.set(next_state);
self.command_to_finish.set(value);
self.rs_pin.clear();
self.write_4_bits(value >> 4, LCDStatus::Command);
}
/// `lcd_clear()` clears the lcd and brings the cursor at position (0,0).
///
/// As argument, there is:
/// - the status of the program after clearing the display
///
/// Example:
/// self.clear(LCDStatus::Idle);
///
fn lcd_clear(&self, next_state: LCDStatus) {
self.lcd_after_delay_status.set(next_state);
self.lcd_command(LCD_CLEARDISPLAY, LCDStatus::Clear);
}
/// `set_delay()` sets an alarm and saved the next state after that.
///
/// As argument, there are:
/// - the duration of the alarm:
/// - 10 means 100 ms
/// - 100 means 10 ms
/// - 500 means 2 ms
/// - the status of the program after the alarm fires
///
/// Example:
/// self.set_delay(10, LCDStatus::Idle);
///
fn set_delay(&self, timer: u32, next_status: LCDStatus) {
self.lcd_status.set(next_status);
self.alarm.set_alarm(
self.alarm.now(),
A::Ticks::from(<A::Frequency>::frequency() / timer),
);
}
/// `write_character()` will send the next character to be written on the
/// LCD display. The character is saved in the "write_buffer" buffer.
///
/// Example:
/// - self.write_character();
///
fn write_character(&self) {
let offset = self.write_offset.get() as usize;
let mut value = 0;
self.write_buffer.map(|buffer| {
value = buffer[offset];
});
self.done_printing.set(false);
self.write_offset.set(self.write_offset.get() + 1);
self.write_len.set(self.write_len.get() - 1);
if self.write_len.get() == 0 {
self.done_printing.set(true);
}
self.rs_pin.set();
self.command_to_finish.set(value);
self.write_4_bits(value >> 4, LCDStatus::Printing);
}
/// `set_cursor()` sends a command to the LCD display about the position for
/// the cursor to be set to.
///
/// As argument, there are:
/// - the column for the position
/// - the row for the position
///
/// Example:
/// - self.set_cursor(16,2);
///
fn set_cursor(&self, col: u8, row: u8) {
let mut value: u8 = 0;
self.row_offsets.map(|buffer| {
value = buffer[row as usize];
});
self.command_to_finish
.replace(LCD_SETDDRAMADDR | (col + value));
self.lcd_command(self.command_to_finish.get(), LCDStatus::Idle);
}
}
impl<'a, A: Alarm<'a>> time::AlarmClient for HD44780<'a, A> {
/// `alarm()` is called after each alarm finished, and depending on the
/// current state of the program, the next step in being decided.
fn alarm(&self) {
self.continue_ops();
}
}
impl<'a, A: Alarm<'a>> TextScreen<'a> for HD44780<'a, A> {
fn get_size(&self) -> (usize, usize) {
(16, 2)
}
fn print(
&self,
buffer: &'static mut [u8],
len: usize,
) -> Result<(), (ErrorCode, &'static mut [u8])> {
if self.lcd_status.get() == LCDStatus::Idle {
self.write_buffer.replace(buffer);
self.write_len.replace(len as u8);
self.write_buffer_len.replace(len as u8);
self.write_offset.set(0);
self.write_character();
Ok(())
} else {
Err((ErrorCode::BUSY, buffer))
}
}
fn set_cursor(&self, x_position: usize, y_position: usize) -> Result<(), ErrorCode> {
if self.lcd_status.get() == LCDStatus::Idle {
let mut line_number: u8 = y_position as u8;
if line_number >= 4 {
line_number = 3;
}
if line_number >= self.num_lines.get() {
line_number = self.num_lines.get() - 1;
}
self.set_cursor(x_position as u8, line_number);
Ok(())
} else {
Err(ErrorCode::BUSY)
}
}
fn hide_cursor(&self) -> Result<(), ErrorCode> {
self.screen_command(1, 1, LCD_CURSORON)
}
fn show_cursor(&self) -> Result<(), ErrorCode> {
self.screen_command(1, 0, LCD_CURSORON)
}
fn blink_cursor_on(&self) -> Result<(), ErrorCode> {
self.screen_command(1, 0, LCD_BLINKON)
}
fn blink_cursor_off(&self) -> Result<(), ErrorCode> {
self.screen_command(1, 1, LCD_BLINKON)
}
fn display_on(&self) -> Result<(), ErrorCode> {
if !self.initialized.get() {
if self.lcd_status.get() == LCDStatus::Idle {
self.set_delay(10, LCDStatus::Begin0);
Ok(())
} else {
Err(ErrorCode::BUSY)
}
} else {
self.screen_command(1, 0, LCD_DISPLAYON)
}
}
fn display_off(&self) -> Result<(), ErrorCode> {
self.screen_command(1, 1, LCD_DISPLAYON)
}
fn clear(&self) -> Result<(), ErrorCode> {
self.screen_command(2, 0, 0)
}
fn set_client(&self, client: Option<&'a dyn TextScreenClient>) {
if let Some(client) = client {
self.text_screen_client.set(client);
} else {
self.text_screen_client.clear();
}
}
}