@@ -14,12 +14,30 @@ struct gd32_usart_config {
1414 uint32_t rcu_periph_clock ;
1515 const struct pinctrl_dev_config * pcfg ;
1616 uint32_t parity ;
17+ #ifdef CONFIG_UART_INTERRUPT_DRIVEN
18+ uart_irq_config_func_t irq_config_func ;
19+ #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
1720};
1821
1922struct gd32_usart_data {
2023 uint32_t baud_rate ;
24+ #ifdef CONFIG_UART_INTERRUPT_DRIVEN
25+ uart_irq_callback_user_data_t user_cb ;
26+ void * user_data ;
27+ #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
2128};
2229
30+ #ifdef CONFIG_UART_INTERRUPT_DRIVEN
31+ static void usart_gd32_isr (const struct device * dev )
32+ {
33+ struct gd32_usart_data * const data = dev -> data ;
34+
35+ if (data -> user_cb ) {
36+ data -> user_cb (dev , data -> user_data );
37+ }
38+ }
39+ #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
40+
2341static int usart_gd32_init (const struct device * dev )
2442{
2543 const struct gd32_usart_config * const cfg = dev -> config ;
@@ -65,6 +83,10 @@ static int usart_gd32_init(const struct device *dev)
6583 usart_transmit_config (cfg -> reg , USART_TRANSMIT_ENABLE );
6684 usart_enable (cfg -> reg );
6785
86+ #ifdef CONFIG_UART_INTERRUPT_DRIVEN
87+ cfg -> irq_config_func (dev );
88+ #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
89+
6890 return 0 ;
6991}
7092
@@ -124,29 +146,180 @@ static int usart_gd32_err_check(const struct device *dev)
124146 return errors ;
125147}
126148
149+ #ifdef CONFIG_UART_INTERRUPT_DRIVEN
150+ int usart_gd32_fifo_fill (const struct device * dev , const uint8_t * tx_data ,
151+ int len )
152+ {
153+ const struct gd32_usart_config * const cfg = dev -> config ;
154+ uint8_t num_tx = 0U ;
155+
156+ while ((len - num_tx > 0 ) &&
157+ usart_flag_get (cfg -> reg , USART_FLAG_TBE )) {
158+ usart_data_transmit (cfg -> reg , tx_data [num_tx ++ ]);
159+ }
160+
161+ return num_tx ;
162+ }
163+
164+ int usart_gd32_fifo_read (const struct device * dev , uint8_t * rx_data ,
165+ const int size )
166+ {
167+ const struct gd32_usart_config * const cfg = dev -> config ;
168+ uint8_t num_rx = 0U ;
169+
170+ while ((size - num_rx > 0 ) &&
171+ usart_flag_get (cfg -> reg , USART_FLAG_RBNE )) {
172+ rx_data [num_rx ++ ] = usart_data_receive (cfg -> reg );
173+ }
174+
175+ return num_rx ;
176+ }
177+
178+ void usart_gd32_irq_tx_enable (const struct device * dev )
179+ {
180+ const struct gd32_usart_config * const cfg = dev -> config ;
181+
182+ usart_interrupt_enable (cfg -> reg , USART_INT_TC );
183+ }
184+
185+ void usart_gd32_irq_tx_disable (const struct device * dev )
186+ {
187+ const struct gd32_usart_config * const cfg = dev -> config ;
188+
189+ usart_interrupt_disable (cfg -> reg , USART_INT_TC );
190+ }
191+
192+ int usart_gd32_irq_tx_ready (const struct device * dev )
193+ {
194+ const struct gd32_usart_config * const cfg = dev -> config ;
195+
196+ return usart_flag_get (cfg -> reg , USART_FLAG_TBE ) &&
197+ usart_interrupt_flag_get (cfg -> reg , USART_INT_FLAG_TC );
198+ }
199+
200+ int usart_gd32_irq_tx_complete (const struct device * dev )
201+ {
202+ const struct gd32_usart_config * const cfg = dev -> config ;
203+
204+ return usart_flag_get (cfg -> reg , USART_FLAG_TC );
205+ }
206+
207+ void usart_gd32_irq_rx_enable (const struct device * dev )
208+ {
209+ const struct gd32_usart_config * const cfg = dev -> config ;
210+
211+ usart_interrupt_enable (cfg -> reg , USART_INT_RBNE );
212+ }
213+
214+ void usart_gd32_irq_rx_disable (const struct device * dev )
215+ {
216+ const struct gd32_usart_config * const cfg = dev -> config ;
217+
218+ usart_interrupt_disable (cfg -> reg , USART_INT_RBNE );
219+ }
220+
221+ int usart_gd32_irq_rx_ready (const struct device * dev )
222+ {
223+ const struct gd32_usart_config * const cfg = dev -> config ;
224+
225+ return usart_flag_get (cfg -> reg , USART_FLAG_RBNE );
226+ }
227+
228+ void usart_gd32_irq_err_enable (const struct device * dev )
229+ {
230+ const struct gd32_usart_config * const cfg = dev -> config ;
231+
232+ usart_interrupt_enable (cfg -> reg , USART_INT_ERR );
233+ usart_interrupt_enable (cfg -> reg , USART_INT_PERR );
234+ }
235+
236+ void usart_gd32_irq_err_disable (const struct device * dev )
237+ {
238+ const struct gd32_usart_config * const cfg = dev -> config ;
239+
240+ usart_interrupt_disable (cfg -> reg , USART_INT_ERR );
241+ usart_interrupt_disable (cfg -> reg , USART_INT_PERR );
242+ }
243+
244+ int usart_gd32_irq_is_pending (const struct device * dev )
245+ {
246+ const struct gd32_usart_config * const cfg = dev -> config ;
247+
248+ return ((usart_flag_get (cfg -> reg , USART_FLAG_RBNE ) &&
249+ usart_interrupt_flag_get (cfg -> reg , USART_INT_FLAG_RBNE )) ||
250+ (usart_flag_get (cfg -> reg , USART_FLAG_TC ) &&
251+ usart_interrupt_flag_get (cfg -> reg , USART_INT_FLAG_TC )));
252+ }
253+
254+ void usart_gd32_irq_callback_set (const struct device * dev ,
255+ uart_irq_callback_user_data_t cb ,
256+ void * user_data )
257+ {
258+ struct gd32_usart_data * const data = dev -> data ;
259+
260+ data -> user_cb = cb ;
261+ data -> user_data = user_data ;
262+ }
263+ #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
264+
127265static const struct uart_driver_api usart_gd32_driver_api = {
128266 .poll_in = usart_gd32_poll_in ,
129267 .poll_out = usart_gd32_poll_out ,
130268 .err_check = usart_gd32_err_check ,
269+ #ifdef CONFIG_UART_INTERRUPT_DRIVEN
270+ .fifo_fill = usart_gd32_fifo_fill ,
271+ .fifo_read = usart_gd32_fifo_read ,
272+ .irq_tx_enable = usart_gd32_irq_tx_enable ,
273+ .irq_tx_disable = usart_gd32_irq_tx_disable ,
274+ .irq_tx_ready = usart_gd32_irq_tx_ready ,
275+ .irq_tx_complete = usart_gd32_irq_tx_complete ,
276+ .irq_rx_enable = usart_gd32_irq_rx_enable ,
277+ .irq_rx_disable = usart_gd32_irq_rx_disable ,
278+ .irq_rx_ready = usart_gd32_irq_rx_ready ,
279+ .irq_err_enable = usart_gd32_irq_err_enable ,
280+ .irq_err_disable = usart_gd32_irq_err_disable ,
281+ .irq_is_pending = usart_gd32_irq_is_pending ,
282+ .irq_callback_set = usart_gd32_irq_callback_set ,
283+ #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
131284};
132285
286+ #ifdef CONFIG_UART_INTERRUPT_DRIVEN
287+ #define GD32_USART_IRQ_HANDLER (n ) \
288+ static void usart_gd32_config_func_##n(const struct device *dev) \
289+ { \
290+ IRQ_CONNECT(DT_INST_IRQN(n), \
291+ DT_INST_IRQ(n, priority), \
292+ usart_gd32_isr, \
293+ DEVICE_DT_INST_GET(n), \
294+ 0); \
295+ irq_enable(DT_INST_IRQN(n)); \
296+ }
297+ #define GD32_USART_IRQ_HANDLER_FUNC_INIT (n ) \
298+ .irq_config_func = usart_gd32_config_func_##n
299+ #else /* CONFIG_UART_INTERRUPT_DRIVEN */
300+ #define GD32_USART_IRQ_HANDLER (n )
301+ #define GD32_USART_IRQ_HANDLER_FUNC_INIT (n )
302+ #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
303+
133304#define GD32_USART_INIT (n ) \
134305 PINCTRL_DT_INST_DEFINE(n) \
135- static struct gd32_usart_data usart##n##_gd32_data = { \
306+ GD32_USART_IRQ_HANDLER(n) \
307+ static struct gd32_usart_data usart_gd32_data_##n = { \
136308 .baud_rate = DT_INST_PROP(n, current_speed), \
137309 }; \
138- static const struct gd32_usart_config usart ##n##_gd32_config = { \
310+ static const struct gd32_usart_config usart_gd32_config_ ##n = { \
139311 .reg = DT_INST_REG_ADDR(n), \
140312 .rcu_periph_clock = DT_INST_PROP(n, rcu_periph_clock), \
141313 .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
142314 .parity = DT_ENUM_IDX_OR(DT_DRV_INST(n), parity, \
143315 UART_CFG_PARITY_NONE), \
316+ GD32_USART_IRQ_HANDLER_FUNC_INIT(n) \
144317 }; \
145318 DEVICE_DT_INST_DEFINE(n, &usart_gd32_init, \
146319 NULL, \
147- &usart ##n##_gd32_data , \
148- &usart ##n##_gd32_config , PRE_KERNEL_1, \
149- CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
320+ &usart_gd32_data_ ##n, \
321+ &usart_gd32_config_ ##n, PRE_KERNEL_1, \
322+ CONFIG_SERIAL_INIT_PRIORITY, \
150323 &usart_gd32_driver_api);
151324
152325DT_INST_FOREACH_STATUS_OKAY (GD32_USART_INIT )
0 commit comments