Urbanite: Parking aid system
port_ultrasound.h File Reference

Header for the portable functions to interact with the HW of the ultrasound sensors. The functions must be implemented in the platform-specific code. More...

#include <stdint.h>
#include <stdbool.h>

Macros

#define PORT_REAR_PARKING_SENSOR_ID   0
 
#define PORT_PARKING_SENSOR_TRIGGER_UP_US   10
 
#define PORT_PARKING_SENSOR_TIMEOUT_MS
 
#define SPEED_OF_SOUND_MS   343
 

Functions

void port_ultrasound_init (uint32_t ultrasound_id)
 Configure the HW specifications of a given ultrasound sensor. More...
 
void port_ultrasound_start_measurement (uint32_t ultrasound_id)
 Start a new measurement of the ultrasound sensor. More...
 
void port_ultrasound_stop_trigger_timer (uint32_t ultrasound_id)
 Stop the timer that controls the trigger signal. More...
 
void port_ultrasound_stop_echo_timer (uint32_t ultrasound_id)
 Stop the timer that controls the echo signal. More...
 
void port_ultrasound_start_new_measurement_timer (void)
 Start the timer that controls the new measurement. More...
 
void port_ultrasound_stop_new_measurement_timer (void)
 Stop the timer that controls the new measurement. More...
 
void port_ultrasound_reset_echo_ticks (uint32_t ultrasound_id)
 Reset the time ticks of the echo signal. More...
 
void port_ultrasound_stop_ultrasound (uint32_t ultrasound_id)
 Stop all the timers of the ultrasound sensor and reset the echo ticks. More...
 
bool port_ultrasound_get_trigger_ready (uint32_t ultrasound_id)
 Get the readiness of the trigger signal. More...
 
void port_ultrasound_set_trigger_ready (uint32_t ultrasound_id, bool trigger_ready)
 Set the readiness of the trigger signal. More...
 
bool port_ultrasound_get_trigger_end (uint32_t ultrasound_id)
 Get the status of the trigger signal. More...
 
void port_ultrasound_set_trigger_end (uint32_t ultrasound_id, bool trigger_end)
 Set the status of the trigger signal. More...
 
uint32_t port_ultrasound_get_echo_init_tick (uint32_t ultrasound_id)
 Get the time tick when the init of echo signal was received. More...
 
void port_ultrasound_set_echo_init_tick (uint32_t ultrasound_id, uint32_t echo_init_tick)
 Set the time tick when the init of echo signal was received. More...
 
uint32_t port_ultrasound_get_echo_end_tick (uint32_t ultrasound_id)
 Get the time tick when the end of echo signal was received. More...
 
void port_ultrasound_set_echo_end_tick (uint32_t ultrasound_id, uint32_t echo_end_tick)
 Set the time tick when the end of echo signal was received. More...
 
bool port_ultrasound_get_echo_received (uint32_t ultrasound_id)
 Get the status of the echo signal. More...
 
void port_ultrasound_set_echo_received (uint32_t ultrasound_id, bool echo_received)
 Set the status of the echo signal. More...
 
uint32_t port_ultrasound_get_echo_overflows (uint32_t ultrasound_id)
 Get the number of overflows of the echo signal timer. More...
 
void port_ultrasound_set_echo_overflows (uint32_t ultrasound_id, uint32_t echo_overflows)
 Set the number of overflows of the echo signal timer. More...
 

Detailed Description

Header for the portable functions to interact with the HW of the ultrasound sensors. The functions must be implemented in the platform-specific code.

Author
Sistemas Digitales II
Date
2025-01-01

Macro Definition Documentation

◆ PORT_PARKING_SENSOR_TIMEOUT_MS

#define PORT_PARKING_SENSOR_TIMEOUT_MS

Time in ms to wait for the next measurement

◆ PORT_PARKING_SENSOR_TRIGGER_UP_US

#define PORT_PARKING_SENSOR_TRIGGER_UP_US   10

Duration in microseconds of the trigger signal

◆ PORT_REAR_PARKING_SENSOR_ID

#define PORT_REAR_PARKING_SENSOR_ID   0

Rear parking sensor identifier

◆ SPEED_OF_SOUND_MS

#define SPEED_OF_SOUND_MS   343

Speed of sound in air in m/s

Function Documentation

◆ port_ultrasound_get_echo_end_tick()

uint32_t port_ultrasound_get_echo_end_tick ( uint32_t  ultrasound_id)

Get the time tick when the end of echo signal was received.

TODO alumnos:

✅ 1. Return the value of the field echo_end_tick.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
Returns
uint32_t

◆ port_ultrasound_get_echo_init_tick()

uint32_t port_ultrasound_get_echo_init_tick ( uint32_t  ultrasound_id)

Get the time tick when the init of echo signal was received.

TODO alumnos:

✅ 1. Return the value of the field echo_init_tick.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
Returns
uint32_t

◆ port_ultrasound_get_echo_overflows()

uint32_t port_ultrasound_get_echo_overflows ( uint32_t  ultrasound_id)

Get the number of overflows of the echo signal timer.

This function returns the number of overflows of the echo signal timer. It is used to calculate the real time elapsed in the echo signal.

TODO alumnos:

✅ 1. Return the value of the field echo_overflows.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
Returns
uint32_t

◆ port_ultrasound_get_echo_received()

bool port_ultrasound_get_echo_received ( uint32_t  ultrasound_id)

Get the status of the echo signal.

This function returns the status of the echo signal. It will be true if the echo signal has been received (both the init and end ticks).

TODO alumnos:

✅ 1. Return the value of the field echo_received.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
Returns
true
false

◆ port_ultrasound_get_trigger_end()

bool port_ultrasound_get_trigger_end ( uint32_t  ultrasound_id)

Get the status of the trigger signal.

This function returns the status of the trigger signal. It will be true if the time to trigger the ultrasound sensor has finished.

TODO alumnos:

✅ 1. Return the value of the field trigger_end.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
Returns
true
false

◆ port_ultrasound_get_trigger_ready()

bool port_ultrasound_get_trigger_ready ( uint32_t  ultrasound_id)

Get the readiness of the trigger signal.

This function returns the status of readiness the trigger signal. If it is true, the ultrasound sensor is ready to start a new measurement.

TODO alumnos:

✅ 1. Return the value of the field trigger_ready.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
Returns
true
false

◆ port_ultrasound_init()

void port_ultrasound_init ( uint32_t  ultrasound_id)

Configure the HW specifications of a given ultrasound sensor.

Assuming we are using an STM32F4-based platform, this function must call the following functions:

TODO alumnos:

✅ 1. Initialize the fields of the ultrasound struct. Set the variables related to ticks to 0 and the flags to false but the trigger_ready to true.
✅ 2. Configure the trigger pin as output and no pull up neither pull down connection.
✅ 3. Configure the echo pin as alternate function and no pull up neither pull down connection.
✅ 4. Configure the alternate function of the echo pin.
✅ 5. Call the 3 private functions that configure the timers of the trigger, echo and new measurement.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array

◆ port_ultrasound_reset_echo_ticks()

void port_ultrasound_reset_echo_ticks ( uint32_t  ultrasound_id)

Reset the time ticks of the echo signal.

This function resets the time ticks of the echo signal once the distance has been calculated.

TODO alumnos:

✅ 1. Reset the time ticks of the echo signal, and the overflows. Also, reset the flag echo_received.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array

◆ port_ultrasound_set_echo_end_tick()

void port_ultrasound_set_echo_end_tick ( uint32_t  ultrasound_id,
uint32_t  echo_end_tick 
)

Set the time tick when the end of echo signal was received.

This function sets the time tick when the end of echo signal was received. It is called by the ISR of the input capture of the echo signal.

TODO alumnos:

✅ 1. Set the value of the field echo_end_tick with the received value.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
echo_end_tickTime tick when the end of echo signal was received.

◆ port_ultrasound_set_echo_init_tick()

void port_ultrasound_set_echo_init_tick ( uint32_t  ultrasound_id,
uint32_t  echo_init_tick 
)

Set the time tick when the init of echo signal was received.

This function sets the time tick when the init of echo signal was received. It is called by the ISR of the input capture of the echo signal.

TODO alumnos:

✅ 1. Set the value of the field echo_init_tick with the received value.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
echo_init_tickTime tick when the init of echo signal was received.

◆ port_ultrasound_set_echo_overflows()

void port_ultrasound_set_echo_overflows ( uint32_t  ultrasound_id,
uint32_t  echo_overflows 
)

Set the number of overflows of the echo signal timer.

This function sets the number of overflows of the echo signal timer. It is called by the ISR of the input capture of the echo signal when an overflow occurs to increment the number of overflows.

TODO alumnos:

✅ 1. Set the value of the field echo_overflows with the received value.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
echo_overflowsNumber of overflows of the echo signal timer.

◆ port_ultrasound_set_echo_received()

void port_ultrasound_set_echo_received ( uint32_t  ultrasound_id,
bool  echo_received 
)

