Carmel (MAXREFDES18#) Code Documentation  V01.00
High Accuracy Analog Current/Voltage Output
 All Files Functions Variables Macros Pages
utilities.c
Go to the documentation of this file.
1 
29 /*
30  * Copyright (C) 2012 Maxim Integrated Products, Inc., All Rights Reserved.
31  *
32  * Permission is hereby granted, free of charge, to any person obtaining a
33  * copy of this software and associated documentation files (the "Software"),
34  * to deal in the Software without restriction, including without limitation
35  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36  * and/or sell copies of the Software, and to permit persons to whom the
37  * Software is furnished to do so, subject to the following conditions:
38  *
39  * The above copyright notice and this permission notice shall be included
40  * in all copies or substantial portions of the Software.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
43  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45  * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
46  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
47  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
48  * OTHER DEALINGS IN THE SOFTWARE.
49  *
50  * Except as contained in this notice, the name of Maxim Integrated Products
51  * shall not be used except as stated in the Maxim Integrated Products
52  * Branding Policy.
53  *
54  * The mere transfer of this software does not imply any licenses
55  * of trade secrets, proprietary technology, copyrights, patents,
56  * trademarks, maskwork rights, or any other form of intellectual
57  * property whatsoever. Maxim Integrated Products retains all ownership rights.
58  *
59  ***************************************************************************/
60 
61 #include "utilities.h"
62 #include "MAXREFDES18.h"
63 
64 void print_asterisks(int nQuantity)
72 {
73  int i=0;
74  for(i=0;i<nQuantity;i++)
75  printf("*");
76 }
77 
78 int SpiRW( u32 unPeripheralAddressSPI, unsigned int unCPHA, unsigned int unCPOL,
79  u8* auchWriteBuf, u8* auchReadBuf, int unNumBytes, u8 uchCsActiveHigh, u32 unSpiSS)
102 {
103  int i;
104  unsigned int unControlData = 0x00000186;
105 
106  //If CPHA or CPOL = 1, we need to set the corresponding bits in the control register
107  unControlData = unControlData | (unCPHA << 4);
108  unControlData = unControlData | (unCPOL << 3);
109 
110  //Write config data to SPICR. We need the inhibit bit=1.
111  XSpi_WriteReg(unPeripheralAddressSPI, 0x60, unControlData);
112 
113  //Deassert CS to 1 to ensure SPI slave is inactive
114  if( uchCsActiveHigh )
115  XSpi_WriteReg(unPeripheralAddressSPI, 0x70, unSpiSS == 0x1? 0xFFFFFFFE : 0xFFFFFFD);
116  else
117  XSpi_WriteReg(unPeripheralAddressSPI, 0x70, 0xFFFFFFFF);
118 
119  for( i = 0; i < unNumBytes; i++)
120  {
121  if( auchWriteBuf != 0 )
122  {
123  //Write data to SPIDTR. This is the data that will be transferred to the SPI slave.
124  XSpi_WriteReg(unPeripheralAddressSPI, 0x68, auchWriteBuf[ i ]);
125  //Debug//printf( "Write %02x; ", auchWriteBuf[i]);
126  }
127  else
128  {
129  //Write data to SPIDTR. This is the data that will be transferred to the SPI slave.
130  XSpi_WriteReg(unPeripheralAddressSPI, 0x68, 0x00);
131  }
132 
133  //Write config data to SPICR. We need the inhibit bit=1.
134  XSpi_WriteReg(unPeripheralAddressSPI, 0x60, unControlData);
135 
136  //Assert CS for our PMOD part
137  if( uchCsActiveHigh )
138  XSpi_WriteReg(unPeripheralAddressSPI, 0x70, 0xFFFFFFFF);
139  else
140  XSpi_WriteReg(unPeripheralAddressSPI, 0x70, unSpiSS == 0x1? 0xFFFFFFFE : 0xFFFFFFFD);
141 
142  //Un-inhibit our SPI master to transfer the data
143  XSpi_WriteReg(unPeripheralAddressSPI, 0x60, unControlData & 0xFFFFFEFF);
144 
145  //Wait for transaction to complete. Check to see if Tx_Empty flag is set before proceeding.
146  while( !(XSpi_ReadReg( unPeripheralAddressSPI, 0x64 ) & 0x00000004) )
147  ;
148 
149  //Inhibit SPI master to prevent further action
150  XSpi_WriteReg(unPeripheralAddressSPI, 0x60, unControlData);
151 
152  //Read received data
153  if( (auchReadBuf != 0) )
154  {
155  auchReadBuf[ i ] = XSpi_ReadReg(unPeripheralAddressSPI, 0x6C);
156  //Debug//printf( "Read %02x\r\n", auchReadBuf[i]);
157  }
158  }
159  if( uchCsActiveHigh )
160  XSpi_WriteReg(unPeripheralAddressSPI, 0x70, unSpiSS == 0x1? 0xFFFFFFFE : 0xFFFFFFFD);
161  else
162  XSpi_WriteReg(unPeripheralAddressSPI, 0x70, 0xFFFFFFFF);
163  return 0;
164 }
165 
166 void delay(int nStopValue)
177 {
178  int i=0;
179  int a=0;
180 
181  for(i=0;i<nStopValue;i++)
182  {
183  a=i;
184  }
185 }
186 
187 void led_knight_rider(XGpio *pLED_GPIO, int nNumberOfTimes)
198 {
199  int i=0;
200  int j=0;
201  u8 uchLedStatus=0;
202 
203  // Blink the LEDs back and forth nNumberOfTimes
204  for(i=0;i<nNumberOfTimes;i++)
205  {
206  for(j=0;j<8;j++) // Scroll the LEDs up
207  {
208  uchLedStatus = 1 << j;
209  XGpio_DiscreteWrite(pLED_GPIO, 1, uchLedStatus);
210  delay(ABOUT_ONE_SECOND / 15);
211  }
212 
213  for(j=0;j<8;j++) // Scroll the LEDs up
214  {
215  uchLedStatus = 8 >> j;
216  XGpio_DiscreteWrite(pLED_GPIO, 1, uchLedStatus);
217  delay(ABOUT_ONE_SECOND / 15);
218  }
219  }
220 }
221 
222 void max_configure_PMOD_port(u8 uchPmodPortA, u8 uchPmodPortB, u8 uchPmodPortC, u8 uchPmodPortD)
241 {
242  XGpio xGpioPmodPortMuxIO;
243  u8 uchPmodPortSelectBits=0;
244  // The PMOD ports are configured with an 8 bit word sent to GPIO2
245  // Bits 1:0 are for port #A
246  // Bits 3:2 are for port #B
247  // Bits 5:4 are for port #C
248  // Bits 7:6 are for Port #D
249 
250  //S = 2'b00 => UART active (PMOD_PORT_TYPE_UART)
251  //S= 2'b01 => SPI active (PMOD_PORT_TYPE_SPI)
252  //S = 2'b10 => GPIO active (PMOD_PORT_TYPE_GPIO)
253  //S = 2'b11 => I2C active (PMOD_PORT_TYPE_I2C)
254 
255  uchPmodPortSelectBits = (uchPmodPortD << 6) + (uchPmodPortC << 4) + (uchPmodPortB << 2) + uchPmodPortA;
256 
257  XGpio_Initialize(&xGpioPmodPortMuxIO, XPAR_AXI_GPIO_2_DEVICE_ID);
258  XGpio_SetDataDirection(&xGpioPmodPortMuxIO, 1, 0x00); // Set the GPIO2 direction to outputs
259  XGpio_DiscreteWrite(&xGpioPmodPortMuxIO, 1, uchPmodPortSelectBits);
260 }
261 
262 int number_raised_to_power(int nBase, int nExponent)
275 {
276  int i=0;
277  int nValue=0;
278  if(nExponent==0)
279  nValue=1;
280  else
281  {
282  nValue = nBase;
283  for(i=1;i<nExponent;i++)
284  {
285  nValue = nValue * nBase;
286  }
287  }
288  return(nValue);
289 }
290 
291 int receive_byte_with_timeout(u32 unUartAddress, int nTimeoutInTenthsOfSeconds, u8 *uchRxData)
301 {
302  int j=0;
303  int nReturnVal = TRUE;
304  u8 uchInput = 0;
305 
306  // Check if there is a character in the UART Rx buffer
307  // Continue checking every 10th of a second for nTimeoutInSeconds
308  while(XUartLite_IsReceiveEmpty(unUartAddress) && (j < nTimeoutInTenthsOfSeconds))
309  {
310  j++;
312  }
313 
314  if(XUartLite_IsReceiveEmpty(unUartAddress))
315  nReturnVal = FALSE;
316  else
317  {
318  uchInput = XUartLite_RecvByte(unUartAddress);
319  // Check if it is an escape sequence
320  if(uchInput==27) // Escape sequence (likely an arrow key)
321  {
322  if(XUartLite_IsReceiveEmpty(unUartAddress))
323  nReturnVal = FALSE;
324  else
325  {
326  uchInput = XUartLite_RecvByte(unUartAddress);
327  if(uchInput==91) // Left bracket (part #2 of the 3 part escape sequence)
328  {
329  if(XUartLite_IsReceiveEmpty(unUartAddress))
330  nReturnVal = FALSE;
331  else
332  {
333  uchInput = XUartLite_RecvByte(unUartAddress);
334  if(uchInput==75)
335  uchInput = 244; // We have defined KEYPRESS_END as 244
336  }
337  }
338  }
339 
340  }
341  *uchRxData = uchInput;
342  nReturnVal = TRUE;
343  }
344  return(nReturnVal);
345 }
346 
347 int GetLine( char* sInputString, unsigned int unMaxSize )
358 {
359  u8 uchInputChar = 0;
360  unsigned int unInputStringIndex = 0;
361 
363  while( (uchInputChar != '\r') && (uchInputChar != '\n') && (unInputStringIndex < unMaxSize-1) )
364  {
365  sInputString[ unInputStringIndex ] = (char)uchInputChar;
366  unInputStringIndex++;
368  }
369  sInputString[ unInputStringIndex ] = '\0';
370 
371  return (unInputStringIndex < unMaxSize) ? unInputStringIndex : -1;
372 }