int main(void){ /* Sample data */ char *data = "REDPITAYA SPI TEST"; /* Init the spi resources */ if(init_spi() < 0){ printf("Initialization of SPI failed. Error: %s\n", strerror(errno)); return -1; } /* Write some sample data */ if(write_spi(data, strlen(data)) < 0){ printf("Write to SPI failed. Error: %s\n", strerror(errno)); return -1; } /* Read flash ID and some sample loopback data */ if(read_flash_id(spi_fd) < 0){ printf("Error reading from SPI bus : %s\n", strerror(errno)); return -1; } /* Release resources */ if(release_spi() < 0){ printf("Relase of SPI resources failed, Error: %s\n", strerror(errno)); return -1; } return 0; }
/** **************************************************************************************** * @brief check whether flash is present * @return ture or false ***************************************************************************************** */ bool is_flash_present(void) { uint32_t id = read_flash_id() & 0xFFFFFF; if(id > 0 && id < 0xFFFFFF) return true; else return false; }
static int ath79_probe(struct flash_bank *bank) { struct target *target = bank->target; struct ath79_flash_bank *ath79_info = bank->driver_priv; struct flash_sector *sectors; uint32_t id = 0; /* silence uninitialized warning */ uint32_t pagesize, sectorsize; const struct ath79_target *target_device; int retval; if (ath79_info->probed) { free(bank->sectors); free(ath79_info->spi.page_buf); } ath79_info->probed = 0; for (target_device = target_devices; target_device->name; ++target_device) if (target_device->tap_idcode == target->tap->idcode) break; if (!target_device->name) { LOG_ERROR("Device ID 0x%" PRIx32 " is not known", target->tap->idcode); return ERROR_FAIL; } ath79_info->io_base = target_device->io_base; LOG_DEBUG("Found device %s at address " TARGET_ADDR_FMT, target_device->name, bank->base); retval = read_flash_id(bank, &id); if (retval != ERROR_OK) return retval; ath79_info->dev = NULL; for (const struct flash_device *p = flash_devices; p->name; p++) if (p->device_id == id) { ath79_info->dev = p; break; } if (!ath79_info->dev) { LOG_ERROR("Unknown flash device (ID 0x%08" PRIx32 ")", id); return ERROR_FAIL; } LOG_INFO("Found flash device \'%s\' (ID 0x%08" PRIx32 ")", ath79_info->dev->name, ath79_info->dev->device_id); /* Set correct size value */ bank->size = ath79_info->dev->size_in_bytes; if (bank->size <= (1UL << 16)) LOG_WARNING("device needs 2-byte addresses - not implemented"); if (bank->size > (1UL << 24)) LOG_WARNING("device needs paging or 4-byte addresses - not implemented"); /* if no sectors, treat whole bank as single sector */ sectorsize = ath79_info->dev->sectorsize ? ath79_info->dev->sectorsize : ath79_info->dev->size_in_bytes; /* create and fill sectors array */ bank->num_sectors = ath79_info->dev->size_in_bytes / sectorsize; sectors = calloc(1, sizeof(struct flash_sector) * bank->num_sectors); if (!sectors) { LOG_ERROR("not enough memory"); return ERROR_FAIL; } /* if no write pagesize, use reasonable default */ pagesize = ath79_info->dev->pagesize ? ath79_info->dev->pagesize : SPIFLASH_DEF_PAGESIZE; ath79_info->spi.page_buf = malloc(pagesize); if (!ath79_info->spi.page_buf) { LOG_ERROR("not enough memory"); free(sectors); return ERROR_FAIL; } for (int sector = 0; sector < bank->num_sectors; sector++) { sectors[sector].offset = sector * sectorsize; sectors[sector].size = sectorsize; sectors[sector].is_erased = 0; sectors[sector].is_protected = 1; } bank->sectors = sectors; ath79_info->probed = 1; return ERROR_OK; }
static int stmsmi_probe(struct flash_bank *bank) { struct target *target = bank->target; struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv; uint32_t io_base; struct flash_sector *sectors; uint32_t id = 0; /* silence uninitialized warning */ struct stmsmi_target *target_device; int retval; if (stmsmi_info->probed) free(bank->sectors); stmsmi_info->probed = 0; for (target_device=target_devices ; target_device->name ; ++target_device) if (target_device->tap_idcode == target->tap->idcode) break; if (!target_device->name) { LOG_ERROR("Device ID 0x%" PRIx32 " is not known as SMI capable", target->tap->idcode); return ERROR_FAIL; } switch (bank->base - target_device->smi_base) { case 0: stmsmi_info->bank_num = SMI_SEL_BANK0; break; case SMI_BANK_SIZE: stmsmi_info->bank_num = SMI_SEL_BANK1; break; case 2*SMI_BANK_SIZE: stmsmi_info->bank_num = SMI_SEL_BANK2; break; case 3*SMI_BANK_SIZE: stmsmi_info->bank_num = SMI_SEL_BANK3; break; default: LOG_ERROR("Invalid SMI base address 0x%" PRIx32, bank->base); return ERROR_FAIL; } io_base = target_device->io_base; stmsmi_info->io_base = io_base; LOG_DEBUG("Valid SMI on device %s at address 0x%" PRIx32, target_device->name, bank->base); /* read and decode flash ID; returns in SW mode */ retval = read_flash_id(bank, &id); SMI_SET_HW_MODE(); if (retval != ERROR_OK) return retval; stmsmi_info->dev = NULL; for (struct flash_device *p = flash_devices; p->name ; p++) if (p->device_id == id) { stmsmi_info->dev = p; break; } if (!stmsmi_info->dev) { LOG_ERROR("Unknown flash device (ID 0x%08" PRIx32 ")", id); return ERROR_FAIL; } LOG_INFO("Found flash device \'%s\' (ID 0x%08" PRIx32 ")", stmsmi_info->dev->name, stmsmi_info->dev->device_id); /* Set correct size value */ bank->size = stmsmi_info->dev->size_in_bytes; /* create and fill sectors array */ bank->num_sectors = stmsmi_info->dev->size_in_bytes / stmsmi_info->dev->sectorsize; sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors); if (sectors == NULL) { LOG_ERROR("not enough memory"); return ERROR_FAIL; } for (int sector = 0; sector < bank->num_sectors; sector++) { sectors[sector].offset = sector * stmsmi_info->dev->sectorsize; sectors[sector].size = stmsmi_info->dev->sectorsize; sectors[sector].is_erased = -1; sectors[sector].is_protected = 1; } bank->sectors = sectors; stmsmi_info->probed = 1; return ERROR_OK; }
int main (void) { uint32_t id, j; uint8_t buffer[256]; uint8_t rxbuffer[256]; // System initialization SystemInit(); // Power on inside serial flash power_on_flash(); // Reset value for (j = 0; j < 256; j++) { rxbuffer[j] = 0; buffer[j] = j; } // Read flash chip ID id = id; //avoid warning id = read_flash_id(); // Erase 1 sector flash data from 0x4000, and 1 sector size is 4KB sector_erase_flash(0x4000, 1); /* * The parameter "address" note: * * When the address range is from 0x00 to 0x1000 (NVDS area), the address must be 4 * integer times. * * When the address range is greater than or equal to 0x1000 (Code area), the * address must be 256 integer times. (Encryption request) * The parameter "size" note: * * When the address range is from 0x00 to 0x1000 (NVDS area), the size must be 4 * integer times and less than or equal to 256. * * When the address range is greater than or equal to 0x1000 (Code area), the * size must be 256 bytes. (Encryption request) */ write_flash(0x4000, (uint32_t *)buffer, 256); /* * The parameter "address" note: * * When the address range is from 0x00 to 0x1000 (NVDS area), the address must be 4 * integer times. * * When the address range is greater than or equal to 0x1000 (Code area), the * address must be 256 integer times. (Encryption request) * The parameter "size" note: * * When the address range is from 0x00 to 0x1000 (NVDS area), the size must be 4 * integer times and less than or equal to 256. * * When the address range is greater than or equal to 0x1000 (Code area), the * size must be 256 bytes integer times. (Encryption request) */ read_flash(0x4000, (uint32_t *)rxbuffer, 256); for (j = 0; j < 256; j++) { if (rxbuffer[j] != j) { // read flash data error while(1); } } while (1) /* Loop forever */ { } }