Skip to content

Commit

Permalink
Major refactor I somehow forgot about and futile attemt to fix I2C (#75)
Browse files Browse the repository at this point in the history
* Major refactor I somehow forgot about and futile attemt to fix I2C

* Replace hal/misc.h

* Use macros to disable stdlib from gpio_ll.h

* Tidy the clang
  • Loading branch information
robotman2412 authored Jun 30, 2024
1 parent 71cf9e0 commit c688ab4
Show file tree
Hide file tree
Showing 96 changed files with 2,006 additions and 852 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[submodule "lib/kbelf"]
path = lib/kbelf
path = kernel/lib/kbelf
url = https://github.com/robotman2412/KiloElfLoader
[submodule "kernel/port/esp_common/esp-idf"]
path = kernel/port/esp_common/esp-idf
Expand Down
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
MAKEFLAGS += --silent
SHELL := /usr/bin/env bash

.PHONY: all prepare openocd gdb flash monitor clean-all clean build clang-format-check clang-tidy-check
.PHONY: all prepare openocd gdb flash monitor clean build clang-format-check clang-tidy-check

all: build

Expand All @@ -17,10 +17,9 @@ build:
$(MAKE) -C files build
$(MAKE) -C kernel build

clean-all: clean
clean:
$(MAKE) -C files clean-all
$(MAKE) -C kernel clean-all
$(MAKE) -C files clean
$(MAKE) -C kernel clean

openocd:
$(MAKE) -C kernel openocd
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
// this file provides convenience macros for the attributes provided by gcc:
// https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html

// Disable address sanitization for a function.
#define NOASAN __attribute__((no_sanitize("address")))

// Declares that a function cannot return.
#define NORETURN __attribute__((noreturn))

// Declares that a function has no observable side effects and does not mutate its parameters.
#define PURE __attribute__((pure))

// Declares that a function has no observable side effects, does not mutate its parameters and does not read memory.
#define CONST __attribute__((pure))
#define CONST __attribute__((const))

// Declares that a function is not called very often and can be size-optimized even in fast builds.
#define COLD __attribute__((cold))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ typedef enum {
_badge_ecause_num,
} badge_ecause_t;

#include "assertions.h"
#include "log.h"
#include "attributes.h"
#include "meta.h"


Expand All @@ -129,12 +128,14 @@ extern char const *badge_eloc_name[_badge_eloc_num];
extern char const *badge_ecause_name[_badge_ecause_num];

// Get the name of a badge_eloc_t.
char const *badge_eloc_get_name(badge_eloc_t eloc) PURE;
char const *badge_eloc_get_name(badge_eloc_t eloc) CONST;
// Get the name of a badge_ecause_t.
char const *badge_ecause_get_name(badge_ecause_t eloc) PURE;
char const *badge_ecause_get_name(badge_ecause_t eloc) CONST;



