Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.20 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <inttypes.h>
  5.  
  6. #include "alt_clock_manager.h"
  7. #include "alt_generalpurpose_io.h"
  8. #include "alt_globaltmr.h"
  9. #include "alt_interrupt.h"
  10. #include "alt_timers.h"
  11. #include "alt_i2c.h"
  12.  
  13. #include "socal/alt_gpio.h"
  14. #include "socal/hps.h"
  15. #include "socal/socal.h"
  16.  
  17. #include "MyApp_Gsensor.h"
  18. #include "ADXL345.h"
  19. #include "hwlib.h"
  20. #include "hps_0.h"
  21.  
  22. /* Interrupt service routine for the buttons */
  23. void button_isr_callback (uint32_t icciar, void *context)
  24. {
  25.     uint32_t button_mask = alt_read_word(fpga_buttons);
  26.  
  27.         uint32_t freq_value = alt_read_word(fpga_freq_led);
  28.         if(button_mask / 2 != 1){
  29.             freq_value -= 2500000;
  30.         } else if(button_mask / 1 != 1){
  31.             freq_value += 2500000;
  32.         }
  33.  
  34.         if (freq_value < 25000000){
  35.             freq_value = 25000000;
  36.         } else if(freq_value > 650000000){
  37.             freq_value = 650000000;
  38.         }
  39.  
  40.         alt_write_word(fpga_freq_led,freq_value);
  41.     printf("INFO: IRQ  from buttons : %x (edge = %x)\r\n", alt_read_word(fpga_buttons), alt_read_word(fpga_buttons + 12));
  42.    
  43.     // Clear the interrupt
  44.     alt_gpt_int_clear_pending (GPT_BUTTON_IRQ);
  45.     // Clear the interruptmask of PIO core
  46.     alt_write_word(fpga_buttons + (2*4), 0x0);
  47.  
  48.     // Enable the interruptmask and edge register of PIO core for new interrupt
  49.     alt_write_word(fpga_buttons + (2*4), 0xF);
  50.     alt_write_word(fpga_buttons + (3*4), 0xF);
  51. }
  52.  
  53. void dipsw_callback (uint32_t icciar, void *context)
  54. {
  55.     // Do something
  56.     printf("DIPSW : %d \r\n", alt_read_word(fpga_dipsw));
  57.     uint32_t dipsw_mask = alt_read_word(fpga_dipsw);
  58.  
  59.     if(dipsw_mask == 14){
  60.         axis_selector = 1;
  61.     } else if(dipsw_mask == 13){
  62.         axis_selector = 2;
  63.     } else if (dipsw_mask == 11){
  64.         axis_selector = 3;
  65.     } else {
  66.         axis_selector = 0;
  67.     }
  68.  
  69.     // Clear the interrupt
  70.     alt_gpt_int_clear_pending (GPT_DIPSW_IRQ);
  71.     // Enable the interruptmask and edge register of PIO core for new interrupt
  72.     alt_write_word(fpga_dipsw + (2*4), 0xF);
  73.     alt_write_word(fpga_dipsw + (3*4), 0xF);
  74. }
  75.  
  76. /* Interrupt service routine for the timer */
  77. void timer_isr_callback (uint32_t icciar, void *context)
  78. {
  79.     // Clear the interrupt
  80.     alt_gpt_int_clear_pending (GPT_TIMER_ID);
  81.    
  82.     // Toggle hps led
  83.     uint32_t hps_led_value = alt_read_word(ALT_GPIO1_SWPORTA_DR_ADDR);
  84.     hps_led_value >>= HPS_LED_PORT_BIT;
  85.     hps_led_value = !hps_led_value;
  86.     hps_led_value <<= HPS_LED_PORT_BIT;
  87.     assert(ALT_E_SUCCESS == alt_gpio_port_data_write(HPS_LED_PORT, HPS_LED_MASK, hps_led_value));
  88. }
  89.  
  90. void setup_hps_interrupt() {
  91.    
  92.     // Initialize global interrupts
  93.     assert(ALT_E_SUCCESS == alt_int_global_init());
  94.     // Initialize CPU interrupts
  95.     assert(ALT_E_SUCCESS == alt_int_cpu_init());
  96.     // Register timer ISR
  97.     assert(ALT_E_SUCCESS == alt_int_isr_register(GPT_TIMER_IRQ, timer_isr_callback, NULL));
  98.     // Ignore target_set() for non-SPI interrupts
  99.     if (GPT_TIMER_IRQ >= 32) {
  100.         // Set timer interrupt distributor target (=0x3) (1 = CPU0, 2=CPU1)
  101.         assert(ALT_E_SUCCESS == alt_int_dist_target_set (GPT_TIMER_IRQ, (1 << alt_int_util_cpu_count()) - 1));
  102.     }
  103.     // Enable distributor interrupt
  104.     assert(ALT_E_SUCCESS == alt_int_dist_enable(GPT_TIMER_IRQ));
  105.    
  106.     // Register button ISR
  107.     assert(ALT_E_SUCCESS == alt_int_isr_register(GPT_BUTTON_IRQ, button_isr_callback, NULL));
  108.     // Ignore target_set() for non-SPI interrupts
  109.     if ((ALT_INT_INTERRUPT_F2S_FPGA_IRQ0 + BUTTON_PIO_IRQ) >= 32) {
  110.         // Set timer interrupt distributor target (=0x3) (1 = CPU0, 2=CPU1)
  111.         assert(ALT_E_SUCCESS == alt_int_dist_target_set (GPT_BUTTON_IRQ, (1 << alt_int_util_cpu_count()) - 1));
  112.     }
  113.     // Enable distributor interrupt
  114.     assert(ALT_E_SUCCESS == alt_int_dist_enable(GPT_BUTTON_IRQ));
  115.    
  116.     // Enable interruptmask and edgecapture of PIO core for buttons 0 and 1
  117.     alt_write_word(fpga_buttons + (2*4), 0x3);
  118.     alt_write_word(fpga_buttons + (3*4), 0x3);
  119.  
  120.     // Enable CPU interrupts
  121.     assert(ALT_E_SUCCESS == alt_int_cpu_enable());
  122.     // Enable all non-secure interrupt
  123.     assert(ALT_E_SUCCESS == alt_int_cpu_enable_ns());
  124.     // Enable global interrupts
  125.     assert(ALT_E_SUCCESS == alt_int_global_enable());
  126.  
  127.     // Enable interrupt dipsw
  128.     assert(ALT_E_SUCCESS == alt_int_isr_register(GPT_DIPSW_IRQ, dipsw_callback, NULL));
  129.     if ((ALT_INT_INTERRUPT_F2S_FPGA_IRQ0 + DIPSW_PIO_IRQ) >= 32) {
  130.             // Set timer interrupt distributor target (=0x3) (1 = CPU0, 2=CPU1)
  131.             assert(ALT_E_SUCCESS == alt_int_dist_target_set (GPT_DIPSW_IRQ, (1 << alt_int_util_cpu_count()) - 1));
  132.     }
  133.     // Enable distributor interrupt
  134.     assert(ALT_E_SUCCESS == alt_int_dist_enable(GPT_DIPSW_IRQ));
  135.     alt_write_word(fpga_dipsw + (2*4), 0xF);
  136.     alt_write_word(fpga_dipsw + (3*4), 0xF);
  137. }
  138.  
  139. void setup_hps_timer() {
  140.    
  141.     // Initialize global timer
  142.     assert(ALT_E_SUCCESS == alt_globaltmr_init());
  143.  
  144.     // Determine the frequency of the timer
  145.     uint32_t freq;
  146.     uint32_t period_in_ms = 500;
  147.     assert(ALT_E_SUCCESS == alt_clk_freq_get(ALT_CLK_MPU_PERIPH, &freq));
  148.     uint32_t counter = (freq / 1000) * period_in_ms;
  149.     assert(ALT_E_SUCCESS == alt_gpt_counter_set(GPT_TIMER_ID, counter));
  150.  
  151.     printf("INFO: Frequency = %" PRIu32 ".\r\n", freq);
  152.     printf("INFO: Period    = %" PRIu32 " millisecond(s).\r\n", period_in_ms);
  153.     printf("INFO: Counter   = %" PRIu32 ".\r\n", counter);
  154.     printf("INFO: Check     = %" PRIu32 " microsecond(s).\r\n", alt_gpt_time_microsecs_get(GPT_TIMER_ID));
  155.  
  156.     // Set to periodic, meaning it fires repeatedly
  157.     assert(ALT_E_SUCCESS == alt_gpt_mode_set(GPT_TIMER_ID, ALT_GPT_RESTART_MODE_PERIODIC));
  158.     // Set the prescaler of the timer to 0
  159.     assert(ALT_E_SUCCESS == alt_gpt_prescaler_set(GPT_TIMER_ID, 0));
  160.     // Clear pending interrupts
  161.     assert(ALT_E_SUCCESS == alt_gpt_int_if_pending_clear(GPT_TIMER_ID));
  162.     // Enable timer interrupts
  163.     assert(ALT_E_SUCCESS == alt_gpt_int_enable(GPT_TIMER_ID));
  164.     // Start the timer
  165.     assert(ALT_E_SUCCESS == alt_gpt_tmr_start(GPT_TIMER_ID));
  166. }
  167.  
  168. void setup_hps_gpio() {
  169.     uint32_t hps_gpio_config_len = 2;
  170.     ALT_GPIO_CONFIG_RECORD_t hps_gpio_config[] = {
  171.         {HPS_LED_IDX  , ALT_GPIO_PIN_OUTPUT, 0, 0, ALT_GPIO_PIN_DEBOUNCE, ALT_GPIO_PIN_DATAZERO},
  172.         {HPS_KEY_N_IDX, ALT_GPIO_PIN_INPUT , 0, 0, ALT_GPIO_PIN_DEBOUNCE, ALT_GPIO_PIN_DATAZERO}
  173.     };
  174.  
  175.     assert(ALT_E_SUCCESS == alt_gpio_init());
  176.     assert(ALT_E_SUCCESS == alt_gpio_group_config(hps_gpio_config, hps_gpio_config_len));
  177. }
  178.  
  179. void setup_hps_i2c() {
  180.    
  181.     uint32_t target_addr = 0b01010011; // I2C address of ADXL345 is 0x53
  182.     uint8_t  id;
  183.    
  184.     assert(ALT_E_SUCCESS == alt_i2c_init(ALT_I2C_I2C0, &i2c_dev));
  185.     assert(ALT_E_SUCCESS == alt_i2c_op_mode_set(&i2c_dev, ALT_I2C_MODE_MASTER));
  186.     assert(ALT_E_SUCCESS == alt_i2c_master_target_set(&i2c_dev, target_addr));
  187.     assert(ALT_E_SUCCESS == alt_i2c_enable(&i2c_dev));
  188.  
  189.    
  190.     if (ADXL345_Init(&i2c_dev)) {
  191.         if (ADXL345_IdRead(&i2c_dev, &id))
  192.             printf("ADXL345 id = %02Xh\r\n", id);
  193.     }
  194.    
  195. }
  196.  
  197. void setup_fpga_leds() {
  198.     // Switch on first LED only
  199.     alt_write_word(fpga_leds, 0x1);
  200. }
  201.  
  202. /* The HPS doesn't have a sleep() function like the Nios II, so we can make one
  203.  * by using the global timer. */
  204. void delay_us(uint32_t us) {
  205.     uint64_t start_time = alt_globaltmr_get64();
  206.     uint32_t timer_prescaler = alt_globaltmr_prescaler_get() + 1;
  207.     uint64_t end_time;
  208.     alt_freq_t timer_clock;
  209.  
  210.     assert(ALT_E_SUCCESS == alt_clk_freq_get(ALT_CLK_MPU_PERIPH, &timer_clock));
  211.     end_time = start_time + us * ((timer_clock / timer_prescaler) / ALT_MICROSECS_IN_A_SEC);
  212.  
  213.     // polling wait
  214.     while(alt_globaltmr_get64() < end_time);
  215. }
  216.  
  217. void handle_hps_led() {
  218.     uint32_t hps_gpio_input = alt_gpio_port_data_read(HPS_KEY_N_PORT, HPS_KEY_N_MASK);
  219.  
  220.     // HPS_KEY_N is active-low
  221.     bool toggle_hps_led = (~hps_gpio_input & HPS_KEY_N_MASK);
  222.  
  223.     if (toggle_hps_led) {
  224.         alt_write_word(fpga_leds, 0x3);    }
  225. }
  226.  
  227. void handle_gsensor() {
  228.     bool bSuccess;
  229.     const int mg_per_digi = 4;
  230.     uint16_t szXYZ[3];
  231.    
  232.     if (ADXL345_IsDataReady(&i2c_dev)){
  233.         bSuccess = ADXL345_XYZ_Read(&i2c_dev, szXYZ);
  234.         if (bSuccess){
  235.             int16_t led_val = 0;
  236.             if(axis_selector == 1){
  237.                 led_val = (int16_t)szXYZ[0];
  238.             } else if(axis_selector == 2){
  239.                 led_val = (int16_t)szXYZ[1];
  240.             } else if(axis_selector == 3){
  241.                 led_val = (int16_t)szXYZ[2];
  242.             }
  243.  
  244.             if(led_val < 0){
  245.                 led_val = -led_val;
  246.             }
  247.  
  248.             if(led_val > 127){
  249.                 led_val = 127;
  250.             }
  251.             alt_write_word(fpga_leds, led_val);
  252.             printf("X=%d , Y=%d , Z=%d led_val=%d\r\n",(int16_t)szXYZ[0], (int16_t)szXYZ[1], (int16_t)szXYZ[2],led_val);
  253.             // show raw data,
  254.             //printf("X=%04x, Y=%04x, Z=%04x\r\n", (alt_u16)szXYZ[0], (alt_u16)szXYZ[1],(alt_u16)szXYZ[2]);
  255.         }
  256.     }
  257. }
  258.  
  259. int main() {
  260.     printf("\r\n DE10-Nano - MyApp_Gsensor\r\n\r\n");
  261.  
  262.     setup_hps_interrupt();
  263.     setup_hps_timer();
  264.     setup_hps_gpio();
  265.     setup_hps_i2c();
  266.     setup_fpga_leds();
  267.  
  268.     while (true) {
  269.         handle_hps_led();
  270.         handle_gsensor();
  271.         delay_us(ALT_MICROSECS_IN_A_SEC);
  272.     }
  273.  
  274.     return 0;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement