From 7d8681ce3c15f0a429a140e69c68d043f7e99c0f Mon Sep 17 00:00:00 2001 From: fg Date: Sat, 15 Feb 2025 13:34:42 -0600 Subject: [PATCH 1/5] Copied Brian Atmos code with minor comments --- app/src/Uart4Setup.c | 117 +++++++++++++++++++++++++++++++++++++++ app/src/Uart4Setup.h | 37 +++++++++++++ app/src/remote_control.c | 70 +++++++++++++++++++++++ 3 files changed, 224 insertions(+) create mode 100644 app/src/Uart4Setup.c create mode 100644 app/src/Uart4Setup.h create mode 100644 app/src/remote_control.c diff --git a/app/src/Uart4Setup.c b/app/src/Uart4Setup.c new file mode 100644 index 0000000..bb70fd4 --- /dev/null +++ b/app/src/Uart4Setup.c @@ -0,0 +1,117 @@ +/** + * MIT License + * + * Copyright (c) 2019 Brian Amos + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#include +#include +#include + +//static const uint8_t uart4Msg[] = "0123456789abcdefghijklmnopqrstuvwxyz"; +static const uint8_t uart4Msg[] = "data from uart4"; +static DMA_HandleTypeDef uart4DmaTx; + +static void uart4TxDmaStartRepeat( const uint8_t* Msg, uint16_t Len ); +static void uart4TxDmaSetup( void ); + +/** + * Setup UART4 to repeatedly transmit a message + * via DMA (no CPU intervention required). This simulates + * what would happen if there was data flowing from an + * external off-chip source and let's us concentrate on + * what's going on with UART2 + * + * @param Baudrate desired baudrate for the UART4 + * + * This is a quick and dirty setup. . . + */ +void SetupUart4ExternalSim( uint32_t BaudRate ) +{ + //setup DMA + uart4TxDmaSetup(); + + //GPIO pins are setup in BSP/Nucleo_F767ZI_Init + STM_UartInit(UART4, BaudRate, &uart4DmaTx, NULL); + + //also enable DMA for UART4 Transmits + UART4->CR3 |= USART_CR3_DMAT_Msk; + + /** + * start the repeating DMA transfer. Eventually, non-circular + * receivers will loose a character here or there at high baudrates. + * When this happens, SEGGER_SYSVIEW_Print() will stop printing when it hits + * the first NULL character. + */ + uart4TxDmaStartRepeat(uart4Msg, sizeof(uart4Msg)); +} + +static void uart4TxDmaSetup( void ) +{ + __HAL_RCC_DMA1_CLK_ENABLE(); + //no need to enable any interrupts + HAL_NVIC_SetPriority(DMA1_Stream4_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Stream4_IRQn); + + //initialize the DMA peripheral to transfer uart4Msg + //to UART4 repeatedly + memset(&uart4DmaTx, 0, sizeof(uart4DmaTx)); + uart4DmaTx.Instance = DMA1_Stream4; + uart4DmaTx.Init.Channel = DMA_CHANNEL_4; //channel 4 is for UART4 Tx + uart4DmaTx.Init.Direction = DMA_MEMORY_TO_PERIPH; //transfering out of memory and into the peripheral register + uart4DmaTx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; //no fifo + uart4DmaTx.Init.MemBurst = DMA_MBURST_SINGLE; //transfer 1 at a time + uart4DmaTx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + uart4DmaTx.Init.MemInc = DMA_MINC_ENABLE; //increment 1 byte at a time + uart4DmaTx.Init.Mode = DMA_CIRCULAR; //this will automatically restart the transfer at the beginning after it has finished + uart4DmaTx.Init.PeriphBurst = DMA_PBURST_SINGLE; //write 1 at a time to the peripheral + uart4DmaTx.Init.PeriphInc = DMA_PINC_DISABLE; //always keep the peripheral address the same (the Tx data register is always in the same location) + uart4DmaTx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + //we're setting low priority since this is meant to be simulated data - the DMA + //transfers of the active code should take priority + uart4DmaTx.Init.Priority = DMA_PRIORITY_VERY_HIGH; + assert_param(HAL_DMA_Init(&uart4DmaTx) == HAL_OK); + DMA1_Stream4->CR &= ~DMA_SxCR_EN; + + //set the DMA transmit mode flag to enable DMA transfers + UART4->CR3 |= USART_CR3_DMAT_Msk; +} + +/** + * starts a DMA transfer to the UART4 Tx register + * that will automatically repeat after it is finished + * @param Msg pointer to array to transfer + * @param Len number of elements in the array + */ +static void uart4TxDmaStartRepeat( const uint8_t* Msg, uint16_t Len ) +{ + //clear the transfer complete flag to make sure our transfer starts + UART4->ICR |= USART_ICR_TCCF; + assert_param(HAL_DMA_Start(&uart4DmaTx, (uint32_t) Msg, (uint32_t)&(UART4->TDR), Len) == HAL_OK); +} + +void DMA1_Stream4_IRQHandler(void) +{ + //shouldn't ever get here - interrupts are not enabled + while(1); +// HAL_DMA_IRQHandler(&hdma_uart4_tx); +} diff --git a/app/src/Uart4Setup.h b/app/src/Uart4Setup.h new file mode 100644 index 0000000..0348f22 --- /dev/null +++ b/app/src/Uart4Setup.h @@ -0,0 +1,37 @@ +/** + * MIT License + * + * Copyright (c) 2019 Brian Amos + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ +#ifndef SRC_UART4SETUP_H_ +#define SRC_UART4SETUP_H_ +#ifdef __cplusplus + extern "C" { +#endif + +void SetupUart4ExternalSim( uint32_t Baudrate ); + + +#ifdef __cplusplus + } +#endif +#endif /* SRC_UART4SETUP_H_ */ diff --git a/app/src/remote_control.c b/app/src/remote_control.c new file mode 100644 index 0000000..6845f83 --- /dev/null +++ b/app/src/remote_control.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "Uart4Setup.h" + +// Exportar los setups necesarios para que funcione + + + +void polledUartReceive ( void* NotUsed ); // void not used? +void uartPrintOutTask( void* NotUsed); // void not used? + +static QueueHandle_t uart2_BytesReceived = NULL; + +int main(void) +{ + HWInit(); + SetupUart4ExternalSim(9600); + SEGGER_SYSVIEW_Conf(); + HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); + + assert_param(xTaskCreate(polledUartReceive, "polledUartRx", STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL) == pdPASS); + assert_param(xTaskCreate(uartPrintOutTask, "uartPrintTask", STACK_SIZE, NULL, tskIDLE_PRIORITY + 3, NULL) == pdPASS); + + uart2_BytesReceived = xQueueCreate(10, sizeof(char)); + + +// porque esta comentado? +// for(int i = 0; i < 10; i++) +// { +// UART4->TDR = i; +// while(!(UART4->ISR & USART_ISR_TXE)); +// } + //start the scheduler - shouldn't return unless there's a problem + vTaskStartScheduler(); + + while(1) + { + } +} + +void uartPrintOutTask( void* NotUsed) +{ + char nextByte; + + while(1) + { + xQueueReceive(uart2_BytesReceived, &nextByte, portMAX_DELAY); + SEGGER_SYSVIEW_PrintfHost("%c", nextByte); //Donde lo esta imprimiendo? + } +} + +// Como funciona esto? +void polledUartReceive( void* NotUsed ) +{ + uint8_t nextByte; + STM_UartInit(USART2, 9600, NULL, NULL); + while(1) + { + while(!(USART2->ISR & USART_ISR_RXNE_Msk)); + nextByte = USART2->RDR; + xQueueSend(uart2_BytesReceived, &nextByte, 0); + } +} + From 85437a84681cf9333c6ab9b461bdddd7255af24c Mon Sep 17 00:00:00 2001 From: fabianglz993 Date: Sat, 15 Feb 2025 21:29:16 +0000 Subject: [PATCH 2/5] test --- test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.txt diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..e69de29 From d5694a81751b4daabc346b99a0a3a8bdad34ed44 Mon Sep 17 00:00:00 2001 From: fabianglz993 Date: Thu, 27 Mar 2025 14:05:16 -0600 Subject: [PATCH 3/5] Corrected remote control pr comments and updated ioc file --- .vscode/settings.json | 3 +- Middlewares/owlware | 2 +- app/inc/CanBusTask.h | 0 app/src/CanBusTask.c | 79 -------------------------- app/src/Uart4Setup.c | 117 --------------------------------------- app/src/Uart4Setup.h | 37 ------------- app/src/remote_control.c | 104 +++++++++++++--------------------- robotConfig | 2 +- 8 files changed, 41 insertions(+), 303 deletions(-) delete mode 100644 app/inc/CanBusTask.h delete mode 100644 app/src/CanBusTask.c delete mode 100644 app/src/Uart4Setup.c delete mode 100644 app/src/Uart4Setup.h diff --git a/.vscode/settings.json b/.vscode/settings.json index f3ac2bf..e0910a9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,6 +10,7 @@ "stm32_hal_legacy.h": "c", "stm32f3xx.h": "c", "stm32f303xe.h": "c", - "robotpins.h": "c" + "robotpins.h": "c", + "stm32f3xx_it.h": "c" } } \ No newline at end of file diff --git a/Middlewares/owlware b/Middlewares/owlware index e5a6928..6d1b817 160000 --- a/Middlewares/owlware +++ b/Middlewares/owlware @@ -1 +1 @@ -Subproject commit e5a69282aba578d68f7fb33fe6973eef60d6f9e8 +Subproject commit 6d1b8171a0f0c1ae99836c41bdfffa49e6dcfe61 diff --git a/app/inc/CanBusTask.h b/app/inc/CanBusTask.h deleted file mode 100644 index e69de29..0000000 diff --git a/app/src/CanBusTask.c b/app/src/CanBusTask.c deleted file mode 100644 index 19750ba..0000000 --- a/app/src/CanBusTask.c +++ /dev/null @@ -1,79 +0,0 @@ -/* - * BaseFileC.h - * - * Created on: May 24, 2023 - * Author: @yourName - */ - -#ifndef CANBUSTASK_C -#define CANBUSTASK_C - -#include "CanBusTask.h" -#ifdef __cplusplus -extern "C" { -#endif - -// YOUR CODE -// We need at least the following principles: -// * StartReceiveInt: Sets up an interrupt-based reception for USART2 and configures the necessary variables used by the -// ISR for the transfer. -// * StartCAN4Traffic: Starts a continuous stream of data transmitted from UART4 to be received by USART2 (provided the -// jumpers are correctly set). -// * canActionTask: This function initializes USART2 and associated hardware, starts a reception, and waits for -// completion (with a deadline of 100 ms). The complete message is either printed or a timeout occurs and an error is -// printed. -// * CAN2_IRQHandler: An ISR is issued when an interrupt occurs for the USART2 peripheral. - -#ifdef __cplusplus -} -#endif - -#endif /* TemplateCFile_C */ - -osSemaphoreId_t I2C_semaphore; -I2C_HandleTypeDef hi2c1; - -void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef* hi2c) { - if (hi2c->State == HAL_I2C_STATE_READY) { - osSemaphoreRelease(I2C_semaphore); - } -} - -void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef* hi2c) { - if (hi2c->State == HAL_I2C_STATE_READY) { - osSemaphoreRelease(I2C_semaphore); - } -} - -static void Task1(void* argument) { - while (1) { - HAL_I2C_Master_Transmit_IT(&hi2c1, I2C_addr, write_data_array, sizeof(write_data_array)); - osSemaphoreAcquire(I2C_semaphore, 100); - HAL_I2C_Master_Receive_IT(&hi2c1, I2C_addr, read_data_array, sizeof(read_data_array)); - osSemaphoreAcquire(I2C_semaphore, 100); - } -} - -osSemaphoreId_t I2C_semaphore; -I2C_HandleTypeDef hi2c1; - -void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef* hi2c) { - if (hi2c->State == HAL_I2C_STATE_READY) { - osSemaphoreRelease(I2C_semaphore); - } -} - -void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef* hi2c) { - if (hi2c->State == HAL_I2C_STATE_READY) { - osSemaphoreRelease(I2C_semaphore); - } -} - -static void Task1(void* argument) { - while (1) { - HAL_I2C_Master_Transmit_IT(&hi2c1, I2C_addr, write_data_array, sizeof(write_data_array)); - osSemaphoreAcquire(I2C_semaphore, 100); - HAL_I2C_Master_Receive_IT(&hi2c1, I2C_addr, read_data_array, sizeof(read_data_array)); - osSemaphoreAcquire(I2C_semaphore, 100); - } -} \ No newline at end of file diff --git a/app/src/Uart4Setup.c b/app/src/Uart4Setup.c deleted file mode 100644 index bb70fd4..0000000 --- a/app/src/Uart4Setup.c +++ /dev/null @@ -1,117 +0,0 @@ -/** - * MIT License - * - * Copyright (c) 2019 Brian Amos - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ - -#include -#include -#include - -//static const uint8_t uart4Msg[] = "0123456789abcdefghijklmnopqrstuvwxyz"; -static const uint8_t uart4Msg[] = "data from uart4"; -static DMA_HandleTypeDef uart4DmaTx; - -static void uart4TxDmaStartRepeat( const uint8_t* Msg, uint16_t Len ); -static void uart4TxDmaSetup( void ); - -/** - * Setup UART4 to repeatedly transmit a message - * via DMA (no CPU intervention required). This simulates - * what would happen if there was data flowing from an - * external off-chip source and let's us concentrate on - * what's going on with UART2 - * - * @param Baudrate desired baudrate for the UART4 - * - * This is a quick and dirty setup. . . - */ -void SetupUart4ExternalSim( uint32_t BaudRate ) -{ - //setup DMA - uart4TxDmaSetup(); - - //GPIO pins are setup in BSP/Nucleo_F767ZI_Init - STM_UartInit(UART4, BaudRate, &uart4DmaTx, NULL); - - //also enable DMA for UART4 Transmits - UART4->CR3 |= USART_CR3_DMAT_Msk; - - /** - * start the repeating DMA transfer. Eventually, non-circular - * receivers will loose a character here or there at high baudrates. - * When this happens, SEGGER_SYSVIEW_Print() will stop printing when it hits - * the first NULL character. - */ - uart4TxDmaStartRepeat(uart4Msg, sizeof(uart4Msg)); -} - -static void uart4TxDmaSetup( void ) -{ - __HAL_RCC_DMA1_CLK_ENABLE(); - //no need to enable any interrupts - HAL_NVIC_SetPriority(DMA1_Stream4_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(DMA1_Stream4_IRQn); - - //initialize the DMA peripheral to transfer uart4Msg - //to UART4 repeatedly - memset(&uart4DmaTx, 0, sizeof(uart4DmaTx)); - uart4DmaTx.Instance = DMA1_Stream4; - uart4DmaTx.Init.Channel = DMA_CHANNEL_4; //channel 4 is for UART4 Tx - uart4DmaTx.Init.Direction = DMA_MEMORY_TO_PERIPH; //transfering out of memory and into the peripheral register - uart4DmaTx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; //no fifo - uart4DmaTx.Init.MemBurst = DMA_MBURST_SINGLE; //transfer 1 at a time - uart4DmaTx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; - uart4DmaTx.Init.MemInc = DMA_MINC_ENABLE; //increment 1 byte at a time - uart4DmaTx.Init.Mode = DMA_CIRCULAR; //this will automatically restart the transfer at the beginning after it has finished - uart4DmaTx.Init.PeriphBurst = DMA_PBURST_SINGLE; //write 1 at a time to the peripheral - uart4DmaTx.Init.PeriphInc = DMA_PINC_DISABLE; //always keep the peripheral address the same (the Tx data register is always in the same location) - uart4DmaTx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; - //we're setting low priority since this is meant to be simulated data - the DMA - //transfers of the active code should take priority - uart4DmaTx.Init.Priority = DMA_PRIORITY_VERY_HIGH; - assert_param(HAL_DMA_Init(&uart4DmaTx) == HAL_OK); - DMA1_Stream4->CR &= ~DMA_SxCR_EN; - - //set the DMA transmit mode flag to enable DMA transfers - UART4->CR3 |= USART_CR3_DMAT_Msk; -} - -/** - * starts a DMA transfer to the UART4 Tx register - * that will automatically repeat after it is finished - * @param Msg pointer to array to transfer - * @param Len number of elements in the array - */ -static void uart4TxDmaStartRepeat( const uint8_t* Msg, uint16_t Len ) -{ - //clear the transfer complete flag to make sure our transfer starts - UART4->ICR |= USART_ICR_TCCF; - assert_param(HAL_DMA_Start(&uart4DmaTx, (uint32_t) Msg, (uint32_t)&(UART4->TDR), Len) == HAL_OK); -} - -void DMA1_Stream4_IRQHandler(void) -{ - //shouldn't ever get here - interrupts are not enabled - while(1); -// HAL_DMA_IRQHandler(&hdma_uart4_tx); -} diff --git a/app/src/Uart4Setup.h b/app/src/Uart4Setup.h deleted file mode 100644 index 0348f22..0000000 --- a/app/src/Uart4Setup.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * MIT License - * - * Copyright (c) 2019 Brian Amos - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - */ -#ifndef SRC_UART4SETUP_H_ -#define SRC_UART4SETUP_H_ -#ifdef __cplusplus - extern "C" { -#endif - -void SetupUart4ExternalSim( uint32_t Baudrate ); - - -#ifdef __cplusplus - } -#endif -#endif /* SRC_UART4SETUP_H_ */ diff --git a/app/src/remote_control.c b/app/src/remote_control.c index 6845f83..b28835f 100644 --- a/app/src/remote_control.c +++ b/app/src/remote_control.c @@ -1,70 +1,40 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "Uart4Setup.h" - -// Exportar los setups necesarios para que funcione - - - -void polledUartReceive ( void* NotUsed ); // void not used? -void uartPrintOutTask( void* NotUsed); // void not used? - -static QueueHandle_t uart2_BytesReceived = NULL; - -int main(void) -{ - HWInit(); - SetupUart4ExternalSim(9600); - SEGGER_SYSVIEW_Conf(); - HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); - - assert_param(xTaskCreate(polledUartReceive, "polledUartRx", STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL) == pdPASS); - assert_param(xTaskCreate(uartPrintOutTask, "uartPrintTask", STACK_SIZE, NULL, tskIDLE_PRIORITY + 3, NULL) == pdPASS); - - uart2_BytesReceived = xQueueCreate(10, sizeof(char)); - - -// porque esta comentado? -// for(int i = 0; i < 10; i++) -// { -// UART4->TDR = i; -// while(!(UART4->ISR & USART_ISR_TXE)); -// } - //start the scheduler - shouldn't return unless there's a problem - vTaskStartScheduler(); - - while(1) - { - } +#include +#include +#include + +#include "cmsis_os.h" +#include "usart.h" + +uint8_t UART1_rxBuffer[12]; +UART_HandleTypeDef uart; +osMessageQId remoteQueue; +osMessageQDef(remoteQueue, 16, unsigned int); + +#pragma pack(push, 1) +struct control_data { + uint8_t joystickA; + uint8_t joystickB; + uint8_t knobA; + uint8_t knobB; + uint8_t switchA; + uint8_t switchB; + uint8_t switchC; + uint8_t switchD; +} control_data; +#pragma pack(pop) + +void UART_Init(void) {} + +void UART_Task(void* argument) { + HAL_UART_Receive_IT(&uart, UART1_rxBuffer, 12); + remoteQueue = osMessageCreate(osMessageQ(remoteQueue), NULL); + for (;;) { + } } -void uartPrintOutTask( void* NotUsed) -{ - char nextByte; - - while(1) - { - xQueueReceive(uart2_BytesReceived, &nextByte, portMAX_DELAY); - SEGGER_SYSVIEW_PrintfHost("%c", nextByte); //Donde lo esta imprimiendo? - } +void HAL_UART_RxCpltCallback(UART_HandleTypeDef* uart) { + HAL_UART_Receive_IT(uart, UART1_rxBuffer, 12); + memcpy(&control_data, &UART1_rxBuffer, sizeof(control_data)); + osMessagePut(remoteQueue, (unsigned int)&control_data, osWaitForever); + HAL_UART_Transmit(uart, (uint8_t*)"\n\nSent from ISR\n\n", 17, 500); } - -// Como funciona esto? -void polledUartReceive( void* NotUsed ) -{ - uint8_t nextByte; - STM_UartInit(USART2, 9600, NULL, NULL); - while(1) - { - while(!(USART2->ISR & USART_ISR_RXNE_Msk)); - nextByte = USART2->RDR; - xQueueSend(uart2_BytesReceived, &nextByte, 0); - } -} - diff --git a/robotConfig b/robotConfig index 4eb10cf..f3211d7 160000 --- a/robotConfig +++ b/robotConfig @@ -1 +1 @@ -Subproject commit 4eb10cf505c67e5eef39c2dd317c92f00899dd89 +Subproject commit f3211d7eef7e2493636ba9ca675b0305bea68def From 667c692f571aaa74443108da5102396254d3d687 Mon Sep 17 00:00:00 2001 From: fabianglz993 Date: Sat, 29 Mar 2025 23:07:56 +0000 Subject: [PATCH 4/5] Comments resolved, with sbus translation implemented --- .vscode/settings.json | 3 +- app/inc/CommunicationStructs.h | 27 +++++++++++++--- app/inc/RobomasterCanProtocol.h | 55 --------------------------------- app/src/remote_control.c | 43 ++++++++++++-------------- 4 files changed, 44 insertions(+), 84 deletions(-) delete mode 100644 app/inc/RobomasterCanProtocol.h diff --git a/.vscode/settings.json b/.vscode/settings.json index e0910a9..a51ad4a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,6 +11,7 @@ "stm32f3xx.h": "c", "stm32f303xe.h": "c", "robotpins.h": "c", - "stm32f3xx_it.h": "c" + "stm32f3xx_it.h": "c", + "communicationstructs.h": "c" } } \ No newline at end of file diff --git a/app/inc/CommunicationStructs.h b/app/inc/CommunicationStructs.h index 7a296d7..21cc703 100644 --- a/app/inc/CommunicationStructs.h +++ b/app/inc/CommunicationStructs.h @@ -8,8 +8,8 @@ #pragma once -#include "main.h" #include "cmsis_os.h" +#include "main.h" /** * @brief Represents a CAN bus message splitted into the velocity @@ -35,10 +35,27 @@ typedef struct { uint8_t vMotor_pitch; } GimballControlMessage; - -// Message queues from protocols +/** + * @brief Represents a Control Data Message + */ +#pragma pack(push, 1) +typedef struct control_data { + uint8_t joystickA; + uint8_t joystickB; + uint8_t knobA; + uint8_t knobB; + uint8_t switchA; + uint8_t switchB; + uint8_t switchC; + uint8_t switchD; +} control_data; +#pragma pack(pop) + +// Message queues from protocols extern osPoolId can_rx_mpool; extern osPoolId can_tx_mpool; -extern osMessageQId outputQueueChassis; -extern osMessageQId inputQueueChassis; \ No newline at end of file +extern osMessageQId outputQueueChassis; +extern osMessageQId inputQueueChassis; + +extern osMessageQId remoteQueue; diff --git a/app/inc/RobomasterCanProtocol.h b/app/inc/RobomasterCanProtocol.h deleted file mode 100644 index 19b0bd8..0000000 --- a/app/inc/RobomasterCanProtocol.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * RobomasterCanProtocol.h - * - * Created on: May 24, 2023 - * Author: @yourName - */ - -#ifndef RobomasterCanProtocol_H -#define RobomasterCanProtocol_H - -// TODO: Fix assignment -#define CAN_ID_1 0x1U -#define CAN_ID_2 0x2U -#define CAN_ID_3 0x3U -#define CAN_ID_4 0x4U -#define CAN_ID_5 0x5U -#define CAN_ID_6 0x6U -#define CAN_ID_7 0x7U -#define CAN_ID_8 0x8U - -// Read information from a given motor -// The MSG is defined by -// FEEDBACK_ID + MotorID -#define FEEDBACK_ID 0x204 -#define FEEDBACK_ID_ALT 0x200 - -// Sent control output to motors -#define CONTROL_ID_1FF 0x1FF -#define CONTROL_ID_2FF 0x2FF -#define CONTROL_ID_200 0x200 - -// Read motor status -#define CONTROL_ANGLE_BEND 0x0 -#define CONTROL_ANGLE_LEND 0x1 - -#define CONTROL_SPEED_BEND 0x2 -#define CONTROL_SPEED_LEND 0x3 - -// TODO: SET AS TORQUE? -#define READ_CURRENT_BEND 0x4 -#define READ_CURRENT_LEND 0x5 - -#define READ_TEMP 0x6 -// Data Length Code 8 BYTES -#define RM_DLC 0x8 - -typedef struct { - uint16_t ecd; - int16_t speed_rpm; - int16_t current; - uint8_t temperature; - int16_t last_ecd; -} rmMotorFeedback; - -#endif /* RobomasterCanProtocol_H */ \ No newline at end of file diff --git a/app/src/remote_control.c b/app/src/remote_control.c index b28835f..712bb4a 100644 --- a/app/src/remote_control.c +++ b/app/src/remote_control.c @@ -2,39 +2,36 @@ #include #include +#include "CommunicationStructs.h" #include "cmsis_os.h" #include "usart.h" -uint8_t UART1_rxBuffer[12]; -UART_HandleTypeDef uart; -osMessageQId remoteQueue; -osMessageQDef(remoteQueue, 16, unsigned int); +control_data control1; +UART_HandleTypeDef huart1; +uint8_t UART1_rxBuffer; -#pragma pack(push, 1) -struct control_data { - uint8_t joystickA; - uint8_t joystickB; - uint8_t knobA; - uint8_t knobB; - uint8_t switchA; - uint8_t switchB; - uint8_t switchC; - uint8_t switchD; -} control_data; -#pragma pack(pop) +osMessageQDef(remoteQueue, 16, unsigned int); void UART_Init(void) {} +void sbus_translate(control_data* _control, uint8_t* _buffer) { + _control->joystickA = (_buffer[0] >> 1) & 0x7F; // 7bits + _control->joystickB = ((_buffer[0] & 0x01) << 6) | ((_buffer[1] >> 2) & 0x3F); // 7bits + _control->switchA = (_buffer[1] >> 1) & 0x01; // 2bits + _control->switchB = _buffer[1] & 0x01; // 2bits + _control->switchC = (_buffer[2] >> 6) & 0x03; // 4bits + _control->switchD = (_buffer[2] >> 4) & 0x03; // 4bits + _control->knobA = _buffer[3]; // 8bits + _control->knobB = _buffer[4]; // 8bits +} + void UART_Task(void* argument) { - HAL_UART_Receive_IT(&uart, UART1_rxBuffer, 12); + HAL_UART_Receive_IT(&huart1, &UART1_rxBuffer, 12); remoteQueue = osMessageCreate(osMessageQ(remoteQueue), NULL); for (;;) { + sbus_translate(&control1, &UART1_rxBuffer); + osMessagePut(remoteQueue, (unsigned int)&control1, osWaitForever); } } -void HAL_UART_RxCpltCallback(UART_HandleTypeDef* uart) { - HAL_UART_Receive_IT(uart, UART1_rxBuffer, 12); - memcpy(&control_data, &UART1_rxBuffer, sizeof(control_data)); - osMessagePut(remoteQueue, (unsigned int)&control_data, osWaitForever); - HAL_UART_Transmit(uart, (uint8_t*)"\n\nSent from ISR\n\n", 17, 500); -} +void HAL_UART_RxCpltCallback(UART_HandleTypeDef* uart) { HAL_UART_Receive_IT(&huart1, &UART1_rxBuffer, 12); } From 42f7ae77430abfd3a2b8ba3555eca7b0d12ed7b6 Mon Sep 17 00:00:00 2001 From: fabianglz993 Date: Thu, 3 Apr 2025 20:48:16 +0000 Subject: [PATCH 5/5] Updated sbus translate funciton and added pools --- app/inc/CommunicationStructs.h | 7 +++++-- app/src/remote_control.c | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/inc/CommunicationStructs.h b/app/inc/CommunicationStructs.h index 21cc703..f8937c4 100644 --- a/app/inc/CommunicationStructs.h +++ b/app/inc/CommunicationStructs.h @@ -40,8 +40,10 @@ typedef struct { */ #pragma pack(push, 1) typedef struct control_data { - uint8_t joystickA; - uint8_t joystickB; + uint8_t joystickAx; + uint8_t joystickAy; + uint8_t joystickBx; + uint8_t joystickBy; uint8_t knobA; uint8_t knobB; uint8_t switchA; @@ -54,6 +56,7 @@ typedef struct control_data { // Message queues from protocols extern osPoolId can_rx_mpool; extern osPoolId can_tx_mpool; +extern osPoolId remote_pool; extern osMessageQId outputQueueChassis; extern osMessageQId inputQueueChassis; diff --git a/app/src/remote_control.c b/app/src/remote_control.c index 712bb4a..a815b02 100644 --- a/app/src/remote_control.c +++ b/app/src/remote_control.c @@ -15,18 +15,21 @@ osMessageQDef(remoteQueue, 16, unsigned int); void UART_Init(void) {} void sbus_translate(control_data* _control, uint8_t* _buffer) { - _control->joystickA = (_buffer[0] >> 1) & 0x7F; // 7bits - _control->joystickB = ((_buffer[0] & 0x01) << 6) | ((_buffer[1] >> 2) & 0x3F); // 7bits - _control->switchA = (_buffer[1] >> 1) & 0x01; // 2bits - _control->switchB = _buffer[1] & 0x01; // 2bits - _control->switchC = (_buffer[2] >> 6) & 0x03; // 4bits - _control->switchD = (_buffer[2] >> 4) & 0x03; // 4bits - _control->knobA = _buffer[3]; // 8bits - _control->knobB = _buffer[4]; // 8bits + _control->joystickAx = ((_buffer[1] >> 3) | (_buffer[2] << 5)) & 0xff; + _control->joystickAy = ((_buffer[2] >> 6) | (_buffer[3] << 2) | (_buffer[4] << 10)) & 0xff; + _control->joystickBx = ((_buffer[4] >> 1) | (_buffer[5] << 7)) & 0x0ff; + _control->joystickBy = ((_buffer[5] >> 1) | (_buffer[6] << 7)) & 0x0ff; + _control->knobA = ((_buffer[6] >> 7) | (_buffer[7] << 1)) & 0xff; + _control->knobB = ((_buffer[7]) | (_buffer[8])) & 0xff; + _control->switchA = (_buffer[9] & 0x07) >> 2; + _control->switchB = (_buffer[10] & 0x20) >> 5; + _control->switchC = ((_buffer[12] >> 3) & 0x03); + _control->switchD = ((_buffer[13] & 0x08) >> 3); } void UART_Task(void* argument) { HAL_UART_Receive_IT(&huart1, &UART1_rxBuffer, 12); + osPoolDef(remote_pool, 16, control_data); remoteQueue = osMessageCreate(osMessageQ(remoteQueue), NULL); for (;;) { sbus_translate(&control1, &UART1_rxBuffer);