-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatalink.c
More file actions
92 lines (74 loc) · 2.86 KB
/
datalink.c
File metadata and controls
92 lines (74 loc) · 2.86 KB
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
/**
* @file datalink.c
* @brief Datalink layer implementation for SSD1306 OLED Driver, I2C-based
* operations.
* @author Luyao Han (luyaohan1001@gmail.com)
* @date 12-21-2022
*/
#include "datalink.h"
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/module.h>
/**
* @brief Pointer to the i2c_client instance.
* @note Original symbol declared in driver.c.
*/
extern struct i2c_client *i2c_client;
/**
* @brief Write to SSD1306 register address.
* @param control_option DATA_CONTROL indicates to transmit data,
* COMMAND_CONTROL indicates to transmit command.
* @param address The register address to write param to.
* @param param_len Length of parameter if there is any.
* @param p_param Pointer to parameter to be written.
* @note The I2C bus interface write-data scheme is explained in
* section 8.1.5.1 in SSD1306 datasheet by Solomon Systech.
*/
void ssd1306_write_address(eControl_t control_option, uint8_t address,
uint8_t param_len, uint8_t *p_param) {
uint8_t control_byte = 0;
uint8_t repeat = 0;
/* A 2-byte tuple that consists of control and address/data bytes to abstract
* ssd1306 i2c communication. */
uint8_t packet[2];
/* Differentiate COMMAND versus DATA control. */
if (control_option == DATA_CONTROL) {
control_byte = SET_DISPLAY_START_LINE;
} else if (control_option == COMMAND_CONTROL) {
memcpy(packet, (uint8_t[]){control_byte, address}, 2);
i2c_master_send(i2c_client, (uint8_t[]){control_byte, address}, 2);
}
/* NULL pointer check. */
if (param_len <= 0 || NULL == p_param) {
goto EXIT;
}
/* Transmit the packet. */
for (repeat = 0; repeat < param_len; ++repeat) {
memcpy(packet, (uint8_t[]){control_byte, p_param[repeat]}, 2);
i2c_master_send(i2c_client, packet, 2);
}
EXIT:
return;
}
/**
* @brief Initialize SSD1306 OLED controller.
* @param None.
* @return None.
* @note Using anonymous array to pass single parameters.
*/
void ssd1306_controller_init(void) {
ssd1306_write_address(COMMAND_CONTROL, SET_DISPLAY_OFF, 0, NULL);
ssd1306_write_address(COMMAND_CONTROL, SET_DISPLAY_OFFSET, 1,
(uint8_t[]){0x00});
ssd1306_write_address(COMMAND_CONTROL, SET_DISPLAY_START_LINE, 0, NULL);
ssd1306_write_address(COMMAND_CONTROL, SET_CHARGE_PUMP, 0, NULL);
ssd1306_write_address(COMMAND_CONTROL, SET_CHARGE_PUMP_ENABLE, 0, NULL);
ssd1306_write_address(COMMAND_CONTROL, SET_MEMORY_ADDRESSING_MODE, 1,
(uint8_t[]){0x00});
ssd1306_write_address(COMMAND_CONTROL, SET_CONTRAST_CONTROL, 1,
(uint8_t[]){0x80});
ssd1306_write_address(COMMAND_CONTROL, SET_ENTIRE_DISPLAY_ON, 0, NULL);
ssd1306_write_address(COMMAND_CONTROL, SET_DISPLAY_ON, 0, NULL);
ssd1306_write_address(COMMAND_CONTROL, SET_DEACTIVATE_SCROLL, 0, NULL);
ssd1306_write_address(COMMAND_CONTROL, SET_DISPLAY_ON, 0, NULL);
}