Skip to content

Commit

Permalink
working on silabs support
Browse files Browse the repository at this point in the history
  • Loading branch information
runger1101001 committed Oct 6, 2024
1 parent 2326f98 commit 01a5edf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/drivers/hardware_specific/silabs/silabs_mcu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ void setupPWM(int pin_nr, long pwm_frequency, bool active_high, SilabsDriverPara
GPIO_Port_TypeDef port = getSilabsPortFromArduinoPin(pin_nr);
uint32_t pin = getSilabsPinFromArduinoPin(pin_nr);
TIMER_TypeDef* timer = TIMER0; // TODO determine correct timer
uint32_t timerFreq = 0;
uint32_t topValue = 0;
//CMU_ClockEnable(cmuClock_GPIO, true); assume this is done by Arduino core
CMU_ClockEnable(cmuClock_TIMER0, true); // enable timer clock

TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;
timerInit.enable = false;
timerInit.mode = timerModeUpDown;
// TODO adjust pre-scaler as needed to get the desired frequency
uint32_t max = TIMER_MaxCount(timer);
timerFreq = CMU_ClockFreqGet(cmuClock_TIMER0) / (timerInit.prescale + 1);
topValue = (timerFreq / pwm_frequency);
timerInit.prescale = pwm_frequency / max;
// TODO adjust pre-scaler as needed to get the desired frequency
uint32_t timerFreq = CMU_ClockFreqGet(cmuClock_TIMER0) / (timerInit.prescale + 1);
uint32_t topValue = (timerFreq / pwm_frequency / 2);
TIMER_TopSet(timer, topValue);

TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT;
Expand All @@ -41,6 +40,12 @@ void setupPWM(int pin_nr, long pwm_frequency, bool active_high, SilabsDriverPara
| (pin << _GPIO_TIMER_CC0ROUTE_PIN_SHIFT);
TIMER_InitCC(timer, 0, &timerCCInit);

params->timer[index] = timer;
params->channel[index] = 0;
params->pins[index] = pin_nr;
params->pwm_frequency = pwm_frequency;
params->resolution = topValue;

// TODO enable all the timers at the same time?
TIMER_Enable(timer, true);
}
Expand Down
65 changes: 64 additions & 1 deletion src/drivers/hardware_specific/silabs/silabs_mcu.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,65 @@
/**
* Silabs support for SimpleFOC library
*
* The Silabs Gecko EFR32 procossors as used in the Arduino Nano Matter have the following
* features:
* - 2 × 32-bit Timer/Counter with 3 Compare/Capture/PWM channels (TIMER0, TIMER1)
* - 3 × 16-bit Timer/Counter with 3 Compare/Capture/PWM channels (TIMER2, TIMER3, TIMER4)
* - All timers support dead-time insertion (DTI)
* - Centre-aligned PWM mode
* - Buffered compare register to ensure glitch-free update of compare values
* - Timers can trigger ADC and DMA via reflex system
* - Timers can be started/stopped synchronously by other timers
*
* The MCU is very flexible regarding its pin assigments. The following is the possible
* assignments:
*
* GPIO port A: TIMER0, TIMER1, TIMER2, TIMER4
* GPIO port B: TIMER0, TIMER1, TIMER2, TIMER4
* GPIO port C: TIMER0, TIMER1, TIMER3
* GPIO port D: TIMER0, TIMER1, TIMER3
*
* Any pin of these ports can be used with any of the 3 channels or the inverted
* channels of the possible timers.
*
* This suggests the following usage paradigm:
* - 3-PWM or 6-PWM use a single timer, all 3 channels
* - 2-PWM uses two channels of a single timer
* - 1-PWM uses a single channel
* - 4-PWM uses two timers, two channels each (we could try to sync them)
* - TIMER0 and TIMER1 are preferred, since they can be used with all GPIO ports
*
* Nano Matter Pin/Port assignments:
* D0 TX: PA04
* D1 RX: PA05
* D3: PC06
* D4 SDA1: PC07
* D5 SCL1: PC08
* D6: PC09
* D7: PD02
* D8: PD03
* D9: PTI_DATA: PD04
* D10 SS PTI_SYNC: PD05
* D11 MOSI: PA09
* D12 MISO: PA08
* D13 CLK: PB04
* A0: PB00
* A1: PB02
* A2: PB05
* A3: PC00
* A4 SDA: PA06
* A5 SCL: PA07
* A6: PB01
* A7: PB03
* RGB_R: PC01
* RGB_G: PC02
* RGB_B: PC03
*
* So the Nano Matter could use any of TIMER0, TIMER1, or TIMER3 with
* the SimpleFOC Nano Shield (3-PWM mode).
*
*/


#pragma once

Expand All @@ -12,7 +74,8 @@ typedef struct SilabsDriverParams {
int pins[6];
TIMER_TypeDef* timer[6];
uint8_t channel[6];
long pwm_frequency;
uint32_t pwm_frequency;
uint32_t resolution;
float dead_zone;
};

Expand Down

0 comments on commit 01a5edf

Please sign in to comment.