Created
July 15, 2018 08:55
-
-
Save k3170makan/e024310a74c3eac0e4fb14d38d3cba54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stddef.h> | |
| #include <stdint.h> | |
| static inline void mmio_write(uint32_t reg,uint32_t data){ | |
| *(volatile uint32_t*)reg = data; | |
| } | |
| static inline uint32_t mmio_read(uint32_t reg){ | |
| return *(volatile uint32_t*) reg; | |
| } | |
| static inline void delay(int32_t count){ | |
| asm volatile( | |
| "__delay_%=: subs %[count], %[count], #1; bne __delay_%=\n" : "=r" (count):[count]"0"(count) : "cc" ); | |
| } | |
| //should move this to the uart file laters | |
| //the GPIO is offered as a list of memory addresses all we need to do is add handles to the addressses to get to the functions they host | |
| enum{ | |
| GPIO_BASE = 0x3F200000, | |
| GPPUD = (GPIO_BASE + 0x94), | |
| GPPUDCLK0 = (GPIO_BASE + 0x98), | |
| UART0_BASE = 0x3F201000, | |
| UART0_DR = (UART0_BASE + 0x00), | |
| UART0_RSRECR = (UART0_BASE + 0x04), | |
| UART0_FR = (UART0_BASE + 0x18), | |
| UART0_ILPR = (UART0_BASE + 0x20), | |
| UART0_IBRD = (UART0_BASE + 0x24), | |
| UART0_FBRD = (UART0_BASE + 0x28), | |
| UART0_LCRH = (UART0_BASE + 0x2C), | |
| UART0_CR = (UART0_BASE + 0x30), | |
| UART0_IFLS = (UART0_BASE + 0x34), | |
| UART0_IMSC = (UART0_BASE + 0x38), | |
| UART0_RIS = (UART0_BASE + 0x3C), | |
| UART0_MIS = (UART0_BASE + 0x40), | |
| UART0_ICR = (UART0_BASE + 0x44), | |
| UART0_DMACR = (UART0_BASE + 0x48), | |
| UART0_ITCR = (UART0_BASE + 0x80), | |
| UART0_ITIP = (UART0_BASE + 0x84), | |
| UART0_ITOP = (UART0_BASE + 0x88), | |
| UART0_TDR = (UART0_BASE + 0x8C) | |
| }; | |
| /* | |
| void uart_init(); | |
| - set up registers and turn off the functions we don't need | |
| */ | |
| void uart_init(){ | |
| mmio_write(UART0_CR,0x000000); | |
| mmio_write(GPPUD,0x00000000); | |
| delay(150); | |
| mmio_write(GPPUDCLK0, (1 << 14) | (1 << 15)); | |
| delay(150); | |
| mmio_write(GPPUDCLK0,0x00000000); | |
| mmio_write(UART0_ICR,0x7FF); | |
| mmio_write(UART0_IBRD,1); | |
| mmio_write(UART0_FBRD,40); | |
| mmio_write(UART0_IMSC, (1 << 1) | (1 << 4) | (1 << 5) | (1 << 6)); | |
| mmio_write(UART0_LCRH, (1 << 4) | | |
| (1 << 5) | | |
| (1 << 6) | | |
| (1 << 7) | | |
| (1 << 8) | | |
| (1 << 9) | | |
| (1 << 10)); | |
| mmio_write(UART0_CR, (1 << 0) | | |
| (1 << 8) | | |
| (1 << 9)); | |
| } | |
| void uart_putc(unsigned char c){ | |
| while ( mmio_read(UART0_FR) & ( 1 << 5)) {} | |
| mmio_write(UART0_DR, c); | |
| } | |
| unsigned char uart_getc(){ | |
| while ( mmio_read(UART0_FR) & ( 1 << 4)) {} | |
| return mmio_read(UART0_DR); | |
| } | |
| void uart_puts(const char* str){ | |
| for (size_t i = 0; str[i] != '\0';i++){ | |
| uart_putc((unsigned char) str[i]); | |
| } | |
| } | |
| void kernel_main(uint32_t r0, uint32_t r1, uint32_t atags){ | |
| (void) r0; | |
| (void) r1; | |
| (void) atags; | |
| //we are probably going to do some cool stuff with the atags later | |
| uart_init(); | |
| uart_puts("Hi this is Keith's raspberry pi kernel :)\r\n"); | |
| uart_puts("Anyting you'd like to say:"); | |
| while(1){ | |
| uart_putc(uart_getc()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment