File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed
Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -104,6 +104,19 @@ static serial_t *tx_callback_obj[UART_NUM];
104104
105105static 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
Original file line number Diff line number Diff line change @@ -53,6 +53,11 @@ extern "C" {
5353typedef struct serial_s serial_t ;
5454
5555struct 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 ;
You can’t perform that action at this time.
0 commit comments