Updated most recent files

This commit is contained in:
sorek 2024-05-03 10:24:53 +01:00
parent 25bffa889e
commit 07945d5bd2
9 changed files with 484403 additions and 15 deletions

View File

@ -8,7 +8,7 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:genericSTM32F103C8]
[env:EGT-8-STM32F103C6]
platform = ststm32
board = genericSTM32F103C8
framework = arduino
@ -18,4 +18,18 @@ build_flags =
-DSERIAL_UART_INSTANCE=1
-DPIN_SERIAL_RX=PA10
-DPIN_SERIAL_TX=PA9
-D HSE_VALUE=8000000U
-D HSE_VALUE=8000000U
[env:EGT-8-STM32F103C6-debug]
platform = ststm32
board = genericSTM32F103C8
framework = arduino
upload_protocol = serial
monitor_speed = 115200
build_flags =
-DSERIAL_UART_INSTANCE=1
-DPIN_SERIAL_RX=PA10
-DPIN_SERIAL_TX=PA9
-D HSE_VALUE=8000000U
-D DEBUG=1

View File

@ -1,7 +1,7 @@
#include <Arduino.h>
#include "MAX31855.h"
#define DEBUG 1
//#define DEBUG 1
#ifndef DEBUG
#define DEBUG 0
@ -37,20 +37,22 @@ typedef struct {
uint8_t len;
} CAN_msg_t;
typedef const struct {
uint8_t TS2;
uint8_t TS1;
uint8_t BRP;
} CAN_bit_timing_config_t;
//typedef const struct {
// uint8_t TS2;
// uint8_t TS1;
// uint8_t BRP;
//} CAN_bit_timing_config_t;
//
//CAN_bit_timing_config_t can_configs[6] = {{2, 13, 45}, {2, 15, 20}, {2, 13, 18}, {2, 13, 9}, {2, 15, 4}, {2, 15, 2}};
CAN_bit_timing_config_t can_configs[6] = {{2, 13, 45}, {2, 15, 20}, {2, 13, 18}, {2, 13, 9}, {2, 15, 4}, {2, 15, 2}};
CAN_msg_t CAN_msg_14; // data from egt 1-4 that will be sent to CAN bus
CAN_msg_t CAN_msg_58; // data from egt 5-8 that will be sent to CAN bus
uint16_t canAddress;
uint8_t canin_channel;
extern CAN_bit_timing_config_t can_configs[6];
//extern CAN_bit_timing_config_t can_configs[6];
uint8_t cs[8] = { PA0, PA1, PA2, PA3, PB0, PB1, PB13, PB12 }; //chip select pins for the MAX31855 chips
int32_t rawData[8] = { 0 }; //raw data from all 8 MAX31855 chips
@ -104,6 +106,164 @@ IdAddrCan pins[ID_CAN_SIZE] = {
{ ID_PIN4, 10 },
};
/* Real speed for bit rate of CAN message */
uint32_t SPEED[6] = {50*1000, 100*1000, 125*1000, 250*1000, 500*1000, 1000*1000};
typedef struct
{
uint16_t baud_rate_prescaler; /// [1 to 1024]
uint8_t time_segment_1; /// [1 to 16]
uint8_t time_segment_2; /// [1 to 8]
uint8_t resynchronization_jump_width; /// [1 to 4] (recommended value is 1)
} CAN_bit_timing_config_t;
#define CAN_STM32_ERROR_UNSUPPORTED_BIT_RATE 1000
#define CAN_STM32_ERROR_MSR_INAK_NOT_SET 1001
#define CAN_STM32_ERROR_MSR_INAK_NOT_CLEARED 1002
#define CAN_STM32_ERROR_UNSUPPORTED_FRAME_FORMAT 1003
/*
* Calculation of bit timing dependent on peripheral clock rate
*/
int16_t ComputeCANTimings(const uint32_t peripheral_clock_rate,
const uint32_t target_bitrate,
CAN_bit_timing_config_t* out_timings)
{
if (target_bitrate < 1000)
{
return -CAN_STM32_ERROR_UNSUPPORTED_BIT_RATE;
}
//assert(out_timings != NULL); // NOLINT
memset(out_timings, 0, sizeof(*out_timings));
/*
* Hardware configuration
*/
static const uint8_t MaxBS1 = 16;
static const uint8_t MaxBS2 = 8;
/*
* Ref. "Automatic Baudrate Detection in CANopen Networks", U. Koppe, MicroControl GmbH & Co. KG
* CAN in Automation, 2003
*
* According to the source, optimal quanta per bit are:
* Bitrate Optimal Maximum
* 1000 kbps 8 10
* 500 kbps 16 17
* 250 kbps 16 17
* 125 kbps 16 17
*/
const uint8_t max_quanta_per_bit = (uint8_t)((target_bitrate >= 1000000) ? 10 : 17); // NOLINT
//assert(max_quanta_per_bit <= (MaxBS1 + MaxBS2));
static const uint16_t MaxSamplePointLocationPermill = 900;
/*
* Computing (prescaler * BS):
* BITRATE = 1 / (PRESCALER * (1 / PCLK) * (1 + BS1 + BS2)) -- See the Reference Manual
* BITRATE = PCLK / (PRESCALER * (1 + BS1 + BS2)) -- Simplified
* let:
* BS = 1 + BS1 + BS2 -- Number of time quanta per bit
* PRESCALER_BS = PRESCALER * BS
* ==>
* PRESCALER_BS = PCLK / BITRATE
*/
const uint32_t prescaler_bs = peripheral_clock_rate / target_bitrate;
/*
* Searching for such prescaler value so that the number of quanta per bit is highest.
*/
uint8_t bs1_bs2_sum = (uint8_t)(max_quanta_per_bit - 1); // NOLINT
while ((prescaler_bs % (1U + bs1_bs2_sum)) != 0)
{
if (bs1_bs2_sum <= 2)
{
return -CAN_STM32_ERROR_UNSUPPORTED_BIT_RATE; // No solution
}
bs1_bs2_sum--;
}
const uint32_t prescaler = prescaler_bs / (1U + bs1_bs2_sum);
if ((prescaler < 1U) || (prescaler > 1024U))
{
return -CAN_STM32_ERROR_UNSUPPORTED_BIT_RATE; // No solution
}
/*
* Now we have a constraint: (BS1 + BS2) == bs1_bs2_sum.
* We need to find such values so that the sample point is as close as possible to the optimal value,
* which is 87.5%, which is 7/8.
*
* Solve[(1 + bs1)/(1 + bs1 + bs2) == 7/8, bs2] (* Where 7/8 is 0.875, the recommended sample point location *)
* {{bs2 -> (1 + bs1)/7}}
*
* Hence:
* bs2 = (1 + bs1) / 7
* bs1 = (7 * bs1_bs2_sum - 1) / 8
*
* Sample point location can be computed as follows:
* Sample point location = (1 + bs1) / (1 + bs1 + bs2)
*
* Since the optimal solution is so close to the maximum, we prepare two solutions, and then pick the best one:
* - With rounding to nearest
* - With rounding to zero
*/
uint8_t bs1 = (uint8_t)(((7 * bs1_bs2_sum - 1) + 4) / 8); // Trying rounding to nearest first // NOLINT
uint8_t bs2 = (uint8_t)(bs1_bs2_sum - bs1); // NOLINT
//assert(bs1_bs2_sum > bs1);
{
const uint16_t sample_point_permill = (uint16_t)(1000U * (1U + bs1) / (1U + bs1 + bs2)); // NOLINT
if (sample_point_permill > MaxSamplePointLocationPermill) // Strictly more!
{
bs1 = (uint8_t)((7 * bs1_bs2_sum - 1) / 8); // Nope, too far; now rounding to zero
bs2 = (uint8_t)(bs1_bs2_sum - bs1);
}
}
const bool valid = (bs1 >= 1) && (bs1 <= MaxBS1) && (bs2 >= 1) && (bs2 <= MaxBS2);
/*
* Final validation
* Helpful Python:
* def sample_point_from_btr(x):
* assert 0b0011110010000000111111000000000 & x == 0
* ts2,ts1,brp = (x>>20)&7, (x>>16)&15, x&511
* return (1+ts1+1)/(1+ts1+1+ts2+1)
*/
if ((target_bitrate != (peripheral_clock_rate / (prescaler * (1U + bs1 + bs2)))) ||
!valid)
{
// This actually means that the algorithm has a logic error, hence assert(0).
//assert(0); // NOLINT
return -CAN_STM32_ERROR_UNSUPPORTED_BIT_RATE;
}
out_timings->baud_rate_prescaler = (uint16_t) prescaler;
out_timings->resynchronization_jump_width = 1; // One is recommended by UAVCAN, CANOpen, and DeviceNet
out_timings->time_segment_1 = bs1;
out_timings->time_segment_2 = bs2;
if (DEBUG) {
Serial.print("target_bitrate=");
Serial.println(target_bitrate);
Serial.print("peripheral_clock_rate=");
Serial.println(peripheral_clock_rate);
Serial.print("timings.baud_rate_prescaler=");
Serial.println(out_timings->baud_rate_prescaler);
Serial.print("timings.time_segment_1=");
Serial.println(out_timings->time_segment_1);
Serial.print("timings.time_segment_2=");
Serial.println(out_timings->time_segment_2);
Serial.print("timings.resynchronization_jump_width=");
Serial.println(out_timings->resynchronization_jump_width);
}
return 0;
}
void canInit(enum BitRate bitrate) {
RCC->APB1ENR |= 0x2000000UL; // Enable CAN clock
RCC->APB2ENR |= 0x1UL; // Enable AFIO clock
@ -113,15 +273,29 @@ void canInit(enum BitRate bitrate) {
RCC->APB2ENR |= 0x4UL; // Enable GPIOA clock (bit2 to 1)
GPIOA->CRH &= 0xFFF00FFF;
GPIOA->CRH |= 0xB8000UL; // Configure PA11 and PA12
GPIOA->ODR |= 0x1000UL;
GPIOA->ODR |= 0x1800UL;
CAN1->MCR = 0x51UL; // Set CAN to initialization mode
while ((CAN1->MSR & CAN_MSR_INAK) == 0U);
// Set bit rates
CAN1->BTR &= ~(((0x03) << 24) | ((0x07) << 20) | ((0x0F) << 16) | (0x1FF));
CAN1->BTR |= (((can_configs[bitrate].TS2-1) & 0x07) << 20) | (((can_configs[bitrate].TS1-1) & 0x0F) << 16) | ((can_configs[bitrate].BRP-1) & 0x1FF);
CAN_bit_timing_config_t timings;
Serial.print("bitrate=");
Serial.println(bitrate);
uint32_t target_bitrate = SPEED[bitrate];
Serial.print("target_bitrate=");
Serial.println(target_bitrate);
int result = ComputeCANTimings(HAL_RCC_GetPCLK1Freq(), target_bitrate, &timings);
if (result) while(true);
CAN1->BTR = (((timings.resynchronization_jump_width - 1U) & 3U) << 24U) |
(((timings.time_segment_1 - 1U) & 15U) << 16U) |
(((timings.time_segment_2 - 1U) & 7U) << 20U) |
((timings.baud_rate_prescaler - 1U) & 1023U);
//// Set bit rates
//CAN1->BTR &= ~(((0x03) << 24) | ((0x07) << 20) | ((0x0F) << 16) | (0x1FF));
//CAN1->BTR |= (((can_configs[bitrate].TS2-1) & 0x07) << 20) | (((can_configs[bitrate].TS1-1) & 0x0F) << 16) | ((can_configs[bitrate].BRP-1) & 0x1FF);
// Configure Filters to default values
CAN1->FMR |= 0x1UL; // Set to filter initialization mode
@ -232,6 +406,7 @@ void checkDataRequest() {
void canSend(CAN_msg_t* CAN_tx_msg, uint8_t mbx = 0) {
volatile uint32_t count = 0;
CAN1->sTxMailBox[mbx].TIR = 0;
CAN1->sTxMailBox[mbx].TIR = (CAN_tx_msg->id) << 21;
CAN1->sTxMailBox[mbx].TDTR &= ~(0xF);
@ -269,7 +444,7 @@ void loop() {
switch(MAX31855_chips[i].detectThermocouple(rawData[i])) {
case MAX31855_THERMOCOUPLE_OK:
//egt[i] = (MAX31855_chips[i].getTemperature(rawData[i]));
egt[i] = (rawData[i] >> 18)/4;
egt[i] = (rawData[i] >> 20);
LOG_D("Temp: %6d \n\r", egt[i]);
break;
case MAX31855_THERMOCOUPLE_SHORT_TO_VCC:

BIN
Case/v2.0/EGT.S v2.3mf Normal file

Binary file not shown.

BIN
Case/v2.0/EGT.S v2.f3d Normal file

Binary file not shown.

BIN
Case/v2.0/EGT.S v3.3mf Normal file

Binary file not shown.

BIN
Case/v2.0/EGT.S v3.f3d Normal file

Binary file not shown.

145
Case/v2.0/OBJ_PCB v2.X.mtl Normal file
View File

@ -0,0 +1,145 @@
newmtl mtl1
Ka 0.62 0.62 0.36
Kd 0.62 0.62 0.36
Ks 0.00 0.00 0.00
endmtl
newmtl mtl2
Ka 0.21 0.21 0.21
Kd 0.21 0.21 0.21
Ks 0.02 0.02 0.02
endmtl
newmtl mtl3
Ka 0.35 0.35 0.35
Kd 0.35 0.35 0.35
Ks 0.02 0.02 0.02
endmtl
newmtl mtl4
Ka 0.67 0.67 0.67
Kd 0.67 0.67 0.67
Ks 0.47 0.47 0.47
endmtl
newmtl mtl5
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.88 0.88 0.88
endmtl
newmtl mtl6
Ka 0.59 0.46 0.00
Kd 0.59 0.46 0.00
Ks 0.29 0.23 0.00
endmtl
newmtl mtl7
Ka 0.85 0.85 0.85
Kd 0.85 0.85 0.85
Ks 0.43 0.43 0.43
endmtl
newmtl mtl8
Ka 0.22 0.22 0.22
Kd 0.22 0.22 0.22
Ks 0.11 0.11 0.11
endmtl
newmtl mtl9
Ka 0.85 0.85 0.85
Kd 0.85 0.85 0.85
Ks 0.42 0.42 0.42
endmtl
newmtl mtl10
Ka 1.00 0.98 0.94
Kd 1.00 0.98 0.94
Ks 0.50 0.49 0.47
endmtl
newmtl mtl11
Ka 0.75 0.75 0.75
Kd 0.75 0.75 0.75
Ks 0.38 0.38 0.38
endmtl
newmtl mtl12
Ka 0.50 0.50 0.50
Kd 0.50 0.50 0.50
Ks 0.25 0.25 0.25
endmtl
newmtl mtl13
Ka 0.70 0.65 0.09
Kd 0.70 0.65 0.09
Ks 0.05 0.05 0.01
endmtl
newmtl mtl14
Ka 0.77 0.77 0.77
Kd 0.77 0.77 0.77
Ks 0.62 0.62 0.62
endmtl
newmtl mtl15
Ka 0.25 0.25 0.25
Kd 0.25 0.25 0.25
Ks 0.07 0.07 0.07
endmtl
newmtl mtl16
Ka 0.41 0.41 0.41
Kd 0.41 0.41 0.41
Ks 0.20 0.20 0.20
endmtl
newmtl mtl17
Ka 0.15 0.15 0.15
Kd 0.15 0.15 0.15
Ks 0.04 0.04 0.04
endmtl
newmtl mtl18
Ka 0.65 0.65 0.65
Kd 0.65 0.65 0.65
Ks 0.20 0.20 0.20
endmtl
newmtl mtl19
Ka 0.45 0.43 0.42
Kd 0.45 0.43 0.42
Ks 0.03 0.03 0.03
endmtl
newmtl mtl20
Ka 0.63 0.63 0.63
Kd 0.63 0.63 0.63
Ks 0.04 0.04 0.04
endmtl
newmtl mtl21
Ka 0.25 0.25 0.25
Kd 0.25 0.25 0.25
Ks 0.13 0.13 0.13
endmtl
newmtl mtl22
Ka 1.00 0.65 0.00
Kd 1.00 0.65 0.00
Ks 0.50 0.33 0.00
endmtl
newmtl mtl23
Ka 0.00 0.00 0.00
Kd 0.00 0.00 0.00
Ks 0.00 0.00 0.00
endmtl
newmtl mtl24
Ka 0.83 0.67 0.13
Kd 0.83 0.67 0.13
Ks 0.83 0.67 0.13
endmtl
newmtl mtl25
Ka 0.97 0.88 0.60
Kd 0.97 0.88 0.60
Ks 0.68 0.62 0.42
endmtl
newmtl mtl26
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.50 0.50 0.50
endmtl
newmtl mtl27
Ka 0.50 0.25 0.00
Kd 0.50 0.25 0.00
Ks 0.44 0.22 0.00
endmtl
newmtl mtl28
Ka 0.39 0.51 0.70
Kd 0.39 0.51 0.70
Ks 0.03 0.04 0.05
endmtl
newmtl mtl29
Ka 0.79 0.82 0.93
Kd 0.79 0.82 0.93
Ks 0.40 0.41 0.47
endmtl

484054
Case/v2.0/OBJ_PCB v2.X.obj Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.