2 * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /******************************************************************************
19 * @brief source file for the devices
22 ******************************************************************************/
25 #include <drv_usart.h>
30 #include <drv_eflash.h>
31 #include <drv_timer.h>
46 //typedef int32_t pin_t;
56 const sg_iic_config[CONFIG_IIC_NUM] = {
57 {CSKY_I2C0_BASE, I2C0_IRQn},
58 {CSKY_I2C1_BASE, I2C1_IRQn}
65 uint16_t cfg_idx; //idx of sg_iic_config[]
72 const static iic_pin_map_t s_iic_pin_map[] = {
74 PA4_SCL0_PWM4_SPI0RX_XX,
75 PA5_SDA0_PWM5_SPI0CS_XX,
80 PA6_SPI0CLK_PWMTRIG0_SCL0_XX,
81 PA7_SPI0TX_PWMTRIG1_SDA0_XX,
86 PA31_I2SSDA__SCL0_PWM4_XX,
87 PB0_ADC0_SDA0_PWM5_XX,
92 PA8_SPI0RX_TRIGFAULT_SCL1_XX,
93 PA9_SPI0CS_PWM0_SDA1_XX,
98 PA14_SCL1_PWM5_SPI1RX_XX,
99 PA15_SDA1_PWMTRIG0_SPI1CS0_XX,
104 PB1_ADC1_SCL1_USISCLK_XX,
105 PB2_ADC2_SDA1_USISD0_XX,
113 \param[in] instance idx, must not exceed return value of target_get_iic_count()
114 \brief get iic instance.
115 \return pointer to iic instance
117 int32_t target_iic_init(pin_t scl, pin_t sda, uint32_t *base, uint32_t *irq)
121 for (idx = 0; idx < sizeof(s_iic_pin_map) / sizeof(iic_pin_map_t); idx++) {
122 if (s_iic_pin_map[idx].scl == scl && s_iic_pin_map[idx].sda == sda) {
123 *base = sg_iic_config[s_iic_pin_map[idx].cfg_idx].base;
124 *irq = sg_iic_config[s_iic_pin_map[idx].cfg_idx].irq;
126 pin_mux(s_iic_pin_map[idx].scl, s_iic_pin_map[idx].function);
127 pin_mux(s_iic_pin_map[idx].sda, s_iic_pin_map[idx].function);
128 return s_iic_pin_map[idx].cfg_idx;
143 const sg_usart_config[CONFIG_USART_NUM] = {
144 {CSKY_UART0_BASE, UART0_IRQn},
145 {CSKY_UART1_BASE, UART1_IRQn},
146 {CSKY_UART2_BASE, UART2_IRQn},
147 {CSKY_UART3_BASE, UART3_IRQn}
154 uint16_t cfg_idx; //idx of sg_usart_config[]
157 const static usart_pin_map_t s_usart_pin_map[] = {
159 PA0_TXD0_PWM0_XX_SIROUT0,
160 PA1_RXD0_PWM1_XX_SIRIN0,
167 PA10_TXD1_PWM1_XX_SIROUT1,
168 PA11_RXD1_PWM2_XX_SIRIN1,
175 PA23_TXD2_PWM5_XX_SIROUT2,
176 PA22_RXD2_PWM4_XX_SIRIN2,
177 PA24_CTS2_PWMTRIG0_SPI1CS1_XX,
178 PA25_XX_PWMTRIG1_SPI1CS2_XX,
183 PA26_TXD3_PWMFAULT_XX_SIROUT3,
184 PA27_RXD3_PWM0_XX_SIRIN3,
193 \param[in] instance idx, must not exceed return value of target_get_usart_count()
194 \brief get usart instance.
195 \return pointer to usart instance
197 int32_t target_usart_init(pin_t tx, pin_t rx, uint32_t *base, uint32_t *irq)
201 for (idx = 0; idx < sizeof(s_usart_pin_map) / sizeof(usart_pin_map_t); idx++) {
202 if (s_usart_pin_map[idx].tx == tx && s_usart_pin_map[idx].rx == rx) {
203 *base = sg_usart_config[s_usart_pin_map[idx].cfg_idx].base;
204 *irq = sg_usart_config[s_usart_pin_map[idx].cfg_idx].irq;
206 pin_mux(s_usart_pin_map[idx].tx, s_usart_pin_map[idx].function);
207 pin_mux(s_usart_pin_map[idx].rx, s_usart_pin_map[idx].function);
208 return s_usart_pin_map[idx].cfg_idx;
216 \brief control usart flow.
217 \param[in] tx_flow The TX flow pin name
218 \param[in] rx_flow The RX flow pin name
219 \param[in] flag 0-disable, 1-enable.
220 \return 0 if setting ready ,negative for error code
222 int32_t target_usart_flowctrl_init(pin_t tx_flow, pin_t rx_flow, uint32_t flag)
226 for (idx = 0; idx < sizeof(s_usart_pin_map) / sizeof(usart_pin_map_t); idx++) {
227 if ((s_usart_pin_map[idx].cts == tx_flow) &&(s_usart_pin_map[idx].rts == rx_flow))
231 if (idx >= sizeof(s_usart_pin_map) / sizeof(usart_pin_map_t)) {
235 if ((s_usart_pin_map[idx].cts == tx_flow) && flag) {
236 pin_mux(s_usart_pin_map[idx].cts, s_usart_pin_map[idx].function);
237 } else if ((s_usart_pin_map[idx].cts == tx_flow) && (flag == 0)) {
238 pin_mux(s_usart_pin_map[idx].cts, 0xff);
243 if ((s_usart_pin_map[idx].rts == rx_flow) && flag) {
244 pin_mux(s_usart_pin_map[idx].rts, s_usart_pin_map[idx].function);
245 } else if ((s_usart_pin_map[idx].rts == rx_flow) && (flag == 0)) {
246 pin_mux(s_usart_pin_map[idx].rts, 0xff);
260 const sg_trng_config[CONFIG_TRNG_NUM] = {
265 \brief get trng instance count.
266 \return trng instance count
268 int32_t target_get_trng_count(void)
270 return CONFIG_TRNG_NUM;
274 \param[in] instance idx, must not exceed return value of target_get_trng_count()
275 \brief get trng instance.
276 \return pointer to trng instance
278 int32_t target_get_trng(int32_t idx, uint32_t *base)
280 if (idx >= target_get_trng_count()) {
284 *base = sg_trng_config[idx].base;
294 const sg_crc_config[CONFIG_CRC_NUM] = {
299 \brief get crc instance count.
300 \return crc instance count
302 int32_t target_get_crc_count(void)
304 return CONFIG_CRC_NUM;
308 \param[in] instance idx, must not exceed return value of target_get_crc_count()
309 \brief get crc instance.
310 \return pointer to crc instance
312 int32_t target_get_crc(int32_t idx, uint32_t *base)
314 if (idx >= target_get_crc_count()) {
318 *base = sg_crc_config[idx].base;
330 const sg_eflash_config[CONFIG_EFLASH_NUM] = {
331 {CSKY_EFLASH_CONTROL_BASE, {0x10000000, 0x1003f800, 0x1fc}}
335 \brief get eflash instance count.
336 \return eflash instance count
338 int32_t target_get_eflash_count(void)
340 return CONFIG_EFLASH_NUM;
344 \param[in] instance idx, must not exceed return value of target_get_eflash_count()
345 \brief get eflash instance.
346 \return pointer to eflash instance
348 int32_t target_get_eflash(int32_t idx, uint32_t *base, eflash_info *info)
350 if (idx >= target_get_eflash_count()) {
354 *base = sg_eflash_config[idx].base;
355 info->start = sg_eflash_config[idx].info.start;
356 info->end = sg_eflash_config[idx].info.end;
357 info->sector_count = sg_eflash_config[idx].info.sector_count;
368 const sg_timer_config[CONFIG_TIMER_NUM] = {
369 {CSKY_TIMERA0_BASE, TIMA0_IRQn},
370 {CSKY_TIMERA1_BASE, TIMA1_IRQn},
371 {CSKY_TIMERB0_BASE, TIMB0_IRQn},
372 {CSKY_TIMERB1_BASE, TIMB1_IRQn}
375 int32_t target_get_timer_count(void)
377 return CONFIG_TIMER_NUM;
380 int32_t target_get_timer(int32_t idx, uint32_t *base, uint32_t *irq)
382 if (idx >= target_get_timer_count()) {
386 *base = sg_timer_config[idx].base;
387 *irq = sg_timer_config[idx].irq;
400 const sg_gpio_config[CONFIG_GPIO_NUM] = {
401 {CSKY_GPIOA_BASE, GPIOA_IRQn, 32, PORTA},
402 {CSKY_GPIOB_BASE, GPIOB_IRQn, 11, PORTB}
407 uint32_t cfg_idx; //idx of sg_gpio_config[]
409 const static gpio_pin_map_t s_gpio_pin_map[] = {
410 {PA0_TXD0_PWM0_XX_SIROUT0, 0},
411 {PA1_RXD0_PWM1_XX_SIRIN0, 0},
412 {PA2_CTS0_PWM2_SPI0CLK_XX, 0},
413 {PA3_RTS0_PWM3_SPI0TX_XX, 0},
414 {PA4_SCL0_PWM4_SPI0RX_XX, 0},
415 {PA5_SDA0_PWM5_SPI0CS_XX, 0},
416 {PA6_SPI0CLK_PWMTRIG0_SCL0_XX, 0},
417 {PA7_SPI0TX_PWMTRIG1_SDA0_XX, 0},
418 {PA8_SPI0RX_TRIGFAULT_SCL1_XX, 0},
419 {PA9_SPI0CS_PWM0_SDA1_XX, 0},
420 {PA10_TXD1_PWM1_XX_SIROUT1, 0},
421 {PA11_RXD1_PWM2_XX_SIRIN1, 0},
422 {PA12_CTS1_PWM3_SPI1CLK_XX, 0},
423 {PA13_RTS1_PWM4_SPI1TX_XX, 0},
424 {PA14_SCL1_PWM5_SPI1RX_XX, 0},
425 {PA15_SDA1_PWMTRIG0_SPI1CS0_XX, 0},
426 {PA16_SPI1CLK_PWMTRIG1_XX_XX, 0},
427 {PA17_SPI1TX_PWMFAULT_XX_XX, 0},
428 {PA18_SPI1RX_PWM0_XX_XX, 0},
429 {PA19_SPI1CS0_PWM1_XX_XX, 0},
430 {PA20_SPI1CS1_PWM2_XX_XX, 0},
431 {PA21_SPI1CS2_PWM3_XX_XX, 0},
432 {PA22_RXD2_PWM4_XX_SIRIN2, 0},
433 {PA23_TXD2_PWM5_XX_SIROUT2, 0},
434 {PA24_CTS2_PWMTRIG0_SPI1CS1_XX, 0},
435 {PA25_XX_PWMTRIG1_SPI1CS2_XX, 0},
436 {PA26_TXD3_PWMFAULT_XX_SIROUT3, 0},
437 {PA27_RXD3_PWM0_XX_SIRIN3, 0},
438 {PA28_I2SMCLK_PWM1_XX_XX, 0},
439 {PA29_I2SSCLK_PWM2_XX_XX, 0},
440 {PA30_I2SWSCLK_PWM3_XX_XX, 0},
441 {PA31_I2SSDA__SCL0_PWM4_XX, 0},
442 {PB0_ADC0_SDA0_PWM5_XX, 1},
443 {PB1_ADC1_SCL1_USISCLK_XX, 1},
444 {PB2_ADC2_SDA1_USISD0_XX, 1},
445 {PB3_ADC3_SPI1CLK_USISD1_XX, 1},
446 {PB4_ADC4_SPI1TX_USINSS_XX, 1},
447 {PB5_ADC5_SPI1RX_USISCLK_XX, 1},
448 {PB6_ADC6_SPI1CS0_USISD0_XX, 1},
449 {PB7_ADC7_SPI1CS1_USISD1_XX, 1},
450 {PB8_PWMTRIG0_SPI1CS2_USINSS_XX, 1},
451 {PB9_PWMTRIG1_CTS3_XX_XX, 1},
452 {PB10_PWMFAULT_RTS3_XX_XX, 1}
455 int32_t target_gpio_port_init(port_name_t port, uint32_t *base, uint32_t *irq, uint32_t *pin_num)
459 for (i = 0; i < CONFIG_GPIO_NUM; i++) {
460 if (sg_gpio_config[i].port == port) {
461 *base = sg_gpio_config[i].base;
462 *irq = sg_gpio_config[i].irq;
463 *pin_num = sg_gpio_config[i].pin_num;
472 \param[in] instance idx, must not exceed return value of target_get_gpio_count()
473 \brief get gpio instance.
474 \return pointer to gpio instance
476 int32_t target_gpio_pin_init(pin_t gpio_pin, uint32_t *port_idx)
480 for (idx = 0; idx < sizeof(s_gpio_pin_map) / sizeof(gpio_pin_map_t); idx++) {
481 if (s_gpio_pin_map[idx].gpio_pin == gpio_pin) {
482 *port_idx = s_gpio_pin_map[idx].cfg_idx;
484 pin_mux(s_gpio_pin_map[idx].gpio_pin, 0xff);
502 const sg_aes_config[CONFIG_AES_NUM] = {
503 {CSKY_AES_BASE, AES_IRQn}
507 \brief get aes instance count.
508 \return aes instance count
510 int32_t target_get_aes_count(void)
512 return CONFIG_AES_NUM;
516 \param[in] instance idx, must not exceed return value of target_get_aes_count()
517 \brief get aes instance.
518 \return pointer to aes instance
520 int32_t target_get_aes(int32_t idx, uint32_t *base, uint32_t *irq)
522 if (idx >= target_get_aes_count()) {
526 *base = sg_aes_config[idx].base;
527 *irq = sg_aes_config[idx].irq;
539 const sg_rsa_config[CONFIG_RSA_NUM] = {
540 {CSKY_RSA_BASE, RSA_IRQn}
544 \brief get rsa instance count.
545 \return rsa instance count
547 int32_t target_get_rsa_count(void)
549 return CONFIG_RSA_NUM;
553 \param[in] instance idx, must not exceed return value of target_get_rsa_count()
554 \brief get rsa instance.
555 \return pointer to rsa instance
557 int32_t target_get_rsa(int32_t idx, uint32_t *base, uint32_t *irq)
559 if (idx >= target_get_rsa_count()) {
563 *base = sg_rsa_config[idx].base;
564 *irq = sg_rsa_config[idx].irq;
575 const sg_rtc_config[CONFIG_RTC_NUM] = {
576 {CSKY_RTC_BASE, RTC_IRQn}
579 int32_t target_get_rtc_count(void)
581 return CONFIG_RTC_NUM;
584 int32_t target_get_rtc(int32_t idx, uint32_t *base, uint32_t *irq)
586 if (idx >= target_get_rtc_count()) {
590 *base = sg_rtc_config[idx].base;
591 *irq = sg_rtc_config[idx].irq;
603 const sg_spi_config[CONFIG_SPI_NUM] = {
604 {CSKY_SPI0_BASE, SPI0_IRQn},
605 {CSKY_SPI1_BASE, SPI1_IRQn}
612 uint32_t cfg_idx; //idx of sg_iic_config[]
615 const static spi_pin_map_t s_spi_pin_map[] = {
617 PA7_SPI0TX_PWMTRIG1_SDA0_XX,
618 PA8_SPI0RX_TRIGFAULT_SCL1_XX,
619 PA6_SPI0CLK_PWMTRIG0_SCL0_XX,
620 PA9_SPI0CS_PWM0_SDA1_XX,
625 PA17_SPI1TX_PWMFAULT_XX_XX,
626 PA18_SPI1RX_PWM0_XX_XX,
627 PA16_SPI1CLK_PWMTRIG1_XX_XX,
628 PA19_SPI1CS0_PWM1_XX_XX,
633 PA13_RTS1_PWM4_SPI1TX_XX,
634 PA14_SCL1_PWM5_SPI1RX_XX,
635 PA12_CTS1_PWM3_SPI1CLK_XX,
636 PA15_SDA1_PWMTRIG0_SPI1CS0_XX,
641 PB4_ADC4_SPI1TX_USINSS_XX,
642 PB5_ADC5_SPI1RX_USISCLK_XX,
643 PB3_ADC3_SPI1CLK_USISD1_XX,
644 PB6_ADC6_SPI1CS0_USISD0_XX,
651 \param[in] instance idx, must not exceed return value of target_get_spi_count()
652 \brief get spi instance.
653 \return pointer to spi instance
655 int32_t target_spi_init(pin_t mosi, pin_t miso, pin_t sclk, pin_t ssel, uint32_t *base, uint32_t *irq)
659 for (idx = 0; idx < sizeof(s_spi_pin_map) / sizeof(spi_pin_map_t); idx++) {
660 if (s_spi_pin_map[idx].mosi == mosi && s_spi_pin_map[idx].miso == miso
661 && s_spi_pin_map[idx].sclk == sclk && s_spi_pin_map[idx].ssel == ssel) {
662 *base = sg_spi_config[s_spi_pin_map[idx].cfg_idx].base;
663 *irq = sg_spi_config[s_spi_pin_map[idx].cfg_idx].irq;
665 pin_mux(s_spi_pin_map[idx].mosi, s_spi_pin_map[idx].function);
666 pin_mux(s_spi_pin_map[idx].miso, s_spi_pin_map[idx].function);
667 pin_mux(s_spi_pin_map[idx].sclk, s_spi_pin_map[idx].function);
668 pin_mux(s_spi_pin_map[idx].ssel, s_spi_pin_map[idx].function);
670 return s_spi_pin_map[idx].cfg_idx;
685 const sg_wdt_config[CONFIG_WDT_NUM] = {
686 {CSKY_WDT_BASE, WDT_IRQn}
689 int32_t target_get_wdt_count(void)
691 return CONFIG_WDT_NUM;
694 int32_t target_get_wdt(int32_t idx, uint32_t *base, uint32_t *irq)
696 if (idx >= target_get_wdt_count()) {
700 *base = sg_wdt_config[idx].base;
701 *irq = sg_wdt_config[idx].irq;
712 const sg_sha_config[CONFIG_SHA_NUM] = {
713 {CSKY_SHA_BASE, SHA_IRQn}
717 \brief get sha instance count.
718 \return sha instance count
720 int32_t target_get_sha_count(void)
722 return CONFIG_SHA_NUM;
726 \param[in] instance idx, must not exceed return value of target_get_sha_count()
727 \brief get sha instance.
728 \return pointer to sha instance
730 int32_t target_get_sha(int32_t idx, uint32_t *base, uint32_t *irq)
732 if (idx >= target_get_sha_count()) {
736 *base = sg_sha_config[idx].base;
737 *irq = sg_sha_config[idx].irq;
748 const sg_pwm_config[CONFIG_PWM_NUM] = {
749 {CSKY_PWM_BASE, PWM_IRQn},
754 uint32_t cfg_idx; //idx of sg_pwm_config[]
758 const static pwm_pin_map_t s_pwm_pin_map[] = {
759 {PA0_TXD0_PWM0_XX_SIROUT0, 0, 0, 1},
760 {PA9_SPI0CS_PWM0_SDA1_XX, 0, 0, 1},
761 {PA18_SPI1RX_PWM0_XX_XX, 0, 0, 1},
762 {PA27_RXD3_PWM0_XX_SIRIN3, 0, 0, 1},
764 {PA1_RXD0_PWM1_XX_SIRIN0, 0, 1, 1},
765 {PA10_TXD1_PWM1_XX_SIROUT1, 0, 1, 1},
766 {PA19_SPI1CS0_PWM1_XX_XX, 0, 1, 1},
767 {PA28_I2SMCLK_PWM1_XX_XX, 0, 1, 1},
769 {PA2_CTS0_PWM2_SPI0CLK_XX, 0, 2, 1},
770 {PA11_RXD1_PWM2_XX_SIRIN1, 0, 2, 1},
771 {PA20_SPI1CS1_PWM2_XX_XX, 0, 2, 1},
772 {PA29_I2SSCLK_PWM2_XX_XX, 0, 2, 1},
774 {PA3_RTS0_PWM3_SPI0TX_XX, 0, 3, 1},
775 {PA12_CTS1_PWM3_SPI1CLK_XX, 0, 3, 1},
776 {PA21_SPI1CS2_PWM3_XX_XX, 0, 3, 1},
777 {PA30_I2SWSCLK_PWM3_XX_XX, 0, 3, 1},
779 {PA4_SCL0_PWM4_SPI0RX_XX, 0, 4, 1},
780 {PA13_RTS1_PWM4_SPI1TX_XX, 0, 4, 1},
781 {PA22_RXD2_PWM4_XX_SIRIN2, 0, 4, 1},
782 {PA31_I2SSDA__SCL0_PWM4_XX, 0, 4, 1},
784 {PA5_SDA0_PWM5_SPI0CS_XX, 0, 5, 1},
785 {PA14_SCL1_PWM5_SPI1RX_XX, 0, 5, 1},
786 {PA23_TXD2_PWM5_XX_SIROUT2, 0, 5, 1},
787 {PB0_ADC0_SDA0_PWM5_XX, 0, 5, 1},
791 \param[in] instance idx, must not exceed return value of target_get_pwm_count()
792 \brief get pwm instance.
793 \return pointer to pwm instance
795 int32_t target_pwm_init(pin_t pwm_pin, uint32_t *ch_num, uint32_t *base, uint32_t *irq)
799 for (idx = 0; idx < sizeof(s_pwm_pin_map) / sizeof(pwm_pin_map_t); idx++) {
800 if (s_pwm_pin_map[idx].pwm_pin == pwm_pin) {
801 *base = sg_pwm_config[s_pwm_pin_map[idx].cfg_idx].base;
802 *irq = sg_pwm_config[s_pwm_pin_map[idx].cfg_idx].irq;
803 *ch_num = s_pwm_pin_map[idx].ch_num;
805 pin_mux(s_pwm_pin_map[idx].pwm_pin, s_pwm_pin_map[idx].function);
806 return s_pwm_pin_map[idx].cfg_idx;
823 const sg_dmac_config[CONFIG_DMAC_NUM] = {
824 {CSKY_DMA_BASE, DMAC_IRQn},
827 int32_t target_get_dmac_count(void)
829 return CONFIG_DMAC_NUM;
832 int32_t target_get_dmac(int32_t idx, uint32_t *base, uint32_t *irq)
834 if (idx >= target_get_dmac_count()) {
838 *base = sg_dmac_config[idx].base;
839 *irq = sg_dmac_config[idx].irq;