/** * @brief This function handles SPI interrupt request. * @param None * @retval None */ void SPIx_IRQHANDLER(void) { #ifdef SPI_SLAVE /* SPI in Slave Receiver mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_RXNE) == SET) { RxBuffer[Rx_Idx++] = SPI_I2S_ReceiveData(SPIx); } #endif #ifdef SPI_MASTER /* SPI in Master Tramitter mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_TXE) == SET) { if (CmdStatus == 0x00) { /* Send Transaction code */ SPI_I2S_SendData(SPIx, CmdTransmitted); CmdStatus = 0x01; } else { if (Tx_Idx < GetVar_NbrOfData()) { /* Send Transaction data */ SPI_I2S_SendData(SPIx, TxBuffer[Tx_Idx++]); } else { /* Disable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); } } } #endif /* SPI_SLAVE */ }
/** * @brief This function handles USRAT interrupt request. * @param None * @retval None */ void USARTx_IRQHandler(void) { /* USART in mode Tramitter -------------------------------------------------*/ if (USART_GetITStatus(USARTx, USART_IT_TXE) == SET) { /* When Joystick Pressed send the command then send the data */ if (UsartMode == USART_MODE_TRANSMITTER) { /* Send the command */ if (UsartTransactionType == USART_TRANSACTIONTYPE_CMD) { USART_SendData(USARTx, CmdBuffer[TxIndex++]); if (TxIndex == 0x02) { /* Disable the USARTx transmit data register empty interrupt */ USART_ITConfig(USARTx, USART_IT_TXE, DISABLE); } } /* Send the data */ else { USART_SendData(USARTx, TxBuffer[TxIndex++]); if (TxIndex == GetVar_NbrOfData()) { /* Disable the USARTx transmit data register empty interrupt */ USART_ITConfig(USARTx, USART_IT_TXE, DISABLE); } } } /*If Data Received send the ACK*/ else { USART_SendData(USARTx, AckBuffer[TxIndex++]); if (TxIndex == 0x02) { /* Disable the USARTx transmit data register empty interrupt */ USART_ITConfig(USARTx, USART_IT_TXE, DISABLE); } } } /* USART in mode Receiver --------------------------------------------------*/ if (USART_GetITStatus(USARTx, USART_IT_RXNE) == SET) { if (UsartMode == USART_MODE_TRANSMITTER) { AckBuffer[RxIndex++] = USART_ReceiveData(USARTx); } else { /* Receive the command */ if (UsartTransactionType == USART_TRANSACTIONTYPE_CMD) { CmdBuffer[RxIndex++] = USART_ReceiveData(USARTx); } /* Receive the USART data */ else { RxBuffer[RxIndex++] = USART_ReceiveData(USARTx); } } } }
/** * @brief This function handles SPI interrupt request. * @param None * @retval None */ void SPI3_IRQHandler(void) { #if defined (SPI_SLAVE) /* SPI in Slave Tramitter mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_TXE) == SET) { SPI_SendData8(SPIx, TxBuffer[Tx_Idx++]); if (Tx_Idx == GetVar_NbrOfData()) { /* Disable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); } } /* SPI in Slave Receiver mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_RXNE) == SET) { if (CmdReceived == 0x00) { CmdReceived = SPI_ReceiveData8(SPIx); CmdStatus = 0x01; } else { RxBuffer[Rx_Idx++] = SPI_ReceiveData8(SPIx); } } /* SPI Error interrupt--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_OVR) == SET) { SPI_ReceiveData8(SPIx); SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_OVR); } #endif /* SPI_SLAVE*/ #if defined (SPI_MASTER) /* SPI in Master Tramitter mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_TXE) == SET) { if (CmdStatus == 0x00) { SPI_SendData8(SPIx, CmdTransmitted); CmdStatus = 0x01; } else { SPI_SendData8(SPIx, TxBuffer[Tx_Idx++]); if (Tx_Idx == GetVar_NbrOfData()) { /* Disable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); } } } /* SPI in Master Receiver mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_RXNE) == SET) { if (CmdReceived == 0x00) { CmdReceived = SPI_ReceiveData8(SPIx); Rx_Idx = 0x00; } else { RxBuffer[Rx_Idx++] = SPI_ReceiveData8(SPIx); } } /* SPI Error interrupt--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_OVR) == SET) { SPI_ReceiveData8(SPIx); SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_OVR); } #endif /* SPI_MASTER*/ }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32l1xx_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ /* SPI configuration ------------------------------------------------------*/ SPI_Config(); /* SysTick configuration ---------------------------------------------------*/ SysTickConfig(); /* Initialize LEDs mounted on STM32L152-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Master board configuration ------------------------------------------------*/ #ifdef SPI_MASTER /* Initialize push-buttons mounted on STM32L152-EVAL board */ STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_GPIO); /* Initializes the SPI communication */ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPIx, &SPI_InitStructure); /* Enable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE); /* Enable the SPI Error interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_ERR, ENABLE); /* Data transfer is performed in the SPI interrupt routine */ /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); while (1) { CmdTransmitted = 0x00; CmdReceived = 0x00; CmdStatus = 0x00; Tx_Idx = 0x00; Rx_Idx = 0x00; /* Clear the RxBuffer */ Fill_Buffer(RxBuffer, TXBUFFERSIZE); PressedButton = Read_Joystick(); while (PressedButton == JOY_NONE) { PressedButton = Read_Joystick(); } switch (PressedButton) { /* JOY_RIGHT button pressed */ case JOY_RIGHT: CmdTransmitted = CMD_RIGHT; NumberOfByte = CMD_RIGHT_SIZE; break; /* JOY_LEFT button pressed */ case JOY_LEFT: CmdTransmitted = CMD_LEFT; NumberOfByte = CMD_LEFT_SIZE; break; /* JOY_UP button pressed */ case JOY_UP: CmdTransmitted = CMD_UP; NumberOfByte = CMD_UP_SIZE; break; /* JOY_DOWN button pressed */ case JOY_DOWN: CmdTransmitted = CMD_DOWN; NumberOfByte = CMD_DOWN_SIZE; break; /* JOY_SEL button pressed */ case JOY_SEL: CmdTransmitted = CMD_SEL; NumberOfByte = CMD_SEL_SIZE; break; default: break; } if (CmdTransmitted != 0x00) { /* Enable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE); /* Wait until end of data transfer or time out*/ TimeOut = USER_TIMEOUT; while ((Rx_Idx < GetVar_NbrOfData())&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } } switch (Rx_Idx) { /* Right button pressed */ case CMD_RIGHT_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE) == PASSED) && (CmdReceived == CMD_ACK)) { /* Turn ON LED2 and LED3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED4); } break; /* Left button pressed*/ case CMD_LEFT_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE) == PASSED) && (CmdReceived == CMD_ACK)) { /* Turn ON LED4 */ STM_EVAL_LEDOn(LED4); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); } break; /* Up button pressed */ case CMD_UP_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE) == PASSED) && (CmdReceived == CMD_ACK)) { /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); } break; /* Down button pressed */ case CMD_DOWN_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE) == PASSED) && (CmdReceived == CMD_ACK)) { /* Turn ON LED3 */ STM_EVAL_LEDOn(LED3); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } break; /* Sel button pressed */ case CMD_SEL_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE) == PASSED) && (CmdReceived == CMD_ACK)) { /* Turn ON all LEDs */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } } #endif /* SPI_MASTER */ /* Slave board configuration ----------------------------------------------*/ #ifdef SPI_SLAVE /* Initializes the SPI communication */ SPI_I2S_DeInit(SPIx); SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_Init(SPIx, &SPI_InitStructure); /* Enable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE); /* Enable the SPI Error interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_ERR, ENABLE); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); /* Infinite Loop */ while (1) { CmdStatus = 0x00; CmdReceived = 0x00; Rx_Idx = 0x00; Tx_Idx = 0x00; /* Write the first data in SPI shift register before enabling the interrupt this data will be transmitted when the Slave receive the generated clock by the Master */ /* Enable the Tx buffer empty interrupt */ SPI_I2S_SendData(SPIx, CMD_ACK); SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE); /* Waiting Transaction code Byte */ while (CmdStatus == 0x00) {} switch (CmdReceived) { /* CMD_RIGHT command received or time out*/ case CMD_RIGHT: TimeOut = USER_TIMEOUT; while ((Rx_Idx < CMD_RIGHT_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } if (Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE) != FAILED) { /* Turn ON LED2 and LED3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn OFF LED4 */ STM_EVAL_LEDOff(LED4); } break; /* CMD_LEFT command received or time out */ case CMD_LEFT: TimeOut = USER_TIMEOUT; while ((Rx_Idx < CMD_LEFT_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } if (Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE) != FAILED) { /* Turn ON LED4 */ STM_EVAL_LEDOn(LED4); /* Turn OFF LED2 and LED3 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); } break; /* CMD_UP command received or time out*/ case CMD_UP: TimeOut = USER_TIMEOUT; while ((Rx_Idx < CMD_UP_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } if (Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE) != FAILED) { /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); /* Turn OFF LED3 and LED4 */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); } break; /* CMD_DOWN command received or time out */ case CMD_DOWN: TimeOut = USER_TIMEOUT; while ((Rx_Idx < CMD_DOWN_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } if (Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE) != FAILED) { /* Turn ON LED3 */ STM_EVAL_LEDOn(LED3); /* Turn OFF LED2 and LED4 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } break; /* CMD_SEL command received or time out */ case CMD_SEL: TimeOut = USER_TIMEOUT; while ((Rx_Idx < CMD_SEL_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } if (Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE) != FAILED) { /* Turn ON LED2, LED3 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } /* Disable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); } #endif /* SPI_SLAVE */ }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f2xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f2xx.c file */ /* USART configuration -----------------------------------------------------*/ USART_Config(); /* SysTick configuration ---------------------------------------------------*/ SysTickConfig(); /* Initialize LEDs mounted on STM322xG-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Configure the IO Expander mounted on STM322xG-EVAL board */ TimeOut = USER_TIMEOUT; while ((IOE_Config() != IOE_OK) && (TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* Enable the USARTx Receive interrupt: this interrupt is generated when the USARTx receive data register is not empty */ USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); while (1) { TxIndex = 0x00; RxIndex = 0x00; UsartTransactionType = USART_TRANSACTIONTYPE_CMD; UsartMode = USART_MODE_RECEIVER; Fill_Buffer(CmdBuffer, 0x02); Fill_Buffer(AckBuffer, 0x02); /* Clear the RxBuffer */ Fill_Buffer(RxBuffer, TXBUFFERSIZE); PressedButton = IOE_JoyStickGetState(); /* Waiting Joystick is pressed or transaction command is received */ while ((PressedButton == JOY_NONE) && (CmdBuffer[0x00] == 0x00)) { PressedButton = IOE_JoyStickGetState(); } /* If the Joystick is pressed go to transmitter mode, otherwise (the transaction command is received) go to receiver mode */ /******************************************************************************/ /* USART in Mode Transmitter */ /******************************************************************************/ if ((PressedButton != JOY_NONE) && (CmdBuffer[0x00] == 0x00)) { UsartMode = USART_MODE_TRANSMITTER; switch (PressedButton) { /* JOY_RIGHT button pressed */ case JOY_RIGHT: CmdBuffer[0x00] = CMD_RIGHT; CmdBuffer[0x01] = CMD_RIGHT_SIZE; break; /* JOY_LEFT button pressed */ case JOY_LEFT: CmdBuffer[0x00] = CMD_LEFT; CmdBuffer[0x01] = CMD_LEFT_SIZE; break; /* JOY_UP button pressed */ case JOY_UP: CmdBuffer[0x00] = CMD_UP; CmdBuffer[0x01] = CMD_UP_SIZE; break; /* JOY_DOWN button pressed */ case JOY_DOWN: CmdBuffer[0x00] = CMD_DOWN; CmdBuffer[0x01] = CMD_DOWN_SIZE; break; /* JOY_SEL button pressed */ case JOY_SEL: CmdBuffer[0x00] = CMD_SEL; CmdBuffer[0x01] = CMD_SEL_SIZE; break; default: break; } if (CmdBuffer[0x00]!= 0x00) { /* Enable the USARTx transmit data register empty interrupt */ USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); /* Wait until USART sends the command or time out */ TimeOut = USER_TIMEOUT; while ((TxIndex < 0x02)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* The software must wait until TC=1. The TC flag remains cleared during all data transfers and it is set by hardware at the last frame’s end of transmission*/ TimeOut = USER_TIMEOUT; while ((USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)&&(TimeOut != 0x00)) { } if(TimeOut == 0) { TimeOut_UserCallback(); } /* Wait until USART receives the Ack command or time out*/ TimeOut = USER_TIMEOUT; while ((RxIndex < 0x02)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* USART sends the data */ UsartTransactionType = USART_TRANSACTIONTYPE_DATA; TxIndex = 0x00; RxIndex = 0x00; /* Enable the USARTx transmit data register empty interrupt */ USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); /* Wait until end of data transfer */ TimeOut = USER_TIMEOUT; while ((TxIndex < GetVar_NbrOfData())&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* The software must wait until TC=1. The TC flag remains cleared during all data transfers and it is set by hardware at the last frame’s end of transmission*/ TimeOut = USER_TIMEOUT; while ((USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)&&(TimeOut != 0x00)) { } if(TimeOut == 0) { TimeOut_UserCallback(); } } CmdBuffer[0x00] = 0x00; } /******************************************************************************/ /* USART in Receiver Mode */ /******************************************************************************/ if (CmdBuffer[0x00] != 0x00) { /* Wait until USART receives the command or time out */ TimeOut = USER_TIMEOUT; while ((RxIndex < 0x02)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } UsartMode = USART_MODE_RECEIVER; /* Enable the USARTx transmit data register empty interrupt */ USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); /* Wait until USART sends the ACK command or time out*/ TimeOut = USER_TIMEOUT; while ((TxIndex < 0x02)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* The software must wait until TC=1. The TC flag remains cleared during all data transfers and it is set by hardware at the last frame’s end of transmission*/ TimeOut = USER_TIMEOUT; while ((USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)&&(TimeOut != 0x00)) { } if(TimeOut == 0) { TimeOut_UserCallback(); } /* USART receives the data */ UsartTransactionType = USART_TRANSACTIONTYPE_DATA; TxIndex = 0x00; RxIndex = 0x00; /* Wait until end of data transfer or time out */ TimeOut = USER_TIMEOUT; while ((RxIndex < GetVar_NbrOfData())&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } switch (CmdBuffer[0x01]) { /* CMD_RIGHT command received */ case CMD_RIGHT_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE) != FAILED) { /* Turn ON LED2 and LED3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn OFF LED4 */ STM_EVAL_LEDOff(LED4); } break; /* CMD_LEFT command received */ case CMD_LEFT_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE) != FAILED) { /* Turn ON LED4 */ STM_EVAL_LEDOn(LED4); /* Turn OFF LED2 and LED3 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); } break; /* CMD_UP command received */ case CMD_UP_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE) != FAILED) { /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); /* Turn OFF LED3 and LED4 */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); } break; /* CMD_DOWN command received */ case CMD_DOWN_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE) != FAILED) { /* Turn ON LED3 */ STM_EVAL_LEDOn(LED3); /* Turn OFF LED2 and LED4 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } break; /* CMD_SEL command received */ case CMD_SEL_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE) != FAILED) { /* Turn ON all LED2, LED3 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } CmdBuffer[0x00] = 0x00; } } }