Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2014-2018 Cesanta Software Limited
- * All rights reserved
- *
- * Licensed under the Apache License, Version 2.0 (the ""License"");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an ""AS IS"" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include "mgos.h"
- #include "mgos_pwm.h"
- #include "mgos_spi.h"
- #define LED_PIN 2
- #define CS_PIN 15
- #define SPI_MODE 3
- #define LCD_RESET_PIN 16
- #define LCD_DATA 1 // Data bit
- #define LCD_COMMAND 0 // Command bit
- #define LCD_CLEAR_SCREEN 0x01 // Clear screen
- #define LCD_ADDRESS_RESET 0x02 // The address counter is reset
- #define LCD_BASIC_FUNCTION 0x30 // Basic instruction set
- #define LCD_EXTEND_FUNCTION 0x34 // Extended instruction set
- static void st7920_spi_write(const uint8_t *data, uint32_t size) {
- struct mgos_spi *spi = mgos_spi_get_global();
- if (!spi) {
- LOG(LL_ERROR, ("SPI is disabled, set spi.enable=true"));
- return;
- }
- struct mgos_spi_txn txn = {
- .cs = 0,
- .mode = SPI_MODE,
- .freq = 200000,
- };
- txn.hd.tx_data = data,
- txn.hd.tx_len = size,
- txn.hd.dummy_len = 0,
- txn.hd.rx_len = 0,
- txn.hd.rx_data = NULL,
- mgos_spi_run_txn(spi, false, &txn);
- }
- static void st7920_spi_write8_cmd(bool command, uint8_t byte) {
- st7920_spi_write(command ? 0xFA : 0xF8, 1);
- st7920_spi_write(byte & 0xF0, 1);
- st7920_spi_write((byte << 4) & 0xF0, 1);
- }
- void ST7920_Init() {
- mgos_gpio_setup_output(LCD_RESET_PIN, 0);
- mgos_gpio_write(LCD_RESET_PIN, 0);
- mgos_msleep(100);
- mgos_gpio_write(LCD_RESET_PIN, 1);
- st7920_spi_write8_cmd(LCD_COMMAND, LCD_BASIC_FUNCTION); // Function set
- st7920_spi_write8_cmd(LCD_COMMAND, LCD_CLEAR_SCREEN); // Display clear
- st7920_spi_write8_cmd(LCD_COMMAND, 0x06); // Entry mode set
- st7920_spi_write8_cmd(LCD_COMMAND, 0x0C); // Display control
- }
- enum mgos_app_init_result mgos_app_init(void) {
- mgos_gpio_setup_output(LED_PIN, 0);
- ST7920_Init();
- return MGOS_APP_INIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement