Skip to content

Commit

Permalink
Adding preliminary support for temperature reading on Hisi
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Dec 11, 2024
1 parent 5dfc9ea commit 3e42afe
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 55 deletions.
15 changes: 15 additions & 0 deletions src/hal/hisi/v2_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -901,4 +901,19 @@ int v2_system_init(char *snrConfig)
return EXIT_SUCCESS;
}

float v2_system_readtemp(void)
{
int val, prep = 0x60fa0000;
float result = 0.0 / 0.0;

if (hal_registry(0x20270110, &val, OP_READ) && prep != val)
hal_registry(0x20270110, &prep, OP_WRITE);

if (!hal_registry(0x20270114, &val, OP_READ))
return result;

result = val & ((1 << 8) - 1);
return ((result * 180) / 256) - 40;
}

#endif
3 changes: 2 additions & 1 deletion src/hal/hisi/v2_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ void *v2_video_thread(void);
int v2_system_calculate_block(short width, short height, v2_common_pixfmt pixFmt,
unsigned int alignWidth);
void v2_system_deinit(void);
int v2_system_init(char *snrConfig);
int v2_system_init(char *snrConfig);
float v2_system_readtemp(void);
22 changes: 22 additions & 0 deletions src/hal/hisi/v3_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -920,4 +920,26 @@ int v3_system_init(char *snrConfig)
return EXIT_SUCCESS;
}

float v3_system_readtemp(void)
{
char v3a_device = 0;
int val, prep = 0x60fa0000;
float result = 0.0 / 0.0;

if (EQUALS(chip, "Hi3516AV200") ||
EQUALS(chip, "Hi3519V101") ||
EQUALS(chip, "Hi3556V100") ||
EQUALS(chip, "Hi3559V100"))
v3a_device = 1;

if (hal_registry(v3a_device ? 0x120a0110 : 0x1203009c, &val, OP_READ) && prep != val)
hal_registry(v3a_device ? 0x120a0110 : 0x1203009c, &prep, OP_WRITE);

if (!hal_registry(v3a_device ? 0x120a0118 : 0x120300a4, &val, OP_READ))
return result;

result = val & ((1 << 10) - 1);
return ((result - 125) / 806) * 165 - 40;
}

#endif
5 changes: 4 additions & 1 deletion src/hal/hisi/v3_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "v3_vi.h"
#include "v3_vpss.h"

#include "../support.h"

#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/select.h>
Expand Down Expand Up @@ -59,4 +61,5 @@ void *v3_video_thread(void);
int v3_system_calculate_block(short width, short height, v3_common_pixfmt pixFmt,
unsigned int alignWidth);
void v3_system_deinit(void);
int v3_system_init(char *snrConfig);
int v3_system_init(char *snrConfig);
float v3_system_readtemp(void);
23 changes: 23 additions & 0 deletions src/hal/hisi/v4_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1006,4 +1006,27 @@ int v4_system_init(char *snrConfig)
return EXIT_SUCCESS;
}

float v4_system_readtemp(void)
{
char v4a_device = 0;
int val, prep;
float result = 0.0 / 0.0;

if (EQUALS(chip, "Hi3516AV300") ||
EQUALS(chip, "Hi3516DV300") ||
EQUALS(chip, "Hi3516CV500"))
v4a_device = 1;

prep = v4a_device ? 0x60fa0000 : 0xc3200000;

if (hal_registry(v4a_device ? 0x120300b4 : 0x120280b4, &val, OP_READ) && prep != val)
hal_registry(v4a_device ? 0x120300b4 : 0x120280b4, &prep, OP_WRITE);

if (!hal_registry(v4a_device ? 0x120300bc : 0x120280bc, &val, OP_READ))
return result;

result = val & ((1 << 10) - 1);
return ((result - (v4a_device ? 136 : 117)) / (v4a_device ? 793 : 798)) * 165 - 40;
}

#endif
3 changes: 2 additions & 1 deletion src/hal/hisi/v4_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ void *v4_video_thread(void);
int v4_system_calculate_block(short width, short height, v4_common_pixfmt pixFmt,
unsigned int alignWidth);
void v4_system_deinit(void);
int v4_system_init(char *snrConfig);
int v4_system_init(char *snrConfig);
float v4_system_readtemp(void);
45 changes: 0 additions & 45 deletions src/hal/support.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,6 @@ hal_platform plat = HAL_PLATFORM_UNK;
char sensor[16] = "unidentified";
int series = 0;

