Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #include <inttypes.h>
  2. //#include <stdio.h>
  3. #include <avr/io.h>
  4. #include <avr/interrupt.h>
  5. #include <drivers/can/avr_internal/avr_internal.h>
  6.  
  7. /* Hack to fix lack of PBx, PCx defines in /usr/lib/avr/include/avr/portpins.h */
  8. /* http://savannah.nongnu.org/bugs/?25930 http://www.mail-archive.com/avr-libc-dev@nongnu.org/msg03306.html */
  9. #include <drivers/mcu/portpins.h>
  10.  
  11. #include <vectors.h>
  12. #include <drivers/can/avr_internal/can_lib.h>
  13.  
  14. static Can_Message_t msgbuf;
  15.  
  16. Can_Return_t Can_Receive(Can_Message_t *msg);
  17. /*
  18. ISR(MCP_INT_VECTOR) {
  19.     // Get first available message from controller and pass it to
  20.     // application handler. If both RX buffers contain messages
  21.     // we will get another interrupt as soon as this one returns.
  22.     if (Can_Receive(&msgbuf) == CAN_OK) {
  23.         // Callbacks are run with global interrupts disabled but
  24.         // with controller flag cleared so another msg can be
  25.         // received while this one is processed.
  26. #if MCP_CAN_PROCESS_RENAMED == 1
  27.         // when running a gateway application which uses stdcan
  28.         Can_Process_USART(&msgbuf);
  29. #else
  30.         Can_Process(&msgbuf);
  31. #endif
  32.     }
  33. }
  34. */
  35. /*-----------------------------------------------------------------------------
  36.  * Public Functions
  37.  *---------------------------------------------------------------------------*/
  38.  
  39. /**
  40.  * Initializes the CAN interface. Edit can_cfg.h to choose bitrate.
  41.  *
  42.  * @return
  43.  *      CAN_OK if initialization was successful.
  44.  *      CAN_INIT_FAIL_SET_BITRATE if bitrate could not be set correctly.
  45.  *      CAN_INIT_FAIL_SET_MODE if the controller could not be set to normal operation mode.
  46.  *      CAN_INIT_FAIL in case of general error.
  47.  */
  48. Can_Return_t Can_Init(void) {
  49.     can_init(0);
  50.     return CAN_OK;
  51. }
  52.  
  53. /**
  54.  * Sends a CAN message immediately with the controller hardware. If the CAN
  55.  * controller is busy, the function will return CAN_FAIL.
  56.  *
  57.  * @param msg
  58.  *      Pointer to the CAN message storage buffer.
  59.  *
  60.  * @return
  61.  *      CAN_OK if the message was successfully sent to the controller.
  62.  *      CAN_FAIL if the controller is busy.
  63.  */
  64. Can_Return_t Can_Send(Can_Message_t *msg) {
  65.     #define DATA_BUFFER_SIZE 8 // Up to 8 bytes Max
  66.     uint8_t tx_remote_buffer[DATA_BUFFER_SIZE];
  67.     st_cmd_t tx_remote_msg; //
  68.     tx_remote_msg.pt_data = &tx_remote_buffer[0]; // Point Remote Tx MOb to first element of buffer
  69.     tx_remote_msg.status = 0; // clear status
  70.  
  71.     // Configure Remote Tx MOb
  72.     for(uint8_t i=0; i<DATA_BUFFER_SIZE; i++) {tx_remote_buffer[i]=msg->Data.bytes[i];} // set data
  73.     tx_remote_msg.id.ext = msg->Id; //
  74.     tx_remote_msg.ctrl.ide = msg->ExtendedFlag; // This message object sends extended (2.0A) CAN frames
  75.     tx_remote_msg.ctrl.rtr = msg->RemoteFlag; //
  76.     tx_remote_msg.dlc = msg->DataLength; // Number of data bytes (8 max)
  77.     tx_remote_msg.cmd = CMD_TX_DATA; //
  78.  
  79.     while(can_cmd(&tx_remote_msg) != CAN_CMD_ACCEPTED); // Wait for MOb to configure (Must re-configure MOb for every transaction) and send request
  80.  
  81.     while(can_get_status(&tx_remote_msg) == CAN_STATUS_NOT_COMPLETED); // Wait for Tx to complete
  82.  
  83.     return CAN_OK;
  84. }
  85.  
  86.  
  87. /**
  88.  * Receives a CAN message from the CAN controller hardware.
  89.  *
  90.  * @param msg
  91.  *      Pointer to the message storage buffer into which the message should be copied.
  92.  *
  93.  * @return
  94.  *      CAN_OK if a received message was successfully copied into the buffer.
  95.  *      CAN_NO_MSG_AVAILABLE if there is no message available in the controller.
  96.  */
  97. Can_Return_t Can_Receive(Can_Message_t *msg) {
  98.  
  99.  
  100.     return CAN_OK;
  101. }
  102.