-
Notifications
You must be signed in to change notification settings - Fork 1
/
lcd_driver.c
630 lines (559 loc) · 17.6 KB
/
lcd_driver.c
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
/*
* Filename: lcd_driver.c
* Date: 11/11/2019
* Description: character driver for a LCD 16x02 device
* Author: VietAnh Bui
*/
#include <linux/module.h> /* Define module_init(), module_exit()*/
#include <linux/fs.h> /*contain function allocate/free device number*/
#include <linux/device.h> /*contain function for creating device file*/
#include <linux/slab.h> /* cointain kmalloc & kfree*/
#include <linux/cdev.h> /*contain function work with cdev*/
#include <linux/uaccess.h> /*transport data between user vs kernel*/
#include <linux/ioctl.h> /* contain function services ioctl */
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fcntl.h>
#include <linux/gpio.h> /*linux gpio interface*/
#include <linux/kdev_t.h>
#include <linux/delay.h> /*delay*/
#include "lcd_driver.h"
#define DRIVER_AUTHOR "AnhBV-DiemPV"
#define DRIVER_VERSION "1.0"
#define DRIVER_DESC "The character device driver for LCD 16x02 HD44780"
// ********* Linux driver Constants ******************************************************************
#define MINOR_NUM_START 0 /* minor number starts from 0 */
#define MINOR_NUM_COUNT 1 /* the number of minor numbers required */
/************************Driver Structures*******************************/
#define CLASS_NAME "class_lcd"
#define DEVICE_NAME "lcd_1602"
struct _lcd_driver
{
dev_t dev_number; /*dynamically allocated device major number*/
struct class *lcd_class; /*class structure*/
struct device *dev;
struct cdev *lcd_cdev; /*cdev structure*/
} lcd_driver;
/****************************Device Specific-START************************/
/*************Setup GPIO**********************/
static int lcd_pin_setup(unsigned int pin_number)
{
int ret;
PIN_DIRECTION gpio_direction = OUTPUT_PIN;
/* Request GPIO allocation */
ret = gpio_request(pin_number, "GPIO Pin Request");
if (ret != 0)
{
printk(KERN_DEBUG "ERR: Failed to request gpio %d\n", pin_number);
return ret;
}
/* Set GPIO export & disallow user space to change direction of GPIO */
ret = gpio_export(pin_number, 0);
if (ret != 0)
{
printk(KERN_DEBUG "ERR: Failed to export gpio %d\n", pin_number);
return ret;
}
/* Set GPIO direction */
ret = gpio_direction_output(pin_number, gpio_direction);
if (ret != 0)
{
printk(KERN_DEBUG "ERR: Failed to set direction gpio %d\n", pin_number);
return ret;
}
/* Set init value of GPIO*/
gpio_set_value(pin_number, 0);
/* Return 0 if there is no error*/
return 0;
}
/***********Setup All GPIO*******************/
static void lcd_pin_setup_All(void)
{
lcd_pin_setup(LCD_RS_PIN_NUMBER);
lcd_pin_setup(LCD_EN_PIN_NUMBER);
lcd_pin_setup(LCD_D4_PIN_NUMBER);
lcd_pin_setup(LCD_D5_PIN_NUMBER);
lcd_pin_setup(LCD_D6_PIN_NUMBER);
lcd_pin_setup(LCD_D7_PIN_NUMBER);
}
/***********Release a GPIO********************/
static void lcd_pin_release(unsigned int pin_number)
{
gpio_unexport(pin_number); /* return GPIO pin */
gpio_free(pin_number); /* unexport GPIO pin */
}
/***********Release All GPIO*******************/
static void lcd_pin_release_All(void)
{
lcd_pin_release(LCD_RS_PIN_NUMBER);
lcd_pin_release(LCD_EN_PIN_NUMBER);
lcd_pin_release(LCD_D4_PIN_NUMBER);
lcd_pin_release(LCD_D5_PIN_NUMBER);
lcd_pin_release(LCD_D6_PIN_NUMBER);
lcd_pin_release(LCD_D7_PIN_NUMBER);
}
/********************LCD Nibble************************/
static void lcd_nibble(char data)
{
int db7_data = 0;
int db6_data = 0;
int db5_data = 0;
int db4_data = 0;
usleep_range(2000, 3000); /* added delay instead of busy checking */
/*Get 4 bit Upper*/
db7_data = ((data) & (0x01 << 7)) >> (7);
db6_data = ((data) & (0x01 << 6)) >> (6);
db5_data = ((data) & (0x01 << 5)) >> (5);
db4_data = ((data) & (0x01 << 4)) >> (4);
/*Set value to correspond GPIO*/
gpio_set_value(LCD_D7_PIN_NUMBER, db7_data);
gpio_set_value(LCD_D6_PIN_NUMBER, db6_data);
gpio_set_value(LCD_D5_PIN_NUMBER, db5_data);
gpio_set_value(LCD_D4_PIN_NUMBER, db4_data);
}
/***********LCD send command*******************
* Only 4 upper bit was used */
static void lcd_send_command(char command)
{
/*Send signal to Pin*/
lcd_nibble(command);
/*Set to command mode*/
gpio_set_value(LCD_RS_PIN_NUMBER, RS_COMMAND_MODE);
usleep_range(5, 10);
/* Simulate falling edge triggered clock */
gpio_set_value(LCD_EN_PIN_NUMBER, 1);
usleep_range(5, 10);
gpio_set_value(LCD_EN_PIN_NUMBER, 0);
}
/******************LCD send data************************************/
static void lcd_send_data(char data)
{
/* Part 1. Upper 4 bit data (from bit 7 to bit 4)*/
lcd_nibble(data & 0xF0);
/* Part 1. Set to data mode*/
gpio_set_value(LCD_RS_PIN_NUMBER, RS_DATA_MODE);
usleep_range(5, 10);
/* Part 1. Simulate falling edge triggered clock */
gpio_set_value(LCD_EN_PIN_NUMBER, 1);
usleep_range(5, 10);
gpio_set_value(LCD_EN_PIN_NUMBER, 0);
/* Part 2. Lower 4 bit data (from bit 3 to bit 0)*/
lcd_nibble((data & 0x0F) << 4);
/* Part 2. Set to data mode */
gpio_set_value(LCD_RS_PIN_NUMBER, RS_DATA_MODE);
usleep_range(5, 10);
/* Part 2. Simulate falling edge triggered clock */
gpio_set_value(LCD_EN_PIN_NUMBER, 1);
usleep_range(5, 10);
gpio_set_value(LCD_EN_PIN_NUMBER, 0);
}
/************************LCD init******************************/
/* Initialize the LCD in 4 bit mode as described on the HD44780 LCD controller document.*/
static void lcd_initialize()
{
usleep_range(41 * 1000, 50 * 1000); /* wait for more than 40 ms once the power is on */
lcd_send_command(0x30); /* Instruction 0011b (Function set) */
usleep_range(5 * 1000, 6 * 1000); /* wait for more than 4.1 ms */
lcd_send_command(0x30); /* Instruction 0011b (Function set) */
usleep_range(100, 200); /* wait for more than 100 us */
lcd_send_command(0x30); /* Instruction 0011b (Function set) */
usleep_range(100, 200); /* wait for more than 100 us */
lcd_send_command(0x20); /* Instruction 0010b (Function set)*/ /*Set interface to be 4 bits long*/
usleep_range(100, 200); /* wait for more than 100 us */
lcd_send_command(0x20); /* Instruction 0010b (Function set) */
lcd_send_command(0x80); /* Instruction NF**b
Set N = 1, or 2-line display
Set F = 0, or 5x8 dot character font*/
usleep_range(41 * 1000, 50 * 1000);
/* Display off, Cursor Off, Blink Off*/
lcd_send_command(0x00); /* Instruction 0000b */
lcd_send_command(0x80); /* Instruction 1000b */
usleep_range(100, 200);
/* Display clear, return the cursor Home */
lcd_send_command(0x00); /* Instruction 0000b */
lcd_send_command(0x10); /* Instruction 0001b */
usleep_range(100, 200);
/* Entry mode set: Inc cursor to the right when writing, don't shift screen*/
lcd_send_command(0x00); /* Instruction 0000b */
lcd_send_command(0x60); /* Instruction 01(I/D)Sb -> 0110b
Set I/D = 1, or increment or decrement DDRAM address by 1
Set S = 0, or no display shift */
usleep_range(100, 200);
/* Initialization Completed, but set up default LCD setting here */
/* Display On/off Control */
lcd_send_command(0x00); /* Instruction 0000b*/
lcd_send_command(0xF0); /* Instruction 1DCBb
Set D= 1, or Display on
Set C= 1, or Cursor on
Set B= 1, or Blinking on
*/
usleep_range(100, 200);
}
/**************Display string on LCD with line number*******************************/
static int lcd_print(char *msg, unsigned int lineNumber)
{
unsigned int counter = 0;
unsigned int lineNum = lineNumber;
if (msg == NULL)
{
printk(KERN_DEBUG "ERR: Empty data for lcd_print \n");
return -1;
}
if ((lineNum != 1) && (lineNum != 2))
{
printk(KERN_DEBUG "ERR: Invalid line number readjusted to 1 \n");
lineNum = 1;
}
lcd_setLine(LCD_FIRST_LINE);
if (lineNum == 1)
{
lcd_setLine(LCD_FIRST_LINE);
while (*(msg) != '\0')
{
if (counter >= NUM_CHAR_PER_LINE)
{
lineNum = 2; /* continue writing on the next line if the string is too long */
counter = 0;
break;
}
lcd_send_data(*msg);
msg++;
counter++;
}
}
if (lineNum == 2)
{
lcd_setLine(LCD_SECOND_LINE);
while (*(msg) != '\0')
{
if (counter >= NUM_CHAR_PER_LINE)
{
break;
}
lcd_send_data(*msg);
msg++;
counter++;
}
}
return 0;
}
/*************************Set line to display *********************************/
void lcd_setLine(unsigned int line)
{
if (line == 1)
{
lcd_send_command(0x80); /* set position to LCD line 1*/
lcd_send_command(0x00);
}
else if (line == 2)
{
lcd_send_command(0xC0);
lcd_send_command(0x00);
}
else
{
printk("ERR: Invalid line number. Select either 1 or 2 \n");
}
}
/*************************LCD clean display*********************************/
static void lcd_clearDisplay(void)
{
/*Send 0x01 command*/
lcd_send_command(0x00); /* upper 4 bits of command - 0000b */
lcd_send_command(0x10); /* lower 4 bits of command - 0001b */
printk(KERN_INFO "LCD Driver: display clear\n");
}
/*************************LCD scroll left*********************************/
static void lcd_scroll_left(void)
{
unsigned char command = LCD_SHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT;
lcd_send_command(command & 0xF0); /* upper 4 bits of command */
lcd_send_command((command & 0x0F) << 4); /* lower 4 bits of command */
printk(KERN_INFO "LCD Driver: display clear\n");
}
/*************************LCD scroll right*********************************/
static void lcd_scroll_right(void)
{
unsigned char command = LCD_SHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT;
lcd_send_command(command & 0xF0); /* upper 4 bits of command */
lcd_send_command((command & 0x0F) << 4); /* lower 4 bits of command */
printk(KERN_INFO "LCD Driver: display clear\n");
}
/************************LCD Set cursor to position (x,y)*********************/
static void lcd_gotoxy(axis_t *position)
{
char command;
if (1 == position->x)
{
command = 0x80 + position->y;
lcd_send_command(command & 0xF0); /* upper 4 bits of command */
lcd_send_command((command & 0x0F) << 4); /* lower 4 bits of command */
}
else if (2 == position->x)
{
command = 0xC0 + position->y;
lcd_send_command(command & 0xF0); /* upper 4 bits of command */
lcd_send_command((command & 0x0F) << 4); /* lower 4 bits of command */
}
else
{
printk("ERR: Invalid line number. Select either 1 or 2 \n");
}
}
/**********************LCD Set ON/OFF Display/Cursor/Blink********************************/
static void lcd_set_display(display_control_t *display_option)
{
/* Display On/off Control */
/* Instruction 1DCBb - COMMAND CONTROL DISPLAY 0x08*/
/* Set D= 1, or Display on
Set C= 1, or Cursor on
Set B= 1, or Blinking on
*/
unsigned char command = 0;
command = LCD_DISPLAYCONTROL | (display_option->display ? LCD_DISPLAYON : LCD_DISPLAYOFF) | (display_option->cursor ? LCD_CURSORON : LCD_CURSOROFF) | (display_option->blink ? LCD_BLINKON : LCD_BLINKOFF);
lcd_send_command(command & 0xF0); /* 4 Upper bit*/
lcd_send_command((command & 0x0F) << 4); /* 4 Lower bit - contain display config */
}
/**********************LCD Upload custom character to CGRAM********************************/
static void lcd_create_char(custom_char_t *custom_chr)
{
unsigned char command = 0x00, i;
command = LCD_SETCGRAMADDR | ((custom_chr->location & 0x07) << 3); /* = LCD_SETCGRAMADDR + (location*8) => 5x8 character */
lcd_send_command(command & 0xF0); /* 4 Upper bit */
lcd_send_command((command & 0x0F) << 4); /* 4 Lower bit */
usleep_range(60, 100);
for (i = 0; i < 8; i++)
{
lcd_send_data(custom_chr->charmap[i]);
usleep_range(60, 100);
}
lcd_clearDisplay();
}
static void lcd_set_autoscroll(unsigned char status)
{
unsigned char command = 0x00;
if (ENABLE == status)
{
command = LCD_ENTRYMODESET | LCD_ENTRYSHIFTINCREMENT | LCD_ENTRYLEFT;
}
else if (DISABLE == status)
{
command = LCD_ENTRYMODESET & (~LCD_ENTRYSHIFTINCREMENT) & (~LCD_ENTRYLEFT);
}
else
{
printk(KERN_DEBUG "Invalid status to set Enable/Disable autoscroll\n");
return;
}
lcd_send_command(command & 0xF0); /* 4 Upper bit */
lcd_send_command((command & 0x0F) << 4); /* 4 Lower bit */
}
/****************************Device Specific-END**************************/
/****************************OS Specific-START************************/
/****Function open entry point*****/
static int char_lcd_open(struct inode *inode, struct file *filp)
{
printk(KERN_INFO "Open device file event\n");
return 0;
}
/****Function release entry point*****/
static int char_lcd_release(struct inode *inode, struct file *filp)
{
printk(KERN_INFO "Close event\n");
return 0;
}
/****Function read entry point*****/
/****Function write entry point*****/
static ssize_t char_lcd_write(struct file *filp, const char __user *user_buf, size_t len, loff_t *off)
{
char *kernel_buf = NULL;
if (NULL == user_buf)
{
printk(KERN_DEBUG "ERR: Empty user buffer\n");
return -ENOMEM;
}
printk(KERN_INFO "Handle write event from %lld, %zu bytes\n", *off, len);
kernel_buf = kzalloc(len, GFP_KERNEL);
if (copy_from_user(kernel_buf, user_buf, len))
{
return -EFAULT;
}
kernel_buf[len] = '\0';
/*clear display*/
lcd_clearDisplay();
/*print on the first line by default*/
lcd_print(kernel_buf, LCD_FIRST_LINE);
kfree(kernel_buf);
printk(KERN_INFO "LCD Driver: write()\n");
return len;
}
/**************************Function ioctl entry point********************************/
static long char_lcd_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
int ret = 0;
printk("Handle ioctl event (cmd: %u)\n", cmd);
switch (cmd)
{
case 1:
{
lcd_clearDisplay();
break;
}
case 10:
{
axis_t position;
if (copy_from_user(&position, (axis_t *)arg, sizeof(position)))
{
printk(KERN_DEBUG "ERR: Failed to copy from user space buffer \n");
return -EFAULT;
}
printk(KERN_INFO "Cursor go [%d][%d]\n", position.x, position.y);
lcd_gotoxy(&position);
printk(KERN_INFO "After call lcd_gotoxy [%d][%d]\n", position.x, position.y);
break;
}
case 3:
{
display_control_t display;
if (copy_from_user(&display, (display_control_t *)arg, sizeof(display)))
{
printk(KERN_DEBUG "ERR: Failed to copy from user space buffer \n");
return -EFAULT;
}
lcd_set_display(&display);
printk(KERN_INFO "Control display: Display [%s], Cursor [%s], Blink [%s]\n",
(display.display) ? "enable" : "disable",
(display.cursor) ? "enable" : "disable",
(display.blink) ? "enable" : "disable");
break;
}
case 4:
{
unsigned char character;
if (copy_from_user(&character, (unsigned char *)arg, sizeof(character)))
{
printk(KERN_DEBUG "ERR: Failed to copy from user space buffer \n");
return -EFAULT;
}
lcd_send_data(character);
printk(KERN_INFO "Put character: %c\n", character);
break;
}
case 5:
{
lcd_scroll_left();
break;
}
case 6:
{
lcd_scroll_right();
break;
}
case 7:
{
custom_char_t custom_chr;
if (copy_from_user(&custom_chr, (custom_char_t *)arg, sizeof(custom_chr)))
{
printk(KERN_DEBUG "ERR: Failed to copy from user space buffer \n");
return -EFAULT;
}
lcd_create_char(&custom_chr);
break;
}
case 8:
{
lcd_initialize();
break;
}
case 9:
{
unsigned char status;
if (copy_from_user(&status, (unsigned char *)arg, sizeof(status)))
{
printk(KERN_DEBUG "ERR: Failed to copy from user space buffer \n");
return -EFAULT;
}
lcd_set_autoscroll(status);
printk(KERN_INFO "%s auto scroll\n", (ENABLE == status) ? "Enable" : "Disable");
break;
}
}
return ret;
}
/* file operation structure */
static struct file_operations lcd_fops =
{
.owner = THIS_MODULE,
.open = char_lcd_open,
.release = char_lcd_release,
.write = char_lcd_write,
.unlocked_ioctl = char_lcd_ioctl,
};
/****************************OS Specific-END**************************/
static int __init lcd_driver_init(void)
{
/* dynamically allocate device major number */
if (alloc_chrdev_region(&lcd_driver.dev_number, MINOR_NUM_START, MINOR_NUM_COUNT, DEVICE_NAME) < 0)
{
printk(KERN_DEBUG "ERR: Failed to allocate major number \n");
return -1;
}
/* create a class structure */
lcd_driver.lcd_class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(lcd_driver.lcd_class))
{
unregister_chrdev_region(lcd_driver.dev_number, MINOR_NUM_COUNT);
printk(KERN_DEBUG "ERR: Failed to create class structure \n");
return PTR_ERR(lcd_driver.lcd_class);
}
/* create a device and registers it with sysfs */
lcd_driver.dev = device_create(lcd_driver.lcd_class, NULL, lcd_driver.dev_number, NULL, DEVICE_NAME);
if (IS_ERR(lcd_driver.dev))
{
class_destroy(lcd_driver.lcd_class);
unregister_chrdev_region(lcd_driver.dev_number, MINOR_NUM_COUNT);
printk(KERN_DEBUG "ERR: Failed to create device structure \n");
return PTR_ERR(lcd_driver.dev);
}
/* initialize a cdev structure */
lcd_driver.lcd_cdev = cdev_alloc();
cdev_init(lcd_driver.lcd_cdev, &lcd_fops);
/* add a character device to the system */
if (cdev_add(lcd_driver.lcd_cdev, lcd_driver.dev_number, MINOR_NUM_COUNT) < 0)
{
device_destroy(lcd_driver.lcd_class, lcd_driver.dev_number);
class_destroy(lcd_driver.lcd_class);
unregister_chrdev_region(lcd_driver.dev_number, MINOR_NUM_COUNT);
printk(KERN_DEBUG "ERR: Failed to add cdev \n");
return -1;
}
/*setup GPIO pins*/
lcd_pin_setup_All();
/* initialize LCD once */
lcd_initialize();
printk(KERN_INFO "Initialize lcd driver successfull!\n");
return 0;
}
static void __exit lcd_driver_exit(void)
{
/* clear LCD display */
lcd_clearDisplay();
/* remove a cdev from the system */
cdev_del(lcd_driver.lcd_cdev);
/* remove device */
device_destroy(lcd_driver.lcd_class, lcd_driver.dev_number);
/* destroy class */
class_destroy(lcd_driver.lcd_class);
/* deallocate device number */
unregister_chrdev_region(lcd_driver.dev_number, MINOR_NUM_COUNT);
/* releasse GPIO pins */
lcd_pin_release_All();
printk(KERN_INFO "lcd Driver Exited. \n");
printk(KERN_INFO "Goodbye!!!\n");
}
module_init(lcd_driver_init);
module_exit(lcd_driver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_VERSION(DRIVER_VERSION);