Skip to content

Commit 5180e68

Browse files
committed
misrc_capture support for non-Linux POSIX platforms, reduced hsdaoh framerate
1 parent 40b6953 commit 5180e68

File tree

6 files changed

+134
-18
lines changed

6 files changed

+134
-18
lines changed

firmware/hsdaoh/hsdaoh_nano20k_misrc/top.v

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ module top (
1010
tmds_d_n,
1111
tmds_d_p,
1212
adc0_data,
13-
adc1_data,
14-
gpio_data,
13+
adc1_data,
14+
gpio_data,
1515
adc_clk,
1616
uart_rx,
1717
uart_tx,
@@ -27,8 +27,8 @@ module top (
2727
output wire [2:0] tmds_d_p;
2828

2929
input wire [11:0] adc0_data;
30-
input wire [11:0] adc1_data;
31-
input wire [7:0] gpio_data;
30+
input wire [11:0] adc1_data;
31+
input wire [7:0] gpio_data;
3232

3333

3434
input wire uart_rx;
@@ -45,10 +45,14 @@ module top (
4545

4646
// assign adc_clkout = clk_data_pll0_ext;
4747

48+
// with FBDIV = 52 (maximum)
4849
// 477 MHz, maximum that works with the nano 20K
4950
// 477/5 = 95.4 MHz
51+
// with FBDIV = 47 (enough for 32 bit at 40 MHz)
52+
// 432 MHz
53+
// 432/5 = 86.4 MHz
5054
localparam HDMI_PLL_IDIV = 2;
51-
localparam HDMI_PLL_FBDIV = 52;
55+
localparam HDMI_PLL_FBDIV = 47;
5256
localparam HDMI_PLL_ODIV = 2;
5357

5458
// PLL for HDMI clock
@@ -92,7 +96,7 @@ module top (
9296
reg [31:0] fifo_in;
9397

9498
wire write_enable;
95-
wire dword_enable;
99+
wire dword_enable;
96100

97101
wire [31:0] fifo_out;
98102
wire fifo_empty;
@@ -105,7 +109,7 @@ module top (
105109

106110
wire [31:0] settings;
107111

108-
assign dword_enable = settings[19];
112+
assign dword_enable = settings[19];
109113

110114
async_fifo #(
111115
.DSIZE(32),

misrc_tools/cthreads.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <pthread.h>
2+
3+
typedef pthread_t thrd_t;
4+
typedef int (*thrd_start_t) (void*);
5+
6+
#define thrd_success 0
7+
8+
#define thrd_create(a,b,c) pthread_create(a,NULL,b,c)
9+
#define thrd_join(a,b) pthread_join(a,b)
10+
#define thrd_sleep(a,b) nanosleep(a,b)

misrc_tools/extract.asm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ default rel
5252
%endif
5353
%endmacro
5454

55-
section .note.GNU-stack noalloc noexec nowrite progbits
55+
%ifidn __OUTPUT_FORMAT__,elf64
56+
section .note.GNU-stack noalloc noexec nowrite progbits
57+
%endif
5658

5759
section .data
5860
ALIGN 16

misrc_tools/misrc_capture.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
#include <math.h>
3030
#include <stdint.h>
3131
#include <stdbool.h>
32+
#if __STDC_NO_THREADS__
33+
#include "cthreads.h"
34+
#else
3235
#include <threads.h>
36+
#endif
3337
#include <time.h>
3438

3539
#ifndef _WIN32
@@ -335,7 +339,7 @@ int main(int argc, char **argv)
335339
break;
336340
#endif
337341
case 'x':
338-
output_names[2] = optarg;
342+
output_name_aux = optarg;
339343
break;
340344
case 'r':
341345
output_name_raw = optarg;

misrc_tools/ringbuffer.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,14 @@
2121
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
*/
2323

24-
#define _GNU_SOURCE
24+
#include "shm_anon.h"
2525
#include <string.h>
2626
#include <unistd.h>
27-
#include <linux/memfd.h>
2827
#include <sys/mman.h>
2928
#include <sys/types.h>
3029

3130
#include "ringbuffer.h"
3231

33-
#if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 27)
34-
#include <sys/syscall.h>
35-
36-
static inline int memfd_create(const char *name, unsigned int flags) {
37-
return syscall(__NR_memfd_create, name, flags);
38-
}
39-
#endif
4032

4133
int rb_init(ringbuffer_t *rb, char *name, size_t size) {
4234

misrc_tools/shm_anon.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// based on: https://github.com/lassik/shm_open_anon/
2+
3+
// Copyright 2019 Lassi Kortela
4+
// SPDX-License-Identifier: ISC
5+
6+
#ifdef __linux__
7+
#define _GNU_SOURCE
8+
#include <linux/memfd.h>
9+
#include <linux/unistd.h>
10+
#endif
11+
12+
#include <sys/types.h>
13+
14+
#include <sys/mman.h>
15+
16+
#include <errno.h>
17+
#include <fcntl.h>
18+
#include <string.h>
19+
#include <time.h>
20+
#include <unistd.h>
21+
22+
#undef IMPL_MEMFD
23+
#undef IMPL_POSIX
24+
#undef IMPL_SHM_ANON
25+
#undef IMPL_SHM_MKSTEMP
26+
#undef IMPL_UNLINK_OR_CLOSE
27+
28+
#if defined(__linux__)
29+
#if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 27)
30+
#include <sys/syscall.h>
31+
static inline int memfd_create(const char *name, unsigned int flags) {
32+
return syscall(__NR_memfd_create, name, flags);
33+
}
34+
#endif
35+
#elif defined(__FreeBSD__)
36+
#define memfd_create(a,b) shm_open(SHM_ANON,O_RDWR,0)
37+
#elif defined(__OpenBSD__)
38+
#define IMPL_SHM_MKSTEMP
39+
#else
40+
#define IMPL_POSIX
41+
#endif
42+
43+
#if defined(IMPL_POSIX) || defined(IMPL_SHM_MKSTEMP)
44+
#define IMPL_UNLINK_OR_CLOSE
45+
#endif
46+
47+
#ifdef IMPL_UNLINK_OR_CLOSE
48+
static int
49+
shm_unlink_or_close(const char *name, int fd)
50+
{
51+
int save;
52+
53+
if (shm_unlink(name) == -1) {
54+
save = errno;
55+
close(fd);
56+
errno = save;
57+
return -1;
58+
}
59+
return fd;
60+
}
61+
#endif
62+
63+
#ifdef IMPL_POSIX
64+
static int
65+
memfd_create(const char *_name, unsigned int flags)
66+
{
67+
char name[16] = "/shm-";
68+
struct timespec tv;
69+
unsigned long r;
70+
char *const limit = name + sizeof(name) - 1;
71+
char *start;
72+
char *fill;
73+
int fd, tries;
74+
75+
*limit = 0;
76+
start = name + strlen(name);
77+
for (tries = 0; tries < 4; tries++) {
78+
clock_gettime(CLOCK_REALTIME, &tv);
79+
r = (unsigned long)tv.tv_sec + (unsigned long)tv.tv_nsec;
80+
for (fill = start; fill < limit; r /= 8)
81+
*fill++ = '0' + (r % 8);
82+
fd = shm_open(
83+
name, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
84+
if (fd != -1)
85+
return shm_unlink_or_close(name, fd);
86+
if (errno != EEXIST)
87+
break;
88+
}
89+
return -1;
90+
}
91+
#endif
92+
93+
#ifdef IMPL_SHM_MKSTEMP
94+
static int
95+
memfd_create(const char *_name, unsigned int flags)
96+
{
97+
char name[16] = "/shm-XXXXXXXXXX";
98+
int fd;
99+
100+
if ((fd = shm_mkstemp(name)) == -1)
101+
return -1;
102+
return shm_unlink_or_close(name, fd);
103+
}
104+
#endif

0 commit comments

Comments
 (0)