struct Max31855Data readMax31855Data() { union { uint32_t dword; struct Max31855Data data; } u; GPIO_PinClear(p_cs); for (int i = 0; i < 32; i++) { u.dword <<= 1; GPIO_PinSet(p_clk); /* Set CLK to sample value */ WAIT_Waitus(1); if ( GPIO_Read(GPIOB) & (1l << (p_miso - 32)) ) u.dword |= 1; /* Read value from MISO */ GPIO_PinClear(p_clk); /* Set CLK to shift value */ } GPIO_PinSet(p_cs); return u.data; }
static void sensorUnplug4( void ) { #if ENABLE_UART_PRINTF UARTprintf ( "Sensor 4 Unplug Task\n" ); #endif // Wait a few milliseconds to debounce the port interrupt Delay_MS( SENSOR_UNPLUG_DELAY ); // If the sensor has been re-connected, abort the unplug routine. if( 1 == GPIO_Read( SensorPort4.int_port, SensorPort4.int_pin ) ) { GPIO_EnableInterrupts( SensorPort4.int_port, SensorPort4.int_pin ); } else { // Remove this sensor from the sensor task scheduler ActiveSensor[3] = -1; NumActiveSensors--; SensorSampRate[3] = 0; SampRateChanged = 1; #if ENABLE_TRANSCEIVER // Remove sensor id Transceiver_UnregisterSensor ( SensorPort4.port_number ); #endif // Change the interrupt to detect when a sensor is plugged into this port GPIO_SetInterruptTask( SensorPort4.int_port, SensorPort4.int_pin, GPIO_RISING_EDGE, 2, sensorDetect4 ); } // Kill this task vTaskDelete( NULL ); }
// *************** InitializeSensorPorts ************** // Initilizes all sensor ports. Enables the INT pin and initializes the rx and tx // fifos for each port. static void InitializeSensorPorts( void ) { unsigned long i; // Populate the array of sensor task function pointers SensorTask[0] = (int (*)(PORT_T *))&SensorTask1; SensorTask[1] = (int (*)(PORT_T *))&SensorTask2; SensorTask[2] = (int (*)(PORT_T *))&SensorTask3; SensorTask[3] = (int (*)(PORT_T *))&SensorTask4; for( i = 0; i < NUM_SENSOR_PORTS; i++ ) { // Enable the INT interrupt for each sensor port GPIO_Init( SensorPort[i]->int_port, SensorPort[i]->int_pin, GPIO_DIR_MODE_IN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPD ); GPIO_SetInterruptTask( SensorPort[i]->int_port, SensorPort[i]->int_pin, GPIO_RISING_EDGE, 2, SensorDetect[i] ); // Initialize and set high the ROM_CS pin high for each sensor port GPIO_Init( SensorPort[i]->rom_cs_port, SensorPort[i]->rom_cs_pin, GPIO_DIR_MODE_OUT, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD ); GPIO_Set( SensorPort[i]->rom_cs_port, SensorPort[i]->rom_cs_pin ); // Intialize the RX and TX queues (FIFOs) for each port SensorPort[i]->rx_fifo = (void *)xQueueCreate( SENSOR_RX_QUEUE_SIZE, sizeof( SENSOR_MSG_T ) ); SensorPort[i]->tx_fifo = (void *)xQueueCreate( SENSOR_TX_QUEUE_SIZE, sizeof( SENSOR_MSG_T ) ); } // If any sensors are already plugged in, trigger the detection routine. // No rising edge interrupt can happen because the pin is high before the // interrupt is enabled. if( 0 != GPIO_Read( SensorPort1.int_port, SensorPort1.int_pin ) ) sensorDetect1(); if( 0 != GPIO_Read( SensorPort2.int_port, SensorPort2.int_pin ) ) sensorDetect2(); if( 0 != GPIO_Read( SensorPort3.int_port, SensorPort3.int_pin ) ) sensorDetect3(); if( 0 != GPIO_Read( SensorPort4.int_port, SensorPort4.int_pin ) ) sensorDetect4(); }
pio_type platform_pio_op( unsigned port, pio_type pinmask, int op ) { GPIO_TypeDef* base = ( GPIO_TypeDef* )port_data[ port ]; GPIO_InitTypeDef data; pio_type retval = 1; GPIO_StructInit( &data ); switch( op ) { case PLATFORM_IO_PORT_SET_VALUE: GPIO_Write( base, ( u8 )pinmask ); break; case PLATFORM_IO_PIN_SET: GPIO_WriteBit( base, ( u8 )pinmask, Bit_SET ); break; case PLATFORM_IO_PIN_CLEAR: GPIO_WriteBit( base, ( u8 )pinmask, Bit_RESET ); break; case PLATFORM_IO_PORT_DIR_OUTPUT: pinmask = 0xFF; case PLATFORM_IO_PIN_DIR_OUTPUT: data.GPIO_Direction = GPIO_PinOutput; data.GPIO_Type = GPIO_Type_PushPull ; data.GPIO_Alternate=GPIO_OutputAlt1; data.GPIO_Pin = ( u8 )pinmask; GPIO_Init(base, &data); break; case PLATFORM_IO_PORT_DIR_INPUT: pinmask = 0xFF; case PLATFORM_IO_PIN_DIR_INPUT: data.GPIO_Pin = ( u8 )pinmask; GPIO_Init(base, &data); break; case PLATFORM_IO_PORT_GET_VALUE: retval = GPIO_Read( base ); break; case PLATFORM_IO_PIN_GET: retval = GPIO_ReadBit( base, ( u8 )pinmask ); break; default: retval = 0; break; } return retval; }
static void sensorInit4( void ) { unsigned short eeprom_addr = 0x1800; unsigned short driver_size = 0x700; unsigned char *node_addr = NULL; #if ENABLE_UART_PRINTF UARTprintf( "Sensor 4 Init\n" ); #endif // Wait a few milliseconds to debounce the port interrupt Delay_MS(SENSOR_INIT_DELAY); // If a sensor is no longer plugged in, abort the initialization. if( 0 == GPIO_Read( SensorPort4.int_port, SensorPort4.int_pin ) ) { GPIO_EnableInterrupts( SensorPort4.int_port, SensorPort4.int_pin ); } else { // Read in the sensor driver from this sensor's EEPROM node_addr = (unsigned char *)SensorDriver4Ptr; EEPROM_Open( SensorPort4.rom_cs_port, SensorPort4.rom_cs_pin ); EEPROM_ReadBurst( eeprom_addr, node_addr, driver_size ); EEPROM_Close( SensorPort4.rom_cs_port, SensorPort4.rom_cs_pin ); // Call the sensor_init routine SensorInit4( (PORT_T *)&SensorPort4 ); // Call the sensor_config routine SensorConfig4( (PORT_T *)&SensorPort4 ); #if ENABLE_TRANSCEIVER // Send sensor id to transceiver Transceiver_RegisterSensor ( SensorPort4.port_number, SensorPort4.sensor_id ); #endif taskENTER_CRITICAL(); SensorSampRate[3] = 1; SampRateChanged = 1; ActiveSensor[3] = 1; NumActiveSensors++; taskEXIT_CRITICAL(); GPIO_SetInterruptTask( SensorPort4.int_port, SensorPort4.int_pin, GPIO_FALLING_EDGE, 2, sensorDetectUnplug4 ); } // Kill this task vTaskDelete( NULL ); }
int main(void) { SIM_COPC = 0x00; //Deshabilito el watchdog CLK_init(); //Activo relojes de PORTA y PORTB, Core Clock = 47.972.352 Hz, Bus Clock = 23.986.176 Hz, desactivo WDOG GPIO_Init(PORT_A, 11, IO_MUX | PULL_UP | PULL_EN); //Selecciono MUX de IO para PTA11 GPIO_Init(PORT_A, 9, IO_MUX | PULL_UP | PULL_EN); //Selecciono MUX de IO para PTA9 GPIO_Init(PORT_A, 10, IO_MUX); //Selecciono MUX de IO para PTA10 GPIO_Init(PORT_A, 8, IO_MUX); //Selecciono MUX de IO para PTA8 GPIO_IO(PORT_A, 11, INPUT); //Configuro PTA11 como entrada GPIO_IO(PORT_A, 9, INPUT); //Configuro PTA9 como entrada GPIO_IO(PORT_A, 10, OUTPUT); //Configuro PTA10 como salida GPIO_IO(PORT_A, 8, OUTPUT); //Configuro PTA8 como salida for (;;) { estado = GPIO_Read(PORT_A, 11); if (!estado) { GPIO_Set(PORT_A, 10); } else if (estado) { GPIO_Clear(PORT_A, 10); } estado1 = GPIO_Read(PORT_A, 9); if (!estado1) { GPIO_Set(PORT_A, 8); } else if (estado1) { GPIO_Clear(PORT_A, 8); } } /* Never leave main */ return 0; }
readByte(char *byte, int lastbyte) { int idx; *byte = 0x0; for(idx=7 ; idx >= 0 ; --idx) { GPIO_Set(SDA); wait(); GPIO_Set(SCL); wait(); char cs = (GPIO_Read()&SDA)>>SDA_OFFSET; char myByte = ((GPIO_Read()&SDA)>>SDA_OFFSET)<<idx; *byte |= ((GPIO_Read()&SDA)>>SDA_OFFSET)<<idx; GPIO_Clr(SCL); } if(lastbyte == 1) GPIO_Set(SDA);//NACK else GPIO_Clr(SDA);//ACK giveClk(); }
unsigned int writeByte(char *byte) { int idx, count; count = 0; do{ for(idx=7 ; idx >= 0 ; idx--) { int c = ((*byte)>>idx)&0x1; if(c==1) GPIO_Set(SDA); else GPIO_Clr(SDA); giveClk(); } giveClk(); ++count; }while((count <= MAX_SEND) &&(GPIO_Read()&SDA == 0)); //Verifica se ainda é NACK, enquanto for tenta enviar os dados //if(GPIO_Read()&SDA != 0) return 1; //Faz read ao ACK que vem do dispositivo para garantir que recebeu bem os dados if(count > MAX_SEND) return -1; return 1; }
void Linesensor_Update(void){ ++_counter; switch(_counter){ case 1 : GPIO_Set(_OutputPins[0],1); GPIO_Set(_OutputPins[2],1); GPIO_Set(_OutputPins[6],1); GPIO_Set(_OutputPins[11],1); break; case 2 : GPIO_Set(_OutputPins[0],0); GPIO_Set(_OutputPins[2],0); GPIO_Set(_OutputPins[6],0); GPIO_Set(_OutputPins[11],0); break; case 3 : _Readings[0] = GPIO_Read(_InputPins[0]); _Readings[2] = GPIO_Read(_InputPins[2]); _Readings[6] = GPIO_Read(_InputPins[6]); _Readings[11] = GPIO_Read(_InputPins[11]); GPIO_Set(_OutputPins[3],1); GPIO_Set(_OutputPins[5],1); GPIO_Set(_OutputPins[8],1); GPIO_Set(_OutputPins[10],1); break; case 4 : GPIO_Set(_OutputPins[3],0); GPIO_Set(_OutputPins[5],0); GPIO_Set(_OutputPins[8],0); GPIO_Set(_OutputPins[10],0); break; case 5 : _Readings[3] = GPIO_Read(_InputPins[3]); _Readings[5] = GPIO_Read(_InputPins[5]); _Readings[8] = GPIO_Read(_InputPins[8]); _Readings[10] = GPIO_Read(_InputPins[10]); GPIO_Set(_OutputPins[1],1); GPIO_Set(_OutputPins[4],1); GPIO_Set(_OutputPins[7],1); GPIO_Set(_OutputPins[9],1); break; case 6 : GPIO_Set(_OutputPins[1],0); GPIO_Set(_OutputPins[4],0); GPIO_Set(_OutputPins[7],0); GPIO_Set(_OutputPins[9],0); break; case 7 : _Readings[1] = GPIO_Read(_InputPins[1]); _Readings[4] = GPIO_Read(_InputPins[4]); _Readings[7] = GPIO_Read(_InputPins[7]); _Readings[9] = GPIO_Read(_InputPins[9]); break; case 10 : _counter = 0; break; } return; }
// *************** Sensor_ReadGPIO *************** int Sensor_ReadGPIO( PORT_T *port, SENSOR_GPIO_T pin ) { return GPIO_Read( port->gpio_port[pin], port->gpio_pin[pin] ); }
/* Devolve 0 se o LED está apagado e um valor diferente de zero no caso contrário. */ int LED_GetState(){ return ledOutPut & GPIO_Read(); }