Skip to content

Commit

Permalink
Merge pull request #103 from larus-breeze/work
Browse files Browse the repository at this point in the history
Work
  • Loading branch information
realtimepeople authored Jul 12, 2024
2 parents b9706c6 + 18a5ce0 commit c9cb71d
Show file tree
Hide file tree
Showing 11 changed files with 470 additions and 378 deletions.
695 changes: 368 additions & 327 deletions sw_stm32/.cproject

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions sw_stm32/Communication/NMEA_Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
#include "sensor_dump.h"
#include "uSD_handler.h"

COMMON string_buffer_t NMEA_buf;
COMMON string_buffer_t __ALIGNED( sizeof(string_buffer_t)) NMEA_buf;
extern USBD_HandleTypeDef hUsbDeviceFS; // from usb_device.c

static void runnable (void* data)
static void NMEA_runnable (void* data)
{
delay( NMEA_START_DELAY);

Expand Down Expand Up @@ -93,12 +93,15 @@ static void runnable (void* data)
{
NMEA_buf.length = 0; // start at the beginning of the buffer
format_NMEA_string_fast( output_data, NMEA_buf, horizon_available);
#if FAST_NMEA_POSITION_OUTPUT
format_NMEA_string_slow( output_data, NMEA_buf);
#else
if( --decimating_counter == 0)
{
decimating_counter = NMEA_DECIMATION_RATIO;
format_NMEA_string_slow( output_data, NMEA_buf);
}

#endif
//Check if there is a CAN Message received which needs to be replayed via a Larus NMEA PLARS Sentence.
float32_t value;
char *next = NMEA_buf.string + NMEA_buf.length;
Expand Down Expand Up @@ -138,5 +141,19 @@ static void runnable (void* data)
}
}

COMMON RestrictedTask NMEA_task( runnable, "NMEA", 256, 0, (NMEA_USB_PRIORITY) | portPRIVILEGE_BIT);
static TaskParameters_t p =
{
NMEA_runnable,
"NMEA",
256, 0,
NMEA_USB_PRIORITY | portPRIVILEGE_BIT,
0,
{
{ COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE },
{ (void *)&NMEA_buf, sizeof(NMEA_buf), portMPU_REGION_READ_WRITE },
{ 0, 0, 0 }
}
};

COMMON RestrictedTask NMEA_task( p);

15 changes: 9 additions & 6 deletions sw_stm32/Communication/sensor_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA
s=append_string( s, "Sats: ");
s = format_2_digits( s, (float32_t)(output_data.c.SATS_number));

s=append_string( s, " GNSS time: ");
s=append_string( s, " Speed-Accuracy = ");
s = to_ascii_2_decimals( 100.0f * (float32_t)(output_data.c.speed_acc), s);

s=append_string( s, "m/s, GNSS time: ");
s = format_2_digits( s, output_data.c.hour);
*s ++ = ':';
s = format_2_digits( s, output_data.c.minute);
Expand All @@ -157,7 +160,7 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA
*s++=' ';
}

s=append_string( s, " Strength: ");
s=append_string( s, " Strength = ");
s = to_ascii_2_decimals( 100.0f * output_data.nav_induction_gnss.abs(), s);
newline( s);

Expand All @@ -168,10 +171,11 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA
s = to_ascii_1_decimal( RAD_2_DEGREES_10 * heading, s);

s=append_string( s, " Inclination = ");
s = to_ascii_2_decimals( RAD_2_DEGREES_10 * inclination_decimator.get_output(), s);
s = to_ascii_1_decimal( RAD_2_DEGREES_10 * inclination_decimator.get_output(), s);

s=append_string( s, " MagAnomaly / % = ");
s=append_string( s, " MagAnomaly = ");
s = to_ascii_2_decimals( output_data.magnetic_disturbance * 10000.0f, s);
*s++ = '%';
newline( s);

if( output_data.c.sat_fix_type == (SAT_FIX | SAT_HEADING))
Expand All @@ -185,7 +189,7 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA
s = to_ascii_1_decimal( RAD_2_DEGREES_10 * output_data.c.relPosHeading, s);
}
else
s=append_string( s, "No D-GNSS");
s=append_string( s, "No D-GNSS-fix");

newline( s);

