summaryrefslogtreecommitdiff
path: root/src/resource/resource_PCA9685.c
blob: a4c1e55fc6ac0aadb31f7a2dec447c949c767257 (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
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
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *
 * Contact: Jeonghoon Park <jh1979.park@samsung.com>
 *
 * Licensed under the Flora License, Version 1.1 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <peripheral_io.h>
#include "log.h"

#define RPI3_I2C_BUS 1

/* Registers/etc: */
#define PCA9685_ADDRESS    0x40
#define MODE1              0x00
#define MODE2              0x01
#define SUBADR1            0x02
#define SUBADR2            0x03
#define SUBADR3            0x04
#define PRESCALE           0xFE
#define LED0_ON_L          0x06
#define LED0_ON_H          0x07
#define LED0_OFF_L         0x08
#define LED0_OFF_H         0x09
#define ALL_LED_ON_L       0xFA
#define ALL_LED_ON_H       0xFB
#define ALL_LED_OFF_L      0xFC
#define ALL_LED_OFF_H      0xFD

/* Bits: */
#define RESTART            0x80
#define SLEEP              0x10
#define ALLCALL            0x01
#define INVRT              0x10
#define OUTDRV             0x04

typedef enum {
	PCA9685_CH_STATE_NONE,
	PCA9685_CH_STATE_USED,
} pca9685_ch_state_e;

static peripheral_i2c_h g_i2c_h = NULL;
static unsigned int ref_count = 0;
static pca9685_ch_state_e ch_state[PCA9685_CH_MAX] = {PCA9685_CH_STATE_NONE, };

int resource_pca9685_set_frequency(unsigned int freq_hz)
{
	int ret = PERIPHERAL_ERROR_NONE;
	double prescale_value = 0.0;
	int prescale = 0;
	uint8_t oldmode = 0;
	uint8_t newmode = 0;

	prescale_value = 25000000.0;	// 25MHz
	prescale_value /= 4096.0;	// 12-bit
	prescale_value /= (double)freq_hz;
	prescale_value -= 1.0;

	prescale = (int)floor(prescale_value + 0.5);

	ret = peripheral_i2c_read_register_byte(g_i2c_h, MODE1, &oldmode);
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to read register");

	newmode = (oldmode & 0x7F) | 0x10; // sleep
	ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE1, newmode); // go to sleep
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	ret = peripheral_i2c_write_register_byte(g_i2c_h, PRESCALE, prescale);
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE1, oldmode);
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	usleep(500);

	ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE1, (oldmode | 0x80));
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	return 0;
}

int resource_pca9685_set_value_to_channel(unsigned int channel, int on, int off)
{
	int ret = PERIPHERAL_ERROR_NONE;
	retvm_if(g_i2c_h == NULL, -1, "Not initialized yet");

	retvm_if(ch_state[channel] == PCA9685_CH_STATE_NONE, -1,
		"ch[%u] is not in used state", channel);

	ret = peripheral_i2c_write_register_byte(g_i2c_h,
			LED0_ON_L + 4*channel, on & 0xFF);
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	ret = peripheral_i2c_write_register_byte(g_i2c_h,
			LED0_ON_H + 4*channel, on >> 8);
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	ret = peripheral_i2c_write_register_byte(g_i2c_h,
			LED0_OFF_L + 4*channel, off & 0xFF);
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	ret = peripheral_i2c_write_register_byte(g_i2c_h,
			LED0_OFF_H + 4*channel, off >> 8);
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	return 0;
}

static int resource_pca9685_set_value_to_all(int on, int off)
{
	int ret = PERIPHERAL_ERROR_NONE;
	retvm_if(g_i2c_h == NULL, -1, "Not initialized yet");

	ret = peripheral_i2c_write_register_byte(g_i2c_h,
		ALL_LED_ON_L, on & 0xFF);
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	ret = peripheral_i2c_write_register_byte(g_i2c_h,
		ALL_LED_ON_H, on >> 8);
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	ret = peripheral_i2c_write_register_byte(g_i2c_h,
		ALL_LED_OFF_L, off & 0xFF);
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	ret = peripheral_i2c_write_register_byte(g_i2c_h,
		ALL_LED_OFF_H, off >> 8);
	retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");

	return 0;
}

int resource_pca9685_init(unsigned int ch)
{
	uint8_t mode1 = 0;
	int ret = PERIPHERAL_ERROR_NONE;

	if (ch == 0 || ch >= PCA9685_CH_MAX) {
		_E("channel[%u] is out of range", ch);
		return -1;
	}

	if (ch_state[ch] == PCA9685_CH_STATE_USED) {
		_E("channel[%u] is already in used state", ch);
		return -1;
	}

	if (g_i2c_h)
		goto PASS_OPEN_HANDLE;

	ret = peripheral_i2c_open(RPI3_I2C_BUS, PCA9685_ADDRESS, &g_i2c_h);
	if (ret != PERIPHERAL_ERROR_NONE) {
		_E("failed to open pca9685-[bus:%d, addr:%d]",
			RPI3_I2C_BUS, PCA9685_ADDRESS);
		return -1;
	}
	ret = resource_pca9685_set_value_to_all(0, 0);
	if (ret) {
		_E("failed to reset all value to register");
		goto ERROR;
	}

	ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE2, OUTDRV);
	if (ret != PERIPHERAL_ERROR_NONE) {
		_E("failed to write register");
		goto ERROR;
	}

	ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE1, ALLCALL);
	if (ret != PERIPHERAL_ERROR_NONE) {
		_E("failed to write register");
		goto ERROR;
	}

	usleep(500); // wait for oscillator

	ret = peripheral_i2c_read_register_byte(g_i2c_h, MODE1, &mode1);
	if (ret != PERIPHERAL_ERROR_NONE) {
		_E("failed to read register");
		goto ERROR;
	}

	mode1 = mode1 & (~SLEEP); // # wake up (reset sleep)
	ret = peripheral_i2c_write_register_byte(g_i2c_h, MODE1, mode1);
	if (ret != PERIPHERAL_ERROR_NONE) {
		_E("failed to write register");
		goto ERROR;
	}

	usleep(500); // wait for oscillator

	ret = resource_pca9685_set_frequency(60);
	if (ret) {
		_E("failed to set frequency");
		goto ERROR;
	}

PASS_OPEN_HANDLE:
	ref_count++;
	ch_state[ch] = PCA9685_CH_STATE_USED;
	_D("pca9685 - ref_count[%u]", ref_count);
	_D("sets ch[%u] used state", ch);

	return 0;

ERROR:
	if (g_i2c_h)
		peripheral_i2c_close(g_i2c_h);

	g_i2c_h = NULL;
	return -1;
}

int resource_pca9685_fini(unsigned int ch)
{
	if (ch_state[ch] == PCA9685_CH_STATE_NONE) {
		_E("channel[%u] is not in used state", ch);
		return -1;
	}
	resource_pca9685_set_value_to_channel(ch, 0, 0);
	ch_state[ch] = PCA9685_CH_STATE_NONE;

	ref_count--;
	_D("ref count - %u", ref_count);

	if (ref_count == 0 && g_i2c_h) {
		_D("finalizing pca9685");
		resource_pca9685_set_value_to_all(0, 0);
		peripheral_i2c_close(g_i2c_h);
		g_i2c_h = NULL;
	}

	return 0;
}