LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 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 */
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 */
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 */
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;
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 {
142  int ret;
143 
144  /* Initialize I2C */
149 
150  /* Initialize the default values */
152 
153  if (ret) {
155  }
156 
157  if (ret) {
159  }
160 
161  if (ret && input) {
162  /* Disable Power On for ADCR, PGAR, PGAL to get mic sound more clearly */
165 
166  if (ret) {
169  }
170  }
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