Skip to content

Commit

Permalink
Finish LaserPulsGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
gaerber committed May 20, 2014
2 parents ae5fc96 + 1048317 commit a32515c
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 27 deletions.
46 changes: 35 additions & 11 deletions Libraries/BSP/inc/bsp.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* \file bsp.h
* \brief Board support package.
* \date 2014-04-15
* \version 0.3
* \file bsp.h
* \brief Board support package.
* \date 2014-05-20
* \version 0.4
* \author Kevin Gerber
*
* \addtogroup bsp
* \addtogroup bsp
* \brief The board support package contains all medium layer functionality
* to use the hardware module. The BSP is based on the standard peripheral
* library from ST.
Expand All @@ -17,13 +17,12 @@

#include "stm32f4xx.h"

/**
* \brief Assert makro.
* \param cond Conditions, which must be true.
* \todo Define an assert function.
*/
#define assert(cond) //((cond)?(0): (fprintf (stderr, "assertion failed: \ %s, file %s, line %d \n",#cond,__FILE__,__LINE__), abort()))

/*
* ----------------------------------------------------------------------------
* Data types
* ----------------------------------------------------------------------------
*/

/**
* \brief Structure of all important hardware configuration of each GPIO.
Expand All @@ -38,6 +37,31 @@ typedef struct {
} bsp_gpioconf_t;


/**
* \brief All available PWM controller and channel.
*/
typedef enum {
CHANEL1, /*!< PWM Channel 1 */
CHANEL2, /*!< PWM Channel 2 */
CHANEL3, /*!< PWM Channel 3 */
CHANEL4 /*!< PWM Channel 4 */
} bsp_pwmport_t;


/*
* ----------------------------------------------------------------------------
* Macros
* ----------------------------------------------------------------------------
*/

/**
* \brief Assert makro.
* \param cond Conditions, which must be true.
* \todo Define an assert function.
*/
#define assert(cond) //((cond)?(0): (fprintf (stderr, "assertion failed: \ %s, file %s, line %d \n",#cond,__FILE__,__LINE__), abort()))


/**
* \brief Get the pin source from the pin number.
* \author Tobias Rueetschi
Expand Down
78 changes: 78 additions & 0 deletions Libraries/BSP/inc/bsp_laser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* \file bsp_laser.h
* \brief Supports the laser pulse generator.
* \date 2014-05-20
* \version 0.1
* \author Kevin Gerber
*
* \addtogroup bsp
* @{
*
* \addtogroup bsp_laser
* \brief This Module provides the functions to configure the laser pulse generator.
* @{
*/

#ifndef BSP_LASER_H_
#define BSP_LASER_H_

#include "bsp.h"


/*
* ----------------------------------------------------------------------------
* Pulse settings
* ----------------------------------------------------------------------------
*/

/** PWM frequency of the laser pulse generator. */
#define BSP_LASER_FREQ 84000000
/** Period register of the PWM. The frequency of the laser pulse replay is f = BSP_LASER_FREQ[Hz] / (BSP_LASER_PERIOD-1) */
#define BSP_LASER_PERIOD 1681
/** Laser pulse width. The duty cycle is D = BSP_LASER_PULSE_WIDTH / (BSP_LASER_PERIOD-1) */
#define BSP_LASER_PULSE_WIDTH 3


/*
* ----------------------------------------------------------------------------
* Hardware configurations
* ----------------------------------------------------------------------------
*/

/** RCC AHB peripheral of the timer port. */
#define BSP_LASER_TIMER_PORT_PERIPH RCC_APB1Periph_TIM5
/** Port base address of the timer port */
#define BSP_LASER_TIMER_PORT_BASE TIM5
/** Used PWM type */
#define BSP_LASER_TIMER_PORT_CHANEL CHANEL1

/** Hardware label of the PWM output pin, which is connected to the engine. */
static const bsp_gpioconf_t BSP_LASER_PORT = {
RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_0, GPIO_Mode_AF, GPIO_PuPd_NOPULL, GPIO_AF_TIM5
};

/* Interrupt settings */
#define BSP_LASER_IRQ_CHANEL TIM5_IRQn /*!< NVIC timer interrupt */
#define BSP_LASER_IRQ_SOURCE TIM_IT_CC1 /*!< NVIC timer interrupt source */
#define BSP_LASER_IRQ_PRIORITY 2 /*!< NVIC timer interrupt priority */
#define BSP_LASER_IRQ_Handler TIM5_IRQHandler /*!< NVIC timer handler */


/*
* ----------------------------------------------------------------------------
* Prototypes
* ----------------------------------------------------------------------------
*/
extern void bsp_LaserInit(void);
extern void bsp_LaserPulse(uint32_t nr_of_pulses);


#endif /* BSP_LASER_H_ */

/**
* @}
*/

/**
* @}
*/
4 changes: 2 additions & 2 deletions Libraries/BSP/inc/bsp_led.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
typedef enum {
BSP_LED_GREEN = 0, /*!< The green LED identification number. */
BSP_LED_OUT_0, /*!< Carme IO2 OUT0 LED */
//BSP_LED_OUT_0, /*!< Carme IO2 OUT0 LED */
BSP_LED_OUT_1, /*!< Carme IO2 OUT1 LED */
BSP_LED_OUT_2, /*!< Carme IO2 OUT2 LED */
BSP_LED_OUT_3, /*!< Carme IO2 OUT3 LED */
Expand All @@ -45,7 +45,7 @@ typedef enum {
*/
static const bsp_gpioconf_t BSP_LED_PORTS[] = {
{RCC_AHB1Periph_GPIOI, GPIOI, GPIO_Pin_6, GPIO_Mode_OUT, GPIO_PuPd_NOPULL}, /* BSP_LED_GREEN */
{RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_0, GPIO_Mode_OUT, GPIO_PuPd_NOPULL}, /* BSP_LED_OUT_0 */
//{RCC_AHB1Periph_GPIOA, GPIOA, GPIO_Pin_0, GPIO_Mode_OUT, GPIO_PuPd_NOPULL}, /* BSP_LED_OUT_0 */
{RCC_AHB1Periph_GPIOH, GPIOH, GPIO_Pin_11, GPIO_Mode_OUT, GPIO_PuPd_NOPULL}, /* BSP_LED_OUT_1 */
{RCC_AHB1Periph_GPIOH, GPIOH, GPIO_Pin_12, GPIO_Mode_OUT, GPIO_PuPd_NOPULL}, /* BSP_LED_OUT_2 */
{RCC_AHB1Periph_GPIOB, GPIOB, GPIO_Pin_8, GPIO_Mode_OUT, GPIO_PuPd_NOPULL} /* BSP_LED_OUT_3 */
Expand Down
10 changes: 5 additions & 5 deletions Libraries/BSP/src/bsp.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* \file bsp.c
* \brief Board support package main functions.
* \date 2014-03-18
* \version 0.2
* \file bsp.c
* \brief Board support package main functions.
* \date 2014-03-18
* \version 0.2
* \author Kevin Gerber
*
* \addtogroup bsp
* \addtogroup bsp
* @{
*/

Expand Down
179 changes: 179 additions & 0 deletions Libraries/BSP/src/bsp_laser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/**
* \file bsp_laser.c
* \brief Supports the laser pulse generator.
* \date 2014-05-20
* \version 0.1
* \author Kevin Gerber
*
* \addtogroup bsp
* @{
*
* \addtogroup bsp_laser
* @{
*/

#include "bsp_laser.h"


/*
* ----------------------------------------------------------------------------
* Private functions prototypes
* ----------------------------------------------------------------------------
*/
void bsp_LaserEnable(void);
void bsp_LaserDisable(void);


/*
* ----------------------------------------------------------------------------
* Private variables
* ----------------------------------------------------------------------------
*/

/**
* \brief Counter of the remaining laser pulses.
*/
static uint32_t g_laserPulseCtr = 0;


/*
* ----------------------------------------------------------------------------
* Interrupt functions
* ----------------------------------------------------------------------------
*/

/**
* \brief Timer interrupt handler. It is triggered by the capture compare register.
*/
void BSP_LASER_IRQ_Handler(void) {
if (TIM_GetITStatus(BSP_LASER_TIMER_PORT_BASE, BSP_LASER_IRQ_SOURCE) != RESET) {
TIM_ClearITPendingBit(BSP_LASER_TIMER_PORT_BASE, BSP_LASER_IRQ_SOURCE);
/* One pulse generated */
g_laserPulseCtr--;
if (g_laserPulseCtr == 0) {
/* All laser pulse were made */
bsp_LaserDisable();
}
}
}


/*
* ----------------------------------------------------------------------------
* Implementation
* ----------------------------------------------------------------------------
*/

/**
* \brief Initialize the laser pulse generator. It is used one PWM channel to make the pulse.
*/
void bsp_LaserInit(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

uint16_t PrescalerValue;

/* Initialize all GPIOs in their function */
bsp_GpioInit(&BSP_LASER_PORT);

/* TIM clock enable */
RCC_APB1PeriphClockCmd(BSP_LASER_TIMER_PORT_PERIPH, ENABLE);

/* Compute the prescaler value */
PrescalerValue = (uint16_t) ((SystemCoreClock /2) / BSP_LASER_FREQ) - 1;

/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = BSP_LASER_PERIOD;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(BSP_LASER_TIMER_PORT_BASE, &TIM_TimeBaseStructure);

/* PWM Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;//BSP_LASER_PULSE_WIDTH;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* Check the PWM port */
switch (BSP_LASER_TIMER_PORT_CHANEL) {
case CHANEL1:
/* PWM Mode configuration: Channel1 */
TIM_OC1Init(BSP_LASER_TIMER_PORT_BASE, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(BSP_LASER_TIMER_PORT_BASE, TIM_OCPreload_Enable);
break;

case CHANEL2:
/* PWM Mode configuration: Channel2 */
TIM_OC2Init(BSP_LASER_TIMER_PORT_BASE, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(BSP_LASER_TIMER_PORT_BASE, TIM_OCPreload_Enable);
break;

case CHANEL3:
/* PWM Mode configuration: Channel3 */
TIM_OC3Init(BSP_LASER_TIMER_PORT_BASE, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(BSP_LASER_TIMER_PORT_BASE, TIM_OCPreload_Enable);
break;

case CHANEL4:
/* PWM Mode configuration: Channel4 */
TIM_OC4Init(BSP_LASER_TIMER_PORT_BASE, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(BSP_LASER_TIMER_PORT_BASE, TIM_OCPreload_Enable);
break;

default:
assert(BSP_LASER_TIMER_PORT_CHANEL);
break;
}

/* Enables TIMx peripheral preload register on ARR */
TIM_ARRPreloadConfig(BSP_LASER_TIMER_PORT_BASE, ENABLE);

/* Enable timer interrupt on capture compare */
TIM_ITConfig(BSP_LASER_TIMER_PORT_BASE, BSP_LASER_IRQ_SOURCE, ENABLE);

NVIC_InitStructure.NVIC_IRQChannel = BSP_LASER_IRQ_CHANEL;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = BSP_LASER_IRQ_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

/* Enable the timer */
TIM_Cmd(BSP_LASER_TIMER_PORT_BASE, ENABLE);
}

/**
* \brief Generates a number of pulses in the configured repetition frequency and pulse width.
* \param[in] nr_of_pulses is the number of pulse repetition.
*/
void bsp_LaserPulse(uint32_t nr_of_pulses) {
g_laserPulseCtr = nr_of_pulses;
bsp_LaserEnable();
}

/**
* \brief Enable the engine. It will turn with the configured speed.
*/
void bsp_LaserEnable(void) {
/* Starts a new pulse */
TIM_SetCounter(BSP_LASER_TIMER_PORT_BASE, BSP_LASER_PERIOD-20);
/* Sets pulse width */
TIM_SetCompare1(BSP_LASER_TIMER_PORT_BASE, BSP_LASER_PULSE_WIDTH);
}

/**
* \brief Disable the engine. It will be stopped.
*/
void bsp_LaserDisable(void) {
/* Set output to low */
TIM_SetCompare1(BSP_LASER_TIMER_PORT_BASE, 0);
}


/**
* @}
*/

/**
* @}
*/
12 changes: 3 additions & 9 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <stdint.h>
#include <string.h>

#include "bsp_led.h"
#include "bsp_laser.h"


/**
Expand All @@ -45,20 +45,14 @@ int main(void) {
/* Ensure all priority bits are assigned as preemption priority bits. */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

bsp_LedInit();
bsp_LaserInit();

/* Enable IRQ */
__enable_irq();

/* Infinite loop */
while (1) {
bsp_LedSetToggle(BSP_LED_OUT_0);
bsp_LedSetToggle(BSP_LED_OUT_1);
bsp_LedSetToggle(BSP_LED_OUT_2);
bsp_LedSetToggle(BSP_LED_OUT_3);
bsp_LedSetOn(BSP_LED_GREEN);
delay();
bsp_LedSetOff(BSP_LED_GREEN);
bsp_LaserPulse(10);
delay();
}

Expand Down

0 comments on commit a32515c

Please sign in to comment.