Paparazzi UAS v7.0_unstable
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
43
44// Init function
45void 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
57 eeprom->spi_trans.select = SPISelectUnselect;
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
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
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.
@ EEPROM_READ
@ EEPROM_WREN
@ EEPROM_WRDI
@ EEPROM_RDSR
@ EEPROM_WRITE
@ EEPROM_WRSR
void eeprom25AA256_event(struct Eeprom25AA256 *eeprom)
Event function.
void eeprom25AA256_init(struct Eeprom25AA256 *eeprom, struct spi_periph *spi_p, uint8_t slave_idx)
Init function.
void eeprom25AA256_read(struct Eeprom25AA256 *eeprom, uint16_t addr, uint16_t length)
Read function.
Driver for the eeprom 225AA256 (and 25LC256) 256K SPI bus serial EEPROM from Microchip.
#define E25_IN_BUFFER_LEN
Input buffer length.
#define E25_OUT_BUFFER_LEN
Output buffer length.
25AA256 eeprom structure
enum SPIStatus status
internal state of the peripheral
Definition spi.h:180
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
uint16_t foo
Definition main_demo5.c:58
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.