Set the status of the echo signal.

This function sets the status of the echo signal. The ISR of the input capture of the echo signal calls this function to set the status of the echo signal when both the init and end ticks have been received.

TODO alumnos:

✅ 1. Set the value of the field echo_received with the received value.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
echo_receivedStatus of the echo signal.

◆ port_ultrasound_set_trigger_end()

void port_ultrasound_set_trigger_end ( uint32_t  ultrasound_id,
bool  trigger_end 
)

Set the status of the trigger signal.

This function sets the status of the trigger signal. It will be true to indicate that the time to trigger the ultrasound sensor has finished and the trigger signal is low.

TODO alumnos:

✅ 1. Set the value of the field trigger_end with the received value.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
trigger_endStatus of the trigger signal.

◆ port_ultrasound_set_trigger_ready()

void port_ultrasound_set_trigger_ready ( uint32_t  ultrasound_id,
bool  trigger_ready 
)

Set the readiness of the trigger signal.

This function sets the status of readiness of the trigger signal. If it is true, the ultrasound sensor will ready to start a new measurement.

TODO alumnos:

✅ 1. Set the value of the field trigger_ready with the received value.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array
trigger_readyStatus of the trigger signal.

◆ port_ultrasound_start_measurement()

void port_ultrasound_start_measurement ( uint32_t  ultrasound_id)

Start a new measurement of the ultrasound sensor.

This function prepares the timer of the trigger and the timer of the echo signal to start a new measurement. It also enables the timer that controls the new measurement.

Warning
The timer that controls the time of a new measurement is common for all the ultrasound sensors in the system, but the timers that controls the trigger and echo signal are specific for each ultrasound sensor and must be configured separately (within a conditional statement).

TODO alumnos:

✅ 1. Reset the flag trigger_ready to indicate that a new measurement has started.
✅ 2. Reset the counters (CNT) of the trigger timer, the echo timer, and the new measurement timer. Enclose the configuration of the trigger and echo timers within a conditional statement.
✅ 3. Set the trigger pin to high. You can use the BSRR or call the stm32f4_system_gpio_write() function.
✅ 4. Enable the timers interrupts in the NVIC using the NVIC_EnableIRQ() function and the TIMx_IRQn interrupts.
✅ 5. Enable the timers of the new measurement, the trigger timer, and the echo timer (register CR1 of the timers). Enclose the configuration of the trigger and echo timer within a conditional statement.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array

◆ port_ultrasound_start_new_measurement_timer()

void port_ultrasound_start_new_measurement_timer ( void  )

Start the timer that controls the new measurement.

This function starts the timer that controls the new measurement.

TODO alumnos:

✅ 1. Enable the interrupt of the new measurement timer in the NVIC.
✅ 2. Enable the new measurement timer (register CR1 of the timer).

◆ port_ultrasound_stop_echo_timer()

void port_ultrasound_stop_echo_timer ( uint32_t  ultrasound_id)

Stop the timer that controls the echo signal.

This function stops the timer that controls the echo signal because the echo signal has been received.

TODO alumnos:

✅ 1. Disable the echo timer (register CR1 of the timer). Enclose the configuration of the echo timer within a conditional statement.

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array

◆ port_ultrasound_stop_new_measurement_timer()

void port_ultrasound_stop_new_measurement_timer ( void  )

Stop the timer that controls the new measurement.

This function stops the timer that controls the new measurement.

TODO alumnos:

✅ 1. Disable the new measurement timer (register CR1 of the timer).

◆ port_ultrasound_stop_trigger_timer()

void port_ultrasound_stop_trigger_timer ( uint32_t  ultrasound_id)

Stop the timer that controls the trigger signal.

This function stops the timer that controls the trigger signal because the time to trigger the ultrasound sensor has finished. It also sets the trigger signal to low.

TODO alumnos:

✅ 1. Set the trigger pin to low. You can use the BSRR or call the stm32f4_system_gpio_write() function.
✅ 2. Disable the trigger timer (register CR1 of the timer).

Parameters
ultrasound_idUltrasound ID. This index is used to select the element of the ultrasound_arr[] array

◆ port_ultrasound_stop_ultrasound()

void port_ultrasound_stop_ultrasound ( uint32_t  ultrasound_id)

Stop all the timers of the ultrasound sensor and reset the echo ticks.

This function calls the functions to stop the trigger and echo timers and to reset the echo ticks.

TODO alumnos:

✅ 1. Call all the 4 functions to stop the trigger timer, the echo timer, the new measurement timer, and to reset the echo ticks.