Example #1
0
void
SPI::read(void* buf, size_t count)
{
  if (UNLIKELY(count == 0)) return;
  uint8_t* dp = (uint8_t*) buf;
  transfer_start(0xff);
  while (--count) *dp++ = transfer_next(0xff);
  *dp = transfer_await();
}
Example #2
0
void
SPI::write_P(const void* buf, size_t count)
{
  if (UNLIKELY(count == 0)) return;
  const uint8_t* sp = (const uint8_t*) buf;
  uint8_t data = pgm_read_byte(sp++);
  transfer_start(data);
  while (--count) {
    data = pgm_read_byte(sp++);
    transfer_next(data);
  }
  transfer_await();
}
Example #3
0
void 
SPI::write(const void* buf, size_t count)
{
  if (count == 0) return;
  const uint8_t* sp = (const uint8_t*) buf;
  uint8_t data = *sp++;
  transfer_start(data);
  while (--count) {
    data = *sp++;
    transfer_next(data);
  }
  transfer_await();
}
Example #4
0
void
SPI::transfer(void* buf, size_t count)
{
  if (UNLIKELY(count == 0)) return;
  uint8_t* dp = (uint8_t*) buf;
  uint8_t data = *dp;
  transfer_start(data);
  while (--count) {
    uint8_t* tp = dp + 1;
    data = *tp;
    *dp = transfer_next(data);
    dp = tp;
  }
  *dp = transfer_await();
}
Example #5
0
void
SPI::transfer(void* dst, const void* src, size_t count)
{
  if (UNLIKELY(count == 0)) return;
  uint8_t* dp = (uint8_t*) dst;
  const uint8_t* sp = (const uint8_t*) src;
  uint8_t data = *sp++;
  transfer_start(data);
  while (--count) {
    uint8_t* tp = dp + 1;
    data = *sp++;
    *dp = transfer_next(data);
    dp = tp;
  }
  *dp = transfer_await();
}
Example #6
0
void data_received(unsigned char src_address, unsigned char dst_address, unsigned char command, char* data, unsigned char size)
{
	if (command == CLUNET_COMMAND_TIME && size >= 6) // Синхронизация времени
	{
		set_time(data[3]+1900, data[4], data[5], data[0],data[1],data[2]);
	}
	else if (command == CLUNET_COMMAND_INTERCOM_MODE_REQUEST) // У нас запрашивают режим
	{
		send_current_mode(src_address);
	}
	else if (command == CLUNET_COMMAND_INTERCOM_MODE_SET) // Установка режима
	{
		if (size >= 1 && (unsigned char)data[0] != 0xFF) mode_current = data[0];
		if (size >= 2 && (unsigned char)data[1] != 0xFF) mode_temp = data[1];
		save_mode();
	}	
	else if (command == CLUNET_COMMAND_INTERCOM_RECORD_REQUEST) // Передача записей
	{
		if (size == 4) transfer_start(*((unsigned long int*)data), src_address); // Начинаем передачу
		else if (size == 1 && data[0] == 1) transfer_ack(); // Подтвержают приём, переходим к следующему куску
		else if (size == 1 && data[0] == 0) transfer_stop(); // Прекращение передачи данных
	}
}
int gestic_message_write(gestic_t *gestic, void *msg, int size)
{
    I2C_MODULE port = gestic->io.I2cPort;
    const unsigned char * const buffer = (unsigned char*)msg;
    int result = 0;
    int i;

    GESTIC_ASSERT(buffer[0] == size);

    if(transfer_start(port, gestic->io.I2cSlaveAddr, 0))
        result = -1;

    for(i = 0; !result && i < size; ++i) {
        if(transmit_byte(port, buffer[i]) < 0)
            result = -1;
        
        if(!I2CByteWasAcknowledged(port))
            result = -1;
    }

    transfer_stop(port);

    return result;
}
Example #8
0
//
//  Main program loop.
//
int main(void)
{
	unsigned char memflags;
	unsigned char action;
	unsigned char stage;
	unsigned char timeidx;
	unsigned char i;

	time = 0;
	
	__disable_interrupt();
	InitialiseSystemClock();
	InitialiseUSART();
	InitialiseTIM2();
	spi_init();
	nrf_init();
	network_init(RF_ROLE_TX);
	__enable_interrupt();

	delay_ms(1000);
	
#ifdef DUMP
	nrf_setrx();
#endif
#ifdef CITY
	cities_init();
	timeidx = 0;
	memflags = 0;
	my_info.type = TYPE_CITY;
	my_info.team = NO_TEAM;
	my_info.id = MY_ID;
	my_info.units = CITY_UNITS;
	my_info.mode = MODE_DEFENSE;
	my_info.level = 0;

	pk_out.node_from = my_info.id;
	pk_out.type = my_info.type;
	for (i = 0;i<10;i++){
		pk_out.teams[i] = 0xFF;
		pk_out.times[i] = 0xFF;	
	}
#endif

#ifdef SOURCE
	my_info.type = TYPE_SOURCE;
	my_info.team = NO_TEAM;
	my_info.id = MY_ID;	

	pk_out.node_from = my_info.id;
	pk_out.type = my_info.type;
#endif

	// Configure pins
	PB_DDR = (1<<5);
	PB_CR1 = (1<<5);
//	PB_ODR ^= (1<<5);
	// Loop
	do {
	//	PB_ODR ^= (1<<5);

#ifdef DUMP
		nrf_listen();
		delay_ms(10);
		nrf_nolisten();
		if (nrf_receive(data,32)){
			nrf_listen();
			puts("Data:");	
			for (i=0;i<32;i++){
				putchex(data[i]);	
				putc(' ');
			}
			puts("\n");	
		}
#endif
#ifdef SOURCE
		pk_out.team = my_info.team;
		pk_out.units = SOURCE_UNITS;
		pk_out.action = ACTION_SOURCE;
		pk_out.node_to = 0;
		if (memflags & FL_SEC){
			PB_ODR ^= (1<<5);
			network_send(&pk_out,0);
		}
		delay_ms(100);
	
#endif
#ifdef CITY
		pk_out.team = my_info.team;
		if (memflags & FL_SEC){
			puts("Team: ");
			putchex(my_info.team);
			puts(" level: ");
			putchex(my_info.level);
			puts(" units: ");
			putchex(my_info.units);
			puts(" uplevel: ");
			putchex(my_info.uplevel);
			putchex(timeidx);
			putc('\n');
			cities_broadcast();		
			if (time%(10-CITY_MAXLEVEL) == 0){
				if (my_info.units < CITY_MAXUNITS){ 
					my_info.units++;
				} else if (my_info.uplevel < CITY_LEVEL_UP){
					my_info.uplevel++;
				} else {
					my_info.uplevel = 0;
					if (my_info.level < CITY_MAXLEVEL){
						my_info.level++;	
					}	
				}
			}
			if (time%60 == 0){
				pk_out.teams[timeidx]=my_info.team;
				pk_out.times[timeidx]=time/60;	
				timeidx++;
				timeidx %= 10;
			}
		} 
		network_arcv_stop();
		delay_us(100);
                while (network_arcv(&pk_in)){
			if (my_info.units==0){
				break;	
			}
			PB_ODR ^= (1<<5);
                        if (pk_in.action == ACTION_BROADCAST){
				if ((pk_in.team == my_info.team)&&(my_info.units >=1)){
					transfer_start(pk_in.node_from);	
				}
                        } else {
                                if (pk_in.node_to == my_info.id){
					cities_city_process_action();
					puts("Processing\n");
                                }
                        }
                }
 

		delay_us(100);
		network_arcv_start();	
		delay_ms(100);


#endif


			

	memflags = flags;
	flags = 0;		
	} while(1);
}
void* message_read(gestic_t *gestic, int *size)
{
    UINT8 RcvCount = 0;
    UINT8 writeIndex = 0;
    I2C_MODULE port = gestic->io.I2cPort;
    unsigned char *result = gestic->io.read_buffer;

    // Get TSLine
    if(gestic->io.device(GestICDev_TsLine_Get) == 1)
        result = NULL;

    if(result)
        gestic->io.device(GestICDev_TsLine_Assert);

    if(result && transfer_start(port, gestic->io.I2cSlaveAddr, 1))
        result = NULL;

    // Read the data from the desired address
    while(result) {
        UINT8 i2cbyte;

        /* Check for buffer overflow */
        if(I2CReceiverEnable(port, TRUE) == I2C_RECEIVE_OVERFLOW) {
            result = NULL;
            break;
        }

        while(!I2CReceivedDataIsAvailable(port));

        i2cbyte = I2CGetByte(gestic->io.I2cPort);
        result[writeIndex] = i2cbyte;
        writeIndex++;

        if(RcvCount == 0) {
            /* Set size if message fits into buffer */
            if(i2cbyte < sizeof(gestic->io.read_buffer))
                RcvCount = i2cbyte;
            else
                result = NULL;
        }

        /* In case of length == 0 or last byte (length == 1) send a NACK */
        if(RcvCount == 0 || RcvCount == 1)
            I2CAcknowledgeByte(port, FALSE);
        else
            I2CAcknowledgeByte(port, TRUE);

        while(!I2CAcknowledgeHasCompleted( gestic->io.I2cPort ));

        RcvCount--;
        if(RcvCount <= 0)
            break;
    }

    gestic->io.device(GestICDev_TsLine_Release);

    transfer_stop(gestic->io.I2cPort);
    
    if(result && size)
        *size = result[0];

    return result;
}