- void iram_attr scan_semaphore(){
- fast_command(FAST_ADC_START);
- vTaskDelay(40 / portTICK_PERIOD_MS);
- for(int i=0;i<17;i++){
- read_adc_wchid();
- vTaskDelay(40 / portTICK_PERIOD_MS);
- };
- }
Implementation of Interrupt Allocation
-
- Posts: 26
- Joined: Tue Jun 09, 2020 5:27 pm
Re: Implementation of Interrupt Allocation
This felt weird but worked?
it's like centering a vcr on the right signal for the blanking, I have the pin pulled up and the adc just sometimes outputs mdat to irq and I had to reject the information. this function replaced start_scan
-
- Posts: 26
- Joined: Tue Jun 09, 2020 5:27 pm
Re: Implementation of Interrupt Allocation
I still can't really get this to implement with a interrupt only solution, any calling of adc read events from the interrupt causes crashes and right now my reads are on a loop and I'm protecting my read function with a semaphore and nothing else... interrupt overlaps incorrectly with other events and still not getting solution I want...
How should I call the read event here so that it can trigger from the interrupt? I've tried about 30 permutations and still not got it.
- int32_t IRAM_ATTR read_adc_wchid(){
- int32_t adc_reading = 0;
- char adc_sign = 0x0;
- char adc_channel = 0x0;
- ets_printf("Semaphore Take\n");
- xSemaphoreTake(irqSem, portMAX_DELAY);
- ets_printf("Semaphore Taken\n");
- //ets_printf("Semaphore Give\n");
- //xSemaphoreGive(irqSem);
- // ets_printf("Semaphore Given\n");
- ets_printf("Reading\n");
- read_from_register(REG_ADCDATA,4);
- ets_printf("Read\n");
- adc_sign = recvbuf[1] & 0b00001111;
- adc_channel = recvbuf[1] & 0b11110000;
- adc_channel = adc_channel >> 4;
- if (MCP_MODEL == 3461 || MCP_MODEL == 3462 || MCP_MODEL == 3464){
- adc_reading = recvbuf[3];
- adc_reading = adc_reading<<8;
- adc_reading = adc_reading | recvbuf[4];
- if(adc_sign!=0x0){//In case of a negative value, convert to negative
- adc_reading = -65536 + adc_reading;
- }
- }
- else if (MCP_MODEL == 3561 || MCP_MODEL == 3562 || MCP_MODEL == 3564){
- adc_reading = recvbuf[2];
- adc_reading = adc_reading << 8;
- adc_reading = adc_reading | recvbuf[3];
- adc_reading = adc_reading << 8;
- adc_reading = adc_reading | recvbuf[4];
- if(adc_sign!=0x0){//In case of a negative value, convert to negative
- adc_reading = -16777216 + adc_reading;
- }
- // if((adc_channel = 0b00001111)){
- // offsetbits[0] = recvbuf[2];
- // offsetbits[1] = recvbuf[3];
- // offsetbits[2] = recvbuf[4];
- // if ((adc_sign!=0x0)){
- // offsetbits[0] = offsetbits[0] | 0b10000000;
- // }
- // else {
- // offsetbits[0] <<= 1;
- // offsetbits[0] >>= 1;
- // }
- // }
- }
- int adc_intch = adc_channel;
- adc_results[adc_intch] = adc_reading;
- ets_printf("Semaphore Give\n");
- xSemaphoreGive(irqSem);
- ets_printf("Semaphore Given\n");
- //ets_printf("Semaphore Take\n");
- //xSemaphoreTake(irqSem, portMAX_DELAY);
- //ets_printf("Semaphore Taken\n");
- return adc_reading;
- }
- static void IRAM_ATTR gpio_isr_handler(void* arg)
- {
- ets_printf("Interrupt Start\n");
- BaseType_t mustYield=false;
- xSemaphoreGiveFromISR(irqSem, &mustYield);
- if (mustYield) portYIELD_FROM_ISR();
- ets_printf("Interrupt Stop\n");
- }
- void config_interrupt(){
- gpio_config_t io_conf;
- io_conf.intr_type = GPIO_INTR_NEGEDGE;
- io_conf.pin_bit_mask = 1<<GPIO_IRQ;
- io_conf.mode = GPIO_MODE_INPUT;
- io_conf.pull_down_en = 0;
- io_conf.pull_up_en = 1;
- irqSem=xSemaphoreCreateBinary();
- gpio_config(&io_conf);
- gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
- gpio_isr_handler_add(GPIO_IRQ, gpio_isr_handler, (void*) GPIO_IRQ);
- gpio_intr_disable(GPIO_IRQ);
- }
- void IRAM_ATTR scan_semaphore(){
- fast_command(FAST_ADC_START);//Send the read cmd
- ets_printf("Interrupt Enable\n");
- gpio_intr_enable(GPIO_IRQ);
- ets_printf("Interrupt Enabled\n");
- for(int i=0;i<17;i++){
- ets_printf("Delay Start\n");
- vTaskDelay(40 / portTICK_PERIOD_MS);
- ets_printf("Delay End\n");
- read_adc_wchid();
- ets_printf("Read Loop\n");
- };
- ets_printf("Interrupt Disable\n");
- gpio_intr_disable(GPIO_IRQ);
- ets_printf("Interrupt Disabled\n");
- }
- void app_main(void)
- {
- init_adc(EXT_CLK);
- enter_scan_mode();
- read_scan_reg();
- config_interrupt();
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- while(1) {
- scan_semaphore();
- printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",adc_results[0],adc_results[1],adc_results[2],adc_results[3],adc_results[4],adc_results[5],adc_results[6],adc_results[7],adc_results[8],adc_results[9],adc_results[10],adc_results[11],adc_results[12],adc_results[13],adc_results[14],adc_results[15]);
- }
-
- Posts: 26
- Joined: Tue Jun 09, 2020 5:27 pm
Re: Implementation of Interrupt Allocation
any advice on what to use to allow me to call the read function immediately after/in response to the interrupt?
Deferred handling? using rtos task notifcation as a lightweigh semaphore? task suspension followed by xTaskResumeFromISR?
I'm unsure of my options.
All I really need is to make sure the read command triggers as soon as the interrupt releases but I'm reading conflicting use cases between rtos and esp documentation?
I understand why I can't call another semaphore give or take, or call the spi while interrupt is active, that includes the functions called in the isr. but I want to directly call the read function in response to the interrupt.... I've got it working where I can do a non-loop event with a delay and register all the interrupts correctly, I just don't understand how to call the other function in response to the interrupt.
Deferred handling? using rtos task notifcation as a lightweigh semaphore? task suspension followed by xTaskResumeFromISR?
I'm unsure of my options.
All I really need is to make sure the read command triggers as soon as the interrupt releases but I'm reading conflicting use cases between rtos and esp documentation?
I understand why I can't call another semaphore give or take, or call the spi while interrupt is active, that includes the functions called in the isr. but I want to directly call the read function in response to the interrupt.... I've got it working where I can do a non-loop event with a delay and register all the interrupts correctly, I just don't understand how to call the other function in response to the interrupt.
-
- Posts: 9709
- Joined: Thu Nov 26, 2015 4:08 am
Re: Implementation of Interrupt Allocation
A semaphore isn't to protect anything here, it's signalling between the IRQ and the task that something interesting happened. I think you're confusing the use with a mutex (which is pretty similar) here.
In the code above, the IRQ core looks correct. All you need to do is rip all code referring to semaphores out of the rest of the routines, then simply place a single xSemaphoreTake(irqSem, portMAX_DELAY); after the line saying 'gpio_intr_enable(GPIO_IRQ);' in scan_semaphore. The logic is that that the semaphoreTake call blocks until the IRQ has actually given the semaphore, so the rest of the code will only happen when the IRQ has happened.
(Also, on second thought, something like a task notification may be a bit more optimal here... but first course of action is to get this working, I'd say.)
In the code above, the IRQ core looks correct. All you need to do is rip all code referring to semaphores out of the rest of the routines, then simply place a single xSemaphoreTake(irqSem, portMAX_DELAY); after the line saying 'gpio_intr_enable(GPIO_IRQ);' in scan_semaphore. The logic is that that the semaphoreTake call blocks until the IRQ has actually given the semaphore, so the rest of the code will only happen when the IRQ has happened.
(Also, on second thought, something like a task notification may be a bit more optimal here... but first course of action is to get this working, I'd say.)
-
- Posts: 26
- Joined: Tue Jun 09, 2020 5:27 pm
Re: Implementation of Interrupt Allocation
Thanks so much, right now I have it working with the semaphore as you suggested.
Right now I am keeping the interrupt enabled during the read window for scan, and then creating task to check for bool irqready that is also toggled by irq trigger. I have a delay function at end of the task loop to allow for the code to resume as no taskdelay at end of every task loop would not allow other code to return due to priorties I believe.
This works, but I will definitely want to use a task notification like notifiy from isr to resume the read function.
Right now I am keeping the interrupt enabled during the read window for scan, and then creating task to check for bool irqready that is also toggled by irq trigger. I have a delay function at end of the task loop to allow for the code to resume as no taskdelay at end of every task loop would not allow other code to return due to priorties I believe.
This works, but I will definitely want to use a task notification like notifiy from isr to resume the read function.
Who is online
Users browsing this forum: Bing [Bot] and 141 guests