9ea7d7b91d00ffece17e4ae2cb0b89e41b71d75a
[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
12 // register set bit at position
13 void rsetbit(volatile uint32_t * reg, short pos) {
14         *reg = *reg | (0x1 << pos);
15 }
16
17 // register set bits from certain pos
18 void rsetbitsfrom(volatile uint32_t * reg, short pos, int val) {
19         *reg = *reg | (val << pos);
20 }
21
22 // register clear bit at position
23 void rclrbit(volatile uint32_t * reg, short pos) {
24         *reg = *reg & ~(0x1 << pos);
25 }
26
27 int rchkbit(volatile uint32_t * reg, short pos) {
28         if ((*reg >> pos) & 0x1)
29                 return 1;
30         return 0;
31 }
32
33 // register (over)write
34 void rwrite(volatile uint32_t * reg, uint32_t val) {
35         *reg = val;
36 }
37
38
39 /* write value (uint8_t) to register */
40 void regw_u8(volatile uint32_t * reg, uint8_t val, short shift, short flag) {
41
42         switch(flag) {
43                 case OWRITE:
44                         *reg = (val << shift);
45                         break;
46                 case SETBIT:
47                         *reg = *reg | (val << shift);
48                         break;
49                 case CLRBIT:
50                         *reg = *reg & ~(val << shift);
51                         break;
52         }
53 }
54
55 /* write value (uint32_t) to register */
56 void regw_u32(volatile uint32_t * reg, uint32_t val, short shift, short flag) {
57
58         switch(flag) {
59                 case OWRITE:
60                         *reg = (val << shift);
61                         break;
62                 case SETBIT:
63                         *reg = *reg | (val << shift);
64                         break;
65                 case CLRBIT:
66                         *reg = *reg & ~(val << shift);
67                         break;
68         }
69 }
70
71 /* Print out the hexidecimal representation of an integer
72    After implementation of scanf or sth this will be obsolete.  */
73
74 char hexbuf[8];
75 char * regtohex(uint32_t addr) {
76         char tmpbuf[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
77         memset(&hexbuf, 0, sizeof(uint32_t) * 8);
78
79
80         for (int i = 0; i < 8 ; i++) {
81                 uint32_t tmp = addr;
82                 tmp = tmp >> (28 - (i * 4));
83                 tmp = tmp & 0xF;
84                 if ((tmp >= 0) && tmp < 10) {
85                         hexbuf[i] = (char) tmp + 48;
86                 }
87                 else {
88                         hexbuf[i] = tmpbuf[tmp - 10];
89                 }
90         }
91         return &hexbuf[0];      
92 }
93 int singlehextoreg(char  hex) {
94
95         int conv = 0;
96         if (hex >= 'A' && hex <= 'F') 
97                 conv = hex - '7';
98
99         else {
100                 conv = hex - '0';
101         }
102         return conv;
103
104 }
105
106 uint32_t hextoreg(char * a) {
107         
108         uint32_t x = 0;
109         int tmp;
110         for(int i = 0; i < 8; i++) {
111                 tmp = singlehextoreg(*a++);
112                 x += tmp << (28 - (i * 4));
113         }
114         return x;
115
116 }
117