aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/usrp3/include/flash/spi_flash.h
blob: 8ed73f6484146166988f4046d1b27ae723ef70ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright 2014 Free Software Foundation, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_SPI_FLASH
#define INCLUDED_SPI_FLASH

#include <wb_spi.h>

//Device class that encapsulates the geometry and control
//interface for the flash chip
typedef struct {
    uint32_t page_size;     //in bytes
    uint32_t sector_size;   //in bytes
    uint32_t num_sectors;
    const wb_spi_slave_t* bus;
} spi_flash_dev_t;

//Low level device specific operations
typedef uint16_t (*spif_read_id_fn_t)(const spi_flash_dev_t* flash);
typedef void (*spif_read_fn_t)(const spi_flash_dev_t* flash, uint32_t offset, void *buf, uint32_t num_bytes);
typedef bool (*spif_erase_sector_dispatch_fn_t)(const spi_flash_dev_t* flash, uint32_t offset);
typedef bool (*spif_erase_sector_commit_fn_t)(const spi_flash_dev_t* flash, uint32_t offset);
typedef bool (*spif_erase_sector_busy_fn_t)(const spi_flash_dev_t* flash);
typedef bool (*spif_write_page_dispatch_fn_t)(const spi_flash_dev_t* flash, uint32_t offset, const void *buf, uint32_t num_bytes);
typedef bool (*spif_write_page_commit_fn_t)(const spi_flash_dev_t* flash, uint32_t offset, const void *buf, uint32_t num_bytes);
typedef bool (*spif_write_page_busy_fn_t)(const spi_flash_dev_t* flash);

//Interface struct for all low level device operations
typedef struct {
    spif_read_id_fn_t               read_id;
    spif_read_fn_t                  read;
    spif_erase_sector_dispatch_fn_t erase_sector_dispatch;
    spif_erase_sector_commit_fn_t   erase_sector_commit;
    spif_erase_sector_busy_fn_t     erase_sector_busy;
    spif_write_page_dispatch_fn_t   write_page_dispatch;
    spif_write_page_commit_fn_t     write_page_commit;
    spif_write_page_busy_fn_t       write_page_busy;
} spi_flash_ops_t;

typedef enum {
    IDLE, WRITE_IN_PROGRESS, ERASE_IN_PROGRESS
} spi_flash_state_t;

//A session struct that encapsulates everything about the flash
//in a device agnostic way
typedef struct {
    const spi_flash_dev_t*  device;
    const spi_flash_ops_t*  ops;
    spi_flash_state_t       state;
    uint32_t                last_offset;
    uint16_t                id;
} spi_flash_session_t;

/*!
 * Initialize the spi_flash_session_t object
 */
void spif_init(spi_flash_session_t* flash, const spi_flash_dev_t* device, const spi_flash_ops_t* ops);

/*!
 * Read "num_bytes" from "offset" in the flash into the buffer "buf".
 * This call will block until all data is available.
 */
void spif_read_sync(const spi_flash_session_t* flash, uint32_t offset, void *buf, uint32_t num_bytes);

/*!
 * Erase sector at "offset" in the flash.
 * This call will block until the erase is complete.
 */
bool spif_erase_sector_sync(const spi_flash_session_t* flash, uint32_t offset);

/*!
 * Write "num_bytes" from buffer "buf" at "offset" in the flash.
 * This call will block until the write is complete.
 */
bool spif_write_page_sync(const spi_flash_session_t* flash, uint32_t offset, const void *buf, uint32_t num_bytes);


#endif /* INCLUDED_SPI_FLASH */