summaryrefslogtreecommitdiff
path: root/software/global_interrupts/global_interrupts.c
blob: 4d3a554c3e7c7a5e622d4c17e0192c9aff1bfd0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// See LICENSE for license details.

#include <stdio.h>
#include <stdlib.h>
#include "platform.h"
#include <string.h>
#include "plic/plic_driver.h"
#include "encoding.h"
#include <unistd.h>

#ifndef _SIFIVE_COREPLEXIP_ARTY_H
#error 'global_interrupts' demo only supported for Coreplex IP Eval Kits
#endif

// Global Instance data for the PLIC
// for use by the PLIC Driver.
plic_instance_t g_plic;

// Flag for state
int g_switch1Wins;

// Debounce counter (PWM can't go slow enough)
int g_debounce;

void debounce();

// Structures for registering different interrupt handlers
// for different parts of the application.
typedef void (*interrupt_function_ptr_t) (void);

// See bsp/env/<BOARD>/init.c for how this
// interrupt vector is used.
interrupt_function_ptr_t localISR[32]; 

interrupt_function_ptr_t g_ext_interrupt_handlers[PLIC_NUM_INTERRUPTS];

void set_timer() {
  
  volatile uint64_t * mtime       = (uint64_t*) (CLINT_CTRL_ADDR + CLINT_MTIME);
  volatile uint64_t * mtimecmp    = (uint64_t*) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);
  uint64_t now = *mtime;
  uint64_t then = now + 10*RTC_FREQ;
  *mtimecmp = then;

}

/*Entry Point for Machine Timer Interrupt Handler*/
void mti_isr(){
 
  if (g_switch1Wins) {
    printf("#### Giving Switch 1 Priority for 10 seconds ####\n");
    // All other things being equal, lower IDs have
    // higher priority. We have already set
    // Switch 1 to priority 2
    // in the setup, so by giving these equal priority Switch 1 will win.
    PLIC_set_priority(&g_plic, INT_EXT_DEVICE_SW_2, 2);
  } else {
    printf("**** Giving Switch 2 Priority for 10 seconds ****\n");
    // By setting Switch 2 a higher integer priority, it will win over switch 1.
    PLIC_set_priority(&g_plic, INT_EXT_DEVICE_SW_2, 3);
  }    
  g_switch1Wins ^= 0x1;

  set_timer();

}

/*Entry Point for PLIC Interrupt Handler*/
void mei_isr(){
  plic_source int_num  = PLIC_claim_interrupt(&g_plic);
  if ((int_num >=1 ) && (int_num < PLIC_NUM_INTERRUPTS)) {
    g_ext_interrupt_handlers[int_num]();
  }
  else {
    exit(1 + (uintptr_t) int_num);
  }
  PLIC_complete_interrupt(&g_plic, int_num);
}

const char * instructions_msg = " \
\n\
                SIFIVE, INC.\n\
E31/E51 Coreplex IP Eval Kit 'global_interrupts' demo.\n\
\n\
Switches  1 and 2 are enabled as External Global Interrupts \n\
(they don't go through the PLIC). You an observe priorities.\n\
Priorities invert every few seconds, which is driven by the \n\
PWM0 global interrupt.                                      \n\
\n";

void print_instructions() {

  write (STDOUT_FILENO, instructions_msg, strlen(instructions_msg));

}

void invalid_global_isr() {
  printf("Unexpected global interrupt!\n");
}

void invalid_local_isr() {
  printf ("Unexpected local interrupt!\n");
}

void switch_1_handler() {

  printf("Switch 1 is on! Even if Switch 2 is on, Switch 1 must have higher priority right now.\n");
  
  // Set Green LED
  GPIO_REG(GPIO_OUTPUT_VAL)  |=  (0x1 << GREEN_LED_OFFSET) ;
  GPIO_REG(GPIO_OUTPUT_VAL)  &=  ~((0x1<< RED_LED_OFFSET));

  debounce();
  
}

void switch_2_handler() {
  printf("Switch 2 is on! Even if Switch 1 is on, Switch 2 must have higher priority right now.\n");

  // Set RED LED
  GPIO_REG(GPIO_OUTPUT_VAL)  &=  ~(0x1 << GREEN_LED_OFFSET) ;
  GPIO_REG(GPIO_OUTPUT_VAL)  |=   (0x1<< RED_LED_OFFSET);

  debounce();
}

// We use PWM 0 as a 
// timer interrupt for debouncing.

