Line 156... |
Line 156... |
void spi_flash_write_byte(uint32_t addr, uint8_t wdata);
|
void spi_flash_write_byte(uint32_t addr, uint8_t wdata);
|
void spi_flash_write_word(uint32_t addr, uint32_t wdata);
|
void spi_flash_write_word(uint32_t addr, uint32_t wdata);
|
void spi_flash_erase_sector(uint32_t addr);
|
void spi_flash_erase_sector(uint32_t addr);
|
uint8_t spi_flash_read_status(void);
|
uint8_t spi_flash_read_status(void);
|
uint8_t spi_flash_read_1st_id(void);
|
uint8_t spi_flash_read_1st_id(void);
|
void spi_flash_write_cmd(uint8_t cmd);
|
void spi_flash_write_enable(void);
|
|
void spi_flash_write_addr(uint32_t addr);
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Bootloader main.
|
* Bootloader main.
|
**************************************************************************/
|
**************************************************************************/
|
Line 310... |
Line 311... |
/**********************************************************************//**
|
/**********************************************************************//**
|
* Start application program at the beginning of instruction space.
|
* Start application program at the beginning of instruction space.
|
**************************************************************************/
|
**************************************************************************/
|
void start_app(void) {
|
void start_app(void) {
|
|
|
|
// executable available?
|
|
if (neorv32_cpu_csr_read(CSR_MSCRATCH) == 0) {
|
|
neorv32_uart_print("No executable available.");
|
|
return;
|
|
}
|
|
|
// no need to shutdown or reset the used peripherals
|
// no need to shutdown or reset the used peripherals
|
// -> this will be done by application's crt0
|
// -> this will be done by application's crt0
|
|
|
// deactivate IRQs and IRQ sources
|
// deactivate IRQs and IRQ sources
|
neorv32_cpu_dint();
|
neorv32_cpu_dint();
|
Line 621... |
Line 628... |
* @param[in] addr Flash read address.
|
* @param[in] addr Flash read address.
|
* @return Read byte from SPI flash.
|
* @return Read byte from SPI flash.
|
**************************************************************************/
|
**************************************************************************/
|
uint8_t spi_flash_read_byte(uint32_t addr) {
|
uint8_t spi_flash_read_byte(uint32_t addr) {
|
|
|
union {
|
|
uint32_t uint32;
|
|
uint8_t uint8[sizeof(uint32_t)];
|
|
} address;
|
|
|
|
address.uint32 = addr;
|
|
|
|
neorv32_spi_cs_en(SPI_FLASH_CS);
|
neorv32_spi_cs_en(SPI_FLASH_CS);
|
|
|
neorv32_spi_trans(SPI_FLASH_CMD_READ);
|
neorv32_spi_trans(SPI_FLASH_CMD_READ);
|
neorv32_spi_trans(address.uint8[2]);
|
spi_flash_write_addr(addr);
|
neorv32_spi_trans(address.uint8[1]);
|
|
neorv32_spi_trans(address.uint8[0]);
|
|
uint8_t rdata = (uint8_t)neorv32_spi_trans(0);
|
uint8_t rdata = (uint8_t)neorv32_spi_trans(0);
|
|
|
neorv32_spi_cs_dis(SPI_FLASH_CS);
|
neorv32_spi_cs_dis(SPI_FLASH_CS);
|
|
|
return rdata;
|
return rdata;
|
Line 650... |
Line 648... |
* @param[in] addr SPI flash read address.
|
* @param[in] addr SPI flash read address.
|
* @param[in] wdata SPI flash read data.
|
* @param[in] wdata SPI flash read data.
|
**************************************************************************/
|
**************************************************************************/
|
void spi_flash_write_byte(uint32_t addr, uint8_t wdata) {
|
void spi_flash_write_byte(uint32_t addr, uint8_t wdata) {
|
|
|
union {
|
spi_flash_write_enable(); // allow write-access
|
uint32_t uint32;
|
|
uint8_t uint8[sizeof(uint32_t)];
|
|
} address;
|
|
|
|
address.uint32 = addr;
|
|
|
|
spi_flash_write_cmd(SPI_FLASH_CMD_WRITE_ENABLE); // allow write-access
|
|
|
|
neorv32_spi_cs_en(SPI_FLASH_CS);
|
neorv32_spi_cs_en(SPI_FLASH_CS);
|
|
|
neorv32_spi_trans(SPI_FLASH_CMD_PAGE_PROGRAM);
|
neorv32_spi_trans(SPI_FLASH_CMD_PAGE_PROGRAM);
|
neorv32_spi_trans(address.uint8[2]);
|
spi_flash_write_addr(addr);
|
neorv32_spi_trans(address.uint8[1]);
|
|
neorv32_spi_trans(address.uint8[0]);
|
|
neorv32_spi_trans(wdata);
|
neorv32_spi_trans(wdata);
|
|
|
neorv32_spi_cs_dis(SPI_FLASH_CS);
|
neorv32_spi_cs_dis(SPI_FLASH_CS);
|
|
|
while (1) {
|
while (1) {
|
Line 707... |
Line 696... |
*
|
*
|
* @param[in] addr Base address of sector to erase.
|
* @param[in] addr Base address of sector to erase.
|
**************************************************************************/
|
**************************************************************************/
|
void spi_flash_erase_sector(uint32_t addr) {
|
void spi_flash_erase_sector(uint32_t addr) {
|
|
|
union {
|
spi_flash_write_enable(); // allow write-access
|
uint32_t uint32;
|
|
uint8_t uint8[sizeof(uint32_t)];
|
|
} address;
|
|
|
|
address.uint32 = addr;
|
|
|
|
spi_flash_write_cmd(SPI_FLASH_CMD_WRITE_ENABLE); // allow write-access
|
|
|
|
neorv32_spi_cs_en(SPI_FLASH_CS);
|
neorv32_spi_cs_en(SPI_FLASH_CS);
|
|
|
neorv32_spi_trans(SPI_FLASH_CMD_SECTOR_ERASE);
|
neorv32_spi_trans(SPI_FLASH_CMD_SECTOR_ERASE);
|
neorv32_spi_trans(address.uint8[2]);
|
spi_flash_write_addr(addr);
|
neorv32_spi_trans(address.uint8[1]);
|
|
neorv32_spi_trans(address.uint8[0]);
|
|
|
|
neorv32_spi_cs_dis(SPI_FLASH_CS);
|
neorv32_spi_cs_dis(SPI_FLASH_CS);
|
|
|
while (1) {
|
while (1) {
|
uint8_t tmp = spi_flash_read_status();
|
uint8_t tmp = spi_flash_read_status();
|
Line 773... |
Line 753... |
return id;
|
return id;
|
}
|
}
|
|
|
|
|
/**********************************************************************//**
|
/**********************************************************************//**
|
* Write command to flash.
|
* Enable flash write access.
|
*
|
|
* @param[in] cmd Command byte.
|
|
**************************************************************************/
|
**************************************************************************/
|
void spi_flash_write_cmd(uint8_t cmd) {
|
void spi_flash_write_enable(void) {
|
|
|
neorv32_spi_cs_en(SPI_FLASH_CS);
|
neorv32_spi_cs_en(SPI_FLASH_CS);
|
|
neorv32_spi_trans(SPI_FLASH_CMD_WRITE_ENABLE);
|
|
neorv32_spi_cs_dis(SPI_FLASH_CS);
|
|
}
|
|
|
neorv32_spi_trans(cmd);
|
|
|
|
neorv32_spi_cs_dis(SPI_FLASH_CS);
|
/**********************************************************************//**
|
|
* Send address word to flash.
|
|
*
|
|
* @param[in] addr Address word.
|
|
**************************************************************************/
|
|
void spi_flash_write_addr(uint32_t addr) {
|
|
|
|
union {
|
|
uint32_t uint32;
|
|
uint8_t uint8[sizeof(uint32_t)];
|
|
} address;
|
|
|
|
address.uint32 = addr;
|
|
|
|
neorv32_spi_trans(address.uint8[2]);
|
|
neorv32_spi_trans(address.uint8[1]);
|
|
neorv32_spi_trans(address.uint8[0]);
|
}
|
}
|
|
|
|
|
No newline at end of file
|
No newline at end of file
|