Sample SPI drivers for a number of the Adesto Technologies flash devices.
helper_functions.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2018 Adesto Technologies Corporation, Inc
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted (subject to the limitations in the disclaimer below) provided
8  * that the following conditions are met:
9  *
10  * o Redistributions of source code must retain the above copyright notice, this list
11  * of conditions and the following disclaimer.
12  *
13  * o Redistributions in binary form must reproduce the above copyright notice, this
14  * list of conditions and the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  *
17  * o Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from this
19  * software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
26  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
42 #include "helper_functions.h"
43 
44 void displayByteArray(uint8_t *byteArray, uint32_t numBytes)
45 {
46  printSPIExchange(NULL, 0, byteArray, numBytes);
47 }
48 
49 void fillArrayPattern(uint8_t * byteArray, uint32_t numBytes, int seedNumber)
50 {
51  for(int i = 0; i < numBytes; i++)
52  {
53  byteArray[i] = (uint8_t)(seedNumber+i);
54  }
55 }
56 
57 void fillArrayConst(uint8_t * byteArray, uint32_t numBytes, int constantNum)
58 {
59  for(int i = 0; i < numBytes; i++)
60  {
61  byteArray[i] = (uint8_t)(constantNum);
62  }
63 }
64 
65 bool compareByteArrays(uint8_t *arr1, uint8_t *arr2, uint32_t arrLength)
66 {
67  uint32_t numErrors = 0;
68  for(int i = 0; i < arrLength; i++)
69  {
70  if(arr1[i] != arr2[i])
71  {
72  printf("Mismatch @ index: %d - Array 1: 0x%02X | Array 2: 0x%02X\n", i, arr1[i], arr2[i]);
73  numErrors++;
74  }
75  }
76  printf("Byte comparison total mismatches: %d\n", numErrors);
77  return (numErrors == 0) ? 1 : 0;
78 }
79 
80 void load4BytesToTxBuffer(uint8_t *txBuffer, uint8_t opcode, uint32_t address)
81 {
82  txBuffer[0] = opcode;
83  txBuffer[1] = (uint8_t) (address >> 16);
84  txBuffer[2] = (uint8_t) (address >> 8);
85  txBuffer[3] = (uint8_t) address;
86 }
87 
88 // TODO: Make this faster.
89 void printSPIExchange(uint8_t *txBuffer,
90  uint32_t txNumBytes,
91  uint8_t *rxBuffer,
92  uint32_t rxNumBytes)
93 {
94  if(txNumBytes != 0)
95  {
96  printf("\nSent bytes (0x):");
97  for(uint32_t i = 0; i < txNumBytes; i++)
98  {
99  // %4 to print every 4 byte word
100  if(i%1 == 0)
101  {
102  printf(" ");
103  }
104  if(i%16 == 0)
105  {
106  printf("\n");
107  }
108  printf("%02X", txBuffer[i]);
109  }
110  }
111  if(rxNumBytes != 0)
112  {
113  printf("\nReceived bytes (0x):");
114  for(uint32_t i = 0; i < rxNumBytes; i++)
115  {
116  // %4 to print every word (4 bytes)
117  if(i%1 == 0)
118  {
119  printf(" ");
120  }
121  if(i%16 == 0)
122  {
123  printf("\n");
124  }
125  printf("%02X", rxBuffer[i]);
126  }
127  }
128  printf("\n");
129 }
void displayByteArray(uint8_t *byteArray, uint32_t numBytes)
Helper function to display a byte array/buffer. This function uses the printSPIExchange() function in...
void printSPIExchange(uint8_t *txBuffer, uint32_t txNumBytes, uint8_t *rxBuffer, uint32_t rxNumBytes)
Prints the byte array in hexadecimal with a formatted output. Indicates what bytes were sent...
bool compareByteArrays(uint8_t *arr1, uint8_t *arr2, uint32_t arrLength)
Helper function to compare 2 byte arrays and print an error message if they do not match...
void load4BytesToTxBuffer(uint8_t *txBuffer, uint8_t opcode, uint32_t address)
Loads 1 byte of opcode followed by 3 address bytes into the txBuffer. The data is stored at the first...
void fillArrayPattern(uint8_t *byteArray, uint32_t numBytes, int seedNumber)
Helper function to display fill a byte array/buffer with a pattern based on a seedNumber. The seed will be incremented by the index of the array.
Declarations of helper functions.
void fillArrayConst(uint8_t *byteArray, uint32_t numBytes, int constantNum)
Helper function to display fill a byte array/buffer with a constant number.