123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872 |
- #include "gd32f30x_usart.h"
- #define GP_GUAT_OFFSET ((uint32_t)8U)
- #define CTL3_SCRTNUM_OFFSET ((uint32_t)1U)
- #define RT_BL_OFFSET ((uint32_t)24U)
- void usart_deinit(uint32_t usart_periph)
- {
- switch(usart_periph){
- case USART0:
-
- rcu_periph_reset_enable(RCU_USART0RST);
- rcu_periph_reset_disable(RCU_USART0RST);
- break;
- case USART1:
-
- rcu_periph_reset_enable(RCU_USART1RST);
- rcu_periph_reset_disable(RCU_USART1RST);
- break;
- case USART2:
-
- rcu_periph_reset_enable(RCU_USART2RST);
- rcu_periph_reset_disable(RCU_USART2RST);
- break;
- case UART3:
-
- rcu_periph_reset_enable(RCU_UART3RST);
- rcu_periph_reset_disable(RCU_UART3RST);
- break;
- case UART4:
-
- rcu_periph_reset_enable(RCU_UART4RST);
- rcu_periph_reset_disable(RCU_UART4RST);
- break;
- default:
- break;
- }
- }
-
- void usart_baudrate_set(uint32_t usart_periph, uint32_t baudval)
- {
- uint32_t uclk=0U, intdiv=0U, fradiv=0U, udiv=0U;
- switch(usart_periph){
-
- case USART0:
-
- uclk = rcu_clock_freq_get(CK_APB2);
- break;
- case USART1:
-
- uclk = rcu_clock_freq_get(CK_APB1);
- break;
- case USART2:
-
- uclk = rcu_clock_freq_get(CK_APB1);
- break;
- case UART3:
-
- uclk = rcu_clock_freq_get(CK_APB1);
- break;
- case UART4:
-
- uclk = rcu_clock_freq_get(CK_APB1);
- break;
- default:
- break;
- }
-
- udiv = (uclk + baudval / 2U) / baudval;
- intdiv = udiv & 0xfff0U;
- fradiv = udiv & 0xfU;
- USART_BAUD(usart_periph) = ((USART_BAUD_FRADIV | USART_BAUD_INTDIV) & (intdiv | fradiv));
- }
- void usart_parity_config(uint32_t usart_periph, uint32_t paritycfg)
- {
-
- USART_CTL0(usart_periph) &= ~(USART_CTL0_PM | USART_CTL0_PCEN);
-
- USART_CTL0(usart_periph) |= paritycfg ;
- }
- void usart_word_length_set(uint32_t usart_periph, uint32_t wlen)
- {
-
- USART_CTL0(usart_periph) &= ~USART_CTL0_WL;
-
- USART_CTL0(usart_periph) |= wlen;
- }
- void usart_stop_bit_set(uint32_t usart_periph, uint32_t stblen)
- {
-
- USART_CTL1(usart_periph) &= ~USART_CTL1_STB;
-
- USART_CTL1(usart_periph) |= stblen;
- }
- void usart_enable(uint32_t usart_periph)
- {
- USART_CTL0(usart_periph) |= USART_CTL0_UEN;
- }
- void usart_disable(uint32_t usart_periph)
- {
- USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
- }
- void usart_transmit_config(uint32_t usart_periph, uint32_t txconfig)
- {
- USART_CTL0(usart_periph) &= ~(USART_CTL0_TEN);
- USART_CTL0(usart_periph) |= (USART_CTL0_TEN & txconfig);
- }
- void usart_receive_config(uint32_t usart_periph, uint32_t rxconfig)
- {
- USART_CTL0(usart_periph) &= ~(USART_CTL0_REN);
- USART_CTL0(usart_periph) |= (USART_CTL0_REN & rxconfig);
- }
- void usart_data_first_config(uint32_t usart_periph, uint32_t msbf)
- {
- USART_CTL3(usart_periph) &= ~(USART_CTL3_MSBF);
- USART_CTL3(usart_periph) |= msbf;
- }
- void usart_invert_config(uint32_t usart_periph, usart_invert_enum invertpara)
- {
-
- switch(invertpara){
- case USART_DINV_ENABLE:
-
- USART_CTL3(usart_periph) |= USART_CTL3_DINV;
- break;
- case USART_TXPIN_ENABLE:
-
- USART_CTL3(usart_periph) |= USART_CTL3_TINV;
- break;
- case USART_RXPIN_ENABLE:
-
- USART_CTL3(usart_periph) |= USART_CTL3_RINV;
- break;
- case USART_DINV_DISABLE:
-
- USART_CTL3(usart_periph) &= ~(USART_CTL3_DINV);
- break;
- case USART_TXPIN_DISABLE:
-
- USART_CTL3(usart_periph) &= ~(USART_CTL3_TINV);
- break;
- case USART_RXPIN_DISABLE:
-
- USART_CTL3(usart_periph) &= ~(USART_CTL3_RINV);
- break;
- default:
- break;
- }
- }
- void usart_receiver_timeout_enable(uint32_t usart_periph)
- {
- USART_CTL3(usart_periph) |= USART_CTL3_RTEN;
- }
- void usart_receiver_timeout_disable(uint32_t usart_periph)
- {
- USART_CTL3(usart_periph) &= ~(USART_CTL3_RTEN);
- }
- void usart_receiver_timeout_threshold_config(uint32_t usart_periph, uint32_t rtimeout)
- {
- USART_RT(usart_periph) &= ~(USART_RT_RT);
- USART_RT(usart_periph) |= rtimeout;
- }
- void usart_data_transmit(uint32_t usart_periph, uint16_t data)
- {
- USART_DATA(usart_periph) = USART_DATA_DATA & (uint32_t)data;
- }
- uint16_t usart_data_receive(uint32_t usart_periph)
- {
- return (uint16_t)(GET_BITS(USART_DATA(usart_periph), 0U, 8U));
- }
- void usart_address_config(uint32_t usart_periph, uint8_t addr)
- {
- USART_CTL1(usart_periph) &= ~(USART_CTL1_ADDR);
- USART_CTL1(usart_periph) |= (USART_CTL1_ADDR & (uint32_t)addr);
- }
- void usart_mute_mode_enable(uint32_t usart_periph)
- {
- USART_CTL0(usart_periph) |= USART_CTL0_RWU;
- }
- void usart_mute_mode_disable(uint32_t usart_periph)
- {
- USART_CTL0(usart_periph) &= ~(USART_CTL0_RWU);
- }
- void usart_mute_mode_wakeup_config(uint32_t usart_periph, uint32_t wmethod)
- {
- USART_CTL0(usart_periph) &= ~(USART_CTL0_WM);
- USART_CTL0(usart_periph) |= wmethod;
- }
- void usart_lin_mode_enable(uint32_t usart_periph)
- {
- USART_CTL1(usart_periph) |= USART_CTL1_LMEN;
- }
- void usart_lin_mode_disable(uint32_t usart_periph)
- {
- USART_CTL1(usart_periph) &= ~(USART_CTL1_LMEN);
- }
- void usart_lin_break_detection_length_config(uint32_t usart_periph, uint32_t lblen)
- {
- USART_CTL1(usart_periph) &= ~(USART_CTL1_LBLEN);
- USART_CTL1(usart_periph) |= (USART_CTL1_LBLEN & lblen);
- }
- void usart_send_break(uint32_t usart_periph)
- {
- USART_CTL0(usart_periph) |= USART_CTL0_SBKCMD;
- }
- void usart_halfduplex_enable(uint32_t usart_periph)
- {
- USART_CTL2(usart_periph) |= USART_CTL2_HDEN;
- }
- void usart_halfduplex_disable(uint32_t usart_periph)
- {
- USART_CTL2(usart_periph) &= ~(USART_CTL2_HDEN);
- }
- void usart_synchronous_clock_enable(uint32_t usart_periph)
- {
- USART_CTL1(usart_periph) |= USART_CTL1_CKEN;
- }
- void usart_synchronous_clock_disable(uint32_t usart_periph)
- {
- USART_CTL1(usart_periph) &= ~(USART_CTL1_CKEN);
- }
- void usart_synchronous_clock_config(uint32_t usart_periph, uint32_t clen, uint32_t cph, uint32_t cpl)
- {
- USART_CTL1(usart_periph) &= ~(USART_CTL1_CLEN | USART_CTL1_CPH | USART_CTL1_CPL);
- USART_CTL1(usart_periph) |= (USART_CTL1_CLEN & clen) | (USART_CTL1_CPH & cph) | (USART_CTL1_CPL & cpl);
- }
- void usart_guard_time_config(uint32_t usart_periph, uint8_t guat)
- {
- USART_GP(usart_periph) &= ~(USART_GP_GUAT);
- USART_GP(usart_periph) |= (USART_GP_GUAT & ((uint32_t)guat << GP_GUAT_OFFSET));
- }
- void usart_smartcard_mode_enable(uint32_t usart_periph)
- {
- USART_CTL2(usart_periph) |= USART_CTL2_SCEN;
- }
- void usart_smartcard_mode_disable(uint32_t usart_periph)
- {
- USART_CTL2(usart_periph) &= ~(USART_CTL2_SCEN);
- }
- void usart_smartcard_mode_nack_enable(uint32_t usart_periph)
- {
- USART_CTL2(usart_periph) |= USART_CTL2_NKEN;
- }
- void usart_smartcard_mode_nack_disable(uint32_t usart_periph)
- {
- USART_CTL2(usart_periph) &= ~(USART_CTL2_NKEN);
- }
- void usart_smartcard_autoretry_config(uint32_t usart_periph, uint8_t scrtnum)
- {
- USART_CTL3(usart_periph) &= ~(USART_CTL3_SCRTNUM);
- USART_CTL3(usart_periph) |= (USART_CTL3_SCRTNUM & ((uint32_t)scrtnum << CTL3_SCRTNUM_OFFSET));
- }
- void usart_block_length_config(uint32_t usart_periph, uint8_t bl)
- {
- USART_RT(usart_periph) &= ~(USART_RT_BL);
- USART_RT(usart_periph) |= (USART_RT_BL & ((uint32_t)bl << RT_BL_OFFSET));
- }
- void usart_irda_mode_enable(uint32_t usart_periph)
- {
- USART_CTL2(usart_periph) |= USART_CTL2_IREN;
- }
- void usart_irda_mode_disable(uint32_t usart_periph)
- {
- USART_CTL2(usart_periph) &= ~(USART_CTL2_IREN);
- }
- void usart_prescaler_config(uint32_t usart_periph, uint8_t psc)
- {
- USART_GP(usart_periph) &= ~(USART_GP_PSC);
- USART_GP(usart_periph) |= (uint32_t)psc;
- }
- void usart_irda_lowpower_config(uint32_t usart_periph, uint32_t irlp)
- {
- USART_CTL2(usart_periph) &= ~(USART_CTL2_IRLP);
- USART_CTL2(usart_periph) |= (USART_CTL2_IRLP & irlp);
- }
- void usart_hardware_flow_rts_config(uint32_t usart_periph, uint32_t rtsconfig)
- {
- USART_CTL2(usart_periph) &= ~(USART_CTL2_RTSEN);
- USART_CTL2(usart_periph) |= (USART_CTL2_RTSEN & rtsconfig);
- }
- void usart_hardware_flow_cts_config(uint32_t usart_periph, uint32_t ctsconfig)
- {
- USART_CTL2(usart_periph) &= ~(USART_CTL2_CTSEN);
- USART_CTL2(usart_periph) |= (USART_CTL2_CTSEN & ctsconfig);
- }
- void usart_dma_receive_config(uint32_t usart_periph, uint8_t dmacmd)
- {
- USART_CTL2(usart_periph) &= ~(USART_CTL2_DENR);
- USART_CTL2(usart_periph) |= (USART_CTL2_DENR & dmacmd);
- }
- void usart_dma_transmit_config(uint32_t usart_periph, uint8_t dmacmd)
- {
- USART_CTL2(usart_periph) &= ~(USART_CTL2_DENT);
- USART_CTL2(usart_periph) |= (USART_CTL2_DENT & dmacmd);
- }
- FlagStatus usart_flag_get(uint32_t usart_periph, usart_flag_enum flag)
- {
- if(RESET != (USART_REG_VAL(usart_periph, flag) & BIT(USART_BIT_POS(flag)))){
- return SET;
- }else{
- return RESET;
- }
- }
- void usart_flag_clear(uint32_t usart_periph, usart_flag_enum flag)
- {
- USART_REG_VAL(usart_periph, flag) = ~BIT(USART_BIT_POS(flag));
- }
- void usart_interrupt_enable(uint32_t usart_periph, usart_interrupt_enum interrupt)
- {
- USART_REG_VAL(usart_periph, interrupt) |= BIT(USART_BIT_POS(interrupt));
- }
- void usart_interrupt_disable(uint32_t usart_periph, usart_interrupt_enum interrupt)
- {
- USART_REG_VAL(usart_periph, interrupt) &= ~BIT(USART_BIT_POS(interrupt));
- }
- FlagStatus usart_interrupt_flag_get(uint32_t usart_periph, usart_interrupt_flag_enum int_flag)
- {
- uint32_t intenable = 0U, flagstatus = 0U;
-
- intenable = (USART_REG_VAL(usart_periph, int_flag) & BIT(USART_BIT_POS(int_flag)));
-
- flagstatus = (USART_REG_VAL2(usart_periph, int_flag) & BIT(USART_BIT_POS2(int_flag)));
- if((0U != flagstatus) && (0U != intenable)){
- return SET;
- }else{
- return RESET;
- }
- }
- void usart_interrupt_flag_clear(uint32_t usart_periph, usart_interrupt_flag_enum int_flag)
- {
- USART_REG_VAL2(usart_periph, int_flag) = ~BIT(USART_BIT_POS2(int_flag));
- }
|