Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2014-2018 Cesanta Software Limited
  3.  * All rights reserved
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the ""License"");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an ""AS IS"" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17.  
  18. #include "mgos.h"
  19. #include "mgos_pwm.h"
  20. #include "mgos_spi.h"
  21.  
  22. #define LED_PIN 2
  23. #define CS_PIN 15
  24. #define SPI_MODE 3
  25. #define LCD_RESET_PIN 16
  26.  
  27. #define LCD_DATA                1       // Data bit
  28. #define LCD_COMMAND             0       // Command bit
  29. #define LCD_CLEAR_SCREEN        0x01    // Clear screen
  30. #define LCD_ADDRESS_RESET       0x02    // The address counter is reset
  31. #define LCD_BASIC_FUNCTION      0x30    // Basic instruction set
  32. #define LCD_EXTEND_FUNCTION     0x34    // Extended instruction set
  33.  
  34.  
  35.  
  36. static void st7920_spi_write(const uint8_t *data, uint32_t size) {
  37.     struct mgos_spi *spi = mgos_spi_get_global();
  38.  
  39.     if (!spi) {
  40.         LOG(LL_ERROR, ("SPI is disabled, set spi.enable=true"));
  41.         return;
  42.     }
  43.  
  44.     struct mgos_spi_txn txn = {
  45.         .cs   = 0,
  46.         .mode = SPI_MODE,
  47.         .freq = 200000,
  48.     };
  49.     txn.hd.tx_data   = data,
  50.     txn.hd.tx_len    = size,
  51.     txn.hd.dummy_len = 0,
  52.     txn.hd.rx_len    = 0,
  53.     txn.hd.rx_data   = NULL,
  54.     mgos_spi_run_txn(spi, false, &txn);
  55. }
  56.  
  57. static void st7920_spi_write8_cmd(bool command, uint8_t byte) {
  58.  
  59.     st7920_spi_write(command ? 0xFA : 0xF8, 1);
  60.     st7920_spi_write(byte & 0xF0, 1);
  61.     st7920_spi_write((byte << 4) & 0xF0, 1);
  62. }
  63.  
  64. void ST7920_Init() {
  65.     mgos_gpio_setup_output(LCD_RESET_PIN, 0);
  66.     mgos_gpio_write(LCD_RESET_PIN, 0);
  67.     mgos_msleep(100);
  68.     mgos_gpio_write(LCD_RESET_PIN, 1);
  69.  
  70.     st7920_spi_write8_cmd(LCD_COMMAND, LCD_BASIC_FUNCTION); // Function set
  71.     st7920_spi_write8_cmd(LCD_COMMAND, LCD_CLEAR_SCREEN);   // Display clear
  72.     st7920_spi_write8_cmd(LCD_COMMAND, 0x06); // Entry mode set
  73.     st7920_spi_write8_cmd(LCD_COMMAND, 0x0C); // Display control
  74.  
  75. }
  76.  
  77. enum mgos_app_init_result mgos_app_init(void) {
  78.  
  79.     mgos_gpio_setup_output(LED_PIN, 0);
  80.  
  81.     ST7920_Init();
  82.  
  83.     return MGOS_APP_INIT_SUCCESS;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement