Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
316cc8ec7This file is pinned to the selected release. Download the original to open it in your design tool.
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "app.h"
#include "min.h"
#define DELAY_READ_WRITE_MS 1
#define DELAY_CMD_MS 1
struct min_context ctx;
uint16_t flash_buf[128];
pthread_t target_rx_thd;
static void sleep_ms(int n) {
struct timespec t = {.tv_sec = 0, .tv_nsec = n * 1000000};
nanosleep(&t, NULL);
}
void *target_rx(void *vargp) {
int n;
uint8_t buf[UART_RX_BUF_SIZE];
while ((n = read(STDIN_FILENO, buf, UART_RX_BUF_SIZE))) {
min_poll(&ctx, buf, n);
}
return 0;
}
void bus_acquire(void) {
fprintf(stderr, "hw: acquiring bus\n");
sleep_ms(DELAY_CMD_MS);
}
void bus_release(void) {
fprintf(stderr, "hw: releasing bus\n");
sleep_ms(DELAY_CMD_MS);
}
void cmd_chip_erase(void) {
fprintf(stderr, "hw: erasing flash\n");
sleep_ms(DELAY_CMD_MS);
}
void cmd_read(uint32_t addr, uint16_t *buf, size_t n) {
fprintf(stderr, "hw: reading %ld bytes @ %08x\n", 2 * n, addr);
memcpy(buf, flash_buf, 2 * n);
sleep_ms(DELAY_READ_WRITE_MS);
}
void cmd_write(uint32_t addr, uint16_t *buf, size_t n) {
fprintf(stderr, "hw: writing at addr %06x (%ld bytes)\n", addr, 2 * n);
memcpy(flash_buf, buf, 2 * n);
sleep_ms(DELAY_READ_WRITE_MS);
}
void hw_init(void) { sleep_ms(DELAY_CMD_MS); }
void hw_start(void) {
pthread_create(&target_rx_thd, NULL, target_rx, NULL);
sleep_ms(DELAY_CMD_MS);
}
void hw_refresh(void) {}
void hw_send_byte(uint8_t byte) {
fwrite(&byte, 1, 1, stdout);
fflush(stdout);
}
int main(int argc, char *argv[]) {
app_main();
return 0;
}