From ade0b6465b7f92c98b8573f166785d7db558f246 Mon Sep 17 00:00:00 2001 From: Jackie Zhu <64090163+jckpokimain@users.noreply.github.com> Date: Sun, 25 Apr 2021 19:34:22 -0400 Subject: [PATCH 1/5] Implemented Chip Id --- libraries/ms-common/inc/chip_id.h | 9 +++++++ libraries/ms-common/src/stm32f0xx/chip_id.c | 23 ++++++++++++++++ libraries/ms-common/src/x86/chip_id.c | 16 ++++++++++++ libraries/ms-common/test/test_chip_id.c | 29 +++++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 libraries/ms-common/inc/chip_id.h create mode 100644 libraries/ms-common/src/stm32f0xx/chip_id.c create mode 100644 libraries/ms-common/src/x86/chip_id.c create mode 100644 libraries/ms-common/test/test_chip_id.c diff --git a/libraries/ms-common/inc/chip_id.h b/libraries/ms-common/inc/chip_id.h new file mode 100644 index 000000000..60296ae3f --- /dev/null +++ b/libraries/ms-common/inc/chip_id.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +typedef struct ChipId { + uint32_t id[3]; +} ChipId; + +ChipId chip_id_get(void); diff --git a/libraries/ms-common/src/stm32f0xx/chip_id.c b/libraries/ms-common/src/stm32f0xx/chip_id.c new file mode 100644 index 000000000..7bd8b8f04 --- /dev/null +++ b/libraries/ms-common/src/stm32f0xx/chip_id.c @@ -0,0 +1,23 @@ +#include + +#include "chip_id.h" + +// Unique device identifier base address from section 33.1 of the specific device +// datasheet +#define DEVICE_ID_ADDR 0x1FFFF7AC + +// returns chip id in the form of an array of uint16_ts +ChipId chip_id_get(void) { + ChipId chip_id = { 0 }; + uint16_t *address_pointer = (uint16_t *)DEVICE_ID_ADDR; + + // Iterates through 96 bits of id and stores them in uint32_t array + for (int i = 0; i < 2; i++) { + chip_id.id[i] = *address_pointer; + + // Offsets address by 0x04 + address_pointer++; + } + + return chip_id; +} diff --git a/libraries/ms-common/src/x86/chip_id.c b/libraries/ms-common/src/x86/chip_id.c new file mode 100644 index 000000000..bd2da8c17 --- /dev/null +++ b/libraries/ms-common/src/x86/chip_id.c @@ -0,0 +1,16 @@ +#include "chip_id.h" + +#include +#include + +// returns process id in the form of an array of uint16_ts +ChipId chip_id_get(void) { + ChipId chip_id = { 0 }; + pid_t process_id; + process_id = getpid(); + + // Max of uint32_t is larger than max of pid_t + chip_id.id[0] = (uint32_t)process_id; + + return chip_id; +} diff --git a/libraries/ms-common/test/test_chip_id.c b/libraries/ms-common/test/test_chip_id.c new file mode 100644 index 000000000..71a09ad28 --- /dev/null +++ b/libraries/ms-common/test/test_chip_id.c @@ -0,0 +1,29 @@ +#include "chip_id.h" + +#include "delay.h" +#include "interrupt.h" +#include "log.h" +#include "soft_timer.h" +#include "test_helpers.h" +#include "unity.h" + +static ChipId chip_Id_1 = { 0 }; +static ChipId chip_Id_2 = { 0 }; + +void setup_test(void) { + interrupt_init(); + soft_timer_init(); +} + +void teardown_test(void) {} + +void test_chip_id(void) { + const uint32_t half_second_ms = 500; + chip_Id_1 = chip_id_get(); + delay_us(half_second_ms); + chip_Id_2 = chip_id_get(); + for (int i = 0; i < 2; i++) { + LOG_DEBUG("%d", chip_Id_1.id[i]); + } + TEST_ASSERT_EQUAL_INT32_ARRAY(chip_Id_1.id, chip_Id_2.id, 2); +} From ba7db53b3714470e553acef3122d2a506a70efc0 Mon Sep 17 00:00:00 2001 From: Jackie Zhu <64090163+jckpokimain@users.noreply.github.com> Date: Sun, 25 Apr 2021 21:26:13 -0400 Subject: [PATCH 2/5] Fixed pipline error --- libraries/ms-common/test/test_chip_id.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ms-common/test/test_chip_id.c b/libraries/ms-common/test/test_chip_id.c index 71a09ad28..a17da82f3 100644 --- a/libraries/ms-common/test/test_chip_id.c +++ b/libraries/ms-common/test/test_chip_id.c @@ -23,7 +23,7 @@ void test_chip_id(void) { delay_us(half_second_ms); chip_Id_2 = chip_id_get(); for (int i = 0; i < 2; i++) { - LOG_DEBUG("%d", chip_Id_1.id[i]); + LOG_DEBUG("%u", chip_Id_1.id[i]); } TEST_ASSERT_EQUAL_INT32_ARRAY(chip_Id_1.id, chip_Id_2.id, 2); } From 269b6b00a42203d8afa6309adb7279edfb8e783c Mon Sep 17 00:00:00 2001 From: Jackie Zhu <64090163+jckpokimain@users.noreply.github.com> Date: Wed, 28 Apr 2021 14:13:23 -0400 Subject: [PATCH 3/5] made changes to comments --- libraries/ms-common/inc/chip_id.h | 6 ++++++ libraries/ms-common/src/stm32f0xx/chip_id.c | 6 +++--- libraries/ms-common/src/x86/chip_id.c | 5 ++--- libraries/ms-common/test/test_chip_id.c | 19 +++++++++++-------- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/libraries/ms-common/inc/chip_id.h b/libraries/ms-common/inc/chip_id.h index 60296ae3f..e37ab4585 100644 --- a/libraries/ms-common/inc/chip_id.h +++ b/libraries/ms-common/inc/chip_id.h @@ -1,4 +1,10 @@ #pragma once +// Module for accessing hardware chip id. +// Chip id should stay constant for each controller board. +// Unique device identifier base address from section 33.1 of the specific device +// datasheet. +// +// Emulates x86 system by accessing process id instead of chip id. #include diff --git a/libraries/ms-common/src/stm32f0xx/chip_id.c b/libraries/ms-common/src/stm32f0xx/chip_id.c index 7bd8b8f04..71b9a211e 100644 --- a/libraries/ms-common/src/stm32f0xx/chip_id.c +++ b/libraries/ms-common/src/stm32f0xx/chip_id.c @@ -6,13 +6,13 @@ // datasheet #define DEVICE_ID_ADDR 0x1FFFF7AC -// returns chip id in the form of an array of uint16_ts +// returns chip id in the form of an array of uint32_ts ChipId chip_id_get(void) { ChipId chip_id = { 0 }; - uint16_t *address_pointer = (uint16_t *)DEVICE_ID_ADDR; + uint32_t *address_pointer = (uint32_t *)DEVICE_ID_ADDR; // Iterates through 96 bits of id and stores them in uint32_t array - for (int i = 0; i < 2; i++) { + for (uint8_t i = 0; i < 2; i++) { chip_id.id[i] = *address_pointer; // Offsets address by 0x04 diff --git a/libraries/ms-common/src/x86/chip_id.c b/libraries/ms-common/src/x86/chip_id.c index bd2da8c17..e15656e93 100644 --- a/libraries/ms-common/src/x86/chip_id.c +++ b/libraries/ms-common/src/x86/chip_id.c @@ -3,11 +3,10 @@ #include #include -// returns process id in the form of an array of uint16_ts +// returns process id in the form of an array of uint32_ts ChipId chip_id_get(void) { ChipId chip_id = { 0 }; - pid_t process_id; - process_id = getpid(); + pid_t process_id = getpid(); // Max of uint32_t is larger than max of pid_t chip_id.id[0] = (uint32_t)process_id; diff --git a/libraries/ms-common/test/test_chip_id.c b/libraries/ms-common/test/test_chip_id.c index a17da82f3..0ba22cc39 100644 --- a/libraries/ms-common/test/test_chip_id.c +++ b/libraries/ms-common/test/test_chip_id.c @@ -1,14 +1,16 @@ #include "chip_id.h" +#include #include "delay.h" #include "interrupt.h" #include "log.h" +#include "misc.h" #include "soft_timer.h" #include "test_helpers.h" #include "unity.h" -static ChipId chip_Id_1 = { 0 }; -static ChipId chip_Id_2 = { 0 }; +ChipId chip_id_1 = { 0 }; +ChipId chip_id_2 = { 0 }; void setup_test(void) { interrupt_init(); @@ -17,13 +19,14 @@ void setup_test(void) { void teardown_test(void) {} +// Verifies chip ID is consistant and prints chip ID for debugging void test_chip_id(void) { - const uint32_t half_second_ms = 500; - chip_Id_1 = chip_id_get(); - delay_us(half_second_ms); - chip_Id_2 = chip_id_get(); + const uint32_t delay_second_ms = 10; + chip_id_1 = chip_id_get(); + delay_ms(delay_second_ms); + chip_id_2 = chip_id_get(); for (int i = 0; i < 2; i++) { - LOG_DEBUG("%u", chip_Id_1.id[i]); + LOG_DEBUG("%" PRIu32 "\n", chip_id_1.id[i]); } - TEST_ASSERT_EQUAL_INT32_ARRAY(chip_Id_1.id, chip_Id_2.id, 2); + TEST_ASSERT_EQUAL_UINT32_ARRAY(chip_id_1.id, chip_id_2.id, SIZEOF_ARRAY(chip_id_1.id)); } From 836a4c9eb4e7f2a7192efb06a6c6d6f67b7e73b3 Mon Sep 17 00:00:00 2001 From: Jackie Zhu <64090163+jckpokimain@users.noreply.github.com> Date: Fri, 30 Apr 2021 12:09:07 -0400 Subject: [PATCH 4/5] made minor changes --- libraries/ms-common/src/stm32f0xx/chip_id.c | 4 ++-- libraries/ms-common/test/test_chip_id.c | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libraries/ms-common/src/stm32f0xx/chip_id.c b/libraries/ms-common/src/stm32f0xx/chip_id.c index 71b9a211e..e2fab1400 100644 --- a/libraries/ms-common/src/stm32f0xx/chip_id.c +++ b/libraries/ms-common/src/stm32f0xx/chip_id.c @@ -1,7 +1,7 @@ -#include - #include "chip_id.h" +#include + // Unique device identifier base address from section 33.1 of the specific device // datasheet #define DEVICE_ID_ADDR 0x1FFFF7AC diff --git a/libraries/ms-common/test/test_chip_id.c b/libraries/ms-common/test/test_chip_id.c index 0ba22cc39..4ce412048 100644 --- a/libraries/ms-common/test/test_chip_id.c +++ b/libraries/ms-common/test/test_chip_id.c @@ -1,6 +1,7 @@ #include "chip_id.h" #include + #include "delay.h" #include "interrupt.h" #include "log.h" @@ -9,9 +10,6 @@ #include "test_helpers.h" #include "unity.h" -ChipId chip_id_1 = { 0 }; -ChipId chip_id_2 = { 0 }; - void setup_test(void) { interrupt_init(); soft_timer_init(); @@ -21,12 +19,14 @@ void teardown_test(void) {} // Verifies chip ID is consistant and prints chip ID for debugging void test_chip_id(void) { + ChipId chip_id_1 = { 0 }; + ChipId chip_id_2 = { 0 }; const uint32_t delay_second_ms = 10; chip_id_1 = chip_id_get(); delay_ms(delay_second_ms); chip_id_2 = chip_id_get(); - for (int i = 0; i < 2; i++) { - LOG_DEBUG("%" PRIu32 "\n", chip_id_1.id[i]); + for (uint8_t i = 0; i < 3; i++) { + LOG_DEBUG("0x%" PRIx32 "\n", chip_id_1.id[i]); } TEST_ASSERT_EQUAL_UINT32_ARRAY(chip_id_1.id, chip_id_2.id, SIZEOF_ARRAY(chip_id_1.id)); } From 81551d4bd181d2c9900cb2f1cfa158628eb1d653 Mon Sep 17 00:00:00 2001 From: Jackie Zhu <64090163+jckpokimain@users.noreply.github.com> Date: Thu, 1 Jul 2021 21:00:42 -0400 Subject: [PATCH 5/5] Half finished rx and tx of motor temp --- projects/mci/inc/motor_temperature.h | 32 ++++++++++++ projects/mci/src/motor_temperature.c | 76 ++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 projects/mci/inc/motor_temperature.h create mode 100644 projects/mci/src/motor_temperature.c diff --git a/projects/mci/inc/motor_temperature.h b/projects/mci/inc/motor_temperature.h new file mode 100644 index 000000000..b76b7044c --- /dev/null +++ b/projects/mci/inc/motor_temperature.h @@ -0,0 +1,32 @@ +#pragma once + +#include "generic_can.h" + +#include "motor_can.h" +#include "wavesculptor.h" + +#define MOTOR_TEMPERATURE_TX_PERIOD_MS 500 + +typedef enum { + LEFT_MOTOR_CONTROLLER = 0, + RIGHT_MOTOR_CONTROLLER, + NUM_MOTOR_CONTROLLERS, +} MotorController; + +typedef struct MotorTemperatureStorage { + MotorTemperatureMeasurements measurements; + MotorCanDeviceId ids[NUM_MOTOR_CONTROLLERS]; +} MotorTemperatureStorage; + +typedef struct MotorTemperatureSettings { + GenericCan *motor_can; + MotorCanDeviceId device_ids[NUM_MOTOR_CONTROLLERS]; +} MotorTemperatureSettings; + +typedef struct MotorTemperatureMeasurements { + WaveSculptorAirInCpuTempMeasurement cpu_measurements[NUM_MOTOR_CONTROLLERS]; + WaveSculptorSinkMotorTempMeasurement sink_motor_measurements[NUM_MOTOR_CONTROLLERS]; +} + +StatusCode +motor_temperature_init(MotorTemperatureStorage *storage, GenericCan *motor_can); diff --git a/projects/mci/src/motor_temperature.c b/projects/mci/src/motor_temperature.c new file mode 100644 index 000000000..8fe87a3d5 --- /dev/null +++ b/projects/mci/src/motor_temperature.c @@ -0,0 +1,76 @@ +#include "motor_temperature.h" + +#include "can_transmit.h" +#include "can_unpack.h" +#include "generic_can.h" +#include "motor_can.h" +#include "soft_timer.h" +#include "status.h" + +// Retreiving temperature from left and right motor should be implemented, +// transmitting the data still needs to be done. + +static void prv_handle_sink_temperature_rx(const GenericCanMsg *msg, void *context) { + // add data to storage + MotorTemperatureStorage *storage = context; + WaveSculptorSinkMotorTempMeasurement *measurements = + storage->measurements.sink_motor_measurements; + + WaveSculptorCanId can_id = { .raw = msg->id }; + WaveSculptorCanData can_data = { .raw = msg->data }; + + for (size_t motor_id = 0; motor_id < NUM_MOTOR_CONTROLLERS; motor_id++) { + if (can_id.device_id == storage->ids[motor_id]) { + measurements[motor_id] = can_data.sink_motor_temp_measurement; + } + } +} + +static void prv_handle_cpu_temperature_rx(const GenericCanMsg *msg, void *context) { + MotorTemperatureStorage *storage = context; + WaveSculptorAirInCpuTempMeasurement *measurements = storage->measurements.cpu_measurements; + + WaveSculptorCanId can_id = { .raw = msg->id }; + WaveSculptorCanData can_data = { .raw = msg->data }; + + for (size_t motor_id = 0; motor_id < NUM_MOTOR_CONTROLLERS; motor_id++) { + if (can_id.device_id == storage->ids[motor_id]) { + measurements[motor_id] = can_data.air_in_cpu_temp_measurement; + } + } +} + +static void prv_handle_temperature_tx(SoftTimerId timer_id, void *context) { + // transmit data through CAN periodically + MotorTemperatureStorage *storage = context; + prv_temperature_tx(storage); + soft_timer_start_millis(MOTOR_TEMPERATURE_TX_PERIOD_MS, prv_handle_temperature_tx, storage, NULL); +} + +static void prv_temperature_tx(MotorTemperatureStorage *storage) { + MotorTemperatureMeasurements measurements = storage->measurements; + // Not finished: transmit CAN message +} + +// Not finished: Function needs to be called in main.c +StatusCode motor_temperature_init(MotorTemperatureStorage *storage, + MotorTemperatureSettings *setting) { + status_ok_or_return(generic_can_register_rx( + setting->motor_can, prv_handle_sink_temperature_rx, GENERIC_CAN_EMPTY_MASK, + MOTOR_CAN_RIGHT_SINK_MOTOR_TEMPERATURE_FRAME_ID, false, storage)); + + status_ok_or_return(generic_can_register_rx( + setting->motor_can, prv_handle_sink_temperature_rx, GENERIC_CAN_EMPTY_MASK, + MOTOR_CAN_LEFT_SINK_MOTOR_TEMPERATURE_FRAME_ID, false, storage)); + + status_ok_or_return(generic_can_register_rx( + setting->motor_can, prv_handle_sink_temperature_rx, GENERIC_CAN_EMPTY_MASK, + MOTOR_CAN_RIGHT_CPU_TEMPERATURE_FRAME_ID, false, storage)); + + status_ok_or_return(generic_can_register_rx( + setting->motor_can, prv_handle_sink_temperature_rx, GENERIC_CAN_EMPTY_MASK, + MOTOR_CAN_LEFT_CPU_TEMPERATURE_FRAME_ID, false, storage)); + + return soft_timer_start_millis(MOTOR_TEMPERATURE_TX_PERIOD_MS, prv_handle_temperature_tx, storage, + NULL); +}