STM8S/A Standard Peripherals Firmware Library
|
STM8S_StdPeriph_Examples/SPI/SPI_FullDuplexUART1/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file SPI_FullDuplexUART1\main.c 00004 * @author MCD Application Team 00005 * @version V2.2.0 00006 * @date 30-September-2014 00007 * @brief This file contains the main function for SPI full duplex 00008 * communication with UART1 example. 00009 ****************************************************************************** 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00013 * 00014 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00015 * You may not use this file except in compliance with the License. 00016 * You may obtain a copy of the License at: 00017 * 00018 * http://www.st.com/software_license_agreement_liberty_v2 00019 * 00020 * Unless required by applicable law or agreed to in writing, software 00021 * distributed under the License is distributed on an "AS IS" BASIS, 00022 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00023 * See the License for the specific language governing permissions and 00024 * limitations under the License. 00025 * 00026 ****************************************************************************** 00027 */ 00028 00029 /* Includes ------------------------------------------------------------------*/ 00030 #include "stm8s.h" 00031 00032 /** 00033 * @addtogroup SPI_FullDuplexUART1 00034 * @{ 00035 */ 00036 00037 /* Private typedef -----------------------------------------------------------*/ 00038 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; 00039 00040 /* Private define ------------------------------------------------------------*/ 00041 #define TxBufferSize1 (countof(TxBuffer1) - 1) 00042 #define TxBufferSize2 (countof(TxBuffer2) - 1) 00043 00044 /* Private macro -------------------------------------------------------------*/ 00045 #define countof(a) (sizeof(a) / sizeof(*(a))) 00046 00047 /* Private variables ---------------------------------------------------------*/ 00048 00049 uint8_t TxBuffer1[] = "UART1 Example: UART1 -> SPI using TXE and RXNE Flags"; 00050 uint8_t TxBuffer2[] = "UART1 Example: SPI -> UART1 using TXE and RXNE Flags"; 00051 uint8_t RxBuffer1[TxBufferSize2] = {0}; 00052 uint8_t RxBuffer2[TxBufferSize1] = {0}; 00053 uint8_t NbrOfDataToRead = TxBufferSize1; 00054 __IO uint8_t TxCounter = 0, RxCounter = 0; 00055 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED; 00056 00057 /* Private function prototypes -----------------------------------------------*/ 00058 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength); 00059 void Delay (uint16_t nCount); 00060 /* Private functions ----------------------------------------------------------*/ 00061 00062 /** 00063 * @brief Main program. 00064 * @param None 00065 * @retval None 00066 */ 00067 void main(void) 00068 { 00069 /*High speed internal clock prescaler: 1*/ 00070 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); 00071 00072 /* UART1 configuration -------------------------------------------------------*/ 00073 /* UART1 configured as follow: 00074 - Word Length = 8 Bits 00075 - 1 Stop Bit 00076 - No parity 00077 - BaudRate = 9600 baud 00078 - UART1 Clock enabled 00079 - Polarity Low 00080 - Phase Middle 00081 - Last Bit enabled 00082 - Receive and transmit enabled 00083 */ 00084 UART1_DeInit(); 00085 00086 UART1_Init((uint32_t)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, 00087 (UART1_SyncMode_TypeDef)(UART1_SYNCMODE_CLOCK_ENABLE | UART1_SYNCMODE_CPOL_LOW |UART1_SYNCMODE_CPHA_MIDDLE |UART1_SYNCMODE_LASTBIT_ENABLE), 00088 UART1_MODE_TXRX_ENABLE); 00089 UART1_Cmd(DISABLE); 00090 00091 /* SPI configuration */ 00092 SPI_DeInit(); 00093 /* Initialize SPI in Slave mode */ 00094 SPI_Init(SPI_FIRSTBIT_LSB, SPI_BAUDRATEPRESCALER_2, SPI_MODE_SLAVE, SPI_CLOCKPOLARITY_LOW, 00095 SPI_CLOCKPHASE_1EDGE, SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT,(uint8_t)0x07); 00096 00097 /* Enable the UART1*/ 00098 UART1_Cmd(ENABLE); 00099 00100 Delay(0xFFF); 00101 00102 /* Enable the SPI*/ 00103 SPI_Cmd(ENABLE); 00104 00105 while (NbrOfDataToRead--) 00106 { 00107 /* Wait until end of transmit */ 00108 while (SPI_GetFlagStatus(SPI_FLAG_TXE)== RESET) 00109 { 00110 } 00111 /* Write one byte in the SPI Transmit Data Register */ 00112 SPI_SendData(TxBuffer2[TxCounter]); 00113 /* Write one byte in the UART1 Transmit Data Register */ 00114 UART1_SendData8(TxBuffer1[TxCounter++]); 00115 /* Wait until end of transmit */ 00116 while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET) 00117 { 00118 } 00119 /* Wait the byte is entirely received by UART1 */ 00120 while (UART1_GetFlagStatus(UART1_FLAG_RXNE) == RESET) 00121 { 00122 } 00123 /* Store the received byte in the RxBuffer1 */ 00124 RxBuffer1[RxCounter] = UART1_ReceiveData8(); 00125 /* Wait the byte is entirely received by SPI */ 00126 while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET) 00127 { 00128 } 00129 /* Store the received byte in the RxBuffer2 */ 00130 RxBuffer2[RxCounter++] = SPI_ReceiveData(); 00131 } 00132 00133 /* Check the received data with the sent ones */ 00134 TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1); 00135 /* TransferStatus = PASSED, if the data transmitted from UART1 and received by SPI are the same */ 00136 /* TransferStatus = FAILED, if the data transmitted from UART1 and received by SPI are different */ 00137 TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2); 00138 /* TransferStatus = PASSED, if the data transmitted from SPI and received by UART1 are the same */ 00139 /* TransferStatus = FAILED, if the data transmitted from SPI and received by UART11 are different */ 00140 00141 while (1); 00142 } 00143 /** 00144 * @brief Compares two buffers. 00145 * @param pBuffer1 First buffer to be compared. 00146 * @param pBuffer2 Second buffer to be compared. 00147 * @param BufferLength Buffer's length 00148 * @retval TestStatus Status of buffer comparison 00149 * - PASSED: pBuffer1 identical to pBuffer2 00150 * - FAILED: pBuffer1 differs from pBuffer2 00151 */ 00152 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength) 00153 { 00154 while (BufferLength--) 00155 { 00156 if (*pBuffer1 != *pBuffer2) 00157 { 00158 return FAILED; 00159 } 00160 00161 pBuffer1++; 00162 pBuffer2++; 00163 } 00164 00165 return PASSED; 00166 } 00167 00168 /** 00169 * @brief Delay. 00170 * @param nCount 00171 * @retval None 00172 */ 00173 void Delay(uint16_t nCount) 00174 { 00175 /* Decrement nCount value */ 00176 while (nCount != 0) 00177 { 00178 nCount--; 00179 } 00180 } 00181 00182 #ifdef USE_FULL_ASSERT 00183 00184 /** 00185 * @brief Reports the name of the source file and the source line number 00186 * where the assert_param error has occurred. 00187 * @param file: pointer to the source file name 00188 * @param line: assert_param error line source number 00189 * @retval None 00190 */ 00191 void assert_failed(uint8_t* file, uint32_t line) 00192 { 00193 /* User can add his own implementation to report the file name and line number, 00194 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00195 00196 /* Infinite loop */ 00197 while (1) 00198 { 00199 } 00200 } 00201 #endif 00202 00203 /** 00204 * @} 00205 */ 00206 00207 00208 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/