You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.2 KiB

  1. #include "hal.h"
  2. #include <stddef.h>
  3. /* Murax UART */
  4. typedef struct {
  5. volatile uint32_t DATA;
  6. volatile uint32_t STATUS;
  7. volatile uint32_t CLOCK_DIVIDER;
  8. volatile uint32_t FRAME_CONFIG;
  9. } Uart_Reg;
  10. enum UartParity { NONE = 0, EVEN = 1, ODD = 2 };
  11. enum UartStop { ONE = 0, TWO = 1 };
  12. typedef struct {
  13. uint32_t dataLength;
  14. enum UartParity parity;
  15. enum UartStop stop;
  16. uint32_t clockDivider;
  17. } Uart_Config;
  18. static uint32_t uart_writeAvailability(Uart_Reg* reg)
  19. {
  20. return (reg->STATUS >> 16) & 0xFF;
  21. }
  22. static void uart_write(Uart_Reg* reg, uint32_t data)
  23. {
  24. while (uart_writeAvailability(reg) == 0)
  25. ;
  26. reg->DATA = data;
  27. }
  28. #define UART ((Uart_Reg*)(0xF0010000))
  29. void hal_send(const uint8_t* in, const size_t len) {
  30. for (size_t i = 0; i < len; i++) {
  31. uart_write(UART, in[i]);
  32. }
  33. }
  34. void hal_send_str(const char* in)
  35. {
  36. const char* cur = in;
  37. while(*cur) {
  38. uart_write(UART, *cur);
  39. cur += 1;
  40. }
  41. }
  42. __attribute__((naked)) uint64_t hal_get_time(void)
  43. {
  44. #define LE "\n\t"
  45. asm volatile (LE"csrr a1, mcycleh"
  46. LE"csrr a0, mcycle"
  47. LE"csrr a2, mcycleh"
  48. LE"bne a1, a2, hal_get_time"
  49. LE"ret");
  50. }