summaryrefslogtreecommitdiffstats
path: root/drivers/flash/flash_spi.h
blob: abe95e955e56051d6460f4fa1abfbd9cbf0bbfc1 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright (C) 2021 Westberry Technology (ChangZhou) Corp., Ltd

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 2 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/>.
*/

#pragma once

/* All the following default configurations are based on MX25L4006E Nor FLASH. */

/*
    The slave select pin of the FLASH.
    This needs to be a normal GPIO pin_t value, such as B14.
*/
#ifndef EXTERNAL_FLASH_SPI_SLAVE_SELECT_PIN
#    error "No chip select pin defined -- missing EXTERNAL_FLASH_SPI_SLAVE_SELECT_PIN"
#endif

/*
    The clock divisor for SPI to ensure that the MCU is within the
    specifications of the FLASH chip. Generally this will be PCLK divided by
    the intended divisor -- check your clock settings and the datasheet of
    your FLASH.
*/
#ifndef EXTERNAL_FLASH_SPI_CLOCK_DIVISOR
#    ifdef __AVR__
#        define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR 4
#    else
#        define EXTERNAL_FLASH_SPI_CLOCK_DIVISOR 8
#    endif
#endif

/*
    The SPI mode to communicate with the FLASH.
*/
#ifndef EXTERNAL_FLASH_SPI_MODE
#    define EXTERNAL_FLASH_SPI_MODE 0
#endif

/*
    Whether or not the SPI communication between the MCU and FLASH should be
    LSB-first.
*/
#ifndef EXTERNAL_FLASH_SPI_LSBFIRST
#    define EXTERNAL_FLASH_SPI_LSBFIRST false
#endif

/*
    The Flash address size in bytes, as specified in datasheet.
*/
#ifndef EXTERNAL_FLASH_ADDRESS_SIZE
#    define EXTERNAL_FLASH_ADDRESS_SIZE 3
#endif

/*
    The page size of the FLASH in bytes, as specified in the datasheet.
*/
#ifndef EXTERNAL_FLASH_PAGE_SIZE
#    define EXTERNAL_FLASH_PAGE_SIZE 256
#endif

/*
    The sector size of the FLASH in bytes, as specified in the datasheet.
*/
#ifndef EXTERNAL_FLASH_SECTOR_SIZE
#    define EXTERNAL_FLASH_SECTOR_SIZE (4 * 1024)
#endif

/*
    The block size of the FLASH in bytes, as specified in the datasheet.
*/
#ifndef EXTERNAL_FLASH_BLOCK_SIZE
#    define EXTERNAL_FLASH_BLOCK_SIZE (64 * 1024)
#endif

/*
    The total size of the FLASH in bytes, as specified in the datasheet.
*/
#ifndef EXTERNAL_FLASH_SIZE
#    define EXTERNAL_FLASH_SIZE (512 * 1024)
#endif

/*
    The block count of the FLASH, calculated by total FLASH size and block size.
*/
#define EXTERNAL_FLASH_BLOCK_COUNT ((EXTERNAL_FLASH_SIZE) / (EXTERNAL_FLASH_BLOCK_SIZE))

/*
    The sector count of the FLASH, calculated by total FLASH size and sector size.
*/
#define EXTERNAL_FLASH_SECTOR_COUNT ((EXTERNAL_FLASH_SIZE) / (EXTERNAL_FLASH_SECTOR_SIZE))

/*
    The page count of the FLASH, calculated by total FLASH size and page size.
*/
#define EXTERNAL_FLASH_PAGE_COUNT ((EXTERNAL_FLASH_SIZE) / (EXTERNAL_FLASH_PAGE_SIZE))

typedef int16_t flash_status_t;

#define FLASH_STATUS_SUCCESS (0)
#define FLASH_STATUS_ERROR (-1)
#define FLASH_STATUS_TIMEOUT (-2)
#define FLASH_STATUS_BAD_ADDRESS (-3)

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

void flash_init(void);

flash_status_t flash_erase_chip(void);

flash_status_t flash_erase_block(uint32_t addr);

flash_status_t flash_erase_sector(uint32_t addr);

flash_status_t flash_read_block(uint32_t addr, void *buf, size_t len);

flash_status_t flash_write_block(uint32_t addr, const void *buf, size_t len);

#ifdef __cplusplus
}
#endif