Skip to content

Commit

Permalink
-Changes to Analog Out/DAC command interface
Browse files Browse the repository at this point in the history
-Added some more comments
  • Loading branch information
NotBlackMagic committed Jun 25, 2020
1 parent 3a091f1 commit ae07e0d
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 82 deletions.
1 change: 1 addition & 0 deletions include/HAL/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void TIM4Init();
void TIM6Init();
void TIM3SetFreq(uint32_t freq);
void TIM4SetFreq(uint32_t freq);
void TIM6SetFreq(uint32_t freq);

#ifdef __cplusplus
}
Expand Down
7 changes: 4 additions & 3 deletions include/analogOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ typedef struct {

typedef struct {
uint8_t channel;
AnalogOutCHConfigStruct config;
uint16_t buffer[20];
uint32_t frequency;
uint16_t buffer[512];
uint16_t bufferLength;
} AnalogOutCHStruct;

void AnalogOutInit();
void AnalogOutConfigChannel(uint8_t anBlock, uint8_t channel, AnalogOutCHConfigStruct config);
void AnalogOutConfigChannel(uint8_t anBlock, uint8_t channel, AnalogOutCHStruct config);
void AnalogOutHandler(uint8_t anBlock);

#ifdef __cplusplus
Expand Down
18 changes: 10 additions & 8 deletions src/Commands/commandInterpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ uint8_t CommandInterpreter(uint8_t* data, uint16_t dataLength) {
break;
}
case OPCODE_SET_ANALOG_OUT_A_CH: {
AnalogOutCHConfigStruct config;
AnalogOutCHStruct config;

uint8_t channel = data[COMMAND_PAYLOAD_OFFSET+0];
config.mode = data[COMMAND_PAYLOAD_OFFSET+1];
config.offset = (data[COMMAND_PAYLOAD_OFFSET+2] << 8) + data[COMMAND_PAYLOAD_OFFSET+3];
config.frequency = (data[COMMAND_PAYLOAD_OFFSET+4] << 16) + (data[COMMAND_PAYLOAD_OFFSET+5] << 8) + data[COMMAND_PAYLOAD_OFFSET+6];
config.amplitude = (data[COMMAND_PAYLOAD_OFFSET+7] << 8) + data[COMMAND_PAYLOAD_OFFSET+8];
config.dtc = data[COMMAND_PAYLOAD_OFFSET+9];
config.channel = data[COMMAND_PAYLOAD_OFFSET+0];
config.frequency = (data[COMMAND_PAYLOAD_OFFSET+1] << 16) + (data[COMMAND_PAYLOAD_OFFSET+2] << 8) + data[COMMAND_PAYLOAD_OFFSET+3];
config.bufferLength = (data[COMMAND_PAYLOAD_OFFSET+4] << 8) + data[COMMAND_PAYLOAD_OFFSET+5];

uint16_t i;
for(i = 0; i < config.bufferLength; i++) {
config.buffer[i] = (data[(COMMAND_PAYLOAD_OFFSET+6) + (i*2)] << 8) + data[(COMMAND_PAYLOAD_OFFSET+6) + (i*2 + 1)];
}

AnalogOutConfigChannel(ANALOG_OUT_BLOCK_A, channel, config);
AnalogOutConfigChannel(ANALOG_OUT_BLOCK_A, config.channel, config);
break;
}
default:
Expand Down
43 changes: 35 additions & 8 deletions src/HAL/timer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "timer.h"

/**
* @brief This function initializes the TIM3 and respective GPIO
* @param None
* @return None
*/
void TIM3Init() {
//Enable bus clocks
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
Expand Down Expand Up @@ -35,6 +40,11 @@ void TIM3Init() {
LL_TIM_GenerateEvent_UPDATE(TIM3);
}

/**
* @brief This function initializes the TIM4 and respective GPIO
* @param None
* @return None
*/
void TIM4Init() {
//Enable bus clocks
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
Expand Down Expand Up @@ -70,14 +80,20 @@ void TIM4Init() {
LL_TIM_GenerateEvent_UPDATE(TIM4);
}

/**
* @brief This function initializes the TIM6
* @param None
* @return None
*/
void TIM6Init() {
//Enable bus clocks
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM6);

//Configure the Timer
// LL_TIM_SetCounterMode(TIM6, LL_TIM_COUNTERMODE_UP);
LL_TIM_SetPrescaler(TIM6, __LL_TIM_CALC_PSC(SystemCoreClock, 10000000));
LL_TIM_SetAutoReload(TIM6, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM6), 2000000)); //Set output clock to 1MHz
LL_TIM_SetPrescaler(TIM6, 0); //Leave timer at full clock speed
volatile uint16_t arr = __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM6), 2000000); //Calculate ARR register value for clock to be 2MHz
LL_TIM_SetAutoReload(TIM6, arr); //Set ARR value
LL_TIM_DisableARRPreload(TIM6);
LL_TIM_SetTriggerOutput(TIM6, LL_TIM_TRGO_UPDATE);
LL_TIM_DisableMasterSlaveMode(TIM6);
Expand All @@ -91,20 +107,31 @@ void TIM6Init() {
LL_TIM_EnableCounter(TIM6);
}

