Use mutex/semaphores for emulation, like in Pico and ESP.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos 2024-08-30 12:42:22 +02:00
parent f8c4106367
commit 3235cd8595
No known key found for this signature in database
GPG key ID: C0095B7870A4CCD3
2 changed files with 105 additions and 130 deletions

View file

@ -20,47 +20,48 @@
#include <stdlib.h>
#include <stdio.h>
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#include "pico/stdlib.h"
#include "hardware/flash.h"
#include "hardware/sync.h"
#include "pico/mutex.h"
#include "pico/sem.h"
#include "pico/multicore.h"
#else
#ifdef _MSC_VER
#include <windows.h>
#include <io.h>
#define O_RDWR _O_RDWR
#define O_CREAT _O_CREAT
#define open _open
#define write _write
#define mode_t unsigned short
#define lseek _lseek
#include "mman.h"
#else
#ifdef ESP_PLATFORM
#include "esp_compat.h"
#include "esp_partition.h"
const esp_partition_t *part0;
#define save_and_disable_interrupts() 1
#define flash_range_erase(a,b) esp_partition_erase_range(part0, a, b)
#define flash_range_program(a,b,c) esp_partition_write(part0, a, b, c);
#define restore_interrupts(a) (void)a
#else
#include <unistd.h>
#include <sys/mman.h>
#endif
#endif
#define FLASH_SECTOR_SIZE 4096
#define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024)
#define XIP_BASE 0
int fd_map = 0;
uint8_t *map = NULL;
#include <fcntl.h>
#endif
#include "pico_keys.h"
#include <string.h>
#ifdef PICO_PLATFORM
#include "pico/stdlib.h"
#include "hardware/flash.h"
#include "hardware/sync.h"
#include "pico/mutex.h"
#include "pico/sem.h"
#include "pico/multicore.h"
#else
#ifdef _MSC_VER
#include <windows.h>
#include <io.h>
#define O_RDWR _O_RDWR
#define O_CREAT _O_CREAT
#define open _open
#define write _write
#define mode_t unsigned short
#define lseek _lseek
#include "mman.h"
#else
#ifdef ESP_PLATFORM
#include "esp_compat.h"
#include "esp_partition.h"
const esp_partition_t *part0;
#define save_and_disable_interrupts() 1
#define flash_range_erase(a,b) esp_partition_erase_range(part0, a, b)
#define flash_range_program(a,b,c) esp_partition_write(part0, a, b, c);
#define restore_interrupts(a) (void)a
#else
#include <unistd.h>
#include <sys/mman.h>
#include "emulation.h"
#endif
#endif
#define FLASH_SECTOR_SIZE 4096
#define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024)
#define XIP_BASE 0
int fd_map = 0;
uint8_t *map = NULL;
#include <fcntl.h>
#endif
#define TOTAL_FLASH_PAGES 6
@ -78,10 +79,9 @@ typedef struct page_flash {
static page_flash_t flash_pages[TOTAL_FLASH_PAGES];
#ifndef ENABLE_EMULATION
static mutex_t mtx_flash;
static semaphore_t sem_wait;
#endif
static semaphore_t sem_flash;
#ifndef ENABLE_EMULATION
static bool locked_out = false;
#else
@ -95,83 +95,79 @@ bool flash_available = false;
//this function has to be called from the core 0
void do_flash() {
#ifndef ENABLE_EMULATION
if (mutex_try_enter(&mtx_flash, NULL) == true) {
#endif
if (locked_out == true && flash_available == true && ready_pages > 0) {
//printf(" DO_FLASH AVAILABLE\n");
for (int r = 0; r < TOTAL_FLASH_PAGES; r++) {
if (flash_pages[r].ready == true) {
if (locked_out == true && flash_available == true && ready_pages > 0) {
//printf(" DO_FLASH AVAILABLE\n");
for (int r = 0; r < TOTAL_FLASH_PAGES; r++) {
if (flash_pages[r].ready == true) {
#ifndef ENABLE_EMULATION
//printf("WRITTING %X\n",flash_pages[r].address-XIP_BASE);
while (multicore_lockout_start_timeout_us(1000) == false) {
;
}
//printf("WRITTING %X\n",flash_pages[r].address-XIP_BASE);
uint32_t ints = save_and_disable_interrupts();
flash_range_erase(flash_pages[r].address - XIP_BASE, FLASH_SECTOR_SIZE);
flash_range_program(flash_pages[r].address - XIP_BASE, flash_pages[r].page, FLASH_SECTOR_SIZE);
restore_interrupts(ints);
while (multicore_lockout_end_timeout_us(1000) == false) {
;
}
//printf("WRITEN %X !\n",flash_pages[r].address);
//printf("WRITTING %X\n",flash_pages[r].address-XIP_BASE);
while (multicore_lockout_start_timeout_us(1000) == false) {
;
}
//printf("WRITTING %X\n",flash_pages[r].address-XIP_BASE);
uint32_t ints = save_and_disable_interrupts();
flash_range_erase(flash_pages[r].address - XIP_BASE, FLASH_SECTOR_SIZE);
flash_range_program(flash_pages[r].address - XIP_BASE, flash_pages[r].page, FLASH_SECTOR_SIZE);
restore_interrupts(ints);
while (multicore_lockout_end_timeout_us(1000) == false) {
;
}
//printf("WRITEN %X !\n",flash_pages[r].address);
#else
memcpy(map + flash_pages[r].address, flash_pages[r].page, FLASH_SECTOR_SIZE);
memcpy(map + flash_pages[r].address, flash_pages[r].page, FLASH_SECTOR_SIZE);
#endif
flash_pages[r].ready = false;
ready_pages--;
}
else if (flash_pages[r].erase == true) {
flash_pages[r].ready = false;
ready_pages--;
}
else if (flash_pages[r].erase == true) {
#ifndef ENABLE_EMULATION
while (multicore_lockout_start_timeout_us(1000) == false) {
;
}
//printf("WRITTING\n");
flash_range_erase(flash_pages[r].address - XIP_BASE,
flash_pages[r].page_size ? ((int) (flash_pages[r].page_size /
FLASH_SECTOR_SIZE)) *
FLASH_SECTOR_SIZE : FLASH_SECTOR_SIZE);
while (multicore_lockout_end_timeout_us(1000) == false) {
;
}
while (multicore_lockout_start_timeout_us(1000) == false) {
;
}
//printf("WRITTING\n");
flash_range_erase(flash_pages[r].address - XIP_BASE,
flash_pages[r].page_size ? ((int) (flash_pages[r].page_size /
FLASH_SECTOR_SIZE)) *
FLASH_SECTOR_SIZE : FLASH_SECTOR_SIZE);
while (multicore_lockout_end_timeout_us(1000) == false) {
;
}
#else
memset(map + flash_pages[r].address, 0, FLASH_SECTOR_SIZE);
memset(map + flash_pages[r].address, 0, FLASH_SECTOR_SIZE);
#endif
flash_pages[r].erase = false;
ready_pages--;
flash_pages[r].erase = false;
ready_pages--;
}
}
}
#ifdef ENABLE_EMULATION
msync(map, PICO_FLASH_SIZE_BYTES, MS_SYNC);
msync(map, PICO_FLASH_SIZE_BYTES, MS_SYNC);
#endif
if (ready_pages != 0) {
printf("ERROR: DO FLASH DOES NOT HAVE ZERO PAGES\n");
if (ready_pages != 0) {
printf("ERROR: DO FLASH DOES NOT HAVE ZERO PAGES\n");
}
}
}
flash_available = false;
flash_available = false;
#ifdef ESP_PLATFORM
esp_partition_munmap(fd_map);
esp_partition_mmap(part0, 0, part0->size, ESP_PARTITION_MMAP_DATA, (const void **)&map, (esp_partition_mmap_handle_t *)&fd_map);
#endif
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
}
sem_release(&sem_wait);
esp_partition_munmap(fd_map);
esp_partition_mmap(part0, 0, part0->size, ESP_PARTITION_MMAP_DATA, (const void **)&map, (esp_partition_mmap_handle_t *)&fd_map);
#endif
mutex_exit(&mtx_flash);
}
sem_release(&sem_flash);
}
//this function has to be called from the core 0
void low_flash_init() {
memset(flash_pages, 0, sizeof(page_flash_t) * TOTAL_FLASH_PAGES);
mutex_init(&mtx_flash);
sem_init(&sem_flash, 0, 1);
#if defined(ENABLE_EMULATION)
fd_map = open("memory.flash", O_RDWR | O_CREAT, (mode_t) 0600);
lseek(fd_map, PICO_FLASH_SIZE_BYTES - 1, SEEK_SET);
write(fd_map, "", 1);
map = mmap(0, PICO_FLASH_SIZE_BYTES, PROT_READ | PROT_WRITE, MAP_SHARED, fd_map, 0);
#else
mutex_init(&mtx_flash);
sem_init(&sem_wait, 0, 1);
#if defined(ESP_PLATFORM)
part0 = esp_partition_find_first(0x40, 0x1, "part0");
esp_partition_mmap(part0, 0, part0->size, ESP_PARTITION_MMAP_DATA, (const void **)&map, (esp_partition_mmap_handle_t *)&fd_map);
@ -180,32 +176,22 @@ void low_flash_init() {
}
void low_flash_init_core1() {
#ifndef ENABLE_EMULATION
mutex_enter_blocking(&mtx_flash);
multicore_lockout_victim_init();
#endif
locked_out = true;
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
}
void wait_flash_finish() {
#ifndef ENABLE_EMULATION
sem_acquire_blocking(&sem_wait); //blocks until released
sem_acquire_blocking(&sem_flash); //blocks until released
//wake up
sem_acquire_blocking(&sem_wait); //decrease permits
#endif
sem_acquire_blocking(&sem_flash); //decrease permits
}
void low_flash_available() {
#ifndef ENABLE_EMULATION
mutex_enter_blocking(&mtx_flash);
#endif
flash_available = true;
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
}
page_flash_t *find_free_page(uintptr_t addr) {
@ -216,7 +202,7 @@ page_flash_t *find_free_page(uintptr_t addr) {
flash_pages[r].address == addr_alg) { //first available
p = &flash_pages[r];
if (!flash_pages[r].ready && !flash_pages[r].erase) {
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
#ifdef PICO_PLATFORM
memcpy(p->page, (uint8_t *) addr_alg, FLASH_SECTOR_SIZE);
#else
memcpy(p->page,
@ -241,28 +227,20 @@ int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len) {
return CCID_ERR_NULL_PARAM;
}
#ifndef ENABLE_EMULATION
mutex_enter_blocking(&mtx_flash);
#endif
if (ready_pages == TOTAL_FLASH_PAGES) {
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
printf("ERROR: ALL FLASH PAGES CACHED\n");
return CCID_ERR_NO_MEMORY;
}
if (!(p = find_free_page(addr))) {
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
printf("ERROR: FLASH CANNOT FIND A PAGE (rare error)\n");
return CCID_ERR_MEMORY_FATAL;
}
memcpy(&p->page[addr & (FLASH_SECTOR_SIZE - 1)], data, len);
//printf("Flash: modified page %X with data %x at [%x]\n",(uintptr_t)addr,(uintptr_t)data,addr&(FLASH_SECTOR_SIZE-1));
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
return CCID_OK;
}
@ -280,24 +258,18 @@ int flash_program_uintptr(uintptr_t addr, uintptr_t data) {
uint8_t *flash_read(uintptr_t addr) {
uintptr_t addr_alg = addr & -FLASH_SECTOR_SIZE;
#ifndef ENABLE_EMULATION
mutex_enter_blocking(&mtx_flash);
#endif
if (ready_pages > 0) {
for (int r = 0; r < TOTAL_FLASH_PAGES; r++) {
if (flash_pages[r].ready && flash_pages[r].address == addr_alg) {
uint8_t *v = &flash_pages[r].page[addr & (FLASH_SECTOR_SIZE - 1)];
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
return v;
}
}
}
uint8_t *v = (uint8_t *) addr;
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM)
if (addr >= start_data_pool && addr <= end_rom_pool + sizeof(uintptr_t)) {
v += (uintptr_t) map;
@ -329,29 +301,21 @@ uint8_t flash_read_uint8(uintptr_t addr) {
int flash_erase_page(uintptr_t addr, size_t page_size) {
page_flash_t *p = NULL;
#ifndef ENABLE_EMULATION
mutex_enter_blocking(&mtx_flash);
#endif
if (ready_pages == TOTAL_FLASH_PAGES) {
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
printf("ERROR: ALL FLASH PAGES CACHED\n");
return CCID_ERR_NO_MEMORY;
}
if (!(p = find_free_page(addr))) {
printf("ERROR: FLASH CANNOT FIND A PAGE (rare error)\n");
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
return CCID_ERR_MEMORY_FATAL;
}
p->erase = true;
p->ready = false;
p->page_size = page_size;
#ifndef ENABLE_EMULATION
mutex_exit(&mtx_flash);
#endif
return CCID_OK;
}

View file

@ -63,6 +63,7 @@ extern bool tud_hid_n_report(uint8_t itf, uint8_t report_id, const uint8_t *buff
#endif
#include <pthread.h>
#include <semaphore.h>
typedef struct {
pthread_mutex_t mtx;
pthread_cond_t cnd;
@ -156,4 +157,14 @@ extern pthread_t hcore0, hcore1;
#define multicore_launch_core1(a) pthread_create(&hcore1, NULL, (void *(*) (void *))a, NULL)
#define multicore_reset_core1()
typedef pthread_mutex_t mutex_t;
typedef sem_t semaphore_t;
#define mutex_init(a) pthread_mutex_init(a, NULL)
#define mutex_try_enter(a,b) (pthread_mutex_trylock(a) == 0)
#define mutex_enter_blocking(a) pthread_mutex_lock(a)
#define mutex_exit(a) pthread_mutex_unlock(a)
#define sem_release(a) sem_post(a)
#define sem_acquire_blocking(a) sem_wait(a)
#define multicore_lockout_victim_init() (void)0
#endif // _EMULATION_H_