Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions engine/HAL/LINUX/luos_hal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/******************************************************************************
* @file luosHAL
* @brief Luos Hardware Abstration Layer. Describe Low layer fonction
* @Family Linux (Raspberry Pi)
* @author Luos
* @version 0.0.0
******************************************************************************/
#include "luos_hal.h"

#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#include <sys/stat.h>
#include <libgen.h>

pthread_mutex_t luos_recursive_mutex;

/*******************************************************************************
* Function
******************************************************************************/
static void LuosHAL_SystickInit(void);
static void LuosHAL_FlashInit(void);

/******************************************************************************
* @brief Luos HAL general initialisation
* @param None
* @return None
******************************************************************************/
void LuosHAL_Init(void)
{
// Initialize the recursive mutex FIRST, before anything else.
// MsgAlloc_Init() and Phy_Init() call Phy_SetIrqState() during
// initialization, so the mutex must be ready before they run.
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&luos_recursive_mutex, &attr);
pthread_mutexattr_destroy(&attr);

LuosHAL_SystickInit();
LuosHAL_FlashInit();
LuosHAL_StartTimestamp();
}

/******************************************************************************
* @brief Luos HAL IRQ state control via recursive mutex
* @param Enable: false=lock, true=unlock
* @return None
******************************************************************************/
void LuosHAL_SetIrqState(bool Enable)
{
if (Enable == false)
{
pthread_mutex_lock(&luos_recursive_mutex);
}
else
{
pthread_mutex_unlock(&luos_recursive_mutex);
}
}

/******************************************************************************
* @brief Luos HAL general systick tick at 1ms initialize
* @param None
* @return None
******************************************************************************/
static void LuosHAL_SystickInit(void)
{
}

/******************************************************************************
* @brief Luos HAL general systick tick at 1ms
* @param None
* @return tick Counter
******************************************************************************/
uint32_t LuosHAL_GetSystick(void)
{
struct timespec time;
uint32_t ms;
time_t s;
clock_gettime(CLOCK_MONOTONIC, &time);
s = time.tv_sec;
ms = round(time.tv_nsec / 1.0e6);
if (ms > 999)
{
s++;
ms = 0;
}
ms += s * 1000;
return ms;
}

/******************************************************************************
* @brief Luos GetTimestamp
* @param None
* @return uint64_t
******************************************************************************/
uint64_t LuosHAL_GetTimestamp(void)
{
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
uint64_t timestamp = time.tv_nsec + time.tv_sec * 1000000000;
return timestamp;
}

/******************************************************************************
* @brief Luos start Timestamp
* @param None
* @return None
******************************************************************************/
void LuosHAL_StartTimestamp(void)
{
}

/******************************************************************************
* @brief Luos stop Timestamp
* @param None
* @return None
******************************************************************************/
void LuosHAL_StopTimestamp(void)
{
}

/******************************************************************************
* @brief Flash Initialisation
* @param None
* @return None
******************************************************************************/
static void LuosHAL_FlashInit(void)
{
for (uint16_t i = 0; i < FLASH_PAGE_NUMBER; i++)
{
for (uint16_t j = 0; j < FLASH_PAGE_SIZE; j++)
{
stub_flash_x86[i][j] = 0;
}
}
}

/******************************************************************************
* @brief Write flash page where Luos keep permanente information
* @param Address page / size to write / pointer to data to write
* @return
******************************************************************************/
void LuosHAL_FlashWriteLuosMemoryInfo(uint32_t addr, uint16_t size, uint8_t *data)
{
}

/******************************************************************************
* @brief read information from page where Luos keep permanente information
* @param Address info / size to read / pointer callback data to read
* @return
******************************************************************************/
void LuosHAL_FlashReadLuosMemoryInfo(uint32_t addr, uint16_t size, uint8_t *data)
{
memset(data, 0xFF, size);
}

/******************************************************************************
* @brief Set boot mode -- no-op on Linux (no bootloader)
* @param mode
* @return
******************************************************************************/
void LuosHAL_SetMode(uint8_t mode)
{
}

/******************************************************************************
* @brief Save node ID to persistent file
* @param node_id
* @return
******************************************************************************/
void LuosHAL_SaveNodeID(uint16_t node_id)
{
// Ensure the parent directory exists
char path_copy[256];
strncpy(path_copy, LUOS_PERSIST_PATH, sizeof(path_copy) - 1);
path_copy[sizeof(path_copy) - 1] = '\0';
mkdir(dirname(path_copy), 0755);

FILE *f = fopen(LUOS_PERSIST_PATH, "wb");
if (f)
{
fwrite(&node_id, sizeof(uint16_t), 1, f);
fclose(f);
}
}

/******************************************************************************
* @brief software reboot -- no-op on Linux
* @param
* @return
******************************************************************************/
void LuosHAL_Reboot(void)
{
}
55 changes: 55 additions & 0 deletions engine/HAL/LINUX/luos_hal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/******************************************************************************
* @file luosHAL
* @brief Luos Hardware Abstration Layer. Describe Low layer fonction
* @Family Linux (Raspberry Pi)
* @author Luos
* @version 0.0.0
******************************************************************************/
#ifndef _LUOSHAL_H_
#define _LUOSHAL_H_

#include <stdint.h>
#include <stdbool.h>
#include "luos_hal_config.h"

/*******************************************************************************
* Definitions
******************************************************************************/
#ifndef _CRITICAL
#define _CRITICAL
#endif

#define LUOS_UUID ((uint32_t *)0x00000001)

#define ADDRESS_ALIASES_FLASH ADDRESS_LAST_PAGE_FLASH
#define ADDRESS_BOOT_FLAG_FLASH (ADDRESS_LAST_PAGE_FLASH + PAGE_SIZE) - 4

/*******************************************************************************
* Function
******************************************************************************/
void LuosHAL_Init(void);
void LuosHAL_SetIrqState(bool Enable);
uint32_t LuosHAL_GetSystick(void);
void LuosHAL_FlashWriteLuosMemoryInfo(uint32_t addr, uint16_t size, uint8_t *data);
void LuosHAL_FlashReadLuosMemoryInfo(uint32_t addr, uint16_t size, uint8_t *data);

// bootloader functions
void LuosHAL_SetMode(uint8_t mode);
void LuosHAL_Reboot(void);
void LuosHAL_SaveNodeID(uint16_t);

#ifdef BOOTLOADER_CONFIG
void LuosHAL_DeInit(void);
void LuosHAL_JumpToApp(uint32_t);
uint8_t LuosHAL_GetMode(void);
uint16_t LuosHAL_GetNodeID(void);
void LuosHAL_EraseMemory(uint32_t, uint16_t);
void LuosHAL_ProgramFlash(uint32_t, uint16_t, uint8_t *);
#endif

// timestamp functions
uint64_t LuosHAL_GetTimestamp(void);
void LuosHAL_StartTimestamp(void);
void LuosHAL_StopTimestamp(void);

#endif /* _LUOSHAL_H_ */
69 changes: 69 additions & 0 deletions engine/HAL/LINUX/luos_hal_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/******************************************************************************
* @file luosHAL_Config
* @brief This file allow you to configure LuosHAL according to your design
* this is the default configuration created by Luos team for this MCU Family
* Do not modify this file if you want to ovewrite change define in you project
* @Family Linux (Raspberry Pi)
* @author Luos
* @version 0.0.0
******************************************************************************/
#ifndef _LUOSHAL_CONFIG_H_
#define _LUOSHAL_CONFIG_H_

#include <pthread.h>

#ifndef MCUFREQ
#define MCUFREQ 1500000000
#endif

/*******************************************************************************
* DEFINE STUB FLASH FOR LINUX
******************************************************************************/
#ifndef FLASH_PAGE_SIZE
#define FLASH_PAGE_SIZE 0x100
#endif
#ifndef FLASH_PAGE_NUMBER
#define FLASH_PAGE_NUMBER 8
#endif
static uint32_t stub_flash_x86[FLASH_PAGE_NUMBER][FLASH_PAGE_SIZE];
static uint32_t *last_page_stub_flash_x86 = &stub_flash_x86[FLASH_PAGE_NUMBER - 1][FLASH_PAGE_SIZE];

/*******************************************************************************
* SINGLE RECURSIVE MUTEX FOR ALL CRITICAL SECTIONS
* Consolidates IRQ state, MSGALLOC_MUTEX, and LUOS_MUTEX into one
* recursive mutex to prevent lock-ordering deadlocks.
******************************************************************************/
extern pthread_mutex_t luos_recursive_mutex;

#ifndef MSGALLOC_MUTEX_LOCK
#define MSGALLOC_MUTEX_LOCK pthread_mutex_lock(&luos_recursive_mutex);
#endif
#ifndef MSGALLOC_MUTEX_UNLOCK
#define MSGALLOC_MUTEX_UNLOCK pthread_mutex_unlock(&luos_recursive_mutex);
#endif

#ifndef LUOS_MUTEX_LOCK
#define LUOS_MUTEX_LOCK pthread_mutex_lock(&luos_recursive_mutex);
#endif
#ifndef LUOS_MUTEX_UNLOCK
#define LUOS_MUTEX_UNLOCK pthread_mutex_unlock(&luos_recursive_mutex);
#endif

/*******************************************************************************
* FLASH CONFIG
******************************************************************************/
#ifndef PAGE_SIZE
#define PAGE_SIZE (uint32_t) FLASH_PAGE_SIZE
#endif
#ifndef ADDRESS_LAST_PAGE_FLASH
#define ADDRESS_LAST_PAGE_FLASH (uint32_t) last_page_stub_flash_x86
#endif

/*******************************************************************************
* NODE PERSISTENCE
******************************************************************************/
#ifndef LUOS_PERSIST_PATH
#define LUOS_PERSIST_PATH "/var/lib/luos/node_config"
#endif

#endif /* _LUOSHAL_CONFIG_H_ */
2 changes: 2 additions & 0 deletions engine/IO/inc/luos_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void LuosIO_Loop(void);
int LuosIO_TopologyDetection(service_t *service, connection_t *connection_table);
error_return_t LuosIO_Send(service_t *service, msg_t *msg);

uint8_t LuosIO_GetDetectAckMode(void);

// Job management
error_return_t LuosIO_GetNextJob(phy_job_t **job);
void LuosIO_RmJob(phy_job_t *job);
Expand Down
Loading
Loading