void main(void) { gpio_set_function(pin, GPIO_FUNC_OUTPUT); while (1) { puts("hello, laptop\n\r"); delay_us(8 * DELAY); } }
void keyboard_init(void) { gpio_init(); // Ensure all interrupts are disabled. PUT32(RPI_INT_DISABLE_1, 0xFFFFFFFF); PUT32(RPI_INT_DISABLE_2, 0xFFFFFFFF); gpio_set_function(CLK, GPIO_FUNC_INPUT); gpio_set_pullup(CLK); gpio_detect_falling_edge(CLK); gpio_set_function(DATA, GPIO_FUNC_INPUT); gpio_set_pullup(DATA); // Enable GPIO interrupt 0 PUT32(RPI_INT_ENABLE_2, 0x00100000); // Enable interrupts system_enable_interrupts(); }
s32 os_main(u32 sp) { struct __os_task__ *ptask; int_init(); uart_init(); dram_init(); timer_init(); mmc_init(); PRINT_INFO("%s\n", sys_banner); coretimer_init(); task_init(); semaphore_init(); PRINT_INFO("cpu_mode: %s; lr: 0x%x; sp: 0x%x; cpsr: 0x%x\n", get_cpu_mode(NULL), __get_lr(), sp, __get_cpsr()); gpio_set_function(GPIO_16, OUTPUT); gpio_set_output(GPIO_16, 0); /* set_log_level(LOG_DEBUG); */ /* create idle task */ if ((ptask = tcb_alloc()) == NULL) { panic(); } tcb_init(ptask, idle_task, 0, 256); /*os_ready_insert(ptask);*/ current_task = &tcb[IDLE_TASK_ID]; /* assume that this is idle_task */ /* create main task */ if ((ptask = tcb_alloc()) == NULL) { panic(); } tcb_init(ptask, main_task, 0, 100); os_ready_insert(ptask); /* 'slip into idle task', cause the os_main() is not a task (it's the god code of system) */ __set_sp(&(task_stack[0][TASK_STK_SIZE])); current_task->state = TASK_RUNNING; idle_task(0); kassert(0); return 0; }
void uart_init(void) { int *aux = (int*)AUX_ENABLES;; *aux = AUX_ENABLE; // turn on mini-uart uart->ier = 0; uart->cntl = 0; uart->lcr = MINI_UART_LCR_8BIT; uart->mcr = 0; uart->ier = 0; uart->iir = MINI_UART_IIR_RX_FIFO_CLEAR | MINI_UART_IIR_RX_FIFO_ENABLE | MINI_UART_IIR_TX_FIFO_CLEAR | MINI_UART_IIR_TX_FIFO_ENABLE; uart->baud = 270; // baud rate ((250,000,000/115200)/8)-1 = 270 gpio_set_function(GPIO_TX, GPIO_FUNC_ALT5); gpio_set_function(GPIO_RX, GPIO_FUNC_ALT5); uart->cntl = MINI_UART_CNTL_TX_ENABLE | MINI_UART_CNTL_RX_ENABLE;; }
void uart_init(unsigned baud) { int *aux = (int*)AUX_ENABLES;; *aux = AUX_ENABLE; // turn on mini-uart uart->ier = 0; uart->cntl = 0; uart->lcr = MINI_UART_LCR_8BIT; uart->mcr = 0; uart->ier = 0; uart->iir = MINI_UART_IIR_RX_FIFO_CLEAR | MINI_UART_IIR_RX_FIFO_ENABLE | MINI_UART_IIR_TX_FIFO_CLEAR | MINI_UART_IIR_TX_FIFO_ENABLE; unsigned baud_val = ((250000000)/baud)/8 - 1; uart->baud = baud_val; // Should be 270 for 115200 gpio_set_function(GPIO_TX, GPIO_FUNC_ALT5); gpio_set_function(GPIO_RX, GPIO_FUNC_ALT5); uart->cntl = MINI_UART_CNTL_TX_ENABLE | MINI_UART_CNTL_RX_ENABLE;; }
int PX4FMU::gpio_ioctl(struct file *filp, int cmd, unsigned long arg) { int ret = OK; lock(); switch (cmd) { case GPIO_RESET: gpio_reset(); break; case GPIO_SENSOR_RAIL_RESET: sensor_reset(arg); break; case GPIO_PERIPHERAL_RAIL_RESET: peripheral_reset(arg); break; case GPIO_SET_OUTPUT: case GPIO_SET_INPUT: case GPIO_SET_ALT_1: gpio_set_function(arg, cmd); break; case GPIO_SET_ALT_2: case GPIO_SET_ALT_3: case GPIO_SET_ALT_4: ret = -EINVAL; break; case GPIO_SET: case GPIO_CLEAR: gpio_write(arg, cmd); break; case GPIO_GET: *(uint32_t *)arg = gpio_read(); break; default: ret = -ENOTTY; } unlock(); return ret; }
void hall_effect(void) { const unsigned vout= GPIO_PIN4; gpio_set_function(vout, GPIO_FUNC_INPUT); gpio_set_pullup(vout); for(int i = 0; i < 10; i++) { while(gpio_read(vout) == 1) {} printf("magnet close!\n"); while(gpio_read(vout) == 0) {} printf("magnet out of range!\n"); } //reboot(); }
void main(void) { uart_init(); const unsigned vout= GPIO_PIN2; // XXX how to setup pin? gpio_set_function(vout, GPIO_FUNC_INPUT); // gpio_set_pulldown(vout); for(int i = 0; i < 10; i++) { // XXX: how to tell when magnet close or far? while(gpio_read(vout) == 1) { } printf("magnet close\n"); while(gpio_read(vout) == 0) { } printf("magnet far\n"); } reboot(); }
unsigned int gpio_set_output(unsigned int pin) { return gpio_set_function(pin, GPIO_FUNC_OUTPUT); }
unsigned int gpio_set_input(unsigned int pin) { return gpio_set_function(pin, GPIO_FUNC_INPUT); }
/******************************************************************************* * Function Name : uart_open * Description : Open the UART port communication * Input : - Uart: Select the USART or the UART peripheral * : - BaudRate: Baud rate configuration * : - DmaBufSize: DMA buffer size * : - RxBufSize: Receive buffer size * : - TxBufSize: Transmit buffer size * : - HwCtrl: Hardware control options * : - Gpio: GPIO used for hardware control * Output : None * Return : 0 if OK, -1 in case of error *******************************************************************************/ int uart_open (const _Uart_Descriptor *Uart, u32 BaudRate, u8 DmaBufSize, u16 RxBufSize, u16 TxBufSize, u8 HwCtrl, const _Gpio_Descriptor *Gpio) { USART_InitTypeDef usart_init_structure; DMA_InitTypeDef dma_init_structure; NVIC_InitTypeDef NVIC_InitStructure; /* Init control variables and bufers */ if (*Uart->Ctrl) _sys_free(*Uart->Ctrl); if ((*Uart->Ctrl = _sys_malloc(sizeof(_Uart_Ctrl) + DmaBufSize + RxBufSize + TxBufSize)) == 0) return(-1); memset(*Uart->Ctrl, 0, sizeof(_Uart_Ctrl)); (*Uart->Ctrl)->DmaBufPtr = (char *)*Uart->Ctrl + sizeof(_Uart_Ctrl); (*Uart->Ctrl)->DmaBufSize = DmaBufSize; (*Uart->Ctrl)->iDma = DmaBufSize; (*Uart->Ctrl)->RxBufSize = RxBufSize; (*Uart->Ctrl)->RxBufPtr = (*Uart->Ctrl)->DmaBufPtr + (*Uart->Ctrl)->DmaBufSize; (*Uart->Ctrl)->TxBufSize = TxBufSize; (*Uart->Ctrl)->TxBufPtr = (*Uart->Ctrl)->RxBufPtr + (*Uart->Ctrl)->RxBufSize; (*Uart->Ctrl)->HwCtrl = HwCtrl; (*Uart->Ctrl)->Gpio = Gpio; #ifdef _UART_OS_SUPPORT (*Uart->Ctrl)->Event = SYS_EVT_INCOMING_DATA; (*Uart->Ctrl)->Task = sys_task_self(); #endif /* Enable peripheral clock */ (*Uart->RCC_APBxPeriphClockCmd)(Uart->RCC_APBxPeriph, ENABLE); /* Init GPIO */ gpio_set_function(Uart->TxGpio, Uart->GPIO_AF); gpio_set_function(Uart->RxGpio, Uart->GPIO_AF); gpio_set_mode(Uart->TxGpio, GPIO_MODE_AF, 0); gpio_set_mode(Uart->RxGpio, GPIO_MODE_AF, 0); if ((*Uart->Ctrl)->HwCtrl & (UART_HW_FLOW_CTRL_RX | UART_HALF_DUPLEX)) gpio_set_mode((*Uart->Ctrl)->Gpio, GPIO_FCT_OUT, 0); /* Init UART peripheral */ USART_DeInit(Uart->UARTx); USART_StructInit(&usart_init_structure); usart_init_structure.USART_BaudRate = BaudRate; if ((*Uart->Ctrl)->HwCtrl & UART_HW_FLOW_CTRL_TX) usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_CTS; USART_Init(Uart->UARTx, &usart_init_structure); /* Configure DMA (if used) */ if ((*Uart->Ctrl)->DmaBufSize) { DMA_DeInit(Uart->DMAy_Streamx); DMA_StructInit(&dma_init_structure); dma_init_structure.DMA_Channel = Uart->DMA_Channel; dma_init_structure.DMA_PeripheralBaseAddr = (u32)&Uart->UARTx->DR; dma_init_structure.DMA_Memory0BaseAddr = (u32)(*Uart->Ctrl)->DmaBufPtr; dma_init_structure.DMA_BufferSize = (*Uart->Ctrl)->DmaBufSize; dma_init_structure.DMA_MemoryInc = DMA_MemoryInc_Enable; dma_init_structure.DMA_Mode = DMA_Mode_Circular; dma_init_structure.DMA_Priority = DMA_Priority_Medium; dma_init_structure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_Init(Uart->DMAy_Streamx, &dma_init_structure); DMA_ITConfig(Uart->DMAy_Streamx, DMA_IT_TC | DMA_IT_HT, ENABLE); DMA_Cmd(Uart->DMAy_Streamx, ENABLE); NVIC_EnableIRQ(Uart->DMAx_IRQn); // //NVIC_SetPriority((IRQn_Type)Uart->DMAx_IRQn, (uint32_t)129); NVIC_SetPriority((IRQn_Type)Uart->DMAx_IRQn, (1 << __NVIC_PRIO_BITS) -3); // USART_DMACmd(Uart->UARTx, USART_DMAReq_Rx, ENABLE); USART_ITConfig(Uart->UARTx, USART_IT_IDLE, ENABLE); } else USART_ITConfig(Uart->UARTx, USART_IT_RXNE, ENABLE); /* Enable IT and start peripheral */ //NVIC_InitStructure.NVIC_IRQChannel = Uart->IRQn; // we want to configure the USART1 interrupts //NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART1 interrupts //NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group //NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // the USART1 interrupts are globally enabled //NVIC_Init(&NVIC_InitStructure); // //NVIC_SetPriority((IRQn_Type)Uart->IRQn, (uint32_t)129); NVIC_SetPriority((IRQn_Type)Uart->IRQn, (1 << __NVIC_PRIO_BITS) -4); // USART_ITConfig(Uart->UARTx, USART_IT_TC, ENABLE); NVIC_EnableIRQ(Uart->IRQn); USART_Cmd(Uart->UARTx, ENABLE); while (!gpio_read(Uart->TxGpio)); return(0); }
void led_init(void) { gpio_init(); gpio_set_function(ACT, GPIO_FUNC_OUTPUT); }