Lab2 DeviceNetwork
Lab2 DeviceNetwork
LAB 2
The code is added in between /* USER CODE BEGIN WHILE*/ and /* USER CODE END
WHILE*/. Insert this code in between:
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == 0) //Button for slave B
{
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == 0) {}
sprintf((char*)SPI1_TxBuff, "A hello B\n");
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1, SPI1_TxBuff, SPI1_RxBuff, sizeofBuff, 100);
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
state = 1;
}
else if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) == 0)// Button for slave C
{
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0) {}
sprintf((char*)SPI1_TxBuff, "A hello C\n");
HAL_GPIO_WritePin(CS2_GPIO_Port, CS2_Pin, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1, SPI1_TxBuff, SPI1_RxBuff, sizeofBuff, 100);
HAL_GPIO_WritePin(CS2_GPIO_Port, CS2_Pin, GPIO_PIN_SET);
state = 2;
}
2. Slave B and C
The code is added in between /* USER CODE BEGIN Includes */ and /* USER CODE END
Includes */. Insert this code in between:
#include <stdio.h>
#define sizeofBuff 12
uint8_t u8_SPI1_TxBuff[sizeofBuff] = "B hello A\n"; // Change to "C hello A" for Slave C
uint8_t u8_SPI1_RxBuff[sizeofBuff];
The code is added in between /* USER CODE BEGIN 0*/ and /* USER CODE END 0*/. Insert
this code in between:
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hspi);
if(hspi->Instance == SPI1)
{
HAL_SPI_TransmitReceive_IT(&hspi1, u8_SPI2_TxBuff, u8_SPI2_RxBuff, sizeofBuff);
}
sprintf((char*)u8_SPI2_TxBuff, "B hello A\n");
}
The code is added in between /* USER CODE BEGIN 2*/ and /* USER CODE END 2*/. Insert
this code in between:
HAL_SPI_TransmitReceive_IT(&hspi1, u8_SPI2_TxBuff, u8_SPI2_RxBuff, sizeofBuff);
VI. Results
1. Testing Plan
• Button Functionality Test
Pressing Button 1 on the Master sends the message "A hello B" to Slave B, and
pressing Button 2 sends "A hello C" to Slave C, with each button triggering the correct
message to the respective slave.
• Slave Response Test
Slave B responds with "B hello A" only upon receiving "A hello B," and Slave C
responds with "C hello A" only when it receives "A hello C," ensuring accurate and
specific responses from each slave.
• SPI Data Integrity Check
A logic analyzer monitors SPI communication between the Master and Slaves,
confirming that all exchanged messages are complete and free from corruption.
• Sequential Communication Test
Pressing the Master’s buttons sequentially triggers communications one after another,
with the Master handling each transaction in turn without conflicts.
• Idle State Test
When the system is powered on without pressing any buttons, no SPI communication
occurs until a button is pressed, ensuring the system remains idle as expected.
• Error Handling Test
Simulating unexpected or malformed messages, the Slaves ignore unknown
messages and do not respond incorrectly, maintaining robust error handling.
• Simultaneous Press Test
Pressing both buttons simultaneously (if feasible) allows observation of the Master’s
ability to handle message collisions or queue them properly without errors.
• Response Timing Test
The time between sending a message and receiving a response is measured,
confirming that communication completes within an acceptable timeframe, such as
under 100ms.
2. Result of Debug