Skip to content

zenitheesc/Platform-Lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Platform Lib

The project was created to refactor and finish the lib platform, which was begun for the old members of Zenith to enable some drivers (utilizing the lib) for embedded systems with different microcontrollers.

Platform.h

Objective

Platform headers is an abstraction layer to help application drivers to be platform agnostic. That means that including the header will provide a common interface between our embedded platforms such as STM32 (HAL) or Arduino.

Scope

Each platform implementation provides access to common (internal) peripherals, such as I2C, SPI, UART and ADCs. As it's expected to be implemented in various circumstances, the scope is limited to transmitting and receiving raw bytes.

Usage

Just add the platform/ folder to the project, then #include "platform.h" in the driver. It then checks for preprocessor macros to detect which platform is being compiled. For STM32, it will also attempt to find the proper chip family HAL. If a platform is not found it is assumed to be running on a PC. This means calls will be logged to stdout with no side-effects.

Notes

If you're using an arduino (or esp-idf) implementation, put the `#include "platform/platform.h" after includes to other arduino (or esp-idf) libraries. Check issue #43.

Specification

CommonGPIOI2CSPIUARTADCPWM

Common

Types

  • error_t
 typedef int error_t;

This is a common error code type, it is recommended to be an underlying int but can also be a platform specific. The requirement is that evaluation of no error is false. So essentially, that the following pattern still applies:

 if(error){
   // error handling...
 }
 // ... code assuming no errors

This pattern was chosen to prevent Indent Hadouken

  • buffer_view_t
typedef struct {
	uint8_t *data;
	int size;
} buffer_view_t;

It's used to pass pointers to data buffers of size. It is used throughout this header to pass a bunch of bytes. This was chosen to keep things simple and prevent mixing up sizes and arrays.

  • result{8,16,float}_t
typedef struct {
	error_t hasError;
	uint8_t value;
} result8_t;

The results are a complement to the error handling strategy. Because there are no parameters in C, the platform header only creates result types for common types: uint8_t, uint16_t and float. Other result types should be declared on a driver-by-driver basis.

Functions

  • delay_ms
 void delay_ms(uint32_t time);

A blocking millisecond delay. It is recommended that RTOS use be detected via preprocessor macros, so that drivers can take advantage.

GPIO

Types

  • gpio_pin_t
typedef struct {
   <PORT> *port;
   uint16_t pin;
} gpio_pin_t;

GPIO Pin Type

Functions

  • gpio_low
void gpio_low(gpio_pin_t pin);

Reset GPIO

  • gpio_high
void gpio_high(gpio_pin_t pin);

Set GPIO

  • gpio_toggle
void gpio_toggle(gpio_pin_t pin);

Toggle GPIO

I2C

Types

  • i2c_t
 typedef <I2C_Type> i2c_dev_t;

I2C Interface Type

  • i2c_dev_t
typedef struct {
   i2c_t *i2c;
   uint8_t address;
} i2c_device_t;

I2C Device Type

Functions

  • i2c_transmit
error_t i2c_transmit(i2c_device_t device, buffer_view_t buffer);

Transmit bytes via I2C

  • i2c_receive
error_t i2c_receive(i2c_device_t device, buffer_view_t buffer);

Receive bytes via I2C

SPI

Types

  • spi_t
 typedef <SPI_Type> spi_t;

SPI Interface Type

  • spi_dev_t
typedef struct {
   spi_t *spi;
   gpio_pin_t pin;
} spi_device_t;

SPI Device Type

Functions

  • spi_transceive
error_t spi_transceive(spi_device_t device, buffer_view_t rx_buffer, 
  buffer_view_t tx_buffer);

Transceive (Transmit and Receive) via SPI

  • spi_transmit
error_t spi_transmit(spi_device_t device, buffer_view_t tx_buffer);

Transmit via SPI

  • spi_transceive
error_t spi_receive(spi_device_t device, buffer_view_t rx_buffer);

Receive via SPI

UART

Types

  • uart_t
 typedef <UART_Type> uart_t;

UART Interface Type

  • uart_connection_t
typedef struct {
	uart_t *uart;
} uart_connection_t;

UART Connection Type

Functions

  • uart_transmit
error_t uart_transmit(uart_connection_t conn, buffer_view_t buffer);

Transmit bytes via UART

  • uart_receive
error_t uart_receive(uart_connection_t conn, buffer_view_t buffer);

Receive bytes via UART

ADC

Types

  • adc_handle_t
typedef <ADC_Type> adc_handle_t;

ADC Channel Type

  • adc_t
typedef struct {
  adc_handle_t handle;
  uint8_t bits;
  float voltage_reference;
} adc_t;

ADC Type

Functions

  • adc_init
error_t adc_init(adc_t *adc);

ADC initialization

  • adc_read
result_uint16_t adc_read(adc_t *adc);

ADC Read raw value

  • adc_raw_to_voltage
float adc_raw_to_voltage(const adc_t *adc, const uint16_t value);

ADC conversion to volts.

PWM

Types

  • pwm_handle_t
typedef <PWM_Handle_Type> pwm_handle_t;

PWM Handle Type

  • pwm_channel_t
typedef <PWM_Channel_Type_> pwm_channel_t;

PWM Channel Type

  • pwm_t
typedef struct {
  pwm_handle_t handle;
  pwm_channel_t channel;
  uint8_t bits;
} pwm_t;

PWM Type

Functions

-pwm_start

error_t pwm_start(pwm_t pwm)

PWM start conection

-pwm_write

error_t pwm_write(pwm_t pwm)

PWM module signal

[email protected]

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages