Part Number: TMS320F28379D
main.c内容如下:
//
// Included Files
//
#include "driverlib.h"
#include "device.h"
#include "board.h"
#include "ringpool.h"
#define SPI_disableRXFIFO(base) HWREGH(base + SPI_O_FFRX) &= ~SPI_FFRX_RXFIFORESET
#define SPI_enableRXFIFO(base) HWREGH(base + SPI_O_FFRX) |= SPI_FFRX_RXFIFORESET
__interrupt void INT_mySPI0_RX_DMA_ISR(void);
#define SPI_FRAME_LEN 21
#define PAYLOAD_LEN SPI_FRAME_LEN - 1
#pragma DATA_SECTION(rData, "ramgs0");
uint16_t rData[20];
const void *rdata = (void*)rData;
typedef enum
{
SPI_ERR_NONE = 0,
SPI_ERR_TIMEOUT,
SPI_ERR_OVERRUN,
SPI_ERR_CRC,
SPI_ERR_DMA,
SPI_ERR_FRAME,
} spi_error_t;
typedef enum
{
SPI_STATE_IDLE = 0, // 空闲
SPI_STATE_BUSY_TX, // 正在发送
SPI_STATE_BUSY_RX, // 正在接收
SPI_STATE_BUSY_TXRX, // 收发同时进行
SPI_STATE_ERROR // 错误状态
} spi_state_t;
typedef enum {
SPI_RX_FIND_HEAD1,
SPI_RX_FIND_HEAD2,
SPI_RX_PAYLOAD
} spi_rx_state_t;
typedef struct
{
volatile spi_state_t state; // 当前状态
volatile spi_error_t error; // 最近一次错误
uint16_t *tx_buf; // 发送缓冲区
ringpool_buffer_str rx_buf; // 接收环形缓冲区
uint16_t tx_len; // 发送总长度
uint16_t rx_len; // 接收总长度
uint16_t tx_cnt; // 已发送字节数
uint16_t rx_cnt; // 已接收字节数
uint32_t start_tick; // 通信开始时间(用于超时)
uint32_t timeout_ms; // 超时时间
uint16_t cs_active; // CS 是否已拉低
} spi_comm_t;
spi_comm_t spib_com;
static spi_rx_state_t rx_state = SPI_RX_FIND_HEAD1;
//
// Main
//
void main(void)
{
//
// Initialize device clock and peripherals
//
Device_init();
//
// Disable pin locks and enable internal pullups.
//
Device_initGPIO();
//
// Initialize PIE and clear PIE registers. Disables CPU interrupts.
//
Interrupt_initModule();
//
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
//
Interrupt_initVectorTable();
//
// Board initialization
//
Board_init();
ringpool_init(&spib_com.rx_buf, rData,SPI_FRAME_LEN);
spib_com.state = SPI_STATE_IDLE;
spib_com.tx_len = SPI_FRAME_LEN;
spib_com.rx_len = SPI_FRAME_LEN;
spib_com.rx_cnt = 0;
spib_com.tx_cnt = 0;
//
// Enable Global Interrupt (INTM) and realtime interrupt (DBGM)
//
EINT;
ERTM;
//
// Loop forever. Suspend or place breakpoints to observe the buffers.
//
while(1)
{
DEVICE_DELAY_US(1000);
}
}
__interrupt void INT_mySPI0_RX_DMA_ISR(void)
{
// DMA_clearInterruptStatus(mySPI0_RX_DMA_BASE);
Interrupt_clearACKGroup(INT_mySPI0_RX_DMA_INTERRUPT_ACK_GROUP);
// uint16_t raw = SPI_readDataNonBlocking(mySPI0_BASE);
uint16_t vaild_data = ((uint16_t*)rdata)[0];
vaild_data = ((uint16_t*)rdata)[1];
}
board.c中各项配置如下
/*
* Copyright (c) 2020 Texas Instruments Incorporated - http://www.ti.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "board.h"
//*****************************************************************************
//
// Board Configurations
// Initializes the rest of the modules.
// Call this function in your application if you wish to do all module
// initialization.
// If you wish to not use some of the initializations, instead of the
// Board_init use the individual Module_inits
//
//*****************************************************************************
void Board_init()
{
EALLOW;
PinMux_init();
DMA_init();
SPI_init();
INTERRUPT_init();
EDIS;
}
//*****************************************************************************
//
// PINMUX Configurations
//
//*****************************************************************************
void PinMux_init()
{
//
// PinMux for modules assigned to CPU1
//
//
// SPIB -> mySPI0 Pinmux
//
GPIO_setPinConfig(mySPI0_SPIPICO_PIN_CONFIG);
GPIO_setPadConfig(mySPI0_SPIPICO_GPIO, GPIO_PIN_TYPE_STD);
GPIO_setQualificationMode(mySPI0_SPIPICO_GPIO, GPIO_QUAL_ASYNC);
GPIO_setPinConfig(mySPI0_SPIPOCI_PIN_CONFIG);
GPIO_setPadConfig(mySPI0_SPIPOCI_GPIO, GPIO_PIN_TYPE_STD);
GPIO_setQualificationMode(mySPI0_SPIPOCI_GPIO, GPIO_QUAL_ASYNC);
GPIO_setPinConfig(mySPI0_SPICLK_PIN_CONFIG);
GPIO_setPadConfig(mySPI0_SPICLK_GPIO, GPIO_PIN_TYPE_STD);
GPIO_setQualificationMode(mySPI0_SPICLK_GPIO, GPIO_QUAL_ASYNC);
GPIO_setPinConfig(mySPI0_SPIPTE_PIN_CONFIG);
GPIO_setPadConfig(mySPI0_SPIPTE_GPIO, GPIO_PIN_TYPE_STD);
GPIO_setQualificationMode(mySPI0_SPIPTE_GPIO, GPIO_QUAL_ASYNC);
}
//*****************************************************************************
//
// DMA Configurations
//
//*****************************************************************************
void DMA_init(){
DMA_initController();
mySPI0_RX_DMA_init();
}
void mySPI0_RX_DMA_init(){
DMA_setEmulationMode(DMA_EMULATION_FREE_RUN);
DMA_configAddresses(mySPI0_RX_DMA_BASE, rdata, mySPI0_RX_DMA_ADDRESS);
DMA_configBurst(mySPI0_RX_DMA_BASE, 1U, 0, 0);
DMA_configTransfer(mySPI0_RX_DMA_BASE, 20U, 1, 1);
DMA_configWrap(mySPI0_RX_DMA_BASE, 20U, 0, 20U, 0);
DMA_configMode(mySPI0_RX_DMA_BASE, mySPI0_RX_DMA_TRIGGER, DMA_CFG_ONESHOT_DISABLE | DMA_CFG_CONTINUOUS_ENABLE | DMA_CFG_SIZE_16BIT);
DMA_setInterruptMode(mySPI0_RX_DMA_BASE, DMA_INT_AT_END);
DMA_enableInterrupt(mySPI0_RX_DMA_BASE);
DMA_disableOverrunInterrupt(mySPI0_RX_DMA_BASE);
DMA_enableTrigger(mySPI0_RX_DMA_BASE);
DMA_startChannel(mySPI0_RX_DMA_BASE);
}
//*****************************************************************************
//
// INTERRUPT Configurations
//
//*****************************************************************************
void INTERRUPT_init(){
// Interrupt Settings for INT_mySPI0_RX_DMA
// ISR need to be defined for the registered interrupts
Interrupt_register(INT_mySPI0_RX_DMA, &INT_mySPI0_RX_DMA_ISR);
Interrupt_enable(INT_mySPI0_RX_DMA);
}
//*****************************************************************************
//
// SPI Configurations
//
//*****************************************************************************
void SPI_init(){
mySPI0_init();
}
void mySPI0_init(){
SPI_disableModule(mySPI0_BASE);
SPI_setConfig(mySPI0_BASE, DEVICE_LSPCLK_FREQ, SPI_PROT_POL1PHA0,
SPI_MODE_PERIPHERAL, mySPI0_BITRATE, mySPI0_DATAWIDTH);
SPI_setPTESignalPolarity(mySPI0_BASE, SPI_PTE_ACTIVE_LOW);
SPI_enableFIFO(mySPI0_BASE);
SPI_setFIFOInterruptLevel(mySPI0_BASE, SPI_FIFO_TXEMPTY, SPI_FIFO_RX1);
SPI_clearInterruptStatus(mySPI0_BASE, SPI_INT_RXFF);
SPI_enableInterrupt(mySPI0_BASE, SPI_INT_RXFF);
SPI_disableLoopback(mySPI0_BASE);
SPI_setEmulationMode(mySPI0_BASE, SPI_EMULATION_STOP_AFTER_TRANSMIT);
SPI_enableModule(mySPI0_BASE);
}