]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/T-HEAD_CB2201_CDK/csi/csi_driver/csky/hobbit1_2/devices.c
Introduce a port for T-HEAD CK802. A simple demo for T-HEAD CB2201 is also included.
[freertos] / FreeRTOS / Demo / T-HEAD_CB2201_CDK / csi / csi_driver / csky / hobbit1_2 / devices.c
1 /*
2  * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /******************************************************************************
18  * @file     devices.c
19  * @brief    source file for the devices
20  * @version  V1.0
21  * @date     24. August 2017
22  ******************************************************************************/
23 #include "soc.h"
24 #include "config.h"
25 #include <drv_usart.h>
26 #include <drv_timer.h>
27 #include <drv_rtc.h>
28 #include <drv_trng.h>
29 #include <drv_crc.h>
30 #include <drv_aes.h>
31 #include <drv_rsa.h>
32 #include <drv_eflash.h>
33 #include <drv_spi.h>
34 #include <drv_gpio.h>
35
36 #include <stdio.h>
37
38 #include "pin_name.h"
39 #include "pinmux.h"
40
41 #define readl(addr) \
42     ({ unsigned int __v = (*(volatile unsigned int *) (addr)); __v; })
43
44 #define writel(b,addr) (void)((*(volatile unsigned int *) (addr)) = (b))
45
46
47 #if CONFIG_GPIO
48 struct {
49     uint32_t base;
50     uint32_t irq;
51     uint32_t pin_num;
52     port_name_t port;
53 }
54 const sg_gpio_config[CONFIG_GPIO_NUM] = {
55     {CSKY_GPIO0_BASE, GPIOA_IRQn, 28, PORTA},
56     {CSKY_GPIO1_BASE, GPIOB_IRQn, 4, PORTB},
57 };
58
59 typedef struct {
60     pin_t    gpio_pin;
61     uint32_t cfg_idx;    //idx of sg_gpio_config[]
62 } gpio_pin_map_t;
63 const static gpio_pin_map_t s_gpio_pin_map[] = {
64         {PA0_TRIG0_ACMP1P_TCK, 0},
65         {PA1_TRIG1_ACMP1N_TMS, 0},
66         {PA2_TXD0_SPI0MISO, 0},
67         {PA3_RXD0_SPI0MOSI, 0},
68         {PA4_CTS0_PWM0_SPI0SCK_TRIG0, 0},
69         {PA5_RTS0_PWM1_SPI0SSN_TRIG1, 0},
70
71         {PB0_SCL0_PWM2_I2SMCLK, 1},
72         {PB1_SDA0_PWM3_I2SSCK, 1},
73         {PB2_SPI0SCK_PWM4_I2SWS, 1},
74         {PB3_SPI0MISO_PWM5_I2SSD, 1},
75
76         {PA6_SPI0MOSI_PWM6_SCL0, 0},
77         {PA7_SPI0SSN_PWM7_SDA0, 0},
78         {PA8_WKUP_ADC0_ACMP0P, 0},
79         {PA9_BOOT_ADC1_PWMFAULT, 0},
80         {PA10_ADC2_TXD0, 0},
81         {PA11_ACMP0N_ADC3_RXD0, 0},
82         {PA12_PWM8_TCK_ADC4, 0},
83         {PA13_PWM9_TMS_ADC5, 0},
84         {PA14_PWM10_ADC6, 0},
85         {PA15_PWM11_ADC7, 0},
86         {PA16_RXD1_ADC8, 0},
87         {PA17_TXD1_ADC9, 0},
88         {PA18_SPI1SSN0_ACMP0O, 0},
89         {PA19_SPI1SSN1_ACMP1O, 0},
90         {PA20_SPI1SSN2_TRIG0_RXD1, 0},
91         {PA21_SPI1SCK_TRIG1_TXD1, 0},
92         {PA22_SPI1MISO_PWM0_ADC10, 0},
93         {PA23_SPI1MOSI_PWM1_ADC11, 0},
94         {PA24_TXD2_I2SMCLK_SPI1SSN0, 0},
95         {PA25_RXD2_I2SSCK_SPI1SSN1, 0},
96         {PA26_CTS2_I2SWS_ADC12, 0},
97         {PA27_RTS2_I2SSD_ADC13, 0}
98 };
99
100 int32_t target_gpio_port_init(port_name_t port, uint32_t *base, uint32_t *irq, uint32_t *pin_num)
101 {
102     int i;
103
104     for (i = 0; i < CONFIG_GPIO_NUM; i++) {
105         if (sg_gpio_config[i].port == port) {
106             *base = sg_gpio_config[i].base;
107             *irq = sg_gpio_config[i].irq;
108             *pin_num = sg_gpio_config[i].pin_num;
109             return i;
110         }
111     }
112
113     return -1;
114 }
115 /**
116   \param[in]   instance idx, must not exceed return value of target_get_gpio_count()
117   \brief       get gpio instance.
118   \return      pointer to gpio instance
119 */
120 int32_t target_gpio_pin_init(pin_t gpio_pin, uint32_t *port_idx)
121 {
122     uint32_t idx;
123
124     for (idx = 0; idx < sizeof(s_gpio_pin_map) / sizeof(gpio_pin_map_t); idx++) {
125         if (s_gpio_pin_map[idx].gpio_pin == gpio_pin) {
126             *port_idx = s_gpio_pin_map[idx].cfg_idx;
127             /*pinmux*/
128             pin_mux(s_gpio_pin_map[idx].gpio_pin, 0xff);
129             if (idx >= 10) {
130                 return idx - 4;
131             } else if (idx >= 6) {
132                 return idx - 6;
133             } else {
134                 return idx;
135             }
136         }
137     }
138
139     return -1;
140 }
141
142 #endif
143
144 #if CONFIG_TIMER
145 struct {
146     uint32_t base;
147     uint32_t irq;
148 }
149 const sg_timer_config[CONFIG_TIMER_NUM] = {
150     {CSKY_TIM0_BASE, TIMA0_IRQn},
151     {CSKY_TIM0_BASE + 0x14, TIMA1_IRQn},
152     {CSKY_TIM1_BASE, TIMB0_IRQn},
153     {CSKY_TIM1_BASE + 0x14, TIMB1_IRQn}
154
155 };
156
157 int32_t target_get_timer_count(void)
158 {
159     return CONFIG_TIMER_NUM;
160 }
161
162 int32_t target_get_timer(int32_t idx, uint32_t *base, uint32_t *irq)
163 {
164     if (idx >= target_get_timer_count()) {
165         return NULL;
166     }
167
168     *base = sg_timer_config[idx].base;
169     *irq = sg_timer_config[idx].irq;
170     return idx;
171 }
172
173 #endif
174
175 #if CONFIG_PMU
176 struct {
177     uint32_t base;
178     uint32_t irq;
179 }
180 const sg_pmu_config[CONFIG_PMU_NUM] = {
181     {CSKY_CLKGEN_BASE, POWM_IRQn}
182 };
183
184 int32_t target_get_pmu(int32_t idx, uint32_t *base, uint32_t *irq)
185 {
186     if (idx > CONFIG_PMU_NUM) {
187         return -1;
188     }
189     *base = sg_pmu_config[idx].base;
190     *irq = sg_pmu_config[idx].irq;
191     return idx;
192 }
193 #endif
194
195 #if CONFIG_RTC
196 #undef  CSKY_PMU_BASE
197 #define CSKY_PMU_BASE  0x40002000
198 #define BIT1 (0x1)
199 struct {
200     uint32_t base;
201     uint32_t irq;
202 }
203 const sg_rtc_config[CONFIG_RTC_NUM] = {
204     {CSKY_RTC0_BASE, RTC_IRQn},
205     {CSKY_RTC1_BASE, RTC1_IRQn}
206
207 };
208
209 int32_t target_get_rtc_count(void)
210 {
211     return CONFIG_RTC_NUM;
212 }
213
214 int32_t target_get_rtc(int32_t idx, uint32_t *base, uint32_t *irq)
215 {
216     unsigned int value;
217
218     if (idx >= target_get_rtc_count()) {
219         return NULL;
220     }
221
222     value  = readl(CSKY_PMU_BASE);
223     value &= ~BIT1;
224     writel(value, CSKY_PMU_BASE);
225
226     *base = sg_rtc_config[idx].base;
227     *irq = sg_rtc_config[idx].irq;
228     return idx;
229 }
230
231 #endif
232
233 #if CONFIG_TRNG
234 struct {
235     uint32_t base;
236 }
237 const sg_trng_config[CONFIG_TRNG_NUM] = {
238     {CSKY_TRNG_BASE}
239 };
240
241 /**
242   \brief       get trng instance count.
243   \return      trng instance count
244 */
245 int32_t target_get_trng_count(void)
246 {
247     return CONFIG_TRNG_NUM;
248 }
249
250 /**
251   \param[in]   instance idx, must not exceed return value of target_get_trng_count()
252   \brief       get trng instance.
253   \return      pointer to trng instance
254 */
255 int32_t target_get_trng(int32_t idx, uint32_t *base)
256 {
257     if (idx >= target_get_trng_count()) {
258         return NULL;
259     }
260
261     *base = sg_trng_config[idx].base;
262     return idx;
263 }
264
265 #endif
266
267 #if CONFIG_CRC
268 struct {
269     uint32_t base;
270 }
271 const sg_crc_config[CONFIG_CRC_NUM] = {
272     {CSKY_CRC_BASE}
273 };
274
275 /**
276   \brief       get crc instance count.
277   \return      crc instance count
278 */
279 int32_t target_get_crc_count(void)
280 {
281     return CONFIG_CRC_NUM;
282 }
283
284 /**
285   \param[in]   instance idx, must not exceed return value of target_get_crc_count()
286   \brief       get crc instance.
287   \return      pointer to crc instance
288 */
289 int32_t target_get_crc(int32_t idx, uint32_t *base)
290 {
291     if (idx >= target_get_crc_count()) {
292         return NULL;
293     }
294
295     *base = sg_crc_config[idx].base;
296     return idx;
297 }
298
299 #endif
300
301 #if CONFIG_USART
302 struct {
303     uint32_t base;
304     uint32_t irq;
305 }
306 const sg_usart_config[CONFIG_USART_NUM] = {
307     {CSKY_UART0_BASE, UART0_IRQn},
308     {CSKY_UART1_BASE, UART1_IRQn},
309     {CSKY_UART2_BASE, UART2_IRQn},
310 };
311 typedef struct {
312     pin_t    tx;
313     pin_t    rx;
314     pin_t    cts;
315     pin_t    rts;
316     uint16_t cfg_idx;    //idx of sg_usart_config[]
317     uint16_t function;
318 } usart_pin_map_t;
319 const static usart_pin_map_t s_usart_pin_map[] = {
320     {
321         PA2_TXD0_SPI0MISO,
322         PA3_RXD0_SPI0MOSI,
323         -1,
324         -1,
325         0,
326         0
327     },
328     {
329         PA10_ADC2_TXD0,
330         PA11_ACMP0N_ADC3_RXD0,
331         -1,
332         -1,
333         0,
334         2
335     },
336     {
337         PA17_TXD1_ADC9,
338         PA16_RXD1_ADC8,
339         -1,
340         -1,
341         1,
342         0
343     },
344     {
345         PA21_SPI1SCK_TRIG1_TXD1,
346         PA20_SPI1SSN2_TRIG0_RXD1,
347         -1,
348         -1,
349         1,
350         2,
351     },
352     {
353         PA24_TXD2_I2SMCLK_SPI1SSN0,
354         PA25_RXD2_I2SSCK_SPI1SSN1,
355         PA26_CTS2_I2SWS_ADC12,
356         PA27_RTS2_I2SSD_ADC13,
357         2,
358         0
359     },
360 };
361
362 /**
363   \param[in]   instance idx, must not exceed return value of target_get_usart_count()
364   \brief       get usart instance.
365   \return      pointer to usart instance
366 */
367 int32_t target_usart_init(pin_t tx, pin_t rx, uint32_t *base, uint32_t *irq)
368 {
369     uint32_t idx;
370
371     for (idx = 0; idx < sizeof(s_usart_pin_map) / sizeof(usart_pin_map_t); idx++) {
372         if (s_usart_pin_map[idx].tx == tx && s_usart_pin_map[idx].rx == rx) {
373             *base = sg_usart_config[s_usart_pin_map[idx].cfg_idx].base;
374             *irq = sg_usart_config[s_usart_pin_map[idx].cfg_idx].irq;
375             /*pinmux*/
376             pin_mux(s_usart_pin_map[idx].tx, s_usart_pin_map[idx].function);
377             pin_mux(s_usart_pin_map[idx].rx, s_usart_pin_map[idx].function);
378             return s_usart_pin_map[idx].cfg_idx;
379         }
380     }
381
382     return -1;
383
384 }
385 /**
386   \brief       control usart flow.
387   \param[in]   tx_flow  The TX flow pin name
388   \param[in]   rx_flow  The RX flow pin name
389   \param[in]   flag 0-disable, 1-enable.
390   \return      0 if setting ready ,negative for error code
391 */
392 int32_t target_usart_flowctrl_init(pin_t tx_flow, pin_t rx_flow, uint32_t flag)
393 {
394     uint32_t idx;
395
396     for (idx = 0; idx < sizeof(s_usart_pin_map) / sizeof(usart_pin_map_t); idx++) {
397         if ((s_usart_pin_map[idx].cts == tx_flow) &&(s_usart_pin_map[idx].rts == rx_flow))
398             break;
399     }
400
401     if (idx >= sizeof(s_usart_pin_map) / sizeof(usart_pin_map_t)) {
402         return -1;
403     }
404
405     if ((s_usart_pin_map[idx].cts == tx_flow) && flag) {
406         pin_mux(s_usart_pin_map[idx].cts, s_usart_pin_map[idx].function);
407     } else if ((s_usart_pin_map[idx].cts == tx_flow) && (flag == 0)) {
408         pin_mux(s_usart_pin_map[idx].cts, 0xff);
409     } else {
410         return -1;
411     }
412
413     if ((s_usart_pin_map[idx].rts == rx_flow) && flag) {
414         pin_mux(s_usart_pin_map[idx].rts, s_usart_pin_map[idx].function);
415     } else if ((s_usart_pin_map[idx].rts == rx_flow) && (flag == 0)) {
416         pin_mux(s_usart_pin_map[idx].rts, 0xff);
417     } else {
418         return -1;
419     }
420
421     return 0;
422 }
423
424 #endif
425
426 #if CONFIG_SPI
427 struct {
428     uint32_t base;
429     uint32_t irq;
430 }
431
432 const sg_spi_config[CONFIG_SPI_NUM] = {
433     {CSKY_SPI0_BASE, SPI0_IRQn},
434     {CSKY_SPI1_BASE, SPI1_IRQn}
435 };
436 typedef struct {
437     pin_t    mosi;
438     pin_t    miso;
439     pin_t    sclk;
440     pin_t    ssel;
441     uint32_t cfg_idx;    //idx of sg_iic_config[]
442     uint16_t function;
443 } spi_pin_map_t;
444 const static spi_pin_map_t s_spi_pin_map[] = {
445     {
446         PA2_TXD0_SPI0MISO,
447         PA3_RXD0_SPI0MOSI,
448         PA4_CTS0_PWM0_SPI0SCK_TRIG0,
449         PA5_RTS0_PWM1_SPI0SSN_TRIG1,
450         0,
451         2
452     },
453     {
454         PB3_SPI0MISO_PWM5_I2SSD,
455         PA6_SPI0MOSI_PWM6_SCL0,
456         PB2_SPI0SCK_PWM4_I2SWS,
457         PA7_SPI0SSN_PWM7_SDA0,
458         0,
459         0
460     },
461     {
462         PA22_SPI1MISO_PWM0_ADC10,
463         PA23_SPI1MOSI_PWM1_ADC11,
464         PA21_SPI1SCK_TRIG1_TXD1,
465         PA18_SPI1SSN0_ACMP0O,
466         1,
467         0
468     }
469 };
470
471 /**
472   \param[in]   instance idx, must not exceed return value of target_get_spi_count()
473   \brief       get spi instance.
474   \return      pointer to spi instance
475 */
476 int32_t target_spi_init(pin_t mosi, pin_t miso, pin_t sclk, pin_t ssel, uint32_t *base, uint32_t *irq)
477 {
478     uint32_t idx;
479
480     for (idx = 0; idx < sizeof(s_spi_pin_map) / sizeof(spi_pin_map_t); idx++) {
481         if (s_spi_pin_map[idx].mosi == mosi && s_spi_pin_map[idx].miso == miso
482             && s_spi_pin_map[idx].sclk == sclk && s_spi_pin_map[idx].ssel == ssel) {
483             *base = sg_spi_config[s_spi_pin_map[idx].cfg_idx].base;
484             *irq = sg_spi_config[s_spi_pin_map[idx].cfg_idx].irq;
485             /*pinmux*/
486             pin_mux(s_spi_pin_map[idx].mosi, s_spi_pin_map[idx].function);
487             pin_mux(s_spi_pin_map[idx].miso, s_spi_pin_map[idx].function);
488             pin_mux(s_spi_pin_map[idx].sclk, s_spi_pin_map[idx].function);
489             pin_mux(s_spi_pin_map[idx].ssel, s_spi_pin_map[idx].function);
490
491             return s_spi_pin_map[idx].cfg_idx;
492         }
493     }
494
495     return -1;
496
497 }
498
499 #endif
500
501 #if CONFIG_AES
502 struct {
503     uint32_t base;
504     uint32_t irq;
505 }
506 const sg_aes_config[CONFIG_AES_NUM] = {
507     {CSKY_AES_BASE, AES_IRQn}
508 };
509
510 /**
511   \brief       get aes instance count.
512   \return      aes instance count
513 */
514 int32_t target_get_aes_count(void)
515 {
516     return CONFIG_AES_NUM;
517 }
518
519 /**
520   \param[in]   instance idx, must not exceed return value of target_get_aes_count()
521   \brief       get aes instance.
522   \return      pointer to aes instance
523 */
524 int32_t target_get_aes(int32_t idx, uint32_t *base, uint32_t *irq)
525 {
526     if (idx >= target_get_aes_count()) {
527         return NULL;
528     }
529
530     *base = sg_aes_config[idx].base;
531     *irq = sg_aes_config[idx].irq;
532     return idx;
533 }
534
535 #endif
536
537 #if CONFIG_RSA
538 struct {
539     uint32_t base;
540     uint32_t irq;
541 }
542 const sg_rsa_config[CONFIG_RSA_NUM] = {
543     {CSKY_RSA_BASE, RSA_IRQn}
544 };
545
546 /**
547   \brief       get rsa instance count.
548   \return      rsa instance count
549 */
550 int32_t target_get_rsa_count(void)
551 {
552     return CONFIG_RSA_NUM;
553 }
554
555 /**
556   \param[in]   instance idx, must not exceed return value of target_get_rsa_count()
557   \brief       get rsa instance.
558   \return      pointer to rsa instance
559 */
560 int32_t target_get_rsa(int32_t idx, uint32_t *base, uint32_t *irq)
561 {
562     if (idx >= target_get_rsa_count()) {
563         return NULL;
564     }
565
566     *base = sg_rsa_config[idx].base;
567     *irq = sg_rsa_config[idx].irq;
568     return idx;
569 }
570
571 #endif
572
573 #if CONFIG_EFLASH
574 struct {
575     uint32_t base;
576     eflash_info_t info;
577 }
578 const sg_eflash_config[CONFIG_EFLASH_NUM] = {
579     {CSKY_EFLASH_CONTROL_BASE, {0x10000000, 0x1003f800, 0x1fc}}
580 };
581
582 /**
583   \brief       get eflash instance count.
584   \return      eflash instance count
585 */
586 int32_t target_get_eflash_count(void)
587 {
588     return CONFIG_EFLASH_NUM;
589 }
590
591 /**
592   \param[in]   instance idx, must not exceed return value of target_get_eflash_count()
593   \brief       get eflash instance.
594   \return      pointer to eflash instance
595 */
596 int32_t target_get_eflash(int32_t idx, uint32_t *base, eflash_info_t *info)
597 {
598     if (idx >= target_get_eflash_count()) {
599         return NULL;
600     }
601
602     *base = sg_eflash_config[idx].base;
603     info->start = sg_eflash_config[idx].info.start;
604     info->end = sg_eflash_config[idx].info.end;
605     info->sector_count = sg_eflash_config[idx].info.sector_count;
606     return idx;
607 }
608
609 #endif
610
611 #if CONFIG_WDT
612 struct {
613     uint32_t base;
614     uint32_t irq;
615 }
616 const sg_wdt_config[CONFIG_WDT_NUM] = {
617     {CSKY_WDT_BASE, WDT_IRQn}
618 };
619
620 int32_t target_get_wdt_count(void)
621 {
622     return CONFIG_WDT_NUM;
623 }
624
625 int32_t target_get_wdt(int32_t idx, uint32_t *base, uint32_t *irq)
626 {
627     if (idx >= target_get_wdt_count()) {
628         return NULL;
629     }
630
631     *base = sg_wdt_config[idx].base;
632     *irq = sg_wdt_config[idx].irq;
633     return idx;
634 }
635 #endif
636
637 #if CONFIG_DMAC
638 struct {
639     uint32_t base;
640     uint32_t irq;
641 }
642 const sg_dmac_config[CONFIG_DMAC_NUM] = {
643     {CSKY_DMAC0_BASE, SEU_DMAC_IRQn},
644     {CSKY_DMAC1_BASE, NONSEU_DMAC_IRQn}
645 };
646
647 int32_t target_get_dmac_count(void)
648 {
649     return CONFIG_DMAC_NUM;
650 }
651
652 int32_t target_get_dmac(int32_t idx, uint32_t *base, uint32_t *irq)
653 {
654     if (idx >= target_get_dmac_count()) {
655         return NULL;
656     }
657
658     *base = sg_dmac_config[idx].base;
659     *irq = sg_dmac_config[idx].irq;
660     return idx;
661 }
662 #endif
663
664 #if CONFIG_IIC
665
666
667 struct {
668     uint32_t base;
669     uint32_t irq;
670 }
671 const sg_iic_config[CONFIG_IIC_NUM] = {
672     {CSKY_I2C0_BASE, I2C0_IRQn},
673     {CSKY_I2C1_BASE, I2C1_IRQn}
674 };
675
676
677 typedef struct {
678     pin_t    scl;
679     pin_t    sda;
680     uint16_t cfg_idx;    //idx of sg_iic_config[]
681     uint16_t function;
682 } iic_pin_map_t;
683 const static iic_pin_map_t s_iic_pin_map[] = {
684     {
685         PB0_SCL0_PWM2_I2SMCLK,
686         PB1_SDA0_PWM3_I2SSCK,
687         0,
688         0
689     },
690     {
691         PA6_SPI0MOSI_PWM6_SCL0,
692         PA7_SPI0SSN_PWM7_SDA0,
693         0,
694         2
695     },
696     {
697         PC0_SCL1_CTS1_PWM10_ADC14,
698         PC1_SDA1_RTS1_PWM11_ADC15,
699         1,
700         0
701     }
702 };
703
704
705 /**
706   \param[in]   instance idx, must not exceed return value of target_get_iic_count()
707   \brief       get iic instance.
708   \return      pointer to iic instance
709 */
710 int32_t target_iic_init(pin_t scl, pin_t sda, uint32_t *base, uint32_t *irq)
711 {
712     uint32_t idx;
713
714     for (idx = 0; idx < sizeof(s_iic_pin_map) / sizeof(iic_pin_map_t); idx++) {
715         if (s_iic_pin_map[idx].scl == scl && s_iic_pin_map[idx].sda == sda) {
716             *base = sg_iic_config[s_iic_pin_map[idx].cfg_idx].base;
717             *irq = sg_iic_config[s_iic_pin_map[idx].cfg_idx].irq;
718             /*pinmux*/
719             if (s_iic_pin_map[idx].cfg_idx == 0) {
720                 pin_mux(s_iic_pin_map[idx].scl, s_iic_pin_map[idx].function);
721                 pin_mux(s_iic_pin_map[idx].sda, s_iic_pin_map[idx].function);
722             }
723             return s_iic_pin_map[idx].cfg_idx;
724         }
725     }
726
727     return -1;
728 }
729
730 #endif
731
732 #if CONFIG_PWM
733 struct {
734     uint32_t base;
735     uint32_t irq;
736 }
737 const sg_pwm_config[CONFIG_PWM_NUM] = {
738     {CSKY_PWM_BASE, PWM_IRQn},
739 };
740
741 typedef struct {
742     pin_t    pwm_pin;
743     uint32_t cfg_idx;    //idx of sg_pwm_config[]
744     uint32_t ch_num;
745     uint16_t function;
746 } pwm_pin_map_t;
747 const static pwm_pin_map_t s_pwm_pin_map[] = {
748     {PA4_CTS0_PWM0_SPI0SCK_TRIG0, 0, 0, 1},
749     {PA5_RTS0_PWM1_SPI0SSN_TRIG1, 0, 0, 1},
750     {PB0_SCL0_PWM2_I2SMCLK, 0, 1, 1},
751     {PB1_SDA0_PWM3_I2SSCK, 0, 1, 1},
752
753     {PB2_SPI0SCK_PWM4_I2SWS, 0, 2, 1},
754     {PB3_SPI0MISO_PWM5_I2SSD, 0, 2, 1},
755     {PA6_SPI0MOSI_PWM6_SCL0, 0, 3, 1},
756     {PA7_SPI0SSN_PWM7_SDA0, 0, 3, 1},
757
758     {PA12_PWM8_TCK_ADC4, 0, 4, 0},
759     {PA13_PWM9_TMS_ADC5, 0, 4, 0},
760     {PA14_PWM10_ADC6, 0, 5, 0},
761     {PA15_PWM11_ADC7, 0, 5, 0},
762
763     {PA22_SPI1MISO_PWM0_ADC10, 0, 0, 1},
764     {PA23_SPI1MOSI_PWM1_ADC11, 0, 0, 1},
765     {PC0_SCL1_CTS1_PWM10_ADC14, 0, 5, 2},
766     {PC1_SDA1_RTS1_PWM11_ADC15, 0, 5, 2}
767
768 };
769
770 /**
771   \param[in]   instance idx, must not exceed return value of target_get_pwm_count()
772   \brief       get pwm instance.
773   \return      pointer to pwm instance
774 */
775 int32_t target_pwm_init(pin_t pwm_pin, uint32_t *ch_num, uint32_t *base, uint32_t *irq)
776 {
777     uint32_t idx;
778
779     for (idx = 0; idx < sizeof(s_pwm_pin_map) / sizeof(pwm_pin_map_t); idx++) {
780         if (s_pwm_pin_map[idx].pwm_pin == pwm_pin) {
781             *base = sg_pwm_config[s_pwm_pin_map[idx].cfg_idx].base;
782             *irq = sg_pwm_config[s_pwm_pin_map[idx].cfg_idx].irq;
783             *ch_num = s_pwm_pin_map[idx].ch_num;
784             /*pinmux*/
785             pin_mux(s_pwm_pin_map[idx].pwm_pin, s_pwm_pin_map[idx].function);
786             return s_pwm_pin_map[idx].cfg_idx;
787         }
788     }
789
790     return -1;
791 }
792
793 #endif
794
795 #if CONFIG_SHA
796 struct {
797     uint32_t base;
798     uint32_t irq;
799 }
800 const sg_sha_config[CONFIG_SHA_NUM] = {
801     {CSKY_SHA_BASE, SHA_IRQn}
802 };
803
804 /**
805   \brief       get sha instance count.
806   \return      sha instance count
807 */
808 int32_t target_get_sha_count(void)
809 {
810     return CONFIG_SHA_NUM;
811 }
812
813 /**
814   \param[in]   instance idx, must not exceed return value of target_get_sha_count()
815   \brief       get sha instance.
816   \return      pointer to sha instance
817 */
818 int32_t target_get_sha(int32_t idx, uint32_t *base, uint32_t *irq)
819 {
820     if (idx >= target_get_sha_count()) {
821         return NULL;
822     }
823
824     *base = sg_sha_config[idx].base;
825     *irq = sg_sha_config[idx].irq;
826     return idx;
827 }
828
829 #endif