| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
uda1380.c
Go to the documentation of this file.
1 /*
2 * @brief UDA1380 Audio codec interface file
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2012
6 * All rights reserved.
7 *
8 * @par
9 * Software that is described herein is for illustrative purposes only
10 * which provides customers with programming information regarding the
11 * LPC products. This software is supplied "AS IS" without any warranties of
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
13 * all warranties, express or implied, including all implied warranties of
14 * merchantability, fitness for a particular purpose and non-infringement of
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
16 * or liability for the use of the software, conveys no license or rights under any
17 * patent, copyright, mask work right, or any other intellectual property rights in
18 * or to any products. NXP Semiconductors reserves the right to make changes
19 * in the software without notification. NXP Semiconductors also makes no
20 * representation or warranty that such application will be suitable for the
21 * specified use without further testing or modification.
22 *
23 * @par
24 * Permission to use, copy, modify, and distribute this software and its
25 * documentation is hereby granted, under NXP Semiconductors' and its
26 * licensor's relevant copyrights in the software, without fee, provided that it
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
28 * copyright, permission, and disclaimer notice must appear in all copies of
29 * this code.
30 */
31
32 #include "board.h"
33
37 /*****************************************************************************
38 * Private types/enumerations/variables
39 ****************************************************************************/
40 /* Defalut UDA values */
41 /* System Register Data Set */
42 static const uint8_t UDA_sys_regs_dat[] = {
43 UDA_EVALM_CLK, /* Register to which following data be written */
44 UDA1380_U8(UDA1380_REG_EVALCLK_DEFAULT_VALUE),
45 UDA1380_U8(UDA1380_REG_I2S_DEFAULT_VALUE),
46 UDA1380_U8(UDA1380_REG_PWRCTRL_DEFAULT_VALUE),
47 UDA1380_U8(UDA1380_REG_ANAMIX_DEFAULT_VALUE),
48 UDA1380_U8(UDA1380_REG_HEADAMP_DEFAULT_VALUE)
49 };
50
51 /* System Register Data Set */
52 static const uint8_t UDA_interfil_regs_dat[] = {
53 UDA_MASTER_VOL_CTRL,/* Register to which following data be written */
54 UDA1380_U8(UDA1380_REG_MSTRVOL_DEFAULT_VALUE),
55 UDA1380_U8(UDA1380_REG_MIXVOL_DEFAULT_VALUE),
56 UDA1380_U8(UDA1380_REG_MODEBBT_DEFAULT_VALUE),
57 UDA1380_U8(UDA1380_REG_MSTRMUTE_DEFAULT_VALUE),
58 UDA1380_U8(UDA1380_REG_MIXSDO_DEFAULT_VALUE)
59 };
60
61 /* decimator Register Data Set */
62 static const uint8_t UDA_decimator_regs_dat[] = {
63 UDA_DEC_VOL_CTRL, /* Register to which following data be written */
64 UDA1380_U8(UDA1380_REG_DECVOL_DEFAULT_VALUE),
65 UDA1380_U8(UDA1380_REG_PGA_DEFAULT_VALUE),
66 UDA1380_U8(UDA1380_REG_ADC_DEFAULT_VALUE),
67 UDA1380_U8(UDA1380_REG_AGC_DEFAULT_VALUE)
68 };
69
70 /*****************************************************************************
71 * Public types/enumerations/variables
72 ****************************************************************************/
73
74 /*****************************************************************************
75 * Private functions
76 ****************************************************************************/
77 /* Set the default values to the codec registers */
78 static int Audio_Codec_SetDefaultValues(const uint8_t *values, int sz)
79 {
80 int ret;
81 uint8_t buff[10]; /* Verification buffer */
82
83 /* Set System register's default values */
84 ret = UDA1380_REG_WriteMult(values, sz);
85 if (ret) {
86 ret = UDA1380_REG_VerifyMult(values[0], &values[1], buff, sz - 1);
87 }
88 return ret;
89 }
90
91 /*****************************************************************************
92 * Public functions
93 ****************************************************************************/
94 /* Write data to UDA register */
95 void UDA1380_REG_Write(uint8_t reg, uint16_t val)
96 {
97 uint8_t dat[3];
98 dat[0] = reg; dat[1] = val >> 8; dat[2] = val & 0xFF;
99 Chip_I2C_MasterSend(UDA1380_I2C_BUS, I2CDEV_UDA1380_ADDR, dat, sizeof(dat));
100 }
101
102 /* Read data from UDA register */
103 uint16_t UDA1380_REG_Read(uint8_t reg) {
104 uint8_t rx_data[2];
105 if (Chip_I2C_MasterCmdRead(UDA1380_I2C_BUS, I2CDEV_UDA1380_ADDR, reg, rx_data, 2) == 2) {
106 return (rx_data[0] << 8) | rx_data[1];
107 }
108 return 0;
109 }
110
111 /* Write data to UDA register and verify the value by reading it back */
112 int UDA1380_REG_WriteVerify(uint8_t reg, uint16_t val)
113 {
114 uint16_t ret;
115 UDA1380_REG_Write(reg, val);
116 ret = UDA1380_REG_Read(reg);
117 return ret == val;
118 }
119
120 /* Multiple value verification function */
121 int UDA1380_REG_VerifyMult(uint8_t reg, const uint8_t *value, uint8_t *buff, int len)
122 {
123 int i;
124 if (Chip_I2C_MasterCmdRead(UDA1380_I2C_BUS, I2CDEV_UDA1380_ADDR, reg, buff, len) != len) {
125 return 0; /* Partial read */
126
127 }
128 /* Compare the values */
129 for (i = 0; i < len; i++) {
130 if (value[i] != buff[i]) {
131 break;
132 }
133 }
134
135 return i == len;
136 }
137
138 /* UDA1380 initialize function */
139 int UDA1380_Init(int input)
140 {
141 I2C_EVENTHANDLER_T old = Chip_I2C_GetMasterEventHandler(UDA1380_I2C_BUS);
142 int ret;
143
144 /* Initialize I2C */
145 Board_I2C_Init(UDA1380_I2C_BUS);
146 Chip_I2C_Init(UDA1380_I2C_BUS);
147 Chip_I2C_SetClockRate(UDA1380_I2C_BUS, 100000);
148 Chip_I2C_SetMasterEventHandler(UDA1380_I2C_BUS, Chip_I2C_EventHandlerPolling);
149
150 /* Initialize the default values */
151 ret = Audio_Codec_SetDefaultValues(UDA_sys_regs_dat, sizeof(UDA_sys_regs_dat));
152
153 if (ret) {
154 ret = Audio_Codec_SetDefaultValues(UDA_interfil_regs_dat, sizeof(UDA_interfil_regs_dat));
155 }
156
157 if (ret) {
158 ret = Audio_Codec_SetDefaultValues(UDA_decimator_regs_dat, sizeof(UDA_decimator_regs_dat));
159 }
160
161 if (ret && input) {
162 /* Disable Power On for ADCR, PGAR, PGAL to get mic sound more clearly */
163 ret = UDA1380_REG_WriteVerify(UDA_POWER_CTRL,
164 UDA1380_REG_PWRCTRL_DEFAULT_VALUE & (~(0x0B)));
165
166 if (ret) {
167 ret = UDA1380_REG_WriteVerify(UDA_ADC_CTRL,
168 UDA1380_REG_ADC_DEFAULT_VALUE | input);
169 }
170 }
171 Chip_I2C_SetMasterEventHandler(UDA1380_I2C_BUS, old);
172
173 return ret;
174 }
175
176 /* Write multiple registers in one go */
177 int UDA1380_REG_WriteMult(const uint8_t *buff, int len)
178 {
179 return Chip_I2C_MasterSend(UDA1380_I2C_BUS, I2CDEV_UDA1380_ADDR, buff, len) == len;
180 }
181