0% found this document useful (0 votes)
15 views

Lab4_Cau1

Uploaded by

Minh Thư
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab4_Cau1

Uploaded by

Minh Thư
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

/***************************************************************************//**

* @file main.c
* @brief main() function.
*******************************************************************************
* # License
* <b>Copyright 2020 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
#include "sl_component_catalog.h"
#include "sl_system_init.h"
#include "app.h"
#if defined(SL_CATALOG_POWER_MANAGER_PRESENT)
#include "sl_power_manager.h"
#endif
#if defined(SL_CATALOG_KERNEL_PRESENT)
#include "sl_system_kernel.h"
#else // SL_CATALOG_KERNEL_PRESENT
#include "sl_system_process_action.h"
#endif // SL_CATALOG_KERNEL_PRESENT

#include "em_chip.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_usart.h"
#include "em_ldma.h"
#include "em_emu.h"

/**************************************************************************//**
* DEFINE
*****************************************************************************/

#define BSP_TXPORT gpioPortA


#define BSP_RXPORT gpioPortA
#define BSP_TXPIN 5
#define BSP_RXPIN 6
#define BSP_ENABLE_PORT gpioPortD
#define BSP_ENABLE_PIN 4
// LDMA channel for receive and transmit servicing
#define RX_LDMA_CHANNEL 0
#define TX_LDMA_CHANNEL 1

/**************************************************************************//**
* STATIC VARIABLES
*****************************************************************************/
// LDMA descriptor and transfer configuration structures for USART TX channel
LDMA_Descriptor_t ldmaTXDescriptor;
LDMA_TransferCfg_t ldmaTXConfig;

// LDMA descriptor and transfer configuration structures for USART RX channel


LDMA_Descriptor_t ldmaRXDescriptor;
LDMA_TransferCfg_t ldmaRXConfig;

// Size of the data buffers


#define BUFLEN 25

// Outgoing data
uint8_t outbuf[BUFLEN];

// Incoming data
uint8_t inbuf[BUFLEN];

// Data reception complete


bool rx_done;

/**************************************************************************//**
* @brief
* GPIO initialization
*****************************************************************************/
void initGPIO(void)
{
// Configure the USART TX pin to the board controller as an output
GPIO_PinModeSet(BSP_TXPORT, BSP_TXPIN, gpioModePushPull, 1);

// Configure the USART RX pin to the board controller as an input


GPIO_PinModeSet(BSP_RXPORT, BSP_RXPIN, gpioModeInput, 0);

/*
* Configure the BCC_ENABLE pin as output and set high. This enables
* the virtual COM port (VCOM) connection to the board controller and
* permits serial port traffic over the debug connection to the host
* PC.
*
* To disable the VCOM connection and use the pins on the kit
* expansion (EXP) header, comment out the following line.
*/
GPIO_PinModeSet(BSP_ENABLE_PORT, BSP_ENABLE_PIN, gpioModePushPull, 1);
}
/**************************************************************************//**
* @brief
* USART0 initialization
*****************************************************************************/
void initUSART0(void)
{
// Default asynchronous initializer (115.2 Kbps, 8N1, no flow control)
USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT;

// Route USART0 TX and RX to the board controller TX and RX pins


GPIO->USARTROUTE[0].TXROUTE = (BSP_TXPORT << _GPIO_USART_TXROUTE_PORT_SHIFT)
| (BSP_TXPIN << _GPIO_USART_TXROUTE_PIN_SHIFT);
GPIO->USARTROUTE[0].RXROUTE = (BSP_RXPORT << _GPIO_USART_RXROUTE_PORT_SHIFT)
| (BSP_RXPIN << _GPIO_USART_RXROUTE_PIN_SHIFT);

// Enable RX and TX signals now that they have been routed


GPIO->USARTROUTE[0].ROUTEEN = GPIO_USART_ROUTEEN_RXPEN |
GPIO_USART_ROUTEEN_TXPEN;

// Configure and enable USART0


USART_InitAsync(USART0, &init);
}
void initLDMA(void)
{
// First, initialize the LDMA unit itself
LDMA_Init_t ldmaInit = LDMA_INIT_DEFAULT;
LDMA_Init(&ldmaInit);

// Source is outbuf, destination is USART0_TXDATA, and length is BUFLEN


ldmaTXDescriptor = (LDMA_Descriptor_t)LDMA_DESCRIPTOR_SINGLE_M2P_BYTE(outbuf,
&(USART0->TXDATA), BUFLEN);

ldmaTXDescriptor.xfer.blockSize = ldmaCtrlBlockSizeUnit1;

ldmaTXConfig =
(LDMA_TransferCfg_t)LDMA_TRANSFER_CFG_PERIPHERAL(ldmaPeripheralSignal_USART0_TXBL);

// Source is USART0_RXDATA, destination is inbuf, and length is BUFLEN


ldmaRXDescriptor = (LDMA_Descriptor_t)LDMA_DESCRIPTOR_SINGLE_P2M_BYTE(&(USART0-
>RXDATA), inbuf, BUFLEN);

ldmaRXDescriptor.xfer.blockSize = ldmaCtrlBlockSizeUnit1;

ldmaRXConfig =
(LDMA_TransferCfg_t)LDMA_TRANSFER_CFG_PERIPHERAL(ldmaPeripheralSignal_USART0_RXDATA
V);
}

