raspberry3_bare/utils.c

101 lines
1.6 KiB
C
Raw Permalink Normal View History

2022-03-17 22:35:47 +01:00
#include "clock.h"
2022-03-21 21:54:58 +01:00
#include "uart.h"
#include <stdint.h>
2022-03-15 23:09:55 +01:00
// K&R inspirated
void printhex(unsigned int d)
{
char n;
2022-03-21 21:54:58 +01:00
for (int c = 28; c >= 0; c -= 4) {
2022-03-15 23:09:55 +01:00
// get highest tetrad
2022-03-21 21:54:58 +01:00
n = (d >> c) & 0xF;
2022-03-15 23:09:55 +01:00
// 0-9 => '0'-'9', 10-15 => 'A'-'F'
2022-03-21 21:54:58 +01:00
n += n > 9 ? 0x37 : 0x30;
2022-03-15 23:09:55 +01:00
putc(n);
}
}
int strlen(char s[])
{
2022-03-21 21:54:58 +01:00
int i = 0;
while (s[i] != '\0') {
2022-03-15 23:09:55 +01:00
++i;
}
return i;
}
void reverse(char s[])
{
int c, i, j;
2022-03-21 21:54:58 +01:00
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
2022-03-15 23:09:55 +01:00
}
}
void itoa(unsigned int n, char s[])
{
2022-03-21 21:54:58 +01:00
int i = 0;
2022-03-15 23:09:55 +01:00
do {
2022-03-21 21:54:58 +01:00
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
s[i] = '\0';
2022-03-15 23:09:55 +01:00
reverse(s);
}
void printdec(unsigned int d)
{
char s[16];
itoa(d, s);
2022-03-21 21:54:58 +01:00
for (int i = 0; i < strlen(s); i++) {
2022-03-15 23:09:55 +01:00
putc(s[i]);
}
}
2022-03-17 22:35:47 +01:00
void printclock()
{
static const char *clocktxts[] = {"reserved", "EMMC", "UART", "ARM", "CORE",
"V3D", "H264", "ISP", "SDRAM", "PIXEL",
"PWM", "HEVC", "EMMC2", "M2MC", "PIXEL_BVB"};
puts("Clocks:\n");
2022-03-21 21:54:58 +01:00
for (int i = 1; i <= CLK_PIXEL_BVB; i++) {
2022-03-17 22:35:47 +01:00
puts(" ");
puts(clocktxts[i]);
puts(": ");
printdec(getclock(i, 0));
puts("/");
printdec(getclock(i, 1));
puts(" Hz\n");
}
}
2022-03-21 21:54:58 +01:00
int readline(char *buf, int maxlen)
{
int num = 0;
while (num < maxlen - 1) {
char c = uart_recv();
// It seems like screen sends \r when I press enter
if (c == '\n' || c == '\0' || c == '\r') {
break;
}
buf[num] = c;
num++;
}
buf[num] = '\0';
return num;
}
// Returns <0 if s1<s2, 0 if s1==s2, >0 if s1>s2
int strcmp(const char s1[], const char s2[])
{
int i;
for (i = 0; s1[i] == s2[i]; i++) {
if (s1[i] == '\0')
return 0;
}
return s1[i] - s2[i];
}