Expand All @@ -211,7 +215,6 @@ void format_sensor_dump( const output_data_t &output_data, string_buffer_t &NMEA
format_PLARV ( vario, 0.0f, 0.0f, 0.0f, s);
}
newline( s);
*s = 0;
NMEA_buf.length = s - NMEA_buf.string;
}

Expand Down
12 changes: 6 additions & 6 deletions sw_stm32/Communication/uSD_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,24 +631,24 @@ void uSD_handler_runnable (void*)
write_crash_dump();
}

char out_filename[30];

// wait until a GNSS timestamp is available.
while (output_data.c.sat_fix_type == 0)
{
if( crashfile)
write_crash_dump();
delay (100);
}

// generate filename based on timestamp
char out_filename[30];
char * next = append_string( out_filename, "logger/");
next = format_date_time( next);

write_EEPROM_dump( out_filename); // now we have date+time, start logging

*next++ = '.';
*next++ = 'f';
next = format_2_digits( next, (sizeof(coordinates_t) + sizeof(measurement_data_t)) / sizeof(float));
next = format_2_digits( next, sizeof(observations_type) / sizeof(float));

fresult = f_open (&the_file, out_filename, FA_CREATE_ALWAYS | FA_WRITE);
if (fresult != FR_OK)
Expand All @@ -670,8 +670,8 @@ void uSD_handler_runnable (void*)
if( crashfile)
write_crash_dump();

memcpy (buf_ptr, (uint8_t*) &output_data.m, sizeof(measurement_data_t)+sizeof(coordinates_t));
buf_ptr += sizeof(measurement_data_t)+sizeof(coordinates_t);
memcpy (buf_ptr, (uint8_t*) &output_data.m, sizeof(observations_type));
buf_ptr += sizeof(observations_type);

if (buf_ptr < mem_buffer + MEM_BUFSIZE)
continue; // buffer only filled partially
Expand Down Expand Up @@ -729,7 +729,7 @@ static TaskParameters_t p =
{
{ COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE },
{ (void *)0x80f8000, 0x8000, portMPU_REGION_READ_WRITE },
{ 0, 0, 0 }
{ 0, 0, 0}
}
};

Expand Down
2 changes: 1 addition & 1 deletion sw_stm32/Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void heartbeat(void);
#define L3GD20_INT1_GPIO_Port GPIOB
#define L3GD20_INT2_Pin GPIO_PIN_1
#define L3GD20_INT2_GPIO_Port GPIOB
#define SPI2_NSS_Pin GPIO_PIN_12
#define SPI2_NSS_Pin GPIO_PIN_4
#define SPI2_NSS_GPIO_Port GPIOB
#define MTi_1IMU_PSEL0_Pin GPIO_PIN_10
#define MTi_1IMU_PSEL0_GPIO_Port GPIOD
Expand Down
2 changes: 2 additions & 0 deletions sw_stm32/Core/Inc/system_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#define RUN_L3GD20 0
#define RUN_FXOS8700 0
#define RUN_PITOT_MODULE 1

#define RUN_MICROPHONE 0

#define RUN_CAN_TESTER 0
Expand Down Expand Up @@ -92,6 +93,7 @@

#define NMEA_REPORTING_PERIOD 250 // period in clock ticks for NMEA output
#define NMEA_DECIMATION_RATIO 6 // slow-down factor for the slow properties
#define FAST_NMEA_POSITION_OUTPUT 0

#define ACTIVATE_FPU_EXCEPTION_TRAP 1 // I want to be SET !
#define SET_FPU_FLUSH_TO_ZERO 1
Expand Down
4 changes: 3 additions & 1 deletion sw_stm32/Core/Startup/startup_stm32f407vgtx.s
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*
******************************************************************************
*/
#include "git-commit-version.h"

.syntax unified
.cpu cortex-m4
Expand Down Expand Up @@ -229,7 +230,8 @@ g_pfnVectors:
.word 0 /* CRYP crypto */
.word HASH_RNG_IRQHandler /* Hash and Rng */
.word FPU_IRQHandler /* FPU */


.ascii GIT_COMMIT_HASH

