blob: d26bee070730a8e0bf55599cdf7dc37994dd7286 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2** Copyright (c) 2013-2017 by Silicon Laboratories, Inc.
3**
4** $Id: api_demo.c 6594 2017-06-30 22:51:35Z mjmourni $
5**
6** This file contains proprietary information.
7** No dissemination allowed without prior written permission from
8** Silicon Laboratories, Inc.
9**
10** File Description:
11**
12** This file contains example implementation and use of ProSLIC API
13**
14*/
15
16////#include "stdio.h"
17#include "api_demo.h"
18#include "user_intf.h"
19#include "spi_main.h"
20#include "SLICApp.h"
21
22#ifdef MLT_ENABLED
23#include "proslic_mlt.h"
24#endif
25
26/*
27** Device Specific Includes
28*/
29#ifdef SI3217X
30#include "si3217x.h"
31#include "vdaa.h"
32extern Si3217x_General_Cfg Si3217x_General_Configuration;
33#endif
34
35#ifdef SI_ENABLE_LOGGING
36#include <stdio.h>
37FILE *SILABS_LOG_FP;
38FILE *std_out;
39int is_stdout = 1;
40#endif
41
42#ifdef LUA
43#include <lua_util.h>
44#endif
45
46/*****************************************************************************************************/
47void change_channel(demo_state_t *pState)
48{
49 int i,m;
50 demo_port_t *cPort;
51
52 if(pState->totalChannelCount == 1)
53 {
54 printf("Only 1 channel supported, aborting request.\n");
55 return;
56 }
57
58 do
59 {
60 printf("\nPlease Enter Channel: (0->%d) %s", (pState->totalChannelCount-1),
61 PROSLIC_PROMPT);
62 fflush(stdin);
63 m = get_int(0, (pState->totalChannelCount-1));
64 }
65 while( m >= pState->totalChannelCount);
66
67 pState->currentChannel = m;
68
69 for(i = 0; i < DEMO_PORT_COUNT; i++)
70 {
71 cPort = &(pState->ports[i]);
72 if(m < ((cPort->numberOfChan) + (cPort->channelBaseIndex) ) )
73 {
74 pState->currentPort = cPort;
75 pState->currentChanPtr = cPort->channelPtrs[(m - (cPort->channelBaseIndex) ) ];
76 return;
77 }
78 }
79}
80
81demo_state_t demo_state; /* declare channel state structures */
82si_hctrl_t demo_spiGciObj; /* Link to host spi obj (os specific) */
83systemTimer_S demo_timerObj; /* Link to host timer obj (os specific)*/
84controlInterfaceType demo_ProHWIntf; /* proslic hardware interface object */
85demo_port_t *demo_ports; /* declare channel state structures */
86
87/*****************************************************************************************************/
88int API_Demo(void)
89{
90 int i = 0;
91 int rc = 0;
92
93 //Link with the global pointer
94 SLICAPP_pState = &demo_state;
95
96 SETUP_STDOUT_FLUSH;
97
98#ifdef SI_ENABLE_LOGGING
99 std_out = stdout;
100
101#ifdef ENABLE_INITIAL_LOGGING
102 SILABS_LOG_FP = fopen("api_demo.log", "w");
103 if(SILABS_LOG_FP == NULL)
104 {
105 perror("Failed to open log file - aborting");
106 rc = -2;
107 return rc;
108 }
109 is_stdout = 0;
110#else
111 SILABS_LOG_FP = stdout;
112#endif
113
114#endif
115
116 demo_ports = SIVOICE_CALLOC(sizeof(demo_port_t), DEMO_PORT_COUNT);
117 if(demo_ports == NULL)
118 {
119 printf("%s Failed to allocate memory\n", LOGPRINT_PREFIX);
120 rc = -1;
121 return rc;
122 }
123
124 demo_state.currentChannel = 0;
125 demo_state.totalChannelCount = 0;
126 demo_state.ports = demo_ports;
127 demo_state.currentPort = demo_ports;
128
129 ////PROSLIC_CLS;
130 printf("ProSLIC API Implementation Demo");
131 printf("\nCopyright 2013-2017, Silicon Labs\n");
132 printf("API Version %s\n", ProSLIC_Version());
133#ifdef MLT_ENABLED
134 printf("MLT Version: %s\n", ProSLIC_mlt_version());
135#endif
136
137 /*
138 ** --------------------------------------------------------------------
139 ** Initialize host SPI interface object (optional - platform dependent)
140 ** --------------------------------------------------------------------
141 */
142 printf("\n%sConnecting to Host -> %s ...\n", LOGPRINT_PREFIX, VMB_INFO);
143 if(SPI_Init(&demo_spiGciObj))
144 {
145 printf("%sCannot connect to %s\n",LOGPRINT_PREFIX, VMB_INFO);
146 rc = -1;
147 goto API_DEMO_EXIT_SIVOICE_FREE;
148 }
149
150 /*
151 ** ----------------------------------------------------------------------
152 ** Initialize host TIMER interface object (optional - platform dependent)
153 ** ----------------------------------------------------------------------
154 */
155 printf("%sInitializing system timer...\n", LOGPRINT_PREFIX);
156 TimerInit(&demo_timerObj);
157
158 LOGPRINT("%sLinking function pointers...\n", LOGPRINT_PREFIX);
159 initControlInterfaces(&demo_ProHWIntf, &demo_spiGciObj, &demo_timerObj);
160
161 /*
162 ** In theory, you could have multiple DEMO_PORTs aka evb boards/chipset combos.
163 ** This feature has not been tested well enough at this point...
164 */
165 for(i = 0; i < DEMO_PORT_COUNT; i++)
166 {
167 demo_init_port_info(&demo_ports[i], i);
168
169 /* We assume in our sivoice_init_port_info that we have a 178/179 - which
170 is a SLIC + VDAA device, 2 ports, if the VDAA support is disabled,
171 stop here and not allocate/init the 2nd port.
172 */
173#if defined(SI3217X) && !defined(VDAA_SUPPORT)
174 /* Subtract from #chan the device count since we doubled it */
175 demo_ports[i].numberOfChan -= demo_ports[i].numberOfDevice;
176 demo_ports[i].chanPerDevice--;
177#endif
178
179 if(demo_alloc(&demo_ports[i], &(demo_state.totalChannelCount),
180 &demo_ProHWIntf) != RC_NONE)
181 {
182 LOGPRINT("%sFailed to allocate for port %d - exiting program.\n",
183 LOGPRINT_PREFIX, i);
184 rc = -2;
185 goto API_DEMO_EXIT_SIVOICE_FREE;
186 }
187#if defined(SI3217X) && defined(VDAA_SUPPORT)
188 /* Check if we do have a 178/179 - we assume the 1st device is just like the rest... */
189 if(Si3217x_General_Configuration.daa_cntl!= 0)
190 {
191 uInt8 reg_data;
192 reg_data = SiVoice_ReadReg(*(demo_ports->channelPtrs), PROSLIC_REG_ID);
193 if( (( (reg_data>>3) & 7) != 6)
194 && ( (reg_data & 0xF) == 1 ) )
195 {
196 demo_ports->numberOfChan -= SI3217XB_NUMBER_OF_DEVICE;
197 demo_ports->chanPerDevice--;
198 demo_state.totalChannelCount -= SI3217XB_NUMBER_OF_DEVICE;
199 }
200 }
201 else
202 {
203 demo_ports->numberOfChan -= SI3217XB_NUMBER_OF_DEVICE;
204 demo_ports->chanPerDevice--;
205 demo_state.totalChannelCount -= SI3217XB_NUMBER_OF_DEVICE;
206 }
207#endif
208 printf("\tNumber of devices: %d Number of channels: %d\n",
209 demo_ports[i].numberOfDevice,
210 demo_ports[i].numberOfChan);
211
212
213 if( (demo_init_devices(&demo_ports[i]) != 0)
214 || ( demo_load_presets(&demo_ports[i]) != 0)
215 || (demo_set_chan_state(&demo_ports[i]) ) )
216 {
217 rc = -3;
218 goto API_DEMO_EXIT_demo_free;
219 }
220 }
221 demo_state.currentChanPtr = demo_ports->channels;
222
223 printf("%s Initialization Complete\n", LOGPRINT_PREFIX);
224#ifdef LUA
225 init_lua(&demo_state);
226#endif
227 if( demo_state.currentChanPtr->channelType != DAA)
228 {
229#ifdef NOFXS_SUPPORT
230 printf("Detected a FXS in a FXO only build!\n");
231 rc = -4;
232 goto API_DEMO_EXIT_demo_shutdown;
233#else
234
235 /*
236 ** Main Demo Menu
237 */
238#ifdef PROSLIC_API_TEST
239 proslic_main_menu(&demo_state);
240#else
241 extern void SLICAPP_Main(void);
242 SLICAPP_Main();
243#endif
244#endif
245 }
246#ifdef VDAA_SUPPORT
247 else
248 {
249 daa_main_menu(&demo_state);
250 }
251#endif
252
253API_DEMO_EXIT_demo_shutdown:
254 for(i = 0; i < DEMO_PORT_COUNT; i++)
255 {
256 demo_shutdown(&demo_ports[i]);
257 }
258
259 SiVoice_Reset(demo_state.currentChanPtr);
260
261API_DEMO_EXIT_demo_free:
262 for(i = 0; i < DEMO_PORT_COUNT; i++)
263 {
264 demo_free(&demo_ports[i]);
265 }
266
267API_DEMO_EXIT_SIVOICE_FREE:
268 SIVOICE_FREE(demo_ports);
269 demo_ports = (demo_port_t *)NULL;
270
271 return rc;
272}
273