- /**************************************************************************//**
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V1.00
- * [url=home.php?mod=space&uid=247401]@brief[/url] Show how to use GPIO input/output to control ARGB2 strip.
- *
- * SPDX-License-Identifier: Apache-2.0
- * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2022 Nuvoton Technology Corp. All rights reserved.
- ******************************************************************************/
- #include "stdio.h"
- #include "NuMicro.h"
- #include "Nuvoton_ARGB2_Strip.h"
- /*---------------------------------------------------------------------------------------------------------*/
- /* Global variables */
- /*---------------------------------------------------------------------------------------------------------*/
- volatile int32_t g_i32Wait = TRUE;
- int FeedbackMode_LEDcnt = 0;
- int ch;
- char MODE = '0'; //Which MODE to run, 0: Strip_GRBLoop()
- // 1: Strip_rainbowCycle()
- // 2: Strip_theaterChaseRainbow()
- // 3: Strip_feedbackMode()
- // 4: Strip_Th20SetMode()
- // 5: Y cable mode()
- //Y Cable Mode
- char Command = '2'; //Set_ID = 1, Clr_ID = 2, Chk_ID = 3, Specify_ID = 4
- char ID = '0'; //range: 0x0~0xf
- char ID_set_status[16] = {0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0};
- char ID_set_status_chg = FALSE;
- char ID_chk_status[16] = {0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0};
- char ID_chk_status_chg = FALSE;
- extern int YCableMode_SetID_cnt;
- extern int YCableMode_ChkID_cnt;
- /*---------------------------------------------------------------------------------------------------------*/
- /* Define functions prototype */
- /*---------------------------------------------------------------------------------------------------------*/
- void TerminalShow_Init(void);
- void FeedbackMode_waitFeedback(void);
- void YCableMode_SetCommandID(void);
- void SYS_Init(void)
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
-
- /* Enable HIRC clock (Internal RC 48 MHz) */
- CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
- /* Wait for HIRC clock ready */
- CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
- /* Select HCLK clock source as HIRC and and HCLK clock divider as 1 */
- CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
-
- /* Set both PCLK0 and PCLK1 as HCLK */
- CLK->PCLKDIV = CLK_PCLKDIV_APB0DIV_DIV1 | CLK_PCLKDIV_APB1DIV_DIV1;
- /* Select IP clock source */
- /* Select UART0 clock source is HIRC */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
-
- /* Enable IP clock */
- CLK_EnableModuleClock(GPC_MODULE);
- CLK_EnableModuleClock(UART0_MODULE);
- CLK_EnableModuleClock(TMR0_MODULE);
-
- /* Select IP clock source */
- CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0);
-
- /* Update System Core Clock */
- /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
- SystemCoreClockUpdate();
-
- /*----------------------------------------------------------------------*/
- /* Init I/O Multi-function */
- /*----------------------------------------------------------------------*/
- /* Set PB multi-function pins for UART0 RXD and TXD */
- Uart0DefaultMPF();
-
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* ISR to handle GPIO(PC) interrupt event */
- /*---------------------------------------------------------------------------------------------------------*/
- void GPC_IRQHandler(void)
- {
- /* To check if PC.2 interrupt occurred */
- if (GPIO_GET_INT_FLAG(Dout_Port, Dout_PinMask))
- {
- GPIO_CLR_INT_FLAG(Dout_Port, Dout_PinMask);
-
- if(MODE == '3')
- {
- FeedbackMode_LEDcnt++;
- }
- else if((MODE == '5') && (Command == 1))
- {
- ID_set_status[YCableMode_SetID_cnt] = 1;
- ID_set_status_chg = TRUE;
- }
- else if((MODE == '5') && ((Command == 3) || (Command == 4)))
- {
- ID_chk_status[YCableMode_ChkID_cnt] = 1;
- ID_chk_status_chg = TRUE;
- }
- }
- else
- {
- uint32_t u32Status;
- u32Status = PC->INTSRC;
- /* Un-expected interrupt. Just clear all PC interrupts */
- PC->INTSRC = u32Status;
- }
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init UART0 */
- /*---------------------------------------------------------------------------------------------------------*/
- void UART0_Init(void)
- {
- /* Configure UART0 and set UART0 baud rate */
- UART_Open(UART0, 115200);
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* ISR to handle UART0 interrupt event */
- /*---------------------------------------------------------------------------------------------------------*/
- void UART0_IRQHandler(void)
- {
-
- uint32_t u32IntSts = UART0->INTSTS;
-
- g_i32Wait = FALSE;
-
- if ((u32IntSts & UART_INTSTS_RDAINT_Msk) || (u32IntSts & UART_INTSTS_RXTOINT_Msk))
- {
- /* Get all the input characters */
- while (UART_GET_RX_EMPTY(UART0) == 0)
- {
- /* Get the character from UART Buffer */
- uint8_t u8InChar = UART_READ(UART0);
- MODE = u8InChar;
- printf("%c\n\n", u8InChar);
-
- if(MODE == '5')
- {
- UART_DisableInt(UART0, UART_INTEN_RDAIEN_Msk);
-
- YCableMode_SetCommandID();
-
- UART_EnableInt(UART0, UART_INTEN_RDAIEN_Msk);
- }
- else
- {
- if((MODE == '0') || (MODE == '1') || (MODE == '2'))
- {
- GPIO_SetMode(Dout_Port, Dout_PinMask, GPIO_MODE_OUTPUT);
- }
-
- printf("+---------------------------------------+\n");
- printf(" Press the key to start test ARGB2\n");
- printf("+---------------------------------------+\n");
- printf(" 0: Run GRBLoop\n");
- printf(" 1: Run rainbowCycle\n");
- printf(" 2: Run theaterChaseRainbow\n");
- printf(" 3: Run Feedback mode\n");
- printf(" 4: Run TH20 SET mode\n");
- printf(" 5: Run Y cable mode\n");
- printf(" Others: mode pause\n");
- printf("+---------------------------------------+\n");
-
- if((MODE == '0') || (MODE == '1') || (MODE == '2') || (MODE == '3') || (MODE == '4') || (MODE == '5'))
- {
- printf("Start to run MODE: %c!\n\n", MODE);
-
- if(MODE == '3')
- {
- printf("Please check only one strip to test.\n");
- }
- else
- {
- printf("Press the key: ");
- }
- }
- else
- {
- printf("Please press key range from 0 to 5\n");
- printf("Press the key: ");
- }
- }
- }
-
- GPIO_SetMode(Dout_Port, Dout_PinMask, GPIO_MODE_OUTPUT);
-
- /*Forces a write of all user-space buffered data for the given output*/
- fflush(stdout);
- }
-
- if (UART0->FIFOSTS & (UART_FIFOSTS_BIF_Msk | UART_FIFOSTS_FEF_Msk | UART_FIFOSTS_PEF_Msk | UART_FIFOSTS_RXOVIF_Msk))
- {
- UART0->FIFOSTS = (UART_FIFOSTS_BIF_Msk | UART_FIFOSTS_FEF_Msk | UART_FIFOSTS_PEF_Msk | UART_FIFOSTS_RXOVIF_Msk);
- }
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init TIMER0 */
- /*---------------------------------------------------------------------------------------------------------*/
- void TIMER0_Init(void)
- {
- /* Configure and set TIMER0 */
- // Give a dummy target frequency here. Will over write prescale and compare value with macro
- TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 48000000);
- // Update prescale and compare value to what we need in event counter mode.
- TIMER_SET_PRESCALE_VALUE(TIMER0, 0);
- TIMER_SET_CMP_VALUE(TIMER0, 160);
-
- // Counter increase on rising edge
- TIMER_EnableEventCounter(TIMER0, TIMER_COUNTER_EVENT_RISING);
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* Main Function */
- /*---------------------------------------------------------------------------------------------------------*/
- int main(void)
- {
- /* Unlock protected registers */
- SYS_UnlockReg();
- /* Init System, peripheral clock and multi-function I/O */
- SYS_Init();
-
- /* Init UART0 for printf and test */
- UART0_Init();
- /* Lock protected registers */
- SYS_LockReg();
- /* Init ARGB2 Strip */
- Strip_Init();
-
- /* Init display of first show in terminal*/
- TerminalShow_Init();
-
- while(1)
- {
- switch(MODE)
- {
- case '0': //Run GRBLoop
- Strip_GRBLoop();
- break;
- case '1': //Run rainbowCycle
- Strip_rainbowCycle();
- break;
- case '2': //Run theaterChaseRainbow
- Strip_theaterChaseRainbow();
- break;
- case '3': //feedback mode
- Strip_FeedbackMode();
- FeedbackMode_waitFeedback();
- break;
- case '4': //TH20 SET mode
- Strip_Th20SetMode();
- break;
- case '5': //Y cable mode
- Strip_YCableMode();
- break;
- default:
- break;
- }
- }
-
- }
- /**
- * @brief Setting of first show in terminal after power on
- * @param None
- * @retval None
- */
- void TerminalShow_Init(void)
- {
- printf("+---------------------------------------+\n");
- printf(" Press the key to start test ARGB2\n");
- printf("+---------------------------------------+\n");
- printf(" 0: Run GRBLoop\n");
- printf(" 1: Run rainbowCycle\n");
- printf(" 2: Run theaterChaseRainbow\n");
- printf(" 3: Run Feedback mode\n");
- printf(" 4: Run TH20 SET mode\n");
- printf(" 5: Run Y cable mode\n");
- printf(" Others: mode pause\n");
- printf("+---------------------------------------+\n");
- printf(" Press the key: ");
-
- UART_SetTimeoutCnt(UART0, 0x10);
- UART0->FIFO &= ~ UART_FIFO_RFITL_Msk;
- UART0->FIFO |= UART_FIFO_RFITL_1BYTE;
-
- /* Enable UART RDA/THRE/Time-out interrupt */
- NVIC_EnableIRQ(UART0_IRQn);
- UART_EnableInt(UART0, UART_INTEN_RDAIEN_Msk);
-
- while (g_i32Wait);
-
- g_i32Wait = FALSE;
- }
- /**
- * @brief Wait pulse from ARGB2 that know LED's number in a strip
- * @param None
- * @retval None
- */
- void FeedbackMode_waitFeedback(void)
- {
- TIMER_Delay(TIMER0, 1000000);
-
- printf("There are %d pcs LED in the strip\n", FeedbackMode_LEDcnt);
-
- GPIO_DisableInt(Dout_Port, Dout_IRQn_Pin);
- FeedbackMode_LEDcnt = 0;
- printf("Press ENTER key to Continue...\n");
- scanf("%d",&ch);
- }
- /**
- * @brief Use to show information and set Command and ID about Y Cable mode
- * @param None
- * @retval None
- */
- void YCableMode_SetCommandID(void)
- {
- printf("Start the setting of Y Cable mode\n");
-
- printf("Please set Command, then press the ENTER.: (Set_ID = 1, Clr_ID = 2, Chk_ID = 3, Specify_ID = 4)\n");
- printf("Command:");
- scanf("%d",&ch);
- Command = ch;
- printf("%d\n\n", Command);
-
- printf("Please set ID, then press the ENTER.: (Range: 0x0~0xf)\n");
- printf("ID:");
- scanf("%d",&ch);
- ID = ch;
- printf("%d\n\n", ID);
- printf("You set the Command = %d, ID = %d\n\n", Command, ID);
-
- if(ID > 0xf)
- {
- printf("Setting ID is over range. (0x0~0xf)\n");
- printf("Leave Y Cable mode.\n\n");
-
- printf("Press any key to start test.\n");
-
- return;
- }
-
- if(Command == 1)
- {
- printf("Run Set_ID function. Setting number is all strip in parallel. ID start from %d to last .\n", ID);
- printf("You can check waveform from an oscilloscope now.\n");
- }
- else if(Command == 2)
- {
- if(ID == 0)
- {
- printf("Run Clr_ID function. Clear all strip's ID to 0.\n");
- }
- else if((ID > 0) && (ID <= 0xf))
- {
- printf("Run Clr_ID. Clear strip's ID to 0.\n");
- }
-
- printf("You can check waveform from an oscilloscope now.\n");
- }
- else if(Command == 3)
- {
- printf("Run Chk_ID function. Checking number is all strip in parallel. ID start from %d to last .\n", ID);
- printf("You can check waveform from an oscilloscope now.\n");
- }
- else if(Command == 4)
- {
- printf("Run Specify_ID function. Specifying number is all strip in parallel. ID start from %d to last one.\n", ID);
- printf("You can see all strip show the different light effect.\n");
- }
- else
- {
- printf("Set Command error!\n");
- printf("Leave Y Cable mode.\n\n");
- }
-
-
- printf("Press any key to run other mode.\n");
- }