#ifdef BADGEROS_KERNEL
#include "log.h"
// Show a warning message if the error condition fails.
#define badge_err_log_warn(ec) \
do { \
Expand Down Expand Up @@ -188,17 +189,13 @@ char const *badge_ecause_get_name(badge_ecause_t eloc) PURE;
// Assert if the error condition fails in debug mode.
#define badge_err_assert_dev(ec) ((void)0)
#endif
#endif

// Sets `ec` to the given `location` and `cause` values if `ec` is not `NULL`.
// `ec` must be a variable name.
#define badge_err_set(ec, location_value, cause_value) \
do { \
if ((ec) != NULL) { \
if (cause_value) { \
uint32_t pc; \
asm("auipc %0, 0" : "=r"(pc)); \
logkf(LOG_DEBUG, "ELOC=%{d}, ECAUSE=%{d}, PC=0x%{size;x}", location_value, cause_value, pc); \
} \
(ec)->location = location_value; \
(ec)->cause = cause_value; \
} \
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions common/compiler.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

# SPDX-License-Identifier: MIT

cmake_minimum_required(VERSION 3.10.0)

# The target platform
if(NOT DEFINED ENV{BADGEROS_PORT})
set(BADGEROS_PORT esp32c6)
else()
set(BADGEROS_PORT $ENV{BADGEROS_PORT})
endif()

# Set the C compiler
if(DEFINED CMAKE_C_COMPILER)
message("Using compiler '${CMAKE_C_COMPILER}' from environment")
elseif(DEFINED ENV{CMAKE_C_COMPILER})
set(CMAKE_C_COMPILER $ENV{CMAKE_C_COMPILER})
message("Using compiler '${CMAKE_C_COMPILER}' from environment")
else()
find_program(CMAKE_C_COMPILER NAMES riscv32-badgeros-linux-gnu-gcc riscv32-unknown-linux-gnu-gcc riscv32-linux-gnu-gcc riscv64-unknown-linux-gnu-gcc riscv64-linux-gnu-gcc REQUIRED)
message("Detected RISC-V C compiler as '${CMAKE_C_COMPILER}'")
endif()

# Determine the compiler prefix
get_filename_component(compiler_name "${CMAKE_C_COMPILER}" NAME)
string(REGEX MATCH "^([A-Za-z0-9_]+\-)*" BADGER_COMPILER_PREFIX "${compiler_name}")
find_program(BADGER_OBJCOPY NAMES "${BADGER_COMPILER_PREFIX}objcopy" REQUIRED)
find_program(BADGER_OBJDUMP NAMES "${BADGER_COMPILER_PREFIX}objdump" REQUIRED)
File renamed without changes.
16 changes: 10 additions & 6 deletions kernel/include/hal/gpio.h → common/include/hal/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@ typedef enum {

// Returns the amount of GPIO pins present.
// Cannot produce an error.
#define io_count() (31)
int io_count() CONST;
// Sets the mode of GPIO pin `pin` to `mode`.
void io_mode(badge_err_t *ec, int pin, io_mode_t mode);
void io_mode(badge_err_t *ec, int pin, io_mode_t mode);
// Get the mode of GPIO pin `pin`.
io_mode_t io_getmode(badge_err_t *ec, int pin);
// Sets the pull resistor behaviour of GPIO pin `pin` to `dir`.
void io_pull(badge_err_t *ec, int pin, io_pull_t dir);
void io_pull(badge_err_t *ec, int pin, io_pull_t dir);
// Get the pull resistor behaviour of GPIO pin `pin`.
io_pull_t io_getpull(badge_err_t *ec, int pin);
// Writes level to GPIO pin pin.
void io_write(badge_err_t *ec, int pin, bool level);
void io_write(badge_err_t *ec, int pin, bool level);
// Reads logic level value from GPIO pin `pin`.
// Returns false on error.
bool io_read(badge_err_t *ec, int pin);
bool io_read(badge_err_t *ec, int pin);
// Determine whether GPIO `pin` is claimed by a peripheral.
// Returns false on error.
bool io_is_peripheral(badge_err_t *ec, int pin);
bool io_is_peripheral(badge_err_t *ec, int pin);
7 changes: 6 additions & 1 deletion kernel/include/hal/i2c.h → common/include/hal/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
#include <stdint.h>


// Determines whether an I²C address is valid.
static inline bool i2c_is_valid_addr(int slave_id) {
return slave_id >= 8 && (slave_id & 0xf0) != 0xf0 && slave_id < 1024;
}

// Returns the amount of I²C peripherals present.
// Cannot produce an error.
#define i2c_count() (1)
int i2c_count() CONST;

/*
// Initialises I²C peripheral i2c_num in slave mode with SDA pin sda_pin, SCL pin scl_pin and a write buffer size of at
Expand Down
13 changes: 8 additions & 5 deletions kernel/include/hal/spi.h → common/include/hal/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

// Returns the amount of SPI peripherals present.
// Cannot produce an error.
#define spi_count() (1)
int spi_count() CONST;

// Initialises SPI peripheral spi_num in controller mode with SCLK pin `sclk_pin`, MOSI pin `mosi_pin`, MISO pin `miso_pin`, SS pin `ss_pin` and clock speed/bitrate
// bitrate. The modes of the SCLK, MOSI, MISO and SS pins are changed automatically. This
// function may be used again to change the settings on an initialised SPI peripheral in controller mode.
void spi_controller_init(badge_err_t *ec, int spi_num, int sclk_pin, int mosi_pin, int miso_pin, int ss_pin, int32_t bitrate);
// Initialises SPI peripheral spi_num in controller mode with SCLK pin `sclk_pin`, MOSI pin `mosi_pin`, MISO pin
// `miso_pin`, SS pin `ss_pin` and clock speed/bitrate bitrate. The modes of the SCLK, MOSI, MISO and SS pins are
// changed automatically. This function may be used again to change the settings on an initialised SPI peripheral in
// controller mode.
void spi_controller_init(
badge_err_t *ec, int spi_num, int sclk_pin, int mosi_pin, int miso_pin, int ss_pin, int32_t bitrate
);
// De-initialises SPI peripheral.
void spi_deinit(badge_err_t *ec, int spi_num);
// Reads len bytes into buffer buf from SPI device.
Expand Down
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions common/syscall.h → common/include/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,27 @@

#else

#include "hal/gpio.h"

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#ifdef BADGEROS_KERNEL
#include "filesystem.h"
#include "scheduler/scheduler.h"

#define SYSCALL_FLAG_EXISTS 0x01
#define SYSCALL_FLAG_FAST 0x02

typedef struct {
void (*funcptr)();
uint8_t retwidth;
uint8_t flags;
} syscall_info_t;

syscall_info_t syscall_info(int no);

#else
typedef int file_t;
typedef int tid_t;
Expand Down
Loading

0 comments on commit c688ab4

Please sign in to comment.