forked from lowRISC/sonata-system
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved SPI test over to upstream driver
- Loading branch information
1 parent
6b4fafa
commit e3ff0ec
Showing
6 changed files
with
238 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#pragma once | ||
#include <cheri.hh> | ||
#include <platform-gpio.hh> | ||
#include <platform-spi.hh> | ||
|
||
typedef CHERI::Capability<volatile SonataGPIO> &GpioRef; | ||
typedef CHERI::Capability<volatile SonataSpi> &SpiRef; | ||
|
||
static const uint8_t CmdReadJEDECId = 0x9f; | ||
static const uint8_t CmdWriteEnable = 0x06; | ||
static const uint8_t CmdSectorErase = 0x20; | ||
static const uint8_t CmdReadStatusRegister1 = 0x05; | ||
static const uint8_t CmdPageProgram = 0x02; | ||
static const uint8_t CmdReadData = 0x03; | ||
|
||
class SpiFlash | ||
{ | ||
private: | ||
SpiRef spi; | ||
GpioRef gpio; | ||
uint32_t csn_bit; | ||
|
||
void set_cs(bool enable) | ||
{ | ||
gpio->output = | ||
enable ? (gpio->output & ~csn_bit) : (gpio->output | csn_bit); | ||
} | ||
|
||
public: | ||
SpiFlash(SpiRef spi_, GpioRef gpio_, size_t csn_index) | ||
: spi(spi_), gpio(gpio_), csn_bit(1 << csn_index) | ||
{ | ||
} | ||
|
||
void read_jedec_id(uint8_t *jedec_id_out) | ||
{ | ||
set_cs(true); | ||
spi->tx(&CmdReadJEDECId, 1); | ||
spi->rx(jedec_id_out, 3); | ||
set_cs(false); | ||
} | ||
|
||
void erase_sector(uint32_t address) | ||
{ | ||
const uint8_t erase_cmd[4] = {CmdSectorErase, | ||
uint8_t((address >> 16) & 0xff), | ||
uint8_t((address >> 8) & 0xff), | ||
uint8_t(address & 0xff)}; | ||
|
||
set_cs(true); | ||
spi->tx(&CmdWriteEnable, 1); | ||
set_cs(false); | ||
|
||
set_cs(true); | ||
spi->tx(erase_cmd, 4); | ||
set_cs(false); | ||
|
||
set_cs(true); | ||
spi->tx(&CmdReadStatusRegister1, 1); | ||
|
||
uint8_t status; | ||
do | ||
{ | ||
spi->rx(&status, 1); | ||
} while ((status & 0x1) == 1); | ||
|
||
set_cs(false); | ||
} | ||
|
||
void write_page(uint32_t address, uint8_t *data) | ||
{ | ||
const uint8_t write_cmd[4] = {CmdPageProgram, | ||
uint8_t((address >> 16) & 0xff), | ||
uint8_t((address >> 8) & 0xff), | ||
uint8_t(address & 0xff)}; | ||
|
||
set_cs(true); | ||
spi->tx(&CmdWriteEnable, 1); | ||
set_cs(false); | ||
|
||
set_cs(true); | ||
spi->tx(write_cmd, 4); | ||
spi->tx(data, 256); | ||
set_cs(false); | ||
|
||
set_cs(true); | ||
spi->tx(&CmdReadStatusRegister1, 1); | ||
|
||
uint8_t status; | ||
do | ||
{ | ||
spi->rx(&status, 1); | ||
} while ((status & 0x1) == 1); | ||
|
||
set_cs(false); | ||
} | ||
|
||
void read(uint32_t address, uint8_t *data_out, uint32_t len) | ||
{ | ||
const uint8_t read_cmd[4] = {CmdReadData, | ||
uint8_t((address >> 16) & 0xff), | ||
uint8_t((address >> 8) & 0xff), | ||
uint8_t(address & 0xff)}; | ||
set_cs(true); | ||
spi->tx(read_cmd, 4); | ||
spi->rx(data_out, len); | ||
set_cs(false); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
#include "../../common/defs.h" | ||
#include <platform-uart.hh> | ||
|
||
typedef volatile OpenTitanUart<> *UartPtr; | ||
|
||
static void write_str(UartPtr uart, const char *str) | ||
{ | ||
for (; *str != '\0'; ++str) | ||
{ | ||
uart->blocking_write(*str); | ||
} | ||
} | ||
|
||
template<uint32_t Digits> | ||
static void to_hex(char str_buf[Digits + 1], uint32_t num) | ||
{ | ||
static_assert(Digits < 9); | ||
for (size_t i = 0; i < Digits; ++i) | ||
{ | ||
if ((num & 0xf) < 10) | ||
{ | ||
str_buf[Digits - 1 - i] = (num & 0xf) + '0'; | ||
} | ||
else | ||
{ | ||
str_buf[Digits - 1 - i] = (num & 0xf) + 'a' - 10; | ||
} | ||
num >>= 4; | ||
} | ||
str_buf[Digits] = 0; | ||
} | ||
|
||
[[maybe_unused]] static void write_hex(volatile OpenTitanUart<> *uart, | ||
uint32_t num) | ||
{ | ||
char str_buf[9]; | ||
to_hex<8>(str_buf, num); | ||
write_str(uart, str_buf); | ||
} | ||
|
||
static void write_hex8b(volatile OpenTitanUart<> *uart, uint8_t num) | ||
{ | ||
char str_buf[3]; | ||
to_hex<2>(str_buf, num); | ||
write_str(uart, str_buf); | ||
} |
Oops, something went wrong.