tm1637 signs of life, terminal builtin cmds
[cortex-from-scratch] / lib / regfunc.c
1 #include <stdbool.h>
2 #include <stddef.h>
3 #include <stdint.h>
4
5 #include <lib/regfunc.h>
6 #include <lib/string.h>
7 #include <lib/stdio.h>
8
9 #include <sys/mmap.h>
10
11 /* write value (uint8_t) to register */
12 void regw_u8(volatile uint32_t * reg, uint8_t val, short shift, short flag) {
13
14         switch(flag) {
15                 case OWRITE:
16                         *reg = (val << shift);
17                         break;
18                 case SETBIT:
19                         *reg = *reg | (val << shift);
20                         break;
21                 case CLRBIT:
22                         *reg = (val << shift);
23                         break;
24         }
25 }
26
27 /* write value (uint32_t) to register */
28 void regw_u32(volatile uint32_t * reg, uint32_t val, short shift, short flag) {
29
30         switch(flag) {
31                 case OWRITE:
32                         *reg = (val << shift);
33                         break;
34                 case SETBIT:
35                         *reg = *reg | (val << shift);
36                         break;
37                 case CLRBIT:
38                         break;
39         }
40 }
41
42 /* Print out the hexidecimal representation of an integer
43    After implementation of scanf or sth this will be obsolete.  */
44
45 char hexbuf[8];
46 char * regtohex(uint32_t addr) {
47         char tmpbuf[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
48         memset(&hexbuf, 0, sizeof(uint32_t) * 8);
49
50
51         for (int i = 0; i < 8 ; i++) {
52                 uint32_t tmp = addr;
53                 tmp = tmp >> (28 - (i * 4));
54                 tmp = tmp & 0xF;
55                 if ((tmp >= 0) && tmp < 10) {
56                         hexbuf[i] = (char) tmp + 48;
57                 }
58                 else {
59                         hexbuf[i] = tmpbuf[tmp - 10];
60                 }
61         }
62         return &hexbuf[0];      
63 }
64 int singlehextoreg(char  hex) {
65
66         int conv = 0;
67         if (hex >= 'A' && hex <= 'F') 
68                 conv = hex - '7';
69
70         else {
71                 conv = hex - '0';
72         }
73         return conv;
74
75 }
76
77 uint32_t hextoreg(char * a) {
78         
79         uint32_t x = 0;
80         int tmp;
81         for(int i = 0; i < 8; i++) {
82                 tmp = singlehextoreg(*a++);
83                 x += tmp << (28 - (i * 4));
84         }
85         return x;
86
87 }
88