CTS-SAT-1-OBC-Firmware
Loading...
Searching...
No Matches
flash_driver.h
Go to the documentation of this file.
1#ifndef INCLUDE_GUARD__FLASH_DRIVER_H__
2#define INCLUDE_GUARD__FLASH_DRIVER_H__
3
4/*-----------------------------INCLUDES-----------------------------*/
5#include <stdint.h>
6
7#include "main.h"
8
9#include "littlefs/lfs.h"
10
11/*----------------------------- CONFIG VARIABLES ----------------------------- */
12// Number of CS pins available
13#define FLASH_NUMBER_OF_FLASH_DEVICES 8 // TODO: update to 8, or 10 with FRAM maybe
14
15// Total size of a singular Memory Module in bytes
16#define FLASH_CHIP_SIZE_BYTES 134217728 // 128MiB // TODO: update
17
18// Number of pages contained within a single block of memory module
19#define FLASH_CHIP_PAGES_PER_BLOCK 64
20
21// NAND Flash Memory Datasheet https://www.farnell.com/datasheets/3151163.pdf
22// Each page is divided into a 2048-byte data storage region, and a 128 bytes spare area (2176 bytes total).
23#define FLASH_MAX_BYTES_PER_PAGE 2048
24
25/*-------------------------------FLASH FEATURES-------------------------------*/
26// Features that can be accessed using Get Feature command
27
28static const uint8_t FLASH_FEAT_BLOCK_LOCK = 0xA0; // Block Lock
29
30static const uint8_t FLASH_FEAT_CONFIG = 0xB0; // Configuration Register
31
32static const uint8_t FLASH_FEAT_STATUS = 0xC0; // Status Register
33
34static const uint8_t FLASH_FEAT_DIE_SELECT = 0xD0; // Die Select
35
36/*-----------------------------COMMAND VARIABLES-----------------------------*/
37// All comments in this section refer to the "MT29F1G 1Gib (128MiB)" Datasheet by Micron
38
39static const uint8_t FLASH_CMD_PAGE_READ = 0x13; // Read Page
40static const uint8_t FLASH_CMD_READ_FROM_CACHE = 0x03; // Read Page
41
42static const uint8_t FLASH_CMD_PROGRAM_LOAD = 0x02; // Load Program into cache resgiters
43static const uint8_t FLASH_CMD_PROGRAM_EXEC = 0x10; // Send data from cache to memory
44
45static const uint8_t FLASH_CMD_BLOCK_ERASE = 0xD8; // Block Erase
46
47static const uint8_t FLASH_CMD_WRITE_ENABLE = 0x06; // Write Enable
48static const uint8_t FLASH_CMD_WRITE_DISABLE = 0x04; // Write Disable
49
50static const uint8_t FLASH_CMD_GET_FEATURES = 0x0F; // Get Features
51static const uint8_t FLASH_CMD_SET_FEATURES = 0x1F; // Set Features
52
53static const uint8_t FLASH_CMD_READ_ID = 0x9F; // Read ID (0x2C 0x14)
54
55static const uint8_t FLASH_CMD_RESET = 0xFF; // Reset operation
56
57// ------------------- Status Register 1 - Byte Masks -------------------
58// Source: Table 5
59static const uint8_t FLASH_SR1_WRITE_IN_PROGRESS_MASK = (1 << 0);
60static const uint8_t FLASH_SR1_WRITE_ENABLE_LATCH_MASK = (1 << 1);
61static const uint8_t FLASH_SR1_PROGRAMMING_ERROR_MASK = (1 << 3);
62static const uint8_t FLASH_SR1_ERASE_ERROR_MASK = (1 << 2);
63
64/*-----------------------------FLASH ERROR CODES-----------------------------*/
65typedef enum {
66 FLASH_ERR_OK = 0, // No error occurred
67 FLASH_ERR_SPI_TRANSMIT_FAILED = -3, // Error occurred while transmitting SPI signal
68 FLASH_ERR_SPI_RECEIVE_FAILED = -4, // Error occurred while receiving SPI signal
69 FLASH_ERR_DEVICE_BUSY_TIMEOUT = -6, // Took too long for the device to be in standby
70 FLASH_ERR_UNKNOWN = -7, // Unknown error occurred (code reached where it shouldn't have been possible)
71 FLASH_ERR_STATUS_REG_ERROR = -8, // Error occurred which was indicated by one of the Status Register Bits.
72 FLASH_ERR_SPI_TRANSMIT_TIMEOUT = -10, // Timeout when transmitting SPI signal
73 FLASH_ERR_SPI_RECEIVE_TIMEOUT = -11 // Timeout when receiving SPI signal
75
76/*-----------------------------DRIVER FUNCTIONS-----------------------------*/
77void FLASH_activate_chip_select(uint8_t chip_number);
79FLASH_error_enum_t FLASH_unblock_block_lock(SPI_HandleTypeDef *hspi, uint8_t chip_number, uint8_t *buf);
80FLASH_error_enum_t FLASH_read_status_register(SPI_HandleTypeDef *hspi, uint8_t chip_number, uint8_t *buf);
81FLASH_error_enum_t FLASH_read_block_lock_register(SPI_HandleTypeDef *hspi, uint8_t chip_number, uint8_t *buf);
82FLASH_error_enum_t FLASH_write_enable(SPI_HandleTypeDef *hspi, uint8_t chip_number);
83FLASH_error_enum_t FLASH_write_disable(SPI_HandleTypeDef *hspi, uint8_t chip_number);
84FLASH_error_enum_t FLASH_erase(SPI_HandleTypeDef *hspi, uint8_t chip_number, lfs_block_t page);
85FLASH_error_enum_t FLASH_write_data(SPI_HandleTypeDef *hspi, uint8_t chip_number, lfs_block_t page, uint8_t *packet_buffer, lfs_size_t packet_buffer_len);
86FLASH_error_enum_t FLASH_read_data(SPI_HandleTypeDef *hspi, uint8_t chip_number, lfs_block_t page, uint8_t *rx_buffer, lfs_size_t rx_buffer_len);
87
88FLASH_error_enum_t FLASH_is_reachable(SPI_HandleTypeDef *hspi, uint8_t chip_number);
89FLASH_error_enum_t FLASH_reset(SPI_HandleTypeDef *hspi, uint8_t chip_number);
90
91#endif /* INCLUDE_GUARD__FLASH_DRIVER_H__ */
static const uint8_t FLASH_CMD_BLOCK_ERASE
Definition flash_driver.h:45
static const uint8_t FLASH_CMD_RESET
Definition flash_driver.h:55
static const uint8_t FLASH_CMD_READ_ID
Definition flash_driver.h:53
static const uint8_t FLASH_CMD_PAGE_READ
Definition flash_driver.h:39
static const uint8_t FLASH_SR1_WRITE_ENABLE_LATCH_MASK
Definition flash_driver.h:60
FLASH_error_enum_t FLASH_reset(SPI_HandleTypeDef *hspi, uint8_t chip_number)
Resets the NAND flash memory module.
Definition flash_driver.c:778
FLASH_error_enum_t FLASH_read_data(SPI_HandleTypeDef *hspi, uint8_t chip_number, lfs_block_t page, uint8_t *rx_buffer, lfs_size_t rx_buffer_len)
Sends Page Read Command.
Definition flash_driver.c:636
FLASH_error_enum_t FLASH_write_disable(SPI_HandleTypeDef *hspi, uint8_t chip_number)
Sends Write Disable Command.
Definition flash_driver.c:325
static const uint8_t FLASH_CMD_WRITE_ENABLE
Definition flash_driver.h:47
FLASH_error_enum_t FLASH_write_data(SPI_HandleTypeDef *hspi, uint8_t chip_number, lfs_block_t page, uint8_t *packet_buffer, lfs_size_t packet_buffer_len)
Sends Page Program Command.
Definition flash_driver.c:484
FLASH_error_enum_t FLASH_read_block_lock_register(SPI_HandleTypeDef *hspi, uint8_t chip_number, uint8_t *buf)
Read Block Lock Register and store the values in given buffer.
Definition flash_driver.c:213
FLASH_error_enum_t FLASH_is_reachable(SPI_HandleTypeDef *hspi, uint8_t chip_number)
Checks if the FLASH chip is reachable by checking it's ID.
Definition flash_driver.c:803
static const uint8_t FLASH_FEAT_DIE_SELECT
Definition flash_driver.h:34
static const uint8_t FLASH_FEAT_STATUS
Definition flash_driver.h:32
static const uint8_t FLASH_CMD_PROGRAM_LOAD
Definition flash_driver.h:42
static const uint8_t FLASH_CMD_READ_FROM_CACHE
Definition flash_driver.h:40
static const uint8_t FLASH_SR1_WRITE_IN_PROGRESS_MASK
Definition flash_driver.h:59
void FLASH_activate_chip_select(uint8_t chip_number)
Activates the chip select for the given flash module number.
Definition flash_driver.c:44
static const uint8_t FLASH_CMD_GET_FEATURES
Definition flash_driver.h:50
FLASH_error_enum_t FLASH_erase(SPI_HandleTypeDef *hspi, uint8_t chip_number, lfs_block_t page)
Sends Block Erase Command.
Definition flash_driver.c:384
static const uint8_t FLASH_CMD_SET_FEATURES
Definition flash_driver.h:51
static const uint8_t FLASH_CMD_PROGRAM_EXEC
Definition flash_driver.h:43
FLASH_error_enum_t FLASH_unblock_block_lock(SPI_HandleTypeDef *hspi, uint8_t chip_number, uint8_t *buf)
Unblocks all blocked blocks of memory on the NAND flash memory module.
Definition flash_driver.c:89
void FLASH_deactivate_chip_select()
Deactivates the chip select for all lines.
Definition flash_driver.c:69
FLASH_error_enum_t
Definition flash_driver.h:65
@ FLASH_ERR_SPI_TRANSMIT_FAILED
Definition flash_driver.h:67
@ FLASH_ERR_STATUS_REG_ERROR
Definition flash_driver.h:71
@ FLASH_ERR_SPI_RECEIVE_TIMEOUT
Definition flash_driver.h:73
@ FLASH_ERR_SPI_TRANSMIT_TIMEOUT
Definition flash_driver.h:72
@ FLASH_ERR_SPI_RECEIVE_FAILED
Definition flash_driver.h:68
@ FLASH_ERR_DEVICE_BUSY_TIMEOUT
Definition flash_driver.h:69
@ FLASH_ERR_OK
Definition flash_driver.h:66
@ FLASH_ERR_UNKNOWN
Definition flash_driver.h:70
FLASH_error_enum_t FLASH_read_status_register(SPI_HandleTypeDef *hspi, uint8_t chip_number, uint8_t *buf)
Read Status Register and store the values in given buffer.
Definition flash_driver.c:158
static const uint8_t FLASH_SR1_PROGRAMMING_ERROR_MASK
Definition flash_driver.h:61
static const uint8_t FLASH_FEAT_BLOCK_LOCK
Definition flash_driver.h:28
static const uint8_t FLASH_SR1_ERASE_ERROR_MASK
Definition flash_driver.h:62
static const uint8_t FLASH_FEAT_CONFIG
Definition flash_driver.h:30
FLASH_error_enum_t FLASH_write_enable(SPI_HandleTypeDef *hspi, uint8_t chip_number)
Sends Write Enable Command.
Definition flash_driver.c:267
static const uint8_t FLASH_CMD_WRITE_DISABLE
Definition flash_driver.h:48
uint32_t lfs_block_t
Definition lfs.h:45
uint32_t lfs_size_t
Definitions ///.
Definition lfs.h:39
: Header for main.c file. This file contains the common defines of the application.
static uint8_t rx_buffer[5120]
Definition uart_telelecommand_defs.c:22