1 /* 2 // Copyright (C) 2022 Beken Corporation 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include <common/bk_include.h> 16 #include <os/mem.h> 17 #include <os/str.h> 18 #include <os/os.h> 19 #include <stdlib.h> 20 #include <driver/spi.h> 21 #include <driver/gpio.h> 22 #include "bk_misc.h" 23 #include <driver/hal/hal_gpio_types.h> 24 25 #define LCD_SPI_ID 1 26 #define LCD_SPI_CLK_GPIO GPIO_2 27 #define LCD_SPI_CSX_GPIO GPIO_3 28 #define LCD_SPI_SDA_GPIO GPIO_4 29 #define LCD_SPI_DELAY 10 30 31 SPI_SendData(uint8_t data)32static void SPI_SendData(uint8_t data) 33 { 34 uint8_t n; 35 36 //in while loop, to avoid disable IRQ too much time, release it if finish one byte. 37 GLOBAL_INT_DECLARATION(); 38 GLOBAL_INT_DISABLE(); 39 for(n = 0; n < 8; n++) { 40 if (data & 0x80) 41 bk_gpio_set_output_high(LCD_SPI_SDA_GPIO); 42 else 43 bk_gpio_set_output_low(LCD_SPI_SDA_GPIO); 44 45 delay_us(LCD_SPI_DELAY); 46 data <<= 1; 47 48 bk_gpio_set_output_low(LCD_SPI_CLK_GPIO); 49 delay_us(LCD_SPI_DELAY); 50 bk_gpio_set_output_high(LCD_SPI_CLK_GPIO); 51 delay_us(LCD_SPI_DELAY); 52 53 } 54 GLOBAL_INT_RESTORE(); 55 } 56 lcd_spi_write_cmd(uint8_t cmd)57void lcd_spi_write_cmd(uint8_t cmd) 58 { 59 bk_gpio_set_output_low(LCD_SPI_CSX_GPIO); 60 delay_us(LCD_SPI_DELAY); 61 bk_gpio_set_output_low(LCD_SPI_SDA_GPIO); 62 delay_us(LCD_SPI_DELAY); 63 64 bk_gpio_set_output_low(LCD_SPI_CLK_GPIO); 65 delay_us(LCD_SPI_DELAY); 66 bk_gpio_set_output_high(LCD_SPI_CLK_GPIO); 67 delay_us(LCD_SPI_DELAY); 68 69 SPI_SendData(cmd); 70 71 bk_gpio_set_output_high(LCD_SPI_CSX_GPIO); 72 delay_us(LCD_SPI_DELAY); 73 } 74 lcd_spi_write_data(uint8_t data)75void lcd_spi_write_data(uint8_t data) 76 { 77 bk_gpio_set_output_low(LCD_SPI_CSX_GPIO); 78 delay_us(LCD_SPI_DELAY); 79 bk_gpio_set_output_high(LCD_SPI_SDA_GPIO); 80 delay_us(LCD_SPI_DELAY); 81 82 bk_gpio_set_output_low(LCD_SPI_CLK_GPIO); 83 delay_us(LCD_SPI_DELAY); 84 bk_gpio_set_output_high(LCD_SPI_CLK_GPIO); 85 delay_us(LCD_SPI_DELAY); 86 87 SPI_SendData(data); 88 89 bk_gpio_set_output_high(LCD_SPI_CSX_GPIO); 90 delay_us(LCD_SPI_DELAY); 91 } 92 93 lcd_spi_init_gpio(void)94void lcd_spi_init_gpio(void) 95 { 96 BK_LOG_ON_ERR(bk_gpio_disable_input(LCD_SPI_CLK_GPIO)); 97 BK_LOG_ON_ERR(bk_gpio_enable_output(LCD_SPI_CLK_GPIO)); 98 99 BK_LOG_ON_ERR(bk_gpio_disable_input(LCD_SPI_CSX_GPIO)); 100 BK_LOG_ON_ERR(bk_gpio_enable_output(LCD_SPI_CSX_GPIO)); 101 102 BK_LOG_ON_ERR(bk_gpio_disable_input(LCD_SPI_SDA_GPIO)); 103 BK_LOG_ON_ERR(bk_gpio_enable_output(LCD_SPI_SDA_GPIO)); 104 105 bk_gpio_set_output_high(LCD_SPI_CLK_GPIO); 106 bk_gpio_set_output_high(LCD_SPI_CSX_GPIO); 107 delay_us(200); 108 } 109 110 111