void next_start_code() { /* byte align */ Flush_Buffer(ld->Incnt&7); while (Show_Bits(24)!=0x01L) Flush_Buffer(8); }
void Initialize_Buffer () { ld_Incnt = 0; ld_Rdptr = ld_Rdbfr + 2048; ld_Rdmax = ld_Rdptr; ld_Bfr = 68157440; Flush_Buffer (0); /* fills valid data into bfr */ }
int Get_motion_code () { int code; if (Get_Bits1 ()) { return 0; } if ((code = Show_Bits (9)) >= 64) { code >>= 6; Flush_Buffer (MVtab0[code][1]); return Get_Bits1 ()? -MVtab0[code][0] : MVtab0[code][0]; }
void Initialize_Buffer() { ld->Incnt = 0; ld->Rdptr = ld->Rdbfr + 2048; ld->Rdmax = ld->Rdptr; #ifdef VERIFY /* only the verifier uses this particular bit counter * Bitcnt keeps track of the current parser position with respect * to the video elementary stream being decoded, regardless * of whether or not it is wrapped within a systems layer stream */ ld->Bitcnt = 0; #endif ld->Bfr = 0; Flush_Buffer(0); /* fills valid data into bfr */ }
static int Get_P_macroblock_type() { int code; #ifdef TRACE if (Trace_Flag) printf("macroblock_type(P) ("); #endif /* TRACE */ if ((code = Show_Bits(6))>=8) { code >>= 3; Flush_Buffer(PMBtab0[code].len); #ifdef TRACE if (Trace_Flag) { Print_Bits(code,3,PMBtab0[code].len); printf("): %s (%d)\n",MBdescr[PMBtab0[code].val],PMBtab0[code].val); } #endif /* TRACE */ return PMBtab0[code].val; }
int DecDCTvlc(u_short *mdec_bs,u_short *mdec_rl) { // u_short *mdec_bs = mdecbs,*mdec_rl = mdecrl u_short *rl_end; u_long bitbuf; int incnt; /* 16-有効bit数 x86=char risc = long */ int q_code; int type,n; int last_dc[3]; /* BS_HDR u_short rlsize,magic,ver,q_scale */ //printf("%04x,%04x,",mdec_bs[0],mdec_bs[1]); *(long*)mdec_rl=*(long*)mdec_bs; mdec_rl+=2; rl_end = mdec_rl+(int)mdec_bs[0]*2; q_code = (mdec_bs[2]<<10); /* code = q */ type = mdec_bs[3]; mdec_bs+=4; Init_Buffer(); n = 0; last_dc[0]=last_dc[1]=last_dc[2] = 0; while(mdec_rl<rl_end) { u_long code2; /* DC */ if (type==2) { code2 = Show_Bits(10)|(10<<16); /* DC code */ } else { code2 = Show_Bits(6); if (n>=2) { /* Y */ if (code2<48) { code2 = DC_Ytab0[code2]; code2 = (code2&0xffff0000)|((last_dc[2]+=VALOF(code2)*4)&0x3ff); } else { int nbit,val; int bit = 3; while(Show_Bits(bit)&1) { bit++;} bit++; nbit = bit*2-1; val = Show_Bits(nbit)&((1<<bit)-1); if ((val&(1<<(bit-1)))==0) val -= (1<<bit)-1; val = (last_dc[2]+=val*4); code2 = (nbit<<16) | (val&0x3ff); } //printf("%d ",last_dc[2]); } else { /* U,V */ if (code2<56) { code2 = DC_UVtab0[code2]; code2 = (code2&0xffff0000)|((last_dc[n]+=VALOF(code2)*4)&0x3ff); } else { int nbit,val; int bit = 4; while(Show_Bits(bit)&1) { bit++;} nbit = bit*2; val = Show_Bits(nbit)&((1<<bit)-1); if ((val&(1<<(bit-1)))==0) val -= (1<<bit)-1; val = (last_dc[n]+=val*4); code2 = (nbit<<16) | (val&0x3ff); } //printf("%d ",last_dc[n]); } if (++n==6) n=0; } // printf("%d ",VALOF(code2)); code2 |= q_code; /* AC */ for(;;){ // u_long code; #define code code2 #define SBIT 17 *mdec_rl++=code2; Flush_Buffer(BITOF(code2)); code = Show_Bits(SBIT); if (code>=1<<(SBIT- 2)) { code2 = VLCtabnext[(code>>12)-8]; if (code2==EOB_CODE) break; } else if (code>=1<<(SBIT- 6)) { code2 = VLCtab0[(code>>8)-8]; if (code2==ESCAPE_CODE) { Flush_Buffer(6); /* ESCAPE len */ code2 = Show_Bits(16)| (16<<16); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { uint16_t addrcmd = 0; uint16_t comlength = 0; uint8_t pAddrcmd[CMD_LENGTH] = {0x00}; uint16_t ackbyte = 0x0000; /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 168 MHz */ SystemClock_Config(); /* Configure LED5 and LED6 */ BSP_LED_Init(LED5); BSP_LED_Init(LED6); /*##-1- Configure the SPI peripheral #######################################*/ /* Set the SPI parameters */ SpiHandle.Instance = SPIx; SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; SpiHandle.Init.CLKPhase = SPI_PHASE_2EDGE; SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; SpiHandle.Init.CRCPolynomial = 7; SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; SpiHandle.Init.NSS = SPI_NSS_SOFT; SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE; SpiHandle.Init.Mode = SPI_MODE_SLAVE; if(HAL_SPI_Init(&SpiHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* enter while loop too keep treating new request from Master */ while(1) { /* Synchronization between Master and Slave */ Slave_Synchro(); /* Receive command from Master */ if(HAL_SPI_Receive(&SpiHandle, pAddrcmd, CMD_LENGTH, SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* Compute command and required data length */ addrcmd = (uint16_t) ((pAddrcmd[0] << 8) | pAddrcmd[1]); comlength = (uint16_t) ((pAddrcmd[2] << 8) | pAddrcmd[3]); /* Check if received command correct */ if(((addrcmd == ADDRCMD_MASTER_READ) || (addrcmd == ADDRCMD_MASTER_WRITE)) && (comlength > 0)) { /* Synchronization between Master and Slave */ Slave_Synchro(); /* Send acknowledge to Master */ ackbyte = SPI_ACK_BYTES; if(HAL_SPI_Transmit(&SpiHandle, (uint8_t *)&ackbyte, sizeof(ackbyte), SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* Check if Master requiring data read or write */ if(addrcmd == ADDRCMD_MASTER_READ) { /* Synchronization between Master and Slave */ Slave_Synchro(); /* Send data to Master */ if(HAL_SPI_Transmit(&SpiHandle, aTxSlaveBuffer, DATA_LENGTH, SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* Synchronization between Master and Slave */ Slave_Synchro(); /* Receive acknowledgement from Master */ ackbyte = 0; if(HAL_SPI_Receive(&SpiHandle, (uint8_t *)&ackbyte, sizeof(ackbyte), SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* Check acknowledgement */ if(ackbyte != SPI_ACK_BYTES) { Error_Handler(); } } else if(addrcmd == ADDRCMD_MASTER_WRITE) { /* Synchronization between Master and Slave */ Slave_Synchro(); /* Receive data from Master */ if(HAL_SPI_Receive(&SpiHandle, aRxBuffer, DATA_LENGTH, SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* Synchronization between Master and Slave */ Slave_Synchro(); /* Send acknowledgement to Master */ ackbyte = SPI_ACK_BYTES; if(HAL_SPI_Transmit(&SpiHandle, (uint8_t *)&ackbyte, sizeof(ackbyte), SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } /* In case, Master has sent data, compare received buffer with one expected */ if(Buffercmp((uint8_t*)aTxMasterBuffer, (uint8_t*)aRxBuffer, DATA_LENGTH)) { /* Transfer error in transmission process */ Error_Handler(); } else { /* Toggle LED6 on: Reception is correct */ BSP_LED_Toggle(LED6); } } } else { /* Synchronization between Master and Slave */ Slave_Synchro(); /* Send acknowledgement to Master */ ackbyte = SPI_NACK_BYTES; if(HAL_SPI_Transmit(&SpiHandle, (uint8_t *)&ackbyte, sizeof(ackbyte), SPI_TIMEOUT_MAX) != HAL_OK) { Error_Handler(); } Error_Handler(); } /* Flush Rx buffer for next transmission */ Flush_Buffer(aRxBuffer, DATA_LENGTH); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F103xG HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 72 MHz */ SystemClock_Config(); /* Configure LED1 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED3); /*##-1- Configure the I2C peripheral ######################################*/ I2cHandle.Instance = I2Cx; I2cHandle.Init.ClockSpeed = I2C_SPEEDCLOCK; I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE; I2cHandle.Init.OwnAddress1 = I2C_ADDRESS; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; I2cHandle.Init.OwnAddress2 = 0xFF; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if(HAL_I2C_Init(&I2cHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } #ifdef MASTER_BOARD /* Configure Key push-button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* Wait for Key push-button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != GPIO_PIN_RESET) { } /* Wait for Key push-button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != GPIO_PIN_SET) { } while(1) { /* Initialize number of data variables */ hTxNumData = TXBUFFERSIZE; hRxNumData = RXBUFFERSIZE; /* Update bTransferRequest to send buffer write request for Slave */ bTransferRequest = MASTER_REQ_WRITE; /*##-2- Master sends write request for slave #############################*/ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-3- Master sends number of data to be written ########################*/ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&hTxNumData, 2)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-4- Master sends aTxBuffer to slave ##################################*/ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Update bTransferRequest to send buffer read request for Slave */ bTransferRequest = MASTER_REQ_READ; /*##-5- Master sends read request for slave ##############################*/ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-6- Master sends number of data to be read ###########################*/ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&hRxNumData, 2)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-7- Master receives aRxBuffer from slave #############################*/ while(HAL_I2C_Master_Receive_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Check correctness of received buffer ##################################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,hRxNumData)) { /* Processing Error */ Error_Handler(); } /* Flush Rx buffers */ Flush_Buffer((uint8_t*)aRxBuffer,RXBUFFERSIZE); /* Toggle LED1 */ BSP_LED_Toggle(LED1); /* This delay permits to see LED1 toggling */ HAL_Delay(25); } #else while(1) { /* Initialize number of data variables */ hTxNumData = 0; hRxNumData = 0; /*##-2- Slave receive request from master ################################*/ while(HAL_I2C_Slave_Receive_IT(&I2cHandle, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* If master request write operation #####################################*/ if (bTransferRequest == MASTER_REQ_WRITE) { /*##-3- Slave receive number of data to be read ########################*/ while(HAL_I2C_Slave_Receive_IT(&I2cHandle, (uint8_t*)&hRxNumData, 2)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-4- Slave receives aRxBuffer from master ###########################*/ while(HAL_I2C_Slave_Receive_IT(&I2cHandle, (uint8_t*)aRxBuffer, hRxNumData)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Check correctness of received buffer ################################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,hRxNumData)) { /* Processing Error */ Error_Handler(); } /* Flush Rx buffers */ Flush_Buffer((uint8_t*)aRxBuffer,RXBUFFERSIZE); /* Toggle LED1 */ BSP_LED_Toggle(LED1); } /* If master request write operation #####################################*/ else { /*##-3- Slave receive number of data to be written #####################*/ while(HAL_I2C_Slave_Receive_IT(&I2cHandle, (uint8_t*)&hTxNumData, 2)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-4- Slave transmit aTxBuffer to master #############################*/ while(HAL_I2C_Slave_Transmit_IT(&I2cHandle, (uint8_t*)aTxBuffer, RXBUFFERSIZE)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } } } #endif /* MASTER_BOARD */ }
/** * @brief EPS I2C Task * @param argument: I2C's Handle * @retval None */ static void I2C_2_Slave_Mode(void *argument) { I2C_HandleTypeDef* I2CHandle; uint8_t priTransferDataBuffer[50]; uint8_t* default_string = "012345678996543210"; I2CHandle = (I2C_HandleTypeDef*) argument; prvNewPrintString("EPS I2C Task\n",13); for(;;) { //prvNewPrintString("Hello",5); /*##-2- Slave receive request from master ################################*/ while(HAL_I2C_Slave_Receive_IT(I2CHandle, priTransferDataBuffer, TRANSFERBUFFERSIZE)!= HAL_OK) vTaskDelay(1); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it? busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(I2CHandle) != HAL_I2C_STATE_READY) { vTaskDelay(1); //prvNewPrintString("Chuck",5); } if(priI2C_NewCommand_2==1) { priI2C_NewCommand_2 =0; //get command number and command arguments from master device I2C_transfer_slave_2.priCommand = priTransferDataBuffer[0]; I2C_transfer_slave_2.priRequestPtr = priTransferDataBuffer+1; switch(I2C_transfer_slave_2.priCommand) { case 0x08: I2C_transfer_slave_2.priErrorcode = 0; //reply data I2C_transfer_slave_2.priSizeofReply = 2+43; priTransferDataBuffer[0] = I2C_transfer_slave_2.priCommand; priTransferDataBuffer[1] = I2C_transfer_slave_2.priErrorcode; memcpy( (void *)(priTransferDataBuffer+2), (void *)&hk_test, 43); break; case 0x09: output_test.output_byte = *(I2C_transfer_slave_2.priRequestPtr); //print result //HAL_UART_Transmit(&UartHandle, &output_test, 1 , 1000); /* Flush Tx/Rx buffers */ Flush_Buffer(priTransferDataBuffer,TRANSFERBUFFERSIZE); break; default: priTransferDataBuffer[0] = 0xff; priTransferDataBuffer[1] = 0xEE; memcpy( (void *)(priTransferDataBuffer+2), (void *)default_string, 18); } } } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F2xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure LED1 and LED2 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); /* Configure the system clock to 120 MHz */ SystemClock_Config(); /*##-1- Configure the I2C peripheral #######################################*/ I2CxHandle.Instance = I2Cx; I2CxHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2CxHandle.Init.ClockSpeed = 400000; I2CxHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; I2CxHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9; I2CxHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; I2CxHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; I2CxHandle.Init.OwnAddress1 = I2C_ADDRESS; I2CxHandle.Init.OwnAddress2 = 0; if(HAL_I2C_Init(&I2CxHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } #ifdef MASTER_BOARD /* Configure User Button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* Wait for User Button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 1) { } /* Wait for User Button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 0) { } while(1) { /* Initialize number of data variables */ hTxNumData = TXBUFFERSIZE; hRxNumData = RXBUFFERSIZE; /* Update bTransferRequest to send buffer write request for Slave */ bTransferRequest = MASTER_REQ_WRITE; /*##-2- Master sends write request for slave #############################*/ while(HAL_I2C_Master_Transmit_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-3- Master sends number of data to be written ########################*/ while(HAL_I2C_Master_Transmit_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&hTxNumData, 2)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-4- Master sends aTxBuffer to slave ##################################*/ while(HAL_I2C_Master_Transmit_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /* Update bTransferRequest to send buffer read request for Slave */ bTransferRequest = MASTER_REQ_READ; /*##-5- Master sends read request for slave ##############################*/ while(HAL_I2C_Master_Transmit_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-6- Master sends number of data to be read ###########################*/ while(HAL_I2C_Master_Transmit_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&hRxNumData, 2)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-7- Master receives aRxBuffer from slave #############################*/ while(HAL_I2C_Master_Receive_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /* Check correctness of received buffer ##################################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,hRxNumData)) { /* Processing Error */ Error_Handler(); } /* Flush Rx buffers */ Flush_Buffer((uint8_t*)aRxBuffer,RXBUFFERSIZE); /* Toggle LED1 */ BSP_LED_Toggle(LED1); /* This delay permit the user to see LED1 toggling */ HAL_Delay(25); } #else while(1) { /* Initialize number of data variables */ hTxNumData = 0; hRxNumData = 0; /*##-2- Slave receive request from master ################################*/ while(HAL_I2C_Slave_Receive_IT(&I2CxHandle, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /* If master request write operation #####################################*/ if (bTransferRequest == MASTER_REQ_WRITE) { /*##-3- Slave receive number of data to be read ########################*/ while(HAL_I2C_Slave_Receive_IT(&I2CxHandle, (uint8_t*)&hRxNumData, 2)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-4- Slave receives aRxBuffer from master ###########################*/ while(HAL_I2C_Slave_Receive_IT(&I2CxHandle, (uint8_t*)aRxBuffer, hRxNumData)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /* Check correctness of received buffer ################################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,hRxNumData)) { /* Processing Error */ Error_Handler(); } /* Flush Rx buffers */ Flush_Buffer((uint8_t*)aRxBuffer,RXBUFFERSIZE); /* Toggle LED1 */ BSP_LED_Toggle(LED1); } /* If master request write operation #####################################*/ else { /*##-3- Slave receive number of data to be written #####################*/ while(HAL_I2C_Slave_Receive_IT(&I2CxHandle, (uint8_t*)&hTxNumData, 2)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-4- Slave transmit aTxBuffer to master #############################*/ while(HAL_I2C_Slave_Transmit_IT(&I2CxHandle, (uint8_t*)aTxBuffer, RXBUFFERSIZE)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } } } #endif /* MASTER_BOARD */ }