Paparazzi UAS  v5.15_devel-88-gb3ad7fe
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
eeprom25AA256.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Gautier Hattenberger
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  */
21 
32 
35 typedef enum {
36  EEPROM_READ = 0x3,
37  EEPROM_WRITE = 0x2,
38  EEPROM_WRDI = 0x4,
39  EEPROM_WREN = 0x6,
40  EEPROM_RDSR = 0x5,
43 
44 // Init function
45 void eeprom25AA256_init(struct Eeprom25AA256 *eeprom, struct spi_periph *spi_p, uint8_t slave_idx)
46 {
47  /* set spi_peripheral */
48  eeprom->spi_p = spi_p;
49 
50  /* configure spi transaction */
51  eeprom->spi_trans.cpol = SPICpolIdleLow;
52  eeprom->spi_trans.cpha = SPICphaEdge1;
53  eeprom->spi_trans.dss = SPIDss8bit;
54  eeprom->spi_trans.bitorder = SPIMSBFirst;
55  eeprom->spi_trans.cdiv = SPIDiv128; // f_PCLK / div
56 
58  eeprom->spi_trans.slave_idx = slave_idx;
59  eeprom->spi_trans.output_length = 0;
60  eeprom->spi_trans.input_length = 0;
61  eeprom->spi_trans.before_cb = NULL;
62  eeprom->spi_trans.after_cb = NULL;
63  eeprom->spi_trans.input_buf = &(eeprom->rx_buf[0]);
64  eeprom->spi_trans.output_buf = &(eeprom->tx_buf[0]);
65 
66  /* set inital status: Success or Done */
67  eeprom->spi_trans.status = SPITransDone;
68 
69 }
70 
71 // Read data
72 void eeprom25AA256_read(struct Eeprom25AA256 *eeprom, uint16_t addr, uint16_t length)
73 {
74  if (E25_OUT_BUFFER_LEN < 3 || E25_IN_BUFFER_LEN < (length + 3)) {
75  return; // Buffer or too small
76  }
77  eeprom->tx_buf[0] = EEPROM_READ;
78  eeprom->tx_buf[1] = (addr >> 8) & 0xff;
79  eeprom->tx_buf[2] = addr & 0xff;
80 
81  if (eeprom->spi_trans.status == SPITransDone) {
82  eeprom->spi_trans.output_length = 3;
83  eeprom->spi_trans.input_length = length + 3;
84  spi_submit(eeprom->spi_p, &(eeprom->spi_trans));
85  }
86 }
87 
88 // Check end of transaction
89 void eeprom25AA256_event(struct Eeprom25AA256 *eeprom)
90 {
91  // TODO better report status
92  if (eeprom->spi_trans.status == SPITransFailed) {
93  // Fail reading
94  eeprom->spi_trans.status = SPITransDone;
95  } else if (eeprom->spi_trans.status == SPITransSuccess) {
96  // Successfull reading
97  eeprom->data_available = true;
98  eeprom->spi_trans.status = SPITransDone;
99  }
100 }
101 
unsigned short uint16_t
Definition: types.h:16
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
uint16_t output_length
number of data words to write
Definition: spi.h:146
void eeprom25AA256_init(struct Eeprom25AA256 *eeprom, struct spi_periph *spi_p, uint8_t slave_idx)
Init function.
Definition: eeprom25AA256.c:45
struct spi_transaction spi_trans
spi transaction
Definition: eeprom25AA256.h:59
void eeprom25AA256_read(struct Eeprom25AA256 *eeprom, uint16_t addr, uint16_t length)
Read function.
Definition: eeprom25AA256.c:72
Eeprom25AA256InstructionSet
Instruction set.
Definition: eeprom25AA256.c:35
void eeprom25AA256_event(struct Eeprom25AA256 *eeprom)
Event function.
Definition: eeprom25AA256.c:89
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:505
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
struct spi_periph * spi_p
spi peripheral
Definition: eeprom25AA256.h:58
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
bool data_available
data read flag
Definition: eeprom25AA256.h:62
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
#define E25_OUT_BUFFER_LEN
Output buffer length.
Definition: eeprom25AA256.h:52
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
Driver for the eeprom 225AA256 (and 25LC256) 256K SPI bus serial EEPROM from Microchip.
SPI peripheral structure.
Definition: spi.h:168
Definition: spi.h:84
#define E25_IN_BUFFER_LEN
Input buffer length.
Definition: eeprom25AA256.h:43
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155
25AA256 eeprom structure
Definition: eeprom25AA256.h:57
CPOL = 0.
Definition: spi.h:77
uint16_t input_length
number of data words to read
Definition: spi.h:145
unsigned char uint8_t
Definition: types.h:14
volatile uint8_t rx_buf[E25_IN_BUFFER_LEN]
receive buffer
Definition: eeprom25AA256.h:61
CPHA = 0.
Definition: spi.h:68
slave is selected before transaction and unselected after
Definition: spi.h:57
volatile uint8_t tx_buf[E25_OUT_BUFFER_LEN]
transmit buffer
Definition: eeprom25AA256.h:60
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:154
Definition: spi.h:120
enum SPITransactionStatus status
Definition: spi.h:156