📄 spi.c
字号:
#include <ecog.h>#include <ecog1.h>#include "mmcConf.h" // Include the SPI CS defines/******************************************************************************NAME spiTransferByteSYNOPSIS static unsigned int spiTransferByte(unsigned int mosi)FUNCTIONS Simultaneously transmits and receives one byte on the SPI.RETURNS The MISO signal that was received.******************************************************************************/unsigned int spiTransferByte(unsigned int mosi){ static unsigned int miso = 0; // Should not write/read to the _last registers in the tx/rx registers // Transmit the byte rg.dusart.a0_tx8 = mosi; /* Wait for the transaction. * The usart tx_?_ready signals arrive when the output data is transferred * from the tx register to the output shift register. This is before the * data transfer has completed, so we must wait for the rx_?_rdy status * which only occurs when all receive data bits have arrived */ while (fd.dusart.a_int_sts.tx0_rdy != 1) ; while (fd.dusart.a_int_sts.rx0_1b_rdy != 1) ; // Read the received data byte miso = rg.dusart.a0_rx8; return (miso);}/******************************************************************************NAME spiAssertCSSYNOPSIS static void spiAssertCS(void)FUNCTION Asserts the SPI chip select signal CS0 for internal use and GPIO16 for the external device chip select.RETURNS Nothing.******************************************************************************/void spiAssertCS(void){ /* The MMC/SD card daughter board uses GPIO22 for CS. * The hardware does not allow the CS signal to stay asserted * for more than one single SPI transfer, so this is done manually. * Chip select 0 must be asserted internally to enable data to be received. * There are two problems in the hardware: * 1. CS0 must be asserted to enable data reception. * 2. CS does not stay asserted across byte boundaries. */ // Enable transmit and receive rg.dusart.spi_ctrl = DUSART_SPI_CTRL_RX_EN_MASK | DUSART_SPI_CTRL_TX_EN_MASK; MMC_CS_Set = 1; fd.dusart.spi_frame_ctrl.tx_slave_sel = 1; // Assert CS0 internally LED2_Set = 1; // Turn on the LED to indicate accessing}/******************************************************************************NAME spiDeassertCSSYNOPSIS static void spiDeassertCS(void)FUNCTION Deasserts all chip selects.RETURNS Nothing.******************************************************************************/void spiDeassertCS(void){ // See comments for spiAssertCS() MMC_CS_Clr = 1; fd.dusart.spi_frame_ctrl.tx_slave_sel = 0; // Disable transmit and receive rg.dusart.spi_ctrl = DUSART_SPI_CTRL_RX_DIS_MASK | DUSART_SPI_CTRL_TX_DIS_MASK; LED2_Clr = 1; // Turn off indicator LED}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -