Sample SPI drivers for a number of the Adesto Technologies flash devices.
moneta.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 
41 #include "moneta.h"
42 
43 #if (PARTNO == RM331x) || \
44  (ALL == 1)
45 
46 static bool DISPLAY_OUTPUT = 0;
47 
48 static void debugOn() {DISPLAY_OUTPUT = 1;};
49 static void debugOff() {DISPLAY_OUTPUT = 0;};
50 
52 
54 {
55  uint8_t SR[2];
56  do
57  {
58  monetaReadSR(SR);
59  SPI_Delay(10);
60  }
61  while(SR[0] & 1);
62 }
63 
65 {
67  SPI_Exchange(txMonetaInternalBuffer, 1, NULL, 0, 0);
68  if(DISPLAY_OUTPUT)
69  {
71  }
72 }
73 
74 
76 {
78  SPI_Exchange(txMonetaInternalBuffer, 1, NULL, 0, 0);
79  if(DISPLAY_OUTPUT)
80  {
82  }
83 }
84 
85 
86 void monetaReadSR(uint8_t *rxBuffer)
87 {
89  SPI_Exchange(txMonetaInternalBuffer, 1, rxBuffer, 2, 0);
90  if(DISPLAY_OUTPUT)
91  {
93  }
94 }
95 
96 
97 void monetaWriteSRB1(uint8_t data)
98 {
100  txMonetaInternalBuffer[1] = data;
101  SPI_Exchange(txMonetaInternalBuffer, 2, NULL, 0, 0);
102  if(DISPLAY_OUTPUT)
103  {
105  }
106 }
107 
108 
109 void monetaWriteSRB2(uint8_t data)
110 {
112  txMonetaInternalBuffer[1] = data;
113  SPI_Exchange(txMonetaInternalBuffer, 2, NULL, 0, 0);
114  if(DISPLAY_OUTPUT)
115  {
117  }
118 }
119 
120 
121 void monetaReadArray(uint16_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
122 {
124  txMonetaInternalBuffer[1] = (uint8_t) (address >> 8);
125  txMonetaInternalBuffer[2] = (uint8_t) address;
126  SPI_Exchange(txMonetaInternalBuffer, 3, rxBuffer, rxNumBytes, 0);
127  if(DISPLAY_OUTPUT)
128  {
129  printSPIExchange(txMonetaInternalBuffer, 3, rxBuffer, rxNumBytes);
130  }
131 }
132 
133 
134 void monetaWriteArray(uint16_t address, uint8_t *txBuffer, uint32_t txNumBytes)
135 {
136  // TODO: Eliminate this copy. I think this needs to be done at the lower level,
137  // possibly by passing in the opcode and data separately.
139  txMonetaInternalBuffer[1] = (uint8_t) (address >> 8);
140  txMonetaInternalBuffer[2] = (uint8_t) address;
141  // Offset the data bytes by 3; opcode+address takes up the first 3 bytes of a transmission.
142  uint32_t totalBytes = txNumBytes + 3;
143  uint32_t i;
144  uint32_t j;
145  for(i = 3, j = 0; i < totalBytes; i++, j++)
146  {
147  txMonetaInternalBuffer[i] = txBuffer[j];
148  }
149  SPI_Exchange(txMonetaInternalBuffer, totalBytes, NULL, 0, 0);
150  if(DISPLAY_OUTPUT)
151  {
152  printSPIExchange(txMonetaInternalBuffer, totalBytes, NULL, 0);
153  }
154 }
155 
156 
157 void monetaReadMID(uint8_t *rxBuffer)
158 {
160  SPI_Exchange(txMonetaInternalBuffer, 1, rxBuffer, 8, 0);
161  if(DISPLAY_OUTPUT)
162  {
163  printSPIExchange(txMonetaInternalBuffer, 1, rxBuffer, 8);
164  }
165 }
166 
168 {
170  SPI_Exchange(txMonetaInternalBuffer, 1, NULL, 0, 0);
171  if(DISPLAY_OUTPUT)
172  {
174  }
175 }
176 
178 {
179  SPI_JEDECReset();
180  if(DISPLAY_OUTPUT)
181  {
182  printf("\n\n\nPerformed hardware reset.\n");
183  }
184 }
185 
186 #endif
void monetaWaitOnReady()
: Wait while Read/Busy Status bit in SRB is 1 (device is busy).
Definition: moneta.c:53
#define CMD_MONETA_UDPD_MODE1
Definition: cmd_defs.h:106
void SPI_JEDECReset()
Performs a JEDEC reset on the SPI device.
Definition: spi_driver.c:492
#define CMD_MONETA_WRITE_DISABLE
Definition: cmd_defs.h:99
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...
void monetaReadArray(uint16_t address, uint8_t *rxBuffer, uint32_t rxNumBytes)
OPCODE: 0x03 Reads rxNumBytes starting from location &#39;address&#39; and stores the data in the byte arra...
Definition: moneta.c:121
#define CMD_MONETA_READ_SRB1
Definition: cmd_defs.h:100
void monetaHardwareReset()
Exits Ultra Deep Power Down mode using the hardware reset option.
Definition: moneta.c:177
#define CMD_MONETA_WRITE_SRB2
Definition: cmd_defs.h:102
#define CMD_MONETA_WRITE_ARRAY
Definition: cmd_defs.h:104
void monetaWriteSRB2(uint8_t data)
OPCODE: 0x31 Writes the value in data to status register byte 2.
Definition: moneta.c:109
Declarations of Moneta functions.
void monetaWriteDisable()
OPCODE: 0x04 Sends opcode to disable writing.
Definition: moneta.c:75
#define MAXIMUM_TX_BYTES
Definition: cmd_defs.h:46
#define CMD_MONETA_WRITE_SRB1
Definition: cmd_defs.h:101
#define CMD_MONETA_WRITE_ENABLE
Definition: cmd_defs.h:98
void monetaReadMID(uint8_t *rxBuffer)
OPCODE: 0x9F Reads the manufacturer ID and stores the data in rxBuffer.
Definition: moneta.c:157
void monetaWriteSRB1(uint8_t data)
OPCODE: 0x01 Writes the value in data to status register byte 1.
Definition: moneta.c:97
void SPI_Delay(uint32_t delayTime)
Performs a delayTime number of NOPs.
Definition: spi_driver.c:140
void monetaWriteEnable()
OPCODE: 0x06 Sends opcode to enable writing.
Definition: moneta.c:64
void monetaReadSR(uint8_t *rxBuffer)
OPCODE: 0x05 Reads the value in the status registers.
Definition: moneta.c:86
#define CMD_MONETA_READ_MID
Definition: cmd_defs.h:105
void monetaWriteArray(uint16_t address, uint8_t *txBuffer, uint32_t txNumBytes)
OPCODE: 0x02 Writes txNumBytes bytes of data starting at the address indicated by address...
Definition: moneta.c:134
#define CMD_MONETA_READ_ARRAY
Definition: cmd_defs.h:103
void monetaUDPDMode1()
OPCODE: 0x79 Sends the device into Ultra Deep Power Down Mode 1. Exit UDPD Mode 1 by power cycling ...
Definition: moneta.c:167
void SPI_Exchange(uint8_t *txBuffer, uint32_t txNumBytes, uint8_t *rxBuffer, uint32_t rxNumBytes, uint32_t dummyNumBytes)
Sends and receives bytes based on the function parameters. MISO and MOSI fill their standard SPI role...
Definition: spi_driver.c:325
uint8_t txMonetaInternalBuffer[MAXIMUM_TX_BYTES]
Definition: moneta.c:49