Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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,
41  EEPROM_WRSR = 0x1
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 
Eeprom25AA256InstructionSet
Instruction set.
Definition: eeprom25AA256.c:35
@ EEPROM_READ
Definition: eeprom25AA256.c:36
@ EEPROM_WREN
Definition: eeprom25AA256.c:39
@ EEPROM_WRDI
Definition: eeprom25AA256.c:38
@ EEPROM_RDSR
Definition: eeprom25AA256.c:40
@ EEPROM_WRITE
Definition: eeprom25AA256.c:37
@ EEPROM_WRSR
Definition: eeprom25AA256.c:41
void eeprom25AA256_event(struct Eeprom25AA256 *eeprom)
Event function.
Definition: eeprom25AA256.c:89
void eeprom25AA256_init(struct Eeprom25AA256 *eeprom, struct spi_periph *spi_p, uint8_t slave_idx)
Init function.
Definition: eeprom25AA256.c:45
void eeprom25AA256_read(struct Eeprom25AA256 *eeprom, uint16_t addr, uint16_t length)
Read function.
Definition: eeprom25AA256.c:72
Driver for the eeprom 225AA256 (and 25LC256) 256K SPI bus serial EEPROM from Microchip.
#define E25_IN_BUFFER_LEN
Input buffer length.
Definition: eeprom25AA256.h:43
struct spi_periph * spi_p
spi peripheral
Definition: eeprom25AA256.h:58
struct spi_transaction spi_trans
spi transaction
Definition: eeprom25AA256.h:59
volatile uint8_t tx_buf[E25_OUT_BUFFER_LEN]
transmit buffer
Definition: eeprom25AA256.h:60
volatile uint8_t rx_buf[E25_IN_BUFFER_LEN]
receive buffer
Definition: eeprom25AA256.h:61
#define E25_OUT_BUFFER_LEN
Output buffer length.
Definition: eeprom25AA256.h:52
bool data_available
data read flag
Definition: eeprom25AA256.h:62
25AA256 eeprom structure
Definition: eeprom25AA256.h:57
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:156
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:154
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:161
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:157
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:150
uint16_t input_length
number of data words to read
Definition: spi.h:151
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:153
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
uint16_t output_length
number of data words to write
Definition: spi.h:152
enum SPITransactionStatus status
Definition: spi.h:162
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:533
@ SPICphaEdge1
CPHA = 0.
Definition: spi.h:74
@ SPITransFailed
Definition: spi.h:100
@ SPITransSuccess
Definition: spi.h:99
@ SPITransDone
Definition: spi.h:101
@ SPICpolIdleLow
CPOL = 0.
Definition: spi.h:83
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition: spi.h:63
@ SPIMSBFirst
Definition: spi.h:112
@ SPIDiv128
Definition: spi.h:126
@ SPIDss8bit
Definition: spi.h:90
SPI peripheral structure.
Definition: spi.h:174
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98