From a120d2c4449328b569f40842f76730422f8b4242 Mon Sep 17 00:00:00 2001 From: DiegoLizarraga Date: Wed, 5 Feb 2025 19:27:22 -0600 Subject: [PATCH 1/8] nuevos cambios --- Middlewares/owlware | 2 +- app/src/spi2 | 43 +++++++++++++++++++++++++++++++++++++++++++ robotConfig | 2 +- 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 app/src/spi2 diff --git a/Middlewares/owlware b/Middlewares/owlware index e5a6928..ee55f02 160000 --- a/Middlewares/owlware +++ b/Middlewares/owlware @@ -1 +1 @@ -Subproject commit e5a69282aba578d68f7fb33fe6973eef60d6f9e8 +Subproject commit ee55f02e9db76e4344b55dd508d086d25c9fe6e6 diff --git a/app/src/spi2 b/app/src/spi2 new file mode 100644 index 0000000..b2e5ec6 --- /dev/null +++ b/app/src/spi2 @@ -0,0 +1,43 @@ +#include "main.h" +#include "robotconfig.h" + + +//quite el struct porque esta en robot config + +SPI_Config SPI1_Config = { + .spiHandle = &hspi1, + .csPort = SPI1_CS_PORT, + .csPin = SPI1_CS_PIN +}; + +// Inicia SPI +void SPI_Init(SPI_Config* config) { + HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_SET); +} + +// envia y recibe +uint8_t SPI_Transfer(SPI_Config* config, uint8_t data) { + uint8_t receivedData; + HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_RESET); + HAL_SPI_TransmitReceive(config->spiHandle, &data, &receivedData, 1, HAL_MAX_DELAY); + HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_SET); + return receivedData; +} + +int main(void) { + HAL_Init(); + tim(); //voy a creer que es lo del relog + MX_GPIO_Init(); + MX_SPI1_Init(); + + + SPI_Init(&SPI1_Config); + + while (1) { + ///preguntar a anita el comando + uint8_t command = 0x01; + uint8_t response = SPI_Transfer(&SPI1_Config, command); + + HAL_Delay(1000); // awanta 1 segundo + } +} diff --git a/robotConfig b/robotConfig index 4eb10cf..17e1700 160000 --- a/robotConfig +++ b/robotConfig @@ -1 +1 @@ -Subproject commit 4eb10cf505c67e5eef39c2dd317c92f00899dd89 +Subproject commit 17e17007c0865acd1ab07c0f0c7806c297789705 From bef362dc920a9e7a00c2ccfc84a9707372726c98 Mon Sep 17 00:00:00 2001 From: DiegoLizarraga Date: Sat, 15 Feb 2025 18:19:57 -0600 Subject: [PATCH 2/8] favor de agregedirme padrino, cree un archivo .h en para eso de las variables que mande a llamar para el c. tambien puse lo que me indico anita sobre los datos que recibimos de IMU, tambien en el .h puse para conseguir los datos de los sensores que se leen en spi. tambien ingrese las cosas del semaforo adaptado a freertos cmsis v2. en general puse todas las variables ultilizable con los metodos. asi que todo joya que dios lo bendiga --- app/inc/SPI_task.h | 70 +++++++++++++++++++++++++++++++++++++++++++ app/src/SPI_task.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++ app/src/spi2 | 43 --------------------------- 3 files changed, 144 insertions(+), 43 deletions(-) create mode 100644 app/inc/SPI_task.h create mode 100644 app/src/SPI_task.c delete mode 100644 app/src/spi2 diff --git a/app/inc/SPI_task.h b/app/inc/SPI_task.h new file mode 100644 index 0000000..abdd53c --- /dev/null +++ b/app/inc/SPI_task.h @@ -0,0 +1,70 @@ +#include "cmsis_os.h" +#include "robotconfig/inc/spi.h" +//esta llamada no existe pero cuando me mezcle por la branch puede que no haya problema + + +float vel_x, float vel_y, float vel_z; +float acce_x,float acce_y,float acce_z; + + +//--------------------------------------------------------------- +//la cosa del semaforo que debo implementar +void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c); + +void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c); + +static void Task1(void *argument); + +//------------------------------------------------------------------------- + + + //Funciones para manejar SPI y datos del robot +void SPI_Init(SPI_HandleTypeDef *hspi); // Inicializar SPI +void SPI_ReadData(SPI_HandleTypeDef *hspi, uint8_t *buffer, uint16_t len); // Leer datos SPI (le puse el nombre de read data) +void SPI_WriteSensor(SPI_HandleTypeDef *hspi, uint8_t *data, uint16_t len); // Escribir datos SPI + + +//Declaracion de funciones de SPI +int16_t INU_ReadResgister16(uinit8_t reg); + +void ProcesarDatos(){ + +//leer datos del acelerometro +int16_t ax= IMU_ReadREegister16(0x3B); //Eje X +int16_t ay= IMU_ReadREegister16(0x3D); //Eje y +int16_t az= IMU_ReadREegister16(0x3F); //Eje z + +//leer datos del giroscopio +int16_t gx= IMU_ReadREegister16(0x43); //Eje X +int16_t gy= IMU_ReadREegister16(0x45); //Eje y +int16_t gz= IMU_ReadREegister16(0x47); //Eje z + +//convertir los datos a unidade fisicas +float x1g= ax * (9.81f / 16384.0f); +float y1g= ay * (9.81f / 16384.0f); +float z1g= az * (9.81f / 16384.0f); + +float x2g= gx * (9.81f / 16384.0f); +float y2g= gy * (9.81f / 16384.0f); +float z2g= gz * (9.81f / 16384.0f); + + +}; // Calcular velocidad y aceleración + + +//a lo que invesige el metodo de conseguir los datos +void ReadData(float *x1, float *y1, float *z1, + float *x2, float *y2, float *z2) { +uint8_t buffer[40]; +uint8_t command = 0x02; +SPI_Transfer(&robotConfig, command); +for (int i = 0; i < 40; i++) { +buffer[i] = SPI_Transfer(&SPI1_Config, 0x00); +} +*x1 = *((float*)&buffer[0]); +*y1 = *((float*)&buffer[4]); +*z1 = *((float*)&buffer[8]); +*x2 = *((float*)&buffer[12]); +*y2 = *((float*)&buffer[16]); +*z2 = *((float*)&buffer[20]); +} \ No newline at end of file diff --git a/app/src/SPI_task.c b/app/src/SPI_task.c new file mode 100644 index 0000000..02bc88f --- /dev/null +++ b/app/src/SPI_task.c @@ -0,0 +1,74 @@ +#include "main.h" +#include "robotconfig.h" +#include "SPI_task.h" + + +SPI_Config SPI1_Config = { + .spiHandle = &hspi1, + .csPort = SPI1_CS_PORT, + .csPin = SPI1_CS_PIN +}; + +// Inicia SPI +void SPI_Init(SPI_Config* config) { + HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_SET); +} + +// envia y recibe +uint8_t SPI_Transfer(SPI_Config* config, uint8_t data) { + uint8_t receivedData; + HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_RESET); + HAL_SPI_TransmitReceive(config->spiHandle, &data, &receivedData, 1, HAL_MAX_DELAY); + HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_SET); + return receivedData; +} +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); + } +} + + +int main(void) { + HAL_Init(); + tim(); + MX_GPIO_Init(); + MX_SPI1_Init(); + + + SPI_Init(&SPI1_Config); + //esta parte para mandar a llamar al semaforo que esta guardado + I2C_semaphore = osSemaphoreNew(1, 0, NULL); + + osKernelInitialize(); + osThreadNew(Task1, NULL, NULL); + osKernelStart(); + + //aqui la repeticion de spi + while (1) { + + uint8_t command = 0x01; + uint8_t response = SPI_Transfer(&SPI1_Config, command); + + HAL_Delay(1000); + } +} diff --git a/app/src/spi2 b/app/src/spi2 deleted file mode 100644 index b2e5ec6..0000000 --- a/app/src/spi2 +++ /dev/null @@ -1,43 +0,0 @@ -#include "main.h" -#include "robotconfig.h" - - -//quite el struct porque esta en robot config - -SPI_Config SPI1_Config = { - .spiHandle = &hspi1, - .csPort = SPI1_CS_PORT, - .csPin = SPI1_CS_PIN -}; - -// Inicia SPI -void SPI_Init(SPI_Config* config) { - HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_SET); -} - -// envia y recibe -uint8_t SPI_Transfer(SPI_Config* config, uint8_t data) { - uint8_t receivedData; - HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_RESET); - HAL_SPI_TransmitReceive(config->spiHandle, &data, &receivedData, 1, HAL_MAX_DELAY); - HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_SET); - return receivedData; -} - -int main(void) { - HAL_Init(); - tim(); //voy a creer que es lo del relog - MX_GPIO_Init(); - MX_SPI1_Init(); - - - SPI_Init(&SPI1_Config); - - while (1) { - ///preguntar a anita el comando - uint8_t command = 0x01; - uint8_t response = SPI_Transfer(&SPI1_Config, command); - - HAL_Delay(1000); // awanta 1 segundo - } -} From a210819b4daf992dc5f34b917d68245b41dbe339 Mon Sep 17 00:00:00 2001 From: DiegoLizarraga Date: Sat, 15 Feb 2025 18:22:42 -0600 Subject: [PATCH 3/8] favor de agregedirme padrino, cree un archivo .h en para eso de las variables que mande a llamar para el c. tambien puse lo que me indico anita sobre los datos que recibimos de IMU, tambien en el .h puse para conseguir los datos de los sensores que se leen en spi. tambien ingrese las cosas del semaforo adaptado a freertos cmsis v2. en general puse todas las variables ultilizable con los metodos. asi que todo joya que dios lo bendiga --- app/inc/SPI_task.h | 15 ++++++++------- app/src/SPI_task.c | 4 ++-- app/src/main.c | 5 +++++ robotConfig | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/inc/SPI_task.h b/app/inc/SPI_task.h index abdd53c..d53f0d6 100644 --- a/app/inc/SPI_task.h +++ b/app/inc/SPI_task.h @@ -3,8 +3,9 @@ //esta llamada no existe pero cuando me mezcle por la branch puede que no haya problema -float vel_x, float vel_y, float vel_z; -float acce_x,float acce_y,float acce_z; +float vel_x, float vel_y, float vel_z; //velocidad variables +float acce_x,float acce_y,float acce_z; //aceleracion variables +float gir_x, float gir_y, float gir_z; //giroscopio variable //--------------------------------------------------------------- @@ -30,20 +31,20 @@ int16_t INU_ReadResgister16(uinit8_t reg); void ProcesarDatos(){ //leer datos del acelerometro -int16_t ax= IMU_ReadREegister16(0x3B); //Eje X +int16_t acce_x= IMU_ReadREegister16(0x3B); //Eje X int16_t ay= IMU_ReadREegister16(0x3D); //Eje y int16_t az= IMU_ReadREegister16(0x3F); //Eje z //leer datos del giroscopio -int16_t gx= IMU_ReadREegister16(0x43); //Eje X -int16_t gy= IMU_ReadREegister16(0x45); //Eje y -int16_t gz= IMU_ReadREegister16(0x47); //Eje z +int16_t gir_x= IMU_ReadREegister16(0x43); //Eje X +int16_t gir_y= IMU_ReadREegister16(0x45); //Eje y +int16_t gir_z= IMU_ReadREegister16(0x47); //Eje z //convertir los datos a unidade fisicas float x1g= ax * (9.81f / 16384.0f); float y1g= ay * (9.81f / 16384.0f); float z1g= az * (9.81f / 16384.0f); - +//quedan pendiente de asignacion para su uso float x2g= gx * (9.81f / 16384.0f); float y2g= gy * (9.81f / 16384.0f); float z2g= gz * (9.81f / 16384.0f); diff --git a/app/src/SPI_task.c b/app/src/SPI_task.c index 02bc88f..18e2c3b 100644 --- a/app/src/SPI_task.c +++ b/app/src/SPI_task.c @@ -60,8 +60,8 @@ int main(void) { I2C_semaphore = osSemaphoreNew(1, 0, NULL); osKernelInitialize(); - osThreadNew(Task1, NULL, NULL); - osKernelStart(); + task1Handle = osThreadNew(Task1, NULL, NULL); //actualiazado a freertos cmsis v2 + osKernelStart(); // Inicia el planificador de tareas //aqui la repeticion de spi while (1) { diff --git a/app/src/main.c b/app/src/main.c index 5e2955a..b05c753 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -111,7 +111,12 @@ int main(void) { osThreadDef(THREAD_1, BlinkyThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE); LEDThread1Handle = osThreadCreate(osThread(THREAD_1), NULL); + //esta parte para mandar a llamar al semaforo que esta guardado + I2C_semaphore = osSemaphoreNew(1, 0, NULL); + osKernelInitialize(); + osThreadNew(Task1, NULL, NULL); + osKernelStart(); // Start the RTOS kernel osKernelStart(); diff --git a/robotConfig b/robotConfig index 17e1700..764c826 160000 --- a/robotConfig +++ b/robotConfig @@ -1 +1 @@ -Subproject commit 17e17007c0865acd1ab07c0f0c7806c297789705 +Subproject commit 764c826e2f26e1d07ee84755f5f9cb2b6011264f From 61127b720f543c5f232d2d51b0912b296b42b8f2 Mon Sep 17 00:00:00 2001 From: DiegoLizarraga Date: Sat, 1 Mar 2025 18:11:52 +0000 Subject: [PATCH 4/8] se corrigieron errores de CMSIS-RTOS que estaban en v2 y se cambio a v1 --- app/inc/SPI_task.h | 124 ++++++++++++++++++++------------------------- app/src/main.c | 63 +++++++++++++++++++---- 2 files changed, 106 insertions(+), 81 deletions(-) diff --git a/app/inc/SPI_task.h b/app/inc/SPI_task.h index d53f0d6..3ecc233 100644 --- a/app/inc/SPI_task.h +++ b/app/inc/SPI_task.h @@ -1,71 +1,55 @@ -#include "cmsis_os.h" -#include "robotconfig/inc/spi.h" -//esta llamada no existe pero cuando me mezcle por la branch puede que no haya problema - - -float vel_x, float vel_y, float vel_z; //velocidad variables -float acce_x,float acce_y,float acce_z; //aceleracion variables -float gir_x, float gir_y, float gir_z; //giroscopio variable - - -//--------------------------------------------------------------- -//la cosa del semaforo que debo implementar -void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c); - -void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c); - -static void Task1(void *argument); - -//------------------------------------------------------------------------- - - - //Funciones para manejar SPI y datos del robot -void SPI_Init(SPI_HandleTypeDef *hspi); // Inicializar SPI -void SPI_ReadData(SPI_HandleTypeDef *hspi, uint8_t *buffer, uint16_t len); // Leer datos SPI (le puse el nombre de read data) -void SPI_WriteSensor(SPI_HandleTypeDef *hspi, uint8_t *data, uint16_t len); // Escribir datos SPI - - -//Declaracion de funciones de SPI -int16_t INU_ReadResgister16(uinit8_t reg); - -void ProcesarDatos(){ - -//leer datos del acelerometro -int16_t acce_x= IMU_ReadREegister16(0x3B); //Eje X -int16_t ay= IMU_ReadREegister16(0x3D); //Eje y -int16_t az= IMU_ReadREegister16(0x3F); //Eje z - -//leer datos del giroscopio -int16_t gir_x= IMU_ReadREegister16(0x43); //Eje X -int16_t gir_y= IMU_ReadREegister16(0x45); //Eje y -int16_t gir_z= IMU_ReadREegister16(0x47); //Eje z - -//convertir los datos a unidade fisicas -float x1g= ax * (9.81f / 16384.0f); -float y1g= ay * (9.81f / 16384.0f); -float z1g= az * (9.81f / 16384.0f); -//quedan pendiente de asignacion para su uso -float x2g= gx * (9.81f / 16384.0f); -float y2g= gy * (9.81f / 16384.0f); -float z2g= gz * (9.81f / 16384.0f); - - -}; // Calcular velocidad y aceleración - - -//a lo que invesige el metodo de conseguir los datos -void ReadData(float *x1, float *y1, float *z1, - float *x2, float *y2, float *z2) { -uint8_t buffer[40]; -uint8_t command = 0x02; -SPI_Transfer(&robotConfig, command); -for (int i = 0; i < 40; i++) { -buffer[i] = SPI_Transfer(&SPI1_Config, 0x00); -} -*x1 = *((float*)&buffer[0]); -*y1 = *((float*)&buffer[4]); -*z1 = *((float*)&buffer[8]); -*x2 = *((float*)&buffer[12]); -*y2 = *((float*)&buffer[16]); -*z2 = *((float*)&buffer[20]); +#include "cmsis_os.h" +#include "spi.h" + + +typedef struct { + SPI_HandleTypeDef* spiHandle; + GPIO_TypeDef* csPort; + uint16_t csPin; +} SPI_Config; +uint8_t buffer[40]; +float vel_x, vel_y, vel_z; //velocidad variables +float acce_x, acce_y, acce_z; //aceleracion variables +float gir_x, gir_y, gir_z; //giroscopio variable + + +/*---------------------------------------------------------------*/ +/*la cosa del semaforo que debo implementar*/ +void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c); + +void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c); + +static void Task1(void *argument); + +//*-----------------------------------------------------------------------*/ + + + //Funciones para manejar SPI y datos del robot +void SPI_Init(SPI_HandleTypeDef *hspi); // Inicializar SPI +void SPI_ReadData(SPI_HandleTypeDef *hspi, uint8_t *buffer, uint16_t len); // Leer datos SPI (le puse el nombre de read data) +void SPI_WriteSensor(SPI_HandleTypeDef *hspi, uint8_t *data, uint16_t len); // Escribir datos SPI + + +//Declaracion de funciones de SPI +int16_t INU_ReadResgister16(uint8_t reg); + + // Calcular velocidad y aceleración + + +//a lo que invesige el metodo de conseguir los datos +void ReadData(float *x1, float *y1, float *z1, + float *x2, float *y2, float *z2) { +//si falla lo de 0x02 probar poner command que esta declarado arriba + +uint8_t command = 0x02; +SPI_Transfer(&hspi2, 0x02); //aqui dice ques no esta definido +for (int i = 0; i < 40; i++) { +buffer[i] = SPI_Transfer(&hspi2, 0x00); +} +*x1 = *((float*)&buffer[0]); +*y1 = *((float*)&buffer[4]); +*z1 = *((float*)&buffer[8]); +*x2 = *((float*)&buffer[12]); +*y2 = *((float*)&buffer[16]); +*z2 = *((float*)&buffer[20]); } \ No newline at end of file diff --git a/app/src/main.c b/app/src/main.c index b05c753..50dfd43 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -27,7 +27,8 @@ // #include // CMSIS Include #include "cmsis_os.h" - +#include "SPI_task.h" +#include "spi.h" /* Standard includes. */ #include @@ -87,6 +88,45 @@ static void BlinkyThread(void const* argument) { * @brief The application entry point. * @retval int */ +//la inilizacion del semaforo +osThreadId tid_thread1; // ID for thread 1 +osThreadId tid_thread2; // ID for thread 2 + +osSemaphoreId semaphore; // Semaphore ID +osSemaphoreDef(semaphore); // Semaphore definition +void thread1 (void const *argument) { + int32_t value; + while (1) { + osDelay(3); // Pass control to other tasks for 3ms + value = osSemaphoreWait(semaphore, 1);// Wait 1ms for the free semaphore + if (value > 0) { + // If there was no time-out the semaphore was acquired + // OK, the interface is free now, use it. + osSemaphoreRelease (semaphore); // Return a token back to a semaphore + } + } + } + + /* + Thread 2 - Normal Priority - looks for a free semaphore and uses + the resource whenever it is available + */ + void thread2 (void const *argument) { + while (1) { + osSemaphoreWait (semaphore, osWaitForever); // Wait indefinitely for a free semaphore + // OK, the interface is free now, use it. + + osSemaphoreRelease (semaphore); // Return a token back to a semaphore. + } + } + +osThreadDef(thread1, osPriorityHigh, 1, 0); +osThreadDef(thread2, osPriorityNormal, 1, 0); +osThreadDef(Task1, osPriorityNormal, 1, 0); //la task 1 ya esta en el spi_task.c hecho + + +//Esta función se usa para definir tareas antes de crearlas. ya lo pase a la CMSIS-RTOS 1 pero sigue sin querer xd + int main(void) { /* MCU Configuration--------------------------------------------------------*/ @@ -103,28 +143,29 @@ int main(void) { MX_TIM3_Init(); MX_TIM4_Init(); MX_USART2_UART_Init(); + MX_SPI1_Init(); //lo copie directo spi.c esta declarado como una funcion ahi pero dice que no esta definida + /* Infinite loop */ // osKernelInitialize(); + SPI_Config(); - // Create the Blinky thread - osThreadDef(THREAD_1, BlinkyThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE); - - LEDThread1Handle = osThreadCreate(osThread(THREAD_1), NULL); - //esta parte para mandar a llamar al semaforo que esta guardado - I2C_semaphore = osSemaphoreNew(1, 0, NULL); + semaphore = osSemaphoreCreate(osSemaphore(semaphore), 1); + tid_thread1 = osThreadCreate(osThread(thread1), NULL); + tid_thread2 = osThreadCreate(osThread(thread2), NULL); osKernelInitialize(); - osThreadNew(Task1, NULL, NULL); - osKernelStart(); - // Start the RTOS kernel - osKernelStart(); + + osThreadId id1 = osThreadCreate(osThread(Task1), NULL); + +osKernelStart(); // Iniciar el scheduler del RTOS v1 // This is a fake comment, delete for (;;) { /* Should not reach here. */ } + return 0; } From 2b346103c47e16827cf5ad93829c83540f71c58d Mon Sep 17 00:00:00 2001 From: DiegoLizarraga Date: Sat, 1 Mar 2025 18:26:40 +0000 Subject: [PATCH 5/8] y esta corregi mis errores, cada dia mas cerca de ser como el santi mi patron uwu --- app/inc/SPI_task.h | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/app/inc/SPI_task.h b/app/inc/SPI_task.h index 3ecc233..b6175b0 100644 --- a/app/inc/SPI_task.h +++ b/app/inc/SPI_task.h @@ -1,6 +1,7 @@ #include "cmsis_os.h" #include "spi.h" +extern SPI_HandleTypeDef hspi2; //esta en el archivo de spi typedef struct { SPI_HandleTypeDef* spiHandle; @@ -39,17 +40,28 @@ int16_t INU_ReadResgister16(uint8_t reg); //a lo que invesige el metodo de conseguir los datos void ReadData(float *x1, float *y1, float *z1, float *x2, float *y2, float *z2) { -//si falla lo de 0x02 probar poner command que esta declarado arriba + + +//correccion de las cosas del buffer uint8_t command = 0x02; -SPI_Transfer(&hspi2, 0x02); //aqui dice ques no esta definido +uint8_t buffer[40]; + +// Enviar comando +uint8_t txData = 0x02; +uint8_t rxData; +HAL_SPI_TransmitReceive(&hspi2, &txData, &rxData, 1, HAL_MAX_DELAY); + +// Leer 40 bytes de datos for (int i = 0; i < 40; i++) { -buffer[i] = SPI_Transfer(&hspi2, 0x00); + txData = 0x00; // Enviar datos vacíos para recibir respuesta + HAL_SPI_TransmitReceive(&hspi2, &txData, &buffer[i], 1, HAL_MAX_DELAY); } -*x1 = *((float*)&buffer[0]); -*y1 = *((float*)&buffer[4]); -*z1 = *((float*)&buffer[8]); -*x2 = *((float*)&buffer[12]); -*y2 = *((float*)&buffer[16]); -*z2 = *((float*)&buffer[20]); -} \ No newline at end of file + +// Convertir los datos en valores float +float *x1 = (float*)&buffer[0]; +float *y1 = (float*)&buffer[4]; +float *z1 = (float*)&buffer[8]; +float *x2 = (float*)&buffer[12]; +float *y2 = (float*)&buffer[16]; +float *z2 = (float*)&buffer[20]; \ No newline at end of file From 0e77947c18a66f971605b37bd7d1688117b77235 Mon Sep 17 00:00:00 2001 From: DiegoLizarraga Date: Sat, 1 Mar 2025 20:41:21 +0000 Subject: [PATCH 6/8] jorge me va pegar un sapisa despues de moverle a main xd --- app/src/main.c | 102 +++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 46 deletions(-) diff --git a/app/src/main.c b/app/src/main.c index 50dfd43..0ad78c8 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -43,13 +43,32 @@ /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ -void SystemClock_Config(void); + typedef enum { THREAD_1 = 0, THREAD_2 } Thread_TypeDef; osThreadId LEDThread1Handle; +osThreadId tid_thread1; +osThreadId tid_thread2; +osThreadId id1; + +osSemaphoreId semaphore; // Semaphore ID +osSemaphoreDef(semaphore); // Semaphore definition +void SystemClock_Config(void); static void BlinkyThread(void const* argument); +void thread1(void const* argument); +void thread2(void const* argument); + +/* Definición de la macro osThreadDef */ +#define osThreadDef(name, priority, instances, stacksz) \ + const osThreadDef_t os_thread_def_##name = { \ + .name = #name, \ + .pthread = name, \ + .tpriority = priority, \ + .instances = instances, \ + .stacksize = stacksz \ + } /*-----------------------------------------------------------*/ // osThreadId_t defaultTaskHandle; @@ -60,6 +79,7 @@ static void BlinkyThread(void const* argument); // }; // void BlinkyThread(void* argument); /*-----------------------------------------------------------*/ + static void BlinkyThread(void const* argument) { HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET); while (1) { @@ -88,45 +108,39 @@ static void BlinkyThread(void const* argument) { * @brief The application entry point. * @retval int */ -//la inilizacion del semaforo -osThreadId tid_thread1; // ID for thread 1 -osThreadId tid_thread2; // ID for thread 2 - -osSemaphoreId semaphore; // Semaphore ID -osSemaphoreDef(semaphore); // Semaphore definition -void thread1 (void const *argument) { + + + + + void thread1(void const* argument) { int32_t value; while (1) { - osDelay(3); // Pass control to other tasks for 3ms - value = osSemaphoreWait(semaphore, 1);// Wait 1ms for the free semaphore - if (value > 0) { - // If there was no time-out the semaphore was acquired - // OK, the interface is free now, use it. - osSemaphoreRelease (semaphore); // Return a token back to a semaphore - } + osDelay(3); // Pass control to other tasks for 3ms + value = osSemaphoreWait(semaphore, 1); // Wait 1ms for the free semaphore + if (value > 0) { + // Si no hubo timeout, el semáforo fue adquirido + // Usar el recurso aquí + osSemaphoreRelease(semaphore); // Liberar el semáforo + } } - } +} - /* - Thread 2 - Normal Priority - looks for a free semaphore and uses - the resource whenever it is available - */ - void thread2 (void const *argument) { + + // thread 2 - Normal Priority - looks for a free semaphore and uses the resource whenever it is available + +void thread2(void const* argument) { while (1) { - osSemaphoreWait (semaphore, osWaitForever); // Wait indefinitely for a free semaphore - // OK, the interface is free now, use it. - - osSemaphoreRelease (semaphore); // Return a token back to a semaphore. + osSemaphoreWait(semaphore, osWaitForever); // Esperar indefinidamente por el semáforo + // Usar el recurso aquí + osSemaphoreRelease(semaphore); // Liberar el semáforo + } } - } - -osThreadDef(thread1, osPriorityHigh, 1, 0); -osThreadDef(thread2, osPriorityNormal, 1, 0); -osThreadDef(Task1, osPriorityNormal, 1, 0); //la task 1 ya esta en el spi_task.c hecho + +/*osThreadDef(thread1, osPriorityHigh, 1, 128); +osThreadDef(thread2, osPriorityNormal, 1, 128);*/ +osThreadDef(Task1, osPriorityNormal, 1, 128); -//Esta función se usa para definir tareas antes de crearlas. ya lo pase a la CMSIS-RTOS 1 pero sigue sin querer xd - int main(void) { /* MCU Configuration--------------------------------------------------------*/ @@ -145,21 +159,17 @@ int main(void) { MX_USART2_UART_Init(); MX_SPI1_Init(); //lo copie directo spi.c esta declarado como una funcion ahi pero dice que no esta definida - - /* Infinite loop */ - // osKernelInitialize(); - SPI_Config(); - + osKernelInitialize(); semaphore = osSemaphoreCreate(osSemaphore(semaphore), 1); - tid_thread1 = osThreadCreate(osThread(thread1), NULL); - tid_thread2 = osThreadCreate(osThread(thread2), NULL); - - osKernelInitialize(); - + + SPI_Config(); - osThreadId id1 = osThreadCreate(osThread(Task1), NULL); + /*Crear los hilos */ + tid_thread1 = osThreadCreate(osThread(thread1), NULL); + tid_thread2 = osThreadCreate(osThread(thread2), NULL); + osThreadId id1 = osThreadCreate(osThread(Task1), NULL); -osKernelStart(); // Iniciar el scheduler del RTOS v1 + osKernelStart(); // Iniciar el scheduler del RTOS v1 // This is a fake comment, delete for (;;) { /* Should not reach here. */ @@ -186,7 +196,7 @@ void SystemClock_Config(void) { RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { /* Initialization Error */ - while (1) + while (1){} ; } @@ -200,7 +210,7 @@ void SystemClock_Config(void) { RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { /* Initialization Error */ - while (1) + while (1){} ; } } From 66da8a06a567d3c98b8045918664cc7aaf48682b Mon Sep 17 00:00:00 2001 From: DiegoLizarraga Date: Sat, 1 Mar 2025 21:27:46 +0000 Subject: [PATCH 7/8] si me va pegar unu --- app/inc/SPI_task.h | 98 +++++++++++++++--------------- app/src/SPI_task.c | 144 ++++++++++++++++++++++----------------------- app/src/main.c | 57 ++++++++---------- 3 files changed, 144 insertions(+), 155 deletions(-) diff --git a/app/inc/SPI_task.h b/app/inc/SPI_task.h index b6175b0..a15fb01 100644 --- a/app/inc/SPI_task.h +++ b/app/inc/SPI_task.h @@ -1,67 +1,71 @@ #include "cmsis_os.h" #include "spi.h" +#ifndef __SPI_task.h__ + #define __SPI_task .h__ + #ifdef __cplusplus +extern "C" { + #endif -extern SPI_HandleTypeDef hspi2; //esta en el archivo de spi +extern SPI_HandleTypeDef hspi2; // esta en el archivo de spi typedef struct { - SPI_HandleTypeDef* spiHandle; - GPIO_TypeDef* csPort; - uint16_t csPin; + SPI_HandleTypeDef* spiHandle; + GPIO_TypeDef* csPort; + uint16_t csPin; } SPI_Config; uint8_t buffer[40]; -float vel_x, vel_y, vel_z; //velocidad variables -float acce_x, acce_y, acce_z; //aceleracion variables -float gir_x, gir_y, gir_z; //giroscopio variable - +float vel_x, vel_y, vel_z; // velocidad variables +float acce_x, acce_y, acce_z; // aceleracion variables +float gir_x, gir_y, gir_z; // giroscopio variable /*---------------------------------------------------------------*/ /*la cosa del semaforo que debo implementar*/ -void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c); +void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef* hi2c); -void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c); +void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef* hi2c); -static void Task1(void *argument); +static void Task1(void const* argument); //*-----------------------------------------------------------------------*/ +// Funciones para manejar SPI y datos del robot +void SPI_Init(SPI_HandleTypeDef* hspi); // Inicializar SPI +void SPI_ReadData(SPI_HandleTypeDef* hspi, uint8_t* buffer, + uint16_t len); // Leer datos SPI (le puse el nombre de read data) +void SPI_WriteSensor(SPI_HandleTypeDef* hspi, uint8_t* data, uint16_t len); // Escribir datos SPI - //Funciones para manejar SPI y datos del robot -void SPI_Init(SPI_HandleTypeDef *hspi); // Inicializar SPI -void SPI_ReadData(SPI_HandleTypeDef *hspi, uint8_t *buffer, uint16_t len); // Leer datos SPI (le puse el nombre de read data) -void SPI_WriteSensor(SPI_HandleTypeDef *hspi, uint8_t *data, uint16_t len); // Escribir datos SPI - - -//Declaracion de funciones de SPI +// Declaracion de funciones de SPI int16_t INU_ReadResgister16(uint8_t reg); - // Calcular velocidad y aceleración - - -//a lo que invesige el metodo de conseguir los datos -void ReadData(float *x1, float *y1, float *z1, - float *x2, float *y2, float *z2) { - - - -//correccion de las cosas del buffer -uint8_t command = 0x02; -uint8_t buffer[40]; - -// Enviar comando -uint8_t txData = 0x02; -uint8_t rxData; -HAL_SPI_TransmitReceive(&hspi2, &txData, &rxData, 1, HAL_MAX_DELAY); - -// Leer 40 bytes de datos -for (int i = 0; i < 40; i++) { - txData = 0x00; // Enviar datos vacíos para recibir respuesta - HAL_SPI_TransmitReceive(&hspi2, &txData, &buffer[i], 1, HAL_MAX_DELAY); +// Calcular velocidad y aceleración + +// a lo que invesige el metodo de conseguir los datos +void ReadData(float* x1, float* y1, float* z1, float* x2, float* y2, float* z2) { + // correccion de las cosas del buffer + uint8_t command = 0x02; + uint8_t buffer[40]; + + // Enviar comando + uint8_t txData = 0x02; + uint8_t rxData; + HAL_SPI_TransmitReceive(&hspi2, &txData, &rxData, 1, HAL_MAX_DELAY); + + // Leer 40 bytes de datos + for (int i = 0; i < 40; i++) { + txData = 0x00; // Enviar datos vacíos para recibir respuesta + HAL_SPI_TransmitReceive(&hspi2, &txData, &buffer[i], 1, HAL_MAX_DELAY); + } + + // Convertir los datos en valores float + float* x1 = (float*)&buffer[0]; + float* y1 = (float*)&buffer[4]; + float* z1 = (float*)&buffer[8]; + float* x2 = (float*)&buffer[12]; + float* y2 = (float*)&buffer[16]; + float* z2 = (float*)&buffer[20]; + + #ifdef __cplusplus } + #endif -// Convertir los datos en valores float -float *x1 = (float*)&buffer[0]; -float *y1 = (float*)&buffer[4]; -float *z1 = (float*)&buffer[8]; -float *x2 = (float*)&buffer[12]; -float *y2 = (float*)&buffer[16]; -float *z2 = (float*)&buffer[20]; \ No newline at end of file +#endif /* __SPI_task.h__ */ \ No newline at end of file diff --git a/app/src/SPI_task.c b/app/src/SPI_task.c index 18e2c3b..ad9cbbe 100644 --- a/app/src/SPI_task.c +++ b/app/src/SPI_task.c @@ -1,74 +1,70 @@ -#include "main.h" -#include "robotconfig.h" -#include "SPI_task.h" - - -SPI_Config SPI1_Config = { - .spiHandle = &hspi1, - .csPort = SPI1_CS_PORT, - .csPin = SPI1_CS_PIN -}; - -// Inicia SPI -void SPI_Init(SPI_Config* config) { - HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_SET); -} - -// envia y recibe -uint8_t SPI_Transfer(SPI_Config* config, uint8_t data) { - uint8_t receivedData; - HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_RESET); - HAL_SPI_TransmitReceive(config->spiHandle, &data, &receivedData, 1, HAL_MAX_DELAY); - HAL_GPIO_WritePin(config->csPort, config->csPin, GPIO_PIN_SET); - return receivedData; -} -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); - } -} - - -int main(void) { - HAL_Init(); - tim(); - MX_GPIO_Init(); - MX_SPI1_Init(); - - - SPI_Init(&SPI1_Config); - //esta parte para mandar a llamar al semaforo que esta guardado - I2C_semaphore = osSemaphoreNew(1, 0, NULL); - - osKernelInitialize(); - task1Handle = osThreadNew(Task1, NULL, NULL); //actualiazado a freertos cmsis v2 - osKernelStart(); // Inicia el planificador de tareas - - //aqui la repeticion de spi - while (1) { - - uint8_t command = 0x01; - uint8_t response = SPI_Transfer(&SPI1_Config, command); - - HAL_Delay(1000); - } -} +#include "SPI_task.h" + +#include + +#include "main.h" +#ifndef __SPI_task.c__ + #define __SPI_task .c__ + #ifdef __cplusplus +extern "C" { + #endif + +buffer_spi[5]; // esta en bytes +// es un array para guardar los datos del acelerometro y giroscopio en memoria + +SPI_Config SPI1_Config = {.spiHandle = &hspi2, .csPort = SPI1_CS_GPIO_Port, .csPin = SPI1_CS_Pin}; + +// funcion para leer un registro de 16 bits +uint16_t todos_addres() { + for (int i = 0; i < 128; i++) { + uint16_t data = IMU_ReadRegister8(i); + if (data != 0) { + printf("0x%02X: 0x%02X\n", i, data); + } + } +} + +// las cosas del semaforo +osSemaphoreId I2C_semaphore; // define el semaforo +osSemaphoreDef(I2C_semaphore); // define la estructura del semaforo + +void initSemaphore(void) { + I2C_semaphore = osSemaphoreCreate(osSemaphore(I2C_semaphore), 1); // Crear semáforo con 1 permiso + if (I2C_semaphore == NULL) { + } +} + +I2C_HandleTypeDef hi2c1; + +void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef* hi2c) { + if (hi2c->State == HAL_I2C_STATE_READY) { + osSemaphoreRelease(I2C_semaphore); + } +} + +void ProcesarDatos() { + // leer datos del acelerometro + int16_t ax = IMU_ReadRegister16(0x3B); // Eje X + int16_t ay = IMU_ReadRegister16(0x3D); // Eje y + int16_t az = IMU_ReadRegister16(0x3F); // Eje z + + // leer datos del giroscopio + int16_t gx = IMU_ReadRegister16(0x43); // Eje X + int16_t gy = IMU_ReadRegister16(0x45); // Eje y + int16_t gz = IMU_ReadRegister16(0x47); // Eje z + + // convertir los datos a unidade fisicas + float x1g = ax * (9.81f / 16384.0f); + float y1g = ay * (9.81f / 16384.0f); + float z1g = az * (9.81f / 16384.0f); + // quedan pendiente de asignacion para su uso + float x2g = gx * (9.81f / 16384.0f); + float y2g = gy * (9.81f / 16384.0f); + float z2g = gz * (9.81f / 16384.0f); +} + + #ifdef __cplusplus +} + #endif + +#endif /* __SPI_task.h__ */ diff --git a/app/src/main.c b/app/src/main.c index 0ad78c8..b191d3b 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -26,8 +26,8 @@ // #include // #include // CMSIS Include -#include "cmsis_os.h" #include "SPI_task.h" +#include "cmsis_os.h" #include "spi.h" /* Standard includes. */ #include @@ -44,7 +44,6 @@ /* Private function prototypes -----------------------------------------------*/ - typedef enum { THREAD_1 = 0, THREAD_2 } Thread_TypeDef; osThreadId LEDThread1Handle; @@ -52,7 +51,7 @@ osThreadId tid_thread1; osThreadId tid_thread2; osThreadId id1; -osSemaphoreId semaphore; // Semaphore ID +osSemaphoreId semaphore; // Semaphore ID osSemaphoreDef(semaphore); // Semaphore definition void SystemClock_Config(void); @@ -62,13 +61,8 @@ void thread2(void const* argument); /* Definición de la macro osThreadDef */ #define osThreadDef(name, priority, instances, stacksz) \ - const osThreadDef_t os_thread_def_##name = { \ - .name = #name, \ - .pthread = name, \ - .tpriority = priority, \ - .instances = instances, \ - .stacksize = stacksz \ - } + const osThreadDef_t os_thread_def_##name = { \ + .name = #name, .pthread = name, .tpriority = priority, .instances = instances, .stacksize = stacksz} /*-----------------------------------------------------------*/ // osThreadId_t defaultTaskHandle; @@ -109,13 +103,10 @@ static void BlinkyThread(void const* argument) { * @retval int */ - - - - void thread1(void const* argument) { +void thread1(void const* argument) { int32_t value; while (1) { - osDelay(3); // Pass control to other tasks for 3ms + osDelay(3); // Pass control to other tasks for 3ms value = osSemaphoreWait(semaphore, 1); // Wait 1ms for the free semaphore if (value > 0) { // Si no hubo timeout, el semáforo fue adquirido @@ -124,23 +115,22 @@ static void BlinkyThread(void const* argument) { } } } - - - // thread 2 - Normal Priority - looks for a free semaphore and uses the resource whenever it is available - + +// thread 2 - Normal Priority - looks for a free semaphore and uses the resource whenever it is available + void thread2(void const* argument) { while (1) { - osSemaphoreWait(semaphore, osWaitForever); // Esperar indefinidamente por el semáforo - // Usar el recurso aquí + osSemaphoreWait(semaphore, osWaitForever); // Esperar indefinidamente por el semáforo + // Usar el recurso aquí osSemaphoreRelease(semaphore); // Liberar el semáforo - } } +} -/*osThreadDef(thread1, osPriorityHigh, 1, 128); -osThreadDef(thread2, osPriorityNormal, 1, 128);*/ +/* Definición de los hilos */ +osThreadDef(thread1, osPriorityHigh, 1, 128); +osThreadDef(thread2, osPriorityNormal, 1, 128); osThreadDef(Task1, osPriorityNormal, 1, 128); - int main(void) { /* MCU Configuration--------------------------------------------------------*/ @@ -157,11 +147,11 @@ int main(void) { MX_TIM3_Init(); MX_TIM4_Init(); MX_USART2_UART_Init(); - MX_SPI1_Init(); //lo copie directo spi.c esta declarado como una funcion ahi pero dice que no esta definida - - osKernelInitialize(); + MX_SPI1_Init(); // lo copie directo spi.c esta declarado como una funcion ahi pero dice que no esta definida + + osKernelInitialize(); semaphore = osSemaphoreCreate(osSemaphore(semaphore), 1); - + SPI_Config(); /*Crear los hilos */ @@ -175,7 +165,6 @@ int main(void) { /* Should not reach here. */ } - return 0; } @@ -196,8 +185,8 @@ void SystemClock_Config(void) { RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { /* Initialization Error */ - while (1){} - ; + while (1) { + }; } /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 @@ -210,8 +199,8 @@ void SystemClock_Config(void) { RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { /* Initialization Error */ - while (1){} - ; + while (1) { + }; } } From 4716781960ca76e84e7b2cac2a06fc7a4429a3ea Mon Sep 17 00:00:00 2001 From: DiegoLizarraga Date: Tue, 1 Apr 2025 01:21:42 +0000 Subject: [PATCH 8/8] sis a ver si le atine --- app/src/SPI_task.c | 107 ++++++++++++++++++++++++++++++++++++--------- app/src/main.c | 67 +++++++++++++++++----------- 2 files changed, 128 insertions(+), 46 deletions(-) diff --git a/app/src/SPI_task.c b/app/src/SPI_task.c index ad9cbbe..8a54987 100644 --- a/app/src/SPI_task.c +++ b/app/src/SPI_task.c @@ -1,27 +1,33 @@ + + +#ifndef SPI_task_C +#define SPI_task_C + +#ifdef __cplusplus +extern "C" { +#endif + #include "SPI_task.h" #include +#include #include "main.h" -#ifndef __SPI_task.c__ - #define __SPI_task .c__ - #ifdef __cplusplus -extern "C" { - #endif -buffer_spi[5]; // esta en bytes -// es un array para guardar los datos del acelerometro y giroscopio en memoria +// unsigned char buffer_spi[40]; // esta en bytes este buffer solo sera para el spi ya tenemos uno en el task.h +// es un array para guardar los datos del acelerometro y giroscopio en memoria SPI_Config SPI1_Config = {.spiHandle = &hspi2, .csPort = SPI1_CS_GPIO_Port, .csPin = SPI1_CS_Pin}; // funcion para leer un registro de 16 bits uint16_t todos_addres() { for (int i = 0; i < 128; i++) { - uint16_t data = IMU_ReadRegister8(i); + uint16_t data = IMU_ReadResgister16(i); if (data != 0) { - printf("0x%02X: 0x%02X\n", i, data); + // printf("0x %02X: 0x%02X\n", i, data); } } + return 0; } // las cosas del semaforo @@ -41,17 +47,44 @@ void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef* hi2c) { osSemaphoreRelease(I2C_semaphore); } } +// proceso de empaquetados de datos con los datos del acelerometro y giroscopio que recibimos +// creacion strucct +typedef struct { + float vel_x, vel_y, vel_z; // velocidad variables + float ace_x, ace_y, ace_z; // aceleracion variables + float gir_x, gir_y, gir_z; // giroscopio variable +} IMU_Data_t; + +// Definir la cola +osMessageQId imuQueue; +#define QUEUE_SIZE 10 + +// Definir la estructura de la cola +osMessageQDef(imuQueue, QUEUE_SIZE, IMU_Data_t); + +// Pool de memoria para los datos del IMU +IMU_Data_t imuDataPool[QUEUE_SIZE]; +uint8_t poolIndex = 0; + +// Función para inicializar la cola +void initQueue(void) { + imuQueue = osMessageCreate(osMessageQ(imuQueue), NULL); + if (imuQueue == NULL) { + // Manejar el error si la cola no se pudo crear + } +} -void ProcesarDatos() { - // leer datos del acelerometro - int16_t ax = IMU_ReadRegister16(0x3B); // Eje X - int16_t ay = IMU_ReadRegister16(0x3D); // Eje y - int16_t az = IMU_ReadRegister16(0x3F); // Eje z +// funcion para inicializar el SPI +void ProcesarDatos(void) { + // Leer datos del acelerómetro + int16_t ax = IMU_ReadResgister16(0x3B); // Eje X + int16_t ay = IMU_ReadResgister16(0x3D); // Eje Y + int16_t az = IMU_ReadResgister16(0x3F); // Eje Z - // leer datos del giroscopio - int16_t gx = IMU_ReadRegister16(0x43); // Eje X - int16_t gy = IMU_ReadRegister16(0x45); // Eje y - int16_t gz = IMU_ReadRegister16(0x47); // Eje z + // Leer datos del giroscopio + int16_t gx = IMU_ReadResgister16(0x43); // Eje X + int16_t gy = IMU_ReadResgister16(0x45); // Eje Y + int16_t gz = IMU_ReadResgister16(0x47); // Eje Z // convertir los datos a unidade fisicas float x1g = ax * (9.81f / 16384.0f); @@ -61,10 +94,42 @@ void ProcesarDatos() { float x2g = gx * (9.81f / 16384.0f); float y2g = gy * (9.81f / 16384.0f); float z2g = gz * (9.81f / 16384.0f); + + // Almacenar los datos en el struct + IMU_Data_t* imuData = &imuDataPool[poolIndex]; + imuData->ace_x = x1g; + imuData->ace_y = y1g; + imuData->ace_z = z1g; + imuData->gir_x = x2g; + imuData->gir_y = y2g; + imuData->gir_z = z2g; + + // Enviar los datos a la cola + osStatus status = osMessagePut(imuQueue, (uint32_t)imuData, 0); + if (status != osOK) { + // Manejar el error si no se pudo enviar el mensaje + } + + // Actualizar el índice del pool de memoria + poolIndex = (poolIndex + 1) % QUEUE_SIZE; +} +void Task_ProcessIMUData(void const* argument) { + while (1) { + // Esperar a recibir un mensaje de la cola + osEvent event = osMessageGet(imuQueue, osWaitForever); + if (event.status == osEventMessage) { + // Obtener el puntero al struct + IMU_Data_t* imuData = (IMU_Data_t*)event.value.v; + + // Procesar los datos recibidos + // printf("Accel X: %f, Accel Y: %f, Accel Z: %f\n", imuData->ace_x, imuData->ace_y, imuData->ace_z); + // printf("Gyro X: %f, Gyro Y: %f, Gyro Z: %f\n", imuData->gir_x, imuData->gir_y, imuData->gir_z); + } + } } - #ifdef __cplusplus +#ifdef __cplusplus } - #endif +#endif -#endif /* __SPI_task.h__ */ +#endif /* SPI_task.C */ diff --git a/app/src/main.c b/app/src/main.c index b191d3b..34beac2 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -41,28 +41,25 @@ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ +osMessageQId imuQueue; // Declare the imuQueue variable /* Private function prototypes -----------------------------------------------*/ - -typedef enum { THREAD_1 = 0, THREAD_2 } Thread_TypeDef; +void SystemClock_Config(void); +typedef enum { THREAD_ID_1 = 0, THREAD_ID_2 = 1, TASK1_ID = 2 } Thread_TypeDef; +void Task_ProcessIMUData(void const* argument); // Add this line to declare the function osThreadId LEDThread1Handle; osThreadId tid_thread1; osThreadId tid_thread2; +osThreadId tid_task1; osThreadId id1; -osSemaphoreId semaphore; // Semaphore ID -osSemaphoreDef(semaphore); // Semaphore definition - -void SystemClock_Config(void); static void BlinkyThread(void const* argument); -void thread1(void const* argument); -void thread2(void const* argument); +static void thread1(void const* argument); +static void thread2(void const* argument); +// static void Task1(void const* argument); // por si acaso -/* Definición de la macro osThreadDef */ -#define osThreadDef(name, priority, instances, stacksz) \ - const osThreadDef_t os_thread_def_##name = { \ - .name = #name, .pthread = name, .tpriority = priority, .instances = instances, .stacksize = stacksz} +// Definición de la macro osThreadDef /*-----------------------------------------------------------*/ // osThreadId_t defaultTaskHandle; @@ -103,6 +100,15 @@ static void BlinkyThread(void const* argument) { * @retval int */ +/* Definición de los hilos */ +// el compilador no reconoce la macro +osThreadDef(THREAD_ID_1, thread1, osPriorityHigh, 1, 128); // Define el hilo thread1 +osThreadDef(THREAD_ID_2, thread2, osPriorityNormal, 1, 128); // Define el hilo thread2 +osThreadDef(TASK1_ID, Task1, osPriorityNormal, 1, 128); // Define el hilo Task1 + +osSemaphoreId semaphore; // Semaphore ID +osSemaphoreDef(semaphore); // Semaphore definition + void thread1(void const* argument) { int32_t value; while (1) { @@ -117,19 +123,20 @@ void thread1(void const* argument) { } // thread 2 - Normal Priority - looks for a free semaphore and uses the resource whenever it is available - void thread2(void const* argument) { while (1) { osSemaphoreWait(semaphore, osWaitForever); // Esperar indefinidamente por el semáforo // Usar el recurso aquí - osSemaphoreRelease(semaphore); // Liberar el semáforo + osSemaphoreRelease(semaphore); // Liberar el semáforo + } +} +void Task_ProcessIMUData(void const* argument) { + // Implement the task to process IMU data here + while (1) { + // Process IMU data + osDelay(100); // Adjust the delay as necessary } } - -/* Definición de los hilos */ -osThreadDef(thread1, osPriorityHigh, 1, 128); -osThreadDef(thread2, osPriorityNormal, 1, 128); -osThreadDef(Task1, osPriorityNormal, 1, 128); int main(void) { /* MCU Configuration--------------------------------------------------------*/ @@ -149,15 +156,25 @@ int main(void) { MX_USART2_UART_Init(); MX_SPI1_Init(); // lo copie directo spi.c esta declarado como una funcion ahi pero dice que no esta definida + MX_SPI1_Init(); osKernelInitialize(); - semaphore = osSemaphoreCreate(osSemaphore(semaphore), 1); - SPI_Config(); + /* Creación de los hilos */ + tid_thread1 = osThreadCreate(osThread(THREAD_ID_1), NULL); // Crea el hilo thread1 + tid_thread2 = osThreadCreate(osThread(THREAD_ID_2), NULL); // Crea el hilo thread2 + tid_task1 = osThreadCreate(osThread(TASK1_ID), NULL); // Crea el hilo Task1 + + // ahora toca la cola + // Inicializar la cola + osMessageQDef(QUEUE_SIZE, 16, uint32_t); // Define the message queue + imuQueue = osMessageCreate(osMessageQ(QUEUE_SIZE), NULL); + if (imuQueue == NULL) { + // Manejar el error si la cola no se pudo crear + } - /*Crear los hilos */ - tid_thread1 = osThreadCreate(osThread(thread1), NULL); - tid_thread2 = osThreadCreate(osThread(thread2), NULL); - osThreadId id1 = osThreadCreate(osThread(Task1), NULL); + // Crear la tarea para procesar los datos del IMU + osThreadDef(Task_ProcessIMUData, Task_ProcessIMUData, osPriorityNormal, 0, 128); + osThreadCreate(osThread(Task_ProcessIMUData), NULL); osKernelStart(); // Iniciar el scheduler del RTOS v1 // This is a fake comment, delete