新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 瑞薩RA0單片機連載之移植面向對象之UART驅動

        瑞薩RA0單片機連載之移植面向對象之UART驅動

        作者:lulugl 時間:2024-11-21 來源:EEPW 收藏

        串口是最常用的外設之一。本次創建面向對象來移植UART 的驅動。

        本文引用地址:http://www.104case.com/article/202411/464857.htm

        1   學習例程

        百問網的面向對象的這,源代碼為百問網的RA6M5 的驅動,我這里做了細小的改動而實現快速的驅動。

        2   創建工程

        在上一篇連載之三基于面向對象的LED燈(刊載于《電子設計與芯片應用(10 月刊)》)的基礎上添加串口的驅動。

        1.本次驅動選用的驅動的串口為r_sau_uart0, 配置的IO 為P100,P101 為TX 與RX。

        打開RASC, 添加uart0添回串口回調函數為sau_uart_callback。并生成工程。

        1732196677847980.png

        2.移植dev_uart.c/h、drv_uart.c/h 到工程中:

        1732196769396381.png

        3. 其代碼dev_uart.c 如下:

        view plaincopy to clipboardprint?

        1. #include “dev_uart.h”

        2. #include <drivers.h>

        3.

        4. #include <stdlib.h>

        5. #include <string.h>

        6. #include <stdio.h>

        7.

        8.

        9. static struct UartDev *gHeadUartDev;

        10.

        11. void UartDevicesRegister(void)

        12. {

        13. UartDevicesCreate();

        14. UartDeviceList();

        15. }

        16.

        17. void UartDeviceInsert(struct UartDev *ptdev)

        18. {

        19. if(NULL == gHeadUartDev)

        20. gHeadUartDev = ptdev;

        21. else

        22. {

        23. ptdev->next = gHeadUartDev;

        24. gHeadUartDev = ptdev;

        25. }

        26. }

        27.

        28. struct UartDev * UartDeviceFind(const char

        *name)

        29. {

        30. struct UartDev *ptdev = gHeadUartDev;

        31. while(ptdev)

        unsigned char * const buf, unsigned int length);

        19.

        20. static volatile bool gUart0TxCplt = false;

        21. static volatile bool gUart0RxCplt = false;

        22.

        23. static struct UartDev gLogDevice = {

        24. .name = “Log”,

        25. .channel = 0,

        26. .Init = UARTDrvInit,

        27. .Read = UARTDrvRead,

        28. .Write = UARTDrvWrite,

        29. .next = NULL

        30. };

        31.

        32. void UartDevicesCreate(void)

        33. {

        34. UartDeviceInsert(&gLogDevice);

        35. gLogDevice.Init(&gLogDevice);

        36. }

        37.

        38. static int UARTDrvInit(struct UartDev *ptdev)

        39. {

        40. if(NULL == ptdev) return -EINVAL;

        41.

        42. switch(ptdev->channel)

        43 {

        44. case 0:

        45. {

        46. fsp_err_t err = g_uart0.p_api->open(g_

        uart0.p_ctrl, g_uart0.p_cfg);

        47. assert(FSP_SUCCESS == err);

        48. break;

        49. }

        50.

        51. case 1:case 2:

        52. case 3:case 4:case 5:

        53. case 6:

        54. {

        55. break;

        56. }

        57. case 7:

        58.

        59. case 8:case 9:

        60. break;

        61. default:break;

        62. }

        63.

        64. return ESUCCESS;

        65. }

        66.

        67. static int UARTDrvWrite(struct UartDev *ptdev,

        unsigned char * const buf, unsigned int length)

        68. {

        69. if(NULL == ptdev) return -EINVAL;

        70. if(NULL == buf) return -EINVAL;

        71. if(0 == length) return -EINVAL;

        72.

        73. switch(ptdev->channel)

        74. {

        75. case 0:

        76. {

        77. fsp_err_t err = g_uart0.p_api->write(g_

        uart0.p_ctrl, buf, length);

        78. assert(FSP_SUCCESS == err);

        79. UART0WaitTxCplt();

        80. break;

        81. }

        82. case 1:case 2:

        83. case 3:case 4:case 5:

        84. case 6:

        85. {

        86. break;

        87. }

        88. case 7:

        89.

        90. case 8:case 9:

        91. break;

        92. default:break;

        93. }

        94. return ESUCCESS;

        95. }

        96. static int UARTDrvRead(struct UartDev *ptdev,

        unsigned char *buf, unsigned int length)

        97. {

        98. if(NULL == ptdev) return -EINVAL;

        99. if(NULL == buf) return -EINVAL;

        100. if(0 == length) return -EINVAL;

        101.

        102. switch(ptdev->channel)

        103. {

        104. case 0:

        105. {

        106. fsp_err_t err = g_uart0.p_api->read(g_

        uart0.p_ctrl, buf, length);

        107. assert(FSP_SUCCESS == err);

        108. UART0WaitRxCplt();

        109. break;

        110. }

        111. case 1:case 2:

        112. case 3:case 4:case 5:

        113. case 6:

        114. {

        115. break;

        116. }

        117. case 7:

        118.

        119. case 8:case 9:

        120. break;

        121. default:break;

        122. }

        123.

        124. return (int)length;

        125. }

        126.

        127. void sau_uart_callback(uart_callback_args_

        t * p_args)

        128. {

        129. switch(p_args->event)

        130. {

        131. case UART_EVENT_RX_COMPLETE:

        132. {

        133. gUart0RxCplt = true;

        134. break;

        135. }

        136. case UART_EVENT_TX_COMPLETE:

        137. {

        138. gUart0TxCplt = true;

        139. break;

        140. }

        141. case UART_EVENT_RX_CHAR:

        142. {

        143. break;

        144. }

        145. case UART_EVENT_ERR_PARITY:case

        UART_EVENT_ERR_FRAMING:

        146. case UART_EVENT_ERR_OVERFLOW:case

        UART_EVENT_BREAK_DETECT:

        147. case UART_EVENT_TX_DATA_EMPTY:

        148. break;

        149. default:break;

        150. }

        151. }

        152.

        153. static void UART0WaitTxCplt(void)

        154. {

        155. while(!gUart0TxCplt);

        156. gUart0TxCplt = false;

        157. }

        158.

        159. static void UART0WaitRxCplt(void)

        160. {

        161. while(!gUart0RxCplt);

        162. gUart0RxCplt = false;

        163. }

        這里我們需要修改的地方就是對源碼修改UART0的自幾回調函數以及

        view plaincopy to clipboardprint?

        1. switch(ptdev->channel)

        drv_uart.h

        view plaincopy to clipboardprint?

        1. /*

        2. * drv_uart.h

        3. *

        4. * Created on: 2023 年4 月13 日

        5. * Author: slhuan

        6. */

        7. #ifndef __DRV_UART_H

        8. #defi ne __DRV_UART_H

        9.

        10. #define UART_DEVICE_NAME_MAX_

        LENGTH (16)

        11. #def ine UART_TOTAL_CHANNELS

        (10)

        12. #define UART_RECEIVE_BUFFER_SIZE

        (1024)

        13.

        14. void UartDevicesCreate(void);

        15.

        16. #endif /* __DRV_UART_H */

        到此為止,我們的驅動就移植完畢了。

        測試代碼:

        image.png

        實驗效果,編譯下載程序到開發板后,開機打印出初始化的LOG

        1732196925546326.png

        3 總結

        我們實現了面向對象的UART 的移植,從RA6M5的工程中移植過來,只需要修改少量的代碼就可以實現面向對象的移植。

        (本文來源于《EEPW》



        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 嘉黎县| 哈密市| 栖霞市| 博湖县| 申扎县| 兴安县| 凤凰县| 体育| 普兰店市| 孟连| 扎兰屯市| 平度市| 饶平县| 石屏县| 墨玉县| 砚山县| 牡丹江市| 锦州市| 白山市| 巴彦县| 吴忠市| 江达县| 衡山县| 夏津县| 青州市| 邢台市| 沾益县| 绥芬河市| 奉贤区| 弥勒县| 松江区| 渭源县| 沐川县| 扬州市| 阳山县| 景宁| 黑龙江省| 耒阳市| 龙山县| 碌曲县| 呼伦贝尔市|