1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  * Copyright (C) 2022, Intel Corporation
4  * Description:
5  * Example to use LCD Display in Cyclone V SoC FPGA devkit
6  */
7 
8 #include <zephyr/sys/printk.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/kernel.h>
13 #include "commands.h"
14 #define I2C_INST DT_NODELABEL(i2c0)
15 #define LCD_ADDRESS 0x28
16 
17 const struct device *const i2c = DEVICE_DT_GET(I2C_INST);
18 
send_ascii(char c)19 void send_ascii(char c)
20 {
21 
22 	const uint8_t buf = c;
23 	int ret;
24 
25 	ret = i2c_write(i2c, &buf, sizeof(buf), LCD_ADDRESS);
26 
27 	if (ret != 0) {
28 		printf("Fail!\n");
29 	} else {
30 		printf("Success!\n");
31 	}
32 }
33 
send_string(uint8_t * str_ptr,int len)34 void send_string(uint8_t *str_ptr, int len)
35 {
36 	int ret;
37 
38 	ret = i2c_write(i2c, str_ptr, len, LCD_ADDRESS);
39 
40 	if (ret != 0) {
41 		printf("Fail!\n");
42 	} else {
43 		printf("Success!\n");
44 	}
45 }
46 
send_command_no_param(uint8_t command_id)47 void send_command_no_param(uint8_t command_id)
48 {
49 	const uint8_t buf[] = {0xFE, command_id};
50 
51 	int ret;
52 
53 	ret = i2c_write(i2c, (const uint8_t *) &buf, sizeof(buf), LCD_ADDRESS);
54 
55 	if (ret != 0) {
56 		printf("Fail!\n");
57 	} else {
58 		printf("Success!\n");
59 	}
60 }
61 
send_command_one_param(uint8_t command_id,uint8_t param)62 void send_command_one_param(uint8_t command_id, uint8_t param)
63 {
64 	const uint8_t buf[] = { 0xFE, command_id, param};
65 
66 	int ret;
67 
68 	ret = i2c_write(i2c, (const uint8_t *) &buf, sizeof(buf), LCD_ADDRESS);
69 
70 	if (ret != 0) {
71 		printf("Fail!\n");
72 	} else {
73 		printf("Success!\n");
74 	}
75 }
76 
clear(void)77 void clear(void)
78 {
79 	send_command_no_param(CLEAR_SCREEN);
80 }
81 
next_line(void)82 void next_line(void)
83 {
84 	send_command_one_param(SET_CURSOR, 0x40);
85 }
86 
main(void)87 int main(void)
88 {
89 	printk("Hello World! %s\n", CONFIG_BOARD);
90 	static const unsigned char string[] = "Hello world!";
91 	int len = strlen((const unsigned char *) &string);
92 
93 	if (!device_is_ready(i2c)) {
94 		printf("i2c is not ready!\n");
95 	} else {
96 		printf("i2c is ready\n");
97 	}
98 
99 	send_ascii('A');
100 	k_sleep(K_MSEC(1000));
101 	next_line();
102 	k_sleep(K_MSEC(10));
103 	send_string((uint8_t *)&string, len);
104 
105 	return 0;
106 }
107