void LDMA_IRQHandler()
{
uint32_t flags = LDMA_IntGet();

// Clear the transmit channel's done flag if set


if (flags & (1 << TX_LDMA_CHANNEL)){
LDMA_IntClear(1 << TX_LDMA_CHANNEL);
}

if (flags & (1 << RX_LDMA_CHANNEL)){


LDMA_IntClear(1 << RX_LDMA_CHANNEL);
rx_done = true;
}
}
void countCharacters(uint8_t *buffer, uint32_t length, char *result) {
int freq[256] = {0}; // Lưu số lần xuất hiện của mỗi ký tự ASCII

// Đếm số lần xuất hiện của từng ký tự


for (uint32_t i = 0; i < length; i++) {
freq[buffer[i]]++;
}

// Tìm ký tự xuất hiện nhiều nhất


char maxChar = 0;
int maxCount = 0;
for (int i = 0; i < 256; i++) {
if (freq[i] > maxCount) {
maxCount = freq[i];
maxChar = (char)i;
}
}
// Trả kết quả dạng "ký tự: số lần"
sprintf(result, " %c: %d ", maxChar, maxCount);
}

int main(void)
{
sl_system_init();
app_init();

#if defined(SL_CATALOG_KERNEL_PRESENT)
sl_system_kernel_start();
#else // SL_CATALOG_KERNEL_PRESENT

initGPIO();
initUSART0();
initLDMA();

while (1) {
sl_system_process_action();
app_process_action();

rx_done = false;

LDMA_StartTransfer(RX_LDMA_CHANNEL, &ldmaRXConfig, &ldmaRXDescriptor);

while (!rx_done){
EMU_EnterEM1();
}

for (uint32_t i = 0; i < BUFLEN; i++){


USART_Tx(USART0, inbuf[i]);
outbuf[i] = inbuf[i];
}
USART_Tx(USART0, ' ');
// Xử lý dữ liệu nhận được
char result[50];
countCharacters(inbuf, BUFLEN, result);

// Chuẩn bị phản hồi


strncpy((char *)outbuf, result, BUFLEN);

LDMA_StartTransfer(TX_LDMA_CHANNEL, &ldmaTXConfig, &ldmaTXDescriptor);

#if defined(SL_CATALOG_POWER_MANAGER_PRESENT)
// Let the CPU go to sleep if the system allows it.
sl_power_manager_sleep();
#endif
}
#endif // SL_CATALOG_KERNEL_PRESENT
}

You might also like