Advertisement
Soupborsh

b288 uart putc baremetal

Dec 15th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | Source Code | 0 0
  1. // SPDX-License-Identifier: GPL-2.0 or later
  2. // Made by Soupborsh, tested on PB618. I tried to do puts function but it outputs garbage
  3.  
  4. // compile and run this by:
  5. // arm-none-eabi-gcc -I /usr/include/ -nostdlib -nostartfiles -static serial-putc.c -o serial-putc -O3
  6. // arm-none-eabi-objcopy -O binary serial-putc serial-putc.bin
  7. // sunxi-fel write 0x2000 /path/to/serial-putc.bin
  8. // sunxi-fel exe 0x2000
  9.  
  10. #include <stdint.h>
  11.  
  12. #define UART0_BASE 0x01c28000
  13. #define UART_VALUE_TX_MODE 0x00100000
  14. #define UART_VALUE_RX_MODE 0x00000000
  15. #define UART_VALUE_CHAR 0x000000ff
  16.  
  17. uint32_t inline readl(volatile uint32_t *address);
  18. void inline writel(volatile uint32_t *address, uint32_t value);
  19. void uart_putc(const char c);
  20.  
  21. void main(void) {
  22.   uart_putc('B');
  23.   uart_putc('2');
  24.   uart_putc('8');
  25.   uart_putc('8');
  26.   uart_putc('\n');
  27.   return;
  28. }
  29.  
  30. void inline writel(volatile uint32_t *address, uint32_t value) {
  31.   volatile uint32_t *ptr = address;
  32.   *ptr = value;
  33.   return;
  34. }
  35.  
  36. uint32_t inline readl(volatile uint32_t *address) {
  37.   volatile uint32_t *ptr = address;
  38.   return *ptr;
  39. }
  40.  
  41. void uart_putc(const char c) {
  42.   while (((readl((volatile uint32_t *)UART0_BASE)) & UART_VALUE_TX_MODE) !=
  43.          UART_VALUE_RX_MODE)
  44.     ;
  45.   writel((volatile uint32_t *)UART0_BASE,
  46.          UART_VALUE_TX_MODE | (c & UART_VALUE_CHAR));
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement