Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //My SPI setup
- hspi1.Instance = SPI1;
- hspi1.Init.Mode = SPI_MODE_MASTER;
- hspi1.Init.Direction = SPI_DIRECTION_2LINES;
- hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
- hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
- hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
- hspi1.Init.NSS = SPI_NSS_SOFT;
- hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
- hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
- hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
- hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
- hspi1.Init.CRCPolynomial = 10;
- if (HAL_SPI_Init(&hspi1) != HAL_OK)
- {
- Error_Handler();
- }
- //Restart BT
- BluetoothReset();
- //Wait for restart to finish
- HAL_Delay(50);
- //Declare arrays for this example
- uint8_t indata1[5];
- uint8_t indata2[5];
- uint8_t indata3[6];
- uint8_t indata4[5];
- uint8_t outdata1[5];
- uint8_t outdata2[5];
- uint8_t outdata3[6];
- uint8_t outdata4[5];
- //Clear all arrays to 0
- cleararray(indata1,5);
- cleararray(indata2,5);
- cleararray(indata3,6);
- cleararray(indata4,5);
- cleararray(outdata1,5);
- cleararray(outdata2,5);
- cleararray(outdata3,6);
- cleararray(outdata4,5);
- //Set first bytes of arrays to indicate what type of transfer is about to commence
- /* The SPI frame header of the master on the MOSI line is composed of 1 control byte and 4 dummy bytes (0x00). */
- indata1[0] = 0x0a; //Write
- indata2[0] = 0x0b; //Read
- indata4[0] = 0x0a; //Write
- //Loop until BT responds it is ready to receive
- while (outdata1[0] == 0 || outdata1[1] == 0)
- {
- //Select BT chip for SPI transfer
- SelectBluetoothDevice();
- //Start transfer immediately
- /*In order to correctly read the ready status of the device in any condition, the CTRL byte
- must be sent within 8 µs from the start of the frame*/
- HAL_SPI_TransmitReceive(&hspi1, indata1, outdata1, 5, 15);
- //Deselect BT chip for SPI transfer
- /* It is mandatory to release and assert again nCS before reading again the SPI header (incase we failed) */
- DeselectBluetoothDevice();
- //Give it time to process
- HAL_Delay(10);
- }
- /*outdata1 contains the expected response, indicating it is ready and write buffer is > 0*/
- HAL_Delay(50);
- //Loop while there is data available to read from BT
- while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0))
- {
- SelectBluetoothDevice();
- HAL_SPI_TransmitReceive(&hspi1, indata2, outdata2, 5, 15);
- if(outdata2[0] != 0 || outdata2[3] != 0)
- {
- HAL_SPI_TransmitReceive(&hspi1, indata3, outdata3, 6, 15);
- }
- DeselectBluetoothDevice();
- HAL_Delay(10);
- }
- /*outdata2 contains the expected response, indicating it is ready and read buffer is 6 bytes (response generated by the restart)*/
- /*outdata3 contains the expected response, the everything went ok bytes: 4, 255, 3, 1, 0, 1*/
- HAL_Delay(50);
- //Loop until BT responds it is ready to receive
- while (outdata4[0] == 0 || outdata4[1] == 0)
- {
- SelectBluetoothDevice();
- HAL_SPI_TransmitReceive(&hspi1, indata4, outdata4, 5, 15);
- DeselectBluetoothDevice();
- HAL_Delay(10);
- }
- /*Never reach here since we get no response anymore from BT*/
- //Set bluetooth device as active SPI device
- void SelectBluetoothDevice()
- {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
- }
- //Release bluetooth device as active SPI device
- void DeselectBluetoothDevice()
- {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement