#include <stdint.h>

void reg_write_bits_pos(volatile uint32_t *reg, uint32_t data, uint8_t pos, uint32_t mask) {
  *reg &= ~(mask << pos);
  *reg |= (data & mask) << pos;
}

void reg_write_bits(volatile uint32_t *reg, uint32_t data, uint32_t mask) {
  *reg &= ~(mask);
  *reg |= (data & mask);
}

void reg_set_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  *reg |= mask << pos;
}

void reg_set_bits(volatile uint32_t *reg, uint32_t mask) {
  *reg |= mask;
}

void reg_toggle_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  *reg ^= (mask << pos);
}

void reg_toggle_bits(volatile uint32_t *reg, uint32_t mask) {
  *reg ^= mask;
}

uint32_t reg_read_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  return ((*reg) >> pos) & mask;
}

void reg_clear_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  *reg ^= ~(mask << pos);
}

void reg_clear_bits(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  *reg &= ~mask;
}
