在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32F10x 學(xué)習(xí)筆記7(USART實(shí)現(xiàn)串口通訊 3)

            STM32F10x 學(xué)習(xí)筆記7(USART實(shí)現(xiàn)串口通訊 3)

            作者: 時(shí)間:2016-11-20 來(lái)源:網(wǎng)絡(luò) 收藏
            在上一篇學(xué)習(xí)筆記《STM32F10x 學(xué)習(xí)筆記6(USART實(shí)現(xiàn)串口通訊 2)》給出了個(gè)利用環(huán)形緩沖區(qū)的串口驅(qū)動(dòng)。最近研究了uCOS-II在STM32上的移植。下面再給個(gè)利用uCOS-II的信號(hào)量的串口驅(qū)動(dòng)。整個(gè)驅(qū)動(dòng)的基本框架和上一篇沒(méi)什么區(qū)別,所以不多介紹。直接貼代碼:

            整個(gè)驅(qū)動(dòng)包含四個(gè)文件:

            本文引用地址:http://www.biyoush.com/article/201611/318843.htm

            uart.h

            uart.c

            COMMRTOS.H

            COMMRTOS.c

            其中前兩個(gè)文件是對(duì)串口基本功能的封裝。

            uart.h 的代碼如下:

            1. #ifndef_UART_H_
            2. #define_UART_H_
            3. voidUSART1_Init(void);
            4. voidUSART2_Init(void);
            5. voidUART_PutChar(USART_TypeDef*USARTx,uint8_tData);
            6. voidUART_PutStr(USART_TypeDef*USARTx,uint8_t*str);
            7. uint8_tUART_GetChar(USART_TypeDef*USARTx);
            8. #endif

            uart.c 的代碼

            1. #include"stm32f10x.h"
            2. voidUSART1_Init(void)
            3. {
            4. GPIO_InitTypeDefGPIO_InitStructure;
            5. USART_InitTypeDefUSART_InitStructure;
            6. NVIC_InitTypeDefNVIC_InitStructure;
            7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
            8. /*ConfigureUSARTTxasalternatefunctionpush-pull*/
            9. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
            10. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
            11. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
            12. GPIO_Init(GPIOA,&GPIO_InitStructure);
            13. /*ConfigureUSARTRxasinputfloating*/
            14. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
            15. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
            16. GPIO_Init(GPIOA,&GPIO_InitStructure);
            17. USART_InitStructure.USART_BaudRate=9600;
            18. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
            19. USART_InitStructure.USART_StopBits=USART_StopBits_1;
            20. USART_InitStructure.USART_Parity=USART_Parity_No;
            21. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
            22. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
            23. USART_Init(USART1,&USART_InitStructure);
            24. USART_Cmd(USART1,ENABLE);
            25. NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
            26. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
            27. NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
            28. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
            29. NVIC_Init(&NVIC_InitStructure);
            30. }
            31. voidUSART2_Init(void)
            32. {
            33. GPIO_InitTypeDefGPIO_InitStructure;
            34. USART_InitTypeDefUSART_InitStructure;
            35. NVIC_InitTypeDefNVIC_InitStructure;
            36. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
            37. /*ConfigureUSARTTxasalternatefunctionpush-pull*/
            38. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
            39. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
            40. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
            41. GPIO_Init(GPIOD,&GPIO_InitStructure);
            42. /*ConfigureUSARTRxasinputfloating*/
            43. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
            44. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
            45. GPIO_Init(GPIOD,&GPIO_InitStructure);
            46. GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
            47. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
            48. USART_InitStructure.USART_BaudRate=9600;
            49. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
            50. USART_InitStructure.USART_StopBits=USART_StopBits_1;
            51. USART_InitStructure.USART_Parity=USART_Parity_No;
            52. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
            53. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
            54. USART_Init(USART2,&USART_InitStructure);
            55. USART_Cmd(USART2,ENABLE);
            56. NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
            57. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
            58. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
            59. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
            60. NVIC_Init(&NVIC_InitStructure);
            61. }
            62. voidUART_PutChar(USART_TypeDef*USARTx,uint8_tData)
            63. {
            64. //while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET){};
            65. while((USARTx->SR&USART_FLAG_TXE)==0x00){};
            66. //USART_SendData(USARTx,Data);
            67. USARTx->DR=Data;
            68. }
            69. voidUART_PutStr(USART_TypeDef*USARTx,uint8_t*str)
            70. {
            71. while(0!=*str)
            72. {
            73. UART_PutChar(USARTx,*str);
            74. str++;
            75. }
            76. }
            77. uint8_tUART_GetChar(USART_TypeDef*USARTx)
            78. {
            79. //while(USART_GetFlagStatus(USARTx,USART_FLAG_RXNE)==RESET){};
            80. while((USARTx->SR&USART_FLAG_RXNE)==0x00){};
            81. //returnUSART_ReceiveData(USARTx);
            82. return(USARTx->DR&0xff);
            83. }

            commrtos.h 的代碼如下:

            1. #ifndef_COMMRTOS_H_
            2. #define_COMMRTOS_H_
            3. #ifndefCFG_H
            4. #defineCOMM_RX_BUF_SIZE64/*NumberofcharactersinRxringbuffer*/
            5. #defineCOMM_TX_BUF_SIZE64/*NumberofcharactersinTxringbuffer*/
            6. #endif
            7. /*
            8. *********************************************************************************************************
            9. *CONSTANTS
            10. *********************************************************************************************************
            11. */
            12. #ifndefFALSE
            13. #defineFALSE0x00
            14. #endif
            15. #ifndefTRUE
            16. #defineTRUE0xff
            17. #endif
            18. #ifndefNUL
            19. #defineNUL0x00
            20. #endif
            21. #defineCOM10
            22. #defineCOM21
            23. /*ERRORCODES*/
            24. #defineCOMM_NO_ERR0/*Functioncallwassuccessful*/
            25. #defineCOMM_BAD_CH1/*Invalidcommunicationsportchannel*/
            26. #defineCOMM_RX_EMPTY2/*Rxbufferisempty,nocharacteravailable*/
            27. #defineCOMM_TX_FULL3/*Txbufferisfull,couldnotdepositcharacter*/
            28. #defineCOMM_TX_EMPTY4/*IftheTxbufferisempty.*/
            29. #defineCOMM_RX_TIMEOUT5/*Ifatimeoutoccurredwhilewaitingforacharacter*/
            30. #defineCOMM_TX_TIMEOUT6/*Ifatimeoutoccurredwhilewaitingtosendachar.*/
            31. #defineCOMM_PARITY_NONE0/*Definesforsettingparity*/
            32. #defineCOMM_PARITY_ODD1
            33. #defineCOMM_PARITY_EVEN2
            34. unsignedcharCommGetChar(unsignedcharch,unsignedshortto,unsignedchar*err);
            35. voidCOMInit(void);
            36. unsignedcharCommIsEmpty(unsignedcharch);
            37. unsignedcharCommIsFull(unsignedcharch);
            38. unsignedcharCommPutChar(unsignedcharch,unsignedcharc,unsignedshortto);
            39. voidCommPutStr(unsignedcharch,uint8_t*str);
            40. #endif

            commrtos.c 的代碼如下:

            1. #include"stm32f10x_usart.h"
            2. #include"includes.h"
            3. #include"COMMRTOS.H"
            4. /*
            5. *DATATYPES
            6. */
            7. typedefstruct{
            8. unsignedshortRingBufRxCtr;/*NumberofcharactersintheRxringbuffer*/
            9. OS_EVENT*RingBufRxSem;/*PointertoRxsemaphore*/
            10. unsignedchar*RingBufRxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
            11. unsignedchar*RingBufRxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
            12. unsignedcharRingBufRx[COMM_RX_BUF_SIZE];/*Ringbuffercharacterstorage(Rx)*/
            13. unsignedshortRingBufTxCtr;/*NumberofcharactersintheTxringbuffer*/
            14. OS_EVENT*RingBufTxSem;/*PointertoTxsemaphore*/
            15. unsignedchar*RingBufTxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
            16. unsignedchar*RingBufTxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
            17. unsignedcharRingBufTx[COMM_TX_BUF_SIZE];/*Ringbuffercharacterstorage(Tx)*/
            18. }COMM_RING_BUF;
            19. /*
            20. *GLOBALVARIABLES
            21. */
            22. COMM_RING_BUFComm1Buf;
            23. COMM_RING_BUFComm2Buf;
            24. staticvoidCOMEnableTxInt(unsignedcharport)
            25. {
            26. staticUSART_TypeDef*map[2]={USART1,USART2};
            27. //USART_ITConfig(map[port],USART_IT_TXE,ENABLE);
            28. map[port]->CR1|=USART_FLAG_TXE;
            29. }
            30. /*
            31. *********************************************************************************************************
            32. *REMOVECHARACTERFROMRINGBUFFER
            33. *
            34. *
            35. *Description:Thisfunctioniscalledbyyourapplicationtoobtainacharacterfromthecommunications
            36. *channel.Thefunctionwillwaitforacharactertobereceivedontheserialchannelor
            37. *untilthefunctiontimesout.
            38. *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
            39. *COMM1
            40. *COMM2
            41. *toistheamountoftime(inclockticks)thatthecallingfunctioniswillingto
            42. *waitforacharactertoarrive.Ifyouspecifyatimeoutof0,thefunctionwill
            43. *waitforeverforacharactertoarrive.
            44. *errisapointertowhereanerrorcodewillbeplaced:
            45. **errissettoCOMM_NO_ERRifacharacterhasbeenreceived
            46. **errissettoCOMM_RX_TIMEOUTifatimeoutoccurred
            47. **errissettoCOMM_BAD_CHifyouspecifyaninvalidchannelnumber
            48. *Returns:Thecharacterinthebuffer(orNULifatimeoutoccurred)
            49. *********************************************************************************************************
            50. */
            51. unsignedcharCommGetChar(unsignedcharch,unsignedshortto,unsignedchar*err)
            52. {
            53. unsignedcharc;
            54. unsignedcharoserr;
            55. COMM_RING_BUF*pbuf;
            56. OS_CPU_SRcpu_sr;
            57. switch(ch)
            58. {/*Obtainpointertocommunicationschannel*/
            59. caseCOM1:
            60. pbuf=&Comm1Buf;
            61. break;
            62. caseCOM2:
            63. pbuf=&Comm2Buf;
            64. break;
            65. default:
            66. *err=COMM_BAD_CH;
            67. return(NUL);
            68. }
            69. OSSemPend(pbuf->RingBufRxSem,to,&oserr);/*Waitforcharactertoarrive*/
            70. if(oserr==OS_TIMEOUT)
            71. {/*Seeifcharactersreceivedwithintimeout*/
            72. *err=COMM_RX_TIMEOUT;/*No,returnerrorcode*/
            73. return(NUL);
            74. }
            75. else
            76. {
            77. OS_ENTER_CRITICAL();
            78. pbuf->RingBufRxCtr--;/*Yes,decrementcharactercount*/
            79. c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
            80. if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[COMM_RX_BUF_SIZE])
            81. {/*WrapOUTpointer*/
            82. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
            83. }
            84. OS_EXIT_CRITICAL();
            85. *err=COMM_NO_ERR;
            86. return(c);
            87. }
            88. }
            89. voidCommPutStr(unsignedcharch,uint8_t*str)
            90. {
            91. while(0!=*str)
            92. {
            93. CommPutChar(ch,*str,0);
            94. str++;
            95. }
            96. }
            97. staticunsignedcharCOMGetTxChar(unsignedcharch,unsignedchar*err)
            98. {
            99. unsignedcharc;
            100. COMM_RING_BUF*pbuf;
            101. switch(ch)
            102. {/*Obtainpointertocommunicationschannel*/
            103. caseCOM1:
            104. pbuf=&Comm1Buf;
            105. break;
            106. caseCOM2:
            107. pbuf=&Comm2Buf;
            108. break;
            109. default:
            110. *err=COMM_BAD_CH;
            111. return(NUL);
            112. }
            113. if(pbuf->RingBufTxCtr>0)
            114. {/*Seeifbufferisempty*/
            115. pbuf->RingBufTxCtr--;/*No,decrementcharactercount*/
            116. c=*pbuf->RingBufTxOutPtr++;/*Getcharacterfrombuffer*/
            117. if(pbuf->RingBufTxOutPtr==&pbuf->RingBufTx[COMM_TX_BUF_SIZE])
            118. {/*WrapOUTpointer*/
            119. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
            120. }
            121. OSSemPost(pbuf->RingBufTxSem);/*Indicatethatcharacterwillbesent*/
            122. *err=COMM_NO_ERR;
            123. return(c);/*Charactersarestillavailable*/
            124. }
            125. else
            126. {
            127. *err=COMM_TX_EMPTY;
            128. return(NUL);/*Bufferisempty*/
            129. }
            130. }
            131. /*
            132. *********************************************************************************************************
            133. *INITIALIZECOMMUNICATIONSMODULE
            134. *Description:Thisfunctioniscalledbyyourapplicationtoinitializethecommunicationsmodule.You
            135. *mustcallthisfunctionbeforecallinganyotherfunctions.
            136. *Arguments:none
            137. *********************************************************************************************************
            138. */
            139. voidCOMInit(void)
            140. {
            141. COMM_RING_BUF*pbuf;
            142. pbuf=&Comm1Buf;/*InitializetheringbufferforCOMM1*/
            143. pbuf->RingBufRxCtr=0;
            144. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
            145. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
            146. pbuf->RingBufRxSem=OSSemCreate(0);
            147. pbuf->RingBufTxCtr=0;
            148. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
            149. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
            150. pbuf->RingBufTxSem=OSSemCreate(COMM_TX_BUF_SIZE);
            151. pbuf=&Comm2Buf;/*InitializetheringbufferforCOMM2*/
            152. pbuf->RingBufRxCtr=0;
            153. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
            154. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
            155. pbuf->RingBufRxSem=OSSemCreate(0);
            156. pbuf->RingBufTxCtr=0;
            157. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
            158. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
            159. pbuf->RingBufTxSem=OSSemCreate(COMM_TX_BUF_SIZE);
            160. }
            161. /*
            162. *********************************************************************************************************
            163. *SEEIFRXCHARACTERBUFFERISEMPTY
            164. *
            165. *
            166. *Description:Thisfunctioniscalledbyyourapplicationtoseeifanycharacterisavailablefromthe
            167. *communicationschannel.Ifatleastonecharacterisavailable,thefunctionreturns
            168. *FALSEotherwise,thefunctionreturnsTRUE.
            169. *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
            170. *COMM1
            171. *COMM2
            172. *Returns:TRUEifthebufferISempty.
            173. *FALSEifthebufferISNOTemptyoryouhavespecifiedanincorrectchannel.
            174. *********************************************************************************************************
            175. */
            176. unsignedcharCommIsEmpty(unsignedcharch)
            177. {
            178. unsignedcharempty;
            179. COMM_RING_BUF*pbuf;
            180. OS_CPU_SRcpu_sr;
            181. switch(ch)
            182. {/*Obtainpointertocommunicationschannel*/
            183. caseCOM1:
            184. pbuf=&Comm1Buf;
            185. break;
            186. caseCOM2:
            187. pbuf=&Comm2Buf;
            188. break;
            189. default:
            190. return(TRUE);
            191. }
            192. OS_ENTER_CRITICAL();
            193. if(pbuf->RingBufRxCtr>0)
            194. {/*Seeifbufferisempty*/
            195. empty=FALSE;/*BufferisNOTempty*/
            196. }
            197. else
            198. {
            199. empty=TRUE;/*Bufferisempty*/
            200. }
            201. OS_EXIT_CRITICAL();
            202. return(empty);
            203. }
            204. /*
            205. *********************************************************************************************************
            206. *SEEIFTXCHARACTERBUFFERISFULL
            207. *Description:Thisfunctioniscalledbyyourapplicationtoseeifanymorecharacterscanbeplaced
            208. *intheTxbuffer.Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
            209. *Ifthebufferisfull,thefunctionreturnsTRUEotherwise,thefunctionreturnsFALSE.
            210. *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
            211. *COMM1
            212. *COMM2
            213. *Returns:TRUEifthebufferISfull.
            214. *FALSEifthebufferISNOTfulloryouhavespecifiedanincorrectchannel.
            215. *********************************************************************************************************
            216. */
            217. unsignedcharCommIsFull(unsignedcharch)
            218. {
            219. unsignedcharfull;
            220. COMM_RING_BUF*pbuf;
            221. OS_CPU_SRcpu_sr;
            222. switch(ch)
            223. {/*Obtainpointertocommunicationschannel*/
            224. caseCOM1:
            225. pbuf=&Comm1Buf;
            226. break;
            227. caseCOM2:
            228. pbuf=&Comm2Buf;
            229. break;
            230. default:
            231. return(TRUE);
            232. }
            233. OS_ENTER_CRITICAL();
            234. if(pbuf->RingBufTxCtr
            235. {/*Seeifbufferisfull*/
            236. full=FALSE;/*BufferisNOTfull*/
            237. }
            238. else
            239. {
            240. full=TRUE;/*Bufferisfull*/
            241. }
            242. OS_EXIT_CRITICAL();
            243. return(full);
            244. }
            245. /*
            246. *********************************************************************************************************
            247. *OUTPUTCHARACTER
            248. *
            249. *
            250. *Description:Thisfunctioniscalledbyyourapplicationtosendacharacteronthecommunications
            251. *channel.Thefunctionwillwaitforthebuffertoemptyoutifthebufferisfull.
            252. *Thefunctionreturnstoyourapplicationifthebufferdoesntemptywithinthespecified
            253. *timeout.Atimeoutvalueof0meansthatthecallingfunctionwillwaitforeverforthe
            254. *buffertoemptyout.ThecharactertosendisfirstinsertedintotheTxbufferandwill
            255. *besentbytheTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISR
            256. *willbeenabled.
            257. *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
            258. *COMM1
            259. *COMM2
            260. *cisthecharactertosend.
            261. *toisthetimeout(inclockticks)towaitincasethebufferisfull.Ifyou
            262. *specifyatimeoutof0,thefunctionwillwaitforeverforthebuffertoempty.
            263. *Returns:COMM_NO_ERRifthecharacterwasplacedintheTxbuffer
            264. *COMM_TX_TIMEOUTifthebufferdidntemptywithinthespecifiedtimeoutperiod
            265. *COMM_BAD_CHifyouspecifyaninvalidchannelnumber
            266. *********************************************************************************************************
            267. */
            268. unsignedcharCommPutChar(unsignedcharch,unsignedcharc,unsignedshortto)
            269. {
            270. unsignedcharoserr;
            271. COMM_RING_BUF*pbuf;
            272. OS_CPU_SRcpu_sr;
            273. switch(ch)
            274. {/*Obtainpointertocommunicationschannel*/
            275. caseCOM1:
            276. pbuf=&Comm1Buf;
            277. break;
            278. caseCOM2:
            279. pbuf=&Comm2Buf;
            280. break;
            281. default:
            282. return(COMM_BAD_CH);
            283. }
            284. OSSemPend(pbuf->RingBufTxSem,to,&oserr);/*WaitforspaceinTxbuffer*/
            285. if(oserr==OS_TIMEOUT)
            286. {
            287. return(COMM_TX_TIMEOUT);/*Timedout,returnerrorcode*/
            288. }
            289. OS_ENTER_CRITICAL();
            290. pbuf->RingBufTxCtr++;/*No,incrementcharactercount*/
            291. *pbuf->RingBufTxInPtr++=c;/*Putcharacterintobuffer*/
            292. if(pbuf->RingBufTxInPtr==&pbuf->RingBufTx[COMM_TX_BUF_SIZE])
            293. {/*WrapINpointer*/
            294. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
            295. }
            296. if(pbuf->RingBufTxCtr==1)
            297. {/*Seeifthisisthefirstcharacter*/
            298. COMEnableTxInt(ch);/*Yes,EnableTxinterrupts*/
            299. }
            300. OS_EXIT_CRITICAL();
            301. return(COMM_NO_ERR);
            302. }
            303. /*
            304. *********************************************************************************************************
            305. *INSERTCHARACTERINTORINGBUFFER
            306. *
            307. *
            308. *Description:ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
            309. *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
            310. *COMM1
            311. *COMM2
            312. *cisthecharactertoinsertintotheringbuffer.Ifthebufferisfull,the
            313. *characterwillnotbeinserted,itwillbelost.
            314. *********************************************************************************************************
            315. */
            316. staticvoidCOMPutRxChar(unsignedcharch,unsignedcharc)
            317. {
            318. COMM_RING_BUF*pbuf;
            319. switch(ch)
            320. {/*Obtainpointertocommunicationschannel*/
            321. caseCOM1:
            322. pbuf=&Comm1Buf;
            323. break;
            324. caseCOM2:
            325. pbuf=&Comm2Buf;
            326. break;
            327. default:
            328. return;
            329. }
            330. if(pbuf->RingBufRxCtr
            331. {/*Seeifbufferisfull*/
            332. pbuf->RingBufRxCtr++;/*No,incrementcharactercount*/
            333. *pbuf->RingBufRxInPtr++=c;/*Putcharacterintobuffer*/
            334. if(pbuf->RingBufRxInPtr==&pbuf->RingBufRx[COMM_RX_BUF_SIZE])
            335. {/*WrapINpointer*/
            336. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
            337. }
            338. OSSemPost(pbuf->RingBufRxSem);/*Indicatethatcharacterwasreceived*/
            339. }
            340. }
            341. //ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
            342. voidUSART1_IRQHandler(void)
            343. {
            344. unsignedintdata;
            345. unsignedcharerr;
            346. OS_CPU_SRcpu_sr;
            347. OS_ENTER_CRITICAL();/*TelluC/OS-IIthatwearestartinganISR*/
            348. OSIntNesting++;
            349. OS_EXIT_CRITICAL();
            350. if(USART1->SR&0x0F)
            351. {
            352. //Seeifwehavesomekindoferror
            353. //Clearinterrupt(donothingaboutit!)
            354. data=USART1->DR;
            355. }
            356. elseif(USART1->SR&USART_FLAG_RXNE)//ReceiveDataRegFullFlag
            357. {
            358. data=USART1->DR;
            359. COMPutRxChar(COM1,data);//Insertreceivedcharacterintobuffer
            360. }
            361. elseif(USART1->SR&USART_FLAG_TXE)
            362. {
            363. data=COMGetTxChar(COM1,&err);//Getnextcharactertosend.
            364. if(err==COMM_TX_EMPTY)
            365. {//Dowehaveanymorecharacterstosend?
            366. //No,DisableTxinterrupts
            367. //USART_ITConfig(USART1,USART_IT_TXE|USART_IT_TC,DISABLE);
            368. USART1->CR1&=~USART_FLAG_TXE|USART_FLAG_TC;
            369. }
            370. else
            371. {
            372. USART1->DR=data;//Yes,Sendcharacter
            373. }
            374. }
            375. OSIntExit();
            376. }
            377. voidUSART2_IRQHandler(void)
            378. {
            379. unsignedintdata;
            380. unsignedcharerr;
            381. OS_CPU_SRcpu_sr;
            382. OS_ENTER_CRITICAL();/*TelluC/OS-IIthatwearestartinganISR*/
            383. OSIntNesting++;
            384. OS_EXIT_CRITICAL();
            385. if(USART2->SR&0x0F)
            386. {
            387. //Seeifwehavesomekindoferror
            388. //Clearinterrupt(donothingaboutit!)
            389. data=USART2->DR;
            390. }
            391. elseif(USART2->SR&USART_FLAG_RXNE)//ReceiveDataRegFullFlag
            392. {
            393. data=USART2->DR;
            394. COMPutRxChar(COM2,data);//Insertreceivedcharacterintobuffer
            395. }
            396. elseif(USART2->SR&USART_FLAG_TXE)
            397. {
            398. data=COMGetTxChar(COM2,&err);//Getnextcharactertosend.
            399. if(err==COMM_TX_EMPTY)
            400. {//Dowehaveanymorecharacterstosend?
            401. //No,DisableTxinterrupts
            402. //USART_ITConfig(USART2,USART_IT_TXE|USART_IT_TC,DISABLE);
            403. USART2->CR1&=~USART_FLAG_TXE|USART_FLAG_TC;
            404. }
            405. else
            406. {
            407. USART2->DR=data;//Yes,Sendcharacter
            408. }
            409. }
            410. OSIntExit();
            411. }

            下面再給出個(gè)測(cè)試代碼:

            1. #include"stm32f10x.h"
            2. #include"uart.h"
            3. #include"led.h"
            4. #include"COMMRTOS.H"
            5. #include"ucos_ii.h"
            6. #defineTASK_STK_SIZE128
            7. OS_STKTaskStartStk[TASK_STK_SIZE];
            8. OS_STKTaskUartReadStk[TASK_STK_SIZE];
            9. voidTaskUartRead(void*pdata)
            10. {
            11. unsignedcharerr;
            12. unsignedcharc;
            13. for(;;)
            14. {
            15. c=CommGetChar(COM2,0,&err);
            16. if(err==COMM_NO_ERR)
            17. CommPutChar(COM2,c,0);
            18. }
            19. }
            20. voidTaskStart(void*pdata)
            21. {
            22. SysTick_Config(SystemCoreClock/10);
            23. USART1_Init();
            24. USART2_Init();
            25. COMInit();
            26. //USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
            27. //USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
            28. USART1->CR1|=USART_FLAG_RXNE;
            29. USART2->CR1|=USART_FLAG_RXNE;
            30. OSTaskCreate(TaskUartRead,(void*)0,&(TaskUartReadStk[TASK_STK_SIZE-1]),2);
            31. UART_PutStr(USART2,"USART2HelloWorld!nr");
            32. //CommPutChar(COM2,+,0);
            33. CommPutStr(COM2,"CommPutCharBnr");
            34. for(;;)
            35. {
            36. LED_Spark();
            37. CommPutChar(COM2,+,0);
            38. OSTimeDly(10);
            39. }
            40. }
            41. intmain(void)
            42. {
            43. SystemInit();
            44. LED_Init();
            45. OSInit();
            46. OSTaskCreate(TaskStart,(void*)0,&(TaskStartStk[TASK_STK_SIZE-1]),1);
            47. OSStart();
            48. for(;;)
            49. {
            50. }
            51. }



            關(guān)鍵詞: STM32F10xUSART串口通

            評(píng)論


            技術(shù)專區(qū)

            關(guān)閉