view main/packages/avr/can/m1.h @ 220:74381e5dd204 main

Demo for the Adafruit LCD product number 1480.
author Bob Cook <bob@bobcookdev.com>
date Mon, 07 Jul 2014 18:42:15 -0700
parents 7374d1cd677f
children
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  avr/can/m1.h
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
//    
//  This file implements CAN support for the mega(16,32,64)m1 chip.
//
//  Copyright (C) 2012-2014 Bob Cook
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
// ----------------------------------------------------------------------------------------

#if !defined( BCDRL_AVR_CAN_M1_H )
#define BCDRL_AVR_CAN_M1_H

#include <stdint.h>

// ----------------------------------------------------------------------------------------
//  definitions from "project_defs.h"
//
//  enable M1 CAN support with:
//
//      + PRJ_M1CAN_ENABLE
//
//  specify the size of the interrupt-driven RX and TX buffers:
//
//      + PRJ_M1CAN_RX_BUFFER_SIZE (14 bytes each, default is 4)
//      + PRJ_M1CAN_TX_BUFFER_SIZE (14 bytes each, default is 4)
//
//  specify the CAN bus speed with one of:
//
//      + PRJ_M1CAN_CANBUS_20_KHZ
//      + PRJ_M1CAN_CANBUS_125_KHZ
//      + PRJ_M1CAN_CANBUS_250_KHZ
//      + PRJ_M1CAN_CANBUS_500_KHZ
//
//  enable the debug support with:
//
//      + PRJ_M1CAN_ENABLE_DEBUG
//
// ----------------------------------------------------------------------------------------

const uint8_t m1can_max_message_data = 0x08;

// ----------------------------------------------------------------------------------------
//  m1can_init
//
//      Initialize the CAN h/w according to the project_defs.h specification.
//
//      Returns true if the initialization was successful.
//

bool m1can_init();
    
// ----------------------------------------------------------------------------------------
//  m1can_reset
//
//      Reset the CAN h/w. Any pending messages in the transmit or recieve buffers are
//      lost permanently.
//
//      Returns true if the reset was successful.
//

bool m1can_reset();

// ----------------------------------------------------------------------------------------
//  m1can_read
//
//      Read a pending message from one of the receive buffers. Does not block if no
//      message is available.
//
//      Returns true if a message was successfully received.
//

bool m1can_read    
    (
        uint32_t* can_id,
        bool*     is_request,
        uint8_t*  data_ptr,
        uint8_t*  length
    );

// ----------------------------------------------------------------------------------------
//  m1can_send
//
//      Queue an outgoing message into the transmit buffer.
//
//      Returns true if the message was successfully queued/sent.
//

bool m1can_send( const uint32_t& can_id, const uint8_t* data_ptr, uint8_t length );

// ----------------------------------------------------------------------------------------
//  m1can_request
//
//      Queue an outgoing request into the transmit buffer.
//
//      Returns true if the request was successfully queued/sent.
//

bool m1can_request( const uint32_t& can_id );

// ----------------------------------------------------------------------------------------
//  m1can_status
//
//      Return the current status of the controller. May return more than one item.
//
//      Internal status is cleared after this call, such that the next call will return
//      status = ok until another issue occurs.
//

static const uint8_t m1can_status_ok       = 0x00;
static const uint8_t m1can_status_overflow = ( 1 << 0 );

uint8_t m1can_status();

// ----------------------------------------------------------------------------------------
//  m1can_debug_print_registers
//
//      Print the current state of the mega(16,32,64)m1 CAN h/w registers to stdout.
//
//      Only compiled if PRJ_M1CAN_ENABLE_DEBUG is defined.
//

void m1can_debug_print_registers();

#endif  // #if defined(BCDRL_AVR_CAN_M1_H)
// ----------------------------------------------------------------------------------------