Skip to content

Commit

Permalink
libcatom improvement and fixes
Browse files Browse the repository at this point in the history
- use pragma instead of header guards
- add interrupt and exception support
- add assert.h
- improve stdio
    - add stream support with the help of file.h (streams are files now)
    - add file stream operations
- add file.h
- other minor fixes
  • Loading branch information
saursin committed Nov 13, 2023
1 parent cc408a9 commit 50ee650
Show file tree
Hide file tree
Showing 38 changed files with 805 additions and 499 deletions.
6 changes: 1 addition & 5 deletions sw/lib/include/W25Q64.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef __W25Q64_H__
#define __W25Q64_H__
#pragma once
#include <stdint.h>

// CODES
Expand Down Expand Up @@ -135,6 +134,3 @@ bool W25Q64_eraseAll(bool flagwait);
* @return char* buffer to store bytes
*/
uint8_t *W25Q64_read(uint8_t *buf, uint32_t len, uint32_t addr);


#endif //__W25Q64_H__
112 changes: 112 additions & 0 deletions sw/lib/include/arch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#pragma once
#include <utils.h>

/**
* @brief Get the hartid
* @return int hartid
*/
static inline int get_hartid(){
unsigned id;
asm volatile("csrr %0, mhartid" : "=r" (id));
return id;
}

/**
* @brief enable global interrupts
*/
static inline void arch_en_int(){
unsigned bits = (1 << 3);
asm volatile("csrs mstatus, %0" : : "r" (bits));
}

/**
* @brief Disable global interrupts
*/
static inline void arch_di_int(){
unsigned bits = (1 << 3);
asm volatile("csrc mstatus, %0" : : "r" (bits));
}

/**
* @brief Enable machine software interrupts
*/
static inline void arch_en_msi()
{
unsigned bits = (1 << 3);
asm volatile("csrs mie, %0" : : "r" (bits));
}

/**
* @brief Disable machine software interrupts
*/
static inline void arch_di_msi()
{
unsigned bits = (1 << 3);
asm volatile("csrc mie, %0" : : "r" (bits));
}

/**
* @brief Enable machine timer interrupts
*/
static inline void arch_en_mti()
{
unsigned bits = (1 << 7);
asm volatile("csrs mie, %0" : : "r" (bits));
}

/**
* @brief Disable machine timer interrupts
*/
static inline void arch_di_mti()
{
unsigned bits = (1 << 7);
asm volatile("csrc mie, %0" : : "r" (bits));
}

/**
* @brief Enable machine external interrupts
*/
static inline void arch_en_mei()
{
unsigned bits = (1 << 11);
asm volatile("csrs mie, %0" : : "r" (bits));
}

/**
* @brief Disable machine external interrupts
*/
static inline void arch_di_mei()
{
unsigned bits = (1 << 11);
asm volatile("csrc mie, %0" : : "r" (bits));
}

_WEAK void arch_unhandled_irq();
_WEAK void arch_unhandled_exception();

/**
* @brief Register exception handler
* @param id exception id
* @param handler handler function
*/
void register_exception_handler(unsigned id, void (*handler)(void));

/**
* @brief Deregister exception handler
* @param id exception id
*/
void deregister_exception_handler(unsigned id);

/**
* @brief Register interrupt handler
* @param id Interrupt id
* @param handler handler function
*/
void register_interrupt_handler(unsigned id, void (*handler)(void));

/**
* @brief Deregister interrupt handler
* @param id interrupt id
*/
void deregister_interrupt_handler(unsigned id);

11 changes: 11 additions & 0 deletions sw/lib/include/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <utils.h>


#define assert(x) \
if(!(x)) { \
puts("[ERROR]: Assert Failed:" EXPAND_AND_STRINGIFY(__FILE__) EXPAND_AND_STRINGIFY(__LINE__)); \
exit(1); \
}
43 changes: 0 additions & 43 deletions sw/lib/include/board.h

This file was deleted.

7 changes: 2 additions & 5 deletions sw/lib/include/csr.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#ifndef __CSR_H__
#define __CSR_H__

#pragma once
#include <utils.h>
#include <stdint.h>

enum CSR_ID
Expand Down Expand Up @@ -80,5 +79,3 @@ inline void __attribute__ ((always_inline)) CSR_write(const int csr_id, uint32_t
register uint32_t csr_data = data;
asm volatile ("csrw %[input_i], %[input_j]" : : [input_i] "i" (csr_id), [input_j] "r" (csr_data));
}

#endif // __CSR_H__
30 changes: 30 additions & 0 deletions sw/lib/include/file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include <stdint.h>

// Pseudo File System
typedef struct {
int (*write)(char *, uint32_t);
int (*read)(char *, uint32_t);
} Device_t;

extern Device_t stddev[];

/**
* @brief Write characters into device from buffer
*
* @param dev device
* @param buf buffer
* @param n number of chars
* @return int
*/
int dev_write(const Device_t *dev, char *buf, uint32_t n);

