QEMU PCI Device fires forever - interrupt

I've written a small PCI device in QEMU using https://github.com/qemu/qemu/blob/v2.7.0/hw/misc/edu.c as a base, and https://github.com/cirosantilli/linux-kernel-module-cheat/blob/6788a577c394a2fc512d8f3df0806d84dc09f355/kernel_module/pci.c to interact with it. However, interrupts appear to fire roughly ever 2 seconds.
I write data to the PCI device to acknowledge the interrupt, and I can see that before and after the call to:
if (!device->irq_status && !device_msi_enabled(device)) {
printf("Set state from %d ", device->pdev.irq_state);
pci_set_irq(&device->pdev, 0);
printf("to %d\n", device->pdev.irq_state);
}
The output is
Set state from 0 to 0
Set state from 0 to 0
Set state from 0 to 0
Set state from 0 to 0
I can't tell what could cause the interrupt to continue firing - is there a property I've forgotten?

Related

STM32 USB Tx Busy

I have an application running on STM32F429ZIT6 using USB stack to communicate with PC client.
MCU receives one type of message of 686 bytes every second and receives another type of message of 14 bytes afterwards with 0.5 seconds of delay between messages. The 14 bytes message is a heartbeat so it needs to replied by MCU.
It happens that after 5 to 10 minutes of continuous operation, MCU is not able to send data because
hcdc->TxState is always busy. Reception works fine.
During Rx interruption, application only adds data to ring buffer, so that this buffer is later serialized and processed by main function.
static int8_t CDC_Receive_HS(uint8_t* Buf, uint32_t *Len) {
/* USER CODE BEGIN 11 */
/* Message RX Completed, Send it to Ring Buffer to be processed at FMC_Run()*/
for(uint16_t i = 0; i < *Len; i++){
ring_push(RMP_RXRingBuffer, (uint8_t *) &Buf[i]);
}
USBD_CDC_SetRxBuffer(&hUsbDeviceHS, &Buf[0]);
USBD_CDC_ReceivePacket(&hUsbDeviceHS);
return (USBD_OK);
/* USER CODE END 11 */ }
USB TX is also kept as simple as possible:
uint8_t CDC_Transmit_HS(uint8_t\* Buf, uint16_t Len) {
uint8_t result = USBD_OK;
/\* USER CODE BEGIN 12 */
USBD_CDC_HandleTypeDef hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceHS.pClassData;
if (hcdc-\>TxState != 0)
{
ZF_LOGE("Tx failed, resource busy\\n\\r"); return USBD_BUSY;
}
USBD_CDC_SetTxBuffer(&hUsbDeviceHS, Buf, Len);
result = USBD_CDC_TransmitPacket(&hUsbDeviceHS);
ZF_LOGD("TX Message Result:%d\\n\\r", result);
/ USER CODE END 12 \*/
return result;
}
I'm using latest HAL Drivers and software from CubeIDE (1.27.1).
I have tried expanding heap min size from 0x200 to larger values but result is the same.
Also Line Coding is set according to what recommended values:
case CDC_SET_LINE_CODING:
LineCoding.bitrate = (uint32_t) (pbuf[0] | (pbuf[1] << 8) | (pbuf[2] << 16) | (pbuf[3] << 24));
LineCoding.format = pbuf[4];
LineCoding.paritytype = pbuf[5];
LineCoding.datatype = pbuf[6];
ZF_LOGD("Line Coding Set\n\r");
break;
case CDC_GET_LINE_CODING:
pbuf[0] = (uint8_t) (LineCoding.bitrate);
pbuf[1] = (uint8_t) (LineCoding.bitrate >> 8);
pbuf[2] = (uint8_t) (LineCoding.bitrate >> 16);
pbuf[3] = (uint8_t) (LineCoding.bitrate >> 24);
pbuf[4] = LineCoding.format;
pbuf[5] = LineCoding.paritytype;
pbuf[6] = LineCoding.datatype;
ZF_LOGD("Line Coding Get\n\r");
break;
Thanks in advance, any support is appreciated.
I don't know enough about the STM32 libraries to really check your code, but I suspect you are forgetting to read the bytes transmitted by the STM32 on PC side. Try opening a terminal program like PuTTY and connecting to the STM32's virtual serial port. Otherwise, the Windows USB-to-serial driver (usbser.sys) will eventually have its buffers filled with data from your device and it will stop requesting more, at which point the buffers on your device will fill up as well.

ATMEGA2561 WINC1500 Driver implementation SPI problem

I am trying to implement the WINC1500 MLA Driver to work with the ATMEGA2561 MCU and I have written my driver code and it's stuck on the line "while((SPSR & (1 << SPIF)) == 0);" in the m2mStub_SpiTxRx function.
I have no idea why it's not progressing through. I'm using the jumpstart ImageCraft IDE for this project.
Here's the implementation of it
void m2mStub_SpiTxRx(uint8_t *p_txBuf,
uint16_t txLen,
uint8_t *p_rxBuf,
uint16_t rxLen)
{
uint16_t byteCount;
uint16_t i;
// Calculate the number of clock cycles necessary, this implies a full-duplex SPI.
byteCount = (txLen >= rxLen) ? txLen : rxLen;
DEBUGOUTF("Calculate the number of clock cycles\n");
DEBUGOUTF("byteCount %d", byteCount, "\n");
DEBUGOUTF("txLen %d", txLen, "\n");
DEBUGOUTF("rxLen %d", rxLen, "\n");
// Read / Transmit.
for (i = 0; i < byteCount; ++i)
{
// Wait for transmitter to be ready. (This is causing the entire thing to crash)
while((SPSR & (1 << SPIF)) == 0);
// Transmit.
if (txLen > 0)
{
// Send data from the transmit buffer.
SPDR = (*p_txBuf++);
--txLen;
}
else
{
// No more Tx data to send, just send something to keep clock active.
SPDR = 0x00U;
}
// Wait for transfer to finish.
while((SPSR & (1 << SPIF)) == 0);
// Send dummy data to slave, so we can read something from it.
SPDR = 0x00U;
// Wait for transfer to finish.
while((SPSR & (1 << SPIF)) == 0);
// Read or throw away data from the slave as required.
if (rxLen > 0)
{
*p_rxBuf++ = SPDR;
--rxLen;
}
else
{
// Clear the registers
volatile uint8_t reg_clear = 0U;
reg_clear = SPDR;
(void)reg_clear;
}
}
}
I don't have enough information to say for sure, but my assumption is that your SPI connection is not set up correctly.
In particular, I guess you forgot to set /SS as output, same as this problem or this.
In the datasheet it says:
Master Mode When the SPI is configured as a master (MSTR in SPCR is
set), the user can determine the direction of the SS pin.
If SS is configured as an output, the pin is a general output pin
which does not affect the SPI system. Typically, the pin will be
driving the SS pin of the SPI slave.
If SS is configured as an input, it must be held high to ensure Master
SPI operation. If the SS pin is driven low by peripheral circuitry
when the SPI is configured as a master with the SS pin defined as an
input, the SPI system interprets this as another master selecting the
SPI as a slave and starting to send data to it. To avoid bus
contention, the SPI system takes the following actions:
The MSTR bit in SPCR is cleared and the SPI system becomes a slave. As a result of the SPI becoming a slave, the MOSI and SCK pins become
inputs.
The SPIF flag in SPSR is set, and if the SPI interrupt is enabled, and the I-bit in SREG is set, the interrupt routine will be executed.
Thus, when interrupt-driven SPI transmission is used in master mode,
and there exists a possibility that SS is driven low, the interrupt
should always check that the MSTR bit is still set. If the MSTR bit
has been cleared by a slave select, it must be set by the user to
re-enable SPI master mode.
So, you just need to configure the /SS pin as output and set to high in your init code, this should solve your problem:
DDRB |= (1 << PB0); // Set /SS (PB0) as output
PORTB |= (1 << PB0); // Set /SS (PB0) high

How Can I Establish UART Communication between 2 Stm32 and produce PWM signal

Edit: I solved UART communication problem but I have new problem getting pwm signal after receiving Transmit Data. I can blink led I can drive relay with transmitted data but I could not produce PWM signal.
maps(120, 1, 1, 250, RxData[4]);
ADC_Left = Yx; __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,ADC_Left);
I used __HAL_TIM_SET_COMPARE function but it doesnt work. I can observe ADC_Left’s value on Debug site but its not work.
I am trying to realize UART communication between 2 stm32. I know there are several topic related with but my question focused another one.
I am reading 2 adc value on stm32 which is only transmit these value and other one only receive these 2 adc value. To do this
MX_USART1_UART_Init();
__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); // Interrupt Enable
__HAL_UART_ENABLE_IT(&huart1, UART_IT_TC);
char TxData1[10];
..............
TxData1[0] = 0xEA;
TxData1[1] = wData.Byte_1;
TxData1[2] = wData.Byte_2;
TxData1[3] = wData.Byte_3;
TxData1[4] = wData.Right_Adc_Val;
TxData1[5] = wData.Left_Adc_Val;
TxData1[6] = wData.Byte_6;
for(uint8_t i = 1 ; i < 7; i++)
{
wData.Checksum = wData.Checksum + TxData1[i];
}
wData.Checksum_H = (wData.Checksum >> 8)&0xFF;
wData.Checksum_L = (wData.Checksum)&0xFF;
TxData1[7] = wData.Checksum_H;
TxData1[8] = wData.Checksum_L;
TxData1[9] = 0xAE;
HAL_UART_Transmit_IT(&huart1,(uint8_t*) &TxData1,10);
............
This block sent them I can observate them on Debug screen and using TTL module's Tx Rx pins.
MX_USART1_UART_Init();
__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); // Interrupt Enable
__HAL_UART_ENABLE_IT(&huart1, UART_IT_TC);
char RxData[10];
while(1){
HAL_UART_Receive_IT(&huart1,(uint8_t*) &RxData,10);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1)
{
HAL_UART_Receive_IT(&huart1,(uint8_t*) &RxData,10);
}
There is no problem up to here but when i getting RxData 0. index , it gives EA . Of course it should be give EA. When the adc data change all the ranking is changing. RxData[0] gives meaningless data. adc value is jumping over the all RxData array.
data locations must always be in the same index. How Can I get these data in stability for ex.
RxData[0]=EA
.
.
RxData[4]= should give adc value. so on.
..
Edit: I tried other mode of UART, DMA (in circular mode) and direct mode were used. I cant receive even 1 byte with DMA .
In your example code, you have an extra & that needs to be removed from both the transmit and receive HAL method calls. Example:
HAL_UART_Transmit_IT(&huart1,(uint8_t*) &TxData1,10);
HAL_UART_Transmit_IT(&huart1,(uint8_t*) TxData1,10);
To avoid this type of error in the future, recommend not using the cast and try something like the following:
uint8_t TxData1[10];
...
HAL_UART_Transmit_IT(&huart1, TxData1, sizeof(TxData1);

Why does my input signal timing change when changing interrupt method?

The AVR in use is ATmega2560.
I've got an input signal that has a pulse width of 1 second that is generated.
This signal is attached to an external interrupt pin on my AVR (INT0).
INT0 is being initialized as follows:
Code:
DDRD &= ~(1 << PD0);
PORTD |= (1 << PD0);
EIMSK = 1 << INT0; // enable
EICRA |= (1 << ISC00) | (1 << ISC01); // trigger on rising edge
sei(); //global interrupts
The mission of the ISR for this external interrupt is to a) figure out which edge (first case should be rising) and b) perform action based on which edge
The ISR looks something like this:
Code:
ISR(INT0_vect)
{
if(EICRA == 0x02)
{
// falling edge detected
doFallingEdgeFunction_lightLED0();
// quickly change the trigger to capture opposite edge
EICRA |= (1 << ISC00) | (1 << ISC01); // trigger on rising edge
}
else if(EICRA == 0x03)
{
doRisingEdgeFunction_lightLED1();
// change trigger on falling edge
EICRA = (1 << ISC01);
EICRA &= ~(1 << ISC00);
}
}
It is able to detect the edges; the correct LEDs light up, but for some reason changing the edge interrupt bit in the ISR decreases my input signal to 0.1 second width instead of the full 1 second width.
On the scope, I see the original signal being mirrored but with 10x less width! If I remove the "switching trigger" items, the signal is fine.
From section 15.2.2, p. 114
Note:
1. n = 3, 2, 1or 0. When changing the ISCn1/ISCn0 bits, the interrupt must be disabled by clearing its Interrupt Enable bit in the EIMSK
Register. Otherwise an interrupt can occur when the bits are changed.
EDIT:
EIFR – External Interrupt Flag Register
• Bits 7:0 – INTF7:0: External Interrupt Flags 7 - 0
When an edge or logic change on the INT7:0 pin triggers an interrupt
request, INTF7:0 becomes set (one). If the I-bit in SREG and the
corresponding interrupt enable bit, INT7:0 in EIMSK, are set (one),
the MCU will jump to the interrupt vector. The flag is cleared when
the interrupt routine is executed. Alternatively, the flag can be
cleared by writing a logical one to it.
Try clearing the flag in the interrupt
EIFR = (1 << INT0);
Don't use
EIFR |= (1<< INT0); // DO NOT USE
That will clear all pending flags.

PIC Capture mode not triggering interrupt

I'm trying to use the capture module on a PIC 16LF1827, but the ISR is never entered. I started with a basic falling-edge interrupt (worked), then added in the timer 1 configuration (still working), then disabled the IOC interrupt and configured/enabled the relevant CCP interrupt (ISR is never entered). The code is below: the commented section is the original basic IOC setup.
I've verified with the MPLab debugger that the ISR is not entered, and confirmed this by hooking it up to a logic analyzer and watching RB1.
#include "htc.h"
//config1
//internal osc, no wdt, use power-up timer, enable reset
// no code protection, brown-out-reset enabled, clkout is gpio,
// internal-external switchover off, failsafe clock monitor off
__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_ON
& MCLRE_ON & CP_OFF & CPD_OFF & BOREN_ON
& CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
//config2 (following MPLab's complaints when running debugger)
//low-voltage programming off, debug on, brown-out reset at 2.7 v
// stack over/under flow triggers reset, no 4x pll,
// no flash write protection
__CONFIG(LVP_OFF & DEBUG_ON & BORV_27
& STVREN_ON & PLLEN_OFF & WRT_OFF);
void interrupt isr(void){
//bounce pin 1
LATB ^= 0b10;
LATB ^= 0b10;
if(IOCIF && IOCBF0){
IOCBF0 = 0;
IOCIF = 0;
}
if (CCP1IF){
CCP1IF = 0;
}
}
void main(void){
//configure internal oscillator:
//PLL = 0, source = from config 1, frequency = 4 mhz
//0b0: SPLLEN_OFF
OSCCONbits.SPLLEN = 0b0;
//0b00: use config word 1
OSCCONbits.SCS = 0b00;
//0b1101: 4 mhz frequency
OSCCONbits.IRCF = 0b1101;
//configure peripherals
//PORT A: LEDs (output), digital
TRISA = 0x00;
ANSELA = 0;
//PORT B: digital, 0 = input, 1 = output, rest don't care
TRISB = 0b11111101;
ANSELB = 0;
//configure timer 1 (not needed for basic IOC)
//source = instruction clock, prescale = 1:1, disable LP osc, do synchronize (DC)
//0b00: instruction clock
T1CONbits.TMR1CS = 0b00;
//0b00: 1:1
T1CONbits.T1CKPS = 0b00;
//0b0: lp osc off
T1OSCEN = 0b0;
//0b0: synch (ignored)
nT1SYNC = 0b0;
//interrupts
/*
//IOC enabled on falling edge for port B 0
IOCBN0 = 0b00000001;
IOCIE = 1;
*/
//Capture on falling edge for port B 0
//notes in 23.1 of DS: disable interrupt, set operating mode, clear flag, enable interrupt
CCP1IE = 0b0;
//0b100: every falling edge
CCP1CONbits.CCP1M = 0b100;
CCP1IF = 0b0;
CCP1IE = 0b1;
//enable peripheral interrupts, global interrupts
PEIE = 1;
GIE = 1;
//start timer 1
TMR1ON = 1;
while(1){
//Toggle led 0
LATA ^= 0b1;
}
}
I'm using the HI-TECH C compiler (lite), running in MPLab.
Any suggestions would be greatly appreciated. My apologies if I butcher terminology, this is my first project on a PIC.
Your setting for TRISB1 is as an output. According to the datasheet, the capture pin needs to be configured as an input. For the GPIO pins, setting the TRIS bit a 0 is an output, 1 is for an input.
EDIT: Forgive the initial stupid answer as I didn't realize you were using PORTB1 as a GPIO indicator for your scope.
So initially you used PORTB0 as your capture pin correct (using IOC)? The capture module uses a different GPIO port for its input (PORTB3 for CCP1). Did you move the connection to PORTB3 for your capture source?
EDIT: After some more looking through the PIC datasheet I noticed that CCP1's GPIO pin can be moved from PORTB3 to PORTB0 but I don't see any reference to how you set the APFCON0.CCP1SEL bit. That would be something else to check.