Skip to content

Commit 5c3fb05

Browse files
committed
[UART] Add private function to get serial_t obj
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent a7a1690 commit 5c3fb05

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

cores/arduino/stm32/uart.c

+13
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ static serial_t *tx_callback_obj[UART_NUM];
104104

105105
static serial_t serial_debug = { .uart = NP, .index = UART_NUM };
106106

107+
/* Aim of the function is to get serial_s pointer using huart pointer */
108+
/* Highly inspired from magical linux kernel's "container_of" */
109+
serial_t *get_serial_obj(UART_HandleTypeDef *huart)
110+
{
111+
struct serial_s *obj_s;
112+
serial_t *obj;
113+
114+
obj_s = (struct serial_s *)((char *)huart - offsetof(struct serial_s, handle));
115+
obj = (serial_t *)((char *)obj_s - offsetof(serial_t, uart));
116+
117+
return (obj);
118+
}
119+
107120
/**
108121
* @brief Function called to initialize the uart interface
109122
* @param obj : pointer to serial_t structure

cores/arduino/stm32/uart.h

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ extern "C" {
5353
typedef struct serial_s serial_t;
5454

5555
struct serial_s {
56+
/* The 1st 2 members USART_TypeDef *uart
57+
* and UART_HandleTypeDef handle should
58+
* be kept as the first members of this struct
59+
* to have get_serial_obj() function work as expected
60+
*/
5661
USART_TypeDef *uart;
5762
UART_HandleTypeDef handle;
5863
uint32_t baudrate;

0 commit comments

Comments
 (0)