Lab4_Cau2
Lab4_Cau2
Lab4_Cau2
h"
#include "app.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_usart.h"
#include "em_ldma.h"
#include "em_emu.h"
#include "em_timer.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define BUFFER_SIZE_10 10
#define BUFFER_SIZE_100 100
#define BUFFER_SIZE_1000 1000
uint8_t outbuf[BUFFER_SIZE_1000];
uint8_t inbuf[1];
LDMA_Descriptor_t ldmaTXDescriptor;
LDMA_TransferCfg_t ldmaTXConfig;
volatile bool dma_done = false;
void initGPIO(void) {
GPIO_PinModeSet(BSP_TXPORT, BSP_TXPIN, gpioModePushPull, 1);
GPIO_PinModeSet(BSP_RXPORT, BSP_RXPIN, gpioModeInput, 0);
}
void initUSART0(void) {
USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT;
USART_InitAsync(USART0, &init);
}
void initLDMA(void) {
LDMA_Init_t ldmaInit = LDMA_INIT_DEFAULT;
LDMA_Init(&ldmaInit);
}
void LDMA_IRQHandler() {
uint32_t flags = LDMA_IntGet();
LDMA_IntClear(flags);
if (flags & (1 << TX_LDMA_CHANNEL)) {
dma_done = true;
}
}
void initTimer(void) {
CMU_ClockEnable(cmuClock_TIMER0, true);
TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;
TIMER_Init(TIMER0, &timerInit);
}
int main(void) {
sl_system_init();
initGPIO();
initUSART0();
initLDMA();
initTimer();
while (1) {
if (USART_StatusGet(USART0) & USART_STATUS_RXDATAV) {
inbuf[0] = USART_Rx(USART0);
uint32_t length = 0;
if (inbuf[0] == '1') {
length = BUFFER_SIZE_10;
} else if (inbuf[0] == '2') {
length = BUFFER_SIZE_100;
} else if (inbuf[0] == '3') {
length = BUFFER_SIZE_1000;
} else {
continue;
}
generateRandomData(outbuf, length);
uint32_t dmaTime = measureDMA(outbuf, length);
char result[50];
sprintf(result, "\r\nDMA Time: %lu ms\r\n", dmaTime);
startDMA((uint8_t *)result, strlen(result));
}
}
}