void pwm_0_handler() {


  if (g_debounce == 0) {
    printf("    Done debouncing.\n");
    
    //Lower the threshold s.t. the switches can hit.
    PLIC_set_threshold(&g_plic, 1);
    
    // Clear the PWM interrupt
    PWM0_REG(PWM_CFG) = 0;
    
  } else {
    // Keep waiting
    g_debounce --;
    // This clears out the interrupt and re-arms the timer.
    PWM0_REG(PWM_CFG) = ((PWM_CFG_ONESHOT) | (PWM_CFG_ZEROCMP)| 0x7 | (PWM_CFG_STICKY));
    
  }

}

void debounce(int local_interrupt_num) {

  printf("    Starting a debounce.\n");

  g_debounce = 600;
  
  // This clears out the interrupt and re-arms the timer.
  PWM0_REG(PWM_CFG) = ((PWM_CFG_ONESHOT) | (PWM_CFG_ZEROCMP)| 0x7 | (PWM_CFG_STICKY));
  
  // Set the threshold high enough that the
  // switches won't cause the interrupt to fire,
  // only the PWM or timer interrupts.
  PLIC_set_threshold(&g_plic, 4);

}

int main(int argc, char **argv)
{

  for (int gisr = 0; gisr < PLIC_NUM_INTERRUPTS; gisr++){
    g_ext_interrupt_handlers[PLIC_NUM_INTERRUPTS] = invalid_global_isr;
  }
  g_ext_interrupt_handlers[PWM0_INT_BASE + 0] = pwm_0_handler;
  g_ext_interrupt_handlers[INT_EXT_DEVICE_SW_1] = switch_1_handler;
  g_ext_interrupt_handlers[INT_EXT_DEVICE_SW_2] = switch_2_handler;
  
  for (int lisr = 0; lisr < 32; lisr++){
    localISR[lisr] = invalid_local_isr;
  }
  
  localISR[IRQ_M_TIMER] = mti_isr;
  localISR[IRQ_M_EXT]   = mei_isr;

  print_instructions();
  
  // Set up RGB LEDs for a visual.
  
  GPIO_REG(GPIO_OUTPUT_EN)   |=  ((0x1<< RED_LED_OFFSET)| (0x1<< GREEN_LED_OFFSET));
  GPIO_REG(GPIO_OUTPUT_VAL)  |=  (0x1 << GREEN_LED_OFFSET) ;
  GPIO_REG(GPIO_OUTPUT_VAL)  &=  ~(0x1<< RED_LED_OFFSET);
				   
  /**************************************************************************
   * Set up the PLIC
   *
   *************************************************************************/
  PLIC_init(&g_plic,
	    PLIC_CTRL_ADDR,
	    PLIC_NUM_INTERRUPTS,
	    PLIC_NUM_PRIORITIES);

  /**************************************************************************
   * Give Switch 1 and Switch 2 Equal priority of 2.
   *
   *************************************************************************/

  PLIC_enable_interrupt (&g_plic, PWM0_INT_BASE + 0);
  PLIC_enable_interrupt (&g_plic, INT_EXT_DEVICE_SW_1);
  PLIC_enable_interrupt (&g_plic, INT_EXT_DEVICE_SW_2);

  // PWM always beats the switches, because we use it
  // as a debouncer, and we lower the threshold
  // to do so.

  PWM0_REG(PWM_CFG) = 0;

  // Make sure people aren't blinded by LEDs connected here.
  PWM0_REG(PWM_CMP0)  = 0xFE;
  PWM0_REG(PWM_CMP1)  = 0xFF;
  PWM0_REG(PWM_CMP2)  = 0xFF;
  PWM0_REG(PWM_CMP3)  = 0xFF;
  PLIC_set_priority(&g_plic, PWM0_INT_BASE + 0   , 5);

  // Start the switches out at the same priority. Switch1
  // would win.
  PLIC_set_priority(&g_plic, INT_EXT_DEVICE_SW_1, 2);
  PLIC_set_priority(&g_plic, INT_EXT_DEVICE_SW_2, 2);

  // Set up machine timer interrupt. Every few seconds it
  // will invert the switch priorities.
  set_timer();

  // Enable timer interrupts.
  set_csr(mie, MIP_MTIP);

  // Enable Global (PLIC) interrupts.
  set_csr(mie, MIP_MEIP);

  g_switch1Wins = 1;
  
  // Enable all interrupts
  set_csr(mstatus, MSTATUS_MIE);
  
  while(1){
    asm volatile ("wfi");
  }
  
  return 0;

}