STM8S/A Standard Peripherals Firmware Library: main.c Source File

STM8S/A

STM8S_StdPeriph_Examples/I2C/I2C_TwoBoards/I2C_DataExchange/Master/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    I2C/I2C_TwoBoards/DataExchange/Master/main.c
00004   * @author  MCD Application Team
00005   * @version  V2.2.0
00006   * @date     30-September-2014
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */
00027 
00028 /* Includes ------------------------------------------------------------------*/
00029 #include "stm8s.h"
00030 #include "stm8s_eval.h"
00031 #include "main.h"
00032 
00033 /** @addtogroup I2C_TwoBoards
00034   * @{
00035   */
00036 
00037 /** @addtogroup DataExchange
00038   * @{
00039   */
00040 
00041 /* Private typedef -----------------------------------------------------------*/
00042 /* Private define ------------------------------------------------------------*/
00043 /* Private macro -------------------------------------------------------------*/
00044 extern uint8_t HEADER_ADDRESS_Read = (((SLAVE_ADDRESS & 0xFF00) >> 7) | 0xF1);
00045 extern uint8_t HEADER_ADDRESS_Write;
00046 /* Private variables ---------------------------------------------------------*/
00047 __IO uint8_t Rx_Idx = 0, Tx_Idx = 0;
00048 __IO uint8_t NumByteToRead = BUFFERSIZE;
00049 __IO uint8_t NumOfBytes = BUFFERSIZE;
00050 uint8_t i = 0;
00051 __IO uint8_t RxBuffer[BUFFERSIZE];
00052 TestStatus TransferStatus1 = FAILED;
00053 extern __IO uint8_t TxBuffer[BUFFERSIZE];
00054 /* Private function prototypes -----------------------------------------------*/
00055 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00056 void Delay(__IO uint32_t nCount);
00057 /* Private functions ---------------------------------------------------------*/
00058 
00059 /**
00060   * @brief  Main program.
00061   * @param  None
00062   * @retval None
00063   */
00064 void main()
00065 {
00066 
00067 #ifdef FAST_I2C_MODE
00068   /* system_clock / 1 */
00069   CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
00070 #else
00071   /* system_clock / 2 */
00072   CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV2);
00073 #endif
00074 
00075   /* Initialize LEDs mounted on STM8/128-EVAL board */
00076   STM_EVAL_LEDInit(LED1);
00077   STM_EVAL_LEDInit(LED2);
00078   STM_EVAL_LEDInit(LED3);
00079   STM_EVAL_LEDInit(LED4);
00080         
00081         STM_EVAL_LEDOff(LED1);
00082   STM_EVAL_LEDOff(LED2);
00083   STM_EVAL_LEDOff(LED3);
00084   STM_EVAL_LEDOff(LED4);
00085 
00086 
00087   /* I2C Initialize */
00088   I2C_Init(I2C_SPEED, 0xA0, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, 16);
00089 
00090 
00091   /* Enable Buffer and Event Interrupt*/
00092   I2C_ITConfig((I2C_IT_TypeDef)(I2C_IT_EVT | I2C_IT_BUF) , ENABLE);
00093 
00094   enableInterrupts();
00095 
00096   /* TXBuffer initializtion */
00097   for (i = 0; i < BUFFERSIZE; i++)
00098     TxBuffer[i] = i;
00099 
00100   /* Send START condition */
00101   I2C_GenerateSTART(ENABLE);
00102   while (NumOfBytes);
00103   while (I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
00104 
00105   /* Add a delay to be sure that communication is finished */
00106   Delay(0xFFFF);
00107 
00108   /*****  reception phase ***/
00109   /*  Wait while the bus is busy */
00110   while (I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
00111 
00112   /* Send START condition */
00113   I2C_GenerateSTART(ENABLE);
00114 
00115   /* Test on EV5 and clear it */
00116   while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
00117 
00118 #ifdef TEN_BITS_ADDRESS
00119   /* Send Header to Slave for write */
00120   I2C_SendData(HEADER_ADDRESS_Write);
00121 
00122   /* Test on EV9 and clear it*/
00123   while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_ADDRESS10));
00124 
00125   /* Send slave Address */
00126   I2C_Send7bitAddress(SLAVE_ADDRESS, I2C_DIRECTION_TX);
00127 
00128   /* Test on EV6 and clear it */
00129   while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
00130 
00131   /* Repeated Start */
00132   I2C_GenerateSTART(ENABLE);
00133 
00134   /* Test on EV5 and clear it */
00135   while (!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
00136 
00137   /* Send Header to Slave for Read */
00138   I2C_SendData(HEADER_ADDRESS_Read);
00139 
00140   /* Test on EV6 and clear it */
00141   while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
00142 
00143 #else
00144   /* Send slave Address for write */
00145   I2C_Send7bitAddress(SLAVE_ADDRESS, I2C_DIRECTION_RX);
00146 
00147   /* Test on EV6 and clear it */
00148   while (!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
00149 #endif /* TEN_BITS_ADDRESS */
00150 
00151   /* While there is data to be read */
00152   while (NumByteToRead)
00153   {
00154 #ifdef SAFE_PROCEDURE
00155     if (NumByteToRead != 3) /* Receive bytes from first byte until byte N-3 */
00156     {
00157       while ((I2C_GetFlagStatus(I2C_FLAG_TRANSFERFINISHED) == RESET)); /* Poll on BTF */
00158 
00159       /* Read a byte from the Slave */
00160       RxBuffer[Rx_Idx] = I2C_ReceiveData();
00161 
00162       /* Point to the next location where the byte read will be saved */
00163       Rx_Idx++;
00164 
00165       /* Decrement the read bytes counter */
00166       NumByteToRead--;
00167     }
00168 
00169     if (NumByteToRead == 3)  /* it remains to read three data: data N-2, data N-1, Data N */
00170     {
00171       /* Data N-2 in DR and data N -1 in shift register */
00172       while ((I2C_GetFlagStatus(I2C_FLAG_TRANSFERFINISHED) == RESET)); /* Poll on BTF */
00173 
00174       /* Clear ACK */
00175       I2C_AcknowledgeConfig(I2C_ACK_NONE);
00176 
00177       /* Disable general interrupts */
00178       disableInterrupts();
00179 
00180       /* Read Data N-2 */
00181       RxBuffer[Rx_Idx] = I2C_ReceiveData();
00182 
00183       /* Point to the next location where the byte read will be saved */
00184       Rx_Idx++;
00185 
00186       /* Program the STOP */
00187       I2C_GenerateSTOP(ENABLE);
00188 
00189       /* Read DataN-1 */
00190       RxBuffer[Rx_Idx] = I2C_ReceiveData();
00191 
00192       /* Enable General interrupts */
00193       enableInterrupts();
00194 
00195       /* Point to the next location where the byte read will be saved */
00196       Rx_Idx++;
00197 
00198       while ((I2C_GetFlagStatus(I2C_FLAG_RXNOTEMPTY) == RESET)); /* Poll on RxNE */
00199 
00200       /* Read DataN */
00201       RxBuffer[Rx_Idx] = I2C_ReceiveData();
00202 
00203       /* Reset the number of bytes to be read by master */
00204       NumByteToRead = 0;
00205 
00206     }
00207 #else
00208     if (NumByteToRead == 1)
00209     {
00210       /* Disable Acknowledgement */
00211       I2C_AcknowledgeConfig(I2C_ACK_NONE);
00212 
00213       /* Send STOP Condition */
00214       I2C_GenerateSTOP(ENABLE);
00215 
00216       /* Poll on RxNE Flag */
00217       while ((I2C_GetFlagStatus(I2C_FLAG_RXNOTEMPTY) == RESET));
00218       /* Read a byte from the Slave */
00219       RxBuffer[Rx_Idx] = I2C_ReceiveData();
00220 
00221       /* Point to the next location where the byte read will be saved */
00222       Rx_Idx++;
00223 
00224       /* Decrement the read bytes counter */
00225       NumByteToRead--;
00226     }
00227 
00228     /* Test on EV7 and clear it */
00229     if (I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED) )
00230     {
00231       /* Read a byte from the EEPROM */
00232       RxBuffer[Rx_Idx] = I2C_ReceiveData();
00233 
00234       /* Point to the next location where the byte read will be saved */
00235       Rx_Idx++;
00236 
00237       /* Decrement the read bytes counter */
00238       NumByteToRead--;
00239         }
00240 #endif /* SAFE_PROCEDURE */
00241   }
00242 
00243   /* check if sent and received data are not corrupted */
00244   TransferStatus1 = Buffercmp((uint8_t*)TxBuffer, (uint8_t*) RxBuffer, BUFFERSIZE);
00245 
00246   if (TransferStatus1 != FAILED)
00247   {
00248     while (1)
00249     {
00250       /* Toggle LED1*/
00251       STM_EVAL_LEDToggle(LED1);
00252       /* Insert delay */
00253       Delay(0x7FFF);
00254     }
00255   }
00256   else
00257   {
00258     while (1)
00259     {
00260       /* Toggle LED4*/
00261       STM_EVAL_LEDToggle(LED4);
00262       /* Insert delay */
00263       Delay(0x7FFF);
00264     }
00265   }
00266 }
00267 
00268 /**
00269   * @brief  Compares two buffers.
00270   * @param  pBuffer1, pBuffer2: buffers to be compared.
00271   * @param  BufferLength: buffer's length
00272   * @retval PASSED: pBuffer1 identical to pBuffer2
00273   *   FAILED: pBuffer1 differs from pBuffer2
00274   */
00275 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00276 {
00277   while (BufferLength--)
00278   {
00279     if (*pBuffer1 != *pBuffer2)
00280     {
00281       return FAILED;
00282     }
00283 
00284     pBuffer1++;
00285     pBuffer2++;
00286   }
00287 
00288   return PASSED;
00289 }
00290 
00291 /**
00292   * @brief  Inserts a delay time.
00293   * @param  nCount: specifies the delay time length.
00294   * @retval None
00295   */
00296 void Delay(__IO uint32_t nCount)
00297 {
00298   for (; nCount != 0; nCount--);
00299 }
00300 
00301 #ifdef  USE_FULL_ASSERT
00302 /**
00303   * @brief  Reports the name of the source file and the source line number
00304   *   where the assert_param error has occurred.
00305   * @param  file: pointer to the source file name
00306   * @param  line: assert_param error line source number
00307   * @retval None
00308   */
00309 void assert_failed(uint8_t* file, uint32_t line)
00310 {
00311   /* User can add his own implementation to report the file name and line number,
00312      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00313 
00314   /* Infinite loop */
00315   while (1)
00316   {}
00317 }
00318 #endif
00319 
00320 /**
00321   * @}
00322   */
00323 
00324 /**
00325   * @}
00326   */
00327 
00328 
00329 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com