Martin Johnson / Colour_Video_Demo

Dependencies:   STM32F3-Discovery-minimal

Fork of Space_Invaders_Demo by Martin Johnson

Committer:
MartinJohnson
Date:
Wed May 16 02:39:14 2018 +0000
Revision:
3:93e488fbb8a2
Parent:
1:1b37c4b989b4
working colour video output

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MartinJohnson 1:1b37c4b989b4 1 #include "stm32f30x.h"
MartinJohnson 1:1b37c4b989b4 2 #include "stm32f3_discovery.h"
MartinJohnson 1:1b37c4b989b4 3 #include <stdio.h>
MartinJohnson 1:1b37c4b989b4 4 #include "stm32f3_discovery_lsm303dlhc.h"
MartinJohnson 1:1b37c4b989b4 5
MartinJohnson 3:93e488fbb8a2 6 #define GPIO_MODE_INPUT 0
MartinJohnson 3:93e488fbb8a2 7 #define GPIO_MODE_OUTPUT 1
MartinJohnson 3:93e488fbb8a2 8 #define GPIO_MODE_AF 2
MartinJohnson 1:1b37c4b989b4 9
MartinJohnson 3:93e488fbb8a2 10 #define GPIO_NO_PULL 0
MartinJohnson 3:93e488fbb8a2 11 #define GPIO_PULL_UP 1
MartinJohnson 3:93e488fbb8a2 12 #define GPIO_PULL_DOWN 2
MartinJohnson 1:1b37c4b989b4 13
MartinJohnson 3:93e488fbb8a2 14 #define GPIO_OUTPUT_PUSH_PULL 0
MartinJohnson 1:1b37c4b989b4 15
MartinJohnson 3:93e488fbb8a2 16 #define GPIO_SPEED_MEDIUM 1
MartinJohnson 3:93e488fbb8a2 17 #define GPIO_SPEED_LOW 0
MartinJohnson 3:93e488fbb8a2 18 #define GPIO_SPEED_HIGH 3
MartinJohnson 0:404dae88af71 19
MartinJohnson 3:93e488fbb8a2 20 extern void gpio_set_mode(GPIO_TypeDef *g,int n,int mode);
MartinJohnson 1:1b37c4b989b4 21
MartinJohnson 3:93e488fbb8a2 22 extern void gpio_set_af(GPIO_TypeDef *g,int n,int af, int otype, int pupd, int speed);
MartinJohnson 1:1b37c4b989b4 23
MartinJohnson 1:1b37c4b989b4 24
MartinJohnson 3:93e488fbb8a2 25 void i2cInit() {
MartinJohnson 3:93e488fbb8a2 26 RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
MartinJohnson 3:93e488fbb8a2 27 RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
MartinJohnson 3:93e488fbb8a2 28 // configure GPIOPB6 and 7
MartinJohnson 3:93e488fbb8a2 29 gpio_set_af(GPIOB,6,4,GPIO_OUTPUT_PUSH_PULL,GPIO_PULL_DOWN,GPIO_SPEED_LOW);
MartinJohnson 3:93e488fbb8a2 30 gpio_set_af(GPIOB,7,4,GPIO_OUTPUT_PUSH_PULL,GPIO_PULL_DOWN,GPIO_SPEED_LOW);
MartinJohnson 1:1b37c4b989b4 31
MartinJohnson 3:93e488fbb8a2 32 I2C1->TIMINGR = 0x00902025;
MartinJohnson 3:93e488fbb8a2 33 I2C1->CR1 |= I2C_CR1_PE;
MartinJohnson 1:1b37c4b989b4 34
MartinJohnson 1:1b37c4b989b4 35 }
MartinJohnson 1:1b37c4b989b4 36
MartinJohnson 3:93e488fbb8a2 37 #define I2C_CR2_CLEAR_MASK ~(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)
MartinJohnson 1:1b37c4b989b4 38
MartinJohnson 3:93e488fbb8a2 39 void i2cWrite(int address, int reg, int val) {
MartinJohnson 3:93e488fbb8a2 40 while(I2C1->ISR & I2C_ISR_BUSY);
MartinJohnson 3:93e488fbb8a2 41 I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_RELOAD | I2C_CR2_START | (1<<16) | address;
MartinJohnson 3:93e488fbb8a2 42 while(!(I2C1->ISR & I2C_ISR_TXIS));
MartinJohnson 3:93e488fbb8a2 43 I2C1->TXDR=reg;
MartinJohnson 3:93e488fbb8a2 44 while(!(I2C1->ISR & I2C_ISR_TCR));
MartinJohnson 3:93e488fbb8a2 45 I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_AUTOEND | (1<<16) | address;
MartinJohnson 3:93e488fbb8a2 46 while(!(I2C1->ISR & I2C_ISR_TXIS));
MartinJohnson 3:93e488fbb8a2 47 I2C1->TXDR=val;
MartinJohnson 3:93e488fbb8a2 48 while(!(I2C1->ISR & I2C_ISR_STOPF));
MartinJohnson 3:93e488fbb8a2 49 I2C1->ICR = I2C_ICR_STOPCF;
MartinJohnson 3:93e488fbb8a2 50 }
MartinJohnson 3:93e488fbb8a2 51
MartinJohnson 3:93e488fbb8a2 52 void i2cRead(int address, int reg, uint8_t *data, int nbytes) {
MartinJohnson 3:93e488fbb8a2 53 while(I2C1->ISR & I2C_ISR_BUSY);
MartinJohnson 3:93e488fbb8a2 54 I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_START | (1<<16) | address;
MartinJohnson 3:93e488fbb8a2 55 while(!(I2C1->ISR & I2C_ISR_TXIS));
MartinJohnson 3:93e488fbb8a2 56 if(nbytes>1) reg |= 0x80;
MartinJohnson 3:93e488fbb8a2 57 I2C1->TXDR=reg;
MartinJohnson 3:93e488fbb8a2 58 while(!(I2C1->ISR & I2C_ISR_TC));
MartinJohnson 3:93e488fbb8a2 59 I2C1->CR2 = (I2C1->CR2 & I2C_CR2_CLEAR_MASK) | I2C_CR2_AUTOEND | I2C_CR2_START | I2C_CR2_RD_WRN | (nbytes<<16) | address;
MartinJohnson 3:93e488fbb8a2 60 while(nbytes--) {
MartinJohnson 3:93e488fbb8a2 61 while(!(I2C1->ISR & I2C_ISR_RXNE));
MartinJohnson 3:93e488fbb8a2 62 *data++=I2C1->RXDR;
MartinJohnson 3:93e488fbb8a2 63 }
MartinJohnson 3:93e488fbb8a2 64 while(!(I2C1->ISR & I2C_ISR_STOPF));
MartinJohnson 3:93e488fbb8a2 65 I2C1->ICR = I2C_ICR_STOPCF;
MartinJohnson 3:93e488fbb8a2 66 }
MartinJohnson 3:93e488fbb8a2 67 void MemsConfig(void)
MartinJohnson 3:93e488fbb8a2 68 {
MartinJohnson 3:93e488fbb8a2 69 i2cInit();
MartinJohnson 3:93e488fbb8a2 70 i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG1_A,LSM303DLHC_NORMAL_MODE | LSM303DLHC_ODR_50_HZ | LSM303DLHC_AXES_ENABLE);
MartinJohnson 3:93e488fbb8a2 71 i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG4_A,LSM303DLHC_FULLSCALE_2G | LSM303DLHC_BlockUpdate_Continous | LSM303DLHC_BLE_LSB | LSM303DLHC_HR_ENABLE);
MartinJohnson 3:93e488fbb8a2 72 i2cWrite(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A,LSM303DLHC_HPM_NORMAL_MODE | LSM303DLHC_HPFCF_16 | LSM303DLHC_HPF_AOI1_DISABLE | LSM303DLHC_HPF_AOI2_DISABLE);
MartinJohnson 3:93e488fbb8a2 73
MartinJohnson 3:93e488fbb8a2 74 i2cWrite(MAG_I2C_ADDRESS, LSM303DLHC_CRA_REG_M, LSM303DLHC_TEMPSENSOR_ENABLE | LSM303DLHC_ODR_30_HZ);
MartinJohnson 3:93e488fbb8a2 75 i2cWrite(MAG_I2C_ADDRESS, LSM303DLHC_CRB_REG_M, LSM303DLHC_FS_8_1_GA);
MartinJohnson 3:93e488fbb8a2 76 i2cWrite(MAG_I2C_ADDRESS, LSM303DLHC_MR_REG_M,LSM303DLHC_CONTINUOS_CONVERSION);
MartinJohnson 3:93e488fbb8a2 77 }
MartinJohnson 3:93e488fbb8a2 78
MartinJohnson 3:93e488fbb8a2 79 void ReadAccelerometer(int16_t * data) {
MartinJohnson 3:93e488fbb8a2 80 i2cRead(ACC_I2C_ADDRESS, LSM303DLHC_OUT_X_L_A, (uint8_t *)data, 6);
MartinJohnson 3:93e488fbb8a2 81 }
MartinJohnson 3:93e488fbb8a2 82
MartinJohnson 3:93e488fbb8a2 83 void ReadMagnetometer(int16_t * data) {
MartinJohnson 3:93e488fbb8a2 84 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_X_L_M, (uint8_t *)data, 1);
MartinJohnson 3:93e488fbb8a2 85 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_X_H_M, (uint8_t *)data+1, 1);
MartinJohnson 3:93e488fbb8a2 86 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_Y_L_M, (uint8_t *)data+2, 1);
MartinJohnson 3:93e488fbb8a2 87 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_Y_H_M, (uint8_t *)data+3, 1);
MartinJohnson 3:93e488fbb8a2 88 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_Z_L_M, (uint8_t *)data+4, 1);
MartinJohnson 3:93e488fbb8a2 89 i2cRead(MAG_I2C_ADDRESS, LSM303DLHC_OUT_Z_H_M, (uint8_t *)data+5, 1);
MartinJohnson 3:93e488fbb8a2 90 }
MartinJohnson 3:93e488fbb8a2 91
MartinJohnson 3:93e488fbb8a2 92
MartinJohnson 3:93e488fbb8a2 93