|
| 1 | +/*! @file |
| 2 | + @brief |
| 3 | + Hardware abstraction layer |
| 4 | + for RP2040 |
| 5 | +
|
| 6 | + <pre> |
| 7 | + Copyright (C) 2016-2021 Kyushu Institute of Technology. |
| 8 | + Copyright (C) 2016-2021 Shimane IT Open-Innovation Center. |
| 9 | +
|
| 10 | + This file is distributed under BSD 3-Clause License. |
| 11 | + </pre> |
| 12 | +*/ |
| 13 | + |
| 14 | +/***** Feature test switches ************************************************/ |
| 15 | +/***** System headers *******************************************************/ |
| 16 | +#include <string.h> |
| 17 | + |
| 18 | +/***** Local headers ********************************************************/ |
| 19 | +#include "hal.h" |
| 20 | + |
| 21 | +/***** Constat values *******************************************************/ |
| 22 | +/***** Macros ***************************************************************/ |
| 23 | +/***** Typedefs *************************************************************/ |
| 24 | +/***** Function prototypes **************************************************/ |
| 25 | +/***** Local variables ******************************************************/ |
| 26 | +struct repeating_timer timer; |
| 27 | + |
| 28 | +/***** Global variables *****************************************************/ |
| 29 | +/***** Signal catching functions ********************************************/ |
| 30 | +/***** Local functions ******************************************************/ |
| 31 | +/***** Global functions *****************************************************/ |
| 32 | +#ifndef MRBC_NO_TIMER |
| 33 | + |
| 34 | +//================================================================ |
| 35 | +/*!@brief |
| 36 | + timer alarm irq |
| 37 | +
|
| 38 | +*/ |
| 39 | +bool alarm_irq(struct repeating_timer *t) { |
| 40 | + mrbc_tick(); |
| 41 | +} |
| 42 | + |
| 43 | +//================================================================ |
| 44 | +/*!@brief |
| 45 | + initialize |
| 46 | +
|
| 47 | +*/ |
| 48 | +void hal_init(void){ |
| 49 | + add_repeating_timer_ms(1, alarm_irq, NULL, &timer); |
| 50 | + clocks_hw->sleep_en0 = 0; |
| 51 | + clocks_hw->sleep_en1 = CLOCKS_SLEEP_EN1_CLK_SYS_TIMER_BITS |
| 52 | + | CLOCKS_SLEEP_EN1_CLK_SYS_USBCTRL_BITS |
| 53 | + | CLOCKS_SLEEP_EN1_CLK_USB_USBCTRL_BITS |
| 54 | + | CLOCKS_SLEEP_EN1_CLK_SYS_UART0_BITS |
| 55 | + | CLOCKS_SLEEP_EN1_CLK_PERI_UART0_BITS; |
| 56 | +} |
| 57 | + |
| 58 | +//================================================================ |
| 59 | +/*!@brief |
| 60 | + Write |
| 61 | +
|
| 62 | + @param fd dummy, but 1. |
| 63 | + @param buf pointer of buffer. |
| 64 | + @param nbytes output byte length. |
| 65 | +
|
| 66 | + Memo: Steps to use uart_putc_raw() with hal_write. |
| 67 | + 1. Write in main function↓ |
| 68 | + uart_init(uart0,115200); |
| 69 | + gpio_set_function(0,GPIO_FUNC_UART); |
| 70 | + gpio_set_function(1,GPIO_FUNC_UART); |
| 71 | +
|
| 72 | + 2. Comment out the putchar for hal_write. |
| 73 | + 3. Uncomment uart_putc_raw for hal_write. |
| 74 | +*/ |
| 75 | +//int hal_write(int fd, const void *buf, int nbytes) |
| 76 | +//{ |
| 77 | +// int i = nbytes; |
| 78 | +// const uint8_t *p = buf; |
| 79 | +// |
| 80 | +// while( --i >= 0 ) { |
| 81 | +// putchar( *p++ ); |
| 82 | +// // uart_putc_raw(uart0, *p++ ); |
| 83 | +// } |
| 84 | +// |
| 85 | +// return nbytes; |
| 86 | +//} |
| 87 | +/* |
| 88 | + * hal_write() using tinyusb |
| 89 | + */ |
| 90 | +#define CFG_TUSB_MCU 1 |
| 91 | +#include "tusb_config.h" |
| 92 | +#include "tusb.h" |
| 93 | +int hal_write(int fd, const void *buf, int nbytes) |
| 94 | +{ |
| 95 | + tud_cdc_write(buf, nbytes); |
| 96 | + tud_cdc_write_flush(); |
| 97 | + return nbytes; |
| 98 | +} |
| 99 | + |
| 100 | +//================================================================ |
| 101 | +/*!@brief |
| 102 | + Flush write baffer |
| 103 | +
|
| 104 | + @param fd dummy, but 1. |
| 105 | +*/ |
| 106 | +int hal_flush(int fd) { |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +#endif /* ifndef MRBC_NO_TIMER */ |
| 111 | + |
| 112 | + |
| 113 | +//================================================================ |
| 114 | +/*!@brief |
| 115 | + abort program |
| 116 | +
|
| 117 | + @param s additional message. |
| 118 | +*/ |
| 119 | +void hal_abort(const char *s) |
| 120 | +{ |
| 121 | + if( s ) { |
| 122 | + hal_write(1, s, strlen(s)); |
| 123 | + } |
| 124 | + |
| 125 | + abort(); |
| 126 | +} |
| 127 | + |
0 commit comments