utils.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stddef.h>
  16. #include "encoding.h"
  17. #include "utils.h"
  18. void set_bit(volatile uint32_t *bits, uint32_t mask, uint32_t value)
  19. {
  20. uint32_t org = (*bits) & ~mask;
  21. *bits = org | (value & mask);
  22. }
  23. void set_bit_offset(volatile uint32_t *bits, uint32_t mask, size_t offset, uint32_t value)
  24. {
  25. set_bit(bits, mask << offset, value << offset);
  26. }
  27. void set_gpio_bit(volatile uint32_t *bits, size_t offset, uint32_t value)
  28. {
  29. set_bit_offset(bits, 1, offset, value);
  30. }
  31. uint32_t get_bit(volatile uint32_t *bits, uint32_t mask, size_t offset)
  32. {
  33. return ((*bits) & (mask << offset)) >> offset;
  34. }
  35. uint32_t get_gpio_bit(volatile uint32_t *bits, size_t offset)
  36. {
  37. return get_bit(bits, 1, offset);
  38. }
  39. uint32_t is_memory_cache(uintptr_t address)
  40. {
  41. #define MEM_CACHE_LEN (6 * 1024 * 1024)
  42. return ((address >= 0x80000000) && (address < 0x80000000 + MEM_CACHE_LEN));
  43. }