/**
* @brief This function sets the output frequency of TIM3, sets the ARR and CCR values
* @param freq: Desired timer frequency on PWM output
* @return None
*/
void TIM3SetFreq(uint32_t freq) {
LL_TIM_SetAutoReload(TIM3, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM3), freq)); //Set output clock to freq
LL_TIM_OC_SetCompareCH1(TIM3, ( (LL_TIM_GetAutoReload(TIM3) + 1 ) / 2)); //Set Output duty-cycle to 50%
}

/**
* @brief This function sets the output frequency of TIM4, sets the ARR and CCR values
* @param freq: Desired timer frequency on PWM output
* @return None
*/
void TIM4SetFreq(uint32_t freq) {
LL_TIM_SetAutoReload(TIM4, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM4), freq)); //Set output clock to freq
LL_TIM_OC_SetCompareCH1(TIM4, ( (LL_TIM_GetAutoReload(TIM4) + 1 ) / 2)); //Set Output duty-cycle to 50%
}

void TIM6_DAC_IRQHandler(void) {
// if(LL_TIM_IsActiveFlag_UPDATE(TIM6) == 0x01) {
// AnalogOutHandler(ANALOG_IN_BLOCK_A);
//
// LL_TIM_ClearFlag_UPDATE(TIM6);
// }
/**
* @brief This function sets the output frequency of TIM6, sets the ARR value
* @param freq: Desired timer frequency of TIM update calls
* @return None
*/
void TIM6SetFreq(uint32_t freq) {
LL_TIM_SetAutoReload(TIM6, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM6), freq)); //Set output clock to freq
}
11 changes: 7 additions & 4 deletions src/analogIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ uint8_t analogInBChSeqIndex;
uint8_t analogInBCHSequencer[ANALOG_IN_SEQUENCER_LENGTH];

/**
* @brief This function initializes the Analog IN Channels
* @brief This function initializes the Analog IN Channels:
* - Initialize structures
* - Initialize ADA4254 Amplifier
* - Calibrate ADC offset
* @param None
* @return None
*/
Expand Down Expand Up @@ -56,7 +59,7 @@ void AnalogInInit() {
}

/**
* @brief This function calibrates the Analog In Block
* @brief This function calibrates the Analog In Block, the ADC offset
* @param anBlocks: Which analog block, A or B
* @return None
*/
Expand Down Expand Up @@ -89,9 +92,9 @@ void AnalogInCalibration(uint8_t anBlock) {


/**
* @brief This function sets an Analog IN Channel
* @brief This function sets an Analog IN Block
* @param anBlock: Which analog block, A or B
* @param config: The set configuration for the analog block, AnalogInConfigStruct
* @param config: The configuration for the analog IN block, AnalogInConfigStruct
* @return None
*/
void AnalogInConfig(uint8_t anBlock, AnalogInConfigStruct config) {
Expand Down
95 changes: 36 additions & 59 deletions src/analogOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
AnalogOutCHStruct analogOutAChannels[2];
AnalogOutCHStruct analogOutBChannels[2];

/**
* @brief This function initializes the Analog OUT Channel:
* - Initialize structures
* - Initializes DAC DMA Channels
* @param None
* @return None
*/
void AnalogOutInit() {
//Enable bus clocks
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMAMUX1);
Expand Down Expand Up @@ -42,71 +49,41 @@ void AnalogOutInit() {
// NVIC_EnableIRQ(DMA1_Channel3_IRQn);

//{2048, 2680, 3252, 3705, 3996, 4095, 3996, 3705, 3252, 2680, 2048, 1415, 844, 391, 100, 0, 100, 391, 844, 1415}
analogOutAChannels[0].buffer[0] = 2048;
analogOutAChannels[0].buffer[1] = 2680;
analogOutAChannels[0].buffer[2] = 3252;
analogOutAChannels[0].buffer[3] = 3705;
analogOutAChannels[0].buffer[4] = 3996;
analogOutAChannels[0].buffer[5] = 4095;
analogOutAChannels[0].buffer[6] = 3996;
analogOutAChannels[0].buffer[7] = 3705;
analogOutAChannels[0].buffer[8] = 3252;
analogOutAChannels[0].buffer[9] = 2680;
analogOutAChannels[0].buffer[10] = 2048;
analogOutAChannels[0].buffer[11] = 1415;
analogOutAChannels[0].buffer[12] = 844;
analogOutAChannels[0].buffer[13] = 391;
analogOutAChannels[0].buffer[14] = 100;
analogOutAChannels[0].buffer[15] = 0;
analogOutAChannels[0].buffer[16] = 100;
analogOutAChannels[0].buffer[17] = 391;
analogOutAChannels[0].buffer[18] = 844;
analogOutAChannels[0].buffer[19] = 1415;

analogOutAChannels[1].buffer[0] = 2048;
analogOutAChannels[1].buffer[1] = 2680;
analogOutAChannels[1].buffer[2] = 3252;
analogOutAChannels[1].buffer[3] = 3705;
analogOutAChannels[1].buffer[4] = 3996;
analogOutAChannels[1].buffer[5] = 4095;
analogOutAChannels[1].buffer[6] = 3996;
analogOutAChannels[1].buffer[7] = 3705;
analogOutAChannels[1].buffer[8] = 3252;
analogOutAChannels[1].buffer[9] = 2680;
analogOutAChannels[1].buffer[10] = 2048;
analogOutAChannels[1].buffer[11] = 1415;
analogOutAChannels[1].buffer[12] = 844;
analogOutAChannels[1].buffer[13] = 391;
analogOutAChannels[1].buffer[14] = 100;
analogOutAChannels[1].buffer[15] = 0;
analogOutAChannels[1].buffer[16] = 100;
analogOutAChannels[1].buffer[17] = 391;
analogOutAChannels[1].buffer[18] = 844;
analogOutAChannels[1].buffer[19] = 1415;
uint16_t i;
for(i = 0; i < 512; i++) {
analogOutAChannels[0].buffer[i] = 0;
analogOutAChannels[1].buffer[i] = 0;
}
}

void AnalogOutConfigChannel(uint8_t anBlock, uint8_t channel, AnalogOutCHConfigStruct config) {
/**
* @brief This function sets an Analog OUT Channel
* @param anBlock: Which analog block, A or B
* @param channel: Which channel to configure
* @param config: The configuration for the analog OUT channel, AnalogOutCHStruct
* @return None
*/
void AnalogOutConfigChannel(uint8_t anBlock, uint8_t channel, AnalogOutCHStruct config) {
if(anBlock == ANALOG_OUT_BLOCK_A) {
analogOutAChannels[channel - 1].config = config;
analogOutAChannels[channel - 1] = config;

if(channel == 0x01) {
//Change Timer settings, DAC update rate
TIM6SetFreq(analogOutAChannels[channel - 1].frequency);

if(analogOutAChannels[channel - 1].config.mode == Mode_DC) {
DAC1Write(channel, analogOutAChannels[channel - 1].config.offset);
//Change DMA Settings, buffer length
LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_1);
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_1, analogOutAChannels[channel - 1].bufferLength);
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
}
else if(channel == 0x02) {
//Change DMA Settings, buffer length
LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_2);
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_2, analogOutAChannels[channel - 1].bufferLength);
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_2);
}
}
else if(anBlock == ANALOG_OUT_BLOCK_B) {
analogOutBChannels[channel - 1].config = config;
}
}

uint16_t value = 0;
void AnalogOutHandler(uint8_t anBlock) {
if(anBlock == ANALOG_IN_BLOCK_A) {
LL_DAC_ConvertData12RightAligned(DAC1, LL_DAC_CHANNEL_1, value);
LL_DAC_ConvertData12RightAligned(DAC1, LL_DAC_CHANNEL_2, value);

value += 128;
if(value > 4095) {
value = 0;
}
analogOutBChannels[channel - 1] = config;
}
}

0 comments on commit ae07e0d

Please sign in to comment.