【极海APM32F407 Tiny Board】跑个串口例子USART_Interrupt
[复制链接]
【极海APM32F407 Tiny Board】跑个串口例子USART_Interrupt
1,修改预处理符号APM32F407_MINI为APM32F407_TINY
2,修改代码如下 :
#include "main.h"
#include "Board.h"
#define DATA_BUF_SIZE (32)
typedef enum {FALSE, TRUE} BOOL;
/** USART1 receive buffer*/
uint8_t rxDataBufUSART1[DATA_BUF_SIZE] = {0};
/** USART1 transmit buffer*/
uint8_t txDataBufUSART1[DATA_BUF_SIZE] = {0};
/** USART2 receive buffer*/
uint8_t rxDataBufUSART2[DATA_BUF_SIZE] = {0};
/** USART2 transmit buffer*/
uint8_t txDataBufUSART2[DATA_BUF_SIZE] = {0};
uint32_t rxCountUSART1 = 0;
uint32_t rxCountUSART2 = 0;
/** USART Initialization */
void USART_Init(void);
/** USART Write data */
void USART_Write(USART_T* usart,uint8_t *dat, uint32_t count);
/** Delay */
void Delay(uint32_t count);
/** Buffer compare*/
BOOL BufferCompare(uint8_t *buf1, uint8_t *buf2, uint8_t size);
void delayXms(uint32_t x)
{
for(uint32_t i=0;i<10000;i++){
for(uint32_t j=0;j<x;j++){}
}
}
int main(void)
{
BOOL state = TRUE;
/** Init txDataBufUSART2 */
for(int i = 0; i < DATA_BUF_SIZE; i++){
txDataBufUSART1[i] = i;
txDataBufUSART2[i] = i;
}
APM_TINY_LEDInit(LED2);
APM_TINY_LEDInit(LED3);
/** USART Initialization */
USART_Init();
while(1){
rxCountUSART1 = 0;
rxCountUSART2 = 0;
/** Send Data from USART1 to USART2 */
USART_Write(TINY_COM1,txDataBufUSART1,DATA_BUF_SIZE);
while(rxCountUSART2 != DATA_BUF_SIZE);
/** Verify data */
state = BufferCompare(rxDataBufUSART2, txDataBufUSART1, DATA_BUF_SIZE);
/** Data is ok then turn off LED2 */
if(state == TRUE){ APM_TINY_LEDOn(LED2);}
/** Send Data from USART2 to USART1 */
USART_Write(TINY_COM2,txDataBufUSART2,DATA_BUF_SIZE);
while(rxCountUSART1 != DATA_BUF_SIZE);
/** Verify data */
state = BufferCompare(rxDataBufUSART1, txDataBufUSART2, DATA_BUF_SIZE);
/** Data is ok then turn off LED2 */
if(state == TRUE){ APM_TINY_LEDOn(LED3);}
delayXms(1000); APM_TINY_LEDOff(LED2); APM_TINY_LEDOff(LED3); delayXms(1000);
}
}
void USART_Init(void){
USART_Config_T usartConfigStruct;
usartConfigStruct.baudRate = 115200;
usartConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
usartConfigStruct.mode = USART_MODE_TX_RX;
usartConfigStruct.parity = USART_PARITY_NONE;
usartConfigStruct.stopBits = USART_STOP_BIT_1;
usartConfigStruct.wordLength = USART_WORD_LEN_8B;
APM_TINY_COMInit(COM1, &usartConfigStruct);
APM_TINY_COMInit(COM2, &usartConfigStruct);
Delay(0x7FFF);
/** Enable USART1 RXBNE interrput */
USART_EnableInterrupt(TINY_COM1, USART_INT_RXBNE);
USART_ClearStatusFlag(TINY_COM1, USART_FLAG_RXBNE);
NVIC_EnableIRQRequest(TINY_COM1_IRQn,1,0);
/** Enable USART2 RXBNE interrput */
USART_EnableInterrupt(TINY_COM2, USART_INT_RXBNE);
USART_ClearStatusFlag(TINY_COM2, USART_FLAG_RXBNE);
NVIC_EnableIRQRequest(TINY_COM2_IRQn,2,0);
}
void USART_Receive_Isr(void){
/** USART1 Recieve Data */
if(USART_ReadStatusFlag(TINY_COM1, USART_FLAG_RXBNE) == SET){
rxDataBufUSART1[rxCountUSART1++] = (uint8_t)USART_RxData(TINY_COM1);
}
/** USART2 Recieve Data */
if(USART_ReadStatusFlag(TINY_COM2, USART_FLAG_RXBNE) == SET){
rxDataBufUSART2[rxCountUSART2++] = (uint8_t)USART_RxData(TINY_COM2);
}
}
BOOL BufferCompare(uint8_t *buf1, uint8_t *buf2, uint8_t size){
uint8_t i;
for(i = 0; i < size; i++){
if(buf1[i] != buf2[i]){
return FALSE;
}
}
return TRUE;
}
void USART_Write(USART_T* usart,uint8_t *dat, uint32_t count){
while(count--)
{
while(USART_ReadStatusFlag(usart, USART_FLAG_TXBE) == RESET);
USART_TxData(usart, *dat++);
}
}
void Delay(uint32_t count){
volatile uint32_t delay = count;
while(delay--);
}
3,接线串口2和3
4,编译运行,效果如下:
2
|