소스 검색

DAC mit verbesserter struktur (globale Variablen), Bugfix bei der Slot-Zuordnung

Heinrich Blatt 8 달 전
부모
커밋
533a2cd5ae
4개의 변경된 파일20개의 추가작업 그리고 24개의 파일을 삭제
  1. 8 6
      src/i2c_comm/mcu_slave_interface.c
  2. 1 1
      src/i2c_comm/mcu_slave_interface.h
  3. 9 15
      src/peripherals/dac/dac.c
  4. 2 2
      src/peripherals/dac/dac.h

+ 8 - 6
src/i2c_comm/mcu_slave_interface.c

@@ -10,6 +10,7 @@ https://stackoverflow.com/questions/246127/why-is-volatile-needed-in-c
 #include <stdio.h>
 #include <string.h>
 #include "src/peripherals/dac/dac.h"
+#include <inttypes.h>
 
 /*Function to Rx and Tx data from Target to Controller*/
 // The code has multiple i2c instances (multiple MCUs connected) from which we
@@ -48,7 +49,7 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
     SetChargeDischargeCurrent set_current;
     // Read incoming bytes from the Controller:
     uint8_t rx_index = 0;
-    while (rx_index < sizeof(SetChargeDischargeCurrent)) {
+    while (rx_index < sizeof(SetChargeDischargeCurrent)+1) {
       // TODO: Need to have a workaround, currently the code is getting stuck on
       // the first trigger and provides result on the second trigger
       if (!DL_I2C_isTargetRXFIFOEmpty(i2c)) {
@@ -60,20 +61,21 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
     // Byte array received from the Controller will be typecasted to (const
     // uint8_t *), treats the rx_buffer as an array of READ ONLY bytes because
     // of the const
-    if (rx_index != sizeof(SetChargeDischargeCurrent)) {
+    if (rx_index != sizeof(SetChargeDischargeCurrent)+1) {
       printf("ERROR: Incomplete I2C Rx: received %d%zu bytes\n", rx_index,
-             sizeof(SetChargeDischargeCurrent));
+             sizeof(SetChargeDischargeCurrent)+1);
       DL_I2C_flushTargetRXFIFO(i2c);
       rx_index = 0;
       return;
     }
-    memcpy(&set_current, (const uint8_t *)rx_buffer,
+    printf("size: %d", sizeof(SetChargeDischargeCurrent));
+    memcpy(&set_current, (const uint8_t *)rx_buffer+1,
            sizeof(SetChargeDischargeCurrent));
     uint8_t slot = set_current.slot_id;
     int16_t current = set_current.current;
-    printf("Slot id: %d, Current: %d\n", slot, current);
+    printf("Slot id: %d, Current: %" SCNd16 "\n", slot, current);
     if (current >= 0) {
-      DAC_SingleWrite(current);
+      DAC_SingleWrite(slot, current);
     } else if (current < 0) {
         
         DL_TimerG_startCounter(PWM_0_INST);

+ 1 - 1
src/i2c_comm/mcu_slave_interface.h

@@ -15,7 +15,7 @@ typedef enum{
 }mcu_I2C_command;
 
 //Command structures:
-typedef struct{
+typedef struct __attribute__((packed)) {
     uint8_t slot_id;
     int16_t current;
     

+ 9 - 15
src/peripherals/dac/dac.c

@@ -28,37 +28,31 @@ void DAC_UpdateOutput() {
 }
 
 
-bool DAC_SingleWrite(uint16_t channel_value) {
+bool DAC_SingleWrite(uint8_t slot, uint16_t channel_value) {
   if(channel_value > 4095){
     printf("DAC Error: channel_value out of range. Must be between 0 and 4095\n");
     return false;
   }
-  uint8_t data_length = 3;
-  uint8_t output_buffer_SingleWrite[data_length];
-  output_buffer_SingleWrite[0] = 0x58; //0x58 for Channel 0; 0x5A for Channel 1
-  printf("((channel_value >> 8) & 0x0F): 0x%02X", ((channel_value >> 8) & 0x0F));
-  output_buffer_SingleWrite[1] = (0x10) | ((channel_value >> 8) & 0x0F);
-  output_buffer_SingleWrite[2] = (channel_value & 0xFF);
+  controllerTxPackage.len = 3;
+  controllerTxPackage.packet[0] = 0x58; //0x58 for Channel 0; 0x5A for Channel 1
+  controllerTxPackage.packet[1] = (0x10) | ((channel_value >> 8) & 0x0F);
+  controllerTxPackage.packet[2] = (channel_value & 0xFF);
 
 
   // Log data being sent
   printf("Sending to DAC: 0x%02X 0x%02X 0x%02X\n",
-           output_buffer_SingleWrite[0],
-           output_buffer_SingleWrite[1],
-           output_buffer_SingleWrite[2]);
+           controllerTxPackage.packet[0],
+           controllerTxPackage.packet[1],
+           controllerTxPackage.packet[2]);
 
-
-  // @TODO FIXME to right global variables!!
   // Write data to DAC
-  if (!i2c_hal.write(DAC_TARGET_BASE_ADDRESS)) {
+  if (!i2c_hal.write(DAC_TARGET_ADDRESS)) {
         printf("I2C DAC Write Error: Failed to write to DAC.\n");
         return false;
     }
 
   DAC_UpdateOutput();
 
-  printf("Output_Buffer_1: 0x%02X, Output_Buffer_2: 0x%02X\n", output_buffer_SingleWrite[1], output_buffer_SingleWrite[2]);
-
   return true;
 
 }

+ 2 - 2
src/peripherals/dac/dac.h

@@ -3,9 +3,9 @@
 #include "ti_msp_dl_config.h"
 #include <stdbool.h>
 
-#define DAC_TARGET_BASE_ADDRESS (0x60)
+#define DAC_TARGET_ADDRESS (0x60)
 #define DAC_VREF_MV 2048
 
-bool DAC_SingleWrite(uint16_t channel_value);
+bool DAC_SingleWrite(uint8_t slot, uint16_t channel_value);
 
 #endif