tested working terminal and reordering of code
[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 a printf function, this code
44    will be obsolete.  */
45
46 char hexbuf[8];
47 char * regtohex(uint32_t addr) {
48         char tmpbuf[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
49         memset(&hexbuf, 0, sizeof(uint32_t) * 8);
50
51
52         for (int i = 0; i < 8 ; i++) {
53                 uint32_t tmp = addr;
54                 tmp = tmp >> (28 - (i * 4));
55                 tmp = tmp & 0xF;
56                 if ((tmp >= 0) && tmp < 10) {
57                         hexbuf[i] = (char) tmp + 48;
58                 }
59                 else {
60                         hexbuf[i] = tmpbuf[tmp - 10];
61                 }
62         }
63         return &hexbuf[0];      
64 }