bool hal_registry(unsigned int addr, unsigned int *data, hal_register_op op) {
static int mem_fd;
static char *loaded_area;
static unsigned int loaded_offset;
static unsigned int loaded_size;

unsigned int offset = addr & 0xffff0000;
unsigned int size = 0xffff;
if (!addr || (loaded_area && offset != loaded_offset))
if (munmap(loaded_area, loaded_size))
fprintf(stderr, "hal_registry munmap error: %s (%d)\n",
strerror(errno), errno);

if (!addr) {
close(mem_fd);
return true;
}

if (!mem_fd && (mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
fprintf(stderr, "can't open /dev/mem\n");
return false;
}

volatile char *mapped_area;
if (offset != loaded_offset) {
mapped_area = mmap64(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, offset);
if (mapped_area == MAP_FAILED) {
fprintf(stderr, "hal_registry mmap error: %s (%d)\n",
strerror(errno), errno);
return false;
}
loaded_area = (char *)mapped_area;
loaded_size = size;
loaded_offset = offset;
} else
mapped_area = loaded_area;

if (op & OP_READ)
*data = *(volatile unsigned int *)(mapped_area + (addr - offset));
if (op & OP_WRITE)
*(volatile unsigned int *)(mapped_area + (addr - offset)) = *data;

return true;
}

void hal_identify(void) {
unsigned int val = 0;
FILE *file;
Expand Down
9 changes: 2 additions & 7 deletions src/hal/support.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "tools.h"
#include "types.h"

#if defined(__arm__)
#include "plus/ak_hal.h"
#include "plus/gm_hal.h"
Expand All @@ -16,13 +18,9 @@
#include "plus/cvi_hal.h"
#endif

#include <errno.h>
#include <fcntl.h>
#include <linux/version.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>

// Newer versions of musl have UAPI headers
// that redefine struct sysinfo
Expand All @@ -39,8 +37,6 @@ extern int asprintf(char **restrict strp, const char *restrict fmt, ...);

extern int sysinfo (struct sysinfo *__info);

void *mmap64(void *start, size_t len, int prot, int flags, int fd, off_t off);

extern void *aud_thread;
extern void *isp_thread;
extern void *vid_thread;
Expand All @@ -54,5 +50,4 @@ extern hal_platform plat;
extern char sensor[16];
extern int series;

bool hal_registry(unsigned int addr, unsigned int *data, hal_register_op op);
void hal_identify(void);
45 changes: 45 additions & 0 deletions src/hal/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,51 @@ bool get_uint8(char *str, char *pattern, uint8_t *value) {
return true;
}

bool hal_registry(unsigned int addr, unsigned int *data, hal_register_op op) {
static int mem_fd;
static char *loaded_area;
static unsigned int loaded_offset;
static unsigned int loaded_size;

unsigned int offset = addr & 0xffff0000;
unsigned int size = 0xffff;
if (!addr || (loaded_area && offset != loaded_offset))
if (munmap(loaded_area, loaded_size))
fprintf(stderr, "hal_registry munmap error: %s (%d)\n",
strerror(errno), errno);

if (!addr) {
close(mem_fd);
return true;
}

if (!mem_fd && (mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
fprintf(stderr, "can't open /dev/mem\n");
return false;
}

volatile char *mapped_area;
if (offset != loaded_offset) {
mapped_area = mmap64(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, offset);
if (mapped_area == MAP_FAILED) {
fprintf(stderr, "hal_registry mmap error: %s (%d)\n",
strerror(errno), errno);
return false;
}
loaded_area = (char *)mapped_area;
loaded_size = size;
loaded_offset = offset;
} else
mapped_area = loaded_area;

if (op & OP_READ)
*data = *(volatile unsigned int *)(mapped_area + (addr - offset));
if (op & OP_WRITE)
*(volatile unsigned int *)(mapped_area + (addr - offset)) = *data;

return true;
}

char *memstr(char *haystack, char *needle, int size, char needlesize) {
for (char *p = haystack; p <= (haystack - needlesize + size); p++)
if (!memcmp(p, needle, needlesize)) return p;
Expand Down
10 changes: 10 additions & 0 deletions src/hal/tools.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
#pragma once

#include "types.h"

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <regex.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <unistd.h>

void *mmap64(void *start, size_t len, int prot, int flags, int fd, off_t off);

int base64_encode_length(int len);

Expand All @@ -25,6 +33,8 @@ bool get_uint16(char *str, char *pattern, uint16_t *value);

bool get_uint8(char *str, char *pattern, uint8_t *value);

bool hal_registry(unsigned int addr, unsigned int *data, hal_register_op op);

char *memstr(char *haystack, char *needle, int size, char needlesize);

unsigned int millis();
Expand Down
16 changes: 16 additions & 0 deletions src/region.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ void region_fill_formatted(char* str)
strcat(out, m);
opos += strlen(m);
}
else if (str[ipos + 1] == 'T')
{
ipos++;
char s[8];
float t = 0.0 / 0.0;
switch (plat) {
#if defined(__arm__)
case HAL_PLATFORM_V2: t = v2_system_readtemp(); break;
case HAL_PLATFORM_V3: t = v3_system_readtemp(); break;
case HAL_PLATFORM_V4: t = v4_system_readtemp(); break;
#endif
}
sprintf(s, "%.1f", t);
strcat(out, s);
opos += strlen(s);
}
else if (str[ipos + 1] == 't')
{
ipos++;
Expand Down

0 comments on commit 3e42afe

Please sign in to comment.