kitchen_sink.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdio.h>
  7. #include "pico/stdlib.h"
  8. #include "pico/time.h"
  9. #include "hardware/dma.h"
  10. #include "pico/bit_ops.h"
  11. #include "hardware/i2c.h"
  12. #include "hardware/pwm.h"
  13. #include "hardware/pio.h"
  14. #include "hardware/irq.h"
  15. #include "hardware/timer.h"
  16. #include "pico/divider.h"
  17. #include "pico/critical_section.h"
  18. #include "pico/binary_info.h"
  19. bi_decl(bi_block_device(
  20. BINARY_INFO_MAKE_TAG('K', 'S'),
  21. "foo",
  22. 0x80000,
  23. 0x40000,
  24. NULL,
  25. BINARY_INFO_BLOCK_DEV_FLAG_READ | BINARY_INFO_BLOCK_DEV_FLAG_WRITE |
  26. BINARY_INFO_BLOCK_DEV_FLAG_PT_UNKNOWN));
  27. void my_timer(uint i) {
  28. puts("XXXX timer");
  29. }
  30. //#pragma GCC push_options
  31. //#pragma GCC optimize ("O3")
  32. uint32_t *foo = (uint32_t *) 200;
  33. uint32_t dma_to = 0;
  34. uint32_t dma_from = 0xaaaa5555;
  35. void spiggle() {
  36. dma_channel_config c = dma_channel_get_default_config(1);
  37. channel_config_set_bswap(&c, true);
  38. channel_config_set_transfer_data_size(&c, DMA_SIZE_16);
  39. channel_config_set_ring(&c, true, 13);
  40. dma_channel_set_config(1, &c, false);
  41. dma_channel_transfer_from_buffer_now(1, foo, 23);
  42. }
  43. void __isr dma_handler_a() {
  44. printf("HELLO A\n");
  45. if (dma_hw->ints1 & 1) {
  46. dma_hw->ints1 = 1;
  47. printf("A WINS DMA_TO %08x\n", (uint) dma_to);
  48. irq_remove_handler(DMA_IRQ_1, dma_handler_a);
  49. }
  50. }
  51. void __isr dma_handler_b() {
  52. printf("HELLO B\n");
  53. if (dma_hw->ints1 & 1) {
  54. dma_hw->ints1 = 1;
  55. printf("B WINS DNA_TO %08x\n", (uint) dma_to);
  56. // irq_remove_handler(DMA_IRQ_1, dma_handler_b);
  57. }
  58. }
  59. //#pragma GCC pop_options
  60. int main() {
  61. spiggle();
  62. stdio_init_all();
  63. printf("HI %d\n", (int)time_us_32());
  64. puts("Hello Everything!");
  65. puts("Hello Everything2!");
  66. irq_add_shared_handler(DMA_IRQ_1, dma_handler_a, 0x80);
  67. irq_add_shared_handler(DMA_IRQ_1, dma_handler_b, 0xC0);
  68. dma_channel_config config = dma_channel_get_default_config(0);
  69. // set_exclusive_irq_handler(DMA_IRQ_1, dma_handler_a);
  70. dma_channel_set_irq1_enabled(0, true);
  71. irq_set_enabled(DMA_IRQ_1, true);
  72. dma_channel_configure(0, &config, &dma_to, &dma_from, 1, true);
  73. dma_channel_set_config(0, &config, false);
  74. // timer_start_ms(2, 2000, my_timer);
  75. for (int i = 0; i < 20; i++) {
  76. puts("sleepy");
  77. sleep_ms(1000);
  78. dma_channel_configure(0, &config, &dma_to, &dma_from, 1, true);
  79. if (i==3) {
  80. irq_remove_handler(DMA_IRQ_1, dma_handler_a);
  81. }
  82. if (i==2) {
  83. irq_remove_handler(DMA_IRQ_1, dma_handler_b);
  84. }
  85. }
  86. }