Advertisement
vrangan

pico-ice basic functions

Sep 14th, 2022 (edited)
1,388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.47 KB | Source Code | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "pico/stdlib.h"
  6. #include "pico/stdio.h"
  7. #include "hardware/gpio.h"
  8. #include "pico-ice/flash.h"
  9. #include "pico-ice/ice.h"
  10. #include "tusb.h"
  11. #include "hardware/pll.h"
  12. #include "hardware/clocks.h"
  13. #include "hardware/structs/pll.h"
  14. #include "hardware/structs/clocks.h"
  15. #include "time.h"
  16.  
  17. #define ICE_SO      0
  18. #define ICE_SSn     1
  19. #define ICE_SCK     2
  20. #define ICE_SI      3
  21. #define ICE_DONE    26
  22. #define ICE_RSTn    27
  23.  
  24. #define RED_LED     24
  25. #define GREEN_LED   23
  26. #define BLUE_LED    22
  27.  
  28. #define GPIO_HI     1
  29. #define GPIO_LO     0
  30. #define LED_ON      false
  31. #define LED_OFF     true
  32.  
  33. static void
  34. cdc_task(void)
  35. {
  36.     uint8_t buf[64];
  37.     uint32_t sz;
  38.  
  39.     if (!tud_cdc_n_available(0))
  40.         return;
  41.  
  42.     sz = tud_cdc_n_read(0, buf, sizeof(buf));
  43.     for (uint32_t i = 0; i < sz; i++)
  44.         tud_cdc_n_write_char(0, toupper(buf[i]));
  45.     tud_cdc_n_write_flush(0);
  46. }
  47.  
  48. static void send_buf(uint8_t *buf) {
  49.     for (uint8_t i = 0; i<strlen(buf); i++)
  50.         tud_cdc_n_write_char(0, buf[i]);
  51.     tud_cdc_n_write_flush(0);
  52. }
  53.  
  54. static void setup_pico_ice (void) {
  55.     gpio_init(RED_LED);
  56.     gpio_init(GREEN_LED);
  57.     gpio_init(BLUE_LED);
  58.     gpio_set_dir_out_masked( (1 << RED_LED) | (1<<GREEN_LED) | (1<<BLUE_LED) );
  59.     tusb_init();
  60.  
  61.     // Safe FGA settings:
  62.     gpio_init(ICE_RSTn);
  63.     gpio_init(ICE_DONE);
  64.     gpio_init(ICE_SSn);
  65.     gpio_init(ICE_SI);
  66.     gpio_pull_up(ICE_RSTn);
  67.     gpio_pull_up(ICE_DONE);
  68.     gpio_pull_up(ICE_SSn);
  69.     gpio_pull_up(ICE_SI);
  70.  
  71.     gpio_set_dir_in_masked( (1<<ICE_DONE) | (1<<ICE_SSn) | (1<<ICE_SO)
  72.                           | (1<<ICE_SI)   | (1<<ICE_SCK) | (1<<ICE_RSTn));
  73. }
  74.  
  75. static void reset_fpga(void) {
  76.         // Reset the FPGA:
  77.     gpio_set_dir_out_masked( (1 << ICE_RSTn) );
  78.     gpio_put(ICE_RSTn, GPIO_LO);
  79.     sleep_ms(10); // Reset time
  80.     // Let the ICE boot up
  81.     gpio_put(ICE_RSTn, GPIO_HI);
  82. }
  83.  
  84. int main(void)
  85. {
  86.     stdio_init_all();
  87.  
  88.     ice_flash_init();
  89.  
  90.     setup_pico_ice();
  91.  
  92.     reset_fpga();
  93.    
  94.     clock_gpio_init(21, CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLK_USB, 4);
  95.     clock_gpio_init(25, CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLK_USB, 8);
  96.  
  97.     for (;;) {
  98.         tud_task();
  99.         cdc_task();
  100.         gpio_put(RED_LED, LED_ON);
  101.         gpio_put(GREEN_LED, LED_OFF);
  102.         gpio_put(BLUE_LED, LED_OFF);
  103.         send_buf("Testing 1 2 3\n\r");
  104.     }
  105.  
  106.     return 0;
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement