When programming any MCU, no matter Arduino IDE, Keil, Atmel studio or other compilers and assemblers, many times we got intro problem not knowing what happening on our data transfer from and to MCU. For example, we want to use I2C protocol for communicating with some sensor, but it does not respond. Many things can be wrong, and without logic analyzer, we just don’t know whether our code is wrong, the sensor is broken, or maybe something else.
Cheap Chinese clone or professional one?
Good question, depend of many factors. Do you use it for your work, or do you program for fun? I am using for my personal projects, and this one that cost less than $50 US.:
It works fine, but there is limit in speed, especially with more than one channel. It is USB 2.0 product and has no analog part as it has original Saleae Logic analyzer. It is too much expensive for my pocket, especially because I am not professional. I just have dream that it will be mine one day, but until then… Anyway, here is video for anyone who are interested and does not know what I am talking about:
Recently I had problem programming STM32 and connecting OLED display. Despite spending hours googling what exact command should be implemented, and reading their documentation, I was not able to get picture on the OLED display. Then got idea – programmed Atmega 328p with Arduino example, then put logic analyzer to I2C bus, and got the codes for initialization of the display.
Another example is when I wanted to know toy grade quadcopters, how transmitter and receiver know where to ‘jump’ by using ‘frequency hopping’. Using SPI protocol and Logic analyzer, I was able to see exactly what is ‘the secret’. Aside many bytes that transmitter sending for moving flying device up/down, left and right, there is one byte that constantly changes even when remote sticks are still – that is information for next channel. So, TX sending information on current channel, then last bit is channel number for next transmission. Whole protocol is made so that if it goes briefly out of range, or missing one packet due to noise, receiver back to original ‘calling’ channel, and in the same time transmitter sending on this channel periodically next possible channel where will be next time. That is so cool to know.
Yes, DSP (Digital Signal Processing) is possible with some speed limitations. For example, if FIR filter (Finite Impulse Response) has too much taps, whole loop process will be slow, and sampling ratio depends strongly of number of those elements. Out there exist specialized MCUs with additional hardware for floating point calculation (FPU), but our STM 32 or whatever MCU you are using, can do DSP.
First, we need to find some math to calculate ‘taps’, you may use your GNU radio companion for that, or some free online calculators as is this one (really simple): http://t-filter.engineerjs.com/
Just set parameter of wanted frequency filtering, and on the right side you have two choices: plain text or C/C++ code. Chose code and copy/paste into my codes in ‘coefficients.h’ file, replacing old ones (or just comment old one with ‘//’. Change “static double filter_taps[FILTER_TAP_NUM] = {…” into “static const float taps[] = {….”, that is because it is intended for PC and other high frequency CPUs instead our MCUs. Name in ‘[]’ square brackets is defined above by “#define FILTER_TAP_NUM”, so leave those brackets empty, else compiler may complain about re-definition.
#include "stm32f10x.h"
#include "delayUs.h"
#include "adc.h"
void ADCenable(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN | RCC_APB2ENR_ADC1EN | RCC_APB2ENR_AFIOEN; //enabling ADC clock, interrupt enable,
RCC->CFGR |= RCC_CFGR_ADCPRE_DIV4;// ADC clock = 12 MHz, maximum is 14, but there is no divider for that freq (72MHz / 6 = 12MHz). works with div6 too
//while clock for port A and B is enabled down below
GPIOB->CRL &= ~(GPIO_CRL_CNF0_0|GPIO_CRL_CNF0_1|GPIO_CRL_MODE0_0|GPIO_CRL_MODE0_1); // pin A0 is analog input
//ADC1->CR1 |=ADC_CR1_EOCIE; //ADC interrupt enabled
//NVIC_EnableIRQ(ADC1_2_IRQn); //interrupt enabled
//ADC1->SMPR2 |= ADC_SMPR2_SMP8_0;//|ADC_SMPR2_SMP8_1|ADC_SMPR2_SMP8_2;
ADC1->SQR3 |= ADC_SQR3_SQ1_3; //for B0 in sequence 1, channel 8, it is 0b1000 = 8 (IN8)
//ADC1->SQR3 |=8; // alternative way of setting the same thing as above
ADC1->CR2 &= ~ADC_CR2_ALIGN; //data is right aligned (0bxxxx111111111111)
ADC1->CR2 |= ADC_CR2_ADON | ADC_CR2_CONT; //ADC converter is on
delay(1000); //alow ADC to stabilize - 1 mS, but my delay is not exactly 1 mS, it is much shorter...
ADC1->CR2 |= ADC_CR2_CAL;
delay(1000); //it is better to leave some time, just few clock cycles...
ADC1->CR2 |= ADC_CR2_ADON; //not sure it requires to call it again?
delay(1000); //After first ADON, ADC is just set, then second time ADC is actually enabled
}
/* For some unknown reason, DSP does not work if algorithm is included into IRQ handler, so NVIC is disabled for this IRQ */
void ADC1_2_IRQHandler(void)
{
if (ADC1->SR & ADC_SR_EOC)
{
adc_value=ADC1->DR;
}
}
int adc(void)
{
int adc=0;
ADC1->CR2 |=ADC_CR2_SWSTART;
//if(ADC1->SR & ADC_SR_EOC)
while(!(ADC1->SR & ADC_SR_EOC));
adc=ADC1->DR;
return adc;
}
#ifndef adc_h
#define adc_h
extern int adc_value;
void ADCenable(void);
int adc(void);
#endif
#include "stm32f10x.h"
void timer2enablePWM(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_AFIOEN; //port B clock enabled (3), port A clock enable (2), Alternate IO clock enable (0)
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; //timer 2 clock enable (2)
GPIOA->CRL |= GPIO_CRL_CNF1_1|GPIO_CRL_MODE1_0|GPIO_CRL_MODE1_1;
GPIOA->CRL &= ~(GPIO_CRL_CNF1_0);
TIM2->CCER |= TIM_CCER_CC2E; //capture/compare timer2 output enable
TIM2->CR1 |= TIM_CR1_ARPE; //auto reload preload enable TIMxARR is buffered
TIM2->CCMR1 |= TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2PE; //output compare 2 mode 0b110 (14:12)?, output compare 2 preload enable
//PWM freq = Fclk/PSC/ARR 72MHz/1000
//PWM Duty = CCR1/ARR
TIM2->PSC = 0; //prescaler value, 72 MHz divided by:
TIM2->ARR = 1024; //auto reload register, value of 1024 with prescaler value 0 result in PWM frequency of 70 kHz
//TIM2->CCR2= 512; //capture/compare value, duty cycle (disabled here, enabling in call function in DSP_1.c)
TIM2->EGR |= TIM_EGR_UG; //update generation, re-initialize
TIM2->CR1 |= TIM_CR1_CEN; //counter enabled
}
void dac(int value)
{
TIM2->CCR2 = value; //here we update capture/compare value for duty cycle
}