aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gdisp/SSD1306/gdisp_lld_board_example_i2c.h
blob: 0e7e153a1c0e93daef2b958aa3b596acc1510784 (plain)
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
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.org/license.html
 */

#ifndef _GDISP_LLD_BOARD_H
#define _GDISP_LLD_BOARD_H

#define SSD1306_RESET_PORT		GPIOB
#define SSD1306_RESET_PIN		5

/**
 * The default slave address is 0x3D, (talking about
 * only the real address part here) and the slave
 * address can be changed to 0x3C by soldering the
 * SA0 pads on the bottom side of the module.
 *
 * b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0
 * --------------------------------------
 * 0  | 1  | 1  | 1  | 1  | 0  |SA0 | R/W
 */
#define SSD1306_I2C_ADDRESS   	0x3D
#define SSD1306_SDA_PORT		GPIOB
#define SSD1306_SDA_PIN			7
#define SSD1306_SCL_PORT		GPIOB
#define SSD1306_SCL_PIN			6
#define SET_RST					palSetPad(SSD1306_RESET_PORT, SSD1306_RESET_PIN);
#define CLR_RST					palClearPad(SSD1306_RESET_PORT, SSD1306_RESET_PIN);
int8_t vccstate;
int32_t row_offset = 0;

// I2C configuration structure.
static I2CConfig i2cconfig;


/**
 * @brief   Initialize the board for the display.
 * @notes	This board definition uses GPIO and assumes exclusive access to these GPIO pins
 * @notapi
 */
static inline void init_board(void) {

	// RESET pin.
	palSetPadMode(SSD1306_RESET_PORT, SSD1306_RESET_PIN, PAL_MODE_OUTPUT_PUSHPULL);


	/*
	 * Initializes the I2C driver 1. The I2C1 signals are routed as follows:
	 * PB6 - SCL.
	 * PB7 - SDA.
	 * Timing value comes from ST I2C config tool (xls):
	 * 0x00901D2B;		// 100kHz Standard Mode
	 * 0x00300444;		// 100kHz Fast Mode
	 * 0x0030020A;		// 400kHz Fast Mode
	 * 0x00100002;		// 800kHz Fast Mode +
	 */
	i2cconfig.timingr = 0x00100002;		// 800kHz Fast Mode+
	i2cInit();
	palSetPadMode(SSD1306_SCL_PORT, SSD1306_SCL_PIN, PAL_MODE_ALTERNATE(1));
	palSetPadMode(SSD1306_SDA_PORT, SSD1306_SDA_PIN, PAL_MODE_ALTERNATE(1));
	vccstate = SSD1306_SWITCHCAPVCC;
}

/**
 * @brief   Set or clear the lcd reset pin.
 * @param[in] state		TRUE = lcd in reset, FALSE = normal operation
 * @notapi
 */
static inline void setpin_reset(bool_t state) {
	if(state)
		SET_RST
	else
		CLR_RST
}

/**
 * @brief   Set the lcd back-light level.
 * @param[in] percent		0 to 100%
 * @notapi
 */
static inline void set_backlight(uint8_t percent) {
	// Since we are on OLED no backlight needed
}

/**
 * @brief   Take exclusive control of the bus
 * @notapi
 */
static inline void acquire_bus(void) {
		i2cAcquireBus(&I2CD1);
}

/**
 * @brief   Release exclusive control of the bus
 * @notapi
 */
static inline void release_bus(void) {
		i2cReleaseBus(&I2CD1);
}

/**
 * @brief   Send command to the display.
 * @param[in] cmd	The command to send *
 * @notapi
 */
static inline void write_cmd(uint8_t cmd) {
	uint8_t command[] = { 0x00, 		// Co = 0, D/C = 0
						  cmd 		},
						  txLength = sizeof(command)/sizeof(command[0]),
						  rxLength = 0;
	i2cStart(&I2CD1, &i2cconfig);
	i2cMasterTransmitTimeout(&I2CD1, SSD1306_I2C_ADDRESS, command, txLength, NULL, rxLength, MS2ST(10));
	i2cStop(&I2CD1);
}

/**
 * @brief   Send data to the display.
 * @param[in] data	The data to send
 * @notapi
 */
static inline void write_data(uint8_t* data, uint16_t length) {
	uint8_t command[length+1],
			txLength = length+1,
			rxLength = 0;
	command[0] = 0x40; 		// Co = 0, D/C = 1
	memmove(&command[1], data, length);

	i2cStart(&I2CD1, &i2cconfig);
	i2cMasterTransmitTimeout(&I2CD1, SSD1306_I2C_ADDRESS, command, txLength, NULL, rxLength, MS2ST(10));
	i2cStop(&I2CD1);
}

#endif /* _GDISP_LLD_BOARD_H */
/** @} */