/*******************************************************************************
*
Expand Down
60 changes: 31 additions & 29 deletions sw_stm32/Drivers/Custom/microphone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "stm32f4xx_hal_cortex.h"
#include "stm32f4xx_hal_spi.h"
#include "spi.h"
#include "communicator.h"
#include "microphone.h"
#include "embedded_math.h"

#if RUN_MICROPHONE
Expand Down Expand Up @@ -94,109 +96,109 @@ void configure_SPI_interface (void)
__HAL_LINKDMA( &hspi2 , hdmarx, hdma_spi2_rx);
}

#define MIC_DMA_BUFSIZE_HALFWORDS 256
#define SAMPLE_BUFSIZE 512

uint16_t __ALIGNED( MIC_DMA_BUFSIZE_HALFWORDS * sizeof( uint16_t)) mic_DMA_buffer[ MIC_DMA_BUFSIZE_HALFWORDS];
int8_t __ALIGNED( SAMPLE_BUFSIZE) samples_at_5_kHz[SAMPLE_BUFSIZE];
int8_t __ALIGNED( SAMPLE_BUFSIZE) audio_samples[2][SAMPLE_BUFSIZE];

// our byte samples have 328kHz sampling-rate
// resampling by 64 gives 5.125 kHz sampling-rate
void resample_by_64( uint8_t * bytes, int8_t * samples, unsigned bytecount)
void resample( uint8_t * bytes, int8_t * samples, unsigned output_bytecount)
{
do
{
int accumulator = 0;

for( unsigned i=0; i<64; ++i)
for( unsigned i=0; i<RESAMPLING_RATIO; ++i)
accumulator += bit_count_table[*bytes++];

*samples++ = (int8_t)(accumulator - 256);
*samples++ = (int8_t)(accumulator - RESAMPLING_RATIO * 4);

bytecount -= 64;
--output_bytecount;
}
while( bytecount > 0);
while( output_bytecount > 0);
}

uint64_t getTime_usec(void);

COMMON float ac_power;

static void runnable (void*)
{
configure_SPI_interface ();
register_SPI_usertask( &hspi2);
uint32_t double_buffer_index=0;

delay(3000);

HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS);

uint32_t BufferIndex; // 0 -> first half, 1 second half
uint32_t BufferIndex=0; // 0 -> first half, 1 second half

int8_t * samples_pointer = samples_at_5_kHz;
int8_t * samples_pointer = audio_samples[double_buffer_index];
int32_t sum = 0;
int32_t qsum = 0;

while (true)
{
BufferIndex = 0xffffffff;
uint64_t timestamp = getTime_usec();
xTaskNotifyWait( 0, 0, &BufferIndex, 2);
xTaskNotifyWait( 0, 0, &BufferIndex, 40); // trigger on half complete interrupt

if( BufferIndex != 0) // be sure to be behind the half transfer interrupt
{
HAL_SPI_DMAStop( &hspi2);
HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS);

samples_pointer = samples_at_5_kHz;
double_buffer_index = 0;
samples_pointer = audio_samples[double_buffer_index];
sum = 0;
qsum = 0;

continue; // re-synchronize
}
// now we have 256 byte samples @ the beginning of our buffer
resample_by_64( (uint8_t *)mic_DMA_buffer, samples_pointer, MIC_DMA_BUFSIZE_HALFWORDS);
// now we have 128 byte samples @ the beginning of our buffer
resample( (uint8_t *)mic_DMA_buffer, samples_pointer, SAMPLE_BUFSIZE_HALF);

// do statistics and advance pointer
for( unsigned i=0; i<4; ++i)
for( unsigned i=0; i < SAMPLE_BUFSIZE_HALF; ++i)
{
sum += (int32_t)*samples_pointer;
qsum += (int32_t)*samples_pointer * *samples_pointer;
++samples_pointer;
}

BufferIndex = 0xffffffff;
xTaskNotifyWait( 0, 0, &BufferIndex, 2);
xTaskNotifyWait( 0, 0, &BufferIndex, 40);

if( BufferIndex != 1) // be sure to be behind the transfer complete interrupt
{
HAL_SPI_DMAStop( &hspi2);
HAL_SPI_Receive_DMA( &hspi2, (uint8_t *)mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS);

samples_pointer = samples_at_5_kHz;
double_buffer_index = 0;
samples_pointer = audio_samples[double_buffer_index];
sum = 0;
qsum = 0;

continue; // re-synchronize
}
// now we have another 256 byte samples starting @ the middle of our buffer
resample_by_64( (uint8_t *)(&mic_DMA_buffer[MIC_DMA_BUFSIZE_HALFWORDS / 2]), samples_pointer, MIC_DMA_BUFSIZE_HALFWORDS);
// now we have 128 byte samples @ the middle of our buffer
resample( (uint8_t *)(&mic_DMA_buffer[MIC_DMA_BUFSIZE_HALFWORDS / 2]), samples_pointer, SAMPLE_BUFSIZE_HALF);

// do statistics and advance pointer
for( unsigned i=0; i<4; ++i)
for( unsigned i=0; i < SAMPLE_BUFSIZE_HALF; ++i)
{
sum += (int32_t)*samples_pointer;
qsum += (int32_t)*samples_pointer * *samples_pointer;
++samples_pointer;
}

// summa summarum ...
if( samples_pointer >= samples_at_5_kHz + SAMPLE_BUFSIZE)
if( samples_pointer >= audio_samples[double_buffer_index] + SAMPLE_BUFSIZE)
{
// this scaling delivers ac_power = 13.0 @ 94dB
ac_power = (qsum * SAMPLE_BUFSIZE - sum * sum) / ((float)SAMPLE_BUFSIZE * SAMPLE_BUFSIZE);

samples_pointer = samples_at_5_kHz;
output_data.sound_intensity = (qsum * SAMPLE_BUFSIZE - sum * sum) / ((float)SAMPLE_BUFSIZE * SAMPLE_BUFSIZE);
sum = 0;
qsum = 0;

double_buffer_index ^= 1; // switch our twin buffer
samples_pointer = audio_samples[double_buffer_index];
}
}
}
Expand All @@ -212,7 +214,7 @@ static TaskParameters_t p =
{
{ COMMON_BLOCK, COMMON_SIZE, portMPU_REGION_READ_WRITE },
{ mic_DMA_buffer, MIC_DMA_BUFSIZE_HALFWORDS * 2, portMPU_REGION_READ_ONLY },
{ samples_at_5_kHz, SAMPLE_BUFSIZE, portMPU_REGION_READ_WRITE }
{ audio_samples, sizeof(audio_samples), portMPU_REGION_READ_WRITE}
}
};

Expand Down
25 changes: 25 additions & 0 deletions sw_stm32/Drivers/Custom/microphone.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* microphone.h
*
* Created on: Jun 22, 2024
* Author: schaefer
*/

#ifndef CUSTOM_MICROPHONE_H_
#define CUSTOM_MICROPHONE_H_

// byte samples come with 2.625 MHz / 8 = 328.125 kHz
// resampling by 32 gives 10.254 kHz
#define RESAMPLING_RATIO 32
#define NUM_BUFFERS 2 // double buffered for DMA and for uSD writing
#define MIC_DMA_BUFSIZE_HALFWORDS 4096 // filled after 50ms
#define MIC_DMA_BUFSIZE_BYTES (MIC_DMA_BUFSIZE_HALFWORDS * sizeof(uint16_t))
// the sample buffer contains 512 bytes sampled at 10.254 kHz
// acquisition time 50ms -> 20 Hz min freq.
#define SAMPLE_BUFSIZE (MIC_DMA_BUFSIZE_BYTES / RESAMPLING_RATIO)
#define SAMPLE_BUFSIZE_HALF (SAMPLE_BUFSIZE / 2)

extern Queue <uint8_t *> mic_data_pointer_Q;
extern int8_t audio_samples[2][SAMPLE_BUFSIZE];

#endif /* CUSTOM_MICROPHONE_H_ */
6 changes: 3 additions & 3 deletions sw_stm32/Tracealyzer/trcSnapshotConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@
* check the actual usage by selecting View menu -> Trace Details ->
* Resource Usage -> Object Table.
******************************************************************************/
#define TRC_CFG_NTASK 15
#define TRC_CFG_NISR 5
#define TRC_CFG_NQUEUE 10
#define TRC_CFG_NTASK 20
#define TRC_CFG_NISR 20
#define TRC_CFG_NQUEUE 20
#define TRC_CFG_NSEMAPHORE 10
#define TRC_CFG_NMUTEX 10
#define TRC_CFG_NTIMER 5
Expand Down
2 changes: 1 addition & 1 deletion sw_stm32/lib
Submodule lib updated from aaa49a to 65a7d5

0 comments on commit c9cb71d

Please sign in to comment.