1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
4 * 2019/7/20 - ROBIN KRENS
8 * Helper functions to set registers
16 #include <lib/regfunc.h>
17 #include <lib/string.h>
18 #include <lib/stdio.h>
20 #include <lib/tinyprintf.h>
23 #include <sys/robsys.h>
25 // register set bit at position
26 void rsetbit(volatile uint32_t * reg, short pos) {
27 *reg = *reg | (0x1 << pos);
30 // register set bits from certain pos
31 void rsetbitsfrom(volatile uint32_t * reg, short pos, int val) {
32 *reg = *reg | (val << pos);
35 // register clear bit at position
36 void rclrbit(volatile uint32_t * reg, short pos) {
37 *reg = *reg & ~(0x1 << pos);
40 // check if a bit is set
41 int rchkbit(volatile uint32_t * reg, short pos) {
42 if ((*reg >> pos) & 0x1)
47 // register (over)write
48 void rwrite(volatile uint32_t * reg, uint32_t val) {
53 /* Deprecated use printf instead
55 char * regtohex(uint32_t addr) {
56 char tmpbuf[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
57 memset(&hexbuf, 0, sizeof(uint32_t) * 8);
60 for (int i = 0; i < 8 ; i++) {
62 tmp = tmp >> (28 - (i * 4));
64 if ((tmp >= 0) && tmp < 10) {
65 hexbuf[i] = (char) tmp + 48;
68 hexbuf[i] = tmpbuf[tmp - 10];
74 // TODO: implement simple scanf functions
75 int singlehextoreg(char hex) {
78 if (hex >= 'A' && hex <= 'F')
88 uint32_t hextoreg(char * a) {
92 for(int i = 0; i < 8; i++) {
93 tmp = singlehextoreg(*a++);
94 x += tmp << (28 - (i * 4));
100 /* Busy-loop block implementation. Each iteration will take 3 CPU cycles.
101 * Of course, when interrupts are enabled, the exact delay time will be
104 * Example: for a standard STM32x config board (8MHz) the maximum delay is
105 * 0xFFFF * (1/8,000,000) * 3 = 24.58ms
106 * 0xFFFFFFFF * (1/8MHz) * 3 = 1610ms
108 /* static void __block(uint32_t count) {
112 /* Delay us microsecond
113 * Note: delay includes setup time (about 4 clockcycles), so is quite
115 void _block(uint32_t us) {
117 uint32_t count = (us/3) * CLKSPEED_MHZ; // x cycles
118 asm volatile("b1: subs %0, %1, #1" "\n\t"
119 "bne b1" : "=r" (count) : "r" (count));