#include "bat32g137.h"
#include "sci.h"
#include "stdio.h"
// bref: spi发送
// para:
// note:
static void spi_send(uint8_t data)
{
SCI0->SDR02 = data;
// 等待传输结束
while(SCI0->SSR02 & _0040_SCI_UNDER_EXECUTE);
}
int main(void)
{
spi_mode_t mode = SPI_MODE_0;
// 开启外设时钟
CGC->PER0 |= CGC_PER0_SCI0EN_Msk;
// 停止通道
SCI0->ST0 |= _0004_SCI_CH2_STOP_TRG_ON;
// 分频器
SCI0->SPS0 &= ~SCI0_SPS0_PRS01_Msk;
SCI0->SPS0 |= (0 << SCI0_SPS0_PRS01_Pos);
// 设置为主模式
SCI0->SIR02 = _0004_SCI_SIRMN_FECTMN | _0002_SCI_SIRMN_PECTMN | _0001_SCI_SIRMN_OVCTMN;
// 设置为SPI模式 传输结束产生中断
SCI0->SMR02 = _0020_SMRMN_DEFAULT_VALUE | _8000_SCI_CLOCK_SELECT_CK01 | _0000_SCI_CLOCK_MODE_CKS |
_0000_SCI_TRIGGER_SOFTWARE | _0000_SCI_MODE_SPI | _0001_SCI_BUFFER_EMPTY;
// 设置SPI 极性
SCI0->SCR02 = _0004_SCRMN_DEFAULT_VALUE | _8000_SCI_TRANSMISSION | mode | _0000_SCI_INTSRE_MASK |
_0000_SCI_PARITY_NONE | _0000_SCI_MSB | _0000_SCI_STOP_NONE | _0003_SCI_LENGTH_8;
//(2+1)*2 = 6
// 48/6 = 8M
SCI0->SDR02 = 23 << 9;
/* Set output enable */
if ((mode == SPI_MODE_0) || (mode == SPI_MODE_1))
{
SCI0->SO0 &= ~_0400_SCI_CH2_CLOCK_OUTPUT_1;
}
if ((mode == SPI_MODE_2) || (mode == SPI_MODE_3))
{
SCI0->SO0 |= _0400_SCI_CH2_CLOCK_OUTPUT_1;
}
SCI0->SOE0 |= _0004_SCI_CH2_OUTPUT_ENABLE;
// /* Set P02/SDO10 pin */
PORT->PMC0 &= ~(1<<2);
PORT->P0 |= 1<<2;
PORT->PM0 &= ~(1<<2);
/* Set P03/SDI10 pin */
PORT->PMC0 &= 0xF7U;
PORT->PM0 |= 0x08U;
/* Set P04/SCK10 pin */
PORT->PMC0 &= 0xEFU;
PORT->P0 |= 0x10;
PORT->PM0 &= ~(1<<4);
//
PORT->PMC0 &= ~(1<<5);
PORT->PM0 &= ~(1<<5);
PORT->P0 &=~(1<<5);
// 开启对应的中断
/* clear INTSPI10 interrupt flag */
INTC_ClearPendingIRQ(SPI10_IRQn);
NVIC_ClearPendingIRQ(SPI10_IRQn);
/* enable INTSPI10 interrupt */
INTC_EnableIRQ(SPI10_IRQn);
NVIC_EnableIRQ(SPI10_IRQn);
// 使能通道
SCI0->SS0 |= _0004_SCI_CH2_START_TRG_ON;
while(1)
{
spi_send(0xA5);
}
}
void IRQ13_Handler(void) __attribute__((alias("spi10_interrupt")));
/***********************************************************************************************************************
* Function Name: spi10_interrupt
* Description : None
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void spi10_interrupt(void)
{
volatile uint8_t err_type;
volatile uint8_t sio_dummy;
INTC_ClearPendingIRQ(SPI10_IRQn);
err_type = (uint8_t)(SCI0->SSR02 & _0001_SCI_OVERRUN_ERROR);
SCI0->SIR02 = (uint16_t)err_type;
if (1U == err_type)
{
//spi10_callback_error(err_type); /* overrun error occurs */
}
else
{
if(PORT->P0 & (1<<5))
{
PORT->P0 &=~(1<<5);
}
else
{
PORT->P0 |=(1<<5);
}
}
}
注意设置SPI我发送模式,