-
Notifications
You must be signed in to change notification settings - Fork 1
I2c integration #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I2c integration #15
Changes from all commits
7b4c6be
50eb69c
97f778e
eeb60b6
a82b343
f97c0c7
b88bfb8
bf1404a
e83c4d0
d222f24
1a10116
698174a
3740e70
71c7acf
5dae9c6
0e6c816
0c6ed14
891e567
ec58759
8b624b7
9873009
3c7abee
0e3f205
34d9a18
061d33a
af94b0a
44933e5
4e768c8
865c483
21e10d3
32d62c8
6316619
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,15 +35,43 @@ extern "C" { | |||||||||||||||
| #include <stdio.h> | ||||||||||||||||
| #include <stdint.h> | ||||||||||||||||
| #include <utils.h> | ||||||||||||||||
|
|
||||||||||||||||
| /* USER CODE END Includes */ | ||||||||||||||||
|
|
||||||||||||||||
| /* Exported types ------------------------------------------------------------*/ | ||||||||||||||||
| /* USER CODE BEGIN ET */ | ||||||||||||||||
|
|
||||||||||||||||
| // Thread with max priority | ||||||||||||||||
| #define MAX_PRIO 0 | ||||||||||||||||
|
|
||||||||||||||||
| // Thread with medium priority | ||||||||||||||||
| #define MEDIUM_PRIO 5 | ||||||||||||||||
|
|
||||||||||||||||
| // Thread with low priority | ||||||||||||||||
| #define LOW_PRIO 10 | ||||||||||||||||
|
|
||||||||||||||||
| // Thread with no priority (lowest) | ||||||||||||||||
| #define NONE_PRIO 15 | ||||||||||||||||
|
|
||||||||||||||||
| //Queue size (number of messages) | ||||||||||||||||
| #define QUEUE_SIZE 8 | ||||||||||||||||
|
|
||||||||||||||||
| /* | ||||||||||||||||
| Number of threads | ||||||||||||||||
| 1 -> Speed sensor thread | ||||||||||||||||
| 2 -> CAN TX thread | ||||||||||||||||
| 3 -> CAN RX thread | ||||||||||||||||
| 4 -> dc_motors thread | ||||||||||||||||
| 5 -> servo thread | ||||||||||||||||
| 6 -> battery thread | ||||||||||||||||
| 7 -> emergency brake thread | ||||||||||||||||
| */ | ||||||||||||||||
| #define THREAD_COUNT 7 | ||||||||||||||||
|
Comment on lines
+66
to
+69
|
||||||||||||||||
| 6 -> battery thread | |
| 7 -> emergency brake thread | |
| */ | |
| #define THREAD_COUNT 7 | |
| 6 -> emergency brake thread | |
| */ | |
| #define THREAD_COUNT 6 |
Copilot
AI
Feb 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thread stack is changed from uint8_t to UINT type. UINT is typically defined as uint32_t, which means each stack element is now 4 bytes instead of 1 byte. With 1024 elements, this changes the stack size from 1KB to 4KB per thread. With 7 threads (THREAD_COUNT), this is 28KB total instead of 7KB. Verify this significant increase in memory usage is intentional and that the MCU has sufficient RAM. If 1KB stacks are sufficient, change back to uint8_t stack[1024].
Copilot
AI
Feb 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The i2c_mutex is declared as extern in app_threadx.h and used throughout the I2C code, but there's no initialization code (tx_mutex_create call) in App_ThreadX_Init or anywhere else in the visible code. The mutex must be created before it can be used. Add mutex creation in App_ThreadX_Init similar to how the queues are created.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #ifndef CAN_PROTOCOL_H | ||
| #define CAN_PROTOCOL_H | ||
|
|
||
| #include "app_threadx.h" | ||
|
|
||
| // CAN message types | ||
| typedef enum { | ||
| CAN_MSG_SPEED, | ||
| CAN_MSG_BATTERY, | ||
| } e_can_msg_type; | ||
|
|
||
| // TX CAN message structure (sending) | ||
| typedef struct s_tx_can_message { | ||
| e_can_msg_type type; | ||
| uint8_t data[8]; | ||
| } t_tx_can_msg; | ||
|
|
||
| // RX CAN message structure (receiving) | ||
| typedef struct s_rx_can_message { | ||
| uint32_t type; | ||
| uint8_t data[8]; | ||
| uint8_t len; | ||
| } t_rx_can_msg; | ||
|
|
||
| /* // Steering/throttle CAN message instructions | ||
| typedef struct s_i2c_message { | ||
| int8_t steering; | ||
| int8_t throttle; | ||
| } t_i2c_msg; */ | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #ifndef I2C_INA219_H | ||
| # define I2C_INA219_H | ||
|
|
||
| #include "app_threadx.h" | ||
|
|
||
| #define INA219_ADDR 0x41 | ||
|
|
||
| #define INA219_REG_CONFIG 0x00 | ||
| #define INA219_REG_SHUNT_VOLT 0x01 | ||
| #define INA219_REG_BUS_VOLT 0x02 | ||
| #define INA219_REG_POWER 0x03 | ||
| #define INA219_REG_CURRENT 0x04 | ||
| #define INA219_REG_CALIBRATION 0x05 | ||
|
|
||
| #define INA219_CALIBRATION 4096 | ||
|
|
||
| HAL_StatusTypeDef ina219_init(UINT addr); | ||
| HAL_StatusTypeDef ina219_read_voltage(float* voltage); | ||
| HAL_StatusTypeDef ina219_read_current(float* current); | ||
| HAL_StatusTypeDef ina219_read_power(float* power); | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||||||
| #ifndef I2C_PCA9685_H | ||||||||||
| # define I2C_PCA9685_H | ||||||||||
|
|
||||||||||
| #include "app_threadx.h" | ||||||||||
|
|
||||||||||
| #define PCA9685_ADDR_SERVO 0x40 | ||||||||||
| #define PCA9685_ADDR_MOTOR 0x60 | ||||||||||
|
|
||||||||||
| // PWM resolution and channel range | ||||||||||
| #define PCA9685_PWM_MAX 4095 | ||||||||||
|
|
||||||||||
| // PCA9685 channel limits | ||||||||||
| #define PCA9685_CHANNEL_MIN 0 | ||||||||||
| #define PCA9685_CHANNEL_MAX 15 | ||||||||||
|
|
||||||||||
| // PCA9685 register addresses | ||||||||||
| #define MODE1 0x00 | ||||||||||
| #define MODE2 0x01 | ||||||||||
| #define PRE_SCALE 0xFE | ||||||||||
|
|
||||||||||
| // LED0 registers | ||||||||||
| #define LED0_ON_L 0x06 | ||||||||||
| #define LED0_ON_H 0x07 | ||||||||||
| #define LED0_OFF_L 0x08 | ||||||||||
| #define LED0_OFF_H 0x09 | ||||||||||
|
|
||||||||||
| // PCA9685 mode settings | ||||||||||
| #define PCA9685_SLEEP_MODE 0x10 | ||||||||||
| #define PCA9685_50HZ_PRESCALE 0x79 | ||||||||||
| #define PCA9685_1KHZ_PRESCALE 0x05 | ||||||||||
| #define PCA9685_WAKE_AUTOINC 0x20 | ||||||||||
|
|
||||||||||
| // Servo pulse limits | ||||||||||
| #define SERVO_MIN_PULSE 205 | ||||||||||
| #define SERVO_MAX_PULSE 410 | ||||||||||
| #define SERVO_MAX_ANGLE 180 | ||||||||||
|
|
||||||||||
| // Motor speed limits | ||||||||||
| #define MOTOR_MAX_SPEED 100 | ||||||||||
|
|
||||||||||
| #define MOTOR_PWM_MIN 0 | ||||||||||
|
|
||||||||||
| typedef struct s_motor_channel { | ||||||||||
| UINT pwm_ch; // PWM motor channel | ||||||||||
| UINT in1_ch; // IN1 channel | ||||||||||
| UINT in2_ch; // IN2 channel | ||||||||||
| } t_motor_channel; | ||||||||||
|
|
||||||||||
| // Predefined motor channels | ||||||||||
| static const t_motor_channel MOTOR_LEFT = { 0, 1, 2 }; | ||||||||||
| static const t_motor_channel MOTOR_RIGHT = { 7, 5, 6 }; | ||||||||||
|
Comment on lines
+50
to
+51
|
||||||||||
| static const t_motor_channel MOTOR_LEFT = { 0, 1, 2 }; | |
| static const t_motor_channel MOTOR_RIGHT = { 7, 5, 6 }; | |
| #define MOTOR_LEFT ((t_motor_channel){ 0u, 1u, 2u }) | |
| #define MOTOR_RIGHT ((t_motor_channel){ 7u, 5u, 6u }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spelling error in comment. "diferent" should be "different".