Subversion Repositories HomeAutomation

Rev

Rev 1910 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "sns_TCN75A.h"
  3.  
  4. unsigned char messageBuf[5];
  5.  
  6. #if sns_TCN75A_USEEEPROM==1
  7. #include "sns_TCN75A_eeprom.h"
  8. struct eeprom_sns_TCN75A EEMEM eeprom_sns_TCN75A =
  9. {
  10.     {
  11.         ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file.
  12.         0xAB,   // x
  13.         0x1234  // y
  14.     },
  15.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  16. };
  17. #endif
  18.  
  19. void sns_TCN75A_Init(void)
  20. {
  21. #if sns_TCN75A_USEEEPROM==1
  22.     if (EEDATA_OK)
  23.     {
  24.       ///TODO: Use stored data to set initial values for the module
  25.       blablaX = eeprom_read_byte(EEDATA.x);
  26.       blablaY = eeprom_read_word(EEDATA.y);
  27.     } else
  28.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  29.       eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC);
  30.       eeprom_write_word_crc(EEDATA.y, 0x1234, WITHOUT_CRC);
  31.       EEDATA_UPDATE_CRC;
  32.     }
  33. #endif  
  34.     ///TODO: Initialize hardware etc here
  35.  
  36.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  37.     // Pcint_SetCallbackPin(sns_TCN75A_PCINT, EXP_C , &sns_TCN75A_pcint_callback);
  38.  
  39.     TWI_Master_Initialise();
  40.     Timer_SetTimeout(sns_TCN75A_POLL_TIMER, sns_TCN75A_POLL_PERIOD*1000, TimerTypeFreeRunning, 0);
  41.  
  42.     messageBuf[0] = (sns_TCN75A_I2C_ADDRESS<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  43.     messageBuf[1] = TCN75A_REG_CONFIG;
  44.     messageBuf[2] = (TCN75A_CNF_RESOLUTION_12<<TCN75A_CNF_RESOLUTION_BIT);
  45.     /* write config register, 12bit resolution */
  46.     TWI_Start_Read_Write( messageBuf, 3 );
  47.     while ( TWI_Transceiver_Busy() );
  48.  
  49.     messageBuf[0] = (sns_TCN75A_I2C_ADDRESS<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT);
  50.     messageBuf[1] = TCN75A_REG_TEMPERATURE;
  51.     /* choose temperature register */
  52.     TWI_Start_Read_Write( messageBuf, 2 );
  53.    
  54.     while ( TWI_Transceiver_Busy() );
  55. }
  56.  
  57. void sns_TCN75A_Process(void)
  58. {
  59.     if (Timer_Expired(sns_TCN75A_POLL_TIMER)) {
  60.         /* Read temperature from I2C-device */
  61.         messageBuf[0] = (sns_TCN75A_I2C_ADDRESS<<TWI_ADR_BITS) | (TRUE<<TWI_READ_BIT);
  62.         /* initiate a read of temperature */
  63.         TWI_Start_Read_Write( messageBuf, 3 );
  64.        
  65.         /* wait for data and then fetch data to messageBuf */
  66.         if (TWI_Read_Data_From_Buffer(messageBuf, 3) == TRUE)
  67.         {
  68.             StdCan_Msg_t txMsg;
  69.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  70.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  71.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TCN75A;
  72.             txMsg.Header.ModuleId = sns_TCN75A_ID;
  73.             txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_TEMPERATURE_CELSIUS;
  74.             txMsg.Length = 3;
  75.             /* set up sensor id byte */
  76.             txMsg.Data[0] = 0;
  77.             /* set up sign bit */
  78.             txMsg.Data[1] = messageBuf[1]&0x80;
  79.             /* set up msb byte */
  80.             txMsg.Data[1] |= ((messageBuf[1]&0x7f) >>2 );
  81.             /* set up lsb byte */
  82.             txMsg.Data[2] = ((messageBuf[1]&0x03) <<6 );
  83.             txMsg.Data[2] |= (messageBuf[2] >>2);
  84.  
  85.             StdCan_Put(&txMsg);
  86.         }
  87.     }
  88. }
  89.  
  90. void sns_TCN75A_HandleMessage(StdCan_Msg_t *rxMsg)
  91. {
  92.     /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
  93.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  94.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_xyz && ///TODO: Change this to the actual module type
  95.         rxMsg->Header.ModuleId == sns_TCN75A_ID)
  96.     {
  97.         switch (rxMsg->Header.Command)
  98.         {
  99.         case CAN_CMD_MODULE_DUMMY:
  100.         ///TODO: Do something dummy
  101.         break;
  102.         }
  103.     }*/
  104. }
  105.  
  106. void sns_TCN75A_List(uint8_t ModuleSequenceNumber)
  107. {
  108.     StdCan_Msg_t txMsg;
  109.    
  110.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  111.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  112.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_TCN75A;
  113.     txMsg.Header.ModuleId = sns_TCN75A_ID;
  114.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  115.     txMsg.Length = 6;
  116.  
  117.     uint32_t HwId=BIOS_GetHwId();
  118.     txMsg.Data[0] = HwId&0xff;
  119.     txMsg.Data[1] = (HwId>>8)&0xff;
  120.     txMsg.Data[2] = (HwId>>16)&0xff;
  121.     txMsg.Data[3] = (HwId>>24)&0xff;
  122.    
  123.     txMsg.Data[4] = NUMBER_OF_MODULES;
  124.     txMsg.Data[5] = ModuleSequenceNumber;
  125.    
  126.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  127. }
  128.