Advertisement
bjornpaulstrom

BlueNRG-MS

Apr 20th, 2017
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 KB | None | 0 0
  1.     //My SPI setup
  2.     hspi1.Instance = SPI1;
  3.     hspi1.Init.Mode = SPI_MODE_MASTER;
  4.     hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  5.     hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  6.     hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  7.     hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  8.     hspi1.Init.NSS = SPI_NSS_SOFT;
  9.     hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  10.     hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  11.     hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  12.     hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  13.     hspi1.Init.CRCPolynomial = 10;
  14.     if (HAL_SPI_Init(&hspi1) != HAL_OK)
  15.     {
  16.         Error_Handler();
  17.     }
  18.  
  19.     //Restart BT
  20.     BluetoothReset();
  21.  
  22.     //Wait for restart to finish
  23.     HAL_Delay(50);
  24.  
  25.     //Declare arrays for this example
  26.     uint8_t indata1[5];
  27.     uint8_t indata2[5];
  28.     uint8_t indata3[6];
  29.     uint8_t indata4[5];
  30.     uint8_t outdata1[5];
  31.     uint8_t outdata2[5];
  32.     uint8_t outdata3[6];
  33.     uint8_t outdata4[5];
  34.  
  35.     //Clear all arrays to 0
  36.     cleararray(indata1,5);
  37.     cleararray(indata2,5);
  38.     cleararray(indata3,6);
  39.     cleararray(indata4,5);
  40.     cleararray(outdata1,5);
  41.     cleararray(outdata2,5);
  42.     cleararray(outdata3,6);
  43.     cleararray(outdata4,5);
  44.  
  45.     //Set first bytes of arrays to indicate what type of transfer is about to commence
  46.     /* The SPI frame header of the master on the MOSI line is composed of 1 control byte and 4 dummy bytes (0x00). */
  47.     indata1[0] = 0x0a; //Write 
  48.     indata2[0] = 0x0b; //Read
  49.     indata4[0] = 0x0a; //Write
  50.  
  51.     //Loop until BT responds it is ready to receive
  52.     while (outdata1[0] == 0 || outdata1[1] == 0)
  53.     {
  54.         //Select BT chip for SPI transfer
  55.         SelectBluetoothDevice();
  56.  
  57.         //Start transfer immediately
  58.         /*In order to correctly read the ready status of the device in any condition, the CTRL byte
  59.         must be sent within 8 µs from the start of the frame*/
  60.         HAL_SPI_TransmitReceive(&hspi1, indata1, outdata1, 5, 15);
  61.  
  62.         //Deselect BT chip for SPI transfer
  63.         /* It is mandatory to release and assert again nCS before reading again the SPI header (incase we failed) */
  64.         DeselectBluetoothDevice();
  65.  
  66.         //Give it time to process
  67.         HAL_Delay(10);
  68.     }
  69.  
  70.     /*outdata1 contains the expected response, indicating it is ready and write buffer is > 0*/
  71.  
  72.     HAL_Delay(50);
  73.    
  74.     //Loop while there is data available to read from BT
  75.     while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0))
  76.     {
  77.         SelectBluetoothDevice();
  78.         HAL_SPI_TransmitReceive(&hspi1, indata2, outdata2, 5, 15);
  79.         if(outdata2[0] != 0 || outdata2[3] != 0)
  80.         {
  81.             HAL_SPI_TransmitReceive(&hspi1, indata3, outdata3, 6, 15);
  82.         }
  83.         DeselectBluetoothDevice();
  84.         HAL_Delay(10);
  85.     }
  86.  
  87.     /*outdata2 contains the expected response, indicating it is ready and read buffer is 6 bytes (response generated by the restart)*/
  88.     /*outdata3 contains the expected response, the everything went ok bytes: 4, 255, 3, 1, 0, 1*/
  89.  
  90.     HAL_Delay(50);
  91.  
  92.     //Loop until BT responds it is ready to receive
  93.     while (outdata4[0] == 0 || outdata4[1] == 0)
  94.     {
  95.         SelectBluetoothDevice();
  96.         HAL_SPI_TransmitReceive(&hspi1, indata4, outdata4, 5, 15);
  97.         DeselectBluetoothDevice();
  98.         HAL_Delay(10);
  99.     }
  100.  
  101.     /*Never reach here since we get no response anymore from BT*/
  102.  
  103.     //Set bluetooth device as active SPI device
  104.     void SelectBluetoothDevice()
  105.     {
  106.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  107.     }
  108.  
  109.     //Release bluetooth device as active SPI device
  110.     void DeselectBluetoothDevice()
  111.     {
  112.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
  113.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement