brazos-trapezoidal
Macros | Functions | Variables
main.c File Reference

Main file. More...

#include "main.h"
#include "includes.h"
Include dependency graph for main.c:

Go to the source code of this file.

Macros

#define SPI_CLK   PIN_PC1
 
#define SPI_SS   PIN_PC0
 
#define SPI_MISO   PIN_PC3
 
#define SPI_MOSI   PIN_PC2
 
#define SPI_CLK_CFG   XBAR_CTL_CFG_1
 
#define SPI_SS_CFG   XBAR_CTL_CFG_1
 
#define SPI_MISO_CFG   XBAR_IN_SPI_CFG_IN_MISO_PC3
 
#define SPI_MOSI_CFG   XBAR_CTL_CFG_1
 
#define UART0_TX   PIN_PB4
 
#define UART0_RX   PIN_PB5
 
#define UART1_TX   PIN_PR4
 
#define UART1_RX   PIN_PR3
 
#define TIM_CLK_DIV   1
 
#define TIM1_PER   3020
 
#define ADC_SAMPLE_PERIOD   7
 

Functions

void GpioXbarSetup (void)
 Gpio and crossbar function initialization . More...
 
void AdcSetup (void)
 Adc configuration function. Called by main_init function. More...
 
void DmaSetup (void)
 Dma configuration function. Called by main_init function. More...
 
void TimerPwmSetup (uint8_t ADC_Advance, uint16_t ADC_Advance_Delay_Syslks)
 Pwm configuration function. Called by main_init function. More...
 
void TimerSync (uint8_t SawUp)
 Timer sync up function between TIM0 and TIM2. Called by TIM0Event function. More...
 
void SpiInit (void)
 Spi init function - (debug purposes). Called by main_init function. More...
 
void UartSetup (void)
 Uart setup funtion. Called by main_init function. More...
 
void main_10ms (void)
 Main handler of 10ms. Called at a while(1) infinite loop at main(). Time controlled by a flag - slot_10ms. More...
 
void main_count_1ms (void)
 Main 1ms handler inside dma event function. Called by DMA_Event function. More...
 
bool UART0Event (uint32_t event)
 Uart0 Event function. More...
 
bool UART1Event (uint32_t event)
 Uart1 Event function. More...
 
void SysTickCallBack (void)
 Systick call back function Passed as argument of systick_initialize function at TimerInterrut.c Call rate of 250 us. More...
 
void TIM0Event (uint32_t event)
 Tim0 Event function Passed as argument of tim_initialize function;. More...
 
void TIM2Event (uint32_t event)
 Tim2 Event function Passed as argument of tim_initialize function;. More...
 
void PWM0Event (uint32_t event)
 Pwm0 Event function Passed as argument of pwm_initialize function;. More...
 
void PWM3Event (uint32_t event)
 Pwm3 Event function Passed as argument of pwm_initialize function;. More...
 
void PWM1Event (uint32_t event)
 Pwm1 Event function Passed as argument of pwm_initialize function;. More...
 
void PWM2Event (uint32_t event)
 Pwm2 Event function Passed as argument of pwm_initialize function;. More...
 
void DMA_Event (uint32_t event)
 Dma Event function Passed as argument of dma_initialize function;. More...
 
void ErrorEvent (uint32_t event)
 Error Event function for Dma Passed as argument of dma_initialize function;. More...
 
void WDogEvent (uint32_t event)
 Watchdog Event function Passed as argument of wdog_initialize function;. More...
 
void main_step (void)
 Main step function (FOR SIMULATION ONLY). Executes at control rate of 4KHz. More...
 
void main_init (void)
 Main initialization function . Executes at start of main function. More...
 
int main (void)
 Main function. More...
 
void HardFault_Handler (void)
 Hard fault handler. Cause reset due while(1) More...
 
void main__Start_Motor (int16_t speed, int16_t accel)
 Main motor start function. More...
 
void main__Stop_Motor (void)
 Main motor stop function. More...
 
void * main__pointerUart0 ()
 Get the uart0 configuration pointer. Used by Serial Buffer module. More...
 

Variables

uint16_t ADC_Samples [NUM_ADC_SAMPLES]
 
uint32_t ADC_CTL_ptr [NUM_ADC_SAMPLES]
 
volatile uint32_t TIM0_PER
 
volatile uint8_t Sync_Timer02
 
volatile uint8_t Saw_Up
 
uint16_t AdvDelay
 
uint32_t SPIData [5]
 
volatile unsigned short TimerOut1s = 100
 
volatile uint8_t TimerOutFlashOperation = 0
 
volatile uint8_t count_10ms = 0
 
volatile uint8_t slot_10ms = false
 
volatile uint8_t Counter1KHz
 
volatile uint8_t DmaEnableRequest = 1
 
void * pTim0
 
void * pTim1
 
void * pTim2
 
void * pPwm0
 
void * pPwm1
 
void * pPwm3
 
void * pPwm2
 
void * pUart0
 
void * pUart1
 
bool bTxDone = false
 
bool bRxDone = false
 
volatile int32_t TestBldcSpd = 350
 
volatile int32_t TestBldcRamp = 50
 
volatile uint16_t TestBldcUpdtSpdTimer = 1000
 
volatile uint8_t TestBldcUpdtSpdReq = 0
 
unsigned char TestBldcOn = 0
 
unsigned char TestBldcOff
 

Detailed Description

Main file.

This control does not fit to delta type motors.

Version
1.0
Author
pegorf, l.salviano jun, 2023

Definition in file main.c.

Macro Definition Documentation

◆ ADC_SAMPLE_PERIOD

#define ADC_SAMPLE_PERIOD   7

ADC sampling with gain amplification is giving by: 6 ADC_CLK + (ADC_SAMPLE_PERIOD + 7ADC_CLK) = 20 ADC_CLK (500ns) @ADC_CLK=40MHz

Definition at line 52 of file main.c.

◆ SPI_CLK

#define SPI_CLK   PIN_PC1

-----------------------------— Theta Power Systems - CONFIDENTIAL --------------------------------------—

< DEFINITIONS FOR PINS - SPI SPI clock pin - Config 1

Definition at line 31 of file main.c.

◆ SPI_CLK_CFG

#define SPI_CLK_CFG   XBAR_CTL_CFG_1

SPI clock pin - Config 1

Definition at line 36 of file main.c.

◆ SPI_MISO

#define SPI_MISO   PIN_PC3

SPI miso pin - Config IN

Definition at line 33 of file main.c.

◆ SPI_MISO_CFG

#define SPI_MISO_CFG   XBAR_IN_SPI_CFG_IN_MISO_PC3

SPI miso pin - Config IN

Definition at line 38 of file main.c.

◆ SPI_MOSI

#define SPI_MOSI   PIN_PC2

SPI mosi pin - Config 1

Definition at line 34 of file main.c.

◆ SPI_MOSI_CFG

#define SPI_MOSI_CFG   XBAR_CTL_CFG_1

SPI mosi pin - Config 1
DEFINITIONS FOR PINS - UART

Definition at line 41 of file main.c.

◆ SPI_SS

#define SPI_SS   PIN_PC0

SPI chip select pin - Config 1

Definition at line 32 of file main.c.

◆ SPI_SS_CFG

#define SPI_SS_CFG   XBAR_CTL_CFG_1

SPI chip select pin - Config 1

Definition at line 37 of file main.c.

◆ TIM1_PER

#define TIM1_PER   3020

Equation for baud rate (65536*Baud*16)/Fclk => Value used of baud is 115200 , Fclk is 40MHz DEFINITIONS FOR ADC

Definition at line 51 of file main.c.

◆ TIM_CLK_DIV

#define TIM_CLK_DIV   1

Clock divider for TIM counter is 1, therefore the peripheral clock is 40 MHz

Definition at line 48 of file main.c.

◆ UART0_RX

#define UART0_RX   PIN_PB5

UART0 RX pin

Definition at line 43 of file main.c.

◆ UART0_TX

#define UART0_TX   PIN_PB4

UART0 TX pin

Definition at line 42 of file main.c.

◆ UART1_RX

#define UART1_RX   PIN_PR3

UART1 TX pin
Timer configuration for UART - 115200

Definition at line 47 of file main.c.

◆ UART1_TX

#define UART1_TX   PIN_PR4

UART1 TX pin

Definition at line 44 of file main.c.

Function Documentation

◆ AdcSetup()

void AdcSetup ( void  )

Adc configuration function. Called by main_init function.

For all channels.
Continuous conversions: 0x00000006. For single count: 0x00000005
CNT_LIMIT [18:8]: 0x0 Count limit is set to 0
Reserved [7] : 0x0
SEQ [6] : 0x0 0: Reset the channel sequence to start at the lowest enabled channel. Self clearing
EDGE_TRIG [5:4] : 0x0 on rising edge
AHW_TRIG [3] : 0x0
OVER_EN [2] : 0x1 ADC over write enabled
CONT_EN [1] : 0x1 Continuous mode enabled
CNT_EN [0] : 0x0 Count mode disabled.

< For last converted channel HW Trigger Rise Edge 0x0000004D CNT_LIMIT [18:8]: 0x1 Count limit is set to 1 Reserved [7] : 0x0 SEQ [6] : 0x0 1: Reset the channel sequence to start at the lowest enabled channel. Self clearing EDGE_TRIG [5:4] : 0x0 on rising edge AHW_TRIG [3] : 0x1 trigger ADC StartConversion on rising edge of hardware signal - continuous mode OVER_EN [2] : 0x1 ADC over write enabled CONT_EN [1] : 0x0 Continuous mode disabled, ahw will manage trigger. CNT_EN [0] : 0x1 Count mode enabled, number of conversions is CNT_LIMIT+1.

< CH0 as analog pin

< CH0 no pull down

< CH1 as analog pin

< CH1 no pull down

< CH2 as analog pin

< CH2 no pull down

< CH3 as analog pin

< CH3 no pull down

< CH4 as analog pin

< CH4 no pull down

< CH5 as analog pin

< CH5 no pull down

< CH6 as analog pin

< CH6 no pull down

< CH7 as analog pin

< CH7 no pull down

< AVDD regulator Internal Compensation

< Single Range Selection, 2 times VREF

< Select ADC internal reference source - VDDIO over 2

< Select ADC reference voltage - Internal

< Select ADC gain multiplier - times 1

< ADC sampling with gain amplification is giving by: 6 ADC_CLK + (ADC_SAMPLE_PERIOD + 7ADC_CLK) = 20 ADC_CLK (500ns) @ADC_CLK=40MHz

< Set channel as single ended

< Set channel as single ended

< Set channel as single ended

< Set channel as single ended

< Set channel as single ended

< Set channel as single ended

< Set channel as single ended

< Set channel as single ended

< ADC over-write (of data and channel number) enabled

< ADC continuous mode disabled

< ADC count conversion enable. Self clearing when number of requested conversion (CNT_LIMIT + 1) are done. Writing 1 will abort current sequence

< ADC CNT_LIMIT set to zero

< ADC interrupt enabled

< ADC Trigger set as PWM0

< XBAR ADC trigger type set as TIM source, pwm0

< Edge trigger on the rising edge

< AHW trigger source enabled

< Continuous mode enabled

< ADC interrupt priority set to highest

< AHW trigger source enabled

< ADC over-write (of data and channel number) disabled

< ADC interrupt disabled

Definition at line 700 of file main.c.

◆ DMA_Event()

void DMA_Event ( uint32_t  event)

Dma Event function Passed as argument of dma_initialize function;.

Parameters
event- callback event in the form of DMA_SignalEvent_t

Definition at line 344 of file main.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DmaSetup()

void DmaSetup ( void  )

Dma configuration function. Called by main_init function.

Definition at line 806 of file main.c.

Here is the call graph for this function:

◆ ErrorEvent()

void ErrorEvent ( uint32_t  event)

Error Event function for Dma Passed as argument of dma_initialize function;.

Parameters
event- callback event in the form of DMA_SignalEvent_t

Definition at line 451 of file main.c.

Here is the caller graph for this function:

◆ GpioXbarSetup()

void GpioXbarSetup ( void  )

Gpio and crossbar function initialization .

Definition at line 618 of file main.c.

Here is the caller graph for this function:

◆ HardFault_Handler()

void HardFault_Handler ( void  )

Hard fault handler. Cause reset due while(1)

Definition at line 691 of file main.c.

◆ main()

int main ( void  )

Main function.

Definition at line 672 of file main.c.

Here is the call graph for this function:

◆ main_10ms()

void main_10ms ( void  )

Main handler of 10ms. Called at a while(1) infinite loop at main(). Time controlled by a flag - slot_10ms.

< Service the watchdog

< Decrement 1s timer

< Toggle leds at 1s rate

< Reload 1s timer

< Check for a request on flash parameter access (write, read, post write)

< Access to flash only possible with motor stopped

< Motor is stopped if it was running and after 1s

Definition at line 558 of file main.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main__pointerUart0()

void * main__pointerUart0 ( )

Get the uart0 configuration pointer. Used by Serial Buffer module.

Definition at line 1126 of file main.c.

◆ main__Start_Motor()

void main__Start_Motor ( int16_t  speed,
int16_t  accel 
)

Main motor start function.

Parameters
speed- motor speed in rpm
accel- motor ramp in rpm/s

Definition at line 1100 of file main.c.

Here is the call graph for this function:

◆ main__Stop_Motor()

void main__Stop_Motor ( void  )

Main motor stop function.

Definition at line 1117 of file main.c.

◆ main_count_1ms()

void main_count_1ms ( void  )

Main 1ms handler inside dma event function. Called by DMA_Event function.

Definition at line 1084 of file main.c.

Here is the caller graph for this function:

◆ main_init()

void main_init ( void  )

Main initialization function . Executes at start of main function.

< XBAR peripheral initialization

< GPIO peripheral initialization

< LSI initialization

< Microcontroller clocks initialization

< Timer Interrupt (Systick) initialization

< Calibration initialization

< Trapezoidal motor control initialization

< LEDs at off condition

< LEDs at off condition

< DMA enable request is set to true, enable it, when appropriate

< Analog to Digital converter configuration

< Dma configuration

< TIM0 is the pwm timer and its timer period is set for 40MHz clock with 16KHz operation. As it is triangular counter, the equation is divided by 2

< AdvDelay is the location of the pwm trigger, which is the peak of the triangular counter of pwm with a delay of 83 counts, or 2.1 [us], at 40MHz

< Setup the pwm (timers, pins and interrupts)

< ADC sequence is reset to get started

< Uart configuration - UART0 (PC interface) and UART1 (Jlink on board)

< API006MemoryRW module configuration

< API023MotorControl module configuration

< Tsp module configuration (Theta serial protocol)

< watchdog module initialization

< Enable write access to all other registers by writing 0x1ACCE551

< Update the value of 32-bit LOAD register - 64MHz counter targeting 150ms 64000000*0.150 = 9600000

< Reloads the counter from the value in WDOG LOAD

< Enable the interrupt event, WDOGINT. Set HIGH to Enable the counter and the interrupt

< Enable Watchdog reset output, WDOGRES. Set HIGH to enable the reset

< Disable write access by writing any other value than 0x1ACCE551

< Service the watchdog

< Tick timer for Bemf counting - free running counter

< Initialize Tick Timer as Free Runner Counter for Bemf time counting - 40MHz counter

< Set the source clock of the tick counter as HSOC = 40MHz

< Set the multiplier of clock of the tick counter as 1

Definition at line 489 of file main.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main_step()

void main_step ( void  )

Main step function (FOR SIMULATION ONLY). Executes at control rate of 4KHz.

Definition at line 468 of file main.c.

Here is the call graph for this function:

◆ PWM0Event()

static void PWM0Event ( uint32_t  event)

Pwm0 Event function Passed as argument of pwm_initialize function;.

Parameters
event- callback event in the form of PWM_SignalEvent_t

Definition at line 307 of file main.c.

Here is the caller graph for this function:

◆ PWM1Event()

static void PWM1Event ( uint32_t  event)

Pwm1 Event function Passed as argument of pwm_initialize function;.

Parameters
event- callback event in the form of PWM_SignalEvent_t

Definition at line 328 of file main.c.

Here is the caller graph for this function:

◆ PWM2Event()

static void PWM2Event ( uint32_t  event)

Pwm2 Event function Passed as argument of pwm_initialize function;.

Parameters
event- callback event in the form of PWM_SignalEvent_t

Definition at line 336 of file main.c.

Here is the caller graph for this function:

◆ PWM3Event()

static void PWM3Event ( uint32_t  event)

Pwm3 Event function Passed as argument of pwm_initialize function;.

Parameters
event- callback event in the form of PWM_SignalEvent_t

Definition at line 319 of file main.c.

Here is the caller graph for this function:

◆ SpiInit()

void SpiInit ( void  )

Spi init function - (debug purposes). Called by main_init function.

Definition at line 1000 of file main.c.

◆ SysTickCallBack()

void SysTickCallBack ( void  )

Systick call back function Passed as argument of systick_initialize function at TimerInterrut.c Call rate of 250 us.

Definition at line 200 of file main.c.

◆ TIM0Event()

void TIM0Event ( uint32_t  event)

Tim0 Event function Passed as argument of tim_initialize function;.

Parameters
event- callback event in the form of TIM_SignalEvent_t

Definition at line 212 of file main.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TIM2Event()

void TIM2Event ( uint32_t  event)

Tim2 Event function Passed as argument of tim_initialize function;.

Parameters
event- callback event in the form of TIM_SignalEvent_t

Definition at line 293 of file main.c.

Here is the caller graph for this function:

◆ TimerPwmSetup()

void TimerPwmSetup ( uint8_t  ADC_Advance,
uint16_t  ADC_Advance_Delay_Syslks 
)

Pwm configuration function. Called by main_init function.

Parameters
ADC_Advance- (0) for synchronization at Saw count up of TIM2
(1) for synchronization at Saw count down of TIM2
ADC_Advance_Delay_Syslks- pwm count clocks to trigger the adc sampling

Definition at line 856 of file main.c.

Here is the call graph for this function:

◆ TimerSync()

void TimerSync ( uint8_t  SawUp)

Timer sync up function between TIM0 and TIM2. Called by TIM0Event function.

Parameters
SawUp- (0) for synchronization at Saw count up of TIM2
(1) for synchronization at Saw count down of TIM2

Definition at line 955 of file main.c.

Here is the caller graph for this function:

◆ UART0Event()

bool UART0Event ( uint32_t  event)

Uart0 Event function.

Parameters
event- callback event in the form of USART_SignalEvent_t
Returns
true or false

Definition at line 134 of file main.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UART1Event()

bool UART1Event ( uint32_t  event)

Uart1 Event function.

Parameters
event- callback event in the form of USART_SignalEvent_t
Returns
true or false

Definition at line 181 of file main.c.

Here is the caller graph for this function:

◆ UartSetup()

void UartSetup ( void  )

Uart setup funtion. Called by main_init function.

Definition at line 1043 of file main.c.

Here is the call graph for this function:

◆ WDogEvent()

void WDogEvent ( uint32_t  event)

Watchdog Event function Passed as argument of wdog_initialize function;.

Parameters
event- callback event in the form of WDOG_SignalEvent_t

Definition at line 459 of file main.c.

Variable Documentation

◆ ADC_CTL_ptr

uint32_t ADC_CTL_ptr[NUM_ADC_SAMPLES]

ADC control configuration array carrying the adc's channel configuration

Definition at line 62 of file main.c.

◆ ADC_Samples

uint16_t ADC_Samples[NUM_ADC_SAMPLES]

< ADC variables definition ADC array carrying the converted ADCs values

Definition at line 61 of file main.c.

◆ AdvDelay

uint16_t AdvDelay

ADC triggering point Spi variables

Definition at line 72 of file main.c.

◆ bRxDone

bool bRxDone = false

Signaling flag for Rx on Uart0 Motor control variables

Definition at line 100 of file main.c.

◆ bTxDone

bool bTxDone = false

Signaling flag for Tx on Uart0

Definition at line 99 of file main.c.

◆ count_10ms

volatile uint8_t count_10ms = 0

Counter managed within 1ms slot

Definition at line 80 of file main.c.

◆ Counter1KHz

volatile uint8_t Counter1KHz

Counter to managem 1Khz execution rate at Dma event handler Dma control variables

Definition at line 82 of file main.c.

◆ DmaEnableRequest

volatile uint8_t DmaEnableRequest = 1

This will sync the reenabling of dma interrupt Timer and pwm control pointers

Definition at line 85 of file main.c.

◆ pPwm0

void* pPwm0

Pwm device pointer for ADC trig

Definition at line 91 of file main.c.

◆ pPwm1

void* pPwm1

Pwm device pointer for pwm on U phase

Definition at line 92 of file main.c.

◆ pPwm2

void* pPwm2

Pwm device pointer for pwm on W phase

Definition at line 94 of file main.c.

◆ pPwm3

void* pPwm3

Pwm device pointer for pwm on V phase

Definition at line 93 of file main.c.

◆ pTim0

void* pTim0

TIM device pointer for pwm generation (pwm1:U / pwm2:V / pwm3:W)

Definition at line 88 of file main.c.

◆ pTim1

void* pTim1

TIM device pointer for uart

Definition at line 89 of file main.c.

◆ pTim2

void* pTim2

TIM device pointer for adc triggering using pwm 0

Definition at line 90 of file main.c.

◆ pUart0

void* pUart0

Uart0 config device pointer

Definition at line 95 of file main.c.

◆ pUart1

void* pUart1

Uart1 config device pointer

Definition at line 96 of file main.c.

◆ Saw_Up

volatile uint8_t Saw_Up

This is a flag used for edge selection sync up TIM2 with TIM0 timer

Definition at line 71 of file main.c.

◆ slot_10ms

volatile uint8_t slot_10ms = false

Flag signaling 10ms time elapsed withing 1ms slot

Definition at line 81 of file main.c.

◆ SPIData

uint32_t SPIData[5]

SPI array carrying the debug variables Timer control variables

Definition at line 75 of file main.c.

◆ Sync_Timer02

volatile uint8_t Sync_Timer02

This is a flag used to sync up TIM2 with TIM0 timer

Definition at line 70 of file main.c.

◆ TestBldcOff

unsigned char TestBldcOff

Flag to command motor stop - 1 will stop

Definition at line 109 of file main.c.

◆ TestBldcOn

unsigned char TestBldcOn = 0

Flag to command motor run - 1 will run

Definition at line 108 of file main.c.

◆ TestBldcRamp

volatile int32_t TestBldcRamp = 50

Motor ramp target - rpm/s

Definition at line 105 of file main.c.

◆ TestBldcSpd

volatile int32_t TestBldcSpd = 350

Motor speed target - rpm

Definition at line 104 of file main.c.

◆ TestBldcUpdtSpdReq

volatile uint8_t TestBldcUpdtSpdReq = 0

Flag for motor speed update - 1 will update

Definition at line 107 of file main.c.

◆ TestBldcUpdtSpdTimer

volatile uint16_t TestBldcUpdtSpdTimer = 1000

Motor speed update request elapse timer - ms

Definition at line 106 of file main.c.

◆ TIM0_PER

volatile uint32_t TIM0_PER

This carries the number of clock events within a pwm period
Eq is (SR_MTU_FREQUENCY / (2*pwm_frequency*(1<<(PWM_TIM_DIV-1))))
(40000000 / (2*16000*(1<<(PWM_TIM_DIV-1)))) function to call: hw_tim_write_per_reg(TIM0, value); Timers sync up variables

Definition at line 64 of file main.c.

◆ TimerOut1s

volatile unsigned short TimerOut1s = 100

Counter that runs at 10ms time slot - for Leds management

Definition at line 78 of file main.c.

◆ TimerOutFlashOperation

volatile uint8_t TimerOutFlashOperation = 0

Counter to manage flash operation

Definition at line 79 of file main.c.