ICARUS OS (Intelligent Cooperative Architecture for Real-time Unified Systems) 0.1.0
Preemptive Real-Time Operating System for ARM Cortex-M7
Loading...
Searching...
No Matches
retarget_hal.c
Go to the documentation of this file.
1
37#include "bsp/retarget_hal.h"
38#include "bsp/error.h"
39
40/* ============================================================================
41 * FORWARD DECLARATIONS
42 * ========================================================================= */
43
44static void SystemClock_Config(void);
45extern uint32_t task_active_sleep(uint32_t ticks);
46
47/* Function prototypes */
48int32_t platform_write(void* handle, uint8_t reg, const uint8_t *bufp, uint16_t len);
49int32_t platform_read(void* handle, uint8_t reg, uint8_t *bufp, uint16_t len);
50
51
66static void CPU_CACHE_Enable(void)
67{
68 /* Enable I-Cache */
69 SCB_EnableICache();
70
71 /* Enable D-Cache */
72 SCB_EnableDCache();
73}
74
75/* ============================================================================
76 * LED CONTROL FUNCTIONS
77 * ========================================================================= */
78
93void LED_Blink(uint32_t Hdelay, uint32_t Ldelay) {
94 HAL_GPIO_WritePin(E3_GPIO_Port,E3_Pin,GPIO_PIN_SET);
95 (void)task_active_sleep(Hdelay - 1u);
96 HAL_GPIO_WritePin(E3_GPIO_Port,E3_Pin,GPIO_PIN_RESET);
97 (void)task_active_sleep(Ldelay - 1u);
98}
99
107void LED_On(void) {
108 HAL_GPIO_WritePin(E3_GPIO_Port,E3_Pin,GPIO_PIN_SET);
109}
110
118void LED_Off(void) {
119 HAL_GPIO_WritePin(E3_GPIO_Port,E3_Pin,GPIO_PIN_RESET);
120}
121
122/* ============================================================================
123 * I2C PLATFORM FUNCTIONS (IMU DRIVER)
124 * ========================================================================= */
125
141int32_t platform_write(void* handle, uint8_t reg, const uint8_t *bufp, uint16_t len) {
142 /* HAL_I2C_Mem_Write requires non-const pointer, but we receive const
143 * This is safe as HAL will only read from the buffer */
144#pragma GCC diagnostic push
145#pragma GCC diagnostic ignored "-Wcast-qual"
146 HAL_I2C_Mem_Write((I2C_HandleTypeDef*) handle, IMU_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT,
147 (uint8_t *)bufp, len, HAL_MAX_DELAY);
148#pragma GCC diagnostic pop
149 return 0;
150}
151
167int32_t platform_read(void* handle, uint8_t reg, uint8_t *bufp, uint16_t len) {
168 HAL_I2C_Mem_Read((I2C_HandleTypeDef*) handle, IMU_ADDRESS, reg, I2C_MEMADD_SIZE_8BIT,
169 bufp, len, HAL_MAX_DELAY);
170 return 0;
171}
172
173/* ============================================================================
174 * HAL INITIALIZATION
175 * ========================================================================= */
176
201void hal_init(void) {
202 /* USER CODE BEGIN 1 */
203 #ifdef W25Qxx
204 SCB->VTOR = QSPI_BASE;
205 #else
206 SCB->VTOR = BSP_FLASH_BASE;
207 #endif
208
209 MPU_Config();
211
212#ifndef HOST_TEST
213 /* Enable individual fault handlers (otherwise escalate to HardFault) */
214 SCB->SHCSR |= (1UL << 18) | /* USGFAULTENA */
215 (1UL << 17) | /* BUSFAULTENA */
216 (1UL << 16); /* MEMFAULTENA */
217#endif
218
219 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
220 HAL_Init();
221
222 /* Configure the system clock */
224
225#ifndef HOST_TEST
226 /* Enable backup SRAM (RAM_D3, 0x38000000, 64KB).
227 * Three steps required per RM0433:
228 * 1. Unlock backup domain writes (DBP bit in PWR_CR1)
229 * 2. Enable BKPRAM clock on AHB4
230 * 3. Enable VBAT backup regulator for retention across VDD loss */
231 HAL_PWR_EnableBkUpAccess();
232 __HAL_RCC_BKPRAM_CLK_ENABLE();
233 HAL_PWREx_EnableBkUpReg();
234#endif
235
236 /* Initialize all configured peripherals */
237 MX_GPIO_Init();
238
239 // Early LED blink to confirm MCU is alive
240 // LED_Blink(200, 200);
241 // LED_Blink(200, 200);
242 // LED_Blink(200, 200);
243
244 MX_RTC_Init();
245 MX_SPI4_Init();
246 MX_TIM1_Init();
247 MX_I2C2_Init();
249
250// HAL_Delay(1000); // Give USB time to initialize
251
252// printf("TEST\n");
253// printf("USB initialized, starting debug output\n");
254//
255// extern volatile uint8_t os_running;
256// printf("OS Running: %d\r\n", os_running);
257// printf("Starting ICARUS OS\r\n");
258}
259
260/* ============================================================================
261 * SYSTEM CLOCK CONFIGURATION
262 * ========================================================================= */
263
293{
294 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
295 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
296
299 HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
300
303 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
304
305 while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
306
314 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_HSE;
315 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
316 RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
317 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
318 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
319 RCC_OscInitStruct.PLL.PLLM = 5;
320 RCC_OscInitStruct.PLL.PLLN = 96;
321 RCC_OscInitStruct.PLL.PLLP = 2;
322 RCC_OscInitStruct.PLL.PLLQ = 2;
323 RCC_OscInitStruct.PLL.PLLR = 2;
324 RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
325 RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
326 RCC_OscInitStruct.PLL.PLLFRACN = 0;
327 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
328 {
330 }
331
334 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
335 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
336 |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
337 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
338 RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
339 RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
340 RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
341 RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
342 RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
343 RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
344
345 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
346 {
348 }
349}
#define E3_Pin
Definition config.h:134
#define E3_GPIO_Port
Definition config.h:135
ICARUS OS Error Handling Interface.
void Error_Handler(void)
System error handler for unrecoverable faults.
Definition error.c:50
void MX_GPIO_Init(void)
Configure pins PC14-OSC32_IN (OSC32_IN) ---—> RCC_OSC32_IN PC15-OSC32_OUT (OSC32_OUT) ---—> RCC_OSC32...
Definition gpio.c:43
#define BSP_FLASH_BASE
Internal Flash base address (128KB)
Definition mpu.h:50
void MX_I2C2_Init(void)
Definition i2c.c:31
void MPU_Config(void)
Configure Memory Protection Unit regions.
Definition mpu.c:64
void LED_Blink(uint32_t Hdelay, uint32_t Ldelay)
Blink LED with specified on/off timing.
static void SystemClock_Config(void)
Configure system clocks for 480MHz operation.
void LED_On(void)
Turn LED on (active high)
int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp, uint16_t len)
Platform write function for LSM9DS1 IMU driver.
uint32_t task_active_sleep(uint32_t ticks)
Sleep for specified ticks (cooperative, via SVC)
Definition svc.c:522
static void CPU_CACHE_Enable(void)
Enable CPU instruction and data caches.
int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len)
Platform read function for LSM9DS1 IMU driver.
void hal_init(void)
Initialize all hardware abstraction layer components.
void LED_Off(void)
Turn LED off.
ICARUS OS Hardware Abstraction Layer Interface.
#define IMU_ADDRESS
I2C address for LSM9DS1 IMU (accelerometer/gyroscope)
void MX_RTC_Init(void)
Definition rtc.c:31
void MX_SPI4_Init(void)
Definition spi.c:31
void MX_TIM1_Init(void)
Initialize TIM1 for PWM output.
Definition timer.c:31
void MX_USB_DEVICE_Init(void)
Initialize USB Device.