From e620a977c0bf06d5307c25ee254e4d18f9dbad36 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 9 Sep 2023 18:00:24 +0200 Subject: [PATCH] HAL emulation --- CMakeLists.txt | 2 +- PSE-firmware | 2 +- fatfs.c | 39 ++++++++++++++++++++++++++ fatfs.h | 63 ++++++++++++++++++++++++++++++++++++++++++ main.c | 54 ++++++++++++++++++++++++++++++++++-- main.h | 43 +++++++++++++++++++++++++++++ stm32f1xx_hal.c | 22 +++++++++++++++ stm32f1xx_hal.h | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 293 insertions(+), 5 deletions(-) create mode 100644 fatfs.c create mode 100644 fatfs.h create mode 100644 stm32f1xx_hal.c create mode 100644 stm32f1xx_hal.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f373248..7625b76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ include_directories(${SDL2_INCLUDE_DIRS}) add_subdirectory(PSE-firmware/Drivers/lvgl) include_directories(PSE-firmware/Core/Inc) -add_executable(main main.c PSE-firmware/Core/Src/logo_mint_resize.c PSE-firmware/Core/Src/home_screen.c PSE-firmware/Core/Src/PSE_unit.c PSE-firmware/Core/Src/PSE_unit_edit_screen.c PSE-firmware/Core/Src/keypad_screen.c ${SOURCES} ${INCLUDES}) +add_executable(main main.c PSE-firmware/Core/Src/logo_mint_resize.c PSE-firmware/Core/Src/home_screen.c PSE-firmware/Core/Src/PSE_unit.c PSE-firmware/Core/Src/PSE_unit_edit_screen.c PSE-firmware/Core/Src/keypad_screen.c PSE-firmware/Core/Src/pse_stepper_planer.c fatfs.c stm32f1xx_hal.c ${SOURCES} ${INCLUDES}) add_compile_definitions(LV_CONF_INCLUDE_SIMPLE) target_link_libraries(main PRIVATE lvgl ${SDL2_LIBRARIES}) add_custom_target (run COMMAND ${EXECUTABLE_OUTPUT_PATH}/main) diff --git a/PSE-firmware b/PSE-firmware index 6156105..b2011af 160000 --- a/PSE-firmware +++ b/PSE-firmware @@ -1 +1 @@ -Subproject commit 6156105818b9e0f0a06380452512b349c55c63bf +Subproject commit b2011af18c834f2d29e9e1f30a2e2f34dd0a7382 diff --git a/fatfs.c b/fatfs.c new file mode 100644 index 0000000..9967001 --- /dev/null +++ b/fatfs.c @@ -0,0 +1,39 @@ +#include "fatfs.h" + +#include + +FRESULT f_open ( + FIL* fp, /* Pointer to the blank file object */ + const TCHAR* path, /* Pointer to the file name */ + BYTE mode /* Access mode and file open mode flags */ +){ + printf("file open try\n"); + return FR_NO_FILE; +} + +FRESULT f_read ( + FIL* fp, /* Pointer to the file object */ + void* buff, /* Pointer to data buffer */ + UINT btr, /* Number of bytes to read */ + UINT* br /* Pointer to number of bytes read */ +){ + printf("file read try\n"); + return FR_NO_FILE; +} + +FRESULT f_close ( + FIL *fp /* Pointer to the file object to be closed */ +){ + printf("file close try\n"); + return FR_NO_FILE; +} + +FRESULT f_write ( + FIL* fp, /* Pointer to the file object */ + const void *buff, /* Pointer to the data to be written */ + UINT btw, /* Number of bytes to write */ + UINT* bw /* Pointer to number of bytes written */ +){ + printf("file write try\n"); + return FR_NO_FILE; +} diff --git a/fatfs.h b/fatfs.h new file mode 100644 index 0000000..4113ab5 --- /dev/null +++ b/fatfs.h @@ -0,0 +1,63 @@ +typedef struct { +} FIL; + +typedef enum { + FR_OK = 0, /* (0) Succeeded */ + FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ + FR_INT_ERR, /* (2) Assertion failed */ + FR_NOT_READY, /* (3) The physical drive cannot work */ + FR_NO_FILE, /* (4) Could not find the file */ + FR_NO_PATH, /* (5) Could not find the path */ + FR_INVALID_NAME, /* (6) The path name format is invalid */ + FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ + FR_EXIST, /* (8) Access denied due to prohibited access */ + FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ + FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ + FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ + FR_NOT_ENABLED, /* (12) The volume has no work area */ + FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ + FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */ + FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ + FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ + FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ + FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_SHARE */ + FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ +} FRESULT; + +typedef char TCHAR; +typedef unsigned char BYTE; +typedef unsigned int UINT; + +#define FA_READ 0x01 +#define FA_OPEN_EXISTING 0x00 + +#define FA_WRITE 0x02 +#define FA_CREATE_NEW 0x04 +#define FA_CREATE_ALWAYS 0x08 +#define FA_OPEN_ALWAYS 0x10 +#define FA__WRITTEN 0x20 +#define FA__DIRTY 0x40 + +FRESULT f_open ( + FIL* fp, /* Pointer to the blank file object */ + const TCHAR* path, /* Pointer to the file name */ + BYTE mode /* Access mode and file open mode flags */ +); + +FRESULT f_read ( + FIL* fp, /* Pointer to the file object */ + void* buff, /* Pointer to data buffer */ + UINT btr, /* Number of bytes to read */ + UINT* br /* Pointer to number of bytes read */ +); + +FRESULT f_close ( + FIL *fp /* Pointer to the file object to be closed */ +); + +FRESULT f_write ( + FIL* fp, /* Pointer to the file object */ + const void *buff, /* Pointer to the data to be written */ + UINT btw, /* Number of bytes to write */ + UINT* bw /* Pointer to number of bytes written */ +); diff --git a/main.c b/main.c index dfc989c..a68add0 100644 --- a/main.c +++ b/main.c @@ -23,6 +23,55 @@ SDL_Window* window = NULL; SDL_Surface* screenSurface = NULL; +TIM_HandleTypeDef htim2; +TIM_HandleTypeDef htim3; +TIM_HandleTypeDef htim4; +TIM_HandleTypeDef htim5; + +pse_unit pse_units[PSE_UNITS_NUM]; +pse_syringe pse_syringes[PSE_UNITS_NUM]; +pse_home_display pse_home_displays[PSE_UNITS_NUM]; + +enum steppers_axis{PSE_X_STEPPER, PSE_Y_STEPPER, PSE_Z_STEPPER, PSE_E_STEPPER, PSE_STEPPER_NUM}; +pse_stepper_conf pse_stepper_confs[PSE_STEPPER_NUM] = { + [PSE_X_STEPPER] = { + .DIR_GPIO_Port = X_STEPPER_DIR_GPIO_Port, + .DIR_GPIO_Pin = X_STEPPER_DIR_Pin, + .EN_GPIO_Port = X_STEPPER_EN_GPIO_Port, + .EN_GPIO_Pin = X_STEPPER_EN_Pin, + .STEP_GPIO_Port = X_STEPPER_STEP_GPIO_Port, + .STEP_GPIO_Pin = X_STEPPER_STEP_Pin, + .tim = &htim2, + }, + [PSE_Y_STEPPER] = { + .DIR_GPIO_Port = Y_STEPPER_DIR_GPIO_Port, + .DIR_GPIO_Pin = Y_STEPPER_DIR_Pin, + .EN_GPIO_Port = Y_STEPPER_EN_GPIO_Port, + .EN_GPIO_Pin = Y_STEPPER_EN_Pin, + .STEP_GPIO_Port = Y_STEPPER_STEP_GPIO_Port, + .STEP_GPIO_Pin = Y_STEPPER_STEP_Pin, + .tim = &htim3, + }, + [PSE_Z_STEPPER] = { + .DIR_GPIO_Port = Z_STEPPER_DIR_GPIO_Port, + .DIR_GPIO_Pin = Z_STEPPER_DIR_Pin, + .EN_GPIO_Port = Z_STEPPER_EN_GPIO_Port, + .EN_GPIO_Pin = Z_STEPPER_EN_Pin, + .STEP_GPIO_Port = Z_STEPPER_STEP_GPIO_Port, + .STEP_GPIO_Pin = Z_STEPPER_STEP_Pin, + .tim = &htim4, + }, + [PSE_E_STEPPER] = { + .DIR_GPIO_Port = E_STEPPER_DIR_GPIO_Port, + .DIR_GPIO_Pin = E_STEPPER_DIR_Pin, + .EN_GPIO_Port = E_STEPPER_EN_GPIO_Port, + .EN_GPIO_Pin = E_STEPPER_EN_Pin, + .STEP_GPIO_Port = E_STEPPER_STEP_GPIO_Port, + .STEP_GPIO_Pin = E_STEPPER_STEP_Pin, + .tim = &htim5, + }, +}; + int main(int argc, char** argv){ if(SDL_Init( SDL_INIT_VIDEO ) < 0){ printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); @@ -70,9 +119,8 @@ int main(int argc, char** argv){ lv_label_set_text(cursor, "A"); lv_indev_set_cursor(my_indev, cursor); - pse_unit pse_units[PSE_UNITS_NUM]; - load_units((pse_unit*)&pse_units, PSE_UNITS_NUM); - Home_Screen_Gen((pse_unit*)&pse_units, PSE_UNITS_NUM); + load_units(pse_units, pse_syringes, pse_stepper_confs, pse_home_displays, PSE_UNITS_NUM, 0); + Home_Screen_Gen(pse_units, PSE_UNITS_NUM); pthread_t tickThread; pthread_create(&tickThread, NULL, tick_thread, NULL); diff --git a/main.h b/main.h index 8eeb46b..41cf9d0 100644 --- a/main.h +++ b/main.h @@ -1,7 +1,50 @@ #include "lvgl.h" +#include "stm32f1xx_hal.h" #define PSE_UNITS_NUM 4 // number of units (4 in our case, X, Y, Z and extr) + +#define E_STEPPER_DIR_Pin GPIO_PIN_2 +#define E_STEPPER_DIR_GPIO_Port GPIOE +#define E_STEPPER_STEP_Pin GPIO_PIN_3 +#define E_STEPPER_STEP_GPIO_Port GPIOE +#define E_STEPPER_EN_Pin GPIO_PIN_4 +#define E_STEPPER_EN_GPIO_Port GPIOE +#define LED_Pin GPIO_PIN_2 +#define LED_GPIO_Port GPIOC +#define LCD_RST_Pin GPIO_PIN_4 +#define LCD_RST_GPIO_Port GPIOC +#define ADS7843_CS_Pin GPIO_PIN_12 +#define ADS7843_CS_GPIO_Port GPIOB +#define ADS7843_SCK_Pin GPIO_PIN_13 +#define ADS7843_SCK_GPIO_Port GPIOB +#define ADS7843_MOSI_Pin GPIO_PIN_14 +#define ADS7843_MOSI_GPIO_Port GPIOB +#define ADS7843_MISO_Pin GPIO_PIN_15 +#define ADS7843_MISO_GPIO_Port GPIOB +#define LCD_CS_Pin GPIO_PIN_12 +#define LCD_CS_GPIO_Port GPIOD +#define ADS7843_Int_Pin GPIO_PIN_6 +#define ADS7843_Int_GPIO_Port GPIOC +#define X_STEPPER_DIR_Pin GPIO_PIN_3 +#define X_STEPPER_DIR_GPIO_Port GPIOB +#define X_STEPPER_STEP_Pin GPIO_PIN_4 +#define X_STEPPER_STEP_GPIO_Port GPIOB +#define X_STEPPER_EN_Pin GPIO_PIN_5 +#define X_STEPPER_EN_GPIO_Port GPIOB +#define Y_STEPPER_DIR_Pin GPIO_PIN_6 +#define Y_STEPPER_DIR_GPIO_Port GPIOB +#define Y_STEPPER_STEP_Pin GPIO_PIN_7 +#define Y_STEPPER_STEP_GPIO_Port GPIOB +#define Y_STEPPER_EN_Pin GPIO_PIN_8 +#define Y_STEPPER_EN_GPIO_Port GPIOB +#define Z_STEPPER_DIR_Pin GPIO_PIN_9 +#define Z_STEPPER_DIR_GPIO_Port GPIOB +#define Z_STEPPER_STEP_Pin GPIO_PIN_0 +#define Z_STEPPER_STEP_GPIO_Port GPIOE +#define Z_STEPPER_EN_Pin GPIO_PIN_1 +#define Z_STEPPER_EN_GPIO_Port GPIOE + void my_flush_cb(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * buf); void touchscreen_read_callback(lv_indev_drv_t * drv, lv_indev_data_t*data); void * tick_thread (void *args); diff --git a/stm32f1xx_hal.c b/stm32f1xx_hal.c new file mode 100644 index 0000000..829be15 --- /dev/null +++ b/stm32f1xx_hal.c @@ -0,0 +1,22 @@ +#include "stm32f1xx_hal.h" +#include + +void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin){ + printf("toggling pin\n"); +} +GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin){ + printf("reading pin, returning 0\n"); + return GPIO_PIN_RESET; +} +void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState){ + printf("writing pin\n"); +} + +HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim){ + printf("starting timer\n"); + return HAL_STATUS_OK; +} +HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim){ + printf("stopping timer\n"); + return HAL_STATUS_OK; +} diff --git a/stm32f1xx_hal.h b/stm32f1xx_hal.h new file mode 100644 index 0000000..5ebe6a0 --- /dev/null +++ b/stm32f1xx_hal.h @@ -0,0 +1,73 @@ +#pragma once + +#include +#include + +typedef struct { + uint8_t index; +} GPIO_TypeDef; + +typedef struct +{ +} TIM_HandleTypeDef; +typedef int HAL_StatusTypeDef; + +#define HAL_STATUS_OK 0 + +#define GPIO_PIN_0 ((uint16_t)0x0001) /* Pin 0 selected */ +#define GPIO_PIN_1 ((uint16_t)0x0002) /* Pin 1 selected */ +#define GPIO_PIN_2 ((uint16_t)0x0004) /* Pin 2 selected */ +#define GPIO_PIN_3 ((uint16_t)0x0008) /* Pin 3 selected */ +#define GPIO_PIN_4 ((uint16_t)0x0010) /* Pin 4 selected */ +#define GPIO_PIN_5 ((uint16_t)0x0020) /* Pin 5 selected */ +#define GPIO_PIN_6 ((uint16_t)0x0040) /* Pin 6 selected */ +#define GPIO_PIN_7 ((uint16_t)0x0080) /* Pin 7 selected */ +#define GPIO_PIN_8 ((uint16_t)0x0100) /* Pin 8 selected */ +#define GPIO_PIN_9 ((uint16_t)0x0200) /* Pin 9 selected */ +#define GPIO_PIN_10 ((uint16_t)0x0400) /* Pin 10 selected */ +#define GPIO_PIN_11 ((uint16_t)0x0800) /* Pin 11 selected */ +#define GPIO_PIN_12 ((uint16_t)0x1000) /* Pin 12 selected */ +#define GPIO_PIN_13 ((uint16_t)0x2000) /* Pin 13 selected */ +#define GPIO_PIN_14 ((uint16_t)0x4000) /* Pin 14 selected */ +#define GPIO_PIN_15 ((uint16_t)0x8000) /* Pin 15 selected */ +#define GPIO_PIN_All ((uint16_t)0xFFFF) /* All pins selected */ + +static GPIO_TypeDef GPIOA_b = (GPIO_TypeDef){ + .index = 0, +}; +static GPIO_TypeDef GPIOB_b = (GPIO_TypeDef){ + .index = 1, +}; +static GPIO_TypeDef GPIOC_b = (GPIO_TypeDef){ + .index = 2, +}; +static GPIO_TypeDef GPIOD_b = (GPIO_TypeDef){ + .index = 3, +}; +static GPIO_TypeDef GPIOE_b = (GPIO_TypeDef){ + .index = 4, +}; +#define GPIOA &GPIOA_b +#define GPIOB &GPIOB_b +#define GPIOC &GPIOC_b +#define GPIOD &GPIOD_b +#define GPIOE &GPIOE_b + +typedef enum +{ + GPIO_PIN_RESET = 0u, + GPIO_PIN_SET +} GPIO_PinState; + +void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin); +GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin); +void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState); + +#define __HAL_TIM_SET_AUTORELOAD(__HANDLE__, __AUTORELOAD__) \ + printf("setting autoreload to %d\n", __AUTORELOAD__); + +#define __HAL_TIM_SET_PRESCALER(__HANDLE__, __PRESC__) \ + printf("setting prescaler to %d\n", __PRESC__); + +HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim); +HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim);