| OLD | NEW |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 #include "platforms/stm/disco_fletch/src/uart.h" | 5 #include "platforms/stm/disco_dartino/src/uart.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 | 8 |
| 9 #include <stm32f7xx_hal.h> | 9 #include <stm32f7xx_hal.h> |
| 10 | 10 |
| 11 #include "src/shared/atomic.h" | 11 #include "src/shared/atomic.h" |
| 12 #include "src/shared/utils.h" | 12 #include "src/shared/utils.h" |
| 13 | 13 |
| 14 // Reference to the instance in the code generated by STM32CubeMX. | 14 // Reference to the instance in the code generated by STM32CubeMX. |
| 15 extern UART_HandleTypeDef huart1; | 15 extern UART_HandleTypeDef huart1; |
| 16 | 16 |
| 17 // TODO(sgjesse): Get rid of these global variables. These global | 17 // TODO(sgjesse): Get rid of these global variables. These global |
| 18 // variables are accessed from the interrupt handlers. | 18 // variables are accessed from the interrupt handlers. |
| 19 static osSemaphoreId sem_; | 19 static osSemaphoreId sem_; |
| 20 static uint32_t error = 0; | 20 static uint32_t error = 0; |
| 21 | 21 |
| 22 // Bits set from interrupt handlers. | 22 // Bits set from interrupt handlers. |
| 23 const int kReceivedBit = 1 << 0; | 23 const int kReceivedBit = 1 << 0; |
| 24 const int kTransmittedBit = 1 << 1; | 24 const int kTransmittedBit = 1 << 1; |
| 25 const int kErrorBit = 1 << 2; | 25 const int kErrorBit = 1 << 2; |
| 26 static fletch::Atomic<uint32_t> interrupt_flags; | 26 static dartino::Atomic<uint32_t> interrupt_flags; |
| 27 | 27 |
| 28 const int kRxBufferSize = 511; | 28 const int kRxBufferSize = 511; |
| 29 const int kTxBufferSize = 511; | 29 const int kTxBufferSize = 511; |
| 30 | 30 |
| 31 // C trampoline for the interrupt handling thread. | 31 // C trampoline for the interrupt handling thread. |
| 32 void __UartTask(void const* argument) { | 32 void __UartTask(void const* argument) { |
| 33 Uart* uart = const_cast<Uart*>(reinterpret_cast<const Uart*>(argument)); | 33 Uart* uart = const_cast<Uart*>(reinterpret_cast<const Uart*>(argument)); |
| 34 uart->Task(); | 34 uart->Task(); |
| 35 } | 35 } |
| 36 | 36 |
| 37 Uart::Uart() { | 37 Uart::Uart() { |
| 38 uart_ = &huart1; | 38 uart_ = &huart1; |
| 39 rx_buffer_ = new CircularBuffer(kRxBufferSize); | 39 rx_buffer_ = new CircularBuffer(kRxBufferSize); |
| 40 tx_mutex_ = fletch::Platform::CreateMutex(); | 40 tx_mutex_ = dartino::Platform::CreateMutex(); |
| 41 tx_buffer_ = new CircularBuffer(kTxBufferSize); | 41 tx_buffer_ = new CircularBuffer(kTxBufferSize); |
| 42 tx_pending_ = false; | 42 tx_pending_ = false; |
| 43 error_count_ = 0; | 43 error_count_ = 0; |
| 44 | 44 |
| 45 // Semaphore for signaling from interrupt handlers. A maximum of | 45 // Semaphore for signaling from interrupt handlers. A maximum of |
| 46 // three tokens - one for data received and one for data transmitted | 46 // three tokens - one for data received and one for data transmitted |
| 47 // and one for error. | 47 // and one for error. |
| 48 semaphore_ = osSemaphoreCreate(osSemaphore(semaphore_def_), 3); | 48 semaphore_ = osSemaphoreCreate(osSemaphore(semaphore_def_), 3); |
| 49 // Store in global variable for access from interrupt handlers. | 49 // Store in global variable for access from interrupt handlers. |
| 50 sem_ = semaphore_; | 50 sem_ = semaphore_; |
| 51 } | 51 } |
| 52 | 52 |
| 53 void Uart::Start() { | 53 void Uart::Start() { |
| 54 // Start thread for handling interrupts. | 54 // Start thread for handling interrupts. |
| 55 osThreadDef(UART_TASK, __UartTask, osPriorityHigh, 0, 1024); | 55 osThreadDef(UART_TASK, __UartTask, osPriorityHigh, 0, 1024); |
| 56 osThreadCreate(osThread(UART_TASK), this); | 56 osThreadCreate(osThread(UART_TASK), this); |
| 57 | 57 |
| 58 // Start receiving. | 58 // Start receiving. |
| 59 HAL_UART_Receive_IT(uart_, &rx_data_, 1); | 59 HAL_UART_Receive_IT(uart_, &rx_data_, 1); |
| 60 } | 60 } |
| 61 | 61 |
| 62 size_t Uart::Read(uint8_t* buffer, size_t count) { | 62 size_t Uart::Read(uint8_t* buffer, size_t count) { |
| 63 return rx_buffer_->Read(buffer, count, CircularBuffer::kBlock); | 63 return rx_buffer_->Read(buffer, count, CircularBuffer::kBlock); |
| 64 } | 64 } |
| 65 | 65 |
| 66 size_t Uart::Write(const uint8_t* buffer, size_t count) { | 66 size_t Uart::Write(const uint8_t* buffer, size_t count) { |
| 67 size_t written = tx_buffer_->Write(buffer, count, CircularBuffer::kBlock); | 67 size_t written = tx_buffer_->Write(buffer, count, CircularBuffer::kBlock); |
| 68 | 68 |
| 69 fletch::ScopedLock lock(tx_mutex_); | 69 dartino::ScopedLock lock(tx_mutex_); |
| 70 EnsureTransmission(); | 70 EnsureTransmission(); |
| 71 return written; | 71 return written; |
| 72 } | 72 } |
| 73 | 73 |
| 74 void Uart::Task() { | 74 void Uart::Task() { |
| 75 // Process notifications from the interrupt handlers. | 75 // Process notifications from the interrupt handlers. |
| 76 for (;;) { | 76 for (;;) { |
| 77 // Wait for an interrupt to be processed. | 77 // Wait for an interrupt to be processed. |
| 78 osSemaphoreWait(semaphore_, osWaitForever); | 78 osSemaphoreWait(semaphore_, osWaitForever); |
| 79 // Read the flags and set them to zero. | 79 // Read the flags and set them to zero. |
| 80 uint32_t flags = interrupt_flags.exchange(0); | 80 uint32_t flags = interrupt_flags.exchange(0); |
| 81 | 81 |
| 82 if ((flags & kReceivedBit) != 0) { | 82 if ((flags & kReceivedBit) != 0) { |
| 83 // Don't block when writing to the buffer. Buffer overrun will | 83 // Don't block when writing to the buffer. Buffer overrun will |
| 84 // cause lost data. | 84 // cause lost data. |
| 85 rx_buffer_->Write(&rx_data_, 1, CircularBuffer::kDontBlock); | 85 rx_buffer_->Write(&rx_data_, 1, CircularBuffer::kDontBlock); |
| 86 | 86 |
| 87 // Start receiving of next byte. | 87 // Start receiving of next byte. |
| 88 HAL_StatusTypeDef status = HAL_UART_Receive_IT(uart_, &rx_data_, 1); | 88 HAL_StatusTypeDef status = HAL_UART_Receive_IT(uart_, &rx_data_, 1); |
| 89 if (status != HAL_OK) { | 89 if (status != HAL_OK) { |
| 90 fletch::Print::Error("HAL_UART_Receive_IT returned %d\n", status); | 90 dartino::Print::Error("HAL_UART_Receive_IT returned %d\n", status); |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 | 93 |
| 94 if ((flags & kTransmittedBit) != 0) { | 94 if ((flags & kTransmittedBit) != 0) { |
| 95 fletch::ScopedLock lock(tx_mutex_); | 95 dartino::ScopedLock lock(tx_mutex_); |
| 96 tx_pending_ = false; | 96 tx_pending_ = false; |
| 97 EnsureTransmission(); | 97 EnsureTransmission(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 if ((flags & kErrorBit) != 0) { | 100 if ((flags & kErrorBit) != 0) { |
| 101 // Ignore errors for now. | 101 // Ignore errors for now. |
| 102 error_count_++; | 102 error_count_++; |
| 103 error = 0; | 103 error = 0; |
| 104 // Setup interrupt for next byte. | 104 // Setup interrupt for next byte. |
| 105 HAL_StatusTypeDef status = HAL_UART_Receive_IT(uart_, &rx_data_, 1); | 105 HAL_StatusTypeDef status = HAL_UART_Receive_IT(uart_, &rx_data_, 1); |
| 106 if (status != HAL_OK) { | 106 if (status != HAL_OK) { |
| 107 fletch::Print::Error("HAL_UART_Receive_IT returned %d\n", status); | 107 dartino::Print::Error("HAL_UART_Receive_IT returned %d\n", status); |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 | 112 |
| 113 void Uart::EnsureTransmission() { | 113 void Uart::EnsureTransmission() { |
| 114 if (!tx_pending_) { | 114 if (!tx_pending_) { |
| 115 // Don't block when there is nothing to send. | 115 // Don't block when there is nothing to send. |
| 116 int bytes = tx_buffer_->Read( | 116 int bytes = tx_buffer_->Read( |
| 117 tx_data_, kTxBlockSize, CircularBuffer::kDontBlock); | 117 tx_data_, kTxBlockSize, CircularBuffer::kDontBlock); |
| 118 if (bytes > 0) { | 118 if (bytes > 0) { |
| 119 HAL_StatusTypeDef status = HAL_UART_Transmit_IT(uart_, tx_data_, bytes); | 119 HAL_StatusTypeDef status = HAL_UART_Transmit_IT(uart_, tx_data_, bytes); |
| 120 if (status != HAL_OK) { | 120 if (status != HAL_OK) { |
| 121 fletch::Print::Error("HAL_UART_Transmit_IT returned %d\n", status); | 121 dartino::Print::Error("HAL_UART_Transmit_IT returned %d\n", status); |
| 122 } | 122 } |
| 123 tx_pending_ = true; | 123 tx_pending_ = true; |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 } | 126 } |
| 127 | 127 |
| 128 // Shared return from interrupt handler. Will set the specified flag | 128 // Shared return from interrupt handler. Will set the specified flag |
| 129 // and transfer control to the thread handling interrupts. | 129 // and transfer control to the thread handling interrupts. |
| 130 static void ReturnFromInterrupt(UART_HandleTypeDef *huart, uint32_t flag) { | 130 static void ReturnFromInterrupt(UART_HandleTypeDef *huart, uint32_t flag) { |
| 131 // Set the requested bit. | 131 // Set the requested bit. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 155 error = HAL_UART_GetError(huart); | 155 error = HAL_UART_GetError(huart); |
| 156 | 156 |
| 157 // Clear all errors. | 157 // Clear all errors. |
| 158 __HAL_UART_CLEAR_OREFLAG(&huart1); | 158 __HAL_UART_CLEAR_OREFLAG(&huart1); |
| 159 __HAL_UART_CLEAR_FEFLAG(&huart1); | 159 __HAL_UART_CLEAR_FEFLAG(&huart1); |
| 160 __HAL_UART_CLEAR_PEFLAG(&huart1); | 160 __HAL_UART_CLEAR_PEFLAG(&huart1); |
| 161 __HAL_UART_CLEAR_NEFLAG(&huart1); | 161 __HAL_UART_CLEAR_NEFLAG(&huart1); |
| 162 | 162 |
| 163 ReturnFromInterrupt(huart, kErrorBit); | 163 ReturnFromInterrupt(huart, kErrorBit); |
| 164 } | 164 } |
| OLD | NEW |