void monitoring_pga(void) { int i = 3; char isResetPga = 0; if(driver_stat.mut) return; driver_stat.mut = 1; GPIO_WriteBit(GPIOD, GPIO_Pin_15, Bit_RESET); for(; i >= 0; i--) { while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData16(SPI2, driver_stat.gain[i]); while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET); if((driver_stat.rec_message[i] = SPI_I2S_ReceiveData16(SPI2)) != driver_stat.gain[i]) { isResetPga = 1; } } while(SPI_GetTransmissionFIFOStatus(SPI2) != SPI_TransmissionFIFOStatus_Empty); while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET); GPIO_WriteBit(GPIOD, GPIO_Pin_15, Bit_SET); if(isResetPga) { GPIO_WriteBit(GPIOE, GPIO_Pin_12, Bit_SET); driver_stat.is_overload = 1; } driver_stat.mut = 0; return; }
/** * Return true if the bus is currently in the middle of a transmission. */ bool spiIsBusBusy(SPI_TypeDef *instance) { #ifdef STM32F303xC return SPI_GetTransmissionFIFOStatus(instance) != SPI_TransmissionFIFOStatus_Empty || SPI_I2S_GetFlagStatus(instance, SPI_I2S_FLAG_BSY) == SET; #else return SPI_I2S_GetFlagStatus(instance, SPI_I2S_FLAG_TXE) == RESET || SPI_I2S_GetFlagStatus(instance, SPI_I2S_FLAG_BSY) == SET; #endif }
void lcd_write_raw(const uint8_t *data, int length) { int i; for(i = 0; i < length; i++) { while(SPI_GetTransmissionFIFOStatus(LCD_SPI) >= SPI_TransmissionFIFOStatus_HalfFull); SPI_I2S_SendData16(LCD_SPI, data[i] | 0x100); } }
void pga2505_write(void) { int i =3; GPIO_WriteBit(GPIOD, GPIO_Pin_15, Bit_RESET); for(; i >= 0; i--) { while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData16(SPI2, driver_stat.gain[i]); while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET); SPI_I2S_ReceiveData16(SPI2); } while(SPI_GetTransmissionFIFOStatus(SPI2) != SPI_TransmissionFIFOStatus_Empty); while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET); GPIO_WriteBit(GPIOD, GPIO_Pin_15, Bit_SET); return; }
/** * @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_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* SPI configuration ------------------------------------------------------*/ SPI_Config(); /* SysTick configuration ---------------------------------------------------*/ SysTickConfig(); /* Initialize LEDs mounted on STM320518-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 STM320518-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); /* Initialize the FIFO threshold */ SPI_RxFIFOThresholdConfig(SPIx, SPI_RxFIFOThreshold_QF); /* 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; break; /* JOY_LEFT button pressed */ case JOY_LEFT: CmdTransmitted = CMD_LEFT; break; /* JOY_UP button pressed */ case JOY_UP: CmdTransmitted = CMD_UP; break; /* JOY_DOWN button pressed */ case JOY_DOWN: CmdTransmitted = CMD_DOWN; break; /* JOY_SEL button pressed */ case JOY_SEL: CmdTransmitted = CMD_SEL; 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 < DATA_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } } /* Waiting until TX FIFO is empty */ while (SPI_GetTransmissionFIFOStatus(SPIx) != SPI_TransmissionFIFOStatus_Empty) {} /* Wait busy flag */ while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET) {} /* Waiting until RX FIFO is empty */ while (SPI_GetReceptionFIFOStatus(SPIx) != SPI_ReceptionFIFOStatus_Empty) {} switch (CmdTransmitted) { /* Right button pressed */ case CMD_RIGHT: if ((Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) == 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: if ((Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) == 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: if ((Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) == 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: if ((Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) == 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: if ((Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) == 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); /* Initialize the FIFO threshold */ SPI_RxFIFOThresholdConfig(SPIx, SPI_RxFIFOThreshold_QF); /* 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_SendData8(SPIx, CMD_ACK); SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE); /* Waiting Transaction code Byte */ while (CmdStatus == 0x00) {} TimeOut = USER_TIMEOUT; while ((Rx_Idx < DATA_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } switch (CmdReceived) { /* CMD_RIGHT command received or time out*/ case CMD_RIGHT: if (Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) != 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: if (Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) != 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: if (Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) != 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: if (Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) != 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: if (Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) != FAILED) { /* Turn ON LED2, LED3 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } /* Clear the RxBuffer */ Fill_Buffer(RxBuffer, TXBUFFERSIZE); /* Waiting until TX FIFO is empty */ while (SPI_GetTransmissionFIFOStatus(SPIx) != SPI_TransmissionFIFOStatus_Empty) {} /* Wait busy flag */ while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET) {} /* Disable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); /* Waiting until RX FIFO is empty */ while (SPI_GetReceptionFIFOStatus(SPIx) != SPI_ReceptionFIFOStatus_Empty) {} } #endif /* SPI_SLAVE */ }
void lcd_write_command(uint8_t command) { while(SPI_GetTransmissionFIFOStatus(LCD_SPI) >= SPI_TransmissionFIFOStatus_HalfFull); SPI_I2S_SendData16(LCD_SPI, command); }