2014-09-12 16:01:33 +02:00
|
|
|
/*
|
2017-10-25 13:39:20 +02:00
|
|
|
* Licensed under the GNU General Public License version 2 with exceptions. See
|
|
|
|
* LICENSE file in the project root for full license information
|
2014-09-12 16:01:33 +02:00
|
|
|
*/
|
2015-05-13 16:44:24 +02:00
|
|
|
|
2014-09-12 16:01:33 +02:00
|
|
|
/** \file
|
|
|
|
* \brief
|
|
|
|
* ESC hardware layer functions.
|
|
|
|
*
|
2015-05-13 16:44:24 +02:00
|
|
|
* Function to read and write commands to the ESC. Used to read/write ESC
|
|
|
|
* registers and memory.
|
2014-09-12 16:01:33 +02:00
|
|
|
*/
|
2015-05-13 16:44:24 +02:00
|
|
|
|
2015-05-27 12:06:08 +02:00
|
|
|
#include "../../esc.h"
|
2014-09-12 16:01:33 +02:00
|
|
|
#include <spi/spi.h>
|
|
|
|
#include <string.h>
|
2015-05-20 13:15:49 +02:00
|
|
|
#include <gpio.h>
|
2014-09-12 16:01:33 +02:00
|
|
|
|
2019-01-17 17:38:46 +01:00
|
|
|
#define MAX_READ_SIZE 128
|
|
|
|
|
2014-09-12 16:01:33 +02:00
|
|
|
#define ESC_CMD_READ 0x02
|
|
|
|
#define ESC_CMD_READWS 0x03
|
|
|
|
#define ESC_CMD_WRITE 0x04
|
|
|
|
#define ESC_CMD_NOP 0x00
|
|
|
|
#define ESC_TERM 0xff
|
|
|
|
#define ESC_NEXT 0x00
|
|
|
|
|
|
|
|
static int et1100 = -1;
|
2019-01-17 17:38:46 +01:00
|
|
|
static uint8_t read_termination[MAX_READ_SIZE] = { 0 };
|
2014-09-12 16:01:33 +02:00
|
|
|
|
2015-05-20 13:15:49 +02:00
|
|
|
#define GPIO_ECAT_RESET 1 /* specific function to hold ESC reset on startup
|
|
|
|
* when emulating EEPROM
|
|
|
|
*/
|
|
|
|
|
2016-08-26 16:53:32 +02:00
|
|
|
static void esc_address (uint16_t address, uint8_t command)
|
2014-09-12 16:01:33 +02:00
|
|
|
{
|
|
|
|
/* Device is selected already.
|
|
|
|
* We use 2 bytes addressing.
|
|
|
|
*/
|
2015-05-21 10:41:13 +02:00
|
|
|
uint8_t data[2];
|
2014-09-12 16:01:33 +02:00
|
|
|
|
|
|
|
/* address 12:5 */
|
|
|
|
data[0] = (address >> 5);
|
|
|
|
/* address 4:0 and cmd 2:0 */
|
|
|
|
data[1] = ((address & 0x1F) << 3) | command;
|
|
|
|
|
|
|
|
/* Write (and read AL interrupt register) */
|
2016-08-26 16:53:32 +02:00
|
|
|
spi_bidirectionally_transfer (et1100, (uint8_t *) &ESCvar.ALevent,
|
|
|
|
data, sizeof (data));
|
|
|
|
ESCvar.ALevent = etohs (ESCvar.ALevent);
|
2014-09-12 16:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** ESC read function used by the Slave stack.
|
|
|
|
*
|
|
|
|
* @param[in] address = address of ESC register to read
|
|
|
|
* @param[out] buf = pointer to buffer to read in
|
|
|
|
* @param[in] len = number of bytes to read
|
|
|
|
*/
|
2016-08-26 17:34:51 +02:00
|
|
|
void ESC_read (uint16_t address, void *buf, uint16_t len)
|
2014-09-12 16:01:33 +02:00
|
|
|
{
|
2019-01-17 17:38:46 +01:00
|
|
|
ASSERT(len <= MAX_READ_SIZE);
|
2015-09-21 18:12:25 +02:00
|
|
|
|
2014-09-12 16:01:33 +02:00
|
|
|
/* Select device. */
|
|
|
|
spi_select (et1100);
|
|
|
|
|
|
|
|
/* Write address and command to device. */
|
2016-08-26 16:53:32 +02:00
|
|
|
esc_address (address, ESC_CMD_READ);
|
2014-09-12 16:01:33 +02:00
|
|
|
|
|
|
|
/* Here we want to read data and keep MOSI low (0x00) during
|
|
|
|
* all bytes except the last one where we want to pull it high (0xFF).
|
|
|
|
* Read (and write termination bytes).
|
|
|
|
*/
|
2015-09-21 18:12:25 +02:00
|
|
|
spi_bidirectionally_transfer (et1100, buf, read_termination +
|
2019-01-17 17:38:46 +01:00
|
|
|
(MAX_READ_SIZE - len), len);
|
2014-09-12 16:01:33 +02:00
|
|
|
|
|
|
|
/* Un-select device. */
|
|
|
|
spi_unselect (et1100);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ESC write function used by the Slave stack.
|
|
|
|
*
|
|
|
|
* @param[in] address = address of ESC register to write
|
|
|
|
* @param[out] buf = pointer to buffer to write from
|
|
|
|
* @param[in] len = number of bytes to write
|
|
|
|
*/
|
2016-08-26 17:34:51 +02:00
|
|
|
void ESC_write (uint16_t address, void *buf, uint16_t len)
|
2014-09-12 16:01:33 +02:00
|
|
|
{
|
|
|
|
/* Select device. */
|
|
|
|
spi_select (et1100);
|
|
|
|
/* Write address and command to device. */
|
2016-08-26 16:53:32 +02:00
|
|
|
esc_address (address, ESC_CMD_WRITE);
|
2014-09-12 16:01:33 +02:00
|
|
|
/* Write data. */
|
|
|
|
write (et1100, buf, len);
|
|
|
|
/* Un-select device. */
|
|
|
|
spi_unselect (et1100);
|
|
|
|
}
|
|
|
|
|
2015-05-20 13:15:49 +02:00
|
|
|
void ESC_reset (void)
|
|
|
|
{
|
|
|
|
volatile int timeout;
|
|
|
|
|
|
|
|
DPRINT("esc_reset_started\n");
|
|
|
|
|
|
|
|
gpio_set (GPIO_ECAT_RESET,0); /* pin =0 */
|
|
|
|
gpio_configure_pin (GPIO_ECAT_RESET,MUX_GPIO,IRQC_DISABLED,MODE_OUTPUT);
|
|
|
|
|
|
|
|
task_delay (1000);
|
|
|
|
|
|
|
|
gpio_configure_pin (GPIO_ECAT_RESET,MUX_GPIO,IRQC_DISABLED,MODE_INPUT);
|
|
|
|
while(timeout<10000000)
|
|
|
|
{
|
|
|
|
/* ECAT releases resetpin */
|
|
|
|
if(gpio_get (GPIO_ECAT_RESET)!=0)
|
|
|
|
{
|
|
|
|
break; // OK
|
|
|
|
}
|
|
|
|
timeout++;
|
|
|
|
task_delay (30);
|
|
|
|
}
|
|
|
|
DPRINT("esc_reset_ended\n");
|
|
|
|
}
|
|
|
|
|
2017-10-25 20:28:13 +02:00
|
|
|
void ESC_init (const esc_cfg_t * config)
|
2014-09-12 16:01:33 +02:00
|
|
|
{
|
2017-10-25 20:28:13 +02:00
|
|
|
const char * spi_name = (char *)config->user_arg;
|
2014-09-12 16:01:33 +02:00
|
|
|
et1100 = open (spi_name, O_RDWR, 0);
|
2019-01-17 17:38:46 +01:00
|
|
|
read_termination[MAX_READ_SIZE - 1] = 0xFF;
|
2014-09-12 16:01:33 +02:00
|
|
|
}
|