Advertisement
smartel99

STM32-Arduino UART trouble

Jul 9th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. // --- ARDUINO CODE ---
  2. void loop() {
  3.     digitalWrite(4, 0); // Clear EXTI11 line.
  4.    
  5.     if (mySerial.available() && received < 480) {   // STM32 sending data and is not done.
  6.         buff[received] = mySerial.read();           // Append received data to the buffer.
  7.         received++;
  8.     }
  9.     if (received >= 480) {                          // If the buffer is full
  10.         received = 0;                               // transmit it to PC.
  11.         Serial.println(buff);
  12.     }
  13.  
  14.  
  15.     if (Serial.available()) {
  16.         if (Serial.read() == 'r') {     // PC requests data from the STM32
  17.             digitalWrite(4, 1);         // Triggers STM32 EXTI11 line.
  18.             while (Serial.available())  // Empty the buffer.
  19.                 Serial.read();
  20.         }
  21.     }
  22. }
  23. // --- ---------- ---
  24.  
  25. // --- STM32 CODE ---
  26. void EXTI15_10_IRQHandler(void)
  27. {
  28.     // Make sure that the interrupt is the good one.
  29.     if (HAL_GPIO_ReadPin(data_req_IRQ_GPIO_Port, data_req_IRQ_Pin)) {
  30.         if (is_sending_data == FALSE)   // If no transmission is happening
  31.             should_send_data = TRUE;    // raise transmission flag.
  32.     }
  33.    
  34.     // IRQ handling stuff...
  35. }
  36.  
  37. void HAL_UART_TxCpltCallback(UART_HandleTypeDef * huart) {
  38.     is_sending_data = FALSE;    // Transmition is completed, unblock requests.
  39. }
  40.  
  41. void main(void){
  42.     // Init and other stuff...
  43.  
  44.     while (1) {
  45.         if (should_send_data == TRUE) { // If data was requested
  46.             HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
  47.             HAL_UART_Transmit_IT(&huart3, matrice, 480);    // Start transmission by interrupt.
  48.             is_sending_data = TRUE;                         // Block requests.
  49.             should_send_data = FALSE;                       // Clear flag.
  50.         }
  51.     // matrice acquisition stuff here
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement