code error solving and cleanup

pull/2/head
polymurph 3 years ago
parent 34ef9bf307
commit 124ad69802

@ -20,6 +20,8 @@
extern "C" {
#endif
#include "pin.h"
#include "spi.h"
#include <stdint.h>
#include "hardwareDescription.h"
@ -33,37 +35,113 @@ typedef enum{
LSB_FIRST
}spi_framef_t;
/*! \brief SPI cannel class
* This class cpntains the pin and spi channel number
* select.
* */
typedef struct{
pinNo_t pin; /*!< pin number */
spiCH_t spi; /*!< spi hardware channel number */
}spi_ch_t;
typedef uint8_t (*readReg_t) (uint8_t);
// generic code
/**
* \brief Set up SPI channel
* Set up a SPI channel by passig a hardware SPI channel and a chipselect pin.
* The chipselect pin will be set to high (chipselect is lowactive).
* \param *ch pointer to SPI channel
* \param spi_hw_ch SPI hardware channel
* \param chipslectPin designated pin for chipslect
*/
void spiSetupCH(spi_ch_t *ch, spiCH_t spi_hw_ch, pinNo_t chipselectPin);
/**
* \brief Read register
* Read one byte from a one register with one byte address.
* \param *spi_ch spi pointer to spi channel object
* \param reg_address register address
* \return register content
*/
uint8_t spiReadReg(spi_ch_t *spi_ch,
uint8_t reg_address);
/**
* \brief Read Block
* Read a block of data starting at a given start address.
* This function makes use of the auto register increment of the device to be read from.
* The address will be sent once and then data is read.
* \param *spi_ch pointer to spi cannel object
* \param start_address start address to the first register
* \param *buffer pointer to the buffer in which the read content is written into
* \param buf_len length of buffer
*/
void spiAutoReadBlock(spi_ch_t *spi_ch,
uint8_t start_address,
uint8_t* buffer,
uint8_t buf_len);
/**
* \brief Write register
* Write one byte to one register with one byte address.
* \param *spi_ch pointer to spi channel object
* \param reg_address register address
* \param data data byte to be written into register
*
*/
void spiWriteReg(spi_ch_t *spi_ch,
uint8_t reg_address,
uint8_t data);
/**
* \brief Write data block
* Write a block of data starting at a given start address.
* This function makes use of the auto register increment of the device to be written to. The
* address will be sent once an then data is written.
* \param *spi_ch pointer to spi channel object
* \param start_address start address of the first reister
* \param *data pointer to data to be written
* \param data_len length of data to be written
*/
void spiWriteBlock(spi_ch_t *spi_ch,
uint8_t start_address,
const uint8_t *data,
uint8_t data_len);
//implementation
/**
* @brief This is the spi hardware channel class
* @param spi_hw_ch SPI hardware channel
*/
void spi_init(spiCH_t spi_hw_ch);
void spiInit(spiCH_t spi_hw_ch);
/**
* @brief SPI hardware peripheral reset
* @param spi_hw_ch SPI hardware channel
*/
void spi_reset(spiCH_t spi_hw_ch);
void spiReset(spiCH_t spi_hw_ch);
/**
* @brief Enable spi hardware channel
* @param spi_hw_ch SPI hardware channel
*/
void spi_enable(spiCH_t spi_hw_ch);
void spiEnable(spiCH_t spi_hw_ch);
/**
* @brief set SPI clock polarity
* @param spi_hw_ch SPI hardware channel
* @param clkPol Clock polarity
*/
void spi_setPolarity(spiCH_t spi_hw_ch, spi_clkPol_t clkPol);
void spiSetPolarity(spiCH_t spi_hw_ch, spi_clkPol_t clkPol);
/**
* @brief Set SPI frame format
* @param spi_hw_ch SPI hardware cannel
* @param framef Frame format
*/
void spi_setFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef);
void spiSetFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef);
/**
* @brief Set Clock Prescaler
@ -72,7 +150,7 @@ void spi_setFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef);
* @param spi_hw_ch SPI hardware channel
* @param clkDiv
*/
void spi_setClockPrescaler(spiCH_t spi_hw_ch,uint32_t clkDiv);
void spiSetClockPrescaler(spiCH_t spi_hw_ch,uint32_t clkDiv);
/*!
* @brief Transmits and receives on byte of data
@ -80,7 +158,7 @@ void spi_setClockPrescaler(spiCH_t spi_hw_ch,uint32_t clkDiv);
* @param tx_data 'tx_data' The byte to be transmitted"
* @return The received data
*/
uint8_t spi_trx(spiCH_t spi_hw_ch, uint8_t tx_data);
uint8_t spiTrx(spiCH_t spi_hw_ch, uint8_t tx_data);
#ifdef __cplusplus

@ -1,92 +0,0 @@
#ifndef _SPI_CH_H
#define _SPI_CH_H
#ifdef __cplusplus
extern "C" {
#endif
#include "pin.h"
#include "spi.h"
#include <stdint.h>
// https://stackoverflow.com/questions/17052443/c-function-inside-struct
typedef uint8_t (*readReg_t) (uint8_t);
/*! \brief SPI cannel class
* This class cpntains the pin and spi channel number
* select.
* */
typedef struct{
pinNo_t pin; /*!< pin number */
spiCH_t spi; /*!< spi hardware channel number */
}spi_ch_t;
/**
* \brief Set up SPI channel
* Set up a SPI channel by passig a hardware SPI channel and a chipselect pin.
* The chipselect pin will be set to high (chipselect is lowactive).
* \param *ch pointer to SPI channel
* \param spi_hw_ch SPI hardware channel
* \param chipslectPin designated pin for chipslect
*/
void spiCH_setupCH(spi_ch_t *ch, spiCH_t spi_hw_ch, pinNo_t chipselectPin);
/**
* \brief Read register
* Read one byte from a one register with one byte address.
* \param *spi_ch spi pointer to spi channel object
* \param reg_address register address
* \return register content
*/
uint8_t spiCH_readReg(spi_ch_t *spi_ch,
uint8_t reg_address);
/**
* \brief Read Block
* Read a block of data starting at a given start address.
* This function makes use of the auto register increment of the device to be read from.
* The address will be sent once and then data is read.
* \param *spi_ch pointer to spi cannel object
* \param start_address start address to the first register
* \param *buffer pointer to the buffer in which the read content is written into
* \param buf_len length of buffer
*/
void spiCH_autoReadBlock(spi_ch_t *spi_ch,
uint8_t start_address,
uint8_t* buffer,
uint8_t buf_len);
/**
* \brief Write register
* Write one byte to one register with one byte address.
* \param *spi_ch pointer to spi channel object
* \param reg_address register address
* \param data data byte to be written into register
*
*/
void spiCH_writeReg(spi_ch_t *spi_ch,
uint8_t reg_address,
uint8_t data);
/**
* \brief Write data block
* Write a block of data starting at a given start address.
* This function makes use of the auto register increment of the device to be written to. The
* address will be sent once an then data is written.
* \param *spi_ch pointer to spi channel object
* \param start_address start address of the first reister
* \param *data pointer to data to be written
* \param data_len length of data to be written
*/
void spiCH_writeBlock(spi_ch_t *spi_ch,
uint8_t start_address,
const uint8_t *data,
uint8_t data_len);
#ifdef __cplusplus
}
#endif
#endif //_SPI_CH_H

@ -4,9 +4,9 @@
// https://controllerstech.com/spi-using-registers-in-stm32/
void spi_init(spiCH_t spi_hw_ch)
void spiInit(spiCH_t spi_hw_ch)
{
spi_reset(spi_hw_ch);
spiReset(spi_hw_ch);
// TODO implement bittwiddeling etc. for generic SPI init
RCC->APB2ENR |= (1<<12); // Enable SPI1 CLock
@ -36,7 +36,7 @@ void spi_init(spiCH_t spi_hw_ch)
SPI_BASE->CR2 = 0;
}
void spi_reset(spiCH_t spi_hw_ch)
void spiReset(spiCH_t spi_hw_ch)
{
if(spiBus_No[spi_hw_ch] == 1) {
RCC->APB1RSTR |= (1 << spiBus_Rst_bitPos[spi_hw_ch]);
@ -48,22 +48,22 @@ void spi_reset(spiCH_t spi_hw_ch)
RCC->APB2RSTR &= ~(1 << spiBus_Rst_bitPos[spi_hw_ch]);
}
void spi_enable(spiCH_t spi_hw_ch)
void spiEnable(spiCH_t spi_hw_ch)
{
//TODO void spi_enable(spiCH_t spi_hw_ch);
}
void spi_setPolarity(spiCH_t spi_hw_ch, spi_clkPol_t clkPol)
void spiSetPolarity(spiCH_t spi_hw_ch, spi_clkPol_t clkPol)
{
//TODO void spi_setPolarity(spi_clkPol_t clkPol);
}
void spi_set_setFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef)
void spiSet_setFrameFormat(spiCH_t spi_hw_ch, spi_framef_t framef)
{
// TODO void spi_set_setFrameFormat(spi_framef_t framef);
}
uint8_t spi_trx(spiCH_t spi_hw_ch, uint8_t tx_data)
uint8_t spiTrx(spiCH_t spi_hw_ch, uint8_t tx_data)
{
uint8_t data;
// example

@ -1,26 +1,26 @@
#include "spi_ch.h"
#include "spi.h"
// generic implementation of spi channel class
void spiCH_setupCH(spi_ch_t *ch, spiCH_t spi_hw_ch, pinNo_t chipslectPin)
void spiSetupCH(spi_ch_t *ch, spiCH_t spi_hw_ch, pinNo_t chipselectPin)
{
ch->pin = chipselectPin;
ch->spi = spi_hw_ch;
pinWrite(chipslectPin, 0);
pinWrite(chipselectPin, 0);
}
uint8_t spiCH_readReg(spi_ch_t *spi_ch,
uint8_t spiReadReg(spi_ch_t *spi_ch,
uint8_t reg_address) {
uitn8_t buf;
uint8_t buf;
// select target device
pinWrite(spi_ch->pin,0);
// send address of target register
spi_trx(spi_ch->spi, reg_address);
spiTrx(spi_ch->spi, reg_address);
// read from target register
buf = spi_trx(spi->spi,0x00);
buf = spiTrx(spi_ch->spi,0x00);
// release target device
pinWrite(spi_ch->pin,1);
@ -28,7 +28,7 @@ uint8_t spiCH_readReg(spi_ch_t *spi_ch,
return buf;
}
void spiCH_autoReadBlock(spi_ch_t *spi_ch,
void spiAutoReadBlock(spi_ch_t *spi_ch,
uint8_t start_address,
uint8_t* buffer,
uint8_t buf_len) {
@ -38,34 +38,34 @@ void spiCH_autoReadBlock(spi_ch_t *spi_ch,
pinWrite(spi_ch->pin,0);
// send address of starting register
spi_trx(spi_ch->spi, reg_address);
spiTrx(spi_ch->spi, start_address);
// read block from device
for(;i < buf_len;i++) {
buffer[i] = spi_trx(spi_ch->spi, 0x00);
buffer[i] = spiTrx(spi_ch->spi, 0x00);
}
// release target device
pinWrite(spi_ch->pin,1);
}
void spiCH_writeReg(spi_ch_t *spi_ch,
void spiWriteReg(spi_ch_t *spi_ch,
uint8_t reg_address,
uint8_t data) {
// select target device
pinWrite(spi_ch->pin,0);
// send address of target register
spi_trx(spi_ch->spi, reg_address);
spiTrx(spi_ch->spi, reg_address);
// write to target register
spi_trx(spi->spi, data);
spiTrx(spi_ch->spi, data);
// release target device
pinWrite(spi_ch->pin,1);
}
void spiCH_writeBlock(spi_ch_t *spi_ch,
void spiWriteBlock(spi_ch_t *spi_ch,
uint8_t start_address,
const uint8_t *data,
uint8_t data_len) {
@ -75,11 +75,11 @@ void spiCH_writeBlock(spi_ch_t *spi_ch,
pinWrite(spi_ch->pin,0);
// send address of starting register
spi_trx(spi_ch->spi, reg_address);
spiTrx(spi_ch->spi, start_address);
// read block from device
for(;i < buf_len;i++) {
spi_trx(spi_ch->spi, data[i]);
for(;i < data_len;i++) {
spiTrx(spi_ch->spi, data[i]);
}
// release target device

@ -5,7 +5,6 @@
#include "ascii.h"
#include "timer.h"
#include "spi.h"
#include "ked/csl/interfaces/spi_ch.h"
int main(int argc, char *argv[])
@ -62,9 +61,9 @@ int main(int argc, char *argv[])
pinSetAlternate(pinA6, 0); // SPI1_MISO
pinSetAlternate(pinA7, 0); // SPI1_MOSI
spi_init(SPI_CH_1);
spiInit(SPI_CH_1);
spiCH_setupCH(&spi_test_channel, SPI_CH_1, pinA4);
spiSetupCH(&spi_test_channel, SPI_CH_1, pinA4);
for(i = 0 ; i < 20 ; i++) {
delayMs(50);
@ -74,7 +73,7 @@ int main(int argc, char *argv[])
for(i = 10; i > 0; i--) {
spiCH_writeReg(&spi_test_channel,0xAE,0xAE);
spiWriteReg(&spi_test_channel,0xAE,0xAE);
//spi_trx(SPI_CH_1, 0xAE);
}

Loading…
Cancel
Save