/**
* @brief Read characters into buffer from device
*
* @param dev device
* @param buf buffer
* @param n number of chars
* @return int
*/
int dev_read(const Device_t *dev, char *buf, uint32_t n);
7 changes: 1 addition & 6 deletions sw/lib/include/gpio.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#ifndef __GPIO_H__
#define __GPIO_H__

#pragma once
#include <stdint.h>
#include <stdbool.h>

typedef enum {
LOW=0,
Expand Down Expand Up @@ -73,5 +70,3 @@ void gpio_setmode(int pin, gpio_direction_t mode);
* @return gpio_direction_t
*/
gpio_direction_t gpio_getmode(int pin);

#endif // __GPIO_H__
5 changes: 1 addition & 4 deletions sw/lib/include/lcd.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef __LCD_H__
#define __LCD_H__
#pragma once

// commands
#define LCD_CLEARDISPLAY 0x01 //!< Clear display, set cursor position to zero
Expand Down Expand Up @@ -120,5 +119,3 @@ void lcd_scrollLeft(void);
* @brief Scroll Display Right
*/
void lcd_scrollRight(void);

#endif //__LCD_H__
5 changes: 1 addition & 4 deletions sw/lib/include/limits.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef __LIMITS_H__
#define __LIMITS_H__
#pragma once

#define LONG_BIT 32
#define LONG_MAX 0x7fffffffL
Expand Down Expand Up @@ -59,5 +58,3 @@

/* Maximum multibyte length of a character across all locales */
#define MB_LEN_MAX 4

#endif // __LIMITS_H__
5 changes: 1 addition & 4 deletions sw/lib/include/math.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef __MATH_H__
#define __MATH_H__
#pragma once

/**
* @brief Computes square root of given number
Expand Down Expand Up @@ -47,5 +46,3 @@ int max(int x, int y);
* @return float
*/
float pow(float x, int y);

#endif //__MATH_H__
5 changes: 1 addition & 4 deletions sw/lib/include/mmio.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#ifndef __MMIO_H__
#define __MMIO_H__
#pragma once

// Memory-Mapped Reg Access Macros
#define REG8(base, offset) *((volatile uint8_t*) (base + offset))
#define REG16(base, offset) *((volatile uint16_t*) (base + offset))
#define REG32(base, offset) *((volatile uint32_t*) (base + offset))

#endif // __MMIO_H__
22 changes: 9 additions & 13 deletions sw/lib/include/platform.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
#ifndef __PLATFORM_H__
#define __PLATFORM_H__
#pragma once

// ============== Target Independent Definitions ==============

// Bitmanip Macros
#define bitset(data, nbit) ((data) | (1<<(nbit)))
#define bitclear(data, nbit) ((data) & ~(1<<(nbit)))
#define bitflip(data, nbit) ((data) ^ (1<<(nbit)))
#define bitcheck(data, nbit) ((data) & (1<<(nbit)))
// #define bitsetv(data, nbit, val)(((data) &= ~(1<<(nbit))) | (val << nbit))

#define N_STDDEV 4
#define DEV_STDIN 0
#define DEV_STDOUT 1
#define DEV_STDERR 2

// ============== Target Specific Definitions ==============
#ifdef TARGET_HYDROGENSOC
// #define EXCEPTION
#define EXCEPTION

extern int __coderam_start;
extern int __coderam_size;
Expand All @@ -39,6 +34,9 @@
#define SPI
#define SPI_ADDR 0x40003000

// --------- TIMER -----------
#define TIMER_ADDR 0x40004000

// ------ CLOCK FREQUENCY --------
#define CLK_FREQ 12000000

Expand All @@ -65,5 +63,3 @@
#warning Must define a target for platform.h
#endif
#endif

#endif //__PLATFORM_H__
37 changes: 1 addition & 36 deletions sw/lib/include/serial.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
#ifndef __SERIAL_H__
#define __SERIAL_H__

#include <stdint.h>
#pragma once
#include <stdbool.h>
#include "platform.h"

#include "platform.h"

// Register Offsets
#define UART_REG_RBR 0x00
#define UART_REG_THR 0x00
#define UART_REG_LCR 0x04
#define UART_REG_LSR 0x08
#define UART_REG_DIV 0x0c

// THR
#define UART_REG_THR_TXDA 0xff
#define UART_REG_RBR_RXDA 0xff

// LCR
#define UART_REG_LCR_RXEN 0b00000001
#define UART_REG_LCR_TXEN 0b00000010
#define UART_REG_LCR_STPB 0b00000100
#define UART_REG_LCR_PARB 0b00001000
#define UART_REG_LCR_EPAR 0b00010000
#define UART_REG_LCR_LPBK 0b10000000

// LSR
#define UART_REG_LCR_RVAL 0b00000001
#define UART_REG_LCR_TEMT 0b00000010
#define UART_REG_LCR_FERR 0b00000100
#define UART_REG_LCR_PERR 0b00001000


// Baud Rates
#ifdef SIM
Expand Down Expand Up @@ -123,6 +91,3 @@ void serial_write(char c);
* @return char
*/
char serial_read();


#endif //__SERIAL_H__
Loading

0 comments on commit 50ee650

Please sign in to comment.