/**************************************************************************//**
* @main_series0_G_GG_WG_LG
* @brief Demonstrates USART1 as SPI master.
* @version 0.0.1
******************************************************************************
* @section License
* <b>Copyright 2018 Silicon Labs, Inc. http://www.silabs.com</b>
*******************************************************************************
*
* This file is licensed under the Silabs License Agreement. See the file
* "Silabs_License_Agreement.txt" for details. Before using this software for
* any purpose, you must agree to the terms of that agreement.
*
******************************************************************************/
#include "em_device.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_usart.h"
#define TX_BUFFER_SIZE 4
#define RX_BUFFER_SIZE TX_BUFFER_SIZE
volatile uint32_t msTicks; /* counts 1ms timeTicks */
void Delay(uint32_t dlyTicks);
void SysTick_Handler(void)
{
msTicks++; /* increment counter necessary in Delay()*/
}
void Delay(uint32_t dlyTicks)
{
uint32_t curTicks;
curTicks = msTicks;
while ((msTicks - curTicks) < dlyTicks) ;
}
uint8_t TxBuffer[TX_BUFFER_SIZE] = {0b00000011,0b00000111,0b11111111, 0b11110000};
uint32_t TxBufferIndex = 0;
uint8_t RxBuffer[RX_BUFFER_SIZE] = {0};
uint32_t RxBufferIndex = 0;
int main(void)
{
// Initialize chip
CHIP_Init();
CMU_ClockEnable(cmuClock_GPIO, true);
CMU_ClockEnable(cmuClock_USART0, true);
GPIO_PinModeSet(gpioPortE, 12, gpioModePushPull, 1); // US1_CLK is push pull
GPIO_PinModeSet(gpioPortA, 2, gpioModePushPull, 1); // US1_CS is push pull
GPIO_PinModeSet(gpioPortE, 10, gpioModePushPull, 0); // US1_TX (MOSI) is push pull
GPIO_PinModeSet(gpioPortE, 11, gpioModeInput, 1); // US1_RX (MISO) is input
GPIO_PinModeSet(gpioPortA, 3, gpioModePushPull, 0); // LDAC
///////////////////////////////////////////////////////
// Start with default config, then modify as necessary
USART_InitSync_TypeDef config = USART_INITSYNC_DEFAULT;
config.master = true; // master mode
config.baudrate = 1000000; // CLK freq is 1 MHz
config.autoCsEnable = false; // CS pin controlled by hardware, not firmware
config.clockMode = usartClockMode0; // clock idle low, sample on rising/first edge
config.msbf = true; // send MSB first
USART_InitSync(USART0, &config);
USART0->ROUTE = USART_ROUTE_CLKPEN | USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC0;
///////////////////////////////////////////
USART_Enable(USART0, usartEnable);
TxBufferIndex = 0;
while(1)
{
Delay(1000);
GPIO_PinOutClear(gpioPortA,2);
USART_Tx(USART0, TxBuffer[TxBufferIndex++]);
GPIO_PinOutSet(gpioPortA,2);
// Stop sending once we've gone through the whole TxBuffer
if (TxBufferIndex == TX_BUFFER_SIZE)
{
TxBufferIndex = 0;
}
}
}