📄 lh7a400_ssp_driver.c
字号:
*
* Function: ssp_get_speed
*
* Purpose:
* return the SSPCLK speed in bits per second
*
* Processing:
* Read the SSP clock prescaler value from the Clock Prescaler register
* Compute the SSP clock divider value by right-justifying the upper 8
* bits of Control Register 0 and then adding 1. The SSPCLK frequency
* will be ssp_clock (roughly 7.4 MHz) divided by the prescaler and
* the divider.
*
* Parameters: None
*
* Outputs: None
*
* Returns:
* The SSPCLK speed in bits per second.
*
* Notes:
*
**********************************************************************/
INT_32 ssp_get_speed(void)
{
INT_32 ssp_prescale;
INT_32 ssp_divider;
ssp_prescale = SSP->cpsr;
ssp_divider = (SSP->cr0 & 0xff00) >> 8;
return sspclk / (ssp_prescale * (ssp_divider + 1) );
}
/**********************************************************************
*
* Function: ssp_spi_set_spo
*
* Purpose:
* For SPI mode, SSPCLK is normally high
*
* Processing:
* Set the SPO bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_spi_set_spo(void)
{
SSP->cr1 |= SSP_CR1_SPO;
}
/**********************************************************************
*
* Function: ssp_spi_clr_spo
*
* Purpose:
* For SPI mode, SSPCLK is normally low
*
* Processing:
* Clear the SPO bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_spi_clr_spo(void)
{
SSP->cr1 &= ~SSP_CR1_SPO;
}
/**********************************************************************
*
* Function: ssp_spi_set_sph
*
* Purpose:
* For SPI mode, data are sampled 1/2 SSPCLK cycle after the first SSPCLK
* transition in a frame
*
* Processing:
* Set the SPH bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_spi_set_sph(void)
{
SSP->cr1 |= SSP_CR1_SPH;
}
/**********************************************************************
*
* Function: ssp_spi_clr_sph
*
* Purpose:
* For SPI mode, data are sampled on the first SSPCLK transition in
* a frame
*
* Processing:
* Clear the SPH bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_spi_clr_sph(void)
{
SSP->cr1 &= ~SSP_CR1_SPH;
}
/**********************************************************************
*
* Function: ssp_int_enable_transmit_idle
*
* Purpose:
* enable SSP transmit idle interrupt
*
* Processing:
* Set the TXIDLE bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
/*
Interrupt control
*/
void ssp_int_enable_transmit_idle(void)
{
SSP->cr1 |= SSP_CR1_TXIDLE;
}
/**********************************************************************
*
* Function: ssp_int_disable_transmit_idle
*
* Purpose:
* disable SSP transmit idle interrupt
*
* Processing:
* Clear the TXIDLE bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_int_disable_transmit_idle(void)
{
SSP->cr1 &= ~SSP_CR1_TXIDLE;
}
/**********************************************************************
*
* Function: ssp_int_enable_receive_overflow
*
* Purpose:
* enable SSP receiver overrun interrupts
*
* Processing:
* Set the RORIE bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_int_enable_receive_overflow(void)
{
SSP->cr1 |= SSP_CR1_RORIE;
}
/**********************************************************************
*
* Function: ssp_int_disable_receive_overflow
*
* Purpose:
* disable SSP receiver overrun interrupts
*
* Processing:
* Clear the RORIE bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_int_disable_receive_overflow(void)
{
SSP->cr1 &= ~SSP_CR1_RORIE;
}
/**********************************************************************
*
* Function: ssp_int_enable_receive
*
* Purpose:
* enable SSP receiver interrupt
*
* Processing:
* Set the RIE bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_int_enable_receive(void)
{
SSP->cr1 |= SSP_CR1_RIE;
}
/**********************************************************************
*
* Function: ssp_int_disable_receive
*
* Purpose:
* disable SSP receiver interrupt
*
* Processing:
* Clear the RIE bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_int_disable_receive(void)
{
SSP->cr1 &= ~SSP_CR1_RIE;
}
/**********************************************************************
*
* Function: ssp_int_enable_transmit
*
* Purpose:
* enable SSP transmitter interrupt
*
* Processing:
* set the TIE bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_int_enable_transmit(void)
{
SSP->cr1 |= SSP_CR1_TIE;
}
/**********************************************************************
*
* Function: ssp_int_disable_transmit
*
* Purpose:
* disable SSP transmitter interrupt
*
* Processing:
* Clear the TIE bit in control register 1
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_int_disable_transmit(void)
{
SSP->cr1 &= ~SSP_CR1_TIE;
}
/**********************************************************************
*
* Function: ssp_int_disable_all
*
* Purpose:
* disable all SSP interrupts
*
* Processing:
* clear all interrupt enable bits in control register 1.
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
void ssp_int_disable_all(void)
{
SSP->cr1 &= ~(SSP_CR1_TIE |
SSP_CR1_RIE |
SSP_CR1_RORIE |
SSP_CR1_TXIDLE);
}
/**********************************************************************
*
* Function: ssp_transmit
*
* Purpose:
* write a single word to the transmitter FIFO (if FIFOs are enabled)
* or to the transmit register (if FIFOs are disabled). If the FIFO
* or transmit register are full, wait until the FIFO or register is
* available.
*
* Processing:
* Wait for space in the transmit FIFO by polling the transmit FIFO
* not full bit. When space is available, load the data into the
* data register.
*
* Parameters:
* data: the word to transmit
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
*
**********************************************************************/
/* transmit data--note that there is no timeout error */
void ssp_transmit(UNS_16 data)
{
while (!ssp_transmit_fifo_not_full() )
;
SSP->dr = data;
}
/**********************************************************************
*
* Function: ssp_transmit_block
*
* Purpose:
* transmit an array of words by repeated calls to ssp_transmit()
*
* Processing:
* If wordcount <= 0, return right away. Otherwise, wait for any
* pending transmissions to complete, then call ssp_transmit()
* with each word in the data array to keep the transmit FIFO full.
*
* Parameters:
* data: An array containing the words to be transmitted
* wordcount: The number of words to be transmitted
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
* In MICROWIRE mode, and in SPI mode with SPH==1, the SSPFRM signal
* will be asserted for the duration of the transmission of all
* wordcount words. In TI mode and in SPI mode with SPH==0, SSPFRM
* toggles for each word. See the User Guide for more information.
*
**********************************************************************/
/*
note that in SPI (w/SPH == 1) and uwire modes, a frame transmit
asserts the frame signal for the wordcount words
*/
void ssp_transmit_block(UNS_16 *data, INT_32 wordcount)
{
if (wordcount <= 0)
return;
/* wait for empty transmit fifo and port not busy */
while (!ssp_transmit_fifo_empty() )
;
while (ssp_busy() )
;
while (wordcount--)
ssp_transmit(*data++);
}
/**********************************************************************
*
* Function: ssp_receive
*
* Purpose:
* wait for a word to become available in the SSP receiver. Read the word
* and return it.
*
* Processing:
* Poll the receive FIFO not empty bit in the status register until
* the bit is set. Return the data in the SSP data registers.
*
* Parameters: None
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
* This function will wait in an infinite loop if no data is being
* transmitted to the FIFO.
*
**********************************************************************/
/* receive data */
UNS_16 ssp_receive(void)
{
while (!ssp_receive_fifo_not_empty() )
;
return SSP->dr;
}
/**********************************************************************
*
* Function: ssp_receive_block
*
* Purpose:
* Fill an array with all the words in the receive FIFO up to the
* limit specified by wordcount
*
* Processing:
* If wordcount <= 0, return 0. Otherwise, read
*
* Parameters:
* data: An array of words for holding received data
* wordcount: The number of words to be received.
*
* Outputs:
* data: the array of words read from the receive FIFO.
*
* Returns:
* The number of words actually read from the receive FIFO
*
* Notes:
*
**********************************************************************/
INT_32 ssp_receive_block(UNS_16 *data, INT_32 wordcount)
{
INT_32 words_read = 0;
if (wordcount <= 0)
return words_read;
while (ssp_receive_fifo_not_empty() && words_read < wordcount )
{
*data++ = SSP->dr;
words_read++;
}
return words_read;
}
/**********************************************************************
*
* Function: ssp_transceive
*
* Purpose:
* Send a word and wait for the data return. Return the received data.
*
* Processing:
* Use ssp_transmit() to send data out via the SSP. Return the data
* SSP_receive() returns
*
* Parameters:
* data: the data word to transmit
*
* Outputs: None
*
* Returns:
* the data word received.
*
* Notes:
*
**********************************************************************/
/* transceive data */
UNS_16 ssp_transceive(UNS_16 data)
{
ssp_transmit(data);
return ssp_receive();
}
/**********************************************************************
*
* Function: ssp_transceive_block
*
* Purpose:
* Send an array of data and record an array of returned data by
* multiple calls to ssp_transceive()
*
* Processing:
*
* Parameters:
* data_in: Array of data to be sent to the SSP transmitter
* data_out: Array of words for storing data from the SSP receiver
* wordcount: Number of data elements sent and received
*
* Outputs:
* data_out: Contains wordcount words from the SSP
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -