diff options
author | David Barksdale <amatus@amatus.name> | 2016-08-02 20:54:43 -0500 |
---|---|---|
committer | David Barksdale <amatus@amatus.name> | 2016-08-02 22:12:15 -0500 |
commit | b9ee239a70d72b04c1008e781e5b716c7c62121d (patch) | |
tree | 3c18cf688fb2058f0fe8ef4374fe114db3a020b2 /laser-tag software | |
parent | c96bdbec1c030647a777a975534521bc30b03e6b (diff) |
Store images in flash, accessable over USB
Diffstat (limited to 'laser-tag software')
-rw-r--r-- | laser-tag software/disk.c | 26 | ||||
-rw-r--r-- | laser-tag software/disk.h | 2 | ||||
-rw-r--r-- | laser-tag software/flash.c | 46 | ||||
-rw-r--r-- | laser-tag software/flash.h | 6 | ||||
-rw-r--r-- | laser-tag software/main.c | 24 |
5 files changed, 66 insertions, 38 deletions
diff --git a/laser-tag software/disk.c b/laser-tag software/disk.c index 275f63d..ce0ddc9 100644 --- a/laser-tag software/disk.c +++ b/laser-tag software/disk.c @@ -39,6 +39,7 @@ #include "usb_device_stack_interface.h" #include "disk.h" #include "adapter_cfg.h" +#include "flash.h" #include "fsl_device_registers.h" #include "fsl_clock_manager.h" #include "fsl_debug_console.h" @@ -71,6 +72,7 @@ usb_class_specific_callback_struct_t class_specific_callback; msc_config_struct_t g_msd_config; disk_struct_t g_disk; +uint8_t sector_buffer[MSD_RECV_BUFFER_SIZE]; /***************************************************************************** * Local Types - None @@ -179,40 +181,29 @@ uint8_t USB_App_Class_Callback switch(event_type) { case USB_DEV_EVENT_DATA_RECEIVED: - /* Add User defined code -- if required*/ lba_data_ptr = (lba_app_struct_t*) size; - USB_PRINTF("DATA_RECEIVED for lun %d\r\n", lba_data_ptr->lun); + flash_write((intptr_t)images[lba_data_ptr->lun] + lba_data_ptr->offset, + sector_buffer, + MSD_RECV_BUFFER_SIZE); break; case USB_DEV_EVENT_SEND_COMPLETE: - /* Add User defined code -- if required*/ lba_data_ptr = (lba_app_struct_t*) size; - if (lba_data_ptr->size != 0) - { - /* read data from mass storage device to driver buffer */ - if(data != NULL) - { - *data = g_disk.storage_disk + lba_data_ptr->offset; - } - } break; case USB_MSC_START_STOP_EJECT_MEDIA: - /* Code to be added by user for starting, stopping or - ejecting the disk drive. e.g. starting/stopping the motor in - case of CD/DVD*/ break; case USB_MSC_DEVICE_READ_REQUEST: lba_data_ptr = (lba_app_struct_t*) size; if(data != NULL) { - *data = g_disk.storage_disk + lba_data_ptr->offset; + *data = images[lba_data_ptr->lun] + lba_data_ptr->offset; } break; case USB_MSC_DEVICE_WRITE_REQUEST: lba_data_ptr = (lba_app_struct_t*) size; if(data != NULL) { - *data = g_disk.storage_disk + lba_data_ptr->offset; + *data = sector_buffer; } break; case USB_MSC_DEVICE_FORMAT_COMPLETE: @@ -254,7 +245,7 @@ uint8_t USB_App_Class_Callback ** *****************************************************************************/ -void disk_init(uint8_t *mem) +void disk_init() { OS_Mem_zero(&g_disk, sizeof(disk_struct_t)); @@ -271,7 +262,6 @@ void disk_init(uint8_t *mem) g_msd_config.desc_callback_ptr = &desc_callback; g_disk.speed = USB_SPEED_FULL; - g_disk.storage_disk = mem; /* Finally, Initialize the device and USB Stack layers*/ USB_Class_MSC_Init(CONTROLLER_ID, &g_msd_config, &g_disk.app_handle); } diff --git a/laser-tag software/disk.h b/laser-tag software/disk.h index 3c72dd5..8d87896 100644 --- a/laser-tag software/disk.h +++ b/laser-tag software/disk.h @@ -81,7 +81,7 @@ typedef struct _disk_variable_struct /***************************************************************************** * Global Functions *****************************************************************************/ -void disk_init(uint8_t *mem); +void disk_init(); #endif diff --git a/laser-tag software/flash.c b/laser-tag software/flash.c index f1e2a93..f8e8327 100644 --- a/laser-tag software/flash.c +++ b/laser-tag software/flash.c @@ -1,4 +1,5 @@ #include <stdint.h> +#include <string.h> #include "flash.h" #include "fsl_debug_console.h" #include "SSD_FTFx.h" @@ -18,6 +19,7 @@ FLASH_SSD_CONFIG flash_config = { uint8_t ram_fn[0x30]; pFLASHCOMMANDSEQUENCE pFlashCommandSequence; +uint8_t *images[IMAGE_COUNT]; int flash_init() { @@ -32,6 +34,50 @@ int flash_init() (intptr_t)ram_fn, sizeof ram_fn, (intptr_t)FlashCommandSequence); + + /* Our images are in the 2nd block and take up 4096 bytes */ + int i; + for (i = 0; i < IMAGE_COUNT; ++i) { + images[i] = (uint8_t *)flash_config.PFlashBase + + FSL_FEATURE_FLASH_PFLASH_BLOCK_SIZE + i * 4096; + } + return 0; +} + +#define SECTOR_SIZE (FSL_FEATURE_FLASH_PFLASH_BLOCK_SECTOR_SIZE) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +uint8_t RMW_buffer[SECTOR_SIZE]; + +int flash_write(uint32_t addr, uint8_t *data, uint32_t len) +{ + uint32_t sector; + uint32_t end = addr + len; + + for (sector = addr / SECTOR_SIZE * SECTOR_SIZE; + sector < end; + data += SECTOR_SIZE - (addr - sector), + len -= SECTOR_SIZE - (addr - sector), + sector += SECTOR_SIZE, + addr = sector) { + uint32_t rc; + + memcpy(RMW_buffer, (void *)sector, SECTOR_SIZE); + memcpy(&RMW_buffer[addr - sector], + data, + MIN(SECTOR_SIZE - (addr - sector), len)); + rc = FlashEraseSector(&flash_config, sector, SECTOR_SIZE, + pFlashCommandSequence); + if (FTFx_OK != rc) { + debug_printf("Error erasing sector 0x%08x: %u\r\n", sector, rc); + return -1; + } + rc = FlashProgram(&flash_config, sector, SECTOR_SIZE, RMW_buffer, + pFlashCommandSequence); + if (FTFx_OK != rc) { + debug_printf("Error programming sector 0x%08x: %u\r\n", sector, rc); + return -1; + } + } return 0; } diff --git a/laser-tag software/flash.h b/laser-tag software/flash.h index a5c06f1..8bf15db 100644 --- a/laser-tag software/flash.h +++ b/laser-tag software/flash.h @@ -1,6 +1,12 @@ #ifndef _FLASH_H_ #define _FLASH_H_ +#include <stdint.h> + +#define IMAGE_COUNT (5) +extern uint8_t *images[IMAGE_COUNT]; + int flash_init(); +int flash_write(uint32_t addr, uint8_t *data, uint32_t len); #endif diff --git a/laser-tag software/main.c b/laser-tag software/main.c index ba39001..7bb633b 100644 --- a/laser-tag software/main.c +++ b/laser-tag software/main.c @@ -49,20 +49,6 @@ #include "text.h" -//////////////////////////// -// Graphics resources -#include "aha.xbm" -#include "dc24.xbm" -#include "my_name_is.xbm" -#include "threatbutt.xbm" - -static const uint8_t *images[] = { - aha_bits, - dc24_bits, - my_name_is_bits, - threatbutt_bits, -}; -static const int image_count = sizeof images / sizeof *images; static int current_image = 0; static volatile int cue_next_image = 0; static uint32_t laser_pulse_length = 32; @@ -577,6 +563,9 @@ int main (void) /* Blank LED just in case, saves power */ led(0x00, 0x00, 0x00); + /* Initialize flash stuff */ + flash_init(); + /* Init e-paper display */ EPD_Init(); @@ -600,17 +589,14 @@ int main (void) led(0x22, 0x00, 0x22); } - /* Initialize flash stuff */ - flash_init(); - /* No good can come of this */ - disk_init(images[2]); + disk_init(); /* We're done, everything else is triggered through interrupts */ for(;;) { if (cue_next_image) { int old_image = current_image; - current_image = (current_image + 1) % image_count; + current_image = (current_image + 1) % IMAGE_COUNT; debug_printf("drawing %d -> %d\r\n", old_image, current_image); EPD_Draw(images[old_image], images[current_image]); cue_next_image = 0; |