Skip to content

Commit 35beb27

Browse files
small refactor of some spi and timerwrapper functionality (#631)
* Add another function to SPI transcieve for slices (ptr + len) * TimerWrapper: Add helper to set the callback+data * fix typo in selecting scheduler timer * Add changeset * formatting * formatting, again * change from patch to minor, I think this fits better * Swap order of setting callback and its data * Update .changesets/refactor-spi-timerwrapper.md * fix wording on changeset --------- Co-authored-by: Óscar Gandía Iglesias <145703854+oganigl@users.noreply.github.com>
1 parent 9589e87 commit 35beb27

4 files changed

Lines changed: 39 additions & 2 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
release: minor
2+
summary: Small refactor of some spi and timerwrapper functionality
3+
4+
timerwrapper:
5+
- Add `set_callback(void (*callback)(void*), void* callback_data)` to set the callback and its data instead of needing to call `configurexxbit()` and set the period.
6+
- `set_limit_value(uint32_t arr)` to set the arr, this will likely be changed to use a `uint32_t` type only when using a 32 bit timer, for now it is just an alias to `instance->tim->ARR = arr;`. Was not added in this pr but also wasn't mentioned before.
7+
8+
spi:
9+
- Add `transceive` with ptr + data explicitly instead of using a span since it's sometimes a pain in the ass to use.

Inc/HALAL/Models/SPI/SPI2.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,29 @@ struct SPIDomain {
698698
return check_error_code(error_code);
699699
}
700700

701+
/**
702+
* @brief Sends and receives data over SPI in blocking mode.
703+
*/
704+
template <typename T> bool transceive(T* tx_data, T* rx_data, size_t count) {
705+
size_t size = count * sizeof(T);
706+
if (size % frame_size != 0) {
707+
ErrorHandler(
708+
"SPI transaction size (%d) not aligned to frame size (%d)",
709+
size,
710+
frame_size
711+
);
712+
return false;
713+
}
714+
auto error_code = HAL_SPI_TransmitReceive(
715+
&spi_instance.hspi,
716+
(uint8_t*)tx_data,
717+
(uint8_t*)rx_data,
718+
size / frame_size,
719+
10
720+
);
721+
return check_error_code(error_code);
722+
}
723+
701724
/**
702725
* @brief Sends and receives a trivially copyable data type over SPI in blocking mode.
703726
*/

Inc/HALAL/Models/TimerDomain/TimerDomain.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#ifndef SCHEDULER_TIMER_DOMAIN
2323
/* default is tim2 */
2424
#define SCHEDULER_TIMER_DOMAIN 2
25-
#elif (SCHEDULER_TIMER_DOMAIN != 2) && (SCHEDULER_TIMER_DOMAIN != 3) && \
25+
#elif (SCHEDULER_TIMER_DOMAIN != 2) && (SCHEDULER_TIMER_DOMAIN != 5) && \
2626
(SCHEDULER_TIMER_DOMAIN != 23) && (SCHEDULER_TIMER_DOMAIN != 24)
2727
#error Scheduler timer must be a 32 bit timer
2828
#endif

Inc/HALAL/Services/Time/TimerWrapper.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,20 @@ template <const TimerDomain::Timer& dev> struct TimerWrapper {
423423
// TODO: 16 bit and 32 bit version (?)
424424
inline void set_limit_value(uint32_t arr) { instance->tim->ARR = arr; }
425425

426+
inline void set_callback(void (*callback)(void*), void* callback_data) {
427+
TimerDomain::callback_data[instance->timer_idx] = callback_data;
428+
TimerDomain::callbacks[instance->timer_idx] = callback;
429+
}
430+
426431
inline void configure32bit(void (*callback)(void*), void* callback_data, uint32_t period) {
427432
static_assert(
428433
this->is_32bit_instance,
429434
"Only timers {TIM2, TIM5, TIM23, TIM24} have a 32-bit resolution"
430435
);
431436

432437
instance->tim->ARR = period;
433-
TimerDomain::callbacks[instance->timer_idx] = callback;
434438
TimerDomain::callback_data[instance->timer_idx] = callback_data;
439+
TimerDomain::callbacks[instance->timer_idx] = callback;
435440
this->counter_enable();
436441
}
437442

0 commit comments

Comments
 (0)