Skip to content

Commit bbdc20c

Browse files
committed
fix: correct eos_hal_set_msp() -- add Cortex-M implementation, remove duplicates; add extern C guards
1 parent 398c9bb commit bbdc20c

1 file changed

Lines changed: 177 additions & 178 deletions

File tree

include/eos_hal.h

Lines changed: 177 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,177 @@
1-
/**
2-
* @file eos_hal.h
3-
* @brief Hardware Abstraction Layer for eBootloader
4-
*
5-
* Board-agnostic interface for flash, watchdog, UART, reset,
6-
* and jump operations. Each target board provides its own
7-
* implementation of eos_board_ops_t.
8-
*/
9-
10-
#ifndef EOS_HAL_H
11-
#define EOS_HAL_H
12-
13-
#include "eos_types.h"
14-
15-
/* ---------------- Board Operations ---------------- */
16-
17-
typedef struct {
18-
/* Memory map */
19-
uint32_t flash_base;
20-
uint32_t flash_size;
21-
uint32_t slot_a_addr;
22-
uint32_t slot_a_size;
23-
uint32_t slot_b_addr;
24-
uint32_t slot_b_size;
25-
uint32_t recovery_addr;
26-
uint32_t recovery_size;
27-
uint32_t bootctl_addr;
28-
uint32_t bootctl_backup_addr;
29-
uint32_t log_addr;
30-
uint32_t app_vector_offset;
31-
32-
/* Flash operations */
33-
int (*flash_read)(uint32_t addr, void *buf, size_t len);
34-
int (*flash_write)(uint32_t addr, const void *buf, size_t len);
35-
int (*flash_erase)(uint32_t addr, size_t len);
36-
37-
/* Watchdog */
38-
void (*watchdog_init)(uint32_t timeout_ms);
39-
void (*watchdog_feed)(void);
40-
41-
/* Reset */
42-
eos_reset_reason_t (*get_reset_reason)(void);
43-
void (*system_reset)(void);
44-
45-
/* Recovery pin */
46-
bool (*recovery_pin_asserted)(void);
47-
48-
/* Jump / handoff */
49-
void (*jump)(uint32_t vector_addr);
50-
51-
/* UART (for recovery transport) */
52-
int (*uart_init)(uint32_t baud);
53-
int (*uart_send)(const void *buf, size_t len);
54-
int (*uart_recv)(void *buf, size_t len, uint32_t timeout_ms);
55-
56-
/* Timing */
57-
uint32_t (*get_tick_ms)(void);
58-
59-
/* Interrupt control */
60-
void (*disable_interrupts)(void);
61-
void (*enable_interrupts)(void);
62-
void (*deinit_peripherals)(void);
63-
} eos_board_ops_t;
64-
65-
/* ---------------- Global Board Handle ---------------- */
66-
67-
/**
68-
* @brief Register board operations.
69-
* Must be called before any HAL function is used.
70-
* @param ops Pointer to board operations (must remain valid).
71-
*/
72-
void eos_hal_init(const eos_board_ops_t *ops);
73-
74-
/**
75-
* @brief Get the registered board operations.
76-
* @return Pointer to the active board ops, or NULL if not initialized.
77-
*/
78-
const eos_board_ops_t *eos_hal_get_ops(void);
79-
80-
/* ---------------- HAL Convenience Functions ---------------- */
81-
82-
int eos_hal_flash_read(uint32_t addr, void *buf, size_t len);
83-
int eos_hal_flash_write(uint32_t addr, const void *buf, size_t len);
84-
int eos_hal_flash_erase(uint32_t addr, size_t len);
85-
86-
void eos_hal_watchdog_init(uint32_t timeout_ms);
87-
void eos_hal_watchdog_feed(void);
88-
89-
eos_reset_reason_t eos_hal_get_reset_reason(void);
90-
void eos_hal_system_reset(void);
91-
92-
bool eos_hal_recovery_pin_asserted(void);
93-
94-
void eos_hal_jump(uint32_t vector_addr);
95-
96-
int eos_hal_uart_init(uint32_t baud);
97-
int eos_hal_uart_send(const void *buf, size_t len);
98-
int eos_hal_uart_recv(void *buf, size_t len, uint32_t timeout_ms);
99-
100-
uint32_t eos_hal_get_tick_ms(void);
101-
102-
void eos_hal_disable_interrupts(void);
103-
void eos_hal_enable_interrupts(void);
104-
void eos_hal_deinit_peripherals(void);
105-
106-
/**
107-
* @brief Get the flash address for a given slot.
108-
* @param slot Slot identifier.
109-
* @return Flash address of the slot, or 0 if invalid.
110-
*/
111-
uint32_t eos_hal_slot_addr(eos_slot_t slot);
112-
113-
/**
114-
* @brief Get the size of a given slot.
115-
* @param slot Slot identifier.
116-
* @return Size in bytes, or 0 if invalid.
117-
*/
118-
uint32_t eos_hal_slot_size(eos_slot_t slot);
119-
120-
/* ---------------- Convenience Aliases ---------------- */
121-
122-
#define eos_hal_uart_write(port, data, len) eos_hal_uart_send(data, len)
123-
#define eos_hal_uart_read(port, data, len, timeout) eos_hal_uart_recv(data, len, timeout)
124-
#define eos_hal_disable_irqs() eos_hal_disable_interrupts()
125-
#define eos_hal_enable_irqs() eos_hal_enable_interrupts()
126-
127-
static inline void eos_hal_set_msp(uint32_t addr) {
128-
#if defined(__ARM_ARCH_PROFILE) && (__ARM_ARCH_PROFILE == 'M')
129-
/* Cortex-M: MSP accessible via MSR */
130-
/* Cortex-M: MSP is accessible via MSR instruction */
131-
#elif defined(__ARM_ARCH_PROFILE) && (__ARM_ARCH_PROFILE == 'R')
132-
/* Cortex-R: no MSP register, set SP directly */
133-
__asm volatile ("MOV SP, %0" : : "r" (addr));
134-
#elif defined(__ARM_ARCH) && (__ARM_ARCH <= 7) && !defined(__aarch64__)
135-
/* Generic ARM fallback (assume Cortex-M if profile unknown) */
136-
__asm volatile ("MSR MSP, %0" : : "r" (addr));
137-
__asm volatile ("MSR MSP, %0" : : "r" (addr));
138-
#elif defined(__ARM_ARCH_PROFILE) && (__ARM_ARCH_PROFILE == 'R')
139-
/* Cortex-R: set SP directly (no MSP register) */
140-
__asm volatile ("MOV SP, %0" : : "r" (addr));
141-
#elif defined(__ARM_ARCH) && (__ARM_ARCH <= 7) && !defined(__aarch64__)
142-
/* Generic ARM fallback: try MSR MSP (Cortex-M assumed) */
143-
__asm volatile ("MSR MSP, %0" : : "r" (addr));
144-
#else
145-
(void)addr;
146-
#endif
147-
}
148-
149-
/* ---------------- Platform Identifiers ---------------- */
150-
151-
typedef enum {
152-
EOS_PLATFORM_ARM_CM0 = 0,
153-
EOS_PLATFORM_ARM_CM3 = 1,
154-
EOS_PLATFORM_ARM_CM4 = 2,
155-
EOS_PLATFORM_ARM_CM7 = 3,
156-
EOS_PLATFORM_ARM_CM33 = 4,
157-
EOS_PLATFORM_ARM_CA53 = 5,
158-
EOS_PLATFORM_ARM_CA72 = 6,
159-
EOS_PLATFORM_ARM_R5F = 7,
160-
EOS_PLATFORM_RISCV32 = 8,
161-
EOS_PLATFORM_RISCV64 = 9,
162-
EOS_PLATFORM_X86 = 10,
163-
EOS_PLATFORM_X86_64 = 11,
164-
EOS_PLATFORM_XTENSA = 12,
165-
EOS_PLATFORM_POWERPC = 13,
166-
EOS_PLATFORM_SPARC = 14,
167-
EOS_PLATFORM_M68K = 15,
168-
EOS_PLATFORM_SH4 = 16,
169-
EOS_PLATFORM_MN103 = 17,
170-
EOS_PLATFORM_V850 = 18,
171-
EOS_PLATFORM_FRV = 19,
172-
EOS_PLATFORM_H8300 = 20,
173-
EOS_PLATFORM_STRONGARM = 21,
174-
EOS_PLATFORM_XSCALE = 22,
175-
EOS_PLATFORM_MIPS = 23,
176-
} eos_platform_t;
177-
178-
#endif /* EOS_HAL_H */
1+
/**
2+
* @file eos_hal.h
3+
* @brief Hardware Abstraction Layer for eBootloader
4+
*
5+
* Board-agnostic interface for flash, watchdog, UART, reset,
6+
* and jump operations. Each target board provides its own
7+
* implementation of eos_board_ops_t.
8+
*/
9+
10+
#ifndef EOS_HAL_H
11+
#define EOS_HAL_H
12+
13+
#include "eos_types.h"
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
/* ---------------- Board Operations ---------------- */
20+
21+
typedef struct {
22+
/* Memory map */
23+
uint32_t flash_base;
24+
uint32_t flash_size;
25+
uint32_t slot_a_addr;
26+
uint32_t slot_a_size;
27+
uint32_t slot_b_addr;
28+
uint32_t slot_b_size;
29+
uint32_t recovery_addr;
30+
uint32_t recovery_size;
31+
uint32_t bootctl_addr;
32+
uint32_t bootctl_backup_addr;
33+
uint32_t log_addr;
34+
uint32_t app_vector_offset;
35+
36+
/* Flash operations */
37+
int (*flash_read)(uint32_t addr, void *buf, size_t len);
38+
int (*flash_write)(uint32_t addr, const void *buf, size_t len);
39+
int (*flash_erase)(uint32_t addr, size_t len);
40+
41+
/* Watchdog */
42+
void (*watchdog_init)(uint32_t timeout_ms);
43+
void (*watchdog_feed)(void);
44+
45+
/* Reset */
46+
eos_reset_reason_t (*get_reset_reason)(void);
47+
void (*system_reset)(void);
48+
49+
/* Recovery pin */
50+
bool (*recovery_pin_asserted)(void);
51+
52+
/* Jump / handoff */
53+
void (*jump)(uint32_t vector_addr);
54+
55+
/* UART (for recovery transport) */
56+
int (*uart_init)(uint32_t baud);
57+
int (*uart_send)(const void *buf, size_t len);
58+
int (*uart_recv)(void *buf, size_t len, uint32_t timeout_ms);
59+
60+
/* Timing */
61+
uint32_t (*get_tick_ms)(void);
62+
63+
/* Interrupt control */
64+
void (*disable_interrupts)(void);
65+
void (*enable_interrupts)(void);
66+
void (*deinit_peripherals)(void);
67+
} eos_board_ops_t;
68+
69+
/* ---------------- Global Board Handle ---------------- */
70+
71+
/**
72+
* @brief Register board operations.
73+
* Must be called before any HAL function is used.
74+
* @param ops Pointer to board operations (must remain valid).
75+
*/
76+
void eos_hal_init(const eos_board_ops_t *ops);
77+
78+
/**
79+
* @brief Get the registered board operations.
80+
* @return Pointer to the active board ops, or NULL if not initialized.
81+
*/
82+
const eos_board_ops_t *eos_hal_get_ops(void);
83+
84+
/* ---------------- HAL Convenience Functions ---------------- */
85+
86+
int eos_hal_flash_read(uint32_t addr, void *buf, size_t len);
87+
int eos_hal_flash_write(uint32_t addr, const void *buf, size_t len);
88+
int eos_hal_flash_erase(uint32_t addr, size_t len);
89+
90+
void eos_hal_watchdog_init(uint32_t timeout_ms);
91+
void eos_hal_watchdog_feed(void);
92+
93+
eos_reset_reason_t eos_hal_get_reset_reason(void);
94+
void eos_hal_system_reset(void);
95+
96+
bool eos_hal_recovery_pin_asserted(void);
97+
98+
void eos_hal_jump(uint32_t vector_addr);
99+
100+
int eos_hal_uart_init(uint32_t baud);
101+
int eos_hal_uart_send(const void *buf, size_t len);
102+
int eos_hal_uart_recv(void *buf, size_t len, uint32_t timeout_ms);
103+
104+
uint32_t eos_hal_get_tick_ms(void);
105+
106+
void eos_hal_disable_interrupts(void);
107+
void eos_hal_enable_interrupts(void);
108+
void eos_hal_deinit_peripherals(void);
109+
110+
/**
111+
* @brief Get the flash address for a given slot.
112+
* @param slot Slot identifier.
113+
* @return Flash address of the slot, or 0 if invalid.
114+
*/
115+
uint32_t eos_hal_slot_addr(eos_slot_t slot);
116+
117+
/**
118+
* @brief Get the size of a given slot.
119+
* @param slot Slot identifier.
120+
* @return Size in bytes, or 0 if invalid.
121+
*/
122+
uint32_t eos_hal_slot_size(eos_slot_t slot);
123+
124+
/* ---------------- Convenience Aliases ---------------- */
125+
126+
#define eos_hal_uart_write(port, data, len) eos_hal_uart_send(data, len)
127+
#define eos_hal_uart_read(port, data, len, timeout) eos_hal_uart_recv(data, len, timeout)
128+
#define eos_hal_disable_irqs() eos_hal_disable_interrupts()
129+
#define eos_hal_enable_irqs() eos_hal_enable_interrupts()
130+
131+
static inline void eos_hal_set_msp(uint32_t addr) {
132+
#if defined(__ARM_ARCH_PROFILE) && (__ARM_ARCH_PROFILE == 'M')
133+
__asm volatile ("MSR MSP, %0" : : "r" (addr));
134+
#elif defined(__ARM_ARCH_PROFILE) && (__ARM_ARCH_PROFILE == 'R')
135+
__asm volatile ("MOV SP, %0" : : "r" (addr));
136+
#elif defined(__aarch64__)
137+
__asm volatile ("MOV SP, %0" : : "r" ((uint64_t)addr));
138+
#elif defined(__ARM_ARCH) && (__ARM_ARCH <= 7)
139+
__asm volatile ("MOV SP, %0" : : "r" (addr));
140+
#elif defined(__riscv)
141+
__asm volatile ("mv sp, %0" : : "r" (addr));
142+
#else
143+
(void)addr;
144+
#endif
145+
}
146+
147+
typedef enum {
148+
EOS_PLATFORM_ARM_CM0 = 0,
149+
EOS_PLATFORM_ARM_CM3 = 1,
150+
EOS_PLATFORM_ARM_CM4 = 2,
151+
EOS_PLATFORM_ARM_CM7 = 3,
152+
EOS_PLATFORM_ARM_CM33 = 4,
153+
EOS_PLATFORM_ARM_CA53 = 5,
154+
EOS_PLATFORM_ARM_CA72 = 6,
155+
EOS_PLATFORM_ARM_R5F = 7,
156+
EOS_PLATFORM_RISCV32 = 8,
157+
EOS_PLATFORM_RISCV64 = 9,
158+
EOS_PLATFORM_X86 = 10,
159+
EOS_PLATFORM_X86_64 = 11,
160+
EOS_PLATFORM_XTENSA = 12,
161+
EOS_PLATFORM_POWERPC = 13,
162+
EOS_PLATFORM_SPARC = 14,
163+
EOS_PLATFORM_M68K = 15,
164+
EOS_PLATFORM_SH4 = 16,
165+
EOS_PLATFORM_MN103 = 17,
166+
EOS_PLATFORM_V850 = 18,
167+
EOS_PLATFORM_FRV = 19,
168+
EOS_PLATFORM_H8300 = 20,
169+
EOS_PLATFORM_STRONGARM = 21,
170+
EOS_PLATFORM_XSCALE = 22,
171+
EOS_PLATFORM_MIPS = 23,
172+
} eos_platform_t;
173+
174+
#ifdef __cplusplus
175+
}
176+
#endif
177+
#endif /* EOS_HAL_H */

0 commit comments

Comments
 (0)