summaryrefslogtreecommitdiffhomepage
path: root/source/pwr_driver.c
blob: 37abbd4cabb36e2dcc09aa04b114a09413507978 (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
/*
 *       pwr_driver.c
 *
 *       Copyright (C) 2018, 2019 Mind Chasers Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   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 "board.h"
#include "pmic.h"
#include "fsl_debug_console.h"
#include "fsl_common.h"
#include "fsl_i2c.h"
#include "fsl_gpio.h"
#include "main.h"

int power_init(i2c_master_transfer_t* pXfer, uint8_t* pBuff) {

	bool repeat = false;
	uint8_t addr,data;
	uint8_t buf[2];	// write address followed by data
_loop:

	PRINTF("Power Init\r\n");

	// Read version
	addr=PMIC_VERSION;
	i2c_write(pXfer,  I2C_PMIC_ADDR, &addr, 1);
	i2c_read(pXfer,  I2C_PMIC_ADDR, (uint8_t*) pBuff, 1);
	//PRINTF("Version: 0x%2x\r\n", *pBuff);

	// Read PGOODZ
	addr=PMIC_PGOODZ;
	i2c_write(pXfer,  I2C_PMIC_ADDR, &addr, 1);
	i2c_read(pXfer,  I2C_PMIC_ADDR, (uint8_t*) pBuff, 1);
	//PRINTF("PGOODZ: 0x%2x\r\n", *pBuff);

	// Read MASK
	addr=PMIC_MASK;
	i2c_write(pXfer,  I2C_PMIC_ADDR, &addr, 1);
	i2c_read(pXfer,  I2C_PMIC_ADDR, (uint8_t*) pBuff, 1);
	//PRINTF("MASK: 0x%2x\r\n", *pBuff);

	// Read REG_CTRL
	addr=PMIC_REG_CTRL;
	i2c_write(pXfer,  I2C_PMIC_ADDR, &addr, 1);
	i2c_read(pXfer,  I2C_PMIC_ADDR, (uint8_t*) pBuff, 1);
	//PRINTF("REG_CTRL: 0x%2x\r\n", *pBuff);

	// Read CON_CTRL
	addr=PMIC_CON_CTRL;
	i2c_write(pXfer,  I2C_PMIC_ADDR, &addr, 1);
	i2c_read(pXfer,  I2C_PMIC_ADDR, (uint8_t*) pBuff, 1);
	// PRINTF("CON_CTRL: 0x%2x\r\n", *pBuff);

	// Read CON_CTRL2
	addr=PMIC_CON_CTRL2;
	i2c_write(pXfer,  I2C_PMIC_ADDR, &addr, 1);
	i2c_read(pXfer,  I2C_PMIC_ADDR, (uint8_t*) pBuff, 1);
	//PRINTF("CON_CTRL2: 0x%2x\r\n", *pBuff);

	// Read DEFCORE
	// We want 1.1V: 0xc
	addr=PMIC_DEFCORE;
	i2c_write(pXfer,  I2C_PMIC_ADDR, &addr, 1);
	i2c_read(pXfer,  I2C_PMIC_ADDR, (uint8_t*) pBuff, 1);
	//PRINTF("DEFSLEW: 0x%2x\r\n", *pBuff);

	// Read DEFSLEW
	addr=PMIC_DEFSLEW;
	i2c_write(pXfer,  I2C_PMIC_ADDR, &addr, 1);
	i2c_read(pXfer,  I2C_PMIC_ADDR, (uint8_t*) pBuff, 1);
	//PRINTF("DEFSLEW: 0x%2x\r\n", *pBuff);

	// Read LDO_CTRL
	addr=PMIC_LDO_CTRL;
	i2c_write(pXfer,  I2C_PMIC_ADDR, &addr, 1);
	i2c_read(pXfer,  I2C_PMIC_ADDR, (uint8_t*) pBuff, 1);
	//PRINTF("LDO_CTRL: 0x%2x\r\n", *pBuff);

	// Write LDO_CTRL
	addr=PMIC_LDO_CTRL;
	data=LDO2_2_5V<<4|LDO1_1_1V;
	buf[0]=addr;
	buf[1]=data;
	i2c_write(pXfer,  I2C_PMIC_ADDR, buf, 2);
	i2c_read(pXfer,  I2C_PMIC_ADDR, (uint8_t*) pBuff, 1);
	//PRINTF("LDO_CTRL: 0x%2x\r\n", *pBuff);

	GPIO_WritePinOutput(GPIOA, 19, 1);

	// Read PGOODZ
	addr=PMIC_PGOODZ;
	i2c_write(pXfer,  I2C_PMIC_ADDR, &addr, 1);
	i2c_read(pXfer,  I2C_PMIC_ADDR, (uint8_t*) pBuff, 1);
	//PRINTF("PGOODZ: 0x%2x\r\n", *pBuff);


	if (repeat) goto _loop;

	return 0;
}



X-ray Engineering Services